Got rid of the rif (window-based redisplay interface) global variable.
[emacs.git] / src / xdisp.c
blobdcddeeb9037f3e995cb43d2f5651a1aa969cd594
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "systty.h" /* For emacs_tty in termchar.h */
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
203 Cursor No_Cursor;
204 #endif
206 #ifndef FRAME_X_OUTPUT
207 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
208 #endif
210 #define INFINITY 10000000
212 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
213 || defined (USE_GTK)
214 extern void set_frame_menubar P_ ((struct frame *f, int, int));
215 extern int pending_menu_activation;
216 #endif
218 extern int interrupt_input;
219 extern int command_loop_level;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Margin around tool bar buttons in pixels. */
270 Lisp_Object Vtool_bar_button_margin;
272 /* Thickness of shadow to draw around tool bar buttons. */
274 EMACS_INT tool_bar_button_relief;
276 /* Non-zero means automatically resize tool-bars so that all tool-bar
277 items are visible, and no blank lines remain. */
279 int auto_resize_tool_bars_p;
281 /* Non-zero means draw block and hollow cursor as wide as the glyph
282 under it. For example, if a block cursor is over a tab, it will be
283 drawn as wide as that tab on the display. */
285 int x_stretch_cursor_p;
287 /* Non-nil means don't actually do any redisplay. */
289 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
291 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
293 int inhibit_eval_during_redisplay;
295 /* Names of text properties relevant for redisplay. */
297 Lisp_Object Qdisplay;
298 extern Lisp_Object Qface, Qinvisible, Qwidth;
300 /* Symbols used in text property values. */
302 Lisp_Object Vdisplay_pixels_per_inch;
303 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
304 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
305 Lisp_Object Qmargin, Qpointer;
306 extern Lisp_Object Qheight;
307 extern Lisp_Object QCwidth, QCheight, QCascent;
308 extern Lisp_Object Qscroll_bar;
310 /* Non-nil means highlight trailing whitespace. */
312 Lisp_Object Vshow_trailing_whitespace;
314 /* Non-nil means show the text cursor in void text areas
315 i.e. in blank areas after eol and eob. This used to be
316 the default in 21.3. */
318 Lisp_Object Vvoid_text_area_pointer;
320 /* Name of the face used to highlight trailing whitespace. */
322 Lisp_Object Qtrailing_whitespace;
324 /* The symbol `image' which is the car of the lists used to represent
325 images in Lisp. */
327 Lisp_Object Qimage;
329 /* The image map types. */
330 Lisp_Object QCmap, QCpointer;
331 Lisp_Object Qrect, Qcircle, Qpoly;
333 /* Non-zero means print newline to stdout before next mini-buffer
334 message. */
336 int noninteractive_need_newline;
338 /* Non-zero means print newline to message log before next message. */
340 static int message_log_need_newline;
342 /* Three markers that message_dolog uses.
343 It could allocate them itself, but that causes trouble
344 in handling memory-full errors. */
345 static Lisp_Object message_dolog_marker1;
346 static Lisp_Object message_dolog_marker2;
347 static Lisp_Object message_dolog_marker3;
349 /* The buffer position of the first character appearing entirely or
350 partially on the line of the selected window which contains the
351 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
352 redisplay optimization in redisplay_internal. */
354 static struct text_pos this_line_start_pos;
356 /* Number of characters past the end of the line above, including the
357 terminating newline. */
359 static struct text_pos this_line_end_pos;
361 /* The vertical positions and the height of this line. */
363 static int this_line_vpos;
364 static int this_line_y;
365 static int this_line_pixel_height;
367 /* X position at which this display line starts. Usually zero;
368 negative if first character is partially visible. */
370 static int this_line_start_x;
372 /* Buffer that this_line_.* variables are referring to. */
374 static struct buffer *this_line_buffer;
376 /* Nonzero means truncate lines in all windows less wide than the
377 frame. */
379 int truncate_partial_width_windows;
381 /* A flag to control how to display unibyte 8-bit character. */
383 int unibyte_display_via_language_environment;
385 /* Nonzero means we have more than one non-mini-buffer-only frame.
386 Not guaranteed to be accurate except while parsing
387 frame-title-format. */
389 int multiple_frames;
391 Lisp_Object Vglobal_mode_string;
393 /* Marker for where to display an arrow on top of the buffer text. */
395 Lisp_Object Voverlay_arrow_position;
397 /* String to display for the arrow. Only used on terminal frames. */
399 Lisp_Object Voverlay_arrow_string;
401 /* Values of those variables at last redisplay. However, if
402 Voverlay_arrow_position is a marker, last_arrow_position is its
403 numerical position. */
405 static Lisp_Object last_arrow_position, last_arrow_string;
407 /* Like mode-line-format, but for the title bar on a visible frame. */
409 Lisp_Object Vframe_title_format;
411 /* Like mode-line-format, but for the title bar on an iconified frame. */
413 Lisp_Object Vicon_title_format;
415 /* List of functions to call when a window's size changes. These
416 functions get one arg, a frame on which one or more windows' sizes
417 have changed. */
419 static Lisp_Object Vwindow_size_change_functions;
421 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
423 /* Nonzero if overlay arrow has been displayed once in this window. */
425 static int overlay_arrow_seen;
427 /* Nonzero means highlight the region even in nonselected windows. */
429 int highlight_nonselected_windows;
431 /* If cursor motion alone moves point off frame, try scrolling this
432 many lines up or down if that will bring it back. */
434 static EMACS_INT scroll_step;
436 /* Nonzero means scroll just far enough to bring point back on the
437 screen, when appropriate. */
439 static EMACS_INT scroll_conservatively;
441 /* Recenter the window whenever point gets within this many lines of
442 the top or bottom of the window. This value is translated into a
443 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
444 that there is really a fixed pixel height scroll margin. */
446 EMACS_INT scroll_margin;
448 /* Number of windows showing the buffer of the selected window (or
449 another buffer with the same base buffer). keyboard.c refers to
450 this. */
452 int buffer_shared;
454 /* Vector containing glyphs for an ellipsis `...'. */
456 static Lisp_Object default_invis_vector[3];
458 /* Zero means display the mode-line/header-line/menu-bar in the default face
459 (this slightly odd definition is for compatibility with previous versions
460 of emacs), non-zero means display them using their respective faces.
462 This variable is deprecated. */
464 int mode_line_inverse_video;
466 /* Prompt to display in front of the mini-buffer contents. */
468 Lisp_Object minibuf_prompt;
470 /* Width of current mini-buffer prompt. Only set after display_line
471 of the line that contains the prompt. */
473 int minibuf_prompt_width;
475 /* This is the window where the echo area message was displayed. It
476 is always a mini-buffer window, but it may not be the same window
477 currently active as a mini-buffer. */
479 Lisp_Object echo_area_window;
481 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
482 pushes the current message and the value of
483 message_enable_multibyte on the stack, the function restore_message
484 pops the stack and displays MESSAGE again. */
486 Lisp_Object Vmessage_stack;
488 /* Nonzero means multibyte characters were enabled when the echo area
489 message was specified. */
491 int message_enable_multibyte;
493 /* Nonzero if we should redraw the mode lines on the next redisplay. */
495 int update_mode_lines;
497 /* Nonzero if window sizes or contents have changed since last
498 redisplay that finished. */
500 int windows_or_buffers_changed;
502 /* Nonzero means a frame's cursor type has been changed. */
504 int cursor_type_changed;
506 /* Nonzero after display_mode_line if %l was used and it displayed a
507 line number. */
509 int line_number_displayed;
511 /* Maximum buffer size for which to display line numbers. */
513 Lisp_Object Vline_number_display_limit;
515 /* Line width to consider when repositioning for line number display. */
517 static EMACS_INT line_number_display_limit_width;
519 /* Number of lines to keep in the message log buffer. t means
520 infinite. nil means don't log at all. */
522 Lisp_Object Vmessage_log_max;
524 /* The name of the *Messages* buffer, a string. */
526 static Lisp_Object Vmessages_buffer_name;
528 /* Current, index 0, and last displayed echo area message. Either
529 buffers from echo_buffers, or nil to indicate no message. */
531 Lisp_Object echo_area_buffer[2];
533 /* The buffers referenced from echo_area_buffer. */
535 static Lisp_Object echo_buffer[2];
537 /* A vector saved used in with_area_buffer to reduce consing. */
539 static Lisp_Object Vwith_echo_area_save_vector;
541 /* Non-zero means display_echo_area should display the last echo area
542 message again. Set by redisplay_preserve_echo_area. */
544 static int display_last_displayed_message_p;
546 /* Nonzero if echo area is being used by print; zero if being used by
547 message. */
549 int message_buf_print;
551 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
553 Lisp_Object Qinhibit_menubar_update;
554 int inhibit_menubar_update;
556 /* Maximum height for resizing mini-windows. Either a float
557 specifying a fraction of the available height, or an integer
558 specifying a number of lines. */
560 Lisp_Object Vmax_mini_window_height;
562 /* Non-zero means messages should be displayed with truncated
563 lines instead of being continued. */
565 int message_truncate_lines;
566 Lisp_Object Qmessage_truncate_lines;
568 /* Set to 1 in clear_message to make redisplay_internal aware
569 of an emptied echo area. */
571 static int message_cleared_p;
573 /* Non-zero means we want a hollow cursor in windows that are not
574 selected. Zero means there's no cursor in such windows. */
576 Lisp_Object Vcursor_in_non_selected_windows;
577 Lisp_Object Qcursor_in_non_selected_windows;
579 /* How to blink the default frame cursor off. */
580 Lisp_Object Vblink_cursor_alist;
582 /* A scratch glyph row with contents used for generating truncation
583 glyphs. Also used in direct_output_for_insert. */
585 #define MAX_SCRATCH_GLYPHS 100
586 struct glyph_row scratch_glyph_row;
587 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
589 /* Ascent and height of the last line processed by move_it_to. */
591 static int last_max_ascent, last_height;
593 /* Non-zero if there's a help-echo in the echo area. */
595 int help_echo_showing_p;
597 /* If >= 0, computed, exact values of mode-line and header-line height
598 to use in the macros CURRENT_MODE_LINE_HEIGHT and
599 CURRENT_HEADER_LINE_HEIGHT. */
601 int current_mode_line_height, current_header_line_height;
603 /* The maximum distance to look ahead for text properties. Values
604 that are too small let us call compute_char_face and similar
605 functions too often which is expensive. Values that are too large
606 let us call compute_char_face and alike too often because we
607 might not be interested in text properties that far away. */
609 #define TEXT_PROP_DISTANCE_LIMIT 100
611 #if GLYPH_DEBUG
613 /* Variables to turn off display optimizations from Lisp. */
615 int inhibit_try_window_id, inhibit_try_window_reusing;
616 int inhibit_try_cursor_movement;
618 /* Non-zero means print traces of redisplay if compiled with
619 GLYPH_DEBUG != 0. */
621 int trace_redisplay_p;
623 #endif /* GLYPH_DEBUG */
625 #ifdef DEBUG_TRACE_MOVE
626 /* Non-zero means trace with TRACE_MOVE to stderr. */
627 int trace_move;
629 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
630 #else
631 #define TRACE_MOVE(x) (void) 0
632 #endif
634 /* Non-zero means automatically scroll windows horizontally to make
635 point visible. */
637 int automatic_hscrolling_p;
639 /* How close to the margin can point get before the window is scrolled
640 horizontally. */
641 EMACS_INT hscroll_margin;
643 /* How much to scroll horizontally when point is inside the above margin. */
644 Lisp_Object Vhscroll_step;
646 /* A list of symbols, one for each supported image type. */
648 Lisp_Object Vimage_types;
650 /* The variable `resize-mini-windows'. If nil, don't resize
651 mini-windows. If t, always resize them to fit the text they
652 display. If `grow-only', let mini-windows grow only until they
653 become empty. */
655 Lisp_Object Vresize_mini_windows;
657 /* Buffer being redisplayed -- for redisplay_window_error. */
659 struct buffer *displayed_buffer;
661 /* Value returned from text property handlers (see below). */
663 enum prop_handled
665 HANDLED_NORMALLY,
666 HANDLED_RECOMPUTE_PROPS,
667 HANDLED_OVERLAY_STRING_CONSUMED,
668 HANDLED_RETURN
671 /* A description of text properties that redisplay is interested
672 in. */
674 struct props
676 /* The name of the property. */
677 Lisp_Object *name;
679 /* A unique index for the property. */
680 enum prop_idx idx;
682 /* A handler function called to set up iterator IT from the property
683 at IT's current position. Value is used to steer handle_stop. */
684 enum prop_handled (*handler) P_ ((struct it *it));
687 static enum prop_handled handle_face_prop P_ ((struct it *));
688 static enum prop_handled handle_invisible_prop P_ ((struct it *));
689 static enum prop_handled handle_display_prop P_ ((struct it *));
690 static enum prop_handled handle_composition_prop P_ ((struct it *));
691 static enum prop_handled handle_overlay_change P_ ((struct it *));
692 static enum prop_handled handle_fontified_prop P_ ((struct it *));
694 /* Properties handled by iterators. */
696 static struct props it_props[] =
698 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
699 /* Handle `face' before `display' because some sub-properties of
700 `display' need to know the face. */
701 {&Qface, FACE_PROP_IDX, handle_face_prop},
702 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
703 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
704 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
705 {NULL, 0, NULL}
708 /* Value is the position described by X. If X is a marker, value is
709 the marker_position of X. Otherwise, value is X. */
711 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
713 /* Enumeration returned by some move_it_.* functions internally. */
715 enum move_it_result
717 /* Not used. Undefined value. */
718 MOVE_UNDEFINED,
720 /* Move ended at the requested buffer position or ZV. */
721 MOVE_POS_MATCH_OR_ZV,
723 /* Move ended at the requested X pixel position. */
724 MOVE_X_REACHED,
726 /* Move within a line ended at the end of a line that must be
727 continued. */
728 MOVE_LINE_CONTINUED,
730 /* Move within a line ended at the end of a line that would
731 be displayed truncated. */
732 MOVE_LINE_TRUNCATED,
734 /* Move within a line ended at a line end. */
735 MOVE_NEWLINE_OR_CR
738 /* This counter is used to clear the face cache every once in a while
739 in redisplay_internal. It is incremented for each redisplay.
740 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
741 cleared. */
743 #define CLEAR_FACE_CACHE_COUNT 500
744 static int clear_face_cache_count;
746 /* Non-zero while redisplay_internal is in progress. */
748 int redisplaying_p;
750 /* Non-zero means don't free realized faces. Bound while freeing
751 realized faces is dangerous because glyph matrices might still
752 reference them. */
754 int inhibit_free_realized_faces;
755 Lisp_Object Qinhibit_free_realized_faces;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 int help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
771 /* Function prototypes. */
773 static void setup_for_ellipsis P_ ((struct it *));
774 static void mark_window_display_accurate_1 P_ ((struct window *, int));
775 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
776 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
777 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
778 static int redisplay_mode_lines P_ ((Lisp_Object, int));
779 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
781 #if 0
782 static int invisible_text_between_p P_ ((struct it *, int, int));
783 #endif
785 static int next_element_from_ellipsis P_ ((struct it *));
786 static void pint2str P_ ((char *, int, int));
787 static void pint2hrstr P_ ((char *, int, int));
788 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
789 struct text_pos));
790 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
791 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
792 static void store_frame_title_char P_ ((char));
793 static int store_frame_title P_ ((const unsigned char *, int, int));
794 static void x_consider_frame_title P_ ((Lisp_Object));
795 static void handle_stop P_ ((struct it *));
796 static int tool_bar_lines_needed P_ ((struct frame *));
797 static int single_display_prop_intangible_p P_ ((Lisp_Object));
798 static void ensure_echo_area_buffers P_ ((void));
799 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
800 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
801 static int with_echo_area_buffer P_ ((struct window *, int,
802 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
803 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
804 static void clear_garbaged_frames P_ ((void));
805 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
806 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
807 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
808 static int display_echo_area P_ ((struct window *));
809 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
810 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
811 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
812 static int string_char_and_length P_ ((const unsigned char *, int, int *));
813 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
814 struct text_pos));
815 static int compute_window_start_on_continuation_line P_ ((struct window *));
816 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
817 static void insert_left_trunc_glyphs P_ ((struct it *));
818 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
819 static void extend_face_to_end_of_line P_ ((struct it *));
820 static int append_space P_ ((struct it *, int));
821 static int make_cursor_line_fully_visible P_ ((struct window *));
822 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
823 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
824 static int trailing_whitespace_p P_ ((int));
825 static int message_log_check_duplicate P_ ((int, int, int, int));
826 static void push_it P_ ((struct it *));
827 static void pop_it P_ ((struct it *));
828 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
829 static void select_frame_for_redisplay P_ ((Lisp_Object));
830 static void redisplay_internal P_ ((int));
831 static int echo_area_display P_ ((int));
832 static void redisplay_windows P_ ((Lisp_Object));
833 static void redisplay_window P_ ((Lisp_Object, int));
834 static Lisp_Object redisplay_window_error ();
835 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
836 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
837 static void update_menu_bar P_ ((struct frame *, int));
838 static int try_window_reusing_current_matrix P_ ((struct window *));
839 static int try_window_id P_ ((struct window *));
840 static int display_line P_ ((struct it *));
841 static int display_mode_lines P_ ((struct window *));
842 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
843 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
844 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
845 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
846 static void display_menu_bar P_ ((struct window *));
847 static int display_count_lines P_ ((int, int, int, int, int *));
848 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
849 int, int, struct it *, int, int, int, int));
850 static void compute_line_metrics P_ ((struct it *));
851 static void run_redisplay_end_trigger_hook P_ ((struct it *));
852 static int get_overlay_strings P_ ((struct it *, int));
853 static void next_overlay_string P_ ((struct it *));
854 static void reseat P_ ((struct it *, struct text_pos, int));
855 static void reseat_1 P_ ((struct it *, struct text_pos, int));
856 static void back_to_previous_visible_line_start P_ ((struct it *));
857 static void reseat_at_previous_visible_line_start P_ ((struct it *));
858 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
859 static int next_element_from_display_vector P_ ((struct it *));
860 static int next_element_from_string P_ ((struct it *));
861 static int next_element_from_c_string P_ ((struct it *));
862 static int next_element_from_buffer P_ ((struct it *));
863 static int next_element_from_composition P_ ((struct it *));
864 static int next_element_from_image P_ ((struct it *));
865 static int next_element_from_stretch P_ ((struct it *));
866 static void load_overlay_strings P_ ((struct it *, int));
867 static int init_from_display_pos P_ ((struct it *, struct window *,
868 struct display_pos *));
869 static void reseat_to_string P_ ((struct it *, unsigned char *,
870 Lisp_Object, int, int, int, int));
871 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
872 int, int, int));
873 void move_it_vertically_backward P_ ((struct it *, int));
874 static void init_to_row_start P_ ((struct it *, struct window *,
875 struct glyph_row *));
876 static int init_to_row_end P_ ((struct it *, struct window *,
877 struct glyph_row *));
878 static void back_to_previous_line_start P_ ((struct it *));
879 static int forward_to_next_line_start P_ ((struct it *, int *));
880 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
881 Lisp_Object, int));
882 static struct text_pos string_pos P_ ((int, Lisp_Object));
883 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
884 static int number_of_chars P_ ((unsigned char *, int));
885 static void compute_stop_pos P_ ((struct it *));
886 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
887 Lisp_Object));
888 static int face_before_or_after_it_pos P_ ((struct it *, int));
889 static int next_overlay_change P_ ((int));
890 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
891 Lisp_Object, struct text_pos *,
892 int));
893 static int underlying_face_id P_ ((struct it *));
894 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
895 struct window *));
897 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
898 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
900 #ifdef HAVE_WINDOW_SYSTEM
902 static void update_tool_bar P_ ((struct frame *, int));
903 static void build_desired_tool_bar_string P_ ((struct frame *f));
904 static int redisplay_tool_bar P_ ((struct frame *));
905 static void display_tool_bar_line P_ ((struct it *));
906 static void notice_overwritten_cursor P_ ((struct window *,
907 enum glyph_row_area,
908 int, int, int, int));
912 #endif /* HAVE_WINDOW_SYSTEM */
915 /***********************************************************************
916 Window display dimensions
917 ***********************************************************************/
919 /* Return the bottom boundary y-position for text lines in window W.
920 This is the first y position at which a line cannot start.
921 It is relative to the top of the window.
923 This is the height of W minus the height of a mode line, if any. */
925 INLINE int
926 window_text_bottom_y (w)
927 struct window *w;
929 int height = WINDOW_TOTAL_HEIGHT (w);
931 if (WINDOW_WANTS_MODELINE_P (w))
932 height -= CURRENT_MODE_LINE_HEIGHT (w);
933 return height;
936 /* Return the pixel width of display area AREA of window W. AREA < 0
937 means return the total width of W, not including fringes to
938 the left and right of the window. */
940 INLINE int
941 window_box_width (w, area)
942 struct window *w;
943 int area;
945 int cols = XFASTINT (w->total_cols);
946 int pixels = 0;
948 if (!w->pseudo_window_p)
950 cols -= WINDOW_SCROLL_BAR_COLS (w);
952 if (area == TEXT_AREA)
954 if (INTEGERP (w->left_margin_cols))
955 cols -= XFASTINT (w->left_margin_cols);
956 if (INTEGERP (w->right_margin_cols))
957 cols -= XFASTINT (w->right_margin_cols);
958 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
960 else if (area == LEFT_MARGIN_AREA)
962 cols = (INTEGERP (w->left_margin_cols)
963 ? XFASTINT (w->left_margin_cols) : 0);
964 pixels = 0;
966 else if (area == RIGHT_MARGIN_AREA)
968 cols = (INTEGERP (w->right_margin_cols)
969 ? XFASTINT (w->right_margin_cols) : 0);
970 pixels = 0;
974 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
978 /* Return the pixel height of the display area of window W, not
979 including mode lines of W, if any. */
981 INLINE int
982 window_box_height (w)
983 struct window *w;
985 struct frame *f = XFRAME (w->frame);
986 int height = WINDOW_TOTAL_HEIGHT (w);
988 xassert (height >= 0);
990 /* Note: the code below that determines the mode-line/header-line
991 height is essentially the same as that contained in the macro
992 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
993 the appropriate glyph row has its `mode_line_p' flag set,
994 and if it doesn't, uses estimate_mode_line_height instead. */
996 if (WINDOW_WANTS_MODELINE_P (w))
998 struct glyph_row *ml_row
999 = (w->current_matrix && w->current_matrix->rows
1000 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1001 : 0);
1002 if (ml_row && ml_row->mode_line_p)
1003 height -= ml_row->height;
1004 else
1005 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1008 if (WINDOW_WANTS_HEADER_LINE_P (w))
1010 struct glyph_row *hl_row
1011 = (w->current_matrix && w->current_matrix->rows
1012 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1013 : 0);
1014 if (hl_row && hl_row->mode_line_p)
1015 height -= hl_row->height;
1016 else
1017 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1020 /* With a very small font and a mode-line that's taller than
1021 default, we might end up with a negative height. */
1022 return max (0, height);
1025 /* Return the window-relative coordinate of the left edge of display
1026 area AREA of window W. AREA < 0 means return the left edge of the
1027 whole window, to the right of the left fringe of W. */
1029 INLINE int
1030 window_box_left_offset (w, area)
1031 struct window *w;
1032 int area;
1034 int x;
1036 if (w->pseudo_window_p)
1037 return 0;
1039 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1041 if (area == TEXT_AREA)
1042 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1043 + window_box_width (w, LEFT_MARGIN_AREA));
1044 else if (area == RIGHT_MARGIN_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA)
1047 + window_box_width (w, TEXT_AREA)
1048 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1050 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1051 else if (area == LEFT_MARGIN_AREA
1052 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1053 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1055 return x;
1059 /* Return the window-relative coordinate of the right edge of display
1060 area AREA of window W. AREA < 0 means return the left edge of the
1061 whole window, to the left of the right fringe of W. */
1063 INLINE int
1064 window_box_right_offset (w, area)
1065 struct window *w;
1066 int area;
1068 return window_box_left_offset (w, area) + window_box_width (w, area);
1071 /* Return the frame-relative coordinate of the left edge of display
1072 area AREA of window W. AREA < 0 means return the left edge of the
1073 whole window, to the right of the left fringe of W. */
1075 INLINE int
1076 window_box_left (w, area)
1077 struct window *w;
1078 int area;
1080 struct frame *f = XFRAME (w->frame);
1081 int x;
1083 if (w->pseudo_window_p)
1084 return FRAME_INTERNAL_BORDER_WIDTH (f);
1086 x = (WINDOW_LEFT_EDGE_X (w)
1087 + window_box_left_offset (w, area));
1089 return x;
1093 /* Return the frame-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the left of the right fringe of W. */
1097 INLINE int
1098 window_box_right (w, area)
1099 struct window *w;
1100 int area;
1102 return window_box_left (w, area) + window_box_width (w, area);
1105 /* Get the bounding box of the display area AREA of window W, without
1106 mode lines, in frame-relative coordinates. AREA < 0 means the
1107 whole window, not including the left and right fringes of
1108 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1109 coordinates of the upper-left corner of the box. Return in
1110 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1112 INLINE void
1113 window_box (w, area, box_x, box_y, box_width, box_height)
1114 struct window *w;
1115 int area;
1116 int *box_x, *box_y, *box_width, *box_height;
1118 if (box_width)
1119 *box_width = window_box_width (w, area);
1120 if (box_height)
1121 *box_height = window_box_height (w);
1122 if (box_x)
1123 *box_x = window_box_left (w, area);
1124 if (box_y)
1126 *box_y = WINDOW_TOP_EDGE_Y (w);
1127 if (WINDOW_WANTS_HEADER_LINE_P (w))
1128 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines. AREA < 0 means the whole window, not including the
1135 left and right fringe of the window. Return in *TOP_LEFT_X
1136 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1137 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1138 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1139 box. */
1141 INLINE void
1142 window_box_edges (w, area, top_left_x, top_left_y,
1143 bottom_right_x, bottom_right_y)
1144 struct window *w;
1145 int area;
1146 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1148 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1149 bottom_right_y);
1150 *bottom_right_x += *top_left_x;
1151 *bottom_right_y += *top_left_y;
1156 /***********************************************************************
1157 Utilities
1158 ***********************************************************************/
1160 /* Return the bottom y-position of the line the iterator IT is in.
1161 This can modify IT's settings. */
1164 line_bottom_y (it)
1165 struct it *it;
1167 int line_height = it->max_ascent + it->max_descent;
1168 int line_top_y = it->current_y;
1170 if (line_height == 0)
1172 if (last_height)
1173 line_height = last_height;
1174 else if (IT_CHARPOS (*it) < ZV)
1176 move_it_by_lines (it, 1, 1);
1177 line_height = (it->max_ascent || it->max_descent
1178 ? it->max_ascent + it->max_descent
1179 : last_height);
1181 else
1183 struct glyph_row *row = it->glyph_row;
1185 /* Use the default character height. */
1186 it->glyph_row = NULL;
1187 it->what = IT_CHARACTER;
1188 it->c = ' ';
1189 it->len = 1;
1190 PRODUCE_GLYPHS (it);
1191 line_height = it->ascent + it->descent;
1192 it->glyph_row = row;
1196 return line_top_y + line_height;
1200 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1201 1 if POS is visible and the line containing POS is fully visible.
1202 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1203 and header-lines heights. */
1206 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1207 struct window *w;
1208 int charpos, *fully, exact_mode_line_heights_p;
1210 struct it it;
1211 struct text_pos top;
1212 int visible_p;
1213 struct buffer *old_buffer = NULL;
1215 if (XBUFFER (w->buffer) != current_buffer)
1217 old_buffer = current_buffer;
1218 set_buffer_internal_1 (XBUFFER (w->buffer));
1221 *fully = visible_p = 0;
1222 SET_TEXT_POS_FROM_MARKER (top, w->start);
1224 /* Compute exact mode line heights, if requested. */
1225 if (exact_mode_line_heights_p)
1227 if (WINDOW_WANTS_MODELINE_P (w))
1228 current_mode_line_height
1229 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1230 current_buffer->mode_line_format);
1232 if (WINDOW_WANTS_HEADER_LINE_P (w))
1233 current_header_line_height
1234 = display_mode_line (w, HEADER_LINE_FACE_ID,
1235 current_buffer->header_line_format);
1238 start_display (&it, w, top);
1239 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1240 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1242 /* Note that we may overshoot because of invisible text. */
1243 if (IT_CHARPOS (it) >= charpos)
1245 int top_y = it.current_y;
1246 int bottom_y = line_bottom_y (&it);
1247 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1249 if (top_y < window_top_y)
1250 visible_p = bottom_y > window_top_y;
1251 else if (top_y < it.last_visible_y)
1253 visible_p = 1;
1254 *fully = bottom_y <= it.last_visible_y;
1257 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1259 move_it_by_lines (&it, 1, 0);
1260 if (charpos < IT_CHARPOS (it))
1262 visible_p = 1;
1263 *fully = 0;
1267 if (old_buffer)
1268 set_buffer_internal_1 (old_buffer);
1270 current_header_line_height = current_mode_line_height = -1;
1271 return visible_p;
1275 /* Return the next character from STR which is MAXLEN bytes long.
1276 Return in *LEN the length of the character. This is like
1277 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1278 we find one, we return a `?', but with the length of the invalid
1279 character. */
1281 static INLINE int
1282 string_char_and_length (str, maxlen, len)
1283 const unsigned char *str;
1284 int maxlen, *len;
1286 int c;
1288 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1289 if (!CHAR_VALID_P (c, 1))
1290 /* We may not change the length here because other places in Emacs
1291 don't use this function, i.e. they silently accept invalid
1292 characters. */
1293 c = '?';
1295 return c;
1300 /* Given a position POS containing a valid character and byte position
1301 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1303 static struct text_pos
1304 string_pos_nchars_ahead (pos, string, nchars)
1305 struct text_pos pos;
1306 Lisp_Object string;
1307 int nchars;
1309 xassert (STRINGP (string) && nchars >= 0);
1311 if (STRING_MULTIBYTE (string))
1313 int rest = SBYTES (string) - BYTEPOS (pos);
1314 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1315 int len;
1317 while (nchars--)
1319 string_char_and_length (p, rest, &len);
1320 p += len, rest -= len;
1321 xassert (rest >= 0);
1322 CHARPOS (pos) += 1;
1323 BYTEPOS (pos) += len;
1326 else
1327 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1329 return pos;
1333 /* Value is the text position, i.e. character and byte position,
1334 for character position CHARPOS in STRING. */
1336 static INLINE struct text_pos
1337 string_pos (charpos, string)
1338 int charpos;
1339 Lisp_Object string;
1341 struct text_pos pos;
1342 xassert (STRINGP (string));
1343 xassert (charpos >= 0);
1344 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1345 return pos;
1349 /* Value is a text position, i.e. character and byte position, for
1350 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1351 means recognize multibyte characters. */
1353 static struct text_pos
1354 c_string_pos (charpos, s, multibyte_p)
1355 int charpos;
1356 unsigned char *s;
1357 int multibyte_p;
1359 struct text_pos pos;
1361 xassert (s != NULL);
1362 xassert (charpos >= 0);
1364 if (multibyte_p)
1366 int rest = strlen (s), len;
1368 SET_TEXT_POS (pos, 0, 0);
1369 while (charpos--)
1371 string_char_and_length (s, rest, &len);
1372 s += len, rest -= len;
1373 xassert (rest >= 0);
1374 CHARPOS (pos) += 1;
1375 BYTEPOS (pos) += len;
1378 else
1379 SET_TEXT_POS (pos, charpos, charpos);
1381 return pos;
1385 /* Value is the number of characters in C string S. MULTIBYTE_P
1386 non-zero means recognize multibyte characters. */
1388 static int
1389 number_of_chars (s, multibyte_p)
1390 unsigned char *s;
1391 int multibyte_p;
1393 int nchars;
1395 if (multibyte_p)
1397 int rest = strlen (s), len;
1398 unsigned char *p = (unsigned char *) s;
1400 for (nchars = 0; rest > 0; ++nchars)
1402 string_char_and_length (p, rest, &len);
1403 rest -= len, p += len;
1406 else
1407 nchars = strlen (s);
1409 return nchars;
1413 /* Compute byte position NEWPOS->bytepos corresponding to
1414 NEWPOS->charpos. POS is a known position in string STRING.
1415 NEWPOS->charpos must be >= POS.charpos. */
1417 static void
1418 compute_string_pos (newpos, pos, string)
1419 struct text_pos *newpos, pos;
1420 Lisp_Object string;
1422 xassert (STRINGP (string));
1423 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1425 if (STRING_MULTIBYTE (string))
1426 *newpos = string_pos_nchars_ahead (pos, string,
1427 CHARPOS (*newpos) - CHARPOS (pos));
1428 else
1429 BYTEPOS (*newpos) = CHARPOS (*newpos);
1432 /* EXPORT:
1433 Return an estimation of the pixel height of mode or top lines on
1434 frame F. FACE_ID specifies what line's height to estimate. */
1437 estimate_mode_line_height (f, face_id)
1438 struct frame *f;
1439 enum face_id face_id;
1441 #ifdef HAVE_WINDOW_SYSTEM
1442 if (FRAME_WINDOW_P (f))
1444 int height = FONT_HEIGHT (FRAME_FONT (f));
1446 /* This function is called so early when Emacs starts that the face
1447 cache and mode line face are not yet initialized. */
1448 if (FRAME_FACE_CACHE (f))
1450 struct face *face = FACE_FROM_ID (f, face_id);
1451 if (face)
1453 if (face->font)
1454 height = FONT_HEIGHT (face->font);
1455 if (face->box_line_width > 0)
1456 height += 2 * face->box_line_width;
1460 return height;
1462 #endif
1464 return 1;
1467 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1468 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1469 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1470 not force the value into range. */
1472 void
1473 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1474 FRAME_PTR f;
1475 register int pix_x, pix_y;
1476 int *x, *y;
1477 NativeRectangle *bounds;
1478 int noclip;
1481 #ifdef HAVE_WINDOW_SYSTEM
1482 if (FRAME_WINDOW_P (f))
1484 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1485 even for negative values. */
1486 if (pix_x < 0)
1487 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1488 if (pix_y < 0)
1489 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1491 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1492 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1494 if (bounds)
1495 STORE_NATIVE_RECT (*bounds,
1496 FRAME_COL_TO_PIXEL_X (f, pix_x),
1497 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1498 FRAME_COLUMN_WIDTH (f) - 1,
1499 FRAME_LINE_HEIGHT (f) - 1);
1501 if (!noclip)
1503 if (pix_x < 0)
1504 pix_x = 0;
1505 else if (pix_x > FRAME_TOTAL_COLS (f))
1506 pix_x = FRAME_TOTAL_COLS (f);
1508 if (pix_y < 0)
1509 pix_y = 0;
1510 else if (pix_y > FRAME_LINES (f))
1511 pix_y = FRAME_LINES (f);
1514 #endif
1516 *x = pix_x;
1517 *y = pix_y;
1521 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1522 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1523 can't tell the positions because W's display is not up to date,
1524 return 0. */
1527 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1528 struct window *w;
1529 int hpos, vpos;
1530 int *frame_x, *frame_y;
1532 #ifdef HAVE_WINDOW_SYSTEM
1533 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1535 int success_p;
1537 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1538 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1540 if (display_completed)
1542 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1543 struct glyph *glyph = row->glyphs[TEXT_AREA];
1544 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1546 hpos = row->x;
1547 vpos = row->y;
1548 while (glyph < end)
1550 hpos += glyph->pixel_width;
1551 ++glyph;
1554 /* If first glyph is partially visible, its first visible position is still 0. */
1555 if (hpos < 0)
1556 hpos = 0;
1558 success_p = 1;
1560 else
1562 hpos = vpos = 0;
1563 success_p = 0;
1566 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1567 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1568 return success_p;
1570 #endif
1572 *frame_x = hpos;
1573 *frame_y = vpos;
1574 return 1;
1578 #ifdef HAVE_WINDOW_SYSTEM
1580 /* Find the glyph under window-relative coordinates X/Y in window W.
1581 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1582 strings. Return in *HPOS and *VPOS the row and column number of
1583 the glyph found. Return in *AREA the glyph area containing X.
1584 Value is a pointer to the glyph found or null if X/Y is not on
1585 text, or we can't tell because W's current matrix is not up to
1586 date. */
1588 static struct glyph *
1589 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1590 struct window *w;
1591 int x, y;
1592 int *hpos, *vpos, *dx, *dy, *area;
1594 struct glyph *glyph, *end;
1595 struct glyph_row *row = NULL;
1596 int x0, i;
1598 /* Find row containing Y. Give up if some row is not enabled. */
1599 for (i = 0; i < w->current_matrix->nrows; ++i)
1601 row = MATRIX_ROW (w->current_matrix, i);
1602 if (!row->enabled_p)
1603 return NULL;
1604 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1605 break;
1608 *vpos = i;
1609 *hpos = 0;
1611 /* Give up if Y is not in the window. */
1612 if (i == w->current_matrix->nrows)
1613 return NULL;
1615 /* Get the glyph area containing X. */
1616 if (w->pseudo_window_p)
1618 *area = TEXT_AREA;
1619 x0 = 0;
1621 else
1623 if (x < window_box_left_offset (w, TEXT_AREA))
1625 *area = LEFT_MARGIN_AREA;
1626 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1628 else if (x < window_box_right_offset (w, TEXT_AREA))
1630 *area = TEXT_AREA;
1631 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1633 else
1635 *area = RIGHT_MARGIN_AREA;
1636 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1640 /* Find glyph containing X. */
1641 glyph = row->glyphs[*area];
1642 end = glyph + row->used[*area];
1643 x -= x0;
1644 while (glyph < end && x >= glyph->pixel_width)
1646 x -= glyph->pixel_width;
1647 ++glyph;
1650 if (glyph == end)
1651 return NULL;
1653 if (dx)
1655 *dx = x;
1656 *dy = y - (row->y + row->ascent - glyph->ascent);
1659 *hpos = glyph - row->glyphs[*area];
1660 return glyph;
1664 /* EXPORT:
1665 Convert frame-relative x/y to coordinates relative to window W.
1666 Takes pseudo-windows into account. */
1668 void
1669 frame_to_window_pixel_xy (w, x, y)
1670 struct window *w;
1671 int *x, *y;
1673 if (w->pseudo_window_p)
1675 /* A pseudo-window is always full-width, and starts at the
1676 left edge of the frame, plus a frame border. */
1677 struct frame *f = XFRAME (w->frame);
1678 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1679 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1681 else
1683 *x -= WINDOW_LEFT_EDGE_X (w);
1684 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1688 /* EXPORT:
1689 Return in *R the clipping rectangle for glyph string S. */
1691 void
1692 get_glyph_string_clip_rect (s, nr)
1693 struct glyph_string *s;
1694 NativeRectangle *nr;
1696 XRectangle r;
1698 if (s->row->full_width_p)
1700 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1701 r.x = WINDOW_LEFT_EDGE_X (s->w);
1702 r.width = WINDOW_TOTAL_WIDTH (s->w);
1704 /* Unless displaying a mode or menu bar line, which are always
1705 fully visible, clip to the visible part of the row. */
1706 if (s->w->pseudo_window_p)
1707 r.height = s->row->visible_height;
1708 else
1709 r.height = s->height;
1711 else
1713 /* This is a text line that may be partially visible. */
1714 r.x = window_box_left (s->w, s->area);
1715 r.width = window_box_width (s->w, s->area);
1716 r.height = s->row->visible_height;
1719 /* If S draws overlapping rows, it's sufficient to use the top and
1720 bottom of the window for clipping because this glyph string
1721 intentionally draws over other lines. */
1722 if (s->for_overlaps_p)
1724 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1725 r.height = window_text_bottom_y (s->w) - r.y;
1727 else
1729 /* Don't use S->y for clipping because it doesn't take partially
1730 visible lines into account. For example, it can be negative for
1731 partially visible lines at the top of a window. */
1732 if (!s->row->full_width_p
1733 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1734 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1735 else
1736 r.y = max (0, s->row->y);
1738 /* If drawing a tool-bar window, draw it over the internal border
1739 at the top of the window. */
1740 if (s->w == XWINDOW (s->f->tool_bar_window))
1741 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1744 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1746 /* If drawing the cursor, don't let glyph draw outside its
1747 advertised boundaries. Cleartype does this under some circumstances. */
1748 if (s->hl == DRAW_CURSOR)
1750 struct glyph *glyph = s->first_glyph;
1751 int height;
1753 if (s->x > r.x)
1755 r.width -= s->x - r.x;
1756 r.x = s->x;
1758 r.width = min (r.width, glyph->pixel_width);
1760 /* Don't draw cursor glyph taller than our actual glyph. */
1761 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1762 if (height < r.height)
1764 r.y = s->ybase + glyph->descent - height;
1765 r.height = height;
1769 #ifdef CONVERT_FROM_XRECT
1770 CONVERT_FROM_XRECT (r, *nr);
1771 #else
1772 *nr = r;
1773 #endif
1776 #endif /* HAVE_WINDOW_SYSTEM */
1779 /***********************************************************************
1780 Lisp form evaluation
1781 ***********************************************************************/
1783 /* Error handler for safe_eval and safe_call. */
1785 static Lisp_Object
1786 safe_eval_handler (arg)
1787 Lisp_Object arg;
1789 add_to_log ("Error during redisplay: %s", arg, Qnil);
1790 return Qnil;
1794 /* Evaluate SEXPR and return the result, or nil if something went
1795 wrong. Prevent redisplay during the evaluation. */
1797 Lisp_Object
1798 safe_eval (sexpr)
1799 Lisp_Object sexpr;
1801 Lisp_Object val;
1803 if (inhibit_eval_during_redisplay)
1804 val = Qnil;
1805 else
1807 int count = SPECPDL_INDEX ();
1808 struct gcpro gcpro1;
1810 GCPRO1 (sexpr);
1811 specbind (Qinhibit_redisplay, Qt);
1812 /* Use Qt to ensure debugger does not run,
1813 so there is no possibility of wanting to redisplay. */
1814 val = internal_condition_case_1 (Feval, sexpr, Qt,
1815 safe_eval_handler);
1816 UNGCPRO;
1817 val = unbind_to (count, val);
1820 return val;
1824 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1825 Return the result, or nil if something went wrong. Prevent
1826 redisplay during the evaluation. */
1828 Lisp_Object
1829 safe_call (nargs, args)
1830 int nargs;
1831 Lisp_Object *args;
1833 Lisp_Object val;
1835 if (inhibit_eval_during_redisplay)
1836 val = Qnil;
1837 else
1839 int count = SPECPDL_INDEX ();
1840 struct gcpro gcpro1;
1842 GCPRO1 (args[0]);
1843 gcpro1.nvars = nargs;
1844 specbind (Qinhibit_redisplay, Qt);
1845 /* Use Qt to ensure debugger does not run,
1846 so there is no possibility of wanting to redisplay. */
1847 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1848 safe_eval_handler);
1849 UNGCPRO;
1850 val = unbind_to (count, val);
1853 return val;
1857 /* Call function FN with one argument ARG.
1858 Return the result, or nil if something went wrong. */
1860 Lisp_Object
1861 safe_call1 (fn, arg)
1862 Lisp_Object fn, arg;
1864 Lisp_Object args[2];
1865 args[0] = fn;
1866 args[1] = arg;
1867 return safe_call (2, args);
1872 /***********************************************************************
1873 Debugging
1874 ***********************************************************************/
1876 #if 0
1878 /* Define CHECK_IT to perform sanity checks on iterators.
1879 This is for debugging. It is too slow to do unconditionally. */
1881 static void
1882 check_it (it)
1883 struct it *it;
1885 if (it->method == next_element_from_string)
1887 xassert (STRINGP (it->string));
1888 xassert (IT_STRING_CHARPOS (*it) >= 0);
1890 else if (it->method == next_element_from_buffer)
1892 /* Check that character and byte positions agree. */
1893 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1896 if (it->dpvec)
1897 xassert (it->current.dpvec_index >= 0);
1898 else
1899 xassert (it->current.dpvec_index < 0);
1902 #define CHECK_IT(IT) check_it ((IT))
1904 #else /* not 0 */
1906 #define CHECK_IT(IT) (void) 0
1908 #endif /* not 0 */
1911 #if GLYPH_DEBUG
1913 /* Check that the window end of window W is what we expect it
1914 to be---the last row in the current matrix displaying text. */
1916 static void
1917 check_window_end (w)
1918 struct window *w;
1920 if (!MINI_WINDOW_P (w)
1921 && !NILP (w->window_end_valid))
1923 struct glyph_row *row;
1924 xassert ((row = MATRIX_ROW (w->current_matrix,
1925 XFASTINT (w->window_end_vpos)),
1926 !row->enabled_p
1927 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1928 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1932 #define CHECK_WINDOW_END(W) check_window_end ((W))
1934 #else /* not GLYPH_DEBUG */
1936 #define CHECK_WINDOW_END(W) (void) 0
1938 #endif /* not GLYPH_DEBUG */
1942 /***********************************************************************
1943 Iterator initialization
1944 ***********************************************************************/
1946 /* Initialize IT for displaying current_buffer in window W, starting
1947 at character position CHARPOS. CHARPOS < 0 means that no buffer
1948 position is specified which is useful when the iterator is assigned
1949 a position later. BYTEPOS is the byte position corresponding to
1950 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1952 If ROW is not null, calls to produce_glyphs with IT as parameter
1953 will produce glyphs in that row.
1955 BASE_FACE_ID is the id of a base face to use. It must be one of
1956 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1957 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1958 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1960 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1961 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1962 will be initialized to use the corresponding mode line glyph row of
1963 the desired matrix of W. */
1965 void
1966 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1967 struct it *it;
1968 struct window *w;
1969 int charpos, bytepos;
1970 struct glyph_row *row;
1971 enum face_id base_face_id;
1973 int highlight_region_p;
1975 /* Some precondition checks. */
1976 xassert (w != NULL && it != NULL);
1977 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1978 && charpos <= ZV));
1980 /* If face attributes have been changed since the last redisplay,
1981 free realized faces now because they depend on face definitions
1982 that might have changed. Don't free faces while there might be
1983 desired matrices pending which reference these faces. */
1984 if (face_change_count && !inhibit_free_realized_faces)
1986 face_change_count = 0;
1987 free_all_realized_faces (Qnil);
1990 /* Use one of the mode line rows of W's desired matrix if
1991 appropriate. */
1992 if (row == NULL)
1994 if (base_face_id == MODE_LINE_FACE_ID
1995 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1996 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1997 else if (base_face_id == HEADER_LINE_FACE_ID)
1998 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2001 /* Clear IT. */
2002 bzero (it, sizeof *it);
2003 it->current.overlay_string_index = -1;
2004 it->current.dpvec_index = -1;
2005 it->base_face_id = base_face_id;
2007 /* The window in which we iterate over current_buffer: */
2008 XSETWINDOW (it->window, w);
2009 it->w = w;
2010 it->f = XFRAME (w->frame);
2012 /* Extra space between lines (on window systems only). */
2013 if (base_face_id == DEFAULT_FACE_ID
2014 && FRAME_WINDOW_P (it->f))
2016 if (NATNUMP (current_buffer->extra_line_spacing))
2017 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2018 else if (it->f->extra_line_spacing > 0)
2019 it->extra_line_spacing = it->f->extra_line_spacing;
2022 /* If realized faces have been removed, e.g. because of face
2023 attribute changes of named faces, recompute them. When running
2024 in batch mode, the face cache of the initial frame is null. If
2025 we happen to get called, make a dummy face cache. */
2026 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2027 init_frame_faces (it->f);
2028 if (FRAME_FACE_CACHE (it->f)->used == 0)
2029 recompute_basic_faces (it->f);
2031 /* Current value of the `space-width', and 'height' properties. */
2032 it->space_width = Qnil;
2033 it->font_height = Qnil;
2035 /* Are control characters displayed as `^C'? */
2036 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2038 /* -1 means everything between a CR and the following line end
2039 is invisible. >0 means lines indented more than this value are
2040 invisible. */
2041 it->selective = (INTEGERP (current_buffer->selective_display)
2042 ? XFASTINT (current_buffer->selective_display)
2043 : (!NILP (current_buffer->selective_display)
2044 ? -1 : 0));
2045 it->selective_display_ellipsis_p
2046 = !NILP (current_buffer->selective_display_ellipses);
2048 /* Display table to use. */
2049 it->dp = window_display_table (w);
2051 /* Are multibyte characters enabled in current_buffer? */
2052 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2054 /* Non-zero if we should highlight the region. */
2055 highlight_region_p
2056 = (!NILP (Vtransient_mark_mode)
2057 && !NILP (current_buffer->mark_active)
2058 && XMARKER (current_buffer->mark)->buffer != 0);
2060 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2061 start and end of a visible region in window IT->w. Set both to
2062 -1 to indicate no region. */
2063 if (highlight_region_p
2064 /* Maybe highlight only in selected window. */
2065 && (/* Either show region everywhere. */
2066 highlight_nonselected_windows
2067 /* Or show region in the selected window. */
2068 || w == XWINDOW (selected_window)
2069 /* Or show the region if we are in the mini-buffer and W is
2070 the window the mini-buffer refers to. */
2071 || (MINI_WINDOW_P (XWINDOW (selected_window))
2072 && WINDOWP (minibuf_selected_window)
2073 && w == XWINDOW (minibuf_selected_window))))
2075 int charpos = marker_position (current_buffer->mark);
2076 it->region_beg_charpos = min (PT, charpos);
2077 it->region_end_charpos = max (PT, charpos);
2079 else
2080 it->region_beg_charpos = it->region_end_charpos = -1;
2082 /* Get the position at which the redisplay_end_trigger hook should
2083 be run, if it is to be run at all. */
2084 if (MARKERP (w->redisplay_end_trigger)
2085 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2086 it->redisplay_end_trigger_charpos
2087 = marker_position (w->redisplay_end_trigger);
2088 else if (INTEGERP (w->redisplay_end_trigger))
2089 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2091 /* Correct bogus values of tab_width. */
2092 it->tab_width = XINT (current_buffer->tab_width);
2093 if (it->tab_width <= 0 || it->tab_width > 1000)
2094 it->tab_width = 8;
2096 /* Are lines in the display truncated? */
2097 it->truncate_lines_p
2098 = (base_face_id != DEFAULT_FACE_ID
2099 || XINT (it->w->hscroll)
2100 || (truncate_partial_width_windows
2101 && !WINDOW_FULL_WIDTH_P (it->w))
2102 || !NILP (current_buffer->truncate_lines));
2104 /* Get dimensions of truncation and continuation glyphs. These are
2105 displayed as fringe bitmaps under X, so we don't need them for such
2106 frames. */
2107 if (!FRAME_WINDOW_P (it->f))
2109 if (it->truncate_lines_p)
2111 /* We will need the truncation glyph. */
2112 xassert (it->glyph_row == NULL);
2113 produce_special_glyphs (it, IT_TRUNCATION);
2114 it->truncation_pixel_width = it->pixel_width;
2116 else
2118 /* We will need the continuation glyph. */
2119 xassert (it->glyph_row == NULL);
2120 produce_special_glyphs (it, IT_CONTINUATION);
2121 it->continuation_pixel_width = it->pixel_width;
2124 /* Reset these values to zero because the produce_special_glyphs
2125 above has changed them. */
2126 it->pixel_width = it->ascent = it->descent = 0;
2127 it->phys_ascent = it->phys_descent = 0;
2130 /* Set this after getting the dimensions of truncation and
2131 continuation glyphs, so that we don't produce glyphs when calling
2132 produce_special_glyphs, above. */
2133 it->glyph_row = row;
2134 it->area = TEXT_AREA;
2136 /* Get the dimensions of the display area. The display area
2137 consists of the visible window area plus a horizontally scrolled
2138 part to the left of the window. All x-values are relative to the
2139 start of this total display area. */
2140 if (base_face_id != DEFAULT_FACE_ID)
2142 /* Mode lines, menu bar in terminal frames. */
2143 it->first_visible_x = 0;
2144 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2146 else
2148 it->first_visible_x
2149 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2150 it->last_visible_x = (it->first_visible_x
2151 + window_box_width (w, TEXT_AREA));
2153 /* If we truncate lines, leave room for the truncator glyph(s) at
2154 the right margin. Otherwise, leave room for the continuation
2155 glyph(s). Truncation and continuation glyphs are not inserted
2156 for window-based redisplay. */
2157 if (!FRAME_WINDOW_P (it->f))
2159 if (it->truncate_lines_p)
2160 it->last_visible_x -= it->truncation_pixel_width;
2161 else
2162 it->last_visible_x -= it->continuation_pixel_width;
2165 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2166 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2169 /* Leave room for a border glyph. */
2170 if (!FRAME_WINDOW_P (it->f)
2171 && !WINDOW_RIGHTMOST_P (it->w))
2172 it->last_visible_x -= 1;
2174 it->last_visible_y = window_text_bottom_y (w);
2176 /* For mode lines and alike, arrange for the first glyph having a
2177 left box line if the face specifies a box. */
2178 if (base_face_id != DEFAULT_FACE_ID)
2180 struct face *face;
2182 it->face_id = base_face_id;
2184 /* If we have a boxed mode line, make the first character appear
2185 with a left box line. */
2186 face = FACE_FROM_ID (it->f, base_face_id);
2187 if (face->box != FACE_NO_BOX)
2188 it->start_of_box_run_p = 1;
2191 /* If a buffer position was specified, set the iterator there,
2192 getting overlays and face properties from that position. */
2193 if (charpos >= BUF_BEG (current_buffer))
2195 it->end_charpos = ZV;
2196 it->face_id = -1;
2197 IT_CHARPOS (*it) = charpos;
2199 /* Compute byte position if not specified. */
2200 if (bytepos < charpos)
2201 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2202 else
2203 IT_BYTEPOS (*it) = bytepos;
2205 it->start = it->current;
2207 /* Compute faces etc. */
2208 reseat (it, it->current.pos, 1);
2211 CHECK_IT (it);
2215 /* Initialize IT for the display of window W with window start POS. */
2217 void
2218 start_display (it, w, pos)
2219 struct it *it;
2220 struct window *w;
2221 struct text_pos pos;
2223 struct glyph_row *row;
2224 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2226 row = w->desired_matrix->rows + first_vpos;
2227 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2228 it->first_vpos = first_vpos;
2230 if (!it->truncate_lines_p)
2232 int start_at_line_beg_p;
2233 int first_y = it->current_y;
2235 /* If window start is not at a line start, skip forward to POS to
2236 get the correct continuation lines width. */
2237 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2238 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2239 if (!start_at_line_beg_p)
2241 int new_x;
2243 reseat_at_previous_visible_line_start (it);
2244 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2246 new_x = it->current_x + it->pixel_width;
2248 /* If lines are continued, this line may end in the middle
2249 of a multi-glyph character (e.g. a control character
2250 displayed as \003, or in the middle of an overlay
2251 string). In this case move_it_to above will not have
2252 taken us to the start of the continuation line but to the
2253 end of the continued line. */
2254 if (it->current_x > 0
2255 && !it->truncate_lines_p /* Lines are continued. */
2256 && (/* And glyph doesn't fit on the line. */
2257 new_x > it->last_visible_x
2258 /* Or it fits exactly and we're on a window
2259 system frame. */
2260 || (new_x == it->last_visible_x
2261 && FRAME_WINDOW_P (it->f))))
2263 if (it->current.dpvec_index >= 0
2264 || it->current.overlay_string_index >= 0)
2266 set_iterator_to_next (it, 1);
2267 move_it_in_display_line_to (it, -1, -1, 0);
2270 it->continuation_lines_width += it->current_x;
2273 /* We're starting a new display line, not affected by the
2274 height of the continued line, so clear the appropriate
2275 fields in the iterator structure. */
2276 it->max_ascent = it->max_descent = 0;
2277 it->max_phys_ascent = it->max_phys_descent = 0;
2279 it->current_y = first_y;
2280 it->vpos = 0;
2281 it->current_x = it->hpos = 0;
2285 #if 0 /* Don't assert the following because start_display is sometimes
2286 called intentionally with a window start that is not at a
2287 line start. Please leave this code in as a comment. */
2289 /* Window start should be on a line start, now. */
2290 xassert (it->continuation_lines_width
2291 || IT_CHARPOS (it) == BEGV
2292 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2293 #endif /* 0 */
2297 /* Return 1 if POS is a position in ellipses displayed for invisible
2298 text. W is the window we display, for text property lookup. */
2300 static int
2301 in_ellipses_for_invisible_text_p (pos, w)
2302 struct display_pos *pos;
2303 struct window *w;
2305 Lisp_Object prop, window;
2306 int ellipses_p = 0;
2307 int charpos = CHARPOS (pos->pos);
2309 /* If POS specifies a position in a display vector, this might
2310 be for an ellipsis displayed for invisible text. We won't
2311 get the iterator set up for delivering that ellipsis unless
2312 we make sure that it gets aware of the invisible text. */
2313 if (pos->dpvec_index >= 0
2314 && pos->overlay_string_index < 0
2315 && CHARPOS (pos->string_pos) < 0
2316 && charpos > BEGV
2317 && (XSETWINDOW (window, w),
2318 prop = Fget_char_property (make_number (charpos),
2319 Qinvisible, window),
2320 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2322 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2323 window);
2324 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2327 return ellipses_p;
2331 /* Initialize IT for stepping through current_buffer in window W,
2332 starting at position POS that includes overlay string and display
2333 vector/ control character translation position information. Value
2334 is zero if there are overlay strings with newlines at POS. */
2336 static int
2337 init_from_display_pos (it, w, pos)
2338 struct it *it;
2339 struct window *w;
2340 struct display_pos *pos;
2342 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2343 int i, overlay_strings_with_newlines = 0;
2345 /* If POS specifies a position in a display vector, this might
2346 be for an ellipsis displayed for invisible text. We won't
2347 get the iterator set up for delivering that ellipsis unless
2348 we make sure that it gets aware of the invisible text. */
2349 if (in_ellipses_for_invisible_text_p (pos, w))
2351 --charpos;
2352 bytepos = 0;
2355 /* Keep in mind: the call to reseat in init_iterator skips invisible
2356 text, so we might end up at a position different from POS. This
2357 is only a problem when POS is a row start after a newline and an
2358 overlay starts there with an after-string, and the overlay has an
2359 invisible property. Since we don't skip invisible text in
2360 display_line and elsewhere immediately after consuming the
2361 newline before the row start, such a POS will not be in a string,
2362 but the call to init_iterator below will move us to the
2363 after-string. */
2364 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2366 for (i = 0; i < it->n_overlay_strings; ++i)
2368 const char *s = SDATA (it->overlay_strings[i]);
2369 const char *e = s + SBYTES (it->overlay_strings[i]);
2371 while (s < e && *s != '\n')
2372 ++s;
2374 if (s < e)
2376 overlay_strings_with_newlines = 1;
2377 break;
2381 /* If position is within an overlay string, set up IT to the right
2382 overlay string. */
2383 if (pos->overlay_string_index >= 0)
2385 int relative_index;
2387 /* If the first overlay string happens to have a `display'
2388 property for an image, the iterator will be set up for that
2389 image, and we have to undo that setup first before we can
2390 correct the overlay string index. */
2391 if (it->method == next_element_from_image)
2392 pop_it (it);
2394 /* We already have the first chunk of overlay strings in
2395 IT->overlay_strings. Load more until the one for
2396 pos->overlay_string_index is in IT->overlay_strings. */
2397 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2399 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2400 it->current.overlay_string_index = 0;
2401 while (n--)
2403 load_overlay_strings (it, 0);
2404 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2408 it->current.overlay_string_index = pos->overlay_string_index;
2409 relative_index = (it->current.overlay_string_index
2410 % OVERLAY_STRING_CHUNK_SIZE);
2411 it->string = it->overlay_strings[relative_index];
2412 xassert (STRINGP (it->string));
2413 it->current.string_pos = pos->string_pos;
2414 it->method = next_element_from_string;
2417 #if 0 /* This is bogus because POS not having an overlay string
2418 position does not mean it's after the string. Example: A
2419 line starting with a before-string and initialization of IT
2420 to the previous row's end position. */
2421 else if (it->current.overlay_string_index >= 0)
2423 /* If POS says we're already after an overlay string ending at
2424 POS, make sure to pop the iterator because it will be in
2425 front of that overlay string. When POS is ZV, we've thereby
2426 also ``processed'' overlay strings at ZV. */
2427 while (it->sp)
2428 pop_it (it);
2429 it->current.overlay_string_index = -1;
2430 it->method = next_element_from_buffer;
2431 if (CHARPOS (pos->pos) == ZV)
2432 it->overlay_strings_at_end_processed_p = 1;
2434 #endif /* 0 */
2436 if (CHARPOS (pos->string_pos) >= 0)
2438 /* Recorded position is not in an overlay string, but in another
2439 string. This can only be a string from a `display' property.
2440 IT should already be filled with that string. */
2441 it->current.string_pos = pos->string_pos;
2442 xassert (STRINGP (it->string));
2445 /* Restore position in display vector translations, control
2446 character translations or ellipses. */
2447 if (pos->dpvec_index >= 0)
2449 if (it->dpvec == NULL)
2450 get_next_display_element (it);
2451 xassert (it->dpvec && it->current.dpvec_index == 0);
2452 it->current.dpvec_index = pos->dpvec_index;
2455 CHECK_IT (it);
2456 return !overlay_strings_with_newlines;
2460 /* Initialize IT for stepping through current_buffer in window W
2461 starting at ROW->start. */
2463 static void
2464 init_to_row_start (it, w, row)
2465 struct it *it;
2466 struct window *w;
2467 struct glyph_row *row;
2469 init_from_display_pos (it, w, &row->start);
2470 it->start = row->start;
2471 it->continuation_lines_width = row->continuation_lines_width;
2472 CHECK_IT (it);
2476 /* Initialize IT for stepping through current_buffer in window W
2477 starting in the line following ROW, i.e. starting at ROW->end.
2478 Value is zero if there are overlay strings with newlines at ROW's
2479 end position. */
2481 static int
2482 init_to_row_end (it, w, row)
2483 struct it *it;
2484 struct window *w;
2485 struct glyph_row *row;
2487 int success = 0;
2489 if (init_from_display_pos (it, w, &row->end))
2491 if (row->continued_p)
2492 it->continuation_lines_width
2493 = row->continuation_lines_width + row->pixel_width;
2494 CHECK_IT (it);
2495 success = 1;
2498 return success;
2504 /***********************************************************************
2505 Text properties
2506 ***********************************************************************/
2508 /* Called when IT reaches IT->stop_charpos. Handle text property and
2509 overlay changes. Set IT->stop_charpos to the next position where
2510 to stop. */
2512 static void
2513 handle_stop (it)
2514 struct it *it;
2516 enum prop_handled handled;
2517 int handle_overlay_change_p = 1;
2518 struct props *p;
2520 it->dpvec = NULL;
2521 it->current.dpvec_index = -1;
2525 handled = HANDLED_NORMALLY;
2527 /* Call text property handlers. */
2528 for (p = it_props; p->handler; ++p)
2530 handled = p->handler (it);
2532 if (handled == HANDLED_RECOMPUTE_PROPS)
2533 break;
2534 else if (handled == HANDLED_RETURN)
2535 return;
2536 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2537 handle_overlay_change_p = 0;
2540 if (handled != HANDLED_RECOMPUTE_PROPS)
2542 /* Don't check for overlay strings below when set to deliver
2543 characters from a display vector. */
2544 if (it->method == next_element_from_display_vector)
2545 handle_overlay_change_p = 0;
2547 /* Handle overlay changes. */
2548 if (handle_overlay_change_p)
2549 handled = handle_overlay_change (it);
2551 /* Determine where to stop next. */
2552 if (handled == HANDLED_NORMALLY)
2553 compute_stop_pos (it);
2556 while (handled == HANDLED_RECOMPUTE_PROPS);
2560 /* Compute IT->stop_charpos from text property and overlay change
2561 information for IT's current position. */
2563 static void
2564 compute_stop_pos (it)
2565 struct it *it;
2567 register INTERVAL iv, next_iv;
2568 Lisp_Object object, limit, position;
2570 /* If nowhere else, stop at the end. */
2571 it->stop_charpos = it->end_charpos;
2573 if (STRINGP (it->string))
2575 /* Strings are usually short, so don't limit the search for
2576 properties. */
2577 object = it->string;
2578 limit = Qnil;
2579 position = make_number (IT_STRING_CHARPOS (*it));
2581 else
2583 int charpos;
2585 /* If next overlay change is in front of the current stop pos
2586 (which is IT->end_charpos), stop there. Note: value of
2587 next_overlay_change is point-max if no overlay change
2588 follows. */
2589 charpos = next_overlay_change (IT_CHARPOS (*it));
2590 if (charpos < it->stop_charpos)
2591 it->stop_charpos = charpos;
2593 /* If showing the region, we have to stop at the region
2594 start or end because the face might change there. */
2595 if (it->region_beg_charpos > 0)
2597 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2598 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2599 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2600 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2603 /* Set up variables for computing the stop position from text
2604 property changes. */
2605 XSETBUFFER (object, current_buffer);
2606 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2607 position = make_number (IT_CHARPOS (*it));
2611 /* Get the interval containing IT's position. Value is a null
2612 interval if there isn't such an interval. */
2613 iv = validate_interval_range (object, &position, &position, 0);
2614 if (!NULL_INTERVAL_P (iv))
2616 Lisp_Object values_here[LAST_PROP_IDX];
2617 struct props *p;
2619 /* Get properties here. */
2620 for (p = it_props; p->handler; ++p)
2621 values_here[p->idx] = textget (iv->plist, *p->name);
2623 /* Look for an interval following iv that has different
2624 properties. */
2625 for (next_iv = next_interval (iv);
2626 (!NULL_INTERVAL_P (next_iv)
2627 && (NILP (limit)
2628 || XFASTINT (limit) > next_iv->position));
2629 next_iv = next_interval (next_iv))
2631 for (p = it_props; p->handler; ++p)
2633 Lisp_Object new_value;
2635 new_value = textget (next_iv->plist, *p->name);
2636 if (!EQ (values_here[p->idx], new_value))
2637 break;
2640 if (p->handler)
2641 break;
2644 if (!NULL_INTERVAL_P (next_iv))
2646 if (INTEGERP (limit)
2647 && next_iv->position >= XFASTINT (limit))
2648 /* No text property change up to limit. */
2649 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2650 else
2651 /* Text properties change in next_iv. */
2652 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2656 xassert (STRINGP (it->string)
2657 || (it->stop_charpos >= BEGV
2658 && it->stop_charpos >= IT_CHARPOS (*it)));
2662 /* Return the position of the next overlay change after POS in
2663 current_buffer. Value is point-max if no overlay change
2664 follows. This is like `next-overlay-change' but doesn't use
2665 xmalloc. */
2667 static int
2668 next_overlay_change (pos)
2669 int pos;
2671 int noverlays;
2672 int endpos;
2673 Lisp_Object *overlays;
2674 int len;
2675 int i;
2677 /* Get all overlays at the given position. */
2678 len = 10;
2679 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2680 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2681 if (noverlays > len)
2683 len = noverlays;
2684 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2685 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2688 /* If any of these overlays ends before endpos,
2689 use its ending point instead. */
2690 for (i = 0; i < noverlays; ++i)
2692 Lisp_Object oend;
2693 int oendpos;
2695 oend = OVERLAY_END (overlays[i]);
2696 oendpos = OVERLAY_POSITION (oend);
2697 endpos = min (endpos, oendpos);
2700 return endpos;
2705 /***********************************************************************
2706 Fontification
2707 ***********************************************************************/
2709 /* Handle changes in the `fontified' property of the current buffer by
2710 calling hook functions from Qfontification_functions to fontify
2711 regions of text. */
2713 static enum prop_handled
2714 handle_fontified_prop (it)
2715 struct it *it;
2717 Lisp_Object prop, pos;
2718 enum prop_handled handled = HANDLED_NORMALLY;
2720 /* Get the value of the `fontified' property at IT's current buffer
2721 position. (The `fontified' property doesn't have a special
2722 meaning in strings.) If the value is nil, call functions from
2723 Qfontification_functions. */
2724 if (!STRINGP (it->string)
2725 && it->s == NULL
2726 && !NILP (Vfontification_functions)
2727 && !NILP (Vrun_hooks)
2728 && (pos = make_number (IT_CHARPOS (*it)),
2729 prop = Fget_char_property (pos, Qfontified, Qnil),
2730 NILP (prop)))
2732 int count = SPECPDL_INDEX ();
2733 Lisp_Object val;
2735 val = Vfontification_functions;
2736 specbind (Qfontification_functions, Qnil);
2738 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2739 safe_call1 (val, pos);
2740 else
2742 Lisp_Object globals, fn;
2743 struct gcpro gcpro1, gcpro2;
2745 globals = Qnil;
2746 GCPRO2 (val, globals);
2748 for (; CONSP (val); val = XCDR (val))
2750 fn = XCAR (val);
2752 if (EQ (fn, Qt))
2754 /* A value of t indicates this hook has a local
2755 binding; it means to run the global binding too.
2756 In a global value, t should not occur. If it
2757 does, we must ignore it to avoid an endless
2758 loop. */
2759 for (globals = Fdefault_value (Qfontification_functions);
2760 CONSP (globals);
2761 globals = XCDR (globals))
2763 fn = XCAR (globals);
2764 if (!EQ (fn, Qt))
2765 safe_call1 (fn, pos);
2768 else
2769 safe_call1 (fn, pos);
2772 UNGCPRO;
2775 unbind_to (count, Qnil);
2777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2778 something. This avoids an endless loop if they failed to
2779 fontify the text for which reason ever. */
2780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2781 handled = HANDLED_RECOMPUTE_PROPS;
2784 return handled;
2789 /***********************************************************************
2790 Faces
2791 ***********************************************************************/
2793 /* Set up iterator IT from face properties at its current position.
2794 Called from handle_stop. */
2796 static enum prop_handled
2797 handle_face_prop (it)
2798 struct it *it;
2800 int new_face_id, next_stop;
2802 if (!STRINGP (it->string))
2804 new_face_id
2805 = face_at_buffer_position (it->w,
2806 IT_CHARPOS (*it),
2807 it->region_beg_charpos,
2808 it->region_end_charpos,
2809 &next_stop,
2810 (IT_CHARPOS (*it)
2811 + TEXT_PROP_DISTANCE_LIMIT),
2814 /* Is this a start of a run of characters with box face?
2815 Caveat: this can be called for a freshly initialized
2816 iterator; face_id is -1 in this case. We know that the new
2817 face will not change until limit, i.e. if the new face has a
2818 box, all characters up to limit will have one. But, as
2819 usual, we don't know whether limit 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);
2824 /* If new face has a box but old face has not, this is
2825 the start of a run of characters with box, i.e. it has
2826 a shadow on the left side. The value of face_id of the
2827 iterator will be -1 if this is the initial call that gets
2828 the face. In this case, we have to look in front of IT's
2829 position and see whether there is a face != new_face_id. */
2830 it->start_of_box_run_p
2831 = (new_face->box != FACE_NO_BOX
2832 && (it->face_id >= 0
2833 || IT_CHARPOS (*it) == BEG
2834 || new_face_id != face_before_it_pos (it)));
2835 it->face_box_p = new_face->box != FACE_NO_BOX;
2838 else
2840 int base_face_id, bufpos;
2842 if (it->current.overlay_string_index >= 0)
2843 bufpos = IT_CHARPOS (*it);
2844 else
2845 bufpos = 0;
2847 /* For strings from a buffer, i.e. overlay strings or strings
2848 from a `display' property, use the face at IT's current
2849 buffer position as the base face to merge with, so that
2850 overlay strings appear in the same face as surrounding
2851 text, unless they specify their own faces. */
2852 base_face_id = underlying_face_id (it);
2854 new_face_id = face_at_string_position (it->w,
2855 it->string,
2856 IT_STRING_CHARPOS (*it),
2857 bufpos,
2858 it->region_beg_charpos,
2859 it->region_end_charpos,
2860 &next_stop,
2861 base_face_id, 0);
2863 #if 0 /* This shouldn't be neccessary. Let's check it. */
2864 /* If IT is used to display a mode line we would really like to
2865 use the mode line face instead of the frame's default face. */
2866 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2867 && new_face_id == DEFAULT_FACE_ID)
2868 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2869 #endif
2871 /* Is this a start of a run of characters with box? Caveat:
2872 this can be called for a freshly allocated iterator; face_id
2873 is -1 is this case. We know that the new face will not
2874 change until the next check pos, i.e. if the new face has a
2875 box, all characters up to that position will have a
2876 box. But, as usual, we don't know whether that position
2877 is really the end. */
2878 if (new_face_id != it->face_id)
2880 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2881 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2883 /* If new face has a box but old face hasn't, this is the
2884 start of a run of characters with box, i.e. it has a
2885 shadow on the left side. */
2886 it->start_of_box_run_p
2887 = new_face->box && (old_face == NULL || !old_face->box);
2888 it->face_box_p = new_face->box != FACE_NO_BOX;
2892 it->face_id = new_face_id;
2893 return HANDLED_NORMALLY;
2897 /* Return the ID of the face ``underlying'' IT's current position,
2898 which is in a string. If the iterator is associated with a
2899 buffer, return the face at IT's current buffer position.
2900 Otherwise, use the iterator's base_face_id. */
2902 static int
2903 underlying_face_id (it)
2904 struct it *it;
2906 int face_id = it->base_face_id, i;
2908 xassert (STRINGP (it->string));
2910 for (i = it->sp - 1; i >= 0; --i)
2911 if (NILP (it->stack[i].string))
2912 face_id = it->stack[i].face_id;
2914 return face_id;
2918 /* Compute the face one character before or after the current position
2919 of IT. BEFORE_P non-zero means get the face in front of IT's
2920 position. Value is the id of the face. */
2922 static int
2923 face_before_or_after_it_pos (it, before_p)
2924 struct it *it;
2925 int before_p;
2927 int face_id, limit;
2928 int next_check_charpos;
2929 struct text_pos pos;
2931 xassert (it->s == NULL);
2933 if (STRINGP (it->string))
2935 int bufpos, base_face_id;
2937 /* No face change past the end of the string (for the case
2938 we are padding with spaces). No face change before the
2939 string start. */
2940 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2941 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2942 return it->face_id;
2944 /* Set pos to the position before or after IT's current position. */
2945 if (before_p)
2946 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2947 else
2948 /* For composition, we must check the character after the
2949 composition. */
2950 pos = (it->what == IT_COMPOSITION
2951 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2952 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2954 if (it->current.overlay_string_index >= 0)
2955 bufpos = IT_CHARPOS (*it);
2956 else
2957 bufpos = 0;
2959 base_face_id = underlying_face_id (it);
2961 /* Get the face for ASCII, or unibyte. */
2962 face_id = face_at_string_position (it->w,
2963 it->string,
2964 CHARPOS (pos),
2965 bufpos,
2966 it->region_beg_charpos,
2967 it->region_end_charpos,
2968 &next_check_charpos,
2969 base_face_id, 0);
2971 /* Correct the face for charsets different from ASCII. Do it
2972 for the multibyte case only. The face returned above is
2973 suitable for unibyte text if IT->string is unibyte. */
2974 if (STRING_MULTIBYTE (it->string))
2976 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2977 int rest = SBYTES (it->string) - BYTEPOS (pos);
2978 int c, len;
2979 struct face *face = FACE_FROM_ID (it->f, face_id);
2981 c = string_char_and_length (p, rest, &len);
2982 face_id = FACE_FOR_CHAR (it->f, face, c);
2985 else
2987 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2988 || (IT_CHARPOS (*it) <= BEGV && before_p))
2989 return it->face_id;
2991 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2992 pos = it->current.pos;
2994 if (before_p)
2995 DEC_TEXT_POS (pos, it->multibyte_p);
2996 else
2998 if (it->what == IT_COMPOSITION)
2999 /* For composition, we must check the position after the
3000 composition. */
3001 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3002 else
3003 INC_TEXT_POS (pos, it->multibyte_p);
3006 /* Determine face for CHARSET_ASCII, or unibyte. */
3007 face_id = face_at_buffer_position (it->w,
3008 CHARPOS (pos),
3009 it->region_beg_charpos,
3010 it->region_end_charpos,
3011 &next_check_charpos,
3012 limit, 0);
3014 /* Correct the face for charsets different from ASCII. Do it
3015 for the multibyte case only. The face returned above is
3016 suitable for unibyte text if current_buffer is unibyte. */
3017 if (it->multibyte_p)
3019 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3020 struct face *face = FACE_FROM_ID (it->f, face_id);
3021 face_id = FACE_FOR_CHAR (it->f, face, c);
3025 return face_id;
3030 /***********************************************************************
3031 Invisible text
3032 ***********************************************************************/
3034 /* Set up iterator IT from invisible properties at its current
3035 position. Called from handle_stop. */
3037 static enum prop_handled
3038 handle_invisible_prop (it)
3039 struct it *it;
3041 enum prop_handled handled = HANDLED_NORMALLY;
3043 if (STRINGP (it->string))
3045 extern Lisp_Object Qinvisible;
3046 Lisp_Object prop, end_charpos, limit, charpos;
3048 /* Get the value of the invisible text property at the
3049 current position. Value will be nil if there is no such
3050 property. */
3051 charpos = make_number (IT_STRING_CHARPOS (*it));
3052 prop = Fget_text_property (charpos, Qinvisible, it->string);
3054 if (!NILP (prop)
3055 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3057 handled = HANDLED_RECOMPUTE_PROPS;
3059 /* Get the position at which the next change of the
3060 invisible text property can be found in IT->string.
3061 Value will be nil if the property value is the same for
3062 all the rest of IT->string. */
3063 XSETINT (limit, SCHARS (it->string));
3064 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3065 it->string, limit);
3067 /* Text at current position is invisible. The next
3068 change in the property is at position end_charpos.
3069 Move IT's current position to that position. */
3070 if (INTEGERP (end_charpos)
3071 && XFASTINT (end_charpos) < XFASTINT (limit))
3073 struct text_pos old;
3074 old = it->current.string_pos;
3075 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3076 compute_string_pos (&it->current.string_pos, old, it->string);
3078 else
3080 /* The rest of the string is invisible. If this is an
3081 overlay string, proceed with the next overlay string
3082 or whatever comes and return a character from there. */
3083 if (it->current.overlay_string_index >= 0)
3085 next_overlay_string (it);
3086 /* Don't check for overlay strings when we just
3087 finished processing them. */
3088 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3090 else
3092 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3093 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3098 else
3100 int invis_p, newpos, next_stop, start_charpos;
3101 Lisp_Object pos, prop, overlay;
3103 /* First of all, is there invisible text at this position? */
3104 start_charpos = IT_CHARPOS (*it);
3105 pos = make_number (IT_CHARPOS (*it));
3106 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3107 &overlay);
3108 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3110 /* If we are on invisible text, skip over it. */
3111 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3113 /* Record whether we have to display an ellipsis for the
3114 invisible text. */
3115 int display_ellipsis_p = invis_p == 2;
3117 handled = HANDLED_RECOMPUTE_PROPS;
3119 /* Loop skipping over invisible text. The loop is left at
3120 ZV or with IT on the first char being visible again. */
3123 /* Try to skip some invisible text. Return value is the
3124 position reached which can be equal to IT's position
3125 if there is nothing invisible here. This skips both
3126 over invisible text properties and overlays with
3127 invisible property. */
3128 newpos = skip_invisible (IT_CHARPOS (*it),
3129 &next_stop, ZV, it->window);
3131 /* If we skipped nothing at all we weren't at invisible
3132 text in the first place. If everything to the end of
3133 the buffer was skipped, end the loop. */
3134 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3135 invis_p = 0;
3136 else
3138 /* We skipped some characters but not necessarily
3139 all there are. Check if we ended up on visible
3140 text. Fget_char_property returns the property of
3141 the char before the given position, i.e. if we
3142 get invis_p = 0, this means that the char at
3143 newpos is visible. */
3144 pos = make_number (newpos);
3145 prop = Fget_char_property (pos, Qinvisible, it->window);
3146 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3149 /* If we ended up on invisible text, proceed to
3150 skip starting with next_stop. */
3151 if (invis_p)
3152 IT_CHARPOS (*it) = next_stop;
3154 while (invis_p);
3156 /* The position newpos is now either ZV or on visible text. */
3157 IT_CHARPOS (*it) = newpos;
3158 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3160 /* If there are before-strings at the start of invisible
3161 text, and the text is invisible because of a text
3162 property, arrange to show before-strings because 20.x did
3163 it that way. (If the text is invisible because of an
3164 overlay property instead of a text property, this is
3165 already handled in the overlay code.) */
3166 if (NILP (overlay)
3167 && get_overlay_strings (it, start_charpos))
3169 handled = HANDLED_RECOMPUTE_PROPS;
3170 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3172 else if (display_ellipsis_p)
3173 setup_for_ellipsis (it);
3177 return handled;
3181 /* Make iterator IT return `...' next. */
3183 static void
3184 setup_for_ellipsis (it)
3185 struct it *it;
3187 if (it->dp
3188 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3190 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3191 it->dpvec = v->contents;
3192 it->dpend = v->contents + v->size;
3194 else
3196 /* Default `...'. */
3197 it->dpvec = default_invis_vector;
3198 it->dpend = default_invis_vector + 3;
3201 /* The ellipsis display does not replace the display of the
3202 character at the new position. Indicate this by setting
3203 IT->dpvec_char_len to zero. */
3204 it->dpvec_char_len = 0;
3206 it->current.dpvec_index = 0;
3207 it->method = next_element_from_display_vector;
3212 /***********************************************************************
3213 'display' property
3214 ***********************************************************************/
3216 /* Set up iterator IT from `display' property at its current position.
3217 Called from handle_stop. */
3219 static enum prop_handled
3220 handle_display_prop (it)
3221 struct it *it;
3223 Lisp_Object prop, object;
3224 struct text_pos *position;
3225 int display_replaced_p = 0;
3227 if (STRINGP (it->string))
3229 object = it->string;
3230 position = &it->current.string_pos;
3232 else
3234 object = it->w->buffer;
3235 position = &it->current.pos;
3238 /* Reset those iterator values set from display property values. */
3239 it->font_height = Qnil;
3240 it->space_width = Qnil;
3241 it->voffset = 0;
3243 /* We don't support recursive `display' properties, i.e. string
3244 values that have a string `display' property, that have a string
3245 `display' property etc. */
3246 if (!it->string_from_display_prop_p)
3247 it->area = TEXT_AREA;
3249 prop = Fget_char_property (make_number (position->charpos),
3250 Qdisplay, object);
3251 if (NILP (prop))
3252 return HANDLED_NORMALLY;
3254 if (CONSP (prop)
3255 /* Simple properties. */
3256 && !EQ (XCAR (prop), Qimage)
3257 && !EQ (XCAR (prop), Qspace)
3258 && !EQ (XCAR (prop), Qwhen)
3259 && !EQ (XCAR (prop), Qspace_width)
3260 && !EQ (XCAR (prop), Qheight)
3261 && !EQ (XCAR (prop), Qraise)
3262 /* Marginal area specifications. */
3263 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3264 && !NILP (XCAR (prop)))
3266 for (; CONSP (prop); prop = XCDR (prop))
3268 if (handle_single_display_prop (it, XCAR (prop), object,
3269 position, display_replaced_p))
3270 display_replaced_p = 1;
3273 else if (VECTORP (prop))
3275 int i;
3276 for (i = 0; i < ASIZE (prop); ++i)
3277 if (handle_single_display_prop (it, AREF (prop, i), object,
3278 position, display_replaced_p))
3279 display_replaced_p = 1;
3281 else
3283 if (handle_single_display_prop (it, prop, object, position, 0))
3284 display_replaced_p = 1;
3287 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3291 /* Value is the position of the end of the `display' property starting
3292 at START_POS in OBJECT. */
3294 static struct text_pos
3295 display_prop_end (it, object, start_pos)
3296 struct it *it;
3297 Lisp_Object object;
3298 struct text_pos start_pos;
3300 Lisp_Object end;
3301 struct text_pos end_pos;
3303 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3304 Qdisplay, object, Qnil);
3305 CHARPOS (end_pos) = XFASTINT (end);
3306 if (STRINGP (object))
3307 compute_string_pos (&end_pos, start_pos, it->string);
3308 else
3309 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3311 return end_pos;
3315 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3316 is the object in which the `display' property was found. *POSITION
3317 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3318 means that we previously saw a display sub-property which already
3319 replaced text display with something else, for example an image;
3320 ignore such properties after the first one has been processed.
3322 If PROP is a `space' or `image' sub-property, set *POSITION to the
3323 end position of the `display' property.
3325 Value is non-zero if something was found which replaces the display
3326 of buffer or string text. */
3328 static int
3329 handle_single_display_prop (it, prop, object, position,
3330 display_replaced_before_p)
3331 struct it *it;
3332 Lisp_Object prop;
3333 Lisp_Object object;
3334 struct text_pos *position;
3335 int display_replaced_before_p;
3337 Lisp_Object value;
3338 int replaces_text_display_p = 0;
3339 Lisp_Object form;
3341 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3342 evaluated. If the result is nil, VALUE is ignored. */
3343 form = Qt;
3344 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3346 prop = XCDR (prop);
3347 if (!CONSP (prop))
3348 return 0;
3349 form = XCAR (prop);
3350 prop = XCDR (prop);
3353 if (!NILP (form) && !EQ (form, Qt))
3355 int count = SPECPDL_INDEX ();
3356 struct gcpro gcpro1;
3358 /* Bind `object' to the object having the `display' property, a
3359 buffer or string. Bind `position' to the position in the
3360 object where the property was found, and `buffer-position'
3361 to the current position in the buffer. */
3362 specbind (Qobject, object);
3363 specbind (Qposition, make_number (CHARPOS (*position)));
3364 specbind (Qbuffer_position,
3365 make_number (STRINGP (object)
3366 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3367 GCPRO1 (form);
3368 form = safe_eval (form);
3369 UNGCPRO;
3370 unbind_to (count, Qnil);
3373 if (NILP (form))
3374 return 0;
3376 if (CONSP (prop)
3377 && EQ (XCAR (prop), Qheight)
3378 && CONSP (XCDR (prop)))
3380 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3381 return 0;
3383 /* `(height HEIGHT)'. */
3384 it->font_height = XCAR (XCDR (prop));
3385 if (!NILP (it->font_height))
3387 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3388 int new_height = -1;
3390 if (CONSP (it->font_height)
3391 && (EQ (XCAR (it->font_height), Qplus)
3392 || EQ (XCAR (it->font_height), Qminus))
3393 && CONSP (XCDR (it->font_height))
3394 && INTEGERP (XCAR (XCDR (it->font_height))))
3396 /* `(+ N)' or `(- N)' where N is an integer. */
3397 int steps = XINT (XCAR (XCDR (it->font_height)));
3398 if (EQ (XCAR (it->font_height), Qplus))
3399 steps = - steps;
3400 it->face_id = smaller_face (it->f, it->face_id, steps);
3402 else if (FUNCTIONP (it->font_height))
3404 /* Call function with current height as argument.
3405 Value is the new height. */
3406 Lisp_Object height;
3407 height = safe_call1 (it->font_height,
3408 face->lface[LFACE_HEIGHT_INDEX]);
3409 if (NUMBERP (height))
3410 new_height = XFLOATINT (height);
3412 else if (NUMBERP (it->font_height))
3414 /* Value is a multiple of the canonical char height. */
3415 struct face *face;
3417 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3418 new_height = (XFLOATINT (it->font_height)
3419 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3421 else
3423 /* Evaluate IT->font_height with `height' bound to the
3424 current specified height to get the new height. */
3425 Lisp_Object value;
3426 int count = SPECPDL_INDEX ();
3428 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3429 value = safe_eval (it->font_height);
3430 unbind_to (count, Qnil);
3432 if (NUMBERP (value))
3433 new_height = XFLOATINT (value);
3436 if (new_height > 0)
3437 it->face_id = face_with_height (it->f, it->face_id, new_height);
3440 else if (CONSP (prop)
3441 && EQ (XCAR (prop), Qspace_width)
3442 && CONSP (XCDR (prop)))
3444 /* `(space_width WIDTH)'. */
3445 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3446 return 0;
3448 value = XCAR (XCDR (prop));
3449 if (NUMBERP (value) && XFLOATINT (value) > 0)
3450 it->space_width = value;
3452 else if (CONSP (prop)
3453 && EQ (XCAR (prop), Qraise)
3454 && CONSP (XCDR (prop)))
3456 /* `(raise FACTOR)'. */
3457 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3458 return 0;
3460 #ifdef HAVE_WINDOW_SYSTEM
3461 value = XCAR (XCDR (prop));
3462 if (NUMBERP (value))
3464 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3465 it->voffset = - (XFLOATINT (value)
3466 * (FONT_HEIGHT (face->font)));
3468 #endif /* HAVE_WINDOW_SYSTEM */
3470 else if (!it->string_from_display_prop_p)
3472 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3473 VALUE) or `((margin nil) VALUE)' or VALUE. */
3474 Lisp_Object location, value;
3475 struct text_pos start_pos;
3476 int valid_p;
3478 /* Characters having this form of property are not displayed, so
3479 we have to find the end of the property. */
3480 start_pos = *position;
3481 *position = display_prop_end (it, object, start_pos);
3482 value = Qnil;
3484 /* Let's stop at the new position and assume that all
3485 text properties change there. */
3486 it->stop_charpos = position->charpos;
3488 location = Qunbound;
3489 if (CONSP (prop) && CONSP (XCAR (prop)))
3491 Lisp_Object tem;
3493 value = XCDR (prop);
3494 if (CONSP (value))
3495 value = XCAR (value);
3497 tem = XCAR (prop);
3498 if (EQ (XCAR (tem), Qmargin)
3499 && (tem = XCDR (tem),
3500 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3501 (NILP (tem)
3502 || EQ (tem, Qleft_margin)
3503 || EQ (tem, Qright_margin))))
3504 location = tem;
3507 if (EQ (location, Qunbound))
3509 location = Qnil;
3510 value = prop;
3513 #ifdef HAVE_WINDOW_SYSTEM
3514 if (FRAME_TERMCAP_P (it->f))
3515 valid_p = STRINGP (value);
3516 else
3517 valid_p = (STRINGP (value)
3518 || (CONSP (value) && EQ (XCAR (value), Qspace))
3519 || valid_image_p (value));
3520 #else /* not HAVE_WINDOW_SYSTEM */
3521 valid_p = STRINGP (value);
3522 #endif /* not HAVE_WINDOW_SYSTEM */
3524 if ((EQ (location, Qleft_margin)
3525 || EQ (location, Qright_margin)
3526 || NILP (location))
3527 && valid_p
3528 && !display_replaced_before_p)
3530 replaces_text_display_p = 1;
3532 /* Save current settings of IT so that we can restore them
3533 when we are finished with the glyph property value. */
3534 push_it (it);
3536 if (NILP (location))
3537 it->area = TEXT_AREA;
3538 else if (EQ (location, Qleft_margin))
3539 it->area = LEFT_MARGIN_AREA;
3540 else
3541 it->area = RIGHT_MARGIN_AREA;
3543 if (STRINGP (value))
3545 it->string = value;
3546 it->multibyte_p = STRING_MULTIBYTE (it->string);
3547 it->current.overlay_string_index = -1;
3548 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3549 it->end_charpos = it->string_nchars = SCHARS (it->string);
3550 it->method = next_element_from_string;
3551 it->stop_charpos = 0;
3552 it->string_from_display_prop_p = 1;
3553 /* Say that we haven't consumed the characters with
3554 `display' property yet. The call to pop_it in
3555 set_iterator_to_next will clean this up. */
3556 *position = start_pos;
3558 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3560 it->method = next_element_from_stretch;
3561 it->object = value;
3562 it->current.pos = it->position = start_pos;
3564 #ifdef HAVE_WINDOW_SYSTEM
3565 else
3567 it->what = IT_IMAGE;
3568 it->image_id = lookup_image (it->f, value);
3569 it->position = start_pos;
3570 it->object = NILP (object) ? it->w->buffer : object;
3571 it->method = next_element_from_image;
3573 /* Say that we haven't consumed the characters with
3574 `display' property yet. The call to pop_it in
3575 set_iterator_to_next will clean this up. */
3576 *position = start_pos;
3578 #endif /* HAVE_WINDOW_SYSTEM */
3580 else
3581 /* Invalid property or property not supported. Restore
3582 the position to what it was before. */
3583 *position = start_pos;
3586 return replaces_text_display_p;
3590 /* Check if PROP is a display sub-property value whose text should be
3591 treated as intangible. */
3593 static int
3594 single_display_prop_intangible_p (prop)
3595 Lisp_Object prop;
3597 /* Skip over `when FORM'. */
3598 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3600 prop = XCDR (prop);
3601 if (!CONSP (prop))
3602 return 0;
3603 prop = XCDR (prop);
3606 if (STRINGP (prop))
3607 return 1;
3609 if (!CONSP (prop))
3610 return 0;
3612 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3613 we don't need to treat text as intangible. */
3614 if (EQ (XCAR (prop), Qmargin))
3616 prop = XCDR (prop);
3617 if (!CONSP (prop))
3618 return 0;
3620 prop = XCDR (prop);
3621 if (!CONSP (prop)
3622 || EQ (XCAR (prop), Qleft_margin)
3623 || EQ (XCAR (prop), Qright_margin))
3624 return 0;
3627 return (CONSP (prop)
3628 && (EQ (XCAR (prop), Qimage)
3629 || EQ (XCAR (prop), Qspace)));
3633 /* Check if PROP is a display property value whose text should be
3634 treated as intangible. */
3637 display_prop_intangible_p (prop)
3638 Lisp_Object prop;
3640 if (CONSP (prop)
3641 && CONSP (XCAR (prop))
3642 && !EQ (Qmargin, XCAR (XCAR (prop))))
3644 /* A list of sub-properties. */
3645 while (CONSP (prop))
3647 if (single_display_prop_intangible_p (XCAR (prop)))
3648 return 1;
3649 prop = XCDR (prop);
3652 else if (VECTORP (prop))
3654 /* A vector of sub-properties. */
3655 int i;
3656 for (i = 0; i < ASIZE (prop); ++i)
3657 if (single_display_prop_intangible_p (AREF (prop, i)))
3658 return 1;
3660 else
3661 return single_display_prop_intangible_p (prop);
3663 return 0;
3667 /* Return 1 if PROP is a display sub-property value containing STRING. */
3669 static int
3670 single_display_prop_string_p (prop, string)
3671 Lisp_Object prop, string;
3673 if (EQ (string, prop))
3674 return 1;
3676 /* Skip over `when FORM'. */
3677 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3679 prop = XCDR (prop);
3680 if (!CONSP (prop))
3681 return 0;
3682 prop = XCDR (prop);
3685 if (CONSP (prop))
3686 /* Skip over `margin LOCATION'. */
3687 if (EQ (XCAR (prop), Qmargin))
3689 prop = XCDR (prop);
3690 if (!CONSP (prop))
3691 return 0;
3693 prop = XCDR (prop);
3694 if (!CONSP (prop))
3695 return 0;
3698 return CONSP (prop) && EQ (XCAR (prop), string);
3702 /* Return 1 if STRING appears in the `display' property PROP. */
3704 static int
3705 display_prop_string_p (prop, string)
3706 Lisp_Object prop, string;
3708 if (CONSP (prop)
3709 && CONSP (XCAR (prop))
3710 && !EQ (Qmargin, XCAR (XCAR (prop))))
3712 /* A list of sub-properties. */
3713 while (CONSP (prop))
3715 if (single_display_prop_string_p (XCAR (prop), string))
3716 return 1;
3717 prop = XCDR (prop);
3720 else if (VECTORP (prop))
3722 /* A vector of sub-properties. */
3723 int i;
3724 for (i = 0; i < ASIZE (prop); ++i)
3725 if (single_display_prop_string_p (AREF (prop, i), string))
3726 return 1;
3728 else
3729 return single_display_prop_string_p (prop, string);
3731 return 0;
3735 /* Determine from which buffer position in W's buffer STRING comes
3736 from. AROUND_CHARPOS is an approximate position where it could
3737 be from. Value is the buffer position or 0 if it couldn't be
3738 determined.
3740 W's buffer must be current.
3742 This function is necessary because we don't record buffer positions
3743 in glyphs generated from strings (to keep struct glyph small).
3744 This function may only use code that doesn't eval because it is
3745 called asynchronously from note_mouse_highlight. */
3748 string_buffer_position (w, string, around_charpos)
3749 struct window *w;
3750 Lisp_Object string;
3751 int around_charpos;
3753 Lisp_Object limit, prop, pos;
3754 const int MAX_DISTANCE = 1000;
3755 int found = 0;
3757 pos = make_number (around_charpos);
3758 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3759 while (!found && !EQ (pos, limit))
3761 prop = Fget_char_property (pos, Qdisplay, Qnil);
3762 if (!NILP (prop) && display_prop_string_p (prop, string))
3763 found = 1;
3764 else
3765 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3768 if (!found)
3770 pos = make_number (around_charpos);
3771 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3772 while (!found && !EQ (pos, limit))
3774 prop = Fget_char_property (pos, Qdisplay, Qnil);
3775 if (!NILP (prop) && display_prop_string_p (prop, string))
3776 found = 1;
3777 else
3778 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3779 limit);
3783 return found ? XINT (pos) : 0;
3788 /***********************************************************************
3789 `composition' property
3790 ***********************************************************************/
3792 /* Set up iterator IT from `composition' property at its current
3793 position. Called from handle_stop. */
3795 static enum prop_handled
3796 handle_composition_prop (it)
3797 struct it *it;
3799 Lisp_Object prop, string;
3800 int pos, pos_byte, end;
3801 enum prop_handled handled = HANDLED_NORMALLY;
3803 if (STRINGP (it->string))
3805 pos = IT_STRING_CHARPOS (*it);
3806 pos_byte = IT_STRING_BYTEPOS (*it);
3807 string = it->string;
3809 else
3811 pos = IT_CHARPOS (*it);
3812 pos_byte = IT_BYTEPOS (*it);
3813 string = Qnil;
3816 /* If there's a valid composition and point is not inside of the
3817 composition (in the case that the composition is from the current
3818 buffer), draw a glyph composed from the composition components. */
3819 if (find_composition (pos, -1, &pos, &end, &prop, string)
3820 && COMPOSITION_VALID_P (pos, end, prop)
3821 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3823 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3825 if (id >= 0)
3827 it->method = next_element_from_composition;
3828 it->cmp_id = id;
3829 it->cmp_len = COMPOSITION_LENGTH (prop);
3830 /* For a terminal, draw only the first character of the
3831 components. */
3832 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3833 it->len = (STRINGP (it->string)
3834 ? string_char_to_byte (it->string, end)
3835 : CHAR_TO_BYTE (end)) - pos_byte;
3836 it->stop_charpos = end;
3837 handled = HANDLED_RETURN;
3841 return handled;
3846 /***********************************************************************
3847 Overlay strings
3848 ***********************************************************************/
3850 /* The following structure is used to record overlay strings for
3851 later sorting in load_overlay_strings. */
3853 struct overlay_entry
3855 Lisp_Object overlay;
3856 Lisp_Object string;
3857 int priority;
3858 int after_string_p;
3862 /* Set up iterator IT from overlay strings at its current position.
3863 Called from handle_stop. */
3865 static enum prop_handled
3866 handle_overlay_change (it)
3867 struct it *it;
3869 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3870 return HANDLED_RECOMPUTE_PROPS;
3871 else
3872 return HANDLED_NORMALLY;
3876 /* Set up the next overlay string for delivery by IT, if there is an
3877 overlay string to deliver. Called by set_iterator_to_next when the
3878 end of the current overlay string is reached. If there are more
3879 overlay strings to display, IT->string and
3880 IT->current.overlay_string_index are set appropriately here.
3881 Otherwise IT->string is set to nil. */
3883 static void
3884 next_overlay_string (it)
3885 struct it *it;
3887 ++it->current.overlay_string_index;
3888 if (it->current.overlay_string_index == it->n_overlay_strings)
3890 /* No more overlay strings. Restore IT's settings to what
3891 they were before overlay strings were processed, and
3892 continue to deliver from current_buffer. */
3893 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3895 pop_it (it);
3896 xassert (it->stop_charpos >= BEGV
3897 && it->stop_charpos <= it->end_charpos);
3898 it->string = Qnil;
3899 it->current.overlay_string_index = -1;
3900 SET_TEXT_POS (it->current.string_pos, -1, -1);
3901 it->n_overlay_strings = 0;
3902 it->method = next_element_from_buffer;
3904 /* If we're at the end of the buffer, record that we have
3905 processed the overlay strings there already, so that
3906 next_element_from_buffer doesn't try it again. */
3907 if (IT_CHARPOS (*it) >= it->end_charpos)
3908 it->overlay_strings_at_end_processed_p = 1;
3910 /* If we have to display `...' for invisible text, set
3911 the iterator up for that. */
3912 if (display_ellipsis_p)
3913 setup_for_ellipsis (it);
3915 else
3917 /* There are more overlay strings to process. If
3918 IT->current.overlay_string_index has advanced to a position
3919 where we must load IT->overlay_strings with more strings, do
3920 it. */
3921 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3923 if (it->current.overlay_string_index && i == 0)
3924 load_overlay_strings (it, 0);
3926 /* Initialize IT to deliver display elements from the overlay
3927 string. */
3928 it->string = it->overlay_strings[i];
3929 it->multibyte_p = STRING_MULTIBYTE (it->string);
3930 SET_TEXT_POS (it->current.string_pos, 0, 0);
3931 it->method = next_element_from_string;
3932 it->stop_charpos = 0;
3935 CHECK_IT (it);
3939 /* Compare two overlay_entry structures E1 and E2. Used as a
3940 comparison function for qsort in load_overlay_strings. Overlay
3941 strings for the same position are sorted so that
3943 1. All after-strings come in front of before-strings, except
3944 when they come from the same overlay.
3946 2. Within after-strings, strings are sorted so that overlay strings
3947 from overlays with higher priorities come first.
3949 2. Within before-strings, strings are sorted so that overlay
3950 strings from overlays with higher priorities come last.
3952 Value is analogous to strcmp. */
3955 static int
3956 compare_overlay_entries (e1, e2)
3957 void *e1, *e2;
3959 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3960 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3961 int result;
3963 if (entry1->after_string_p != entry2->after_string_p)
3965 /* Let after-strings appear in front of before-strings if
3966 they come from different overlays. */
3967 if (EQ (entry1->overlay, entry2->overlay))
3968 result = entry1->after_string_p ? 1 : -1;
3969 else
3970 result = entry1->after_string_p ? -1 : 1;
3972 else if (entry1->after_string_p)
3973 /* After-strings sorted in order of decreasing priority. */
3974 result = entry2->priority - entry1->priority;
3975 else
3976 /* Before-strings sorted in order of increasing priority. */
3977 result = entry1->priority - entry2->priority;
3979 return result;
3983 /* Load the vector IT->overlay_strings with overlay strings from IT's
3984 current buffer position, or from CHARPOS if that is > 0. Set
3985 IT->n_overlays to the total number of overlay strings found.
3987 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3988 a time. On entry into load_overlay_strings,
3989 IT->current.overlay_string_index gives the number of overlay
3990 strings that have already been loaded by previous calls to this
3991 function.
3993 IT->add_overlay_start contains an additional overlay start
3994 position to consider for taking overlay strings from, if non-zero.
3995 This position comes into play when the overlay has an `invisible'
3996 property, and both before and after-strings. When we've skipped to
3997 the end of the overlay, because of its `invisible' property, we
3998 nevertheless want its before-string to appear.
3999 IT->add_overlay_start will contain the overlay start position
4000 in this case.
4002 Overlay strings are sorted so that after-string strings come in
4003 front of before-string strings. Within before and after-strings,
4004 strings are sorted by overlay priority. See also function
4005 compare_overlay_entries. */
4007 static void
4008 load_overlay_strings (it, charpos)
4009 struct it *it;
4010 int charpos;
4012 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4013 Lisp_Object overlay, window, str, invisible;
4014 struct Lisp_Overlay *ov;
4015 int start, end;
4016 int size = 20;
4017 int n = 0, i, j, invis_p;
4018 struct overlay_entry *entries
4019 = (struct overlay_entry *) alloca (size * sizeof *entries);
4021 if (charpos <= 0)
4022 charpos = IT_CHARPOS (*it);
4024 /* Append the overlay string STRING of overlay OVERLAY to vector
4025 `entries' which has size `size' and currently contains `n'
4026 elements. AFTER_P non-zero means STRING is an after-string of
4027 OVERLAY. */
4028 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4029 do \
4031 Lisp_Object priority; \
4033 if (n == size) \
4035 int new_size = 2 * size; \
4036 struct overlay_entry *old = entries; \
4037 entries = \
4038 (struct overlay_entry *) alloca (new_size \
4039 * sizeof *entries); \
4040 bcopy (old, entries, size * sizeof *entries); \
4041 size = new_size; \
4044 entries[n].string = (STRING); \
4045 entries[n].overlay = (OVERLAY); \
4046 priority = Foverlay_get ((OVERLAY), Qpriority); \
4047 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4048 entries[n].after_string_p = (AFTER_P); \
4049 ++n; \
4051 while (0)
4053 /* Process overlay before the overlay center. */
4054 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4056 XSETMISC (overlay, ov);
4057 xassert (OVERLAYP (overlay));
4058 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4059 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4061 if (end < charpos)
4062 break;
4064 /* Skip this overlay if it doesn't start or end at IT's current
4065 position. */
4066 if (end != charpos && start != charpos)
4067 continue;
4069 /* Skip this overlay if it doesn't apply to IT->w. */
4070 window = Foverlay_get (overlay, Qwindow);
4071 if (WINDOWP (window) && XWINDOW (window) != it->w)
4072 continue;
4074 /* If the text ``under'' the overlay is invisible, both before-
4075 and after-strings from this overlay are visible; start and
4076 end position are indistinguishable. */
4077 invisible = Foverlay_get (overlay, Qinvisible);
4078 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4080 /* If overlay has a non-empty before-string, record it. */
4081 if ((start == charpos || (end == charpos && invis_p))
4082 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4083 && SCHARS (str))
4084 RECORD_OVERLAY_STRING (overlay, str, 0);
4086 /* If overlay has a non-empty after-string, record it. */
4087 if ((end == charpos || (start == charpos && invis_p))
4088 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4089 && SCHARS (str))
4090 RECORD_OVERLAY_STRING (overlay, str, 1);
4093 /* Process overlays after the overlay center. */
4094 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4096 XSETMISC (overlay, ov);
4097 xassert (OVERLAYP (overlay));
4098 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4099 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4101 if (start > charpos)
4102 break;
4104 /* Skip this overlay if it doesn't start or end at IT's current
4105 position. */
4106 if (end != charpos && start != charpos)
4107 continue;
4109 /* Skip this overlay if it doesn't apply to IT->w. */
4110 window = Foverlay_get (overlay, Qwindow);
4111 if (WINDOWP (window) && XWINDOW (window) != it->w)
4112 continue;
4114 /* If the text ``under'' the overlay is invisible, it has a zero
4115 dimension, and both before- and after-strings apply. */
4116 invisible = Foverlay_get (overlay, Qinvisible);
4117 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4119 /* If overlay has a non-empty before-string, record it. */
4120 if ((start == charpos || (end == charpos && invis_p))
4121 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4122 && SCHARS (str))
4123 RECORD_OVERLAY_STRING (overlay, str, 0);
4125 /* If overlay has a non-empty after-string, record it. */
4126 if ((end == charpos || (start == charpos && invis_p))
4127 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4128 && SCHARS (str))
4129 RECORD_OVERLAY_STRING (overlay, str, 1);
4132 #undef RECORD_OVERLAY_STRING
4134 /* Sort entries. */
4135 if (n > 1)
4136 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4138 /* Record the total number of strings to process. */
4139 it->n_overlay_strings = n;
4141 /* IT->current.overlay_string_index is the number of overlay strings
4142 that have already been consumed by IT. Copy some of the
4143 remaining overlay strings to IT->overlay_strings. */
4144 i = 0;
4145 j = it->current.overlay_string_index;
4146 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4147 it->overlay_strings[i++] = entries[j++].string;
4149 CHECK_IT (it);
4153 /* Get the first chunk of overlay strings at IT's current buffer
4154 position, or at CHARPOS if that is > 0. Value is non-zero if at
4155 least one overlay string was found. */
4157 static int
4158 get_overlay_strings (it, charpos)
4159 struct it *it;
4160 int charpos;
4162 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4163 process. This fills IT->overlay_strings with strings, and sets
4164 IT->n_overlay_strings to the total number of strings to process.
4165 IT->pos.overlay_string_index has to be set temporarily to zero
4166 because load_overlay_strings needs this; it must be set to -1
4167 when no overlay strings are found because a zero value would
4168 indicate a position in the first overlay string. */
4169 it->current.overlay_string_index = 0;
4170 load_overlay_strings (it, charpos);
4172 /* If we found overlay strings, set up IT to deliver display
4173 elements from the first one. Otherwise set up IT to deliver
4174 from current_buffer. */
4175 if (it->n_overlay_strings)
4177 /* Make sure we know settings in current_buffer, so that we can
4178 restore meaningful values when we're done with the overlay
4179 strings. */
4180 compute_stop_pos (it);
4181 xassert (it->face_id >= 0);
4183 /* Save IT's settings. They are restored after all overlay
4184 strings have been processed. */
4185 xassert (it->sp == 0);
4186 push_it (it);
4188 /* Set up IT to deliver display elements from the first overlay
4189 string. */
4190 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4191 it->string = it->overlay_strings[0];
4192 it->stop_charpos = 0;
4193 xassert (STRINGP (it->string));
4194 it->end_charpos = SCHARS (it->string);
4195 it->multibyte_p = STRING_MULTIBYTE (it->string);
4196 it->method = next_element_from_string;
4198 else
4200 it->string = Qnil;
4201 it->current.overlay_string_index = -1;
4202 it->method = next_element_from_buffer;
4205 CHECK_IT (it);
4207 /* Value is non-zero if we found at least one overlay string. */
4208 return STRINGP (it->string);
4213 /***********************************************************************
4214 Saving and restoring state
4215 ***********************************************************************/
4217 /* Save current settings of IT on IT->stack. Called, for example,
4218 before setting up IT for an overlay string, to be able to restore
4219 IT's settings to what they were after the overlay string has been
4220 processed. */
4222 static void
4223 push_it (it)
4224 struct it *it;
4226 struct iterator_stack_entry *p;
4228 xassert (it->sp < 2);
4229 p = it->stack + it->sp;
4231 p->stop_charpos = it->stop_charpos;
4232 xassert (it->face_id >= 0);
4233 p->face_id = it->face_id;
4234 p->string = it->string;
4235 p->pos = it->current;
4236 p->end_charpos = it->end_charpos;
4237 p->string_nchars = it->string_nchars;
4238 p->area = it->area;
4239 p->multibyte_p = it->multibyte_p;
4240 p->space_width = it->space_width;
4241 p->font_height = it->font_height;
4242 p->voffset = it->voffset;
4243 p->string_from_display_prop_p = it->string_from_display_prop_p;
4244 p->display_ellipsis_p = 0;
4245 ++it->sp;
4249 /* Restore IT's settings from IT->stack. Called, for example, when no
4250 more overlay strings must be processed, and we return to delivering
4251 display elements from a buffer, or when the end of a string from a
4252 `display' property is reached and we return to delivering display
4253 elements from an overlay string, or from a buffer. */
4255 static void
4256 pop_it (it)
4257 struct it *it;
4259 struct iterator_stack_entry *p;
4261 xassert (it->sp > 0);
4262 --it->sp;
4263 p = it->stack + it->sp;
4264 it->stop_charpos = p->stop_charpos;
4265 it->face_id = p->face_id;
4266 it->string = p->string;
4267 it->current = p->pos;
4268 it->end_charpos = p->end_charpos;
4269 it->string_nchars = p->string_nchars;
4270 it->area = p->area;
4271 it->multibyte_p = p->multibyte_p;
4272 it->space_width = p->space_width;
4273 it->font_height = p->font_height;
4274 it->voffset = p->voffset;
4275 it->string_from_display_prop_p = p->string_from_display_prop_p;
4280 /***********************************************************************
4281 Moving over lines
4282 ***********************************************************************/
4284 /* Set IT's current position to the previous line start. */
4286 static void
4287 back_to_previous_line_start (it)
4288 struct it *it;
4290 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4291 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4295 /* Move IT to the next line start.
4297 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4298 we skipped over part of the text (as opposed to moving the iterator
4299 continuously over the text). Otherwise, don't change the value
4300 of *SKIPPED_P.
4302 Newlines may come from buffer text, overlay strings, or strings
4303 displayed via the `display' property. That's the reason we can't
4304 simply use find_next_newline_no_quit.
4306 Note that this function may not skip over invisible text that is so
4307 because of text properties and immediately follows a newline. If
4308 it would, function reseat_at_next_visible_line_start, when called
4309 from set_iterator_to_next, would effectively make invisible
4310 characters following a newline part of the wrong glyph row, which
4311 leads to wrong cursor motion. */
4313 static int
4314 forward_to_next_line_start (it, skipped_p)
4315 struct it *it;
4316 int *skipped_p;
4318 int old_selective, newline_found_p, n;
4319 const int MAX_NEWLINE_DISTANCE = 500;
4321 /* If already on a newline, just consume it to avoid unintended
4322 skipping over invisible text below. */
4323 if (it->what == IT_CHARACTER
4324 && it->c == '\n'
4325 && CHARPOS (it->position) == IT_CHARPOS (*it))
4327 set_iterator_to_next (it, 0);
4328 it->c = 0;
4329 return 1;
4332 /* Don't handle selective display in the following. It's (a)
4333 unnecessary because it's done by the caller, and (b) leads to an
4334 infinite recursion because next_element_from_ellipsis indirectly
4335 calls this function. */
4336 old_selective = it->selective;
4337 it->selective = 0;
4339 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4340 from buffer text. */
4341 for (n = newline_found_p = 0;
4342 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4343 n += STRINGP (it->string) ? 0 : 1)
4345 if (!get_next_display_element (it))
4346 return 0;
4347 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4348 set_iterator_to_next (it, 0);
4351 /* If we didn't find a newline near enough, see if we can use a
4352 short-cut. */
4353 if (!newline_found_p)
4355 int start = IT_CHARPOS (*it);
4356 int limit = find_next_newline_no_quit (start, 1);
4357 Lisp_Object pos;
4359 xassert (!STRINGP (it->string));
4361 /* If there isn't any `display' property in sight, and no
4362 overlays, we can just use the position of the newline in
4363 buffer text. */
4364 if (it->stop_charpos >= limit
4365 || ((pos = Fnext_single_property_change (make_number (start),
4366 Qdisplay,
4367 Qnil, make_number (limit)),
4368 NILP (pos))
4369 && next_overlay_change (start) == ZV))
4371 IT_CHARPOS (*it) = limit;
4372 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4373 *skipped_p = newline_found_p = 1;
4375 else
4377 while (get_next_display_element (it)
4378 && !newline_found_p)
4380 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4381 set_iterator_to_next (it, 0);
4386 it->selective = old_selective;
4387 return newline_found_p;
4391 /* Set IT's current position to the previous visible line start. Skip
4392 invisible text that is so either due to text properties or due to
4393 selective display. Caution: this does not change IT->current_x and
4394 IT->hpos. */
4396 static void
4397 back_to_previous_visible_line_start (it)
4398 struct it *it;
4400 int visible_p = 0;
4402 /* Go back one newline if not on BEGV already. */
4403 if (IT_CHARPOS (*it) > BEGV)
4404 back_to_previous_line_start (it);
4406 /* Move over lines that are invisible because of selective display
4407 or text properties. */
4408 while (IT_CHARPOS (*it) > BEGV
4409 && !visible_p)
4411 visible_p = 1;
4413 /* If selective > 0, then lines indented more than that values
4414 are invisible. */
4415 if (it->selective > 0
4416 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4417 (double) it->selective)) /* iftc */
4418 visible_p = 0;
4419 else
4421 Lisp_Object prop;
4423 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4424 Qinvisible, it->window);
4425 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4426 visible_p = 0;
4429 /* Back one more newline if the current one is invisible. */
4430 if (!visible_p)
4431 back_to_previous_line_start (it);
4434 xassert (IT_CHARPOS (*it) >= BEGV);
4435 xassert (IT_CHARPOS (*it) == BEGV
4436 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4437 CHECK_IT (it);
4441 /* Reseat iterator IT at the previous visible line start. Skip
4442 invisible text that is so either due to text properties or due to
4443 selective display. At the end, update IT's overlay information,
4444 face information etc. */
4446 static void
4447 reseat_at_previous_visible_line_start (it)
4448 struct it *it;
4450 back_to_previous_visible_line_start (it);
4451 reseat (it, it->current.pos, 1);
4452 CHECK_IT (it);
4456 /* Reseat iterator IT on the next visible line start in the current
4457 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4458 preceding the line start. Skip over invisible text that is so
4459 because of selective display. Compute faces, overlays etc at the
4460 new position. Note that this function does not skip over text that
4461 is invisible because of text properties. */
4463 static void
4464 reseat_at_next_visible_line_start (it, on_newline_p)
4465 struct it *it;
4466 int on_newline_p;
4468 int newline_found_p, skipped_p = 0;
4470 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4472 /* Skip over lines that are invisible because they are indented
4473 more than the value of IT->selective. */
4474 if (it->selective > 0)
4475 while (IT_CHARPOS (*it) < ZV
4476 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4477 (double) it->selective)) /* iftc */
4479 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4480 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4483 /* Position on the newline if that's what's requested. */
4484 if (on_newline_p && newline_found_p)
4486 if (STRINGP (it->string))
4488 if (IT_STRING_CHARPOS (*it) > 0)
4490 --IT_STRING_CHARPOS (*it);
4491 --IT_STRING_BYTEPOS (*it);
4494 else if (IT_CHARPOS (*it) > BEGV)
4496 --IT_CHARPOS (*it);
4497 --IT_BYTEPOS (*it);
4498 reseat (it, it->current.pos, 0);
4501 else if (skipped_p)
4502 reseat (it, it->current.pos, 0);
4504 CHECK_IT (it);
4509 /***********************************************************************
4510 Changing an iterator's position
4511 ***********************************************************************/
4513 /* Change IT's current position to POS in current_buffer. If FORCE_P
4514 is non-zero, always check for text properties at the new position.
4515 Otherwise, text properties are only looked up if POS >=
4516 IT->check_charpos of a property. */
4518 static void
4519 reseat (it, pos, force_p)
4520 struct it *it;
4521 struct text_pos pos;
4522 int force_p;
4524 int original_pos = IT_CHARPOS (*it);
4526 reseat_1 (it, pos, 0);
4528 /* Determine where to check text properties. Avoid doing it
4529 where possible because text property lookup is very expensive. */
4530 if (force_p
4531 || CHARPOS (pos) > it->stop_charpos
4532 || CHARPOS (pos) < original_pos)
4533 handle_stop (it);
4535 CHECK_IT (it);
4539 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4540 IT->stop_pos to POS, also. */
4542 static void
4543 reseat_1 (it, pos, set_stop_p)
4544 struct it *it;
4545 struct text_pos pos;
4546 int set_stop_p;
4548 /* Don't call this function when scanning a C string. */
4549 xassert (it->s == NULL);
4551 /* POS must be a reasonable value. */
4552 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4554 it->current.pos = it->position = pos;
4555 XSETBUFFER (it->object, current_buffer);
4556 it->end_charpos = ZV;
4557 it->dpvec = NULL;
4558 it->current.dpvec_index = -1;
4559 it->current.overlay_string_index = -1;
4560 IT_STRING_CHARPOS (*it) = -1;
4561 IT_STRING_BYTEPOS (*it) = -1;
4562 it->string = Qnil;
4563 it->method = next_element_from_buffer;
4564 /* RMS: I added this to fix a bug in move_it_vertically_backward
4565 where it->area continued to relate to the starting point
4566 for the backward motion. Bug report from
4567 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4568 However, I am not sure whether reseat still does the right thing
4569 in general after this change. */
4570 it->area = TEXT_AREA;
4571 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4572 it->sp = 0;
4573 it->face_before_selective_p = 0;
4575 if (set_stop_p)
4576 it->stop_charpos = CHARPOS (pos);
4580 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4581 If S is non-null, it is a C string to iterate over. Otherwise,
4582 STRING gives a Lisp string to iterate over.
4584 If PRECISION > 0, don't return more then PRECISION number of
4585 characters from the string.
4587 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4588 characters have been returned. FIELD_WIDTH < 0 means an infinite
4589 field width.
4591 MULTIBYTE = 0 means disable processing of multibyte characters,
4592 MULTIBYTE > 0 means enable it,
4593 MULTIBYTE < 0 means use IT->multibyte_p.
4595 IT must be initialized via a prior call to init_iterator before
4596 calling this function. */
4598 static void
4599 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4600 struct it *it;
4601 unsigned char *s;
4602 Lisp_Object string;
4603 int charpos;
4604 int precision, field_width, multibyte;
4606 /* No region in strings. */
4607 it->region_beg_charpos = it->region_end_charpos = -1;
4609 /* No text property checks performed by default, but see below. */
4610 it->stop_charpos = -1;
4612 /* Set iterator position and end position. */
4613 bzero (&it->current, sizeof it->current);
4614 it->current.overlay_string_index = -1;
4615 it->current.dpvec_index = -1;
4616 xassert (charpos >= 0);
4618 /* If STRING is specified, use its multibyteness, otherwise use the
4619 setting of MULTIBYTE, if specified. */
4620 if (multibyte >= 0)
4621 it->multibyte_p = multibyte > 0;
4623 if (s == NULL)
4625 xassert (STRINGP (string));
4626 it->string = string;
4627 it->s = NULL;
4628 it->end_charpos = it->string_nchars = SCHARS (string);
4629 it->method = next_element_from_string;
4630 it->current.string_pos = string_pos (charpos, string);
4632 else
4634 it->s = s;
4635 it->string = Qnil;
4637 /* Note that we use IT->current.pos, not it->current.string_pos,
4638 for displaying C strings. */
4639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4640 if (it->multibyte_p)
4642 it->current.pos = c_string_pos (charpos, s, 1);
4643 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4645 else
4647 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4648 it->end_charpos = it->string_nchars = strlen (s);
4651 it->method = next_element_from_c_string;
4654 /* PRECISION > 0 means don't return more than PRECISION characters
4655 from the string. */
4656 if (precision > 0 && it->end_charpos - charpos > precision)
4657 it->end_charpos = it->string_nchars = charpos + precision;
4659 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4660 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4661 FIELD_WIDTH < 0 means infinite field width. This is useful for
4662 padding with `-' at the end of a mode line. */
4663 if (field_width < 0)
4664 field_width = INFINITY;
4665 if (field_width > it->end_charpos - charpos)
4666 it->end_charpos = charpos + field_width;
4668 /* Use the standard display table for displaying strings. */
4669 if (DISP_TABLE_P (Vstandard_display_table))
4670 it->dp = XCHAR_TABLE (Vstandard_display_table);
4672 it->stop_charpos = charpos;
4673 CHECK_IT (it);
4678 /***********************************************************************
4679 Iteration
4680 ***********************************************************************/
4682 /* Load IT's display element fields with information about the next
4683 display element from the current position of IT. Value is zero if
4684 end of buffer (or C string) is reached. */
4687 get_next_display_element (it)
4688 struct it *it;
4690 /* Non-zero means that we found a display element. Zero means that
4691 we hit the end of what we iterate over. Performance note: the
4692 function pointer `method' used here turns out to be faster than
4693 using a sequence of if-statements. */
4694 int success_p = (*it->method) (it);
4696 if (it->what == IT_CHARACTER)
4698 /* Map via display table or translate control characters.
4699 IT->c, IT->len etc. have been set to the next character by
4700 the function call above. If we have a display table, and it
4701 contains an entry for IT->c, translate it. Don't do this if
4702 IT->c itself comes from a display table, otherwise we could
4703 end up in an infinite recursion. (An alternative could be to
4704 count the recursion depth of this function and signal an
4705 error when a certain maximum depth is reached.) Is it worth
4706 it? */
4707 if (success_p && it->dpvec == NULL)
4709 Lisp_Object dv;
4711 if (it->dp
4712 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4713 VECTORP (dv)))
4715 struct Lisp_Vector *v = XVECTOR (dv);
4717 /* Return the first character from the display table
4718 entry, if not empty. If empty, don't display the
4719 current character. */
4720 if (v->size)
4722 it->dpvec_char_len = it->len;
4723 it->dpvec = v->contents;
4724 it->dpend = v->contents + v->size;
4725 it->current.dpvec_index = 0;
4726 it->method = next_element_from_display_vector;
4727 success_p = get_next_display_element (it);
4729 else
4731 set_iterator_to_next (it, 0);
4732 success_p = get_next_display_element (it);
4736 /* Translate control characters into `\003' or `^C' form.
4737 Control characters coming from a display table entry are
4738 currently not translated because we use IT->dpvec to hold
4739 the translation. This could easily be changed but I
4740 don't believe that it is worth doing.
4742 If it->multibyte_p is nonzero, eight-bit characters and
4743 non-printable multibyte characters are also translated to
4744 octal form.
4746 If it->multibyte_p is zero, eight-bit characters that
4747 don't have corresponding multibyte char code are also
4748 translated to octal form. */
4749 else if ((it->c < ' '
4750 && (it->area != TEXT_AREA
4751 || (it->c != '\n' && it->c != '\t')))
4752 || (it->multibyte_p
4753 ? ((it->c >= 127
4754 && it->len == 1)
4755 || !CHAR_PRINTABLE_P (it->c))
4756 : (it->c >= 127
4757 && it->c == unibyte_char_to_multibyte (it->c))))
4759 /* IT->c is a control character which must be displayed
4760 either as '\003' or as `^C' where the '\\' and '^'
4761 can be defined in the display table. Fill
4762 IT->ctl_chars with glyphs for what we have to
4763 display. Then, set IT->dpvec to these glyphs. */
4764 GLYPH g;
4766 if (it->c < 128 && it->ctl_arrow_p)
4768 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4769 if (it->dp
4770 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4771 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4772 g = XINT (DISP_CTRL_GLYPH (it->dp));
4773 else
4774 g = FAST_MAKE_GLYPH ('^', 0);
4775 XSETINT (it->ctl_chars[0], g);
4777 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4778 XSETINT (it->ctl_chars[1], g);
4780 /* Set up IT->dpvec and return first character from it. */
4781 it->dpvec_char_len = it->len;
4782 it->dpvec = it->ctl_chars;
4783 it->dpend = it->dpvec + 2;
4784 it->current.dpvec_index = 0;
4785 it->method = next_element_from_display_vector;
4786 get_next_display_element (it);
4788 else
4790 unsigned char str[MAX_MULTIBYTE_LENGTH];
4791 int len;
4792 int i;
4793 GLYPH escape_glyph;
4795 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4796 if (it->dp
4797 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4798 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4799 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4800 else
4801 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4803 if (SINGLE_BYTE_CHAR_P (it->c))
4804 str[0] = it->c, len = 1;
4805 else
4807 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4808 if (len < 0)
4810 /* It's an invalid character, which
4811 shouldn't happen actually, but due to
4812 bugs it may happen. Let's print the char
4813 as is, there's not much meaningful we can
4814 do with it. */
4815 str[0] = it->c;
4816 str[1] = it->c >> 8;
4817 str[2] = it->c >> 16;
4818 str[3] = it->c >> 24;
4819 len = 4;
4823 for (i = 0; i < len; i++)
4825 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4826 /* Insert three more glyphs into IT->ctl_chars for
4827 the octal display of the character. */
4828 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4829 XSETINT (it->ctl_chars[i * 4 + 1], g);
4830 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4831 XSETINT (it->ctl_chars[i * 4 + 2], g);
4832 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4833 XSETINT (it->ctl_chars[i * 4 + 3], g);
4836 /* Set up IT->dpvec and return the first character
4837 from it. */
4838 it->dpvec_char_len = it->len;
4839 it->dpvec = it->ctl_chars;
4840 it->dpend = it->dpvec + len * 4;
4841 it->current.dpvec_index = 0;
4842 it->method = next_element_from_display_vector;
4843 get_next_display_element (it);
4848 /* Adjust face id for a multibyte character. There are no
4849 multibyte character in unibyte text. */
4850 if (it->multibyte_p
4851 && success_p
4852 && FRAME_WINDOW_P (it->f))
4854 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4855 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4859 /* Is this character the last one of a run of characters with
4860 box? If yes, set IT->end_of_box_run_p to 1. */
4861 if (it->face_box_p
4862 && it->s == NULL)
4864 int face_id;
4865 struct face *face;
4867 it->end_of_box_run_p
4868 = ((face_id = face_after_it_pos (it),
4869 face_id != it->face_id)
4870 && (face = FACE_FROM_ID (it->f, face_id),
4871 face->box == FACE_NO_BOX));
4874 /* Value is 0 if end of buffer or string reached. */
4875 return success_p;
4879 /* Move IT to the next display element.
4881 RESEAT_P non-zero means if called on a newline in buffer text,
4882 skip to the next visible line start.
4884 Functions get_next_display_element and set_iterator_to_next are
4885 separate because I find this arrangement easier to handle than a
4886 get_next_display_element function that also increments IT's
4887 position. The way it is we can first look at an iterator's current
4888 display element, decide whether it fits on a line, and if it does,
4889 increment the iterator position. The other way around we probably
4890 would either need a flag indicating whether the iterator has to be
4891 incremented the next time, or we would have to implement a
4892 decrement position function which would not be easy to write. */
4894 void
4895 set_iterator_to_next (it, reseat_p)
4896 struct it *it;
4897 int reseat_p;
4899 /* Reset flags indicating start and end of a sequence of characters
4900 with box. Reset them at the start of this function because
4901 moving the iterator to a new position might set them. */
4902 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4904 if (it->method == next_element_from_buffer)
4906 /* The current display element of IT is a character from
4907 current_buffer. Advance in the buffer, and maybe skip over
4908 invisible lines that are so because of selective display. */
4909 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4910 reseat_at_next_visible_line_start (it, 0);
4911 else
4913 xassert (it->len != 0);
4914 IT_BYTEPOS (*it) += it->len;
4915 IT_CHARPOS (*it) += 1;
4916 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4919 else if (it->method == next_element_from_composition)
4921 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4922 if (STRINGP (it->string))
4924 IT_STRING_BYTEPOS (*it) += it->len;
4925 IT_STRING_CHARPOS (*it) += it->cmp_len;
4926 it->method = next_element_from_string;
4927 goto consider_string_end;
4929 else
4931 IT_BYTEPOS (*it) += it->len;
4932 IT_CHARPOS (*it) += it->cmp_len;
4933 it->method = next_element_from_buffer;
4936 else if (it->method == next_element_from_c_string)
4938 /* Current display element of IT is from a C string. */
4939 IT_BYTEPOS (*it) += it->len;
4940 IT_CHARPOS (*it) += 1;
4942 else if (it->method == next_element_from_display_vector)
4944 /* Current display element of IT is from a display table entry.
4945 Advance in the display table definition. Reset it to null if
4946 end reached, and continue with characters from buffers/
4947 strings. */
4948 ++it->current.dpvec_index;
4950 /* Restore face of the iterator to what they were before the
4951 display vector entry (these entries may contain faces). */
4952 it->face_id = it->saved_face_id;
4954 if (it->dpvec + it->current.dpvec_index == it->dpend)
4956 if (it->s)
4957 it->method = next_element_from_c_string;
4958 else if (STRINGP (it->string))
4959 it->method = next_element_from_string;
4960 else
4961 it->method = next_element_from_buffer;
4963 it->dpvec = NULL;
4964 it->current.dpvec_index = -1;
4966 /* Skip over characters which were displayed via IT->dpvec. */
4967 if (it->dpvec_char_len < 0)
4968 reseat_at_next_visible_line_start (it, 1);
4969 else if (it->dpvec_char_len > 0)
4971 it->len = it->dpvec_char_len;
4972 set_iterator_to_next (it, reseat_p);
4976 else if (it->method == next_element_from_string)
4978 /* Current display element is a character from a Lisp string. */
4979 xassert (it->s == NULL && STRINGP (it->string));
4980 IT_STRING_BYTEPOS (*it) += it->len;
4981 IT_STRING_CHARPOS (*it) += 1;
4983 consider_string_end:
4985 if (it->current.overlay_string_index >= 0)
4987 /* IT->string is an overlay string. Advance to the
4988 next, if there is one. */
4989 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4990 next_overlay_string (it);
4992 else
4994 /* IT->string is not an overlay string. If we reached
4995 its end, and there is something on IT->stack, proceed
4996 with what is on the stack. This can be either another
4997 string, this time an overlay string, or a buffer. */
4998 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4999 && it->sp > 0)
5001 pop_it (it);
5002 if (!STRINGP (it->string))
5003 it->method = next_element_from_buffer;
5004 else
5005 goto consider_string_end;
5009 else if (it->method == next_element_from_image
5010 || it->method == next_element_from_stretch)
5012 /* The position etc with which we have to proceed are on
5013 the stack. The position may be at the end of a string,
5014 if the `display' property takes up the whole string. */
5015 pop_it (it);
5016 it->image_id = 0;
5017 if (STRINGP (it->string))
5019 it->method = next_element_from_string;
5020 goto consider_string_end;
5022 else
5023 it->method = next_element_from_buffer;
5025 else
5026 /* There are no other methods defined, so this should be a bug. */
5027 abort ();
5029 xassert (it->method != next_element_from_string
5030 || (STRINGP (it->string)
5031 && IT_STRING_CHARPOS (*it) >= 0));
5035 /* Load IT's display element fields with information about the next
5036 display element which comes from a display table entry or from the
5037 result of translating a control character to one of the forms `^C'
5038 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5040 static int
5041 next_element_from_display_vector (it)
5042 struct it *it;
5044 /* Precondition. */
5045 xassert (it->dpvec && it->current.dpvec_index >= 0);
5047 /* Remember the current face id in case glyphs specify faces.
5048 IT's face is restored in set_iterator_to_next. */
5049 it->saved_face_id = it->face_id;
5051 if (INTEGERP (*it->dpvec)
5052 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5054 int lface_id;
5055 GLYPH g;
5057 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5058 it->c = FAST_GLYPH_CHAR (g);
5059 it->len = CHAR_BYTES (it->c);
5061 /* The entry may contain a face id to use. Such a face id is
5062 the id of a Lisp face, not a realized face. A face id of
5063 zero means no face is specified. */
5064 lface_id = FAST_GLYPH_FACE (g);
5065 if (lface_id)
5067 /* The function returns -1 if lface_id is invalid. */
5068 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5069 if (face_id >= 0)
5070 it->face_id = face_id;
5073 else
5074 /* Display table entry is invalid. Return a space. */
5075 it->c = ' ', it->len = 1;
5077 /* Don't change position and object of the iterator here. They are
5078 still the values of the character that had this display table
5079 entry or was translated, and that's what we want. */
5080 it->what = IT_CHARACTER;
5081 return 1;
5085 /* Load IT with the next display element from Lisp string IT->string.
5086 IT->current.string_pos is the current position within the string.
5087 If IT->current.overlay_string_index >= 0, the Lisp string is an
5088 overlay string. */
5090 static int
5091 next_element_from_string (it)
5092 struct it *it;
5094 struct text_pos position;
5096 xassert (STRINGP (it->string));
5097 xassert (IT_STRING_CHARPOS (*it) >= 0);
5098 position = it->current.string_pos;
5100 /* Time to check for invisible text? */
5101 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5102 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5104 handle_stop (it);
5106 /* Since a handler may have changed IT->method, we must
5107 recurse here. */
5108 return get_next_display_element (it);
5111 if (it->current.overlay_string_index >= 0)
5113 /* Get the next character from an overlay string. In overlay
5114 strings, There is no field width or padding with spaces to
5115 do. */
5116 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5118 it->what = IT_EOB;
5119 return 0;
5121 else if (STRING_MULTIBYTE (it->string))
5123 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5124 const unsigned char *s = (SDATA (it->string)
5125 + IT_STRING_BYTEPOS (*it));
5126 it->c = string_char_and_length (s, remaining, &it->len);
5128 else
5130 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5131 it->len = 1;
5134 else
5136 /* Get the next character from a Lisp string that is not an
5137 overlay string. Such strings come from the mode line, for
5138 example. We may have to pad with spaces, or truncate the
5139 string. See also next_element_from_c_string. */
5140 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5142 it->what = IT_EOB;
5143 return 0;
5145 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5147 /* Pad with spaces. */
5148 it->c = ' ', it->len = 1;
5149 CHARPOS (position) = BYTEPOS (position) = -1;
5151 else if (STRING_MULTIBYTE (it->string))
5153 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5154 const unsigned char *s = (SDATA (it->string)
5155 + IT_STRING_BYTEPOS (*it));
5156 it->c = string_char_and_length (s, maxlen, &it->len);
5158 else
5160 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5161 it->len = 1;
5165 /* Record what we have and where it came from. Note that we store a
5166 buffer position in IT->position although it could arguably be a
5167 string position. */
5168 it->what = IT_CHARACTER;
5169 it->object = it->string;
5170 it->position = position;
5171 return 1;
5175 /* Load IT with next display element from C string IT->s.
5176 IT->string_nchars is the maximum number of characters to return
5177 from the string. IT->end_charpos may be greater than
5178 IT->string_nchars when this function is called, in which case we
5179 may have to return padding spaces. Value is zero if end of string
5180 reached, including padding spaces. */
5182 static int
5183 next_element_from_c_string (it)
5184 struct it *it;
5186 int success_p = 1;
5188 xassert (it->s);
5189 it->what = IT_CHARACTER;
5190 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5191 it->object = Qnil;
5193 /* IT's position can be greater IT->string_nchars in case a field
5194 width or precision has been specified when the iterator was
5195 initialized. */
5196 if (IT_CHARPOS (*it) >= it->end_charpos)
5198 /* End of the game. */
5199 it->what = IT_EOB;
5200 success_p = 0;
5202 else if (IT_CHARPOS (*it) >= it->string_nchars)
5204 /* Pad with spaces. */
5205 it->c = ' ', it->len = 1;
5206 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5208 else if (it->multibyte_p)
5210 /* Implementation note: The calls to strlen apparently aren't a
5211 performance problem because there is no noticeable performance
5212 difference between Emacs running in unibyte or multibyte mode. */
5213 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5214 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5215 maxlen, &it->len);
5217 else
5218 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5220 return success_p;
5224 /* Set up IT to return characters from an ellipsis, if appropriate.
5225 The definition of the ellipsis glyphs may come from a display table
5226 entry. This function Fills IT with the first glyph from the
5227 ellipsis if an ellipsis is to be displayed. */
5229 static int
5230 next_element_from_ellipsis (it)
5231 struct it *it;
5233 if (it->selective_display_ellipsis_p)
5235 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5237 /* Use the display table definition for `...'. Invalid glyphs
5238 will be handled by the method returning elements from dpvec. */
5239 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5240 it->dpvec_char_len = it->len;
5241 it->dpvec = v->contents;
5242 it->dpend = v->contents + v->size;
5243 it->current.dpvec_index = 0;
5244 it->method = next_element_from_display_vector;
5246 else
5248 /* Use default `...' which is stored in default_invis_vector. */
5249 it->dpvec_char_len = it->len;
5250 it->dpvec = default_invis_vector;
5251 it->dpend = default_invis_vector + 3;
5252 it->current.dpvec_index = 0;
5253 it->method = next_element_from_display_vector;
5256 else
5258 /* The face at the current position may be different from the
5259 face we find after the invisible text. Remember what it
5260 was in IT->saved_face_id, and signal that it's there by
5261 setting face_before_selective_p. */
5262 it->saved_face_id = it->face_id;
5263 it->method = next_element_from_buffer;
5264 reseat_at_next_visible_line_start (it, 1);
5265 it->face_before_selective_p = 1;
5268 return get_next_display_element (it);
5272 /* Deliver an image display element. The iterator IT is already
5273 filled with image information (done in handle_display_prop). Value
5274 is always 1. */
5277 static int
5278 next_element_from_image (it)
5279 struct it *it;
5281 it->what = IT_IMAGE;
5282 return 1;
5286 /* Fill iterator IT with next display element from a stretch glyph
5287 property. IT->object is the value of the text property. Value is
5288 always 1. */
5290 static int
5291 next_element_from_stretch (it)
5292 struct it *it;
5294 it->what = IT_STRETCH;
5295 return 1;
5299 /* Load IT with the next display element from current_buffer. Value
5300 is zero if end of buffer reached. IT->stop_charpos is the next
5301 position at which to stop and check for text properties or buffer
5302 end. */
5304 static int
5305 next_element_from_buffer (it)
5306 struct it *it;
5308 int success_p = 1;
5310 /* Check this assumption, otherwise, we would never enter the
5311 if-statement, below. */
5312 xassert (IT_CHARPOS (*it) >= BEGV
5313 && IT_CHARPOS (*it) <= it->stop_charpos);
5315 if (IT_CHARPOS (*it) >= it->stop_charpos)
5317 if (IT_CHARPOS (*it) >= it->end_charpos)
5319 int overlay_strings_follow_p;
5321 /* End of the game, except when overlay strings follow that
5322 haven't been returned yet. */
5323 if (it->overlay_strings_at_end_processed_p)
5324 overlay_strings_follow_p = 0;
5325 else
5327 it->overlay_strings_at_end_processed_p = 1;
5328 overlay_strings_follow_p = get_overlay_strings (it, 0);
5331 if (overlay_strings_follow_p)
5332 success_p = get_next_display_element (it);
5333 else
5335 it->what = IT_EOB;
5336 it->position = it->current.pos;
5337 success_p = 0;
5340 else
5342 handle_stop (it);
5343 return get_next_display_element (it);
5346 else
5348 /* No face changes, overlays etc. in sight, so just return a
5349 character from current_buffer. */
5350 unsigned char *p;
5352 /* Maybe run the redisplay end trigger hook. Performance note:
5353 This doesn't seem to cost measurable time. */
5354 if (it->redisplay_end_trigger_charpos
5355 && it->glyph_row
5356 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5357 run_redisplay_end_trigger_hook (it);
5359 /* Get the next character, maybe multibyte. */
5360 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5361 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5363 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5364 - IT_BYTEPOS (*it));
5365 it->c = string_char_and_length (p, maxlen, &it->len);
5367 else
5368 it->c = *p, it->len = 1;
5370 /* Record what we have and where it came from. */
5371 it->what = IT_CHARACTER;;
5372 it->object = it->w->buffer;
5373 it->position = it->current.pos;
5375 /* Normally we return the character found above, except when we
5376 really want to return an ellipsis for selective display. */
5377 if (it->selective)
5379 if (it->c == '\n')
5381 /* A value of selective > 0 means hide lines indented more
5382 than that number of columns. */
5383 if (it->selective > 0
5384 && IT_CHARPOS (*it) + 1 < ZV
5385 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5386 IT_BYTEPOS (*it) + 1,
5387 (double) it->selective)) /* iftc */
5389 success_p = next_element_from_ellipsis (it);
5390 it->dpvec_char_len = -1;
5393 else if (it->c == '\r' && it->selective == -1)
5395 /* A value of selective == -1 means that everything from the
5396 CR to the end of the line is invisible, with maybe an
5397 ellipsis displayed for it. */
5398 success_p = next_element_from_ellipsis (it);
5399 it->dpvec_char_len = -1;
5404 /* Value is zero if end of buffer reached. */
5405 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5406 return success_p;
5410 /* Run the redisplay end trigger hook for IT. */
5412 static void
5413 run_redisplay_end_trigger_hook (it)
5414 struct it *it;
5416 Lisp_Object args[3];
5418 /* IT->glyph_row should be non-null, i.e. we should be actually
5419 displaying something, or otherwise we should not run the hook. */
5420 xassert (it->glyph_row);
5422 /* Set up hook arguments. */
5423 args[0] = Qredisplay_end_trigger_functions;
5424 args[1] = it->window;
5425 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5426 it->redisplay_end_trigger_charpos = 0;
5428 /* Since we are *trying* to run these functions, don't try to run
5429 them again, even if they get an error. */
5430 it->w->redisplay_end_trigger = Qnil;
5431 Frun_hook_with_args (3, args);
5433 /* Notice if it changed the face of the character we are on. */
5434 handle_face_prop (it);
5438 /* Deliver a composition display element. The iterator IT is already
5439 filled with composition information (done in
5440 handle_composition_prop). Value is always 1. */
5442 static int
5443 next_element_from_composition (it)
5444 struct it *it;
5446 it->what = IT_COMPOSITION;
5447 it->position = (STRINGP (it->string)
5448 ? it->current.string_pos
5449 : it->current.pos);
5450 return 1;
5455 /***********************************************************************
5456 Moving an iterator without producing glyphs
5457 ***********************************************************************/
5459 /* Move iterator IT to a specified buffer or X position within one
5460 line on the display without producing glyphs.
5462 OP should be a bit mask including some or all of these bits:
5463 MOVE_TO_X: Stop on reaching x-position TO_X.
5464 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5465 Regardless of OP's value, stop in reaching the end of the display line.
5467 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5468 This means, in particular, that TO_X includes window's horizontal
5469 scroll amount.
5471 The return value has several possible values that
5472 say what condition caused the scan to stop:
5474 MOVE_POS_MATCH_OR_ZV
5475 - when TO_POS or ZV was reached.
5477 MOVE_X_REACHED
5478 -when TO_X was reached before TO_POS or ZV were reached.
5480 MOVE_LINE_CONTINUED
5481 - when we reached the end of the display area and the line must
5482 be continued.
5484 MOVE_LINE_TRUNCATED
5485 - when we reached the end of the display area and the line is
5486 truncated.
5488 MOVE_NEWLINE_OR_CR
5489 - when we stopped at a line end, i.e. a newline or a CR and selective
5490 display is on. */
5492 static enum move_it_result
5493 move_it_in_display_line_to (it, to_charpos, to_x, op)
5494 struct it *it;
5495 int to_charpos, to_x, op;
5497 enum move_it_result result = MOVE_UNDEFINED;
5498 struct glyph_row *saved_glyph_row;
5500 /* Don't produce glyphs in produce_glyphs. */
5501 saved_glyph_row = it->glyph_row;
5502 it->glyph_row = NULL;
5504 while (1)
5506 int x, i, ascent = 0, descent = 0;
5508 /* Stop when ZV or TO_CHARPOS reached. */
5509 if (!get_next_display_element (it)
5510 || ((op & MOVE_TO_POS) != 0
5511 && BUFFERP (it->object)
5512 && IT_CHARPOS (*it) >= to_charpos))
5514 result = MOVE_POS_MATCH_OR_ZV;
5515 break;
5518 /* The call to produce_glyphs will get the metrics of the
5519 display element IT is loaded with. We record in x the
5520 x-position before this display element in case it does not
5521 fit on the line. */
5522 x = it->current_x;
5524 /* Remember the line height so far in case the next element doesn't
5525 fit on the line. */
5526 if (!it->truncate_lines_p)
5528 ascent = it->max_ascent;
5529 descent = it->max_descent;
5532 PRODUCE_GLYPHS (it);
5534 if (it->area != TEXT_AREA)
5536 set_iterator_to_next (it, 1);
5537 continue;
5540 /* The number of glyphs we get back in IT->nglyphs will normally
5541 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5542 character on a terminal frame, or (iii) a line end. For the
5543 second case, IT->nglyphs - 1 padding glyphs will be present
5544 (on X frames, there is only one glyph produced for a
5545 composite character.
5547 The behavior implemented below means, for continuation lines,
5548 that as many spaces of a TAB as fit on the current line are
5549 displayed there. For terminal frames, as many glyphs of a
5550 multi-glyph character are displayed in the current line, too.
5551 This is what the old redisplay code did, and we keep it that
5552 way. Under X, the whole shape of a complex character must
5553 fit on the line or it will be completely displayed in the
5554 next line.
5556 Note that both for tabs and padding glyphs, all glyphs have
5557 the same width. */
5558 if (it->nglyphs)
5560 /* More than one glyph or glyph doesn't fit on line. All
5561 glyphs have the same width. */
5562 int single_glyph_width = it->pixel_width / it->nglyphs;
5563 int new_x;
5565 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5567 new_x = x + single_glyph_width;
5569 /* We want to leave anything reaching TO_X to the caller. */
5570 if ((op & MOVE_TO_X) && new_x > to_x)
5572 it->current_x = x;
5573 result = MOVE_X_REACHED;
5574 break;
5576 else if (/* Lines are continued. */
5577 !it->truncate_lines_p
5578 && (/* And glyph doesn't fit on the line. */
5579 new_x > it->last_visible_x
5580 /* Or it fits exactly and we're on a window
5581 system frame. */
5582 || (new_x == it->last_visible_x
5583 && FRAME_WINDOW_P (it->f))))
5585 if (/* IT->hpos == 0 means the very first glyph
5586 doesn't fit on the line, e.g. a wide image. */
5587 it->hpos == 0
5588 || (new_x == it->last_visible_x
5589 && FRAME_WINDOW_P (it->f)))
5591 ++it->hpos;
5592 it->current_x = new_x;
5593 if (i == it->nglyphs - 1)
5594 set_iterator_to_next (it, 1);
5596 else
5598 it->current_x = x;
5599 it->max_ascent = ascent;
5600 it->max_descent = descent;
5603 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5604 IT_CHARPOS (*it)));
5605 result = MOVE_LINE_CONTINUED;
5606 break;
5608 else if (new_x > it->first_visible_x)
5610 /* Glyph is visible. Increment number of glyphs that
5611 would be displayed. */
5612 ++it->hpos;
5614 else
5616 /* Glyph is completely off the left margin of the display
5617 area. Nothing to do. */
5621 if (result != MOVE_UNDEFINED)
5622 break;
5624 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5626 /* Stop when TO_X specified and reached. This check is
5627 necessary here because of lines consisting of a line end,
5628 only. The line end will not produce any glyphs and we
5629 would never get MOVE_X_REACHED. */
5630 xassert (it->nglyphs == 0);
5631 result = MOVE_X_REACHED;
5632 break;
5635 /* Is this a line end? If yes, we're done. */
5636 if (ITERATOR_AT_END_OF_LINE_P (it))
5638 result = MOVE_NEWLINE_OR_CR;
5639 break;
5642 /* The current display element has been consumed. Advance
5643 to the next. */
5644 set_iterator_to_next (it, 1);
5646 /* Stop if lines are truncated and IT's current x-position is
5647 past the right edge of the window now. */
5648 if (it->truncate_lines_p
5649 && it->current_x >= it->last_visible_x)
5651 result = MOVE_LINE_TRUNCATED;
5652 break;
5656 /* Restore the iterator settings altered at the beginning of this
5657 function. */
5658 it->glyph_row = saved_glyph_row;
5659 return result;
5663 /* Move IT forward until it satisfies one or more of the criteria in
5664 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5666 OP is a bit-mask that specifies where to stop, and in particular,
5667 which of those four position arguments makes a difference. See the
5668 description of enum move_operation_enum.
5670 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5671 screen line, this function will set IT to the next position >
5672 TO_CHARPOS. */
5674 void
5675 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5676 struct it *it;
5677 int to_charpos, to_x, to_y, to_vpos;
5678 int op;
5680 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5681 int line_height;
5682 int reached = 0;
5684 for (;;)
5686 if (op & MOVE_TO_VPOS)
5688 /* If no TO_CHARPOS and no TO_X specified, stop at the
5689 start of the line TO_VPOS. */
5690 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5692 if (it->vpos == to_vpos)
5694 reached = 1;
5695 break;
5697 else
5698 skip = move_it_in_display_line_to (it, -1, -1, 0);
5700 else
5702 /* TO_VPOS >= 0 means stop at TO_X in the line at
5703 TO_VPOS, or at TO_POS, whichever comes first. */
5704 if (it->vpos == to_vpos)
5706 reached = 2;
5707 break;
5710 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5712 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5714 reached = 3;
5715 break;
5717 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5719 /* We have reached TO_X but not in the line we want. */
5720 skip = move_it_in_display_line_to (it, to_charpos,
5721 -1, MOVE_TO_POS);
5722 if (skip == MOVE_POS_MATCH_OR_ZV)
5724 reached = 4;
5725 break;
5730 else if (op & MOVE_TO_Y)
5732 struct it it_backup;
5734 /* TO_Y specified means stop at TO_X in the line containing
5735 TO_Y---or at TO_CHARPOS if this is reached first. The
5736 problem is that we can't really tell whether the line
5737 contains TO_Y before we have completely scanned it, and
5738 this may skip past TO_X. What we do is to first scan to
5739 TO_X.
5741 If TO_X is not specified, use a TO_X of zero. The reason
5742 is to make the outcome of this function more predictable.
5743 If we didn't use TO_X == 0, we would stop at the end of
5744 the line which is probably not what a caller would expect
5745 to happen. */
5746 skip = move_it_in_display_line_to (it, to_charpos,
5747 ((op & MOVE_TO_X)
5748 ? to_x : 0),
5749 (MOVE_TO_X
5750 | (op & MOVE_TO_POS)));
5752 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5753 if (skip == MOVE_POS_MATCH_OR_ZV)
5755 reached = 5;
5756 break;
5759 /* If TO_X was reached, we would like to know whether TO_Y
5760 is in the line. This can only be said if we know the
5761 total line height which requires us to scan the rest of
5762 the line. */
5763 if (skip == MOVE_X_REACHED)
5765 it_backup = *it;
5766 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5767 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5768 op & MOVE_TO_POS);
5769 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5772 /* Now, decide whether TO_Y is in this line. */
5773 line_height = it->max_ascent + it->max_descent;
5774 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5776 if (to_y >= it->current_y
5777 && to_y < it->current_y + line_height)
5779 if (skip == MOVE_X_REACHED)
5780 /* If TO_Y is in this line and TO_X was reached above,
5781 we scanned too far. We have to restore IT's settings
5782 to the ones before skipping. */
5783 *it = it_backup;
5784 reached = 6;
5786 else if (skip == MOVE_X_REACHED)
5788 skip = skip2;
5789 if (skip == MOVE_POS_MATCH_OR_ZV)
5790 reached = 7;
5793 if (reached)
5794 break;
5796 else
5797 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5799 switch (skip)
5801 case MOVE_POS_MATCH_OR_ZV:
5802 reached = 8;
5803 goto out;
5805 case MOVE_NEWLINE_OR_CR:
5806 set_iterator_to_next (it, 1);
5807 it->continuation_lines_width = 0;
5808 break;
5810 case MOVE_LINE_TRUNCATED:
5811 it->continuation_lines_width = 0;
5812 reseat_at_next_visible_line_start (it, 0);
5813 if ((op & MOVE_TO_POS) != 0
5814 && IT_CHARPOS (*it) > to_charpos)
5816 reached = 9;
5817 goto out;
5819 break;
5821 case MOVE_LINE_CONTINUED:
5822 it->continuation_lines_width += it->current_x;
5823 break;
5825 default:
5826 abort ();
5829 /* Reset/increment for the next run. */
5830 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5831 it->current_x = it->hpos = 0;
5832 it->current_y += it->max_ascent + it->max_descent;
5833 ++it->vpos;
5834 last_height = it->max_ascent + it->max_descent;
5835 last_max_ascent = it->max_ascent;
5836 it->max_ascent = it->max_descent = 0;
5839 out:
5841 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5845 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5847 If DY > 0, move IT backward at least that many pixels. DY = 0
5848 means move IT backward to the preceding line start or BEGV. This
5849 function may move over more than DY pixels if IT->current_y - DY
5850 ends up in the middle of a line; in this case IT->current_y will be
5851 set to the top of the line moved to. */
5853 void
5854 move_it_vertically_backward (it, dy)
5855 struct it *it;
5856 int dy;
5858 int nlines, h;
5859 struct it it2, it3;
5860 int start_pos = IT_CHARPOS (*it);
5862 xassert (dy >= 0);
5864 /* Estimate how many newlines we must move back. */
5865 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5867 /* Set the iterator's position that many lines back. */
5868 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5869 back_to_previous_visible_line_start (it);
5871 /* Reseat the iterator here. When moving backward, we don't want
5872 reseat to skip forward over invisible text, set up the iterator
5873 to deliver from overlay strings at the new position etc. So,
5874 use reseat_1 here. */
5875 reseat_1 (it, it->current.pos, 1);
5877 /* We are now surely at a line start. */
5878 it->current_x = it->hpos = 0;
5879 it->continuation_lines_width = 0;
5881 /* Move forward and see what y-distance we moved. First move to the
5882 start of the next line so that we get its height. We need this
5883 height to be able to tell whether we reached the specified
5884 y-distance. */
5885 it2 = *it;
5886 it2.max_ascent = it2.max_descent = 0;
5887 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5888 MOVE_TO_POS | MOVE_TO_VPOS);
5889 xassert (IT_CHARPOS (*it) >= BEGV);
5890 it3 = it2;
5892 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5893 xassert (IT_CHARPOS (*it) >= BEGV);
5894 /* H is the actual vertical distance from the position in *IT
5895 and the starting position. */
5896 h = it2.current_y - it->current_y;
5897 /* NLINES is the distance in number of lines. */
5898 nlines = it2.vpos - it->vpos;
5900 /* Correct IT's y and vpos position
5901 so that they are relative to the starting point. */
5902 it->vpos -= nlines;
5903 it->current_y -= h;
5905 if (dy == 0)
5907 /* DY == 0 means move to the start of the screen line. The
5908 value of nlines is > 0 if continuation lines were involved. */
5909 if (nlines > 0)
5910 move_it_by_lines (it, nlines, 1);
5911 xassert (IT_CHARPOS (*it) <= start_pos);
5913 else
5915 /* The y-position we try to reach, relative to *IT.
5916 Note that H has been subtracted in front of the if-statement. */
5917 int target_y = it->current_y + h - dy;
5918 int y0 = it3.current_y;
5919 int y1 = line_bottom_y (&it3);
5920 int line_height = y1 - y0;
5922 /* If we did not reach target_y, try to move further backward if
5923 we can. If we moved too far backward, try to move forward. */
5924 if (target_y < it->current_y
5925 /* This is heuristic. In a window that's 3 lines high, with
5926 a line height of 13 pixels each, recentering with point
5927 on the bottom line will try to move -39/2 = 19 pixels
5928 backward. Try to avoid moving into the first line. */
5929 && it->current_y - target_y > line_height / 3 * 2
5930 && IT_CHARPOS (*it) > BEGV)
5932 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5933 target_y - it->current_y));
5934 move_it_vertically (it, target_y - it->current_y);
5935 xassert (IT_CHARPOS (*it) >= BEGV);
5937 else if (target_y >= it->current_y + line_height
5938 && IT_CHARPOS (*it) < ZV)
5940 /* Should move forward by at least one line, maybe more.
5942 Note: Calling move_it_by_lines can be expensive on
5943 terminal frames, where compute_motion is used (via
5944 vmotion) to do the job, when there are very long lines
5945 and truncate-lines is nil. That's the reason for
5946 treating terminal frames specially here. */
5948 if (!FRAME_WINDOW_P (it->f))
5949 move_it_vertically (it, target_y - (it->current_y + line_height));
5950 else
5954 move_it_by_lines (it, 1, 1);
5956 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5959 xassert (IT_CHARPOS (*it) >= BEGV);
5965 /* Move IT by a specified amount of pixel lines DY. DY negative means
5966 move backwards. DY = 0 means move to start of screen line. At the
5967 end, IT will be on the start of a screen line. */
5969 void
5970 move_it_vertically (it, dy)
5971 struct it *it;
5972 int dy;
5974 if (dy <= 0)
5975 move_it_vertically_backward (it, -dy);
5976 else if (dy > 0)
5978 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5979 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5980 MOVE_TO_POS | MOVE_TO_Y);
5981 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5983 /* If buffer ends in ZV without a newline, move to the start of
5984 the line to satisfy the post-condition. */
5985 if (IT_CHARPOS (*it) == ZV
5986 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5987 move_it_by_lines (it, 0, 0);
5992 /* Move iterator IT past the end of the text line it is in. */
5994 void
5995 move_it_past_eol (it)
5996 struct it *it;
5998 enum move_it_result rc;
6000 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6001 if (rc == MOVE_NEWLINE_OR_CR)
6002 set_iterator_to_next (it, 0);
6006 #if 0 /* Currently not used. */
6008 /* Return non-zero if some text between buffer positions START_CHARPOS
6009 and END_CHARPOS is invisible. IT->window is the window for text
6010 property lookup. */
6012 static int
6013 invisible_text_between_p (it, start_charpos, end_charpos)
6014 struct it *it;
6015 int start_charpos, end_charpos;
6017 Lisp_Object prop, limit;
6018 int invisible_found_p;
6020 xassert (it != NULL && start_charpos <= end_charpos);
6022 /* Is text at START invisible? */
6023 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6024 it->window);
6025 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6026 invisible_found_p = 1;
6027 else
6029 limit = Fnext_single_char_property_change (make_number (start_charpos),
6030 Qinvisible, Qnil,
6031 make_number (end_charpos));
6032 invisible_found_p = XFASTINT (limit) < end_charpos;
6035 return invisible_found_p;
6038 #endif /* 0 */
6041 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6042 negative means move up. DVPOS == 0 means move to the start of the
6043 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6044 NEED_Y_P is zero, IT->current_y will be left unchanged.
6046 Further optimization ideas: If we would know that IT->f doesn't use
6047 a face with proportional font, we could be faster for
6048 truncate-lines nil. */
6050 void
6051 move_it_by_lines (it, dvpos, need_y_p)
6052 struct it *it;
6053 int dvpos, need_y_p;
6055 struct position pos;
6057 if (!FRAME_WINDOW_P (it->f))
6059 struct text_pos textpos;
6061 /* We can use vmotion on frames without proportional fonts. */
6062 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6063 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6064 reseat (it, textpos, 1);
6065 it->vpos += pos.vpos;
6066 it->current_y += pos.vpos;
6068 else if (dvpos == 0)
6070 /* DVPOS == 0 means move to the start of the screen line. */
6071 move_it_vertically_backward (it, 0);
6072 xassert (it->current_x == 0 && it->hpos == 0);
6074 else if (dvpos > 0)
6075 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6076 else
6078 struct it it2;
6079 int start_charpos, i;
6081 /* Start at the beginning of the screen line containing IT's
6082 position. */
6083 move_it_vertically_backward (it, 0);
6085 /* Go back -DVPOS visible lines and reseat the iterator there. */
6086 start_charpos = IT_CHARPOS (*it);
6087 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6088 back_to_previous_visible_line_start (it);
6089 reseat (it, it->current.pos, 1);
6090 it->current_x = it->hpos = 0;
6092 /* Above call may have moved too far if continuation lines
6093 are involved. Scan forward and see if it did. */
6094 it2 = *it;
6095 it2.vpos = it2.current_y = 0;
6096 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6097 it->vpos -= it2.vpos;
6098 it->current_y -= it2.current_y;
6099 it->current_x = it->hpos = 0;
6101 /* If we moved too far, move IT some lines forward. */
6102 if (it2.vpos > -dvpos)
6104 int delta = it2.vpos + dvpos;
6105 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6110 /* Return 1 if IT points into the middle of a display vector. */
6113 in_display_vector_p (it)
6114 struct it *it;
6116 return (it->method == next_element_from_display_vector
6117 && it->current.dpvec_index > 0
6118 && it->dpvec + it->current.dpvec_index != it->dpend);
6122 /***********************************************************************
6123 Messages
6124 ***********************************************************************/
6127 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6128 to *Messages*. */
6130 void
6131 add_to_log (format, arg1, arg2)
6132 char *format;
6133 Lisp_Object arg1, arg2;
6135 Lisp_Object args[3];
6136 Lisp_Object msg, fmt;
6137 char *buffer;
6138 int len;
6139 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6141 /* Do nothing if called asynchronously. Inserting text into
6142 a buffer may call after-change-functions and alike and
6143 that would means running Lisp asynchronously. */
6144 if (handling_signal)
6145 return;
6147 fmt = msg = Qnil;
6148 GCPRO4 (fmt, msg, arg1, arg2);
6150 args[0] = fmt = build_string (format);
6151 args[1] = arg1;
6152 args[2] = arg2;
6153 msg = Fformat (3, args);
6155 len = SBYTES (msg) + 1;
6156 buffer = (char *) alloca (len);
6157 bcopy (SDATA (msg), buffer, len);
6159 message_dolog (buffer, len - 1, 1, 0);
6160 UNGCPRO;
6164 /* Output a newline in the *Messages* buffer if "needs" one. */
6166 void
6167 message_log_maybe_newline ()
6169 if (message_log_need_newline)
6170 message_dolog ("", 0, 1, 0);
6174 /* Add a string M of length NBYTES to the message log, optionally
6175 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6176 nonzero, means interpret the contents of M as multibyte. This
6177 function calls low-level routines in order to bypass text property
6178 hooks, etc. which might not be safe to run. */
6180 void
6181 message_dolog (m, nbytes, nlflag, multibyte)
6182 const char *m;
6183 int nbytes, nlflag, multibyte;
6185 if (!NILP (Vmemory_full))
6186 return;
6188 if (!NILP (Vmessage_log_max))
6190 struct buffer *oldbuf;
6191 Lisp_Object oldpoint, oldbegv, oldzv;
6192 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6193 int point_at_end = 0;
6194 int zv_at_end = 0;
6195 Lisp_Object old_deactivate_mark, tem;
6196 struct gcpro gcpro1;
6198 old_deactivate_mark = Vdeactivate_mark;
6199 oldbuf = current_buffer;
6200 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6201 current_buffer->undo_list = Qt;
6203 oldpoint = message_dolog_marker1;
6204 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6205 oldbegv = message_dolog_marker2;
6206 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6207 oldzv = message_dolog_marker3;
6208 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6209 GCPRO1 (old_deactivate_mark);
6211 if (PT == Z)
6212 point_at_end = 1;
6213 if (ZV == Z)
6214 zv_at_end = 1;
6216 BEGV = BEG;
6217 BEGV_BYTE = BEG_BYTE;
6218 ZV = Z;
6219 ZV_BYTE = Z_BYTE;
6220 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6222 /* Insert the string--maybe converting multibyte to single byte
6223 or vice versa, so that all the text fits the buffer. */
6224 if (multibyte
6225 && NILP (current_buffer->enable_multibyte_characters))
6227 int i, c, char_bytes;
6228 unsigned char work[1];
6230 /* Convert a multibyte string to single-byte
6231 for the *Message* buffer. */
6232 for (i = 0; i < nbytes; i += char_bytes)
6234 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6235 work[0] = (SINGLE_BYTE_CHAR_P (c)
6237 : multibyte_char_to_unibyte (c, Qnil));
6238 insert_1_both (work, 1, 1, 1, 0, 0);
6241 else if (! multibyte
6242 && ! NILP (current_buffer->enable_multibyte_characters))
6244 int i, c, char_bytes;
6245 unsigned char *msg = (unsigned char *) m;
6246 unsigned char str[MAX_MULTIBYTE_LENGTH];
6247 /* Convert a single-byte string to multibyte
6248 for the *Message* buffer. */
6249 for (i = 0; i < nbytes; i++)
6251 c = unibyte_char_to_multibyte (msg[i]);
6252 char_bytes = CHAR_STRING (c, str);
6253 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6256 else if (nbytes)
6257 insert_1 (m, nbytes, 1, 0, 0);
6259 if (nlflag)
6261 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6262 insert_1 ("\n", 1, 1, 0, 0);
6264 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6265 this_bol = PT;
6266 this_bol_byte = PT_BYTE;
6268 /* See if this line duplicates the previous one.
6269 If so, combine duplicates. */
6270 if (this_bol > BEG)
6272 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6273 prev_bol = PT;
6274 prev_bol_byte = PT_BYTE;
6276 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6277 this_bol, this_bol_byte);
6278 if (dup)
6280 del_range_both (prev_bol, prev_bol_byte,
6281 this_bol, this_bol_byte, 0);
6282 if (dup > 1)
6284 char dupstr[40];
6285 int duplen;
6287 /* If you change this format, don't forget to also
6288 change message_log_check_duplicate. */
6289 sprintf (dupstr, " [%d times]", dup);
6290 duplen = strlen (dupstr);
6291 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6292 insert_1 (dupstr, duplen, 1, 0, 1);
6297 /* If we have more than the desired maximum number of lines
6298 in the *Messages* buffer now, delete the oldest ones.
6299 This is safe because we don't have undo in this buffer. */
6301 if (NATNUMP (Vmessage_log_max))
6303 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6304 -XFASTINT (Vmessage_log_max) - 1, 0);
6305 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6308 BEGV = XMARKER (oldbegv)->charpos;
6309 BEGV_BYTE = marker_byte_position (oldbegv);
6311 if (zv_at_end)
6313 ZV = Z;
6314 ZV_BYTE = Z_BYTE;
6316 else
6318 ZV = XMARKER (oldzv)->charpos;
6319 ZV_BYTE = marker_byte_position (oldzv);
6322 if (point_at_end)
6323 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6324 else
6325 /* We can't do Fgoto_char (oldpoint) because it will run some
6326 Lisp code. */
6327 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6328 XMARKER (oldpoint)->bytepos);
6330 UNGCPRO;
6331 unchain_marker (XMARKER (oldpoint));
6332 unchain_marker (XMARKER (oldbegv));
6333 unchain_marker (XMARKER (oldzv));
6335 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6336 set_buffer_internal (oldbuf);
6337 if (NILP (tem))
6338 windows_or_buffers_changed = old_windows_or_buffers_changed;
6339 message_log_need_newline = !nlflag;
6340 Vdeactivate_mark = old_deactivate_mark;
6345 /* We are at the end of the buffer after just having inserted a newline.
6346 (Note: We depend on the fact we won't be crossing the gap.)
6347 Check to see if the most recent message looks a lot like the previous one.
6348 Return 0 if different, 1 if the new one should just replace it, or a
6349 value N > 1 if we should also append " [N times]". */
6351 static int
6352 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6353 int prev_bol, this_bol;
6354 int prev_bol_byte, this_bol_byte;
6356 int i;
6357 int len = Z_BYTE - 1 - this_bol_byte;
6358 int seen_dots = 0;
6359 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6360 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6362 for (i = 0; i < len; i++)
6364 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6365 seen_dots = 1;
6366 if (p1[i] != p2[i])
6367 return seen_dots;
6369 p1 += len;
6370 if (*p1 == '\n')
6371 return 2;
6372 if (*p1++ == ' ' && *p1++ == '[')
6374 int n = 0;
6375 while (*p1 >= '0' && *p1 <= '9')
6376 n = n * 10 + *p1++ - '0';
6377 if (strncmp (p1, " times]\n", 8) == 0)
6378 return n+1;
6380 return 0;
6384 /* Display an echo area message M with a specified length of NBYTES
6385 bytes. The string may include null characters. If M is 0, clear
6386 out any existing message, and let the mini-buffer text show
6387 through.
6389 The buffer M must continue to exist until after the echo area gets
6390 cleared or some other message gets displayed there. This means do
6391 not pass text that is stored in a Lisp string; do not pass text in
6392 a buffer that was alloca'd. */
6394 void
6395 message2 (m, nbytes, multibyte)
6396 const char *m;
6397 int nbytes;
6398 int multibyte;
6400 /* First flush out any partial line written with print. */
6401 message_log_maybe_newline ();
6402 if (m)
6403 message_dolog (m, nbytes, 1, multibyte);
6404 message2_nolog (m, nbytes, multibyte);
6408 /* The non-logging counterpart of message2. */
6410 void
6411 message2_nolog (m, nbytes, multibyte)
6412 const char *m;
6413 int nbytes, multibyte;
6415 struct frame *sf = SELECTED_FRAME ();
6416 message_enable_multibyte = multibyte;
6418 if (noninteractive)
6420 if (noninteractive_need_newline)
6421 putc ('\n', stderr);
6422 noninteractive_need_newline = 0;
6423 if (m)
6424 fwrite (m, nbytes, 1, stderr);
6425 if (cursor_in_echo_area == 0)
6426 fprintf (stderr, "\n");
6427 fflush (stderr);
6429 /* A null message buffer means that the frame hasn't really been
6430 initialized yet. Error messages get reported properly by
6431 cmd_error, so this must be just an informative message; toss it. */
6432 else if (INTERACTIVE
6433 && sf->glyphs_initialized_p
6434 && FRAME_MESSAGE_BUF (sf))
6436 Lisp_Object mini_window;
6437 struct frame *f;
6439 /* Get the frame containing the mini-buffer
6440 that the selected frame is using. */
6441 mini_window = FRAME_MINIBUF_WINDOW (sf);
6442 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6444 FRAME_SAMPLE_VISIBILITY (f);
6445 if (FRAME_VISIBLE_P (sf)
6446 && ! FRAME_VISIBLE_P (f))
6447 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6449 if (m)
6451 set_message (m, Qnil, nbytes, multibyte);
6452 if (minibuffer_auto_raise)
6453 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6455 else
6456 clear_message (1, 1);
6458 do_pending_window_change (0);
6459 echo_area_display (1);
6460 do_pending_window_change (0);
6461 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6462 (*frame_up_to_date_hook) (f);
6467 /* Display an echo area message M with a specified length of NBYTES
6468 bytes. The string may include null characters. If M is not a
6469 string, clear out any existing message, and let the mini-buffer
6470 text show through. */
6472 void
6473 message3 (m, nbytes, multibyte)
6474 Lisp_Object m;
6475 int nbytes;
6476 int multibyte;
6478 struct gcpro gcpro1;
6480 GCPRO1 (m);
6482 /* First flush out any partial line written with print. */
6483 message_log_maybe_newline ();
6484 if (STRINGP (m))
6485 message_dolog (SDATA (m), nbytes, 1, multibyte);
6486 message3_nolog (m, nbytes, multibyte);
6488 UNGCPRO;
6492 /* The non-logging version of message3. */
6494 void
6495 message3_nolog (m, nbytes, multibyte)
6496 Lisp_Object m;
6497 int nbytes, multibyte;
6499 struct frame *sf = SELECTED_FRAME ();
6500 message_enable_multibyte = multibyte;
6502 if (noninteractive)
6504 if (noninteractive_need_newline)
6505 putc ('\n', stderr);
6506 noninteractive_need_newline = 0;
6507 if (STRINGP (m))
6508 fwrite (SDATA (m), nbytes, 1, stderr);
6509 if (cursor_in_echo_area == 0)
6510 fprintf (stderr, "\n");
6511 fflush (stderr);
6513 /* A null message buffer means that the frame hasn't really been
6514 initialized yet. Error messages get reported properly by
6515 cmd_error, so this must be just an informative message; toss it. */
6516 else if (INTERACTIVE
6517 && sf->glyphs_initialized_p
6518 && FRAME_MESSAGE_BUF (sf))
6520 Lisp_Object mini_window;
6521 Lisp_Object frame;
6522 struct frame *f;
6524 /* Get the frame containing the mini-buffer
6525 that the selected frame is using. */
6526 mini_window = FRAME_MINIBUF_WINDOW (sf);
6527 frame = XWINDOW (mini_window)->frame;
6528 f = XFRAME (frame);
6530 FRAME_SAMPLE_VISIBILITY (f);
6531 if (FRAME_VISIBLE_P (sf)
6532 && !FRAME_VISIBLE_P (f))
6533 Fmake_frame_visible (frame);
6535 if (STRINGP (m) && SCHARS (m) > 0)
6537 set_message (NULL, m, nbytes, multibyte);
6538 if (minibuffer_auto_raise)
6539 Fraise_frame (frame);
6541 else
6542 clear_message (1, 1);
6544 do_pending_window_change (0);
6545 echo_area_display (1);
6546 do_pending_window_change (0);
6547 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6548 (*frame_up_to_date_hook) (f);
6553 /* Display a null-terminated echo area message M. If M is 0, clear
6554 out any existing message, and let the mini-buffer text show through.
6556 The buffer M must continue to exist until after the echo area gets
6557 cleared or some other message gets displayed there. Do not pass
6558 text that is stored in a Lisp string. Do not pass text in a buffer
6559 that was alloca'd. */
6561 void
6562 message1 (m)
6563 char *m;
6565 message2 (m, (m ? strlen (m) : 0), 0);
6569 /* The non-logging counterpart of message1. */
6571 void
6572 message1_nolog (m)
6573 char *m;
6575 message2_nolog (m, (m ? strlen (m) : 0), 0);
6578 /* Display a message M which contains a single %s
6579 which gets replaced with STRING. */
6581 void
6582 message_with_string (m, string, log)
6583 char *m;
6584 Lisp_Object string;
6585 int log;
6587 CHECK_STRING (string);
6589 if (noninteractive)
6591 if (m)
6593 if (noninteractive_need_newline)
6594 putc ('\n', stderr);
6595 noninteractive_need_newline = 0;
6596 fprintf (stderr, m, SDATA (string));
6597 if (cursor_in_echo_area == 0)
6598 fprintf (stderr, "\n");
6599 fflush (stderr);
6602 else if (INTERACTIVE)
6604 /* The frame whose minibuffer we're going to display the message on.
6605 It may be larger than the selected frame, so we need
6606 to use its buffer, not the selected frame's buffer. */
6607 Lisp_Object mini_window;
6608 struct frame *f, *sf = SELECTED_FRAME ();
6610 /* Get the frame containing the minibuffer
6611 that the selected frame is using. */
6612 mini_window = FRAME_MINIBUF_WINDOW (sf);
6613 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6615 /* A null message buffer means that the frame hasn't really been
6616 initialized yet. Error messages get reported properly by
6617 cmd_error, so this must be just an informative message; toss it. */
6618 if (FRAME_MESSAGE_BUF (f))
6620 Lisp_Object args[2], message;
6621 struct gcpro gcpro1, gcpro2;
6623 args[0] = build_string (m);
6624 args[1] = message = string;
6625 GCPRO2 (args[0], message);
6626 gcpro1.nvars = 2;
6628 message = Fformat (2, args);
6630 if (log)
6631 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6632 else
6633 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6635 UNGCPRO;
6637 /* Print should start at the beginning of the message
6638 buffer next time. */
6639 message_buf_print = 0;
6645 /* Dump an informative message to the minibuf. If M is 0, clear out
6646 any existing message, and let the mini-buffer text show through. */
6648 /* VARARGS 1 */
6649 void
6650 message (m, a1, a2, a3)
6651 char *m;
6652 EMACS_INT a1, a2, a3;
6654 if (noninteractive)
6656 if (m)
6658 if (noninteractive_need_newline)
6659 putc ('\n', stderr);
6660 noninteractive_need_newline = 0;
6661 fprintf (stderr, m, a1, a2, a3);
6662 if (cursor_in_echo_area == 0)
6663 fprintf (stderr, "\n");
6664 fflush (stderr);
6667 else if (INTERACTIVE)
6669 /* The frame whose mini-buffer we're going to display the message
6670 on. It may be larger than the selected frame, so we need to
6671 use its buffer, not the selected frame's buffer. */
6672 Lisp_Object mini_window;
6673 struct frame *f, *sf = SELECTED_FRAME ();
6675 /* Get the frame containing the mini-buffer
6676 that the selected frame is using. */
6677 mini_window = FRAME_MINIBUF_WINDOW (sf);
6678 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6680 /* A null message buffer means that the frame hasn't really been
6681 initialized yet. Error messages get reported properly by
6682 cmd_error, so this must be just an informative message; toss
6683 it. */
6684 if (FRAME_MESSAGE_BUF (f))
6686 if (m)
6688 int len;
6689 #ifdef NO_ARG_ARRAY
6690 char *a[3];
6691 a[0] = (char *) a1;
6692 a[1] = (char *) a2;
6693 a[2] = (char *) a3;
6695 len = doprnt (FRAME_MESSAGE_BUF (f),
6696 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6697 #else
6698 len = doprnt (FRAME_MESSAGE_BUF (f),
6699 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6700 (char **) &a1);
6701 #endif /* NO_ARG_ARRAY */
6703 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6705 else
6706 message1 (0);
6708 /* Print should start at the beginning of the message
6709 buffer next time. */
6710 message_buf_print = 0;
6716 /* The non-logging version of message. */
6718 void
6719 message_nolog (m, a1, a2, a3)
6720 char *m;
6721 EMACS_INT a1, a2, a3;
6723 Lisp_Object old_log_max;
6724 old_log_max = Vmessage_log_max;
6725 Vmessage_log_max = Qnil;
6726 message (m, a1, a2, a3);
6727 Vmessage_log_max = old_log_max;
6731 /* Display the current message in the current mini-buffer. This is
6732 only called from error handlers in process.c, and is not time
6733 critical. */
6735 void
6736 update_echo_area ()
6738 if (!NILP (echo_area_buffer[0]))
6740 Lisp_Object string;
6741 string = Fcurrent_message ();
6742 message3 (string, SBYTES (string),
6743 !NILP (current_buffer->enable_multibyte_characters));
6748 /* Make sure echo area buffers in `echo_buffers' are live.
6749 If they aren't, make new ones. */
6751 static void
6752 ensure_echo_area_buffers ()
6754 int i;
6756 for (i = 0; i < 2; ++i)
6757 if (!BUFFERP (echo_buffer[i])
6758 || NILP (XBUFFER (echo_buffer[i])->name))
6760 char name[30];
6761 Lisp_Object old_buffer;
6762 int j;
6764 old_buffer = echo_buffer[i];
6765 sprintf (name, " *Echo Area %d*", i);
6766 echo_buffer[i] = Fget_buffer_create (build_string (name));
6767 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6769 for (j = 0; j < 2; ++j)
6770 if (EQ (old_buffer, echo_area_buffer[j]))
6771 echo_area_buffer[j] = echo_buffer[i];
6776 /* Call FN with args A1..A4 with either the current or last displayed
6777 echo_area_buffer as current buffer.
6779 WHICH zero means use the current message buffer
6780 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6781 from echo_buffer[] and clear it.
6783 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6784 suitable buffer from echo_buffer[] and clear it.
6786 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6787 that the current message becomes the last displayed one, make
6788 choose a suitable buffer for echo_area_buffer[0], and clear it.
6790 Value is what FN returns. */
6792 static int
6793 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6794 struct window *w;
6795 int which;
6796 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6797 EMACS_INT a1;
6798 Lisp_Object a2;
6799 EMACS_INT a3, a4;
6801 Lisp_Object buffer;
6802 int this_one, the_other, clear_buffer_p, rc;
6803 int count = SPECPDL_INDEX ();
6805 /* If buffers aren't live, make new ones. */
6806 ensure_echo_area_buffers ();
6808 clear_buffer_p = 0;
6810 if (which == 0)
6811 this_one = 0, the_other = 1;
6812 else if (which > 0)
6813 this_one = 1, the_other = 0;
6814 else
6816 this_one = 0, the_other = 1;
6817 clear_buffer_p = 1;
6819 /* We need a fresh one in case the current echo buffer equals
6820 the one containing the last displayed echo area message. */
6821 if (!NILP (echo_area_buffer[this_one])
6822 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6823 echo_area_buffer[this_one] = Qnil;
6826 /* Choose a suitable buffer from echo_buffer[] is we don't
6827 have one. */
6828 if (NILP (echo_area_buffer[this_one]))
6830 echo_area_buffer[this_one]
6831 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6832 ? echo_buffer[the_other]
6833 : echo_buffer[this_one]);
6834 clear_buffer_p = 1;
6837 buffer = echo_area_buffer[this_one];
6839 /* Don't get confused by reusing the buffer used for echoing
6840 for a different purpose. */
6841 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6842 cancel_echoing ();
6844 record_unwind_protect (unwind_with_echo_area_buffer,
6845 with_echo_area_buffer_unwind_data (w));
6847 /* Make the echo area buffer current. Note that for display
6848 purposes, it is not necessary that the displayed window's buffer
6849 == current_buffer, except for text property lookup. So, let's
6850 only set that buffer temporarily here without doing a full
6851 Fset_window_buffer. We must also change w->pointm, though,
6852 because otherwise an assertions in unshow_buffer fails, and Emacs
6853 aborts. */
6854 set_buffer_internal_1 (XBUFFER (buffer));
6855 if (w)
6857 w->buffer = buffer;
6858 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6861 current_buffer->undo_list = Qt;
6862 current_buffer->read_only = Qnil;
6863 specbind (Qinhibit_read_only, Qt);
6864 specbind (Qinhibit_modification_hooks, Qt);
6866 if (clear_buffer_p && Z > BEG)
6867 del_range (BEG, Z);
6869 xassert (BEGV >= BEG);
6870 xassert (ZV <= Z && ZV >= BEGV);
6872 rc = fn (a1, a2, a3, a4);
6874 xassert (BEGV >= BEG);
6875 xassert (ZV <= Z && ZV >= BEGV);
6877 unbind_to (count, Qnil);
6878 return rc;
6882 /* Save state that should be preserved around the call to the function
6883 FN called in with_echo_area_buffer. */
6885 static Lisp_Object
6886 with_echo_area_buffer_unwind_data (w)
6887 struct window *w;
6889 int i = 0;
6890 Lisp_Object vector;
6892 /* Reduce consing by keeping one vector in
6893 Vwith_echo_area_save_vector. */
6894 vector = Vwith_echo_area_save_vector;
6895 Vwith_echo_area_save_vector = Qnil;
6897 if (NILP (vector))
6898 vector = Fmake_vector (make_number (7), Qnil);
6900 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6901 AREF (vector, i) = Vdeactivate_mark, ++i;
6902 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6904 if (w)
6906 XSETWINDOW (AREF (vector, i), w); ++i;
6907 AREF (vector, i) = w->buffer; ++i;
6908 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6909 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6911 else
6913 int end = i + 4;
6914 for (; i < end; ++i)
6915 AREF (vector, i) = Qnil;
6918 xassert (i == ASIZE (vector));
6919 return vector;
6923 /* Restore global state from VECTOR which was created by
6924 with_echo_area_buffer_unwind_data. */
6926 static Lisp_Object
6927 unwind_with_echo_area_buffer (vector)
6928 Lisp_Object vector;
6930 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6931 Vdeactivate_mark = AREF (vector, 1);
6932 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6934 if (WINDOWP (AREF (vector, 3)))
6936 struct window *w;
6937 Lisp_Object buffer, charpos, bytepos;
6939 w = XWINDOW (AREF (vector, 3));
6940 buffer = AREF (vector, 4);
6941 charpos = AREF (vector, 5);
6942 bytepos = AREF (vector, 6);
6944 w->buffer = buffer;
6945 set_marker_both (w->pointm, buffer,
6946 XFASTINT (charpos), XFASTINT (bytepos));
6949 Vwith_echo_area_save_vector = vector;
6950 return Qnil;
6954 /* Set up the echo area for use by print functions. MULTIBYTE_P
6955 non-zero means we will print multibyte. */
6957 void
6958 setup_echo_area_for_printing (multibyte_p)
6959 int multibyte_p;
6961 /* If we can't find an echo area any more, exit. */
6962 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6963 Fkill_emacs (Qnil);
6965 ensure_echo_area_buffers ();
6967 if (!message_buf_print)
6969 /* A message has been output since the last time we printed.
6970 Choose a fresh echo area buffer. */
6971 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6972 echo_area_buffer[0] = echo_buffer[1];
6973 else
6974 echo_area_buffer[0] = echo_buffer[0];
6976 /* Switch to that buffer and clear it. */
6977 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6978 current_buffer->truncate_lines = Qnil;
6980 if (Z > BEG)
6982 int count = SPECPDL_INDEX ();
6983 specbind (Qinhibit_read_only, Qt);
6984 /* Note that undo recording is always disabled. */
6985 del_range (BEG, Z);
6986 unbind_to (count, Qnil);
6988 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6990 /* Set up the buffer for the multibyteness we need. */
6991 if (multibyte_p
6992 != !NILP (current_buffer->enable_multibyte_characters))
6993 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6995 /* Raise the frame containing the echo area. */
6996 if (minibuffer_auto_raise)
6998 struct frame *sf = SELECTED_FRAME ();
6999 Lisp_Object mini_window;
7000 mini_window = FRAME_MINIBUF_WINDOW (sf);
7001 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7004 message_log_maybe_newline ();
7005 message_buf_print = 1;
7007 else
7009 if (NILP (echo_area_buffer[0]))
7011 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7012 echo_area_buffer[0] = echo_buffer[1];
7013 else
7014 echo_area_buffer[0] = echo_buffer[0];
7017 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7019 /* Someone switched buffers between print requests. */
7020 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7021 current_buffer->truncate_lines = Qnil;
7027 /* Display an echo area message in window W. Value is non-zero if W's
7028 height is changed. If display_last_displayed_message_p is
7029 non-zero, display the message that was last displayed, otherwise
7030 display the current message. */
7032 static int
7033 display_echo_area (w)
7034 struct window *w;
7036 int i, no_message_p, window_height_changed_p, count;
7038 /* Temporarily disable garbage collections while displaying the echo
7039 area. This is done because a GC can print a message itself.
7040 That message would modify the echo area buffer's contents while a
7041 redisplay of the buffer is going on, and seriously confuse
7042 redisplay. */
7043 count = inhibit_garbage_collection ();
7045 /* If there is no message, we must call display_echo_area_1
7046 nevertheless because it resizes the window. But we will have to
7047 reset the echo_area_buffer in question to nil at the end because
7048 with_echo_area_buffer will sets it to an empty buffer. */
7049 i = display_last_displayed_message_p ? 1 : 0;
7050 no_message_p = NILP (echo_area_buffer[i]);
7052 window_height_changed_p
7053 = with_echo_area_buffer (w, display_last_displayed_message_p,
7054 display_echo_area_1,
7055 (EMACS_INT) w, Qnil, 0, 0);
7057 if (no_message_p)
7058 echo_area_buffer[i] = Qnil;
7060 unbind_to (count, Qnil);
7061 return window_height_changed_p;
7065 /* Helper for display_echo_area. Display the current buffer which
7066 contains the current echo area message in window W, a mini-window,
7067 a pointer to which is passed in A1. A2..A4 are currently not used.
7068 Change the height of W so that all of the message is displayed.
7069 Value is non-zero if height of W was changed. */
7071 static int
7072 display_echo_area_1 (a1, a2, a3, a4)
7073 EMACS_INT a1;
7074 Lisp_Object a2;
7075 EMACS_INT a3, a4;
7077 struct window *w = (struct window *) a1;
7078 Lisp_Object window;
7079 struct text_pos start;
7080 int window_height_changed_p = 0;
7082 /* Do this before displaying, so that we have a large enough glyph
7083 matrix for the display. */
7084 window_height_changed_p = resize_mini_window (w, 0);
7086 /* Display. */
7087 clear_glyph_matrix (w->desired_matrix);
7088 XSETWINDOW (window, w);
7089 SET_TEXT_POS (start, BEG, BEG_BYTE);
7090 try_window (window, start);
7092 return window_height_changed_p;
7096 /* Resize the echo area window to exactly the size needed for the
7097 currently displayed message, if there is one. If a mini-buffer
7098 is active, don't shrink it. */
7100 void
7101 resize_echo_area_exactly ()
7103 if (BUFFERP (echo_area_buffer[0])
7104 && WINDOWP (echo_area_window))
7106 struct window *w = XWINDOW (echo_area_window);
7107 int resized_p;
7108 Lisp_Object resize_exactly;
7110 if (minibuf_level == 0)
7111 resize_exactly = Qt;
7112 else
7113 resize_exactly = Qnil;
7115 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7116 (EMACS_INT) w, resize_exactly, 0, 0);
7117 if (resized_p)
7119 ++windows_or_buffers_changed;
7120 ++update_mode_lines;
7121 redisplay_internal (0);
7127 /* Callback function for with_echo_area_buffer, when used from
7128 resize_echo_area_exactly. A1 contains a pointer to the window to
7129 resize, EXACTLY non-nil means resize the mini-window exactly to the
7130 size of the text displayed. A3 and A4 are not used. Value is what
7131 resize_mini_window returns. */
7133 static int
7134 resize_mini_window_1 (a1, exactly, a3, a4)
7135 EMACS_INT a1;
7136 Lisp_Object exactly;
7137 EMACS_INT a3, a4;
7139 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7143 /* Resize mini-window W to fit the size of its contents. EXACT:P
7144 means size the window exactly to the size needed. Otherwise, it's
7145 only enlarged until W's buffer is empty. Value is non-zero if
7146 the window height has been changed. */
7149 resize_mini_window (w, exact_p)
7150 struct window *w;
7151 int exact_p;
7153 struct frame *f = XFRAME (w->frame);
7154 int window_height_changed_p = 0;
7156 xassert (MINI_WINDOW_P (w));
7158 /* Don't resize windows while redisplaying a window; it would
7159 confuse redisplay functions when the size of the window they are
7160 displaying changes from under them. Such a resizing can happen,
7161 for instance, when which-func prints a long message while
7162 we are running fontification-functions. We're running these
7163 functions with safe_call which binds inhibit-redisplay to t. */
7164 if (!NILP (Vinhibit_redisplay))
7165 return 0;
7167 /* Nil means don't try to resize. */
7168 if (NILP (Vresize_mini_windows)
7169 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7170 return 0;
7172 if (!FRAME_MINIBUF_ONLY_P (f))
7174 struct it it;
7175 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7176 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7177 int height, max_height;
7178 int unit = FRAME_LINE_HEIGHT (f);
7179 struct text_pos start;
7180 struct buffer *old_current_buffer = NULL;
7182 if (current_buffer != XBUFFER (w->buffer))
7184 old_current_buffer = current_buffer;
7185 set_buffer_internal (XBUFFER (w->buffer));
7188 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7190 /* Compute the max. number of lines specified by the user. */
7191 if (FLOATP (Vmax_mini_window_height))
7192 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7193 else if (INTEGERP (Vmax_mini_window_height))
7194 max_height = XINT (Vmax_mini_window_height);
7195 else
7196 max_height = total_height / 4;
7198 /* Correct that max. height if it's bogus. */
7199 max_height = max (1, max_height);
7200 max_height = min (total_height, max_height);
7202 /* Find out the height of the text in the window. */
7203 if (it.truncate_lines_p)
7204 height = 1;
7205 else
7207 last_height = 0;
7208 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7209 if (it.max_ascent == 0 && it.max_descent == 0)
7210 height = it.current_y + last_height;
7211 else
7212 height = it.current_y + it.max_ascent + it.max_descent;
7213 height -= it.extra_line_spacing;
7214 height = (height + unit - 1) / unit;
7217 /* Compute a suitable window start. */
7218 if (height > max_height)
7220 height = max_height;
7221 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7222 move_it_vertically_backward (&it, (height - 1) * unit);
7223 start = it.current.pos;
7225 else
7226 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7227 SET_MARKER_FROM_TEXT_POS (w->start, start);
7229 if (EQ (Vresize_mini_windows, Qgrow_only))
7231 /* Let it grow only, until we display an empty message, in which
7232 case the window shrinks again. */
7233 if (height > WINDOW_TOTAL_LINES (w))
7235 int old_height = WINDOW_TOTAL_LINES (w);
7236 freeze_window_starts (f, 1);
7237 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7238 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7240 else if (height < WINDOW_TOTAL_LINES (w)
7241 && (exact_p || BEGV == ZV))
7243 int old_height = WINDOW_TOTAL_LINES (w);
7244 freeze_window_starts (f, 0);
7245 shrink_mini_window (w);
7246 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7249 else
7251 /* Always resize to exact size needed. */
7252 if (height > WINDOW_TOTAL_LINES (w))
7254 int old_height = WINDOW_TOTAL_LINES (w);
7255 freeze_window_starts (f, 1);
7256 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7257 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7259 else if (height < WINDOW_TOTAL_LINES (w))
7261 int old_height = WINDOW_TOTAL_LINES (w);
7262 freeze_window_starts (f, 0);
7263 shrink_mini_window (w);
7265 if (height)
7267 freeze_window_starts (f, 1);
7268 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7271 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7275 if (old_current_buffer)
7276 set_buffer_internal (old_current_buffer);
7279 return window_height_changed_p;
7283 /* Value is the current message, a string, or nil if there is no
7284 current message. */
7286 Lisp_Object
7287 current_message ()
7289 Lisp_Object msg;
7291 if (NILP (echo_area_buffer[0]))
7292 msg = Qnil;
7293 else
7295 with_echo_area_buffer (0, 0, current_message_1,
7296 (EMACS_INT) &msg, Qnil, 0, 0);
7297 if (NILP (msg))
7298 echo_area_buffer[0] = Qnil;
7301 return msg;
7305 static int
7306 current_message_1 (a1, a2, a3, a4)
7307 EMACS_INT a1;
7308 Lisp_Object a2;
7309 EMACS_INT a3, a4;
7311 Lisp_Object *msg = (Lisp_Object *) a1;
7313 if (Z > BEG)
7314 *msg = make_buffer_string (BEG, Z, 1);
7315 else
7316 *msg = Qnil;
7317 return 0;
7321 /* Push the current message on Vmessage_stack for later restauration
7322 by restore_message. Value is non-zero if the current message isn't
7323 empty. This is a relatively infrequent operation, so it's not
7324 worth optimizing. */
7327 push_message ()
7329 Lisp_Object msg;
7330 msg = current_message ();
7331 Vmessage_stack = Fcons (msg, Vmessage_stack);
7332 return STRINGP (msg);
7336 /* Restore message display from the top of Vmessage_stack. */
7338 void
7339 restore_message ()
7341 Lisp_Object msg;
7343 xassert (CONSP (Vmessage_stack));
7344 msg = XCAR (Vmessage_stack);
7345 if (STRINGP (msg))
7346 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7347 else
7348 message3_nolog (msg, 0, 0);
7352 /* Handler for record_unwind_protect calling pop_message. */
7354 Lisp_Object
7355 pop_message_unwind (dummy)
7356 Lisp_Object dummy;
7358 pop_message ();
7359 return Qnil;
7362 /* Pop the top-most entry off Vmessage_stack. */
7364 void
7365 pop_message ()
7367 xassert (CONSP (Vmessage_stack));
7368 Vmessage_stack = XCDR (Vmessage_stack);
7372 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7373 exits. If the stack is not empty, we have a missing pop_message
7374 somewhere. */
7376 void
7377 check_message_stack ()
7379 if (!NILP (Vmessage_stack))
7380 abort ();
7384 /* Truncate to NCHARS what will be displayed in the echo area the next
7385 time we display it---but don't redisplay it now. */
7387 void
7388 truncate_echo_area (nchars)
7389 int nchars;
7391 if (nchars == 0)
7392 echo_area_buffer[0] = Qnil;
7393 /* A null message buffer means that the frame hasn't really been
7394 initialized yet. Error messages get reported properly by
7395 cmd_error, so this must be just an informative message; toss it. */
7396 else if (!noninteractive
7397 && INTERACTIVE
7398 && !NILP (echo_area_buffer[0]))
7400 struct frame *sf = SELECTED_FRAME ();
7401 if (FRAME_MESSAGE_BUF (sf))
7402 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7407 /* Helper function for truncate_echo_area. Truncate the current
7408 message to at most NCHARS characters. */
7410 static int
7411 truncate_message_1 (nchars, a2, a3, a4)
7412 EMACS_INT nchars;
7413 Lisp_Object a2;
7414 EMACS_INT a3, a4;
7416 if (BEG + nchars < Z)
7417 del_range (BEG + nchars, Z);
7418 if (Z == BEG)
7419 echo_area_buffer[0] = Qnil;
7420 return 0;
7424 /* Set the current message to a substring of S or STRING.
7426 If STRING is a Lisp string, set the message to the first NBYTES
7427 bytes from STRING. NBYTES zero means use the whole string. If
7428 STRING is multibyte, the message will be displayed multibyte.
7430 If S is not null, set the message to the first LEN bytes of S. LEN
7431 zero means use the whole string. MULTIBYTE_P non-zero means S is
7432 multibyte. Display the message multibyte in that case. */
7434 void
7435 set_message (s, string, nbytes, multibyte_p)
7436 const char *s;
7437 Lisp_Object string;
7438 int nbytes, multibyte_p;
7440 message_enable_multibyte
7441 = ((s && multibyte_p)
7442 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7444 with_echo_area_buffer (0, -1, set_message_1,
7445 (EMACS_INT) s, string, nbytes, multibyte_p);
7446 message_buf_print = 0;
7447 help_echo_showing_p = 0;
7451 /* Helper function for set_message. Arguments have the same meaning
7452 as there, with A1 corresponding to S and A2 corresponding to STRING
7453 This function is called with the echo area buffer being
7454 current. */
7456 static int
7457 set_message_1 (a1, a2, nbytes, multibyte_p)
7458 EMACS_INT a1;
7459 Lisp_Object a2;
7460 EMACS_INT nbytes, multibyte_p;
7462 const char *s = (const char *) a1;
7463 Lisp_Object string = a2;
7465 xassert (BEG == Z);
7467 /* Change multibyteness of the echo buffer appropriately. */
7468 if (message_enable_multibyte
7469 != !NILP (current_buffer->enable_multibyte_characters))
7470 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7472 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7474 /* Insert new message at BEG. */
7475 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7477 if (STRINGP (string))
7479 int nchars;
7481 if (nbytes == 0)
7482 nbytes = SBYTES (string);
7483 nchars = string_byte_to_char (string, nbytes);
7485 /* This function takes care of single/multibyte conversion. We
7486 just have to ensure that the echo area buffer has the right
7487 setting of enable_multibyte_characters. */
7488 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7490 else if (s)
7492 if (nbytes == 0)
7493 nbytes = strlen (s);
7495 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7497 /* Convert from multi-byte to single-byte. */
7498 int i, c, n;
7499 unsigned char work[1];
7501 /* Convert a multibyte string to single-byte. */
7502 for (i = 0; i < nbytes; i += n)
7504 c = string_char_and_length (s + i, nbytes - i, &n);
7505 work[0] = (SINGLE_BYTE_CHAR_P (c)
7507 : multibyte_char_to_unibyte (c, Qnil));
7508 insert_1_both (work, 1, 1, 1, 0, 0);
7511 else if (!multibyte_p
7512 && !NILP (current_buffer->enable_multibyte_characters))
7514 /* Convert from single-byte to multi-byte. */
7515 int i, c, n;
7516 const unsigned char *msg = (const unsigned char *) s;
7517 unsigned char str[MAX_MULTIBYTE_LENGTH];
7519 /* Convert a single-byte string to multibyte. */
7520 for (i = 0; i < nbytes; i++)
7522 c = unibyte_char_to_multibyte (msg[i]);
7523 n = CHAR_STRING (c, str);
7524 insert_1_both (str, 1, n, 1, 0, 0);
7527 else
7528 insert_1 (s, nbytes, 1, 0, 0);
7531 return 0;
7535 /* Clear messages. CURRENT_P non-zero means clear the current
7536 message. LAST_DISPLAYED_P non-zero means clear the message
7537 last displayed. */
7539 void
7540 clear_message (current_p, last_displayed_p)
7541 int current_p, last_displayed_p;
7543 if (current_p)
7545 echo_area_buffer[0] = Qnil;
7546 message_cleared_p = 1;
7549 if (last_displayed_p)
7550 echo_area_buffer[1] = Qnil;
7552 message_buf_print = 0;
7555 /* Clear garbaged frames.
7557 This function is used where the old redisplay called
7558 redraw_garbaged_frames which in turn called redraw_frame which in
7559 turn called clear_frame. The call to clear_frame was a source of
7560 flickering. I believe a clear_frame is not necessary. It should
7561 suffice in the new redisplay to invalidate all current matrices,
7562 and ensure a complete redisplay of all windows. */
7564 static void
7565 clear_garbaged_frames ()
7567 if (frame_garbaged)
7569 Lisp_Object tail, frame;
7570 int changed_count = 0;
7572 FOR_EACH_FRAME (tail, frame)
7574 struct frame *f = XFRAME (frame);
7576 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7578 if (f->resized_p)
7579 Fredraw_frame (frame);
7580 clear_current_matrices (f);
7581 changed_count++;
7582 f->garbaged = 0;
7583 f->resized_p = 0;
7587 frame_garbaged = 0;
7588 if (changed_count)
7589 ++windows_or_buffers_changed;
7594 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7595 is non-zero update selected_frame. Value is non-zero if the
7596 mini-windows height has been changed. */
7598 static int
7599 echo_area_display (update_frame_p)
7600 int update_frame_p;
7602 Lisp_Object mini_window;
7603 struct window *w;
7604 struct frame *f;
7605 int window_height_changed_p = 0;
7606 struct frame *sf = SELECTED_FRAME ();
7608 mini_window = FRAME_MINIBUF_WINDOW (sf);
7609 w = XWINDOW (mini_window);
7610 f = XFRAME (WINDOW_FRAME (w));
7612 /* Don't display if frame is invisible or not yet initialized. */
7613 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7614 return 0;
7616 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7617 #ifndef MAC_OS8
7618 #ifdef HAVE_WINDOW_SYSTEM
7619 /* When Emacs starts, selected_frame may be a visible terminal
7620 frame, even if we run under a window system. If we let this
7621 through, a message would be displayed on the terminal. */
7622 if (!FRAME_WINDOW_P (XFRAME (selected_frame))
7623 && !NILP (Vwindow_system))
7624 return 0;
7625 #endif /* HAVE_WINDOW_SYSTEM */
7626 #endif
7628 /* Redraw garbaged frames. */
7629 if (frame_garbaged)
7630 clear_garbaged_frames ();
7632 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7634 echo_area_window = mini_window;
7635 window_height_changed_p = display_echo_area (w);
7636 w->must_be_updated_p = 1;
7638 /* Update the display, unless called from redisplay_internal.
7639 Also don't update the screen during redisplay itself. The
7640 update will happen at the end of redisplay, and an update
7641 here could cause confusion. */
7642 if (update_frame_p && !redisplaying_p)
7644 int n = 0;
7646 /* If the display update has been interrupted by pending
7647 input, update mode lines in the frame. Due to the
7648 pending input, it might have been that redisplay hasn't
7649 been called, so that mode lines above the echo area are
7650 garbaged. This looks odd, so we prevent it here. */
7651 if (!display_completed)
7652 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7654 if (window_height_changed_p
7655 /* Don't do this if Emacs is shutting down. Redisplay
7656 needs to run hooks. */
7657 && !NILP (Vrun_hooks))
7659 /* Must update other windows. Likewise as in other
7660 cases, don't let this update be interrupted by
7661 pending input. */
7662 int count = SPECPDL_INDEX ();
7663 specbind (Qredisplay_dont_pause, Qt);
7664 windows_or_buffers_changed = 1;
7665 redisplay_internal (0);
7666 unbind_to (count, Qnil);
7668 else if (FRAME_WINDOW_P (f) && n == 0)
7670 /* Window configuration is the same as before.
7671 Can do with a display update of the echo area,
7672 unless we displayed some mode lines. */
7673 update_single_window (w, 1);
7674 FRAME_RIF (f)->flush_display (f);
7676 else
7677 update_frame (f, 1, 1);
7679 /* If cursor is in the echo area, make sure that the next
7680 redisplay displays the minibuffer, so that the cursor will
7681 be replaced with what the minibuffer wants. */
7682 if (cursor_in_echo_area)
7683 ++windows_or_buffers_changed;
7686 else if (!EQ (mini_window, selected_window))
7687 windows_or_buffers_changed++;
7689 /* Last displayed message is now the current message. */
7690 echo_area_buffer[1] = echo_area_buffer[0];
7692 /* Prevent redisplay optimization in redisplay_internal by resetting
7693 this_line_start_pos. This is done because the mini-buffer now
7694 displays the message instead of its buffer text. */
7695 if (EQ (mini_window, selected_window))
7696 CHARPOS (this_line_start_pos) = 0;
7698 return window_height_changed_p;
7703 /***********************************************************************
7704 Frame Titles
7705 ***********************************************************************/
7708 /* The frame title buffering code is also used by Fformat_mode_line.
7709 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7711 /* A buffer for constructing frame titles in it; allocated from the
7712 heap in init_xdisp and resized as needed in store_frame_title_char. */
7714 static char *frame_title_buf;
7716 /* The buffer's end, and a current output position in it. */
7718 static char *frame_title_buf_end;
7719 static char *frame_title_ptr;
7722 /* Store a single character C for the frame title in frame_title_buf.
7723 Re-allocate frame_title_buf if necessary. */
7725 static void
7726 #ifdef PROTOTYPES
7727 store_frame_title_char (char c)
7728 #else
7729 store_frame_title_char (c)
7730 char c;
7731 #endif
7733 /* If output position has reached the end of the allocated buffer,
7734 double the buffer's size. */
7735 if (frame_title_ptr == frame_title_buf_end)
7737 int len = frame_title_ptr - frame_title_buf;
7738 int new_size = 2 * len * sizeof *frame_title_buf;
7739 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7740 frame_title_buf_end = frame_title_buf + new_size;
7741 frame_title_ptr = frame_title_buf + len;
7744 *frame_title_ptr++ = c;
7748 /* Store part of a frame title in frame_title_buf, beginning at
7749 frame_title_ptr. STR is the string to store. Do not copy
7750 characters that yield more columns than PRECISION; PRECISION <= 0
7751 means copy the whole string. Pad with spaces until FIELD_WIDTH
7752 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7753 pad. Called from display_mode_element when it is used to build a
7754 frame title. */
7756 static int
7757 store_frame_title (str, field_width, precision)
7758 const unsigned char *str;
7759 int field_width, precision;
7761 int n = 0;
7762 int dummy, nbytes;
7764 /* Copy at most PRECISION chars from STR. */
7765 nbytes = strlen (str);
7766 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7767 while (nbytes--)
7768 store_frame_title_char (*str++);
7770 /* Fill up with spaces until FIELD_WIDTH reached. */
7771 while (field_width > 0
7772 && n < field_width)
7774 store_frame_title_char (' ');
7775 ++n;
7778 return n;
7781 #ifdef HAVE_WINDOW_SYSTEM
7783 /* Set the title of FRAME, if it has changed. The title format is
7784 Vicon_title_format if FRAME is iconified, otherwise it is
7785 frame_title_format. */
7787 static void
7788 x_consider_frame_title (frame)
7789 Lisp_Object frame;
7791 struct frame *f = XFRAME (frame);
7793 if (FRAME_WINDOW_P (f)
7794 || FRAME_MINIBUF_ONLY_P (f)
7795 || f->explicit_name)
7797 /* Do we have more than one visible frame on this X display? */
7798 Lisp_Object tail;
7799 Lisp_Object fmt;
7800 struct buffer *obuf;
7801 int len;
7802 struct it it;
7804 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7806 Lisp_Object other_frame = XCAR (tail);
7807 struct frame *tf = XFRAME (other_frame);
7809 if (tf != f
7810 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7811 && !FRAME_MINIBUF_ONLY_P (tf)
7812 && !EQ (other_frame, tip_frame)
7813 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7814 break;
7817 /* Set global variable indicating that multiple frames exist. */
7818 multiple_frames = CONSP (tail);
7820 /* Switch to the buffer of selected window of the frame. Set up
7821 frame_title_ptr so that display_mode_element will output into it;
7822 then display the title. */
7823 obuf = current_buffer;
7824 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7825 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7826 frame_title_ptr = frame_title_buf;
7827 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7828 NULL, DEFAULT_FACE_ID);
7829 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7830 len = frame_title_ptr - frame_title_buf;
7831 frame_title_ptr = NULL;
7832 set_buffer_internal_1 (obuf);
7834 /* Set the title only if it's changed. This avoids consing in
7835 the common case where it hasn't. (If it turns out that we've
7836 already wasted too much time by walking through the list with
7837 display_mode_element, then we might need to optimize at a
7838 higher level than this.) */
7839 if (! STRINGP (f->name)
7840 || SBYTES (f->name) != len
7841 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7842 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7846 #endif /* not HAVE_WINDOW_SYSTEM */
7851 /***********************************************************************
7852 Menu Bars
7853 ***********************************************************************/
7856 /* Prepare for redisplay by updating menu-bar item lists when
7857 appropriate. This can call eval. */
7859 void
7860 prepare_menu_bars ()
7862 int all_windows;
7863 struct gcpro gcpro1, gcpro2;
7864 struct frame *f;
7865 Lisp_Object tooltip_frame;
7867 #ifdef HAVE_WINDOW_SYSTEM
7868 tooltip_frame = tip_frame;
7869 #else
7870 tooltip_frame = Qnil;
7871 #endif
7873 /* Update all frame titles based on their buffer names, etc. We do
7874 this before the menu bars so that the buffer-menu will show the
7875 up-to-date frame titles. */
7876 #ifdef HAVE_WINDOW_SYSTEM
7877 if (windows_or_buffers_changed || update_mode_lines)
7879 Lisp_Object tail, frame;
7881 FOR_EACH_FRAME (tail, frame)
7883 f = XFRAME (frame);
7884 if (!EQ (frame, tooltip_frame)
7885 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7886 x_consider_frame_title (frame);
7889 #endif /* HAVE_WINDOW_SYSTEM */
7891 /* Update the menu bar item lists, if appropriate. This has to be
7892 done before any actual redisplay or generation of display lines. */
7893 all_windows = (update_mode_lines
7894 || buffer_shared > 1
7895 || windows_or_buffers_changed);
7896 if (all_windows)
7898 Lisp_Object tail, frame;
7899 int count = SPECPDL_INDEX ();
7901 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7903 FOR_EACH_FRAME (tail, frame)
7905 f = XFRAME (frame);
7907 /* Ignore tooltip frame. */
7908 if (EQ (frame, tooltip_frame))
7909 continue;
7911 /* If a window on this frame changed size, report that to
7912 the user and clear the size-change flag. */
7913 if (FRAME_WINDOW_SIZES_CHANGED (f))
7915 Lisp_Object functions;
7917 /* Clear flag first in case we get an error below. */
7918 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7919 functions = Vwindow_size_change_functions;
7920 GCPRO2 (tail, functions);
7922 while (CONSP (functions))
7924 call1 (XCAR (functions), frame);
7925 functions = XCDR (functions);
7927 UNGCPRO;
7930 GCPRO1 (tail);
7931 update_menu_bar (f, 0);
7932 #ifdef HAVE_WINDOW_SYSTEM
7933 update_tool_bar (f, 0);
7934 #endif
7935 UNGCPRO;
7938 unbind_to (count, Qnil);
7940 else
7942 struct frame *sf = SELECTED_FRAME ();
7943 update_menu_bar (sf, 1);
7944 #ifdef HAVE_WINDOW_SYSTEM
7945 update_tool_bar (sf, 1);
7946 #endif
7949 /* Motif needs this. See comment in xmenu.c. Turn it off when
7950 pending_menu_activation is not defined. */
7951 #ifdef USE_X_TOOLKIT
7952 pending_menu_activation = 0;
7953 #endif
7957 /* Update the menu bar item list for frame F. This has to be done
7958 before we start to fill in any display lines, because it can call
7959 eval.
7961 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7963 static void
7964 update_menu_bar (f, save_match_data)
7965 struct frame *f;
7966 int save_match_data;
7968 Lisp_Object window;
7969 register struct window *w;
7971 /* If called recursively during a menu update, do nothing. This can
7972 happen when, for instance, an activate-menubar-hook causes a
7973 redisplay. */
7974 if (inhibit_menubar_update)
7975 return;
7977 window = FRAME_SELECTED_WINDOW (f);
7978 w = XWINDOW (window);
7980 #if 0 /* The if statement below this if statement used to include the
7981 condition !NILP (w->update_mode_line), rather than using
7982 update_mode_lines directly, and this if statement may have
7983 been added to make that condition work. Now the if
7984 statement below matches its comment, this isn't needed. */
7985 if (update_mode_lines)
7986 w->update_mode_line = Qt;
7987 #endif
7989 if (FRAME_WINDOW_P (f)
7991 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7992 || defined (USE_GTK)
7993 FRAME_EXTERNAL_MENU_BAR (f)
7994 #else
7995 FRAME_MENU_BAR_LINES (f) > 0
7996 #endif
7997 : FRAME_MENU_BAR_LINES (f) > 0)
7999 /* If the user has switched buffers or windows, we need to
8000 recompute to reflect the new bindings. But we'll
8001 recompute when update_mode_lines is set too; that means
8002 that people can use force-mode-line-update to request
8003 that the menu bar be recomputed. The adverse effect on
8004 the rest of the redisplay algorithm is about the same as
8005 windows_or_buffers_changed anyway. */
8006 if (windows_or_buffers_changed
8007 /* This used to test w->update_mode_line, but we believe
8008 there is no need to recompute the menu in that case. */
8009 || update_mode_lines
8010 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8011 < BUF_MODIFF (XBUFFER (w->buffer)))
8012 != !NILP (w->last_had_star))
8013 || ((!NILP (Vtransient_mark_mode)
8014 && !NILP (XBUFFER (w->buffer)->mark_active))
8015 != !NILP (w->region_showing)))
8017 struct buffer *prev = current_buffer;
8018 int count = SPECPDL_INDEX ();
8020 specbind (Qinhibit_menubar_update, Qt);
8022 set_buffer_internal_1 (XBUFFER (w->buffer));
8023 if (save_match_data)
8024 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8025 if (NILP (Voverriding_local_map_menu_flag))
8027 specbind (Qoverriding_terminal_local_map, Qnil);
8028 specbind (Qoverriding_local_map, Qnil);
8031 /* Run the Lucid hook. */
8032 safe_run_hooks (Qactivate_menubar_hook);
8034 /* If it has changed current-menubar from previous value,
8035 really recompute the menu-bar from the value. */
8036 if (! NILP (Vlucid_menu_bar_dirty_flag))
8037 call0 (Qrecompute_lucid_menubar);
8039 safe_run_hooks (Qmenu_bar_update_hook);
8040 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8042 /* Redisplay the menu bar in case we changed it. */
8043 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8044 || defined (USE_GTK)
8045 if (FRAME_WINDOW_P (f)
8046 #if defined (MAC_OS)
8047 /* All frames on Mac OS share the same menubar. So only the
8048 selected frame should be allowed to set it. */
8049 && f == SELECTED_FRAME ()
8050 #endif
8052 set_frame_menubar (f, 0, 0);
8053 else
8054 /* On a terminal screen, the menu bar is an ordinary screen
8055 line, and this makes it get updated. */
8056 w->update_mode_line = Qt;
8057 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8058 /* In the non-toolkit version, the menu bar is an ordinary screen
8059 line, and this makes it get updated. */
8060 w->update_mode_line = Qt;
8061 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8063 unbind_to (count, Qnil);
8064 set_buffer_internal_1 (prev);
8071 /***********************************************************************
8072 Output Cursor
8073 ***********************************************************************/
8075 #ifdef HAVE_WINDOW_SYSTEM
8077 /* EXPORT:
8078 Nominal cursor position -- where to draw output.
8079 HPOS and VPOS are window relative glyph matrix coordinates.
8080 X and Y are window relative pixel coordinates. */
8082 struct cursor_pos output_cursor;
8085 /* EXPORT:
8086 Set the global variable output_cursor to CURSOR. All cursor
8087 positions are relative to updated_window. */
8089 void
8090 set_output_cursor (cursor)
8091 struct cursor_pos *cursor;
8093 output_cursor.hpos = cursor->hpos;
8094 output_cursor.vpos = cursor->vpos;
8095 output_cursor.x = cursor->x;
8096 output_cursor.y = cursor->y;
8100 /* EXPORT for RIF:
8101 Set a nominal cursor position.
8103 HPOS and VPOS are column/row positions in a window glyph matrix. X
8104 and Y are window text area relative pixel positions.
8106 If this is done during an update, updated_window will contain the
8107 window that is being updated and the position is the future output
8108 cursor position for that window. If updated_window is null, use
8109 selected_window and display the cursor at the given position. */
8111 void
8112 x_cursor_to (vpos, hpos, y, x)
8113 int vpos, hpos, y, x;
8115 struct window *w;
8117 /* If updated_window is not set, work on selected_window. */
8118 if (updated_window)
8119 w = updated_window;
8120 else
8121 w = XWINDOW (selected_window);
8123 /* Set the output cursor. */
8124 output_cursor.hpos = hpos;
8125 output_cursor.vpos = vpos;
8126 output_cursor.x = x;
8127 output_cursor.y = y;
8129 /* If not called as part of an update, really display the cursor.
8130 This will also set the cursor position of W. */
8131 if (updated_window == NULL)
8133 BLOCK_INPUT;
8134 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8135 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8136 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8137 UNBLOCK_INPUT;
8141 #endif /* HAVE_WINDOW_SYSTEM */
8144 /***********************************************************************
8145 Tool-bars
8146 ***********************************************************************/
8148 #ifdef HAVE_WINDOW_SYSTEM
8150 /* Where the mouse was last time we reported a mouse event. */
8152 FRAME_PTR last_mouse_frame;
8154 /* Tool-bar item index of the item on which a mouse button was pressed
8155 or -1. */
8157 int last_tool_bar_item;
8160 /* Update the tool-bar item list for frame F. This has to be done
8161 before we start to fill in any display lines. Called from
8162 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8163 and restore it here. */
8165 static void
8166 update_tool_bar (f, save_match_data)
8167 struct frame *f;
8168 int save_match_data;
8170 #ifdef USE_GTK
8171 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8172 #else
8173 int do_update = WINDOWP (f->tool_bar_window)
8174 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8175 #endif
8177 if (do_update)
8179 Lisp_Object window;
8180 struct window *w;
8182 window = FRAME_SELECTED_WINDOW (f);
8183 w = XWINDOW (window);
8185 /* If the user has switched buffers or windows, we need to
8186 recompute to reflect the new bindings. But we'll
8187 recompute when update_mode_lines is set too; that means
8188 that people can use force-mode-line-update to request
8189 that the menu bar be recomputed. The adverse effect on
8190 the rest of the redisplay algorithm is about the same as
8191 windows_or_buffers_changed anyway. */
8192 if (windows_or_buffers_changed
8193 || !NILP (w->update_mode_line)
8194 || update_mode_lines
8195 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8196 < BUF_MODIFF (XBUFFER (w->buffer)))
8197 != !NILP (w->last_had_star))
8198 || ((!NILP (Vtransient_mark_mode)
8199 && !NILP (XBUFFER (w->buffer)->mark_active))
8200 != !NILP (w->region_showing)))
8202 struct buffer *prev = current_buffer;
8203 int count = SPECPDL_INDEX ();
8204 Lisp_Object old_tool_bar;
8205 struct gcpro gcpro1;
8207 /* Set current_buffer to the buffer of the selected
8208 window of the frame, so that we get the right local
8209 keymaps. */
8210 set_buffer_internal_1 (XBUFFER (w->buffer));
8212 /* Save match data, if we must. */
8213 if (save_match_data)
8214 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8216 /* Make sure that we don't accidentally use bogus keymaps. */
8217 if (NILP (Voverriding_local_map_menu_flag))
8219 specbind (Qoverriding_terminal_local_map, Qnil);
8220 specbind (Qoverriding_local_map, Qnil);
8223 old_tool_bar = f->tool_bar_items;
8224 GCPRO1 (old_tool_bar);
8226 /* Build desired tool-bar items from keymaps. */
8227 BLOCK_INPUT;
8228 f->tool_bar_items
8229 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8230 UNBLOCK_INPUT;
8232 /* Redisplay the tool-bar if we changed it. */
8233 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8234 w->update_mode_line = Qt;
8236 UNGCPRO;
8238 unbind_to (count, Qnil);
8239 set_buffer_internal_1 (prev);
8245 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8246 F's desired tool-bar contents. F->tool_bar_items must have
8247 been set up previously by calling prepare_menu_bars. */
8249 static void
8250 build_desired_tool_bar_string (f)
8251 struct frame *f;
8253 int i, size, size_needed;
8254 struct gcpro gcpro1, gcpro2, gcpro3;
8255 Lisp_Object image, plist, props;
8257 image = plist = props = Qnil;
8258 GCPRO3 (image, plist, props);
8260 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8261 Otherwise, make a new string. */
8263 /* The size of the string we might be able to reuse. */
8264 size = (STRINGP (f->desired_tool_bar_string)
8265 ? SCHARS (f->desired_tool_bar_string)
8266 : 0);
8268 /* We need one space in the string for each image. */
8269 size_needed = f->n_tool_bar_items;
8271 /* Reuse f->desired_tool_bar_string, if possible. */
8272 if (size < size_needed || NILP (f->desired_tool_bar_string))
8273 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8274 make_number (' '));
8275 else
8277 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8278 Fremove_text_properties (make_number (0), make_number (size),
8279 props, f->desired_tool_bar_string);
8282 /* Put a `display' property on the string for the images to display,
8283 put a `menu_item' property on tool-bar items with a value that
8284 is the index of the item in F's tool-bar item vector. */
8285 for (i = 0; i < f->n_tool_bar_items; ++i)
8287 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8289 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8290 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8291 int hmargin, vmargin, relief, idx, end;
8292 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8294 /* If image is a vector, choose the image according to the
8295 button state. */
8296 image = PROP (TOOL_BAR_ITEM_IMAGES);
8297 if (VECTORP (image))
8299 if (enabled_p)
8300 idx = (selected_p
8301 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8302 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8303 else
8304 idx = (selected_p
8305 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8306 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8308 xassert (ASIZE (image) >= idx);
8309 image = AREF (image, idx);
8311 else
8312 idx = -1;
8314 /* Ignore invalid image specifications. */
8315 if (!valid_image_p (image))
8316 continue;
8318 /* Display the tool-bar button pressed, or depressed. */
8319 plist = Fcopy_sequence (XCDR (image));
8321 /* Compute margin and relief to draw. */
8322 relief = (tool_bar_button_relief >= 0
8323 ? tool_bar_button_relief
8324 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8325 hmargin = vmargin = relief;
8327 if (INTEGERP (Vtool_bar_button_margin)
8328 && XINT (Vtool_bar_button_margin) > 0)
8330 hmargin += XFASTINT (Vtool_bar_button_margin);
8331 vmargin += XFASTINT (Vtool_bar_button_margin);
8333 else if (CONSP (Vtool_bar_button_margin))
8335 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8336 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8337 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8339 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8340 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8341 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8344 if (auto_raise_tool_bar_buttons_p)
8346 /* Add a `:relief' property to the image spec if the item is
8347 selected. */
8348 if (selected_p)
8350 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8351 hmargin -= relief;
8352 vmargin -= relief;
8355 else
8357 /* If image is selected, display it pressed, i.e. with a
8358 negative relief. If it's not selected, display it with a
8359 raised relief. */
8360 plist = Fplist_put (plist, QCrelief,
8361 (selected_p
8362 ? make_number (-relief)
8363 : make_number (relief)));
8364 hmargin -= relief;
8365 vmargin -= relief;
8368 /* Put a margin around the image. */
8369 if (hmargin || vmargin)
8371 if (hmargin == vmargin)
8372 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8373 else
8374 plist = Fplist_put (plist, QCmargin,
8375 Fcons (make_number (hmargin),
8376 make_number (vmargin)));
8379 /* If button is not enabled, and we don't have special images
8380 for the disabled state, make the image appear disabled by
8381 applying an appropriate algorithm to it. */
8382 if (!enabled_p && idx < 0)
8383 plist = Fplist_put (plist, QCconversion, Qdisabled);
8385 /* Put a `display' text property on the string for the image to
8386 display. Put a `menu-item' property on the string that gives
8387 the start of this item's properties in the tool-bar items
8388 vector. */
8389 image = Fcons (Qimage, plist);
8390 props = list4 (Qdisplay, image,
8391 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8393 /* Let the last image hide all remaining spaces in the tool bar
8394 string. The string can be longer than needed when we reuse a
8395 previous string. */
8396 if (i + 1 == f->n_tool_bar_items)
8397 end = SCHARS (f->desired_tool_bar_string);
8398 else
8399 end = i + 1;
8400 Fadd_text_properties (make_number (i), make_number (end),
8401 props, f->desired_tool_bar_string);
8402 #undef PROP
8405 UNGCPRO;
8409 /* Display one line of the tool-bar of frame IT->f. */
8411 static void
8412 display_tool_bar_line (it)
8413 struct it *it;
8415 struct glyph_row *row = it->glyph_row;
8416 int max_x = it->last_visible_x;
8417 struct glyph *last;
8419 prepare_desired_row (row);
8420 row->y = it->current_y;
8422 /* Note that this isn't made use of if the face hasn't a box,
8423 so there's no need to check the face here. */
8424 it->start_of_box_run_p = 1;
8426 while (it->current_x < max_x)
8428 int x_before, x, n_glyphs_before, i, nglyphs;
8430 /* Get the next display element. */
8431 if (!get_next_display_element (it))
8432 break;
8434 /* Produce glyphs. */
8435 x_before = it->current_x;
8436 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8437 PRODUCE_GLYPHS (it);
8439 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8440 i = 0;
8441 x = x_before;
8442 while (i < nglyphs)
8444 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8446 if (x + glyph->pixel_width > max_x)
8448 /* Glyph doesn't fit on line. */
8449 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8450 it->current_x = x;
8451 goto out;
8454 ++it->hpos;
8455 x += glyph->pixel_width;
8456 ++i;
8459 /* Stop at line ends. */
8460 if (ITERATOR_AT_END_OF_LINE_P (it))
8461 break;
8463 set_iterator_to_next (it, 1);
8466 out:;
8468 row->displays_text_p = row->used[TEXT_AREA] != 0;
8469 extend_face_to_end_of_line (it);
8470 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8471 last->right_box_line_p = 1;
8472 if (last == row->glyphs[TEXT_AREA])
8473 last->left_box_line_p = 1;
8474 compute_line_metrics (it);
8476 /* If line is empty, make it occupy the rest of the tool-bar. */
8477 if (!row->displays_text_p)
8479 row->height = row->phys_height = it->last_visible_y - row->y;
8480 row->ascent = row->phys_ascent = 0;
8483 row->full_width_p = 1;
8484 row->continued_p = 0;
8485 row->truncated_on_left_p = 0;
8486 row->truncated_on_right_p = 0;
8488 it->current_x = it->hpos = 0;
8489 it->current_y += row->height;
8490 ++it->vpos;
8491 ++it->glyph_row;
8495 /* Value is the number of screen lines needed to make all tool-bar
8496 items of frame F visible. */
8498 static int
8499 tool_bar_lines_needed (f)
8500 struct frame *f;
8502 struct window *w = XWINDOW (f->tool_bar_window);
8503 struct it it;
8505 /* Initialize an iterator for iteration over
8506 F->desired_tool_bar_string in the tool-bar window of frame F. */
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_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8510 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8512 while (!ITERATOR_AT_END_P (&it))
8514 it.glyph_row = w->desired_matrix->rows;
8515 clear_glyph_row (it.glyph_row);
8516 display_tool_bar_line (&it);
8519 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8523 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8524 0, 1, 0,
8525 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8526 (frame)
8527 Lisp_Object frame;
8529 struct frame *f;
8530 struct window *w;
8531 int nlines = 0;
8533 if (NILP (frame))
8534 frame = selected_frame;
8535 else
8536 CHECK_FRAME (frame);
8537 f = XFRAME (frame);
8539 if (WINDOWP (f->tool_bar_window)
8540 || (w = XWINDOW (f->tool_bar_window),
8541 WINDOW_TOTAL_LINES (w) > 0))
8543 update_tool_bar (f, 1);
8544 if (f->n_tool_bar_items)
8546 build_desired_tool_bar_string (f);
8547 nlines = tool_bar_lines_needed (f);
8551 return make_number (nlines);
8555 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8556 height should be changed. */
8558 static int
8559 redisplay_tool_bar (f)
8560 struct frame *f;
8562 struct window *w;
8563 struct it it;
8564 struct glyph_row *row;
8565 int change_height_p = 0;
8567 #ifdef USE_GTK
8568 if (FRAME_EXTERNAL_TOOL_BAR (f))
8569 update_frame_tool_bar (f);
8570 return 0;
8571 #endif
8573 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8574 do anything. This means you must start with tool-bar-lines
8575 non-zero to get the auto-sizing effect. Or in other words, you
8576 can turn off tool-bars by specifying tool-bar-lines zero. */
8577 if (!WINDOWP (f->tool_bar_window)
8578 || (w = XWINDOW (f->tool_bar_window),
8579 WINDOW_TOTAL_LINES (w) == 0))
8580 return 0;
8582 /* Set up an iterator for the tool-bar window. */
8583 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8584 it.first_visible_x = 0;
8585 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8586 row = it.glyph_row;
8588 /* Build a string that represents the contents of the tool-bar. */
8589 build_desired_tool_bar_string (f);
8590 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8592 /* Display as many lines as needed to display all tool-bar items. */
8593 while (it.current_y < it.last_visible_y)
8594 display_tool_bar_line (&it);
8596 /* It doesn't make much sense to try scrolling in the tool-bar
8597 window, so don't do it. */
8598 w->desired_matrix->no_scrolling_p = 1;
8599 w->must_be_updated_p = 1;
8601 if (auto_resize_tool_bars_p)
8603 int nlines;
8605 /* If we couldn't display everything, change the tool-bar's
8606 height. */
8607 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8608 change_height_p = 1;
8610 /* If there are blank lines at the end, except for a partially
8611 visible blank line at the end that is smaller than
8612 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8613 row = it.glyph_row - 1;
8614 if (!row->displays_text_p
8615 && row->height >= FRAME_LINE_HEIGHT (f))
8616 change_height_p = 1;
8618 /* If row displays tool-bar items, but is partially visible,
8619 change the tool-bar's height. */
8620 if (row->displays_text_p
8621 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8622 change_height_p = 1;
8624 /* Resize windows as needed by changing the `tool-bar-lines'
8625 frame parameter. */
8626 if (change_height_p
8627 && (nlines = tool_bar_lines_needed (f),
8628 nlines != WINDOW_TOTAL_LINES (w)))
8630 extern Lisp_Object Qtool_bar_lines;
8631 Lisp_Object frame;
8632 int old_height = WINDOW_TOTAL_LINES (w);
8634 XSETFRAME (frame, f);
8635 clear_glyph_matrix (w->desired_matrix);
8636 Fmodify_frame_parameters (frame,
8637 Fcons (Fcons (Qtool_bar_lines,
8638 make_number (nlines)),
8639 Qnil));
8640 if (WINDOW_TOTAL_LINES (w) != old_height)
8641 fonts_changed_p = 1;
8645 return change_height_p;
8649 /* Get information about the tool-bar item which is displayed in GLYPH
8650 on frame F. Return in *PROP_IDX the index where tool-bar item
8651 properties start in F->tool_bar_items. Value is zero if
8652 GLYPH doesn't display a tool-bar item. */
8654 static int
8655 tool_bar_item_info (f, glyph, prop_idx)
8656 struct frame *f;
8657 struct glyph *glyph;
8658 int *prop_idx;
8660 Lisp_Object prop;
8661 int success_p;
8662 int charpos;
8664 /* This function can be called asynchronously, which means we must
8665 exclude any possibility that Fget_text_property signals an
8666 error. */
8667 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8668 charpos = max (0, charpos);
8670 /* Get the text property `menu-item' at pos. The value of that
8671 property is the start index of this item's properties in
8672 F->tool_bar_items. */
8673 prop = Fget_text_property (make_number (charpos),
8674 Qmenu_item, f->current_tool_bar_string);
8675 if (INTEGERP (prop))
8677 *prop_idx = XINT (prop);
8678 success_p = 1;
8680 else
8681 success_p = 0;
8683 return success_p;
8687 /* Get information about the tool-bar item at position X/Y on frame F.
8688 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8689 the current matrix of the tool-bar window of F, or NULL if not
8690 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8691 item in F->tool_bar_items. Value is
8693 -1 if X/Y is not on a tool-bar item
8694 0 if X/Y is on the same item that was highlighted before.
8695 1 otherwise. */
8697 static int
8698 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8699 struct frame *f;
8700 int x, y;
8701 struct glyph **glyph;
8702 int *hpos, *vpos, *prop_idx;
8704 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8705 struct window *w = XWINDOW (f->tool_bar_window);
8706 int area;
8708 /* Find the glyph under X/Y. */
8709 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8710 if (*glyph == NULL)
8711 return -1;
8713 /* Get the start of this tool-bar item's properties in
8714 f->tool_bar_items. */
8715 if (!tool_bar_item_info (f, *glyph, prop_idx))
8716 return -1;
8718 /* Is mouse on the highlighted item? */
8719 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8720 && *vpos >= dpyinfo->mouse_face_beg_row
8721 && *vpos <= dpyinfo->mouse_face_end_row
8722 && (*vpos > dpyinfo->mouse_face_beg_row
8723 || *hpos >= dpyinfo->mouse_face_beg_col)
8724 && (*vpos < dpyinfo->mouse_face_end_row
8725 || *hpos < dpyinfo->mouse_face_end_col
8726 || dpyinfo->mouse_face_past_end))
8727 return 0;
8729 return 1;
8733 /* EXPORT:
8734 Handle mouse button event on the tool-bar of frame F, at
8735 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8736 0 for button release. MODIFIERS is event modifiers for button
8737 release. */
8739 void
8740 handle_tool_bar_click (f, x, y, down_p, modifiers)
8741 struct frame *f;
8742 int x, y, down_p;
8743 unsigned int modifiers;
8745 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8746 struct window *w = XWINDOW (f->tool_bar_window);
8747 int hpos, vpos, prop_idx;
8748 struct glyph *glyph;
8749 Lisp_Object enabled_p;
8751 /* If not on the highlighted tool-bar item, return. */
8752 frame_to_window_pixel_xy (w, &x, &y);
8753 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8754 return;
8756 /* If item is disabled, do nothing. */
8757 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8758 if (NILP (enabled_p))
8759 return;
8761 if (down_p)
8763 /* Show item in pressed state. */
8764 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8765 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8766 last_tool_bar_item = prop_idx;
8768 else
8770 Lisp_Object key, frame;
8771 struct input_event event;
8772 EVENT_INIT (event);
8774 /* Show item in released state. */
8775 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8776 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8778 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8780 XSETFRAME (frame, f);
8781 event.kind = TOOL_BAR_EVENT;
8782 event.frame_or_window = frame;
8783 event.arg = frame;
8784 kbd_buffer_store_event (&event);
8786 event.kind = TOOL_BAR_EVENT;
8787 event.frame_or_window = frame;
8788 event.arg = key;
8789 event.modifiers = modifiers;
8790 kbd_buffer_store_event (&event);
8791 last_tool_bar_item = -1;
8796 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8797 tool-bar window-relative coordinates X/Y. Called from
8798 note_mouse_highlight. */
8800 static void
8801 note_tool_bar_highlight (f, x, y)
8802 struct frame *f;
8803 int x, y;
8805 Lisp_Object window = f->tool_bar_window;
8806 struct window *w = XWINDOW (window);
8807 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8808 int hpos, vpos;
8809 struct glyph *glyph;
8810 struct glyph_row *row;
8811 int i;
8812 Lisp_Object enabled_p;
8813 int prop_idx;
8814 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8815 int mouse_down_p, rc;
8817 /* Function note_mouse_highlight is called with negative x(y
8818 values when mouse moves outside of the frame. */
8819 if (x <= 0 || y <= 0)
8821 clear_mouse_face (dpyinfo);
8822 return;
8825 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8826 if (rc < 0)
8828 /* Not on tool-bar item. */
8829 clear_mouse_face (dpyinfo);
8830 return;
8832 else if (rc == 0)
8833 /* On same tool-bar item as before. */
8834 goto set_help_echo;
8836 clear_mouse_face (dpyinfo);
8838 /* Mouse is down, but on different tool-bar item? */
8839 mouse_down_p = (dpyinfo->grabbed
8840 && f == last_mouse_frame
8841 && FRAME_LIVE_P (f));
8842 if (mouse_down_p
8843 && last_tool_bar_item != prop_idx)
8844 return;
8846 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8847 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8849 /* If tool-bar item is not enabled, don't highlight it. */
8850 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8851 if (!NILP (enabled_p))
8853 /* Compute the x-position of the glyph. In front and past the
8854 image is a space. We include this in the highlighted area. */
8855 row = MATRIX_ROW (w->current_matrix, vpos);
8856 for (i = x = 0; i < hpos; ++i)
8857 x += row->glyphs[TEXT_AREA][i].pixel_width;
8859 /* Record this as the current active region. */
8860 dpyinfo->mouse_face_beg_col = hpos;
8861 dpyinfo->mouse_face_beg_row = vpos;
8862 dpyinfo->mouse_face_beg_x = x;
8863 dpyinfo->mouse_face_beg_y = row->y;
8864 dpyinfo->mouse_face_past_end = 0;
8866 dpyinfo->mouse_face_end_col = hpos + 1;
8867 dpyinfo->mouse_face_end_row = vpos;
8868 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8869 dpyinfo->mouse_face_end_y = row->y;
8870 dpyinfo->mouse_face_window = window;
8871 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8873 /* Display it as active. */
8874 show_mouse_face (dpyinfo, draw);
8875 dpyinfo->mouse_face_image_state = draw;
8878 set_help_echo:
8880 /* Set help_echo_string to a help string to display for this tool-bar item.
8881 XTread_socket does the rest. */
8882 help_echo_object = help_echo_window = Qnil;
8883 help_echo_pos = -1;
8884 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8885 if (NILP (help_echo_string))
8886 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8889 #endif /* HAVE_WINDOW_SYSTEM */
8893 /***********************************************************************
8894 Fringes
8895 ***********************************************************************/
8897 #ifdef HAVE_WINDOW_SYSTEM
8899 /* An arrow like this: `<-'. */
8900 static unsigned char left_bits[] = {
8901 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8903 /* Right truncation arrow bitmap `->'. */
8904 static unsigned char right_bits[] = {
8905 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8907 /* Marker for continued lines. */
8908 static unsigned char continued_bits[] = {
8909 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8911 /* Marker for continuation lines. */
8912 static unsigned char continuation_bits[] = {
8913 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8915 /* Overlay arrow bitmap. A triangular arrow. */
8916 static unsigned char ov_bits[] = {
8917 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8919 /* Bitmap drawn to indicate lines not displaying text if
8920 `indicate-empty-lines' is non-nil. */
8921 static unsigned char zv_bits[] = {
8922 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8923 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8924 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8925 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8926 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8927 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8928 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8929 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8931 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8933 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8934 { 8, sizeof (left_bits), 0, left_bits },
8935 { 8, sizeof (right_bits), 0, right_bits },
8936 { 8, sizeof (continued_bits), 0, continued_bits },
8937 { 8, sizeof (continuation_bits), 0, continuation_bits },
8938 { 8, sizeof (ov_bits), 0, ov_bits },
8939 { 8, sizeof (zv_bits), 3, zv_bits }
8943 /* Draw the bitmap WHICH in one of the left or right fringes of
8944 window W. ROW is the glyph row for which to display the bitmap; it
8945 determines the vertical position at which the bitmap has to be
8946 drawn. */
8948 static void
8949 draw_fringe_bitmap (w, row, which, left_p)
8950 struct window *w;
8951 struct glyph_row *row;
8952 enum fringe_bitmap_type which;
8953 int left_p;
8955 struct frame *f = XFRAME (WINDOW_FRAME (w));
8956 struct draw_fringe_bitmap_params p;
8958 /* Convert row to frame coordinates. */
8959 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8961 p.which = which;
8962 p.wd = fringe_bitmaps[which].width;
8964 p.h = fringe_bitmaps[which].height;
8965 p.dh = (fringe_bitmaps[which].period
8966 ? (p.y % fringe_bitmaps[which].period)
8967 : 0);
8968 p.h -= p.dh;
8969 /* Clip bitmap if too high. */
8970 if (p.h > row->height)
8971 p.h = row->height;
8973 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8974 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8976 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8977 the fringe. */
8978 p.bx = -1;
8979 if (left_p)
8981 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8982 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8983 ? LEFT_MARGIN_AREA
8984 : TEXT_AREA));
8985 if (p.wd > wd)
8986 p.wd = wd;
8987 p.x = x - p.wd - (wd - p.wd) / 2;
8989 if (p.wd < wd || row->height > p.h)
8991 /* If W has a vertical border to its left, don't draw over it. */
8992 wd -= ((!WINDOW_LEFTMOST_P (w)
8993 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8994 ? 1 : 0);
8995 p.bx = x - wd;
8996 p.nx = wd;
8999 else
9001 int x = window_box_right (w,
9002 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
9003 ? RIGHT_MARGIN_AREA
9004 : TEXT_AREA));
9005 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
9006 if (p.wd > wd)
9007 p.wd = wd;
9008 p.x = x + (wd - p.wd) / 2;
9009 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
9010 the fringe. */
9011 if (p.wd < wd || row->height > p.h)
9013 p.bx = x;
9014 p.nx = wd;
9018 if (p.bx >= 0)
9020 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
9022 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
9023 p.ny = row->visible_height;
9026 /* Adjust y to the offset in the row to start drawing the bitmap. */
9027 p.y += (row->height - p.h) / 2;
9029 FRAME_RIF (f)->draw_fringe_bitmap (w, row, &p);
9032 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9033 function with input blocked. */
9035 void
9036 draw_row_fringe_bitmaps (w, row)
9037 struct window *w;
9038 struct glyph_row *row;
9040 enum fringe_bitmap_type bitmap;
9042 xassert (interrupt_input_blocked);
9044 /* If row is completely invisible, because of vscrolling, we
9045 don't have to draw anything. */
9046 if (row->visible_height <= 0)
9047 return;
9049 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9051 /* Decide which bitmap to draw in the left fringe. */
9052 if (row->overlay_arrow_p)
9053 bitmap = OVERLAY_ARROW_BITMAP;
9054 else if (row->truncated_on_left_p)
9055 bitmap = LEFT_TRUNCATION_BITMAP;
9056 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9057 bitmap = CONTINUATION_LINE_BITMAP;
9058 else if (row->indicate_empty_line_p)
9059 bitmap = ZV_LINE_BITMAP;
9060 else
9061 bitmap = NO_FRINGE_BITMAP;
9063 draw_fringe_bitmap (w, row, bitmap, 1);
9066 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9068 /* Decide which bitmap to draw in the right fringe. */
9069 if (row->truncated_on_right_p)
9070 bitmap = RIGHT_TRUNCATION_BITMAP;
9071 else if (row->continued_p)
9072 bitmap = CONTINUED_LINE_BITMAP;
9073 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9074 bitmap = ZV_LINE_BITMAP;
9075 else
9076 bitmap = NO_FRINGE_BITMAP;
9078 draw_fringe_bitmap (w, row, bitmap, 0);
9083 /* Compute actual fringe widths for frame F.
9085 If REDRAW is 1, redraw F if the fringe settings was actually
9086 modified and F is visible.
9088 Since the combined left and right fringe must occupy an integral
9089 number of columns, we may need to add some pixels to each fringe.
9090 Typically, we add an equal amount (+/- 1 pixel) to each fringe,
9091 but a negative width value is taken literally (after negating it).
9093 We never make the fringes narrower than specified. It is planned
9094 to make fringe bitmaps customizable and expandable, and at that
9095 time, the user will typically specify the minimum number of pixels
9096 needed for his bitmaps, so we shouldn't select anything less than
9097 what is specified.
9100 void
9101 compute_fringe_widths (f, redraw)
9102 struct frame *f;
9103 int redraw;
9105 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9106 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9107 int o_cols = FRAME_FRINGE_COLS (f);
9109 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9110 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9111 int left_fringe_width, right_fringe_width;
9113 if (!NILP (left_fringe))
9114 left_fringe = Fcdr (left_fringe);
9115 if (!NILP (right_fringe))
9116 right_fringe = Fcdr (right_fringe);
9118 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9119 XINT (left_fringe));
9120 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9121 XINT (right_fringe));
9123 if (left_fringe_width || right_fringe_width)
9125 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9126 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9127 int conf_wid = left_wid + right_wid;
9128 int font_wid = FRAME_COLUMN_WIDTH (f);
9129 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9130 int real_wid = cols * font_wid;
9131 if (left_wid && right_wid)
9133 if (left_fringe_width < 0)
9135 /* Left fringe width is fixed, adjust right fringe if necessary */
9136 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9137 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9139 else if (right_fringe_width < 0)
9141 /* Right fringe width is fixed, adjust left fringe if necessary */
9142 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9143 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9145 else
9147 /* Adjust both fringes with an equal amount.
9148 Note that we are doing integer arithmetic here, so don't
9149 lose a pixel if the total width is an odd number. */
9150 int fill = real_wid - conf_wid;
9151 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9152 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9155 else if (left_fringe_width)
9157 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9158 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9160 else
9162 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9163 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9165 FRAME_FRINGE_COLS (f) = cols;
9167 else
9169 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9170 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9171 FRAME_FRINGE_COLS (f) = 0;
9174 if (redraw && FRAME_VISIBLE_P (f))
9175 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9176 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9177 o_cols != FRAME_FRINGE_COLS (f))
9178 redraw_frame (f);
9181 #endif /* HAVE_WINDOW_SYSTEM */
9185 /************************************************************************
9186 Horizontal scrolling
9187 ************************************************************************/
9189 static int hscroll_window_tree P_ ((Lisp_Object));
9190 static int hscroll_windows P_ ((Lisp_Object));
9192 /* For all leaf windows in the window tree rooted at WINDOW, set their
9193 hscroll value so that PT is (i) visible in the window, and (ii) so
9194 that it is not within a certain margin at the window's left and
9195 right border. Value is non-zero if any window's hscroll has been
9196 changed. */
9198 static int
9199 hscroll_window_tree (window)
9200 Lisp_Object window;
9202 int hscrolled_p = 0;
9203 int hscroll_relative_p = FLOATP (Vhscroll_step);
9204 int hscroll_step_abs = 0;
9205 double hscroll_step_rel = 0;
9207 if (hscroll_relative_p)
9209 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9210 if (hscroll_step_rel < 0)
9212 hscroll_relative_p = 0;
9213 hscroll_step_abs = 0;
9216 else if (INTEGERP (Vhscroll_step))
9218 hscroll_step_abs = XINT (Vhscroll_step);
9219 if (hscroll_step_abs < 0)
9220 hscroll_step_abs = 0;
9222 else
9223 hscroll_step_abs = 0;
9225 while (WINDOWP (window))
9227 struct window *w = XWINDOW (window);
9229 if (WINDOWP (w->hchild))
9230 hscrolled_p |= hscroll_window_tree (w->hchild);
9231 else if (WINDOWP (w->vchild))
9232 hscrolled_p |= hscroll_window_tree (w->vchild);
9233 else if (w->cursor.vpos >= 0)
9235 int h_margin;
9236 int text_area_width;
9237 struct glyph_row *current_cursor_row
9238 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9239 struct glyph_row *desired_cursor_row
9240 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9241 struct glyph_row *cursor_row
9242 = (desired_cursor_row->enabled_p
9243 ? desired_cursor_row
9244 : current_cursor_row);
9246 text_area_width = window_box_width (w, TEXT_AREA);
9248 /* Scroll when cursor is inside this scroll margin. */
9249 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9251 if ((XFASTINT (w->hscroll)
9252 && w->cursor.x <= h_margin)
9253 || (cursor_row->enabled_p
9254 && cursor_row->truncated_on_right_p
9255 && (w->cursor.x >= text_area_width - h_margin)))
9257 struct it it;
9258 int hscroll;
9259 struct buffer *saved_current_buffer;
9260 int pt;
9261 int wanted_x;
9263 /* Find point in a display of infinite width. */
9264 saved_current_buffer = current_buffer;
9265 current_buffer = XBUFFER (w->buffer);
9267 if (w == XWINDOW (selected_window))
9268 pt = BUF_PT (current_buffer);
9269 else
9271 pt = marker_position (w->pointm);
9272 pt = max (BEGV, pt);
9273 pt = min (ZV, pt);
9276 /* Move iterator to pt starting at cursor_row->start in
9277 a line with infinite width. */
9278 init_to_row_start (&it, w, cursor_row);
9279 it.last_visible_x = INFINITY;
9280 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9281 current_buffer = saved_current_buffer;
9283 /* Position cursor in window. */
9284 if (!hscroll_relative_p && hscroll_step_abs == 0)
9285 hscroll = max (0, it.current_x - text_area_width / 2)
9286 / FRAME_COLUMN_WIDTH (it.f);
9287 else if (w->cursor.x >= text_area_width - h_margin)
9289 if (hscroll_relative_p)
9290 wanted_x = text_area_width * (1 - hscroll_step_rel)
9291 - h_margin;
9292 else
9293 wanted_x = text_area_width
9294 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9295 - h_margin;
9296 hscroll
9297 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9299 else
9301 if (hscroll_relative_p)
9302 wanted_x = text_area_width * hscroll_step_rel
9303 + h_margin;
9304 else
9305 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9306 + h_margin;
9307 hscroll
9308 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9310 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9312 /* Don't call Fset_window_hscroll if value hasn't
9313 changed because it will prevent redisplay
9314 optimizations. */
9315 if (XFASTINT (w->hscroll) != hscroll)
9317 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9318 w->hscroll = make_number (hscroll);
9319 hscrolled_p = 1;
9324 window = w->next;
9327 /* Value is non-zero if hscroll of any leaf window has been changed. */
9328 return hscrolled_p;
9332 /* Set hscroll so that cursor is visible and not inside horizontal
9333 scroll margins for all windows in the tree rooted at WINDOW. See
9334 also hscroll_window_tree above. Value is non-zero if any window's
9335 hscroll has been changed. If it has, desired matrices on the frame
9336 of WINDOW are cleared. */
9338 static int
9339 hscroll_windows (window)
9340 Lisp_Object window;
9342 int hscrolled_p;
9344 if (automatic_hscrolling_p)
9346 hscrolled_p = hscroll_window_tree (window);
9347 if (hscrolled_p)
9348 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9350 else
9351 hscrolled_p = 0;
9352 return hscrolled_p;
9357 /************************************************************************
9358 Redisplay
9359 ************************************************************************/
9361 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9362 to a non-zero value. This is sometimes handy to have in a debugger
9363 session. */
9365 #if GLYPH_DEBUG
9367 /* First and last unchanged row for try_window_id. */
9369 int debug_first_unchanged_at_end_vpos;
9370 int debug_last_unchanged_at_beg_vpos;
9372 /* Delta vpos and y. */
9374 int debug_dvpos, debug_dy;
9376 /* Delta in characters and bytes for try_window_id. */
9378 int debug_delta, debug_delta_bytes;
9380 /* Values of window_end_pos and window_end_vpos at the end of
9381 try_window_id. */
9383 EMACS_INT debug_end_pos, debug_end_vpos;
9385 /* Append a string to W->desired_matrix->method. FMT is a printf
9386 format string. A1...A9 are a supplement for a variable-length
9387 argument list. If trace_redisplay_p is non-zero also printf the
9388 resulting string to stderr. */
9390 static void
9391 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9392 struct window *w;
9393 char *fmt;
9394 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9396 char buffer[512];
9397 char *method = w->desired_matrix->method;
9398 int len = strlen (method);
9399 int size = sizeof w->desired_matrix->method;
9400 int remaining = size - len - 1;
9402 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9403 if (len && remaining)
9405 method[len] = '|';
9406 --remaining, ++len;
9409 strncpy (method + len, buffer, remaining);
9411 if (trace_redisplay_p)
9412 fprintf (stderr, "%p (%s): %s\n",
9414 ((BUFFERP (w->buffer)
9415 && STRINGP (XBUFFER (w->buffer)->name))
9416 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9417 : "no buffer"),
9418 buffer);
9421 #endif /* GLYPH_DEBUG */
9424 /* Value is non-zero if all changes in window W, which displays
9425 current_buffer, are in the text between START and END. START is a
9426 buffer position, END is given as a distance from Z. Used in
9427 redisplay_internal for display optimization. */
9429 static INLINE int
9430 text_outside_line_unchanged_p (w, start, end)
9431 struct window *w;
9432 int start, end;
9434 int unchanged_p = 1;
9436 /* If text or overlays have changed, see where. */
9437 if (XFASTINT (w->last_modified) < MODIFF
9438 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9440 /* Gap in the line? */
9441 if (GPT < start || Z - GPT < end)
9442 unchanged_p = 0;
9444 /* Changes start in front of the line, or end after it? */
9445 if (unchanged_p
9446 && (BEG_UNCHANGED < start - 1
9447 || END_UNCHANGED < end))
9448 unchanged_p = 0;
9450 /* If selective display, can't optimize if changes start at the
9451 beginning of the line. */
9452 if (unchanged_p
9453 && INTEGERP (current_buffer->selective_display)
9454 && XINT (current_buffer->selective_display) > 0
9455 && (BEG_UNCHANGED < start || GPT <= start))
9456 unchanged_p = 0;
9458 /* If there are overlays at the start or end of the line, these
9459 may have overlay strings with newlines in them. A change at
9460 START, for instance, may actually concern the display of such
9461 overlay strings as well, and they are displayed on different
9462 lines. So, quickly rule out this case. (For the future, it
9463 might be desirable to implement something more telling than
9464 just BEG/END_UNCHANGED.) */
9465 if (unchanged_p)
9467 if (BEG + BEG_UNCHANGED == start
9468 && overlay_touches_p (start))
9469 unchanged_p = 0;
9470 if (END_UNCHANGED == end
9471 && overlay_touches_p (Z - end))
9472 unchanged_p = 0;
9476 return unchanged_p;
9480 /* Do a frame update, taking possible shortcuts into account. This is
9481 the main external entry point for redisplay.
9483 If the last redisplay displayed an echo area message and that message
9484 is no longer requested, we clear the echo area or bring back the
9485 mini-buffer if that is in use. */
9487 void
9488 redisplay ()
9490 redisplay_internal (0);
9494 /* Return 1 if point moved out of or into a composition. Otherwise
9495 return 0. PREV_BUF and PREV_PT are the last point buffer and
9496 position. BUF and PT are the current point buffer and position. */
9499 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9500 struct buffer *prev_buf, *buf;
9501 int prev_pt, pt;
9503 int start, end;
9504 Lisp_Object prop;
9505 Lisp_Object buffer;
9507 XSETBUFFER (buffer, buf);
9508 /* Check a composition at the last point if point moved within the
9509 same buffer. */
9510 if (prev_buf == buf)
9512 if (prev_pt == pt)
9513 /* Point didn't move. */
9514 return 0;
9516 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9517 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9518 && COMPOSITION_VALID_P (start, end, prop)
9519 && start < prev_pt && end > prev_pt)
9520 /* The last point was within the composition. Return 1 iff
9521 point moved out of the composition. */
9522 return (pt <= start || pt >= end);
9525 /* Check a composition at the current point. */
9526 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9527 && find_composition (pt, -1, &start, &end, &prop, buffer)
9528 && COMPOSITION_VALID_P (start, end, prop)
9529 && start < pt && end > pt);
9533 /* Reconsider the setting of B->clip_changed which is displayed
9534 in window W. */
9536 static INLINE void
9537 reconsider_clip_changes (w, b)
9538 struct window *w;
9539 struct buffer *b;
9541 if (b->clip_changed
9542 && !NILP (w->window_end_valid)
9543 && w->current_matrix->buffer == b
9544 && w->current_matrix->zv == BUF_ZV (b)
9545 && w->current_matrix->begv == BUF_BEGV (b))
9546 b->clip_changed = 0;
9548 /* If display wasn't paused, and W is not a tool bar window, see if
9549 point has been moved into or out of a composition. In that case,
9550 we set b->clip_changed to 1 to force updating the screen. If
9551 b->clip_changed has already been set to 1, we can skip this
9552 check. */
9553 if (!b->clip_changed
9554 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9556 int pt;
9558 if (w == XWINDOW (selected_window))
9559 pt = BUF_PT (current_buffer);
9560 else
9561 pt = marker_position (w->pointm);
9563 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9564 || pt != XINT (w->last_point))
9565 && check_point_in_composition (w->current_matrix->buffer,
9566 XINT (w->last_point),
9567 XBUFFER (w->buffer), pt))
9568 b->clip_changed = 1;
9573 /* Select FRAME to forward the values of frame-local variables into C
9574 variables so that the redisplay routines can access those values
9575 directly. */
9577 static void
9578 select_frame_for_redisplay (frame)
9579 Lisp_Object frame;
9581 Lisp_Object tail, sym, val;
9582 Lisp_Object old = selected_frame;
9584 selected_frame = frame;
9586 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9587 if (CONSP (XCAR (tail))
9588 && (sym = XCAR (XCAR (tail)),
9589 SYMBOLP (sym))
9590 && (sym = indirect_variable (sym),
9591 val = SYMBOL_VALUE (sym),
9592 (BUFFER_LOCAL_VALUEP (val)
9593 || SOME_BUFFER_LOCAL_VALUEP (val)))
9594 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9595 Fsymbol_value (sym);
9597 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9598 if (CONSP (XCAR (tail))
9599 && (sym = XCAR (XCAR (tail)),
9600 SYMBOLP (sym))
9601 && (sym = indirect_variable (sym),
9602 val = SYMBOL_VALUE (sym),
9603 (BUFFER_LOCAL_VALUEP (val)
9604 || SOME_BUFFER_LOCAL_VALUEP (val)))
9605 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9606 Fsymbol_value (sym);
9610 #define STOP_POLLING \
9611 do { if (! polling_stopped_here) stop_polling (); \
9612 polling_stopped_here = 1; } while (0)
9614 #define RESUME_POLLING \
9615 do { if (polling_stopped_here) start_polling (); \
9616 polling_stopped_here = 0; } while (0)
9619 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9620 response to any user action; therefore, we should preserve the echo
9621 area. (Actually, our caller does that job.) Perhaps in the future
9622 avoid recentering windows if it is not necessary; currently that
9623 causes some problems. */
9625 static void
9626 redisplay_internal (preserve_echo_area)
9627 int preserve_echo_area;
9629 struct window *w = XWINDOW (selected_window);
9630 struct frame *f = XFRAME (w->frame);
9631 int pause;
9632 int must_finish = 0;
9633 struct text_pos tlbufpos, tlendpos;
9634 int number_of_visible_frames;
9635 int count;
9636 struct frame *sf = SELECTED_FRAME ();
9637 int polling_stopped_here = 0;
9639 /* Non-zero means redisplay has to consider all windows on all
9640 frames. Zero means, only selected_window is considered. */
9641 int consider_all_windows_p;
9643 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9645 /* No redisplay if running in batch mode or frame is not yet fully
9646 initialized, or redisplay is explicitly turned off by setting
9647 Vinhibit_redisplay. */
9648 if (noninteractive
9649 || !NILP (Vinhibit_redisplay)
9650 || !f->glyphs_initialized_p)
9651 return;
9653 /* The flag redisplay_performed_directly_p is set by
9654 direct_output_for_insert when it already did the whole screen
9655 update necessary. */
9656 if (redisplay_performed_directly_p)
9658 redisplay_performed_directly_p = 0;
9659 if (!hscroll_windows (selected_window))
9660 return;
9663 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9664 if (popup_activated ())
9665 return;
9666 #endif
9668 /* I don't think this happens but let's be paranoid. */
9669 if (redisplaying_p)
9670 return;
9672 /* Record a function that resets redisplaying_p to its old value
9673 when we leave this function. */
9674 count = SPECPDL_INDEX ();
9675 record_unwind_protect (unwind_redisplay,
9676 Fcons (make_number (redisplaying_p), selected_frame));
9677 ++redisplaying_p;
9678 specbind (Qinhibit_free_realized_faces, Qnil);
9680 retry:
9681 pause = 0;
9682 reconsider_clip_changes (w, current_buffer);
9684 /* If new fonts have been loaded that make a glyph matrix adjustment
9685 necessary, do it. */
9686 if (fonts_changed_p)
9688 adjust_glyphs (NULL);
9689 ++windows_or_buffers_changed;
9690 fonts_changed_p = 0;
9693 /* If face_change_count is non-zero, init_iterator will free all
9694 realized faces, which includes the faces referenced from current
9695 matrices. So, we can't reuse current matrices in this case. */
9696 if (face_change_count)
9697 ++windows_or_buffers_changed;
9699 if (FRAME_TERMCAP_P (sf)
9700 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9702 /* Since frames on a single ASCII terminal share the same
9703 display area, displaying a different frame means redisplay
9704 the whole thing. */
9705 windows_or_buffers_changed++;
9706 SET_FRAME_GARBAGED (sf);
9707 FRAME_TTY (sf)->previous_terminal_frame = sf;
9710 /* Set the visible flags for all frames. Do this before checking
9711 for resized or garbaged frames; they want to know if their frames
9712 are visible. See the comment in frame.h for
9713 FRAME_SAMPLE_VISIBILITY. */
9715 Lisp_Object tail, frame;
9717 number_of_visible_frames = 0;
9719 FOR_EACH_FRAME (tail, frame)
9721 struct frame *f = XFRAME (frame);
9723 FRAME_SAMPLE_VISIBILITY (f);
9724 if (FRAME_VISIBLE_P (f))
9725 ++number_of_visible_frames;
9726 clear_desired_matrices (f);
9731 /* Notice any pending interrupt request to change frame size. */
9732 do_pending_window_change (1);
9734 /* Clear frames marked as garbaged. */
9735 if (frame_garbaged)
9736 clear_garbaged_frames ();
9738 /* Build menubar and tool-bar items. */
9739 prepare_menu_bars ();
9741 if (windows_or_buffers_changed)
9742 update_mode_lines++;
9744 /* Detect case that we need to write or remove a star in the mode line. */
9745 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9747 w->update_mode_line = Qt;
9748 if (buffer_shared > 1)
9749 update_mode_lines++;
9752 /* If %c is in the mode line, update it if needed. */
9753 if (!NILP (w->column_number_displayed)
9754 /* This alternative quickly identifies a common case
9755 where no change is needed. */
9756 && !(PT == XFASTINT (w->last_point)
9757 && XFASTINT (w->last_modified) >= MODIFF
9758 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9759 && (XFASTINT (w->column_number_displayed)
9760 != (int) current_column ())) /* iftc */
9761 w->update_mode_line = Qt;
9763 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9765 /* The variable buffer_shared is set in redisplay_window and
9766 indicates that we redisplay a buffer in different windows. See
9767 there. */
9768 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9769 || cursor_type_changed);
9771 /* If specs for an arrow have changed, do thorough redisplay
9772 to ensure we remove any arrow that should no longer exist. */
9773 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9774 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9775 consider_all_windows_p = windows_or_buffers_changed = 1;
9777 /* Normally the message* functions will have already displayed and
9778 updated the echo area, but the frame may have been trashed, or
9779 the update may have been preempted, so display the echo area
9780 again here. Checking message_cleared_p captures the case that
9781 the echo area should be cleared. */
9782 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9783 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9784 || (message_cleared_p
9785 && minibuf_level == 0
9786 /* If the mini-window is currently selected, this means the
9787 echo-area doesn't show through. */
9788 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9790 int window_height_changed_p = echo_area_display (0);
9791 must_finish = 1;
9793 /* If we don't display the current message, don't clear the
9794 message_cleared_p flag, because, if we did, we wouldn't clear
9795 the echo area in the next redisplay which doesn't preserve
9796 the echo area. */
9797 if (!display_last_displayed_message_p)
9798 message_cleared_p = 0;
9800 if (fonts_changed_p)
9801 goto retry;
9802 else if (window_height_changed_p)
9804 consider_all_windows_p = 1;
9805 ++update_mode_lines;
9806 ++windows_or_buffers_changed;
9808 /* If window configuration was changed, frames may have been
9809 marked garbaged. Clear them or we will experience
9810 surprises wrt scrolling. */
9811 if (frame_garbaged)
9812 clear_garbaged_frames ();
9815 else if (EQ (selected_window, minibuf_window)
9816 && (current_buffer->clip_changed
9817 || XFASTINT (w->last_modified) < MODIFF
9818 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9819 && resize_mini_window (w, 0))
9821 /* Resized active mini-window to fit the size of what it is
9822 showing if its contents might have changed. */
9823 must_finish = 1;
9824 consider_all_windows_p = 1;
9825 ++windows_or_buffers_changed;
9826 ++update_mode_lines;
9828 /* If window configuration was changed, frames may have been
9829 marked garbaged. Clear them or we will experience
9830 surprises wrt scrolling. */
9831 if (frame_garbaged)
9832 clear_garbaged_frames ();
9836 /* If showing the region, and mark has changed, we must redisplay
9837 the whole window. The assignment to this_line_start_pos prevents
9838 the optimization directly below this if-statement. */
9839 if (((!NILP (Vtransient_mark_mode)
9840 && !NILP (XBUFFER (w->buffer)->mark_active))
9841 != !NILP (w->region_showing))
9842 || (!NILP (w->region_showing)
9843 && !EQ (w->region_showing,
9844 Fmarker_position (XBUFFER (w->buffer)->mark))))
9845 CHARPOS (this_line_start_pos) = 0;
9847 /* Optimize the case that only the line containing the cursor in the
9848 selected window has changed. Variables starting with this_ are
9849 set in display_line and record information about the line
9850 containing the cursor. */
9851 tlbufpos = this_line_start_pos;
9852 tlendpos = this_line_end_pos;
9853 if (!consider_all_windows_p
9854 && CHARPOS (tlbufpos) > 0
9855 && NILP (w->update_mode_line)
9856 && !current_buffer->clip_changed
9857 && !current_buffer->prevent_redisplay_optimizations_p
9858 && FRAME_VISIBLE_P (XFRAME (w->frame))
9859 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9860 /* Make sure recorded data applies to current buffer, etc. */
9861 && this_line_buffer == current_buffer
9862 && current_buffer == XBUFFER (w->buffer)
9863 && NILP (w->force_start)
9864 && NILP (w->optional_new_start)
9865 /* Point must be on the line that we have info recorded about. */
9866 && PT >= CHARPOS (tlbufpos)
9867 && PT <= Z - CHARPOS (tlendpos)
9868 /* All text outside that line, including its final newline,
9869 must be unchanged */
9870 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9871 CHARPOS (tlendpos)))
9873 if (CHARPOS (tlbufpos) > BEGV
9874 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9875 && (CHARPOS (tlbufpos) == ZV
9876 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9877 /* Former continuation line has disappeared by becoming empty */
9878 goto cancel;
9879 else if (XFASTINT (w->last_modified) < MODIFF
9880 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9881 || MINI_WINDOW_P (w))
9883 /* We have to handle the case of continuation around a
9884 wide-column character (See the comment in indent.c around
9885 line 885).
9887 For instance, in the following case:
9889 -------- Insert --------
9890 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9891 J_I_ ==> J_I_ `^^' are cursors.
9892 ^^ ^^
9893 -------- --------
9895 As we have to redraw the line above, we should goto cancel. */
9897 struct it it;
9898 int line_height_before = this_line_pixel_height;
9900 /* Note that start_display will handle the case that the
9901 line starting at tlbufpos is a continuation lines. */
9902 start_display (&it, w, tlbufpos);
9904 /* Implementation note: It this still necessary? */
9905 if (it.current_x != this_line_start_x)
9906 goto cancel;
9908 TRACE ((stderr, "trying display optimization 1\n"));
9909 w->cursor.vpos = -1;
9910 overlay_arrow_seen = 0;
9911 it.vpos = this_line_vpos;
9912 it.current_y = this_line_y;
9913 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9914 display_line (&it);
9916 /* If line contains point, is not continued,
9917 and ends at same distance from eob as before, we win */
9918 if (w->cursor.vpos >= 0
9919 /* Line is not continued, otherwise this_line_start_pos
9920 would have been set to 0 in display_line. */
9921 && CHARPOS (this_line_start_pos)
9922 /* Line ends as before. */
9923 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9924 /* Line has same height as before. Otherwise other lines
9925 would have to be shifted up or down. */
9926 && this_line_pixel_height == line_height_before)
9928 /* If this is not the window's last line, we must adjust
9929 the charstarts of the lines below. */
9930 if (it.current_y < it.last_visible_y)
9932 struct glyph_row *row
9933 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9934 int delta, delta_bytes;
9936 if (Z - CHARPOS (tlendpos) == ZV)
9938 /* This line ends at end of (accessible part of)
9939 buffer. There is no newline to count. */
9940 delta = (Z
9941 - CHARPOS (tlendpos)
9942 - MATRIX_ROW_START_CHARPOS (row));
9943 delta_bytes = (Z_BYTE
9944 - BYTEPOS (tlendpos)
9945 - MATRIX_ROW_START_BYTEPOS (row));
9947 else
9949 /* This line ends in a newline. Must take
9950 account of the newline and the rest of the
9951 text that follows. */
9952 delta = (Z
9953 - CHARPOS (tlendpos)
9954 - MATRIX_ROW_START_CHARPOS (row));
9955 delta_bytes = (Z_BYTE
9956 - BYTEPOS (tlendpos)
9957 - MATRIX_ROW_START_BYTEPOS (row));
9960 increment_matrix_positions (w->current_matrix,
9961 this_line_vpos + 1,
9962 w->current_matrix->nrows,
9963 delta, delta_bytes);
9966 /* If this row displays text now but previously didn't,
9967 or vice versa, w->window_end_vpos may have to be
9968 adjusted. */
9969 if ((it.glyph_row - 1)->displays_text_p)
9971 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9972 XSETINT (w->window_end_vpos, this_line_vpos);
9974 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9975 && this_line_vpos > 0)
9976 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9977 w->window_end_valid = Qnil;
9979 /* Update hint: No need to try to scroll in update_window. */
9980 w->desired_matrix->no_scrolling_p = 1;
9982 #if GLYPH_DEBUG
9983 *w->desired_matrix->method = 0;
9984 debug_method_add (w, "optimization 1");
9985 #endif
9986 goto update;
9988 else
9989 goto cancel;
9991 else if (/* Cursor position hasn't changed. */
9992 PT == XFASTINT (w->last_point)
9993 /* Make sure the cursor was last displayed
9994 in this window. Otherwise we have to reposition it. */
9995 && 0 <= w->cursor.vpos
9996 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9998 if (!must_finish)
10000 do_pending_window_change (1);
10002 /* We used to always goto end_of_redisplay here, but this
10003 isn't enough if we have a blinking cursor. */
10004 if (w->cursor_off_p == w->last_cursor_off_p)
10005 goto end_of_redisplay;
10007 goto update;
10009 /* If highlighting the region, or if the cursor is in the echo area,
10010 then we can't just move the cursor. */
10011 else if (! (!NILP (Vtransient_mark_mode)
10012 && !NILP (current_buffer->mark_active))
10013 && (EQ (selected_window, current_buffer->last_selected_window)
10014 || highlight_nonselected_windows)
10015 && NILP (w->region_showing)
10016 && NILP (Vshow_trailing_whitespace)
10017 && !cursor_in_echo_area)
10019 struct it it;
10020 struct glyph_row *row;
10022 /* Skip from tlbufpos to PT and see where it is. Note that
10023 PT may be in invisible text. If so, we will end at the
10024 next visible position. */
10025 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10026 NULL, DEFAULT_FACE_ID);
10027 it.current_x = this_line_start_x;
10028 it.current_y = this_line_y;
10029 it.vpos = this_line_vpos;
10031 /* The call to move_it_to stops in front of PT, but
10032 moves over before-strings. */
10033 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10035 if (it.vpos == this_line_vpos
10036 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10037 row->enabled_p))
10039 xassert (this_line_vpos == it.vpos);
10040 xassert (this_line_y == it.current_y);
10041 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10042 #if GLYPH_DEBUG
10043 *w->desired_matrix->method = 0;
10044 debug_method_add (w, "optimization 3");
10045 #endif
10046 goto update;
10048 else
10049 goto cancel;
10052 cancel:
10053 /* Text changed drastically or point moved off of line. */
10054 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10057 CHARPOS (this_line_start_pos) = 0;
10058 consider_all_windows_p |= buffer_shared > 1;
10059 ++clear_face_cache_count;
10062 /* Build desired matrices, and update the display. If
10063 consider_all_windows_p is non-zero, do it for all windows on all
10064 frames. Otherwise do it for selected_window, only. */
10066 if (consider_all_windows_p)
10068 Lisp_Object tail, frame;
10069 int i, n = 0, size = 50;
10070 struct frame **updated
10071 = (struct frame **) alloca (size * sizeof *updated);
10073 /* Clear the face cache eventually. */
10074 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10076 clear_face_cache (0);
10077 clear_face_cache_count = 0;
10080 /* Recompute # windows showing selected buffer. This will be
10081 incremented each time such a window is displayed. */
10082 buffer_shared = 0;
10084 FOR_EACH_FRAME (tail, frame)
10086 struct frame *f = XFRAME (frame);
10088 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10090 if (! EQ (frame, selected_frame))
10091 /* Select the frame, for the sake of frame-local
10092 variables. */
10093 select_frame_for_redisplay (frame);
10095 #ifdef HAVE_WINDOW_SYSTEM
10096 if (clear_face_cache_count % 50 == 0
10097 && FRAME_WINDOW_P (f))
10098 clear_image_cache (f, 0);
10099 #endif /* HAVE_WINDOW_SYSTEM */
10101 /* Mark all the scroll bars to be removed; we'll redeem
10102 the ones we want when we redisplay their windows. */
10103 if (condemn_scroll_bars_hook)
10104 condemn_scroll_bars_hook (f);
10106 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10107 redisplay_windows (FRAME_ROOT_WINDOW (f));
10109 /* Any scroll bars which redisplay_windows should have
10110 nuked should now go away. */
10111 if (judge_scroll_bars_hook)
10112 judge_scroll_bars_hook (f);
10114 /* If fonts changed, display again. */
10115 /* ??? rms: I suspect it is a mistake to jump all the way
10116 back to retry here. It should just retry this frame. */
10117 if (fonts_changed_p)
10118 goto retry;
10120 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10122 /* See if we have to hscroll. */
10123 if (hscroll_windows (f->root_window))
10124 goto retry;
10126 /* Prevent various kinds of signals during display
10127 update. stdio is not robust about handling
10128 signals, which can cause an apparent I/O
10129 error. */
10130 if (interrupt_input)
10131 unrequest_sigio ();
10132 STOP_POLLING;
10134 /* Update the display. */
10135 set_window_update_flags (XWINDOW (f->root_window), 1);
10136 pause |= update_frame (f, 0, 0);
10137 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10138 if (pause)
10139 break;
10140 #endif
10142 if (n == size)
10144 int nbytes = size * sizeof *updated;
10145 struct frame **p = (struct frame **) alloca (2 * nbytes);
10146 bcopy (updated, p, nbytes);
10147 size *= 2;
10150 updated[n++] = f;
10155 if (!pause)
10157 /* Do the mark_window_display_accurate after all windows have
10158 been redisplayed because this call resets flags in buffers
10159 which are needed for proper redisplay. */
10160 for (i = 0; i < n; ++i)
10162 struct frame *f = updated[i];
10163 mark_window_display_accurate (f->root_window, 1);
10164 if (frame_up_to_date_hook)
10165 frame_up_to_date_hook (f);
10169 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10171 Lisp_Object mini_window;
10172 struct frame *mini_frame;
10174 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10175 /* Use list_of_error, not Qerror, so that
10176 we catch only errors and don't run the debugger. */
10177 internal_condition_case_1 (redisplay_window_1, selected_window,
10178 list_of_error,
10179 redisplay_window_error);
10181 /* Compare desired and current matrices, perform output. */
10183 update:
10184 /* If fonts changed, display again. */
10185 if (fonts_changed_p)
10186 goto retry;
10188 /* Prevent various kinds of signals during display update.
10189 stdio is not robust about handling signals,
10190 which can cause an apparent I/O error. */
10191 if (interrupt_input)
10192 unrequest_sigio ();
10193 STOP_POLLING;
10195 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10197 if (hscroll_windows (selected_window))
10198 goto retry;
10200 XWINDOW (selected_window)->must_be_updated_p = 1;
10201 pause = update_frame (sf, 0, 0);
10204 /* We may have called echo_area_display at the top of this
10205 function. If the echo area is on another frame, that may
10206 have put text on a frame other than the selected one, so the
10207 above call to update_frame would not have caught it. Catch
10208 it here. */
10209 mini_window = FRAME_MINIBUF_WINDOW (sf);
10210 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10212 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10214 XWINDOW (mini_window)->must_be_updated_p = 1;
10215 pause |= update_frame (mini_frame, 0, 0);
10216 if (!pause && hscroll_windows (mini_window))
10217 goto retry;
10221 /* If display was paused because of pending input, make sure we do a
10222 thorough update the next time. */
10223 if (pause)
10225 /* Prevent the optimization at the beginning of
10226 redisplay_internal that tries a single-line update of the
10227 line containing the cursor in the selected window. */
10228 CHARPOS (this_line_start_pos) = 0;
10230 /* Let the overlay arrow be updated the next time. */
10231 if (!NILP (last_arrow_position))
10233 last_arrow_position = Qt;
10234 last_arrow_string = Qt;
10237 /* If we pause after scrolling, some rows in the current
10238 matrices of some windows are not valid. */
10239 if (!WINDOW_FULL_WIDTH_P (w)
10240 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10241 update_mode_lines = 1;
10243 else
10245 if (!consider_all_windows_p)
10247 /* This has already been done above if
10248 consider_all_windows_p is set. */
10249 mark_window_display_accurate_1 (w, 1);
10251 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10252 last_arrow_string = Voverlay_arrow_string;
10254 if (frame_up_to_date_hook != 0)
10255 frame_up_to_date_hook (sf);
10258 update_mode_lines = 0;
10259 windows_or_buffers_changed = 0;
10260 cursor_type_changed = 0;
10263 /* Start SIGIO interrupts coming again. Having them off during the
10264 code above makes it less likely one will discard output, but not
10265 impossible, since there might be stuff in the system buffer here.
10266 But it is much hairier to try to do anything about that. */
10267 if (interrupt_input)
10268 request_sigio ();
10269 RESUME_POLLING;
10271 /* If a frame has become visible which was not before, redisplay
10272 again, so that we display it. Expose events for such a frame
10273 (which it gets when becoming visible) don't call the parts of
10274 redisplay constructing glyphs, so simply exposing a frame won't
10275 display anything in this case. So, we have to display these
10276 frames here explicitly. */
10277 if (!pause)
10279 Lisp_Object tail, frame;
10280 int new_count = 0;
10282 FOR_EACH_FRAME (tail, frame)
10284 int this_is_visible = 0;
10286 if (XFRAME (frame)->visible)
10287 this_is_visible = 1;
10288 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10289 if (XFRAME (frame)->visible)
10290 this_is_visible = 1;
10292 if (this_is_visible)
10293 new_count++;
10296 if (new_count != number_of_visible_frames)
10297 windows_or_buffers_changed++;
10300 /* Change frame size now if a change is pending. */
10301 do_pending_window_change (1);
10303 /* If we just did a pending size change, or have additional
10304 visible frames, redisplay again. */
10305 if (windows_or_buffers_changed && !pause)
10306 goto retry;
10308 end_of_redisplay:
10309 unbind_to (count, Qnil);
10310 RESUME_POLLING;
10314 /* Redisplay, but leave alone any recent echo area message unless
10315 another message has been requested in its place.
10317 This is useful in situations where you need to redisplay but no
10318 user action has occurred, making it inappropriate for the message
10319 area to be cleared. See tracking_off and
10320 wait_reading_process_input for examples of these situations.
10322 FROM_WHERE is an integer saying from where this function was
10323 called. This is useful for debugging. */
10325 void
10326 redisplay_preserve_echo_area (from_where)
10327 int from_where;
10329 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10331 if (!NILP (echo_area_buffer[1]))
10333 /* We have a previously displayed message, but no current
10334 message. Redisplay the previous message. */
10335 display_last_displayed_message_p = 1;
10336 redisplay_internal (1);
10337 display_last_displayed_message_p = 0;
10339 else
10340 redisplay_internal (1);
10344 /* Function registered with record_unwind_protect in
10345 redisplay_internal. Reset redisplaying_p to the value it had
10346 before redisplay_internal was called, and clear
10347 prevent_freeing_realized_faces_p. It also selects the previously
10348 selected frame. */
10350 static Lisp_Object
10351 unwind_redisplay (val)
10352 Lisp_Object val;
10354 Lisp_Object old_redisplaying_p, old_frame;
10356 old_redisplaying_p = XCAR (val);
10357 redisplaying_p = XFASTINT (old_redisplaying_p);
10358 old_frame = XCDR (val);
10359 if (! EQ (old_frame, selected_frame))
10360 select_frame_for_redisplay (old_frame);
10361 return Qnil;
10365 /* Mark the display of window W as accurate or inaccurate. If
10366 ACCURATE_P is non-zero mark display of W as accurate. If
10367 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10368 redisplay_internal is called. */
10370 static void
10371 mark_window_display_accurate_1 (w, accurate_p)
10372 struct window *w;
10373 int accurate_p;
10375 if (BUFFERP (w->buffer))
10377 struct buffer *b = XBUFFER (w->buffer);
10379 w->last_modified
10380 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10381 w->last_overlay_modified
10382 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10383 w->last_had_star
10384 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10386 if (accurate_p)
10388 b->clip_changed = 0;
10389 b->prevent_redisplay_optimizations_p = 0;
10391 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10392 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10393 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10394 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10396 w->current_matrix->buffer = b;
10397 w->current_matrix->begv = BUF_BEGV (b);
10398 w->current_matrix->zv = BUF_ZV (b);
10400 w->last_cursor = w->cursor;
10401 w->last_cursor_off_p = w->cursor_off_p;
10403 if (w == XWINDOW (selected_window))
10404 w->last_point = make_number (BUF_PT (b));
10405 else
10406 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10410 if (accurate_p)
10412 w->window_end_valid = w->buffer;
10413 #if 0 /* This is incorrect with variable-height lines. */
10414 xassert (XINT (w->window_end_vpos)
10415 < (WINDOW_TOTAL_LINES (w)
10416 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10417 #endif
10418 w->update_mode_line = Qnil;
10423 /* Mark the display of windows in the window tree rooted at WINDOW as
10424 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10425 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10426 be redisplayed the next time redisplay_internal is called. */
10428 void
10429 mark_window_display_accurate (window, accurate_p)
10430 Lisp_Object window;
10431 int accurate_p;
10433 struct window *w;
10435 for (; !NILP (window); window = w->next)
10437 w = XWINDOW (window);
10438 mark_window_display_accurate_1 (w, accurate_p);
10440 if (!NILP (w->vchild))
10441 mark_window_display_accurate (w->vchild, accurate_p);
10442 if (!NILP (w->hchild))
10443 mark_window_display_accurate (w->hchild, accurate_p);
10446 if (accurate_p)
10448 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10449 last_arrow_string = Voverlay_arrow_string;
10451 else
10453 /* Force a thorough redisplay the next time by setting
10454 last_arrow_position and last_arrow_string to t, which is
10455 unequal to any useful value of Voverlay_arrow_... */
10456 last_arrow_position = Qt;
10457 last_arrow_string = Qt;
10462 /* Return value in display table DP (Lisp_Char_Table *) for character
10463 C. Since a display table doesn't have any parent, we don't have to
10464 follow parent. Do not call this function directly but use the
10465 macro DISP_CHAR_VECTOR. */
10467 Lisp_Object
10468 disp_char_vector (dp, c)
10469 struct Lisp_Char_Table *dp;
10470 int c;
10472 int code[4], i;
10473 Lisp_Object val;
10475 if (SINGLE_BYTE_CHAR_P (c))
10476 return (dp->contents[c]);
10478 SPLIT_CHAR (c, code[0], code[1], code[2]);
10479 if (code[1] < 32)
10480 code[1] = -1;
10481 else if (code[2] < 32)
10482 code[2] = -1;
10484 /* Here, the possible range of code[0] (== charset ID) is
10485 128..max_charset. Since the top level char table contains data
10486 for multibyte characters after 256th element, we must increment
10487 code[0] by 128 to get a correct index. */
10488 code[0] += 128;
10489 code[3] = -1; /* anchor */
10491 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10493 val = dp->contents[code[i]];
10494 if (!SUB_CHAR_TABLE_P (val))
10495 return (NILP (val) ? dp->defalt : val);
10498 /* Here, val is a sub char table. We return the default value of
10499 it. */
10500 return (dp->defalt);
10505 /***********************************************************************
10506 Window Redisplay
10507 ***********************************************************************/
10509 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10511 static void
10512 redisplay_windows (window)
10513 Lisp_Object window;
10515 while (!NILP (window))
10517 struct window *w = XWINDOW (window);
10519 if (!NILP (w->hchild))
10520 redisplay_windows (w->hchild);
10521 else if (!NILP (w->vchild))
10522 redisplay_windows (w->vchild);
10523 else
10525 displayed_buffer = XBUFFER (w->buffer);
10526 /* Use list_of_error, not Qerror, so that
10527 we catch only errors and don't run the debugger. */
10528 internal_condition_case_1 (redisplay_window_0, window,
10529 list_of_error,
10530 redisplay_window_error);
10533 window = w->next;
10537 static Lisp_Object
10538 redisplay_window_error ()
10540 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10541 return Qnil;
10544 static Lisp_Object
10545 redisplay_window_0 (window)
10546 Lisp_Object window;
10548 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10549 redisplay_window (window, 0);
10550 return Qnil;
10553 static Lisp_Object
10554 redisplay_window_1 (window)
10555 Lisp_Object window;
10557 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10558 redisplay_window (window, 1);
10559 return Qnil;
10563 /* Increment GLYPH until it reaches END or CONDITION fails while
10564 adding (GLYPH)->pixel_width to X. */
10566 #define SKIP_GLYPHS(glyph, end, x, condition) \
10567 do \
10569 (x) += (glyph)->pixel_width; \
10570 ++(glyph); \
10572 while ((glyph) < (end) && (condition))
10575 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10576 DELTA is the number of bytes by which positions recorded in ROW
10577 differ from current buffer positions. */
10579 void
10580 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10581 struct window *w;
10582 struct glyph_row *row;
10583 struct glyph_matrix *matrix;
10584 int delta, delta_bytes, dy, dvpos;
10586 struct glyph *glyph = row->glyphs[TEXT_AREA];
10587 struct glyph *end = glyph + row->used[TEXT_AREA];
10588 /* The first glyph that starts a sequence of glyphs from string. */
10589 struct glyph *string_start;
10590 /* The X coordinate of string_start. */
10591 int string_start_x;
10592 /* The last known character position. */
10593 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10594 /* The last known character position before string_start. */
10595 int string_before_pos;
10596 int x = row->x;
10597 int pt_old = PT - delta;
10599 /* Skip over glyphs not having an object at the start of the row.
10600 These are special glyphs like truncation marks on terminal
10601 frames. */
10602 if (row->displays_text_p)
10603 while (glyph < end
10604 && INTEGERP (glyph->object)
10605 && glyph->charpos < 0)
10607 x += glyph->pixel_width;
10608 ++glyph;
10611 string_start = NULL;
10612 while (glyph < end
10613 && !INTEGERP (glyph->object)
10614 && (!BUFFERP (glyph->object)
10615 || (last_pos = glyph->charpos) < pt_old))
10617 if (! STRINGP (glyph->object))
10619 string_start = NULL;
10620 x += glyph->pixel_width;
10621 ++glyph;
10623 else
10625 string_before_pos = last_pos;
10626 string_start = glyph;
10627 string_start_x = x;
10628 /* Skip all glyphs from string. */
10629 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10633 if (string_start
10634 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10636 /* We may have skipped over point because the previous glyphs
10637 are from string. As there's no easy way to know the
10638 character position of the current glyph, find the correct
10639 glyph on point by scanning from string_start again. */
10640 Lisp_Object limit;
10641 Lisp_Object string;
10642 int pos;
10644 limit = make_number (pt_old + 1);
10645 end = glyph;
10646 glyph = string_start;
10647 x = string_start_x;
10648 string = glyph->object;
10649 pos = string_buffer_position (w, string, string_before_pos);
10650 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10651 because we always put cursor after overlay strings. */
10652 while (pos == 0 && glyph < end)
10654 string = glyph->object;
10655 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10656 if (glyph < end)
10657 pos = string_buffer_position (w, glyph->object, string_before_pos);
10660 while (glyph < end)
10662 pos = XINT (Fnext_single_char_property_change
10663 (make_number (pos), Qdisplay, Qnil, limit));
10664 if (pos > pt_old)
10665 break;
10666 /* Skip glyphs from the same string. */
10667 string = glyph->object;
10668 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10669 /* Skip glyphs from an overlay. */
10670 while (glyph < end
10671 && ! string_buffer_position (w, glyph->object, pos))
10673 string = glyph->object;
10674 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10679 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10680 w->cursor.x = x;
10681 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10682 w->cursor.y = row->y + dy;
10684 if (w == XWINDOW (selected_window))
10686 if (!row->continued_p
10687 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10688 && row->x == 0)
10690 this_line_buffer = XBUFFER (w->buffer);
10692 CHARPOS (this_line_start_pos)
10693 = MATRIX_ROW_START_CHARPOS (row) + delta;
10694 BYTEPOS (this_line_start_pos)
10695 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10697 CHARPOS (this_line_end_pos)
10698 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10699 BYTEPOS (this_line_end_pos)
10700 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10702 this_line_y = w->cursor.y;
10703 this_line_pixel_height = row->height;
10704 this_line_vpos = w->cursor.vpos;
10705 this_line_start_x = row->x;
10707 else
10708 CHARPOS (this_line_start_pos) = 0;
10713 /* Run window scroll functions, if any, for WINDOW with new window
10714 start STARTP. Sets the window start of WINDOW to that position.
10716 We assume that the window's buffer is really current. */
10718 static INLINE struct text_pos
10719 run_window_scroll_functions (window, startp)
10720 Lisp_Object window;
10721 struct text_pos startp;
10723 struct window *w = XWINDOW (window);
10724 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10726 if (current_buffer != XBUFFER (w->buffer))
10727 abort ();
10729 if (!NILP (Vwindow_scroll_functions))
10731 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10732 make_number (CHARPOS (startp)));
10733 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10734 /* In case the hook functions switch buffers. */
10735 if (current_buffer != XBUFFER (w->buffer))
10736 set_buffer_internal_1 (XBUFFER (w->buffer));
10739 return startp;
10743 /* Make sure the line containing the cursor is fully visible.
10744 A value of 1 means there is nothing to be done.
10745 (Either the line is fully visible, or it cannot be made so,
10746 or we cannot tell.)
10747 A value of 0 means the caller should do scrolling
10748 as if point had gone off the screen. */
10750 static int
10751 make_cursor_line_fully_visible (w)
10752 struct window *w;
10754 struct glyph_matrix *matrix;
10755 struct glyph_row *row;
10756 int window_height;
10758 /* It's not always possible to find the cursor, e.g, when a window
10759 is full of overlay strings. Don't do anything in that case. */
10760 if (w->cursor.vpos < 0)
10761 return 1;
10763 matrix = w->desired_matrix;
10764 row = MATRIX_ROW (matrix, w->cursor.vpos);
10766 /* If the cursor row is not partially visible, there's nothing to do. */
10767 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10768 return 1;
10770 /* If the row the cursor is in is taller than the window's height,
10771 it's not clear what to do, so do nothing. */
10772 window_height = window_box_height (w);
10773 if (row->height >= window_height)
10774 return 1;
10776 return 0;
10778 #if 0
10779 /* This code used to try to scroll the window just enough to make
10780 the line visible. It returned 0 to say that the caller should
10781 allocate larger glyph matrices. */
10783 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10785 int dy = row->height - row->visible_height;
10786 w->vscroll = 0;
10787 w->cursor.y += dy;
10788 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10790 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10792 int dy = - (row->height - row->visible_height);
10793 w->vscroll = dy;
10794 w->cursor.y += dy;
10795 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10798 /* When we change the cursor y-position of the selected window,
10799 change this_line_y as well so that the display optimization for
10800 the cursor line of the selected window in redisplay_internal uses
10801 the correct y-position. */
10802 if (w == XWINDOW (selected_window))
10803 this_line_y = w->cursor.y;
10805 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10806 redisplay with larger matrices. */
10807 if (matrix->nrows < required_matrix_height (w))
10809 fonts_changed_p = 1;
10810 return 0;
10813 return 1;
10814 #endif /* 0 */
10818 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10819 non-zero means only WINDOW is redisplayed in redisplay_internal.
10820 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10821 in redisplay_window to bring a partially visible line into view in
10822 the case that only the cursor has moved.
10824 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10825 last screen line's vertical height extends past the end of the screen.
10827 Value is
10829 1 if scrolling succeeded
10831 0 if scrolling didn't find point.
10833 -1 if new fonts have been loaded so that we must interrupt
10834 redisplay, adjust glyph matrices, and try again. */
10836 enum
10838 SCROLLING_SUCCESS,
10839 SCROLLING_FAILED,
10840 SCROLLING_NEED_LARGER_MATRICES
10843 static int
10844 try_scrolling (window, just_this_one_p, scroll_conservatively,
10845 scroll_step, temp_scroll_step, last_line_misfit)
10846 Lisp_Object window;
10847 int just_this_one_p;
10848 EMACS_INT scroll_conservatively, scroll_step;
10849 int temp_scroll_step;
10850 int last_line_misfit;
10852 struct window *w = XWINDOW (window);
10853 struct frame *f = XFRAME (w->frame);
10854 struct text_pos scroll_margin_pos;
10855 struct text_pos pos;
10856 struct text_pos startp;
10857 struct it it;
10858 Lisp_Object window_end;
10859 int this_scroll_margin;
10860 int dy = 0;
10861 int scroll_max;
10862 int rc;
10863 int amount_to_scroll = 0;
10864 Lisp_Object aggressive;
10865 int height;
10866 int end_scroll_margin;
10868 #if GLYPH_DEBUG
10869 debug_method_add (w, "try_scrolling");
10870 #endif
10872 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10874 /* Compute scroll margin height in pixels. We scroll when point is
10875 within this distance from the top or bottom of the window. */
10876 if (scroll_margin > 0)
10878 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10879 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10881 else
10882 this_scroll_margin = 0;
10884 /* Compute how much we should try to scroll maximally to bring point
10885 into view. */
10886 if (scroll_step || scroll_conservatively || temp_scroll_step)
10887 scroll_max = max (scroll_step,
10888 max (scroll_conservatively, temp_scroll_step));
10889 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10890 || NUMBERP (current_buffer->scroll_up_aggressively))
10891 /* We're trying to scroll because of aggressive scrolling
10892 but no scroll_step is set. Choose an arbitrary one. Maybe
10893 there should be a variable for this. */
10894 scroll_max = 10;
10895 else
10896 scroll_max = 0;
10897 scroll_max *= FRAME_LINE_HEIGHT (f);
10899 /* Decide whether we have to scroll down. Start at the window end
10900 and move this_scroll_margin up to find the position of the scroll
10901 margin. */
10902 window_end = Fwindow_end (window, Qt);
10904 too_near_end:
10906 CHARPOS (scroll_margin_pos) = XINT (window_end);
10907 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10909 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10910 if (end_scroll_margin)
10912 start_display (&it, w, scroll_margin_pos);
10913 move_it_vertically (&it, - end_scroll_margin);
10914 scroll_margin_pos = it.current.pos;
10917 if (PT >= CHARPOS (scroll_margin_pos))
10919 int y0;
10921 /* Point is in the scroll margin at the bottom of the window, or
10922 below. Compute a new window start that makes point visible. */
10924 /* Compute the distance from the scroll margin to PT.
10925 Give up if the distance is greater than scroll_max. */
10926 start_display (&it, w, scroll_margin_pos);
10927 y0 = it.current_y;
10928 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10929 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10931 /* To make point visible, we have to move the window start
10932 down so that the line the cursor is in is visible, which
10933 means we have to add in the height of the cursor line. */
10934 dy = line_bottom_y (&it) - y0;
10936 if (dy > scroll_max)
10937 return SCROLLING_FAILED;
10939 /* Move the window start down. If scrolling conservatively,
10940 move it just enough down to make point visible. If
10941 scroll_step is set, move it down by scroll_step. */
10942 start_display (&it, w, startp);
10944 if (scroll_conservatively)
10945 /* Set AMOUNT_TO_SCROLL to at least one line,
10946 and at most scroll_conservatively lines. */
10947 amount_to_scroll
10948 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10949 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10950 else if (scroll_step || temp_scroll_step)
10951 amount_to_scroll = scroll_max;
10952 else
10954 aggressive = current_buffer->scroll_up_aggressively;
10955 height = WINDOW_BOX_TEXT_HEIGHT (w);
10956 if (NUMBERP (aggressive))
10957 amount_to_scroll = XFLOATINT (aggressive) * height;
10960 if (amount_to_scroll <= 0)
10961 return SCROLLING_FAILED;
10963 /* If moving by amount_to_scroll leaves STARTP unchanged,
10964 move it down one screen line. */
10966 move_it_vertically (&it, amount_to_scroll);
10967 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10968 move_it_by_lines (&it, 1, 1);
10969 startp = it.current.pos;
10971 else
10973 /* See if point is inside the scroll margin at the top of the
10974 window. */
10975 scroll_margin_pos = startp;
10976 if (this_scroll_margin)
10978 start_display (&it, w, startp);
10979 move_it_vertically (&it, this_scroll_margin);
10980 scroll_margin_pos = it.current.pos;
10983 if (PT < CHARPOS (scroll_margin_pos))
10985 /* Point is in the scroll margin at the top of the window or
10986 above what is displayed in the window. */
10987 int y0;
10989 /* Compute the vertical distance from PT to the scroll
10990 margin position. Give up if distance is greater than
10991 scroll_max. */
10992 SET_TEXT_POS (pos, PT, PT_BYTE);
10993 start_display (&it, w, pos);
10994 y0 = it.current_y;
10995 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10996 it.last_visible_y, -1,
10997 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10998 dy = it.current_y - y0;
10999 if (dy > scroll_max)
11000 return SCROLLING_FAILED;
11002 /* Compute new window start. */
11003 start_display (&it, w, startp);
11005 if (scroll_conservatively)
11006 amount_to_scroll =
11007 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11008 else if (scroll_step || temp_scroll_step)
11009 amount_to_scroll = scroll_max;
11010 else
11012 aggressive = current_buffer->scroll_down_aggressively;
11013 height = WINDOW_BOX_TEXT_HEIGHT (w);
11014 if (NUMBERP (aggressive))
11015 amount_to_scroll = XFLOATINT (aggressive) * height;
11018 if (amount_to_scroll <= 0)
11019 return SCROLLING_FAILED;
11021 move_it_vertically (&it, - amount_to_scroll);
11022 startp = it.current.pos;
11026 /* Run window scroll functions. */
11027 startp = run_window_scroll_functions (window, startp);
11029 /* Display the window. Give up if new fonts are loaded, or if point
11030 doesn't appear. */
11031 if (!try_window (window, startp))
11032 rc = SCROLLING_NEED_LARGER_MATRICES;
11033 else if (w->cursor.vpos < 0)
11035 clear_glyph_matrix (w->desired_matrix);
11036 rc = SCROLLING_FAILED;
11038 else
11040 /* Maybe forget recorded base line for line number display. */
11041 if (!just_this_one_p
11042 || current_buffer->clip_changed
11043 || BEG_UNCHANGED < CHARPOS (startp))
11044 w->base_line_number = Qnil;
11046 /* If cursor ends up on a partially visible line,
11047 treat that as being off the bottom of the screen. */
11048 if (! make_cursor_line_fully_visible (w))
11050 clear_glyph_matrix (w->desired_matrix);
11051 last_line_misfit = 1;
11052 goto too_near_end;
11054 rc = SCROLLING_SUCCESS;
11057 return rc;
11061 /* Compute a suitable window start for window W if display of W starts
11062 on a continuation line. Value is non-zero if a new window start
11063 was computed.
11065 The new window start will be computed, based on W's width, starting
11066 from the start of the continued line. It is the start of the
11067 screen line with the minimum distance from the old start W->start. */
11069 static int
11070 compute_window_start_on_continuation_line (w)
11071 struct window *w;
11073 struct text_pos pos, start_pos;
11074 int window_start_changed_p = 0;
11076 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11078 /* If window start is on a continuation line... Window start may be
11079 < BEGV in case there's invisible text at the start of the
11080 buffer (M-x rmail, for example). */
11081 if (CHARPOS (start_pos) > BEGV
11082 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11084 struct it it;
11085 struct glyph_row *row;
11087 /* Handle the case that the window start is out of range. */
11088 if (CHARPOS (start_pos) < BEGV)
11089 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11090 else if (CHARPOS (start_pos) > ZV)
11091 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11093 /* Find the start of the continued line. This should be fast
11094 because scan_buffer is fast (newline cache). */
11095 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11096 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11097 row, DEFAULT_FACE_ID);
11098 reseat_at_previous_visible_line_start (&it);
11100 /* If the line start is "too far" away from the window start,
11101 say it takes too much time to compute a new window start. */
11102 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11103 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11105 int min_distance, distance;
11107 /* Move forward by display lines to find the new window
11108 start. If window width was enlarged, the new start can
11109 be expected to be > the old start. If window width was
11110 decreased, the new window start will be < the old start.
11111 So, we're looking for the display line start with the
11112 minimum distance from the old window start. */
11113 pos = it.current.pos;
11114 min_distance = INFINITY;
11115 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11116 distance < min_distance)
11118 min_distance = distance;
11119 pos = it.current.pos;
11120 move_it_by_lines (&it, 1, 0);
11123 /* Set the window start there. */
11124 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11125 window_start_changed_p = 1;
11129 return window_start_changed_p;
11133 /* Try cursor movement in case text has not changed in window WINDOW,
11134 with window start STARTP. Value is
11136 CURSOR_MOVEMENT_SUCCESS if successful
11138 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11140 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11141 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11142 we want to scroll as if scroll-step were set to 1. See the code.
11144 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11145 which case we have to abort this redisplay, and adjust matrices
11146 first. */
11148 enum
11150 CURSOR_MOVEMENT_SUCCESS,
11151 CURSOR_MOVEMENT_CANNOT_BE_USED,
11152 CURSOR_MOVEMENT_MUST_SCROLL,
11153 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11156 static int
11157 try_cursor_movement (window, startp, scroll_step)
11158 Lisp_Object window;
11159 struct text_pos startp;
11160 int *scroll_step;
11162 struct window *w = XWINDOW (window);
11163 struct frame *f = XFRAME (w->frame);
11164 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11166 #if GLYPH_DEBUG
11167 if (inhibit_try_cursor_movement)
11168 return rc;
11169 #endif
11171 /* Handle case where text has not changed, only point, and it has
11172 not moved off the frame. */
11173 if (/* Point may be in this window. */
11174 PT >= CHARPOS (startp)
11175 /* Selective display hasn't changed. */
11176 && !current_buffer->clip_changed
11177 /* Function force-mode-line-update is used to force a thorough
11178 redisplay. It sets either windows_or_buffers_changed or
11179 update_mode_lines. So don't take a shortcut here for these
11180 cases. */
11181 && !update_mode_lines
11182 && !windows_or_buffers_changed
11183 && !cursor_type_changed
11184 /* Can't use this case if highlighting a region. When a
11185 region exists, cursor movement has to do more than just
11186 set the cursor. */
11187 && !(!NILP (Vtransient_mark_mode)
11188 && !NILP (current_buffer->mark_active))
11189 && NILP (w->region_showing)
11190 && NILP (Vshow_trailing_whitespace)
11191 /* Right after splitting windows, last_point may be nil. */
11192 && INTEGERP (w->last_point)
11193 /* This code is not used for mini-buffer for the sake of the case
11194 of redisplaying to replace an echo area message; since in
11195 that case the mini-buffer contents per se are usually
11196 unchanged. This code is of no real use in the mini-buffer
11197 since the handling of this_line_start_pos, etc., in redisplay
11198 handles the same cases. */
11199 && !EQ (window, minibuf_window)
11200 /* When splitting windows or for new windows, it happens that
11201 redisplay is called with a nil window_end_vpos or one being
11202 larger than the window. This should really be fixed in
11203 window.c. I don't have this on my list, now, so we do
11204 approximately the same as the old redisplay code. --gerd. */
11205 && INTEGERP (w->window_end_vpos)
11206 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11207 && (FRAME_WINDOW_P (f)
11208 || !MARKERP (Voverlay_arrow_position)
11209 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11211 int this_scroll_margin;
11212 struct glyph_row *row = NULL;
11214 #if GLYPH_DEBUG
11215 debug_method_add (w, "cursor movement");
11216 #endif
11218 /* Scroll if point within this distance from the top or bottom
11219 of the window. This is a pixel value. */
11220 this_scroll_margin = max (0, scroll_margin);
11221 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11222 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11224 /* Start with the row the cursor was displayed during the last
11225 not paused redisplay. Give up if that row is not valid. */
11226 if (w->last_cursor.vpos < 0
11227 || w->last_cursor.vpos >= w->current_matrix->nrows)
11228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11229 else
11231 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11232 if (row->mode_line_p)
11233 ++row;
11234 if (!row->enabled_p)
11235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11238 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11240 int scroll_p = 0;
11241 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11243 if (PT > XFASTINT (w->last_point))
11245 /* Point has moved forward. */
11246 while (MATRIX_ROW_END_CHARPOS (row) < PT
11247 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11249 xassert (row->enabled_p);
11250 ++row;
11253 /* The end position of a row equals the start position
11254 of the next row. If PT is there, we would rather
11255 display it in the next line. */
11256 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11257 && MATRIX_ROW_END_CHARPOS (row) == PT
11258 && !cursor_row_p (w, row))
11259 ++row;
11261 /* If within the scroll margin, scroll. Note that
11262 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11263 the next line would be drawn, and that
11264 this_scroll_margin can be zero. */
11265 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11266 || PT > MATRIX_ROW_END_CHARPOS (row)
11267 /* Line is completely visible last line in window
11268 and PT is to be set in the next line. */
11269 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11270 && PT == MATRIX_ROW_END_CHARPOS (row)
11271 && !row->ends_at_zv_p
11272 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11273 scroll_p = 1;
11275 else if (PT < XFASTINT (w->last_point))
11277 /* Cursor has to be moved backward. Note that PT >=
11278 CHARPOS (startp) because of the outer
11279 if-statement. */
11280 while (!row->mode_line_p
11281 && (MATRIX_ROW_START_CHARPOS (row) > PT
11282 || (MATRIX_ROW_START_CHARPOS (row) == PT
11283 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11284 && (row->y > this_scroll_margin
11285 || CHARPOS (startp) == BEGV))
11287 xassert (row->enabled_p);
11288 --row;
11291 /* Consider the following case: Window starts at BEGV,
11292 there is invisible, intangible text at BEGV, so that
11293 display starts at some point START > BEGV. It can
11294 happen that we are called with PT somewhere between
11295 BEGV and START. Try to handle that case. */
11296 if (row < w->current_matrix->rows
11297 || row->mode_line_p)
11299 row = w->current_matrix->rows;
11300 if (row->mode_line_p)
11301 ++row;
11304 /* Due to newlines in overlay strings, we may have to
11305 skip forward over overlay strings. */
11306 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11307 && MATRIX_ROW_END_CHARPOS (row) == PT
11308 && !cursor_row_p (w, row))
11309 ++row;
11311 /* If within the scroll margin, scroll. */
11312 if (row->y < this_scroll_margin
11313 && CHARPOS (startp) != BEGV)
11314 scroll_p = 1;
11317 if (PT < MATRIX_ROW_START_CHARPOS (row)
11318 || PT > MATRIX_ROW_END_CHARPOS (row))
11320 /* if PT is not in the glyph row, give up. */
11321 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11323 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11325 if (PT == MATRIX_ROW_END_CHARPOS (row)
11326 && !row->ends_at_zv_p
11327 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11328 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11329 else if (row->height > window_box_height (w))
11331 /* If we end up in a partially visible line, let's
11332 make it fully visible, except when it's taller
11333 than the window, in which case we can't do much
11334 about it. */
11335 *scroll_step = 1;
11336 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11338 else
11340 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11341 if (!make_cursor_line_fully_visible (w))
11342 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11343 else
11344 rc = CURSOR_MOVEMENT_SUCCESS;
11347 else if (scroll_p)
11348 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11349 else
11351 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11352 rc = CURSOR_MOVEMENT_SUCCESS;
11357 return rc;
11360 void
11361 set_vertical_scroll_bar (w)
11362 struct window *w;
11364 int start, end, whole;
11366 /* Calculate the start and end positions for the current window.
11367 At some point, it would be nice to choose between scrollbars
11368 which reflect the whole buffer size, with special markers
11369 indicating narrowing, and scrollbars which reflect only the
11370 visible region.
11372 Note that mini-buffers sometimes aren't displaying any text. */
11373 if (!MINI_WINDOW_P (w)
11374 || (w == XWINDOW (minibuf_window)
11375 && NILP (echo_area_buffer[0])))
11377 struct buffer *buf = XBUFFER (w->buffer);
11378 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11379 start = marker_position (w->start) - BUF_BEGV (buf);
11380 /* I don't think this is guaranteed to be right. For the
11381 moment, we'll pretend it is. */
11382 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11384 if (end < start)
11385 end = start;
11386 if (whole < (end - start))
11387 whole = end - start;
11389 else
11390 start = end = whole = 0;
11392 /* Indicate what this scroll bar ought to be displaying now. */
11393 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11396 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11397 selected_window is redisplayed.
11399 We can return without actually redisplaying the window if
11400 fonts_changed_p is nonzero. In that case, redisplay_internal will
11401 retry. */
11403 static void
11404 redisplay_window (window, just_this_one_p)
11405 Lisp_Object window;
11406 int just_this_one_p;
11408 struct window *w = XWINDOW (window);
11409 struct frame *f = XFRAME (w->frame);
11410 struct buffer *buffer = XBUFFER (w->buffer);
11411 struct buffer *old = current_buffer;
11412 struct text_pos lpoint, opoint, startp;
11413 int update_mode_line;
11414 int tem;
11415 struct it it;
11416 /* Record it now because it's overwritten. */
11417 int current_matrix_up_to_date_p = 0;
11418 /* This is less strict than current_matrix_up_to_date_p.
11419 It indictes that the buffer contents and narrowing are unchanged. */
11420 int buffer_unchanged_p = 0;
11421 int temp_scroll_step = 0;
11422 int count = SPECPDL_INDEX ();
11423 int rc;
11424 int centering_position;
11425 int last_line_misfit = 0;
11427 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11428 opoint = lpoint;
11430 /* W must be a leaf window here. */
11431 xassert (!NILP (w->buffer));
11432 #if GLYPH_DEBUG
11433 *w->desired_matrix->method = 0;
11434 #endif
11436 specbind (Qinhibit_point_motion_hooks, Qt);
11438 reconsider_clip_changes (w, buffer);
11440 /* Has the mode line to be updated? */
11441 update_mode_line = (!NILP (w->update_mode_line)
11442 || update_mode_lines
11443 || buffer->clip_changed
11444 || buffer->prevent_redisplay_optimizations_p);
11446 if (MINI_WINDOW_P (w))
11448 if (w == XWINDOW (echo_area_window)
11449 && !NILP (echo_area_buffer[0]))
11451 if (update_mode_line)
11452 /* We may have to update a tty frame's menu bar or a
11453 tool-bar. Example `M-x C-h C-h C-g'. */
11454 goto finish_menu_bars;
11455 else
11456 /* We've already displayed the echo area glyphs in this window. */
11457 goto finish_scroll_bars;
11459 else if ((w != XWINDOW (minibuf_window)
11460 || minibuf_level == 0)
11461 /* When buffer is nonempty, redisplay window normally. */
11462 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11463 /* Quail displays non-mini buffers in minibuffer window.
11464 In that case, redisplay the window normally. */
11465 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11467 /* W is a mini-buffer window, but it's not active, so clear
11468 it. */
11469 int yb = window_text_bottom_y (w);
11470 struct glyph_row *row;
11471 int y;
11473 for (y = 0, row = w->desired_matrix->rows;
11474 y < yb;
11475 y += row->height, ++row)
11476 blank_row (w, row, y);
11477 goto finish_scroll_bars;
11480 clear_glyph_matrix (w->desired_matrix);
11483 /* Otherwise set up data on this window; select its buffer and point
11484 value. */
11485 /* Really select the buffer, for the sake of buffer-local
11486 variables. */
11487 set_buffer_internal_1 (XBUFFER (w->buffer));
11488 SET_TEXT_POS (opoint, PT, PT_BYTE);
11490 current_matrix_up_to_date_p
11491 = (!NILP (w->window_end_valid)
11492 && !current_buffer->clip_changed
11493 && !current_buffer->prevent_redisplay_optimizations_p
11494 && XFASTINT (w->last_modified) >= MODIFF
11495 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11497 buffer_unchanged_p
11498 = (!NILP (w->window_end_valid)
11499 && !current_buffer->clip_changed
11500 && XFASTINT (w->last_modified) >= MODIFF
11501 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11503 /* When windows_or_buffers_changed is non-zero, we can't rely on
11504 the window end being valid, so set it to nil there. */
11505 if (windows_or_buffers_changed)
11507 /* If window starts on a continuation line, maybe adjust the
11508 window start in case the window's width changed. */
11509 if (XMARKER (w->start)->buffer == current_buffer)
11510 compute_window_start_on_continuation_line (w);
11512 w->window_end_valid = Qnil;
11515 /* Some sanity checks. */
11516 CHECK_WINDOW_END (w);
11517 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11518 abort ();
11519 if (BYTEPOS (opoint) < CHARPOS (opoint))
11520 abort ();
11522 /* If %c is in mode line, update it if needed. */
11523 if (!NILP (w->column_number_displayed)
11524 /* This alternative quickly identifies a common case
11525 where no change is needed. */
11526 && !(PT == XFASTINT (w->last_point)
11527 && XFASTINT (w->last_modified) >= MODIFF
11528 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11529 && (XFASTINT (w->column_number_displayed)
11530 != (int) current_column ())) /* iftc */
11531 update_mode_line = 1;
11533 /* Count number of windows showing the selected buffer. An indirect
11534 buffer counts as its base buffer. */
11535 if (!just_this_one_p)
11537 struct buffer *current_base, *window_base;
11538 current_base = current_buffer;
11539 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11540 if (current_base->base_buffer)
11541 current_base = current_base->base_buffer;
11542 if (window_base->base_buffer)
11543 window_base = window_base->base_buffer;
11544 if (current_base == window_base)
11545 buffer_shared++;
11548 /* Point refers normally to the selected window. For any other
11549 window, set up appropriate value. */
11550 if (!EQ (window, selected_window))
11552 int new_pt = XMARKER (w->pointm)->charpos;
11553 int new_pt_byte = marker_byte_position (w->pointm);
11554 if (new_pt < BEGV)
11556 new_pt = BEGV;
11557 new_pt_byte = BEGV_BYTE;
11558 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11560 else if (new_pt > (ZV - 1))
11562 new_pt = ZV;
11563 new_pt_byte = ZV_BYTE;
11564 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11567 /* We don't use SET_PT so that the point-motion hooks don't run. */
11568 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11571 /* If any of the character widths specified in the display table
11572 have changed, invalidate the width run cache. It's true that
11573 this may be a bit late to catch such changes, but the rest of
11574 redisplay goes (non-fatally) haywire when the display table is
11575 changed, so why should we worry about doing any better? */
11576 if (current_buffer->width_run_cache)
11578 struct Lisp_Char_Table *disptab = buffer_display_table ();
11580 if (! disptab_matches_widthtab (disptab,
11581 XVECTOR (current_buffer->width_table)))
11583 invalidate_region_cache (current_buffer,
11584 current_buffer->width_run_cache,
11585 BEG, Z);
11586 recompute_width_table (current_buffer, disptab);
11590 /* If window-start is screwed up, choose a new one. */
11591 if (XMARKER (w->start)->buffer != current_buffer)
11592 goto recenter;
11594 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11596 /* If someone specified a new starting point but did not insist,
11597 check whether it can be used. */
11598 if (!NILP (w->optional_new_start)
11599 && CHARPOS (startp) >= BEGV
11600 && CHARPOS (startp) <= ZV)
11602 w->optional_new_start = Qnil;
11603 start_display (&it, w, startp);
11604 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11605 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11606 if (IT_CHARPOS (it) == PT)
11607 w->force_start = Qt;
11608 /* IT may overshoot PT if text at PT is invisible. */
11609 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11610 w->force_start = Qt;
11615 /* Handle case where place to start displaying has been specified,
11616 unless the specified location is outside the accessible range. */
11617 if (!NILP (w->force_start)
11618 || w->frozen_window_start_p)
11620 /* We set this later on if we have to adjust point. */
11621 int new_vpos = -1;
11623 w->force_start = Qnil;
11624 w->vscroll = 0;
11625 w->window_end_valid = Qnil;
11627 /* Forget any recorded base line for line number display. */
11628 if (!buffer_unchanged_p)
11629 w->base_line_number = Qnil;
11631 /* Redisplay the mode line. Select the buffer properly for that.
11632 Also, run the hook window-scroll-functions
11633 because we have scrolled. */
11634 /* Note, we do this after clearing force_start because
11635 if there's an error, it is better to forget about force_start
11636 than to get into an infinite loop calling the hook functions
11637 and having them get more errors. */
11638 if (!update_mode_line
11639 || ! NILP (Vwindow_scroll_functions))
11641 update_mode_line = 1;
11642 w->update_mode_line = Qt;
11643 startp = run_window_scroll_functions (window, startp);
11646 w->last_modified = make_number (0);
11647 w->last_overlay_modified = make_number (0);
11648 if (CHARPOS (startp) < BEGV)
11649 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11650 else if (CHARPOS (startp) > ZV)
11651 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11653 /* Redisplay, then check if cursor has been set during the
11654 redisplay. Give up if new fonts were loaded. */
11655 if (!try_window (window, startp))
11657 w->force_start = Qt;
11658 clear_glyph_matrix (w->desired_matrix);
11659 goto need_larger_matrices;
11662 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11664 /* If point does not appear, try to move point so it does
11665 appear. The desired matrix has been built above, so we
11666 can use it here. */
11667 new_vpos = window_box_height (w) / 2;
11670 if (!make_cursor_line_fully_visible (w))
11672 /* Point does appear, but on a line partly visible at end of window.
11673 Move it back to a fully-visible line. */
11674 new_vpos = window_box_height (w);
11677 /* If we need to move point for either of the above reasons,
11678 now actually do it. */
11679 if (new_vpos >= 0)
11681 struct glyph_row *row;
11683 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11684 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11685 ++row;
11687 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11688 MATRIX_ROW_START_BYTEPOS (row));
11690 if (w != XWINDOW (selected_window))
11691 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11692 else if (current_buffer == old)
11693 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11695 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11697 /* If we are highlighting the region, then we just changed
11698 the region, so redisplay to show it. */
11699 if (!NILP (Vtransient_mark_mode)
11700 && !NILP (current_buffer->mark_active))
11702 clear_glyph_matrix (w->desired_matrix);
11703 if (!try_window (window, startp))
11704 goto need_larger_matrices;
11708 #if GLYPH_DEBUG
11709 debug_method_add (w, "forced window start");
11710 #endif
11711 goto done;
11714 /* Handle case where text has not changed, only point, and it has
11715 not moved off the frame, and we are not retrying after hscroll.
11716 (current_matrix_up_to_date_p is nonzero when retrying.) */
11717 if (current_matrix_up_to_date_p
11718 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11719 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11721 switch (rc)
11723 case CURSOR_MOVEMENT_SUCCESS:
11724 goto done;
11726 #if 0 /* try_cursor_movement never returns this value. */
11727 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11728 goto need_larger_matrices;
11729 #endif
11731 case CURSOR_MOVEMENT_MUST_SCROLL:
11732 goto try_to_scroll;
11734 default:
11735 abort ();
11738 /* If current starting point was originally the beginning of a line
11739 but no longer is, find a new starting point. */
11740 else if (!NILP (w->start_at_line_beg)
11741 && !(CHARPOS (startp) <= BEGV
11742 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11744 #if GLYPH_DEBUG
11745 debug_method_add (w, "recenter 1");
11746 #endif
11747 goto recenter;
11750 /* Try scrolling with try_window_id. Value is > 0 if update has
11751 been done, it is -1 if we know that the same window start will
11752 not work. It is 0 if unsuccessful for some other reason. */
11753 else if ((tem = try_window_id (w)) != 0)
11755 #if GLYPH_DEBUG
11756 debug_method_add (w, "try_window_id %d", tem);
11757 #endif
11759 if (fonts_changed_p)
11760 goto need_larger_matrices;
11761 if (tem > 0)
11762 goto done;
11764 /* Otherwise try_window_id has returned -1 which means that we
11765 don't want the alternative below this comment to execute. */
11767 else if (CHARPOS (startp) >= BEGV
11768 && CHARPOS (startp) <= ZV
11769 && PT >= CHARPOS (startp)
11770 && (CHARPOS (startp) < ZV
11771 /* Avoid starting at end of buffer. */
11772 || CHARPOS (startp) == BEGV
11773 || (XFASTINT (w->last_modified) >= MODIFF
11774 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11776 #if GLYPH_DEBUG
11777 debug_method_add (w, "same window start");
11778 #endif
11780 /* Try to redisplay starting at same place as before.
11781 If point has not moved off frame, accept the results. */
11782 if (!current_matrix_up_to_date_p
11783 /* Don't use try_window_reusing_current_matrix in this case
11784 because a window scroll function can have changed the
11785 buffer. */
11786 || !NILP (Vwindow_scroll_functions)
11787 || MINI_WINDOW_P (w)
11788 || !try_window_reusing_current_matrix (w))
11790 IF_DEBUG (debug_method_add (w, "1"));
11791 try_window (window, startp);
11794 if (fonts_changed_p)
11795 goto need_larger_matrices;
11797 if (w->cursor.vpos >= 0)
11799 if (!just_this_one_p
11800 || current_buffer->clip_changed
11801 || BEG_UNCHANGED < CHARPOS (startp))
11802 /* Forget any recorded base line for line number display. */
11803 w->base_line_number = Qnil;
11805 if (!make_cursor_line_fully_visible (w))
11807 clear_glyph_matrix (w->desired_matrix);
11808 last_line_misfit = 1;
11810 /* Drop through and scroll. */
11811 else
11812 goto done;
11814 else
11815 clear_glyph_matrix (w->desired_matrix);
11818 try_to_scroll:
11820 w->last_modified = make_number (0);
11821 w->last_overlay_modified = make_number (0);
11823 /* Redisplay the mode line. Select the buffer properly for that. */
11824 if (!update_mode_line)
11826 update_mode_line = 1;
11827 w->update_mode_line = Qt;
11830 /* Try to scroll by specified few lines. */
11831 if ((scroll_conservatively
11832 || scroll_step
11833 || temp_scroll_step
11834 || NUMBERP (current_buffer->scroll_up_aggressively)
11835 || NUMBERP (current_buffer->scroll_down_aggressively))
11836 && !current_buffer->clip_changed
11837 && CHARPOS (startp) >= BEGV
11838 && CHARPOS (startp) <= ZV)
11840 /* The function returns -1 if new fonts were loaded, 1 if
11841 successful, 0 if not successful. */
11842 int rc = try_scrolling (window, just_this_one_p,
11843 scroll_conservatively,
11844 scroll_step,
11845 temp_scroll_step, last_line_misfit);
11846 switch (rc)
11848 case SCROLLING_SUCCESS:
11849 goto done;
11851 case SCROLLING_NEED_LARGER_MATRICES:
11852 goto need_larger_matrices;
11854 case SCROLLING_FAILED:
11855 break;
11857 default:
11858 abort ();
11862 /* Finally, just choose place to start which centers point */
11864 recenter:
11865 centering_position = window_box_height (w) / 2;
11867 point_at_top:
11868 /* Jump here with centering_position already set to 0. */
11870 #if GLYPH_DEBUG
11871 debug_method_add (w, "recenter");
11872 #endif
11874 /* w->vscroll = 0; */
11876 /* Forget any previously recorded base line for line number display. */
11877 if (!buffer_unchanged_p)
11878 w->base_line_number = Qnil;
11880 /* Move backward half the height of the window. */
11881 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11882 it.current_y = it.last_visible_y;
11883 move_it_vertically_backward (&it, centering_position);
11884 xassert (IT_CHARPOS (it) >= BEGV);
11886 /* The function move_it_vertically_backward may move over more
11887 than the specified y-distance. If it->w is small, e.g. a
11888 mini-buffer window, we may end up in front of the window's
11889 display area. Start displaying at the start of the line
11890 containing PT in this case. */
11891 if (it.current_y <= 0)
11893 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11894 move_it_vertically (&it, 0);
11895 xassert (IT_CHARPOS (it) <= PT);
11896 it.current_y = 0;
11899 it.current_x = it.hpos = 0;
11901 /* Set startp here explicitly in case that helps avoid an infinite loop
11902 in case the window-scroll-functions functions get errors. */
11903 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11905 /* Run scroll hooks. */
11906 startp = run_window_scroll_functions (window, it.current.pos);
11908 /* Redisplay the window. */
11909 if (!current_matrix_up_to_date_p
11910 || windows_or_buffers_changed
11911 || cursor_type_changed
11912 /* Don't use try_window_reusing_current_matrix in this case
11913 because it can have changed the buffer. */
11914 || !NILP (Vwindow_scroll_functions)
11915 || !just_this_one_p
11916 || MINI_WINDOW_P (w)
11917 || !try_window_reusing_current_matrix (w))
11918 try_window (window, startp);
11920 /* If new fonts have been loaded (due to fontsets), give up. We
11921 have to start a new redisplay since we need to re-adjust glyph
11922 matrices. */
11923 if (fonts_changed_p)
11924 goto need_larger_matrices;
11926 /* If cursor did not appear assume that the middle of the window is
11927 in the first line of the window. Do it again with the next line.
11928 (Imagine a window of height 100, displaying two lines of height
11929 60. Moving back 50 from it->last_visible_y will end in the first
11930 line.) */
11931 if (w->cursor.vpos < 0)
11933 if (!NILP (w->window_end_valid)
11934 && PT >= Z - XFASTINT (w->window_end_pos))
11936 clear_glyph_matrix (w->desired_matrix);
11937 move_it_by_lines (&it, 1, 0);
11938 try_window (window, it.current.pos);
11940 else if (PT < IT_CHARPOS (it))
11942 clear_glyph_matrix (w->desired_matrix);
11943 move_it_by_lines (&it, -1, 0);
11944 try_window (window, it.current.pos);
11946 else
11948 /* Not much we can do about it. */
11952 /* Consider the following case: Window starts at BEGV, there is
11953 invisible, intangible text at BEGV, so that display starts at
11954 some point START > BEGV. It can happen that we are called with
11955 PT somewhere between BEGV and START. Try to handle that case. */
11956 if (w->cursor.vpos < 0)
11958 struct glyph_row *row = w->current_matrix->rows;
11959 if (row->mode_line_p)
11960 ++row;
11961 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11964 if (!make_cursor_line_fully_visible (w))
11966 /* If vscroll is enabled, disable it and try again. */
11967 if (w->vscroll)
11969 w->vscroll = 0;
11970 clear_glyph_matrix (w->desired_matrix);
11971 goto recenter;
11974 /* If centering point failed to make the whole line visible,
11975 put point at the top instead. That has to make the whole line
11976 visible, if it can be done. */
11977 centering_position = 0;
11978 goto point_at_top;
11981 done:
11983 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11984 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11985 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11986 ? Qt : Qnil);
11988 /* Display the mode line, if we must. */
11989 if ((update_mode_line
11990 /* If window not full width, must redo its mode line
11991 if (a) the window to its side is being redone and
11992 (b) we do a frame-based redisplay. This is a consequence
11993 of how inverted lines are drawn in frame-based redisplay. */
11994 || (!just_this_one_p
11995 && !FRAME_WINDOW_P (f)
11996 && !WINDOW_FULL_WIDTH_P (w))
11997 /* Line number to display. */
11998 || INTEGERP (w->base_line_pos)
11999 /* Column number is displayed and different from the one displayed. */
12000 || (!NILP (w->column_number_displayed)
12001 && (XFASTINT (w->column_number_displayed)
12002 != (int) current_column ()))) /* iftc */
12003 /* This means that the window has a mode line. */
12004 && (WINDOW_WANTS_MODELINE_P (w)
12005 || WINDOW_WANTS_HEADER_LINE_P (w)))
12007 display_mode_lines (w);
12009 /* If mode line height has changed, arrange for a thorough
12010 immediate redisplay using the correct mode line height. */
12011 if (WINDOW_WANTS_MODELINE_P (w)
12012 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12014 fonts_changed_p = 1;
12015 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12016 = DESIRED_MODE_LINE_HEIGHT (w);
12019 /* If top line height has changed, arrange for a thorough
12020 immediate redisplay using the correct mode line height. */
12021 if (WINDOW_WANTS_HEADER_LINE_P (w)
12022 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12024 fonts_changed_p = 1;
12025 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12026 = DESIRED_HEADER_LINE_HEIGHT (w);
12029 if (fonts_changed_p)
12030 goto need_larger_matrices;
12033 if (!line_number_displayed
12034 && !BUFFERP (w->base_line_pos))
12036 w->base_line_pos = Qnil;
12037 w->base_line_number = Qnil;
12040 finish_menu_bars:
12042 /* When we reach a frame's selected window, redo the frame's menu bar. */
12043 if (update_mode_line
12044 && EQ (FRAME_SELECTED_WINDOW (f), window))
12046 int redisplay_menu_p = 0;
12047 int redisplay_tool_bar_p = 0;
12049 if (FRAME_WINDOW_P (f))
12051 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12052 || defined (USE_GTK)
12053 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12054 #else
12055 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12056 #endif
12058 else
12059 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12061 if (redisplay_menu_p)
12062 display_menu_bar (w);
12064 #ifdef HAVE_WINDOW_SYSTEM
12065 #ifdef USE_GTK
12066 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12067 #else
12068 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12069 && (FRAME_TOOL_BAR_LINES (f) > 0
12070 || auto_resize_tool_bars_p);
12072 #endif
12074 if (redisplay_tool_bar_p)
12075 redisplay_tool_bar (f);
12076 #endif
12079 /* We go to this label, with fonts_changed_p nonzero,
12080 if it is necessary to try again using larger glyph matrices.
12081 We have to redeem the scroll bar even in this case,
12082 because the loop in redisplay_internal expects that. */
12083 need_larger_matrices:
12085 finish_scroll_bars:
12087 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12089 /* Set the thumb's position and size. */
12090 set_vertical_scroll_bar (w);
12092 /* Note that we actually used the scroll bar attached to this
12093 window, so it shouldn't be deleted at the end of redisplay. */
12094 redeem_scroll_bar_hook (w);
12097 /* Restore current_buffer and value of point in it. */
12098 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12099 set_buffer_internal_1 (old);
12100 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12102 unbind_to (count, Qnil);
12106 /* Build the complete desired matrix of WINDOW with a window start
12107 buffer position POS. Value is non-zero if successful. It is zero
12108 if fonts were loaded during redisplay which makes re-adjusting
12109 glyph matrices necessary. */
12112 try_window (window, pos)
12113 Lisp_Object window;
12114 struct text_pos pos;
12116 struct window *w = XWINDOW (window);
12117 struct it it;
12118 struct glyph_row *last_text_row = NULL;
12120 /* Make POS the new window start. */
12121 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12123 /* Mark cursor position as unknown. No overlay arrow seen. */
12124 w->cursor.vpos = -1;
12125 overlay_arrow_seen = 0;
12127 /* Initialize iterator and info to start at POS. */
12128 start_display (&it, w, pos);
12130 /* Display all lines of W. */
12131 while (it.current_y < it.last_visible_y)
12133 if (display_line (&it))
12134 last_text_row = it.glyph_row - 1;
12135 if (fonts_changed_p)
12136 return 0;
12139 /* If bottom moved off end of frame, change mode line percentage. */
12140 if (XFASTINT (w->window_end_pos) <= 0
12141 && Z != IT_CHARPOS (it))
12142 w->update_mode_line = Qt;
12144 /* Set window_end_pos to the offset of the last character displayed
12145 on the window from the end of current_buffer. Set
12146 window_end_vpos to its row number. */
12147 if (last_text_row)
12149 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12150 w->window_end_bytepos
12151 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12152 w->window_end_pos
12153 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12154 w->window_end_vpos
12155 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12156 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12157 ->displays_text_p);
12159 else
12161 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12162 w->window_end_pos = make_number (Z - ZV);
12163 w->window_end_vpos = make_number (0);
12166 /* But that is not valid info until redisplay finishes. */
12167 w->window_end_valid = Qnil;
12168 return 1;
12173 /************************************************************************
12174 Window redisplay reusing current matrix when buffer has not changed
12175 ************************************************************************/
12177 /* Try redisplay of window W showing an unchanged buffer with a
12178 different window start than the last time it was displayed by
12179 reusing its current matrix. Value is non-zero if successful.
12180 W->start is the new window start. */
12182 static int
12183 try_window_reusing_current_matrix (w)
12184 struct window *w;
12186 struct frame *f = XFRAME (w->frame);
12187 struct glyph_row *row, *bottom_row;
12188 struct it it;
12189 struct run run;
12190 struct text_pos start, new_start;
12191 int nrows_scrolled, i;
12192 struct glyph_row *last_text_row;
12193 struct glyph_row *last_reused_text_row;
12194 struct glyph_row *start_row;
12195 int start_vpos, min_y, max_y;
12197 #if GLYPH_DEBUG
12198 if (inhibit_try_window_reusing)
12199 return 0;
12200 #endif
12202 if (/* This function doesn't handle terminal frames. */
12203 !FRAME_WINDOW_P (f)
12204 /* Don't try to reuse the display if windows have been split
12205 or such. */
12206 || windows_or_buffers_changed
12207 || cursor_type_changed)
12208 return 0;
12210 /* Can't do this if region may have changed. */
12211 if ((!NILP (Vtransient_mark_mode)
12212 && !NILP (current_buffer->mark_active))
12213 || !NILP (w->region_showing)
12214 || !NILP (Vshow_trailing_whitespace))
12215 return 0;
12217 /* If top-line visibility has changed, give up. */
12218 if (WINDOW_WANTS_HEADER_LINE_P (w)
12219 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12220 return 0;
12222 /* Give up if old or new display is scrolled vertically. We could
12223 make this function handle this, but right now it doesn't. */
12224 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12225 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12226 return 0;
12228 /* The variable new_start now holds the new window start. The old
12229 start `start' can be determined from the current matrix. */
12230 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12231 start = start_row->start.pos;
12232 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12234 /* Clear the desired matrix for the display below. */
12235 clear_glyph_matrix (w->desired_matrix);
12237 if (CHARPOS (new_start) <= CHARPOS (start))
12239 int first_row_y;
12241 /* Don't use this method if the display starts with an ellipsis
12242 displayed for invisible text. It's not easy to handle that case
12243 below, and it's certainly not worth the effort since this is
12244 not a frequent case. */
12245 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12246 return 0;
12248 IF_DEBUG (debug_method_add (w, "twu1"));
12250 /* Display up to a row that can be reused. The variable
12251 last_text_row is set to the last row displayed that displays
12252 text. Note that it.vpos == 0 if or if not there is a
12253 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12254 start_display (&it, w, new_start);
12255 first_row_y = it.current_y;
12256 w->cursor.vpos = -1;
12257 last_text_row = last_reused_text_row = NULL;
12259 while (it.current_y < it.last_visible_y
12260 && IT_CHARPOS (it) < CHARPOS (start)
12261 && !fonts_changed_p)
12262 if (display_line (&it))
12263 last_text_row = it.glyph_row - 1;
12265 /* A value of current_y < last_visible_y means that we stopped
12266 at the previous window start, which in turn means that we
12267 have at least one reusable row. */
12268 if (it.current_y < it.last_visible_y)
12270 /* IT.vpos always starts from 0; it counts text lines. */
12271 nrows_scrolled = it.vpos;
12273 /* Find PT if not already found in the lines displayed. */
12274 if (w->cursor.vpos < 0)
12276 int dy = it.current_y - first_row_y;
12278 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12279 row = row_containing_pos (w, PT, row, NULL, dy);
12280 if (row)
12281 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12282 dy, nrows_scrolled);
12283 else
12285 clear_glyph_matrix (w->desired_matrix);
12286 return 0;
12290 /* Scroll the display. Do it before the current matrix is
12291 changed. The problem here is that update has not yet
12292 run, i.e. part of the current matrix is not up to date.
12293 scroll_run_hook will clear the cursor, and use the
12294 current matrix to get the height of the row the cursor is
12295 in. */
12296 run.current_y = first_row_y;
12297 run.desired_y = it.current_y;
12298 run.height = it.last_visible_y - it.current_y;
12300 if (run.height > 0 && run.current_y != run.desired_y)
12302 update_begin (f);
12303 FRAME_RIF (f)->update_window_begin_hook (w);
12304 FRAME_RIF (f)->clear_window_mouse_face (w);
12305 FRAME_RIF (f)->scroll_run_hook (w, &run);
12306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12307 update_end (f);
12310 /* Shift current matrix down by nrows_scrolled lines. */
12311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12312 rotate_matrix (w->current_matrix,
12313 start_vpos,
12314 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12315 nrows_scrolled);
12317 /* Disable lines that must be updated. */
12318 for (i = 0; i < it.vpos; ++i)
12319 (start_row + i)->enabled_p = 0;
12321 /* Re-compute Y positions. */
12322 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12323 max_y = it.last_visible_y;
12324 for (row = start_row + nrows_scrolled;
12325 row < bottom_row;
12326 ++row)
12328 row->y = it.current_y;
12329 row->visible_height = row->height;
12331 if (row->y < min_y)
12332 row->visible_height -= min_y - row->y;
12333 if (row->y + row->height > max_y)
12334 row->visible_height -= row->y + row->height - max_y;
12336 it.current_y += row->height;
12338 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12339 last_reused_text_row = row;
12340 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12341 break;
12344 /* Disable lines in the current matrix which are now
12345 below the window. */
12346 for (++row; row < bottom_row; ++row)
12347 row->enabled_p = 0;
12350 /* Update window_end_pos etc.; last_reused_text_row is the last
12351 reused row from the current matrix containing text, if any.
12352 The value of last_text_row is the last displayed line
12353 containing text. */
12354 if (last_reused_text_row)
12356 w->window_end_bytepos
12357 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12358 w->window_end_pos
12359 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12360 w->window_end_vpos
12361 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12362 w->current_matrix));
12364 else if (last_text_row)
12366 w->window_end_bytepos
12367 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12368 w->window_end_pos
12369 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12370 w->window_end_vpos
12371 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12373 else
12375 /* This window must be completely empty. */
12376 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12377 w->window_end_pos = make_number (Z - ZV);
12378 w->window_end_vpos = make_number (0);
12380 w->window_end_valid = Qnil;
12382 /* Update hint: don't try scrolling again in update_window. */
12383 w->desired_matrix->no_scrolling_p = 1;
12385 #if GLYPH_DEBUG
12386 debug_method_add (w, "try_window_reusing_current_matrix 1");
12387 #endif
12388 return 1;
12390 else if (CHARPOS (new_start) > CHARPOS (start))
12392 struct glyph_row *pt_row, *row;
12393 struct glyph_row *first_reusable_row;
12394 struct glyph_row *first_row_to_display;
12395 int dy;
12396 int yb = window_text_bottom_y (w);
12398 /* Find the row starting at new_start, if there is one. Don't
12399 reuse a partially visible line at the end. */
12400 first_reusable_row = start_row;
12401 while (first_reusable_row->enabled_p
12402 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12403 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12404 < CHARPOS (new_start)))
12405 ++first_reusable_row;
12407 /* Give up if there is no row to reuse. */
12408 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12409 || !first_reusable_row->enabled_p
12410 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12411 != CHARPOS (new_start)))
12412 return 0;
12414 /* We can reuse fully visible rows beginning with
12415 first_reusable_row to the end of the window. Set
12416 first_row_to_display to the first row that cannot be reused.
12417 Set pt_row to the row containing point, if there is any. */
12418 pt_row = NULL;
12419 for (first_row_to_display = first_reusable_row;
12420 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12421 ++first_row_to_display)
12423 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12424 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12425 pt_row = first_row_to_display;
12428 /* Start displaying at the start of first_row_to_display. */
12429 xassert (first_row_to_display->y < yb);
12430 init_to_row_start (&it, w, first_row_to_display);
12432 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12433 - start_vpos);
12434 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12435 - nrows_scrolled);
12436 it.current_y = (first_row_to_display->y - first_reusable_row->y
12437 + WINDOW_HEADER_LINE_HEIGHT (w));
12439 /* Display lines beginning with first_row_to_display in the
12440 desired matrix. Set last_text_row to the last row displayed
12441 that displays text. */
12442 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12443 if (pt_row == NULL)
12444 w->cursor.vpos = -1;
12445 last_text_row = NULL;
12446 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12447 if (display_line (&it))
12448 last_text_row = it.glyph_row - 1;
12450 /* Give up If point isn't in a row displayed or reused. */
12451 if (w->cursor.vpos < 0)
12453 clear_glyph_matrix (w->desired_matrix);
12454 return 0;
12457 /* If point is in a reused row, adjust y and vpos of the cursor
12458 position. */
12459 if (pt_row)
12461 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12462 w->current_matrix);
12463 w->cursor.y -= first_reusable_row->y;
12466 /* Scroll the display. */
12467 run.current_y = first_reusable_row->y;
12468 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12469 run.height = it.last_visible_y - run.current_y;
12470 dy = run.current_y - run.desired_y;
12472 if (run.height)
12474 struct frame *f = XFRAME (WINDOW_FRAME (w));
12475 update_begin (f);
12476 FRAME_RIF (f)->update_window_begin_hook (w);
12477 FRAME_RIF (f)->clear_window_mouse_face (w);
12478 FRAME_RIF (f)->scroll_run_hook (w, &run);
12479 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12480 update_end (f);
12483 /* Adjust Y positions of reused rows. */
12484 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12485 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12486 max_y = it.last_visible_y;
12487 for (row = first_reusable_row; row < first_row_to_display; ++row)
12489 row->y -= dy;
12490 row->visible_height = row->height;
12491 if (row->y < min_y)
12492 row->visible_height -= min_y - row->y;
12493 if (row->y + row->height > max_y)
12494 row->visible_height -= row->y + row->height - max_y;
12497 /* Scroll the current matrix. */
12498 xassert (nrows_scrolled > 0);
12499 rotate_matrix (w->current_matrix,
12500 start_vpos,
12501 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12502 -nrows_scrolled);
12504 /* Disable rows not reused. */
12505 for (row -= nrows_scrolled; row < bottom_row; ++row)
12506 row->enabled_p = 0;
12508 /* Adjust window end. A null value of last_text_row means that
12509 the window end is in reused rows which in turn means that
12510 only its vpos can have changed. */
12511 if (last_text_row)
12513 w->window_end_bytepos
12514 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12515 w->window_end_pos
12516 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12517 w->window_end_vpos
12518 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12520 else
12522 w->window_end_vpos
12523 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12526 w->window_end_valid = Qnil;
12527 w->desired_matrix->no_scrolling_p = 1;
12529 #if GLYPH_DEBUG
12530 debug_method_add (w, "try_window_reusing_current_matrix 2");
12531 #endif
12532 return 1;
12535 return 0;
12540 /************************************************************************
12541 Window redisplay reusing current matrix when buffer has changed
12542 ************************************************************************/
12544 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12545 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12546 int *, int *));
12547 static struct glyph_row *
12548 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12549 struct glyph_row *));
12552 /* Return the last row in MATRIX displaying text. If row START is
12553 non-null, start searching with that row. IT gives the dimensions
12554 of the display. Value is null if matrix is empty; otherwise it is
12555 a pointer to the row found. */
12557 static struct glyph_row *
12558 find_last_row_displaying_text (matrix, it, start)
12559 struct glyph_matrix *matrix;
12560 struct it *it;
12561 struct glyph_row *start;
12563 struct glyph_row *row, *row_found;
12565 /* Set row_found to the last row in IT->w's current matrix
12566 displaying text. The loop looks funny but think of partially
12567 visible lines. */
12568 row_found = NULL;
12569 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12570 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12572 xassert (row->enabled_p);
12573 row_found = row;
12574 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12575 break;
12576 ++row;
12579 return row_found;
12583 /* Return the last row in the current matrix of W that is not affected
12584 by changes at the start of current_buffer that occurred since W's
12585 current matrix was built. Value is null if no such row exists.
12587 BEG_UNCHANGED us the number of characters unchanged at the start of
12588 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12589 first changed character in current_buffer. Characters at positions <
12590 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12591 when the current matrix was built. */
12593 static struct glyph_row *
12594 find_last_unchanged_at_beg_row (w)
12595 struct window *w;
12597 int first_changed_pos = BEG + BEG_UNCHANGED;
12598 struct glyph_row *row;
12599 struct glyph_row *row_found = NULL;
12600 int yb = window_text_bottom_y (w);
12602 /* Find the last row displaying unchanged text. */
12603 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12604 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12605 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12607 if (/* If row ends before first_changed_pos, it is unchanged,
12608 except in some case. */
12609 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12610 /* When row ends in ZV and we write at ZV it is not
12611 unchanged. */
12612 && !row->ends_at_zv_p
12613 /* When first_changed_pos is the end of a continued line,
12614 row is not unchanged because it may be no longer
12615 continued. */
12616 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12617 && row->continued_p))
12618 row_found = row;
12620 /* Stop if last visible row. */
12621 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12622 break;
12624 ++row;
12627 return row_found;
12631 /* Find the first glyph row in the current matrix of W that is not
12632 affected by changes at the end of current_buffer since the
12633 time W's current matrix was built.
12635 Return in *DELTA the number of chars by which buffer positions in
12636 unchanged text at the end of current_buffer must be adjusted.
12638 Return in *DELTA_BYTES the corresponding number of bytes.
12640 Value is null if no such row exists, i.e. all rows are affected by
12641 changes. */
12643 static struct glyph_row *
12644 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12645 struct window *w;
12646 int *delta, *delta_bytes;
12648 struct glyph_row *row;
12649 struct glyph_row *row_found = NULL;
12651 *delta = *delta_bytes = 0;
12653 /* Display must not have been paused, otherwise the current matrix
12654 is not up to date. */
12655 if (NILP (w->window_end_valid))
12656 abort ();
12658 /* A value of window_end_pos >= END_UNCHANGED means that the window
12659 end is in the range of changed text. If so, there is no
12660 unchanged row at the end of W's current matrix. */
12661 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12662 return NULL;
12664 /* Set row to the last row in W's current matrix displaying text. */
12665 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12667 /* If matrix is entirely empty, no unchanged row exists. */
12668 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12670 /* The value of row is the last glyph row in the matrix having a
12671 meaningful buffer position in it. The end position of row
12672 corresponds to window_end_pos. This allows us to translate
12673 buffer positions in the current matrix to current buffer
12674 positions for characters not in changed text. */
12675 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12676 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12677 int last_unchanged_pos, last_unchanged_pos_old;
12678 struct glyph_row *first_text_row
12679 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12681 *delta = Z - Z_old;
12682 *delta_bytes = Z_BYTE - Z_BYTE_old;
12684 /* Set last_unchanged_pos to the buffer position of the last
12685 character in the buffer that has not been changed. Z is the
12686 index + 1 of the last character in current_buffer, i.e. by
12687 subtracting END_UNCHANGED we get the index of the last
12688 unchanged character, and we have to add BEG to get its buffer
12689 position. */
12690 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12691 last_unchanged_pos_old = last_unchanged_pos - *delta;
12693 /* Search backward from ROW for a row displaying a line that
12694 starts at a minimum position >= last_unchanged_pos_old. */
12695 for (; row > first_text_row; --row)
12697 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12698 abort ();
12700 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12701 row_found = row;
12705 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12706 abort ();
12708 return row_found;
12712 /* Make sure that glyph rows in the current matrix of window W
12713 reference the same glyph memory as corresponding rows in the
12714 frame's frame matrix. This function is called after scrolling W's
12715 current matrix on a terminal frame in try_window_id and
12716 try_window_reusing_current_matrix. */
12718 static void
12719 sync_frame_with_window_matrix_rows (w)
12720 struct window *w;
12722 struct frame *f = XFRAME (w->frame);
12723 struct glyph_row *window_row, *window_row_end, *frame_row;
12725 /* Preconditions: W must be a leaf window and full-width. Its frame
12726 must have a frame matrix. */
12727 xassert (NILP (w->hchild) && NILP (w->vchild));
12728 xassert (WINDOW_FULL_WIDTH_P (w));
12729 xassert (!FRAME_WINDOW_P (f));
12731 /* If W is a full-width window, glyph pointers in W's current matrix
12732 have, by definition, to be the same as glyph pointers in the
12733 corresponding frame matrix. Note that frame matrices have no
12734 marginal areas (see build_frame_matrix). */
12735 window_row = w->current_matrix->rows;
12736 window_row_end = window_row + w->current_matrix->nrows;
12737 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12738 while (window_row < window_row_end)
12740 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12741 struct glyph *end = window_row->glyphs[LAST_AREA];
12743 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12744 frame_row->glyphs[TEXT_AREA] = start;
12745 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12746 frame_row->glyphs[LAST_AREA] = end;
12748 /* Disable frame rows whose corresponding window rows have
12749 been disabled in try_window_id. */
12750 if (!window_row->enabled_p)
12751 frame_row->enabled_p = 0;
12753 ++window_row, ++frame_row;
12758 /* Find the glyph row in window W containing CHARPOS. Consider all
12759 rows between START and END (not inclusive). END null means search
12760 all rows to the end of the display area of W. Value is the row
12761 containing CHARPOS or null. */
12763 struct glyph_row *
12764 row_containing_pos (w, charpos, start, end, dy)
12765 struct window *w;
12766 int charpos;
12767 struct glyph_row *start, *end;
12768 int dy;
12770 struct glyph_row *row = start;
12771 int last_y;
12773 /* If we happen to start on a header-line, skip that. */
12774 if (row->mode_line_p)
12775 ++row;
12777 if ((end && row >= end) || !row->enabled_p)
12778 return NULL;
12780 last_y = window_text_bottom_y (w) - dy;
12782 while (1)
12784 /* Give up if we have gone too far. */
12785 if (end && row >= end)
12786 return NULL;
12787 /* This formerly returned if they were equal.
12788 I think that both quantities are of a "last plus one" type;
12789 if so, when they are equal, the row is within the screen. -- rms. */
12790 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12791 return NULL;
12793 /* If it is in this row, return this row. */
12794 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12795 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12796 /* The end position of a row equals the start
12797 position of the next row. If CHARPOS is there, we
12798 would rather display it in the next line, except
12799 when this line ends in ZV. */
12800 && !row->ends_at_zv_p
12801 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12802 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12803 return row;
12804 ++row;
12809 /* Try to redisplay window W by reusing its existing display. W's
12810 current matrix must be up to date when this function is called,
12811 i.e. window_end_valid must not be nil.
12813 Value is
12815 1 if display has been updated
12816 0 if otherwise unsuccessful
12817 -1 if redisplay with same window start is known not to succeed
12819 The following steps are performed:
12821 1. Find the last row in the current matrix of W that is not
12822 affected by changes at the start of current_buffer. If no such row
12823 is found, give up.
12825 2. Find the first row in W's current matrix that is not affected by
12826 changes at the end of current_buffer. Maybe there is no such row.
12828 3. Display lines beginning with the row + 1 found in step 1 to the
12829 row found in step 2 or, if step 2 didn't find a row, to the end of
12830 the window.
12832 4. If cursor is not known to appear on the window, give up.
12834 5. If display stopped at the row found in step 2, scroll the
12835 display and current matrix as needed.
12837 6. Maybe display some lines at the end of W, if we must. This can
12838 happen under various circumstances, like a partially visible line
12839 becoming fully visible, or because newly displayed lines are displayed
12840 in smaller font sizes.
12842 7. Update W's window end information. */
12844 static int
12845 try_window_id (w)
12846 struct window *w;
12848 struct frame *f = XFRAME (w->frame);
12849 struct glyph_matrix *current_matrix = w->current_matrix;
12850 struct glyph_matrix *desired_matrix = w->desired_matrix;
12851 struct glyph_row *last_unchanged_at_beg_row;
12852 struct glyph_row *first_unchanged_at_end_row;
12853 struct glyph_row *row;
12854 struct glyph_row *bottom_row;
12855 int bottom_vpos;
12856 struct it it;
12857 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12858 struct text_pos start_pos;
12859 struct run run;
12860 int first_unchanged_at_end_vpos = 0;
12861 struct glyph_row *last_text_row, *last_text_row_at_end;
12862 struct text_pos start;
12863 int first_changed_charpos, last_changed_charpos;
12865 #if GLYPH_DEBUG
12866 if (inhibit_try_window_id)
12867 return 0;
12868 #endif
12870 /* This is handy for debugging. */
12871 #if 0
12872 #define GIVE_UP(X) \
12873 do { \
12874 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12875 return 0; \
12876 } while (0)
12877 #else
12878 #define GIVE_UP(X) return 0
12879 #endif
12881 SET_TEXT_POS_FROM_MARKER (start, w->start);
12883 /* Don't use this for mini-windows because these can show
12884 messages and mini-buffers, and we don't handle that here. */
12885 if (MINI_WINDOW_P (w))
12886 GIVE_UP (1);
12888 /* This flag is used to prevent redisplay optimizations. */
12889 if (windows_or_buffers_changed || cursor_type_changed)
12890 GIVE_UP (2);
12892 /* Verify that narrowing has not changed.
12893 Also verify that we were not told to prevent redisplay optimizations.
12894 It would be nice to further
12895 reduce the number of cases where this prevents try_window_id. */
12896 if (current_buffer->clip_changed
12897 || current_buffer->prevent_redisplay_optimizations_p)
12898 GIVE_UP (3);
12900 /* Window must either use window-based redisplay or be full width. */
12901 if (!FRAME_WINDOW_P (f)
12902 && (!FRAME_LINE_INS_DEL_OK (f)
12903 || !WINDOW_FULL_WIDTH_P (w)))
12904 GIVE_UP (4);
12906 /* Give up if point is not known NOT to appear in W. */
12907 if (PT < CHARPOS (start))
12908 GIVE_UP (5);
12910 /* Another way to prevent redisplay optimizations. */
12911 if (XFASTINT (w->last_modified) == 0)
12912 GIVE_UP (6);
12914 /* Verify that window is not hscrolled. */
12915 if (XFASTINT (w->hscroll) != 0)
12916 GIVE_UP (7);
12918 /* Verify that display wasn't paused. */
12919 if (NILP (w->window_end_valid))
12920 GIVE_UP (8);
12922 /* Can't use this if highlighting a region because a cursor movement
12923 will do more than just set the cursor. */
12924 if (!NILP (Vtransient_mark_mode)
12925 && !NILP (current_buffer->mark_active))
12926 GIVE_UP (9);
12928 /* Likewise if highlighting trailing whitespace. */
12929 if (!NILP (Vshow_trailing_whitespace))
12930 GIVE_UP (11);
12932 /* Likewise if showing a region. */
12933 if (!NILP (w->region_showing))
12934 GIVE_UP (10);
12936 /* Can use this if overlay arrow position and or string have changed. */
12937 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12938 || !EQ (last_arrow_string, Voverlay_arrow_string))
12939 GIVE_UP (12);
12942 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12943 only if buffer has really changed. The reason is that the gap is
12944 initially at Z for freshly visited files. The code below would
12945 set end_unchanged to 0 in that case. */
12946 if (MODIFF > SAVE_MODIFF
12947 /* This seems to happen sometimes after saving a buffer. */
12948 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12950 if (GPT - BEG < BEG_UNCHANGED)
12951 BEG_UNCHANGED = GPT - BEG;
12952 if (Z - GPT < END_UNCHANGED)
12953 END_UNCHANGED = Z - GPT;
12956 /* The position of the first and last character that has been changed. */
12957 first_changed_charpos = BEG + BEG_UNCHANGED;
12958 last_changed_charpos = Z - END_UNCHANGED;
12960 /* If window starts after a line end, and the last change is in
12961 front of that newline, then changes don't affect the display.
12962 This case happens with stealth-fontification. Note that although
12963 the display is unchanged, glyph positions in the matrix have to
12964 be adjusted, of course. */
12965 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12966 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12967 && ((last_changed_charpos < CHARPOS (start)
12968 && CHARPOS (start) == BEGV)
12969 || (last_changed_charpos < CHARPOS (start) - 1
12970 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12972 int Z_old, delta, Z_BYTE_old, delta_bytes;
12973 struct glyph_row *r0;
12975 /* Compute how many chars/bytes have been added to or removed
12976 from the buffer. */
12977 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12978 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12979 delta = Z - Z_old;
12980 delta_bytes = Z_BYTE - Z_BYTE_old;
12982 /* Give up if PT is not in the window. Note that it already has
12983 been checked at the start of try_window_id that PT is not in
12984 front of the window start. */
12985 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12986 GIVE_UP (13);
12988 /* If window start is unchanged, we can reuse the whole matrix
12989 as is, after adjusting glyph positions. No need to compute
12990 the window end again, since its offset from Z hasn't changed. */
12991 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12992 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12993 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12994 /* PT must not be in a partially visible line. */
12995 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12996 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12998 /* Adjust positions in the glyph matrix. */
12999 if (delta || delta_bytes)
13001 struct glyph_row *r1
13002 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13003 increment_matrix_positions (w->current_matrix,
13004 MATRIX_ROW_VPOS (r0, current_matrix),
13005 MATRIX_ROW_VPOS (r1, current_matrix),
13006 delta, delta_bytes);
13009 /* Set the cursor. */
13010 row = row_containing_pos (w, PT, r0, NULL, 0);
13011 if (row)
13012 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13013 else
13014 abort ();
13015 return 1;
13019 /* Handle the case that changes are all below what is displayed in
13020 the window, and that PT is in the window. This shortcut cannot
13021 be taken if ZV is visible in the window, and text has been added
13022 there that is visible in the window. */
13023 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13024 /* ZV is not visible in the window, or there are no
13025 changes at ZV, actually. */
13026 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13027 || first_changed_charpos == last_changed_charpos))
13029 struct glyph_row *r0;
13031 /* Give up if PT is not in the window. Note that it already has
13032 been checked at the start of try_window_id that PT is not in
13033 front of the window start. */
13034 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13035 GIVE_UP (14);
13037 /* If window start is unchanged, we can reuse the whole matrix
13038 as is, without changing glyph positions since no text has
13039 been added/removed in front of the window end. */
13040 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13041 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13042 /* PT must not be in a partially visible line. */
13043 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13044 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13046 /* We have to compute the window end anew since text
13047 can have been added/removed after it. */
13048 w->window_end_pos
13049 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13050 w->window_end_bytepos
13051 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13053 /* Set the cursor. */
13054 row = row_containing_pos (w, PT, r0, NULL, 0);
13055 if (row)
13056 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13057 else
13058 abort ();
13059 return 2;
13063 /* Give up if window start is in the changed area.
13065 The condition used to read
13067 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13069 but why that was tested escapes me at the moment. */
13070 if (CHARPOS (start) >= first_changed_charpos
13071 && CHARPOS (start) <= last_changed_charpos)
13072 GIVE_UP (15);
13074 /* Check that window start agrees with the start of the first glyph
13075 row in its current matrix. Check this after we know the window
13076 start is not in changed text, otherwise positions would not be
13077 comparable. */
13078 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13079 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13080 GIVE_UP (16);
13082 /* Give up if the window ends in strings. Overlay strings
13083 at the end are difficult to handle, so don't try. */
13084 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13085 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13086 GIVE_UP (20);
13088 /* Compute the position at which we have to start displaying new
13089 lines. Some of the lines at the top of the window might be
13090 reusable because they are not displaying changed text. Find the
13091 last row in W's current matrix not affected by changes at the
13092 start of current_buffer. Value is null if changes start in the
13093 first line of window. */
13094 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13095 if (last_unchanged_at_beg_row)
13097 /* Avoid starting to display in the moddle of a character, a TAB
13098 for instance. This is easier than to set up the iterator
13099 exactly, and it's not a frequent case, so the additional
13100 effort wouldn't really pay off. */
13101 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13102 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13103 && last_unchanged_at_beg_row > w->current_matrix->rows)
13104 --last_unchanged_at_beg_row;
13106 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13107 GIVE_UP (17);
13109 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13110 GIVE_UP (18);
13111 start_pos = it.current.pos;
13113 /* Start displaying new lines in the desired matrix at the same
13114 vpos we would use in the current matrix, i.e. below
13115 last_unchanged_at_beg_row. */
13116 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13117 current_matrix);
13118 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13119 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13121 xassert (it.hpos == 0 && it.current_x == 0);
13123 else
13125 /* There are no reusable lines at the start of the window.
13126 Start displaying in the first text line. */
13127 start_display (&it, w, start);
13128 it.vpos = it.first_vpos;
13129 start_pos = it.current.pos;
13132 /* Find the first row that is not affected by changes at the end of
13133 the buffer. Value will be null if there is no unchanged row, in
13134 which case we must redisplay to the end of the window. delta
13135 will be set to the value by which buffer positions beginning with
13136 first_unchanged_at_end_row have to be adjusted due to text
13137 changes. */
13138 first_unchanged_at_end_row
13139 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13140 IF_DEBUG (debug_delta = delta);
13141 IF_DEBUG (debug_delta_bytes = delta_bytes);
13143 /* Set stop_pos to the buffer position up to which we will have to
13144 display new lines. If first_unchanged_at_end_row != NULL, this
13145 is the buffer position of the start of the line displayed in that
13146 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13147 that we don't stop at a buffer position. */
13148 stop_pos = 0;
13149 if (first_unchanged_at_end_row)
13151 xassert (last_unchanged_at_beg_row == NULL
13152 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13154 /* If this is a continuation line, move forward to the next one
13155 that isn't. Changes in lines above affect this line.
13156 Caution: this may move first_unchanged_at_end_row to a row
13157 not displaying text. */
13158 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13159 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13160 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13161 < it.last_visible_y))
13162 ++first_unchanged_at_end_row;
13164 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13165 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13166 >= it.last_visible_y))
13167 first_unchanged_at_end_row = NULL;
13168 else
13170 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13171 + delta);
13172 first_unchanged_at_end_vpos
13173 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13174 xassert (stop_pos >= Z - END_UNCHANGED);
13177 else if (last_unchanged_at_beg_row == NULL)
13178 GIVE_UP (19);
13181 #if GLYPH_DEBUG
13183 /* Either there is no unchanged row at the end, or the one we have
13184 now displays text. This is a necessary condition for the window
13185 end pos calculation at the end of this function. */
13186 xassert (first_unchanged_at_end_row == NULL
13187 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13189 debug_last_unchanged_at_beg_vpos
13190 = (last_unchanged_at_beg_row
13191 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13192 : -1);
13193 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13195 #endif /* GLYPH_DEBUG != 0 */
13198 /* Display new lines. Set last_text_row to the last new line
13199 displayed which has text on it, i.e. might end up as being the
13200 line where the window_end_vpos is. */
13201 w->cursor.vpos = -1;
13202 last_text_row = NULL;
13203 overlay_arrow_seen = 0;
13204 while (it.current_y < it.last_visible_y
13205 && !fonts_changed_p
13206 && (first_unchanged_at_end_row == NULL
13207 || IT_CHARPOS (it) < stop_pos))
13209 if (display_line (&it))
13210 last_text_row = it.glyph_row - 1;
13213 if (fonts_changed_p)
13214 return -1;
13217 /* Compute differences in buffer positions, y-positions etc. for
13218 lines reused at the bottom of the window. Compute what we can
13219 scroll. */
13220 if (first_unchanged_at_end_row
13221 /* No lines reused because we displayed everything up to the
13222 bottom of the window. */
13223 && it.current_y < it.last_visible_y)
13225 dvpos = (it.vpos
13226 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13227 current_matrix));
13228 dy = it.current_y - first_unchanged_at_end_row->y;
13229 run.current_y = first_unchanged_at_end_row->y;
13230 run.desired_y = run.current_y + dy;
13231 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13233 else
13235 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13236 first_unchanged_at_end_row = NULL;
13238 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13241 /* Find the cursor if not already found. We have to decide whether
13242 PT will appear on this window (it sometimes doesn't, but this is
13243 not a very frequent case.) This decision has to be made before
13244 the current matrix is altered. A value of cursor.vpos < 0 means
13245 that PT is either in one of the lines beginning at
13246 first_unchanged_at_end_row or below the window. Don't care for
13247 lines that might be displayed later at the window end; as
13248 mentioned, this is not a frequent case. */
13249 if (w->cursor.vpos < 0)
13251 /* Cursor in unchanged rows at the top? */
13252 if (PT < CHARPOS (start_pos)
13253 && last_unchanged_at_beg_row)
13255 row = row_containing_pos (w, PT,
13256 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13257 last_unchanged_at_beg_row + 1, 0);
13258 if (row)
13259 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13262 /* Start from first_unchanged_at_end_row looking for PT. */
13263 else if (first_unchanged_at_end_row)
13265 row = row_containing_pos (w, PT - delta,
13266 first_unchanged_at_end_row, NULL, 0);
13267 if (row)
13268 set_cursor_from_row (w, row, w->current_matrix, delta,
13269 delta_bytes, dy, dvpos);
13272 /* Give up if cursor was not found. */
13273 if (w->cursor.vpos < 0)
13275 clear_glyph_matrix (w->desired_matrix);
13276 return -1;
13280 /* Don't let the cursor end in the scroll margins. */
13282 int this_scroll_margin, cursor_height;
13284 this_scroll_margin = max (0, scroll_margin);
13285 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13286 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13287 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13289 if ((w->cursor.y < this_scroll_margin
13290 && CHARPOS (start) > BEGV)
13291 /* Don't take scroll margin into account at the bottom because
13292 old redisplay didn't do it either. */
13293 || w->cursor.y + cursor_height > it.last_visible_y)
13295 w->cursor.vpos = -1;
13296 clear_glyph_matrix (w->desired_matrix);
13297 return -1;
13301 /* Scroll the display. Do it before changing the current matrix so
13302 that xterm.c doesn't get confused about where the cursor glyph is
13303 found. */
13304 if (dy && run.height)
13306 update_begin (f);
13308 if (FRAME_WINDOW_P (f))
13310 FRAME_RIF (f)->update_window_begin_hook (w);
13311 FRAME_RIF (f)->clear_window_mouse_face (w);
13312 FRAME_RIF (f)->scroll_run_hook (w, &run);
13313 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13315 else
13317 /* Terminal frame. In this case, dvpos gives the number of
13318 lines to scroll by; dvpos < 0 means scroll up. */
13319 int first_unchanged_at_end_vpos
13320 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13321 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13322 int end = (WINDOW_TOP_EDGE_LINE (w)
13323 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13324 + window_internal_height (w));
13326 /* Perform the operation on the screen. */
13327 if (dvpos > 0)
13329 /* Scroll last_unchanged_at_beg_row to the end of the
13330 window down dvpos lines. */
13331 set_terminal_window (end);
13333 /* On dumb terminals delete dvpos lines at the end
13334 before inserting dvpos empty lines. */
13335 if (!FRAME_SCROLL_REGION_OK (f))
13336 ins_del_lines (end - dvpos, -dvpos);
13338 /* Insert dvpos empty lines in front of
13339 last_unchanged_at_beg_row. */
13340 ins_del_lines (from, dvpos);
13342 else if (dvpos < 0)
13344 /* Scroll up last_unchanged_at_beg_vpos to the end of
13345 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13346 set_terminal_window (end);
13348 /* Delete dvpos lines in front of
13349 last_unchanged_at_beg_vpos. ins_del_lines will set
13350 the cursor to the given vpos and emit |dvpos| delete
13351 line sequences. */
13352 ins_del_lines (from + dvpos, dvpos);
13354 /* On a dumb terminal insert dvpos empty lines at the
13355 end. */
13356 if (!FRAME_SCROLL_REGION_OK (f))
13357 ins_del_lines (end + dvpos, -dvpos);
13360 set_terminal_window (0);
13363 update_end (f);
13366 /* Shift reused rows of the current matrix to the right position.
13367 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13368 text. */
13369 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13370 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13371 if (dvpos < 0)
13373 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13374 bottom_vpos, dvpos);
13375 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13376 bottom_vpos, 0);
13378 else if (dvpos > 0)
13380 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13381 bottom_vpos, dvpos);
13382 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13383 first_unchanged_at_end_vpos + dvpos, 0);
13386 /* For frame-based redisplay, make sure that current frame and window
13387 matrix are in sync with respect to glyph memory. */
13388 if (!FRAME_WINDOW_P (f))
13389 sync_frame_with_window_matrix_rows (w);
13391 /* Adjust buffer positions in reused rows. */
13392 if (delta)
13393 increment_matrix_positions (current_matrix,
13394 first_unchanged_at_end_vpos + dvpos,
13395 bottom_vpos, delta, delta_bytes);
13397 /* Adjust Y positions. */
13398 if (dy)
13399 shift_glyph_matrix (w, current_matrix,
13400 first_unchanged_at_end_vpos + dvpos,
13401 bottom_vpos, dy);
13403 if (first_unchanged_at_end_row)
13404 first_unchanged_at_end_row += dvpos;
13406 /* If scrolling up, there may be some lines to display at the end of
13407 the window. */
13408 last_text_row_at_end = NULL;
13409 if (dy < 0)
13411 /* Scrolling up can leave for example a partially visible line
13412 at the end of the window to be redisplayed. */
13413 /* Set last_row to the glyph row in the current matrix where the
13414 window end line is found. It has been moved up or down in
13415 the matrix by dvpos. */
13416 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13417 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13419 /* If last_row is the window end line, it should display text. */
13420 xassert (last_row->displays_text_p);
13422 /* If window end line was partially visible before, begin
13423 displaying at that line. Otherwise begin displaying with the
13424 line following it. */
13425 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13427 init_to_row_start (&it, w, last_row);
13428 it.vpos = last_vpos;
13429 it.current_y = last_row->y;
13431 else
13433 init_to_row_end (&it, w, last_row);
13434 it.vpos = 1 + last_vpos;
13435 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13436 ++last_row;
13439 /* We may start in a continuation line. If so, we have to
13440 get the right continuation_lines_width and current_x. */
13441 it.continuation_lines_width = last_row->continuation_lines_width;
13442 it.hpos = it.current_x = 0;
13444 /* Display the rest of the lines at the window end. */
13445 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13446 while (it.current_y < it.last_visible_y
13447 && !fonts_changed_p)
13449 /* Is it always sure that the display agrees with lines in
13450 the current matrix? I don't think so, so we mark rows
13451 displayed invalid in the current matrix by setting their
13452 enabled_p flag to zero. */
13453 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13454 if (display_line (&it))
13455 last_text_row_at_end = it.glyph_row - 1;
13459 /* Update window_end_pos and window_end_vpos. */
13460 if (first_unchanged_at_end_row
13461 && first_unchanged_at_end_row->y < it.last_visible_y
13462 && !last_text_row_at_end)
13464 /* Window end line if one of the preserved rows from the current
13465 matrix. Set row to the last row displaying text in current
13466 matrix starting at first_unchanged_at_end_row, after
13467 scrolling. */
13468 xassert (first_unchanged_at_end_row->displays_text_p);
13469 row = find_last_row_displaying_text (w->current_matrix, &it,
13470 first_unchanged_at_end_row);
13471 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13473 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13474 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13475 w->window_end_vpos
13476 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13477 xassert (w->window_end_bytepos >= 0);
13478 IF_DEBUG (debug_method_add (w, "A"));
13480 else if (last_text_row_at_end)
13482 w->window_end_pos
13483 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13484 w->window_end_bytepos
13485 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13486 w->window_end_vpos
13487 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13488 xassert (w->window_end_bytepos >= 0);
13489 IF_DEBUG (debug_method_add (w, "B"));
13491 else if (last_text_row)
13493 /* We have displayed either to the end of the window or at the
13494 end of the window, i.e. the last row with text is to be found
13495 in the desired matrix. */
13496 w->window_end_pos
13497 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13498 w->window_end_bytepos
13499 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13500 w->window_end_vpos
13501 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13502 xassert (w->window_end_bytepos >= 0);
13504 else if (first_unchanged_at_end_row == NULL
13505 && last_text_row == NULL
13506 && last_text_row_at_end == NULL)
13508 /* Displayed to end of window, but no line containing text was
13509 displayed. Lines were deleted at the end of the window. */
13510 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13511 int vpos = XFASTINT (w->window_end_vpos);
13512 struct glyph_row *current_row = current_matrix->rows + vpos;
13513 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13515 for (row = NULL;
13516 row == NULL && vpos >= first_vpos;
13517 --vpos, --current_row, --desired_row)
13519 if (desired_row->enabled_p)
13521 if (desired_row->displays_text_p)
13522 row = desired_row;
13524 else if (current_row->displays_text_p)
13525 row = current_row;
13528 xassert (row != NULL);
13529 w->window_end_vpos = make_number (vpos + 1);
13530 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13531 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13532 xassert (w->window_end_bytepos >= 0);
13533 IF_DEBUG (debug_method_add (w, "C"));
13535 else
13536 abort ();
13538 #if 0 /* This leads to problems, for instance when the cursor is
13539 at ZV, and the cursor line displays no text. */
13540 /* Disable rows below what's displayed in the window. This makes
13541 debugging easier. */
13542 enable_glyph_matrix_rows (current_matrix,
13543 XFASTINT (w->window_end_vpos) + 1,
13544 bottom_vpos, 0);
13545 #endif
13547 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13548 debug_end_vpos = XFASTINT (w->window_end_vpos));
13550 /* Record that display has not been completed. */
13551 w->window_end_valid = Qnil;
13552 w->desired_matrix->no_scrolling_p = 1;
13553 return 3;
13555 #undef GIVE_UP
13560 /***********************************************************************
13561 More debugging support
13562 ***********************************************************************/
13564 #if GLYPH_DEBUG
13566 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13567 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13568 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13571 /* Dump the contents of glyph matrix MATRIX on stderr.
13573 GLYPHS 0 means don't show glyph contents.
13574 GLYPHS 1 means show glyphs in short form
13575 GLYPHS > 1 means show glyphs in long form. */
13577 void
13578 dump_glyph_matrix (matrix, glyphs)
13579 struct glyph_matrix *matrix;
13580 int glyphs;
13582 int i;
13583 for (i = 0; i < matrix->nrows; ++i)
13584 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13588 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13589 the glyph row and area where the glyph comes from. */
13591 void
13592 dump_glyph (row, glyph, area)
13593 struct glyph_row *row;
13594 struct glyph *glyph;
13595 int area;
13597 if (glyph->type == CHAR_GLYPH)
13599 fprintf (stderr,
13600 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13601 glyph - row->glyphs[TEXT_AREA],
13602 'C',
13603 glyph->charpos,
13604 (BUFFERP (glyph->object)
13605 ? 'B'
13606 : (STRINGP (glyph->object)
13607 ? 'S'
13608 : '-')),
13609 glyph->pixel_width,
13610 glyph->u.ch,
13611 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13612 ? glyph->u.ch
13613 : '.'),
13614 glyph->face_id,
13615 glyph->left_box_line_p,
13616 glyph->right_box_line_p);
13618 else if (glyph->type == STRETCH_GLYPH)
13620 fprintf (stderr,
13621 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13622 glyph - row->glyphs[TEXT_AREA],
13623 'S',
13624 glyph->charpos,
13625 (BUFFERP (glyph->object)
13626 ? 'B'
13627 : (STRINGP (glyph->object)
13628 ? 'S'
13629 : '-')),
13630 glyph->pixel_width,
13632 '.',
13633 glyph->face_id,
13634 glyph->left_box_line_p,
13635 glyph->right_box_line_p);
13637 else if (glyph->type == IMAGE_GLYPH)
13639 fprintf (stderr,
13640 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13641 glyph - row->glyphs[TEXT_AREA],
13642 'I',
13643 glyph->charpos,
13644 (BUFFERP (glyph->object)
13645 ? 'B'
13646 : (STRINGP (glyph->object)
13647 ? 'S'
13648 : '-')),
13649 glyph->pixel_width,
13650 glyph->u.img_id,
13651 '.',
13652 glyph->face_id,
13653 glyph->left_box_line_p,
13654 glyph->right_box_line_p);
13659 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13660 GLYPHS 0 means don't show glyph contents.
13661 GLYPHS 1 means show glyphs in short form
13662 GLYPHS > 1 means show glyphs in long form. */
13664 void
13665 dump_glyph_row (row, vpos, glyphs)
13666 struct glyph_row *row;
13667 int vpos, glyphs;
13669 if (glyphs != 1)
13671 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13672 fprintf (stderr, "=======================================================================\n");
13674 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13675 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13676 vpos,
13677 MATRIX_ROW_START_CHARPOS (row),
13678 MATRIX_ROW_END_CHARPOS (row),
13679 row->used[TEXT_AREA],
13680 row->contains_overlapping_glyphs_p,
13681 row->enabled_p,
13682 row->truncated_on_left_p,
13683 row->truncated_on_right_p,
13684 row->overlay_arrow_p,
13685 row->continued_p,
13686 MATRIX_ROW_CONTINUATION_LINE_P (row),
13687 row->displays_text_p,
13688 row->ends_at_zv_p,
13689 row->fill_line_p,
13690 row->ends_in_middle_of_char_p,
13691 row->starts_in_middle_of_char_p,
13692 row->mouse_face_p,
13693 row->x,
13694 row->y,
13695 row->pixel_width,
13696 row->height,
13697 row->visible_height,
13698 row->ascent,
13699 row->phys_ascent);
13700 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13701 row->end.overlay_string_index,
13702 row->continuation_lines_width);
13703 fprintf (stderr, "%9d %5d\n",
13704 CHARPOS (row->start.string_pos),
13705 CHARPOS (row->end.string_pos));
13706 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13707 row->end.dpvec_index);
13710 if (glyphs > 1)
13712 int area;
13714 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13716 struct glyph *glyph = row->glyphs[area];
13717 struct glyph *glyph_end = glyph + row->used[area];
13719 /* Glyph for a line end in text. */
13720 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13721 ++glyph_end;
13723 if (glyph < glyph_end)
13724 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13726 for (; glyph < glyph_end; ++glyph)
13727 dump_glyph (row, glyph, area);
13730 else if (glyphs == 1)
13732 int area;
13734 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13736 char *s = (char *) alloca (row->used[area] + 1);
13737 int i;
13739 for (i = 0; i < row->used[area]; ++i)
13741 struct glyph *glyph = row->glyphs[area] + i;
13742 if (glyph->type == CHAR_GLYPH
13743 && glyph->u.ch < 0x80
13744 && glyph->u.ch >= ' ')
13745 s[i] = glyph->u.ch;
13746 else
13747 s[i] = '.';
13750 s[i] = '\0';
13751 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13757 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13758 Sdump_glyph_matrix, 0, 1, "p",
13759 doc: /* Dump the current matrix of the selected window to stderr.
13760 Shows contents of glyph row structures. With non-nil
13761 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13762 glyphs in short form, otherwise show glyphs in long form. */)
13763 (glyphs)
13764 Lisp_Object glyphs;
13766 struct window *w = XWINDOW (selected_window);
13767 struct buffer *buffer = XBUFFER (w->buffer);
13769 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13770 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13771 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13772 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13773 fprintf (stderr, "=============================================\n");
13774 dump_glyph_matrix (w->current_matrix,
13775 NILP (glyphs) ? 0 : XINT (glyphs));
13776 return Qnil;
13780 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13781 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13784 struct frame *f = XFRAME (selected_frame);
13785 dump_glyph_matrix (f->current_matrix, 1);
13786 return Qnil;
13790 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13791 doc: /* Dump glyph row ROW to stderr.
13792 GLYPH 0 means don't dump glyphs.
13793 GLYPH 1 means dump glyphs in short form.
13794 GLYPH > 1 or omitted means dump glyphs in long form. */)
13795 (row, glyphs)
13796 Lisp_Object row, glyphs;
13798 struct glyph_matrix *matrix;
13799 int vpos;
13801 CHECK_NUMBER (row);
13802 matrix = XWINDOW (selected_window)->current_matrix;
13803 vpos = XINT (row);
13804 if (vpos >= 0 && vpos < matrix->nrows)
13805 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13806 vpos,
13807 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13808 return Qnil;
13812 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13813 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13814 GLYPH 0 means don't dump glyphs.
13815 GLYPH 1 means dump glyphs in short form.
13816 GLYPH > 1 or omitted means dump glyphs in long form. */)
13817 (row, glyphs)
13818 Lisp_Object row, glyphs;
13820 struct frame *sf = SELECTED_FRAME ();
13821 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13822 int vpos;
13824 CHECK_NUMBER (row);
13825 vpos = XINT (row);
13826 if (vpos >= 0 && vpos < m->nrows)
13827 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13828 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13829 return Qnil;
13833 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13834 doc: /* Toggle tracing of redisplay.
13835 With ARG, turn tracing on if and only if ARG is positive. */)
13836 (arg)
13837 Lisp_Object arg;
13839 if (NILP (arg))
13840 trace_redisplay_p = !trace_redisplay_p;
13841 else
13843 arg = Fprefix_numeric_value (arg);
13844 trace_redisplay_p = XINT (arg) > 0;
13847 return Qnil;
13851 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13852 doc: /* Like `format', but print result to stderr.
13853 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13854 (nargs, args)
13855 int nargs;
13856 Lisp_Object *args;
13858 Lisp_Object s = Fformat (nargs, args);
13859 fprintf (stderr, "%s", SDATA (s));
13860 return Qnil;
13863 #endif /* GLYPH_DEBUG */
13867 /***********************************************************************
13868 Building Desired Matrix Rows
13869 ***********************************************************************/
13871 /* Return a temporary glyph row holding the glyphs of an overlay
13872 arrow. Only used for non-window-redisplay windows. */
13874 static struct glyph_row *
13875 get_overlay_arrow_glyph_row (w)
13876 struct window *w;
13878 struct frame *f = XFRAME (WINDOW_FRAME (w));
13879 struct buffer *buffer = XBUFFER (w->buffer);
13880 struct buffer *old = current_buffer;
13881 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13882 int arrow_len = SCHARS (Voverlay_arrow_string);
13883 const unsigned char *arrow_end = arrow_string + arrow_len;
13884 const unsigned char *p;
13885 struct it it;
13886 int multibyte_p;
13887 int n_glyphs_before;
13889 set_buffer_temp (buffer);
13890 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13891 it.glyph_row->used[TEXT_AREA] = 0;
13892 SET_TEXT_POS (it.position, 0, 0);
13894 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13895 p = arrow_string;
13896 while (p < arrow_end)
13898 Lisp_Object face, ilisp;
13900 /* Get the next character. */
13901 if (multibyte_p)
13902 it.c = string_char_and_length (p, arrow_len, &it.len);
13903 else
13904 it.c = *p, it.len = 1;
13905 p += it.len;
13907 /* Get its face. */
13908 ilisp = make_number (p - arrow_string);
13909 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13910 it.face_id = compute_char_face (f, it.c, face);
13912 /* Compute its width, get its glyphs. */
13913 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13914 SET_TEXT_POS (it.position, -1, -1);
13915 PRODUCE_GLYPHS (&it);
13917 /* If this character doesn't fit any more in the line, we have
13918 to remove some glyphs. */
13919 if (it.current_x > it.last_visible_x)
13921 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13922 break;
13926 set_buffer_temp (old);
13927 return it.glyph_row;
13931 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13932 glyphs are only inserted for terminal frames since we can't really
13933 win with truncation glyphs when partially visible glyphs are
13934 involved. Which glyphs to insert is determined by
13935 produce_special_glyphs. */
13937 static void
13938 insert_left_trunc_glyphs (it)
13939 struct it *it;
13941 struct it truncate_it;
13942 struct glyph *from, *end, *to, *toend;
13944 xassert (!FRAME_WINDOW_P (it->f));
13946 /* Get the truncation glyphs. */
13947 truncate_it = *it;
13948 truncate_it.current_x = 0;
13949 truncate_it.face_id = DEFAULT_FACE_ID;
13950 truncate_it.glyph_row = &scratch_glyph_row;
13951 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13952 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13953 truncate_it.object = make_number (0);
13954 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13956 /* Overwrite glyphs from IT with truncation glyphs. */
13957 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13958 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13959 to = it->glyph_row->glyphs[TEXT_AREA];
13960 toend = to + it->glyph_row->used[TEXT_AREA];
13962 while (from < end)
13963 *to++ = *from++;
13965 /* There may be padding glyphs left over. Overwrite them too. */
13966 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13968 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13969 while (from < end)
13970 *to++ = *from++;
13973 if (to > toend)
13974 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13978 /* Compute the pixel height and width of IT->glyph_row.
13980 Most of the time, ascent and height of a display line will be equal
13981 to the max_ascent and max_height values of the display iterator
13982 structure. This is not the case if
13984 1. We hit ZV without displaying anything. In this case, max_ascent
13985 and max_height will be zero.
13987 2. We have some glyphs that don't contribute to the line height.
13988 (The glyph row flag contributes_to_line_height_p is for future
13989 pixmap extensions).
13991 The first case is easily covered by using default values because in
13992 these cases, the line height does not really matter, except that it
13993 must not be zero. */
13995 static void
13996 compute_line_metrics (it)
13997 struct it *it;
13999 struct glyph_row *row = it->glyph_row;
14000 int area, i;
14002 if (FRAME_WINDOW_P (it->f))
14004 int i, min_y, max_y;
14006 /* The line may consist of one space only, that was added to
14007 place the cursor on it. If so, the row's height hasn't been
14008 computed yet. */
14009 if (row->height == 0)
14011 if (it->max_ascent + it->max_descent == 0)
14012 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14013 row->ascent = it->max_ascent;
14014 row->height = it->max_ascent + it->max_descent;
14015 row->phys_ascent = it->max_phys_ascent;
14016 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14019 /* Compute the width of this line. */
14020 row->pixel_width = row->x;
14021 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14022 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14024 xassert (row->pixel_width >= 0);
14025 xassert (row->ascent >= 0 && row->height > 0);
14027 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14028 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14030 /* If first line's physical ascent is larger than its logical
14031 ascent, use the physical ascent, and make the row taller.
14032 This makes accented characters fully visible. */
14033 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14034 && row->phys_ascent > row->ascent)
14036 row->height += row->phys_ascent - row->ascent;
14037 row->ascent = row->phys_ascent;
14040 /* Compute how much of the line is visible. */
14041 row->visible_height = row->height;
14043 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14044 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14046 if (row->y < min_y)
14047 row->visible_height -= min_y - row->y;
14048 if (row->y + row->height > max_y)
14049 row->visible_height -= row->y + row->height - max_y;
14051 else
14053 row->pixel_width = row->used[TEXT_AREA];
14054 if (row->continued_p)
14055 row->pixel_width -= it->continuation_pixel_width;
14056 else if (row->truncated_on_right_p)
14057 row->pixel_width -= it->truncation_pixel_width;
14058 row->ascent = row->phys_ascent = 0;
14059 row->height = row->phys_height = row->visible_height = 1;
14062 /* Compute a hash code for this row. */
14063 row->hash = 0;
14064 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14065 for (i = 0; i < row->used[area]; ++i)
14066 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14067 + row->glyphs[area][i].u.val
14068 + row->glyphs[area][i].face_id
14069 + row->glyphs[area][i].padding_p
14070 + (row->glyphs[area][i].type << 2));
14072 it->max_ascent = it->max_descent = 0;
14073 it->max_phys_ascent = it->max_phys_descent = 0;
14077 /* Append one space to the glyph row of iterator IT if doing a
14078 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14079 space have the default face, otherwise let it have the same face as
14080 IT->face_id. Value is non-zero if a space was added.
14082 This function is called to make sure that there is always one glyph
14083 at the end of a glyph row that the cursor can be set on under
14084 window-systems. (If there weren't such a glyph we would not know
14085 how wide and tall a box cursor should be displayed).
14087 At the same time this space let's a nicely handle clearing to the
14088 end of the line if the row ends in italic text. */
14090 static int
14091 append_space (it, default_face_p)
14092 struct it *it;
14093 int default_face_p;
14095 if (FRAME_WINDOW_P (it->f))
14097 int n = it->glyph_row->used[TEXT_AREA];
14099 if (it->glyph_row->glyphs[TEXT_AREA] + n
14100 < it->glyph_row->glyphs[1 + TEXT_AREA])
14102 /* Save some values that must not be changed.
14103 Must save IT->c and IT->len because otherwise
14104 ITERATOR_AT_END_P wouldn't work anymore after
14105 append_space has been called. */
14106 enum display_element_type saved_what = it->what;
14107 int saved_c = it->c, saved_len = it->len;
14108 int saved_x = it->current_x;
14109 int saved_face_id = it->face_id;
14110 struct text_pos saved_pos;
14111 Lisp_Object saved_object;
14112 struct face *face;
14114 saved_object = it->object;
14115 saved_pos = it->position;
14117 it->what = IT_CHARACTER;
14118 bzero (&it->position, sizeof it->position);
14119 it->object = make_number (0);
14120 it->c = ' ';
14121 it->len = 1;
14123 if (default_face_p)
14124 it->face_id = DEFAULT_FACE_ID;
14125 else if (it->face_before_selective_p)
14126 it->face_id = it->saved_face_id;
14127 face = FACE_FROM_ID (it->f, it->face_id);
14128 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14130 PRODUCE_GLYPHS (it);
14132 it->current_x = saved_x;
14133 it->object = saved_object;
14134 it->position = saved_pos;
14135 it->what = saved_what;
14136 it->face_id = saved_face_id;
14137 it->len = saved_len;
14138 it->c = saved_c;
14139 return 1;
14143 return 0;
14147 /* Extend the face of the last glyph in the text area of IT->glyph_row
14148 to the end of the display line. Called from display_line.
14149 If the glyph row is empty, add a space glyph to it so that we
14150 know the face to draw. Set the glyph row flag fill_line_p. */
14152 static void
14153 extend_face_to_end_of_line (it)
14154 struct it *it;
14156 struct face *face;
14157 struct frame *f = it->f;
14159 /* If line is already filled, do nothing. */
14160 if (it->current_x >= it->last_visible_x)
14161 return;
14163 /* Face extension extends the background and box of IT->face_id
14164 to the end of the line. If the background equals the background
14165 of the frame, we don't have to do anything. */
14166 if (it->face_before_selective_p)
14167 face = FACE_FROM_ID (it->f, it->saved_face_id);
14168 else
14169 face = FACE_FROM_ID (f, it->face_id);
14171 if (FRAME_WINDOW_P (f)
14172 && face->box == FACE_NO_BOX
14173 && face->background == FRAME_BACKGROUND_PIXEL (f)
14174 && !face->stipple)
14175 return;
14177 /* Set the glyph row flag indicating that the face of the last glyph
14178 in the text area has to be drawn to the end of the text area. */
14179 it->glyph_row->fill_line_p = 1;
14181 /* If current character of IT is not ASCII, make sure we have the
14182 ASCII face. This will be automatically undone the next time
14183 get_next_display_element returns a multibyte character. Note
14184 that the character will always be single byte in unibyte text. */
14185 if (!SINGLE_BYTE_CHAR_P (it->c))
14187 it->face_id = FACE_FOR_CHAR (f, face, 0);
14190 if (FRAME_WINDOW_P (f))
14192 /* If the row is empty, add a space with the current face of IT,
14193 so that we know which face to draw. */
14194 if (it->glyph_row->used[TEXT_AREA] == 0)
14196 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14197 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14198 it->glyph_row->used[TEXT_AREA] = 1;
14201 else
14203 /* Save some values that must not be changed. */
14204 int saved_x = it->current_x;
14205 struct text_pos saved_pos;
14206 Lisp_Object saved_object;
14207 enum display_element_type saved_what = it->what;
14208 int saved_face_id = it->face_id;
14210 saved_object = it->object;
14211 saved_pos = it->position;
14213 it->what = IT_CHARACTER;
14214 bzero (&it->position, sizeof it->position);
14215 it->object = make_number (0);
14216 it->c = ' ';
14217 it->len = 1;
14218 it->face_id = face->id;
14220 PRODUCE_GLYPHS (it);
14222 while (it->current_x <= it->last_visible_x)
14223 PRODUCE_GLYPHS (it);
14225 /* Don't count these blanks really. It would let us insert a left
14226 truncation glyph below and make us set the cursor on them, maybe. */
14227 it->current_x = saved_x;
14228 it->object = saved_object;
14229 it->position = saved_pos;
14230 it->what = saved_what;
14231 it->face_id = saved_face_id;
14236 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14237 trailing whitespace. */
14239 static int
14240 trailing_whitespace_p (charpos)
14241 int charpos;
14243 int bytepos = CHAR_TO_BYTE (charpos);
14244 int c = 0;
14246 while (bytepos < ZV_BYTE
14247 && (c = FETCH_CHAR (bytepos),
14248 c == ' ' || c == '\t'))
14249 ++bytepos;
14251 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14253 if (bytepos != PT_BYTE)
14254 return 1;
14256 return 0;
14260 /* Highlight trailing whitespace, if any, in ROW. */
14262 void
14263 highlight_trailing_whitespace (f, row)
14264 struct frame *f;
14265 struct glyph_row *row;
14267 int used = row->used[TEXT_AREA];
14269 if (used)
14271 struct glyph *start = row->glyphs[TEXT_AREA];
14272 struct glyph *glyph = start + used - 1;
14274 /* Skip over glyphs inserted to display the cursor at the
14275 end of a line, for extending the face of the last glyph
14276 to the end of the line on terminals, and for truncation
14277 and continuation glyphs. */
14278 while (glyph >= start
14279 && glyph->type == CHAR_GLYPH
14280 && INTEGERP (glyph->object))
14281 --glyph;
14283 /* If last glyph is a space or stretch, and it's trailing
14284 whitespace, set the face of all trailing whitespace glyphs in
14285 IT->glyph_row to `trailing-whitespace'. */
14286 if (glyph >= start
14287 && BUFFERP (glyph->object)
14288 && (glyph->type == STRETCH_GLYPH
14289 || (glyph->type == CHAR_GLYPH
14290 && glyph->u.ch == ' '))
14291 && trailing_whitespace_p (glyph->charpos))
14293 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14295 while (glyph >= start
14296 && BUFFERP (glyph->object)
14297 && (glyph->type == STRETCH_GLYPH
14298 || (glyph->type == CHAR_GLYPH
14299 && glyph->u.ch == ' ')))
14300 (glyph--)->face_id = face_id;
14306 /* Value is non-zero if glyph row ROW in window W should be
14307 used to hold the cursor. */
14309 static int
14310 cursor_row_p (w, row)
14311 struct window *w;
14312 struct glyph_row *row;
14314 int cursor_row_p = 1;
14316 if (PT == MATRIX_ROW_END_CHARPOS (row))
14318 /* If the row ends with a newline from a string, we don't want
14319 the cursor there (if the row is continued it doesn't end in a
14320 newline). */
14321 if (CHARPOS (row->end.string_pos) >= 0
14322 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14323 cursor_row_p = row->continued_p;
14325 /* If the row ends at ZV, display the cursor at the end of that
14326 row instead of at the start of the row below. */
14327 else if (row->ends_at_zv_p)
14328 cursor_row_p = 1;
14329 else
14330 cursor_row_p = 0;
14333 return cursor_row_p;
14337 /* Construct the glyph row IT->glyph_row in the desired matrix of
14338 IT->w from text at the current position of IT. See dispextern.h
14339 for an overview of struct it. Value is non-zero if
14340 IT->glyph_row displays text, as opposed to a line displaying ZV
14341 only. */
14343 static int
14344 display_line (it)
14345 struct it *it;
14347 struct glyph_row *row = it->glyph_row;
14349 /* We always start displaying at hpos zero even if hscrolled. */
14350 xassert (it->hpos == 0 && it->current_x == 0);
14352 /* We must not display in a row that's not a text row. */
14353 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14354 < it->w->desired_matrix->nrows);
14356 /* Is IT->w showing the region? */
14357 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14359 /* Clear the result glyph row and enable it. */
14360 prepare_desired_row (row);
14362 row->y = it->current_y;
14363 row->start = it->start;
14364 row->continuation_lines_width = it->continuation_lines_width;
14365 row->displays_text_p = 1;
14366 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14367 it->starts_in_middle_of_char_p = 0;
14369 /* Arrange the overlays nicely for our purposes. Usually, we call
14370 display_line on only one line at a time, in which case this
14371 can't really hurt too much, or we call it on lines which appear
14372 one after another in the buffer, in which case all calls to
14373 recenter_overlay_lists but the first will be pretty cheap. */
14374 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14376 /* Move over display elements that are not visible because we are
14377 hscrolled. This may stop at an x-position < IT->first_visible_x
14378 if the first glyph is partially visible or if we hit a line end. */
14379 if (it->current_x < it->first_visible_x)
14380 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14381 MOVE_TO_POS | MOVE_TO_X);
14383 /* Get the initial row height. This is either the height of the
14384 text hscrolled, if there is any, or zero. */
14385 row->ascent = it->max_ascent;
14386 row->height = it->max_ascent + it->max_descent;
14387 row->phys_ascent = it->max_phys_ascent;
14388 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14390 /* Loop generating characters. The loop is left with IT on the next
14391 character to display. */
14392 while (1)
14394 int n_glyphs_before, hpos_before, x_before;
14395 int x, i, nglyphs;
14396 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14398 /* Retrieve the next thing to display. Value is zero if end of
14399 buffer reached. */
14400 if (!get_next_display_element (it))
14402 /* Maybe add a space at the end of this line that is used to
14403 display the cursor there under X. Set the charpos of the
14404 first glyph of blank lines not corresponding to any text
14405 to -1. */
14406 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14407 || row->used[TEXT_AREA] == 0)
14409 row->glyphs[TEXT_AREA]->charpos = -1;
14410 row->displays_text_p = 0;
14412 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14413 && (!MINI_WINDOW_P (it->w)
14414 || (minibuf_level && EQ (it->window, minibuf_window))))
14415 row->indicate_empty_line_p = 1;
14418 it->continuation_lines_width = 0;
14419 row->ends_at_zv_p = 1;
14420 break;
14423 /* Now, get the metrics of what we want to display. This also
14424 generates glyphs in `row' (which is IT->glyph_row). */
14425 n_glyphs_before = row->used[TEXT_AREA];
14426 x = it->current_x;
14428 /* Remember the line height so far in case the next element doesn't
14429 fit on the line. */
14430 if (!it->truncate_lines_p)
14432 ascent = it->max_ascent;
14433 descent = it->max_descent;
14434 phys_ascent = it->max_phys_ascent;
14435 phys_descent = it->max_phys_descent;
14438 PRODUCE_GLYPHS (it);
14440 /* If this display element was in marginal areas, continue with
14441 the next one. */
14442 if (it->area != TEXT_AREA)
14444 row->ascent = max (row->ascent, it->max_ascent);
14445 row->height = max (row->height, it->max_ascent + it->max_descent);
14446 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14447 row->phys_height = max (row->phys_height,
14448 it->max_phys_ascent + it->max_phys_descent);
14449 set_iterator_to_next (it, 1);
14450 continue;
14453 /* Does the display element fit on the line? If we truncate
14454 lines, we should draw past the right edge of the window. If
14455 we don't truncate, we want to stop so that we can display the
14456 continuation glyph before the right margin. If lines are
14457 continued, there are two possible strategies for characters
14458 resulting in more than 1 glyph (e.g. tabs): Display as many
14459 glyphs as possible in this line and leave the rest for the
14460 continuation line, or display the whole element in the next
14461 line. Original redisplay did the former, so we do it also. */
14462 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14463 hpos_before = it->hpos;
14464 x_before = x;
14466 if (/* Not a newline. */
14467 nglyphs > 0
14468 /* Glyphs produced fit entirely in the line. */
14469 && it->current_x < it->last_visible_x)
14471 it->hpos += nglyphs;
14472 row->ascent = max (row->ascent, it->max_ascent);
14473 row->height = max (row->height, it->max_ascent + it->max_descent);
14474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14475 row->phys_height = max (row->phys_height,
14476 it->max_phys_ascent + it->max_phys_descent);
14477 if (it->current_x - it->pixel_width < it->first_visible_x)
14478 row->x = x - it->first_visible_x;
14480 else
14482 int new_x;
14483 struct glyph *glyph;
14485 for (i = 0; i < nglyphs; ++i, x = new_x)
14487 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14488 new_x = x + glyph->pixel_width;
14490 if (/* Lines are continued. */
14491 !it->truncate_lines_p
14492 && (/* Glyph doesn't fit on the line. */
14493 new_x > it->last_visible_x
14494 /* Or it fits exactly on a window system frame. */
14495 || (new_x == it->last_visible_x
14496 && FRAME_WINDOW_P (it->f))))
14498 /* End of a continued line. */
14500 if (it->hpos == 0
14501 || (new_x == it->last_visible_x
14502 && FRAME_WINDOW_P (it->f)))
14504 /* Current glyph is the only one on the line or
14505 fits exactly on the line. We must continue
14506 the line because we can't draw the cursor
14507 after the glyph. */
14508 row->continued_p = 1;
14509 it->current_x = new_x;
14510 it->continuation_lines_width += new_x;
14511 ++it->hpos;
14512 if (i == nglyphs - 1)
14513 set_iterator_to_next (it, 1);
14515 else if (CHAR_GLYPH_PADDING_P (*glyph)
14516 && !FRAME_WINDOW_P (it->f))
14518 /* A padding glyph that doesn't fit on this line.
14519 This means the whole character doesn't fit
14520 on the line. */
14521 row->used[TEXT_AREA] = n_glyphs_before;
14523 /* Fill the rest of the row with continuation
14524 glyphs like in 20.x. */
14525 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14526 < row->glyphs[1 + TEXT_AREA])
14527 produce_special_glyphs (it, IT_CONTINUATION);
14529 row->continued_p = 1;
14530 it->current_x = x_before;
14531 it->continuation_lines_width += x_before;
14533 /* Restore the height to what it was before the
14534 element not fitting on the line. */
14535 it->max_ascent = ascent;
14536 it->max_descent = descent;
14537 it->max_phys_ascent = phys_ascent;
14538 it->max_phys_descent = phys_descent;
14540 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14542 /* A TAB that extends past the right edge of the
14543 window. This produces a single glyph on
14544 window system frames. We leave the glyph in
14545 this row and let it fill the row, but don't
14546 consume the TAB. */
14547 it->continuation_lines_width += it->last_visible_x;
14548 row->ends_in_middle_of_char_p = 1;
14549 row->continued_p = 1;
14550 glyph->pixel_width = it->last_visible_x - x;
14551 it->starts_in_middle_of_char_p = 1;
14553 else
14555 /* Something other than a TAB that draws past
14556 the right edge of the window. Restore
14557 positions to values before the element. */
14558 row->used[TEXT_AREA] = n_glyphs_before + i;
14560 /* Display continuation glyphs. */
14561 if (!FRAME_WINDOW_P (it->f))
14562 produce_special_glyphs (it, IT_CONTINUATION);
14563 row->continued_p = 1;
14565 it->continuation_lines_width += x;
14567 if (nglyphs > 1 && i > 0)
14569 row->ends_in_middle_of_char_p = 1;
14570 it->starts_in_middle_of_char_p = 1;
14573 /* Restore the height to what it was before the
14574 element not fitting on the line. */
14575 it->max_ascent = ascent;
14576 it->max_descent = descent;
14577 it->max_phys_ascent = phys_ascent;
14578 it->max_phys_descent = phys_descent;
14581 break;
14583 else if (new_x > it->first_visible_x)
14585 /* Increment number of glyphs actually displayed. */
14586 ++it->hpos;
14588 if (x < it->first_visible_x)
14589 /* Glyph is partially visible, i.e. row starts at
14590 negative X position. */
14591 row->x = x - it->first_visible_x;
14593 else
14595 /* Glyph is completely off the left margin of the
14596 window. This should not happen because of the
14597 move_it_in_display_line at the start of this
14598 function, unless the text display area of the
14599 window is empty. */
14600 xassert (it->first_visible_x <= it->last_visible_x);
14604 row->ascent = max (row->ascent, it->max_ascent);
14605 row->height = max (row->height, it->max_ascent + it->max_descent);
14606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14607 row->phys_height = max (row->phys_height,
14608 it->max_phys_ascent + it->max_phys_descent);
14610 /* End of this display line if row is continued. */
14611 if (row->continued_p)
14612 break;
14615 /* Is this a line end? If yes, we're also done, after making
14616 sure that a non-default face is extended up to the right
14617 margin of the window. */
14618 if (ITERATOR_AT_END_OF_LINE_P (it))
14620 int used_before = row->used[TEXT_AREA];
14622 row->ends_in_newline_from_string_p = STRINGP (it->object);
14624 /* Add a space at the end of the line that is used to
14625 display the cursor there. */
14626 append_space (it, 0);
14628 /* Extend the face to the end of the line. */
14629 extend_face_to_end_of_line (it);
14631 /* Make sure we have the position. */
14632 if (used_before == 0)
14633 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14635 /* Consume the line end. This skips over invisible lines. */
14636 set_iterator_to_next (it, 1);
14637 it->continuation_lines_width = 0;
14638 break;
14641 /* Proceed with next display element. Note that this skips
14642 over lines invisible because of selective display. */
14643 set_iterator_to_next (it, 1);
14645 /* If we truncate lines, we are done when the last displayed
14646 glyphs reach past the right margin of the window. */
14647 if (it->truncate_lines_p
14648 && (FRAME_WINDOW_P (it->f)
14649 ? (it->current_x >= it->last_visible_x)
14650 : (it->current_x > it->last_visible_x)))
14652 /* Maybe add truncation glyphs. */
14653 if (!FRAME_WINDOW_P (it->f))
14655 int i, n;
14657 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14658 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14659 break;
14661 for (n = row->used[TEXT_AREA]; i < n; ++i)
14663 row->used[TEXT_AREA] = i;
14664 produce_special_glyphs (it, IT_TRUNCATION);
14668 row->truncated_on_right_p = 1;
14669 it->continuation_lines_width = 0;
14670 reseat_at_next_visible_line_start (it, 0);
14671 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14672 it->hpos = hpos_before;
14673 it->current_x = x_before;
14674 break;
14678 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14679 at the left window margin. */
14680 if (it->first_visible_x
14681 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14683 if (!FRAME_WINDOW_P (it->f))
14684 insert_left_trunc_glyphs (it);
14685 row->truncated_on_left_p = 1;
14688 /* If the start of this line is the overlay arrow-position, then
14689 mark this glyph row as the one containing the overlay arrow.
14690 This is clearly a mess with variable size fonts. It would be
14691 better to let it be displayed like cursors under X. */
14692 if (MARKERP (Voverlay_arrow_position)
14693 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14694 && (MATRIX_ROW_START_CHARPOS (row)
14695 == marker_position (Voverlay_arrow_position))
14696 && STRINGP (Voverlay_arrow_string)
14697 && ! overlay_arrow_seen)
14699 /* Overlay arrow in window redisplay is a fringe bitmap. */
14700 if (!FRAME_WINDOW_P (it->f))
14702 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14703 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14704 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14705 struct glyph *p = row->glyphs[TEXT_AREA];
14706 struct glyph *p2, *end;
14708 /* Copy the arrow glyphs. */
14709 while (glyph < arrow_end)
14710 *p++ = *glyph++;
14712 /* Throw away padding glyphs. */
14713 p2 = p;
14714 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14715 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14716 ++p2;
14717 if (p2 > p)
14719 while (p2 < end)
14720 *p++ = *p2++;
14721 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14725 overlay_arrow_seen = 1;
14726 row->overlay_arrow_p = 1;
14729 /* Compute pixel dimensions of this line. */
14730 compute_line_metrics (it);
14732 /* Remember the position at which this line ends. */
14733 row->end = it->current;
14735 /* Maybe set the cursor. */
14736 if (it->w->cursor.vpos < 0
14737 && PT >= MATRIX_ROW_START_CHARPOS (row)
14738 && PT <= MATRIX_ROW_END_CHARPOS (row)
14739 && cursor_row_p (it->w, row))
14740 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14742 /* Highlight trailing whitespace. */
14743 if (!NILP (Vshow_trailing_whitespace))
14744 highlight_trailing_whitespace (it->f, it->glyph_row);
14746 /* Prepare for the next line. This line starts horizontally at (X
14747 HPOS) = (0 0). Vertical positions are incremented. As a
14748 convenience for the caller, IT->glyph_row is set to the next
14749 row to be used. */
14750 it->current_x = it->hpos = 0;
14751 it->current_y += row->height;
14752 ++it->vpos;
14753 ++it->glyph_row;
14754 it->start = it->current;
14755 return row->displays_text_p;
14760 /***********************************************************************
14761 Menu Bar
14762 ***********************************************************************/
14764 /* Redisplay the menu bar in the frame for window W.
14766 The menu bar of X frames that don't have X toolkit support is
14767 displayed in a special window W->frame->menu_bar_window.
14769 The menu bar of terminal frames is treated specially as far as
14770 glyph matrices are concerned. Menu bar lines are not part of
14771 windows, so the update is done directly on the frame matrix rows
14772 for the menu bar. */
14774 static void
14775 display_menu_bar (w)
14776 struct window *w;
14778 struct frame *f = XFRAME (WINDOW_FRAME (w));
14779 struct it it;
14780 Lisp_Object items;
14781 int i;
14783 /* Don't do all this for graphical frames. */
14784 #ifdef HAVE_NTGUI
14785 if (!NILP (Vwindow_system))
14786 return;
14787 #endif
14788 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14789 if (FRAME_X_P (f))
14790 return;
14791 #endif
14792 #ifdef MAC_OS
14793 if (FRAME_MAC_P (f))
14794 return;
14795 #endif
14797 #ifdef USE_X_TOOLKIT
14798 xassert (!FRAME_WINDOW_P (f));
14799 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14800 it.first_visible_x = 0;
14801 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14802 #else /* not USE_X_TOOLKIT */
14803 if (FRAME_WINDOW_P (f))
14805 /* Menu bar lines are displayed in the desired matrix of the
14806 dummy window menu_bar_window. */
14807 struct window *menu_w;
14808 xassert (WINDOWP (f->menu_bar_window));
14809 menu_w = XWINDOW (f->menu_bar_window);
14810 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14811 MENU_FACE_ID);
14812 it.first_visible_x = 0;
14813 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14815 else
14817 /* This is a TTY frame, i.e. character hpos/vpos are used as
14818 pixel x/y. */
14819 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14820 MENU_FACE_ID);
14821 it.first_visible_x = 0;
14822 it.last_visible_x = FRAME_COLS (f);
14824 #endif /* not USE_X_TOOLKIT */
14826 if (! mode_line_inverse_video)
14827 /* Force the menu-bar to be displayed in the default face. */
14828 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14830 /* Clear all rows of the menu bar. */
14831 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14833 struct glyph_row *row = it.glyph_row + i;
14834 clear_glyph_row (row);
14835 row->enabled_p = 1;
14836 row->full_width_p = 1;
14839 /* Display all items of the menu bar. */
14840 items = FRAME_MENU_BAR_ITEMS (it.f);
14841 for (i = 0; i < XVECTOR (items)->size; i += 4)
14843 Lisp_Object string;
14845 /* Stop at nil string. */
14846 string = AREF (items, i + 1);
14847 if (NILP (string))
14848 break;
14850 /* Remember where item was displayed. */
14851 AREF (items, i + 3) = make_number (it.hpos);
14853 /* Display the item, pad with one space. */
14854 if (it.current_x < it.last_visible_x)
14855 display_string (NULL, string, Qnil, 0, 0, &it,
14856 SCHARS (string) + 1, 0, 0, -1);
14859 /* Fill out the line with spaces. */
14860 if (it.current_x < it.last_visible_x)
14861 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14863 /* Compute the total height of the lines. */
14864 compute_line_metrics (&it);
14869 /***********************************************************************
14870 Mode Line
14871 ***********************************************************************/
14873 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14874 FORCE is non-zero, redisplay mode lines unconditionally.
14875 Otherwise, redisplay only mode lines that are garbaged. Value is
14876 the number of windows whose mode lines were redisplayed. */
14878 static int
14879 redisplay_mode_lines (window, force)
14880 Lisp_Object window;
14881 int force;
14883 int nwindows = 0;
14885 while (!NILP (window))
14887 struct window *w = XWINDOW (window);
14889 if (WINDOWP (w->hchild))
14890 nwindows += redisplay_mode_lines (w->hchild, force);
14891 else if (WINDOWP (w->vchild))
14892 nwindows += redisplay_mode_lines (w->vchild, force);
14893 else if (force
14894 || FRAME_GARBAGED_P (XFRAME (w->frame))
14895 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14897 struct text_pos lpoint;
14898 struct buffer *old = current_buffer;
14900 /* Set the window's buffer for the mode line display. */
14901 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14902 set_buffer_internal_1 (XBUFFER (w->buffer));
14904 /* Point refers normally to the selected window. For any
14905 other window, set up appropriate value. */
14906 if (!EQ (window, selected_window))
14908 struct text_pos pt;
14910 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14911 if (CHARPOS (pt) < BEGV)
14912 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14913 else if (CHARPOS (pt) > (ZV - 1))
14914 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14915 else
14916 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14919 /* Display mode lines. */
14920 clear_glyph_matrix (w->desired_matrix);
14921 if (display_mode_lines (w))
14923 ++nwindows;
14924 w->must_be_updated_p = 1;
14927 /* Restore old settings. */
14928 set_buffer_internal_1 (old);
14929 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14932 window = w->next;
14935 return nwindows;
14939 /* Display the mode and/or top line of window W. Value is the number
14940 of mode lines displayed. */
14942 static int
14943 display_mode_lines (w)
14944 struct window *w;
14946 Lisp_Object old_selected_window, old_selected_frame;
14947 int n = 0;
14949 old_selected_frame = selected_frame;
14950 selected_frame = w->frame;
14951 old_selected_window = selected_window;
14952 XSETWINDOW (selected_window, w);
14954 /* These will be set while the mode line specs are processed. */
14955 line_number_displayed = 0;
14956 w->column_number_displayed = Qnil;
14958 if (WINDOW_WANTS_MODELINE_P (w))
14960 struct window *sel_w = XWINDOW (old_selected_window);
14962 /* Select mode line face based on the real selected window. */
14963 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14964 current_buffer->mode_line_format);
14965 ++n;
14968 if (WINDOW_WANTS_HEADER_LINE_P (w))
14970 display_mode_line (w, HEADER_LINE_FACE_ID,
14971 current_buffer->header_line_format);
14972 ++n;
14975 selected_frame = old_selected_frame;
14976 selected_window = old_selected_window;
14977 return n;
14981 /* Display mode or top line of window W. FACE_ID specifies which line
14982 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14983 FORMAT is the mode line format to display. Value is the pixel
14984 height of the mode line displayed. */
14986 static int
14987 display_mode_line (w, face_id, format)
14988 struct window *w;
14989 enum face_id face_id;
14990 Lisp_Object format;
14992 struct it it;
14993 struct face *face;
14995 init_iterator (&it, w, -1, -1, NULL, face_id);
14996 prepare_desired_row (it.glyph_row);
14998 if (! mode_line_inverse_video)
14999 /* Force the mode-line to be displayed in the default face. */
15000 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15002 /* Temporarily make frame's keyboard the current kboard so that
15003 kboard-local variables in the mode_line_format will get the right
15004 values. */
15005 push_frame_kboard (it.f);
15006 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15007 pop_frame_kboard ();
15009 /* Fill up with spaces. */
15010 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15012 compute_line_metrics (&it);
15013 it.glyph_row->full_width_p = 1;
15014 it.glyph_row->mode_line_p = 1;
15015 it.glyph_row->continued_p = 0;
15016 it.glyph_row->truncated_on_left_p = 0;
15017 it.glyph_row->truncated_on_right_p = 0;
15019 /* Make a 3D mode-line have a shadow at its right end. */
15020 face = FACE_FROM_ID (it.f, face_id);
15021 extend_face_to_end_of_line (&it);
15022 if (face->box != FACE_NO_BOX)
15024 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15025 + it.glyph_row->used[TEXT_AREA] - 1);
15026 last->right_box_line_p = 1;
15029 return it.glyph_row->height;
15032 /* Alist that caches the results of :propertize.
15033 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15034 Lisp_Object mode_line_proptrans_alist;
15036 /* List of strings making up the mode-line. */
15037 Lisp_Object mode_line_string_list;
15039 /* Base face property when building propertized mode line string. */
15040 static Lisp_Object mode_line_string_face;
15041 static Lisp_Object mode_line_string_face_prop;
15044 /* Contribute ELT to the mode line for window IT->w. How it
15045 translates into text depends on its data type.
15047 IT describes the display environment in which we display, as usual.
15049 DEPTH is the depth in recursion. It is used to prevent
15050 infinite recursion here.
15052 FIELD_WIDTH is the number of characters the display of ELT should
15053 occupy in the mode line, and PRECISION is the maximum number of
15054 characters to display from ELT's representation. See
15055 display_string for details.
15057 Returns the hpos of the end of the text generated by ELT.
15059 PROPS is a property list to add to any string we encounter.
15061 If RISKY is nonzero, remove (disregard) any properties in any string
15062 we encounter, and ignore :eval and :propertize.
15064 If the global variable `frame_title_ptr' is non-NULL, then the output
15065 is passed to `store_frame_title' instead of `display_string'. */
15067 static int
15068 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15069 struct it *it;
15070 int depth;
15071 int field_width, precision;
15072 Lisp_Object elt, props;
15073 int risky;
15075 int n = 0, field, prec;
15076 int literal = 0;
15078 tail_recurse:
15079 if (depth > 100)
15080 elt = build_string ("*too-deep*");
15082 depth++;
15084 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15086 case Lisp_String:
15088 /* A string: output it and check for %-constructs within it. */
15089 unsigned char c;
15090 const unsigned char *this, *lisp_string;
15092 if (!NILP (props) || risky)
15094 Lisp_Object oprops, aelt;
15095 oprops = Ftext_properties_at (make_number (0), elt);
15097 if (NILP (Fequal (props, oprops)) || risky)
15099 /* If the starting string has properties,
15100 merge the specified ones onto the existing ones. */
15101 if (! NILP (oprops) && !risky)
15103 Lisp_Object tem;
15105 oprops = Fcopy_sequence (oprops);
15106 tem = props;
15107 while (CONSP (tem))
15109 oprops = Fplist_put (oprops, XCAR (tem),
15110 XCAR (XCDR (tem)));
15111 tem = XCDR (XCDR (tem));
15113 props = oprops;
15116 aelt = Fassoc (elt, mode_line_proptrans_alist);
15117 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15119 mode_line_proptrans_alist
15120 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15121 elt = XCAR (aelt);
15123 else
15125 Lisp_Object tem;
15127 elt = Fcopy_sequence (elt);
15128 Fset_text_properties (make_number (0), Flength (elt),
15129 props, elt);
15130 /* Add this item to mode_line_proptrans_alist. */
15131 mode_line_proptrans_alist
15132 = Fcons (Fcons (elt, props),
15133 mode_line_proptrans_alist);
15134 /* Truncate mode_line_proptrans_alist
15135 to at most 50 elements. */
15136 tem = Fnthcdr (make_number (50),
15137 mode_line_proptrans_alist);
15138 if (! NILP (tem))
15139 XSETCDR (tem, Qnil);
15144 this = SDATA (elt);
15145 lisp_string = this;
15147 if (literal)
15149 prec = precision - n;
15150 if (frame_title_ptr)
15151 n += store_frame_title (SDATA (elt), -1, prec);
15152 else if (!NILP (mode_line_string_list))
15153 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15154 else
15155 n += display_string (NULL, elt, Qnil, 0, 0, it,
15156 0, prec, 0, STRING_MULTIBYTE (elt));
15158 break;
15161 while ((precision <= 0 || n < precision)
15162 && *this
15163 && (frame_title_ptr
15164 || !NILP (mode_line_string_list)
15165 || it->current_x < it->last_visible_x))
15167 const unsigned char *last = this;
15169 /* Advance to end of string or next format specifier. */
15170 while ((c = *this++) != '\0' && c != '%')
15173 if (this - 1 != last)
15175 /* Output to end of string or up to '%'. Field width
15176 is length of string. Don't output more than
15177 PRECISION allows us. */
15178 --this;
15180 prec = chars_in_text (last, this - last);
15181 if (precision > 0 && prec > precision - n)
15182 prec = precision - n;
15184 if (frame_title_ptr)
15185 n += store_frame_title (last, 0, prec);
15186 else if (!NILP (mode_line_string_list))
15188 int bytepos = last - lisp_string;
15189 int charpos = string_byte_to_char (elt, bytepos);
15190 n += store_mode_line_string (NULL,
15191 Fsubstring (elt, make_number (charpos),
15192 make_number (charpos + prec)),
15193 0, 0, 0, Qnil);
15195 else
15197 int bytepos = last - lisp_string;
15198 int charpos = string_byte_to_char (elt, bytepos);
15199 n += display_string (NULL, elt, Qnil, 0, charpos,
15200 it, 0, prec, 0,
15201 STRING_MULTIBYTE (elt));
15204 else /* c == '%' */
15206 const unsigned char *percent_position = this;
15208 /* Get the specified minimum width. Zero means
15209 don't pad. */
15210 field = 0;
15211 while ((c = *this++) >= '0' && c <= '9')
15212 field = field * 10 + c - '0';
15214 /* Don't pad beyond the total padding allowed. */
15215 if (field_width - n > 0 && field > field_width - n)
15216 field = field_width - n;
15218 /* Note that either PRECISION <= 0 or N < PRECISION. */
15219 prec = precision - n;
15221 if (c == 'M')
15222 n += display_mode_element (it, depth, field, prec,
15223 Vglobal_mode_string, props,
15224 risky);
15225 else if (c != 0)
15227 int multibyte;
15228 int bytepos, charpos;
15229 unsigned char *spec;
15231 bytepos = percent_position - lisp_string;
15232 charpos = (STRING_MULTIBYTE (elt)
15233 ? string_byte_to_char (elt, bytepos)
15234 : bytepos);
15236 spec
15237 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15239 if (frame_title_ptr)
15240 n += store_frame_title (spec, field, prec);
15241 else if (!NILP (mode_line_string_list))
15243 int len = strlen (spec);
15244 Lisp_Object tem = make_string (spec, len);
15245 props = Ftext_properties_at (make_number (charpos), elt);
15246 /* Should only keep face property in props */
15247 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15249 else
15251 int nglyphs_before, nwritten;
15253 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15254 nwritten = display_string (spec, Qnil, elt,
15255 charpos, 0, it,
15256 field, prec, 0,
15257 multibyte);
15259 /* Assign to the glyphs written above the
15260 string where the `%x' came from, position
15261 of the `%'. */
15262 if (nwritten > 0)
15264 struct glyph *glyph
15265 = (it->glyph_row->glyphs[TEXT_AREA]
15266 + nglyphs_before);
15267 int i;
15269 for (i = 0; i < nwritten; ++i)
15271 glyph[i].object = elt;
15272 glyph[i].charpos = charpos;
15275 n += nwritten;
15279 else /* c == 0 */
15280 break;
15284 break;
15286 case Lisp_Symbol:
15287 /* A symbol: process the value of the symbol recursively
15288 as if it appeared here directly. Avoid error if symbol void.
15289 Special case: if value of symbol is a string, output the string
15290 literally. */
15292 register Lisp_Object tem;
15294 /* If the variable is not marked as risky to set
15295 then its contents are risky to use. */
15296 if (NILP (Fget (elt, Qrisky_local_variable)))
15297 risky = 1;
15299 tem = Fboundp (elt);
15300 if (!NILP (tem))
15302 tem = Fsymbol_value (elt);
15303 /* If value is a string, output that string literally:
15304 don't check for % within it. */
15305 if (STRINGP (tem))
15306 literal = 1;
15308 if (!EQ (tem, elt))
15310 /* Give up right away for nil or t. */
15311 elt = tem;
15312 goto tail_recurse;
15316 break;
15318 case Lisp_Cons:
15320 register Lisp_Object car, tem;
15322 /* A cons cell: five distinct cases.
15323 If first element is :eval or :propertize, do something special.
15324 If first element is a string or a cons, process all the elements
15325 and effectively concatenate them.
15326 If first element is a negative number, truncate displaying cdr to
15327 at most that many characters. If positive, pad (with spaces)
15328 to at least that many characters.
15329 If first element is a symbol, process the cadr or caddr recursively
15330 according to whether the symbol's value is non-nil or nil. */
15331 car = XCAR (elt);
15332 if (EQ (car, QCeval))
15334 /* An element of the form (:eval FORM) means evaluate FORM
15335 and use the result as mode line elements. */
15337 if (risky)
15338 break;
15340 if (CONSP (XCDR (elt)))
15342 Lisp_Object spec;
15343 spec = safe_eval (XCAR (XCDR (elt)));
15344 n += display_mode_element (it, depth, field_width - n,
15345 precision - n, spec, props,
15346 risky);
15349 else if (EQ (car, QCpropertize))
15351 /* An element of the form (:propertize ELT PROPS...)
15352 means display ELT but applying properties PROPS. */
15354 if (risky)
15355 break;
15357 if (CONSP (XCDR (elt)))
15358 n += display_mode_element (it, depth, field_width - n,
15359 precision - n, XCAR (XCDR (elt)),
15360 XCDR (XCDR (elt)), risky);
15362 else if (SYMBOLP (car))
15364 tem = Fboundp (car);
15365 elt = XCDR (elt);
15366 if (!CONSP (elt))
15367 goto invalid;
15368 /* elt is now the cdr, and we know it is a cons cell.
15369 Use its car if CAR has a non-nil value. */
15370 if (!NILP (tem))
15372 tem = Fsymbol_value (car);
15373 if (!NILP (tem))
15375 elt = XCAR (elt);
15376 goto tail_recurse;
15379 /* Symbol's value is nil (or symbol is unbound)
15380 Get the cddr of the original list
15381 and if possible find the caddr and use that. */
15382 elt = XCDR (elt);
15383 if (NILP (elt))
15384 break;
15385 else if (!CONSP (elt))
15386 goto invalid;
15387 elt = XCAR (elt);
15388 goto tail_recurse;
15390 else if (INTEGERP (car))
15392 register int lim = XINT (car);
15393 elt = XCDR (elt);
15394 if (lim < 0)
15396 /* Negative int means reduce maximum width. */
15397 if (precision <= 0)
15398 precision = -lim;
15399 else
15400 precision = min (precision, -lim);
15402 else if (lim > 0)
15404 /* Padding specified. Don't let it be more than
15405 current maximum. */
15406 if (precision > 0)
15407 lim = min (precision, lim);
15409 /* If that's more padding than already wanted, queue it.
15410 But don't reduce padding already specified even if
15411 that is beyond the current truncation point. */
15412 field_width = max (lim, field_width);
15414 goto tail_recurse;
15416 else if (STRINGP (car) || CONSP (car))
15418 register int limit = 50;
15419 /* Limit is to protect against circular lists. */
15420 while (CONSP (elt)
15421 && --limit > 0
15422 && (precision <= 0 || n < precision))
15424 n += display_mode_element (it, depth, field_width - n,
15425 precision - n, XCAR (elt),
15426 props, risky);
15427 elt = XCDR (elt);
15431 break;
15433 default:
15434 invalid:
15435 elt = build_string ("*invalid*");
15436 goto tail_recurse;
15439 /* Pad to FIELD_WIDTH. */
15440 if (field_width > 0 && n < field_width)
15442 if (frame_title_ptr)
15443 n += store_frame_title ("", field_width - n, 0);
15444 else if (!NILP (mode_line_string_list))
15445 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15446 else
15447 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15448 0, 0, 0);
15451 return n;
15454 /* Store a mode-line string element in mode_line_string_list.
15456 If STRING is non-null, display that C string. Otherwise, the Lisp
15457 string LISP_STRING is displayed.
15459 FIELD_WIDTH is the minimum number of output glyphs to produce.
15460 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15461 with spaces. FIELD_WIDTH <= 0 means don't pad.
15463 PRECISION is the maximum number of characters to output from
15464 STRING. PRECISION <= 0 means don't truncate the string.
15466 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15467 properties to the string.
15469 PROPS are the properties to add to the string.
15470 The mode_line_string_face face property is always added to the string.
15473 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15474 char *string;
15475 Lisp_Object lisp_string;
15476 int copy_string;
15477 int field_width;
15478 int precision;
15479 Lisp_Object props;
15481 int len;
15482 int n = 0;
15484 if (string != NULL)
15486 len = strlen (string);
15487 if (precision > 0 && len > precision)
15488 len = precision;
15489 lisp_string = make_string (string, len);
15490 if (NILP (props))
15491 props = mode_line_string_face_prop;
15492 else if (!NILP (mode_line_string_face))
15494 Lisp_Object face = Fplist_get (props, Qface);
15495 props = Fcopy_sequence (props);
15496 if (NILP (face))
15497 face = mode_line_string_face;
15498 else
15499 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15500 props = Fplist_put (props, Qface, face);
15502 Fadd_text_properties (make_number (0), make_number (len),
15503 props, lisp_string);
15505 else
15507 len = XFASTINT (Flength (lisp_string));
15508 if (precision > 0 && len > precision)
15510 len = precision;
15511 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15512 precision = -1;
15514 if (!NILP (mode_line_string_face))
15516 Lisp_Object face;
15517 if (NILP (props))
15518 props = Ftext_properties_at (make_number (0), lisp_string);
15519 face = Fplist_get (props, Qface);
15520 if (NILP (face))
15521 face = mode_line_string_face;
15522 else
15523 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15524 props = Fcons (Qface, Fcons (face, Qnil));
15525 if (copy_string)
15526 lisp_string = Fcopy_sequence (lisp_string);
15528 if (!NILP (props))
15529 Fadd_text_properties (make_number (0), make_number (len),
15530 props, lisp_string);
15533 if (len > 0)
15535 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15536 n += len;
15539 if (field_width > len)
15541 field_width -= len;
15542 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15543 if (!NILP (props))
15544 Fadd_text_properties (make_number (0), make_number (field_width),
15545 props, lisp_string);
15546 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15547 n += field_width;
15550 return n;
15554 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15555 0, 3, 0,
15556 doc: /* Return the mode-line of selected window as a string.
15557 First optional arg FORMAT specifies a different format string (see
15558 `mode-line-format' for details) to use. If FORMAT is t, return
15559 the buffer's header-line. Second optional arg WINDOW specifies a
15560 different window to use as the context for the formatting.
15561 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15562 (format, window, no_props)
15563 Lisp_Object format, window, no_props;
15565 struct it it;
15566 int len;
15567 struct window *w;
15568 struct buffer *old_buffer = NULL;
15569 enum face_id face_id = DEFAULT_FACE_ID;
15571 if (NILP (window))
15572 window = selected_window;
15573 CHECK_WINDOW (window);
15574 w = XWINDOW (window);
15575 CHECK_BUFFER (w->buffer);
15577 if (XBUFFER (w->buffer) != current_buffer)
15579 old_buffer = current_buffer;
15580 set_buffer_internal_1 (XBUFFER (w->buffer));
15583 if (NILP (format) || EQ (format, Qt))
15585 face_id = NILP (format)
15586 ? CURRENT_MODE_LINE_FACE_ID (w) :
15587 HEADER_LINE_FACE_ID;
15588 format = NILP (format)
15589 ? current_buffer->mode_line_format
15590 : current_buffer->header_line_format;
15593 init_iterator (&it, w, -1, -1, NULL, face_id);
15595 if (NILP (no_props))
15597 mode_line_string_face =
15598 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15599 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15600 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15602 mode_line_string_face_prop =
15603 NILP (mode_line_string_face) ? Qnil :
15604 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15606 /* We need a dummy last element in mode_line_string_list to
15607 indicate we are building the propertized mode-line string.
15608 Using mode_line_string_face_prop here GC protects it. */
15609 mode_line_string_list =
15610 Fcons (mode_line_string_face_prop, Qnil);
15611 frame_title_ptr = NULL;
15613 else
15615 mode_line_string_face_prop = Qnil;
15616 mode_line_string_list = Qnil;
15617 frame_title_ptr = frame_title_buf;
15620 push_frame_kboard (it.f);
15621 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15622 pop_frame_kboard ();
15624 if (old_buffer)
15625 set_buffer_internal_1 (old_buffer);
15627 if (NILP (no_props))
15629 Lisp_Object str;
15630 mode_line_string_list = Fnreverse (mode_line_string_list);
15631 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15632 make_string ("", 0));
15633 mode_line_string_face_prop = Qnil;
15634 mode_line_string_list = Qnil;
15635 return str;
15638 len = frame_title_ptr - frame_title_buf;
15639 if (len > 0 && frame_title_ptr[-1] == '-')
15641 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15642 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15644 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15645 if (len > frame_title_ptr - frame_title_buf)
15646 len = frame_title_ptr - frame_title_buf;
15649 frame_title_ptr = NULL;
15650 return make_string (frame_title_buf, len);
15653 /* Write a null-terminated, right justified decimal representation of
15654 the positive integer D to BUF using a minimal field width WIDTH. */
15656 static void
15657 pint2str (buf, width, d)
15658 register char *buf;
15659 register int width;
15660 register int d;
15662 register char *p = buf;
15664 if (d <= 0)
15665 *p++ = '0';
15666 else
15668 while (d > 0)
15670 *p++ = d % 10 + '0';
15671 d /= 10;
15675 for (width -= (int) (p - buf); width > 0; --width)
15676 *p++ = ' ';
15677 *p-- = '\0';
15678 while (p > buf)
15680 d = *buf;
15681 *buf++ = *p;
15682 *p-- = d;
15686 /* Write a null-terminated, right justified decimal and "human
15687 readable" representation of the nonnegative integer D to BUF using
15688 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15690 static const char power_letter[] =
15692 0, /* not used */
15693 'k', /* kilo */
15694 'M', /* mega */
15695 'G', /* giga */
15696 'T', /* tera */
15697 'P', /* peta */
15698 'E', /* exa */
15699 'Z', /* zetta */
15700 'Y' /* yotta */
15703 static void
15704 pint2hrstr (buf, width, d)
15705 char *buf;
15706 int width;
15707 int d;
15709 /* We aim to represent the nonnegative integer D as
15710 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15711 int quotient = d;
15712 int remainder = 0;
15713 /* -1 means: do not use TENTHS. */
15714 int tenths = -1;
15715 int exponent = 0;
15717 /* Length of QUOTIENT.TENTHS as a string. */
15718 int length;
15720 char * psuffix;
15721 char * p;
15723 if (1000 <= quotient)
15725 /* Scale to the appropriate EXPONENT. */
15728 remainder = quotient % 1000;
15729 quotient /= 1000;
15730 exponent++;
15732 while (1000 <= quotient);
15734 /* Round to nearest and decide whether to use TENTHS or not. */
15735 if (quotient <= 9)
15737 tenths = remainder / 100;
15738 if (50 <= remainder % 100)
15739 if (tenths < 9)
15740 tenths++;
15741 else
15743 quotient++;
15744 if (quotient == 10)
15745 tenths = -1;
15746 else
15747 tenths = 0;
15750 else
15751 if (500 <= remainder)
15752 if (quotient < 999)
15753 quotient++;
15754 else
15756 quotient = 1;
15757 exponent++;
15758 tenths = 0;
15762 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15763 if (tenths == -1 && quotient <= 99)
15764 if (quotient <= 9)
15765 length = 1;
15766 else
15767 length = 2;
15768 else
15769 length = 3;
15770 p = psuffix = buf + max (width, length);
15772 /* Print EXPONENT. */
15773 if (exponent)
15774 *psuffix++ = power_letter[exponent];
15775 *psuffix = '\0';
15777 /* Print TENTHS. */
15778 if (tenths >= 0)
15780 *--p = '0' + tenths;
15781 *--p = '.';
15784 /* Print QUOTIENT. */
15787 int digit = quotient % 10;
15788 *--p = '0' + digit;
15790 while ((quotient /= 10) != 0);
15792 /* Print leading spaces. */
15793 while (buf < p)
15794 *--p = ' ';
15797 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15798 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15799 type of CODING_SYSTEM. Return updated pointer into BUF. */
15801 static unsigned char invalid_eol_type[] = "(*invalid*)";
15803 static char *
15804 decode_mode_spec_coding (coding_system, buf, eol_flag)
15805 Lisp_Object coding_system;
15806 register char *buf;
15807 int eol_flag;
15809 Lisp_Object val;
15810 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15811 const unsigned char *eol_str;
15812 int eol_str_len;
15813 /* The EOL conversion we are using. */
15814 Lisp_Object eoltype;
15816 val = Fget (coding_system, Qcoding_system);
15817 eoltype = Qnil;
15819 if (!VECTORP (val)) /* Not yet decided. */
15821 if (multibyte)
15822 *buf++ = '-';
15823 if (eol_flag)
15824 eoltype = eol_mnemonic_undecided;
15825 /* Don't mention EOL conversion if it isn't decided. */
15827 else
15829 Lisp_Object eolvalue;
15831 eolvalue = Fget (coding_system, Qeol_type);
15833 if (multibyte)
15834 *buf++ = XFASTINT (AREF (val, 1));
15836 if (eol_flag)
15838 /* The EOL conversion that is normal on this system. */
15840 if (NILP (eolvalue)) /* Not yet decided. */
15841 eoltype = eol_mnemonic_undecided;
15842 else if (VECTORP (eolvalue)) /* Not yet decided. */
15843 eoltype = eol_mnemonic_undecided;
15844 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15845 eoltype = (XFASTINT (eolvalue) == 0
15846 ? eol_mnemonic_unix
15847 : (XFASTINT (eolvalue) == 1
15848 ? eol_mnemonic_dos : eol_mnemonic_mac));
15852 if (eol_flag)
15854 /* Mention the EOL conversion if it is not the usual one. */
15855 if (STRINGP (eoltype))
15857 eol_str = SDATA (eoltype);
15858 eol_str_len = SBYTES (eoltype);
15860 else if (INTEGERP (eoltype)
15861 && CHAR_VALID_P (XINT (eoltype), 0))
15863 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15864 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15865 eol_str = tmp;
15867 else
15869 eol_str = invalid_eol_type;
15870 eol_str_len = sizeof (invalid_eol_type) - 1;
15872 bcopy (eol_str, buf, eol_str_len);
15873 buf += eol_str_len;
15876 return buf;
15879 /* Return a string for the output of a mode line %-spec for window W,
15880 generated by character C. PRECISION >= 0 means don't return a
15881 string longer than that value. FIELD_WIDTH > 0 means pad the
15882 string returned with spaces to that value. Return 1 in *MULTIBYTE
15883 if the result is multibyte text. */
15885 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15887 static char *
15888 decode_mode_spec (w, c, field_width, precision, multibyte)
15889 struct window *w;
15890 register int c;
15891 int field_width, precision;
15892 int *multibyte;
15894 Lisp_Object obj;
15895 struct frame *f = XFRAME (WINDOW_FRAME (w));
15896 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15897 struct buffer *b = XBUFFER (w->buffer);
15899 obj = Qnil;
15900 *multibyte = 0;
15902 switch (c)
15904 case '*':
15905 if (!NILP (b->read_only))
15906 return "%";
15907 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15908 return "*";
15909 return "-";
15911 case '+':
15912 /* This differs from %* only for a modified read-only buffer. */
15913 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15914 return "*";
15915 if (!NILP (b->read_only))
15916 return "%";
15917 return "-";
15919 case '&':
15920 /* This differs from %* in ignoring read-only-ness. */
15921 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15922 return "*";
15923 return "-";
15925 case '%':
15926 return "%";
15928 case '[':
15930 int i;
15931 char *p;
15933 if (command_loop_level > 5)
15934 return "[[[... ";
15935 p = decode_mode_spec_buf;
15936 for (i = 0; i < command_loop_level; i++)
15937 *p++ = '[';
15938 *p = 0;
15939 return decode_mode_spec_buf;
15942 case ']':
15944 int i;
15945 char *p;
15947 if (command_loop_level > 5)
15948 return " ...]]]";
15949 p = decode_mode_spec_buf;
15950 for (i = 0; i < command_loop_level; i++)
15951 *p++ = ']';
15952 *p = 0;
15953 return decode_mode_spec_buf;
15956 case '-':
15958 register int i;
15960 /* Let lots_of_dashes be a string of infinite length. */
15961 if (!NILP (mode_line_string_list))
15962 return "--";
15963 if (field_width <= 0
15964 || field_width > sizeof (lots_of_dashes))
15966 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15967 decode_mode_spec_buf[i] = '-';
15968 decode_mode_spec_buf[i] = '\0';
15969 return decode_mode_spec_buf;
15971 else
15972 return lots_of_dashes;
15975 case 'b':
15976 obj = b->name;
15977 break;
15979 case 'c':
15981 int col = (int) current_column (); /* iftc */
15982 w->column_number_displayed = make_number (col);
15983 pint2str (decode_mode_spec_buf, field_width, col);
15984 return decode_mode_spec_buf;
15987 case 'F':
15988 /* %F displays the frame name. */
15989 if (!NILP (f->title))
15990 return (char *) SDATA (f->title);
15991 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15992 return (char *) SDATA (f->name);
15993 return "Emacs";
15995 case 'f':
15996 obj = b->filename;
15997 break;
15999 case 'i':
16001 int size = ZV - BEGV;
16002 pint2str (decode_mode_spec_buf, field_width, size);
16003 return decode_mode_spec_buf;
16006 case 'I':
16008 int size = ZV - BEGV;
16009 pint2hrstr (decode_mode_spec_buf, field_width, size);
16010 return decode_mode_spec_buf;
16013 case 'l':
16015 int startpos = XMARKER (w->start)->charpos;
16016 int startpos_byte = marker_byte_position (w->start);
16017 int line, linepos, linepos_byte, topline;
16018 int nlines, junk;
16019 int height = WINDOW_TOTAL_LINES (w);
16021 /* If we decided that this buffer isn't suitable for line numbers,
16022 don't forget that too fast. */
16023 if (EQ (w->base_line_pos, w->buffer))
16024 goto no_value;
16025 /* But do forget it, if the window shows a different buffer now. */
16026 else if (BUFFERP (w->base_line_pos))
16027 w->base_line_pos = Qnil;
16029 /* If the buffer is very big, don't waste time. */
16030 if (INTEGERP (Vline_number_display_limit)
16031 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16033 w->base_line_pos = Qnil;
16034 w->base_line_number = Qnil;
16035 goto no_value;
16038 if (!NILP (w->base_line_number)
16039 && !NILP (w->base_line_pos)
16040 && XFASTINT (w->base_line_pos) <= startpos)
16042 line = XFASTINT (w->base_line_number);
16043 linepos = XFASTINT (w->base_line_pos);
16044 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16046 else
16048 line = 1;
16049 linepos = BUF_BEGV (b);
16050 linepos_byte = BUF_BEGV_BYTE (b);
16053 /* Count lines from base line to window start position. */
16054 nlines = display_count_lines (linepos, linepos_byte,
16055 startpos_byte,
16056 startpos, &junk);
16058 topline = nlines + line;
16060 /* Determine a new base line, if the old one is too close
16061 or too far away, or if we did not have one.
16062 "Too close" means it's plausible a scroll-down would
16063 go back past it. */
16064 if (startpos == BUF_BEGV (b))
16066 w->base_line_number = make_number (topline);
16067 w->base_line_pos = make_number (BUF_BEGV (b));
16069 else if (nlines < height + 25 || nlines > height * 3 + 50
16070 || linepos == BUF_BEGV (b))
16072 int limit = BUF_BEGV (b);
16073 int limit_byte = BUF_BEGV_BYTE (b);
16074 int position;
16075 int distance = (height * 2 + 30) * line_number_display_limit_width;
16077 if (startpos - distance > limit)
16079 limit = startpos - distance;
16080 limit_byte = CHAR_TO_BYTE (limit);
16083 nlines = display_count_lines (startpos, startpos_byte,
16084 limit_byte,
16085 - (height * 2 + 30),
16086 &position);
16087 /* If we couldn't find the lines we wanted within
16088 line_number_display_limit_width chars per line,
16089 give up on line numbers for this window. */
16090 if (position == limit_byte && limit == startpos - distance)
16092 w->base_line_pos = w->buffer;
16093 w->base_line_number = Qnil;
16094 goto no_value;
16097 w->base_line_number = make_number (topline - nlines);
16098 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16101 /* Now count lines from the start pos to point. */
16102 nlines = display_count_lines (startpos, startpos_byte,
16103 PT_BYTE, PT, &junk);
16105 /* Record that we did display the line number. */
16106 line_number_displayed = 1;
16108 /* Make the string to show. */
16109 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16110 return decode_mode_spec_buf;
16111 no_value:
16113 char* p = decode_mode_spec_buf;
16114 int pad = field_width - 2;
16115 while (pad-- > 0)
16116 *p++ = ' ';
16117 *p++ = '?';
16118 *p++ = '?';
16119 *p = '\0';
16120 return decode_mode_spec_buf;
16123 break;
16125 case 'm':
16126 obj = b->mode_name;
16127 break;
16129 case 'n':
16130 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16131 return " Narrow";
16132 break;
16134 case 'p':
16136 int pos = marker_position (w->start);
16137 int total = BUF_ZV (b) - BUF_BEGV (b);
16139 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16141 if (pos <= BUF_BEGV (b))
16142 return "All";
16143 else
16144 return "Bottom";
16146 else if (pos <= BUF_BEGV (b))
16147 return "Top";
16148 else
16150 if (total > 1000000)
16151 /* Do it differently for a large value, to avoid overflow. */
16152 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16153 else
16154 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16155 /* We can't normally display a 3-digit number,
16156 so get us a 2-digit number that is close. */
16157 if (total == 100)
16158 total = 99;
16159 sprintf (decode_mode_spec_buf, "%2d%%", total);
16160 return decode_mode_spec_buf;
16164 /* Display percentage of size above the bottom of the screen. */
16165 case 'P':
16167 int toppos = marker_position (w->start);
16168 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16169 int total = BUF_ZV (b) - BUF_BEGV (b);
16171 if (botpos >= BUF_ZV (b))
16173 if (toppos <= BUF_BEGV (b))
16174 return "All";
16175 else
16176 return "Bottom";
16178 else
16180 if (total > 1000000)
16181 /* Do it differently for a large value, to avoid overflow. */
16182 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16183 else
16184 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16185 /* We can't normally display a 3-digit number,
16186 so get us a 2-digit number that is close. */
16187 if (total == 100)
16188 total = 99;
16189 if (toppos <= BUF_BEGV (b))
16190 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16191 else
16192 sprintf (decode_mode_spec_buf, "%2d%%", total);
16193 return decode_mode_spec_buf;
16197 case 's':
16198 /* status of process */
16199 obj = Fget_buffer_process (w->buffer);
16200 if (NILP (obj))
16201 return "no process";
16202 #ifdef subprocesses
16203 obj = Fsymbol_name (Fprocess_status (obj));
16204 #endif
16205 break;
16207 case 't': /* indicate TEXT or BINARY */
16208 #ifdef MODE_LINE_BINARY_TEXT
16209 return MODE_LINE_BINARY_TEXT (b);
16210 #else
16211 return "T";
16212 #endif
16214 case 'z':
16215 /* coding-system (not including end-of-line format) */
16216 case 'Z':
16217 /* coding-system (including end-of-line type) */
16219 int eol_flag = (c == 'Z');
16220 char *p = decode_mode_spec_buf;
16222 if (! FRAME_WINDOW_P (f))
16224 /* No need to mention EOL here--the terminal never needs
16225 to do EOL conversion. */
16226 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16227 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16229 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16230 p, eol_flag);
16232 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16233 #ifdef subprocesses
16234 obj = Fget_buffer_process (Fcurrent_buffer ());
16235 if (PROCESSP (obj))
16237 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16238 p, eol_flag);
16239 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16240 p, eol_flag);
16242 #endif /* subprocesses */
16243 #endif /* 0 */
16244 *p = 0;
16245 return decode_mode_spec_buf;
16249 if (STRINGP (obj))
16251 *multibyte = STRING_MULTIBYTE (obj);
16252 return (char *) SDATA (obj);
16254 else
16255 return "";
16259 /* Count up to COUNT lines starting from START / START_BYTE.
16260 But don't go beyond LIMIT_BYTE.
16261 Return the number of lines thus found (always nonnegative).
16263 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16265 static int
16266 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16267 int start, start_byte, limit_byte, count;
16268 int *byte_pos_ptr;
16270 register unsigned char *cursor;
16271 unsigned char *base;
16273 register int ceiling;
16274 register unsigned char *ceiling_addr;
16275 int orig_count = count;
16277 /* If we are not in selective display mode,
16278 check only for newlines. */
16279 int selective_display = (!NILP (current_buffer->selective_display)
16280 && !INTEGERP (current_buffer->selective_display));
16282 if (count > 0)
16284 while (start_byte < limit_byte)
16286 ceiling = BUFFER_CEILING_OF (start_byte);
16287 ceiling = min (limit_byte - 1, ceiling);
16288 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16289 base = (cursor = BYTE_POS_ADDR (start_byte));
16290 while (1)
16292 if (selective_display)
16293 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16295 else
16296 while (*cursor != '\n' && ++cursor != ceiling_addr)
16299 if (cursor != ceiling_addr)
16301 if (--count == 0)
16303 start_byte += cursor - base + 1;
16304 *byte_pos_ptr = start_byte;
16305 return orig_count;
16307 else
16308 if (++cursor == ceiling_addr)
16309 break;
16311 else
16312 break;
16314 start_byte += cursor - base;
16317 else
16319 while (start_byte > limit_byte)
16321 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16322 ceiling = max (limit_byte, ceiling);
16323 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16324 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16325 while (1)
16327 if (selective_display)
16328 while (--cursor != ceiling_addr
16329 && *cursor != '\n' && *cursor != 015)
16331 else
16332 while (--cursor != ceiling_addr && *cursor != '\n')
16335 if (cursor != ceiling_addr)
16337 if (++count == 0)
16339 start_byte += cursor - base + 1;
16340 *byte_pos_ptr = start_byte;
16341 /* When scanning backwards, we should
16342 not count the newline posterior to which we stop. */
16343 return - orig_count - 1;
16346 else
16347 break;
16349 /* Here we add 1 to compensate for the last decrement
16350 of CURSOR, which took it past the valid range. */
16351 start_byte += cursor - base + 1;
16355 *byte_pos_ptr = limit_byte;
16357 if (count < 0)
16358 return - orig_count + count;
16359 return orig_count - count;
16365 /***********************************************************************
16366 Displaying strings
16367 ***********************************************************************/
16369 /* Display a NUL-terminated string, starting with index START.
16371 If STRING is non-null, display that C string. Otherwise, the Lisp
16372 string LISP_STRING is displayed.
16374 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16375 FACE_STRING. Display STRING or LISP_STRING with the face at
16376 FACE_STRING_POS in FACE_STRING:
16378 Display the string in the environment given by IT, but use the
16379 standard display table, temporarily.
16381 FIELD_WIDTH is the minimum number of output glyphs to produce.
16382 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16383 with spaces. If STRING has more characters, more than FIELD_WIDTH
16384 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16386 PRECISION is the maximum number of characters to output from
16387 STRING. PRECISION < 0 means don't truncate the string.
16389 This is roughly equivalent to printf format specifiers:
16391 FIELD_WIDTH PRECISION PRINTF
16392 ----------------------------------------
16393 -1 -1 %s
16394 -1 10 %.10s
16395 10 -1 %10s
16396 20 10 %20.10s
16398 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16399 display them, and < 0 means obey the current buffer's value of
16400 enable_multibyte_characters.
16402 Value is the number of glyphs produced. */
16404 static int
16405 display_string (string, lisp_string, face_string, face_string_pos,
16406 start, it, field_width, precision, max_x, multibyte)
16407 unsigned char *string;
16408 Lisp_Object lisp_string;
16409 Lisp_Object face_string;
16410 int face_string_pos;
16411 int start;
16412 struct it *it;
16413 int field_width, precision, max_x;
16414 int multibyte;
16416 int hpos_at_start = it->hpos;
16417 int saved_face_id = it->face_id;
16418 struct glyph_row *row = it->glyph_row;
16420 /* Initialize the iterator IT for iteration over STRING beginning
16421 with index START. */
16422 reseat_to_string (it, string, lisp_string, start,
16423 precision, field_width, multibyte);
16425 /* If displaying STRING, set up the face of the iterator
16426 from LISP_STRING, if that's given. */
16427 if (STRINGP (face_string))
16429 int endptr;
16430 struct face *face;
16432 it->face_id
16433 = face_at_string_position (it->w, face_string, face_string_pos,
16434 0, it->region_beg_charpos,
16435 it->region_end_charpos,
16436 &endptr, it->base_face_id, 0);
16437 face = FACE_FROM_ID (it->f, it->face_id);
16438 it->face_box_p = face->box != FACE_NO_BOX;
16441 /* Set max_x to the maximum allowed X position. Don't let it go
16442 beyond the right edge of the window. */
16443 if (max_x <= 0)
16444 max_x = it->last_visible_x;
16445 else
16446 max_x = min (max_x, it->last_visible_x);
16448 /* Skip over display elements that are not visible. because IT->w is
16449 hscrolled. */
16450 if (it->current_x < it->first_visible_x)
16451 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16452 MOVE_TO_POS | MOVE_TO_X);
16454 row->ascent = it->max_ascent;
16455 row->height = it->max_ascent + it->max_descent;
16456 row->phys_ascent = it->max_phys_ascent;
16457 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16459 /* This condition is for the case that we are called with current_x
16460 past last_visible_x. */
16461 while (it->current_x < max_x)
16463 int x_before, x, n_glyphs_before, i, nglyphs;
16465 /* Get the next display element. */
16466 if (!get_next_display_element (it))
16467 break;
16469 /* Produce glyphs. */
16470 x_before = it->current_x;
16471 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16472 PRODUCE_GLYPHS (it);
16474 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16475 i = 0;
16476 x = x_before;
16477 while (i < nglyphs)
16479 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16481 if (!it->truncate_lines_p
16482 && x + glyph->pixel_width > max_x)
16484 /* End of continued line or max_x reached. */
16485 if (CHAR_GLYPH_PADDING_P (*glyph))
16487 /* A wide character is unbreakable. */
16488 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16489 it->current_x = x_before;
16491 else
16493 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16494 it->current_x = x;
16496 break;
16498 else if (x + glyph->pixel_width > it->first_visible_x)
16500 /* Glyph is at least partially visible. */
16501 ++it->hpos;
16502 if (x < it->first_visible_x)
16503 it->glyph_row->x = x - it->first_visible_x;
16505 else
16507 /* Glyph is off the left margin of the display area.
16508 Should not happen. */
16509 abort ();
16512 row->ascent = max (row->ascent, it->max_ascent);
16513 row->height = max (row->height, it->max_ascent + it->max_descent);
16514 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16515 row->phys_height = max (row->phys_height,
16516 it->max_phys_ascent + it->max_phys_descent);
16517 x += glyph->pixel_width;
16518 ++i;
16521 /* Stop if max_x reached. */
16522 if (i < nglyphs)
16523 break;
16525 /* Stop at line ends. */
16526 if (ITERATOR_AT_END_OF_LINE_P (it))
16528 it->continuation_lines_width = 0;
16529 break;
16532 set_iterator_to_next (it, 1);
16534 /* Stop if truncating at the right edge. */
16535 if (it->truncate_lines_p
16536 && it->current_x >= it->last_visible_x)
16538 /* Add truncation mark, but don't do it if the line is
16539 truncated at a padding space. */
16540 if (IT_CHARPOS (*it) < it->string_nchars)
16542 if (!FRAME_WINDOW_P (it->f))
16544 int i, n;
16546 if (it->current_x > it->last_visible_x)
16548 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16549 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16550 break;
16551 for (n = row->used[TEXT_AREA]; i < n; ++i)
16553 row->used[TEXT_AREA] = i;
16554 produce_special_glyphs (it, IT_TRUNCATION);
16557 produce_special_glyphs (it, IT_TRUNCATION);
16559 it->glyph_row->truncated_on_right_p = 1;
16561 break;
16565 /* Maybe insert a truncation at the left. */
16566 if (it->first_visible_x
16567 && IT_CHARPOS (*it) > 0)
16569 if (!FRAME_WINDOW_P (it->f))
16570 insert_left_trunc_glyphs (it);
16571 it->glyph_row->truncated_on_left_p = 1;
16574 it->face_id = saved_face_id;
16576 /* Value is number of columns displayed. */
16577 return it->hpos - hpos_at_start;
16582 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16583 appears as an element of LIST or as the car of an element of LIST.
16584 If PROPVAL is a list, compare each element against LIST in that
16585 way, and return 1/2 if any element of PROPVAL is found in LIST.
16586 Otherwise return 0. This function cannot quit.
16587 The return value is 2 if the text is invisible but with an ellipsis
16588 and 1 if it's invisible and without an ellipsis. */
16591 invisible_p (propval, list)
16592 register Lisp_Object propval;
16593 Lisp_Object list;
16595 register Lisp_Object tail, proptail;
16597 for (tail = list; CONSP (tail); tail = XCDR (tail))
16599 register Lisp_Object tem;
16600 tem = XCAR (tail);
16601 if (EQ (propval, tem))
16602 return 1;
16603 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16604 return NILP (XCDR (tem)) ? 1 : 2;
16607 if (CONSP (propval))
16609 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16611 Lisp_Object propelt;
16612 propelt = XCAR (proptail);
16613 for (tail = list; CONSP (tail); tail = XCDR (tail))
16615 register Lisp_Object tem;
16616 tem = XCAR (tail);
16617 if (EQ (propelt, tem))
16618 return 1;
16619 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16620 return NILP (XCDR (tem)) ? 1 : 2;
16625 return 0;
16629 /***********************************************************************
16630 Glyph Display
16631 ***********************************************************************/
16633 #ifdef HAVE_WINDOW_SYSTEM
16635 #if GLYPH_DEBUG
16637 void
16638 dump_glyph_string (s)
16639 struct glyph_string *s;
16641 fprintf (stderr, "glyph string\n");
16642 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16643 s->x, s->y, s->width, s->height);
16644 fprintf (stderr, " ybase = %d\n", s->ybase);
16645 fprintf (stderr, " hl = %d\n", s->hl);
16646 fprintf (stderr, " left overhang = %d, right = %d\n",
16647 s->left_overhang, s->right_overhang);
16648 fprintf (stderr, " nchars = %d\n", s->nchars);
16649 fprintf (stderr, " extends to end of line = %d\n",
16650 s->extends_to_end_of_line_p);
16651 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16652 fprintf (stderr, " bg width = %d\n", s->background_width);
16655 #endif /* GLYPH_DEBUG */
16657 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16658 of XChar2b structures for S; it can't be allocated in
16659 init_glyph_string because it must be allocated via `alloca'. W
16660 is the window on which S is drawn. ROW and AREA are the glyph row
16661 and area within the row from which S is constructed. START is the
16662 index of the first glyph structure covered by S. HL is a
16663 face-override for drawing S. */
16665 #ifdef HAVE_NTGUI
16666 #define OPTIONAL_HDC(hdc) hdc,
16667 #define DECLARE_HDC(hdc) HDC hdc;
16668 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16669 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16670 #endif
16672 #ifndef OPTIONAL_HDC
16673 #define OPTIONAL_HDC(hdc)
16674 #define DECLARE_HDC(hdc)
16675 #define ALLOCATE_HDC(hdc, f)
16676 #define RELEASE_HDC(hdc, f)
16677 #endif
16679 static void
16680 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16681 struct glyph_string *s;
16682 DECLARE_HDC (hdc)
16683 XChar2b *char2b;
16684 struct window *w;
16685 struct glyph_row *row;
16686 enum glyph_row_area area;
16687 int start;
16688 enum draw_glyphs_face hl;
16690 bzero (s, sizeof *s);
16691 s->w = w;
16692 s->f = XFRAME (w->frame);
16693 #ifdef HAVE_NTGUI
16694 s->hdc = hdc;
16695 #endif
16696 s->display = FRAME_X_DISPLAY (s->f);
16697 s->window = FRAME_X_WINDOW (s->f);
16698 s->char2b = char2b;
16699 s->hl = hl;
16700 s->row = row;
16701 s->area = area;
16702 s->first_glyph = row->glyphs[area] + start;
16703 s->height = row->height;
16704 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16706 /* Display the internal border below the tool-bar window. */
16707 if (s->w == XWINDOW (s->f->tool_bar_window))
16708 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16710 s->ybase = s->y + row->ascent;
16714 /* Append the list of glyph strings with head H and tail T to the list
16715 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16717 static INLINE void
16718 append_glyph_string_lists (head, tail, h, t)
16719 struct glyph_string **head, **tail;
16720 struct glyph_string *h, *t;
16722 if (h)
16724 if (*head)
16725 (*tail)->next = h;
16726 else
16727 *head = h;
16728 h->prev = *tail;
16729 *tail = t;
16734 /* Prepend the list of glyph strings with head H and tail T to the
16735 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16736 result. */
16738 static INLINE void
16739 prepend_glyph_string_lists (head, tail, h, t)
16740 struct glyph_string **head, **tail;
16741 struct glyph_string *h, *t;
16743 if (h)
16745 if (*head)
16746 (*head)->prev = t;
16747 else
16748 *tail = t;
16749 t->next = *head;
16750 *head = h;
16755 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16756 Set *HEAD and *TAIL to the resulting list. */
16758 static INLINE void
16759 append_glyph_string (head, tail, s)
16760 struct glyph_string **head, **tail;
16761 struct glyph_string *s;
16763 s->next = s->prev = NULL;
16764 append_glyph_string_lists (head, tail, s, s);
16768 /* Get face and two-byte form of character glyph GLYPH on frame F.
16769 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16770 a pointer to a realized face that is ready for display. */
16772 static INLINE struct face *
16773 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16774 struct frame *f;
16775 struct glyph *glyph;
16776 XChar2b *char2b;
16777 int *two_byte_p;
16779 struct face *face;
16781 xassert (glyph->type == CHAR_GLYPH);
16782 face = FACE_FROM_ID (f, glyph->face_id);
16784 if (two_byte_p)
16785 *two_byte_p = 0;
16787 if (!glyph->multibyte_p)
16789 /* Unibyte case. We don't have to encode, but we have to make
16790 sure to use a face suitable for unibyte. */
16791 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16793 else if (glyph->u.ch < 128
16794 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16796 /* Case of ASCII in a face known to fit ASCII. */
16797 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16799 else
16801 int c1, c2, charset;
16803 /* Split characters into bytes. If c2 is -1 afterwards, C is
16804 really a one-byte character so that byte1 is zero. */
16805 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16806 if (c2 > 0)
16807 STORE_XCHAR2B (char2b, c1, c2);
16808 else
16809 STORE_XCHAR2B (char2b, 0, c1);
16811 /* Maybe encode the character in *CHAR2B. */
16812 if (charset != CHARSET_ASCII)
16814 struct font_info *font_info
16815 = FONT_INFO_FROM_ID (f, face->font_info_id);
16816 if (font_info)
16817 glyph->font_type
16818 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16822 /* Make sure X resources of the face are allocated. */
16823 xassert (face != NULL);
16824 PREPARE_FACE_FOR_DISPLAY (f, face);
16825 return face;
16829 /* Fill glyph string S with composition components specified by S->cmp.
16831 FACES is an array of faces for all components of this composition.
16832 S->gidx is the index of the first component for S.
16833 OVERLAPS_P non-zero means S should draw the foreground only, and
16834 use its physical height for clipping.
16836 Value is the index of a component not in S. */
16838 static int
16839 fill_composite_glyph_string (s, faces, overlaps_p)
16840 struct glyph_string *s;
16841 struct face **faces;
16842 int overlaps_p;
16844 int i;
16846 xassert (s);
16848 s->for_overlaps_p = overlaps_p;
16850 s->face = faces[s->gidx];
16851 s->font = s->face->font;
16852 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16854 /* For all glyphs of this composition, starting at the offset
16855 S->gidx, until we reach the end of the definition or encounter a
16856 glyph that requires the different face, add it to S. */
16857 ++s->nchars;
16858 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16859 ++s->nchars;
16861 /* All glyph strings for the same composition has the same width,
16862 i.e. the width set for the first component of the composition. */
16864 s->width = s->first_glyph->pixel_width;
16866 /* If the specified font could not be loaded, use the frame's
16867 default font, but record the fact that we couldn't load it in
16868 the glyph string so that we can draw rectangles for the
16869 characters of the glyph string. */
16870 if (s->font == NULL)
16872 s->font_not_found_p = 1;
16873 s->font = FRAME_FONT (s->f);
16876 /* Adjust base line for subscript/superscript text. */
16877 s->ybase += s->first_glyph->voffset;
16879 xassert (s->face && s->face->gc);
16881 /* This glyph string must always be drawn with 16-bit functions. */
16882 s->two_byte_p = 1;
16884 return s->gidx + s->nchars;
16888 /* Fill glyph string S from a sequence of character glyphs.
16890 FACE_ID is the face id of the string. START is the index of the
16891 first glyph to consider, END is the index of the last + 1.
16892 OVERLAPS_P non-zero means S should draw the foreground only, and
16893 use its physical height for clipping.
16895 Value is the index of the first glyph not in S. */
16897 static int
16898 fill_glyph_string (s, face_id, start, end, overlaps_p)
16899 struct glyph_string *s;
16900 int face_id;
16901 int start, end, overlaps_p;
16903 struct glyph *glyph, *last;
16904 int voffset;
16905 int glyph_not_available_p;
16907 xassert (s->f == XFRAME (s->w->frame));
16908 xassert (s->nchars == 0);
16909 xassert (start >= 0 && end > start);
16911 s->for_overlaps_p = overlaps_p,
16912 glyph = s->row->glyphs[s->area] + start;
16913 last = s->row->glyphs[s->area] + end;
16914 voffset = glyph->voffset;
16916 glyph_not_available_p = glyph->glyph_not_available_p;
16918 while (glyph < last
16919 && glyph->type == CHAR_GLYPH
16920 && glyph->voffset == voffset
16921 /* Same face id implies same font, nowadays. */
16922 && glyph->face_id == face_id
16923 && glyph->glyph_not_available_p == glyph_not_available_p)
16925 int two_byte_p;
16927 s->face = get_glyph_face_and_encoding (s->f, glyph,
16928 s->char2b + s->nchars,
16929 &two_byte_p);
16930 s->two_byte_p = two_byte_p;
16931 ++s->nchars;
16932 xassert (s->nchars <= end - start);
16933 s->width += glyph->pixel_width;
16934 ++glyph;
16937 s->font = s->face->font;
16938 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16940 /* If the specified font could not be loaded, use the frame's font,
16941 but record the fact that we couldn't load it in
16942 S->font_not_found_p so that we can draw rectangles for the
16943 characters of the glyph string. */
16944 if (s->font == NULL || glyph_not_available_p)
16946 s->font_not_found_p = 1;
16947 s->font = FRAME_FONT (s->f);
16950 /* Adjust base line for subscript/superscript text. */
16951 s->ybase += voffset;
16953 xassert (s->face && s->face->gc);
16954 return glyph - s->row->glyphs[s->area];
16958 /* Fill glyph string S from image glyph S->first_glyph. */
16960 static void
16961 fill_image_glyph_string (s)
16962 struct glyph_string *s;
16964 xassert (s->first_glyph->type == IMAGE_GLYPH);
16965 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16966 xassert (s->img);
16967 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16968 s->font = s->face->font;
16969 s->width = s->first_glyph->pixel_width;
16971 /* Adjust base line for subscript/superscript text. */
16972 s->ybase += s->first_glyph->voffset;
16976 /* Fill glyph string S from a sequence of stretch glyphs.
16978 ROW is the glyph row in which the glyphs are found, AREA is the
16979 area within the row. START is the index of the first glyph to
16980 consider, END is the index of the last + 1.
16982 Value is the index of the first glyph not in S. */
16984 static int
16985 fill_stretch_glyph_string (s, row, area, start, end)
16986 struct glyph_string *s;
16987 struct glyph_row *row;
16988 enum glyph_row_area area;
16989 int start, end;
16991 struct glyph *glyph, *last;
16992 int voffset, face_id;
16994 xassert (s->first_glyph->type == STRETCH_GLYPH);
16996 glyph = s->row->glyphs[s->area] + start;
16997 last = s->row->glyphs[s->area] + end;
16998 face_id = glyph->face_id;
16999 s->face = FACE_FROM_ID (s->f, face_id);
17000 s->font = s->face->font;
17001 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17002 s->width = glyph->pixel_width;
17003 voffset = glyph->voffset;
17005 for (++glyph;
17006 (glyph < last
17007 && glyph->type == STRETCH_GLYPH
17008 && glyph->voffset == voffset
17009 && glyph->face_id == face_id);
17010 ++glyph)
17011 s->width += glyph->pixel_width;
17013 /* Adjust base line for subscript/superscript text. */
17014 s->ybase += voffset;
17016 /* The case that face->gc == 0 is handled when drawing the glyph
17017 string by calling PREPARE_FACE_FOR_DISPLAY. */
17018 xassert (s->face);
17019 return glyph - s->row->glyphs[s->area];
17023 /* EXPORT for RIF:
17024 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17025 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17026 assumed to be zero. */
17028 void
17029 x_get_glyph_overhangs (glyph, f, left, right)
17030 struct glyph *glyph;
17031 struct frame *f;
17032 int *left, *right;
17034 *left = *right = 0;
17036 if (glyph->type == CHAR_GLYPH)
17038 XFontStruct *font;
17039 struct face *face;
17040 struct font_info *font_info;
17041 XChar2b char2b;
17042 XCharStruct *pcm;
17044 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17045 font = face->font;
17046 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17047 if (font /* ++KFS: Should this be font_info ? */
17048 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17050 if (pcm->rbearing > pcm->width)
17051 *right = pcm->rbearing - pcm->width;
17052 if (pcm->lbearing < 0)
17053 *left = -pcm->lbearing;
17059 /* Return the index of the first glyph preceding glyph string S that
17060 is overwritten by S because of S's left overhang. Value is -1
17061 if no glyphs are overwritten. */
17063 static int
17064 left_overwritten (s)
17065 struct glyph_string *s;
17067 int k;
17069 if (s->left_overhang)
17071 int x = 0, i;
17072 struct glyph *glyphs = s->row->glyphs[s->area];
17073 int first = s->first_glyph - glyphs;
17075 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17076 x -= glyphs[i].pixel_width;
17078 k = i + 1;
17080 else
17081 k = -1;
17083 return k;
17087 /* Return the index of the first glyph preceding glyph string S that
17088 is overwriting S because of its right overhang. Value is -1 if no
17089 glyph in front of S overwrites S. */
17091 static int
17092 left_overwriting (s)
17093 struct glyph_string *s;
17095 int i, k, x;
17096 struct glyph *glyphs = s->row->glyphs[s->area];
17097 int first = s->first_glyph - glyphs;
17099 k = -1;
17100 x = 0;
17101 for (i = first - 1; i >= 0; --i)
17103 int left, right;
17104 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17105 if (x + right > 0)
17106 k = i;
17107 x -= glyphs[i].pixel_width;
17110 return k;
17114 /* Return the index of the last glyph following glyph string S that is
17115 not overwritten by S because of S's right overhang. Value is -1 if
17116 no such glyph is found. */
17118 static int
17119 right_overwritten (s)
17120 struct glyph_string *s;
17122 int k = -1;
17124 if (s->right_overhang)
17126 int x = 0, i;
17127 struct glyph *glyphs = s->row->glyphs[s->area];
17128 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17129 int end = s->row->used[s->area];
17131 for (i = first; i < end && s->right_overhang > x; ++i)
17132 x += glyphs[i].pixel_width;
17134 k = i;
17137 return k;
17141 /* Return the index of the last glyph following glyph string S that
17142 overwrites S because of its left overhang. Value is negative
17143 if no such glyph is found. */
17145 static int
17146 right_overwriting (s)
17147 struct glyph_string *s;
17149 int i, k, x;
17150 int end = s->row->used[s->area];
17151 struct glyph *glyphs = s->row->glyphs[s->area];
17152 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17154 k = -1;
17155 x = 0;
17156 for (i = first; i < end; ++i)
17158 int left, right;
17159 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17160 if (x - left < 0)
17161 k = i;
17162 x += glyphs[i].pixel_width;
17165 return k;
17169 /* Get face and two-byte form of character C in face FACE_ID on frame
17170 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17171 means we want to display multibyte text. DISPLAY_P non-zero means
17172 make sure that X resources for the face returned are allocated.
17173 Value is a pointer to a realized face that is ready for display if
17174 DISPLAY_P is non-zero. */
17176 static INLINE struct face *
17177 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17178 struct frame *f;
17179 int c, face_id;
17180 XChar2b *char2b;
17181 int multibyte_p, display_p;
17183 struct face *face = FACE_FROM_ID (f, face_id);
17185 if (!multibyte_p)
17187 /* Unibyte case. We don't have to encode, but we have to make
17188 sure to use a face suitable for unibyte. */
17189 STORE_XCHAR2B (char2b, 0, c);
17190 face_id = FACE_FOR_CHAR (f, face, c);
17191 face = FACE_FROM_ID (f, face_id);
17193 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17195 /* Case of ASCII in a face known to fit ASCII. */
17196 STORE_XCHAR2B (char2b, 0, c);
17198 else
17200 int c1, c2, charset;
17202 /* Split characters into bytes. If c2 is -1 afterwards, C is
17203 really a one-byte character so that byte1 is zero. */
17204 SPLIT_CHAR (c, charset, c1, c2);
17205 if (c2 > 0)
17206 STORE_XCHAR2B (char2b, c1, c2);
17207 else
17208 STORE_XCHAR2B (char2b, 0, c1);
17210 /* Maybe encode the character in *CHAR2B. */
17211 if (face->font != NULL)
17213 struct font_info *font_info
17214 = FONT_INFO_FROM_ID (f, face->font_info_id);
17215 if (font_info)
17216 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17220 /* Make sure X resources of the face are allocated. */
17221 #ifdef HAVE_X_WINDOWS
17222 if (display_p)
17223 #endif
17225 xassert (face != NULL);
17226 PREPARE_FACE_FOR_DISPLAY (f, face);
17229 return face;
17233 /* Set background width of glyph string S. START is the index of the
17234 first glyph following S. LAST_X is the right-most x-position + 1
17235 in the drawing area. */
17237 static INLINE void
17238 set_glyph_string_background_width (s, start, last_x)
17239 struct glyph_string *s;
17240 int start;
17241 int last_x;
17243 /* If the face of this glyph string has to be drawn to the end of
17244 the drawing area, set S->extends_to_end_of_line_p. */
17245 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17247 if (start == s->row->used[s->area]
17248 && s->area == TEXT_AREA
17249 && ((s->hl == DRAW_NORMAL_TEXT
17250 && (s->row->fill_line_p
17251 || s->face->background != default_face->background
17252 || s->face->stipple != default_face->stipple
17253 || s->row->mouse_face_p))
17254 || s->hl == DRAW_MOUSE_FACE
17255 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17256 && s->row->fill_line_p)))
17257 s->extends_to_end_of_line_p = 1;
17259 /* If S extends its face to the end of the line, set its
17260 background_width to the distance to the right edge of the drawing
17261 area. */
17262 if (s->extends_to_end_of_line_p)
17263 s->background_width = last_x - s->x + 1;
17264 else
17265 s->background_width = s->width;
17269 /* Compute overhangs and x-positions for glyph string S and its
17270 predecessors, or successors. X is the starting x-position for S.
17271 BACKWARD_P non-zero means process predecessors. */
17273 static void
17274 compute_overhangs_and_x (s, x, backward_p)
17275 struct glyph_string *s;
17276 int x;
17277 int backward_p;
17279 if (backward_p)
17281 while (s)
17283 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17284 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17285 x -= s->width;
17286 s->x = x;
17287 s = s->prev;
17290 else
17292 while (s)
17294 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17295 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17296 s->x = x;
17297 x += s->width;
17298 s = s->next;
17305 /* The following macros are only called from draw_glyphs below.
17306 They reference the following parameters of that function directly:
17307 `w', `row', `area', and `overlap_p'
17308 as well as the following local variables:
17309 `s', `f', and `hdc' (in W32) */
17311 #ifdef HAVE_NTGUI
17312 /* On W32, silently add local `hdc' variable to argument list of
17313 init_glyph_string. */
17314 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17315 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17316 #else
17317 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17318 init_glyph_string (s, char2b, w, row, area, start, hl)
17319 #endif
17321 /* Add a glyph string for a stretch glyph to the list of strings
17322 between HEAD and TAIL. START is the index of the stretch glyph in
17323 row area AREA of glyph row ROW. END is the index of the last glyph
17324 in that glyph row area. X is the current output position assigned
17325 to the new glyph string constructed. HL overrides that face of the
17326 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17327 is the right-most x-position of the drawing area. */
17329 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17330 and below -- keep them on one line. */
17331 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17332 do \
17334 s = (struct glyph_string *) alloca (sizeof *s); \
17335 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17336 START = fill_stretch_glyph_string (s, row, area, START, END); \
17337 append_glyph_string (&HEAD, &TAIL, s); \
17338 s->x = (X); \
17340 while (0)
17343 /* Add a glyph string for an image glyph to the list of strings
17344 between HEAD and TAIL. START is the index of the image glyph in
17345 row area AREA of glyph row ROW. END is the index of the last glyph
17346 in that glyph row area. X is the current output position assigned
17347 to the new glyph string constructed. HL overrides that face of the
17348 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17349 is the right-most x-position of the drawing area. */
17351 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17352 do \
17354 s = (struct glyph_string *) alloca (sizeof *s); \
17355 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17356 fill_image_glyph_string (s); \
17357 append_glyph_string (&HEAD, &TAIL, s); \
17358 ++START; \
17359 s->x = (X); \
17361 while (0)
17364 /* Add a glyph string for a sequence of character glyphs to the list
17365 of strings between HEAD and TAIL. START is the index of the first
17366 glyph in row area AREA of glyph row ROW that is part of the new
17367 glyph string. END is the index of the last glyph in that glyph row
17368 area. X is the current output position assigned to the new glyph
17369 string constructed. HL overrides that face of the glyph; e.g. it
17370 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17371 right-most x-position of the drawing area. */
17373 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17374 do \
17376 int c, face_id; \
17377 XChar2b *char2b; \
17379 c = (row)->glyphs[area][START].u.ch; \
17380 face_id = (row)->glyphs[area][START].face_id; \
17382 s = (struct glyph_string *) alloca (sizeof *s); \
17383 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17384 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17385 append_glyph_string (&HEAD, &TAIL, s); \
17386 s->x = (X); \
17387 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17389 while (0)
17392 /* Add a glyph string for a composite sequence to the list of strings
17393 between HEAD and TAIL. START is the index of the first glyph in
17394 row area AREA of glyph row ROW that is part of the new glyph
17395 string. END is the index of the last glyph in that glyph row area.
17396 X is the current output position assigned to the new glyph string
17397 constructed. HL overrides that face of the glyph; e.g. it is
17398 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17399 x-position of the drawing area. */
17401 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17402 do { \
17403 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17404 int face_id = (row)->glyphs[area][START].face_id; \
17405 struct face *base_face = FACE_FROM_ID (f, face_id); \
17406 struct composition *cmp = composition_table[cmp_id]; \
17407 int glyph_len = cmp->glyph_len; \
17408 XChar2b *char2b; \
17409 struct face **faces; \
17410 struct glyph_string *first_s = NULL; \
17411 int n; \
17413 base_face = base_face->ascii_face; \
17414 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17415 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17416 /* At first, fill in `char2b' and `faces'. */ \
17417 for (n = 0; n < glyph_len; n++) \
17419 int c = COMPOSITION_GLYPH (cmp, n); \
17420 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17421 faces[n] = FACE_FROM_ID (f, this_face_id); \
17422 get_char_face_and_encoding (f, c, this_face_id, \
17423 char2b + n, 1, 1); \
17426 /* Make glyph_strings for each glyph sequence that is drawable by \
17427 the same face, and append them to HEAD/TAIL. */ \
17428 for (n = 0; n < cmp->glyph_len;) \
17430 s = (struct glyph_string *) alloca (sizeof *s); \
17431 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17432 append_glyph_string (&(HEAD), &(TAIL), s); \
17433 s->cmp = cmp; \
17434 s->gidx = n; \
17435 s->x = (X); \
17437 if (n == 0) \
17438 first_s = s; \
17440 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17443 ++START; \
17444 s = first_s; \
17445 } while (0)
17448 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17449 of AREA of glyph row ROW on window W between indices START and END.
17450 HL overrides the face for drawing glyph strings, e.g. it is
17451 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17452 x-positions of the drawing area.
17454 This is an ugly monster macro construct because we must use alloca
17455 to allocate glyph strings (because draw_glyphs can be called
17456 asynchronously). */
17458 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17459 do \
17461 HEAD = TAIL = NULL; \
17462 while (START < END) \
17464 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17465 switch (first_glyph->type) \
17467 case CHAR_GLYPH: \
17468 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17469 HL, X, LAST_X); \
17470 break; \
17472 case COMPOSITE_GLYPH: \
17473 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17474 HL, X, LAST_X); \
17475 break; \
17477 case STRETCH_GLYPH: \
17478 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17479 HL, X, LAST_X); \
17480 break; \
17482 case IMAGE_GLYPH: \
17483 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17484 HL, X, LAST_X); \
17485 break; \
17487 default: \
17488 abort (); \
17491 set_glyph_string_background_width (s, START, LAST_X); \
17492 (X) += s->width; \
17495 while (0)
17498 /* Draw glyphs between START and END in AREA of ROW on window W,
17499 starting at x-position X. X is relative to AREA in W. HL is a
17500 face-override with the following meaning:
17502 DRAW_NORMAL_TEXT draw normally
17503 DRAW_CURSOR draw in cursor face
17504 DRAW_MOUSE_FACE draw in mouse face.
17505 DRAW_INVERSE_VIDEO draw in mode line face
17506 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17507 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17509 If OVERLAPS_P is non-zero, draw only the foreground of characters
17510 and clip to the physical height of ROW.
17512 Value is the x-position reached, relative to AREA of W. */
17514 static int
17515 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17516 struct window *w;
17517 int x;
17518 struct glyph_row *row;
17519 enum glyph_row_area area;
17520 int start, end;
17521 enum draw_glyphs_face hl;
17522 int overlaps_p;
17524 struct glyph_string *head, *tail;
17525 struct glyph_string *s;
17526 int last_x, area_width;
17527 int x_reached;
17528 int i, j;
17529 struct frame *f = XFRAME (WINDOW_FRAME (w));
17530 DECLARE_HDC (hdc);
17532 ALLOCATE_HDC (hdc, f);
17534 /* Let's rather be paranoid than getting a SEGV. */
17535 end = min (end, row->used[area]);
17536 start = max (0, start);
17537 start = min (end, start);
17539 /* Translate X to frame coordinates. Set last_x to the right
17540 end of the drawing area. */
17541 if (row->full_width_p)
17543 /* X is relative to the left edge of W, without scroll bars
17544 or fringes. */
17545 x += WINDOW_LEFT_EDGE_X (w);
17546 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17548 else
17550 int area_left = window_box_left (w, area);
17551 x += area_left;
17552 area_width = window_box_width (w, area);
17553 last_x = area_left + area_width;
17556 /* Build a doubly-linked list of glyph_string structures between
17557 head and tail from what we have to draw. Note that the macro
17558 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17559 the reason we use a separate variable `i'. */
17560 i = start;
17561 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17562 if (tail)
17563 x_reached = tail->x + tail->background_width;
17564 else
17565 x_reached = x;
17567 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17568 the row, redraw some glyphs in front or following the glyph
17569 strings built above. */
17570 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17572 int dummy_x = 0;
17573 struct glyph_string *h, *t;
17575 /* Compute overhangs for all glyph strings. */
17576 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17577 for (s = head; s; s = s->next)
17578 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
17580 /* Prepend glyph strings for glyphs in front of the first glyph
17581 string that are overwritten because of the first glyph
17582 string's left overhang. The background of all strings
17583 prepended must be drawn because the first glyph string
17584 draws over it. */
17585 i = left_overwritten (head);
17586 if (i >= 0)
17588 j = i;
17589 BUILD_GLYPH_STRINGS (j, start, h, t,
17590 DRAW_NORMAL_TEXT, dummy_x, last_x);
17591 start = i;
17592 compute_overhangs_and_x (t, head->x, 1);
17593 prepend_glyph_string_lists (&head, &tail, h, t);
17596 /* Prepend glyph strings for glyphs in front of the first glyph
17597 string that overwrite that glyph string because of their
17598 right overhang. For these strings, only the foreground must
17599 be drawn, because it draws over the glyph string at `head'.
17600 The background must not be drawn because this would overwrite
17601 right overhangs of preceding glyphs for which no glyph
17602 strings exist. */
17603 i = left_overwriting (head);
17604 if (i >= 0)
17606 BUILD_GLYPH_STRINGS (i, start, h, t,
17607 DRAW_NORMAL_TEXT, dummy_x, last_x);
17608 for (s = h; s; s = s->next)
17609 s->background_filled_p = 1;
17610 compute_overhangs_and_x (t, head->x, 1);
17611 prepend_glyph_string_lists (&head, &tail, h, t);
17614 /* Append glyphs strings for glyphs following the last glyph
17615 string tail that are overwritten by tail. The background of
17616 these strings has to be drawn because tail's foreground draws
17617 over it. */
17618 i = right_overwritten (tail);
17619 if (i >= 0)
17621 BUILD_GLYPH_STRINGS (end, i, h, t,
17622 DRAW_NORMAL_TEXT, x, last_x);
17623 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17624 append_glyph_string_lists (&head, &tail, h, t);
17627 /* Append glyph strings for glyphs following the last glyph
17628 string tail that overwrite tail. The foreground of such
17629 glyphs has to be drawn because it writes into the background
17630 of tail. The background must not be drawn because it could
17631 paint over the foreground of following glyphs. */
17632 i = right_overwriting (tail);
17633 if (i >= 0)
17635 BUILD_GLYPH_STRINGS (end, i, h, t,
17636 DRAW_NORMAL_TEXT, x, last_x);
17637 for (s = h; s; s = s->next)
17638 s->background_filled_p = 1;
17639 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17640 append_glyph_string_lists (&head, &tail, h, t);
17644 /* Draw all strings. */
17645 for (s = head; s; s = s->next)
17646 FRAME_RIF (f)->draw_glyph_string (s);
17648 if (area == TEXT_AREA
17649 && !row->full_width_p
17650 /* When drawing overlapping rows, only the glyph strings'
17651 foreground is drawn, which doesn't erase a cursor
17652 completely. */
17653 && !overlaps_p)
17655 int x0 = head ? head->x : x;
17656 int x1 = tail ? tail->x + tail->background_width : x;
17658 int text_left = window_box_left (w, TEXT_AREA);
17659 x0 -= text_left;
17660 x1 -= text_left;
17662 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17663 row->y, MATRIX_ROW_BOTTOM_Y (row));
17666 /* Value is the x-position up to which drawn, relative to AREA of W.
17667 This doesn't include parts drawn because of overhangs. */
17668 if (row->full_width_p)
17669 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17670 else
17671 x_reached -= window_box_left (w, area);
17673 RELEASE_HDC (hdc, f);
17675 return x_reached;
17679 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17680 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17682 static INLINE void
17683 append_glyph (it)
17684 struct it *it;
17686 struct glyph *glyph;
17687 enum glyph_row_area area = it->area;
17689 xassert (it->glyph_row);
17690 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17692 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17693 if (glyph < it->glyph_row->glyphs[area + 1])
17695 glyph->charpos = CHARPOS (it->position);
17696 glyph->object = it->object;
17697 glyph->pixel_width = it->pixel_width;
17698 glyph->ascent = it->ascent;
17699 glyph->descent = it->descent;
17700 glyph->voffset = it->voffset;
17701 glyph->type = CHAR_GLYPH;
17702 glyph->multibyte_p = it->multibyte_p;
17703 glyph->left_box_line_p = it->start_of_box_run_p;
17704 glyph->right_box_line_p = it->end_of_box_run_p;
17705 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17706 || it->phys_descent > it->descent);
17707 glyph->padding_p = 0;
17708 glyph->glyph_not_available_p = it->glyph_not_available_p;
17709 glyph->face_id = it->face_id;
17710 glyph->u.ch = it->char_to_display;
17711 glyph->font_type = FONT_TYPE_UNKNOWN;
17712 ++it->glyph_row->used[area];
17716 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17717 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17719 static INLINE void
17720 append_composite_glyph (it)
17721 struct it *it;
17723 struct glyph *glyph;
17724 enum glyph_row_area area = it->area;
17726 xassert (it->glyph_row);
17728 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17729 if (glyph < it->glyph_row->glyphs[area + 1])
17731 glyph->charpos = CHARPOS (it->position);
17732 glyph->object = it->object;
17733 glyph->pixel_width = it->pixel_width;
17734 glyph->ascent = it->ascent;
17735 glyph->descent = it->descent;
17736 glyph->voffset = it->voffset;
17737 glyph->type = COMPOSITE_GLYPH;
17738 glyph->multibyte_p = it->multibyte_p;
17739 glyph->left_box_line_p = it->start_of_box_run_p;
17740 glyph->right_box_line_p = it->end_of_box_run_p;
17741 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17742 || it->phys_descent > it->descent);
17743 glyph->padding_p = 0;
17744 glyph->glyph_not_available_p = 0;
17745 glyph->face_id = it->face_id;
17746 glyph->u.cmp_id = it->cmp_id;
17747 glyph->font_type = FONT_TYPE_UNKNOWN;
17748 ++it->glyph_row->used[area];
17753 /* Change IT->ascent and IT->height according to the setting of
17754 IT->voffset. */
17756 static INLINE void
17757 take_vertical_position_into_account (it)
17758 struct it *it;
17760 if (it->voffset)
17762 if (it->voffset < 0)
17763 /* Increase the ascent so that we can display the text higher
17764 in the line. */
17765 it->ascent += abs (it->voffset);
17766 else
17767 /* Increase the descent so that we can display the text lower
17768 in the line. */
17769 it->descent += it->voffset;
17774 /* Produce glyphs/get display metrics for the image IT is loaded with.
17775 See the description of struct display_iterator in dispextern.h for
17776 an overview of struct display_iterator. */
17778 static void
17779 produce_image_glyph (it)
17780 struct it *it;
17782 struct image *img;
17783 struct face *face;
17784 int face_ascent, glyph_ascent;
17786 xassert (it->what == IT_IMAGE);
17788 face = FACE_FROM_ID (it->f, it->face_id);
17789 img = IMAGE_FROM_ID (it->f, it->image_id);
17790 xassert (img);
17792 /* Make sure X resources of the face and image are loaded. */
17793 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17794 prepare_image_for_display (it->f, img);
17796 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
17797 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17798 it->pixel_width = img->width + 2 * img->hmargin;
17800 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
17801 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
17802 if (face_ascent > it->ascent)
17803 it->ascent = it->phys_ascent = face_ascent;
17805 it->nglyphs = 1;
17807 if (face->box != FACE_NO_BOX)
17809 if (face->box_line_width > 0)
17811 it->ascent += face->box_line_width;
17812 it->descent += face->box_line_width;
17815 if (it->start_of_box_run_p)
17816 it->pixel_width += abs (face->box_line_width);
17817 if (it->end_of_box_run_p)
17818 it->pixel_width += abs (face->box_line_width);
17821 take_vertical_position_into_account (it);
17823 if (it->glyph_row)
17825 struct glyph *glyph;
17826 enum glyph_row_area area = it->area;
17828 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17829 if (glyph < it->glyph_row->glyphs[area + 1])
17831 glyph->charpos = CHARPOS (it->position);
17832 glyph->object = it->object;
17833 glyph->pixel_width = it->pixel_width;
17834 glyph->ascent = glyph_ascent;
17835 glyph->descent = it->descent;
17836 glyph->voffset = it->voffset;
17837 glyph->type = IMAGE_GLYPH;
17838 glyph->multibyte_p = it->multibyte_p;
17839 glyph->left_box_line_p = it->start_of_box_run_p;
17840 glyph->right_box_line_p = it->end_of_box_run_p;
17841 glyph->overlaps_vertically_p = 0;
17842 glyph->padding_p = 0;
17843 glyph->glyph_not_available_p = 0;
17844 glyph->face_id = it->face_id;
17845 glyph->u.img_id = img->id;
17846 glyph->font_type = FONT_TYPE_UNKNOWN;
17847 ++it->glyph_row->used[area];
17853 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17854 of the glyph, WIDTH and HEIGHT are the width and height of the
17855 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
17857 static void
17858 append_stretch_glyph (it, object, width, height, ascent)
17859 struct it *it;
17860 Lisp_Object object;
17861 int width, height;
17862 int ascent;
17864 struct glyph *glyph;
17865 enum glyph_row_area area = it->area;
17867 xassert (ascent >= 0 && ascent <= height);
17869 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17870 if (glyph < it->glyph_row->glyphs[area + 1])
17872 glyph->charpos = CHARPOS (it->position);
17873 glyph->object = object;
17874 glyph->pixel_width = width;
17875 glyph->ascent = ascent;
17876 glyph->descent = height - ascent;
17877 glyph->voffset = it->voffset;
17878 glyph->type = STRETCH_GLYPH;
17879 glyph->multibyte_p = it->multibyte_p;
17880 glyph->left_box_line_p = it->start_of_box_run_p;
17881 glyph->right_box_line_p = it->end_of_box_run_p;
17882 glyph->overlaps_vertically_p = 0;
17883 glyph->padding_p = 0;
17884 glyph->glyph_not_available_p = 0;
17885 glyph->face_id = it->face_id;
17886 glyph->u.stretch.ascent = ascent;
17887 glyph->u.stretch.height = height;
17888 glyph->font_type = FONT_TYPE_UNKNOWN;
17889 ++it->glyph_row->used[area];
17894 /* Calculate a width or height in pixels from a specification using
17895 the following elements:
17897 SPEC ::=
17898 NUM - a (fractional) multiple of the default font width/height
17899 (NUM) - specifies exactly NUM pixels
17900 UNIT - a fixed number of pixels, see below.
17901 ELEMENT - size of a display element in pixels, see below.
17902 (NUM . SPEC) - equals NUM * SPEC
17903 (+ SPEC SPEC ...) - add pixel values
17904 (- SPEC SPEC ...) - subtract pixel values
17905 (- SPEC) - negate pixel value
17907 NUM ::=
17908 INT or FLOAT - a number constant
17909 SYMBOL - use symbol's (buffer local) variable binding.
17911 UNIT ::=
17912 in - pixels per inch *)
17913 mm - pixels per 1/1000 meter *)
17914 cm - pixels per 1/100 meter *)
17915 width - width of current font in pixels.
17916 height - height of current font in pixels.
17918 *) using the ratio(s) defined in display-pixels-per-inch.
17920 ELEMENT ::=
17922 left-fringe - left fringe width in pixels
17923 (left-fringe . nil) - left fringe width if inside margins, else 0
17924 (left-fringe . t) - left fringe width if outside margins, else 0
17926 right-fringe - right fringe width in pixels
17927 (right-fringe . nil) - right fringe width if inside margins, else 0
17928 (right-fringe . t) - right fringe width if outside margins, else 0
17930 left-margin - left margin width in pixels
17931 right-margin - right margin width in pixels
17933 scroll-bar - scroll-bar area width in pixels
17934 (scroll-bar . left) - scroll-bar width if on left, else 0
17935 (scroll-bar . right) - scroll-bar width if on right, else 0
17937 Examples:
17939 Pixels corresponding to 5 inches:
17940 (5 . in)
17942 Total width of non-text areas on left side of window:
17943 (+ left-fringe left-margin (scroll-bar . left))
17945 Total width of fringes if inside display margins:
17946 (+ (left-fringe) (right-fringe))
17948 Width of left margin minus width of 1 character in the default font:
17949 (- left-margin 1)
17951 Width of left margin minus width of 2 characters in the current font:
17952 (- left-margin (2 . width))
17954 Width of left fringe plus left margin minus one pixel:
17955 (- (+ left-fringe left-margin) (1))
17956 (+ left-fringe left-margin (- (1)))
17957 (+ left-fringe left-margin (-1))
17961 #define NUMVAL(X) \
17962 ((INTEGERP (X) || FLOATP (X)) \
17963 ? XFLOATINT (X) \
17964 : - 1)
17966 static int
17967 calc_pixel_width_or_height (res, it, prop, font, width_p)
17968 double *res;
17969 struct it *it;
17970 Lisp_Object prop;
17971 XFontStruct *font;
17972 int width_p;
17974 double pixels;
17976 #define OK_PIXELS(val) ((*res = (val)), 1)
17978 if (SYMBOLP (prop))
17980 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17982 char *unit = SDATA (SYMBOL_NAME (prop));
17984 if (unit[0] == 'i' && unit[1] == 'n')
17985 pixels = 1.0;
17986 else if (unit[0] == 'm' && unit[1] == 'm')
17987 pixels = 25.4;
17988 else if (unit[0] == 'c' && unit[1] == 'm')
17989 pixels = 2.54;
17990 else
17991 pixels = 0;
17992 if (pixels > 0)
17994 double ppi;
17995 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17996 || (CONSP (Vdisplay_pixels_per_inch)
17997 && (ppi = (width_p
17998 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17999 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18000 ppi > 0)))
18001 return OK_PIXELS (ppi / pixels);
18003 return 0;
18007 if (EQ (prop, Qheight))
18008 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18009 if (EQ (prop, Qwidth))
18010 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18011 if (EQ (prop, Qleft_fringe))
18012 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18013 if (EQ (prop, Qright_fringe))
18014 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18015 if (EQ (prop, Qleft_margin))
18016 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18017 if (EQ (prop, Qright_margin))
18018 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18019 if (EQ (prop, Qscroll_bar))
18020 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18022 prop = Fbuffer_local_value (prop, it->w->buffer);
18025 if (INTEGERP (prop) || FLOATP (prop))
18027 int base_unit = (width_p
18028 ? FRAME_COLUMN_WIDTH (it->f)
18029 : FRAME_LINE_HEIGHT (it->f));
18030 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18033 if (CONSP (prop))
18035 Lisp_Object car = XCAR (prop);
18036 Lisp_Object cdr = XCDR (prop);
18038 if (SYMBOLP (car))
18040 if (EQ (car, Qplus) || EQ (car, Qminus))
18042 int first = 1;
18043 double px;
18045 pixels = 0;
18046 while (CONSP (cdr))
18048 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18049 return 0;
18050 if (first)
18051 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18052 else
18053 pixels += px;
18054 cdr = XCDR (cdr);
18056 if (EQ (car, Qminus))
18057 pixels = -pixels;
18058 return OK_PIXELS (pixels);
18061 if (EQ (car, Qleft_fringe))
18062 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18063 == !NILP (cdr))
18064 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18065 : 0);
18066 if (EQ (car, Qright_fringe))
18067 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18068 == !NILP (cdr))
18069 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18070 : 0);
18071 if (EQ (car, Qscroll_bar))
18072 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18073 == EQ (cdr, Qleft))
18074 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18075 : 0);
18077 car = Fbuffer_local_value (car, it->w->buffer);
18080 if (INTEGERP (car) || FLOATP (car))
18082 double fact;
18083 pixels = XFLOATINT (car);
18084 if (NILP (cdr))
18085 return OK_PIXELS (pixels);
18086 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18087 return OK_PIXELS (pixels * fact);
18088 return 0;
18091 return 0;
18094 return 0;
18097 /* Produce a stretch glyph for iterator IT. IT->object is the value
18098 of the glyph property displayed. The value must be a list
18099 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18100 being recognized:
18102 1. `:width WIDTH' specifies that the space should be WIDTH *
18103 canonical char width wide. WIDTH may be an integer or floating
18104 point number.
18106 2. `:relative-width FACTOR' specifies that the width of the stretch
18107 should be computed from the width of the first character having the
18108 `glyph' property, and should be FACTOR times that width.
18110 3. `:align-to HPOS' specifies that the space should be wide enough
18111 to reach HPOS, a value in canonical character units.
18113 Exactly one of the above pairs must be present.
18115 4. `:height HEIGHT' specifies that the height of the stretch produced
18116 should be HEIGHT, measured in canonical character units.
18118 5. `:relative-height FACTOR' specifies that the height of the
18119 stretch should be FACTOR times the height of the characters having
18120 the glyph property.
18122 Either none or exactly one of 4 or 5 must be present.
18124 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18125 of the stretch should be used for the ascent of the stretch.
18126 ASCENT must be in the range 0 <= ASCENT <= 100. */
18128 static void
18129 produce_stretch_glyph (it)
18130 struct it *it;
18132 /* (space :width WIDTH :height HEIGHT ...) */
18133 Lisp_Object prop, plist;
18134 int width = 0, height = 0;
18135 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18136 int ascent = 0;
18137 double tem;
18138 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18139 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18141 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18143 /* List should start with `space'. */
18144 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18145 plist = XCDR (it->object);
18147 /* Compute the width of the stretch. */
18148 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18149 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18151 /* Absolute width `:width WIDTH' specified and valid. */
18152 zero_width_ok_p = 1;
18153 width = (int)tem;
18155 else if (prop = Fplist_get (plist, QCrelative_width),
18156 NUMVAL (prop) > 0)
18158 /* Relative width `:relative-width FACTOR' specified and valid.
18159 Compute the width of the characters having the `glyph'
18160 property. */
18161 struct it it2;
18162 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18164 it2 = *it;
18165 if (it->multibyte_p)
18167 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18168 - IT_BYTEPOS (*it));
18169 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18171 else
18172 it2.c = *p, it2.len = 1;
18174 it2.glyph_row = NULL;
18175 it2.what = IT_CHARACTER;
18176 x_produce_glyphs (&it2);
18177 width = NUMVAL (prop) * it2.pixel_width;
18179 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18180 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18182 width = max (0, (int)tem - it->current_x);
18183 zero_width_ok_p = 1;
18185 else
18186 /* Nothing specified -> width defaults to canonical char width. */
18187 width = FRAME_COLUMN_WIDTH (it->f);
18189 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18190 width = 1;
18192 /* Compute height. */
18193 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18194 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18196 height = (int)tem;
18197 zero_height_ok_p = 1;
18199 else if (prop = Fplist_get (plist, QCrelative_height),
18200 NUMVAL (prop) > 0)
18201 height = FONT_HEIGHT (font) * NUMVAL (prop);
18202 else
18203 height = FONT_HEIGHT (font);
18205 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18206 height = 1;
18208 /* Compute percentage of height used for ascent. If
18209 `:ascent ASCENT' is present and valid, use that. Otherwise,
18210 derive the ascent from the font in use. */
18211 if (prop = Fplist_get (plist, QCascent),
18212 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18213 ascent = height * NUMVAL (prop) / 100.0;
18214 else if (!NILP (prop)
18215 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18216 ascent = min (max (0, (int)tem), height);
18217 else
18218 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18220 if (width > 0 && height > 0 && it->glyph_row)
18222 Lisp_Object object = it->stack[it->sp - 1].string;
18223 if (!STRINGP (object))
18224 object = it->w->buffer;
18225 append_stretch_glyph (it, object, width, height, ascent);
18228 it->pixel_width = width;
18229 it->ascent = it->phys_ascent = ascent;
18230 it->descent = it->phys_descent = height - it->ascent;
18231 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18233 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18235 if (face->box_line_width > 0)
18237 it->ascent += face->box_line_width;
18238 it->descent += face->box_line_width;
18241 if (it->start_of_box_run_p)
18242 it->pixel_width += abs (face->box_line_width);
18243 if (it->end_of_box_run_p)
18244 it->pixel_width += abs (face->box_line_width);
18247 take_vertical_position_into_account (it);
18250 /* RIF:
18251 Produce glyphs/get display metrics for the display element IT is
18252 loaded with. See the description of struct display_iterator in
18253 dispextern.h for an overview of struct display_iterator. */
18255 void
18256 x_produce_glyphs (it)
18257 struct it *it;
18259 it->glyph_not_available_p = 0;
18261 if (it->what == IT_CHARACTER)
18263 XChar2b char2b;
18264 XFontStruct *font;
18265 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18266 XCharStruct *pcm;
18267 int font_not_found_p;
18268 struct font_info *font_info;
18269 int boff; /* baseline offset */
18270 /* We may change it->multibyte_p upon unibyte<->multibyte
18271 conversion. So, save the current value now and restore it
18272 later.
18274 Note: It seems that we don't have to record multibyte_p in
18275 struct glyph because the character code itself tells if or
18276 not the character is multibyte. Thus, in the future, we must
18277 consider eliminating the field `multibyte_p' in the struct
18278 glyph. */
18279 int saved_multibyte_p = it->multibyte_p;
18281 /* Maybe translate single-byte characters to multibyte, or the
18282 other way. */
18283 it->char_to_display = it->c;
18284 if (!ASCII_BYTE_P (it->c))
18286 if (unibyte_display_via_language_environment
18287 && SINGLE_BYTE_CHAR_P (it->c)
18288 && (it->c >= 0240
18289 || !NILP (Vnonascii_translation_table)))
18291 it->char_to_display = unibyte_char_to_multibyte (it->c);
18292 it->multibyte_p = 1;
18293 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18294 face = FACE_FROM_ID (it->f, it->face_id);
18296 else if (!SINGLE_BYTE_CHAR_P (it->c)
18297 && !it->multibyte_p)
18299 it->multibyte_p = 1;
18300 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18301 face = FACE_FROM_ID (it->f, it->face_id);
18305 /* Get font to use. Encode IT->char_to_display. */
18306 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18307 &char2b, it->multibyte_p, 0);
18308 font = face->font;
18310 /* When no suitable font found, use the default font. */
18311 font_not_found_p = font == NULL;
18312 if (font_not_found_p)
18314 font = FRAME_FONT (it->f);
18315 boff = FRAME_BASELINE_OFFSET (it->f);
18316 font_info = NULL;
18318 else
18320 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18321 boff = font_info->baseline_offset;
18322 if (font_info->vertical_centering)
18323 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18326 if (it->char_to_display >= ' '
18327 && (!it->multibyte_p || it->char_to_display < 128))
18329 /* Either unibyte or ASCII. */
18330 int stretched_p;
18332 it->nglyphs = 1;
18334 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18335 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18336 it->ascent = FONT_BASE (font) + boff;
18337 it->descent = FONT_DESCENT (font) - boff;
18339 if (pcm)
18341 it->phys_ascent = pcm->ascent + boff;
18342 it->phys_descent = pcm->descent - boff;
18343 it->pixel_width = pcm->width;
18345 else
18347 it->glyph_not_available_p = 1;
18348 it->phys_ascent = FONT_BASE (font) + boff;
18349 it->phys_descent = FONT_DESCENT (font) - boff;
18350 it->pixel_width = FONT_WIDTH (font);
18353 /* If this is a space inside a region of text with
18354 `space-width' property, change its width. */
18355 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18356 if (stretched_p)
18357 it->pixel_width *= XFLOATINT (it->space_width);
18359 /* If face has a box, add the box thickness to the character
18360 height. If character has a box line to the left and/or
18361 right, add the box line width to the character's width. */
18362 if (face->box != FACE_NO_BOX)
18364 int thick = face->box_line_width;
18366 if (thick > 0)
18368 it->ascent += thick;
18369 it->descent += thick;
18371 else
18372 thick = -thick;
18374 if (it->start_of_box_run_p)
18375 it->pixel_width += thick;
18376 if (it->end_of_box_run_p)
18377 it->pixel_width += thick;
18380 /* If face has an overline, add the height of the overline
18381 (1 pixel) and a 1 pixel margin to the character height. */
18382 if (face->overline_p)
18383 it->ascent += 2;
18385 take_vertical_position_into_account (it);
18387 /* If we have to actually produce glyphs, do it. */
18388 if (it->glyph_row)
18390 if (stretched_p)
18392 /* Translate a space with a `space-width' property
18393 into a stretch glyph. */
18394 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18395 / FONT_HEIGHT (font));
18396 append_stretch_glyph (it, it->object, it->pixel_width,
18397 it->ascent + it->descent, ascent);
18399 else
18400 append_glyph (it);
18402 /* If characters with lbearing or rbearing are displayed
18403 in this line, record that fact in a flag of the
18404 glyph row. This is used to optimize X output code. */
18405 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18406 it->glyph_row->contains_overlapping_glyphs_p = 1;
18409 else if (it->char_to_display == '\n')
18411 /* A newline has no width but we need the height of the line. */
18412 it->pixel_width = 0;
18413 it->nglyphs = 0;
18414 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18415 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18417 if (face->box != FACE_NO_BOX
18418 && face->box_line_width > 0)
18420 it->ascent += face->box_line_width;
18421 it->descent += face->box_line_width;
18424 else if (it->char_to_display == '\t')
18426 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18427 int x = it->current_x + it->continuation_lines_width;
18428 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18430 /* If the distance from the current position to the next tab
18431 stop is less than a canonical character width, use the
18432 tab stop after that. */
18433 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18434 next_tab_x += tab_width;
18436 it->pixel_width = next_tab_x - x;
18437 it->nglyphs = 1;
18438 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18439 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18441 if (it->glyph_row)
18443 append_stretch_glyph (it, it->object, it->pixel_width,
18444 it->ascent + it->descent, it->ascent);
18447 else
18449 /* A multi-byte character. Assume that the display width of the
18450 character is the width of the character multiplied by the
18451 width of the font. */
18453 /* If we found a font, this font should give us the right
18454 metrics. If we didn't find a font, use the frame's
18455 default font and calculate the width of the character
18456 from the charset width; this is what old redisplay code
18457 did. */
18459 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18460 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18462 if (font_not_found_p || !pcm)
18464 int charset = CHAR_CHARSET (it->char_to_display);
18466 it->glyph_not_available_p = 1;
18467 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18468 * CHARSET_WIDTH (charset));
18469 it->phys_ascent = FONT_BASE (font) + boff;
18470 it->phys_descent = FONT_DESCENT (font) - boff;
18472 else
18474 it->pixel_width = pcm->width;
18475 it->phys_ascent = pcm->ascent + boff;
18476 it->phys_descent = pcm->descent - boff;
18477 if (it->glyph_row
18478 && (pcm->lbearing < 0
18479 || pcm->rbearing > pcm->width))
18480 it->glyph_row->contains_overlapping_glyphs_p = 1;
18482 it->nglyphs = 1;
18483 it->ascent = FONT_BASE (font) + boff;
18484 it->descent = FONT_DESCENT (font) - boff;
18485 if (face->box != FACE_NO_BOX)
18487 int thick = face->box_line_width;
18489 if (thick > 0)
18491 it->ascent += thick;
18492 it->descent += thick;
18494 else
18495 thick = - thick;
18497 if (it->start_of_box_run_p)
18498 it->pixel_width += thick;
18499 if (it->end_of_box_run_p)
18500 it->pixel_width += thick;
18503 /* If face has an overline, add the height of the overline
18504 (1 pixel) and a 1 pixel margin to the character height. */
18505 if (face->overline_p)
18506 it->ascent += 2;
18508 take_vertical_position_into_account (it);
18510 if (it->glyph_row)
18511 append_glyph (it);
18513 it->multibyte_p = saved_multibyte_p;
18515 else if (it->what == IT_COMPOSITION)
18517 /* Note: A composition is represented as one glyph in the
18518 glyph matrix. There are no padding glyphs. */
18519 XChar2b char2b;
18520 XFontStruct *font;
18521 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18522 XCharStruct *pcm;
18523 int font_not_found_p;
18524 struct font_info *font_info;
18525 int boff; /* baseline offset */
18526 struct composition *cmp = composition_table[it->cmp_id];
18528 /* Maybe translate single-byte characters to multibyte. */
18529 it->char_to_display = it->c;
18530 if (unibyte_display_via_language_environment
18531 && SINGLE_BYTE_CHAR_P (it->c)
18532 && (it->c >= 0240
18533 || (it->c >= 0200
18534 && !NILP (Vnonascii_translation_table))))
18536 it->char_to_display = unibyte_char_to_multibyte (it->c);
18539 /* Get face and font to use. Encode IT->char_to_display. */
18540 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18541 face = FACE_FROM_ID (it->f, it->face_id);
18542 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18543 &char2b, it->multibyte_p, 0);
18544 font = face->font;
18546 /* When no suitable font found, use the default font. */
18547 font_not_found_p = font == NULL;
18548 if (font_not_found_p)
18550 font = FRAME_FONT (it->f);
18551 boff = FRAME_BASELINE_OFFSET (it->f);
18552 font_info = NULL;
18554 else
18556 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18557 boff = font_info->baseline_offset;
18558 if (font_info->vertical_centering)
18559 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18562 /* There are no padding glyphs, so there is only one glyph to
18563 produce for the composition. Important is that pixel_width,
18564 ascent and descent are the values of what is drawn by
18565 draw_glyphs (i.e. the values of the overall glyphs composed). */
18566 it->nglyphs = 1;
18568 /* If we have not yet calculated pixel size data of glyphs of
18569 the composition for the current face font, calculate them
18570 now. Theoretically, we have to check all fonts for the
18571 glyphs, but that requires much time and memory space. So,
18572 here we check only the font of the first glyph. This leads
18573 to incorrect display very rarely, and C-l (recenter) can
18574 correct the display anyway. */
18575 if (cmp->font != (void *) font)
18577 /* Ascent and descent of the font of the first character of
18578 this composition (adjusted by baseline offset). Ascent
18579 and descent of overall glyphs should not be less than
18580 them respectively. */
18581 int font_ascent = FONT_BASE (font) + boff;
18582 int font_descent = FONT_DESCENT (font) - boff;
18583 /* Bounding box of the overall glyphs. */
18584 int leftmost, rightmost, lowest, highest;
18585 int i, width, ascent, descent;
18587 cmp->font = (void *) font;
18589 /* Initialize the bounding box. */
18590 if (font_info
18591 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18592 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18594 width = pcm->width;
18595 ascent = pcm->ascent;
18596 descent = pcm->descent;
18598 else
18600 width = FONT_WIDTH (font);
18601 ascent = FONT_BASE (font);
18602 descent = FONT_DESCENT (font);
18605 rightmost = width;
18606 lowest = - descent + boff;
18607 highest = ascent + boff;
18608 leftmost = 0;
18610 if (font_info
18611 && font_info->default_ascent
18612 && CHAR_TABLE_P (Vuse_default_ascent)
18613 && !NILP (Faref (Vuse_default_ascent,
18614 make_number (it->char_to_display))))
18615 highest = font_info->default_ascent + boff;
18617 /* Draw the first glyph at the normal position. It may be
18618 shifted to right later if some other glyphs are drawn at
18619 the left. */
18620 cmp->offsets[0] = 0;
18621 cmp->offsets[1] = boff;
18623 /* Set cmp->offsets for the remaining glyphs. */
18624 for (i = 1; i < cmp->glyph_len; i++)
18626 int left, right, btm, top;
18627 int ch = COMPOSITION_GLYPH (cmp, i);
18628 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18630 face = FACE_FROM_ID (it->f, face_id);
18631 get_char_face_and_encoding (it->f, ch, face->id,
18632 &char2b, it->multibyte_p, 0);
18633 font = face->font;
18634 if (font == NULL)
18636 font = FRAME_FONT (it->f);
18637 boff = FRAME_BASELINE_OFFSET (it->f);
18638 font_info = NULL;
18640 else
18642 font_info
18643 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18644 boff = font_info->baseline_offset;
18645 if (font_info->vertical_centering)
18646 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18649 if (font_info
18650 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18651 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18653 width = pcm->width;
18654 ascent = pcm->ascent;
18655 descent = pcm->descent;
18657 else
18659 width = FONT_WIDTH (font);
18660 ascent = 1;
18661 descent = 0;
18664 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18666 /* Relative composition with or without
18667 alternate chars. */
18668 left = (leftmost + rightmost - width) / 2;
18669 btm = - descent + boff;
18670 if (font_info && font_info->relative_compose
18671 && (! CHAR_TABLE_P (Vignore_relative_composition)
18672 || NILP (Faref (Vignore_relative_composition,
18673 make_number (ch)))))
18676 if (- descent >= font_info->relative_compose)
18677 /* One extra pixel between two glyphs. */
18678 btm = highest + 1;
18679 else if (ascent <= 0)
18680 /* One extra pixel between two glyphs. */
18681 btm = lowest - 1 - ascent - descent;
18684 else
18686 /* A composition rule is specified by an integer
18687 value that encodes global and new reference
18688 points (GREF and NREF). GREF and NREF are
18689 specified by numbers as below:
18691 0---1---2 -- ascent
18695 9--10--11 -- center
18697 ---3---4---5--- baseline
18699 6---7---8 -- descent
18701 int rule = COMPOSITION_RULE (cmp, i);
18702 int gref, nref, grefx, grefy, nrefx, nrefy;
18704 COMPOSITION_DECODE_RULE (rule, gref, nref);
18705 grefx = gref % 3, nrefx = nref % 3;
18706 grefy = gref / 3, nrefy = nref / 3;
18708 left = (leftmost
18709 + grefx * (rightmost - leftmost) / 2
18710 - nrefx * width / 2);
18711 btm = ((grefy == 0 ? highest
18712 : grefy == 1 ? 0
18713 : grefy == 2 ? lowest
18714 : (highest + lowest) / 2)
18715 - (nrefy == 0 ? ascent + descent
18716 : nrefy == 1 ? descent - boff
18717 : nrefy == 2 ? 0
18718 : (ascent + descent) / 2));
18721 cmp->offsets[i * 2] = left;
18722 cmp->offsets[i * 2 + 1] = btm + descent;
18724 /* Update the bounding box of the overall glyphs. */
18725 right = left + width;
18726 top = btm + descent + ascent;
18727 if (left < leftmost)
18728 leftmost = left;
18729 if (right > rightmost)
18730 rightmost = right;
18731 if (top > highest)
18732 highest = top;
18733 if (btm < lowest)
18734 lowest = btm;
18737 /* If there are glyphs whose x-offsets are negative,
18738 shift all glyphs to the right and make all x-offsets
18739 non-negative. */
18740 if (leftmost < 0)
18742 for (i = 0; i < cmp->glyph_len; i++)
18743 cmp->offsets[i * 2] -= leftmost;
18744 rightmost -= leftmost;
18747 cmp->pixel_width = rightmost;
18748 cmp->ascent = highest;
18749 cmp->descent = - lowest;
18750 if (cmp->ascent < font_ascent)
18751 cmp->ascent = font_ascent;
18752 if (cmp->descent < font_descent)
18753 cmp->descent = font_descent;
18756 it->pixel_width = cmp->pixel_width;
18757 it->ascent = it->phys_ascent = cmp->ascent;
18758 it->descent = it->phys_descent = cmp->descent;
18760 if (face->box != FACE_NO_BOX)
18762 int thick = face->box_line_width;
18764 if (thick > 0)
18766 it->ascent += thick;
18767 it->descent += thick;
18769 else
18770 thick = - thick;
18772 if (it->start_of_box_run_p)
18773 it->pixel_width += thick;
18774 if (it->end_of_box_run_p)
18775 it->pixel_width += thick;
18778 /* If face has an overline, add the height of the overline
18779 (1 pixel) and a 1 pixel margin to the character height. */
18780 if (face->overline_p)
18781 it->ascent += 2;
18783 take_vertical_position_into_account (it);
18785 if (it->glyph_row)
18786 append_composite_glyph (it);
18788 else if (it->what == IT_IMAGE)
18789 produce_image_glyph (it);
18790 else if (it->what == IT_STRETCH)
18791 produce_stretch_glyph (it);
18793 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18794 because this isn't true for images with `:ascent 100'. */
18795 xassert (it->ascent >= 0 && it->descent >= 0);
18796 if (it->area == TEXT_AREA)
18797 it->current_x += it->pixel_width;
18799 it->descent += it->extra_line_spacing;
18801 it->max_ascent = max (it->max_ascent, it->ascent);
18802 it->max_descent = max (it->max_descent, it->descent);
18803 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18804 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18807 /* EXPORT for RIF:
18808 Output LEN glyphs starting at START at the nominal cursor position.
18809 Advance the nominal cursor over the text. The global variable
18810 updated_window contains the window being updated, updated_row is
18811 the glyph row being updated, and updated_area is the area of that
18812 row being updated. */
18814 void
18815 x_write_glyphs (start, len)
18816 struct glyph *start;
18817 int len;
18819 int x, hpos;
18821 xassert (updated_window && updated_row);
18822 BLOCK_INPUT;
18824 /* Write glyphs. */
18826 hpos = start - updated_row->glyphs[updated_area];
18827 x = draw_glyphs (updated_window, output_cursor.x,
18828 updated_row, updated_area,
18829 hpos, hpos + len,
18830 DRAW_NORMAL_TEXT, 0);
18832 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18833 if (updated_area == TEXT_AREA
18834 && updated_window->phys_cursor_on_p
18835 && updated_window->phys_cursor.vpos == output_cursor.vpos
18836 && updated_window->phys_cursor.hpos >= hpos
18837 && updated_window->phys_cursor.hpos < hpos + len)
18838 updated_window->phys_cursor_on_p = 0;
18840 UNBLOCK_INPUT;
18842 /* Advance the output cursor. */
18843 output_cursor.hpos += len;
18844 output_cursor.x = x;
18848 /* EXPORT for RIF:
18849 Insert LEN glyphs from START at the nominal cursor position. */
18851 void
18852 x_insert_glyphs (start, len)
18853 struct glyph *start;
18854 int len;
18856 struct frame *f;
18857 struct window *w;
18858 int line_height, shift_by_width, shifted_region_width;
18859 struct glyph_row *row;
18860 struct glyph *glyph;
18861 int frame_x, frame_y, hpos;
18863 xassert (updated_window && updated_row);
18864 BLOCK_INPUT;
18865 w = updated_window;
18866 f = XFRAME (WINDOW_FRAME (w));
18868 /* Get the height of the line we are in. */
18869 row = updated_row;
18870 line_height = row->height;
18872 /* Get the width of the glyphs to insert. */
18873 shift_by_width = 0;
18874 for (glyph = start; glyph < start + len; ++glyph)
18875 shift_by_width += glyph->pixel_width;
18877 /* Get the width of the region to shift right. */
18878 shifted_region_width = (window_box_width (w, updated_area)
18879 - output_cursor.x
18880 - shift_by_width);
18882 /* Shift right. */
18883 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18884 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18886 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18887 line_height, shift_by_width);
18889 /* Write the glyphs. */
18890 hpos = start - row->glyphs[updated_area];
18891 draw_glyphs (w, output_cursor.x, row, updated_area,
18892 hpos, hpos + len,
18893 DRAW_NORMAL_TEXT, 0);
18895 /* Advance the output cursor. */
18896 output_cursor.hpos += len;
18897 output_cursor.x += shift_by_width;
18898 UNBLOCK_INPUT;
18902 /* EXPORT for RIF:
18903 Erase the current text line from the nominal cursor position
18904 (inclusive) to pixel column TO_X (exclusive). The idea is that
18905 everything from TO_X onward is already erased.
18907 TO_X is a pixel position relative to updated_area of
18908 updated_window. TO_X == -1 means clear to the end of this area. */
18910 void
18911 x_clear_end_of_line (to_x)
18912 int to_x;
18914 struct frame *f;
18915 struct window *w = updated_window;
18916 int max_x, min_y, max_y;
18917 int from_x, from_y, to_y;
18919 xassert (updated_window && updated_row);
18920 f = XFRAME (w->frame);
18922 if (updated_row->full_width_p)
18923 max_x = WINDOW_TOTAL_WIDTH (w);
18924 else
18925 max_x = window_box_width (w, updated_area);
18926 max_y = window_text_bottom_y (w);
18928 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18929 of window. For TO_X > 0, truncate to end of drawing area. */
18930 if (to_x == 0)
18931 return;
18932 else if (to_x < 0)
18933 to_x = max_x;
18934 else
18935 to_x = min (to_x, max_x);
18937 to_y = min (max_y, output_cursor.y + updated_row->height);
18939 /* Notice if the cursor will be cleared by this operation. */
18940 if (!updated_row->full_width_p)
18941 notice_overwritten_cursor (w, updated_area,
18942 output_cursor.x, -1,
18943 updated_row->y,
18944 MATRIX_ROW_BOTTOM_Y (updated_row));
18946 from_x = output_cursor.x;
18948 /* Translate to frame coordinates. */
18949 if (updated_row->full_width_p)
18951 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18952 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18954 else
18956 int area_left = window_box_left (w, updated_area);
18957 from_x += area_left;
18958 to_x += area_left;
18961 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18962 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18963 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18965 /* Prevent inadvertently clearing to end of the X window. */
18966 if (to_x > from_x && to_y > from_y)
18968 BLOCK_INPUT;
18969 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
18970 to_x - from_x, to_y - from_y);
18971 UNBLOCK_INPUT;
18975 #endif /* HAVE_WINDOW_SYSTEM */
18979 /***********************************************************************
18980 Cursor types
18981 ***********************************************************************/
18983 /* Value is the internal representation of the specified cursor type
18984 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18985 of the bar cursor. */
18987 static enum text_cursor_kinds
18988 get_specified_cursor_type (arg, width)
18989 Lisp_Object arg;
18990 int *width;
18992 enum text_cursor_kinds type;
18994 if (NILP (arg))
18995 return NO_CURSOR;
18997 if (EQ (arg, Qbox))
18998 return FILLED_BOX_CURSOR;
19000 if (EQ (arg, Qhollow))
19001 return HOLLOW_BOX_CURSOR;
19003 if (EQ (arg, Qbar))
19005 *width = 2;
19006 return BAR_CURSOR;
19009 if (CONSP (arg)
19010 && EQ (XCAR (arg), Qbar)
19011 && INTEGERP (XCDR (arg))
19012 && XINT (XCDR (arg)) >= 0)
19014 *width = XINT (XCDR (arg));
19015 return BAR_CURSOR;
19018 if (EQ (arg, Qhbar))
19020 *width = 2;
19021 return HBAR_CURSOR;
19024 if (CONSP (arg)
19025 && EQ (XCAR (arg), Qhbar)
19026 && INTEGERP (XCDR (arg))
19027 && XINT (XCDR (arg)) >= 0)
19029 *width = XINT (XCDR (arg));
19030 return HBAR_CURSOR;
19033 /* Treat anything unknown as "hollow box cursor".
19034 It was bad to signal an error; people have trouble fixing
19035 .Xdefaults with Emacs, when it has something bad in it. */
19036 type = HOLLOW_BOX_CURSOR;
19038 return type;
19041 /* Set the default cursor types for specified frame. */
19042 void
19043 set_frame_cursor_types (f, arg)
19044 struct frame *f;
19045 Lisp_Object arg;
19047 int width;
19048 Lisp_Object tem;
19050 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19051 FRAME_CURSOR_WIDTH (f) = width;
19053 /* By default, set up the blink-off state depending on the on-state. */
19055 tem = Fassoc (arg, Vblink_cursor_alist);
19056 if (!NILP (tem))
19058 FRAME_BLINK_OFF_CURSOR (f)
19059 = get_specified_cursor_type (XCDR (tem), &width);
19060 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19062 else
19063 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19067 /* Return the cursor we want to be displayed in window W. Return
19068 width of bar/hbar cursor through WIDTH arg. Return with
19069 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19070 (i.e. if the `system caret' should track this cursor).
19072 In a mini-buffer window, we want the cursor only to appear if we
19073 are reading input from this window. For the selected window, we
19074 want the cursor type given by the frame parameter or buffer local
19075 setting of cursor-type. If explicitly marked off, draw no cursor.
19076 In all other cases, we want a hollow box cursor. */
19078 static enum text_cursor_kinds
19079 get_window_cursor_type (w, glyph, width, active_cursor)
19080 struct window *w;
19081 struct glyph *glyph;
19082 int *width;
19083 int *active_cursor;
19085 struct frame *f = XFRAME (w->frame);
19086 struct buffer *b = XBUFFER (w->buffer);
19087 int cursor_type = DEFAULT_CURSOR;
19088 Lisp_Object alt_cursor;
19089 int non_selected = 0;
19091 *active_cursor = 1;
19093 /* Echo area */
19094 if (cursor_in_echo_area
19095 && FRAME_HAS_MINIBUF_P (f)
19096 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19098 if (w == XWINDOW (echo_area_window))
19100 *width = FRAME_CURSOR_WIDTH (f);
19101 return FRAME_DESIRED_CURSOR (f);
19104 *active_cursor = 0;
19105 non_selected = 1;
19108 /* Nonselected window or nonselected frame. */
19109 else if (w != XWINDOW (f->selected_window)
19110 #ifdef HAVE_WINDOW_SYSTEM
19111 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19112 #endif
19115 *active_cursor = 0;
19117 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19118 return NO_CURSOR;
19120 non_selected = 1;
19123 /* Never display a cursor in a window in which cursor-type is nil. */
19124 if (NILP (b->cursor_type))
19125 return NO_CURSOR;
19127 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19128 if (non_selected)
19130 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19131 return get_specified_cursor_type (alt_cursor, width);
19134 /* Get the normal cursor type for this window. */
19135 if (EQ (b->cursor_type, Qt))
19137 cursor_type = FRAME_DESIRED_CURSOR (f);
19138 *width = FRAME_CURSOR_WIDTH (f);
19140 else
19141 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19143 /* Use normal cursor if not blinked off. */
19144 if (!w->cursor_off_p)
19146 if (glyph->type == IMAGE_GLYPH) {
19147 if (cursor_type == FILLED_BOX_CURSOR)
19148 cursor_type = HOLLOW_BOX_CURSOR;
19150 return cursor_type;
19153 /* Cursor is blinked off, so determine how to "toggle" it. */
19155 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19156 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19157 return get_specified_cursor_type (XCDR (alt_cursor), width);
19159 /* Then see if frame has specified a specific blink off cursor type. */
19160 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19162 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19163 return FRAME_BLINK_OFF_CURSOR (f);
19166 #if 0
19167 /* Some people liked having a permanently visible blinking cursor,
19168 while others had very strong opinions against it. So it was
19169 decided to remove it. KFS 2003-09-03 */
19171 /* Finally perform built-in cursor blinking:
19172 filled box <-> hollow box
19173 wide [h]bar <-> narrow [h]bar
19174 narrow [h]bar <-> no cursor
19175 other type <-> no cursor */
19177 if (cursor_type == FILLED_BOX_CURSOR)
19178 return HOLLOW_BOX_CURSOR;
19180 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19182 *width = 1;
19183 return cursor_type;
19185 #endif
19187 return NO_CURSOR;
19191 #ifdef HAVE_WINDOW_SYSTEM
19193 /* Notice when the text cursor of window W has been completely
19194 overwritten by a drawing operation that outputs glyphs in AREA
19195 starting at X0 and ending at X1 in the line starting at Y0 and
19196 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19197 the rest of the line after X0 has been written. Y coordinates
19198 are window-relative. */
19200 static void
19201 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19202 struct window *w;
19203 enum glyph_row_area area;
19204 int x0, y0, x1, y1;
19206 if (area == TEXT_AREA && w->phys_cursor_on_p)
19208 int cx0 = w->phys_cursor.x;
19209 int cx1 = cx0 + w->phys_cursor_width;
19210 int cy0 = w->phys_cursor.y;
19211 int cy1 = cy0 + w->phys_cursor_height;
19213 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
19215 /* The cursor image will be completely removed from the
19216 screen if the output area intersects the cursor area in
19217 y-direction. When we draw in [y0 y1[, and some part of
19218 the cursor is at y < y0, that part must have been drawn
19219 before. When scrolling, the cursor is erased before
19220 actually scrolling, so we don't come here. When not
19221 scrolling, the rows above the old cursor row must have
19222 changed, and in this case these rows must have written
19223 over the cursor image.
19225 Likewise if part of the cursor is below y1, with the
19226 exception of the cursor being in the first blank row at
19227 the buffer and window end because update_text_area
19228 doesn't draw that row. (Except when it does, but
19229 that's handled in update_text_area.) */
19231 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
19232 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
19233 w->phys_cursor_on_p = 0;
19238 #endif /* HAVE_WINDOW_SYSTEM */
19241 /************************************************************************
19242 Mouse Face
19243 ************************************************************************/
19245 #ifdef HAVE_WINDOW_SYSTEM
19247 /* EXPORT for RIF:
19248 Fix the display of area AREA of overlapping row ROW in window W. */
19250 void
19251 x_fix_overlapping_area (w, row, area)
19252 struct window *w;
19253 struct glyph_row *row;
19254 enum glyph_row_area area;
19256 int i, x;
19258 BLOCK_INPUT;
19260 x = 0;
19261 for (i = 0; i < row->used[area];)
19263 if (row->glyphs[area][i].overlaps_vertically_p)
19265 int start = i, start_x = x;
19269 x += row->glyphs[area][i].pixel_width;
19270 ++i;
19272 while (i < row->used[area]
19273 && row->glyphs[area][i].overlaps_vertically_p);
19275 draw_glyphs (w, start_x, row, area,
19276 start, i,
19277 DRAW_NORMAL_TEXT, 1);
19279 else
19281 x += row->glyphs[area][i].pixel_width;
19282 ++i;
19286 UNBLOCK_INPUT;
19290 /* EXPORT:
19291 Draw the cursor glyph of window W in glyph row ROW. See the
19292 comment of draw_glyphs for the meaning of HL. */
19294 void
19295 draw_phys_cursor_glyph (w, row, hl)
19296 struct window *w;
19297 struct glyph_row *row;
19298 enum draw_glyphs_face hl;
19300 /* If cursor hpos is out of bounds, don't draw garbage. This can
19301 happen in mini-buffer windows when switching between echo area
19302 glyphs and mini-buffer. */
19303 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19305 int on_p = w->phys_cursor_on_p;
19306 int x1;
19307 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19308 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19309 hl, 0);
19310 w->phys_cursor_on_p = on_p;
19312 if (hl == DRAW_CURSOR)
19313 w->phys_cursor_width = x1 - w->phys_cursor.x;
19314 /* When we erase the cursor, and ROW is overlapped by other
19315 rows, make sure that these overlapping parts of other rows
19316 are redrawn. */
19317 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19319 if (row > w->current_matrix->rows
19320 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19321 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19323 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19324 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19325 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19331 /* EXPORT:
19332 Erase the image of a cursor of window W from the screen. */
19334 void
19335 erase_phys_cursor (w)
19336 struct window *w;
19338 struct frame *f = XFRAME (w->frame);
19339 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19340 int hpos = w->phys_cursor.hpos;
19341 int vpos = w->phys_cursor.vpos;
19342 int mouse_face_here_p = 0;
19343 struct glyph_matrix *active_glyphs = w->current_matrix;
19344 struct glyph_row *cursor_row;
19345 struct glyph *cursor_glyph;
19346 enum draw_glyphs_face hl;
19348 /* No cursor displayed or row invalidated => nothing to do on the
19349 screen. */
19350 if (w->phys_cursor_type == NO_CURSOR)
19351 goto mark_cursor_off;
19353 /* VPOS >= active_glyphs->nrows means that window has been resized.
19354 Don't bother to erase the cursor. */
19355 if (vpos >= active_glyphs->nrows)
19356 goto mark_cursor_off;
19358 /* If row containing cursor is marked invalid, there is nothing we
19359 can do. */
19360 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19361 if (!cursor_row->enabled_p)
19362 goto mark_cursor_off;
19364 /* If row is completely invisible, don't attempt to delete a cursor which
19365 isn't there. This can happen if cursor is at top of a window, and
19366 we switch to a buffer with a header line in that window. */
19367 if (cursor_row->visible_height <= 0)
19368 goto mark_cursor_off;
19370 /* This can happen when the new row is shorter than the old one.
19371 In this case, either draw_glyphs or clear_end_of_line
19372 should have cleared the cursor. Note that we wouldn't be
19373 able to erase the cursor in this case because we don't have a
19374 cursor glyph at hand. */
19375 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19376 goto mark_cursor_off;
19378 /* If the cursor is in the mouse face area, redisplay that when
19379 we clear the cursor. */
19380 if (! NILP (dpyinfo->mouse_face_window)
19381 && w == XWINDOW (dpyinfo->mouse_face_window)
19382 && (vpos > dpyinfo->mouse_face_beg_row
19383 || (vpos == dpyinfo->mouse_face_beg_row
19384 && hpos >= dpyinfo->mouse_face_beg_col))
19385 && (vpos < dpyinfo->mouse_face_end_row
19386 || (vpos == dpyinfo->mouse_face_end_row
19387 && hpos < dpyinfo->mouse_face_end_col))
19388 /* Don't redraw the cursor's spot in mouse face if it is at the
19389 end of a line (on a newline). The cursor appears there, but
19390 mouse highlighting does not. */
19391 && cursor_row->used[TEXT_AREA] > hpos)
19392 mouse_face_here_p = 1;
19394 /* Maybe clear the display under the cursor. */
19395 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19397 int x, y;
19398 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19400 cursor_glyph = get_phys_cursor_glyph (w);
19401 if (cursor_glyph == NULL)
19402 goto mark_cursor_off;
19404 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19405 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19407 FRAME_RIF (f)->clear_frame_area (f, x, y,
19408 cursor_glyph->pixel_width, cursor_row->visible_height);
19411 /* Erase the cursor by redrawing the character underneath it. */
19412 if (mouse_face_here_p)
19413 hl = DRAW_MOUSE_FACE;
19414 else
19415 hl = DRAW_NORMAL_TEXT;
19416 draw_phys_cursor_glyph (w, cursor_row, hl);
19418 mark_cursor_off:
19419 w->phys_cursor_on_p = 0;
19420 w->phys_cursor_type = NO_CURSOR;
19424 /* EXPORT:
19425 Display or clear cursor of window W. If ON is zero, clear the
19426 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19427 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19429 void
19430 display_and_set_cursor (w, on, hpos, vpos, x, y)
19431 struct window *w;
19432 int on, hpos, vpos, x, y;
19434 struct frame *f = XFRAME (w->frame);
19435 int new_cursor_type;
19436 int new_cursor_width;
19437 int active_cursor;
19438 struct glyph_matrix *current_glyphs;
19439 struct glyph_row *glyph_row;
19440 struct glyph *glyph;
19442 /* This is pointless on invisible frames, and dangerous on garbaged
19443 windows and frames; in the latter case, the frame or window may
19444 be in the midst of changing its size, and x and y may be off the
19445 window. */
19446 if (! FRAME_VISIBLE_P (f)
19447 || FRAME_GARBAGED_P (f)
19448 || vpos >= w->current_matrix->nrows
19449 || hpos >= w->current_matrix->matrix_w)
19450 return;
19452 /* If cursor is off and we want it off, return quickly. */
19453 if (!on && !w->phys_cursor_on_p)
19454 return;
19456 current_glyphs = w->current_matrix;
19457 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19458 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19460 /* If cursor row is not enabled, we don't really know where to
19461 display the cursor. */
19462 if (!glyph_row->enabled_p)
19464 w->phys_cursor_on_p = 0;
19465 return;
19468 xassert (interrupt_input_blocked);
19470 /* Set new_cursor_type to the cursor we want to be displayed. */
19471 new_cursor_type = get_window_cursor_type (w, glyph,
19472 &new_cursor_width, &active_cursor);
19474 /* If cursor is currently being shown and we don't want it to be or
19475 it is in the wrong place, or the cursor type is not what we want,
19476 erase it. */
19477 if (w->phys_cursor_on_p
19478 && (!on
19479 || w->phys_cursor.x != x
19480 || w->phys_cursor.y != y
19481 || new_cursor_type != w->phys_cursor_type
19482 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19483 && new_cursor_width != w->phys_cursor_width)))
19484 erase_phys_cursor (w);
19486 /* Don't check phys_cursor_on_p here because that flag is only set
19487 to zero in some cases where we know that the cursor has been
19488 completely erased, to avoid the extra work of erasing the cursor
19489 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19490 still not be visible, or it has only been partly erased. */
19491 if (on)
19493 w->phys_cursor_ascent = glyph_row->ascent;
19494 w->phys_cursor_height = glyph_row->height;
19496 /* Set phys_cursor_.* before x_draw_.* is called because some
19497 of them may need the information. */
19498 w->phys_cursor.x = x;
19499 w->phys_cursor.y = glyph_row->y;
19500 w->phys_cursor.hpos = hpos;
19501 w->phys_cursor.vpos = vpos;
19504 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19505 new_cursor_type, new_cursor_width,
19506 on, active_cursor);
19510 /* Switch the display of W's cursor on or off, according to the value
19511 of ON. */
19513 static void
19514 update_window_cursor (w, on)
19515 struct window *w;
19516 int on;
19518 /* Don't update cursor in windows whose frame is in the process
19519 of being deleted. */
19520 if (w->current_matrix)
19522 BLOCK_INPUT;
19523 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19524 w->phys_cursor.x, w->phys_cursor.y);
19525 UNBLOCK_INPUT;
19530 /* Call update_window_cursor with parameter ON_P on all leaf windows
19531 in the window tree rooted at W. */
19533 static void
19534 update_cursor_in_window_tree (w, on_p)
19535 struct window *w;
19536 int on_p;
19538 while (w)
19540 if (!NILP (w->hchild))
19541 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19542 else if (!NILP (w->vchild))
19543 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19544 else
19545 update_window_cursor (w, on_p);
19547 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19552 /* EXPORT:
19553 Display the cursor on window W, or clear it, according to ON_P.
19554 Don't change the cursor's position. */
19556 void
19557 x_update_cursor (f, on_p)
19558 struct frame *f;
19559 int on_p;
19561 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19565 /* EXPORT:
19566 Clear the cursor of window W to background color, and mark the
19567 cursor as not shown. This is used when the text where the cursor
19568 is is about to be rewritten. */
19570 void
19571 x_clear_cursor (w)
19572 struct window *w;
19574 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19575 update_window_cursor (w, 0);
19579 /* EXPORT:
19580 Display the active region described by mouse_face_* according to DRAW. */
19582 void
19583 show_mouse_face (dpyinfo, draw)
19584 Display_Info *dpyinfo;
19585 enum draw_glyphs_face draw;
19587 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19588 struct frame *f = XFRAME (WINDOW_FRAME (w));
19590 if (/* If window is in the process of being destroyed, don't bother
19591 to do anything. */
19592 w->current_matrix != NULL
19593 /* Don't update mouse highlight if hidden */
19594 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19595 /* Recognize when we are called to operate on rows that don't exist
19596 anymore. This can happen when a window is split. */
19597 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19599 int phys_cursor_on_p = w->phys_cursor_on_p;
19600 struct glyph_row *row, *first, *last;
19602 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19603 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19605 for (row = first; row <= last && row->enabled_p; ++row)
19607 int start_hpos, end_hpos, start_x;
19609 /* For all but the first row, the highlight starts at column 0. */
19610 if (row == first)
19612 start_hpos = dpyinfo->mouse_face_beg_col;
19613 start_x = dpyinfo->mouse_face_beg_x;
19615 else
19617 start_hpos = 0;
19618 start_x = 0;
19621 if (row == last)
19622 end_hpos = dpyinfo->mouse_face_end_col;
19623 else
19624 end_hpos = row->used[TEXT_AREA];
19626 if (end_hpos > start_hpos)
19628 draw_glyphs (w, start_x, row, TEXT_AREA,
19629 start_hpos, end_hpos,
19630 draw, 0);
19632 row->mouse_face_p
19633 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19637 /* When we've written over the cursor, arrange for it to
19638 be displayed again. */
19639 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19641 BLOCK_INPUT;
19642 display_and_set_cursor (w, 1,
19643 w->phys_cursor.hpos, w->phys_cursor.vpos,
19644 w->phys_cursor.x, w->phys_cursor.y);
19645 UNBLOCK_INPUT;
19649 /* Change the mouse cursor. */
19650 if (draw == DRAW_NORMAL_TEXT)
19651 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19652 else if (draw == DRAW_MOUSE_FACE)
19653 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19654 else
19655 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19658 /* EXPORT:
19659 Clear out the mouse-highlighted active region.
19660 Redraw it un-highlighted first. Value is non-zero if mouse
19661 face was actually drawn unhighlighted. */
19664 clear_mouse_face (dpyinfo)
19665 Display_Info *dpyinfo;
19667 int cleared = 0;
19669 if (!NILP (dpyinfo->mouse_face_window))
19671 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19672 cleared = 1;
19675 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19676 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19677 dpyinfo->mouse_face_window = Qnil;
19678 dpyinfo->mouse_face_overlay = Qnil;
19679 return cleared;
19683 /* EXPORT:
19684 Non-zero if physical cursor of window W is within mouse face. */
19687 cursor_in_mouse_face_p (w)
19688 struct window *w;
19690 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19691 int in_mouse_face = 0;
19693 if (WINDOWP (dpyinfo->mouse_face_window)
19694 && XWINDOW (dpyinfo->mouse_face_window) == w)
19696 int hpos = w->phys_cursor.hpos;
19697 int vpos = w->phys_cursor.vpos;
19699 if (vpos >= dpyinfo->mouse_face_beg_row
19700 && vpos <= dpyinfo->mouse_face_end_row
19701 && (vpos > dpyinfo->mouse_face_beg_row
19702 || hpos >= dpyinfo->mouse_face_beg_col)
19703 && (vpos < dpyinfo->mouse_face_end_row
19704 || hpos < dpyinfo->mouse_face_end_col
19705 || dpyinfo->mouse_face_past_end))
19706 in_mouse_face = 1;
19709 return in_mouse_face;
19715 /* Find the glyph matrix position of buffer position CHARPOS in window
19716 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19717 current glyphs must be up to date. If CHARPOS is above window
19718 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19719 of last line in W. In the row containing CHARPOS, stop before glyphs
19720 having STOP as object. */
19722 #if 1 /* This is a version of fast_find_position that's more correct
19723 in the presence of hscrolling, for example. I didn't install
19724 it right away because the problem fixed is minor, it failed
19725 in 20.x as well, and I think it's too risky to install
19726 so near the release of 21.1. 2001-09-25 gerd. */
19728 static int
19729 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19730 struct window *w;
19731 int charpos;
19732 int *hpos, *vpos, *x, *y;
19733 Lisp_Object stop;
19735 struct glyph_row *row, *first;
19736 struct glyph *glyph, *end;
19737 int past_end = 0;
19739 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19740 row = row_containing_pos (w, charpos, first, NULL, 0);
19741 if (row == NULL)
19743 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19745 *x = *y = *hpos = *vpos = 0;
19746 return 0;
19748 else
19750 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19751 past_end = 1;
19755 *x = row->x;
19756 *y = row->y;
19757 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19759 glyph = row->glyphs[TEXT_AREA];
19760 end = glyph + row->used[TEXT_AREA];
19762 /* Skip over glyphs not having an object at the start of the row.
19763 These are special glyphs like truncation marks on terminal
19764 frames. */
19765 if (row->displays_text_p)
19766 while (glyph < end
19767 && INTEGERP (glyph->object)
19768 && !EQ (stop, glyph->object)
19769 && glyph->charpos < 0)
19771 *x += glyph->pixel_width;
19772 ++glyph;
19775 while (glyph < end
19776 && !INTEGERP (glyph->object)
19777 && !EQ (stop, glyph->object)
19778 && (!BUFFERP (glyph->object)
19779 || glyph->charpos < charpos))
19781 *x += glyph->pixel_width;
19782 ++glyph;
19785 *hpos = glyph - row->glyphs[TEXT_AREA];
19786 return past_end;
19789 #else /* not 1 */
19791 static int
19792 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19793 struct window *w;
19794 int pos;
19795 int *hpos, *vpos, *x, *y;
19796 Lisp_Object stop;
19798 int i;
19799 int lastcol;
19800 int maybe_next_line_p = 0;
19801 int line_start_position;
19802 int yb = window_text_bottom_y (w);
19803 struct glyph_row *row, *best_row;
19804 int row_vpos, best_row_vpos;
19805 int current_x;
19807 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19808 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19810 while (row->y < yb)
19812 if (row->used[TEXT_AREA])
19813 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19814 else
19815 line_start_position = 0;
19817 if (line_start_position > pos)
19818 break;
19819 /* If the position sought is the end of the buffer,
19820 don't include the blank lines at the bottom of the window. */
19821 else if (line_start_position == pos
19822 && pos == BUF_ZV (XBUFFER (w->buffer)))
19824 maybe_next_line_p = 1;
19825 break;
19827 else if (line_start_position > 0)
19829 best_row = row;
19830 best_row_vpos = row_vpos;
19833 if (row->y + row->height >= yb)
19834 break;
19836 ++row;
19837 ++row_vpos;
19840 /* Find the right column within BEST_ROW. */
19841 lastcol = 0;
19842 current_x = best_row->x;
19843 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19845 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19846 int charpos = glyph->charpos;
19848 if (BUFFERP (glyph->object))
19850 if (charpos == pos)
19852 *hpos = i;
19853 *vpos = best_row_vpos;
19854 *x = current_x;
19855 *y = best_row->y;
19856 return 1;
19858 else if (charpos > pos)
19859 break;
19861 else if (EQ (glyph->object, stop))
19862 break;
19864 if (charpos > 0)
19865 lastcol = i;
19866 current_x += glyph->pixel_width;
19869 /* If we're looking for the end of the buffer,
19870 and we didn't find it in the line we scanned,
19871 use the start of the following line. */
19872 if (maybe_next_line_p)
19874 ++best_row;
19875 ++best_row_vpos;
19876 lastcol = 0;
19877 current_x = best_row->x;
19880 *vpos = best_row_vpos;
19881 *hpos = lastcol + 1;
19882 *x = current_x;
19883 *y = best_row->y;
19884 return 0;
19887 #endif /* not 1 */
19890 /* Find the position of the glyph for position POS in OBJECT in
19891 window W's current matrix, and return in *X, *Y the pixel
19892 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19894 RIGHT_P non-zero means return the position of the right edge of the
19895 glyph, RIGHT_P zero means return the left edge position.
19897 If no glyph for POS exists in the matrix, return the position of
19898 the glyph with the next smaller position that is in the matrix, if
19899 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19900 exists in the matrix, return the position of the glyph with the
19901 next larger position in OBJECT.
19903 Value is non-zero if a glyph was found. */
19905 static int
19906 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19907 struct window *w;
19908 int pos;
19909 Lisp_Object object;
19910 int *hpos, *vpos, *x, *y;
19911 int right_p;
19913 int yb = window_text_bottom_y (w);
19914 struct glyph_row *r;
19915 struct glyph *best_glyph = NULL;
19916 struct glyph_row *best_row = NULL;
19917 int best_x = 0;
19919 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19920 r->enabled_p && r->y < yb;
19921 ++r)
19923 struct glyph *g = r->glyphs[TEXT_AREA];
19924 struct glyph *e = g + r->used[TEXT_AREA];
19925 int gx;
19927 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19928 if (EQ (g->object, object))
19930 if (g->charpos == pos)
19932 best_glyph = g;
19933 best_x = gx;
19934 best_row = r;
19935 goto found;
19937 else if (best_glyph == NULL
19938 || ((abs (g->charpos - pos)
19939 < abs (best_glyph->charpos - pos))
19940 && (right_p
19941 ? g->charpos < pos
19942 : g->charpos > pos)))
19944 best_glyph = g;
19945 best_x = gx;
19946 best_row = r;
19951 found:
19953 if (best_glyph)
19955 *x = best_x;
19956 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19958 if (right_p)
19960 *x += best_glyph->pixel_width;
19961 ++*hpos;
19964 *y = best_row->y;
19965 *vpos = best_row - w->current_matrix->rows;
19968 return best_glyph != NULL;
19972 /* See if position X, Y is within a hot-spot of an image. */
19974 static int
19975 on_hot_spot_p (hot_spot, x, y)
19976 Lisp_Object hot_spot;
19977 int x, y;
19979 if (!CONSP (hot_spot))
19980 return 0;
19982 if (EQ (XCAR (hot_spot), Qrect))
19984 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
19985 Lisp_Object rect = XCDR (hot_spot);
19986 Lisp_Object tem;
19987 if (!CONSP (rect))
19988 return 0;
19989 if (!CONSP (XCAR (rect)))
19990 return 0;
19991 if (!CONSP (XCDR (rect)))
19992 return 0;
19993 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
19994 return 0;
19995 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
19996 return 0;
19997 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
19998 return 0;
19999 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20000 return 0;
20001 return 1;
20003 else if (EQ (XCAR (hot_spot), Qcircle))
20005 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20006 Lisp_Object circ = XCDR (hot_spot);
20007 Lisp_Object lr, lx0, ly0;
20008 if (CONSP (circ)
20009 && CONSP (XCAR (circ))
20010 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20011 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20012 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20014 double r = XFLOATINT (lr);
20015 double dx = XINT (lx0) - x;
20016 double dy = XINT (ly0) - y;
20017 return (dx * dx + dy * dy <= r * r);
20020 else if (EQ (XCAR (hot_spot), Qpoly))
20022 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20023 if (VECTORP (XCDR (hot_spot)))
20025 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20026 Lisp_Object *poly = v->contents;
20027 int n = v->size;
20028 int i;
20029 int inside = 0;
20030 Lisp_Object lx, ly;
20031 int x0, y0;
20033 /* Need an even number of coordinates, and at least 3 edges. */
20034 if (n < 6 || n & 1)
20035 return 0;
20037 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20038 If count is odd, we are inside polygon. Pixels on edges
20039 may or may not be included depending on actual geometry of the
20040 polygon. */
20041 if ((lx = poly[n-2], !INTEGERP (lx))
20042 || (ly = poly[n-1], !INTEGERP (lx)))
20043 return 0;
20044 x0 = XINT (lx), y0 = XINT (ly);
20045 for (i = 0; i < n; i += 2)
20047 int x1 = x0, y1 = y0;
20048 if ((lx = poly[i], !INTEGERP (lx))
20049 || (ly = poly[i+1], !INTEGERP (ly)))
20050 return 0;
20051 x0 = XINT (lx), y0 = XINT (ly);
20053 /* Does this segment cross the X line? */
20054 if (x0 >= x)
20056 if (x1 >= x)
20057 continue;
20059 else if (x1 < x)
20060 continue;
20061 if (y > y0 && y > y1)
20062 continue;
20063 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20064 inside = !inside;
20066 return inside;
20069 else
20070 return 0;
20073 Lisp_Object
20074 find_hot_spot (map, x, y)
20075 Lisp_Object map;
20076 int x, y;
20078 while (CONSP (map))
20080 if (CONSP (XCAR (map))
20081 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20082 return XCAR (map);
20083 map = XCDR (map);
20086 return Qnil;
20089 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20090 3, 3, 0,
20091 doc: /* Lookup in image map MAP coordinates X and Y.
20092 An image map is an alist where each element has the format (AREA ID PLIST).
20093 An AREA is specified as either a rectangle, a circle, or a polygon:
20094 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20095 pixel coordinates of the upper left and bottom right corners.
20096 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20097 and the radius of the circle; r may be a float or integer.
20098 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20099 vector describes one corner in the polygon.
20100 Returns the alist element for the first matching AREA in MAP. */)
20101 (map, x, y)
20102 Lisp_Object map;
20103 Lisp_Object x, y;
20105 int ix, iy;
20106 if (NILP (map))
20107 return Qnil;
20109 if (!INTEGERP (x))
20110 wrong_type_argument (Qintegerp, x);
20111 if (!INTEGERP (y))
20112 wrong_type_argument (Qintegerp, y);
20114 return find_hot_spot (map, XINT (x), XINT (y));
20118 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20119 static void
20120 define_frame_cursor1 (f, cursor, pointer)
20121 struct frame *f;
20122 Cursor cursor;
20123 Lisp_Object pointer;
20125 if (!NILP (pointer))
20127 if (EQ (pointer, Qarrow))
20128 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20129 else if (EQ (pointer, Qhand))
20130 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20131 else if (EQ (pointer, Qtext))
20132 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20133 else if (EQ (pointer, intern ("hdrag")))
20134 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20135 #ifdef HAVE_X_WINDOWS
20136 else if (EQ (pointer, intern ("vdrag")))
20137 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20138 #endif
20139 else if (EQ (pointer, intern ("hourglass")))
20140 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20141 else if (EQ (pointer, Qmodeline))
20142 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20143 else
20144 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20147 #ifndef HAVE_CARBON
20148 if (cursor != No_Cursor)
20149 #else
20150 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20151 #endif
20152 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20155 /* Take proper action when mouse has moved to the mode or header line
20156 or marginal area AREA of window W, x-position X and y-position Y.
20157 X is relative to the start of the text display area of W, so the
20158 width of bitmap areas and scroll bars must be subtracted to get a
20159 position relative to the start of the mode line. */
20161 static void
20162 note_mode_line_or_margin_highlight (w, x, y, area)
20163 struct window *w;
20164 int x, y;
20165 enum window_part area;
20167 struct frame *f = XFRAME (w->frame);
20168 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20169 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20170 Lisp_Object pointer = Qnil;
20171 int charpos, dx, dy;
20172 Lisp_Object string;
20173 Lisp_Object pos, help, image;
20175 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20176 string = mode_line_string (w, &x, &y, 0, 0, area, &charpos);
20177 else
20179 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20180 string = marginal_area_string (w, &x, &y, &dx, &dy, area, &charpos);
20183 help = Qnil;
20185 if (STRINGP (string))
20187 pos = make_number (charpos);
20188 /* If we're on a string with `help-echo' text property, arrange
20189 for the help to be displayed. This is done by setting the
20190 global variable help_echo_string to the help string. */
20191 help = Fget_text_property (pos, Qhelp_echo, string);
20192 if (!NILP (help))
20194 help_echo_string = help;
20195 XSETWINDOW (help_echo_window, w);
20196 help_echo_object = string;
20197 help_echo_pos = charpos;
20200 if (NILP (pointer))
20201 pointer = Fget_text_property (pos, Qpointer, string);
20203 /* Change the mouse pointer according to what is under X/Y. */
20204 if (NILP (pointer) && area == ON_MODE_LINE)
20206 Lisp_Object map;
20207 map = Fget_text_property (pos, Qlocal_map, string);
20208 if (!KEYMAPP (map))
20209 map = Fget_text_property (pos, Qkeymap, string);
20210 if (!KEYMAPP (map))
20211 cursor = dpyinfo->vertical_scroll_bar_cursor;
20214 else if (IMAGEP (string))
20216 Lisp_Object image_map, hotspot;
20217 if ((image_map = Fplist_get (XCDR (string), QCmap),
20218 !NILP (image_map))
20219 && (hotspot = find_hot_spot (image_map, dx, dy),
20220 CONSP (hotspot))
20221 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20223 Lisp_Object area_id, plist;
20225 area_id = XCAR (hotspot);
20226 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20227 If so, we could look for mouse-enter, mouse-leave
20228 properties in PLIST (and do something...). */
20229 if ((plist = XCDR (hotspot), CONSP (plist)))
20231 pointer = Fplist_get (plist, Qpointer);
20232 if (NILP (pointer))
20233 pointer = Qhand;
20234 help = Fplist_get (plist, Qhelp_echo);
20235 if (!NILP (help))
20237 help_echo_string = help;
20238 /* Is this correct? ++kfs */
20239 XSETWINDOW (help_echo_window, w);
20240 help_echo_object = w->buffer;
20241 help_echo_pos = charpos;
20244 if (NILP (pointer))
20245 pointer = Fplist_get (XCDR (string), QCpointer);
20249 define_frame_cursor1 (f, cursor, pointer);
20253 /* EXPORT:
20254 Take proper action when the mouse has moved to position X, Y on
20255 frame F as regards highlighting characters that have mouse-face
20256 properties. Also de-highlighting chars where the mouse was before.
20257 X and Y can be negative or out of range. */
20259 void
20260 note_mouse_highlight (f, x, y)
20261 struct frame *f;
20262 int x, y;
20264 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20265 enum window_part part;
20266 Lisp_Object window;
20267 struct window *w;
20268 Cursor cursor = No_Cursor;
20269 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20270 struct buffer *b;
20272 /* When a menu is active, don't highlight because this looks odd. */
20273 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20274 if (popup_activated ())
20275 return;
20276 #endif
20278 if (NILP (Vmouse_highlight)
20279 || !f->glyphs_initialized_p)
20280 return;
20282 dpyinfo->mouse_face_mouse_x = x;
20283 dpyinfo->mouse_face_mouse_y = y;
20284 dpyinfo->mouse_face_mouse_frame = f;
20286 if (dpyinfo->mouse_face_defer)
20287 return;
20289 if (gc_in_progress)
20291 dpyinfo->mouse_face_deferred_gc = 1;
20292 return;
20295 /* Which window is that in? */
20296 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20298 /* If we were displaying active text in another window, clear that. */
20299 if (! EQ (window, dpyinfo->mouse_face_window))
20300 clear_mouse_face (dpyinfo);
20302 /* Not on a window -> return. */
20303 if (!WINDOWP (window))
20304 return;
20306 /* Reset help_echo_string. It will get recomputed below. */
20307 help_echo_string = Qnil;
20309 /* Convert to window-relative pixel coordinates. */
20310 w = XWINDOW (window);
20311 frame_to_window_pixel_xy (w, &x, &y);
20313 /* Handle tool-bar window differently since it doesn't display a
20314 buffer. */
20315 if (EQ (window, f->tool_bar_window))
20317 note_tool_bar_highlight (f, x, y);
20318 return;
20321 /* Mouse is on the mode, header line or margin? */
20322 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20323 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20325 note_mode_line_or_margin_highlight (w, x, y, part);
20326 return;
20329 if (part == ON_VERTICAL_BORDER)
20330 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20331 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20332 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20333 else
20334 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20336 /* Are we in a window whose display is up to date?
20337 And verify the buffer's text has not changed. */
20338 b = XBUFFER (w->buffer);
20339 if (part == ON_TEXT
20340 && EQ (w->window_end_valid, w->buffer)
20341 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20342 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20344 int hpos, vpos, pos, i, dx, dy, area;
20345 struct glyph *glyph;
20346 Lisp_Object object;
20347 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20348 Lisp_Object *overlay_vec = NULL;
20349 int len, noverlays;
20350 struct buffer *obuf;
20351 int obegv, ozv, same_region;
20353 /* Find the glyph under X/Y. */
20354 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20356 /* Look for :pointer property on image. */
20357 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20359 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20360 if (img != NULL && IMAGEP (img->spec))
20362 Lisp_Object image_map, hotspot;
20363 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20364 !NILP (image_map))
20365 && (hotspot = find_hot_spot (image_map, dx, dy),
20366 CONSP (hotspot))
20367 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20369 Lisp_Object area_id, plist;
20371 area_id = XCAR (hotspot);
20372 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20373 If so, we could look for mouse-enter, mouse-leave
20374 properties in PLIST (and do something...). */
20375 if ((plist = XCDR (hotspot), CONSP (plist)))
20377 pointer = Fplist_get (plist, Qpointer);
20378 if (NILP (pointer))
20379 pointer = Qhand;
20380 help_echo_string = Fplist_get (plist, Qhelp_echo);
20381 if (!NILP (help_echo_string))
20383 help_echo_window = window;
20384 help_echo_object = glyph->object;
20385 help_echo_pos = glyph->charpos;
20389 if (NILP (pointer))
20390 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20394 /* Clear mouse face if X/Y not over text. */
20395 if (glyph == NULL
20396 || area != TEXT_AREA
20397 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20399 if (clear_mouse_face (dpyinfo))
20400 cursor = No_Cursor;
20401 if (NILP (pointer))
20403 if (area != TEXT_AREA)
20404 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20405 else
20406 pointer = Vvoid_text_area_pointer;
20408 goto set_cursor;
20411 pos = glyph->charpos;
20412 object = glyph->object;
20413 if (!STRINGP (object) && !BUFFERP (object))
20414 goto set_cursor;
20416 /* If we get an out-of-range value, return now; avoid an error. */
20417 if (BUFFERP (object) && pos > BUF_Z (b))
20418 goto set_cursor;
20420 /* Make the window's buffer temporarily current for
20421 overlays_at and compute_char_face. */
20422 obuf = current_buffer;
20423 current_buffer = b;
20424 obegv = BEGV;
20425 ozv = ZV;
20426 BEGV = BEG;
20427 ZV = Z;
20429 /* Is this char mouse-active or does it have help-echo? */
20430 position = make_number (pos);
20432 if (BUFFERP (object))
20434 /* Put all the overlays we want in a vector in overlay_vec.
20435 Store the length in len. If there are more than 10, make
20436 enough space for all, and try again. */
20437 len = 10;
20438 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20439 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20440 if (noverlays > len)
20442 len = noverlays;
20443 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20444 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20447 /* Sort overlays into increasing priority order. */
20448 noverlays = sort_overlays (overlay_vec, noverlays, w);
20450 else
20451 noverlays = 0;
20453 same_region = (EQ (window, dpyinfo->mouse_face_window)
20454 && vpos >= dpyinfo->mouse_face_beg_row
20455 && vpos <= dpyinfo->mouse_face_end_row
20456 && (vpos > dpyinfo->mouse_face_beg_row
20457 || hpos >= dpyinfo->mouse_face_beg_col)
20458 && (vpos < dpyinfo->mouse_face_end_row
20459 || hpos < dpyinfo->mouse_face_end_col
20460 || dpyinfo->mouse_face_past_end));
20462 if (same_region)
20463 cursor = No_Cursor;
20465 /* Check mouse-face highlighting. */
20466 if (! same_region
20467 /* If there exists an overlay with mouse-face overlapping
20468 the one we are currently highlighting, we have to
20469 check if we enter the overlapping overlay, and then
20470 highlight only that. */
20471 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20472 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20474 /* Find the highest priority overlay that has a mouse-face
20475 property. */
20476 overlay = Qnil;
20477 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20479 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20480 if (!NILP (mouse_face))
20481 overlay = overlay_vec[i];
20484 /* If we're actually highlighting the same overlay as
20485 before, there's no need to do that again. */
20486 if (!NILP (overlay)
20487 && EQ (overlay, dpyinfo->mouse_face_overlay))
20488 goto check_help_echo;
20490 dpyinfo->mouse_face_overlay = overlay;
20492 /* Clear the display of the old active region, if any. */
20493 if (clear_mouse_face (dpyinfo))
20494 cursor = No_Cursor;
20496 /* If no overlay applies, get a text property. */
20497 if (NILP (overlay))
20498 mouse_face = Fget_text_property (position, Qmouse_face, object);
20500 /* Handle the overlay case. */
20501 if (!NILP (overlay))
20503 /* Find the range of text around this char that
20504 should be active. */
20505 Lisp_Object before, after;
20506 int ignore;
20508 before = Foverlay_start (overlay);
20509 after = Foverlay_end (overlay);
20510 /* Record this as the current active region. */
20511 fast_find_position (w, XFASTINT (before),
20512 &dpyinfo->mouse_face_beg_col,
20513 &dpyinfo->mouse_face_beg_row,
20514 &dpyinfo->mouse_face_beg_x,
20515 &dpyinfo->mouse_face_beg_y, Qnil);
20517 dpyinfo->mouse_face_past_end
20518 = !fast_find_position (w, XFASTINT (after),
20519 &dpyinfo->mouse_face_end_col,
20520 &dpyinfo->mouse_face_end_row,
20521 &dpyinfo->mouse_face_end_x,
20522 &dpyinfo->mouse_face_end_y, Qnil);
20523 dpyinfo->mouse_face_window = window;
20525 dpyinfo->mouse_face_face_id
20526 = face_at_buffer_position (w, pos, 0, 0,
20527 &ignore, pos + 1,
20528 !dpyinfo->mouse_face_hidden);
20530 /* Display it as active. */
20531 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20532 cursor = No_Cursor;
20534 /* Handle the text property case. */
20535 else if (!NILP (mouse_face) && BUFFERP (object))
20537 /* Find the range of text around this char that
20538 should be active. */
20539 Lisp_Object before, after, beginning, end;
20540 int ignore;
20542 beginning = Fmarker_position (w->start);
20543 end = make_number (BUF_Z (XBUFFER (object))
20544 - XFASTINT (w->window_end_pos));
20545 before
20546 = Fprevious_single_property_change (make_number (pos + 1),
20547 Qmouse_face,
20548 object, beginning);
20549 after
20550 = Fnext_single_property_change (position, Qmouse_face,
20551 object, end);
20553 /* Record this as the current active region. */
20554 fast_find_position (w, XFASTINT (before),
20555 &dpyinfo->mouse_face_beg_col,
20556 &dpyinfo->mouse_face_beg_row,
20557 &dpyinfo->mouse_face_beg_x,
20558 &dpyinfo->mouse_face_beg_y, Qnil);
20559 dpyinfo->mouse_face_past_end
20560 = !fast_find_position (w, XFASTINT (after),
20561 &dpyinfo->mouse_face_end_col,
20562 &dpyinfo->mouse_face_end_row,
20563 &dpyinfo->mouse_face_end_x,
20564 &dpyinfo->mouse_face_end_y, Qnil);
20565 dpyinfo->mouse_face_window = window;
20567 if (BUFFERP (object))
20568 dpyinfo->mouse_face_face_id
20569 = face_at_buffer_position (w, pos, 0, 0,
20570 &ignore, pos + 1,
20571 !dpyinfo->mouse_face_hidden);
20573 /* Display it as active. */
20574 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20575 cursor = No_Cursor;
20577 else if (!NILP (mouse_face) && STRINGP (object))
20579 Lisp_Object b, e;
20580 int ignore;
20582 b = Fprevious_single_property_change (make_number (pos + 1),
20583 Qmouse_face,
20584 object, Qnil);
20585 e = Fnext_single_property_change (position, Qmouse_face,
20586 object, Qnil);
20587 if (NILP (b))
20588 b = make_number (0);
20589 if (NILP (e))
20590 e = make_number (SCHARS (object) - 1);
20591 fast_find_string_pos (w, XINT (b), object,
20592 &dpyinfo->mouse_face_beg_col,
20593 &dpyinfo->mouse_face_beg_row,
20594 &dpyinfo->mouse_face_beg_x,
20595 &dpyinfo->mouse_face_beg_y, 0);
20596 fast_find_string_pos (w, XINT (e), object,
20597 &dpyinfo->mouse_face_end_col,
20598 &dpyinfo->mouse_face_end_row,
20599 &dpyinfo->mouse_face_end_x,
20600 &dpyinfo->mouse_face_end_y, 1);
20601 dpyinfo->mouse_face_past_end = 0;
20602 dpyinfo->mouse_face_window = window;
20603 dpyinfo->mouse_face_face_id
20604 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20605 glyph->face_id, 1);
20606 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20607 cursor = No_Cursor;
20609 else if (STRINGP (object) && NILP (mouse_face))
20611 /* A string which doesn't have mouse-face, but
20612 the text ``under'' it might have. */
20613 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20614 int start = MATRIX_ROW_START_CHARPOS (r);
20616 pos = string_buffer_position (w, object, start);
20617 if (pos > 0)
20618 mouse_face = get_char_property_and_overlay (make_number (pos),
20619 Qmouse_face,
20620 w->buffer,
20621 &overlay);
20622 if (!NILP (mouse_face) && !NILP (overlay))
20624 Lisp_Object before = Foverlay_start (overlay);
20625 Lisp_Object after = Foverlay_end (overlay);
20626 int ignore;
20628 /* Note that we might not be able to find position
20629 BEFORE in the glyph matrix if the overlay is
20630 entirely covered by a `display' property. In
20631 this case, we overshoot. So let's stop in
20632 the glyph matrix before glyphs for OBJECT. */
20633 fast_find_position (w, XFASTINT (before),
20634 &dpyinfo->mouse_face_beg_col,
20635 &dpyinfo->mouse_face_beg_row,
20636 &dpyinfo->mouse_face_beg_x,
20637 &dpyinfo->mouse_face_beg_y,
20638 object);
20640 dpyinfo->mouse_face_past_end
20641 = !fast_find_position (w, XFASTINT (after),
20642 &dpyinfo->mouse_face_end_col,
20643 &dpyinfo->mouse_face_end_row,
20644 &dpyinfo->mouse_face_end_x,
20645 &dpyinfo->mouse_face_end_y,
20646 Qnil);
20647 dpyinfo->mouse_face_window = window;
20648 dpyinfo->mouse_face_face_id
20649 = face_at_buffer_position (w, pos, 0, 0,
20650 &ignore, pos + 1,
20651 !dpyinfo->mouse_face_hidden);
20653 /* Display it as active. */
20654 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20655 cursor = No_Cursor;
20660 check_help_echo:
20662 /* Look for a `help-echo' property. */
20663 if (NILP (help_echo_string)) {
20664 Lisp_Object help, overlay;
20666 /* Check overlays first. */
20667 help = overlay = Qnil;
20668 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20670 overlay = overlay_vec[i];
20671 help = Foverlay_get (overlay, Qhelp_echo);
20674 if (!NILP (help))
20676 help_echo_string = help;
20677 help_echo_window = window;
20678 help_echo_object = overlay;
20679 help_echo_pos = pos;
20681 else
20683 Lisp_Object object = glyph->object;
20684 int charpos = glyph->charpos;
20686 /* Try text properties. */
20687 if (STRINGP (object)
20688 && charpos >= 0
20689 && charpos < SCHARS (object))
20691 help = Fget_text_property (make_number (charpos),
20692 Qhelp_echo, object);
20693 if (NILP (help))
20695 /* If the string itself doesn't specify a help-echo,
20696 see if the buffer text ``under'' it does. */
20697 struct glyph_row *r
20698 = MATRIX_ROW (w->current_matrix, vpos);
20699 int start = MATRIX_ROW_START_CHARPOS (r);
20700 int pos = string_buffer_position (w, object, start);
20701 if (pos > 0)
20703 help = Fget_char_property (make_number (pos),
20704 Qhelp_echo, w->buffer);
20705 if (!NILP (help))
20707 charpos = pos;
20708 object = w->buffer;
20713 else if (BUFFERP (object)
20714 && charpos >= BEGV
20715 && charpos < ZV)
20716 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20717 object);
20719 if (!NILP (help))
20721 help_echo_string = help;
20722 help_echo_window = window;
20723 help_echo_object = object;
20724 help_echo_pos = charpos;
20729 /* Look for a `pointer' property. */
20730 if (NILP (pointer))
20732 /* Check overlays first. */
20733 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
20734 pointer = Foverlay_get (overlay_vec[i], Qpointer);
20736 if (NILP (pointer))
20738 Lisp_Object object = glyph->object;
20739 int charpos = glyph->charpos;
20741 /* Try text properties. */
20742 if (STRINGP (object)
20743 && charpos >= 0
20744 && charpos < SCHARS (object))
20746 pointer = Fget_text_property (make_number (charpos),
20747 Qpointer, object);
20748 if (NILP (pointer))
20750 /* If the string itself doesn't specify a pointer,
20751 see if the buffer text ``under'' it does. */
20752 struct glyph_row *r
20753 = MATRIX_ROW (w->current_matrix, vpos);
20754 int start = MATRIX_ROW_START_CHARPOS (r);
20755 int pos = string_buffer_position (w, object, start);
20756 if (pos > 0)
20757 pointer = Fget_char_property (make_number (pos),
20758 Qpointer, w->buffer);
20761 else if (BUFFERP (object)
20762 && charpos >= BEGV
20763 && charpos < ZV)
20764 pointer = Fget_text_property (make_number (charpos),
20765 Qpointer, object);
20769 BEGV = obegv;
20770 ZV = ozv;
20771 current_buffer = obuf;
20774 set_cursor:
20776 define_frame_cursor1 (f, cursor, pointer);
20780 /* EXPORT for RIF:
20781 Clear any mouse-face on window W. This function is part of the
20782 redisplay interface, and is called from try_window_id and similar
20783 functions to ensure the mouse-highlight is off. */
20785 void
20786 x_clear_window_mouse_face (w)
20787 struct window *w;
20789 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20790 Lisp_Object window;
20792 BLOCK_INPUT;
20793 XSETWINDOW (window, w);
20794 if (EQ (window, dpyinfo->mouse_face_window))
20795 clear_mouse_face (dpyinfo);
20796 UNBLOCK_INPUT;
20800 /* EXPORT:
20801 Just discard the mouse face information for frame F, if any.
20802 This is used when the size of F is changed. */
20804 void
20805 cancel_mouse_face (f)
20806 struct frame *f;
20808 Lisp_Object window;
20809 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20811 window = dpyinfo->mouse_face_window;
20812 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20814 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20815 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20816 dpyinfo->mouse_face_window = Qnil;
20821 #endif /* HAVE_WINDOW_SYSTEM */
20824 /***********************************************************************
20825 Exposure Events
20826 ***********************************************************************/
20828 #ifdef HAVE_WINDOW_SYSTEM
20830 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20831 which intersects rectangle R. R is in window-relative coordinates. */
20833 static void
20834 expose_area (w, row, r, area)
20835 struct window *w;
20836 struct glyph_row *row;
20837 XRectangle *r;
20838 enum glyph_row_area area;
20840 struct glyph *first = row->glyphs[area];
20841 struct glyph *end = row->glyphs[area] + row->used[area];
20842 struct glyph *last;
20843 int first_x, start_x, x;
20845 if (area == TEXT_AREA && row->fill_line_p)
20846 /* If row extends face to end of line write the whole line. */
20847 draw_glyphs (w, 0, row, area,
20848 0, row->used[area],
20849 DRAW_NORMAL_TEXT, 0);
20850 else
20852 /* Set START_X to the window-relative start position for drawing glyphs of
20853 AREA. The first glyph of the text area can be partially visible.
20854 The first glyphs of other areas cannot. */
20855 start_x = window_box_left_offset (w, area);
20856 x = start_x;
20857 if (area == TEXT_AREA)
20858 x += row->x;
20860 /* Find the first glyph that must be redrawn. */
20861 while (first < end
20862 && x + first->pixel_width < r->x)
20864 x += first->pixel_width;
20865 ++first;
20868 /* Find the last one. */
20869 last = first;
20870 first_x = x;
20871 while (last < end
20872 && x < r->x + r->width)
20874 x += last->pixel_width;
20875 ++last;
20878 /* Repaint. */
20879 if (last > first)
20880 draw_glyphs (w, first_x - start_x, row, area,
20881 first - row->glyphs[area], last - row->glyphs[area],
20882 DRAW_NORMAL_TEXT, 0);
20887 /* Redraw the parts of the glyph row ROW on window W intersecting
20888 rectangle R. R is in window-relative coordinates. Value is
20889 non-zero if mouse-face was overwritten. */
20891 static int
20892 expose_line (w, row, r)
20893 struct window *w;
20894 struct glyph_row *row;
20895 XRectangle *r;
20897 xassert (row->enabled_p);
20899 if (row->mode_line_p || w->pseudo_window_p)
20900 draw_glyphs (w, 0, row, TEXT_AREA,
20901 0, row->used[TEXT_AREA],
20902 DRAW_NORMAL_TEXT, 0);
20903 else
20905 if (row->used[LEFT_MARGIN_AREA])
20906 expose_area (w, row, r, LEFT_MARGIN_AREA);
20907 if (row->used[TEXT_AREA])
20908 expose_area (w, row, r, TEXT_AREA);
20909 if (row->used[RIGHT_MARGIN_AREA])
20910 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20911 draw_row_fringe_bitmaps (w, row);
20914 return row->mouse_face_p;
20918 /* Redraw those parts of glyphs rows during expose event handling that
20919 overlap other rows. Redrawing of an exposed line writes over parts
20920 of lines overlapping that exposed line; this function fixes that.
20922 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20923 row in W's current matrix that is exposed and overlaps other rows.
20924 LAST_OVERLAPPING_ROW is the last such row. */
20926 static void
20927 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20928 struct window *w;
20929 struct glyph_row *first_overlapping_row;
20930 struct glyph_row *last_overlapping_row;
20932 struct glyph_row *row;
20934 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20935 if (row->overlapping_p)
20937 xassert (row->enabled_p && !row->mode_line_p);
20939 if (row->used[LEFT_MARGIN_AREA])
20940 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20942 if (row->used[TEXT_AREA])
20943 x_fix_overlapping_area (w, row, TEXT_AREA);
20945 if (row->used[RIGHT_MARGIN_AREA])
20946 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20951 /* Return non-zero if W's cursor intersects rectangle R. */
20953 static int
20954 phys_cursor_in_rect_p (w, r)
20955 struct window *w;
20956 XRectangle *r;
20958 XRectangle cr, result;
20959 struct glyph *cursor_glyph;
20961 cursor_glyph = get_phys_cursor_glyph (w);
20962 if (cursor_glyph)
20964 /* r is relative to W's box, but w->phys_cursor.x is relative
20965 to left edge of W's TEXT area. Adjust it. */
20966 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
20967 cr.y = w->phys_cursor.y;
20968 cr.width = cursor_glyph->pixel_width;
20969 cr.height = w->phys_cursor_height;
20970 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20971 I assume the effect is the same -- and this is portable. */
20972 return x_intersect_rectangles (&cr, r, &result);
20974 else
20975 return 0;
20979 /* EXPORT:
20980 Draw a vertical window border to the right of window W if W doesn't
20981 have vertical scroll bars. */
20983 void
20984 x_draw_vertical_border (w)
20985 struct window *w;
20987 struct frame *f = XFRAME (WINDOW_FRAME (w));
20989 /* We could do better, if we knew what type of scroll-bar the adjacent
20990 windows (on either side) have... But we don't :-(
20991 However, I think this works ok. ++KFS 2003-04-25 */
20993 /* Redraw borders between horizontally adjacent windows. Don't
20994 do it for frames with vertical scroll bars because either the
20995 right scroll bar of a window, or the left scroll bar of its
20996 neighbor will suffice as a border. */
20997 if (!WINDOW_RIGHTMOST_P (w)
20998 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21000 int x0, x1, y0, y1;
21002 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21003 y1 -= 1;
21005 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21007 else if (!WINDOW_LEFTMOST_P (w)
21008 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21010 int x0, x1, y0, y1;
21012 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21013 y1 -= 1;
21015 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21020 /* Redraw the part of window W intersection rectangle FR. Pixel
21021 coordinates in FR are frame-relative. Call this function with
21022 input blocked. Value is non-zero if the exposure overwrites
21023 mouse-face. */
21025 static int
21026 expose_window (w, fr)
21027 struct window *w;
21028 XRectangle *fr;
21030 struct frame *f = XFRAME (w->frame);
21031 XRectangle wr, r;
21032 int mouse_face_overwritten_p = 0;
21034 /* If window is not yet fully initialized, do nothing. This can
21035 happen when toolkit scroll bars are used and a window is split.
21036 Reconfiguring the scroll bar will generate an expose for a newly
21037 created window. */
21038 if (w->current_matrix == NULL)
21039 return 0;
21041 /* When we're currently updating the window, display and current
21042 matrix usually don't agree. Arrange for a thorough display
21043 later. */
21044 if (w == updated_window)
21046 SET_FRAME_GARBAGED (f);
21047 return 0;
21050 /* Frame-relative pixel rectangle of W. */
21051 wr.x = WINDOW_LEFT_EDGE_X (w);
21052 wr.y = WINDOW_TOP_EDGE_Y (w);
21053 wr.width = WINDOW_TOTAL_WIDTH (w);
21054 wr.height = WINDOW_TOTAL_HEIGHT (w);
21056 if (x_intersect_rectangles (fr, &wr, &r))
21058 int yb = window_text_bottom_y (w);
21059 struct glyph_row *row;
21060 int cursor_cleared_p;
21061 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21063 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21064 r.x, r.y, r.width, r.height));
21066 /* Convert to window coordinates. */
21067 r.x -= WINDOW_LEFT_EDGE_X (w);
21068 r.y -= WINDOW_TOP_EDGE_Y (w);
21070 /* Turn off the cursor. */
21071 if (!w->pseudo_window_p
21072 && phys_cursor_in_rect_p (w, &r))
21074 x_clear_cursor (w);
21075 cursor_cleared_p = 1;
21077 else
21078 cursor_cleared_p = 0;
21080 /* Update lines intersecting rectangle R. */
21081 first_overlapping_row = last_overlapping_row = NULL;
21082 for (row = w->current_matrix->rows;
21083 row->enabled_p;
21084 ++row)
21086 int y0 = row->y;
21087 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21089 if ((y0 >= r.y && y0 < r.y + r.height)
21090 || (y1 > r.y && y1 < r.y + r.height)
21091 || (r.y >= y0 && r.y < y1)
21092 || (r.y + r.height > y0 && r.y + r.height < y1))
21094 if (row->overlapping_p)
21096 if (first_overlapping_row == NULL)
21097 first_overlapping_row = row;
21098 last_overlapping_row = row;
21101 if (expose_line (w, row, &r))
21102 mouse_face_overwritten_p = 1;
21105 if (y1 >= yb)
21106 break;
21109 /* Display the mode line if there is one. */
21110 if (WINDOW_WANTS_MODELINE_P (w)
21111 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21112 row->enabled_p)
21113 && row->y < r.y + r.height)
21115 if (expose_line (w, row, &r))
21116 mouse_face_overwritten_p = 1;
21119 if (!w->pseudo_window_p)
21121 /* Fix the display of overlapping rows. */
21122 if (first_overlapping_row)
21123 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21125 /* Draw border between windows. */
21126 x_draw_vertical_border (w);
21128 /* Turn the cursor on again. */
21129 if (cursor_cleared_p)
21130 update_window_cursor (w, 1);
21134 #ifdef HAVE_CARBON
21135 /* Display scroll bar for this window. */
21136 if (!NILP (w->vertical_scroll_bar))
21138 /* ++KFS:
21139 If this doesn't work here (maybe some header files are missing),
21140 make a function in macterm.c and call it to do the job! */
21141 ControlHandle ch
21142 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21144 Draw1Control (ch);
21146 #endif
21148 return mouse_face_overwritten_p;
21153 /* Redraw (parts) of all windows in the window tree rooted at W that
21154 intersect R. R contains frame pixel coordinates. Value is
21155 non-zero if the exposure overwrites mouse-face. */
21157 static int
21158 expose_window_tree (w, r)
21159 struct window *w;
21160 XRectangle *r;
21162 struct frame *f = XFRAME (w->frame);
21163 int mouse_face_overwritten_p = 0;
21165 while (w && !FRAME_GARBAGED_P (f))
21167 if (!NILP (w->hchild))
21168 mouse_face_overwritten_p
21169 |= expose_window_tree (XWINDOW (w->hchild), r);
21170 else if (!NILP (w->vchild))
21171 mouse_face_overwritten_p
21172 |= expose_window_tree (XWINDOW (w->vchild), r);
21173 else
21174 mouse_face_overwritten_p |= expose_window (w, r);
21176 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21179 return mouse_face_overwritten_p;
21183 /* EXPORT:
21184 Redisplay an exposed area of frame F. X and Y are the upper-left
21185 corner of the exposed rectangle. W and H are width and height of
21186 the exposed area. All are pixel values. W or H zero means redraw
21187 the entire frame. */
21189 void
21190 expose_frame (f, x, y, w, h)
21191 struct frame *f;
21192 int x, y, w, h;
21194 XRectangle r;
21195 int mouse_face_overwritten_p = 0;
21197 TRACE ((stderr, "expose_frame "));
21199 /* No need to redraw if frame will be redrawn soon. */
21200 if (FRAME_GARBAGED_P (f))
21202 TRACE ((stderr, " garbaged\n"));
21203 return;
21206 #ifdef HAVE_CARBON
21207 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21208 or deactivated here, for unknown reasons, activated scroll bars
21209 are shown in deactivated frames in some instances. */
21210 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21211 activate_scroll_bars (f);
21212 else
21213 deactivate_scroll_bars (f);
21214 #endif
21216 /* If basic faces haven't been realized yet, there is no point in
21217 trying to redraw anything. This can happen when we get an expose
21218 event while Emacs is starting, e.g. by moving another window. */
21219 if (FRAME_FACE_CACHE (f) == NULL
21220 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21222 TRACE ((stderr, " no faces\n"));
21223 return;
21226 if (w == 0 || h == 0)
21228 r.x = r.y = 0;
21229 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21230 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21232 else
21234 r.x = x;
21235 r.y = y;
21236 r.width = w;
21237 r.height = h;
21240 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21241 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21243 if (WINDOWP (f->tool_bar_window))
21244 mouse_face_overwritten_p
21245 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21247 #ifdef HAVE_X_WINDOWS
21248 #ifndef MSDOS
21249 #ifndef USE_X_TOOLKIT
21250 if (WINDOWP (f->menu_bar_window))
21251 mouse_face_overwritten_p
21252 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21253 #endif /* not USE_X_TOOLKIT */
21254 #endif
21255 #endif
21257 /* Some window managers support a focus-follows-mouse style with
21258 delayed raising of frames. Imagine a partially obscured frame,
21259 and moving the mouse into partially obscured mouse-face on that
21260 frame. The visible part of the mouse-face will be highlighted,
21261 then the WM raises the obscured frame. With at least one WM, KDE
21262 2.1, Emacs is not getting any event for the raising of the frame
21263 (even tried with SubstructureRedirectMask), only Expose events.
21264 These expose events will draw text normally, i.e. not
21265 highlighted. Which means we must redo the highlight here.
21266 Subsume it under ``we love X''. --gerd 2001-08-15 */
21267 /* Included in Windows version because Windows most likely does not
21268 do the right thing if any third party tool offers
21269 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21270 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21272 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21273 if (f == dpyinfo->mouse_face_mouse_frame)
21275 int x = dpyinfo->mouse_face_mouse_x;
21276 int y = dpyinfo->mouse_face_mouse_y;
21277 clear_mouse_face (dpyinfo);
21278 note_mouse_highlight (f, x, y);
21284 /* EXPORT:
21285 Determine the intersection of two rectangles R1 and R2. Return
21286 the intersection in *RESULT. Value is non-zero if RESULT is not
21287 empty. */
21290 x_intersect_rectangles (r1, r2, result)
21291 XRectangle *r1, *r2, *result;
21293 XRectangle *left, *right;
21294 XRectangle *upper, *lower;
21295 int intersection_p = 0;
21297 /* Rearrange so that R1 is the left-most rectangle. */
21298 if (r1->x < r2->x)
21299 left = r1, right = r2;
21300 else
21301 left = r2, right = r1;
21303 /* X0 of the intersection is right.x0, if this is inside R1,
21304 otherwise there is no intersection. */
21305 if (right->x <= left->x + left->width)
21307 result->x = right->x;
21309 /* The right end of the intersection is the minimum of the
21310 the right ends of left and right. */
21311 result->width = (min (left->x + left->width, right->x + right->width)
21312 - result->x);
21314 /* Same game for Y. */
21315 if (r1->y < r2->y)
21316 upper = r1, lower = r2;
21317 else
21318 upper = r2, lower = r1;
21320 /* The upper end of the intersection is lower.y0, if this is inside
21321 of upper. Otherwise, there is no intersection. */
21322 if (lower->y <= upper->y + upper->height)
21324 result->y = lower->y;
21326 /* The lower end of the intersection is the minimum of the lower
21327 ends of upper and lower. */
21328 result->height = (min (lower->y + lower->height,
21329 upper->y + upper->height)
21330 - result->y);
21331 intersection_p = 1;
21335 return intersection_p;
21338 #endif /* HAVE_WINDOW_SYSTEM */
21341 /***********************************************************************
21342 Initialization
21343 ***********************************************************************/
21345 void
21346 syms_of_xdisp ()
21348 Vwith_echo_area_save_vector = Qnil;
21349 staticpro (&Vwith_echo_area_save_vector);
21351 Vmessage_stack = Qnil;
21352 staticpro (&Vmessage_stack);
21354 Qinhibit_redisplay = intern ("inhibit-redisplay");
21355 staticpro (&Qinhibit_redisplay);
21357 message_dolog_marker1 = Fmake_marker ();
21358 staticpro (&message_dolog_marker1);
21359 message_dolog_marker2 = Fmake_marker ();
21360 staticpro (&message_dolog_marker2);
21361 message_dolog_marker3 = Fmake_marker ();
21362 staticpro (&message_dolog_marker3);
21364 #if GLYPH_DEBUG
21365 defsubr (&Sdump_frame_glyph_matrix);
21366 defsubr (&Sdump_glyph_matrix);
21367 defsubr (&Sdump_glyph_row);
21368 defsubr (&Sdump_tool_bar_row);
21369 defsubr (&Strace_redisplay);
21370 defsubr (&Strace_to_stderr);
21371 #endif
21372 #ifdef HAVE_WINDOW_SYSTEM
21373 defsubr (&Stool_bar_lines_needed);
21374 defsubr (&Slookup_image_map);
21375 #endif
21376 defsubr (&Sformat_mode_line);
21378 staticpro (&Qmenu_bar_update_hook);
21379 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21381 staticpro (&Qoverriding_terminal_local_map);
21382 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21384 staticpro (&Qoverriding_local_map);
21385 Qoverriding_local_map = intern ("overriding-local-map");
21387 staticpro (&Qwindow_scroll_functions);
21388 Qwindow_scroll_functions = intern ("window-scroll-functions");
21390 staticpro (&Qredisplay_end_trigger_functions);
21391 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21393 staticpro (&Qinhibit_point_motion_hooks);
21394 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21396 QCdata = intern (":data");
21397 staticpro (&QCdata);
21398 Qdisplay = intern ("display");
21399 staticpro (&Qdisplay);
21400 Qspace_width = intern ("space-width");
21401 staticpro (&Qspace_width);
21402 Qraise = intern ("raise");
21403 staticpro (&Qraise);
21404 Qspace = intern ("space");
21405 staticpro (&Qspace);
21406 Qmargin = intern ("margin");
21407 staticpro (&Qmargin);
21408 Qpointer = intern ("pointer");
21409 staticpro (&Qpointer);
21410 Qleft_margin = intern ("left-margin");
21411 staticpro (&Qleft_margin);
21412 Qright_margin = intern ("right-margin");
21413 staticpro (&Qright_margin);
21414 QCalign_to = intern (":align-to");
21415 staticpro (&QCalign_to);
21416 QCrelative_width = intern (":relative-width");
21417 staticpro (&QCrelative_width);
21418 QCrelative_height = intern (":relative-height");
21419 staticpro (&QCrelative_height);
21420 QCeval = intern (":eval");
21421 staticpro (&QCeval);
21422 QCpropertize = intern (":propertize");
21423 staticpro (&QCpropertize);
21424 QCfile = intern (":file");
21425 staticpro (&QCfile);
21426 Qfontified = intern ("fontified");
21427 staticpro (&Qfontified);
21428 Qfontification_functions = intern ("fontification-functions");
21429 staticpro (&Qfontification_functions);
21430 Qtrailing_whitespace = intern ("trailing-whitespace");
21431 staticpro (&Qtrailing_whitespace);
21432 Qimage = intern ("image");
21433 staticpro (&Qimage);
21434 QCmap = intern (":map");
21435 staticpro (&QCmap);
21436 QCpointer = intern (":pointer");
21437 staticpro (&QCpointer);
21438 Qrect = intern ("rect");
21439 staticpro (&Qrect);
21440 Qcircle = intern ("circle");
21441 staticpro (&Qcircle);
21442 Qpoly = intern ("poly");
21443 staticpro (&Qpoly);
21444 Qmessage_truncate_lines = intern ("message-truncate-lines");
21445 staticpro (&Qmessage_truncate_lines);
21446 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21447 staticpro (&Qcursor_in_non_selected_windows);
21448 Qgrow_only = intern ("grow-only");
21449 staticpro (&Qgrow_only);
21450 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21451 staticpro (&Qinhibit_menubar_update);
21452 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21453 staticpro (&Qinhibit_eval_during_redisplay);
21454 Qposition = intern ("position");
21455 staticpro (&Qposition);
21456 Qbuffer_position = intern ("buffer-position");
21457 staticpro (&Qbuffer_position);
21458 Qobject = intern ("object");
21459 staticpro (&Qobject);
21460 Qbar = intern ("bar");
21461 staticpro (&Qbar);
21462 Qhbar = intern ("hbar");
21463 staticpro (&Qhbar);
21464 Qbox = intern ("box");
21465 staticpro (&Qbox);
21466 Qhollow = intern ("hollow");
21467 staticpro (&Qhollow);
21468 Qhand = intern ("hand");
21469 staticpro (&Qhand);
21470 Qarrow = intern ("arrow");
21471 staticpro (&Qarrow);
21472 Qtext = intern ("text");
21473 staticpro (&Qtext);
21474 Qrisky_local_variable = intern ("risky-local-variable");
21475 staticpro (&Qrisky_local_variable);
21476 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21477 staticpro (&Qinhibit_free_realized_faces);
21479 list_of_error = Fcons (intern ("error"), Qnil);
21480 staticpro (&list_of_error);
21482 last_arrow_position = Qnil;
21483 last_arrow_string = Qnil;
21484 staticpro (&last_arrow_position);
21485 staticpro (&last_arrow_string);
21487 echo_buffer[0] = echo_buffer[1] = Qnil;
21488 staticpro (&echo_buffer[0]);
21489 staticpro (&echo_buffer[1]);
21491 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21492 staticpro (&echo_area_buffer[0]);
21493 staticpro (&echo_area_buffer[1]);
21495 Vmessages_buffer_name = build_string ("*Messages*");
21496 staticpro (&Vmessages_buffer_name);
21498 mode_line_proptrans_alist = Qnil;
21499 staticpro (&mode_line_proptrans_alist);
21501 mode_line_string_list = Qnil;
21502 staticpro (&mode_line_string_list);
21504 help_echo_string = Qnil;
21505 staticpro (&help_echo_string);
21506 help_echo_object = Qnil;
21507 staticpro (&help_echo_object);
21508 help_echo_window = Qnil;
21509 staticpro (&help_echo_window);
21510 previous_help_echo_string = Qnil;
21511 staticpro (&previous_help_echo_string);
21512 help_echo_pos = -1;
21514 #ifdef HAVE_WINDOW_SYSTEM
21515 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21516 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21517 For example, if a block cursor is over a tab, it will be drawn as
21518 wide as that tab on the display. */);
21519 x_stretch_cursor_p = 0;
21520 #endif
21522 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21523 doc: /* Non-nil means highlight trailing whitespace.
21524 The face used for trailing whitespace is `trailing-whitespace'. */);
21525 Vshow_trailing_whitespace = Qnil;
21527 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
21528 doc: /* The pointer shape to show in void text areas.
21529 Nil means to show the text pointer. Other options are `arrow', `text',
21530 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
21531 Vvoid_text_area_pointer = Qarrow;
21533 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21534 doc: /* Non-nil means don't actually do any redisplay.
21535 This is used for internal purposes. */);
21536 Vinhibit_redisplay = Qnil;
21538 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21539 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
21540 Vglobal_mode_string = Qnil;
21542 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21543 doc: /* Marker for where to display an arrow on top of the buffer text.
21544 This must be the beginning of a line in order to work.
21545 See also `overlay-arrow-string'. */);
21546 Voverlay_arrow_position = Qnil;
21548 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21549 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
21550 Voverlay_arrow_string = Qnil;
21552 DEFVAR_INT ("scroll-step", &scroll_step,
21553 doc: /* *The number of lines to try scrolling a window by when point moves out.
21554 If that fails to bring point back on frame, point is centered instead.
21555 If this is zero, point is always centered after it moves off frame.
21556 If you want scrolling to always be a line at a time, you should set
21557 `scroll-conservatively' to a large value rather than set this to 1. */);
21559 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21560 doc: /* *Scroll up to this many lines, to bring point back on screen.
21561 A value of zero means to scroll the text to center point vertically
21562 in the window. */);
21563 scroll_conservatively = 0;
21565 DEFVAR_INT ("scroll-margin", &scroll_margin,
21566 doc: /* *Number of lines of margin at the top and bottom of a window.
21567 Recenter the window whenever point gets within this many lines
21568 of the top or bottom of the window. */);
21569 scroll_margin = 0;
21571 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21572 doc: /* Pixels per inch on current display.
21573 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21574 Vdisplay_pixels_per_inch = make_float (72.0);
21576 #if GLYPH_DEBUG
21577 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
21578 #endif
21580 DEFVAR_BOOL ("truncate-partial-width-windows",
21581 &truncate_partial_width_windows,
21582 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
21583 truncate_partial_width_windows = 1;
21585 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21586 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
21587 Any other value means to use the appropriate face, `mode-line',
21588 `header-line', or `menu' respectively. */);
21589 mode_line_inverse_video = 1;
21591 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21592 doc: /* *Maximum buffer size for which line number should be displayed.
21593 If the buffer is bigger than this, the line number does not appear
21594 in the mode line. A value of nil means no limit. */);
21595 Vline_number_display_limit = Qnil;
21597 DEFVAR_INT ("line-number-display-limit-width",
21598 &line_number_display_limit_width,
21599 doc: /* *Maximum line width (in characters) for line number display.
21600 If the average length of the lines near point is bigger than this, then the
21601 line number may be omitted from the mode line. */);
21602 line_number_display_limit_width = 200;
21604 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21605 doc: /* *Non-nil means highlight region even in nonselected windows. */);
21606 highlight_nonselected_windows = 0;
21608 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21609 doc: /* Non-nil if more than one frame is visible on this display.
21610 Minibuffer-only frames don't count, but iconified frames do.
21611 This variable is not guaranteed to be accurate except while processing
21612 `frame-title-format' and `icon-title-format'. */);
21614 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21615 doc: /* Template for displaying the title bar of visible frames.
21616 \(Assuming the window manager supports this feature.)
21617 This variable has the same structure as `mode-line-format' (which see),
21618 and is used only on frames for which no explicit name has been set
21619 \(see `modify-frame-parameters'). */);
21621 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21622 doc: /* Template for displaying the title bar of an iconified frame.
21623 \(Assuming the window manager supports this feature.)
21624 This variable has the same structure as `mode-line-format' (which see),
21625 and is used only on frames for which no explicit name has been set
21626 \(see `modify-frame-parameters'). */);
21627 Vicon_title_format
21628 = Vframe_title_format
21629 = Fcons (intern ("multiple-frames"),
21630 Fcons (build_string ("%b"),
21631 Fcons (Fcons (empty_string,
21632 Fcons (intern ("invocation-name"),
21633 Fcons (build_string ("@"),
21634 Fcons (intern ("system-name"),
21635 Qnil)))),
21636 Qnil)));
21638 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21639 doc: /* Maximum number of lines to keep in the message log buffer.
21640 If nil, disable message logging. If t, log messages but don't truncate
21641 the buffer when it becomes large. */);
21642 Vmessage_log_max = make_number (50);
21644 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21645 doc: /* Functions called before redisplay, if window sizes have changed.
21646 The value should be a list of functions that take one argument.
21647 Just before redisplay, for each frame, if any of its windows have changed
21648 size since the last redisplay, or have been split or deleted,
21649 all the functions in the list are called, with the frame as argument. */);
21650 Vwindow_size_change_functions = Qnil;
21652 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21653 doc: /* List of Functions to call before redisplaying a window with scrolling.
21654 Each function is called with two arguments, the window
21655 and its new display-start position. Note that the value of `window-end'
21656 is not valid when these functions are called. */);
21657 Vwindow_scroll_functions = Qnil;
21659 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21660 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21661 mouse_autoselect_window = 0;
21663 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21664 doc: /* *Non-nil means automatically resize tool-bars.
21665 This increases a tool-bar's height if not all tool-bar items are visible.
21666 It decreases a tool-bar's height when it would display blank lines
21667 otherwise. */);
21668 auto_resize_tool_bars_p = 1;
21670 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21671 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
21672 auto_raise_tool_bar_buttons_p = 1;
21674 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21675 doc: /* *Margin around tool-bar buttons in pixels.
21676 If an integer, use that for both horizontal and vertical margins.
21677 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
21678 HORZ specifying the horizontal margin, and VERT specifying the
21679 vertical margin. */);
21680 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
21682 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
21683 doc: /* *Relief thickness of tool-bar buttons. */);
21684 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
21686 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21687 doc: /* List of functions to call to fontify regions of text.
21688 Each function is called with one argument POS. Functions must
21689 fontify a region starting at POS in the current buffer, and give
21690 fontified regions the property `fontified'. */);
21691 Vfontification_functions = Qnil;
21692 Fmake_variable_buffer_local (Qfontification_functions);
21694 DEFVAR_BOOL ("unibyte-display-via-language-environment",
21695 &unibyte_display_via_language_environment,
21696 doc: /* *Non-nil means display unibyte text according to language environment.
21697 Specifically this means that unibyte non-ASCII characters
21698 are displayed by converting them to the equivalent multibyte characters
21699 according to the current language environment. As a result, they are
21700 displayed according to the current fontset. */);
21701 unibyte_display_via_language_environment = 0;
21703 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21704 doc: /* *Maximum height for resizing mini-windows.
21705 If a float, it specifies a fraction of the mini-window frame's height.
21706 If an integer, it specifies a number of lines. */);
21707 Vmax_mini_window_height = make_float (0.25);
21709 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21710 doc: /* *How to resize mini-windows.
21711 A value of nil means don't automatically resize mini-windows.
21712 A value of t means resize them to fit the text displayed in them.
21713 A value of `grow-only', the default, means let mini-windows grow
21714 only, until their display becomes empty, at which point the windows
21715 go back to their normal size. */);
21716 Vresize_mini_windows = Qgrow_only;
21718 DEFVAR_LISP ("cursor-in-non-selected-windows",
21719 &Vcursor_in_non_selected_windows,
21720 doc: /* *Cursor type to display in non-selected windows.
21721 t means to use hollow box cursor. See `cursor-type' for other values. */);
21722 Vcursor_in_non_selected_windows = Qt;
21724 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21725 doc: /* Alist specifying how to blink the cursor off.
21726 Each element has the form (ON-STATE . OFF-STATE). Whenever the
21727 `cursor-type' frame-parameter or variable equals ON-STATE,
21728 comparing using `equal', Emacs uses OFF-STATE to specify
21729 how to blink it off. */);
21730 Vblink_cursor_alist = Qnil;
21732 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
21733 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
21734 automatic_hscrolling_p = 1;
21736 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
21737 doc: /* *How many columns away from the window edge point is allowed to get
21738 before automatic hscrolling will horizontally scroll the window. */);
21739 hscroll_margin = 5;
21741 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
21742 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21743 When point is less than `automatic-hscroll-margin' columns from the window
21744 edge, automatic hscrolling will scroll the window by the amount of columns
21745 determined by this variable. If its value is a positive integer, scroll that
21746 many columns. If it's a positive floating-point number, it specifies the
21747 fraction of the window's width to scroll. If it's nil or zero, point will be
21748 centered horizontally after the scroll. Any other value, including negative
21749 numbers, are treated as if the value were zero.
21751 Automatic hscrolling always moves point outside the scroll margin, so if
21752 point was more than scroll step columns inside the margin, the window will
21753 scroll more than the value given by the scroll step.
21755 Note that the lower bound for automatic hscrolling specified by `scroll-left'
21756 and `scroll-right' overrides this variable's effect. */);
21757 Vhscroll_step = make_number (0);
21759 DEFVAR_LISP ("image-types", &Vimage_types,
21760 doc: /* List of supported image types.
21761 Each element of the list is a symbol for a supported image type. */);
21762 Vimage_types = Qnil;
21764 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21765 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
21766 Bind this around calls to `message' to let it take effect. */);
21767 message_truncate_lines = 0;
21769 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21770 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
21771 Can be used to update submenus whose contents should vary. */);
21772 Vmenu_bar_update_hook = Qnil;
21774 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21775 doc: /* Non-nil means don't update menu bars. Internal use only. */);
21776 inhibit_menubar_update = 0;
21778 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21779 doc: /* Non-nil means don't eval Lisp during redisplay. */);
21780 inhibit_eval_during_redisplay = 0;
21782 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21783 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21784 inhibit_free_realized_faces = 0;
21786 #if GLYPH_DEBUG
21787 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21788 doc: /* Inhibit try_window_id display optimization. */);
21789 inhibit_try_window_id = 0;
21791 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21792 doc: /* Inhibit try_window_reusing display optimization. */);
21793 inhibit_try_window_reusing = 0;
21795 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21796 doc: /* Inhibit try_cursor_movement display optimization. */);
21797 inhibit_try_cursor_movement = 0;
21798 #endif /* GLYPH_DEBUG */
21802 /* Initialize this module when Emacs starts. */
21804 void
21805 init_xdisp ()
21807 Lisp_Object root_window;
21808 struct window *mini_w;
21810 current_header_line_height = current_mode_line_height = -1;
21812 CHARPOS (this_line_start_pos) = 0;
21814 mini_w = XWINDOW (minibuf_window);
21815 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21817 if (!noninteractive)
21819 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21820 int i;
21822 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21823 set_window_height (root_window,
21824 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21826 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21827 set_window_height (minibuf_window, 1, 0);
21829 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21830 mini_w->total_cols = make_number (FRAME_COLS (f));
21832 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21833 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21834 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21836 /* The default ellipsis glyphs `...'. */
21837 for (i = 0; i < 3; ++i)
21838 default_invis_vector[i] = make_number ('.');
21842 /* Allocate the buffer for frame titles.
21843 Also used for `format-mode-line'. */
21844 int size = 100;
21845 frame_title_buf = (char *) xmalloc (size);
21846 frame_title_buf_end = frame_title_buf + size;
21847 frame_title_ptr = NULL;
21850 help_echo_showing_p = 0;
21854 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21855 (do not change this comment) */