Merged from emacs@sv.gnu.org
[emacs.git] / src / xdisp.c
blob80b9ff6c8f6d734079ae348fe6c0a2a65d9a2c9c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.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"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
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, Vredisplay_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 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
289 int auto_resize_tool_bars_p;
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
295 int x_stretch_cursor_p;
297 /* Non-nil means don't actually do any redisplay. */
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
303 int inhibit_eval_during_redisplay;
305 /* Names of text properties relevant for redisplay. */
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
310 /* Symbols used in text property values. */
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
324 /* Non-nil means highlight trailing whitespace. */
326 Lisp_Object Vshow_trailing_whitespace;
328 /* Non-nil means escape non-break space and hyphens. */
330 Lisp_Object Vnobreak_char_display;
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
344 #endif /* HAVE_WINDOW_SYSTEM */
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
350 Lisp_Object Vvoid_text_area_pointer;
352 /* Name of the face used to highlight trailing whitespace. */
354 Lisp_Object Qtrailing_whitespace;
356 /* Name and number of the face used to highlight escape glyphs. */
358 Lisp_Object Qescape_glyph;
360 /* Name and number of the face used to highlight non-breaking spaces. */
362 Lisp_Object Qnobreak_space;
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
367 Lisp_Object Qimage;
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
376 int noninteractive_need_newline;
378 /* Non-zero means print newline to message log before next message. */
380 static int message_log_need_newline;
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
394 static struct text_pos this_line_start_pos;
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
399 static struct text_pos this_line_end_pos;
401 /* The vertical positions and the height of this line. */
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
410 static int this_line_start_x;
412 /* Buffer that this_line_.* variables are referring to. */
414 static struct buffer *this_line_buffer;
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
419 int truncate_partial_width_windows;
421 /* A flag to control how to display unibyte 8-bit character. */
423 int unibyte_display_via_language_environment;
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
429 int multiple_frames;
431 Lisp_Object Vglobal_mode_string;
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
438 Lisp_Object Voverlay_arrow_variable_list;
440 /* Marker for where to display an arrow on top of the buffer text. */
442 Lisp_Object Voverlay_arrow_position;
444 /* String to display for the arrow. Only used on terminal frames. */
446 Lisp_Object Voverlay_arrow_string;
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
460 /* Like mode-line-format, but for the title bar on a visible frame. */
462 Lisp_Object Vframe_title_format;
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
466 Lisp_Object Vicon_title_format;
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
472 static Lisp_Object Vwindow_size_change_functions;
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
476 /* Nonzero if an overlay arrow has been displayed in this window. */
478 static int overlay_arrow_seen;
480 /* Nonzero means highlight the region even in nonselected windows. */
482 int highlight_nonselected_windows;
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
487 static EMACS_INT scroll_step;
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
492 static EMACS_INT scroll_conservatively;
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
499 EMACS_INT scroll_margin;
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
505 int buffer_shared;
507 /* Vector containing glyphs for an ellipsis `...'. */
509 static Lisp_Object default_invis_vector[3];
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
515 This variable is deprecated. */
517 int mode_line_inverse_video;
519 /* Prompt to display in front of the mini-buffer contents. */
521 Lisp_Object minibuf_prompt;
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
526 int minibuf_prompt_width;
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
532 Lisp_Object echo_area_window;
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
539 Lisp_Object Vmessage_stack;
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
544 int message_enable_multibyte;
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548 int update_mode_lines;
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
553 int windows_or_buffers_changed;
555 /* Nonzero means a frame's cursor type has been changed. */
557 int cursor_type_changed;
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
562 int line_number_displayed;
564 /* Maximum buffer size for which to display line numbers. */
566 Lisp_Object Vline_number_display_limit;
568 /* Line width to consider when repositioning for line number display. */
570 static EMACS_INT line_number_display_limit_width;
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
575 Lisp_Object Vmessage_log_max;
577 /* The name of the *Messages* buffer, a string. */
579 static Lisp_Object Vmessages_buffer_name;
581 /* Current, index 0, and last displayed echo area message. Either
582 buffers from echo_buffers, or nil to indicate no message. */
584 Lisp_Object echo_area_buffer[2];
586 /* The buffers referenced from echo_area_buffer. */
588 static Lisp_Object echo_buffer[2];
590 /* A vector saved used in with_area_buffer to reduce consing. */
592 static Lisp_Object Vwith_echo_area_save_vector;
594 /* Non-zero means display_echo_area should display the last echo area
595 message again. Set by redisplay_preserve_echo_area. */
597 static int display_last_displayed_message_p;
599 /* Nonzero if echo area is being used by print; zero if being used by
600 message. */
602 int message_buf_print;
604 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
606 Lisp_Object Qinhibit_menubar_update;
607 int inhibit_menubar_update;
609 /* Maximum height for resizing mini-windows. Either a float
610 specifying a fraction of the available height, or an integer
611 specifying a number of lines. */
613 Lisp_Object Vmax_mini_window_height;
615 /* Non-zero means messages should be displayed with truncated
616 lines instead of being continued. */
618 int message_truncate_lines;
619 Lisp_Object Qmessage_truncate_lines;
621 /* Set to 1 in clear_message to make redisplay_internal aware
622 of an emptied echo area. */
624 static int message_cleared_p;
626 /* How to blink the default frame cursor off. */
627 Lisp_Object Vblink_cursor_alist;
629 /* A scratch glyph row with contents used for generating truncation
630 glyphs. Also used in direct_output_for_insert. */
632 #define MAX_SCRATCH_GLYPHS 100
633 struct glyph_row scratch_glyph_row;
634 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
636 /* Ascent and height of the last line processed by move_it_to. */
638 static int last_max_ascent, last_height;
640 /* Non-zero if there's a help-echo in the echo area. */
642 int help_echo_showing_p;
644 /* If >= 0, computed, exact values of mode-line and header-line height
645 to use in the macros CURRENT_MODE_LINE_HEIGHT and
646 CURRENT_HEADER_LINE_HEIGHT. */
648 int current_mode_line_height, current_header_line_height;
650 /* The maximum distance to look ahead for text properties. Values
651 that are too small let us call compute_char_face and similar
652 functions too often which is expensive. Values that are too large
653 let us call compute_char_face and alike too often because we
654 might not be interested in text properties that far away. */
656 #define TEXT_PROP_DISTANCE_LIMIT 100
658 #if GLYPH_DEBUG
660 /* Variables to turn off display optimizations from Lisp. */
662 int inhibit_try_window_id, inhibit_try_window_reusing;
663 int inhibit_try_cursor_movement;
665 /* Non-zero means print traces of redisplay if compiled with
666 GLYPH_DEBUG != 0. */
668 int trace_redisplay_p;
670 #endif /* GLYPH_DEBUG */
672 #ifdef DEBUG_TRACE_MOVE
673 /* Non-zero means trace with TRACE_MOVE to stderr. */
674 int trace_move;
676 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
677 #else
678 #define TRACE_MOVE(x) (void) 0
679 #endif
681 /* Non-zero means automatically scroll windows horizontally to make
682 point visible. */
684 int automatic_hscrolling_p;
686 /* How close to the margin can point get before the window is scrolled
687 horizontally. */
688 EMACS_INT hscroll_margin;
690 /* How much to scroll horizontally when point is inside the above margin. */
691 Lisp_Object Vhscroll_step;
693 /* The variable `resize-mini-windows'. If nil, don't resize
694 mini-windows. If t, always resize them to fit the text they
695 display. If `grow-only', let mini-windows grow only until they
696 become empty. */
698 Lisp_Object Vresize_mini_windows;
700 /* Buffer being redisplayed -- for redisplay_window_error. */
702 struct buffer *displayed_buffer;
704 /* Value returned from text property handlers (see below). */
706 enum prop_handled
708 HANDLED_NORMALLY,
709 HANDLED_RECOMPUTE_PROPS,
710 HANDLED_OVERLAY_STRING_CONSUMED,
711 HANDLED_RETURN
714 /* A description of text properties that redisplay is interested
715 in. */
717 struct props
719 /* The name of the property. */
720 Lisp_Object *name;
722 /* A unique index for the property. */
723 enum prop_idx idx;
725 /* A handler function called to set up iterator IT from the property
726 at IT's current position. Value is used to steer handle_stop. */
727 enum prop_handled (*handler) P_ ((struct it *it));
730 static enum prop_handled handle_face_prop P_ ((struct it *));
731 static enum prop_handled handle_invisible_prop P_ ((struct it *));
732 static enum prop_handled handle_display_prop P_ ((struct it *));
733 static enum prop_handled handle_composition_prop P_ ((struct it *));
734 static enum prop_handled handle_overlay_change P_ ((struct it *));
735 static enum prop_handled handle_fontified_prop P_ ((struct it *));
737 /* Properties handled by iterators. */
739 static struct props it_props[] =
741 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
742 /* Handle `face' before `display' because some sub-properties of
743 `display' need to know the face. */
744 {&Qface, FACE_PROP_IDX, handle_face_prop},
745 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
746 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
747 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
748 {NULL, 0, NULL}
751 /* Value is the position described by X. If X is a marker, value is
752 the marker_position of X. Otherwise, value is X. */
754 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
756 /* Enumeration returned by some move_it_.* functions internally. */
758 enum move_it_result
760 /* Not used. Undefined value. */
761 MOVE_UNDEFINED,
763 /* Move ended at the requested buffer position or ZV. */
764 MOVE_POS_MATCH_OR_ZV,
766 /* Move ended at the requested X pixel position. */
767 MOVE_X_REACHED,
769 /* Move within a line ended at the end of a line that must be
770 continued. */
771 MOVE_LINE_CONTINUED,
773 /* Move within a line ended at the end of a line that would
774 be displayed truncated. */
775 MOVE_LINE_TRUNCATED,
777 /* Move within a line ended at a line end. */
778 MOVE_NEWLINE_OR_CR
781 /* This counter is used to clear the face cache every once in a while
782 in redisplay_internal. It is incremented for each redisplay.
783 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
784 cleared. */
786 #define CLEAR_FACE_CACHE_COUNT 500
787 static int clear_face_cache_count;
789 /* Similarly for the image cache. */
791 #ifdef HAVE_WINDOW_SYSTEM
792 #define CLEAR_IMAGE_CACHE_COUNT 101
793 static int clear_image_cache_count;
794 #endif
796 /* Non-zero while redisplay_internal is in progress. */
798 int redisplaying_p;
800 /* Non-zero means don't free realized faces. Bound while freeing
801 realized faces is dangerous because glyph matrices might still
802 reference them. */
804 int inhibit_free_realized_faces;
805 Lisp_Object Qinhibit_free_realized_faces;
807 /* If a string, XTread_socket generates an event to display that string.
808 (The display is done in read_char.) */
810 Lisp_Object help_echo_string;
811 Lisp_Object help_echo_window;
812 Lisp_Object help_echo_object;
813 int help_echo_pos;
815 /* Temporary variable for XTread_socket. */
817 Lisp_Object previous_help_echo_string;
819 /* Null glyph slice */
821 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
824 /* Function prototypes. */
826 static void setup_for_ellipsis P_ ((struct it *, int));
827 static void mark_window_display_accurate_1 P_ ((struct window *, int));
828 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
829 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
830 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
831 static int redisplay_mode_lines P_ ((Lisp_Object, int));
832 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
834 #if 0
835 static int invisible_text_between_p P_ ((struct it *, int, int));
836 #endif
838 static void pint2str P_ ((char *, int, int));
839 static void pint2hrstr P_ ((char *, int, int));
840 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
841 struct text_pos));
842 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
843 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
844 static void store_mode_line_noprop_char P_ ((char));
845 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
846 static void x_consider_frame_title P_ ((Lisp_Object));
847 static void handle_stop P_ ((struct it *));
848 static int tool_bar_lines_needed P_ ((struct frame *, int *));
849 static int single_display_spec_intangible_p P_ ((Lisp_Object));
850 static void ensure_echo_area_buffers P_ ((void));
851 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
852 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
853 static int with_echo_area_buffer P_ ((struct window *, int,
854 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
855 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static void clear_garbaged_frames P_ ((void));
857 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
858 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
859 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
860 static int display_echo_area P_ ((struct window *));
861 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
864 static int string_char_and_length P_ ((const unsigned char *, int, int *));
865 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
866 struct text_pos));
867 static int compute_window_start_on_continuation_line P_ ((struct window *));
868 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
869 static void insert_left_trunc_glyphs P_ ((struct it *));
870 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
871 Lisp_Object));
872 static void extend_face_to_end_of_line P_ ((struct it *));
873 static int append_space_for_newline P_ ((struct it *, int));
874 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
875 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
876 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
877 static int trailing_whitespace_p P_ ((int));
878 static int message_log_check_duplicate P_ ((int, int, int, int));
879 static void push_it P_ ((struct it *));
880 static void pop_it P_ ((struct it *));
881 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
882 static void select_frame_for_redisplay P_ ((Lisp_Object));
883 static void redisplay_internal P_ ((int));
884 static int echo_area_display P_ ((int));
885 static void redisplay_windows P_ ((Lisp_Object));
886 static void redisplay_window P_ ((Lisp_Object, int));
887 static Lisp_Object redisplay_window_error ();
888 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
889 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
890 static void update_menu_bar P_ ((struct frame *, int));
891 static int try_window_reusing_current_matrix P_ ((struct window *));
892 static int try_window_id P_ ((struct window *));
893 static int display_line P_ ((struct it *));
894 static int display_mode_lines P_ ((struct window *));
895 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
896 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
897 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
898 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
899 static void display_menu_bar P_ ((struct window *));
900 static int display_count_lines P_ ((int, int, int, int, int *));
901 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
902 int, int, struct it *, int, int, int, int));
903 static void compute_line_metrics P_ ((struct it *));
904 static void run_redisplay_end_trigger_hook P_ ((struct it *));
905 static int get_overlay_strings P_ ((struct it *, int));
906 static void next_overlay_string P_ ((struct it *));
907 static void reseat P_ ((struct it *, struct text_pos, int));
908 static void reseat_1 P_ ((struct it *, struct text_pos, int));
909 static void back_to_previous_visible_line_start P_ ((struct it *));
910 void reseat_at_previous_visible_line_start P_ ((struct it *));
911 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
912 static int next_element_from_ellipsis P_ ((struct it *));
913 static int next_element_from_display_vector P_ ((struct it *));
914 static int next_element_from_string P_ ((struct it *));
915 static int next_element_from_c_string P_ ((struct it *));
916 static int next_element_from_buffer P_ ((struct it *));
917 static int next_element_from_composition P_ ((struct it *));
918 static int next_element_from_image P_ ((struct it *));
919 static int next_element_from_stretch P_ ((struct it *));
920 static void load_overlay_strings P_ ((struct it *, int));
921 static int init_from_display_pos P_ ((struct it *, struct window *,
922 struct display_pos *));
923 static void reseat_to_string P_ ((struct it *, unsigned char *,
924 Lisp_Object, int, int, int, int));
925 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
926 int, int, int));
927 void move_it_vertically_backward P_ ((struct it *, int));
928 static void init_to_row_start P_ ((struct it *, struct window *,
929 struct glyph_row *));
930 static int init_to_row_end P_ ((struct it *, struct window *,
931 struct glyph_row *));
932 static void back_to_previous_line_start P_ ((struct it *));
933 static int forward_to_next_line_start P_ ((struct it *, int *));
934 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
935 Lisp_Object, int));
936 static struct text_pos string_pos P_ ((int, Lisp_Object));
937 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
938 static int number_of_chars P_ ((unsigned char *, int));
939 static void compute_stop_pos P_ ((struct it *));
940 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
941 Lisp_Object));
942 static int face_before_or_after_it_pos P_ ((struct it *, int));
943 static int next_overlay_change P_ ((int));
944 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
945 Lisp_Object, struct text_pos *,
946 int));
947 static int underlying_face_id P_ ((struct it *));
948 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
949 struct window *));
951 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
952 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
954 #ifdef HAVE_WINDOW_SYSTEM
956 static void update_tool_bar P_ ((struct frame *, int));
957 static void build_desired_tool_bar_string P_ ((struct frame *f));
958 static int redisplay_tool_bar P_ ((struct frame *));
959 static void display_tool_bar_line P_ ((struct it *, int));
960 static void notice_overwritten_cursor P_ ((struct window *,
961 enum glyph_row_area,
962 int, int, int, int));
966 #endif /* HAVE_WINDOW_SYSTEM */
969 /***********************************************************************
970 Window display dimensions
971 ***********************************************************************/
973 /* Return the bottom boundary y-position for text lines in window W.
974 This is the first y position at which a line cannot start.
975 It is relative to the top of the window.
977 This is the height of W minus the height of a mode line, if any. */
979 INLINE int
980 window_text_bottom_y (w)
981 struct window *w;
983 int height = WINDOW_TOTAL_HEIGHT (w);
985 if (WINDOW_WANTS_MODELINE_P (w))
986 height -= CURRENT_MODE_LINE_HEIGHT (w);
987 return height;
990 /* Return the pixel width of display area AREA of window W. AREA < 0
991 means return the total width of W, not including fringes to
992 the left and right of the window. */
994 INLINE int
995 window_box_width (w, area)
996 struct window *w;
997 int area;
999 int cols = XFASTINT (w->total_cols);
1000 int pixels = 0;
1002 if (!w->pseudo_window_p)
1004 cols -= WINDOW_SCROLL_BAR_COLS (w);
1006 if (area == TEXT_AREA)
1008 if (INTEGERP (w->left_margin_cols))
1009 cols -= XFASTINT (w->left_margin_cols);
1010 if (INTEGERP (w->right_margin_cols))
1011 cols -= XFASTINT (w->right_margin_cols);
1012 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1014 else if (area == LEFT_MARGIN_AREA)
1016 cols = (INTEGERP (w->left_margin_cols)
1017 ? XFASTINT (w->left_margin_cols) : 0);
1018 pixels = 0;
1020 else if (area == RIGHT_MARGIN_AREA)
1022 cols = (INTEGERP (w->right_margin_cols)
1023 ? XFASTINT (w->right_margin_cols) : 0);
1024 pixels = 0;
1028 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1032 /* Return the pixel height of the display area of window W, not
1033 including mode lines of W, if any. */
1035 INLINE int
1036 window_box_height (w)
1037 struct window *w;
1039 struct frame *f = XFRAME (w->frame);
1040 int height = WINDOW_TOTAL_HEIGHT (w);
1042 xassert (height >= 0);
1044 /* Note: the code below that determines the mode-line/header-line
1045 height is essentially the same as that contained in the macro
1046 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1047 the appropriate glyph row has its `mode_line_p' flag set,
1048 and if it doesn't, uses estimate_mode_line_height instead. */
1050 if (WINDOW_WANTS_MODELINE_P (w))
1052 struct glyph_row *ml_row
1053 = (w->current_matrix && w->current_matrix->rows
1054 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1055 : 0);
1056 if (ml_row && ml_row->mode_line_p)
1057 height -= ml_row->height;
1058 else
1059 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1062 if (WINDOW_WANTS_HEADER_LINE_P (w))
1064 struct glyph_row *hl_row
1065 = (w->current_matrix && w->current_matrix->rows
1066 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1067 : 0);
1068 if (hl_row && hl_row->mode_line_p)
1069 height -= hl_row->height;
1070 else
1071 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1074 /* With a very small font and a mode-line that's taller than
1075 default, we might end up with a negative height. */
1076 return max (0, height);
1079 /* Return the window-relative coordinate of the left edge of display
1080 area AREA of window W. AREA < 0 means return the left edge of the
1081 whole window, to the right of the left fringe of W. */
1083 INLINE int
1084 window_box_left_offset (w, area)
1085 struct window *w;
1086 int area;
1088 int x;
1090 if (w->pseudo_window_p)
1091 return 0;
1093 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1095 if (area == TEXT_AREA)
1096 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1097 + window_box_width (w, LEFT_MARGIN_AREA));
1098 else if (area == RIGHT_MARGIN_AREA)
1099 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1100 + window_box_width (w, LEFT_MARGIN_AREA)
1101 + window_box_width (w, TEXT_AREA)
1102 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1104 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1105 else if (area == LEFT_MARGIN_AREA
1106 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1107 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1109 return x;
1113 /* Return the window-relative coordinate of the right edge of display
1114 area AREA of window W. AREA < 0 means return the left edge of the
1115 whole window, to the left of the right fringe of W. */
1117 INLINE int
1118 window_box_right_offset (w, area)
1119 struct window *w;
1120 int area;
1122 return window_box_left_offset (w, area) + window_box_width (w, area);
1125 /* Return the frame-relative coordinate of the left edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the right of the left fringe of W. */
1129 INLINE int
1130 window_box_left (w, area)
1131 struct window *w;
1132 int area;
1134 struct frame *f = XFRAME (w->frame);
1135 int x;
1137 if (w->pseudo_window_p)
1138 return FRAME_INTERNAL_BORDER_WIDTH (f);
1140 x = (WINDOW_LEFT_EDGE_X (w)
1141 + window_box_left_offset (w, area));
1143 return x;
1147 /* Return the frame-relative coordinate of the right edge of display
1148 area AREA of window W. AREA < 0 means return the left edge of the
1149 whole window, to the left of the right fringe of W. */
1151 INLINE int
1152 window_box_right (w, area)
1153 struct window *w;
1154 int area;
1156 return window_box_left (w, area) + window_box_width (w, area);
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines, in frame-relative coordinates. AREA < 0 means the
1161 whole window, not including the left and right fringes of
1162 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1163 coordinates of the upper-left corner of the box. Return in
1164 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1166 INLINE void
1167 window_box (w, area, box_x, box_y, box_width, box_height)
1168 struct window *w;
1169 int area;
1170 int *box_x, *box_y, *box_width, *box_height;
1172 if (box_width)
1173 *box_width = window_box_width (w, area);
1174 if (box_height)
1175 *box_height = window_box_height (w);
1176 if (box_x)
1177 *box_x = window_box_left (w, area);
1178 if (box_y)
1180 *box_y = WINDOW_TOP_EDGE_Y (w);
1181 if (WINDOW_WANTS_HEADER_LINE_P (w))
1182 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1187 /* Get the bounding box of the display area AREA of window W, without
1188 mode lines. AREA < 0 means the whole window, not including the
1189 left and right fringe of the window. Return in *TOP_LEFT_X
1190 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1191 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1192 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1193 box. */
1195 INLINE void
1196 window_box_edges (w, area, top_left_x, top_left_y,
1197 bottom_right_x, bottom_right_y)
1198 struct window *w;
1199 int area;
1200 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1202 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1203 bottom_right_y);
1204 *bottom_right_x += *top_left_x;
1205 *bottom_right_y += *top_left_y;
1210 /***********************************************************************
1211 Utilities
1212 ***********************************************************************/
1214 /* Return the bottom y-position of the line the iterator IT is in.
1215 This can modify IT's settings. */
1218 line_bottom_y (it)
1219 struct it *it;
1221 int line_height = it->max_ascent + it->max_descent;
1222 int line_top_y = it->current_y;
1224 if (line_height == 0)
1226 if (last_height)
1227 line_height = last_height;
1228 else if (IT_CHARPOS (*it) < ZV)
1230 move_it_by_lines (it, 1, 1);
1231 line_height = (it->max_ascent || it->max_descent
1232 ? it->max_ascent + it->max_descent
1233 : last_height);
1235 else
1237 struct glyph_row *row = it->glyph_row;
1239 /* Use the default character height. */
1240 it->glyph_row = NULL;
1241 it->what = IT_CHARACTER;
1242 it->c = ' ';
1243 it->len = 1;
1244 PRODUCE_GLYPHS (it);
1245 line_height = it->ascent + it->descent;
1246 it->glyph_row = row;
1250 return line_top_y + line_height;
1254 /* Return 1 if position CHARPOS is visible in window W.
1255 If visible, set *X and *Y to pixel coordinates of top left corner.
1256 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1257 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1258 and header-lines heights. */
1261 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1262 struct window *w;
1263 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1265 struct it it;
1266 struct text_pos top;
1267 int visible_p = 0;
1268 struct buffer *old_buffer = NULL;
1270 if (noninteractive)
1271 return visible_p;
1273 if (XBUFFER (w->buffer) != current_buffer)
1275 old_buffer = current_buffer;
1276 set_buffer_internal_1 (XBUFFER (w->buffer));
1279 SET_TEXT_POS_FROM_MARKER (top, w->start);
1281 /* Compute exact mode line heights, if requested. */
1282 if (exact_mode_line_heights_p)
1284 if (WINDOW_WANTS_MODELINE_P (w))
1285 current_mode_line_height
1286 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1287 current_buffer->mode_line_format);
1289 if (WINDOW_WANTS_HEADER_LINE_P (w))
1290 current_header_line_height
1291 = display_mode_line (w, HEADER_LINE_FACE_ID,
1292 current_buffer->header_line_format);
1295 start_display (&it, w, top);
1296 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1297 MOVE_TO_POS | MOVE_TO_Y);
1299 /* Note that we may overshoot because of invisible text. */
1300 if (IT_CHARPOS (it) >= charpos)
1302 int top_x = it.current_x;
1303 int top_y = it.current_y;
1304 int bottom_y = (last_height = 0, line_bottom_y (&it));
1305 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1307 if (top_y < window_top_y)
1308 visible_p = bottom_y > window_top_y;
1309 else if (top_y < it.last_visible_y)
1310 visible_p = 1;
1311 if (visible_p)
1313 *x = top_x;
1314 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1315 *rtop = max (0, window_top_y - top_y);
1316 *rbot = max (0, bottom_y - it.last_visible_y);
1319 else
1321 struct it it2;
1323 it2 = it;
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1, 0);
1326 if (charpos < IT_CHARPOS (it))
1328 visible_p = 1;
1329 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1330 *x = it2.current_x;
1331 *y = it2.current_y + it2.max_ascent - it2.ascent;
1332 *rtop = max (0, -it2.current_y);
1333 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1334 - it.last_visible_y));
1338 if (old_buffer)
1339 set_buffer_internal_1 (old_buffer);
1341 current_header_line_height = current_mode_line_height = -1;
1343 if (visible_p && XFASTINT (w->hscroll) > 0)
1344 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1346 return visible_p;
1350 /* Return the next character from STR which is MAXLEN bytes long.
1351 Return in *LEN the length of the character. This is like
1352 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1353 we find one, we return a `?', but with the length of the invalid
1354 character. */
1356 static INLINE int
1357 string_char_and_length (str, maxlen, len)
1358 const unsigned char *str;
1359 int maxlen, *len;
1361 int c;
1363 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1364 if (!CHAR_VALID_P (c, 1))
1365 /* We may not change the length here because other places in Emacs
1366 don't use this function, i.e. they silently accept invalid
1367 characters. */
1368 c = '?';
1370 return c;
1375 /* Given a position POS containing a valid character and byte position
1376 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1378 static struct text_pos
1379 string_pos_nchars_ahead (pos, string, nchars)
1380 struct text_pos pos;
1381 Lisp_Object string;
1382 int nchars;
1384 xassert (STRINGP (string) && nchars >= 0);
1386 if (STRING_MULTIBYTE (string))
1388 int rest = SBYTES (string) - BYTEPOS (pos);
1389 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1390 int len;
1392 while (nchars--)
1394 string_char_and_length (p, rest, &len);
1395 p += len, rest -= len;
1396 xassert (rest >= 0);
1397 CHARPOS (pos) += 1;
1398 BYTEPOS (pos) += len;
1401 else
1402 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1404 return pos;
1408 /* Value is the text position, i.e. character and byte position,
1409 for character position CHARPOS in STRING. */
1411 static INLINE struct text_pos
1412 string_pos (charpos, string)
1413 int charpos;
1414 Lisp_Object string;
1416 struct text_pos pos;
1417 xassert (STRINGP (string));
1418 xassert (charpos >= 0);
1419 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1420 return pos;
1424 /* Value is a text position, i.e. character and byte position, for
1425 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1426 means recognize multibyte characters. */
1428 static struct text_pos
1429 c_string_pos (charpos, s, multibyte_p)
1430 int charpos;
1431 unsigned char *s;
1432 int multibyte_p;
1434 struct text_pos pos;
1436 xassert (s != NULL);
1437 xassert (charpos >= 0);
1439 if (multibyte_p)
1441 int rest = strlen (s), len;
1443 SET_TEXT_POS (pos, 0, 0);
1444 while (charpos--)
1446 string_char_and_length (s, rest, &len);
1447 s += len, rest -= len;
1448 xassert (rest >= 0);
1449 CHARPOS (pos) += 1;
1450 BYTEPOS (pos) += len;
1453 else
1454 SET_TEXT_POS (pos, charpos, charpos);
1456 return pos;
1460 /* Value is the number of characters in C string S. MULTIBYTE_P
1461 non-zero means recognize multibyte characters. */
1463 static int
1464 number_of_chars (s, multibyte_p)
1465 unsigned char *s;
1466 int multibyte_p;
1468 int nchars;
1470 if (multibyte_p)
1472 int rest = strlen (s), len;
1473 unsigned char *p = (unsigned char *) s;
1475 for (nchars = 0; rest > 0; ++nchars)
1477 string_char_and_length (p, rest, &len);
1478 rest -= len, p += len;
1481 else
1482 nchars = strlen (s);
1484 return nchars;
1488 /* Compute byte position NEWPOS->bytepos corresponding to
1489 NEWPOS->charpos. POS is a known position in string STRING.
1490 NEWPOS->charpos must be >= POS.charpos. */
1492 static void
1493 compute_string_pos (newpos, pos, string)
1494 struct text_pos *newpos, pos;
1495 Lisp_Object string;
1497 xassert (STRINGP (string));
1498 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1500 if (STRING_MULTIBYTE (string))
1501 *newpos = string_pos_nchars_ahead (pos, string,
1502 CHARPOS (*newpos) - CHARPOS (pos));
1503 else
1504 BYTEPOS (*newpos) = CHARPOS (*newpos);
1507 /* EXPORT:
1508 Return an estimation of the pixel height of mode or top lines on
1509 frame F. FACE_ID specifies what line's height to estimate. */
1512 estimate_mode_line_height (f, face_id)
1513 struct frame *f;
1514 enum face_id face_id;
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1535 return height;
1537 #endif
1539 return 1;
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1547 void
1548 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1549 FRAME_PTR f;
1550 register int pix_x, pix_y;
1551 int *x, *y;
1552 NativeRectangle *bounds;
1553 int noclip;
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1559 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1560 even for negative values. */
1561 if (pix_x < 0)
1562 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1563 if (pix_y < 0)
1564 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1566 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1567 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1569 if (bounds)
1570 STORE_NATIVE_RECT (*bounds,
1571 FRAME_COL_TO_PIXEL_X (f, pix_x),
1572 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1573 FRAME_COLUMN_WIDTH (f) - 1,
1574 FRAME_LINE_HEIGHT (f) - 1);
1576 if (!noclip)
1578 if (pix_x < 0)
1579 pix_x = 0;
1580 else if (pix_x > FRAME_TOTAL_COLS (f))
1581 pix_x = FRAME_TOTAL_COLS (f);
1583 if (pix_y < 0)
1584 pix_y = 0;
1585 else if (pix_y > FRAME_LINES (f))
1586 pix_y = FRAME_LINES (f);
1589 #endif
1591 *x = pix_x;
1592 *y = pix_y;
1596 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1597 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1598 can't tell the positions because W's display is not up to date,
1599 return 0. */
1602 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1603 struct window *w;
1604 int hpos, vpos;
1605 int *frame_x, *frame_y;
1607 #ifdef HAVE_WINDOW_SYSTEM
1608 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1610 int success_p;
1612 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1613 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1615 if (display_completed)
1617 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1618 struct glyph *glyph = row->glyphs[TEXT_AREA];
1619 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1621 hpos = row->x;
1622 vpos = row->y;
1623 while (glyph < end)
1625 hpos += glyph->pixel_width;
1626 ++glyph;
1629 /* If first glyph is partially visible, its first visible position is still 0. */
1630 if (hpos < 0)
1631 hpos = 0;
1633 success_p = 1;
1635 else
1637 hpos = vpos = 0;
1638 success_p = 0;
1641 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1642 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1643 return success_p;
1645 #endif
1647 *frame_x = hpos;
1648 *frame_y = vpos;
1649 return 1;
1653 #ifdef HAVE_WINDOW_SYSTEM
1655 /* Find the glyph under window-relative coordinates X/Y in window W.
1656 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1657 strings. Return in *HPOS and *VPOS the row and column number of
1658 the glyph found. Return in *AREA the glyph area containing X.
1659 Value is a pointer to the glyph found or null if X/Y is not on
1660 text, or we can't tell because W's current matrix is not up to
1661 date. */
1663 static struct glyph *
1664 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1665 struct window *w;
1666 int x, y;
1667 int *hpos, *vpos, *dx, *dy, *area;
1669 struct glyph *glyph, *end;
1670 struct glyph_row *row = NULL;
1671 int x0, i;
1673 /* Find row containing Y. Give up if some row is not enabled. */
1674 for (i = 0; i < w->current_matrix->nrows; ++i)
1676 row = MATRIX_ROW (w->current_matrix, i);
1677 if (!row->enabled_p)
1678 return NULL;
1679 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1680 break;
1683 *vpos = i;
1684 *hpos = 0;
1686 /* Give up if Y is not in the window. */
1687 if (i == w->current_matrix->nrows)
1688 return NULL;
1690 /* Get the glyph area containing X. */
1691 if (w->pseudo_window_p)
1693 *area = TEXT_AREA;
1694 x0 = 0;
1696 else
1698 if (x < window_box_left_offset (w, TEXT_AREA))
1700 *area = LEFT_MARGIN_AREA;
1701 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1703 else if (x < window_box_right_offset (w, TEXT_AREA))
1705 *area = TEXT_AREA;
1706 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1708 else
1710 *area = RIGHT_MARGIN_AREA;
1711 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1715 /* Find glyph containing X. */
1716 glyph = row->glyphs[*area];
1717 end = glyph + row->used[*area];
1718 x -= x0;
1719 while (glyph < end && x >= glyph->pixel_width)
1721 x -= glyph->pixel_width;
1722 ++glyph;
1725 if (glyph == end)
1726 return NULL;
1728 if (dx)
1730 *dx = x;
1731 *dy = y - (row->y + row->ascent - glyph->ascent);
1734 *hpos = glyph - row->glyphs[*area];
1735 return glyph;
1739 /* EXPORT:
1740 Convert frame-relative x/y to coordinates relative to window W.
1741 Takes pseudo-windows into account. */
1743 void
1744 frame_to_window_pixel_xy (w, x, y)
1745 struct window *w;
1746 int *x, *y;
1748 if (w->pseudo_window_p)
1750 /* A pseudo-window is always full-width, and starts at the
1751 left edge of the frame, plus a frame border. */
1752 struct frame *f = XFRAME (w->frame);
1753 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1754 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1756 else
1758 *x -= WINDOW_LEFT_EDGE_X (w);
1759 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1763 /* EXPORT:
1764 Return in RECTS[] at most N clipping rectangles for glyph string S.
1765 Return the number of stored rectangles. */
1768 get_glyph_string_clip_rects (s, rects, n)
1769 struct glyph_string *s;
1770 NativeRectangle *rects;
1771 int n;
1773 XRectangle r;
1775 if (n <= 0)
1776 return 0;
1778 if (s->row->full_width_p)
1780 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1781 r.x = WINDOW_LEFT_EDGE_X (s->w);
1782 r.width = WINDOW_TOTAL_WIDTH (s->w);
1784 /* Unless displaying a mode or menu bar line, which are always
1785 fully visible, clip to the visible part of the row. */
1786 if (s->w->pseudo_window_p)
1787 r.height = s->row->visible_height;
1788 else
1789 r.height = s->height;
1791 else
1793 /* This is a text line that may be partially visible. */
1794 r.x = window_box_left (s->w, s->area);
1795 r.width = window_box_width (s->w, s->area);
1796 r.height = s->row->visible_height;
1799 if (s->clip_head)
1800 if (r.x < s->clip_head->x)
1802 if (r.width >= s->clip_head->x - r.x)
1803 r.width -= s->clip_head->x - r.x;
1804 else
1805 r.width = 0;
1806 r.x = s->clip_head->x;
1808 if (s->clip_tail)
1809 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1811 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1812 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1813 else
1814 r.width = 0;
1817 /* If S draws overlapping rows, it's sufficient to use the top and
1818 bottom of the window for clipping because this glyph string
1819 intentionally draws over other lines. */
1820 if (s->for_overlaps)
1822 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1823 r.height = window_text_bottom_y (s->w) - r.y;
1825 /* Alas, the above simple strategy does not work for the
1826 environments with anti-aliased text: if the same text is
1827 drawn onto the same place multiple times, it gets thicker.
1828 If the overlap we are processing is for the erased cursor, we
1829 take the intersection with the rectagle of the cursor. */
1830 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1832 XRectangle rc, r_save = r;
1834 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1835 rc.y = s->w->phys_cursor.y;
1836 rc.width = s->w->phys_cursor_width;
1837 rc.height = s->w->phys_cursor_height;
1839 x_intersect_rectangles (&r_save, &rc, &r);
1842 else
1844 /* Don't use S->y for clipping because it doesn't take partially
1845 visible lines into account. For example, it can be negative for
1846 partially visible lines at the top of a window. */
1847 if (!s->row->full_width_p
1848 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1849 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1850 else
1851 r.y = max (0, s->row->y);
1853 /* If drawing a tool-bar window, draw it over the internal border
1854 at the top of the window. */
1855 if (WINDOWP (s->f->tool_bar_window)
1856 && s->w == XWINDOW (s->f->tool_bar_window))
1857 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1860 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1862 /* If drawing the cursor, don't let glyph draw outside its
1863 advertised boundaries. Cleartype does this under some circumstances. */
1864 if (s->hl == DRAW_CURSOR)
1866 struct glyph *glyph = s->first_glyph;
1867 int height, max_y;
1869 if (s->x > r.x)
1871 r.width -= s->x - r.x;
1872 r.x = s->x;
1874 r.width = min (r.width, glyph->pixel_width);
1876 /* If r.y is below window bottom, ensure that we still see a cursor. */
1877 height = min (glyph->ascent + glyph->descent,
1878 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1879 max_y = window_text_bottom_y (s->w) - height;
1880 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1881 if (s->ybase - glyph->ascent > max_y)
1883 r.y = max_y;
1884 r.height = height;
1886 else
1888 /* Don't draw cursor glyph taller than our actual glyph. */
1889 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1890 if (height < r.height)
1892 max_y = r.y + r.height;
1893 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1894 r.height = min (max_y - r.y, height);
1899 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1900 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1902 #ifdef CONVERT_FROM_XRECT
1903 CONVERT_FROM_XRECT (r, *rects);
1904 #else
1905 *rects = r;
1906 #endif
1907 return 1;
1909 else
1911 /* If we are processing overlapping and allowed to return
1912 multiple clipping rectangles, we exclude the row of the glyph
1913 string from the clipping rectangle. This is to avoid drawing
1914 the same text on the environment with anti-aliasing. */
1915 #ifdef CONVERT_FROM_XRECT
1916 XRectangle rs[2];
1917 #else
1918 XRectangle *rs = rects;
1919 #endif
1920 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1922 if (s->for_overlaps & OVERLAPS_PRED)
1924 rs[i] = r;
1925 if (r.y + r.height > row_y)
1927 if (r.y < row_y)
1928 rs[i].height = row_y - r.y;
1929 else
1930 rs[i].height = 0;
1932 i++;
1934 if (s->for_overlaps & OVERLAPS_SUCC)
1936 rs[i] = r;
1937 if (r.y < row_y + s->row->visible_height)
1939 if (r.y + r.height > row_y + s->row->visible_height)
1941 rs[i].y = row_y + s->row->visible_height;
1942 rs[i].height = r.y + r.height - rs[i].y;
1944 else
1945 rs[i].height = 0;
1947 i++;
1950 n = i;
1951 #ifdef CONVERT_FROM_XRECT
1952 for (i = 0; i < n; i++)
1953 CONVERT_FROM_XRECT (rs[i], rects[i]);
1954 #endif
1955 return n;
1959 /* EXPORT:
1960 Return in *NR the clipping rectangle for glyph string S. */
1962 void
1963 get_glyph_string_clip_rect (s, nr)
1964 struct glyph_string *s;
1965 NativeRectangle *nr;
1967 get_glyph_string_clip_rects (s, nr, 1);
1971 /* EXPORT:
1972 Return the position and height of the phys cursor in window W.
1973 Set w->phys_cursor_width to width of phys cursor.
1977 get_phys_cursor_geometry (w, row, glyph, heightp)
1978 struct window *w;
1979 struct glyph_row *row;
1980 struct glyph *glyph;
1981 int *heightp;
1983 struct frame *f = XFRAME (WINDOW_FRAME (w));
1984 int y, wd, h, h0, y0;
1986 /* Compute the width of the rectangle to draw. If on a stretch
1987 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1988 rectangle as wide as the glyph, but use a canonical character
1989 width instead. */
1990 wd = glyph->pixel_width - 1;
1991 #ifdef HAVE_NTGUI
1992 wd++; /* Why? */
1993 #endif
1994 if (glyph->type == STRETCH_GLYPH
1995 && !x_stretch_cursor_p)
1996 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1997 w->phys_cursor_width = wd;
1999 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2001 /* If y is below window bottom, ensure that we still see a cursor. */
2002 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2004 h = max (h0, glyph->ascent + glyph->descent);
2005 h0 = min (h0, glyph->ascent + glyph->descent);
2007 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2008 if (y < y0)
2010 h = max (h - (y0 - y) + 1, h0);
2011 y = y0 - 1;
2013 else
2015 y0 = window_text_bottom_y (w) - h0;
2016 if (y > y0)
2018 h += y - y0;
2019 y = y0;
2023 *heightp = h;
2024 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2028 * Remember which glyph the mouse is over.
2031 void
2032 remember_mouse_glyph (f, gx, gy, rect)
2033 struct frame *f;
2034 int gx, gy;
2035 NativeRectangle *rect;
2037 Lisp_Object window;
2038 struct window *w;
2039 struct glyph_row *r, *gr, *end_row;
2040 enum window_part part;
2041 enum glyph_row_area area;
2042 int x, y, width, height;
2044 /* Try to determine frame pixel position and size of the glyph under
2045 frame pixel coordinates X/Y on frame F. */
2047 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2048 if (NILP (window))
2050 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2051 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2052 goto virtual_glyph;
2055 w = XWINDOW (window);
2056 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2057 height = WINDOW_FRAME_LINE_HEIGHT (w);
2059 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2060 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2062 if (w->pseudo_window_p)
2064 area = TEXT_AREA;
2065 part = ON_MODE_LINE; /* Don't adjust margin. */
2066 goto text_glyph;
2069 switch (part)
2071 case ON_LEFT_MARGIN:
2072 area = LEFT_MARGIN_AREA;
2073 goto text_glyph;
2075 case ON_RIGHT_MARGIN:
2076 area = RIGHT_MARGIN_AREA;
2077 goto text_glyph;
2079 case ON_HEADER_LINE:
2080 case ON_MODE_LINE:
2081 gr = (part == ON_HEADER_LINE
2082 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2083 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2084 gy = gr->y;
2085 area = TEXT_AREA;
2086 goto text_glyph_row_found;
2088 case ON_TEXT:
2089 area = TEXT_AREA;
2091 text_glyph:
2092 gr = 0; gy = 0;
2093 for (; r <= end_row && r->enabled_p; ++r)
2094 if (r->y + r->height > y)
2096 gr = r; gy = r->y;
2097 break;
2100 text_glyph_row_found:
2101 if (gr && gy <= y)
2103 struct glyph *g = gr->glyphs[area];
2104 struct glyph *end = g + gr->used[area];
2106 height = gr->height;
2107 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2108 if (gx + g->pixel_width > x)
2109 break;
2111 if (g < end)
2113 if (g->type == IMAGE_GLYPH)
2115 /* Don't remember when mouse is over image, as
2116 image may have hot-spots. */
2117 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2118 return;
2120 width = g->pixel_width;
2122 else
2124 /* Use nominal char spacing at end of line. */
2125 x -= gx;
2126 gx += (x / width) * width;
2129 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2130 gx += window_box_left_offset (w, area);
2132 else
2134 /* Use nominal line height at end of window. */
2135 gx = (x / width) * width;
2136 y -= gy;
2137 gy += (y / height) * height;
2139 break;
2141 case ON_LEFT_FRINGE:
2142 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2143 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2144 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2145 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2146 goto row_glyph;
2148 case ON_RIGHT_FRINGE:
2149 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2150 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2151 : window_box_right_offset (w, TEXT_AREA));
2152 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2153 goto row_glyph;
2155 case ON_SCROLL_BAR:
2156 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2158 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2159 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2160 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2161 : 0)));
2162 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2164 row_glyph:
2165 gr = 0, gy = 0;
2166 for (; r <= end_row && r->enabled_p; ++r)
2167 if (r->y + r->height > y)
2169 gr = r; gy = r->y;
2170 break;
2173 if (gr && gy <= y)
2174 height = gr->height;
2175 else
2177 /* Use nominal line height at end of window. */
2178 y -= gy;
2179 gy += (y / height) * height;
2181 break;
2183 default:
2185 virtual_glyph:
2186 /* If there is no glyph under the mouse, then we divide the screen
2187 into a grid of the smallest glyph in the frame, and use that
2188 as our "glyph". */
2190 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2191 round down even for negative values. */
2192 if (gx < 0)
2193 gx -= width - 1;
2194 if (gy < 0)
2195 gy -= height - 1;
2197 gx = (gx / width) * width;
2198 gy = (gy / height) * height;
2200 goto store_rect;
2203 gx += WINDOW_LEFT_EDGE_X (w);
2204 gy += WINDOW_TOP_EDGE_Y (w);
2206 store_rect:
2207 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2209 /* Visible feedback for debugging. */
2210 #if 0
2211 #if HAVE_X_WINDOWS
2212 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2213 f->output_data.x->normal_gc,
2214 gx, gy, width, height);
2215 #endif
2216 #endif
2220 #endif /* HAVE_WINDOW_SYSTEM */
2223 /***********************************************************************
2224 Lisp form evaluation
2225 ***********************************************************************/
2227 /* Error handler for safe_eval and safe_call. */
2229 static Lisp_Object
2230 safe_eval_handler (arg)
2231 Lisp_Object arg;
2233 add_to_log ("Error during redisplay: %s", arg, Qnil);
2234 return Qnil;
2238 /* Evaluate SEXPR and return the result, or nil if something went
2239 wrong. Prevent redisplay during the evaluation. */
2241 Lisp_Object
2242 safe_eval (sexpr)
2243 Lisp_Object sexpr;
2245 Lisp_Object val;
2247 if (inhibit_eval_during_redisplay)
2248 val = Qnil;
2249 else
2251 int count = SPECPDL_INDEX ();
2252 struct gcpro gcpro1;
2254 GCPRO1 (sexpr);
2255 specbind (Qinhibit_redisplay, Qt);
2256 /* Use Qt to ensure debugger does not run,
2257 so there is no possibility of wanting to redisplay. */
2258 val = internal_condition_case_1 (Feval, sexpr, Qt,
2259 safe_eval_handler);
2260 UNGCPRO;
2261 val = unbind_to (count, val);
2264 return val;
2268 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2269 Return the result, or nil if something went wrong. Prevent
2270 redisplay during the evaluation. */
2272 Lisp_Object
2273 safe_call (nargs, args)
2274 int nargs;
2275 Lisp_Object *args;
2277 Lisp_Object val;
2279 if (inhibit_eval_during_redisplay)
2280 val = Qnil;
2281 else
2283 int count = SPECPDL_INDEX ();
2284 struct gcpro gcpro1;
2286 GCPRO1 (args[0]);
2287 gcpro1.nvars = nargs;
2288 specbind (Qinhibit_redisplay, Qt);
2289 /* Use Qt to ensure debugger does not run,
2290 so there is no possibility of wanting to redisplay. */
2291 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2292 safe_eval_handler);
2293 UNGCPRO;
2294 val = unbind_to (count, val);
2297 return val;
2301 /* Call function FN with one argument ARG.
2302 Return the result, or nil if something went wrong. */
2304 Lisp_Object
2305 safe_call1 (fn, arg)
2306 Lisp_Object fn, arg;
2308 Lisp_Object args[2];
2309 args[0] = fn;
2310 args[1] = arg;
2311 return safe_call (2, args);
2316 /***********************************************************************
2317 Debugging
2318 ***********************************************************************/
2320 #if 0
2322 /* Define CHECK_IT to perform sanity checks on iterators.
2323 This is for debugging. It is too slow to do unconditionally. */
2325 static void
2326 check_it (it)
2327 struct it *it;
2329 if (it->method == GET_FROM_STRING)
2331 xassert (STRINGP (it->string));
2332 xassert (IT_STRING_CHARPOS (*it) >= 0);
2334 else
2336 xassert (IT_STRING_CHARPOS (*it) < 0);
2337 if (it->method == GET_FROM_BUFFER)
2339 /* Check that character and byte positions agree. */
2340 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2344 if (it->dpvec)
2345 xassert (it->current.dpvec_index >= 0);
2346 else
2347 xassert (it->current.dpvec_index < 0);
2350 #define CHECK_IT(IT) check_it ((IT))
2352 #else /* not 0 */
2354 #define CHECK_IT(IT) (void) 0
2356 #endif /* not 0 */
2359 #if GLYPH_DEBUG
2361 /* Check that the window end of window W is what we expect it
2362 to be---the last row in the current matrix displaying text. */
2364 static void
2365 check_window_end (w)
2366 struct window *w;
2368 if (!MINI_WINDOW_P (w)
2369 && !NILP (w->window_end_valid))
2371 struct glyph_row *row;
2372 xassert ((row = MATRIX_ROW (w->current_matrix,
2373 XFASTINT (w->window_end_vpos)),
2374 !row->enabled_p
2375 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2376 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2380 #define CHECK_WINDOW_END(W) check_window_end ((W))
2382 #else /* not GLYPH_DEBUG */
2384 #define CHECK_WINDOW_END(W) (void) 0
2386 #endif /* not GLYPH_DEBUG */
2390 /***********************************************************************
2391 Iterator initialization
2392 ***********************************************************************/
2394 /* Initialize IT for displaying current_buffer in window W, starting
2395 at character position CHARPOS. CHARPOS < 0 means that no buffer
2396 position is specified which is useful when the iterator is assigned
2397 a position later. BYTEPOS is the byte position corresponding to
2398 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2400 If ROW is not null, calls to produce_glyphs with IT as parameter
2401 will produce glyphs in that row.
2403 BASE_FACE_ID is the id of a base face to use. It must be one of
2404 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2405 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2406 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2408 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2409 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2410 will be initialized to use the corresponding mode line glyph row of
2411 the desired matrix of W. */
2413 void
2414 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2415 struct it *it;
2416 struct window *w;
2417 int charpos, bytepos;
2418 struct glyph_row *row;
2419 enum face_id base_face_id;
2421 int highlight_region_p;
2423 /* Some precondition checks. */
2424 xassert (w != NULL && it != NULL);
2425 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2426 && charpos <= ZV));
2428 /* If face attributes have been changed since the last redisplay,
2429 free realized faces now because they depend on face definitions
2430 that might have changed. Don't free faces while there might be
2431 desired matrices pending which reference these faces. */
2432 if (face_change_count && !inhibit_free_realized_faces)
2434 face_change_count = 0;
2435 free_all_realized_faces (Qnil);
2438 /* Use one of the mode line rows of W's desired matrix if
2439 appropriate. */
2440 if (row == NULL)
2442 if (base_face_id == MODE_LINE_FACE_ID
2443 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2444 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2445 else if (base_face_id == HEADER_LINE_FACE_ID)
2446 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2449 /* Clear IT. */
2450 bzero (it, sizeof *it);
2451 it->current.overlay_string_index = -1;
2452 it->current.dpvec_index = -1;
2453 it->base_face_id = base_face_id;
2454 it->string = Qnil;
2455 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2457 /* The window in which we iterate over current_buffer: */
2458 XSETWINDOW (it->window, w);
2459 it->w = w;
2460 it->f = XFRAME (w->frame);
2462 /* Extra space between lines (on window systems only). */
2463 if (base_face_id == DEFAULT_FACE_ID
2464 && FRAME_WINDOW_P (it->f))
2466 if (NATNUMP (current_buffer->extra_line_spacing))
2467 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2468 else if (FLOATP (current_buffer->extra_line_spacing))
2469 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2470 * FRAME_LINE_HEIGHT (it->f));
2471 else if (it->f->extra_line_spacing > 0)
2472 it->extra_line_spacing = it->f->extra_line_spacing;
2473 it->max_extra_line_spacing = 0;
2476 /* If realized faces have been removed, e.g. because of face
2477 attribute changes of named faces, recompute them. When running
2478 in batch mode, the face cache of the initial frame is null. If
2479 we happen to get called, make a dummy face cache. */
2480 if (FRAME_FACE_CACHE (it->f) == NULL)
2481 init_frame_faces (it->f);
2482 if (FRAME_FACE_CACHE (it->f)->used == 0)
2483 recompute_basic_faces (it->f);
2485 /* Current value of the `slice', `space-width', and 'height' properties. */
2486 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2487 it->space_width = Qnil;
2488 it->font_height = Qnil;
2489 it->override_ascent = -1;
2491 /* Are control characters displayed as `^C'? */
2492 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2494 /* -1 means everything between a CR and the following line end
2495 is invisible. >0 means lines indented more than this value are
2496 invisible. */
2497 it->selective = (INTEGERP (current_buffer->selective_display)
2498 ? XFASTINT (current_buffer->selective_display)
2499 : (!NILP (current_buffer->selective_display)
2500 ? -1 : 0));
2501 it->selective_display_ellipsis_p
2502 = !NILP (current_buffer->selective_display_ellipses);
2504 /* Display table to use. */
2505 it->dp = window_display_table (w);
2507 /* Are multibyte characters enabled in current_buffer? */
2508 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2510 /* Non-zero if we should highlight the region. */
2511 highlight_region_p
2512 = (!NILP (Vtransient_mark_mode)
2513 && !NILP (current_buffer->mark_active)
2514 && XMARKER (current_buffer->mark)->buffer != 0);
2516 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2517 start and end of a visible region in window IT->w. Set both to
2518 -1 to indicate no region. */
2519 if (highlight_region_p
2520 /* Maybe highlight only in selected window. */
2521 && (/* Either show region everywhere. */
2522 highlight_nonselected_windows
2523 /* Or show region in the selected window. */
2524 || w == XWINDOW (selected_window)
2525 /* Or show the region if we are in the mini-buffer and W is
2526 the window the mini-buffer refers to. */
2527 || (MINI_WINDOW_P (XWINDOW (selected_window))
2528 && WINDOWP (minibuf_selected_window)
2529 && w == XWINDOW (minibuf_selected_window))))
2531 int charpos = marker_position (current_buffer->mark);
2532 it->region_beg_charpos = min (PT, charpos);
2533 it->region_end_charpos = max (PT, charpos);
2535 else
2536 it->region_beg_charpos = it->region_end_charpos = -1;
2538 /* Get the position at which the redisplay_end_trigger hook should
2539 be run, if it is to be run at all. */
2540 if (MARKERP (w->redisplay_end_trigger)
2541 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2542 it->redisplay_end_trigger_charpos
2543 = marker_position (w->redisplay_end_trigger);
2544 else if (INTEGERP (w->redisplay_end_trigger))
2545 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2547 /* Correct bogus values of tab_width. */
2548 it->tab_width = XINT (current_buffer->tab_width);
2549 if (it->tab_width <= 0 || it->tab_width > 1000)
2550 it->tab_width = 8;
2552 /* Are lines in the display truncated? */
2553 it->truncate_lines_p
2554 = (base_face_id != DEFAULT_FACE_ID
2555 || XINT (it->w->hscroll)
2556 || (truncate_partial_width_windows
2557 && !WINDOW_FULL_WIDTH_P (it->w))
2558 || !NILP (current_buffer->truncate_lines));
2560 /* Get dimensions of truncation and continuation glyphs. These are
2561 displayed as fringe bitmaps under X, so we don't need them for such
2562 frames. */
2563 if (!FRAME_WINDOW_P (it->f))
2565 if (it->truncate_lines_p)
2567 /* We will need the truncation glyph. */
2568 xassert (it->glyph_row == NULL);
2569 produce_special_glyphs (it, IT_TRUNCATION);
2570 it->truncation_pixel_width = it->pixel_width;
2572 else
2574 /* We will need the continuation glyph. */
2575 xassert (it->glyph_row == NULL);
2576 produce_special_glyphs (it, IT_CONTINUATION);
2577 it->continuation_pixel_width = it->pixel_width;
2580 /* Reset these values to zero because the produce_special_glyphs
2581 above has changed them. */
2582 it->pixel_width = it->ascent = it->descent = 0;
2583 it->phys_ascent = it->phys_descent = 0;
2586 /* Set this after getting the dimensions of truncation and
2587 continuation glyphs, so that we don't produce glyphs when calling
2588 produce_special_glyphs, above. */
2589 it->glyph_row = row;
2590 it->area = TEXT_AREA;
2592 /* Get the dimensions of the display area. The display area
2593 consists of the visible window area plus a horizontally scrolled
2594 part to the left of the window. All x-values are relative to the
2595 start of this total display area. */
2596 if (base_face_id != DEFAULT_FACE_ID)
2598 /* Mode lines, menu bar in terminal frames. */
2599 it->first_visible_x = 0;
2600 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2602 else
2604 it->first_visible_x
2605 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2606 it->last_visible_x = (it->first_visible_x
2607 + window_box_width (w, TEXT_AREA));
2609 /* If we truncate lines, leave room for the truncator glyph(s) at
2610 the right margin. Otherwise, leave room for the continuation
2611 glyph(s). Truncation and continuation glyphs are not inserted
2612 for window-based redisplay. */
2613 if (!FRAME_WINDOW_P (it->f))
2615 if (it->truncate_lines_p)
2616 it->last_visible_x -= it->truncation_pixel_width;
2617 else
2618 it->last_visible_x -= it->continuation_pixel_width;
2621 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2622 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2625 /* Leave room for a border glyph. */
2626 if (!FRAME_WINDOW_P (it->f)
2627 && !WINDOW_RIGHTMOST_P (it->w))
2628 it->last_visible_x -= 1;
2630 it->last_visible_y = window_text_bottom_y (w);
2632 /* For mode lines and alike, arrange for the first glyph having a
2633 left box line if the face specifies a box. */
2634 if (base_face_id != DEFAULT_FACE_ID)
2636 struct face *face;
2638 it->face_id = base_face_id;
2640 /* If we have a boxed mode line, make the first character appear
2641 with a left box line. */
2642 face = FACE_FROM_ID (it->f, base_face_id);
2643 if (face->box != FACE_NO_BOX)
2644 it->start_of_box_run_p = 1;
2647 /* If a buffer position was specified, set the iterator there,
2648 getting overlays and face properties from that position. */
2649 if (charpos >= BUF_BEG (current_buffer))
2651 it->end_charpos = ZV;
2652 it->face_id = -1;
2653 IT_CHARPOS (*it) = charpos;
2655 /* Compute byte position if not specified. */
2656 if (bytepos < charpos)
2657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2658 else
2659 IT_BYTEPOS (*it) = bytepos;
2661 it->start = it->current;
2663 /* Compute faces etc. */
2664 reseat (it, it->current.pos, 1);
2667 CHECK_IT (it);
2671 /* Initialize IT for the display of window W with window start POS. */
2673 void
2674 start_display (it, w, pos)
2675 struct it *it;
2676 struct window *w;
2677 struct text_pos pos;
2679 struct glyph_row *row;
2680 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2682 row = w->desired_matrix->rows + first_vpos;
2683 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2684 it->first_vpos = first_vpos;
2686 /* Don't reseat to previous visible line start if current start
2687 position is in a string or image. */
2688 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2690 int start_at_line_beg_p;
2691 int first_y = it->current_y;
2693 /* If window start is not at a line start, skip forward to POS to
2694 get the correct continuation lines width. */
2695 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2696 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2697 if (!start_at_line_beg_p)
2699 int new_x;
2701 reseat_at_previous_visible_line_start (it);
2702 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2704 new_x = it->current_x + it->pixel_width;
2706 /* If lines are continued, this line may end in the middle
2707 of a multi-glyph character (e.g. a control character
2708 displayed as \003, or in the middle of an overlay
2709 string). In this case move_it_to above will not have
2710 taken us to the start of the continuation line but to the
2711 end of the continued line. */
2712 if (it->current_x > 0
2713 && !it->truncate_lines_p /* Lines are continued. */
2714 && (/* And glyph doesn't fit on the line. */
2715 new_x > it->last_visible_x
2716 /* Or it fits exactly and we're on a window
2717 system frame. */
2718 || (new_x == it->last_visible_x
2719 && FRAME_WINDOW_P (it->f))))
2721 if (it->current.dpvec_index >= 0
2722 || it->current.overlay_string_index >= 0)
2724 set_iterator_to_next (it, 1);
2725 move_it_in_display_line_to (it, -1, -1, 0);
2728 it->continuation_lines_width += it->current_x;
2731 /* We're starting a new display line, not affected by the
2732 height of the continued line, so clear the appropriate
2733 fields in the iterator structure. */
2734 it->max_ascent = it->max_descent = 0;
2735 it->max_phys_ascent = it->max_phys_descent = 0;
2737 it->current_y = first_y;
2738 it->vpos = 0;
2739 it->current_x = it->hpos = 0;
2743 #if 0 /* Don't assert the following because start_display is sometimes
2744 called intentionally with a window start that is not at a
2745 line start. Please leave this code in as a comment. */
2747 /* Window start should be on a line start, now. */
2748 xassert (it->continuation_lines_width
2749 || IT_CHARPOS (it) == BEGV
2750 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2751 #endif /* 0 */
2755 /* Return 1 if POS is a position in ellipses displayed for invisible
2756 text. W is the window we display, for text property lookup. */
2758 static int
2759 in_ellipses_for_invisible_text_p (pos, w)
2760 struct display_pos *pos;
2761 struct window *w;
2763 Lisp_Object prop, window;
2764 int ellipses_p = 0;
2765 int charpos = CHARPOS (pos->pos);
2767 /* If POS specifies a position in a display vector, this might
2768 be for an ellipsis displayed for invisible text. We won't
2769 get the iterator set up for delivering that ellipsis unless
2770 we make sure that it gets aware of the invisible text. */
2771 if (pos->dpvec_index >= 0
2772 && pos->overlay_string_index < 0
2773 && CHARPOS (pos->string_pos) < 0
2774 && charpos > BEGV
2775 && (XSETWINDOW (window, w),
2776 prop = Fget_char_property (make_number (charpos),
2777 Qinvisible, window),
2778 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2780 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2781 window);
2782 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2785 return ellipses_p;
2789 /* Initialize IT for stepping through current_buffer in window W,
2790 starting at position POS that includes overlay string and display
2791 vector/ control character translation position information. Value
2792 is zero if there are overlay strings with newlines at POS. */
2794 static int
2795 init_from_display_pos (it, w, pos)
2796 struct it *it;
2797 struct window *w;
2798 struct display_pos *pos;
2800 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2801 int i, overlay_strings_with_newlines = 0;
2803 /* If POS specifies a position in a display vector, this might
2804 be for an ellipsis displayed for invisible text. We won't
2805 get the iterator set up for delivering that ellipsis unless
2806 we make sure that it gets aware of the invisible text. */
2807 if (in_ellipses_for_invisible_text_p (pos, w))
2809 --charpos;
2810 bytepos = 0;
2813 /* Keep in mind: the call to reseat in init_iterator skips invisible
2814 text, so we might end up at a position different from POS. This
2815 is only a problem when POS is a row start after a newline and an
2816 overlay starts there with an after-string, and the overlay has an
2817 invisible property. Since we don't skip invisible text in
2818 display_line and elsewhere immediately after consuming the
2819 newline before the row start, such a POS will not be in a string,
2820 but the call to init_iterator below will move us to the
2821 after-string. */
2822 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2824 /* This only scans the current chunk -- it should scan all chunks.
2825 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2826 to 16 in 22.1 to make this a lesser problem. */
2827 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2829 const char *s = SDATA (it->overlay_strings[i]);
2830 const char *e = s + SBYTES (it->overlay_strings[i]);
2832 while (s < e && *s != '\n')
2833 ++s;
2835 if (s < e)
2837 overlay_strings_with_newlines = 1;
2838 break;
2842 /* If position is within an overlay string, set up IT to the right
2843 overlay string. */
2844 if (pos->overlay_string_index >= 0)
2846 int relative_index;
2848 /* If the first overlay string happens to have a `display'
2849 property for an image, the iterator will be set up for that
2850 image, and we have to undo that setup first before we can
2851 correct the overlay string index. */
2852 if (it->method == GET_FROM_IMAGE)
2853 pop_it (it);
2855 /* We already have the first chunk of overlay strings in
2856 IT->overlay_strings. Load more until the one for
2857 pos->overlay_string_index is in IT->overlay_strings. */
2858 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2860 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2861 it->current.overlay_string_index = 0;
2862 while (n--)
2864 load_overlay_strings (it, 0);
2865 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2869 it->current.overlay_string_index = pos->overlay_string_index;
2870 relative_index = (it->current.overlay_string_index
2871 % OVERLAY_STRING_CHUNK_SIZE);
2872 it->string = it->overlay_strings[relative_index];
2873 xassert (STRINGP (it->string));
2874 it->current.string_pos = pos->string_pos;
2875 it->method = GET_FROM_STRING;
2878 #if 0 /* This is bogus because POS not having an overlay string
2879 position does not mean it's after the string. Example: A
2880 line starting with a before-string and initialization of IT
2881 to the previous row's end position. */
2882 else if (it->current.overlay_string_index >= 0)
2884 /* If POS says we're already after an overlay string ending at
2885 POS, make sure to pop the iterator because it will be in
2886 front of that overlay string. When POS is ZV, we've thereby
2887 also ``processed'' overlay strings at ZV. */
2888 while (it->sp)
2889 pop_it (it);
2890 it->current.overlay_string_index = -1;
2891 it->method = GET_FROM_BUFFER;
2892 if (CHARPOS (pos->pos) == ZV)
2893 it->overlay_strings_at_end_processed_p = 1;
2895 #endif /* 0 */
2897 if (CHARPOS (pos->string_pos) >= 0)
2899 /* Recorded position is not in an overlay string, but in another
2900 string. This can only be a string from a `display' property.
2901 IT should already be filled with that string. */
2902 it->current.string_pos = pos->string_pos;
2903 xassert (STRINGP (it->string));
2906 /* Restore position in display vector translations, control
2907 character translations or ellipses. */
2908 if (pos->dpvec_index >= 0)
2910 if (it->dpvec == NULL)
2911 get_next_display_element (it);
2912 xassert (it->dpvec && it->current.dpvec_index == 0);
2913 it->current.dpvec_index = pos->dpvec_index;
2916 CHECK_IT (it);
2917 return !overlay_strings_with_newlines;
2921 /* Initialize IT for stepping through current_buffer in window W
2922 starting at ROW->start. */
2924 static void
2925 init_to_row_start (it, w, row)
2926 struct it *it;
2927 struct window *w;
2928 struct glyph_row *row;
2930 init_from_display_pos (it, w, &row->start);
2931 it->start = row->start;
2932 it->continuation_lines_width = row->continuation_lines_width;
2933 CHECK_IT (it);
2937 /* Initialize IT for stepping through current_buffer in window W
2938 starting in the line following ROW, i.e. starting at ROW->end.
2939 Value is zero if there are overlay strings with newlines at ROW's
2940 end position. */
2942 static int
2943 init_to_row_end (it, w, row)
2944 struct it *it;
2945 struct window *w;
2946 struct glyph_row *row;
2948 int success = 0;
2950 if (init_from_display_pos (it, w, &row->end))
2952 if (row->continued_p)
2953 it->continuation_lines_width
2954 = row->continuation_lines_width + row->pixel_width;
2955 CHECK_IT (it);
2956 success = 1;
2959 return success;
2965 /***********************************************************************
2966 Text properties
2967 ***********************************************************************/
2969 /* Called when IT reaches IT->stop_charpos. Handle text property and
2970 overlay changes. Set IT->stop_charpos to the next position where
2971 to stop. */
2973 static void
2974 handle_stop (it)
2975 struct it *it;
2977 enum prop_handled handled;
2978 int handle_overlay_change_p;
2979 struct props *p;
2981 it->dpvec = NULL;
2982 it->current.dpvec_index = -1;
2983 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2984 it->ignore_overlay_strings_at_pos_p = 0;
2986 /* Use face of preceding text for ellipsis (if invisible) */
2987 if (it->selective_display_ellipsis_p)
2988 it->saved_face_id = it->face_id;
2992 handled = HANDLED_NORMALLY;
2994 /* Call text property handlers. */
2995 for (p = it_props; p->handler; ++p)
2997 handled = p->handler (it);
2999 if (handled == HANDLED_RECOMPUTE_PROPS)
3000 break;
3001 else if (handled == HANDLED_RETURN)
3002 return;
3003 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3004 handle_overlay_change_p = 0;
3007 if (handled != HANDLED_RECOMPUTE_PROPS)
3009 /* Don't check for overlay strings below when set to deliver
3010 characters from a display vector. */
3011 if (it->method == GET_FROM_DISPLAY_VECTOR)
3012 handle_overlay_change_p = 0;
3014 /* Handle overlay changes. */
3015 if (handle_overlay_change_p)
3016 handled = handle_overlay_change (it);
3018 /* Determine where to stop next. */
3019 if (handled == HANDLED_NORMALLY)
3020 compute_stop_pos (it);
3023 while (handled == HANDLED_RECOMPUTE_PROPS);
3027 /* Compute IT->stop_charpos from text property and overlay change
3028 information for IT's current position. */
3030 static void
3031 compute_stop_pos (it)
3032 struct it *it;
3034 register INTERVAL iv, next_iv;
3035 Lisp_Object object, limit, position;
3037 /* If nowhere else, stop at the end. */
3038 it->stop_charpos = it->end_charpos;
3040 if (STRINGP (it->string))
3042 /* Strings are usually short, so don't limit the search for
3043 properties. */
3044 object = it->string;
3045 limit = Qnil;
3046 position = make_number (IT_STRING_CHARPOS (*it));
3048 else
3050 int charpos;
3052 /* If next overlay change is in front of the current stop pos
3053 (which is IT->end_charpos), stop there. Note: value of
3054 next_overlay_change is point-max if no overlay change
3055 follows. */
3056 charpos = next_overlay_change (IT_CHARPOS (*it));
3057 if (charpos < it->stop_charpos)
3058 it->stop_charpos = charpos;
3060 /* If showing the region, we have to stop at the region
3061 start or end because the face might change there. */
3062 if (it->region_beg_charpos > 0)
3064 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3065 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3066 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3067 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3070 /* Set up variables for computing the stop position from text
3071 property changes. */
3072 XSETBUFFER (object, current_buffer);
3073 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3074 position = make_number (IT_CHARPOS (*it));
3078 /* Get the interval containing IT's position. Value is a null
3079 interval if there isn't such an interval. */
3080 iv = validate_interval_range (object, &position, &position, 0);
3081 if (!NULL_INTERVAL_P (iv))
3083 Lisp_Object values_here[LAST_PROP_IDX];
3084 struct props *p;
3086 /* Get properties here. */
3087 for (p = it_props; p->handler; ++p)
3088 values_here[p->idx] = textget (iv->plist, *p->name);
3090 /* Look for an interval following iv that has different
3091 properties. */
3092 for (next_iv = next_interval (iv);
3093 (!NULL_INTERVAL_P (next_iv)
3094 && (NILP (limit)
3095 || XFASTINT (limit) > next_iv->position));
3096 next_iv = next_interval (next_iv))
3098 for (p = it_props; p->handler; ++p)
3100 Lisp_Object new_value;
3102 new_value = textget (next_iv->plist, *p->name);
3103 if (!EQ (values_here[p->idx], new_value))
3104 break;
3107 if (p->handler)
3108 break;
3111 if (!NULL_INTERVAL_P (next_iv))
3113 if (INTEGERP (limit)
3114 && next_iv->position >= XFASTINT (limit))
3115 /* No text property change up to limit. */
3116 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3117 else
3118 /* Text properties change in next_iv. */
3119 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3123 xassert (STRINGP (it->string)
3124 || (it->stop_charpos >= BEGV
3125 && it->stop_charpos >= IT_CHARPOS (*it)));
3129 /* Return the position of the next overlay change after POS in
3130 current_buffer. Value is point-max if no overlay change
3131 follows. This is like `next-overlay-change' but doesn't use
3132 xmalloc. */
3134 static int
3135 next_overlay_change (pos)
3136 int pos;
3138 int noverlays;
3139 int endpos;
3140 Lisp_Object *overlays;
3141 int i;
3143 /* Get all overlays at the given position. */
3144 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3146 /* If any of these overlays ends before endpos,
3147 use its ending point instead. */
3148 for (i = 0; i < noverlays; ++i)
3150 Lisp_Object oend;
3151 int oendpos;
3153 oend = OVERLAY_END (overlays[i]);
3154 oendpos = OVERLAY_POSITION (oend);
3155 endpos = min (endpos, oendpos);
3158 return endpos;
3163 /***********************************************************************
3164 Fontification
3165 ***********************************************************************/
3167 /* Handle changes in the `fontified' property of the current buffer by
3168 calling hook functions from Qfontification_functions to fontify
3169 regions of text. */
3171 static enum prop_handled
3172 handle_fontified_prop (it)
3173 struct it *it;
3175 Lisp_Object prop, pos;
3176 enum prop_handled handled = HANDLED_NORMALLY;
3178 if (!NILP (Vmemory_full))
3179 return handled;
3181 /* Get the value of the `fontified' property at IT's current buffer
3182 position. (The `fontified' property doesn't have a special
3183 meaning in strings.) If the value is nil, call functions from
3184 Qfontification_functions. */
3185 if (!STRINGP (it->string)
3186 && it->s == NULL
3187 && !NILP (Vfontification_functions)
3188 && !NILP (Vrun_hooks)
3189 && (pos = make_number (IT_CHARPOS (*it)),
3190 prop = Fget_char_property (pos, Qfontified, Qnil),
3191 NILP (prop)))
3193 int count = SPECPDL_INDEX ();
3194 Lisp_Object val;
3196 val = Vfontification_functions;
3197 specbind (Qfontification_functions, Qnil);
3199 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3200 safe_call1 (val, pos);
3201 else
3203 Lisp_Object globals, fn;
3204 struct gcpro gcpro1, gcpro2;
3206 globals = Qnil;
3207 GCPRO2 (val, globals);
3209 for (; CONSP (val); val = XCDR (val))
3211 fn = XCAR (val);
3213 if (EQ (fn, Qt))
3215 /* A value of t indicates this hook has a local
3216 binding; it means to run the global binding too.
3217 In a global value, t should not occur. If it
3218 does, we must ignore it to avoid an endless
3219 loop. */
3220 for (globals = Fdefault_value (Qfontification_functions);
3221 CONSP (globals);
3222 globals = XCDR (globals))
3224 fn = XCAR (globals);
3225 if (!EQ (fn, Qt))
3226 safe_call1 (fn, pos);
3229 else
3230 safe_call1 (fn, pos);
3233 UNGCPRO;
3236 unbind_to (count, Qnil);
3238 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3239 something. This avoids an endless loop if they failed to
3240 fontify the text for which reason ever. */
3241 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3242 handled = HANDLED_RECOMPUTE_PROPS;
3245 return handled;
3250 /***********************************************************************
3251 Faces
3252 ***********************************************************************/
3254 /* Set up iterator IT from face properties at its current position.
3255 Called from handle_stop. */
3257 static enum prop_handled
3258 handle_face_prop (it)
3259 struct it *it;
3261 int new_face_id, next_stop;
3263 if (!STRINGP (it->string))
3265 new_face_id
3266 = face_at_buffer_position (it->w,
3267 IT_CHARPOS (*it),
3268 it->region_beg_charpos,
3269 it->region_end_charpos,
3270 &next_stop,
3271 (IT_CHARPOS (*it)
3272 + TEXT_PROP_DISTANCE_LIMIT),
3275 /* Is this a start of a run of characters with box face?
3276 Caveat: this can be called for a freshly initialized
3277 iterator; face_id is -1 in this case. We know that the new
3278 face will not change until limit, i.e. if the new face has a
3279 box, all characters up to limit will have one. But, as
3280 usual, we don't know whether limit is really the end. */
3281 if (new_face_id != it->face_id)
3283 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3285 /* If new face has a box but old face has not, this is
3286 the start of a run of characters with box, i.e. it has
3287 a shadow on the left side. The value of face_id of the
3288 iterator will be -1 if this is the initial call that gets
3289 the face. In this case, we have to look in front of IT's
3290 position and see whether there is a face != new_face_id. */
3291 it->start_of_box_run_p
3292 = (new_face->box != FACE_NO_BOX
3293 && (it->face_id >= 0
3294 || IT_CHARPOS (*it) == BEG
3295 || new_face_id != face_before_it_pos (it)));
3296 it->face_box_p = new_face->box != FACE_NO_BOX;
3299 else
3301 int base_face_id, bufpos;
3303 if (it->current.overlay_string_index >= 0)
3304 bufpos = IT_CHARPOS (*it);
3305 else
3306 bufpos = 0;
3308 /* For strings from a buffer, i.e. overlay strings or strings
3309 from a `display' property, use the face at IT's current
3310 buffer position as the base face to merge with, so that
3311 overlay strings appear in the same face as surrounding
3312 text, unless they specify their own faces. */
3313 base_face_id = underlying_face_id (it);
3315 new_face_id = face_at_string_position (it->w,
3316 it->string,
3317 IT_STRING_CHARPOS (*it),
3318 bufpos,
3319 it->region_beg_charpos,
3320 it->region_end_charpos,
3321 &next_stop,
3322 base_face_id, 0);
3324 #if 0 /* This shouldn't be neccessary. Let's check it. */
3325 /* If IT is used to display a mode line we would really like to
3326 use the mode line face instead of the frame's default face. */
3327 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3328 && new_face_id == DEFAULT_FACE_ID)
3329 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3330 #endif
3332 /* Is this a start of a run of characters with box? Caveat:
3333 this can be called for a freshly allocated iterator; face_id
3334 is -1 is this case. We know that the new face will not
3335 change until the next check pos, i.e. if the new face has a
3336 box, all characters up to that position will have a
3337 box. But, as usual, we don't know whether that position
3338 is really the end. */
3339 if (new_face_id != it->face_id)
3341 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3342 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3344 /* If new face has a box but old face hasn't, this is the
3345 start of a run of characters with box, i.e. it has a
3346 shadow on the left side. */
3347 it->start_of_box_run_p
3348 = new_face->box && (old_face == NULL || !old_face->box);
3349 it->face_box_p = new_face->box != FACE_NO_BOX;
3353 it->face_id = new_face_id;
3354 return HANDLED_NORMALLY;
3358 /* Return the ID of the face ``underlying'' IT's current position,
3359 which is in a string. If the iterator is associated with a
3360 buffer, return the face at IT's current buffer position.
3361 Otherwise, use the iterator's base_face_id. */
3363 static int
3364 underlying_face_id (it)
3365 struct it *it;
3367 int face_id = it->base_face_id, i;
3369 xassert (STRINGP (it->string));
3371 for (i = it->sp - 1; i >= 0; --i)
3372 if (NILP (it->stack[i].string))
3373 face_id = it->stack[i].face_id;
3375 return face_id;
3379 /* Compute the face one character before or after the current position
3380 of IT. BEFORE_P non-zero means get the face in front of IT's
3381 position. Value is the id of the face. */
3383 static int
3384 face_before_or_after_it_pos (it, before_p)
3385 struct it *it;
3386 int before_p;
3388 int face_id, limit;
3389 int next_check_charpos;
3390 struct text_pos pos;
3392 xassert (it->s == NULL);
3394 if (STRINGP (it->string))
3396 int bufpos, base_face_id;
3398 /* No face change past the end of the string (for the case
3399 we are padding with spaces). No face change before the
3400 string start. */
3401 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3402 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3403 return it->face_id;
3405 /* Set pos to the position before or after IT's current position. */
3406 if (before_p)
3407 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3408 else
3409 /* For composition, we must check the character after the
3410 composition. */
3411 pos = (it->what == IT_COMPOSITION
3412 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3413 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3415 if (it->current.overlay_string_index >= 0)
3416 bufpos = IT_CHARPOS (*it);
3417 else
3418 bufpos = 0;
3420 base_face_id = underlying_face_id (it);
3422 /* Get the face for ASCII, or unibyte. */
3423 face_id = face_at_string_position (it->w,
3424 it->string,
3425 CHARPOS (pos),
3426 bufpos,
3427 it->region_beg_charpos,
3428 it->region_end_charpos,
3429 &next_check_charpos,
3430 base_face_id, 0);
3432 /* Correct the face for charsets different from ASCII. Do it
3433 for the multibyte case only. The face returned above is
3434 suitable for unibyte text if IT->string is unibyte. */
3435 if (STRING_MULTIBYTE (it->string))
3437 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3438 int rest = SBYTES (it->string) - BYTEPOS (pos);
3439 int c, len;
3440 struct face *face = FACE_FROM_ID (it->f, face_id);
3442 c = string_char_and_length (p, rest, &len);
3443 face_id = FACE_FOR_CHAR (it->f, face, c);
3446 else
3448 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3449 || (IT_CHARPOS (*it) <= BEGV && before_p))
3450 return it->face_id;
3452 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3453 pos = it->current.pos;
3455 if (before_p)
3456 DEC_TEXT_POS (pos, it->multibyte_p);
3457 else
3459 if (it->what == IT_COMPOSITION)
3460 /* For composition, we must check the position after the
3461 composition. */
3462 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3463 else
3464 INC_TEXT_POS (pos, it->multibyte_p);
3467 /* Determine face for CHARSET_ASCII, or unibyte. */
3468 face_id = face_at_buffer_position (it->w,
3469 CHARPOS (pos),
3470 it->region_beg_charpos,
3471 it->region_end_charpos,
3472 &next_check_charpos,
3473 limit, 0);
3475 /* Correct the face for charsets different from ASCII. Do it
3476 for the multibyte case only. The face returned above is
3477 suitable for unibyte text if current_buffer is unibyte. */
3478 if (it->multibyte_p)
3480 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3481 struct face *face = FACE_FROM_ID (it->f, face_id);
3482 face_id = FACE_FOR_CHAR (it->f, face, c);
3486 return face_id;
3491 /***********************************************************************
3492 Invisible text
3493 ***********************************************************************/
3495 /* Set up iterator IT from invisible properties at its current
3496 position. Called from handle_stop. */
3498 static enum prop_handled
3499 handle_invisible_prop (it)
3500 struct it *it;
3502 enum prop_handled handled = HANDLED_NORMALLY;
3504 if (STRINGP (it->string))
3506 extern Lisp_Object Qinvisible;
3507 Lisp_Object prop, end_charpos, limit, charpos;
3509 /* Get the value of the invisible text property at the
3510 current position. Value will be nil if there is no such
3511 property. */
3512 charpos = make_number (IT_STRING_CHARPOS (*it));
3513 prop = Fget_text_property (charpos, Qinvisible, it->string);
3515 if (!NILP (prop)
3516 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3518 handled = HANDLED_RECOMPUTE_PROPS;
3520 /* Get the position at which the next change of the
3521 invisible text property can be found in IT->string.
3522 Value will be nil if the property value is the same for
3523 all the rest of IT->string. */
3524 XSETINT (limit, SCHARS (it->string));
3525 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3526 it->string, limit);
3528 /* Text at current position is invisible. The next
3529 change in the property is at position end_charpos.
3530 Move IT's current position to that position. */
3531 if (INTEGERP (end_charpos)
3532 && XFASTINT (end_charpos) < XFASTINT (limit))
3534 struct text_pos old;
3535 old = it->current.string_pos;
3536 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3537 compute_string_pos (&it->current.string_pos, old, it->string);
3539 else
3541 /* The rest of the string is invisible. If this is an
3542 overlay string, proceed with the next overlay string
3543 or whatever comes and return a character from there. */
3544 if (it->current.overlay_string_index >= 0)
3546 next_overlay_string (it);
3547 /* Don't check for overlay strings when we just
3548 finished processing them. */
3549 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3551 else
3553 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3554 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3559 else
3561 int invis_p, newpos, next_stop, start_charpos;
3562 Lisp_Object pos, prop, overlay;
3564 /* First of all, is there invisible text at this position? */
3565 start_charpos = IT_CHARPOS (*it);
3566 pos = make_number (IT_CHARPOS (*it));
3567 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3568 &overlay);
3569 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3571 /* If we are on invisible text, skip over it. */
3572 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3574 /* Record whether we have to display an ellipsis for the
3575 invisible text. */
3576 int display_ellipsis_p = invis_p == 2;
3578 handled = HANDLED_RECOMPUTE_PROPS;
3580 /* Loop skipping over invisible text. The loop is left at
3581 ZV or with IT on the first char being visible again. */
3584 /* Try to skip some invisible text. Return value is the
3585 position reached which can be equal to IT's position
3586 if there is nothing invisible here. This skips both
3587 over invisible text properties and overlays with
3588 invisible property. */
3589 newpos = skip_invisible (IT_CHARPOS (*it),
3590 &next_stop, ZV, it->window);
3592 /* If we skipped nothing at all we weren't at invisible
3593 text in the first place. If everything to the end of
3594 the buffer was skipped, end the loop. */
3595 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3596 invis_p = 0;
3597 else
3599 /* We skipped some characters but not necessarily
3600 all there are. Check if we ended up on visible
3601 text. Fget_char_property returns the property of
3602 the char before the given position, i.e. if we
3603 get invis_p = 0, this means that the char at
3604 newpos is visible. */
3605 pos = make_number (newpos);
3606 prop = Fget_char_property (pos, Qinvisible, it->window);
3607 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3610 /* If we ended up on invisible text, proceed to
3611 skip starting with next_stop. */
3612 if (invis_p)
3613 IT_CHARPOS (*it) = next_stop;
3615 /* If there are adjacent invisible texts, don't lose the
3616 second one's ellipsis. */
3617 if (invis_p == 2)
3618 display_ellipsis_p = 1;
3620 while (invis_p);
3622 /* The position newpos is now either ZV or on visible text. */
3623 IT_CHARPOS (*it) = newpos;
3624 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3626 /* If there are before-strings at the start of invisible
3627 text, and the text is invisible because of a text
3628 property, arrange to show before-strings because 20.x did
3629 it that way. (If the text is invisible because of an
3630 overlay property instead of a text property, this is
3631 already handled in the overlay code.) */
3632 if (NILP (overlay)
3633 && get_overlay_strings (it, start_charpos))
3635 handled = HANDLED_RECOMPUTE_PROPS;
3636 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3638 else if (display_ellipsis_p)
3640 /* Make sure that the glyphs of the ellipsis will get
3641 correct `charpos' values. If we would not update
3642 it->position here, the glyphs would belong to the
3643 last visible character _before_ the invisible
3644 text, which confuses `set_cursor_from_row'.
3646 We use the last invisible position instead of the
3647 first because this way the cursor is always drawn on
3648 the first "." of the ellipsis, whenever PT is inside
3649 the invisible text. Otherwise the cursor would be
3650 placed _after_ the ellipsis when the point is after the
3651 first invisible character. */
3652 if (!STRINGP (it->object))
3654 it->position.charpos = IT_CHARPOS (*it) - 1;
3655 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3657 setup_for_ellipsis (it, 0);
3662 return handled;
3666 /* Make iterator IT return `...' next.
3667 Replaces LEN characters from buffer. */
3669 static void
3670 setup_for_ellipsis (it, len)
3671 struct it *it;
3672 int len;
3674 /* Use the display table definition for `...'. Invalid glyphs
3675 will be handled by the method returning elements from dpvec. */
3676 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3678 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3679 it->dpvec = v->contents;
3680 it->dpend = v->contents + v->size;
3682 else
3684 /* Default `...'. */
3685 it->dpvec = default_invis_vector;
3686 it->dpend = default_invis_vector + 3;
3689 it->dpvec_char_len = len;
3690 it->current.dpvec_index = 0;
3691 it->dpvec_face_id = -1;
3693 /* Remember the current face id in case glyphs specify faces.
3694 IT's face is restored in set_iterator_to_next.
3695 saved_face_id was set to preceding char's face in handle_stop. */
3696 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3697 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3699 it->method = GET_FROM_DISPLAY_VECTOR;
3700 it->ellipsis_p = 1;
3705 /***********************************************************************
3706 'display' property
3707 ***********************************************************************/
3709 /* Set up iterator IT from `display' property at its current position.
3710 Called from handle_stop.
3711 We return HANDLED_RETURN if some part of the display property
3712 overrides the display of the buffer text itself.
3713 Otherwise we return HANDLED_NORMALLY. */
3715 static enum prop_handled
3716 handle_display_prop (it)
3717 struct it *it;
3719 Lisp_Object prop, object;
3720 struct text_pos *position;
3721 /* Nonzero if some property replaces the display of the text itself. */
3722 int display_replaced_p = 0;
3724 if (STRINGP (it->string))
3726 object = it->string;
3727 position = &it->current.string_pos;
3729 else
3731 XSETWINDOW (object, it->w);
3732 position = &it->current.pos;
3735 /* Reset those iterator values set from display property values. */
3736 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3737 it->space_width = Qnil;
3738 it->font_height = Qnil;
3739 it->voffset = 0;
3741 /* We don't support recursive `display' properties, i.e. string
3742 values that have a string `display' property, that have a string
3743 `display' property etc. */
3744 if (!it->string_from_display_prop_p)
3745 it->area = TEXT_AREA;
3747 prop = Fget_char_property (make_number (position->charpos),
3748 Qdisplay, object);
3749 if (NILP (prop))
3750 return HANDLED_NORMALLY;
3752 if (!STRINGP (it->string))
3753 object = it->w->buffer;
3755 if (CONSP (prop)
3756 /* Simple properties. */
3757 && !EQ (XCAR (prop), Qimage)
3758 && !EQ (XCAR (prop), Qspace)
3759 && !EQ (XCAR (prop), Qwhen)
3760 && !EQ (XCAR (prop), Qslice)
3761 && !EQ (XCAR (prop), Qspace_width)
3762 && !EQ (XCAR (prop), Qheight)
3763 && !EQ (XCAR (prop), Qraise)
3764 /* Marginal area specifications. */
3765 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3766 && !EQ (XCAR (prop), Qleft_fringe)
3767 && !EQ (XCAR (prop), Qright_fringe)
3768 && !NILP (XCAR (prop)))
3770 for (; CONSP (prop); prop = XCDR (prop))
3772 if (handle_single_display_spec (it, XCAR (prop), object,
3773 position, display_replaced_p))
3774 display_replaced_p = 1;
3777 else if (VECTORP (prop))
3779 int i;
3780 for (i = 0; i < ASIZE (prop); ++i)
3781 if (handle_single_display_spec (it, AREF (prop, i), object,
3782 position, display_replaced_p))
3783 display_replaced_p = 1;
3785 else
3787 int ret = handle_single_display_spec (it, prop, object, position, 0);
3788 if (ret < 0) /* Replaced by "", i.e. nothing. */
3789 return HANDLED_RECOMPUTE_PROPS;
3790 if (ret)
3791 display_replaced_p = 1;
3794 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3798 /* Value is the position of the end of the `display' property starting
3799 at START_POS in OBJECT. */
3801 static struct text_pos
3802 display_prop_end (it, object, start_pos)
3803 struct it *it;
3804 Lisp_Object object;
3805 struct text_pos start_pos;
3807 Lisp_Object end;
3808 struct text_pos end_pos;
3810 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3811 Qdisplay, object, Qnil);
3812 CHARPOS (end_pos) = XFASTINT (end);
3813 if (STRINGP (object))
3814 compute_string_pos (&end_pos, start_pos, it->string);
3815 else
3816 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3818 return end_pos;
3822 /* Set up IT from a single `display' specification PROP. OBJECT
3823 is the object in which the `display' property was found. *POSITION
3824 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3825 means that we previously saw a display specification which already
3826 replaced text display with something else, for example an image;
3827 we ignore such properties after the first one has been processed.
3829 If PROP is a `space' or `image' specification, and in some other
3830 cases too, set *POSITION to the position where the `display'
3831 property ends.
3833 Value is non-zero if something was found which replaces the display
3834 of buffer or string text. Specifically, the value is -1 if that
3835 "something" is "nothing". */
3837 static int
3838 handle_single_display_spec (it, spec, object, position,
3839 display_replaced_before_p)
3840 struct it *it;
3841 Lisp_Object spec;
3842 Lisp_Object object;
3843 struct text_pos *position;
3844 int display_replaced_before_p;
3846 Lisp_Object form;
3847 Lisp_Object location, value;
3848 struct text_pos start_pos;
3849 int valid_p;
3851 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3852 If the result is non-nil, use VALUE instead of SPEC. */
3853 form = Qt;
3854 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3856 spec = XCDR (spec);
3857 if (!CONSP (spec))
3858 return 0;
3859 form = XCAR (spec);
3860 spec = XCDR (spec);
3863 if (!NILP (form) && !EQ (form, Qt))
3865 int count = SPECPDL_INDEX ();
3866 struct gcpro gcpro1;
3868 /* Bind `object' to the object having the `display' property, a
3869 buffer or string. Bind `position' to the position in the
3870 object where the property was found, and `buffer-position'
3871 to the current position in the buffer. */
3872 specbind (Qobject, object);
3873 specbind (Qposition, make_number (CHARPOS (*position)));
3874 specbind (Qbuffer_position,
3875 make_number (STRINGP (object)
3876 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3877 GCPRO1 (form);
3878 form = safe_eval (form);
3879 UNGCPRO;
3880 unbind_to (count, Qnil);
3883 if (NILP (form))
3884 return 0;
3886 /* Handle `(height HEIGHT)' specifications. */
3887 if (CONSP (spec)
3888 && EQ (XCAR (spec), Qheight)
3889 && CONSP (XCDR (spec)))
3891 if (!FRAME_WINDOW_P (it->f))
3892 return 0;
3894 it->font_height = XCAR (XCDR (spec));
3895 if (!NILP (it->font_height))
3897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3898 int new_height = -1;
3900 if (CONSP (it->font_height)
3901 && (EQ (XCAR (it->font_height), Qplus)
3902 || EQ (XCAR (it->font_height), Qminus))
3903 && CONSP (XCDR (it->font_height))
3904 && INTEGERP (XCAR (XCDR (it->font_height))))
3906 /* `(+ N)' or `(- N)' where N is an integer. */
3907 int steps = XINT (XCAR (XCDR (it->font_height)));
3908 if (EQ (XCAR (it->font_height), Qplus))
3909 steps = - steps;
3910 it->face_id = smaller_face (it->f, it->face_id, steps);
3912 else if (FUNCTIONP (it->font_height))
3914 /* Call function with current height as argument.
3915 Value is the new height. */
3916 Lisp_Object height;
3917 height = safe_call1 (it->font_height,
3918 face->lface[LFACE_HEIGHT_INDEX]);
3919 if (NUMBERP (height))
3920 new_height = XFLOATINT (height);
3922 else if (NUMBERP (it->font_height))
3924 /* Value is a multiple of the canonical char height. */
3925 struct face *face;
3927 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3928 new_height = (XFLOATINT (it->font_height)
3929 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3931 else
3933 /* Evaluate IT->font_height with `height' bound to the
3934 current specified height to get the new height. */
3935 int count = SPECPDL_INDEX ();
3937 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3938 value = safe_eval (it->font_height);
3939 unbind_to (count, Qnil);
3941 if (NUMBERP (value))
3942 new_height = XFLOATINT (value);
3945 if (new_height > 0)
3946 it->face_id = face_with_height (it->f, it->face_id, new_height);
3949 return 0;
3952 /* Handle `(space_width WIDTH)'. */
3953 if (CONSP (spec)
3954 && EQ (XCAR (spec), Qspace_width)
3955 && CONSP (XCDR (spec)))
3957 if (!FRAME_WINDOW_P (it->f))
3958 return 0;
3960 value = XCAR (XCDR (spec));
3961 if (NUMBERP (value) && XFLOATINT (value) > 0)
3962 it->space_width = value;
3964 return 0;
3967 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3968 if (CONSP (spec)
3969 && EQ (XCAR (spec), Qslice))
3971 Lisp_Object tem;
3973 if (!FRAME_WINDOW_P (it->f))
3974 return 0;
3976 if (tem = XCDR (spec), CONSP (tem))
3978 it->slice.x = XCAR (tem);
3979 if (tem = XCDR (tem), CONSP (tem))
3981 it->slice.y = XCAR (tem);
3982 if (tem = XCDR (tem), CONSP (tem))
3984 it->slice.width = XCAR (tem);
3985 if (tem = XCDR (tem), CONSP (tem))
3986 it->slice.height = XCAR (tem);
3991 return 0;
3994 /* Handle `(raise FACTOR)'. */
3995 if (CONSP (spec)
3996 && EQ (XCAR (spec), Qraise)
3997 && CONSP (XCDR (spec)))
3999 if (!FRAME_WINDOW_P (it->f))
4000 return 0;
4002 #ifdef HAVE_WINDOW_SYSTEM
4003 value = XCAR (XCDR (spec));
4004 if (NUMBERP (value))
4006 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4007 it->voffset = - (XFLOATINT (value)
4008 * (FONT_HEIGHT (face->font)));
4010 #endif /* HAVE_WINDOW_SYSTEM */
4012 return 0;
4015 /* Don't handle the other kinds of display specifications
4016 inside a string that we got from a `display' property. */
4017 if (it->string_from_display_prop_p)
4018 return 0;
4020 /* Characters having this form of property are not displayed, so
4021 we have to find the end of the property. */
4022 start_pos = *position;
4023 *position = display_prop_end (it, object, start_pos);
4024 value = Qnil;
4026 /* Stop the scan at that end position--we assume that all
4027 text properties change there. */
4028 it->stop_charpos = position->charpos;
4030 /* Handle `(left-fringe BITMAP [FACE])'
4031 and `(right-fringe BITMAP [FACE])'. */
4032 if (CONSP (spec)
4033 && (EQ (XCAR (spec), Qleft_fringe)
4034 || EQ (XCAR (spec), Qright_fringe))
4035 && CONSP (XCDR (spec)))
4037 int face_id = DEFAULT_FACE_ID;
4038 int fringe_bitmap;
4040 if (!FRAME_WINDOW_P (it->f))
4041 /* If we return here, POSITION has been advanced
4042 across the text with this property. */
4043 return 0;
4045 #ifdef HAVE_WINDOW_SYSTEM
4046 value = XCAR (XCDR (spec));
4047 if (!SYMBOLP (value)
4048 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4049 /* If we return here, POSITION has been advanced
4050 across the text with this property. */
4051 return 0;
4053 if (CONSP (XCDR (XCDR (spec))))
4055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4056 int face_id2 = lookup_derived_face (it->f, face_name,
4057 'A', FRINGE_FACE_ID, 0);
4058 if (face_id2 >= 0)
4059 face_id = face_id2;
4062 /* Save current settings of IT so that we can restore them
4063 when we are finished with the glyph property value. */
4065 push_it (it);
4067 it->area = TEXT_AREA;
4068 it->what = IT_IMAGE;
4069 it->image_id = -1; /* no image */
4070 it->position = start_pos;
4071 it->object = NILP (object) ? it->w->buffer : object;
4072 it->method = GET_FROM_IMAGE;
4073 it->face_id = face_id;
4075 /* Say that we haven't consumed the characters with
4076 `display' property yet. The call to pop_it in
4077 set_iterator_to_next will clean this up. */
4078 *position = start_pos;
4080 if (EQ (XCAR (spec), Qleft_fringe))
4082 it->left_user_fringe_bitmap = fringe_bitmap;
4083 it->left_user_fringe_face_id = face_id;
4085 else
4087 it->right_user_fringe_bitmap = fringe_bitmap;
4088 it->right_user_fringe_face_id = face_id;
4090 #endif /* HAVE_WINDOW_SYSTEM */
4091 return 1;
4094 /* Prepare to handle `((margin left-margin) ...)',
4095 `((margin right-margin) ...)' and `((margin nil) ...)'
4096 prefixes for display specifications. */
4097 location = Qunbound;
4098 if (CONSP (spec) && CONSP (XCAR (spec)))
4100 Lisp_Object tem;
4102 value = XCDR (spec);
4103 if (CONSP (value))
4104 value = XCAR (value);
4106 tem = XCAR (spec);
4107 if (EQ (XCAR (tem), Qmargin)
4108 && (tem = XCDR (tem),
4109 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4110 (NILP (tem)
4111 || EQ (tem, Qleft_margin)
4112 || EQ (tem, Qright_margin))))
4113 location = tem;
4116 if (EQ (location, Qunbound))
4118 location = Qnil;
4119 value = spec;
4122 /* After this point, VALUE is the property after any
4123 margin prefix has been stripped. It must be a string,
4124 an image specification, or `(space ...)'.
4126 LOCATION specifies where to display: `left-margin',
4127 `right-margin' or nil. */
4129 valid_p = (STRINGP (value)
4130 #ifdef HAVE_WINDOW_SYSTEM
4131 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4132 #endif /* not HAVE_WINDOW_SYSTEM */
4133 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4135 if (valid_p && !display_replaced_before_p)
4137 /* Save current settings of IT so that we can restore them
4138 when we are finished with the glyph property value. */
4139 push_it (it);
4140 if (NILP (location))
4141 it->area = TEXT_AREA;
4142 else if (EQ (location, Qleft_margin))
4143 it->area = LEFT_MARGIN_AREA;
4144 else
4145 it->area = RIGHT_MARGIN_AREA;
4147 if (STRINGP (value))
4149 if (SCHARS (value) == 0)
4151 pop_it (it);
4152 return -1; /* Replaced by "", i.e. nothing. */
4154 it->string = value;
4155 it->multibyte_p = STRING_MULTIBYTE (it->string);
4156 it->current.overlay_string_index = -1;
4157 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4158 it->end_charpos = it->string_nchars = SCHARS (it->string);
4159 it->method = GET_FROM_STRING;
4160 it->stop_charpos = 0;
4161 it->string_from_display_prop_p = 1;
4162 /* Say that we haven't consumed the characters with
4163 `display' property yet. The call to pop_it in
4164 set_iterator_to_next will clean this up. */
4165 *position = start_pos;
4167 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4169 it->method = GET_FROM_STRETCH;
4170 it->object = value;
4171 *position = it->position = start_pos;
4173 #ifdef HAVE_WINDOW_SYSTEM
4174 else
4176 it->what = IT_IMAGE;
4177 it->image_id = lookup_image (it->f, value);
4178 it->position = start_pos;
4179 it->object = NILP (object) ? it->w->buffer : object;
4180 it->method = GET_FROM_IMAGE;
4182 /* Say that we haven't consumed the characters with
4183 `display' property yet. The call to pop_it in
4184 set_iterator_to_next will clean this up. */
4185 *position = start_pos;
4187 #endif /* HAVE_WINDOW_SYSTEM */
4189 return 1;
4192 /* Invalid property or property not supported. Restore
4193 POSITION to what it was before. */
4194 *position = start_pos;
4195 return 0;
4199 /* Check if SPEC is a display sub-property value whose text should be
4200 treated as intangible. */
4202 static int
4203 single_display_spec_intangible_p (prop)
4204 Lisp_Object prop;
4206 /* Skip over `when FORM'. */
4207 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4209 prop = XCDR (prop);
4210 if (!CONSP (prop))
4211 return 0;
4212 prop = XCDR (prop);
4215 if (STRINGP (prop))
4216 return 1;
4218 if (!CONSP (prop))
4219 return 0;
4221 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4222 we don't need to treat text as intangible. */
4223 if (EQ (XCAR (prop), Qmargin))
4225 prop = XCDR (prop);
4226 if (!CONSP (prop))
4227 return 0;
4229 prop = XCDR (prop);
4230 if (!CONSP (prop)
4231 || EQ (XCAR (prop), Qleft_margin)
4232 || EQ (XCAR (prop), Qright_margin))
4233 return 0;
4236 return (CONSP (prop)
4237 && (EQ (XCAR (prop), Qimage)
4238 || EQ (XCAR (prop), Qspace)));
4242 /* Check if PROP is a display property value whose text should be
4243 treated as intangible. */
4246 display_prop_intangible_p (prop)
4247 Lisp_Object prop;
4249 if (CONSP (prop)
4250 && CONSP (XCAR (prop))
4251 && !EQ (Qmargin, XCAR (XCAR (prop))))
4253 /* A list of sub-properties. */
4254 while (CONSP (prop))
4256 if (single_display_spec_intangible_p (XCAR (prop)))
4257 return 1;
4258 prop = XCDR (prop);
4261 else if (VECTORP (prop))
4263 /* A vector of sub-properties. */
4264 int i;
4265 for (i = 0; i < ASIZE (prop); ++i)
4266 if (single_display_spec_intangible_p (AREF (prop, i)))
4267 return 1;
4269 else
4270 return single_display_spec_intangible_p (prop);
4272 return 0;
4276 /* Return 1 if PROP is a display sub-property value containing STRING. */
4278 static int
4279 single_display_spec_string_p (prop, string)
4280 Lisp_Object prop, string;
4282 if (EQ (string, prop))
4283 return 1;
4285 /* Skip over `when FORM'. */
4286 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 prop = XCDR (prop);
4289 if (!CONSP (prop))
4290 return 0;
4291 prop = XCDR (prop);
4294 if (CONSP (prop))
4295 /* Skip over `margin LOCATION'. */
4296 if (EQ (XCAR (prop), Qmargin))
4298 prop = XCDR (prop);
4299 if (!CONSP (prop))
4300 return 0;
4302 prop = XCDR (prop);
4303 if (!CONSP (prop))
4304 return 0;
4307 return CONSP (prop) && EQ (XCAR (prop), string);
4311 /* Return 1 if STRING appears in the `display' property PROP. */
4313 static int
4314 display_prop_string_p (prop, string)
4315 Lisp_Object prop, string;
4317 if (CONSP (prop)
4318 && CONSP (XCAR (prop))
4319 && !EQ (Qmargin, XCAR (XCAR (prop))))
4321 /* A list of sub-properties. */
4322 while (CONSP (prop))
4324 if (single_display_spec_string_p (XCAR (prop), string))
4325 return 1;
4326 prop = XCDR (prop);
4329 else if (VECTORP (prop))
4331 /* A vector of sub-properties. */
4332 int i;
4333 for (i = 0; i < ASIZE (prop); ++i)
4334 if (single_display_spec_string_p (AREF (prop, i), string))
4335 return 1;
4337 else
4338 return single_display_spec_string_p (prop, string);
4340 return 0;
4344 /* Determine from which buffer position in W's buffer STRING comes
4345 from. AROUND_CHARPOS is an approximate position where it could
4346 be from. Value is the buffer position or 0 if it couldn't be
4347 determined.
4349 W's buffer must be current.
4351 This function is necessary because we don't record buffer positions
4352 in glyphs generated from strings (to keep struct glyph small).
4353 This function may only use code that doesn't eval because it is
4354 called asynchronously from note_mouse_highlight. */
4357 string_buffer_position (w, string, around_charpos)
4358 struct window *w;
4359 Lisp_Object string;
4360 int around_charpos;
4362 Lisp_Object limit, prop, pos;
4363 const int MAX_DISTANCE = 1000;
4364 int found = 0;
4366 pos = make_number (around_charpos);
4367 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4368 while (!found && !EQ (pos, limit))
4370 prop = Fget_char_property (pos, Qdisplay, Qnil);
4371 if (!NILP (prop) && display_prop_string_p (prop, string))
4372 found = 1;
4373 else
4374 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4377 if (!found)
4379 pos = make_number (around_charpos);
4380 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4381 while (!found && !EQ (pos, limit))
4383 prop = Fget_char_property (pos, Qdisplay, Qnil);
4384 if (!NILP (prop) && display_prop_string_p (prop, string))
4385 found = 1;
4386 else
4387 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4388 limit);
4392 return found ? XINT (pos) : 0;
4397 /***********************************************************************
4398 `composition' property
4399 ***********************************************************************/
4401 /* Set up iterator IT from `composition' property at its current
4402 position. Called from handle_stop. */
4404 static enum prop_handled
4405 handle_composition_prop (it)
4406 struct it *it;
4408 Lisp_Object prop, string;
4409 int pos, pos_byte, end;
4410 enum prop_handled handled = HANDLED_NORMALLY;
4412 if (STRINGP (it->string))
4414 pos = IT_STRING_CHARPOS (*it);
4415 pos_byte = IT_STRING_BYTEPOS (*it);
4416 string = it->string;
4418 else
4420 pos = IT_CHARPOS (*it);
4421 pos_byte = IT_BYTEPOS (*it);
4422 string = Qnil;
4425 /* If there's a valid composition and point is not inside of the
4426 composition (in the case that the composition is from the current
4427 buffer), draw a glyph composed from the composition components. */
4428 if (find_composition (pos, -1, &pos, &end, &prop, string)
4429 && COMPOSITION_VALID_P (pos, end, prop)
4430 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4432 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4434 if (id >= 0)
4436 it->method = GET_FROM_COMPOSITION;
4437 it->cmp_id = id;
4438 it->cmp_len = COMPOSITION_LENGTH (prop);
4439 /* For a terminal, draw only the first character of the
4440 components. */
4441 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4442 it->len = (STRINGP (it->string)
4443 ? string_char_to_byte (it->string, end)
4444 : CHAR_TO_BYTE (end)) - pos_byte;
4445 it->stop_charpos = end;
4446 handled = HANDLED_RETURN;
4450 return handled;
4455 /***********************************************************************
4456 Overlay strings
4457 ***********************************************************************/
4459 /* The following structure is used to record overlay strings for
4460 later sorting in load_overlay_strings. */
4462 struct overlay_entry
4464 Lisp_Object overlay;
4465 Lisp_Object string;
4466 int priority;
4467 int after_string_p;
4471 /* Set up iterator IT from overlay strings at its current position.
4472 Called from handle_stop. */
4474 static enum prop_handled
4475 handle_overlay_change (it)
4476 struct it *it;
4478 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4479 return HANDLED_RECOMPUTE_PROPS;
4480 else
4481 return HANDLED_NORMALLY;
4485 /* Set up the next overlay string for delivery by IT, if there is an
4486 overlay string to deliver. Called by set_iterator_to_next when the
4487 end of the current overlay string is reached. If there are more
4488 overlay strings to display, IT->string and
4489 IT->current.overlay_string_index are set appropriately here.
4490 Otherwise IT->string is set to nil. */
4492 static void
4493 next_overlay_string (it)
4494 struct it *it;
4496 ++it->current.overlay_string_index;
4497 if (it->current.overlay_string_index == it->n_overlay_strings)
4499 /* No more overlay strings. Restore IT's settings to what
4500 they were before overlay strings were processed, and
4501 continue to deliver from current_buffer. */
4502 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4504 pop_it (it);
4505 xassert (it->stop_charpos >= BEGV
4506 && it->stop_charpos <= it->end_charpos);
4507 it->string = Qnil;
4508 it->current.overlay_string_index = -1;
4509 SET_TEXT_POS (it->current.string_pos, -1, -1);
4510 it->n_overlay_strings = 0;
4511 it->method = GET_FROM_BUFFER;
4513 /* If we're at the end of the buffer, record that we have
4514 processed the overlay strings there already, so that
4515 next_element_from_buffer doesn't try it again. */
4516 if (IT_CHARPOS (*it) >= it->end_charpos)
4517 it->overlay_strings_at_end_processed_p = 1;
4519 /* If we have to display `...' for invisible text, set
4520 the iterator up for that. */
4521 if (display_ellipsis_p)
4522 setup_for_ellipsis (it, 0);
4524 else
4526 /* There are more overlay strings to process. If
4527 IT->current.overlay_string_index has advanced to a position
4528 where we must load IT->overlay_strings with more strings, do
4529 it. */
4530 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4532 if (it->current.overlay_string_index && i == 0)
4533 load_overlay_strings (it, 0);
4535 /* Initialize IT to deliver display elements from the overlay
4536 string. */
4537 it->string = it->overlay_strings[i];
4538 it->multibyte_p = STRING_MULTIBYTE (it->string);
4539 SET_TEXT_POS (it->current.string_pos, 0, 0);
4540 it->method = GET_FROM_STRING;
4541 it->stop_charpos = 0;
4544 CHECK_IT (it);
4548 /* Compare two overlay_entry structures E1 and E2. Used as a
4549 comparison function for qsort in load_overlay_strings. Overlay
4550 strings for the same position are sorted so that
4552 1. All after-strings come in front of before-strings, except
4553 when they come from the same overlay.
4555 2. Within after-strings, strings are sorted so that overlay strings
4556 from overlays with higher priorities come first.
4558 2. Within before-strings, strings are sorted so that overlay
4559 strings from overlays with higher priorities come last.
4561 Value is analogous to strcmp. */
4564 static int
4565 compare_overlay_entries (e1, e2)
4566 void *e1, *e2;
4568 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4569 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4570 int result;
4572 if (entry1->after_string_p != entry2->after_string_p)
4574 /* Let after-strings appear in front of before-strings if
4575 they come from different overlays. */
4576 if (EQ (entry1->overlay, entry2->overlay))
4577 result = entry1->after_string_p ? 1 : -1;
4578 else
4579 result = entry1->after_string_p ? -1 : 1;
4581 else if (entry1->after_string_p)
4582 /* After-strings sorted in order of decreasing priority. */
4583 result = entry2->priority - entry1->priority;
4584 else
4585 /* Before-strings sorted in order of increasing priority. */
4586 result = entry1->priority - entry2->priority;
4588 return result;
4592 /* Load the vector IT->overlay_strings with overlay strings from IT's
4593 current buffer position, or from CHARPOS if that is > 0. Set
4594 IT->n_overlays to the total number of overlay strings found.
4596 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4597 a time. On entry into load_overlay_strings,
4598 IT->current.overlay_string_index gives the number of overlay
4599 strings that have already been loaded by previous calls to this
4600 function.
4602 IT->add_overlay_start contains an additional overlay start
4603 position to consider for taking overlay strings from, if non-zero.
4604 This position comes into play when the overlay has an `invisible'
4605 property, and both before and after-strings. When we've skipped to
4606 the end of the overlay, because of its `invisible' property, we
4607 nevertheless want its before-string to appear.
4608 IT->add_overlay_start will contain the overlay start position
4609 in this case.
4611 Overlay strings are sorted so that after-string strings come in
4612 front of before-string strings. Within before and after-strings,
4613 strings are sorted by overlay priority. See also function
4614 compare_overlay_entries. */
4616 static void
4617 load_overlay_strings (it, charpos)
4618 struct it *it;
4619 int charpos;
4621 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4622 Lisp_Object overlay, window, str, invisible;
4623 struct Lisp_Overlay *ov;
4624 int start, end;
4625 int size = 20;
4626 int n = 0, i, j, invis_p;
4627 struct overlay_entry *entries
4628 = (struct overlay_entry *) alloca (size * sizeof *entries);
4630 if (charpos <= 0)
4631 charpos = IT_CHARPOS (*it);
4633 /* Append the overlay string STRING of overlay OVERLAY to vector
4634 `entries' which has size `size' and currently contains `n'
4635 elements. AFTER_P non-zero means STRING is an after-string of
4636 OVERLAY. */
4637 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4638 do \
4640 Lisp_Object priority; \
4642 if (n == size) \
4644 int new_size = 2 * size; \
4645 struct overlay_entry *old = entries; \
4646 entries = \
4647 (struct overlay_entry *) alloca (new_size \
4648 * sizeof *entries); \
4649 bcopy (old, entries, size * sizeof *entries); \
4650 size = new_size; \
4653 entries[n].string = (STRING); \
4654 entries[n].overlay = (OVERLAY); \
4655 priority = Foverlay_get ((OVERLAY), Qpriority); \
4656 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4657 entries[n].after_string_p = (AFTER_P); \
4658 ++n; \
4660 while (0)
4662 /* Process overlay before the overlay center. */
4663 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4665 XSETMISC (overlay, ov);
4666 xassert (OVERLAYP (overlay));
4667 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4668 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4670 if (end < charpos)
4671 break;
4673 /* Skip this overlay if it doesn't start or end at IT's current
4674 position. */
4675 if (end != charpos && start != charpos)
4676 continue;
4678 /* Skip this overlay if it doesn't apply to IT->w. */
4679 window = Foverlay_get (overlay, Qwindow);
4680 if (WINDOWP (window) && XWINDOW (window) != it->w)
4681 continue;
4683 /* If the text ``under'' the overlay is invisible, both before-
4684 and after-strings from this overlay are visible; start and
4685 end position are indistinguishable. */
4686 invisible = Foverlay_get (overlay, Qinvisible);
4687 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4689 /* If overlay has a non-empty before-string, record it. */
4690 if ((start == charpos || (end == charpos && invis_p))
4691 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4692 && SCHARS (str))
4693 RECORD_OVERLAY_STRING (overlay, str, 0);
4695 /* If overlay has a non-empty after-string, record it. */
4696 if ((end == charpos || (start == charpos && invis_p))
4697 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4698 && SCHARS (str))
4699 RECORD_OVERLAY_STRING (overlay, str, 1);
4702 /* Process overlays after the overlay center. */
4703 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4705 XSETMISC (overlay, ov);
4706 xassert (OVERLAYP (overlay));
4707 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4708 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4710 if (start > charpos)
4711 break;
4713 /* Skip this overlay if it doesn't start or end at IT's current
4714 position. */
4715 if (end != charpos && start != charpos)
4716 continue;
4718 /* Skip this overlay if it doesn't apply to IT->w. */
4719 window = Foverlay_get (overlay, Qwindow);
4720 if (WINDOWP (window) && XWINDOW (window) != it->w)
4721 continue;
4723 /* If the text ``under'' the overlay is invisible, it has a zero
4724 dimension, and both before- and after-strings apply. */
4725 invisible = Foverlay_get (overlay, Qinvisible);
4726 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4728 /* If overlay has a non-empty before-string, record it. */
4729 if ((start == charpos || (end == charpos && invis_p))
4730 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4731 && SCHARS (str))
4732 RECORD_OVERLAY_STRING (overlay, str, 0);
4734 /* If overlay has a non-empty after-string, record it. */
4735 if ((end == charpos || (start == charpos && invis_p))
4736 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4737 && SCHARS (str))
4738 RECORD_OVERLAY_STRING (overlay, str, 1);
4741 #undef RECORD_OVERLAY_STRING
4743 /* Sort entries. */
4744 if (n > 1)
4745 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4747 /* Record the total number of strings to process. */
4748 it->n_overlay_strings = n;
4750 /* IT->current.overlay_string_index is the number of overlay strings
4751 that have already been consumed by IT. Copy some of the
4752 remaining overlay strings to IT->overlay_strings. */
4753 i = 0;
4754 j = it->current.overlay_string_index;
4755 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4756 it->overlay_strings[i++] = entries[j++].string;
4758 CHECK_IT (it);
4762 /* Get the first chunk of overlay strings at IT's current buffer
4763 position, or at CHARPOS if that is > 0. Value is non-zero if at
4764 least one overlay string was found. */
4766 static int
4767 get_overlay_strings (it, charpos)
4768 struct it *it;
4769 int charpos;
4771 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4772 process. This fills IT->overlay_strings with strings, and sets
4773 IT->n_overlay_strings to the total number of strings to process.
4774 IT->pos.overlay_string_index has to be set temporarily to zero
4775 because load_overlay_strings needs this; it must be set to -1
4776 when no overlay strings are found because a zero value would
4777 indicate a position in the first overlay string. */
4778 it->current.overlay_string_index = 0;
4779 load_overlay_strings (it, charpos);
4781 /* If we found overlay strings, set up IT to deliver display
4782 elements from the first one. Otherwise set up IT to deliver
4783 from current_buffer. */
4784 if (it->n_overlay_strings)
4786 /* Make sure we know settings in current_buffer, so that we can
4787 restore meaningful values when we're done with the overlay
4788 strings. */
4789 compute_stop_pos (it);
4790 xassert (it->face_id >= 0);
4792 /* Save IT's settings. They are restored after all overlay
4793 strings have been processed. */
4794 xassert (it->sp == 0);
4795 push_it (it);
4797 /* Set up IT to deliver display elements from the first overlay
4798 string. */
4799 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4800 it->string = it->overlay_strings[0];
4801 it->stop_charpos = 0;
4802 xassert (STRINGP (it->string));
4803 it->end_charpos = SCHARS (it->string);
4804 it->multibyte_p = STRING_MULTIBYTE (it->string);
4805 it->method = GET_FROM_STRING;
4807 else
4809 it->string = Qnil;
4810 it->current.overlay_string_index = -1;
4811 it->method = GET_FROM_BUFFER;
4814 CHECK_IT (it);
4816 /* Value is non-zero if we found at least one overlay string. */
4817 return STRINGP (it->string);
4822 /***********************************************************************
4823 Saving and restoring state
4824 ***********************************************************************/
4826 /* Save current settings of IT on IT->stack. Called, for example,
4827 before setting up IT for an overlay string, to be able to restore
4828 IT's settings to what they were after the overlay string has been
4829 processed. */
4831 static void
4832 push_it (it)
4833 struct it *it;
4835 struct iterator_stack_entry *p;
4837 xassert (it->sp < 2);
4838 p = it->stack + it->sp;
4840 p->stop_charpos = it->stop_charpos;
4841 xassert (it->face_id >= 0);
4842 p->face_id = it->face_id;
4843 p->string = it->string;
4844 p->pos = it->current;
4845 p->end_charpos = it->end_charpos;
4846 p->string_nchars = it->string_nchars;
4847 p->area = it->area;
4848 p->multibyte_p = it->multibyte_p;
4849 p->slice = it->slice;
4850 p->space_width = it->space_width;
4851 p->font_height = it->font_height;
4852 p->voffset = it->voffset;
4853 p->string_from_display_prop_p = it->string_from_display_prop_p;
4854 p->display_ellipsis_p = 0;
4855 ++it->sp;
4859 /* Restore IT's settings from IT->stack. Called, for example, when no
4860 more overlay strings must be processed, and we return to delivering
4861 display elements from a buffer, or when the end of a string from a
4862 `display' property is reached and we return to delivering display
4863 elements from an overlay string, or from a buffer. */
4865 static void
4866 pop_it (it)
4867 struct it *it;
4869 struct iterator_stack_entry *p;
4871 xassert (it->sp > 0);
4872 --it->sp;
4873 p = it->stack + it->sp;
4874 it->stop_charpos = p->stop_charpos;
4875 it->face_id = p->face_id;
4876 it->string = p->string;
4877 it->current = p->pos;
4878 it->end_charpos = p->end_charpos;
4879 it->string_nchars = p->string_nchars;
4880 it->area = p->area;
4881 it->multibyte_p = p->multibyte_p;
4882 it->slice = p->slice;
4883 it->space_width = p->space_width;
4884 it->font_height = p->font_height;
4885 it->voffset = p->voffset;
4886 it->string_from_display_prop_p = p->string_from_display_prop_p;
4891 /***********************************************************************
4892 Moving over lines
4893 ***********************************************************************/
4895 /* Set IT's current position to the previous line start. */
4897 static void
4898 back_to_previous_line_start (it)
4899 struct it *it;
4901 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4902 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4906 /* Move IT to the next line start.
4908 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4909 we skipped over part of the text (as opposed to moving the iterator
4910 continuously over the text). Otherwise, don't change the value
4911 of *SKIPPED_P.
4913 Newlines may come from buffer text, overlay strings, or strings
4914 displayed via the `display' property. That's the reason we can't
4915 simply use find_next_newline_no_quit.
4917 Note that this function may not skip over invisible text that is so
4918 because of text properties and immediately follows a newline. If
4919 it would, function reseat_at_next_visible_line_start, when called
4920 from set_iterator_to_next, would effectively make invisible
4921 characters following a newline part of the wrong glyph row, which
4922 leads to wrong cursor motion. */
4924 static int
4925 forward_to_next_line_start (it, skipped_p)
4926 struct it *it;
4927 int *skipped_p;
4929 int old_selective, newline_found_p, n;
4930 const int MAX_NEWLINE_DISTANCE = 500;
4932 /* If already on a newline, just consume it to avoid unintended
4933 skipping over invisible text below. */
4934 if (it->what == IT_CHARACTER
4935 && it->c == '\n'
4936 && CHARPOS (it->position) == IT_CHARPOS (*it))
4938 set_iterator_to_next (it, 0);
4939 it->c = 0;
4940 return 1;
4943 /* Don't handle selective display in the following. It's (a)
4944 unnecessary because it's done by the caller, and (b) leads to an
4945 infinite recursion because next_element_from_ellipsis indirectly
4946 calls this function. */
4947 old_selective = it->selective;
4948 it->selective = 0;
4950 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4951 from buffer text. */
4952 for (n = newline_found_p = 0;
4953 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4954 n += STRINGP (it->string) ? 0 : 1)
4956 if (!get_next_display_element (it))
4957 return 0;
4958 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4959 set_iterator_to_next (it, 0);
4962 /* If we didn't find a newline near enough, see if we can use a
4963 short-cut. */
4964 if (!newline_found_p)
4966 int start = IT_CHARPOS (*it);
4967 int limit = find_next_newline_no_quit (start, 1);
4968 Lisp_Object pos;
4970 xassert (!STRINGP (it->string));
4972 /* If there isn't any `display' property in sight, and no
4973 overlays, we can just use the position of the newline in
4974 buffer text. */
4975 if (it->stop_charpos >= limit
4976 || ((pos = Fnext_single_property_change (make_number (start),
4977 Qdisplay,
4978 Qnil, make_number (limit)),
4979 NILP (pos))
4980 && next_overlay_change (start) == ZV))
4982 IT_CHARPOS (*it) = limit;
4983 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4984 *skipped_p = newline_found_p = 1;
4986 else
4988 while (get_next_display_element (it)
4989 && !newline_found_p)
4991 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4992 set_iterator_to_next (it, 0);
4997 it->selective = old_selective;
4998 return newline_found_p;
5002 /* Set IT's current position to the previous visible line start. Skip
5003 invisible text that is so either due to text properties or due to
5004 selective display. Caution: this does not change IT->current_x and
5005 IT->hpos. */
5007 static void
5008 back_to_previous_visible_line_start (it)
5009 struct it *it;
5011 while (IT_CHARPOS (*it) > BEGV)
5013 back_to_previous_line_start (it);
5014 if (IT_CHARPOS (*it) <= BEGV)
5015 break;
5017 /* If selective > 0, then lines indented more than that values
5018 are invisible. */
5019 if (it->selective > 0
5020 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5021 (double) it->selective)) /* iftc */
5022 continue;
5024 /* Check the newline before point for invisibility. */
5026 Lisp_Object prop;
5027 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5028 Qinvisible, it->window);
5029 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5030 continue;
5033 /* If newline has a display property that replaces the newline with something
5034 else (image or text), find start of overlay or interval and continue search
5035 from that point. */
5036 if (IT_CHARPOS (*it) > BEGV)
5038 struct it it2 = *it;
5039 int pos;
5040 int beg, end;
5041 Lisp_Object val, overlay;
5043 pos = --IT_CHARPOS (it2);
5044 --IT_BYTEPOS (it2);
5045 it2.sp = 0;
5046 if (handle_display_prop (&it2) == HANDLED_RETURN
5047 && !NILP (val = get_char_property_and_overlay
5048 (make_number (pos), Qdisplay, Qnil, &overlay))
5049 && (OVERLAYP (overlay)
5050 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5051 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5053 if (beg < BEGV)
5054 beg = BEGV;
5055 IT_CHARPOS (*it) = beg;
5056 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5057 continue;
5061 break;
5064 xassert (IT_CHARPOS (*it) >= BEGV);
5065 xassert (IT_CHARPOS (*it) == BEGV
5066 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5067 CHECK_IT (it);
5071 /* Reseat iterator IT at the previous visible line start. Skip
5072 invisible text that is so either due to text properties or due to
5073 selective display. At the end, update IT's overlay information,
5074 face information etc. */
5076 void
5077 reseat_at_previous_visible_line_start (it)
5078 struct it *it;
5080 back_to_previous_visible_line_start (it);
5081 reseat (it, it->current.pos, 1);
5082 CHECK_IT (it);
5086 /* Reseat iterator IT on the next visible line start in the current
5087 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5088 preceding the line start. Skip over invisible text that is so
5089 because of selective display. Compute faces, overlays etc at the
5090 new position. Note that this function does not skip over text that
5091 is invisible because of text properties. */
5093 static void
5094 reseat_at_next_visible_line_start (it, on_newline_p)
5095 struct it *it;
5096 int on_newline_p;
5098 int newline_found_p, skipped_p = 0;
5100 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5102 /* Skip over lines that are invisible because they are indented
5103 more than the value of IT->selective. */
5104 if (it->selective > 0)
5105 while (IT_CHARPOS (*it) < ZV
5106 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5107 (double) it->selective)) /* iftc */
5109 xassert (IT_BYTEPOS (*it) == BEGV
5110 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5111 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5114 /* Position on the newline if that's what's requested. */
5115 if (on_newline_p && newline_found_p)
5117 if (STRINGP (it->string))
5119 if (IT_STRING_CHARPOS (*it) > 0)
5121 --IT_STRING_CHARPOS (*it);
5122 --IT_STRING_BYTEPOS (*it);
5125 else if (IT_CHARPOS (*it) > BEGV)
5127 --IT_CHARPOS (*it);
5128 --IT_BYTEPOS (*it);
5129 reseat (it, it->current.pos, 0);
5132 else if (skipped_p)
5133 reseat (it, it->current.pos, 0);
5135 CHECK_IT (it);
5140 /***********************************************************************
5141 Changing an iterator's position
5142 ***********************************************************************/
5144 /* Change IT's current position to POS in current_buffer. If FORCE_P
5145 is non-zero, always check for text properties at the new position.
5146 Otherwise, text properties are only looked up if POS >=
5147 IT->check_charpos of a property. */
5149 static void
5150 reseat (it, pos, force_p)
5151 struct it *it;
5152 struct text_pos pos;
5153 int force_p;
5155 int original_pos = IT_CHARPOS (*it);
5157 reseat_1 (it, pos, 0);
5159 /* Determine where to check text properties. Avoid doing it
5160 where possible because text property lookup is very expensive. */
5161 if (force_p
5162 || CHARPOS (pos) > it->stop_charpos
5163 || CHARPOS (pos) < original_pos)
5164 handle_stop (it);
5166 CHECK_IT (it);
5170 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5171 IT->stop_pos to POS, also. */
5173 static void
5174 reseat_1 (it, pos, set_stop_p)
5175 struct it *it;
5176 struct text_pos pos;
5177 int set_stop_p;
5179 /* Don't call this function when scanning a C string. */
5180 xassert (it->s == NULL);
5182 /* POS must be a reasonable value. */
5183 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5185 it->current.pos = it->position = pos;
5186 XSETBUFFER (it->object, current_buffer);
5187 it->end_charpos = ZV;
5188 it->dpvec = NULL;
5189 it->current.dpvec_index = -1;
5190 it->current.overlay_string_index = -1;
5191 IT_STRING_CHARPOS (*it) = -1;
5192 IT_STRING_BYTEPOS (*it) = -1;
5193 it->string = Qnil;
5194 it->method = GET_FROM_BUFFER;
5195 /* RMS: I added this to fix a bug in move_it_vertically_backward
5196 where it->area continued to relate to the starting point
5197 for the backward motion. Bug report from
5198 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5199 However, I am not sure whether reseat still does the right thing
5200 in general after this change. */
5201 it->area = TEXT_AREA;
5202 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5203 it->sp = 0;
5204 it->face_before_selective_p = 0;
5206 if (set_stop_p)
5207 it->stop_charpos = CHARPOS (pos);
5211 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5212 If S is non-null, it is a C string to iterate over. Otherwise,
5213 STRING gives a Lisp string to iterate over.
5215 If PRECISION > 0, don't return more then PRECISION number of
5216 characters from the string.
5218 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5219 characters have been returned. FIELD_WIDTH < 0 means an infinite
5220 field width.
5222 MULTIBYTE = 0 means disable processing of multibyte characters,
5223 MULTIBYTE > 0 means enable it,
5224 MULTIBYTE < 0 means use IT->multibyte_p.
5226 IT must be initialized via a prior call to init_iterator before
5227 calling this function. */
5229 static void
5230 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5231 struct it *it;
5232 unsigned char *s;
5233 Lisp_Object string;
5234 int charpos;
5235 int precision, field_width, multibyte;
5237 /* No region in strings. */
5238 it->region_beg_charpos = it->region_end_charpos = -1;
5240 /* No text property checks performed by default, but see below. */
5241 it->stop_charpos = -1;
5243 /* Set iterator position and end position. */
5244 bzero (&it->current, sizeof it->current);
5245 it->current.overlay_string_index = -1;
5246 it->current.dpvec_index = -1;
5247 xassert (charpos >= 0);
5249 /* If STRING is specified, use its multibyteness, otherwise use the
5250 setting of MULTIBYTE, if specified. */
5251 if (multibyte >= 0)
5252 it->multibyte_p = multibyte > 0;
5254 if (s == NULL)
5256 xassert (STRINGP (string));
5257 it->string = string;
5258 it->s = NULL;
5259 it->end_charpos = it->string_nchars = SCHARS (string);
5260 it->method = GET_FROM_STRING;
5261 it->current.string_pos = string_pos (charpos, string);
5263 else
5265 it->s = s;
5266 it->string = Qnil;
5268 /* Note that we use IT->current.pos, not it->current.string_pos,
5269 for displaying C strings. */
5270 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5271 if (it->multibyte_p)
5273 it->current.pos = c_string_pos (charpos, s, 1);
5274 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5276 else
5278 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5279 it->end_charpos = it->string_nchars = strlen (s);
5282 it->method = GET_FROM_C_STRING;
5285 /* PRECISION > 0 means don't return more than PRECISION characters
5286 from the string. */
5287 if (precision > 0 && it->end_charpos - charpos > precision)
5288 it->end_charpos = it->string_nchars = charpos + precision;
5290 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5291 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5292 FIELD_WIDTH < 0 means infinite field width. This is useful for
5293 padding with `-' at the end of a mode line. */
5294 if (field_width < 0)
5295 field_width = INFINITY;
5296 if (field_width > it->end_charpos - charpos)
5297 it->end_charpos = charpos + field_width;
5299 /* Use the standard display table for displaying strings. */
5300 if (DISP_TABLE_P (Vstandard_display_table))
5301 it->dp = XCHAR_TABLE (Vstandard_display_table);
5303 it->stop_charpos = charpos;
5304 CHECK_IT (it);
5309 /***********************************************************************
5310 Iteration
5311 ***********************************************************************/
5313 /* Map enum it_method value to corresponding next_element_from_* function. */
5315 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5317 next_element_from_buffer,
5318 next_element_from_display_vector,
5319 next_element_from_composition,
5320 next_element_from_string,
5321 next_element_from_c_string,
5322 next_element_from_image,
5323 next_element_from_stretch
5327 /* Load IT's display element fields with information about the next
5328 display element from the current position of IT. Value is zero if
5329 end of buffer (or C string) is reached. */
5331 static struct frame *last_escape_glyph_frame = NULL;
5332 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5333 static int last_escape_glyph_merged_face_id = 0;
5336 get_next_display_element (it)
5337 struct it *it;
5339 /* Non-zero means that we found a display element. Zero means that
5340 we hit the end of what we iterate over. Performance note: the
5341 function pointer `method' used here turns out to be faster than
5342 using a sequence of if-statements. */
5343 int success_p;
5345 get_next:
5346 success_p = (*get_next_element[it->method]) (it);
5348 if (it->what == IT_CHARACTER)
5350 /* Map via display table or translate control characters.
5351 IT->c, IT->len etc. have been set to the next character by
5352 the function call above. If we have a display table, and it
5353 contains an entry for IT->c, translate it. Don't do this if
5354 IT->c itself comes from a display table, otherwise we could
5355 end up in an infinite recursion. (An alternative could be to
5356 count the recursion depth of this function and signal an
5357 error when a certain maximum depth is reached.) Is it worth
5358 it? */
5359 if (success_p && it->dpvec == NULL)
5361 Lisp_Object dv;
5363 if (it->dp
5364 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5365 VECTORP (dv)))
5367 struct Lisp_Vector *v = XVECTOR (dv);
5369 /* Return the first character from the display table
5370 entry, if not empty. If empty, don't display the
5371 current character. */
5372 if (v->size)
5374 it->dpvec_char_len = it->len;
5375 it->dpvec = v->contents;
5376 it->dpend = v->contents + v->size;
5377 it->current.dpvec_index = 0;
5378 it->dpvec_face_id = -1;
5379 it->saved_face_id = it->face_id;
5380 it->method = GET_FROM_DISPLAY_VECTOR;
5381 it->ellipsis_p = 0;
5383 else
5385 set_iterator_to_next (it, 0);
5387 goto get_next;
5390 /* Translate control characters into `\003' or `^C' form.
5391 Control characters coming from a display table entry are
5392 currently not translated because we use IT->dpvec to hold
5393 the translation. This could easily be changed but I
5394 don't believe that it is worth doing.
5396 If it->multibyte_p is nonzero, eight-bit characters and
5397 non-printable multibyte characters are also translated to
5398 octal form.
5400 If it->multibyte_p is zero, eight-bit characters that
5401 don't have corresponding multibyte char code are also
5402 translated to octal form. */
5403 else if ((it->c < ' '
5404 && (it->area != TEXT_AREA
5405 /* In mode line, treat \n like other crl chars. */
5406 || (it->c != '\t'
5407 && it->glyph_row && it->glyph_row->mode_line_p)
5408 || (it->c != '\n' && it->c != '\t')))
5409 || (it->multibyte_p
5410 ? ((it->c >= 127
5411 && it->len == 1)
5412 || !CHAR_PRINTABLE_P (it->c)
5413 || (!NILP (Vnobreak_char_display)
5414 && (it->c == 0x8a0 || it->c == 0x8ad
5415 || it->c == 0x920 || it->c == 0x92d
5416 || it->c == 0xe20 || it->c == 0xe2d
5417 || it->c == 0xf20 || it->c == 0xf2d)))
5418 : (it->c >= 127
5419 && (!unibyte_display_via_language_environment
5420 || it->c == unibyte_char_to_multibyte (it->c)))))
5422 /* IT->c is a control character which must be displayed
5423 either as '\003' or as `^C' where the '\\' and '^'
5424 can be defined in the display table. Fill
5425 IT->ctl_chars with glyphs for what we have to
5426 display. Then, set IT->dpvec to these glyphs. */
5427 GLYPH g;
5428 int ctl_len;
5429 int face_id, lface_id = 0 ;
5430 GLYPH escape_glyph;
5432 /* Handle control characters with ^. */
5434 if (it->c < 128 && it->ctl_arrow_p)
5436 g = '^'; /* default glyph for Control */
5437 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5438 if (it->dp
5439 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5440 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5442 g = XINT (DISP_CTRL_GLYPH (it->dp));
5443 lface_id = FAST_GLYPH_FACE (g);
5445 if (lface_id)
5447 g = FAST_GLYPH_CHAR (g);
5448 face_id = merge_faces (it->f, Qt, lface_id,
5449 it->face_id);
5451 else if (it->f == last_escape_glyph_frame
5452 && it->face_id == last_escape_glyph_face_id)
5454 face_id = last_escape_glyph_merged_face_id;
5456 else
5458 /* Merge the escape-glyph face into the current face. */
5459 face_id = merge_faces (it->f, Qescape_glyph, 0,
5460 it->face_id);
5461 last_escape_glyph_frame = it->f;
5462 last_escape_glyph_face_id = it->face_id;
5463 last_escape_glyph_merged_face_id = face_id;
5466 XSETINT (it->ctl_chars[0], g);
5467 g = it->c ^ 0100;
5468 XSETINT (it->ctl_chars[1], g);
5469 ctl_len = 2;
5470 goto display_control;
5473 /* Handle non-break space in the mode where it only gets
5474 highlighting. */
5476 if (EQ (Vnobreak_char_display, Qt)
5477 && (it->c == 0x8a0 || it->c == 0x920
5478 || it->c == 0xe20 || it->c == 0xf20))
5480 /* Merge the no-break-space face into the current face. */
5481 face_id = merge_faces (it->f, Qnobreak_space, 0,
5482 it->face_id);
5484 g = it->c = ' ';
5485 XSETINT (it->ctl_chars[0], g);
5486 ctl_len = 1;
5487 goto display_control;
5490 /* Handle sequences that start with the "escape glyph". */
5492 /* the default escape glyph is \. */
5493 escape_glyph = '\\';
5495 if (it->dp
5496 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5497 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5499 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5500 lface_id = FAST_GLYPH_FACE (escape_glyph);
5502 if (lface_id)
5504 /* The display table specified a face.
5505 Merge it into face_id and also into escape_glyph. */
5506 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5507 face_id = merge_faces (it->f, Qt, lface_id,
5508 it->face_id);
5510 else if (it->f == last_escape_glyph_frame
5511 && it->face_id == last_escape_glyph_face_id)
5513 face_id = last_escape_glyph_merged_face_id;
5515 else
5517 /* Merge the escape-glyph face into the current face. */
5518 face_id = merge_faces (it->f, Qescape_glyph, 0,
5519 it->face_id);
5520 last_escape_glyph_frame = it->f;
5521 last_escape_glyph_face_id = it->face_id;
5522 last_escape_glyph_merged_face_id = face_id;
5525 /* Handle soft hyphens in the mode where they only get
5526 highlighting. */
5528 if (EQ (Vnobreak_char_display, Qt)
5529 && (it->c == 0x8ad || it->c == 0x92d
5530 || it->c == 0xe2d || it->c == 0xf2d))
5532 g = it->c = '-';
5533 XSETINT (it->ctl_chars[0], g);
5534 ctl_len = 1;
5535 goto display_control;
5538 /* Handle non-break space and soft hyphen
5539 with the escape glyph. */
5541 if (it->c == 0x8a0 || it->c == 0x8ad
5542 || it->c == 0x920 || it->c == 0x92d
5543 || it->c == 0xe20 || it->c == 0xe2d
5544 || it->c == 0xf20 || it->c == 0xf2d)
5546 XSETINT (it->ctl_chars[0], escape_glyph);
5547 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5548 XSETINT (it->ctl_chars[1], g);
5549 ctl_len = 2;
5550 goto display_control;
5554 unsigned char str[MAX_MULTIBYTE_LENGTH];
5555 int len;
5556 int i;
5558 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5559 if (SINGLE_BYTE_CHAR_P (it->c))
5560 str[0] = it->c, len = 1;
5561 else
5563 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5564 if (len < 0)
5566 /* It's an invalid character, which shouldn't
5567 happen actually, but due to bugs it may
5568 happen. Let's print the char as is, there's
5569 not much meaningful we can do with it. */
5570 str[0] = it->c;
5571 str[1] = it->c >> 8;
5572 str[2] = it->c >> 16;
5573 str[3] = it->c >> 24;
5574 len = 4;
5578 for (i = 0; i < len; i++)
5580 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5581 /* Insert three more glyphs into IT->ctl_chars for
5582 the octal display of the character. */
5583 g = ((str[i] >> 6) & 7) + '0';
5584 XSETINT (it->ctl_chars[i * 4 + 1], g);
5585 g = ((str[i] >> 3) & 7) + '0';
5586 XSETINT (it->ctl_chars[i * 4 + 2], g);
5587 g = (str[i] & 7) + '0';
5588 XSETINT (it->ctl_chars[i * 4 + 3], g);
5590 ctl_len = len * 4;
5593 display_control:
5594 /* Set up IT->dpvec and return first character from it. */
5595 it->dpvec_char_len = it->len;
5596 it->dpvec = it->ctl_chars;
5597 it->dpend = it->dpvec + ctl_len;
5598 it->current.dpvec_index = 0;
5599 it->dpvec_face_id = face_id;
5600 it->saved_face_id = it->face_id;
5601 it->method = GET_FROM_DISPLAY_VECTOR;
5602 it->ellipsis_p = 0;
5603 goto get_next;
5607 /* Adjust face id for a multibyte character. There are no
5608 multibyte character in unibyte text. */
5609 if (it->multibyte_p
5610 && success_p
5611 && FRAME_WINDOW_P (it->f))
5613 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5614 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5618 /* Is this character the last one of a run of characters with
5619 box? If yes, set IT->end_of_box_run_p to 1. */
5620 if (it->face_box_p
5621 && it->s == NULL)
5623 int face_id;
5624 struct face *face;
5626 it->end_of_box_run_p
5627 = ((face_id = face_after_it_pos (it),
5628 face_id != it->face_id)
5629 && (face = FACE_FROM_ID (it->f, face_id),
5630 face->box == FACE_NO_BOX));
5633 /* Value is 0 if end of buffer or string reached. */
5634 return success_p;
5638 /* Move IT to the next display element.
5640 RESEAT_P non-zero means if called on a newline in buffer text,
5641 skip to the next visible line start.
5643 Functions get_next_display_element and set_iterator_to_next are
5644 separate because I find this arrangement easier to handle than a
5645 get_next_display_element function that also increments IT's
5646 position. The way it is we can first look at an iterator's current
5647 display element, decide whether it fits on a line, and if it does,
5648 increment the iterator position. The other way around we probably
5649 would either need a flag indicating whether the iterator has to be
5650 incremented the next time, or we would have to implement a
5651 decrement position function which would not be easy to write. */
5653 void
5654 set_iterator_to_next (it, reseat_p)
5655 struct it *it;
5656 int reseat_p;
5658 /* Reset flags indicating start and end of a sequence of characters
5659 with box. Reset them at the start of this function because
5660 moving the iterator to a new position might set them. */
5661 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5663 switch (it->method)
5665 case GET_FROM_BUFFER:
5666 /* The current display element of IT is a character from
5667 current_buffer. Advance in the buffer, and maybe skip over
5668 invisible lines that are so because of selective display. */
5669 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5670 reseat_at_next_visible_line_start (it, 0);
5671 else
5673 xassert (it->len != 0);
5674 IT_BYTEPOS (*it) += it->len;
5675 IT_CHARPOS (*it) += 1;
5676 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5678 break;
5680 case GET_FROM_COMPOSITION:
5681 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5682 if (STRINGP (it->string))
5684 IT_STRING_BYTEPOS (*it) += it->len;
5685 IT_STRING_CHARPOS (*it) += it->cmp_len;
5686 it->method = GET_FROM_STRING;
5687 goto consider_string_end;
5689 else
5691 IT_BYTEPOS (*it) += it->len;
5692 IT_CHARPOS (*it) += it->cmp_len;
5693 it->method = GET_FROM_BUFFER;
5695 break;
5697 case GET_FROM_C_STRING:
5698 /* Current display element of IT is from a C string. */
5699 IT_BYTEPOS (*it) += it->len;
5700 IT_CHARPOS (*it) += 1;
5701 break;
5703 case GET_FROM_DISPLAY_VECTOR:
5704 /* Current display element of IT is from a display table entry.
5705 Advance in the display table definition. Reset it to null if
5706 end reached, and continue with characters from buffers/
5707 strings. */
5708 ++it->current.dpvec_index;
5710 /* Restore face of the iterator to what they were before the
5711 display vector entry (these entries may contain faces). */
5712 it->face_id = it->saved_face_id;
5714 if (it->dpvec + it->current.dpvec_index == it->dpend)
5716 int recheck_faces = it->ellipsis_p;
5718 if (it->s)
5719 it->method = GET_FROM_C_STRING;
5720 else if (STRINGP (it->string))
5721 it->method = GET_FROM_STRING;
5722 else
5723 it->method = GET_FROM_BUFFER;
5725 it->dpvec = NULL;
5726 it->current.dpvec_index = -1;
5728 /* Skip over characters which were displayed via IT->dpvec. */
5729 if (it->dpvec_char_len < 0)
5730 reseat_at_next_visible_line_start (it, 1);
5731 else if (it->dpvec_char_len > 0)
5733 if (it->method == GET_FROM_STRING
5734 && it->n_overlay_strings > 0)
5735 it->ignore_overlay_strings_at_pos_p = 1;
5736 it->len = it->dpvec_char_len;
5737 set_iterator_to_next (it, reseat_p);
5740 /* Maybe recheck faces after display vector */
5741 if (recheck_faces)
5742 it->stop_charpos = IT_CHARPOS (*it);
5744 break;
5746 case GET_FROM_STRING:
5747 /* Current display element is a character from a Lisp string. */
5748 xassert (it->s == NULL && STRINGP (it->string));
5749 IT_STRING_BYTEPOS (*it) += it->len;
5750 IT_STRING_CHARPOS (*it) += 1;
5752 consider_string_end:
5754 if (it->current.overlay_string_index >= 0)
5756 /* IT->string is an overlay string. Advance to the
5757 next, if there is one. */
5758 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5759 next_overlay_string (it);
5761 else
5763 /* IT->string is not an overlay string. If we reached
5764 its end, and there is something on IT->stack, proceed
5765 with what is on the stack. This can be either another
5766 string, this time an overlay string, or a buffer. */
5767 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5768 && it->sp > 0)
5770 pop_it (it);
5771 if (STRINGP (it->string))
5772 goto consider_string_end;
5773 it->method = GET_FROM_BUFFER;
5776 break;
5778 case GET_FROM_IMAGE:
5779 case GET_FROM_STRETCH:
5780 /* The position etc with which we have to proceed are on
5781 the stack. The position may be at the end of a string,
5782 if the `display' property takes up the whole string. */
5783 xassert (it->sp > 0);
5784 pop_it (it);
5785 it->image_id = 0;
5786 if (STRINGP (it->string))
5788 it->method = GET_FROM_STRING;
5789 goto consider_string_end;
5791 it->method = GET_FROM_BUFFER;
5792 break;
5794 default:
5795 /* There are no other methods defined, so this should be a bug. */
5796 abort ();
5799 xassert (it->method != GET_FROM_STRING
5800 || (STRINGP (it->string)
5801 && IT_STRING_CHARPOS (*it) >= 0));
5804 /* Load IT's display element fields with information about the next
5805 display element which comes from a display table entry or from the
5806 result of translating a control character to one of the forms `^C'
5807 or `\003'.
5809 IT->dpvec holds the glyphs to return as characters.
5810 IT->saved_face_id holds the face id before the display vector--
5811 it is restored into IT->face_idin set_iterator_to_next. */
5813 static int
5814 next_element_from_display_vector (it)
5815 struct it *it;
5817 /* Precondition. */
5818 xassert (it->dpvec && it->current.dpvec_index >= 0);
5820 it->face_id = it->saved_face_id;
5822 if (INTEGERP (*it->dpvec)
5823 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5825 GLYPH g;
5827 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5828 it->c = FAST_GLYPH_CHAR (g);
5829 it->len = CHAR_BYTES (it->c);
5831 /* The entry may contain a face id to use. Such a face id is
5832 the id of a Lisp face, not a realized face. A face id of
5833 zero means no face is specified. */
5834 if (it->dpvec_face_id >= 0)
5835 it->face_id = it->dpvec_face_id;
5836 else
5838 int lface_id = FAST_GLYPH_FACE (g);
5839 if (lface_id > 0)
5840 it->face_id = merge_faces (it->f, Qt, lface_id,
5841 it->saved_face_id);
5844 else
5845 /* Display table entry is invalid. Return a space. */
5846 it->c = ' ', it->len = 1;
5848 /* Don't change position and object of the iterator here. They are
5849 still the values of the character that had this display table
5850 entry or was translated, and that's what we want. */
5851 it->what = IT_CHARACTER;
5852 return 1;
5856 /* Load IT with the next display element from Lisp string IT->string.
5857 IT->current.string_pos is the current position within the string.
5858 If IT->current.overlay_string_index >= 0, the Lisp string is an
5859 overlay string. */
5861 static int
5862 next_element_from_string (it)
5863 struct it *it;
5865 struct text_pos position;
5867 xassert (STRINGP (it->string));
5868 xassert (IT_STRING_CHARPOS (*it) >= 0);
5869 position = it->current.string_pos;
5871 /* Time to check for invisible text? */
5872 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5873 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5875 handle_stop (it);
5877 /* Since a handler may have changed IT->method, we must
5878 recurse here. */
5879 return get_next_display_element (it);
5882 if (it->current.overlay_string_index >= 0)
5884 /* Get the next character from an overlay string. In overlay
5885 strings, There is no field width or padding with spaces to
5886 do. */
5887 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5889 it->what = IT_EOB;
5890 return 0;
5892 else if (STRING_MULTIBYTE (it->string))
5894 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5895 const unsigned char *s = (SDATA (it->string)
5896 + IT_STRING_BYTEPOS (*it));
5897 it->c = string_char_and_length (s, remaining, &it->len);
5899 else
5901 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5902 it->len = 1;
5905 else
5907 /* Get the next character from a Lisp string that is not an
5908 overlay string. Such strings come from the mode line, for
5909 example. We may have to pad with spaces, or truncate the
5910 string. See also next_element_from_c_string. */
5911 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5913 it->what = IT_EOB;
5914 return 0;
5916 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5918 /* Pad with spaces. */
5919 it->c = ' ', it->len = 1;
5920 CHARPOS (position) = BYTEPOS (position) = -1;
5922 else if (STRING_MULTIBYTE (it->string))
5924 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5925 const unsigned char *s = (SDATA (it->string)
5926 + IT_STRING_BYTEPOS (*it));
5927 it->c = string_char_and_length (s, maxlen, &it->len);
5929 else
5931 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5932 it->len = 1;
5936 /* Record what we have and where it came from. Note that we store a
5937 buffer position in IT->position although it could arguably be a
5938 string position. */
5939 it->what = IT_CHARACTER;
5940 it->object = it->string;
5941 it->position = position;
5942 return 1;
5946 /* Load IT with next display element from C string IT->s.
5947 IT->string_nchars is the maximum number of characters to return
5948 from the string. IT->end_charpos may be greater than
5949 IT->string_nchars when this function is called, in which case we
5950 may have to return padding spaces. Value is zero if end of string
5951 reached, including padding spaces. */
5953 static int
5954 next_element_from_c_string (it)
5955 struct it *it;
5957 int success_p = 1;
5959 xassert (it->s);
5960 it->what = IT_CHARACTER;
5961 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5962 it->object = Qnil;
5964 /* IT's position can be greater IT->string_nchars in case a field
5965 width or precision has been specified when the iterator was
5966 initialized. */
5967 if (IT_CHARPOS (*it) >= it->end_charpos)
5969 /* End of the game. */
5970 it->what = IT_EOB;
5971 success_p = 0;
5973 else if (IT_CHARPOS (*it) >= it->string_nchars)
5975 /* Pad with spaces. */
5976 it->c = ' ', it->len = 1;
5977 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5979 else if (it->multibyte_p)
5981 /* Implementation note: The calls to strlen apparently aren't a
5982 performance problem because there is no noticeable performance
5983 difference between Emacs running in unibyte or multibyte mode. */
5984 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5985 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5986 maxlen, &it->len);
5988 else
5989 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5991 return success_p;
5995 /* Set up IT to return characters from an ellipsis, if appropriate.
5996 The definition of the ellipsis glyphs may come from a display table
5997 entry. This function Fills IT with the first glyph from the
5998 ellipsis if an ellipsis is to be displayed. */
6000 static int
6001 next_element_from_ellipsis (it)
6002 struct it *it;
6004 if (it->selective_display_ellipsis_p)
6005 setup_for_ellipsis (it, it->len);
6006 else
6008 /* The face at the current position may be different from the
6009 face we find after the invisible text. Remember what it
6010 was in IT->saved_face_id, and signal that it's there by
6011 setting face_before_selective_p. */
6012 it->saved_face_id = it->face_id;
6013 it->method = GET_FROM_BUFFER;
6014 reseat_at_next_visible_line_start (it, 1);
6015 it->face_before_selective_p = 1;
6018 return get_next_display_element (it);
6022 /* Deliver an image display element. The iterator IT is already
6023 filled with image information (done in handle_display_prop). Value
6024 is always 1. */
6027 static int
6028 next_element_from_image (it)
6029 struct it *it;
6031 it->what = IT_IMAGE;
6032 return 1;
6036 /* Fill iterator IT with next display element from a stretch glyph
6037 property. IT->object is the value of the text property. Value is
6038 always 1. */
6040 static int
6041 next_element_from_stretch (it)
6042 struct it *it;
6044 it->what = IT_STRETCH;
6045 return 1;
6049 /* Load IT with the next display element from current_buffer. Value
6050 is zero if end of buffer reached. IT->stop_charpos is the next
6051 position at which to stop and check for text properties or buffer
6052 end. */
6054 static int
6055 next_element_from_buffer (it)
6056 struct it *it;
6058 int success_p = 1;
6060 /* Check this assumption, otherwise, we would never enter the
6061 if-statement, below. */
6062 xassert (IT_CHARPOS (*it) >= BEGV
6063 && IT_CHARPOS (*it) <= it->stop_charpos);
6065 if (IT_CHARPOS (*it) >= it->stop_charpos)
6067 if (IT_CHARPOS (*it) >= it->end_charpos)
6069 int overlay_strings_follow_p;
6071 /* End of the game, except when overlay strings follow that
6072 haven't been returned yet. */
6073 if (it->overlay_strings_at_end_processed_p)
6074 overlay_strings_follow_p = 0;
6075 else
6077 it->overlay_strings_at_end_processed_p = 1;
6078 overlay_strings_follow_p = get_overlay_strings (it, 0);
6081 if (overlay_strings_follow_p)
6082 success_p = get_next_display_element (it);
6083 else
6085 it->what = IT_EOB;
6086 it->position = it->current.pos;
6087 success_p = 0;
6090 else
6092 handle_stop (it);
6093 return get_next_display_element (it);
6096 else
6098 /* No face changes, overlays etc. in sight, so just return a
6099 character from current_buffer. */
6100 unsigned char *p;
6102 /* Maybe run the redisplay end trigger hook. Performance note:
6103 This doesn't seem to cost measurable time. */
6104 if (it->redisplay_end_trigger_charpos
6105 && it->glyph_row
6106 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6107 run_redisplay_end_trigger_hook (it);
6109 /* Get the next character, maybe multibyte. */
6110 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6111 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6113 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6114 - IT_BYTEPOS (*it));
6115 it->c = string_char_and_length (p, maxlen, &it->len);
6117 else
6118 it->c = *p, it->len = 1;
6120 /* Record what we have and where it came from. */
6121 it->what = IT_CHARACTER;;
6122 it->object = it->w->buffer;
6123 it->position = it->current.pos;
6125 /* Normally we return the character found above, except when we
6126 really want to return an ellipsis for selective display. */
6127 if (it->selective)
6129 if (it->c == '\n')
6131 /* A value of selective > 0 means hide lines indented more
6132 than that number of columns. */
6133 if (it->selective > 0
6134 && IT_CHARPOS (*it) + 1 < ZV
6135 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6136 IT_BYTEPOS (*it) + 1,
6137 (double) it->selective)) /* iftc */
6139 success_p = next_element_from_ellipsis (it);
6140 it->dpvec_char_len = -1;
6143 else if (it->c == '\r' && it->selective == -1)
6145 /* A value of selective == -1 means that everything from the
6146 CR to the end of the line is invisible, with maybe an
6147 ellipsis displayed for it. */
6148 success_p = next_element_from_ellipsis (it);
6149 it->dpvec_char_len = -1;
6154 /* Value is zero if end of buffer reached. */
6155 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6156 return success_p;
6160 /* Run the redisplay end trigger hook for IT. */
6162 static void
6163 run_redisplay_end_trigger_hook (it)
6164 struct it *it;
6166 Lisp_Object args[3];
6168 /* IT->glyph_row should be non-null, i.e. we should be actually
6169 displaying something, or otherwise we should not run the hook. */
6170 xassert (it->glyph_row);
6172 /* Set up hook arguments. */
6173 args[0] = Qredisplay_end_trigger_functions;
6174 args[1] = it->window;
6175 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6176 it->redisplay_end_trigger_charpos = 0;
6178 /* Since we are *trying* to run these functions, don't try to run
6179 them again, even if they get an error. */
6180 it->w->redisplay_end_trigger = Qnil;
6181 Frun_hook_with_args (3, args);
6183 /* Notice if it changed the face of the character we are on. */
6184 handle_face_prop (it);
6188 /* Deliver a composition display element. The iterator IT is already
6189 filled with composition information (done in
6190 handle_composition_prop). Value is always 1. */
6192 static int
6193 next_element_from_composition (it)
6194 struct it *it;
6196 it->what = IT_COMPOSITION;
6197 it->position = (STRINGP (it->string)
6198 ? it->current.string_pos
6199 : it->current.pos);
6200 return 1;
6205 /***********************************************************************
6206 Moving an iterator without producing glyphs
6207 ***********************************************************************/
6209 /* Check if iterator is at a position corresponding to a valid buffer
6210 position after some move_it_ call. */
6212 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6213 ((it)->method == GET_FROM_STRING \
6214 ? IT_STRING_CHARPOS (*it) == 0 \
6215 : 1)
6218 /* Move iterator IT to a specified buffer or X position within one
6219 line on the display without producing glyphs.
6221 OP should be a bit mask including some or all of these bits:
6222 MOVE_TO_X: Stop on reaching x-position TO_X.
6223 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6224 Regardless of OP's value, stop in reaching the end of the display line.
6226 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6227 This means, in particular, that TO_X includes window's horizontal
6228 scroll amount.
6230 The return value has several possible values that
6231 say what condition caused the scan to stop:
6233 MOVE_POS_MATCH_OR_ZV
6234 - when TO_POS or ZV was reached.
6236 MOVE_X_REACHED
6237 -when TO_X was reached before TO_POS or ZV were reached.
6239 MOVE_LINE_CONTINUED
6240 - when we reached the end of the display area and the line must
6241 be continued.
6243 MOVE_LINE_TRUNCATED
6244 - when we reached the end of the display area and the line is
6245 truncated.
6247 MOVE_NEWLINE_OR_CR
6248 - when we stopped at a line end, i.e. a newline or a CR and selective
6249 display is on. */
6251 static enum move_it_result
6252 move_it_in_display_line_to (it, to_charpos, to_x, op)
6253 struct it *it;
6254 int to_charpos, to_x, op;
6256 enum move_it_result result = MOVE_UNDEFINED;
6257 struct glyph_row *saved_glyph_row;
6259 /* Don't produce glyphs in produce_glyphs. */
6260 saved_glyph_row = it->glyph_row;
6261 it->glyph_row = NULL;
6263 #define BUFFER_POS_REACHED_P() \
6264 ((op & MOVE_TO_POS) != 0 \
6265 && BUFFERP (it->object) \
6266 && IT_CHARPOS (*it) >= to_charpos \
6267 && (it->method == GET_FROM_BUFFER \
6268 || (it->method == GET_FROM_DISPLAY_VECTOR \
6269 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6272 while (1)
6274 int x, i, ascent = 0, descent = 0;
6276 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6277 if ((op & MOVE_TO_POS) != 0
6278 && BUFFERP (it->object)
6279 && it->method == GET_FROM_BUFFER
6280 && IT_CHARPOS (*it) > to_charpos)
6282 result = MOVE_POS_MATCH_OR_ZV;
6283 break;
6286 /* Stop when ZV reached.
6287 We used to stop here when TO_CHARPOS reached as well, but that is
6288 too soon if this glyph does not fit on this line. So we handle it
6289 explicitly below. */
6290 if (!get_next_display_element (it)
6291 || (it->truncate_lines_p
6292 && BUFFER_POS_REACHED_P ()))
6294 result = MOVE_POS_MATCH_OR_ZV;
6295 break;
6298 /* The call to produce_glyphs will get the metrics of the
6299 display element IT is loaded with. We record in x the
6300 x-position before this display element in case it does not
6301 fit on the line. */
6302 x = it->current_x;
6304 /* Remember the line height so far in case the next element doesn't
6305 fit on the line. */
6306 if (!it->truncate_lines_p)
6308 ascent = it->max_ascent;
6309 descent = it->max_descent;
6312 PRODUCE_GLYPHS (it);
6314 if (it->area != TEXT_AREA)
6316 set_iterator_to_next (it, 1);
6317 continue;
6320 /* The number of glyphs we get back in IT->nglyphs will normally
6321 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6322 character on a terminal frame, or (iii) a line end. For the
6323 second case, IT->nglyphs - 1 padding glyphs will be present
6324 (on X frames, there is only one glyph produced for a
6325 composite character.
6327 The behavior implemented below means, for continuation lines,
6328 that as many spaces of a TAB as fit on the current line are
6329 displayed there. For terminal frames, as many glyphs of a
6330 multi-glyph character are displayed in the current line, too.
6331 This is what the old redisplay code did, and we keep it that
6332 way. Under X, the whole shape of a complex character must
6333 fit on the line or it will be completely displayed in the
6334 next line.
6336 Note that both for tabs and padding glyphs, all glyphs have
6337 the same width. */
6338 if (it->nglyphs)
6340 /* More than one glyph or glyph doesn't fit on line. All
6341 glyphs have the same width. */
6342 int single_glyph_width = it->pixel_width / it->nglyphs;
6343 int new_x;
6344 int x_before_this_char = x;
6345 int hpos_before_this_char = it->hpos;
6347 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6349 new_x = x + single_glyph_width;
6351 /* We want to leave anything reaching TO_X to the caller. */
6352 if ((op & MOVE_TO_X) && new_x > to_x)
6354 if (BUFFER_POS_REACHED_P ())
6355 goto buffer_pos_reached;
6356 it->current_x = x;
6357 result = MOVE_X_REACHED;
6358 break;
6360 else if (/* Lines are continued. */
6361 !it->truncate_lines_p
6362 && (/* And glyph doesn't fit on the line. */
6363 new_x > it->last_visible_x
6364 /* Or it fits exactly and we're on a window
6365 system frame. */
6366 || (new_x == it->last_visible_x
6367 && FRAME_WINDOW_P (it->f))))
6369 if (/* IT->hpos == 0 means the very first glyph
6370 doesn't fit on the line, e.g. a wide image. */
6371 it->hpos == 0
6372 || (new_x == it->last_visible_x
6373 && FRAME_WINDOW_P (it->f)))
6375 ++it->hpos;
6376 it->current_x = new_x;
6378 /* The character's last glyph just barely fits
6379 in this row. */
6380 if (i == it->nglyphs - 1)
6382 /* If this is the destination position,
6383 return a position *before* it in this row,
6384 now that we know it fits in this row. */
6385 if (BUFFER_POS_REACHED_P ())
6387 it->hpos = hpos_before_this_char;
6388 it->current_x = x_before_this_char;
6389 result = MOVE_POS_MATCH_OR_ZV;
6390 break;
6393 set_iterator_to_next (it, 1);
6394 #ifdef HAVE_WINDOW_SYSTEM
6395 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6397 if (!get_next_display_element (it))
6399 result = MOVE_POS_MATCH_OR_ZV;
6400 break;
6402 if (BUFFER_POS_REACHED_P ())
6404 if (ITERATOR_AT_END_OF_LINE_P (it))
6405 result = MOVE_POS_MATCH_OR_ZV;
6406 else
6407 result = MOVE_LINE_CONTINUED;
6408 break;
6410 if (ITERATOR_AT_END_OF_LINE_P (it))
6412 result = MOVE_NEWLINE_OR_CR;
6413 break;
6416 #endif /* HAVE_WINDOW_SYSTEM */
6419 else
6421 it->current_x = x;
6422 it->max_ascent = ascent;
6423 it->max_descent = descent;
6426 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6427 IT_CHARPOS (*it)));
6428 result = MOVE_LINE_CONTINUED;
6429 break;
6431 else if (BUFFER_POS_REACHED_P ())
6432 goto buffer_pos_reached;
6433 else if (new_x > it->first_visible_x)
6435 /* Glyph is visible. Increment number of glyphs that
6436 would be displayed. */
6437 ++it->hpos;
6439 else
6441 /* Glyph is completely off the left margin of the display
6442 area. Nothing to do. */
6446 if (result != MOVE_UNDEFINED)
6447 break;
6449 else if (BUFFER_POS_REACHED_P ())
6451 buffer_pos_reached:
6452 it->current_x = x;
6453 it->max_ascent = ascent;
6454 it->max_descent = descent;
6455 result = MOVE_POS_MATCH_OR_ZV;
6456 break;
6458 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6460 /* Stop when TO_X specified and reached. This check is
6461 necessary here because of lines consisting of a line end,
6462 only. The line end will not produce any glyphs and we
6463 would never get MOVE_X_REACHED. */
6464 xassert (it->nglyphs == 0);
6465 result = MOVE_X_REACHED;
6466 break;
6469 /* Is this a line end? If yes, we're done. */
6470 if (ITERATOR_AT_END_OF_LINE_P (it))
6472 result = MOVE_NEWLINE_OR_CR;
6473 break;
6476 /* The current display element has been consumed. Advance
6477 to the next. */
6478 set_iterator_to_next (it, 1);
6480 /* Stop if lines are truncated and IT's current x-position is
6481 past the right edge of the window now. */
6482 if (it->truncate_lines_p
6483 && it->current_x >= it->last_visible_x)
6485 #ifdef HAVE_WINDOW_SYSTEM
6486 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6488 if (!get_next_display_element (it)
6489 || BUFFER_POS_REACHED_P ())
6491 result = MOVE_POS_MATCH_OR_ZV;
6492 break;
6494 if (ITERATOR_AT_END_OF_LINE_P (it))
6496 result = MOVE_NEWLINE_OR_CR;
6497 break;
6500 #endif /* HAVE_WINDOW_SYSTEM */
6501 result = MOVE_LINE_TRUNCATED;
6502 break;
6506 #undef BUFFER_POS_REACHED_P
6508 /* Restore the iterator settings altered at the beginning of this
6509 function. */
6510 it->glyph_row = saved_glyph_row;
6511 return result;
6515 /* Move IT forward until it satisfies one or more of the criteria in
6516 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6518 OP is a bit-mask that specifies where to stop, and in particular,
6519 which of those four position arguments makes a difference. See the
6520 description of enum move_operation_enum.
6522 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6523 screen line, this function will set IT to the next position >
6524 TO_CHARPOS. */
6526 void
6527 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6528 struct it *it;
6529 int to_charpos, to_x, to_y, to_vpos;
6530 int op;
6532 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6533 int line_height;
6534 int reached = 0;
6536 for (;;)
6538 if (op & MOVE_TO_VPOS)
6540 /* If no TO_CHARPOS and no TO_X specified, stop at the
6541 start of the line TO_VPOS. */
6542 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6544 if (it->vpos == to_vpos)
6546 reached = 1;
6547 break;
6549 else
6550 skip = move_it_in_display_line_to (it, -1, -1, 0);
6552 else
6554 /* TO_VPOS >= 0 means stop at TO_X in the line at
6555 TO_VPOS, or at TO_POS, whichever comes first. */
6556 if (it->vpos == to_vpos)
6558 reached = 2;
6559 break;
6562 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6564 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6566 reached = 3;
6567 break;
6569 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6571 /* We have reached TO_X but not in the line we want. */
6572 skip = move_it_in_display_line_to (it, to_charpos,
6573 -1, MOVE_TO_POS);
6574 if (skip == MOVE_POS_MATCH_OR_ZV)
6576 reached = 4;
6577 break;
6582 else if (op & MOVE_TO_Y)
6584 struct it it_backup;
6586 /* TO_Y specified means stop at TO_X in the line containing
6587 TO_Y---or at TO_CHARPOS if this is reached first. The
6588 problem is that we can't really tell whether the line
6589 contains TO_Y before we have completely scanned it, and
6590 this may skip past TO_X. What we do is to first scan to
6591 TO_X.
6593 If TO_X is not specified, use a TO_X of zero. The reason
6594 is to make the outcome of this function more predictable.
6595 If we didn't use TO_X == 0, we would stop at the end of
6596 the line which is probably not what a caller would expect
6597 to happen. */
6598 skip = move_it_in_display_line_to (it, to_charpos,
6599 ((op & MOVE_TO_X)
6600 ? to_x : 0),
6601 (MOVE_TO_X
6602 | (op & MOVE_TO_POS)));
6604 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6605 if (skip == MOVE_POS_MATCH_OR_ZV)
6607 reached = 5;
6608 break;
6611 /* If TO_X was reached, we would like to know whether TO_Y
6612 is in the line. This can only be said if we know the
6613 total line height which requires us to scan the rest of
6614 the line. */
6615 if (skip == MOVE_X_REACHED)
6617 it_backup = *it;
6618 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6619 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6620 op & MOVE_TO_POS);
6621 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6624 /* Now, decide whether TO_Y is in this line. */
6625 line_height = it->max_ascent + it->max_descent;
6626 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6628 if (to_y >= it->current_y
6629 && to_y < it->current_y + line_height)
6631 if (skip == MOVE_X_REACHED)
6632 /* If TO_Y is in this line and TO_X was reached above,
6633 we scanned too far. We have to restore IT's settings
6634 to the ones before skipping. */
6635 *it = it_backup;
6636 reached = 6;
6638 else if (skip == MOVE_X_REACHED)
6640 skip = skip2;
6641 if (skip == MOVE_POS_MATCH_OR_ZV)
6642 reached = 7;
6645 if (reached)
6646 break;
6648 else
6649 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6651 switch (skip)
6653 case MOVE_POS_MATCH_OR_ZV:
6654 reached = 8;
6655 goto out;
6657 case MOVE_NEWLINE_OR_CR:
6658 set_iterator_to_next (it, 1);
6659 it->continuation_lines_width = 0;
6660 break;
6662 case MOVE_LINE_TRUNCATED:
6663 it->continuation_lines_width = 0;
6664 reseat_at_next_visible_line_start (it, 0);
6665 if ((op & MOVE_TO_POS) != 0
6666 && IT_CHARPOS (*it) > to_charpos)
6668 reached = 9;
6669 goto out;
6671 break;
6673 case MOVE_LINE_CONTINUED:
6674 it->continuation_lines_width += it->current_x;
6675 break;
6677 default:
6678 abort ();
6681 /* Reset/increment for the next run. */
6682 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6683 it->current_x = it->hpos = 0;
6684 it->current_y += it->max_ascent + it->max_descent;
6685 ++it->vpos;
6686 last_height = it->max_ascent + it->max_descent;
6687 last_max_ascent = it->max_ascent;
6688 it->max_ascent = it->max_descent = 0;
6691 out:
6693 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6697 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6699 If DY > 0, move IT backward at least that many pixels. DY = 0
6700 means move IT backward to the preceding line start or BEGV. This
6701 function may move over more than DY pixels if IT->current_y - DY
6702 ends up in the middle of a line; in this case IT->current_y will be
6703 set to the top of the line moved to. */
6705 void
6706 move_it_vertically_backward (it, dy)
6707 struct it *it;
6708 int dy;
6710 int nlines, h;
6711 struct it it2, it3;
6712 int start_pos;
6714 move_further_back:
6715 xassert (dy >= 0);
6717 start_pos = IT_CHARPOS (*it);
6719 /* Estimate how many newlines we must move back. */
6720 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6722 /* Set the iterator's position that many lines back. */
6723 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6724 back_to_previous_visible_line_start (it);
6726 /* Reseat the iterator here. When moving backward, we don't want
6727 reseat to skip forward over invisible text, set up the iterator
6728 to deliver from overlay strings at the new position etc. So,
6729 use reseat_1 here. */
6730 reseat_1 (it, it->current.pos, 1);
6732 /* We are now surely at a line start. */
6733 it->current_x = it->hpos = 0;
6734 it->continuation_lines_width = 0;
6736 /* Move forward and see what y-distance we moved. First move to the
6737 start of the next line so that we get its height. We need this
6738 height to be able to tell whether we reached the specified
6739 y-distance. */
6740 it2 = *it;
6741 it2.max_ascent = it2.max_descent = 0;
6744 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6745 MOVE_TO_POS | MOVE_TO_VPOS);
6747 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6748 xassert (IT_CHARPOS (*it) >= BEGV);
6749 it3 = it2;
6751 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6752 xassert (IT_CHARPOS (*it) >= BEGV);
6753 /* H is the actual vertical distance from the position in *IT
6754 and the starting position. */
6755 h = it2.current_y - it->current_y;
6756 /* NLINES is the distance in number of lines. */
6757 nlines = it2.vpos - it->vpos;
6759 /* Correct IT's y and vpos position
6760 so that they are relative to the starting point. */
6761 it->vpos -= nlines;
6762 it->current_y -= h;
6764 if (dy == 0)
6766 /* DY == 0 means move to the start of the screen line. The
6767 value of nlines is > 0 if continuation lines were involved. */
6768 if (nlines > 0)
6769 move_it_by_lines (it, nlines, 1);
6770 #if 0
6771 /* I think this assert is bogus if buffer contains
6772 invisible text or images. KFS. */
6773 xassert (IT_CHARPOS (*it) <= start_pos);
6774 #endif
6776 else
6778 /* The y-position we try to reach, relative to *IT.
6779 Note that H has been subtracted in front of the if-statement. */
6780 int target_y = it->current_y + h - dy;
6781 int y0 = it3.current_y;
6782 int y1 = line_bottom_y (&it3);
6783 int line_height = y1 - y0;
6785 /* If we did not reach target_y, try to move further backward if
6786 we can. If we moved too far backward, try to move forward. */
6787 if (target_y < it->current_y
6788 /* This is heuristic. In a window that's 3 lines high, with
6789 a line height of 13 pixels each, recentering with point
6790 on the bottom line will try to move -39/2 = 19 pixels
6791 backward. Try to avoid moving into the first line. */
6792 && (it->current_y - target_y
6793 > min (window_box_height (it->w), line_height * 2 / 3))
6794 && IT_CHARPOS (*it) > BEGV)
6796 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6797 target_y - it->current_y));
6798 dy = it->current_y - target_y;
6799 goto move_further_back;
6801 else if (target_y >= it->current_y + line_height
6802 && IT_CHARPOS (*it) < ZV)
6804 /* Should move forward by at least one line, maybe more.
6806 Note: Calling move_it_by_lines can be expensive on
6807 terminal frames, where compute_motion is used (via
6808 vmotion) to do the job, when there are very long lines
6809 and truncate-lines is nil. That's the reason for
6810 treating terminal frames specially here. */
6812 if (!FRAME_WINDOW_P (it->f))
6813 move_it_vertically (it, target_y - (it->current_y + line_height));
6814 else
6818 move_it_by_lines (it, 1, 1);
6820 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6823 #if 0
6824 /* I think this assert is bogus if buffer contains
6825 invisible text or images. KFS. */
6826 xassert (IT_CHARPOS (*it) >= BEGV);
6827 #endif
6833 /* Move IT by a specified amount of pixel lines DY. DY negative means
6834 move backwards. DY = 0 means move to start of screen line. At the
6835 end, IT will be on the start of a screen line. */
6837 void
6838 move_it_vertically (it, dy)
6839 struct it *it;
6840 int dy;
6842 if (dy <= 0)
6843 move_it_vertically_backward (it, -dy);
6844 else
6846 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6847 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6848 MOVE_TO_POS | MOVE_TO_Y);
6849 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6851 /* If buffer ends in ZV without a newline, move to the start of
6852 the line to satisfy the post-condition. */
6853 if (IT_CHARPOS (*it) == ZV
6854 && ZV > BEGV
6855 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6856 move_it_by_lines (it, 0, 0);
6861 /* Move iterator IT past the end of the text line it is in. */
6863 void
6864 move_it_past_eol (it)
6865 struct it *it;
6867 enum move_it_result rc;
6869 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6870 if (rc == MOVE_NEWLINE_OR_CR)
6871 set_iterator_to_next (it, 0);
6875 #if 0 /* Currently not used. */
6877 /* Return non-zero if some text between buffer positions START_CHARPOS
6878 and END_CHARPOS is invisible. IT->window is the window for text
6879 property lookup. */
6881 static int
6882 invisible_text_between_p (it, start_charpos, end_charpos)
6883 struct it *it;
6884 int start_charpos, end_charpos;
6886 Lisp_Object prop, limit;
6887 int invisible_found_p;
6889 xassert (it != NULL && start_charpos <= end_charpos);
6891 /* Is text at START invisible? */
6892 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6893 it->window);
6894 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6895 invisible_found_p = 1;
6896 else
6898 limit = Fnext_single_char_property_change (make_number (start_charpos),
6899 Qinvisible, Qnil,
6900 make_number (end_charpos));
6901 invisible_found_p = XFASTINT (limit) < end_charpos;
6904 return invisible_found_p;
6907 #endif /* 0 */
6910 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6911 negative means move up. DVPOS == 0 means move to the start of the
6912 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6913 NEED_Y_P is zero, IT->current_y will be left unchanged.
6915 Further optimization ideas: If we would know that IT->f doesn't use
6916 a face with proportional font, we could be faster for
6917 truncate-lines nil. */
6919 void
6920 move_it_by_lines (it, dvpos, need_y_p)
6921 struct it *it;
6922 int dvpos, need_y_p;
6924 struct position pos;
6926 if (!FRAME_WINDOW_P (it->f))
6928 struct text_pos textpos;
6930 /* We can use vmotion on frames without proportional fonts. */
6931 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6932 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6933 reseat (it, textpos, 1);
6934 it->vpos += pos.vpos;
6935 it->current_y += pos.vpos;
6937 else if (dvpos == 0)
6939 /* DVPOS == 0 means move to the start of the screen line. */
6940 move_it_vertically_backward (it, 0);
6941 xassert (it->current_x == 0 && it->hpos == 0);
6942 /* Let next call to line_bottom_y calculate real line height */
6943 last_height = 0;
6945 else if (dvpos > 0)
6947 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6948 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6949 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6951 else
6953 struct it it2;
6954 int start_charpos, i;
6956 /* Start at the beginning of the screen line containing IT's
6957 position. This may actually move vertically backwards,
6958 in case of overlays, so adjust dvpos accordingly. */
6959 dvpos += it->vpos;
6960 move_it_vertically_backward (it, 0);
6961 dvpos -= it->vpos;
6963 /* Go back -DVPOS visible lines and reseat the iterator there. */
6964 start_charpos = IT_CHARPOS (*it);
6965 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6966 back_to_previous_visible_line_start (it);
6967 reseat (it, it->current.pos, 1);
6969 /* Move further back if we end up in a string or an image. */
6970 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6972 /* First try to move to start of display line. */
6973 dvpos += it->vpos;
6974 move_it_vertically_backward (it, 0);
6975 dvpos -= it->vpos;
6976 if (IT_POS_VALID_AFTER_MOVE_P (it))
6977 break;
6978 /* If start of line is still in string or image,
6979 move further back. */
6980 back_to_previous_visible_line_start (it);
6981 reseat (it, it->current.pos, 1);
6982 dvpos--;
6985 it->current_x = it->hpos = 0;
6987 /* Above call may have moved too far if continuation lines
6988 are involved. Scan forward and see if it did. */
6989 it2 = *it;
6990 it2.vpos = it2.current_y = 0;
6991 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6992 it->vpos -= it2.vpos;
6993 it->current_y -= it2.current_y;
6994 it->current_x = it->hpos = 0;
6996 /* If we moved too far back, move IT some lines forward. */
6997 if (it2.vpos > -dvpos)
6999 int delta = it2.vpos + dvpos;
7000 it2 = *it;
7001 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7002 /* Move back again if we got too far ahead. */
7003 if (IT_CHARPOS (*it) >= start_charpos)
7004 *it = it2;
7009 /* Return 1 if IT points into the middle of a display vector. */
7012 in_display_vector_p (it)
7013 struct it *it;
7015 return (it->method == GET_FROM_DISPLAY_VECTOR
7016 && it->current.dpvec_index > 0
7017 && it->dpvec + it->current.dpvec_index != it->dpend);
7021 /***********************************************************************
7022 Messages
7023 ***********************************************************************/
7026 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7027 to *Messages*. */
7029 void
7030 add_to_log (format, arg1, arg2)
7031 char *format;
7032 Lisp_Object arg1, arg2;
7034 Lisp_Object args[3];
7035 Lisp_Object msg, fmt;
7036 char *buffer;
7037 int len;
7038 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7039 USE_SAFE_ALLOCA;
7041 /* Do nothing if called asynchronously. Inserting text into
7042 a buffer may call after-change-functions and alike and
7043 that would means running Lisp asynchronously. */
7044 if (handling_signal)
7045 return;
7047 fmt = msg = Qnil;
7048 GCPRO4 (fmt, msg, arg1, arg2);
7050 args[0] = fmt = build_string (format);
7051 args[1] = arg1;
7052 args[2] = arg2;
7053 msg = Fformat (3, args);
7055 len = SBYTES (msg) + 1;
7056 SAFE_ALLOCA (buffer, char *, len);
7057 bcopy (SDATA (msg), buffer, len);
7059 message_dolog (buffer, len - 1, 1, 0);
7060 SAFE_FREE ();
7062 UNGCPRO;
7066 /* Output a newline in the *Messages* buffer if "needs" one. */
7068 void
7069 message_log_maybe_newline ()
7071 if (message_log_need_newline)
7072 message_dolog ("", 0, 1, 0);
7076 /* Add a string M of length NBYTES to the message log, optionally
7077 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7078 nonzero, means interpret the contents of M as multibyte. This
7079 function calls low-level routines in order to bypass text property
7080 hooks, etc. which might not be safe to run.
7082 This may GC (insert may run before/after change hooks),
7083 so the buffer M must NOT point to a Lisp string. */
7085 void
7086 message_dolog (m, nbytes, nlflag, multibyte)
7087 const char *m;
7088 int nbytes, nlflag, multibyte;
7090 if (!NILP (Vmemory_full))
7091 return;
7093 if (!NILP (Vmessage_log_max))
7095 struct buffer *oldbuf;
7096 Lisp_Object oldpoint, oldbegv, oldzv;
7097 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7098 int point_at_end = 0;
7099 int zv_at_end = 0;
7100 Lisp_Object old_deactivate_mark, tem;
7101 struct gcpro gcpro1;
7103 old_deactivate_mark = Vdeactivate_mark;
7104 oldbuf = current_buffer;
7105 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7106 current_buffer->undo_list = Qt;
7108 oldpoint = message_dolog_marker1;
7109 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7110 oldbegv = message_dolog_marker2;
7111 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7112 oldzv = message_dolog_marker3;
7113 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7114 GCPRO1 (old_deactivate_mark);
7116 if (PT == Z)
7117 point_at_end = 1;
7118 if (ZV == Z)
7119 zv_at_end = 1;
7121 BEGV = BEG;
7122 BEGV_BYTE = BEG_BYTE;
7123 ZV = Z;
7124 ZV_BYTE = Z_BYTE;
7125 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7127 /* Insert the string--maybe converting multibyte to single byte
7128 or vice versa, so that all the text fits the buffer. */
7129 if (multibyte
7130 && NILP (current_buffer->enable_multibyte_characters))
7132 int i, c, char_bytes;
7133 unsigned char work[1];
7135 /* Convert a multibyte string to single-byte
7136 for the *Message* buffer. */
7137 for (i = 0; i < nbytes; i += char_bytes)
7139 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7140 work[0] = (SINGLE_BYTE_CHAR_P (c)
7142 : multibyte_char_to_unibyte (c, Qnil));
7143 insert_1_both (work, 1, 1, 1, 0, 0);
7146 else if (! multibyte
7147 && ! NILP (current_buffer->enable_multibyte_characters))
7149 int i, c, char_bytes;
7150 unsigned char *msg = (unsigned char *) m;
7151 unsigned char str[MAX_MULTIBYTE_LENGTH];
7152 /* Convert a single-byte string to multibyte
7153 for the *Message* buffer. */
7154 for (i = 0; i < nbytes; i++)
7156 c = unibyte_char_to_multibyte (msg[i]);
7157 char_bytes = CHAR_STRING (c, str);
7158 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7161 else if (nbytes)
7162 insert_1 (m, nbytes, 1, 0, 0);
7164 if (nlflag)
7166 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7167 insert_1 ("\n", 1, 1, 0, 0);
7169 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7170 this_bol = PT;
7171 this_bol_byte = PT_BYTE;
7173 /* See if this line duplicates the previous one.
7174 If so, combine duplicates. */
7175 if (this_bol > BEG)
7177 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7178 prev_bol = PT;
7179 prev_bol_byte = PT_BYTE;
7181 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7182 this_bol, this_bol_byte);
7183 if (dup)
7185 del_range_both (prev_bol, prev_bol_byte,
7186 this_bol, this_bol_byte, 0);
7187 if (dup > 1)
7189 char dupstr[40];
7190 int duplen;
7192 /* If you change this format, don't forget to also
7193 change message_log_check_duplicate. */
7194 sprintf (dupstr, " [%d times]", dup);
7195 duplen = strlen (dupstr);
7196 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7197 insert_1 (dupstr, duplen, 1, 0, 1);
7202 /* If we have more than the desired maximum number of lines
7203 in the *Messages* buffer now, delete the oldest ones.
7204 This is safe because we don't have undo in this buffer. */
7206 if (NATNUMP (Vmessage_log_max))
7208 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7209 -XFASTINT (Vmessage_log_max) - 1, 0);
7210 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7213 BEGV = XMARKER (oldbegv)->charpos;
7214 BEGV_BYTE = marker_byte_position (oldbegv);
7216 if (zv_at_end)
7218 ZV = Z;
7219 ZV_BYTE = Z_BYTE;
7221 else
7223 ZV = XMARKER (oldzv)->charpos;
7224 ZV_BYTE = marker_byte_position (oldzv);
7227 if (point_at_end)
7228 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7229 else
7230 /* We can't do Fgoto_char (oldpoint) because it will run some
7231 Lisp code. */
7232 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7233 XMARKER (oldpoint)->bytepos);
7235 UNGCPRO;
7236 unchain_marker (XMARKER (oldpoint));
7237 unchain_marker (XMARKER (oldbegv));
7238 unchain_marker (XMARKER (oldzv));
7240 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7241 set_buffer_internal (oldbuf);
7242 if (NILP (tem))
7243 windows_or_buffers_changed = old_windows_or_buffers_changed;
7244 message_log_need_newline = !nlflag;
7245 Vdeactivate_mark = old_deactivate_mark;
7250 /* We are at the end of the buffer after just having inserted a newline.
7251 (Note: We depend on the fact we won't be crossing the gap.)
7252 Check to see if the most recent message looks a lot like the previous one.
7253 Return 0 if different, 1 if the new one should just replace it, or a
7254 value N > 1 if we should also append " [N times]". */
7256 static int
7257 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7258 int prev_bol, this_bol;
7259 int prev_bol_byte, this_bol_byte;
7261 int i;
7262 int len = Z_BYTE - 1 - this_bol_byte;
7263 int seen_dots = 0;
7264 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7265 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7267 for (i = 0; i < len; i++)
7269 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7270 seen_dots = 1;
7271 if (p1[i] != p2[i])
7272 return seen_dots;
7274 p1 += len;
7275 if (*p1 == '\n')
7276 return 2;
7277 if (*p1++ == ' ' && *p1++ == '[')
7279 int n = 0;
7280 while (*p1 >= '0' && *p1 <= '9')
7281 n = n * 10 + *p1++ - '0';
7282 if (strncmp (p1, " times]\n", 8) == 0)
7283 return n+1;
7285 return 0;
7289 /* Display an echo area message M with a specified length of NBYTES
7290 bytes. The string may include null characters. If M is 0, clear
7291 out any existing message, and let the mini-buffer text show
7292 through.
7294 This may GC, so the buffer M must NOT point to a Lisp string. */
7296 void
7297 message2 (m, nbytes, multibyte)
7298 const char *m;
7299 int nbytes;
7300 int multibyte;
7302 /* First flush out any partial line written with print. */
7303 message_log_maybe_newline ();
7304 if (m)
7305 message_dolog (m, nbytes, 1, multibyte);
7306 message2_nolog (m, nbytes, multibyte);
7310 /* The non-logging counterpart of message2. */
7312 void
7313 message2_nolog (m, nbytes, multibyte)
7314 const char *m;
7315 int nbytes, multibyte;
7317 struct frame *sf = SELECTED_FRAME ();
7318 message_enable_multibyte = multibyte;
7320 if (noninteractive)
7322 if (noninteractive_need_newline)
7323 putc ('\n', stderr);
7324 noninteractive_need_newline = 0;
7325 if (m)
7326 fwrite (m, nbytes, 1, stderr);
7327 if (cursor_in_echo_area == 0)
7328 fprintf (stderr, "\n");
7329 fflush (stderr);
7331 /* A null message buffer means that the frame hasn't really been
7332 initialized yet. Error messages get reported properly by
7333 cmd_error, so this must be just an informative message; toss it. */
7334 else if (INTERACTIVE
7335 && sf->glyphs_initialized_p
7336 && FRAME_MESSAGE_BUF (sf))
7338 Lisp_Object mini_window;
7339 struct frame *f;
7341 /* Get the frame containing the mini-buffer
7342 that the selected frame is using. */
7343 mini_window = FRAME_MINIBUF_WINDOW (sf);
7344 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7346 FRAME_SAMPLE_VISIBILITY (f);
7347 if (FRAME_VISIBLE_P (sf)
7348 && ! FRAME_VISIBLE_P (f))
7349 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7351 if (m)
7353 set_message (m, Qnil, nbytes, multibyte);
7354 if (minibuffer_auto_raise)
7355 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7357 else
7358 clear_message (1, 1);
7360 do_pending_window_change (0);
7361 echo_area_display (1);
7362 do_pending_window_change (0);
7363 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7364 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7369 /* Display an echo area message M with a specified length of NBYTES
7370 bytes. The string may include null characters. If M is not a
7371 string, clear out any existing message, and let the mini-buffer
7372 text show through.
7374 This function cancels echoing. */
7376 void
7377 message3 (m, nbytes, multibyte)
7378 Lisp_Object m;
7379 int nbytes;
7380 int multibyte;
7382 struct gcpro gcpro1;
7384 GCPRO1 (m);
7385 clear_message (1,1);
7386 cancel_echoing ();
7388 /* First flush out any partial line written with print. */
7389 message_log_maybe_newline ();
7390 if (STRINGP (m))
7392 char *buffer;
7393 USE_SAFE_ALLOCA;
7395 SAFE_ALLOCA (buffer, char *, nbytes);
7396 bcopy (SDATA (m), buffer, nbytes);
7397 message_dolog (buffer, nbytes, 1, multibyte);
7398 SAFE_FREE ();
7400 message3_nolog (m, nbytes, multibyte);
7402 UNGCPRO;
7406 /* The non-logging version of message3.
7407 This does not cancel echoing, because it is used for echoing.
7408 Perhaps we need to make a separate function for echoing
7409 and make this cancel echoing. */
7411 void
7412 message3_nolog (m, nbytes, multibyte)
7413 Lisp_Object m;
7414 int nbytes, multibyte;
7416 struct frame *sf = SELECTED_FRAME ();
7417 message_enable_multibyte = multibyte;
7419 if (noninteractive)
7421 if (noninteractive_need_newline)
7422 putc ('\n', stderr);
7423 noninteractive_need_newline = 0;
7424 if (STRINGP (m))
7425 fwrite (SDATA (m), nbytes, 1, stderr);
7426 if (cursor_in_echo_area == 0)
7427 fprintf (stderr, "\n");
7428 fflush (stderr);
7430 /* A null message buffer means that the frame hasn't really been
7431 initialized yet. Error messages get reported properly by
7432 cmd_error, so this must be just an informative message; toss it. */
7433 else if (INTERACTIVE
7434 && sf->glyphs_initialized_p
7435 && FRAME_MESSAGE_BUF (sf))
7437 Lisp_Object mini_window;
7438 Lisp_Object frame;
7439 struct frame *f;
7441 /* Get the frame containing the mini-buffer
7442 that the selected frame is using. */
7443 mini_window = FRAME_MINIBUF_WINDOW (sf);
7444 frame = XWINDOW (mini_window)->frame;
7445 f = XFRAME (frame);
7447 FRAME_SAMPLE_VISIBILITY (f);
7448 if (FRAME_VISIBLE_P (sf)
7449 && !FRAME_VISIBLE_P (f))
7450 Fmake_frame_visible (frame);
7452 if (STRINGP (m) && SCHARS (m) > 0)
7454 set_message (NULL, m, nbytes, multibyte);
7455 if (minibuffer_auto_raise)
7456 Fraise_frame (frame);
7457 /* Assume we are not echoing.
7458 (If we are, echo_now will override this.) */
7459 echo_message_buffer = Qnil;
7461 else
7462 clear_message (1, 1);
7464 do_pending_window_change (0);
7465 echo_area_display (1);
7466 do_pending_window_change (0);
7467 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7468 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7473 /* Display a null-terminated echo area message M. If M is 0, clear
7474 out any existing message, and let the mini-buffer text show through.
7476 The buffer M must continue to exist until after the echo area gets
7477 cleared or some other message gets displayed there. Do not pass
7478 text that is stored in a Lisp string. Do not pass text in a buffer
7479 that was alloca'd. */
7481 void
7482 message1 (m)
7483 char *m;
7485 message2 (m, (m ? strlen (m) : 0), 0);
7489 /* The non-logging counterpart of message1. */
7491 void
7492 message1_nolog (m)
7493 char *m;
7495 message2_nolog (m, (m ? strlen (m) : 0), 0);
7498 /* Display a message M which contains a single %s
7499 which gets replaced with STRING. */
7501 void
7502 message_with_string (m, string, log)
7503 char *m;
7504 Lisp_Object string;
7505 int log;
7507 CHECK_STRING (string);
7509 if (noninteractive)
7511 if (m)
7513 if (noninteractive_need_newline)
7514 putc ('\n', stderr);
7515 noninteractive_need_newline = 0;
7516 fprintf (stderr, m, SDATA (string));
7517 if (cursor_in_echo_area == 0)
7518 fprintf (stderr, "\n");
7519 fflush (stderr);
7522 else if (INTERACTIVE)
7524 /* The frame whose minibuffer we're going to display the message on.
7525 It may be larger than the selected frame, so we need
7526 to use its buffer, not the selected frame's buffer. */
7527 Lisp_Object mini_window;
7528 struct frame *f, *sf = SELECTED_FRAME ();
7530 /* Get the frame containing the minibuffer
7531 that the selected frame is using. */
7532 mini_window = FRAME_MINIBUF_WINDOW (sf);
7533 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7535 /* A null message buffer means that the frame hasn't really been
7536 initialized yet. Error messages get reported properly by
7537 cmd_error, so this must be just an informative message; toss it. */
7538 if (FRAME_MESSAGE_BUF (f))
7540 Lisp_Object args[2], message;
7541 struct gcpro gcpro1, gcpro2;
7543 args[0] = build_string (m);
7544 args[1] = message = string;
7545 GCPRO2 (args[0], message);
7546 gcpro1.nvars = 2;
7548 message = Fformat (2, args);
7550 if (log)
7551 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7552 else
7553 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7555 UNGCPRO;
7557 /* Print should start at the beginning of the message
7558 buffer next time. */
7559 message_buf_print = 0;
7565 /* Dump an informative message to the minibuf. If M is 0, clear out
7566 any existing message, and let the mini-buffer text show through. */
7568 /* VARARGS 1 */
7569 void
7570 message (m, a1, a2, a3)
7571 char *m;
7572 EMACS_INT a1, a2, a3;
7574 if (noninteractive)
7576 if (m)
7578 if (noninteractive_need_newline)
7579 putc ('\n', stderr);
7580 noninteractive_need_newline = 0;
7581 fprintf (stderr, m, a1, a2, a3);
7582 if (cursor_in_echo_area == 0)
7583 fprintf (stderr, "\n");
7584 fflush (stderr);
7587 else if (INTERACTIVE)
7589 /* The frame whose mini-buffer we're going to display the message
7590 on. It may be larger than the selected frame, so we need to
7591 use its buffer, not the selected frame's buffer. */
7592 Lisp_Object mini_window;
7593 struct frame *f, *sf = SELECTED_FRAME ();
7595 /* Get the frame containing the mini-buffer
7596 that the selected frame is using. */
7597 mini_window = FRAME_MINIBUF_WINDOW (sf);
7598 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7600 /* A null message buffer means that the frame hasn't really been
7601 initialized yet. Error messages get reported properly by
7602 cmd_error, so this must be just an informative message; toss
7603 it. */
7604 if (FRAME_MESSAGE_BUF (f))
7606 if (m)
7608 int len;
7609 #ifdef NO_ARG_ARRAY
7610 char *a[3];
7611 a[0] = (char *) a1;
7612 a[1] = (char *) a2;
7613 a[2] = (char *) a3;
7615 len = doprnt (FRAME_MESSAGE_BUF (f),
7616 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7617 #else
7618 len = doprnt (FRAME_MESSAGE_BUF (f),
7619 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7620 (char **) &a1);
7621 #endif /* NO_ARG_ARRAY */
7623 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7625 else
7626 message1 (0);
7628 /* Print should start at the beginning of the message
7629 buffer next time. */
7630 message_buf_print = 0;
7636 /* The non-logging version of message. */
7638 void
7639 message_nolog (m, a1, a2, a3)
7640 char *m;
7641 EMACS_INT a1, a2, a3;
7643 Lisp_Object old_log_max;
7644 old_log_max = Vmessage_log_max;
7645 Vmessage_log_max = Qnil;
7646 message (m, a1, a2, a3);
7647 Vmessage_log_max = old_log_max;
7651 /* Display the current message in the current mini-buffer. This is
7652 only called from error handlers in process.c, and is not time
7653 critical. */
7655 void
7656 update_echo_area ()
7658 if (!NILP (echo_area_buffer[0]))
7660 Lisp_Object string;
7661 string = Fcurrent_message ();
7662 message3 (string, SBYTES (string),
7663 !NILP (current_buffer->enable_multibyte_characters));
7668 /* Make sure echo area buffers in `echo_buffers' are live.
7669 If they aren't, make new ones. */
7671 static void
7672 ensure_echo_area_buffers ()
7674 int i;
7676 for (i = 0; i < 2; ++i)
7677 if (!BUFFERP (echo_buffer[i])
7678 || NILP (XBUFFER (echo_buffer[i])->name))
7680 char name[30];
7681 Lisp_Object old_buffer;
7682 int j;
7684 old_buffer = echo_buffer[i];
7685 sprintf (name, " *Echo Area %d*", i);
7686 echo_buffer[i] = Fget_buffer_create (build_string (name));
7687 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7689 for (j = 0; j < 2; ++j)
7690 if (EQ (old_buffer, echo_area_buffer[j]))
7691 echo_area_buffer[j] = echo_buffer[i];
7696 /* Call FN with args A1..A4 with either the current or last displayed
7697 echo_area_buffer as current buffer.
7699 WHICH zero means use the current message buffer
7700 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7701 from echo_buffer[] and clear it.
7703 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7704 suitable buffer from echo_buffer[] and clear it.
7706 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7707 that the current message becomes the last displayed one, make
7708 choose a suitable buffer for echo_area_buffer[0], and clear it.
7710 Value is what FN returns. */
7712 static int
7713 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7714 struct window *w;
7715 int which;
7716 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7717 EMACS_INT a1;
7718 Lisp_Object a2;
7719 EMACS_INT a3, a4;
7721 Lisp_Object buffer;
7722 int this_one, the_other, clear_buffer_p, rc;
7723 int count = SPECPDL_INDEX ();
7725 /* If buffers aren't live, make new ones. */
7726 ensure_echo_area_buffers ();
7728 clear_buffer_p = 0;
7730 if (which == 0)
7731 this_one = 0, the_other = 1;
7732 else if (which > 0)
7733 this_one = 1, the_other = 0;
7734 else
7736 this_one = 0, the_other = 1;
7737 clear_buffer_p = 1;
7739 /* We need a fresh one in case the current echo buffer equals
7740 the one containing the last displayed echo area message. */
7741 if (!NILP (echo_area_buffer[this_one])
7742 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7743 echo_area_buffer[this_one] = Qnil;
7746 /* Choose a suitable buffer from echo_buffer[] is we don't
7747 have one. */
7748 if (NILP (echo_area_buffer[this_one]))
7750 echo_area_buffer[this_one]
7751 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7752 ? echo_buffer[the_other]
7753 : echo_buffer[this_one]);
7754 clear_buffer_p = 1;
7757 buffer = echo_area_buffer[this_one];
7759 /* Don't get confused by reusing the buffer used for echoing
7760 for a different purpose. */
7761 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7762 cancel_echoing ();
7764 record_unwind_protect (unwind_with_echo_area_buffer,
7765 with_echo_area_buffer_unwind_data (w));
7767 /* Make the echo area buffer current. Note that for display
7768 purposes, it is not necessary that the displayed window's buffer
7769 == current_buffer, except for text property lookup. So, let's
7770 only set that buffer temporarily here without doing a full
7771 Fset_window_buffer. We must also change w->pointm, though,
7772 because otherwise an assertions in unshow_buffer fails, and Emacs
7773 aborts. */
7774 set_buffer_internal_1 (XBUFFER (buffer));
7775 if (w)
7777 w->buffer = buffer;
7778 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7781 current_buffer->undo_list = Qt;
7782 current_buffer->read_only = Qnil;
7783 specbind (Qinhibit_read_only, Qt);
7784 specbind (Qinhibit_modification_hooks, Qt);
7786 if (clear_buffer_p && Z > BEG)
7787 del_range (BEG, Z);
7789 xassert (BEGV >= BEG);
7790 xassert (ZV <= Z && ZV >= BEGV);
7792 rc = fn (a1, a2, a3, a4);
7794 xassert (BEGV >= BEG);
7795 xassert (ZV <= Z && ZV >= BEGV);
7797 unbind_to (count, Qnil);
7798 return rc;
7802 /* Save state that should be preserved around the call to the function
7803 FN called in with_echo_area_buffer. */
7805 static Lisp_Object
7806 with_echo_area_buffer_unwind_data (w)
7807 struct window *w;
7809 int i = 0;
7810 Lisp_Object vector;
7812 /* Reduce consing by keeping one vector in
7813 Vwith_echo_area_save_vector. */
7814 vector = Vwith_echo_area_save_vector;
7815 Vwith_echo_area_save_vector = Qnil;
7817 if (NILP (vector))
7818 vector = Fmake_vector (make_number (7), Qnil);
7820 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7821 AREF (vector, i) = Vdeactivate_mark, ++i;
7822 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7824 if (w)
7826 XSETWINDOW (AREF (vector, i), w); ++i;
7827 AREF (vector, i) = w->buffer; ++i;
7828 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7829 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7831 else
7833 int end = i + 4;
7834 for (; i < end; ++i)
7835 AREF (vector, i) = Qnil;
7838 xassert (i == ASIZE (vector));
7839 return vector;
7843 /* Restore global state from VECTOR which was created by
7844 with_echo_area_buffer_unwind_data. */
7846 static Lisp_Object
7847 unwind_with_echo_area_buffer (vector)
7848 Lisp_Object vector;
7850 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7851 Vdeactivate_mark = AREF (vector, 1);
7852 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7854 if (WINDOWP (AREF (vector, 3)))
7856 struct window *w;
7857 Lisp_Object buffer, charpos, bytepos;
7859 w = XWINDOW (AREF (vector, 3));
7860 buffer = AREF (vector, 4);
7861 charpos = AREF (vector, 5);
7862 bytepos = AREF (vector, 6);
7864 w->buffer = buffer;
7865 set_marker_both (w->pointm, buffer,
7866 XFASTINT (charpos), XFASTINT (bytepos));
7869 Vwith_echo_area_save_vector = vector;
7870 return Qnil;
7874 /* Set up the echo area for use by print functions. MULTIBYTE_P
7875 non-zero means we will print multibyte. */
7877 void
7878 setup_echo_area_for_printing (multibyte_p)
7879 int multibyte_p;
7881 /* If we can't find an echo area any more, exit. */
7882 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7883 Fkill_emacs (Qnil);
7885 ensure_echo_area_buffers ();
7887 if (!message_buf_print)
7889 /* A message has been output since the last time we printed.
7890 Choose a fresh echo area buffer. */
7891 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7892 echo_area_buffer[0] = echo_buffer[1];
7893 else
7894 echo_area_buffer[0] = echo_buffer[0];
7896 /* Switch to that buffer and clear it. */
7897 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7898 current_buffer->truncate_lines = Qnil;
7900 if (Z > BEG)
7902 int count = SPECPDL_INDEX ();
7903 specbind (Qinhibit_read_only, Qt);
7904 /* Note that undo recording is always disabled. */
7905 del_range (BEG, Z);
7906 unbind_to (count, Qnil);
7908 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7910 /* Set up the buffer for the multibyteness we need. */
7911 if (multibyte_p
7912 != !NILP (current_buffer->enable_multibyte_characters))
7913 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7915 /* Raise the frame containing the echo area. */
7916 if (minibuffer_auto_raise)
7918 struct frame *sf = SELECTED_FRAME ();
7919 Lisp_Object mini_window;
7920 mini_window = FRAME_MINIBUF_WINDOW (sf);
7921 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7924 message_log_maybe_newline ();
7925 message_buf_print = 1;
7927 else
7929 if (NILP (echo_area_buffer[0]))
7931 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7932 echo_area_buffer[0] = echo_buffer[1];
7933 else
7934 echo_area_buffer[0] = echo_buffer[0];
7937 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7939 /* Someone switched buffers between print requests. */
7940 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7941 current_buffer->truncate_lines = Qnil;
7947 /* Display an echo area message in window W. Value is non-zero if W's
7948 height is changed. If display_last_displayed_message_p is
7949 non-zero, display the message that was last displayed, otherwise
7950 display the current message. */
7952 static int
7953 display_echo_area (w)
7954 struct window *w;
7956 int i, no_message_p, window_height_changed_p, count;
7958 /* Temporarily disable garbage collections while displaying the echo
7959 area. This is done because a GC can print a message itself.
7960 That message would modify the echo area buffer's contents while a
7961 redisplay of the buffer is going on, and seriously confuse
7962 redisplay. */
7963 count = inhibit_garbage_collection ();
7965 /* If there is no message, we must call display_echo_area_1
7966 nevertheless because it resizes the window. But we will have to
7967 reset the echo_area_buffer in question to nil at the end because
7968 with_echo_area_buffer will sets it to an empty buffer. */
7969 i = display_last_displayed_message_p ? 1 : 0;
7970 no_message_p = NILP (echo_area_buffer[i]);
7972 window_height_changed_p
7973 = with_echo_area_buffer (w, display_last_displayed_message_p,
7974 display_echo_area_1,
7975 (EMACS_INT) w, Qnil, 0, 0);
7977 if (no_message_p)
7978 echo_area_buffer[i] = Qnil;
7980 unbind_to (count, Qnil);
7981 return window_height_changed_p;
7985 /* Helper for display_echo_area. Display the current buffer which
7986 contains the current echo area message in window W, a mini-window,
7987 a pointer to which is passed in A1. A2..A4 are currently not used.
7988 Change the height of W so that all of the message is displayed.
7989 Value is non-zero if height of W was changed. */
7991 static int
7992 display_echo_area_1 (a1, a2, a3, a4)
7993 EMACS_INT a1;
7994 Lisp_Object a2;
7995 EMACS_INT a3, a4;
7997 struct window *w = (struct window *) a1;
7998 Lisp_Object window;
7999 struct text_pos start;
8000 int window_height_changed_p = 0;
8002 /* Do this before displaying, so that we have a large enough glyph
8003 matrix for the display. If we can't get enough space for the
8004 whole text, display the last N lines. That works by setting w->start. */
8005 window_height_changed_p = resize_mini_window (w, 0);
8007 /* Use the starting position chosen by resize_mini_window. */
8008 SET_TEXT_POS_FROM_MARKER (start, w->start);
8010 /* Display. */
8011 clear_glyph_matrix (w->desired_matrix);
8012 XSETWINDOW (window, w);
8013 try_window (window, start, 0);
8015 return window_height_changed_p;
8019 /* Resize the echo area window to exactly the size needed for the
8020 currently displayed message, if there is one. If a mini-buffer
8021 is active, don't shrink it. */
8023 void
8024 resize_echo_area_exactly ()
8026 if (BUFFERP (echo_area_buffer[0])
8027 && WINDOWP (echo_area_window))
8029 struct window *w = XWINDOW (echo_area_window);
8030 int resized_p;
8031 Lisp_Object resize_exactly;
8033 if (minibuf_level == 0)
8034 resize_exactly = Qt;
8035 else
8036 resize_exactly = Qnil;
8038 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8039 (EMACS_INT) w, resize_exactly, 0, 0);
8040 if (resized_p)
8042 ++windows_or_buffers_changed;
8043 ++update_mode_lines;
8044 redisplay_internal (0);
8050 /* Callback function for with_echo_area_buffer, when used from
8051 resize_echo_area_exactly. A1 contains a pointer to the window to
8052 resize, EXACTLY non-nil means resize the mini-window exactly to the
8053 size of the text displayed. A3 and A4 are not used. Value is what
8054 resize_mini_window returns. */
8056 static int
8057 resize_mini_window_1 (a1, exactly, a3, a4)
8058 EMACS_INT a1;
8059 Lisp_Object exactly;
8060 EMACS_INT a3, a4;
8062 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8066 /* Resize mini-window W to fit the size of its contents. EXACT:P
8067 means size the window exactly to the size needed. Otherwise, it's
8068 only enlarged until W's buffer is empty.
8070 Set W->start to the right place to begin display. If the whole
8071 contents fit, start at the beginning. Otherwise, start so as
8072 to make the end of the contents appear. This is particularly
8073 important for y-or-n-p, but seems desirable generally.
8075 Value is non-zero if the window height has been changed. */
8078 resize_mini_window (w, exact_p)
8079 struct window *w;
8080 int exact_p;
8082 struct frame *f = XFRAME (w->frame);
8083 int window_height_changed_p = 0;
8085 xassert (MINI_WINDOW_P (w));
8087 /* By default, start display at the beginning. */
8088 set_marker_both (w->start, w->buffer,
8089 BUF_BEGV (XBUFFER (w->buffer)),
8090 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8092 /* Don't resize windows while redisplaying a window; it would
8093 confuse redisplay functions when the size of the window they are
8094 displaying changes from under them. Such a resizing can happen,
8095 for instance, when which-func prints a long message while
8096 we are running fontification-functions. We're running these
8097 functions with safe_call which binds inhibit-redisplay to t. */
8098 if (!NILP (Vinhibit_redisplay))
8099 return 0;
8101 /* Nil means don't try to resize. */
8102 if (NILP (Vresize_mini_windows)
8103 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8104 return 0;
8106 if (!FRAME_MINIBUF_ONLY_P (f))
8108 struct it it;
8109 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8110 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8111 int height, max_height;
8112 int unit = FRAME_LINE_HEIGHT (f);
8113 struct text_pos start;
8114 struct buffer *old_current_buffer = NULL;
8116 if (current_buffer != XBUFFER (w->buffer))
8118 old_current_buffer = current_buffer;
8119 set_buffer_internal (XBUFFER (w->buffer));
8122 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8124 /* Compute the max. number of lines specified by the user. */
8125 if (FLOATP (Vmax_mini_window_height))
8126 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8127 else if (INTEGERP (Vmax_mini_window_height))
8128 max_height = XINT (Vmax_mini_window_height);
8129 else
8130 max_height = total_height / 4;
8132 /* Correct that max. height if it's bogus. */
8133 max_height = max (1, max_height);
8134 max_height = min (total_height, max_height);
8136 /* Find out the height of the text in the window. */
8137 if (it.truncate_lines_p)
8138 height = 1;
8139 else
8141 last_height = 0;
8142 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8143 if (it.max_ascent == 0 && it.max_descent == 0)
8144 height = it.current_y + last_height;
8145 else
8146 height = it.current_y + it.max_ascent + it.max_descent;
8147 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8148 height = (height + unit - 1) / unit;
8151 /* Compute a suitable window start. */
8152 if (height > max_height)
8154 height = max_height;
8155 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8156 move_it_vertically_backward (&it, (height - 1) * unit);
8157 start = it.current.pos;
8159 else
8160 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8161 SET_MARKER_FROM_TEXT_POS (w->start, start);
8163 if (EQ (Vresize_mini_windows, Qgrow_only))
8165 /* Let it grow only, until we display an empty message, in which
8166 case the window shrinks again. */
8167 if (height > WINDOW_TOTAL_LINES (w))
8169 int old_height = WINDOW_TOTAL_LINES (w);
8170 freeze_window_starts (f, 1);
8171 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8172 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8174 else if (height < WINDOW_TOTAL_LINES (w)
8175 && (exact_p || BEGV == ZV))
8177 int old_height = WINDOW_TOTAL_LINES (w);
8178 freeze_window_starts (f, 0);
8179 shrink_mini_window (w);
8180 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8183 else
8185 /* Always resize to exact size needed. */
8186 if (height > WINDOW_TOTAL_LINES (w))
8188 int old_height = WINDOW_TOTAL_LINES (w);
8189 freeze_window_starts (f, 1);
8190 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8191 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8193 else if (height < WINDOW_TOTAL_LINES (w))
8195 int old_height = WINDOW_TOTAL_LINES (w);
8196 freeze_window_starts (f, 0);
8197 shrink_mini_window (w);
8199 if (height)
8201 freeze_window_starts (f, 1);
8202 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8205 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8209 if (old_current_buffer)
8210 set_buffer_internal (old_current_buffer);
8213 return window_height_changed_p;
8217 /* Value is the current message, a string, or nil if there is no
8218 current message. */
8220 Lisp_Object
8221 current_message ()
8223 Lisp_Object msg;
8225 if (NILP (echo_area_buffer[0]))
8226 msg = Qnil;
8227 else
8229 with_echo_area_buffer (0, 0, current_message_1,
8230 (EMACS_INT) &msg, Qnil, 0, 0);
8231 if (NILP (msg))
8232 echo_area_buffer[0] = Qnil;
8235 return msg;
8239 static int
8240 current_message_1 (a1, a2, a3, a4)
8241 EMACS_INT a1;
8242 Lisp_Object a2;
8243 EMACS_INT a3, a4;
8245 Lisp_Object *msg = (Lisp_Object *) a1;
8247 if (Z > BEG)
8248 *msg = make_buffer_string (BEG, Z, 1);
8249 else
8250 *msg = Qnil;
8251 return 0;
8255 /* Push the current message on Vmessage_stack for later restauration
8256 by restore_message. Value is non-zero if the current message isn't
8257 empty. This is a relatively infrequent operation, so it's not
8258 worth optimizing. */
8261 push_message ()
8263 Lisp_Object msg;
8264 msg = current_message ();
8265 Vmessage_stack = Fcons (msg, Vmessage_stack);
8266 return STRINGP (msg);
8270 /* Restore message display from the top of Vmessage_stack. */
8272 void
8273 restore_message ()
8275 Lisp_Object msg;
8277 xassert (CONSP (Vmessage_stack));
8278 msg = XCAR (Vmessage_stack);
8279 if (STRINGP (msg))
8280 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8281 else
8282 message3_nolog (msg, 0, 0);
8286 /* Handler for record_unwind_protect calling pop_message. */
8288 Lisp_Object
8289 pop_message_unwind (dummy)
8290 Lisp_Object dummy;
8292 pop_message ();
8293 return Qnil;
8296 /* Pop the top-most entry off Vmessage_stack. */
8298 void
8299 pop_message ()
8301 xassert (CONSP (Vmessage_stack));
8302 Vmessage_stack = XCDR (Vmessage_stack);
8306 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8307 exits. If the stack is not empty, we have a missing pop_message
8308 somewhere. */
8310 void
8311 check_message_stack ()
8313 if (!NILP (Vmessage_stack))
8314 abort ();
8318 /* Truncate to NCHARS what will be displayed in the echo area the next
8319 time we display it---but don't redisplay it now. */
8321 void
8322 truncate_echo_area (nchars)
8323 int nchars;
8325 if (nchars == 0)
8326 echo_area_buffer[0] = Qnil;
8327 /* A null message buffer means that the frame hasn't really been
8328 initialized yet. Error messages get reported properly by
8329 cmd_error, so this must be just an informative message; toss it. */
8330 else if (!noninteractive
8331 && INTERACTIVE
8332 && !NILP (echo_area_buffer[0]))
8334 struct frame *sf = SELECTED_FRAME ();
8335 if (FRAME_MESSAGE_BUF (sf))
8336 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8341 /* Helper function for truncate_echo_area. Truncate the current
8342 message to at most NCHARS characters. */
8344 static int
8345 truncate_message_1 (nchars, a2, a3, a4)
8346 EMACS_INT nchars;
8347 Lisp_Object a2;
8348 EMACS_INT a3, a4;
8350 if (BEG + nchars < Z)
8351 del_range (BEG + nchars, Z);
8352 if (Z == BEG)
8353 echo_area_buffer[0] = Qnil;
8354 return 0;
8358 /* Set the current message to a substring of S or STRING.
8360 If STRING is a Lisp string, set the message to the first NBYTES
8361 bytes from STRING. NBYTES zero means use the whole string. If
8362 STRING is multibyte, the message will be displayed multibyte.
8364 If S is not null, set the message to the first LEN bytes of S. LEN
8365 zero means use the whole string. MULTIBYTE_P non-zero means S is
8366 multibyte. Display the message multibyte in that case.
8368 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8369 to t before calling set_message_1 (which calls insert).
8372 void
8373 set_message (s, string, nbytes, multibyte_p)
8374 const char *s;
8375 Lisp_Object string;
8376 int nbytes, multibyte_p;
8378 message_enable_multibyte
8379 = ((s && multibyte_p)
8380 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8382 with_echo_area_buffer (0, -1, set_message_1,
8383 (EMACS_INT) s, string, nbytes, multibyte_p);
8384 message_buf_print = 0;
8385 help_echo_showing_p = 0;
8389 /* Helper function for set_message. Arguments have the same meaning
8390 as there, with A1 corresponding to S and A2 corresponding to STRING
8391 This function is called with the echo area buffer being
8392 current. */
8394 static int
8395 set_message_1 (a1, a2, nbytes, multibyte_p)
8396 EMACS_INT a1;
8397 Lisp_Object a2;
8398 EMACS_INT nbytes, multibyte_p;
8400 const char *s = (const char *) a1;
8401 Lisp_Object string = a2;
8403 /* Change multibyteness of the echo buffer appropriately. */
8404 if (message_enable_multibyte
8405 != !NILP (current_buffer->enable_multibyte_characters))
8406 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8408 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8410 /* Insert new message at BEG. */
8411 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8413 if (STRINGP (string))
8415 int nchars;
8417 if (nbytes == 0)
8418 nbytes = SBYTES (string);
8419 nchars = string_byte_to_char (string, nbytes);
8421 /* This function takes care of single/multibyte conversion. We
8422 just have to ensure that the echo area buffer has the right
8423 setting of enable_multibyte_characters. */
8424 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8426 else if (s)
8428 if (nbytes == 0)
8429 nbytes = strlen (s);
8431 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8433 /* Convert from multi-byte to single-byte. */
8434 int i, c, n;
8435 unsigned char work[1];
8437 /* Convert a multibyte string to single-byte. */
8438 for (i = 0; i < nbytes; i += n)
8440 c = string_char_and_length (s + i, nbytes - i, &n);
8441 work[0] = (SINGLE_BYTE_CHAR_P (c)
8443 : multibyte_char_to_unibyte (c, Qnil));
8444 insert_1_both (work, 1, 1, 1, 0, 0);
8447 else if (!multibyte_p
8448 && !NILP (current_buffer->enable_multibyte_characters))
8450 /* Convert from single-byte to multi-byte. */
8451 int i, c, n;
8452 const unsigned char *msg = (const unsigned char *) s;
8453 unsigned char str[MAX_MULTIBYTE_LENGTH];
8455 /* Convert a single-byte string to multibyte. */
8456 for (i = 0; i < nbytes; i++)
8458 c = unibyte_char_to_multibyte (msg[i]);
8459 n = CHAR_STRING (c, str);
8460 insert_1_both (str, 1, n, 1, 0, 0);
8463 else
8464 insert_1 (s, nbytes, 1, 0, 0);
8467 return 0;
8471 /* Clear messages. CURRENT_P non-zero means clear the current
8472 message. LAST_DISPLAYED_P non-zero means clear the message
8473 last displayed. */
8475 void
8476 clear_message (current_p, last_displayed_p)
8477 int current_p, last_displayed_p;
8479 if (current_p)
8481 echo_area_buffer[0] = Qnil;
8482 message_cleared_p = 1;
8485 if (last_displayed_p)
8486 echo_area_buffer[1] = Qnil;
8488 message_buf_print = 0;
8491 /* Clear garbaged frames.
8493 This function is used where the old redisplay called
8494 redraw_garbaged_frames which in turn called redraw_frame which in
8495 turn called clear_frame. The call to clear_frame was a source of
8496 flickering. I believe a clear_frame is not necessary. It should
8497 suffice in the new redisplay to invalidate all current matrices,
8498 and ensure a complete redisplay of all windows. */
8500 static void
8501 clear_garbaged_frames ()
8503 if (frame_garbaged)
8505 Lisp_Object tail, frame;
8506 int changed_count = 0;
8508 FOR_EACH_FRAME (tail, frame)
8510 struct frame *f = XFRAME (frame);
8512 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8514 if (f->resized_p)
8516 Fredraw_frame (frame);
8517 f->force_flush_display_p = 1;
8519 clear_current_matrices (f);
8520 changed_count++;
8521 f->garbaged = 0;
8522 f->resized_p = 0;
8526 frame_garbaged = 0;
8527 if (changed_count)
8528 ++windows_or_buffers_changed;
8533 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8534 is non-zero update selected_frame. Value is non-zero if the
8535 mini-windows height has been changed. */
8537 static int
8538 echo_area_display (update_frame_p)
8539 int update_frame_p;
8541 Lisp_Object mini_window;
8542 struct window *w;
8543 struct frame *f;
8544 int window_height_changed_p = 0;
8545 struct frame *sf = SELECTED_FRAME ();
8547 mini_window = FRAME_MINIBUF_WINDOW (sf);
8548 w = XWINDOW (mini_window);
8549 f = XFRAME (WINDOW_FRAME (w));
8551 /* Don't display if frame is invisible or not yet initialized. */
8552 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8553 return 0;
8555 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8556 #ifndef MAC_OS8
8557 #ifdef HAVE_WINDOW_SYSTEM
8558 /* When Emacs starts, selected_frame may be the initial terminal
8559 frame. If we let this through, a message would be displayed on
8560 the terminal. */
8561 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8562 return 0;
8563 #endif /* HAVE_WINDOW_SYSTEM */
8564 #endif
8566 /* Redraw garbaged frames. */
8567 if (frame_garbaged)
8568 clear_garbaged_frames ();
8570 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8572 echo_area_window = mini_window;
8573 window_height_changed_p = display_echo_area (w);
8574 w->must_be_updated_p = 1;
8576 /* Update the display, unless called from redisplay_internal.
8577 Also don't update the screen during redisplay itself. The
8578 update will happen at the end of redisplay, and an update
8579 here could cause confusion. */
8580 if (update_frame_p && !redisplaying_p)
8582 int n = 0;
8584 /* If the display update has been interrupted by pending
8585 input, update mode lines in the frame. Due to the
8586 pending input, it might have been that redisplay hasn't
8587 been called, so that mode lines above the echo area are
8588 garbaged. This looks odd, so we prevent it here. */
8589 if (!display_completed)
8590 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8592 if (window_height_changed_p
8593 /* Don't do this if Emacs is shutting down. Redisplay
8594 needs to run hooks. */
8595 && !NILP (Vrun_hooks))
8597 /* Must update other windows. Likewise as in other
8598 cases, don't let this update be interrupted by
8599 pending input. */
8600 int count = SPECPDL_INDEX ();
8601 specbind (Qredisplay_dont_pause, Qt);
8602 windows_or_buffers_changed = 1;
8603 redisplay_internal (0);
8604 unbind_to (count, Qnil);
8606 else if (FRAME_WINDOW_P (f) && n == 0)
8608 /* Window configuration is the same as before.
8609 Can do with a display update of the echo area,
8610 unless we displayed some mode lines. */
8611 update_single_window (w, 1);
8612 FRAME_RIF (f)->flush_display (f);
8614 else
8615 update_frame (f, 1, 1);
8617 /* If cursor is in the echo area, make sure that the next
8618 redisplay displays the minibuffer, so that the cursor will
8619 be replaced with what the minibuffer wants. */
8620 if (cursor_in_echo_area)
8621 ++windows_or_buffers_changed;
8624 else if (!EQ (mini_window, selected_window))
8625 windows_or_buffers_changed++;
8627 /* Last displayed message is now the current message. */
8628 echo_area_buffer[1] = echo_area_buffer[0];
8629 /* Inform read_char that we're not echoing. */
8630 echo_message_buffer = Qnil;
8632 /* Prevent redisplay optimization in redisplay_internal by resetting
8633 this_line_start_pos. This is done because the mini-buffer now
8634 displays the message instead of its buffer text. */
8635 if (EQ (mini_window, selected_window))
8636 CHARPOS (this_line_start_pos) = 0;
8638 return window_height_changed_p;
8643 /***********************************************************************
8644 Mode Lines and Frame Titles
8645 ***********************************************************************/
8647 /* A buffer for constructing non-propertized mode-line strings and
8648 frame titles in it; allocated from the heap in init_xdisp and
8649 resized as needed in store_mode_line_noprop_char. */
8651 static char *mode_line_noprop_buf;
8653 /* The buffer's end, and a current output position in it. */
8655 static char *mode_line_noprop_buf_end;
8656 static char *mode_line_noprop_ptr;
8658 #define MODE_LINE_NOPROP_LEN(start) \
8659 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8661 static enum {
8662 MODE_LINE_DISPLAY = 0,
8663 MODE_LINE_TITLE,
8664 MODE_LINE_NOPROP,
8665 MODE_LINE_STRING
8666 } mode_line_target;
8668 /* Alist that caches the results of :propertize.
8669 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8670 static Lisp_Object mode_line_proptrans_alist;
8672 /* List of strings making up the mode-line. */
8673 static Lisp_Object mode_line_string_list;
8675 /* Base face property when building propertized mode line string. */
8676 static Lisp_Object mode_line_string_face;
8677 static Lisp_Object mode_line_string_face_prop;
8680 /* Unwind data for mode line strings */
8682 static Lisp_Object Vmode_line_unwind_vector;
8684 static Lisp_Object
8685 format_mode_line_unwind_data (obuf, save_proptrans)
8686 struct buffer *obuf;
8688 Lisp_Object vector;
8690 /* Reduce consing by keeping one vector in
8691 Vwith_echo_area_save_vector. */
8692 vector = Vmode_line_unwind_vector;
8693 Vmode_line_unwind_vector = Qnil;
8695 if (NILP (vector))
8696 vector = Fmake_vector (make_number (7), Qnil);
8698 AREF (vector, 0) = make_number (mode_line_target);
8699 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8700 AREF (vector, 2) = mode_line_string_list;
8701 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8702 AREF (vector, 4) = mode_line_string_face;
8703 AREF (vector, 5) = mode_line_string_face_prop;
8705 if (obuf)
8706 XSETBUFFER (AREF (vector, 6), obuf);
8707 else
8708 AREF (vector, 6) = Qnil;
8710 return vector;
8713 static Lisp_Object
8714 unwind_format_mode_line (vector)
8715 Lisp_Object vector;
8717 mode_line_target = XINT (AREF (vector, 0));
8718 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8719 mode_line_string_list = AREF (vector, 2);
8720 if (! EQ (AREF (vector, 3), Qt))
8721 mode_line_proptrans_alist = AREF (vector, 3);
8722 mode_line_string_face = AREF (vector, 4);
8723 mode_line_string_face_prop = AREF (vector, 5);
8725 if (!NILP (AREF (vector, 6)))
8727 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8728 AREF (vector, 6) = Qnil;
8731 Vmode_line_unwind_vector = vector;
8732 return Qnil;
8736 /* Store a single character C for the frame title in mode_line_noprop_buf.
8737 Re-allocate mode_line_noprop_buf if necessary. */
8739 static void
8740 #ifdef PROTOTYPES
8741 store_mode_line_noprop_char (char c)
8742 #else
8743 store_mode_line_noprop_char (c)
8744 char c;
8745 #endif
8747 /* If output position has reached the end of the allocated buffer,
8748 double the buffer's size. */
8749 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8751 int len = MODE_LINE_NOPROP_LEN (0);
8752 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8753 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8754 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8755 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8758 *mode_line_noprop_ptr++ = c;
8762 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8763 mode_line_noprop_ptr. STR is the string to store. Do not copy
8764 characters that yield more columns than PRECISION; PRECISION <= 0
8765 means copy the whole string. Pad with spaces until FIELD_WIDTH
8766 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8767 pad. Called from display_mode_element when it is used to build a
8768 frame title. */
8770 static int
8771 store_mode_line_noprop (str, field_width, precision)
8772 const unsigned char *str;
8773 int field_width, precision;
8775 int n = 0;
8776 int dummy, nbytes;
8778 /* Copy at most PRECISION chars from STR. */
8779 nbytes = strlen (str);
8780 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8781 while (nbytes--)
8782 store_mode_line_noprop_char (*str++);
8784 /* Fill up with spaces until FIELD_WIDTH reached. */
8785 while (field_width > 0
8786 && n < field_width)
8788 store_mode_line_noprop_char (' ');
8789 ++n;
8792 return n;
8795 /***********************************************************************
8796 Frame Titles
8797 ***********************************************************************/
8799 #ifdef HAVE_WINDOW_SYSTEM
8801 /* Set the title of FRAME, if it has changed. The title format is
8802 Vicon_title_format if FRAME is iconified, otherwise it is
8803 frame_title_format. */
8805 static void
8806 x_consider_frame_title (frame)
8807 Lisp_Object frame;
8809 struct frame *f = XFRAME (frame);
8811 if (FRAME_WINDOW_P (f)
8812 || FRAME_MINIBUF_ONLY_P (f)
8813 || f->explicit_name)
8815 /* Do we have more than one visible frame on this X display? */
8816 Lisp_Object tail;
8817 Lisp_Object fmt;
8818 int title_start;
8819 char *title;
8820 int len;
8821 struct it it;
8822 int count = SPECPDL_INDEX ();
8824 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8826 Lisp_Object other_frame = XCAR (tail);
8827 struct frame *tf = XFRAME (other_frame);
8829 if (tf != f
8830 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8831 && !FRAME_MINIBUF_ONLY_P (tf)
8832 && !EQ (other_frame, tip_frame)
8833 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8834 break;
8837 /* Set global variable indicating that multiple frames exist. */
8838 multiple_frames = CONSP (tail);
8840 /* Switch to the buffer of selected window of the frame. Set up
8841 mode_line_target so that display_mode_element will output into
8842 mode_line_noprop_buf; then display the title. */
8843 record_unwind_protect (unwind_format_mode_line,
8844 format_mode_line_unwind_data (current_buffer, 0));
8846 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8847 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8849 mode_line_target = MODE_LINE_TITLE;
8850 title_start = MODE_LINE_NOPROP_LEN (0);
8851 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8852 NULL, DEFAULT_FACE_ID);
8853 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8854 len = MODE_LINE_NOPROP_LEN (title_start);
8855 title = mode_line_noprop_buf + title_start;
8856 unbind_to (count, Qnil);
8858 /* Set the title only if it's changed. This avoids consing in
8859 the common case where it hasn't. (If it turns out that we've
8860 already wasted too much time by walking through the list with
8861 display_mode_element, then we might need to optimize at a
8862 higher level than this.) */
8863 if (! STRINGP (f->name)
8864 || SBYTES (f->name) != len
8865 || bcmp (title, SDATA (f->name), len) != 0)
8866 x_implicitly_set_name (f, make_string (title, len), Qnil);
8870 #endif /* not HAVE_WINDOW_SYSTEM */
8875 /***********************************************************************
8876 Menu Bars
8877 ***********************************************************************/
8880 /* Prepare for redisplay by updating menu-bar item lists when
8881 appropriate. This can call eval. */
8883 void
8884 prepare_menu_bars ()
8886 int all_windows;
8887 struct gcpro gcpro1, gcpro2;
8888 struct frame *f;
8889 Lisp_Object tooltip_frame;
8891 #ifdef HAVE_WINDOW_SYSTEM
8892 tooltip_frame = tip_frame;
8893 #else
8894 tooltip_frame = Qnil;
8895 #endif
8897 /* Update all frame titles based on their buffer names, etc. We do
8898 this before the menu bars so that the buffer-menu will show the
8899 up-to-date frame titles. */
8900 #ifdef HAVE_WINDOW_SYSTEM
8901 if (windows_or_buffers_changed || update_mode_lines)
8903 Lisp_Object tail, frame;
8905 FOR_EACH_FRAME (tail, frame)
8907 f = XFRAME (frame);
8908 if (!EQ (frame, tooltip_frame)
8909 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8910 x_consider_frame_title (frame);
8913 #endif /* HAVE_WINDOW_SYSTEM */
8915 /* Update the menu bar item lists, if appropriate. This has to be
8916 done before any actual redisplay or generation of display lines. */
8917 all_windows = (update_mode_lines
8918 || buffer_shared > 1
8919 || windows_or_buffers_changed);
8920 if (all_windows)
8922 Lisp_Object tail, frame;
8923 int count = SPECPDL_INDEX ();
8925 record_unwind_save_match_data ();
8927 FOR_EACH_FRAME (tail, frame)
8929 f = XFRAME (frame);
8931 /* Ignore tooltip frame. */
8932 if (EQ (frame, tooltip_frame))
8933 continue;
8935 /* If a window on this frame changed size, report that to
8936 the user and clear the size-change flag. */
8937 if (FRAME_WINDOW_SIZES_CHANGED (f))
8939 Lisp_Object functions;
8941 /* Clear flag first in case we get an error below. */
8942 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8943 functions = Vwindow_size_change_functions;
8944 GCPRO2 (tail, functions);
8946 while (CONSP (functions))
8948 call1 (XCAR (functions), frame);
8949 functions = XCDR (functions);
8951 UNGCPRO;
8954 GCPRO1 (tail);
8955 update_menu_bar (f, 0);
8956 #ifdef HAVE_WINDOW_SYSTEM
8957 update_tool_bar (f, 0);
8958 #endif
8959 UNGCPRO;
8962 unbind_to (count, Qnil);
8964 else
8966 struct frame *sf = SELECTED_FRAME ();
8967 update_menu_bar (sf, 1);
8968 #ifdef HAVE_WINDOW_SYSTEM
8969 update_tool_bar (sf, 1);
8970 #endif
8973 /* Motif needs this. See comment in xmenu.c. Turn it off when
8974 pending_menu_activation is not defined. */
8975 #ifdef USE_X_TOOLKIT
8976 pending_menu_activation = 0;
8977 #endif
8981 /* Update the menu bar item list for frame F. This has to be done
8982 before we start to fill in any display lines, because it can call
8983 eval.
8985 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8987 static void
8988 update_menu_bar (f, save_match_data)
8989 struct frame *f;
8990 int save_match_data;
8992 Lisp_Object window;
8993 register struct window *w;
8995 /* If called recursively during a menu update, do nothing. This can
8996 happen when, for instance, an activate-menubar-hook causes a
8997 redisplay. */
8998 if (inhibit_menubar_update)
8999 return;
9001 window = FRAME_SELECTED_WINDOW (f);
9002 w = XWINDOW (window);
9004 #if 0 /* The if statement below this if statement used to include the
9005 condition !NILP (w->update_mode_line), rather than using
9006 update_mode_lines directly, and this if statement may have
9007 been added to make that condition work. Now the if
9008 statement below matches its comment, this isn't needed. */
9009 if (update_mode_lines)
9010 w->update_mode_line = Qt;
9011 #endif
9013 if (FRAME_WINDOW_P (f)
9015 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9016 || defined (USE_GTK)
9017 FRAME_EXTERNAL_MENU_BAR (f)
9018 #else
9019 FRAME_MENU_BAR_LINES (f) > 0
9020 #endif
9021 : FRAME_MENU_BAR_LINES (f) > 0)
9023 /* If the user has switched buffers or windows, we need to
9024 recompute to reflect the new bindings. But we'll
9025 recompute when update_mode_lines is set too; that means
9026 that people can use force-mode-line-update to request
9027 that the menu bar be recomputed. The adverse effect on
9028 the rest of the redisplay algorithm is about the same as
9029 windows_or_buffers_changed anyway. */
9030 if (windows_or_buffers_changed
9031 /* This used to test w->update_mode_line, but we believe
9032 there is no need to recompute the menu in that case. */
9033 || update_mode_lines
9034 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9035 < BUF_MODIFF (XBUFFER (w->buffer)))
9036 != !NILP (w->last_had_star))
9037 || ((!NILP (Vtransient_mark_mode)
9038 && !NILP (XBUFFER (w->buffer)->mark_active))
9039 != !NILP (w->region_showing)))
9041 struct buffer *prev = current_buffer;
9042 int count = SPECPDL_INDEX ();
9044 specbind (Qinhibit_menubar_update, Qt);
9046 set_buffer_internal_1 (XBUFFER (w->buffer));
9047 if (save_match_data)
9048 record_unwind_save_match_data ();
9049 if (NILP (Voverriding_local_map_menu_flag))
9051 specbind (Qoverriding_terminal_local_map, Qnil);
9052 specbind (Qoverriding_local_map, Qnil);
9055 /* Run the Lucid hook. */
9056 safe_run_hooks (Qactivate_menubar_hook);
9058 /* If it has changed current-menubar from previous value,
9059 really recompute the menu-bar from the value. */
9060 if (! NILP (Vlucid_menu_bar_dirty_flag))
9061 call0 (Qrecompute_lucid_menubar);
9063 safe_run_hooks (Qmenu_bar_update_hook);
9064 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9066 /* Redisplay the menu bar in case we changed it. */
9067 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9068 || defined (USE_GTK)
9069 if (FRAME_WINDOW_P (f))
9071 #ifdef MAC_OS
9072 /* All frames on Mac OS share the same menubar. So only
9073 the selected frame should be allowed to set it. */
9074 if (f == SELECTED_FRAME ())
9075 #endif
9076 set_frame_menubar (f, 0, 0);
9078 else
9079 /* On a terminal screen, the menu bar is an ordinary screen
9080 line, and this makes it get updated. */
9081 w->update_mode_line = Qt;
9082 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9083 /* In the non-toolkit version, the menu bar is an ordinary screen
9084 line, and this makes it get updated. */
9085 w->update_mode_line = Qt;
9086 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9088 unbind_to (count, Qnil);
9089 set_buffer_internal_1 (prev);
9096 /***********************************************************************
9097 Output Cursor
9098 ***********************************************************************/
9100 #ifdef HAVE_WINDOW_SYSTEM
9102 /* EXPORT:
9103 Nominal cursor position -- where to draw output.
9104 HPOS and VPOS are window relative glyph matrix coordinates.
9105 X and Y are window relative pixel coordinates. */
9107 struct cursor_pos output_cursor;
9110 /* EXPORT:
9111 Set the global variable output_cursor to CURSOR. All cursor
9112 positions are relative to updated_window. */
9114 void
9115 set_output_cursor (cursor)
9116 struct cursor_pos *cursor;
9118 output_cursor.hpos = cursor->hpos;
9119 output_cursor.vpos = cursor->vpos;
9120 output_cursor.x = cursor->x;
9121 output_cursor.y = cursor->y;
9125 /* EXPORT for RIF:
9126 Set a nominal cursor position.
9128 HPOS and VPOS are column/row positions in a window glyph matrix. X
9129 and Y are window text area relative pixel positions.
9131 If this is done during an update, updated_window will contain the
9132 window that is being updated and the position is the future output
9133 cursor position for that window. If updated_window is null, use
9134 selected_window and display the cursor at the given position. */
9136 void
9137 x_cursor_to (vpos, hpos, y, x)
9138 int vpos, hpos, y, x;
9140 struct window *w;
9142 /* If updated_window is not set, work on selected_window. */
9143 if (updated_window)
9144 w = updated_window;
9145 else
9146 w = XWINDOW (selected_window);
9148 /* Set the output cursor. */
9149 output_cursor.hpos = hpos;
9150 output_cursor.vpos = vpos;
9151 output_cursor.x = x;
9152 output_cursor.y = y;
9154 /* If not called as part of an update, really display the cursor.
9155 This will also set the cursor position of W. */
9156 if (updated_window == NULL)
9158 BLOCK_INPUT;
9159 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9160 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9161 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9162 UNBLOCK_INPUT;
9166 #endif /* HAVE_WINDOW_SYSTEM */
9169 /***********************************************************************
9170 Tool-bars
9171 ***********************************************************************/
9173 #ifdef HAVE_WINDOW_SYSTEM
9175 /* Where the mouse was last time we reported a mouse event. */
9177 FRAME_PTR last_mouse_frame;
9179 /* Tool-bar item index of the item on which a mouse button was pressed
9180 or -1. */
9182 int last_tool_bar_item;
9185 /* Update the tool-bar item list for frame F. This has to be done
9186 before we start to fill in any display lines. Called from
9187 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9188 and restore it here. */
9190 static void
9191 update_tool_bar (f, save_match_data)
9192 struct frame *f;
9193 int save_match_data;
9195 #ifdef USE_GTK
9196 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9197 #else
9198 int do_update = WINDOWP (f->tool_bar_window)
9199 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9200 #endif
9202 if (do_update)
9204 Lisp_Object window;
9205 struct window *w;
9207 window = FRAME_SELECTED_WINDOW (f);
9208 w = XWINDOW (window);
9210 /* If the user has switched buffers or windows, we need to
9211 recompute to reflect the new bindings. But we'll
9212 recompute when update_mode_lines is set too; that means
9213 that people can use force-mode-line-update to request
9214 that the menu bar be recomputed. The adverse effect on
9215 the rest of the redisplay algorithm is about the same as
9216 windows_or_buffers_changed anyway. */
9217 if (windows_or_buffers_changed
9218 || !NILP (w->update_mode_line)
9219 || update_mode_lines
9220 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9221 < BUF_MODIFF (XBUFFER (w->buffer)))
9222 != !NILP (w->last_had_star))
9223 || ((!NILP (Vtransient_mark_mode)
9224 && !NILP (XBUFFER (w->buffer)->mark_active))
9225 != !NILP (w->region_showing)))
9227 struct buffer *prev = current_buffer;
9228 int count = SPECPDL_INDEX ();
9229 Lisp_Object new_tool_bar;
9230 int new_n_tool_bar;
9231 struct gcpro gcpro1;
9233 /* Set current_buffer to the buffer of the selected
9234 window of the frame, so that we get the right local
9235 keymaps. */
9236 set_buffer_internal_1 (XBUFFER (w->buffer));
9238 /* Save match data, if we must. */
9239 if (save_match_data)
9240 record_unwind_save_match_data ();
9242 /* Make sure that we don't accidentally use bogus keymaps. */
9243 if (NILP (Voverriding_local_map_menu_flag))
9245 specbind (Qoverriding_terminal_local_map, Qnil);
9246 specbind (Qoverriding_local_map, Qnil);
9249 GCPRO1 (new_tool_bar);
9251 /* Build desired tool-bar items from keymaps. */
9252 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9253 &new_n_tool_bar);
9255 /* Redisplay the tool-bar if we changed it. */
9256 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9258 /* Redisplay that happens asynchronously due to an expose event
9259 may access f->tool_bar_items. Make sure we update both
9260 variables within BLOCK_INPUT so no such event interrupts. */
9261 BLOCK_INPUT;
9262 f->tool_bar_items = new_tool_bar;
9263 f->n_tool_bar_items = new_n_tool_bar;
9264 w->update_mode_line = Qt;
9265 UNBLOCK_INPUT;
9268 UNGCPRO;
9270 unbind_to (count, Qnil);
9271 set_buffer_internal_1 (prev);
9277 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9278 F's desired tool-bar contents. F->tool_bar_items must have
9279 been set up previously by calling prepare_menu_bars. */
9281 static void
9282 build_desired_tool_bar_string (f)
9283 struct frame *f;
9285 int i, size, size_needed;
9286 struct gcpro gcpro1, gcpro2, gcpro3;
9287 Lisp_Object image, plist, props;
9289 image = plist = props = Qnil;
9290 GCPRO3 (image, plist, props);
9292 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9293 Otherwise, make a new string. */
9295 /* The size of the string we might be able to reuse. */
9296 size = (STRINGP (f->desired_tool_bar_string)
9297 ? SCHARS (f->desired_tool_bar_string)
9298 : 0);
9300 /* We need one space in the string for each image. */
9301 size_needed = f->n_tool_bar_items;
9303 /* Reuse f->desired_tool_bar_string, if possible. */
9304 if (size < size_needed || NILP (f->desired_tool_bar_string))
9305 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9306 make_number (' '));
9307 else
9309 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9310 Fremove_text_properties (make_number (0), make_number (size),
9311 props, f->desired_tool_bar_string);
9314 /* Put a `display' property on the string for the images to display,
9315 put a `menu_item' property on tool-bar items with a value that
9316 is the index of the item in F's tool-bar item vector. */
9317 for (i = 0; i < f->n_tool_bar_items; ++i)
9319 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9321 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9322 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9323 int hmargin, vmargin, relief, idx, end;
9324 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9326 /* If image is a vector, choose the image according to the
9327 button state. */
9328 image = PROP (TOOL_BAR_ITEM_IMAGES);
9329 if (VECTORP (image))
9331 if (enabled_p)
9332 idx = (selected_p
9333 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9334 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9335 else
9336 idx = (selected_p
9337 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9338 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9340 xassert (ASIZE (image) >= idx);
9341 image = AREF (image, idx);
9343 else
9344 idx = -1;
9346 /* Ignore invalid image specifications. */
9347 if (!valid_image_p (image))
9348 continue;
9350 /* Display the tool-bar button pressed, or depressed. */
9351 plist = Fcopy_sequence (XCDR (image));
9353 /* Compute margin and relief to draw. */
9354 relief = (tool_bar_button_relief >= 0
9355 ? tool_bar_button_relief
9356 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9357 hmargin = vmargin = relief;
9359 if (INTEGERP (Vtool_bar_button_margin)
9360 && XINT (Vtool_bar_button_margin) > 0)
9362 hmargin += XFASTINT (Vtool_bar_button_margin);
9363 vmargin += XFASTINT (Vtool_bar_button_margin);
9365 else if (CONSP (Vtool_bar_button_margin))
9367 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9368 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9369 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9371 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9372 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9373 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9376 if (auto_raise_tool_bar_buttons_p)
9378 /* Add a `:relief' property to the image spec if the item is
9379 selected. */
9380 if (selected_p)
9382 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9383 hmargin -= relief;
9384 vmargin -= relief;
9387 else
9389 /* If image is selected, display it pressed, i.e. with a
9390 negative relief. If it's not selected, display it with a
9391 raised relief. */
9392 plist = Fplist_put (plist, QCrelief,
9393 (selected_p
9394 ? make_number (-relief)
9395 : make_number (relief)));
9396 hmargin -= relief;
9397 vmargin -= relief;
9400 /* Put a margin around the image. */
9401 if (hmargin || vmargin)
9403 if (hmargin == vmargin)
9404 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9405 else
9406 plist = Fplist_put (plist, QCmargin,
9407 Fcons (make_number (hmargin),
9408 make_number (vmargin)));
9411 /* If button is not enabled, and we don't have special images
9412 for the disabled state, make the image appear disabled by
9413 applying an appropriate algorithm to it. */
9414 if (!enabled_p && idx < 0)
9415 plist = Fplist_put (plist, QCconversion, Qdisabled);
9417 /* Put a `display' text property on the string for the image to
9418 display. Put a `menu-item' property on the string that gives
9419 the start of this item's properties in the tool-bar items
9420 vector. */
9421 image = Fcons (Qimage, plist);
9422 props = list4 (Qdisplay, image,
9423 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9425 /* Let the last image hide all remaining spaces in the tool bar
9426 string. The string can be longer than needed when we reuse a
9427 previous string. */
9428 if (i + 1 == f->n_tool_bar_items)
9429 end = SCHARS (f->desired_tool_bar_string);
9430 else
9431 end = i + 1;
9432 Fadd_text_properties (make_number (i), make_number (end),
9433 props, f->desired_tool_bar_string);
9434 #undef PROP
9437 UNGCPRO;
9441 /* Display one line of the tool-bar of frame IT->f.
9443 HEIGHT specifies the desired height of the tool-bar line.
9444 If the actual height of the glyph row is less than HEIGHT, the
9445 row's height is increased to HEIGHT, and the icons are centered
9446 vertically in the new height.
9448 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9449 count a final empty row in case the tool-bar width exactly matches
9450 the window width.
9453 static void
9454 display_tool_bar_line (it, height)
9455 struct it *it;
9456 int height;
9458 struct glyph_row *row = it->glyph_row;
9459 int max_x = it->last_visible_x;
9460 struct glyph *last;
9462 prepare_desired_row (row);
9463 row->y = it->current_y;
9465 /* Note that this isn't made use of if the face hasn't a box,
9466 so there's no need to check the face here. */
9467 it->start_of_box_run_p = 1;
9469 while (it->current_x < max_x)
9471 int x_before, x, n_glyphs_before, i, nglyphs;
9473 /* Get the next display element. */
9474 if (!get_next_display_element (it))
9476 /* Don't count empty row if we are counting needed tool-bar lines. */
9477 if (height < 0 && !it->hpos)
9478 return;
9479 break;
9482 /* Produce glyphs. */
9483 x_before = it->current_x;
9484 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9485 PRODUCE_GLYPHS (it);
9487 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9488 i = 0;
9489 x = x_before;
9490 while (i < nglyphs)
9492 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9494 if (x + glyph->pixel_width > max_x)
9496 /* Glyph doesn't fit on line. */
9497 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9498 it->current_x = x;
9499 goto out;
9502 ++it->hpos;
9503 x += glyph->pixel_width;
9504 ++i;
9507 /* Stop at line ends. */
9508 if (ITERATOR_AT_END_OF_LINE_P (it))
9509 break;
9511 set_iterator_to_next (it, 1);
9514 out:;
9516 row->displays_text_p = row->used[TEXT_AREA] != 0;
9517 /* Use default face for the border below the tool bar. */
9518 if (!row->displays_text_p)
9519 it->face_id = DEFAULT_FACE_ID;
9520 extend_face_to_end_of_line (it);
9521 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9522 last->right_box_line_p = 1;
9523 if (last == row->glyphs[TEXT_AREA])
9524 last->left_box_line_p = 1;
9526 /* Make line the desired height and center it vertically. */
9527 if ((height -= it->max_ascent + it->max_descent) > 0)
9529 it->max_ascent += height / 2;
9530 it->max_descent += (height + 1) / 2;
9533 compute_line_metrics (it);
9535 /* If line is empty, make it occupy the rest of the tool-bar. */
9536 if (!row->displays_text_p)
9538 row->height = row->phys_height = it->last_visible_y - row->y;
9539 row->ascent = row->phys_ascent = 0;
9540 row->extra_line_spacing = 0;
9543 row->full_width_p = 1;
9544 row->continued_p = 0;
9545 row->truncated_on_left_p = 0;
9546 row->truncated_on_right_p = 0;
9548 it->current_x = it->hpos = 0;
9549 it->current_y += row->height;
9550 ++it->vpos;
9551 ++it->glyph_row;
9555 /* Value is the number of screen lines needed to make all tool-bar
9556 items of frame F visible. The number of actual rows needed is
9557 returned in *N_ROWS if non-NULL. */
9559 static int
9560 tool_bar_lines_needed (f, n_rows)
9561 struct frame *f;
9562 int *n_rows;
9564 struct window *w = XWINDOW (f->tool_bar_window);
9565 struct it it;
9567 /* Initialize an iterator for iteration over
9568 F->desired_tool_bar_string in the tool-bar window of frame F. */
9569 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9570 it.first_visible_x = 0;
9571 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9572 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9574 while (!ITERATOR_AT_END_P (&it))
9576 it.glyph_row = w->desired_matrix->rows;
9577 clear_glyph_row (it.glyph_row);
9578 display_tool_bar_line (&it, -1);
9581 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9582 if (n_rows)
9583 *n_rows = it.vpos > 0 ? it.vpos : -1;
9585 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9589 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9590 0, 1, 0,
9591 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9592 (frame)
9593 Lisp_Object frame;
9595 struct frame *f;
9596 struct window *w;
9597 int nlines = 0;
9599 if (NILP (frame))
9600 frame = selected_frame;
9601 else
9602 CHECK_FRAME (frame);
9603 f = XFRAME (frame);
9605 if (WINDOWP (f->tool_bar_window)
9606 || (w = XWINDOW (f->tool_bar_window),
9607 WINDOW_TOTAL_LINES (w) > 0))
9609 update_tool_bar (f, 1);
9610 if (f->n_tool_bar_items)
9612 build_desired_tool_bar_string (f);
9613 nlines = tool_bar_lines_needed (f, NULL);
9617 return make_number (nlines);
9621 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9622 height should be changed. */
9624 static int
9625 redisplay_tool_bar (f)
9626 struct frame *f;
9628 struct window *w;
9629 struct it it;
9630 struct glyph_row *row;
9631 int change_height_p = 0;
9633 #ifdef USE_GTK
9634 if (FRAME_EXTERNAL_TOOL_BAR (f))
9635 update_frame_tool_bar (f);
9636 return 0;
9637 #endif
9639 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9640 do anything. This means you must start with tool-bar-lines
9641 non-zero to get the auto-sizing effect. Or in other words, you
9642 can turn off tool-bars by specifying tool-bar-lines zero. */
9643 if (!WINDOWP (f->tool_bar_window)
9644 || (w = XWINDOW (f->tool_bar_window),
9645 WINDOW_TOTAL_LINES (w) == 0))
9646 return 0;
9648 /* Set up an iterator for the tool-bar window. */
9649 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9650 it.first_visible_x = 0;
9651 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9652 row = it.glyph_row;
9654 /* Build a string that represents the contents of the tool-bar. */
9655 build_desired_tool_bar_string (f);
9656 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9658 if (f->n_tool_bar_rows == 0)
9659 (void)tool_bar_lines_needed (f, &f->n_tool_bar_rows);
9661 /* Display as many lines as needed to display all tool-bar items. */
9663 if (f->n_tool_bar_rows > 0)
9665 int border, rows, height, extra;
9667 if (INTEGERP (Vtool_bar_border))
9668 border = XINT (Vtool_bar_border);
9669 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9670 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9671 else if (EQ (Vtool_bar_border, Qborder_width))
9672 border = f->border_width;
9673 else
9674 border = 0;
9675 if (border < 0)
9676 border = 0;
9678 rows = f->n_tool_bar_rows;
9679 height = (it.last_visible_y - border) / rows;
9680 extra = it.last_visible_y - border - height * rows;
9682 while (it.current_y < it.last_visible_y)
9684 int h = 0;
9685 if (extra > 0 && rows-- > 0)
9687 h = (extra + rows - 1) / rows;
9688 extra -= h;
9690 display_tool_bar_line (&it, height + h);
9693 else
9695 while (it.current_y < it.last_visible_y)
9696 display_tool_bar_line (&it, 0);
9699 /* It doesn't make much sense to try scrolling in the tool-bar
9700 window, so don't do it. */
9701 w->desired_matrix->no_scrolling_p = 1;
9702 w->must_be_updated_p = 1;
9704 if (auto_resize_tool_bars_p)
9706 int nlines;
9708 /* If we couldn't display everything, change the tool-bar's
9709 height. */
9710 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9711 change_height_p = 1;
9713 /* If there are blank lines at the end, except for a partially
9714 visible blank line at the end that is smaller than
9715 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9716 row = it.glyph_row - 1;
9717 if (!row->displays_text_p
9718 && row->height >= FRAME_LINE_HEIGHT (f))
9719 change_height_p = 1;
9721 /* If row displays tool-bar items, but is partially visible,
9722 change the tool-bar's height. */
9723 if (row->displays_text_p
9724 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9725 change_height_p = 1;
9727 /* Resize windows as needed by changing the `tool-bar-lines'
9728 frame parameter. */
9729 if (change_height_p
9730 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9731 nlines != WINDOW_TOTAL_LINES (w)))
9733 extern Lisp_Object Qtool_bar_lines;
9734 Lisp_Object frame;
9735 int old_height = WINDOW_TOTAL_LINES (w);
9737 XSETFRAME (frame, f);
9738 clear_glyph_matrix (w->desired_matrix);
9739 Fmodify_frame_parameters (frame,
9740 Fcons (Fcons (Qtool_bar_lines,
9741 make_number (nlines)),
9742 Qnil));
9743 if (WINDOW_TOTAL_LINES (w) != old_height)
9744 fonts_changed_p = 1;
9748 return change_height_p;
9752 /* Get information about the tool-bar item which is displayed in GLYPH
9753 on frame F. Return in *PROP_IDX the index where tool-bar item
9754 properties start in F->tool_bar_items. Value is zero if
9755 GLYPH doesn't display a tool-bar item. */
9757 static int
9758 tool_bar_item_info (f, glyph, prop_idx)
9759 struct frame *f;
9760 struct glyph *glyph;
9761 int *prop_idx;
9763 Lisp_Object prop;
9764 int success_p;
9765 int charpos;
9767 /* This function can be called asynchronously, which means we must
9768 exclude any possibility that Fget_text_property signals an
9769 error. */
9770 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9771 charpos = max (0, charpos);
9773 /* Get the text property `menu-item' at pos. The value of that
9774 property is the start index of this item's properties in
9775 F->tool_bar_items. */
9776 prop = Fget_text_property (make_number (charpos),
9777 Qmenu_item, f->current_tool_bar_string);
9778 if (INTEGERP (prop))
9780 *prop_idx = XINT (prop);
9781 success_p = 1;
9783 else
9784 success_p = 0;
9786 return success_p;
9790 /* Get information about the tool-bar item at position X/Y on frame F.
9791 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9792 the current matrix of the tool-bar window of F, or NULL if not
9793 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9794 item in F->tool_bar_items. Value is
9796 -1 if X/Y is not on a tool-bar item
9797 0 if X/Y is on the same item that was highlighted before.
9798 1 otherwise. */
9800 static int
9801 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9802 struct frame *f;
9803 int x, y;
9804 struct glyph **glyph;
9805 int *hpos, *vpos, *prop_idx;
9807 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9808 struct window *w = XWINDOW (f->tool_bar_window);
9809 int area;
9811 /* Find the glyph under X/Y. */
9812 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9813 if (*glyph == NULL)
9814 return -1;
9816 /* Get the start of this tool-bar item's properties in
9817 f->tool_bar_items. */
9818 if (!tool_bar_item_info (f, *glyph, prop_idx))
9819 return -1;
9821 /* Is mouse on the highlighted item? */
9822 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9823 && *vpos >= dpyinfo->mouse_face_beg_row
9824 && *vpos <= dpyinfo->mouse_face_end_row
9825 && (*vpos > dpyinfo->mouse_face_beg_row
9826 || *hpos >= dpyinfo->mouse_face_beg_col)
9827 && (*vpos < dpyinfo->mouse_face_end_row
9828 || *hpos < dpyinfo->mouse_face_end_col
9829 || dpyinfo->mouse_face_past_end))
9830 return 0;
9832 return 1;
9836 /* EXPORT:
9837 Handle mouse button event on the tool-bar of frame F, at
9838 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9839 0 for button release. MODIFIERS is event modifiers for button
9840 release. */
9842 void
9843 handle_tool_bar_click (f, x, y, down_p, modifiers)
9844 struct frame *f;
9845 int x, y, down_p;
9846 unsigned int modifiers;
9848 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9849 struct window *w = XWINDOW (f->tool_bar_window);
9850 int hpos, vpos, prop_idx;
9851 struct glyph *glyph;
9852 Lisp_Object enabled_p;
9854 /* If not on the highlighted tool-bar item, return. */
9855 frame_to_window_pixel_xy (w, &x, &y);
9856 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9857 return;
9859 /* If item is disabled, do nothing. */
9860 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9861 if (NILP (enabled_p))
9862 return;
9864 if (down_p)
9866 /* Show item in pressed state. */
9867 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9868 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9869 last_tool_bar_item = prop_idx;
9871 else
9873 Lisp_Object key, frame;
9874 struct input_event event;
9875 EVENT_INIT (event);
9877 /* Show item in released state. */
9878 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9879 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9881 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9883 XSETFRAME (frame, f);
9884 event.kind = TOOL_BAR_EVENT;
9885 event.frame_or_window = frame;
9886 event.arg = frame;
9887 kbd_buffer_store_event (&event);
9889 event.kind = TOOL_BAR_EVENT;
9890 event.frame_or_window = frame;
9891 event.arg = key;
9892 event.modifiers = modifiers;
9893 kbd_buffer_store_event (&event);
9894 last_tool_bar_item = -1;
9899 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9900 tool-bar window-relative coordinates X/Y. Called from
9901 note_mouse_highlight. */
9903 static void
9904 note_tool_bar_highlight (f, x, y)
9905 struct frame *f;
9906 int x, y;
9908 Lisp_Object window = f->tool_bar_window;
9909 struct window *w = XWINDOW (window);
9910 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9911 int hpos, vpos;
9912 struct glyph *glyph;
9913 struct glyph_row *row;
9914 int i;
9915 Lisp_Object enabled_p;
9916 int prop_idx;
9917 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9918 int mouse_down_p, rc;
9920 /* Function note_mouse_highlight is called with negative x(y
9921 values when mouse moves outside of the frame. */
9922 if (x <= 0 || y <= 0)
9924 clear_mouse_face (dpyinfo);
9925 return;
9928 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9929 if (rc < 0)
9931 /* Not on tool-bar item. */
9932 clear_mouse_face (dpyinfo);
9933 return;
9935 else if (rc == 0)
9936 /* On same tool-bar item as before. */
9937 goto set_help_echo;
9939 clear_mouse_face (dpyinfo);
9941 /* Mouse is down, but on different tool-bar item? */
9942 mouse_down_p = (dpyinfo->grabbed
9943 && f == last_mouse_frame
9944 && FRAME_LIVE_P (f));
9945 if (mouse_down_p
9946 && last_tool_bar_item != prop_idx)
9947 return;
9949 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9950 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9952 /* If tool-bar item is not enabled, don't highlight it. */
9953 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9954 if (!NILP (enabled_p))
9956 /* Compute the x-position of the glyph. In front and past the
9957 image is a space. We include this in the highlighted area. */
9958 row = MATRIX_ROW (w->current_matrix, vpos);
9959 for (i = x = 0; i < hpos; ++i)
9960 x += row->glyphs[TEXT_AREA][i].pixel_width;
9962 /* Record this as the current active region. */
9963 dpyinfo->mouse_face_beg_col = hpos;
9964 dpyinfo->mouse_face_beg_row = vpos;
9965 dpyinfo->mouse_face_beg_x = x;
9966 dpyinfo->mouse_face_beg_y = row->y;
9967 dpyinfo->mouse_face_past_end = 0;
9969 dpyinfo->mouse_face_end_col = hpos + 1;
9970 dpyinfo->mouse_face_end_row = vpos;
9971 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9972 dpyinfo->mouse_face_end_y = row->y;
9973 dpyinfo->mouse_face_window = window;
9974 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9976 /* Display it as active. */
9977 show_mouse_face (dpyinfo, draw);
9978 dpyinfo->mouse_face_image_state = draw;
9981 set_help_echo:
9983 /* Set help_echo_string to a help string to display for this tool-bar item.
9984 XTread_socket does the rest. */
9985 help_echo_object = help_echo_window = Qnil;
9986 help_echo_pos = -1;
9987 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9988 if (NILP (help_echo_string))
9989 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9992 #endif /* HAVE_WINDOW_SYSTEM */
9996 /************************************************************************
9997 Horizontal scrolling
9998 ************************************************************************/
10000 static int hscroll_window_tree P_ ((Lisp_Object));
10001 static int hscroll_windows P_ ((Lisp_Object));
10003 /* For all leaf windows in the window tree rooted at WINDOW, set their
10004 hscroll value so that PT is (i) visible in the window, and (ii) so
10005 that it is not within a certain margin at the window's left and
10006 right border. Value is non-zero if any window's hscroll has been
10007 changed. */
10009 static int
10010 hscroll_window_tree (window)
10011 Lisp_Object window;
10013 int hscrolled_p = 0;
10014 int hscroll_relative_p = FLOATP (Vhscroll_step);
10015 int hscroll_step_abs = 0;
10016 double hscroll_step_rel = 0;
10018 if (hscroll_relative_p)
10020 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10021 if (hscroll_step_rel < 0)
10023 hscroll_relative_p = 0;
10024 hscroll_step_abs = 0;
10027 else if (INTEGERP (Vhscroll_step))
10029 hscroll_step_abs = XINT (Vhscroll_step);
10030 if (hscroll_step_abs < 0)
10031 hscroll_step_abs = 0;
10033 else
10034 hscroll_step_abs = 0;
10036 while (WINDOWP (window))
10038 struct window *w = XWINDOW (window);
10040 if (WINDOWP (w->hchild))
10041 hscrolled_p |= hscroll_window_tree (w->hchild);
10042 else if (WINDOWP (w->vchild))
10043 hscrolled_p |= hscroll_window_tree (w->vchild);
10044 else if (w->cursor.vpos >= 0)
10046 int h_margin;
10047 int text_area_width;
10048 struct glyph_row *current_cursor_row
10049 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10050 struct glyph_row *desired_cursor_row
10051 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10052 struct glyph_row *cursor_row
10053 = (desired_cursor_row->enabled_p
10054 ? desired_cursor_row
10055 : current_cursor_row);
10057 text_area_width = window_box_width (w, TEXT_AREA);
10059 /* Scroll when cursor is inside this scroll margin. */
10060 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10062 if ((XFASTINT (w->hscroll)
10063 && w->cursor.x <= h_margin)
10064 || (cursor_row->enabled_p
10065 && cursor_row->truncated_on_right_p
10066 && (w->cursor.x >= text_area_width - h_margin)))
10068 struct it it;
10069 int hscroll;
10070 struct buffer *saved_current_buffer;
10071 int pt;
10072 int wanted_x;
10074 /* Find point in a display of infinite width. */
10075 saved_current_buffer = current_buffer;
10076 current_buffer = XBUFFER (w->buffer);
10078 if (w == XWINDOW (selected_window))
10079 pt = BUF_PT (current_buffer);
10080 else
10082 pt = marker_position (w->pointm);
10083 pt = max (BEGV, pt);
10084 pt = min (ZV, pt);
10087 /* Move iterator to pt starting at cursor_row->start in
10088 a line with infinite width. */
10089 init_to_row_start (&it, w, cursor_row);
10090 it.last_visible_x = INFINITY;
10091 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10092 current_buffer = saved_current_buffer;
10094 /* Position cursor in window. */
10095 if (!hscroll_relative_p && hscroll_step_abs == 0)
10096 hscroll = max (0, (it.current_x
10097 - (ITERATOR_AT_END_OF_LINE_P (&it)
10098 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10099 : (text_area_width / 2))))
10100 / FRAME_COLUMN_WIDTH (it.f);
10101 else if (w->cursor.x >= text_area_width - h_margin)
10103 if (hscroll_relative_p)
10104 wanted_x = text_area_width * (1 - hscroll_step_rel)
10105 - h_margin;
10106 else
10107 wanted_x = text_area_width
10108 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10109 - h_margin;
10110 hscroll
10111 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10113 else
10115 if (hscroll_relative_p)
10116 wanted_x = text_area_width * hscroll_step_rel
10117 + h_margin;
10118 else
10119 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10120 + h_margin;
10121 hscroll
10122 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10124 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10126 /* Don't call Fset_window_hscroll if value hasn't
10127 changed because it will prevent redisplay
10128 optimizations. */
10129 if (XFASTINT (w->hscroll) != hscroll)
10131 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10132 w->hscroll = make_number (hscroll);
10133 hscrolled_p = 1;
10138 window = w->next;
10141 /* Value is non-zero if hscroll of any leaf window has been changed. */
10142 return hscrolled_p;
10146 /* Set hscroll so that cursor is visible and not inside horizontal
10147 scroll margins for all windows in the tree rooted at WINDOW. See
10148 also hscroll_window_tree above. Value is non-zero if any window's
10149 hscroll has been changed. If it has, desired matrices on the frame
10150 of WINDOW are cleared. */
10152 static int
10153 hscroll_windows (window)
10154 Lisp_Object window;
10156 int hscrolled_p;
10158 if (automatic_hscrolling_p)
10160 hscrolled_p = hscroll_window_tree (window);
10161 if (hscrolled_p)
10162 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10164 else
10165 hscrolled_p = 0;
10166 return hscrolled_p;
10171 /************************************************************************
10172 Redisplay
10173 ************************************************************************/
10175 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10176 to a non-zero value. This is sometimes handy to have in a debugger
10177 session. */
10179 #if GLYPH_DEBUG
10181 /* First and last unchanged row for try_window_id. */
10183 int debug_first_unchanged_at_end_vpos;
10184 int debug_last_unchanged_at_beg_vpos;
10186 /* Delta vpos and y. */
10188 int debug_dvpos, debug_dy;
10190 /* Delta in characters and bytes for try_window_id. */
10192 int debug_delta, debug_delta_bytes;
10194 /* Values of window_end_pos and window_end_vpos at the end of
10195 try_window_id. */
10197 EMACS_INT debug_end_pos, debug_end_vpos;
10199 /* Append a string to W->desired_matrix->method. FMT is a printf
10200 format string. A1...A9 are a supplement for a variable-length
10201 argument list. If trace_redisplay_p is non-zero also printf the
10202 resulting string to stderr. */
10204 static void
10205 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10206 struct window *w;
10207 char *fmt;
10208 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10210 char buffer[512];
10211 char *method = w->desired_matrix->method;
10212 int len = strlen (method);
10213 int size = sizeof w->desired_matrix->method;
10214 int remaining = size - len - 1;
10216 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10217 if (len && remaining)
10219 method[len] = '|';
10220 --remaining, ++len;
10223 strncpy (method + len, buffer, remaining);
10225 if (trace_redisplay_p)
10226 fprintf (stderr, "%p (%s): %s\n",
10228 ((BUFFERP (w->buffer)
10229 && STRINGP (XBUFFER (w->buffer)->name))
10230 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10231 : "no buffer"),
10232 buffer);
10235 #endif /* GLYPH_DEBUG */
10238 /* Value is non-zero if all changes in window W, which displays
10239 current_buffer, are in the text between START and END. START is a
10240 buffer position, END is given as a distance from Z. Used in
10241 redisplay_internal for display optimization. */
10243 static INLINE int
10244 text_outside_line_unchanged_p (w, start, end)
10245 struct window *w;
10246 int start, end;
10248 int unchanged_p = 1;
10250 /* If text or overlays have changed, see where. */
10251 if (XFASTINT (w->last_modified) < MODIFF
10252 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10254 /* Gap in the line? */
10255 if (GPT < start || Z - GPT < end)
10256 unchanged_p = 0;
10258 /* Changes start in front of the line, or end after it? */
10259 if (unchanged_p
10260 && (BEG_UNCHANGED < start - 1
10261 || END_UNCHANGED < end))
10262 unchanged_p = 0;
10264 /* If selective display, can't optimize if changes start at the
10265 beginning of the line. */
10266 if (unchanged_p
10267 && INTEGERP (current_buffer->selective_display)
10268 && XINT (current_buffer->selective_display) > 0
10269 && (BEG_UNCHANGED < start || GPT <= start))
10270 unchanged_p = 0;
10272 /* If there are overlays at the start or end of the line, these
10273 may have overlay strings with newlines in them. A change at
10274 START, for instance, may actually concern the display of such
10275 overlay strings as well, and they are displayed on different
10276 lines. So, quickly rule out this case. (For the future, it
10277 might be desirable to implement something more telling than
10278 just BEG/END_UNCHANGED.) */
10279 if (unchanged_p)
10281 if (BEG + BEG_UNCHANGED == start
10282 && overlay_touches_p (start))
10283 unchanged_p = 0;
10284 if (END_UNCHANGED == end
10285 && overlay_touches_p (Z - end))
10286 unchanged_p = 0;
10290 return unchanged_p;
10294 /* Do a frame update, taking possible shortcuts into account. This is
10295 the main external entry point for redisplay.
10297 If the last redisplay displayed an echo area message and that message
10298 is no longer requested, we clear the echo area or bring back the
10299 mini-buffer if that is in use. */
10301 void
10302 redisplay ()
10304 redisplay_internal (0);
10308 static Lisp_Object
10309 overlay_arrow_string_or_property (var)
10310 Lisp_Object var;
10312 Lisp_Object val;
10314 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10315 return val;
10317 return Voverlay_arrow_string;
10320 /* Return 1 if there are any overlay-arrows in current_buffer. */
10321 static int
10322 overlay_arrow_in_current_buffer_p ()
10324 Lisp_Object vlist;
10326 for (vlist = Voverlay_arrow_variable_list;
10327 CONSP (vlist);
10328 vlist = XCDR (vlist))
10330 Lisp_Object var = XCAR (vlist);
10331 Lisp_Object val;
10333 if (!SYMBOLP (var))
10334 continue;
10335 val = find_symbol_value (var);
10336 if (MARKERP (val)
10337 && current_buffer == XMARKER (val)->buffer)
10338 return 1;
10340 return 0;
10344 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10345 has changed. */
10347 static int
10348 overlay_arrows_changed_p ()
10350 Lisp_Object vlist;
10352 for (vlist = Voverlay_arrow_variable_list;
10353 CONSP (vlist);
10354 vlist = XCDR (vlist))
10356 Lisp_Object var = XCAR (vlist);
10357 Lisp_Object val, pstr;
10359 if (!SYMBOLP (var))
10360 continue;
10361 val = find_symbol_value (var);
10362 if (!MARKERP (val))
10363 continue;
10364 if (! EQ (COERCE_MARKER (val),
10365 Fget (var, Qlast_arrow_position))
10366 || ! (pstr = overlay_arrow_string_or_property (var),
10367 EQ (pstr, Fget (var, Qlast_arrow_string))))
10368 return 1;
10370 return 0;
10373 /* Mark overlay arrows to be updated on next redisplay. */
10375 static void
10376 update_overlay_arrows (up_to_date)
10377 int up_to_date;
10379 Lisp_Object vlist;
10381 for (vlist = Voverlay_arrow_variable_list;
10382 CONSP (vlist);
10383 vlist = XCDR (vlist))
10385 Lisp_Object var = XCAR (vlist);
10387 if (!SYMBOLP (var))
10388 continue;
10390 if (up_to_date > 0)
10392 Lisp_Object val = find_symbol_value (var);
10393 Fput (var, Qlast_arrow_position,
10394 COERCE_MARKER (val));
10395 Fput (var, Qlast_arrow_string,
10396 overlay_arrow_string_or_property (var));
10398 else if (up_to_date < 0
10399 || !NILP (Fget (var, Qlast_arrow_position)))
10401 Fput (var, Qlast_arrow_position, Qt);
10402 Fput (var, Qlast_arrow_string, Qt);
10408 /* Return overlay arrow string to display at row.
10409 Return integer (bitmap number) for arrow bitmap in left fringe.
10410 Return nil if no overlay arrow. */
10412 static Lisp_Object
10413 overlay_arrow_at_row (it, row)
10414 struct it *it;
10415 struct glyph_row *row;
10417 Lisp_Object vlist;
10419 for (vlist = Voverlay_arrow_variable_list;
10420 CONSP (vlist);
10421 vlist = XCDR (vlist))
10423 Lisp_Object var = XCAR (vlist);
10424 Lisp_Object val;
10426 if (!SYMBOLP (var))
10427 continue;
10429 val = find_symbol_value (var);
10431 if (MARKERP (val)
10432 && current_buffer == XMARKER (val)->buffer
10433 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10435 if (FRAME_WINDOW_P (it->f)
10436 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10438 #ifdef HAVE_WINDOW_SYSTEM
10439 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10441 int fringe_bitmap;
10442 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10443 return make_number (fringe_bitmap);
10445 #endif
10446 return make_number (-1); /* Use default arrow bitmap */
10448 return overlay_arrow_string_or_property (var);
10452 return Qnil;
10455 /* Return 1 if point moved out of or into a composition. Otherwise
10456 return 0. PREV_BUF and PREV_PT are the last point buffer and
10457 position. BUF and PT are the current point buffer and position. */
10460 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10461 struct buffer *prev_buf, *buf;
10462 int prev_pt, pt;
10464 int start, end;
10465 Lisp_Object prop;
10466 Lisp_Object buffer;
10468 XSETBUFFER (buffer, buf);
10469 /* Check a composition at the last point if point moved within the
10470 same buffer. */
10471 if (prev_buf == buf)
10473 if (prev_pt == pt)
10474 /* Point didn't move. */
10475 return 0;
10477 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10478 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10479 && COMPOSITION_VALID_P (start, end, prop)
10480 && start < prev_pt && end > prev_pt)
10481 /* The last point was within the composition. Return 1 iff
10482 point moved out of the composition. */
10483 return (pt <= start || pt >= end);
10486 /* Check a composition at the current point. */
10487 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10488 && find_composition (pt, -1, &start, &end, &prop, buffer)
10489 && COMPOSITION_VALID_P (start, end, prop)
10490 && start < pt && end > pt);
10494 /* Reconsider the setting of B->clip_changed which is displayed
10495 in window W. */
10497 static INLINE void
10498 reconsider_clip_changes (w, b)
10499 struct window *w;
10500 struct buffer *b;
10502 if (b->clip_changed
10503 && !NILP (w->window_end_valid)
10504 && w->current_matrix->buffer == b
10505 && w->current_matrix->zv == BUF_ZV (b)
10506 && w->current_matrix->begv == BUF_BEGV (b))
10507 b->clip_changed = 0;
10509 /* If display wasn't paused, and W is not a tool bar window, see if
10510 point has been moved into or out of a composition. In that case,
10511 we set b->clip_changed to 1 to force updating the screen. If
10512 b->clip_changed has already been set to 1, we can skip this
10513 check. */
10514 if (!b->clip_changed
10515 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10517 int pt;
10519 if (w == XWINDOW (selected_window))
10520 pt = BUF_PT (current_buffer);
10521 else
10522 pt = marker_position (w->pointm);
10524 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10525 || pt != XINT (w->last_point))
10526 && check_point_in_composition (w->current_matrix->buffer,
10527 XINT (w->last_point),
10528 XBUFFER (w->buffer), pt))
10529 b->clip_changed = 1;
10534 /* Select FRAME to forward the values of frame-local variables into C
10535 variables so that the redisplay routines can access those values
10536 directly. */
10538 static void
10539 select_frame_for_redisplay (frame)
10540 Lisp_Object frame;
10542 Lisp_Object tail, sym, val;
10543 Lisp_Object old = selected_frame;
10545 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10547 selected_frame = frame;
10549 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10550 if (CONSP (XCAR (tail))
10551 && (sym = XCAR (XCAR (tail)),
10552 SYMBOLP (sym))
10553 && (sym = indirect_variable (sym),
10554 val = SYMBOL_VALUE (sym),
10555 (BUFFER_LOCAL_VALUEP (val)
10556 || SOME_BUFFER_LOCAL_VALUEP (val)))
10557 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10558 /* Use find_symbol_value rather than Fsymbol_value
10559 to avoid an error if it is void. */
10560 find_symbol_value (sym);
10562 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10563 if (CONSP (XCAR (tail))
10564 && (sym = XCAR (XCAR (tail)),
10565 SYMBOLP (sym))
10566 && (sym = indirect_variable (sym),
10567 val = SYMBOL_VALUE (sym),
10568 (BUFFER_LOCAL_VALUEP (val)
10569 || SOME_BUFFER_LOCAL_VALUEP (val)))
10570 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10571 find_symbol_value (sym);
10575 #define STOP_POLLING \
10576 do { if (! polling_stopped_here) stop_polling (); \
10577 polling_stopped_here = 1; } while (0)
10579 #define RESUME_POLLING \
10580 do { if (polling_stopped_here) start_polling (); \
10581 polling_stopped_here = 0; } while (0)
10584 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10585 response to any user action; therefore, we should preserve the echo
10586 area. (Actually, our caller does that job.) Perhaps in the future
10587 avoid recentering windows if it is not necessary; currently that
10588 causes some problems. */
10590 static void
10591 redisplay_internal (preserve_echo_area)
10592 int preserve_echo_area;
10594 struct window *w = XWINDOW (selected_window);
10595 struct frame *f = XFRAME (w->frame);
10596 int pause;
10597 int must_finish = 0;
10598 struct text_pos tlbufpos, tlendpos;
10599 int number_of_visible_frames;
10600 int count;
10601 struct frame *sf = SELECTED_FRAME ();
10602 int polling_stopped_here = 0;
10604 /* Non-zero means redisplay has to consider all windows on all
10605 frames. Zero means, only selected_window is considered. */
10606 int consider_all_windows_p;
10608 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10610 /* No redisplay if running in batch mode or frame is not yet fully
10611 initialized, or redisplay is explicitly turned off by setting
10612 Vinhibit_redisplay. */
10613 if (noninteractive
10614 || !NILP (Vinhibit_redisplay)
10615 || !f->glyphs_initialized_p)
10616 return;
10618 /* The flag redisplay_performed_directly_p is set by
10619 direct_output_for_insert when it already did the whole screen
10620 update necessary. */
10621 if (redisplay_performed_directly_p)
10623 redisplay_performed_directly_p = 0;
10624 if (!hscroll_windows (selected_window))
10625 return;
10628 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10629 if (popup_activated ())
10630 return;
10631 #endif
10633 /* I don't think this happens but let's be paranoid. */
10634 if (redisplaying_p)
10635 return;
10637 /* Record a function that resets redisplaying_p to its old value
10638 when we leave this function. */
10639 count = SPECPDL_INDEX ();
10640 record_unwind_protect (unwind_redisplay,
10641 Fcons (make_number (redisplaying_p), selected_frame));
10642 ++redisplaying_p;
10643 specbind (Qinhibit_free_realized_faces, Qnil);
10646 Lisp_Object tail, frame;
10648 FOR_EACH_FRAME (tail, frame)
10650 struct frame *f = XFRAME (frame);
10651 f->already_hscrolled_p = 0;
10655 retry:
10656 pause = 0;
10657 reconsider_clip_changes (w, current_buffer);
10658 last_escape_glyph_frame = NULL;
10659 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10661 /* If new fonts have been loaded that make a glyph matrix adjustment
10662 necessary, do it. */
10663 if (fonts_changed_p)
10665 adjust_glyphs (NULL);
10666 ++windows_or_buffers_changed;
10667 fonts_changed_p = 0;
10670 /* If face_change_count is non-zero, init_iterator will free all
10671 realized faces, which includes the faces referenced from current
10672 matrices. So, we can't reuse current matrices in this case. */
10673 if (face_change_count)
10674 ++windows_or_buffers_changed;
10676 if (FRAME_TERMCAP_P (sf)
10677 && FRAME_TTY (sf)->previous_frame != sf)
10679 /* Since frames on a single ASCII terminal share the same
10680 display area, displaying a different frame means redisplay
10681 the whole thing. */
10682 windows_or_buffers_changed++;
10683 SET_FRAME_GARBAGED (sf);
10684 FRAME_TTY (sf)->previous_frame = sf;
10687 /* Set the visible flags for all frames. Do this before checking
10688 for resized or garbaged frames; they want to know if their frames
10689 are visible. See the comment in frame.h for
10690 FRAME_SAMPLE_VISIBILITY. */
10692 Lisp_Object tail, frame;
10694 number_of_visible_frames = 0;
10696 FOR_EACH_FRAME (tail, frame)
10698 struct frame *f = XFRAME (frame);
10700 FRAME_SAMPLE_VISIBILITY (f);
10701 if (FRAME_VISIBLE_P (f))
10702 ++number_of_visible_frames;
10703 clear_desired_matrices (f);
10708 /* Notice any pending interrupt request to change frame size. */
10709 do_pending_window_change (1);
10711 /* Clear frames marked as garbaged. */
10712 if (frame_garbaged)
10713 clear_garbaged_frames ();
10715 /* Build menubar and tool-bar items. */
10716 if (NILP (Vmemory_full))
10717 prepare_menu_bars ();
10719 if (windows_or_buffers_changed)
10720 update_mode_lines++;
10722 /* Detect case that we need to write or remove a star in the mode line. */
10723 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10725 w->update_mode_line = Qt;
10726 if (buffer_shared > 1)
10727 update_mode_lines++;
10730 /* If %c is in the mode line, update it if needed. */
10731 if (!NILP (w->column_number_displayed)
10732 /* This alternative quickly identifies a common case
10733 where no change is needed. */
10734 && !(PT == XFASTINT (w->last_point)
10735 && XFASTINT (w->last_modified) >= MODIFF
10736 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10737 && (XFASTINT (w->column_number_displayed)
10738 != (int) current_column ())) /* iftc */
10739 w->update_mode_line = Qt;
10741 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10743 /* The variable buffer_shared is set in redisplay_window and
10744 indicates that we redisplay a buffer in different windows. See
10745 there. */
10746 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10747 || cursor_type_changed);
10749 /* If specs for an arrow have changed, do thorough redisplay
10750 to ensure we remove any arrow that should no longer exist. */
10751 if (overlay_arrows_changed_p ())
10752 consider_all_windows_p = windows_or_buffers_changed = 1;
10754 /* Normally the message* functions will have already displayed and
10755 updated the echo area, but the frame may have been trashed, or
10756 the update may have been preempted, so display the echo area
10757 again here. Checking message_cleared_p captures the case that
10758 the echo area should be cleared. */
10759 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10760 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10761 || (message_cleared_p
10762 && minibuf_level == 0
10763 /* If the mini-window is currently selected, this means the
10764 echo-area doesn't show through. */
10765 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10767 int window_height_changed_p = echo_area_display (0);
10768 must_finish = 1;
10770 /* If we don't display the current message, don't clear the
10771 message_cleared_p flag, because, if we did, we wouldn't clear
10772 the echo area in the next redisplay which doesn't preserve
10773 the echo area. */
10774 if (!display_last_displayed_message_p)
10775 message_cleared_p = 0;
10777 if (fonts_changed_p)
10778 goto retry;
10779 else if (window_height_changed_p)
10781 consider_all_windows_p = 1;
10782 ++update_mode_lines;
10783 ++windows_or_buffers_changed;
10785 /* If window configuration was changed, frames may have been
10786 marked garbaged. Clear them or we will experience
10787 surprises wrt scrolling. */
10788 if (frame_garbaged)
10789 clear_garbaged_frames ();
10792 else if (EQ (selected_window, minibuf_window)
10793 && (current_buffer->clip_changed
10794 || XFASTINT (w->last_modified) < MODIFF
10795 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10796 && resize_mini_window (w, 0))
10798 /* Resized active mini-window to fit the size of what it is
10799 showing if its contents might have changed. */
10800 must_finish = 1;
10801 consider_all_windows_p = 1;
10802 ++windows_or_buffers_changed;
10803 ++update_mode_lines;
10805 /* If window configuration was changed, frames may have been
10806 marked garbaged. Clear them or we will experience
10807 surprises wrt scrolling. */
10808 if (frame_garbaged)
10809 clear_garbaged_frames ();
10813 /* If showing the region, and mark has changed, we must redisplay
10814 the whole window. The assignment to this_line_start_pos prevents
10815 the optimization directly below this if-statement. */
10816 if (((!NILP (Vtransient_mark_mode)
10817 && !NILP (XBUFFER (w->buffer)->mark_active))
10818 != !NILP (w->region_showing))
10819 || (!NILP (w->region_showing)
10820 && !EQ (w->region_showing,
10821 Fmarker_position (XBUFFER (w->buffer)->mark))))
10822 CHARPOS (this_line_start_pos) = 0;
10824 /* Optimize the case that only the line containing the cursor in the
10825 selected window has changed. Variables starting with this_ are
10826 set in display_line and record information about the line
10827 containing the cursor. */
10828 tlbufpos = this_line_start_pos;
10829 tlendpos = this_line_end_pos;
10830 if (!consider_all_windows_p
10831 && CHARPOS (tlbufpos) > 0
10832 && NILP (w->update_mode_line)
10833 && !current_buffer->clip_changed
10834 && !current_buffer->prevent_redisplay_optimizations_p
10835 && FRAME_VISIBLE_P (XFRAME (w->frame))
10836 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10837 /* Make sure recorded data applies to current buffer, etc. */
10838 && this_line_buffer == current_buffer
10839 && current_buffer == XBUFFER (w->buffer)
10840 && NILP (w->force_start)
10841 && NILP (w->optional_new_start)
10842 /* Point must be on the line that we have info recorded about. */
10843 && PT >= CHARPOS (tlbufpos)
10844 && PT <= Z - CHARPOS (tlendpos)
10845 /* All text outside that line, including its final newline,
10846 must be unchanged */
10847 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10848 CHARPOS (tlendpos)))
10850 if (CHARPOS (tlbufpos) > BEGV
10851 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10852 && (CHARPOS (tlbufpos) == ZV
10853 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10854 /* Former continuation line has disappeared by becoming empty */
10855 goto cancel;
10856 else if (XFASTINT (w->last_modified) < MODIFF
10857 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10858 || MINI_WINDOW_P (w))
10860 /* We have to handle the case of continuation around a
10861 wide-column character (See the comment in indent.c around
10862 line 885).
10864 For instance, in the following case:
10866 -------- Insert --------
10867 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10868 J_I_ ==> J_I_ `^^' are cursors.
10869 ^^ ^^
10870 -------- --------
10872 As we have to redraw the line above, we should goto cancel. */
10874 struct it it;
10875 int line_height_before = this_line_pixel_height;
10877 /* Note that start_display will handle the case that the
10878 line starting at tlbufpos is a continuation lines. */
10879 start_display (&it, w, tlbufpos);
10881 /* Implementation note: It this still necessary? */
10882 if (it.current_x != this_line_start_x)
10883 goto cancel;
10885 TRACE ((stderr, "trying display optimization 1\n"));
10886 w->cursor.vpos = -1;
10887 overlay_arrow_seen = 0;
10888 it.vpos = this_line_vpos;
10889 it.current_y = this_line_y;
10890 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10891 display_line (&it);
10893 /* If line contains point, is not continued,
10894 and ends at same distance from eob as before, we win */
10895 if (w->cursor.vpos >= 0
10896 /* Line is not continued, otherwise this_line_start_pos
10897 would have been set to 0 in display_line. */
10898 && CHARPOS (this_line_start_pos)
10899 /* Line ends as before. */
10900 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10901 /* Line has same height as before. Otherwise other lines
10902 would have to be shifted up or down. */
10903 && this_line_pixel_height == line_height_before)
10905 /* If this is not the window's last line, we must adjust
10906 the charstarts of the lines below. */
10907 if (it.current_y < it.last_visible_y)
10909 struct glyph_row *row
10910 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10911 int delta, delta_bytes;
10913 if (Z - CHARPOS (tlendpos) == ZV)
10915 /* This line ends at end of (accessible part of)
10916 buffer. There is no newline to count. */
10917 delta = (Z
10918 - CHARPOS (tlendpos)
10919 - MATRIX_ROW_START_CHARPOS (row));
10920 delta_bytes = (Z_BYTE
10921 - BYTEPOS (tlendpos)
10922 - MATRIX_ROW_START_BYTEPOS (row));
10924 else
10926 /* This line ends in a newline. Must take
10927 account of the newline and the rest of the
10928 text that follows. */
10929 delta = (Z
10930 - CHARPOS (tlendpos)
10931 - MATRIX_ROW_START_CHARPOS (row));
10932 delta_bytes = (Z_BYTE
10933 - BYTEPOS (tlendpos)
10934 - MATRIX_ROW_START_BYTEPOS (row));
10937 increment_matrix_positions (w->current_matrix,
10938 this_line_vpos + 1,
10939 w->current_matrix->nrows,
10940 delta, delta_bytes);
10943 /* If this row displays text now but previously didn't,
10944 or vice versa, w->window_end_vpos may have to be
10945 adjusted. */
10946 if ((it.glyph_row - 1)->displays_text_p)
10948 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10949 XSETINT (w->window_end_vpos, this_line_vpos);
10951 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10952 && this_line_vpos > 0)
10953 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10954 w->window_end_valid = Qnil;
10956 /* Update hint: No need to try to scroll in update_window. */
10957 w->desired_matrix->no_scrolling_p = 1;
10959 #if GLYPH_DEBUG
10960 *w->desired_matrix->method = 0;
10961 debug_method_add (w, "optimization 1");
10962 #endif
10963 #ifdef HAVE_WINDOW_SYSTEM
10964 update_window_fringes (w, 0);
10965 #endif
10966 goto update;
10968 else
10969 goto cancel;
10971 else if (/* Cursor position hasn't changed. */
10972 PT == XFASTINT (w->last_point)
10973 /* Make sure the cursor was last displayed
10974 in this window. Otherwise we have to reposition it. */
10975 && 0 <= w->cursor.vpos
10976 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10978 if (!must_finish)
10980 do_pending_window_change (1);
10982 /* We used to always goto end_of_redisplay here, but this
10983 isn't enough if we have a blinking cursor. */
10984 if (w->cursor_off_p == w->last_cursor_off_p)
10985 goto end_of_redisplay;
10987 goto update;
10989 /* If highlighting the region, or if the cursor is in the echo area,
10990 then we can't just move the cursor. */
10991 else if (! (!NILP (Vtransient_mark_mode)
10992 && !NILP (current_buffer->mark_active))
10993 && (EQ (selected_window, current_buffer->last_selected_window)
10994 || highlight_nonselected_windows)
10995 && NILP (w->region_showing)
10996 && NILP (Vshow_trailing_whitespace)
10997 && !cursor_in_echo_area)
10999 struct it it;
11000 struct glyph_row *row;
11002 /* Skip from tlbufpos to PT and see where it is. Note that
11003 PT may be in invisible text. If so, we will end at the
11004 next visible position. */
11005 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11006 NULL, DEFAULT_FACE_ID);
11007 it.current_x = this_line_start_x;
11008 it.current_y = this_line_y;
11009 it.vpos = this_line_vpos;
11011 /* The call to move_it_to stops in front of PT, but
11012 moves over before-strings. */
11013 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11015 if (it.vpos == this_line_vpos
11016 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11017 row->enabled_p))
11019 xassert (this_line_vpos == it.vpos);
11020 xassert (this_line_y == it.current_y);
11021 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11022 #if GLYPH_DEBUG
11023 *w->desired_matrix->method = 0;
11024 debug_method_add (w, "optimization 3");
11025 #endif
11026 goto update;
11028 else
11029 goto cancel;
11032 cancel:
11033 /* Text changed drastically or point moved off of line. */
11034 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11037 CHARPOS (this_line_start_pos) = 0;
11038 consider_all_windows_p |= buffer_shared > 1;
11039 ++clear_face_cache_count;
11040 #ifdef HAVE_WINDOW_SYSTEM
11041 ++clear_image_cache_count;
11042 #endif
11044 /* Build desired matrices, and update the display. If
11045 consider_all_windows_p is non-zero, do it for all windows on all
11046 frames. Otherwise do it for selected_window, only. */
11048 if (consider_all_windows_p)
11050 Lisp_Object tail, frame;
11052 FOR_EACH_FRAME (tail, frame)
11053 XFRAME (frame)->updated_p = 0;
11055 /* Recompute # windows showing selected buffer. This will be
11056 incremented each time such a window is displayed. */
11057 buffer_shared = 0;
11059 FOR_EACH_FRAME (tail, frame)
11061 struct frame *f = XFRAME (frame);
11063 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11065 if (! EQ (frame, selected_frame))
11066 /* Select the frame, for the sake of frame-local
11067 variables. */
11068 select_frame_for_redisplay (frame);
11070 /* Mark all the scroll bars to be removed; we'll redeem
11071 the ones we want when we redisplay their windows. */
11072 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11073 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11075 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11076 redisplay_windows (FRAME_ROOT_WINDOW (f));
11078 /* Any scroll bars which redisplay_windows should have
11079 nuked should now go away. */
11080 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11081 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11083 /* If fonts changed, display again. */
11084 /* ??? rms: I suspect it is a mistake to jump all the way
11085 back to retry here. It should just retry this frame. */
11086 if (fonts_changed_p)
11087 goto retry;
11089 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11091 /* See if we have to hscroll. */
11092 if (!f->already_hscrolled_p)
11094 f->already_hscrolled_p = 1;
11095 if (hscroll_windows (f->root_window))
11096 goto retry;
11099 /* Prevent various kinds of signals during display
11100 update. stdio is not robust about handling
11101 signals, which can cause an apparent I/O
11102 error. */
11103 if (interrupt_input)
11104 unrequest_sigio ();
11105 STOP_POLLING;
11107 /* Update the display. */
11108 set_window_update_flags (XWINDOW (f->root_window), 1);
11109 pause |= update_frame (f, 0, 0);
11110 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11111 if (pause)
11112 break;
11113 #endif
11115 f->updated_p = 1;
11120 if (!pause)
11122 /* Do the mark_window_display_accurate after all windows have
11123 been redisplayed because this call resets flags in buffers
11124 which are needed for proper redisplay. */
11125 FOR_EACH_FRAME (tail, frame)
11127 struct frame *f = XFRAME (frame);
11128 if (f->updated_p)
11130 mark_window_display_accurate (f->root_window, 1);
11131 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11132 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11137 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11139 Lisp_Object mini_window;
11140 struct frame *mini_frame;
11142 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11143 /* Use list_of_error, not Qerror, so that
11144 we catch only errors and don't run the debugger. */
11145 internal_condition_case_1 (redisplay_window_1, selected_window,
11146 list_of_error,
11147 redisplay_window_error);
11149 /* Compare desired and current matrices, perform output. */
11151 update:
11152 /* If fonts changed, display again. */
11153 if (fonts_changed_p)
11154 goto retry;
11156 /* Prevent various kinds of signals during display update.
11157 stdio is not robust about handling signals,
11158 which can cause an apparent I/O error. */
11159 if (interrupt_input)
11160 unrequest_sigio ();
11161 STOP_POLLING;
11163 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11165 if (hscroll_windows (selected_window))
11166 goto retry;
11168 XWINDOW (selected_window)->must_be_updated_p = 1;
11169 pause = update_frame (sf, 0, 0);
11172 /* We may have called echo_area_display at the top of this
11173 function. If the echo area is on another frame, that may
11174 have put text on a frame other than the selected one, so the
11175 above call to update_frame would not have caught it. Catch
11176 it here. */
11177 mini_window = FRAME_MINIBUF_WINDOW (sf);
11178 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11180 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11182 XWINDOW (mini_window)->must_be_updated_p = 1;
11183 pause |= update_frame (mini_frame, 0, 0);
11184 if (!pause && hscroll_windows (mini_window))
11185 goto retry;
11189 /* If display was paused because of pending input, make sure we do a
11190 thorough update the next time. */
11191 if (pause)
11193 /* Prevent the optimization at the beginning of
11194 redisplay_internal that tries a single-line update of the
11195 line containing the cursor in the selected window. */
11196 CHARPOS (this_line_start_pos) = 0;
11198 /* Let the overlay arrow be updated the next time. */
11199 update_overlay_arrows (0);
11201 /* If we pause after scrolling, some rows in the current
11202 matrices of some windows are not valid. */
11203 if (!WINDOW_FULL_WIDTH_P (w)
11204 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11205 update_mode_lines = 1;
11207 else
11209 if (!consider_all_windows_p)
11211 /* This has already been done above if
11212 consider_all_windows_p is set. */
11213 mark_window_display_accurate_1 (w, 1);
11215 /* Say overlay arrows are up to date. */
11216 update_overlay_arrows (1);
11218 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11219 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11222 update_mode_lines = 0;
11223 windows_or_buffers_changed = 0;
11224 cursor_type_changed = 0;
11227 /* Start SIGIO interrupts coming again. Having them off during the
11228 code above makes it less likely one will discard output, but not
11229 impossible, since there might be stuff in the system buffer here.
11230 But it is much hairier to try to do anything about that. */
11231 if (interrupt_input)
11232 request_sigio ();
11233 RESUME_POLLING;
11235 /* If a frame has become visible which was not before, redisplay
11236 again, so that we display it. Expose events for such a frame
11237 (which it gets when becoming visible) don't call the parts of
11238 redisplay constructing glyphs, so simply exposing a frame won't
11239 display anything in this case. So, we have to display these
11240 frames here explicitly. */
11241 if (!pause)
11243 Lisp_Object tail, frame;
11244 int new_count = 0;
11246 FOR_EACH_FRAME (tail, frame)
11248 int this_is_visible = 0;
11250 if (XFRAME (frame)->visible)
11251 this_is_visible = 1;
11252 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11253 if (XFRAME (frame)->visible)
11254 this_is_visible = 1;
11256 if (this_is_visible)
11257 new_count++;
11260 if (new_count != number_of_visible_frames)
11261 windows_or_buffers_changed++;
11264 /* Change frame size now if a change is pending. */
11265 do_pending_window_change (1);
11267 /* If we just did a pending size change, or have additional
11268 visible frames, redisplay again. */
11269 if (windows_or_buffers_changed && !pause)
11270 goto retry;
11272 /* Clear the face cache eventually. */
11273 if (consider_all_windows_p)
11275 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11277 clear_face_cache (0);
11278 clear_face_cache_count = 0;
11280 #ifdef HAVE_WINDOW_SYSTEM
11281 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11283 Lisp_Object tail, frame;
11284 FOR_EACH_FRAME (tail, frame)
11286 struct frame *f = XFRAME (frame);
11287 if (FRAME_WINDOW_P (f))
11288 clear_image_cache (f, 0);
11290 clear_image_cache_count = 0;
11292 #endif /* HAVE_WINDOW_SYSTEM */
11295 end_of_redisplay:
11296 unbind_to (count, Qnil);
11297 RESUME_POLLING;
11301 /* Redisplay, but leave alone any recent echo area message unless
11302 another message has been requested in its place.
11304 This is useful in situations where you need to redisplay but no
11305 user action has occurred, making it inappropriate for the message
11306 area to be cleared. See tracking_off and
11307 wait_reading_process_output for examples of these situations.
11309 FROM_WHERE is an integer saying from where this function was
11310 called. This is useful for debugging. */
11312 void
11313 redisplay_preserve_echo_area (from_where)
11314 int from_where;
11316 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11318 if (!NILP (echo_area_buffer[1]))
11320 /* We have a previously displayed message, but no current
11321 message. Redisplay the previous message. */
11322 display_last_displayed_message_p = 1;
11323 redisplay_internal (1);
11324 display_last_displayed_message_p = 0;
11326 else
11327 redisplay_internal (1);
11329 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11330 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11331 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11335 /* Function registered with record_unwind_protect in
11336 redisplay_internal. Reset redisplaying_p to the value it had
11337 before redisplay_internal was called, and clear
11338 prevent_freeing_realized_faces_p. It also selects the previously
11339 selected frame, unless it has been deleted (by an X connection
11340 failure during redisplay, for example). */
11342 static Lisp_Object
11343 unwind_redisplay (val)
11344 Lisp_Object val;
11346 Lisp_Object old_redisplaying_p, old_frame;
11348 old_redisplaying_p = XCAR (val);
11349 redisplaying_p = XFASTINT (old_redisplaying_p);
11350 old_frame = XCDR (val);
11351 if (! EQ (old_frame, selected_frame)
11352 && FRAME_LIVE_P (XFRAME (old_frame)))
11353 select_frame_for_redisplay (old_frame);
11354 return Qnil;
11358 /* Mark the display of window W as accurate or inaccurate. If
11359 ACCURATE_P is non-zero mark display of W as accurate. If
11360 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11361 redisplay_internal is called. */
11363 static void
11364 mark_window_display_accurate_1 (w, accurate_p)
11365 struct window *w;
11366 int accurate_p;
11368 if (BUFFERP (w->buffer))
11370 struct buffer *b = XBUFFER (w->buffer);
11372 w->last_modified
11373 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11374 w->last_overlay_modified
11375 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11376 w->last_had_star
11377 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11379 if (accurate_p)
11381 b->clip_changed = 0;
11382 b->prevent_redisplay_optimizations_p = 0;
11384 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11385 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11386 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11387 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11389 w->current_matrix->buffer = b;
11390 w->current_matrix->begv = BUF_BEGV (b);
11391 w->current_matrix->zv = BUF_ZV (b);
11393 w->last_cursor = w->cursor;
11394 w->last_cursor_off_p = w->cursor_off_p;
11396 if (w == XWINDOW (selected_window))
11397 w->last_point = make_number (BUF_PT (b));
11398 else
11399 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11403 if (accurate_p)
11405 w->window_end_valid = w->buffer;
11406 #if 0 /* This is incorrect with variable-height lines. */
11407 xassert (XINT (w->window_end_vpos)
11408 < (WINDOW_TOTAL_LINES (w)
11409 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11410 #endif
11411 w->update_mode_line = Qnil;
11416 /* Mark the display of windows in the window tree rooted at WINDOW as
11417 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11418 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11419 be redisplayed the next time redisplay_internal is called. */
11421 void
11422 mark_window_display_accurate (window, accurate_p)
11423 Lisp_Object window;
11424 int accurate_p;
11426 struct window *w;
11428 for (; !NILP (window); window = w->next)
11430 w = XWINDOW (window);
11431 mark_window_display_accurate_1 (w, accurate_p);
11433 if (!NILP (w->vchild))
11434 mark_window_display_accurate (w->vchild, accurate_p);
11435 if (!NILP (w->hchild))
11436 mark_window_display_accurate (w->hchild, accurate_p);
11439 if (accurate_p)
11441 update_overlay_arrows (1);
11443 else
11445 /* Force a thorough redisplay the next time by setting
11446 last_arrow_position and last_arrow_string to t, which is
11447 unequal to any useful value of Voverlay_arrow_... */
11448 update_overlay_arrows (-1);
11453 /* Return value in display table DP (Lisp_Char_Table *) for character
11454 C. Since a display table doesn't have any parent, we don't have to
11455 follow parent. Do not call this function directly but use the
11456 macro DISP_CHAR_VECTOR. */
11458 Lisp_Object
11459 disp_char_vector (dp, c)
11460 struct Lisp_Char_Table *dp;
11461 int c;
11463 int code[4], i;
11464 Lisp_Object val;
11466 if (SINGLE_BYTE_CHAR_P (c))
11467 return (dp->contents[c]);
11469 SPLIT_CHAR (c, code[0], code[1], code[2]);
11470 if (code[1] < 32)
11471 code[1] = -1;
11472 else if (code[2] < 32)
11473 code[2] = -1;
11475 /* Here, the possible range of code[0] (== charset ID) is
11476 128..max_charset. Since the top level char table contains data
11477 for multibyte characters after 256th element, we must increment
11478 code[0] by 128 to get a correct index. */
11479 code[0] += 128;
11480 code[3] = -1; /* anchor */
11482 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11484 val = dp->contents[code[i]];
11485 if (!SUB_CHAR_TABLE_P (val))
11486 return (NILP (val) ? dp->defalt : val);
11489 /* Here, val is a sub char table. We return the default value of
11490 it. */
11491 return (dp->defalt);
11496 /***********************************************************************
11497 Window Redisplay
11498 ***********************************************************************/
11500 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11502 static void
11503 redisplay_windows (window)
11504 Lisp_Object window;
11506 while (!NILP (window))
11508 struct window *w = XWINDOW (window);
11510 if (!NILP (w->hchild))
11511 redisplay_windows (w->hchild);
11512 else if (!NILP (w->vchild))
11513 redisplay_windows (w->vchild);
11514 else
11516 displayed_buffer = XBUFFER (w->buffer);
11517 /* Use list_of_error, not Qerror, so that
11518 we catch only errors and don't run the debugger. */
11519 internal_condition_case_1 (redisplay_window_0, window,
11520 list_of_error,
11521 redisplay_window_error);
11524 window = w->next;
11528 static Lisp_Object
11529 redisplay_window_error ()
11531 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11532 return Qnil;
11535 static Lisp_Object
11536 redisplay_window_0 (window)
11537 Lisp_Object window;
11539 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11540 redisplay_window (window, 0);
11541 return Qnil;
11544 static Lisp_Object
11545 redisplay_window_1 (window)
11546 Lisp_Object window;
11548 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11549 redisplay_window (window, 1);
11550 return Qnil;
11554 /* Increment GLYPH until it reaches END or CONDITION fails while
11555 adding (GLYPH)->pixel_width to X. */
11557 #define SKIP_GLYPHS(glyph, end, x, condition) \
11558 do \
11560 (x) += (glyph)->pixel_width; \
11561 ++(glyph); \
11563 while ((glyph) < (end) && (condition))
11566 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11567 DELTA is the number of bytes by which positions recorded in ROW
11568 differ from current buffer positions. */
11570 void
11571 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11572 struct window *w;
11573 struct glyph_row *row;
11574 struct glyph_matrix *matrix;
11575 int delta, delta_bytes, dy, dvpos;
11577 struct glyph *glyph = row->glyphs[TEXT_AREA];
11578 struct glyph *end = glyph + row->used[TEXT_AREA];
11579 struct glyph *cursor = NULL;
11580 /* The first glyph that starts a sequence of glyphs from string. */
11581 struct glyph *string_start;
11582 /* The X coordinate of string_start. */
11583 int string_start_x;
11584 /* The last known character position. */
11585 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11586 /* The last known character position before string_start. */
11587 int string_before_pos;
11588 int x = row->x;
11589 int cursor_x = x;
11590 int cursor_from_overlay_pos = 0;
11591 int pt_old = PT - delta;
11593 /* Skip over glyphs not having an object at the start of the row.
11594 These are special glyphs like truncation marks on terminal
11595 frames. */
11596 if (row->displays_text_p)
11597 while (glyph < end
11598 && INTEGERP (glyph->object)
11599 && glyph->charpos < 0)
11601 x += glyph->pixel_width;
11602 ++glyph;
11605 string_start = NULL;
11606 while (glyph < end
11607 && !INTEGERP (glyph->object)
11608 && (!BUFFERP (glyph->object)
11609 || (last_pos = glyph->charpos) < pt_old))
11611 if (! STRINGP (glyph->object))
11613 string_start = NULL;
11614 x += glyph->pixel_width;
11615 ++glyph;
11616 if (cursor_from_overlay_pos
11617 && last_pos >= cursor_from_overlay_pos)
11619 cursor_from_overlay_pos = 0;
11620 cursor = 0;
11623 else
11625 string_before_pos = last_pos;
11626 string_start = glyph;
11627 string_start_x = x;
11628 /* Skip all glyphs from string. */
11631 Lisp_Object cprop;
11632 int pos;
11633 if ((cursor == NULL || glyph > cursor)
11634 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11635 Qcursor, (glyph)->object),
11636 !NILP (cprop))
11637 && (pos = string_buffer_position (w, glyph->object,
11638 string_before_pos),
11639 (pos == 0 /* From overlay */
11640 || pos == pt_old)))
11642 /* Estimate overlay buffer position from the buffer
11643 positions of the glyphs before and after the overlay.
11644 Add 1 to last_pos so that if point corresponds to the
11645 glyph right after the overlay, we still use a 'cursor'
11646 property found in that overlay. */
11647 cursor_from_overlay_pos = (pos ? 0 : last_pos
11648 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11649 cursor = glyph;
11650 cursor_x = x;
11652 x += glyph->pixel_width;
11653 ++glyph;
11655 while (glyph < end && EQ (glyph->object, string_start->object));
11659 if (cursor != NULL)
11661 glyph = cursor;
11662 x = cursor_x;
11664 else if (row->ends_in_ellipsis_p && glyph == end)
11666 /* Scan back over the ellipsis glyphs, decrementing positions. */
11667 while (glyph > row->glyphs[TEXT_AREA]
11668 && (glyph - 1)->charpos == last_pos)
11669 glyph--, x -= glyph->pixel_width;
11670 /* That loop always goes one position too far,
11671 including the glyph before the ellipsis.
11672 So scan forward over that one. */
11673 x += glyph->pixel_width;
11674 glyph++;
11676 else if (string_start
11677 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11679 /* We may have skipped over point because the previous glyphs
11680 are from string. As there's no easy way to know the
11681 character position of the current glyph, find the correct
11682 glyph on point by scanning from string_start again. */
11683 Lisp_Object limit;
11684 Lisp_Object string;
11685 int pos;
11687 limit = make_number (pt_old + 1);
11688 end = glyph;
11689 glyph = string_start;
11690 x = string_start_x;
11691 string = glyph->object;
11692 pos = string_buffer_position (w, string, string_before_pos);
11693 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11694 because we always put cursor after overlay strings. */
11695 while (pos == 0 && glyph < end)
11697 string = glyph->object;
11698 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11699 if (glyph < end)
11700 pos = string_buffer_position (w, glyph->object, string_before_pos);
11703 while (glyph < end)
11705 pos = XINT (Fnext_single_char_property_change
11706 (make_number (pos), Qdisplay, Qnil, limit));
11707 if (pos > pt_old)
11708 break;
11709 /* Skip glyphs from the same string. */
11710 string = glyph->object;
11711 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11712 /* Skip glyphs from an overlay. */
11713 while (glyph < end
11714 && ! string_buffer_position (w, glyph->object, pos))
11716 string = glyph->object;
11717 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11722 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11723 w->cursor.x = x;
11724 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11725 w->cursor.y = row->y + dy;
11727 if (w == XWINDOW (selected_window))
11729 if (!row->continued_p
11730 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11731 && row->x == 0)
11733 this_line_buffer = XBUFFER (w->buffer);
11735 CHARPOS (this_line_start_pos)
11736 = MATRIX_ROW_START_CHARPOS (row) + delta;
11737 BYTEPOS (this_line_start_pos)
11738 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11740 CHARPOS (this_line_end_pos)
11741 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11742 BYTEPOS (this_line_end_pos)
11743 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11745 this_line_y = w->cursor.y;
11746 this_line_pixel_height = row->height;
11747 this_line_vpos = w->cursor.vpos;
11748 this_line_start_x = row->x;
11750 else
11751 CHARPOS (this_line_start_pos) = 0;
11756 /* Run window scroll functions, if any, for WINDOW with new window
11757 start STARTP. Sets the window start of WINDOW to that position.
11759 We assume that the window's buffer is really current. */
11761 static INLINE struct text_pos
11762 run_window_scroll_functions (window, startp)
11763 Lisp_Object window;
11764 struct text_pos startp;
11766 struct window *w = XWINDOW (window);
11767 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11769 if (current_buffer != XBUFFER (w->buffer))
11770 abort ();
11772 if (!NILP (Vwindow_scroll_functions))
11774 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11775 make_number (CHARPOS (startp)));
11776 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11777 /* In case the hook functions switch buffers. */
11778 if (current_buffer != XBUFFER (w->buffer))
11779 set_buffer_internal_1 (XBUFFER (w->buffer));
11782 return startp;
11786 /* Make sure the line containing the cursor is fully visible.
11787 A value of 1 means there is nothing to be done.
11788 (Either the line is fully visible, or it cannot be made so,
11789 or we cannot tell.)
11791 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11792 is higher than window.
11794 A value of 0 means the caller should do scrolling
11795 as if point had gone off the screen. */
11797 static int
11798 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11799 struct window *w;
11800 int force_p;
11801 int current_matrix_p;
11803 struct glyph_matrix *matrix;
11804 struct glyph_row *row;
11805 int window_height;
11807 if (!make_cursor_line_fully_visible_p)
11808 return 1;
11810 /* It's not always possible to find the cursor, e.g, when a window
11811 is full of overlay strings. Don't do anything in that case. */
11812 if (w->cursor.vpos < 0)
11813 return 1;
11815 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11816 row = MATRIX_ROW (matrix, w->cursor.vpos);
11818 /* If the cursor row is not partially visible, there's nothing to do. */
11819 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11820 return 1;
11822 /* If the row the cursor is in is taller than the window's height,
11823 it's not clear what to do, so do nothing. */
11824 window_height = window_box_height (w);
11825 if (row->height >= window_height)
11827 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11828 return 1;
11830 return 0;
11832 #if 0
11833 /* This code used to try to scroll the window just enough to make
11834 the line visible. It returned 0 to say that the caller should
11835 allocate larger glyph matrices. */
11837 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11839 int dy = row->height - row->visible_height;
11840 w->vscroll = 0;
11841 w->cursor.y += dy;
11842 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11844 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11846 int dy = - (row->height - row->visible_height);
11847 w->vscroll = dy;
11848 w->cursor.y += dy;
11849 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11852 /* When we change the cursor y-position of the selected window,
11853 change this_line_y as well so that the display optimization for
11854 the cursor line of the selected window in redisplay_internal uses
11855 the correct y-position. */
11856 if (w == XWINDOW (selected_window))
11857 this_line_y = w->cursor.y;
11859 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11860 redisplay with larger matrices. */
11861 if (matrix->nrows < required_matrix_height (w))
11863 fonts_changed_p = 1;
11864 return 0;
11867 return 1;
11868 #endif /* 0 */
11872 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11873 non-zero means only WINDOW is redisplayed in redisplay_internal.
11874 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11875 in redisplay_window to bring a partially visible line into view in
11876 the case that only the cursor has moved.
11878 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11879 last screen line's vertical height extends past the end of the screen.
11881 Value is
11883 1 if scrolling succeeded
11885 0 if scrolling didn't find point.
11887 -1 if new fonts have been loaded so that we must interrupt
11888 redisplay, adjust glyph matrices, and try again. */
11890 enum
11892 SCROLLING_SUCCESS,
11893 SCROLLING_FAILED,
11894 SCROLLING_NEED_LARGER_MATRICES
11897 static int
11898 try_scrolling (window, just_this_one_p, scroll_conservatively,
11899 scroll_step, temp_scroll_step, last_line_misfit)
11900 Lisp_Object window;
11901 int just_this_one_p;
11902 EMACS_INT scroll_conservatively, scroll_step;
11903 int temp_scroll_step;
11904 int last_line_misfit;
11906 struct window *w = XWINDOW (window);
11907 struct frame *f = XFRAME (w->frame);
11908 struct text_pos scroll_margin_pos;
11909 struct text_pos pos;
11910 struct text_pos startp;
11911 struct it it;
11912 Lisp_Object window_end;
11913 int this_scroll_margin;
11914 int dy = 0;
11915 int scroll_max;
11916 int rc;
11917 int amount_to_scroll = 0;
11918 Lisp_Object aggressive;
11919 int height;
11920 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11922 #if GLYPH_DEBUG
11923 debug_method_add (w, "try_scrolling");
11924 #endif
11926 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11928 /* Compute scroll margin height in pixels. We scroll when point is
11929 within this distance from the top or bottom of the window. */
11930 if (scroll_margin > 0)
11932 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11933 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11935 else
11936 this_scroll_margin = 0;
11938 /* Force scroll_conservatively to have a reasonable value so it doesn't
11939 cause an overflow while computing how much to scroll. */
11940 if (scroll_conservatively)
11941 scroll_conservatively = min (scroll_conservatively,
11942 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11944 /* Compute how much we should try to scroll maximally to bring point
11945 into view. */
11946 if (scroll_step || scroll_conservatively || temp_scroll_step)
11947 scroll_max = max (scroll_step,
11948 max (scroll_conservatively, temp_scroll_step));
11949 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11950 || NUMBERP (current_buffer->scroll_up_aggressively))
11951 /* We're trying to scroll because of aggressive scrolling
11952 but no scroll_step is set. Choose an arbitrary one. Maybe
11953 there should be a variable for this. */
11954 scroll_max = 10;
11955 else
11956 scroll_max = 0;
11957 scroll_max *= FRAME_LINE_HEIGHT (f);
11959 /* Decide whether we have to scroll down. Start at the window end
11960 and move this_scroll_margin up to find the position of the scroll
11961 margin. */
11962 window_end = Fwindow_end (window, Qt);
11964 too_near_end:
11966 CHARPOS (scroll_margin_pos) = XINT (window_end);
11967 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11969 if (this_scroll_margin || extra_scroll_margin_lines)
11971 start_display (&it, w, scroll_margin_pos);
11972 if (this_scroll_margin)
11973 move_it_vertically_backward (&it, this_scroll_margin);
11974 if (extra_scroll_margin_lines)
11975 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11976 scroll_margin_pos = it.current.pos;
11979 if (PT >= CHARPOS (scroll_margin_pos))
11981 int y0;
11983 /* Point is in the scroll margin at the bottom of the window, or
11984 below. Compute a new window start that makes point visible. */
11986 /* Compute the distance from the scroll margin to PT.
11987 Give up if the distance is greater than scroll_max. */
11988 start_display (&it, w, scroll_margin_pos);
11989 y0 = it.current_y;
11990 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11991 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11993 /* To make point visible, we have to move the window start
11994 down so that the line the cursor is in is visible, which
11995 means we have to add in the height of the cursor line. */
11996 dy = line_bottom_y (&it) - y0;
11998 if (dy > scroll_max)
11999 return SCROLLING_FAILED;
12001 /* Move the window start down. If scrolling conservatively,
12002 move it just enough down to make point visible. If
12003 scroll_step is set, move it down by scroll_step. */
12004 start_display (&it, w, startp);
12006 if (scroll_conservatively)
12007 /* Set AMOUNT_TO_SCROLL to at least one line,
12008 and at most scroll_conservatively lines. */
12009 amount_to_scroll
12010 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12011 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12012 else if (scroll_step || temp_scroll_step)
12013 amount_to_scroll = scroll_max;
12014 else
12016 aggressive = current_buffer->scroll_up_aggressively;
12017 height = WINDOW_BOX_TEXT_HEIGHT (w);
12018 if (NUMBERP (aggressive))
12020 double float_amount = XFLOATINT (aggressive) * height;
12021 amount_to_scroll = float_amount;
12022 if (amount_to_scroll == 0 && float_amount > 0)
12023 amount_to_scroll = 1;
12027 if (amount_to_scroll <= 0)
12028 return SCROLLING_FAILED;
12030 /* If moving by amount_to_scroll leaves STARTP unchanged,
12031 move it down one screen line. */
12033 move_it_vertically (&it, amount_to_scroll);
12034 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12035 move_it_by_lines (&it, 1, 1);
12036 startp = it.current.pos;
12038 else
12040 /* See if point is inside the scroll margin at the top of the
12041 window. */
12042 scroll_margin_pos = startp;
12043 if (this_scroll_margin)
12045 start_display (&it, w, startp);
12046 move_it_vertically (&it, this_scroll_margin);
12047 scroll_margin_pos = it.current.pos;
12050 if (PT < CHARPOS (scroll_margin_pos))
12052 /* Point is in the scroll margin at the top of the window or
12053 above what is displayed in the window. */
12054 int y0;
12056 /* Compute the vertical distance from PT to the scroll
12057 margin position. Give up if distance is greater than
12058 scroll_max. */
12059 SET_TEXT_POS (pos, PT, PT_BYTE);
12060 start_display (&it, w, pos);
12061 y0 = it.current_y;
12062 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12063 it.last_visible_y, -1,
12064 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12065 dy = it.current_y - y0;
12066 if (dy > scroll_max)
12067 return SCROLLING_FAILED;
12069 /* Compute new window start. */
12070 start_display (&it, w, startp);
12072 if (scroll_conservatively)
12073 amount_to_scroll
12074 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12075 else if (scroll_step || temp_scroll_step)
12076 amount_to_scroll = scroll_max;
12077 else
12079 aggressive = current_buffer->scroll_down_aggressively;
12080 height = WINDOW_BOX_TEXT_HEIGHT (w);
12081 if (NUMBERP (aggressive))
12083 double float_amount = XFLOATINT (aggressive) * height;
12084 amount_to_scroll = float_amount;
12085 if (amount_to_scroll == 0 && float_amount > 0)
12086 amount_to_scroll = 1;
12090 if (amount_to_scroll <= 0)
12091 return SCROLLING_FAILED;
12093 move_it_vertically_backward (&it, amount_to_scroll);
12094 startp = it.current.pos;
12098 /* Run window scroll functions. */
12099 startp = run_window_scroll_functions (window, startp);
12101 /* Display the window. Give up if new fonts are loaded, or if point
12102 doesn't appear. */
12103 if (!try_window (window, startp, 0))
12104 rc = SCROLLING_NEED_LARGER_MATRICES;
12105 else if (w->cursor.vpos < 0)
12107 clear_glyph_matrix (w->desired_matrix);
12108 rc = SCROLLING_FAILED;
12110 else
12112 /* Maybe forget recorded base line for line number display. */
12113 if (!just_this_one_p
12114 || current_buffer->clip_changed
12115 || BEG_UNCHANGED < CHARPOS (startp))
12116 w->base_line_number = Qnil;
12118 /* If cursor ends up on a partially visible line,
12119 treat that as being off the bottom of the screen. */
12120 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12122 clear_glyph_matrix (w->desired_matrix);
12123 ++extra_scroll_margin_lines;
12124 goto too_near_end;
12126 rc = SCROLLING_SUCCESS;
12129 return rc;
12133 /* Compute a suitable window start for window W if display of W starts
12134 on a continuation line. Value is non-zero if a new window start
12135 was computed.
12137 The new window start will be computed, based on W's width, starting
12138 from the start of the continued line. It is the start of the
12139 screen line with the minimum distance from the old start W->start. */
12141 static int
12142 compute_window_start_on_continuation_line (w)
12143 struct window *w;
12145 struct text_pos pos, start_pos;
12146 int window_start_changed_p = 0;
12148 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12150 /* If window start is on a continuation line... Window start may be
12151 < BEGV in case there's invisible text at the start of the
12152 buffer (M-x rmail, for example). */
12153 if (CHARPOS (start_pos) > BEGV
12154 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12156 struct it it;
12157 struct glyph_row *row;
12159 /* Handle the case that the window start is out of range. */
12160 if (CHARPOS (start_pos) < BEGV)
12161 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12162 else if (CHARPOS (start_pos) > ZV)
12163 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12165 /* Find the start of the continued line. This should be fast
12166 because scan_buffer is fast (newline cache). */
12167 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12168 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12169 row, DEFAULT_FACE_ID);
12170 reseat_at_previous_visible_line_start (&it);
12172 /* If the line start is "too far" away from the window start,
12173 say it takes too much time to compute a new window start. */
12174 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12175 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12177 int min_distance, distance;
12179 /* Move forward by display lines to find the new window
12180 start. If window width was enlarged, the new start can
12181 be expected to be > the old start. If window width was
12182 decreased, the new window start will be < the old start.
12183 So, we're looking for the display line start with the
12184 minimum distance from the old window start. */
12185 pos = it.current.pos;
12186 min_distance = INFINITY;
12187 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12188 distance < min_distance)
12190 min_distance = distance;
12191 pos = it.current.pos;
12192 move_it_by_lines (&it, 1, 0);
12195 /* Set the window start there. */
12196 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12197 window_start_changed_p = 1;
12201 return window_start_changed_p;
12205 /* Try cursor movement in case text has not changed in window WINDOW,
12206 with window start STARTP. Value is
12208 CURSOR_MOVEMENT_SUCCESS if successful
12210 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12212 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12213 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12214 we want to scroll as if scroll-step were set to 1. See the code.
12216 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12217 which case we have to abort this redisplay, and adjust matrices
12218 first. */
12220 enum
12222 CURSOR_MOVEMENT_SUCCESS,
12223 CURSOR_MOVEMENT_CANNOT_BE_USED,
12224 CURSOR_MOVEMENT_MUST_SCROLL,
12225 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12228 static int
12229 try_cursor_movement (window, startp, scroll_step)
12230 Lisp_Object window;
12231 struct text_pos startp;
12232 int *scroll_step;
12234 struct window *w = XWINDOW (window);
12235 struct frame *f = XFRAME (w->frame);
12236 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12238 #if GLYPH_DEBUG
12239 if (inhibit_try_cursor_movement)
12240 return rc;
12241 #endif
12243 /* Handle case where text has not changed, only point, and it has
12244 not moved off the frame. */
12245 if (/* Point may be in this window. */
12246 PT >= CHARPOS (startp)
12247 /* Selective display hasn't changed. */
12248 && !current_buffer->clip_changed
12249 /* Function force-mode-line-update is used to force a thorough
12250 redisplay. It sets either windows_or_buffers_changed or
12251 update_mode_lines. So don't take a shortcut here for these
12252 cases. */
12253 && !update_mode_lines
12254 && !windows_or_buffers_changed
12255 && !cursor_type_changed
12256 /* Can't use this case if highlighting a region. When a
12257 region exists, cursor movement has to do more than just
12258 set the cursor. */
12259 && !(!NILP (Vtransient_mark_mode)
12260 && !NILP (current_buffer->mark_active))
12261 && NILP (w->region_showing)
12262 && NILP (Vshow_trailing_whitespace)
12263 /* Right after splitting windows, last_point may be nil. */
12264 && INTEGERP (w->last_point)
12265 /* This code is not used for mini-buffer for the sake of the case
12266 of redisplaying to replace an echo area message; since in
12267 that case the mini-buffer contents per se are usually
12268 unchanged. This code is of no real use in the mini-buffer
12269 since the handling of this_line_start_pos, etc., in redisplay
12270 handles the same cases. */
12271 && !EQ (window, minibuf_window)
12272 /* When splitting windows or for new windows, it happens that
12273 redisplay is called with a nil window_end_vpos or one being
12274 larger than the window. This should really be fixed in
12275 window.c. I don't have this on my list, now, so we do
12276 approximately the same as the old redisplay code. --gerd. */
12277 && INTEGERP (w->window_end_vpos)
12278 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12279 && (FRAME_WINDOW_P (f)
12280 || !overlay_arrow_in_current_buffer_p ()))
12282 int this_scroll_margin, top_scroll_margin;
12283 struct glyph_row *row = NULL;
12285 #if GLYPH_DEBUG
12286 debug_method_add (w, "cursor movement");
12287 #endif
12289 /* Scroll if point within this distance from the top or bottom
12290 of the window. This is a pixel value. */
12291 this_scroll_margin = max (0, scroll_margin);
12292 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12293 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12295 top_scroll_margin = this_scroll_margin;
12296 if (WINDOW_WANTS_HEADER_LINE_P (w))
12297 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12299 /* Start with the row the cursor was displayed during the last
12300 not paused redisplay. Give up if that row is not valid. */
12301 if (w->last_cursor.vpos < 0
12302 || w->last_cursor.vpos >= w->current_matrix->nrows)
12303 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12304 else
12306 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12307 if (row->mode_line_p)
12308 ++row;
12309 if (!row->enabled_p)
12310 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12313 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12315 int scroll_p = 0;
12316 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12318 if (PT > XFASTINT (w->last_point))
12320 /* Point has moved forward. */
12321 while (MATRIX_ROW_END_CHARPOS (row) < PT
12322 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12324 xassert (row->enabled_p);
12325 ++row;
12328 /* The end position of a row equals the start position
12329 of the next row. If PT is there, we would rather
12330 display it in the next line. */
12331 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12332 && MATRIX_ROW_END_CHARPOS (row) == PT
12333 && !cursor_row_p (w, row))
12334 ++row;
12336 /* If within the scroll margin, scroll. Note that
12337 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12338 the next line would be drawn, and that
12339 this_scroll_margin can be zero. */
12340 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12341 || PT > MATRIX_ROW_END_CHARPOS (row)
12342 /* Line is completely visible last line in window
12343 and PT is to be set in the next line. */
12344 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12345 && PT == MATRIX_ROW_END_CHARPOS (row)
12346 && !row->ends_at_zv_p
12347 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12348 scroll_p = 1;
12350 else if (PT < XFASTINT (w->last_point))
12352 /* Cursor has to be moved backward. Note that PT >=
12353 CHARPOS (startp) because of the outer if-statement. */
12354 while (!row->mode_line_p
12355 && (MATRIX_ROW_START_CHARPOS (row) > PT
12356 || (MATRIX_ROW_START_CHARPOS (row) == PT
12357 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12358 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12359 row > w->current_matrix->rows
12360 && (row-1)->ends_in_newline_from_string_p))))
12361 && (row->y > top_scroll_margin
12362 || CHARPOS (startp) == BEGV))
12364 xassert (row->enabled_p);
12365 --row;
12368 /* Consider the following case: Window starts at BEGV,
12369 there is invisible, intangible text at BEGV, so that
12370 display starts at some point START > BEGV. It can
12371 happen that we are called with PT somewhere between
12372 BEGV and START. Try to handle that case. */
12373 if (row < w->current_matrix->rows
12374 || row->mode_line_p)
12376 row = w->current_matrix->rows;
12377 if (row->mode_line_p)
12378 ++row;
12381 /* Due to newlines in overlay strings, we may have to
12382 skip forward over overlay strings. */
12383 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12384 && MATRIX_ROW_END_CHARPOS (row) == PT
12385 && !cursor_row_p (w, row))
12386 ++row;
12388 /* If within the scroll margin, scroll. */
12389 if (row->y < top_scroll_margin
12390 && CHARPOS (startp) != BEGV)
12391 scroll_p = 1;
12393 else
12395 /* Cursor did not move. So don't scroll even if cursor line
12396 is partially visible, as it was so before. */
12397 rc = CURSOR_MOVEMENT_SUCCESS;
12400 if (PT < MATRIX_ROW_START_CHARPOS (row)
12401 || PT > MATRIX_ROW_END_CHARPOS (row))
12403 /* if PT is not in the glyph row, give up. */
12404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12406 else if (rc != CURSOR_MOVEMENT_SUCCESS
12407 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12408 && make_cursor_line_fully_visible_p)
12410 if (PT == MATRIX_ROW_END_CHARPOS (row)
12411 && !row->ends_at_zv_p
12412 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12414 else if (row->height > window_box_height (w))
12416 /* If we end up in a partially visible line, let's
12417 make it fully visible, except when it's taller
12418 than the window, in which case we can't do much
12419 about it. */
12420 *scroll_step = 1;
12421 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12423 else
12425 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12426 if (!cursor_row_fully_visible_p (w, 0, 1))
12427 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12428 else
12429 rc = CURSOR_MOVEMENT_SUCCESS;
12432 else if (scroll_p)
12433 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12434 else
12436 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12437 rc = CURSOR_MOVEMENT_SUCCESS;
12442 return rc;
12445 void
12446 set_vertical_scroll_bar (w)
12447 struct window *w;
12449 int start, end, whole;
12451 /* Calculate the start and end positions for the current window.
12452 At some point, it would be nice to choose between scrollbars
12453 which reflect the whole buffer size, with special markers
12454 indicating narrowing, and scrollbars which reflect only the
12455 visible region.
12457 Note that mini-buffers sometimes aren't displaying any text. */
12458 if (!MINI_WINDOW_P (w)
12459 || (w == XWINDOW (minibuf_window)
12460 && NILP (echo_area_buffer[0])))
12462 struct buffer *buf = XBUFFER (w->buffer);
12463 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12464 start = marker_position (w->start) - BUF_BEGV (buf);
12465 /* I don't think this is guaranteed to be right. For the
12466 moment, we'll pretend it is. */
12467 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12469 if (end < start)
12470 end = start;
12471 if (whole < (end - start))
12472 whole = end - start;
12474 else
12475 start = end = whole = 0;
12477 /* Indicate what this scroll bar ought to be displaying now. */
12478 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12479 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12480 (w, end - start, whole, start);
12484 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12485 selected_window is redisplayed.
12487 We can return without actually redisplaying the window if
12488 fonts_changed_p is nonzero. In that case, redisplay_internal will
12489 retry. */
12491 static void
12492 redisplay_window (window, just_this_one_p)
12493 Lisp_Object window;
12494 int just_this_one_p;
12496 struct window *w = XWINDOW (window);
12497 struct frame *f = XFRAME (w->frame);
12498 struct buffer *buffer = XBUFFER (w->buffer);
12499 struct buffer *old = current_buffer;
12500 struct text_pos lpoint, opoint, startp;
12501 int update_mode_line;
12502 int tem;
12503 struct it it;
12504 /* Record it now because it's overwritten. */
12505 int current_matrix_up_to_date_p = 0;
12506 int used_current_matrix_p = 0;
12507 /* This is less strict than current_matrix_up_to_date_p.
12508 It indictes that the buffer contents and narrowing are unchanged. */
12509 int buffer_unchanged_p = 0;
12510 int temp_scroll_step = 0;
12511 int count = SPECPDL_INDEX ();
12512 int rc;
12513 int centering_position = -1;
12514 int last_line_misfit = 0;
12516 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12517 opoint = lpoint;
12519 /* W must be a leaf window here. */
12520 xassert (!NILP (w->buffer));
12521 #if GLYPH_DEBUG
12522 *w->desired_matrix->method = 0;
12523 #endif
12525 specbind (Qinhibit_point_motion_hooks, Qt);
12527 reconsider_clip_changes (w, buffer);
12529 /* Has the mode line to be updated? */
12530 update_mode_line = (!NILP (w->update_mode_line)
12531 || update_mode_lines
12532 || buffer->clip_changed
12533 || buffer->prevent_redisplay_optimizations_p);
12535 if (MINI_WINDOW_P (w))
12537 if (w == XWINDOW (echo_area_window)
12538 && !NILP (echo_area_buffer[0]))
12540 if (update_mode_line)
12541 /* We may have to update a tty frame's menu bar or a
12542 tool-bar. Example `M-x C-h C-h C-g'. */
12543 goto finish_menu_bars;
12544 else
12545 /* We've already displayed the echo area glyphs in this window. */
12546 goto finish_scroll_bars;
12548 else if ((w != XWINDOW (minibuf_window)
12549 || minibuf_level == 0)
12550 /* When buffer is nonempty, redisplay window normally. */
12551 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12552 /* Quail displays non-mini buffers in minibuffer window.
12553 In that case, redisplay the window normally. */
12554 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12556 /* W is a mini-buffer window, but it's not active, so clear
12557 it. */
12558 int yb = window_text_bottom_y (w);
12559 struct glyph_row *row;
12560 int y;
12562 for (y = 0, row = w->desired_matrix->rows;
12563 y < yb;
12564 y += row->height, ++row)
12565 blank_row (w, row, y);
12566 goto finish_scroll_bars;
12569 clear_glyph_matrix (w->desired_matrix);
12572 /* Otherwise set up data on this window; select its buffer and point
12573 value. */
12574 /* Really select the buffer, for the sake of buffer-local
12575 variables. */
12576 set_buffer_internal_1 (XBUFFER (w->buffer));
12577 SET_TEXT_POS (opoint, PT, PT_BYTE);
12579 current_matrix_up_to_date_p
12580 = (!NILP (w->window_end_valid)
12581 && !current_buffer->clip_changed
12582 && !current_buffer->prevent_redisplay_optimizations_p
12583 && XFASTINT (w->last_modified) >= MODIFF
12584 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12586 buffer_unchanged_p
12587 = (!NILP (w->window_end_valid)
12588 && !current_buffer->clip_changed
12589 && XFASTINT (w->last_modified) >= MODIFF
12590 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12592 /* When windows_or_buffers_changed is non-zero, we can't rely on
12593 the window end being valid, so set it to nil there. */
12594 if (windows_or_buffers_changed)
12596 /* If window starts on a continuation line, maybe adjust the
12597 window start in case the window's width changed. */
12598 if (XMARKER (w->start)->buffer == current_buffer)
12599 compute_window_start_on_continuation_line (w);
12601 w->window_end_valid = Qnil;
12604 /* Some sanity checks. */
12605 CHECK_WINDOW_END (w);
12606 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12607 abort ();
12608 if (BYTEPOS (opoint) < CHARPOS (opoint))
12609 abort ();
12611 /* If %c is in mode line, update it if needed. */
12612 if (!NILP (w->column_number_displayed)
12613 /* This alternative quickly identifies a common case
12614 where no change is needed. */
12615 && !(PT == XFASTINT (w->last_point)
12616 && XFASTINT (w->last_modified) >= MODIFF
12617 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12618 && (XFASTINT (w->column_number_displayed)
12619 != (int) current_column ())) /* iftc */
12620 update_mode_line = 1;
12622 /* Count number of windows showing the selected buffer. An indirect
12623 buffer counts as its base buffer. */
12624 if (!just_this_one_p)
12626 struct buffer *current_base, *window_base;
12627 current_base = current_buffer;
12628 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12629 if (current_base->base_buffer)
12630 current_base = current_base->base_buffer;
12631 if (window_base->base_buffer)
12632 window_base = window_base->base_buffer;
12633 if (current_base == window_base)
12634 buffer_shared++;
12637 /* Point refers normally to the selected window. For any other
12638 window, set up appropriate value. */
12639 if (!EQ (window, selected_window))
12641 int new_pt = XMARKER (w->pointm)->charpos;
12642 int new_pt_byte = marker_byte_position (w->pointm);
12643 if (new_pt < BEGV)
12645 new_pt = BEGV;
12646 new_pt_byte = BEGV_BYTE;
12647 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12649 else if (new_pt > (ZV - 1))
12651 new_pt = ZV;
12652 new_pt_byte = ZV_BYTE;
12653 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12656 /* We don't use SET_PT so that the point-motion hooks don't run. */
12657 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12660 /* If any of the character widths specified in the display table
12661 have changed, invalidate the width run cache. It's true that
12662 this may be a bit late to catch such changes, but the rest of
12663 redisplay goes (non-fatally) haywire when the display table is
12664 changed, so why should we worry about doing any better? */
12665 if (current_buffer->width_run_cache)
12667 struct Lisp_Char_Table *disptab = buffer_display_table ();
12669 if (! disptab_matches_widthtab (disptab,
12670 XVECTOR (current_buffer->width_table)))
12672 invalidate_region_cache (current_buffer,
12673 current_buffer->width_run_cache,
12674 BEG, Z);
12675 recompute_width_table (current_buffer, disptab);
12679 /* If window-start is screwed up, choose a new one. */
12680 if (XMARKER (w->start)->buffer != current_buffer)
12681 goto recenter;
12683 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12685 /* If someone specified a new starting point but did not insist,
12686 check whether it can be used. */
12687 if (!NILP (w->optional_new_start)
12688 && CHARPOS (startp) >= BEGV
12689 && CHARPOS (startp) <= ZV)
12691 w->optional_new_start = Qnil;
12692 start_display (&it, w, startp);
12693 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12694 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12695 if (IT_CHARPOS (it) == PT)
12696 w->force_start = Qt;
12697 /* IT may overshoot PT if text at PT is invisible. */
12698 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12699 w->force_start = Qt;
12704 /* Handle case where place to start displaying has been specified,
12705 unless the specified location is outside the accessible range. */
12706 if (!NILP (w->force_start)
12707 || w->frozen_window_start_p)
12709 /* We set this later on if we have to adjust point. */
12710 int new_vpos = -1;
12711 int val;
12713 w->force_start = Qnil;
12714 w->vscroll = 0;
12715 w->window_end_valid = Qnil;
12717 /* Forget any recorded base line for line number display. */
12718 if (!buffer_unchanged_p)
12719 w->base_line_number = Qnil;
12721 /* Redisplay the mode line. Select the buffer properly for that.
12722 Also, run the hook window-scroll-functions
12723 because we have scrolled. */
12724 /* Note, we do this after clearing force_start because
12725 if there's an error, it is better to forget about force_start
12726 than to get into an infinite loop calling the hook functions
12727 and having them get more errors. */
12728 if (!update_mode_line
12729 || ! NILP (Vwindow_scroll_functions))
12731 update_mode_line = 1;
12732 w->update_mode_line = Qt;
12733 startp = run_window_scroll_functions (window, startp);
12736 w->last_modified = make_number (0);
12737 w->last_overlay_modified = make_number (0);
12738 if (CHARPOS (startp) < BEGV)
12739 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12740 else if (CHARPOS (startp) > ZV)
12741 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12743 /* Redisplay, then check if cursor has been set during the
12744 redisplay. Give up if new fonts were loaded. */
12745 val = try_window (window, startp, 1);
12746 if (!val)
12748 w->force_start = Qt;
12749 clear_glyph_matrix (w->desired_matrix);
12750 goto need_larger_matrices;
12752 /* Point was outside the scroll margins. */
12753 if (val < 0)
12754 new_vpos = window_box_height (w) / 2;
12756 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12758 /* If point does not appear, try to move point so it does
12759 appear. The desired matrix has been built above, so we
12760 can use it here. */
12761 new_vpos = window_box_height (w) / 2;
12764 if (!cursor_row_fully_visible_p (w, 0, 0))
12766 /* Point does appear, but on a line partly visible at end of window.
12767 Move it back to a fully-visible line. */
12768 new_vpos = window_box_height (w);
12771 /* If we need to move point for either of the above reasons,
12772 now actually do it. */
12773 if (new_vpos >= 0)
12775 struct glyph_row *row;
12777 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12778 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12779 ++row;
12781 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12782 MATRIX_ROW_START_BYTEPOS (row));
12784 if (w != XWINDOW (selected_window))
12785 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12786 else if (current_buffer == old)
12787 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12789 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12791 /* If we are highlighting the region, then we just changed
12792 the region, so redisplay to show it. */
12793 if (!NILP (Vtransient_mark_mode)
12794 && !NILP (current_buffer->mark_active))
12796 clear_glyph_matrix (w->desired_matrix);
12797 if (!try_window (window, startp, 0))
12798 goto need_larger_matrices;
12802 #if GLYPH_DEBUG
12803 debug_method_add (w, "forced window start");
12804 #endif
12805 goto done;
12808 /* Handle case where text has not changed, only point, and it has
12809 not moved off the frame, and we are not retrying after hscroll.
12810 (current_matrix_up_to_date_p is nonzero when retrying.) */
12811 if (current_matrix_up_to_date_p
12812 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12813 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12815 switch (rc)
12817 case CURSOR_MOVEMENT_SUCCESS:
12818 used_current_matrix_p = 1;
12819 goto done;
12821 #if 0 /* try_cursor_movement never returns this value. */
12822 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12823 goto need_larger_matrices;
12824 #endif
12826 case CURSOR_MOVEMENT_MUST_SCROLL:
12827 goto try_to_scroll;
12829 default:
12830 abort ();
12833 /* If current starting point was originally the beginning of a line
12834 but no longer is, find a new starting point. */
12835 else if (!NILP (w->start_at_line_beg)
12836 && !(CHARPOS (startp) <= BEGV
12837 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12839 #if GLYPH_DEBUG
12840 debug_method_add (w, "recenter 1");
12841 #endif
12842 goto recenter;
12845 /* Try scrolling with try_window_id. Value is > 0 if update has
12846 been done, it is -1 if we know that the same window start will
12847 not work. It is 0 if unsuccessful for some other reason. */
12848 else if ((tem = try_window_id (w)) != 0)
12850 #if GLYPH_DEBUG
12851 debug_method_add (w, "try_window_id %d", tem);
12852 #endif
12854 if (fonts_changed_p)
12855 goto need_larger_matrices;
12856 if (tem > 0)
12857 goto done;
12859 /* Otherwise try_window_id has returned -1 which means that we
12860 don't want the alternative below this comment to execute. */
12862 else if (CHARPOS (startp) >= BEGV
12863 && CHARPOS (startp) <= ZV
12864 && PT >= CHARPOS (startp)
12865 && (CHARPOS (startp) < ZV
12866 /* Avoid starting at end of buffer. */
12867 || CHARPOS (startp) == BEGV
12868 || (XFASTINT (w->last_modified) >= MODIFF
12869 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12871 #if GLYPH_DEBUG
12872 debug_method_add (w, "same window start");
12873 #endif
12875 /* Try to redisplay starting at same place as before.
12876 If point has not moved off frame, accept the results. */
12877 if (!current_matrix_up_to_date_p
12878 /* Don't use try_window_reusing_current_matrix in this case
12879 because a window scroll function can have changed the
12880 buffer. */
12881 || !NILP (Vwindow_scroll_functions)
12882 || MINI_WINDOW_P (w)
12883 || !(used_current_matrix_p
12884 = try_window_reusing_current_matrix (w)))
12886 IF_DEBUG (debug_method_add (w, "1"));
12887 if (try_window (window, startp, 1) < 0)
12888 /* -1 means we need to scroll.
12889 0 means we need new matrices, but fonts_changed_p
12890 is set in that case, so we will detect it below. */
12891 goto try_to_scroll;
12894 if (fonts_changed_p)
12895 goto need_larger_matrices;
12897 if (w->cursor.vpos >= 0)
12899 if (!just_this_one_p
12900 || current_buffer->clip_changed
12901 || BEG_UNCHANGED < CHARPOS (startp))
12902 /* Forget any recorded base line for line number display. */
12903 w->base_line_number = Qnil;
12905 if (!cursor_row_fully_visible_p (w, 1, 0))
12907 clear_glyph_matrix (w->desired_matrix);
12908 last_line_misfit = 1;
12910 /* Drop through and scroll. */
12911 else
12912 goto done;
12914 else
12915 clear_glyph_matrix (w->desired_matrix);
12918 try_to_scroll:
12920 w->last_modified = make_number (0);
12921 w->last_overlay_modified = make_number (0);
12923 /* Redisplay the mode line. Select the buffer properly for that. */
12924 if (!update_mode_line)
12926 update_mode_line = 1;
12927 w->update_mode_line = Qt;
12930 /* Try to scroll by specified few lines. */
12931 if ((scroll_conservatively
12932 || scroll_step
12933 || temp_scroll_step
12934 || NUMBERP (current_buffer->scroll_up_aggressively)
12935 || NUMBERP (current_buffer->scroll_down_aggressively))
12936 && !current_buffer->clip_changed
12937 && CHARPOS (startp) >= BEGV
12938 && CHARPOS (startp) <= ZV)
12940 /* The function returns -1 if new fonts were loaded, 1 if
12941 successful, 0 if not successful. */
12942 int rc = try_scrolling (window, just_this_one_p,
12943 scroll_conservatively,
12944 scroll_step,
12945 temp_scroll_step, last_line_misfit);
12946 switch (rc)
12948 case SCROLLING_SUCCESS:
12949 goto done;
12951 case SCROLLING_NEED_LARGER_MATRICES:
12952 goto need_larger_matrices;
12954 case SCROLLING_FAILED:
12955 break;
12957 default:
12958 abort ();
12962 /* Finally, just choose place to start which centers point */
12964 recenter:
12965 if (centering_position < 0)
12966 centering_position = window_box_height (w) / 2;
12968 #if GLYPH_DEBUG
12969 debug_method_add (w, "recenter");
12970 #endif
12972 /* w->vscroll = 0; */
12974 /* Forget any previously recorded base line for line number display. */
12975 if (!buffer_unchanged_p)
12976 w->base_line_number = Qnil;
12978 /* Move backward half the height of the window. */
12979 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12980 it.current_y = it.last_visible_y;
12981 move_it_vertically_backward (&it, centering_position);
12982 xassert (IT_CHARPOS (it) >= BEGV);
12984 /* The function move_it_vertically_backward may move over more
12985 than the specified y-distance. If it->w is small, e.g. a
12986 mini-buffer window, we may end up in front of the window's
12987 display area. Start displaying at the start of the line
12988 containing PT in this case. */
12989 if (it.current_y <= 0)
12991 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12992 move_it_vertically_backward (&it, 0);
12993 #if 0
12994 /* I think this assert is bogus if buffer contains
12995 invisible text or images. KFS. */
12996 xassert (IT_CHARPOS (it) <= PT);
12997 #endif
12998 it.current_y = 0;
13001 it.current_x = it.hpos = 0;
13003 /* Set startp here explicitly in case that helps avoid an infinite loop
13004 in case the window-scroll-functions functions get errors. */
13005 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13007 /* Run scroll hooks. */
13008 startp = run_window_scroll_functions (window, it.current.pos);
13010 /* Redisplay the window. */
13011 if (!current_matrix_up_to_date_p
13012 || windows_or_buffers_changed
13013 || cursor_type_changed
13014 /* Don't use try_window_reusing_current_matrix in this case
13015 because it can have changed the buffer. */
13016 || !NILP (Vwindow_scroll_functions)
13017 || !just_this_one_p
13018 || MINI_WINDOW_P (w)
13019 || !(used_current_matrix_p
13020 = try_window_reusing_current_matrix (w)))
13021 try_window (window, startp, 0);
13023 /* If new fonts have been loaded (due to fontsets), give up. We
13024 have to start a new redisplay since we need to re-adjust glyph
13025 matrices. */
13026 if (fonts_changed_p)
13027 goto need_larger_matrices;
13029 /* If cursor did not appear assume that the middle of the window is
13030 in the first line of the window. Do it again with the next line.
13031 (Imagine a window of height 100, displaying two lines of height
13032 60. Moving back 50 from it->last_visible_y will end in the first
13033 line.) */
13034 if (w->cursor.vpos < 0)
13036 if (!NILP (w->window_end_valid)
13037 && PT >= Z - XFASTINT (w->window_end_pos))
13039 clear_glyph_matrix (w->desired_matrix);
13040 move_it_by_lines (&it, 1, 0);
13041 try_window (window, it.current.pos, 0);
13043 else if (PT < IT_CHARPOS (it))
13045 clear_glyph_matrix (w->desired_matrix);
13046 move_it_by_lines (&it, -1, 0);
13047 try_window (window, it.current.pos, 0);
13049 else
13051 /* Not much we can do about it. */
13055 /* Consider the following case: Window starts at BEGV, there is
13056 invisible, intangible text at BEGV, so that display starts at
13057 some point START > BEGV. It can happen that we are called with
13058 PT somewhere between BEGV and START. Try to handle that case. */
13059 if (w->cursor.vpos < 0)
13061 struct glyph_row *row = w->current_matrix->rows;
13062 if (row->mode_line_p)
13063 ++row;
13064 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13067 if (!cursor_row_fully_visible_p (w, 0, 0))
13069 /* If vscroll is enabled, disable it and try again. */
13070 if (w->vscroll)
13072 w->vscroll = 0;
13073 clear_glyph_matrix (w->desired_matrix);
13074 goto recenter;
13077 /* If centering point failed to make the whole line visible,
13078 put point at the top instead. That has to make the whole line
13079 visible, if it can be done. */
13080 if (centering_position == 0)
13081 goto done;
13083 clear_glyph_matrix (w->desired_matrix);
13084 centering_position = 0;
13085 goto recenter;
13088 done:
13090 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13091 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13092 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13093 ? Qt : Qnil);
13095 /* Display the mode line, if we must. */
13096 if ((update_mode_line
13097 /* If window not full width, must redo its mode line
13098 if (a) the window to its side is being redone and
13099 (b) we do a frame-based redisplay. This is a consequence
13100 of how inverted lines are drawn in frame-based redisplay. */
13101 || (!just_this_one_p
13102 && !FRAME_WINDOW_P (f)
13103 && !WINDOW_FULL_WIDTH_P (w))
13104 /* Line number to display. */
13105 || INTEGERP (w->base_line_pos)
13106 /* Column number is displayed and different from the one displayed. */
13107 || (!NILP (w->column_number_displayed)
13108 && (XFASTINT (w->column_number_displayed)
13109 != (int) current_column ()))) /* iftc */
13110 /* This means that the window has a mode line. */
13111 && (WINDOW_WANTS_MODELINE_P (w)
13112 || WINDOW_WANTS_HEADER_LINE_P (w)))
13114 display_mode_lines (w);
13116 /* If mode line height has changed, arrange for a thorough
13117 immediate redisplay using the correct mode line height. */
13118 if (WINDOW_WANTS_MODELINE_P (w)
13119 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13121 fonts_changed_p = 1;
13122 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13123 = DESIRED_MODE_LINE_HEIGHT (w);
13126 /* If top line height has changed, arrange for a thorough
13127 immediate redisplay using the correct mode line height. */
13128 if (WINDOW_WANTS_HEADER_LINE_P (w)
13129 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13131 fonts_changed_p = 1;
13132 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13133 = DESIRED_HEADER_LINE_HEIGHT (w);
13136 if (fonts_changed_p)
13137 goto need_larger_matrices;
13140 if (!line_number_displayed
13141 && !BUFFERP (w->base_line_pos))
13143 w->base_line_pos = Qnil;
13144 w->base_line_number = Qnil;
13147 finish_menu_bars:
13149 /* When we reach a frame's selected window, redo the frame's menu bar. */
13150 if (update_mode_line
13151 && EQ (FRAME_SELECTED_WINDOW (f), window))
13153 int redisplay_menu_p = 0;
13154 int redisplay_tool_bar_p = 0;
13156 if (FRAME_WINDOW_P (f))
13158 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13159 || defined (USE_GTK)
13160 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13161 #else
13162 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13163 #endif
13165 else
13166 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13168 if (redisplay_menu_p)
13169 display_menu_bar (w);
13171 #ifdef HAVE_WINDOW_SYSTEM
13172 if (FRAME_WINDOW_P (f))
13174 #ifdef USE_GTK
13175 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13176 #else
13177 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13178 && (FRAME_TOOL_BAR_LINES (f) > 0
13179 || auto_resize_tool_bars_p);
13180 #endif
13182 if (redisplay_tool_bar_p)
13183 redisplay_tool_bar (f);
13185 #endif
13188 #ifdef HAVE_WINDOW_SYSTEM
13189 if (FRAME_WINDOW_P (f)
13190 && update_window_fringes (w, (just_this_one_p
13191 || (!used_current_matrix_p && !overlay_arrow_seen)
13192 || w->pseudo_window_p)))
13194 update_begin (f);
13195 BLOCK_INPUT;
13196 if (draw_window_fringes (w, 1))
13197 x_draw_vertical_border (w);
13198 UNBLOCK_INPUT;
13199 update_end (f);
13201 #endif /* HAVE_WINDOW_SYSTEM */
13203 /* We go to this label, with fonts_changed_p nonzero,
13204 if it is necessary to try again using larger glyph matrices.
13205 We have to redeem the scroll bar even in this case,
13206 because the loop in redisplay_internal expects that. */
13207 need_larger_matrices:
13209 finish_scroll_bars:
13211 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13213 /* Set the thumb's position and size. */
13214 set_vertical_scroll_bar (w);
13216 /* Note that we actually used the scroll bar attached to this
13217 window, so it shouldn't be deleted at the end of redisplay. */
13218 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13219 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13222 /* Restore current_buffer and value of point in it. */
13223 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13224 set_buffer_internal_1 (old);
13225 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13227 unbind_to (count, Qnil);
13231 /* Build the complete desired matrix of WINDOW with a window start
13232 buffer position POS.
13234 Value is 1 if successful. It is zero if fonts were loaded during
13235 redisplay which makes re-adjusting glyph matrices necessary, and -1
13236 if point would appear in the scroll margins.
13237 (We check that only if CHECK_MARGINS is nonzero. */
13240 try_window (window, pos, check_margins)
13241 Lisp_Object window;
13242 struct text_pos pos;
13243 int check_margins;
13245 struct window *w = XWINDOW (window);
13246 struct it it;
13247 struct glyph_row *last_text_row = NULL;
13249 /* Make POS the new window start. */
13250 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13252 /* Mark cursor position as unknown. No overlay arrow seen. */
13253 w->cursor.vpos = -1;
13254 overlay_arrow_seen = 0;
13256 /* Initialize iterator and info to start at POS. */
13257 start_display (&it, w, pos);
13259 /* Display all lines of W. */
13260 while (it.current_y < it.last_visible_y)
13262 if (display_line (&it))
13263 last_text_row = it.glyph_row - 1;
13264 if (fonts_changed_p)
13265 return 0;
13268 /* Don't let the cursor end in the scroll margins. */
13269 if (check_margins
13270 && !MINI_WINDOW_P (w))
13272 int this_scroll_margin;
13274 this_scroll_margin = max (0, scroll_margin);
13275 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13276 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13278 if ((w->cursor.y < this_scroll_margin
13279 && CHARPOS (pos) > BEGV
13280 && IT_CHARPOS (it) < ZV)
13281 /* rms: considering make_cursor_line_fully_visible_p here
13282 seems to give wrong results. We don't want to recenter
13283 when the last line is partly visible, we want to allow
13284 that case to be handled in the usual way. */
13285 || (w->cursor.y + 1) > it.last_visible_y)
13287 w->cursor.vpos = -1;
13288 clear_glyph_matrix (w->desired_matrix);
13289 return -1;
13293 /* If bottom moved off end of frame, change mode line percentage. */
13294 if (XFASTINT (w->window_end_pos) <= 0
13295 && Z != IT_CHARPOS (it))
13296 w->update_mode_line = Qt;
13298 /* Set window_end_pos to the offset of the last character displayed
13299 on the window from the end of current_buffer. Set
13300 window_end_vpos to its row number. */
13301 if (last_text_row)
13303 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13304 w->window_end_bytepos
13305 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13306 w->window_end_pos
13307 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13308 w->window_end_vpos
13309 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13310 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13311 ->displays_text_p);
13313 else
13315 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13316 w->window_end_pos = make_number (Z - ZV);
13317 w->window_end_vpos = make_number (0);
13320 /* But that is not valid info until redisplay finishes. */
13321 w->window_end_valid = Qnil;
13322 return 1;
13327 /************************************************************************
13328 Window redisplay reusing current matrix when buffer has not changed
13329 ************************************************************************/
13331 /* Try redisplay of window W showing an unchanged buffer with a
13332 different window start than the last time it was displayed by
13333 reusing its current matrix. Value is non-zero if successful.
13334 W->start is the new window start. */
13336 static int
13337 try_window_reusing_current_matrix (w)
13338 struct window *w;
13340 struct frame *f = XFRAME (w->frame);
13341 struct glyph_row *row, *bottom_row;
13342 struct it it;
13343 struct run run;
13344 struct text_pos start, new_start;
13345 int nrows_scrolled, i;
13346 struct glyph_row *last_text_row;
13347 struct glyph_row *last_reused_text_row;
13348 struct glyph_row *start_row;
13349 int start_vpos, min_y, max_y;
13351 #if GLYPH_DEBUG
13352 if (inhibit_try_window_reusing)
13353 return 0;
13354 #endif
13356 if (/* This function doesn't handle terminal frames. */
13357 !FRAME_WINDOW_P (f)
13358 /* Don't try to reuse the display if windows have been split
13359 or such. */
13360 || windows_or_buffers_changed
13361 || cursor_type_changed)
13362 return 0;
13364 /* Can't do this if region may have changed. */
13365 if ((!NILP (Vtransient_mark_mode)
13366 && !NILP (current_buffer->mark_active))
13367 || !NILP (w->region_showing)
13368 || !NILP (Vshow_trailing_whitespace))
13369 return 0;
13371 /* If top-line visibility has changed, give up. */
13372 if (WINDOW_WANTS_HEADER_LINE_P (w)
13373 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13374 return 0;
13376 /* Give up if old or new display is scrolled vertically. We could
13377 make this function handle this, but right now it doesn't. */
13378 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13379 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13380 return 0;
13382 /* The variable new_start now holds the new window start. The old
13383 start `start' can be determined from the current matrix. */
13384 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13385 start = start_row->start.pos;
13386 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13388 /* Clear the desired matrix for the display below. */
13389 clear_glyph_matrix (w->desired_matrix);
13391 if (CHARPOS (new_start) <= CHARPOS (start))
13393 int first_row_y;
13395 /* Don't use this method if the display starts with an ellipsis
13396 displayed for invisible text. It's not easy to handle that case
13397 below, and it's certainly not worth the effort since this is
13398 not a frequent case. */
13399 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13400 return 0;
13402 IF_DEBUG (debug_method_add (w, "twu1"));
13404 /* Display up to a row that can be reused. The variable
13405 last_text_row is set to the last row displayed that displays
13406 text. Note that it.vpos == 0 if or if not there is a
13407 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13408 start_display (&it, w, new_start);
13409 first_row_y = it.current_y;
13410 w->cursor.vpos = -1;
13411 last_text_row = last_reused_text_row = NULL;
13413 while (it.current_y < it.last_visible_y
13414 && !fonts_changed_p)
13416 /* If we have reached into the characters in the START row,
13417 that means the line boundaries have changed. So we
13418 can't start copying with the row START. Maybe it will
13419 work to start copying with the following row. */
13420 while (IT_CHARPOS (it) > CHARPOS (start))
13422 /* Advance to the next row as the "start". */
13423 start_row++;
13424 start = start_row->start.pos;
13425 /* If there are no more rows to try, or just one, give up. */
13426 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13427 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13428 || CHARPOS (start) == ZV)
13430 clear_glyph_matrix (w->desired_matrix);
13431 return 0;
13434 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13436 /* If we have reached alignment,
13437 we can copy the rest of the rows. */
13438 if (IT_CHARPOS (it) == CHARPOS (start))
13439 break;
13441 if (display_line (&it))
13442 last_text_row = it.glyph_row - 1;
13445 /* A value of current_y < last_visible_y means that we stopped
13446 at the previous window start, which in turn means that we
13447 have at least one reusable row. */
13448 if (it.current_y < it.last_visible_y)
13450 /* IT.vpos always starts from 0; it counts text lines. */
13451 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13453 /* Find PT if not already found in the lines displayed. */
13454 if (w->cursor.vpos < 0)
13456 int dy = it.current_y - start_row->y;
13458 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13459 row = row_containing_pos (w, PT, row, NULL, dy);
13460 if (row)
13461 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13462 dy, nrows_scrolled);
13463 else
13465 clear_glyph_matrix (w->desired_matrix);
13466 return 0;
13470 /* Scroll the display. Do it before the current matrix is
13471 changed. The problem here is that update has not yet
13472 run, i.e. part of the current matrix is not up to date.
13473 scroll_run_hook will clear the cursor, and use the
13474 current matrix to get the height of the row the cursor is
13475 in. */
13476 run.current_y = start_row->y;
13477 run.desired_y = it.current_y;
13478 run.height = it.last_visible_y - it.current_y;
13480 if (run.height > 0 && run.current_y != run.desired_y)
13482 update_begin (f);
13483 FRAME_RIF (f)->update_window_begin_hook (w);
13484 FRAME_RIF (f)->clear_window_mouse_face (w);
13485 FRAME_RIF (f)->scroll_run_hook (w, &run);
13486 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13487 update_end (f);
13490 /* Shift current matrix down by nrows_scrolled lines. */
13491 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13492 rotate_matrix (w->current_matrix,
13493 start_vpos,
13494 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13495 nrows_scrolled);
13497 /* Disable lines that must be updated. */
13498 for (i = 0; i < it.vpos; ++i)
13499 (start_row + i)->enabled_p = 0;
13501 /* Re-compute Y positions. */
13502 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13503 max_y = it.last_visible_y;
13504 for (row = start_row + nrows_scrolled;
13505 row < bottom_row;
13506 ++row)
13508 row->y = it.current_y;
13509 row->visible_height = row->height;
13511 if (row->y < min_y)
13512 row->visible_height -= min_y - row->y;
13513 if (row->y + row->height > max_y)
13514 row->visible_height -= row->y + row->height - max_y;
13515 row->redraw_fringe_bitmaps_p = 1;
13517 it.current_y += row->height;
13519 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13520 last_reused_text_row = row;
13521 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13522 break;
13525 /* Disable lines in the current matrix which are now
13526 below the window. */
13527 for (++row; row < bottom_row; ++row)
13528 row->enabled_p = row->mode_line_p = 0;
13531 /* Update window_end_pos etc.; last_reused_text_row is the last
13532 reused row from the current matrix containing text, if any.
13533 The value of last_text_row is the last displayed line
13534 containing text. */
13535 if (last_reused_text_row)
13537 w->window_end_bytepos
13538 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13539 w->window_end_pos
13540 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13541 w->window_end_vpos
13542 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13543 w->current_matrix));
13545 else if (last_text_row)
13547 w->window_end_bytepos
13548 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13549 w->window_end_pos
13550 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13551 w->window_end_vpos
13552 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13554 else
13556 /* This window must be completely empty. */
13557 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13558 w->window_end_pos = make_number (Z - ZV);
13559 w->window_end_vpos = make_number (0);
13561 w->window_end_valid = Qnil;
13563 /* Update hint: don't try scrolling again in update_window. */
13564 w->desired_matrix->no_scrolling_p = 1;
13566 #if GLYPH_DEBUG
13567 debug_method_add (w, "try_window_reusing_current_matrix 1");
13568 #endif
13569 return 1;
13571 else if (CHARPOS (new_start) > CHARPOS (start))
13573 struct glyph_row *pt_row, *row;
13574 struct glyph_row *first_reusable_row;
13575 struct glyph_row *first_row_to_display;
13576 int dy;
13577 int yb = window_text_bottom_y (w);
13579 /* Find the row starting at new_start, if there is one. Don't
13580 reuse a partially visible line at the end. */
13581 first_reusable_row = start_row;
13582 while (first_reusable_row->enabled_p
13583 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13584 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13585 < CHARPOS (new_start)))
13586 ++first_reusable_row;
13588 /* Give up if there is no row to reuse. */
13589 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13590 || !first_reusable_row->enabled_p
13591 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13592 != CHARPOS (new_start)))
13593 return 0;
13595 /* We can reuse fully visible rows beginning with
13596 first_reusable_row to the end of the window. Set
13597 first_row_to_display to the first row that cannot be reused.
13598 Set pt_row to the row containing point, if there is any. */
13599 pt_row = NULL;
13600 for (first_row_to_display = first_reusable_row;
13601 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13602 ++first_row_to_display)
13604 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13605 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13606 pt_row = first_row_to_display;
13609 /* Start displaying at the start of first_row_to_display. */
13610 xassert (first_row_to_display->y < yb);
13611 init_to_row_start (&it, w, first_row_to_display);
13613 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13614 - start_vpos);
13615 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13616 - nrows_scrolled);
13617 it.current_y = (first_row_to_display->y - first_reusable_row->y
13618 + WINDOW_HEADER_LINE_HEIGHT (w));
13620 /* Display lines beginning with first_row_to_display in the
13621 desired matrix. Set last_text_row to the last row displayed
13622 that displays text. */
13623 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13624 if (pt_row == NULL)
13625 w->cursor.vpos = -1;
13626 last_text_row = NULL;
13627 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13628 if (display_line (&it))
13629 last_text_row = it.glyph_row - 1;
13631 /* Give up If point isn't in a row displayed or reused. */
13632 if (w->cursor.vpos < 0)
13634 clear_glyph_matrix (w->desired_matrix);
13635 return 0;
13638 /* If point is in a reused row, adjust y and vpos of the cursor
13639 position. */
13640 if (pt_row)
13642 w->cursor.vpos -= nrows_scrolled;
13643 w->cursor.y -= first_reusable_row->y - start_row->y;
13646 /* Scroll the display. */
13647 run.current_y = first_reusable_row->y;
13648 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13649 run.height = it.last_visible_y - run.current_y;
13650 dy = run.current_y - run.desired_y;
13652 if (run.height)
13654 update_begin (f);
13655 FRAME_RIF (f)->update_window_begin_hook (w);
13656 FRAME_RIF (f)->clear_window_mouse_face (w);
13657 FRAME_RIF (f)->scroll_run_hook (w, &run);
13658 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13659 update_end (f);
13662 /* Adjust Y positions of reused rows. */
13663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13664 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13665 max_y = it.last_visible_y;
13666 for (row = first_reusable_row; row < first_row_to_display; ++row)
13668 row->y -= dy;
13669 row->visible_height = row->height;
13670 if (row->y < min_y)
13671 row->visible_height -= min_y - row->y;
13672 if (row->y + row->height > max_y)
13673 row->visible_height -= row->y + row->height - max_y;
13674 row->redraw_fringe_bitmaps_p = 1;
13677 /* Scroll the current matrix. */
13678 xassert (nrows_scrolled > 0);
13679 rotate_matrix (w->current_matrix,
13680 start_vpos,
13681 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13682 -nrows_scrolled);
13684 /* Disable rows not reused. */
13685 for (row -= nrows_scrolled; row < bottom_row; ++row)
13686 row->enabled_p = 0;
13688 /* Point may have moved to a different line, so we cannot assume that
13689 the previous cursor position is valid; locate the correct row. */
13690 if (pt_row)
13692 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13693 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13694 row++)
13696 w->cursor.vpos++;
13697 w->cursor.y = row->y;
13699 if (row < bottom_row)
13701 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13702 while (glyph->charpos < PT)
13704 w->cursor.hpos++;
13705 w->cursor.x += glyph->pixel_width;
13706 glyph++;
13711 /* Adjust window end. A null value of last_text_row means that
13712 the window end is in reused rows which in turn means that
13713 only its vpos can have changed. */
13714 if (last_text_row)
13716 w->window_end_bytepos
13717 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13718 w->window_end_pos
13719 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13720 w->window_end_vpos
13721 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13723 else
13725 w->window_end_vpos
13726 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13729 w->window_end_valid = Qnil;
13730 w->desired_matrix->no_scrolling_p = 1;
13732 #if GLYPH_DEBUG
13733 debug_method_add (w, "try_window_reusing_current_matrix 2");
13734 #endif
13735 return 1;
13738 return 0;
13743 /************************************************************************
13744 Window redisplay reusing current matrix when buffer has changed
13745 ************************************************************************/
13747 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13748 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13749 int *, int *));
13750 static struct glyph_row *
13751 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13752 struct glyph_row *));
13755 /* Return the last row in MATRIX displaying text. If row START is
13756 non-null, start searching with that row. IT gives the dimensions
13757 of the display. Value is null if matrix is empty; otherwise it is
13758 a pointer to the row found. */
13760 static struct glyph_row *
13761 find_last_row_displaying_text (matrix, it, start)
13762 struct glyph_matrix *matrix;
13763 struct it *it;
13764 struct glyph_row *start;
13766 struct glyph_row *row, *row_found;
13768 /* Set row_found to the last row in IT->w's current matrix
13769 displaying text. The loop looks funny but think of partially
13770 visible lines. */
13771 row_found = NULL;
13772 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13773 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13775 xassert (row->enabled_p);
13776 row_found = row;
13777 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13778 break;
13779 ++row;
13782 return row_found;
13786 /* Return the last row in the current matrix of W that is not affected
13787 by changes at the start of current_buffer that occurred since W's
13788 current matrix was built. Value is null if no such row exists.
13790 BEG_UNCHANGED us the number of characters unchanged at the start of
13791 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13792 first changed character in current_buffer. Characters at positions <
13793 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13794 when the current matrix was built. */
13796 static struct glyph_row *
13797 find_last_unchanged_at_beg_row (w)
13798 struct window *w;
13800 int first_changed_pos = BEG + BEG_UNCHANGED;
13801 struct glyph_row *row;
13802 struct glyph_row *row_found = NULL;
13803 int yb = window_text_bottom_y (w);
13805 /* Find the last row displaying unchanged text. */
13806 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13807 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13808 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13810 if (/* If row ends before first_changed_pos, it is unchanged,
13811 except in some case. */
13812 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13813 /* When row ends in ZV and we write at ZV it is not
13814 unchanged. */
13815 && !row->ends_at_zv_p
13816 /* When first_changed_pos is the end of a continued line,
13817 row is not unchanged because it may be no longer
13818 continued. */
13819 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13820 && (row->continued_p
13821 || row->exact_window_width_line_p)))
13822 row_found = row;
13824 /* Stop if last visible row. */
13825 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13826 break;
13828 ++row;
13831 return row_found;
13835 /* Find the first glyph row in the current matrix of W that is not
13836 affected by changes at the end of current_buffer since the
13837 time W's current matrix was built.
13839 Return in *DELTA the number of chars by which buffer positions in
13840 unchanged text at the end of current_buffer must be adjusted.
13842 Return in *DELTA_BYTES the corresponding number of bytes.
13844 Value is null if no such row exists, i.e. all rows are affected by
13845 changes. */
13847 static struct glyph_row *
13848 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13849 struct window *w;
13850 int *delta, *delta_bytes;
13852 struct glyph_row *row;
13853 struct glyph_row *row_found = NULL;
13855 *delta = *delta_bytes = 0;
13857 /* Display must not have been paused, otherwise the current matrix
13858 is not up to date. */
13859 if (NILP (w->window_end_valid))
13860 abort ();
13862 /* A value of window_end_pos >= END_UNCHANGED means that the window
13863 end is in the range of changed text. If so, there is no
13864 unchanged row at the end of W's current matrix. */
13865 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13866 return NULL;
13868 /* Set row to the last row in W's current matrix displaying text. */
13869 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13871 /* If matrix is entirely empty, no unchanged row exists. */
13872 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13874 /* The value of row is the last glyph row in the matrix having a
13875 meaningful buffer position in it. The end position of row
13876 corresponds to window_end_pos. This allows us to translate
13877 buffer positions in the current matrix to current buffer
13878 positions for characters not in changed text. */
13879 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13880 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13881 int last_unchanged_pos, last_unchanged_pos_old;
13882 struct glyph_row *first_text_row
13883 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13885 *delta = Z - Z_old;
13886 *delta_bytes = Z_BYTE - Z_BYTE_old;
13888 /* Set last_unchanged_pos to the buffer position of the last
13889 character in the buffer that has not been changed. Z is the
13890 index + 1 of the last character in current_buffer, i.e. by
13891 subtracting END_UNCHANGED we get the index of the last
13892 unchanged character, and we have to add BEG to get its buffer
13893 position. */
13894 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13895 last_unchanged_pos_old = last_unchanged_pos - *delta;
13897 /* Search backward from ROW for a row displaying a line that
13898 starts at a minimum position >= last_unchanged_pos_old. */
13899 for (; row > first_text_row; --row)
13901 /* This used to abort, but it can happen.
13902 It is ok to just stop the search instead here. KFS. */
13903 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13904 break;
13906 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13907 row_found = row;
13911 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13912 abort ();
13914 return row_found;
13918 /* Make sure that glyph rows in the current matrix of window W
13919 reference the same glyph memory as corresponding rows in the
13920 frame's frame matrix. This function is called after scrolling W's
13921 current matrix on a terminal frame in try_window_id and
13922 try_window_reusing_current_matrix. */
13924 static void
13925 sync_frame_with_window_matrix_rows (w)
13926 struct window *w;
13928 struct frame *f = XFRAME (w->frame);
13929 struct glyph_row *window_row, *window_row_end, *frame_row;
13931 /* Preconditions: W must be a leaf window and full-width. Its frame
13932 must have a frame matrix. */
13933 xassert (NILP (w->hchild) && NILP (w->vchild));
13934 xassert (WINDOW_FULL_WIDTH_P (w));
13935 xassert (!FRAME_WINDOW_P (f));
13937 /* If W is a full-width window, glyph pointers in W's current matrix
13938 have, by definition, to be the same as glyph pointers in the
13939 corresponding frame matrix. Note that frame matrices have no
13940 marginal areas (see build_frame_matrix). */
13941 window_row = w->current_matrix->rows;
13942 window_row_end = window_row + w->current_matrix->nrows;
13943 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13944 while (window_row < window_row_end)
13946 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13947 struct glyph *end = window_row->glyphs[LAST_AREA];
13949 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13950 frame_row->glyphs[TEXT_AREA] = start;
13951 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13952 frame_row->glyphs[LAST_AREA] = end;
13954 /* Disable frame rows whose corresponding window rows have
13955 been disabled in try_window_id. */
13956 if (!window_row->enabled_p)
13957 frame_row->enabled_p = 0;
13959 ++window_row, ++frame_row;
13964 /* Find the glyph row in window W containing CHARPOS. Consider all
13965 rows between START and END (not inclusive). END null means search
13966 all rows to the end of the display area of W. Value is the row
13967 containing CHARPOS or null. */
13969 struct glyph_row *
13970 row_containing_pos (w, charpos, start, end, dy)
13971 struct window *w;
13972 int charpos;
13973 struct glyph_row *start, *end;
13974 int dy;
13976 struct glyph_row *row = start;
13977 int last_y;
13979 /* If we happen to start on a header-line, skip that. */
13980 if (row->mode_line_p)
13981 ++row;
13983 if ((end && row >= end) || !row->enabled_p)
13984 return NULL;
13986 last_y = window_text_bottom_y (w) - dy;
13988 while (1)
13990 /* Give up if we have gone too far. */
13991 if (end && row >= end)
13992 return NULL;
13993 /* This formerly returned if they were equal.
13994 I think that both quantities are of a "last plus one" type;
13995 if so, when they are equal, the row is within the screen. -- rms. */
13996 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13997 return NULL;
13999 /* If it is in this row, return this row. */
14000 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14001 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14002 /* The end position of a row equals the start
14003 position of the next row. If CHARPOS is there, we
14004 would rather display it in the next line, except
14005 when this line ends in ZV. */
14006 && !row->ends_at_zv_p
14007 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14008 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14009 return row;
14010 ++row;
14015 /* Try to redisplay window W by reusing its existing display. W's
14016 current matrix must be up to date when this function is called,
14017 i.e. window_end_valid must not be nil.
14019 Value is
14021 1 if display has been updated
14022 0 if otherwise unsuccessful
14023 -1 if redisplay with same window start is known not to succeed
14025 The following steps are performed:
14027 1. Find the last row in the current matrix of W that is not
14028 affected by changes at the start of current_buffer. If no such row
14029 is found, give up.
14031 2. Find the first row in W's current matrix that is not affected by
14032 changes at the end of current_buffer. Maybe there is no such row.
14034 3. Display lines beginning with the row + 1 found in step 1 to the
14035 row found in step 2 or, if step 2 didn't find a row, to the end of
14036 the window.
14038 4. If cursor is not known to appear on the window, give up.
14040 5. If display stopped at the row found in step 2, scroll the
14041 display and current matrix as needed.
14043 6. Maybe display some lines at the end of W, if we must. This can
14044 happen under various circumstances, like a partially visible line
14045 becoming fully visible, or because newly displayed lines are displayed
14046 in smaller font sizes.
14048 7. Update W's window end information. */
14050 static int
14051 try_window_id (w)
14052 struct window *w;
14054 struct frame *f = XFRAME (w->frame);
14055 struct glyph_matrix *current_matrix = w->current_matrix;
14056 struct glyph_matrix *desired_matrix = w->desired_matrix;
14057 struct glyph_row *last_unchanged_at_beg_row;
14058 struct glyph_row *first_unchanged_at_end_row;
14059 struct glyph_row *row;
14060 struct glyph_row *bottom_row;
14061 int bottom_vpos;
14062 struct it it;
14063 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14064 struct text_pos start_pos;
14065 struct run run;
14066 int first_unchanged_at_end_vpos = 0;
14067 struct glyph_row *last_text_row, *last_text_row_at_end;
14068 struct text_pos start;
14069 int first_changed_charpos, last_changed_charpos;
14071 #if GLYPH_DEBUG
14072 if (inhibit_try_window_id)
14073 return 0;
14074 #endif
14076 /* This is handy for debugging. */
14077 #if 0
14078 #define GIVE_UP(X) \
14079 do { \
14080 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14081 return 0; \
14082 } while (0)
14083 #else
14084 #define GIVE_UP(X) return 0
14085 #endif
14087 SET_TEXT_POS_FROM_MARKER (start, w->start);
14089 /* Don't use this for mini-windows because these can show
14090 messages and mini-buffers, and we don't handle that here. */
14091 if (MINI_WINDOW_P (w))
14092 GIVE_UP (1);
14094 /* This flag is used to prevent redisplay optimizations. */
14095 if (windows_or_buffers_changed || cursor_type_changed)
14096 GIVE_UP (2);
14098 /* Verify that narrowing has not changed.
14099 Also verify that we were not told to prevent redisplay optimizations.
14100 It would be nice to further
14101 reduce the number of cases where this prevents try_window_id. */
14102 if (current_buffer->clip_changed
14103 || current_buffer->prevent_redisplay_optimizations_p)
14104 GIVE_UP (3);
14106 /* Window must either use window-based redisplay or be full width. */
14107 if (!FRAME_WINDOW_P (f)
14108 && (!FRAME_LINE_INS_DEL_OK (f)
14109 || !WINDOW_FULL_WIDTH_P (w)))
14110 GIVE_UP (4);
14112 /* Give up if point is not known NOT to appear in W. */
14113 if (PT < CHARPOS (start))
14114 GIVE_UP (5);
14116 /* Another way to prevent redisplay optimizations. */
14117 if (XFASTINT (w->last_modified) == 0)
14118 GIVE_UP (6);
14120 /* Verify that window is not hscrolled. */
14121 if (XFASTINT (w->hscroll) != 0)
14122 GIVE_UP (7);
14124 /* Verify that display wasn't paused. */
14125 if (NILP (w->window_end_valid))
14126 GIVE_UP (8);
14128 /* Can't use this if highlighting a region because a cursor movement
14129 will do more than just set the cursor. */
14130 if (!NILP (Vtransient_mark_mode)
14131 && !NILP (current_buffer->mark_active))
14132 GIVE_UP (9);
14134 /* Likewise if highlighting trailing whitespace. */
14135 if (!NILP (Vshow_trailing_whitespace))
14136 GIVE_UP (11);
14138 /* Likewise if showing a region. */
14139 if (!NILP (w->region_showing))
14140 GIVE_UP (10);
14142 /* Can use this if overlay arrow position and or string have changed. */
14143 if (overlay_arrows_changed_p ())
14144 GIVE_UP (12);
14147 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14148 only if buffer has really changed. The reason is that the gap is
14149 initially at Z for freshly visited files. The code below would
14150 set end_unchanged to 0 in that case. */
14151 if (MODIFF > SAVE_MODIFF
14152 /* This seems to happen sometimes after saving a buffer. */
14153 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14155 if (GPT - BEG < BEG_UNCHANGED)
14156 BEG_UNCHANGED = GPT - BEG;
14157 if (Z - GPT < END_UNCHANGED)
14158 END_UNCHANGED = Z - GPT;
14161 /* The position of the first and last character that has been changed. */
14162 first_changed_charpos = BEG + BEG_UNCHANGED;
14163 last_changed_charpos = Z - END_UNCHANGED;
14165 /* If window starts after a line end, and the last change is in
14166 front of that newline, then changes don't affect the display.
14167 This case happens with stealth-fontification. Note that although
14168 the display is unchanged, glyph positions in the matrix have to
14169 be adjusted, of course. */
14170 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14171 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14172 && ((last_changed_charpos < CHARPOS (start)
14173 && CHARPOS (start) == BEGV)
14174 || (last_changed_charpos < CHARPOS (start) - 1
14175 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14177 int Z_old, delta, Z_BYTE_old, delta_bytes;
14178 struct glyph_row *r0;
14180 /* Compute how many chars/bytes have been added to or removed
14181 from the buffer. */
14182 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14183 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14184 delta = Z - Z_old;
14185 delta_bytes = Z_BYTE - Z_BYTE_old;
14187 /* Give up if PT is not in the window. Note that it already has
14188 been checked at the start of try_window_id that PT is not in
14189 front of the window start. */
14190 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14191 GIVE_UP (13);
14193 /* If window start is unchanged, we can reuse the whole matrix
14194 as is, after adjusting glyph positions. No need to compute
14195 the window end again, since its offset from Z hasn't changed. */
14196 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14197 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14198 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14199 /* PT must not be in a partially visible line. */
14200 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14201 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14203 /* Adjust positions in the glyph matrix. */
14204 if (delta || delta_bytes)
14206 struct glyph_row *r1
14207 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14208 increment_matrix_positions (w->current_matrix,
14209 MATRIX_ROW_VPOS (r0, current_matrix),
14210 MATRIX_ROW_VPOS (r1, current_matrix),
14211 delta, delta_bytes);
14214 /* Set the cursor. */
14215 row = row_containing_pos (w, PT, r0, NULL, 0);
14216 if (row)
14217 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14218 else
14219 abort ();
14220 return 1;
14224 /* Handle the case that changes are all below what is displayed in
14225 the window, and that PT is in the window. This shortcut cannot
14226 be taken if ZV is visible in the window, and text has been added
14227 there that is visible in the window. */
14228 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14229 /* ZV is not visible in the window, or there are no
14230 changes at ZV, actually. */
14231 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14232 || first_changed_charpos == last_changed_charpos))
14234 struct glyph_row *r0;
14236 /* Give up if PT is not in the window. Note that it already has
14237 been checked at the start of try_window_id that PT is not in
14238 front of the window start. */
14239 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14240 GIVE_UP (14);
14242 /* If window start is unchanged, we can reuse the whole matrix
14243 as is, without changing glyph positions since no text has
14244 been added/removed in front of the window end. */
14245 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14246 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14247 /* PT must not be in a partially visible line. */
14248 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14249 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14251 /* We have to compute the window end anew since text
14252 can have been added/removed after it. */
14253 w->window_end_pos
14254 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14255 w->window_end_bytepos
14256 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14258 /* Set the cursor. */
14259 row = row_containing_pos (w, PT, r0, NULL, 0);
14260 if (row)
14261 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14262 else
14263 abort ();
14264 return 2;
14268 /* Give up if window start is in the changed area.
14270 The condition used to read
14272 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14274 but why that was tested escapes me at the moment. */
14275 if (CHARPOS (start) >= first_changed_charpos
14276 && CHARPOS (start) <= last_changed_charpos)
14277 GIVE_UP (15);
14279 /* Check that window start agrees with the start of the first glyph
14280 row in its current matrix. Check this after we know the window
14281 start is not in changed text, otherwise positions would not be
14282 comparable. */
14283 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14284 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14285 GIVE_UP (16);
14287 /* Give up if the window ends in strings. Overlay strings
14288 at the end are difficult to handle, so don't try. */
14289 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14290 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14291 GIVE_UP (20);
14293 /* Compute the position at which we have to start displaying new
14294 lines. Some of the lines at the top of the window might be
14295 reusable because they are not displaying changed text. Find the
14296 last row in W's current matrix not affected by changes at the
14297 start of current_buffer. Value is null if changes start in the
14298 first line of window. */
14299 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14300 if (last_unchanged_at_beg_row)
14302 /* Avoid starting to display in the moddle of a character, a TAB
14303 for instance. This is easier than to set up the iterator
14304 exactly, and it's not a frequent case, so the additional
14305 effort wouldn't really pay off. */
14306 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14307 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14308 && last_unchanged_at_beg_row > w->current_matrix->rows)
14309 --last_unchanged_at_beg_row;
14311 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14312 GIVE_UP (17);
14314 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14315 GIVE_UP (18);
14316 start_pos = it.current.pos;
14318 /* Start displaying new lines in the desired matrix at the same
14319 vpos we would use in the current matrix, i.e. below
14320 last_unchanged_at_beg_row. */
14321 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14322 current_matrix);
14323 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14324 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14326 xassert (it.hpos == 0 && it.current_x == 0);
14328 else
14330 /* There are no reusable lines at the start of the window.
14331 Start displaying in the first text line. */
14332 start_display (&it, w, start);
14333 it.vpos = it.first_vpos;
14334 start_pos = it.current.pos;
14337 /* Find the first row that is not affected by changes at the end of
14338 the buffer. Value will be null if there is no unchanged row, in
14339 which case we must redisplay to the end of the window. delta
14340 will be set to the value by which buffer positions beginning with
14341 first_unchanged_at_end_row have to be adjusted due to text
14342 changes. */
14343 first_unchanged_at_end_row
14344 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14345 IF_DEBUG (debug_delta = delta);
14346 IF_DEBUG (debug_delta_bytes = delta_bytes);
14348 /* Set stop_pos to the buffer position up to which we will have to
14349 display new lines. If first_unchanged_at_end_row != NULL, this
14350 is the buffer position of the start of the line displayed in that
14351 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14352 that we don't stop at a buffer position. */
14353 stop_pos = 0;
14354 if (first_unchanged_at_end_row)
14356 xassert (last_unchanged_at_beg_row == NULL
14357 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14359 /* If this is a continuation line, move forward to the next one
14360 that isn't. Changes in lines above affect this line.
14361 Caution: this may move first_unchanged_at_end_row to a row
14362 not displaying text. */
14363 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14364 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14365 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14366 < it.last_visible_y))
14367 ++first_unchanged_at_end_row;
14369 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14370 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14371 >= it.last_visible_y))
14372 first_unchanged_at_end_row = NULL;
14373 else
14375 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14376 + delta);
14377 first_unchanged_at_end_vpos
14378 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14379 xassert (stop_pos >= Z - END_UNCHANGED);
14382 else if (last_unchanged_at_beg_row == NULL)
14383 GIVE_UP (19);
14386 #if GLYPH_DEBUG
14388 /* Either there is no unchanged row at the end, or the one we have
14389 now displays text. This is a necessary condition for the window
14390 end pos calculation at the end of this function. */
14391 xassert (first_unchanged_at_end_row == NULL
14392 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14394 debug_last_unchanged_at_beg_vpos
14395 = (last_unchanged_at_beg_row
14396 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14397 : -1);
14398 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14400 #endif /* GLYPH_DEBUG != 0 */
14403 /* Display new lines. Set last_text_row to the last new line
14404 displayed which has text on it, i.e. might end up as being the
14405 line where the window_end_vpos is. */
14406 w->cursor.vpos = -1;
14407 last_text_row = NULL;
14408 overlay_arrow_seen = 0;
14409 while (it.current_y < it.last_visible_y
14410 && !fonts_changed_p
14411 && (first_unchanged_at_end_row == NULL
14412 || IT_CHARPOS (it) < stop_pos))
14414 if (display_line (&it))
14415 last_text_row = it.glyph_row - 1;
14418 if (fonts_changed_p)
14419 return -1;
14422 /* Compute differences in buffer positions, y-positions etc. for
14423 lines reused at the bottom of the window. Compute what we can
14424 scroll. */
14425 if (first_unchanged_at_end_row
14426 /* No lines reused because we displayed everything up to the
14427 bottom of the window. */
14428 && it.current_y < it.last_visible_y)
14430 dvpos = (it.vpos
14431 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14432 current_matrix));
14433 dy = it.current_y - first_unchanged_at_end_row->y;
14434 run.current_y = first_unchanged_at_end_row->y;
14435 run.desired_y = run.current_y + dy;
14436 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14438 else
14440 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14441 first_unchanged_at_end_row = NULL;
14443 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14446 /* Find the cursor if not already found. We have to decide whether
14447 PT will appear on this window (it sometimes doesn't, but this is
14448 not a very frequent case.) This decision has to be made before
14449 the current matrix is altered. A value of cursor.vpos < 0 means
14450 that PT is either in one of the lines beginning at
14451 first_unchanged_at_end_row or below the window. Don't care for
14452 lines that might be displayed later at the window end; as
14453 mentioned, this is not a frequent case. */
14454 if (w->cursor.vpos < 0)
14456 /* Cursor in unchanged rows at the top? */
14457 if (PT < CHARPOS (start_pos)
14458 && last_unchanged_at_beg_row)
14460 row = row_containing_pos (w, PT,
14461 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14462 last_unchanged_at_beg_row + 1, 0);
14463 if (row)
14464 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14467 /* Start from first_unchanged_at_end_row looking for PT. */
14468 else if (first_unchanged_at_end_row)
14470 row = row_containing_pos (w, PT - delta,
14471 first_unchanged_at_end_row, NULL, 0);
14472 if (row)
14473 set_cursor_from_row (w, row, w->current_matrix, delta,
14474 delta_bytes, dy, dvpos);
14477 /* Give up if cursor was not found. */
14478 if (w->cursor.vpos < 0)
14480 clear_glyph_matrix (w->desired_matrix);
14481 return -1;
14485 /* Don't let the cursor end in the scroll margins. */
14487 int this_scroll_margin, cursor_height;
14489 this_scroll_margin = max (0, scroll_margin);
14490 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14491 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14492 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14494 if ((w->cursor.y < this_scroll_margin
14495 && CHARPOS (start) > BEGV)
14496 /* Old redisplay didn't take scroll margin into account at the bottom,
14497 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14498 || (w->cursor.y + (make_cursor_line_fully_visible_p
14499 ? cursor_height + this_scroll_margin
14500 : 1)) > it.last_visible_y)
14502 w->cursor.vpos = -1;
14503 clear_glyph_matrix (w->desired_matrix);
14504 return -1;
14508 /* Scroll the display. Do it before changing the current matrix so
14509 that xterm.c doesn't get confused about where the cursor glyph is
14510 found. */
14511 if (dy && run.height)
14513 update_begin (f);
14515 if (FRAME_WINDOW_P (f))
14517 FRAME_RIF (f)->update_window_begin_hook (w);
14518 FRAME_RIF (f)->clear_window_mouse_face (w);
14519 FRAME_RIF (f)->scroll_run_hook (w, &run);
14520 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14522 else
14524 /* Terminal frame. In this case, dvpos gives the number of
14525 lines to scroll by; dvpos < 0 means scroll up. */
14526 int first_unchanged_at_end_vpos
14527 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14528 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14529 int end = (WINDOW_TOP_EDGE_LINE (w)
14530 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14531 + window_internal_height (w));
14533 /* Perform the operation on the screen. */
14534 if (dvpos > 0)
14536 /* Scroll last_unchanged_at_beg_row to the end of the
14537 window down dvpos lines. */
14538 set_terminal_window (f, end);
14540 /* On dumb terminals delete dvpos lines at the end
14541 before inserting dvpos empty lines. */
14542 if (!FRAME_SCROLL_REGION_OK (f))
14543 ins_del_lines (f, end - dvpos, -dvpos);
14545 /* Insert dvpos empty lines in front of
14546 last_unchanged_at_beg_row. */
14547 ins_del_lines (f, from, dvpos);
14549 else if (dvpos < 0)
14551 /* Scroll up last_unchanged_at_beg_vpos to the end of
14552 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14553 set_terminal_window (f, end);
14555 /* Delete dvpos lines in front of
14556 last_unchanged_at_beg_vpos. ins_del_lines will set
14557 the cursor to the given vpos and emit |dvpos| delete
14558 line sequences. */
14559 ins_del_lines (f, from + dvpos, dvpos);
14561 /* On a dumb terminal insert dvpos empty lines at the
14562 end. */
14563 if (!FRAME_SCROLL_REGION_OK (f))
14564 ins_del_lines (f, end + dvpos, -dvpos);
14567 set_terminal_window (f, 0);
14570 update_end (f);
14573 /* Shift reused rows of the current matrix to the right position.
14574 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14575 text. */
14576 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14577 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14578 if (dvpos < 0)
14580 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14581 bottom_vpos, dvpos);
14582 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14583 bottom_vpos, 0);
14585 else if (dvpos > 0)
14587 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14588 bottom_vpos, dvpos);
14589 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14590 first_unchanged_at_end_vpos + dvpos, 0);
14593 /* For frame-based redisplay, make sure that current frame and window
14594 matrix are in sync with respect to glyph memory. */
14595 if (!FRAME_WINDOW_P (f))
14596 sync_frame_with_window_matrix_rows (w);
14598 /* Adjust buffer positions in reused rows. */
14599 if (delta)
14600 increment_matrix_positions (current_matrix,
14601 first_unchanged_at_end_vpos + dvpos,
14602 bottom_vpos, delta, delta_bytes);
14604 /* Adjust Y positions. */
14605 if (dy)
14606 shift_glyph_matrix (w, current_matrix,
14607 first_unchanged_at_end_vpos + dvpos,
14608 bottom_vpos, dy);
14610 if (first_unchanged_at_end_row)
14612 first_unchanged_at_end_row += dvpos;
14613 if (first_unchanged_at_end_row->y >= it.last_visible_y
14614 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14615 first_unchanged_at_end_row = NULL;
14618 /* If scrolling up, there may be some lines to display at the end of
14619 the window. */
14620 last_text_row_at_end = NULL;
14621 if (dy < 0)
14623 /* Scrolling up can leave for example a partially visible line
14624 at the end of the window to be redisplayed. */
14625 /* Set last_row to the glyph row in the current matrix where the
14626 window end line is found. It has been moved up or down in
14627 the matrix by dvpos. */
14628 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14629 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14631 /* If last_row is the window end line, it should display text. */
14632 xassert (last_row->displays_text_p);
14634 /* If window end line was partially visible before, begin
14635 displaying at that line. Otherwise begin displaying with the
14636 line following it. */
14637 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14639 init_to_row_start (&it, w, last_row);
14640 it.vpos = last_vpos;
14641 it.current_y = last_row->y;
14643 else
14645 init_to_row_end (&it, w, last_row);
14646 it.vpos = 1 + last_vpos;
14647 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14648 ++last_row;
14651 /* We may start in a continuation line. If so, we have to
14652 get the right continuation_lines_width and current_x. */
14653 it.continuation_lines_width = last_row->continuation_lines_width;
14654 it.hpos = it.current_x = 0;
14656 /* Display the rest of the lines at the window end. */
14657 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14658 while (it.current_y < it.last_visible_y
14659 && !fonts_changed_p)
14661 /* Is it always sure that the display agrees with lines in
14662 the current matrix? I don't think so, so we mark rows
14663 displayed invalid in the current matrix by setting their
14664 enabled_p flag to zero. */
14665 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14666 if (display_line (&it))
14667 last_text_row_at_end = it.glyph_row - 1;
14671 /* Update window_end_pos and window_end_vpos. */
14672 if (first_unchanged_at_end_row
14673 && !last_text_row_at_end)
14675 /* Window end line if one of the preserved rows from the current
14676 matrix. Set row to the last row displaying text in current
14677 matrix starting at first_unchanged_at_end_row, after
14678 scrolling. */
14679 xassert (first_unchanged_at_end_row->displays_text_p);
14680 row = find_last_row_displaying_text (w->current_matrix, &it,
14681 first_unchanged_at_end_row);
14682 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14684 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14685 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14686 w->window_end_vpos
14687 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14688 xassert (w->window_end_bytepos >= 0);
14689 IF_DEBUG (debug_method_add (w, "A"));
14691 else if (last_text_row_at_end)
14693 w->window_end_pos
14694 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14695 w->window_end_bytepos
14696 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14697 w->window_end_vpos
14698 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14699 xassert (w->window_end_bytepos >= 0);
14700 IF_DEBUG (debug_method_add (w, "B"));
14702 else if (last_text_row)
14704 /* We have displayed either to the end of the window or at the
14705 end of the window, i.e. the last row with text is to be found
14706 in the desired matrix. */
14707 w->window_end_pos
14708 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14709 w->window_end_bytepos
14710 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14711 w->window_end_vpos
14712 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14713 xassert (w->window_end_bytepos >= 0);
14715 else if (first_unchanged_at_end_row == NULL
14716 && last_text_row == NULL
14717 && last_text_row_at_end == NULL)
14719 /* Displayed to end of window, but no line containing text was
14720 displayed. Lines were deleted at the end of the window. */
14721 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14722 int vpos = XFASTINT (w->window_end_vpos);
14723 struct glyph_row *current_row = current_matrix->rows + vpos;
14724 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14726 for (row = NULL;
14727 row == NULL && vpos >= first_vpos;
14728 --vpos, --current_row, --desired_row)
14730 if (desired_row->enabled_p)
14732 if (desired_row->displays_text_p)
14733 row = desired_row;
14735 else if (current_row->displays_text_p)
14736 row = current_row;
14739 xassert (row != NULL);
14740 w->window_end_vpos = make_number (vpos + 1);
14741 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14742 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14743 xassert (w->window_end_bytepos >= 0);
14744 IF_DEBUG (debug_method_add (w, "C"));
14746 else
14747 abort ();
14749 #if 0 /* This leads to problems, for instance when the cursor is
14750 at ZV, and the cursor line displays no text. */
14751 /* Disable rows below what's displayed in the window. This makes
14752 debugging easier. */
14753 enable_glyph_matrix_rows (current_matrix,
14754 XFASTINT (w->window_end_vpos) + 1,
14755 bottom_vpos, 0);
14756 #endif
14758 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14759 debug_end_vpos = XFASTINT (w->window_end_vpos));
14761 /* Record that display has not been completed. */
14762 w->window_end_valid = Qnil;
14763 w->desired_matrix->no_scrolling_p = 1;
14764 return 3;
14766 #undef GIVE_UP
14771 /***********************************************************************
14772 More debugging support
14773 ***********************************************************************/
14775 #if GLYPH_DEBUG
14777 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14778 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14779 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14782 /* Dump the contents of glyph matrix MATRIX on stderr.
14784 GLYPHS 0 means don't show glyph contents.
14785 GLYPHS 1 means show glyphs in short form
14786 GLYPHS > 1 means show glyphs in long form. */
14788 void
14789 dump_glyph_matrix (matrix, glyphs)
14790 struct glyph_matrix *matrix;
14791 int glyphs;
14793 int i;
14794 for (i = 0; i < matrix->nrows; ++i)
14795 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14799 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14800 the glyph row and area where the glyph comes from. */
14802 void
14803 dump_glyph (row, glyph, area)
14804 struct glyph_row *row;
14805 struct glyph *glyph;
14806 int area;
14808 if (glyph->type == CHAR_GLYPH)
14810 fprintf (stderr,
14811 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14812 glyph - row->glyphs[TEXT_AREA],
14813 'C',
14814 glyph->charpos,
14815 (BUFFERP (glyph->object)
14816 ? 'B'
14817 : (STRINGP (glyph->object)
14818 ? 'S'
14819 : '-')),
14820 glyph->pixel_width,
14821 glyph->u.ch,
14822 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14823 ? glyph->u.ch
14824 : '.'),
14825 glyph->face_id,
14826 glyph->left_box_line_p,
14827 glyph->right_box_line_p);
14829 else if (glyph->type == STRETCH_GLYPH)
14831 fprintf (stderr,
14832 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14833 glyph - row->glyphs[TEXT_AREA],
14834 'S',
14835 glyph->charpos,
14836 (BUFFERP (glyph->object)
14837 ? 'B'
14838 : (STRINGP (glyph->object)
14839 ? 'S'
14840 : '-')),
14841 glyph->pixel_width,
14843 '.',
14844 glyph->face_id,
14845 glyph->left_box_line_p,
14846 glyph->right_box_line_p);
14848 else if (glyph->type == IMAGE_GLYPH)
14850 fprintf (stderr,
14851 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14852 glyph - row->glyphs[TEXT_AREA],
14853 'I',
14854 glyph->charpos,
14855 (BUFFERP (glyph->object)
14856 ? 'B'
14857 : (STRINGP (glyph->object)
14858 ? 'S'
14859 : '-')),
14860 glyph->pixel_width,
14861 glyph->u.img_id,
14862 '.',
14863 glyph->face_id,
14864 glyph->left_box_line_p,
14865 glyph->right_box_line_p);
14870 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14871 GLYPHS 0 means don't show glyph contents.
14872 GLYPHS 1 means show glyphs in short form
14873 GLYPHS > 1 means show glyphs in long form. */
14875 void
14876 dump_glyph_row (row, vpos, glyphs)
14877 struct glyph_row *row;
14878 int vpos, glyphs;
14880 if (glyphs != 1)
14882 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
14883 fprintf (stderr, "======================================================================\n");
14885 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14886 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14887 vpos,
14888 MATRIX_ROW_START_CHARPOS (row),
14889 MATRIX_ROW_END_CHARPOS (row),
14890 row->used[TEXT_AREA],
14891 row->contains_overlapping_glyphs_p,
14892 row->enabled_p,
14893 row->truncated_on_left_p,
14894 row->truncated_on_right_p,
14895 row->continued_p,
14896 MATRIX_ROW_CONTINUATION_LINE_P (row),
14897 row->displays_text_p,
14898 row->ends_at_zv_p,
14899 row->fill_line_p,
14900 row->ends_in_middle_of_char_p,
14901 row->starts_in_middle_of_char_p,
14902 row->mouse_face_p,
14903 row->x,
14904 row->y,
14905 row->pixel_width,
14906 row->height,
14907 row->visible_height,
14908 row->ascent,
14909 row->phys_ascent);
14910 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14911 row->end.overlay_string_index,
14912 row->continuation_lines_width);
14913 fprintf (stderr, "%9d %5d\n",
14914 CHARPOS (row->start.string_pos),
14915 CHARPOS (row->end.string_pos));
14916 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14917 row->end.dpvec_index);
14920 if (glyphs > 1)
14922 int area;
14924 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14926 struct glyph *glyph = row->glyphs[area];
14927 struct glyph *glyph_end = glyph + row->used[area];
14929 /* Glyph for a line end in text. */
14930 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14931 ++glyph_end;
14933 if (glyph < glyph_end)
14934 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14936 for (; glyph < glyph_end; ++glyph)
14937 dump_glyph (row, glyph, area);
14940 else if (glyphs == 1)
14942 int area;
14944 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14946 char *s = (char *) alloca (row->used[area] + 1);
14947 int i;
14949 for (i = 0; i < row->used[area]; ++i)
14951 struct glyph *glyph = row->glyphs[area] + i;
14952 if (glyph->type == CHAR_GLYPH
14953 && glyph->u.ch < 0x80
14954 && glyph->u.ch >= ' ')
14955 s[i] = glyph->u.ch;
14956 else
14957 s[i] = '.';
14960 s[i] = '\0';
14961 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14967 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14968 Sdump_glyph_matrix, 0, 1, "p",
14969 doc: /* Dump the current matrix of the selected window to stderr.
14970 Shows contents of glyph row structures. With non-nil
14971 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14972 glyphs in short form, otherwise show glyphs in long form. */)
14973 (glyphs)
14974 Lisp_Object glyphs;
14976 struct window *w = XWINDOW (selected_window);
14977 struct buffer *buffer = XBUFFER (w->buffer);
14979 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14980 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14981 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14982 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14983 fprintf (stderr, "=============================================\n");
14984 dump_glyph_matrix (w->current_matrix,
14985 NILP (glyphs) ? 0 : XINT (glyphs));
14986 return Qnil;
14990 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14991 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14994 struct frame *f = XFRAME (selected_frame);
14995 dump_glyph_matrix (f->current_matrix, 1);
14996 return Qnil;
15000 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15001 doc: /* Dump glyph row ROW to stderr.
15002 GLYPH 0 means don't dump glyphs.
15003 GLYPH 1 means dump glyphs in short form.
15004 GLYPH > 1 or omitted means dump glyphs in long form. */)
15005 (row, glyphs)
15006 Lisp_Object row, glyphs;
15008 struct glyph_matrix *matrix;
15009 int vpos;
15011 CHECK_NUMBER (row);
15012 matrix = XWINDOW (selected_window)->current_matrix;
15013 vpos = XINT (row);
15014 if (vpos >= 0 && vpos < matrix->nrows)
15015 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15016 vpos,
15017 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15018 return Qnil;
15022 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15023 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15024 GLYPH 0 means don't dump glyphs.
15025 GLYPH 1 means dump glyphs in short form.
15026 GLYPH > 1 or omitted means dump glyphs in long form. */)
15027 (row, glyphs)
15028 Lisp_Object row, glyphs;
15030 struct frame *sf = SELECTED_FRAME ();
15031 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15032 int vpos;
15034 CHECK_NUMBER (row);
15035 vpos = XINT (row);
15036 if (vpos >= 0 && vpos < m->nrows)
15037 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15038 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15039 return Qnil;
15043 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15044 doc: /* Toggle tracing of redisplay.
15045 With ARG, turn tracing on if and only if ARG is positive. */)
15046 (arg)
15047 Lisp_Object arg;
15049 if (NILP (arg))
15050 trace_redisplay_p = !trace_redisplay_p;
15051 else
15053 arg = Fprefix_numeric_value (arg);
15054 trace_redisplay_p = XINT (arg) > 0;
15057 return Qnil;
15061 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15062 doc: /* Like `format', but print result to stderr.
15063 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15064 (nargs, args)
15065 int nargs;
15066 Lisp_Object *args;
15068 Lisp_Object s = Fformat (nargs, args);
15069 fprintf (stderr, "%s", SDATA (s));
15070 return Qnil;
15073 #endif /* GLYPH_DEBUG */
15077 /***********************************************************************
15078 Building Desired Matrix Rows
15079 ***********************************************************************/
15081 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15082 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15084 static struct glyph_row *
15085 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15086 struct window *w;
15087 Lisp_Object overlay_arrow_string;
15089 struct frame *f = XFRAME (WINDOW_FRAME (w));
15090 struct buffer *buffer = XBUFFER (w->buffer);
15091 struct buffer *old = current_buffer;
15092 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15093 int arrow_len = SCHARS (overlay_arrow_string);
15094 const unsigned char *arrow_end = arrow_string + arrow_len;
15095 const unsigned char *p;
15096 struct it it;
15097 int multibyte_p;
15098 int n_glyphs_before;
15100 set_buffer_temp (buffer);
15101 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15102 it.glyph_row->used[TEXT_AREA] = 0;
15103 SET_TEXT_POS (it.position, 0, 0);
15105 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15106 p = arrow_string;
15107 while (p < arrow_end)
15109 Lisp_Object face, ilisp;
15111 /* Get the next character. */
15112 if (multibyte_p)
15113 it.c = string_char_and_length (p, arrow_len, &it.len);
15114 else
15115 it.c = *p, it.len = 1;
15116 p += it.len;
15118 /* Get its face. */
15119 ilisp = make_number (p - arrow_string);
15120 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15121 it.face_id = compute_char_face (f, it.c, face);
15123 /* Compute its width, get its glyphs. */
15124 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15125 SET_TEXT_POS (it.position, -1, -1);
15126 PRODUCE_GLYPHS (&it);
15128 /* If this character doesn't fit any more in the line, we have
15129 to remove some glyphs. */
15130 if (it.current_x > it.last_visible_x)
15132 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15133 break;
15137 set_buffer_temp (old);
15138 return it.glyph_row;
15142 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15143 glyphs are only inserted for terminal frames since we can't really
15144 win with truncation glyphs when partially visible glyphs are
15145 involved. Which glyphs to insert is determined by
15146 produce_special_glyphs. */
15148 static void
15149 insert_left_trunc_glyphs (it)
15150 struct it *it;
15152 struct it truncate_it;
15153 struct glyph *from, *end, *to, *toend;
15155 xassert (!FRAME_WINDOW_P (it->f));
15157 /* Get the truncation glyphs. */
15158 truncate_it = *it;
15159 truncate_it.current_x = 0;
15160 truncate_it.face_id = DEFAULT_FACE_ID;
15161 truncate_it.glyph_row = &scratch_glyph_row;
15162 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15163 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15164 truncate_it.object = make_number (0);
15165 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15167 /* Overwrite glyphs from IT with truncation glyphs. */
15168 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15169 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15170 to = it->glyph_row->glyphs[TEXT_AREA];
15171 toend = to + it->glyph_row->used[TEXT_AREA];
15173 while (from < end)
15174 *to++ = *from++;
15176 /* There may be padding glyphs left over. Overwrite them too. */
15177 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15179 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15180 while (from < end)
15181 *to++ = *from++;
15184 if (to > toend)
15185 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15189 /* Compute the pixel height and width of IT->glyph_row.
15191 Most of the time, ascent and height of a display line will be equal
15192 to the max_ascent and max_height values of the display iterator
15193 structure. This is not the case if
15195 1. We hit ZV without displaying anything. In this case, max_ascent
15196 and max_height will be zero.
15198 2. We have some glyphs that don't contribute to the line height.
15199 (The glyph row flag contributes_to_line_height_p is for future
15200 pixmap extensions).
15202 The first case is easily covered by using default values because in
15203 these cases, the line height does not really matter, except that it
15204 must not be zero. */
15206 static void
15207 compute_line_metrics (it)
15208 struct it *it;
15210 struct glyph_row *row = it->glyph_row;
15211 int area, i;
15213 if (FRAME_WINDOW_P (it->f))
15215 int i, min_y, max_y;
15217 /* The line may consist of one space only, that was added to
15218 place the cursor on it. If so, the row's height hasn't been
15219 computed yet. */
15220 if (row->height == 0)
15222 if (it->max_ascent + it->max_descent == 0)
15223 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15224 row->ascent = it->max_ascent;
15225 row->height = it->max_ascent + it->max_descent;
15226 row->phys_ascent = it->max_phys_ascent;
15227 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15228 row->extra_line_spacing = it->max_extra_line_spacing;
15231 /* Compute the width of this line. */
15232 row->pixel_width = row->x;
15233 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15234 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15236 xassert (row->pixel_width >= 0);
15237 xassert (row->ascent >= 0 && row->height > 0);
15239 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15240 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15242 /* If first line's physical ascent is larger than its logical
15243 ascent, use the physical ascent, and make the row taller.
15244 This makes accented characters fully visible. */
15245 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15246 && row->phys_ascent > row->ascent)
15248 row->height += row->phys_ascent - row->ascent;
15249 row->ascent = row->phys_ascent;
15252 /* Compute how much of the line is visible. */
15253 row->visible_height = row->height;
15255 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15256 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15258 if (row->y < min_y)
15259 row->visible_height -= min_y - row->y;
15260 if (row->y + row->height > max_y)
15261 row->visible_height -= row->y + row->height - max_y;
15263 else
15265 row->pixel_width = row->used[TEXT_AREA];
15266 if (row->continued_p)
15267 row->pixel_width -= it->continuation_pixel_width;
15268 else if (row->truncated_on_right_p)
15269 row->pixel_width -= it->truncation_pixel_width;
15270 row->ascent = row->phys_ascent = 0;
15271 row->height = row->phys_height = row->visible_height = 1;
15272 row->extra_line_spacing = 0;
15275 /* Compute a hash code for this row. */
15276 row->hash = 0;
15277 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15278 for (i = 0; i < row->used[area]; ++i)
15279 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15280 + row->glyphs[area][i].u.val
15281 + row->glyphs[area][i].face_id
15282 + row->glyphs[area][i].padding_p
15283 + (row->glyphs[area][i].type << 2));
15285 it->max_ascent = it->max_descent = 0;
15286 it->max_phys_ascent = it->max_phys_descent = 0;
15290 /* Append one space to the glyph row of iterator IT if doing a
15291 window-based redisplay. The space has the same face as
15292 IT->face_id. Value is non-zero if a space was added.
15294 This function is called to make sure that there is always one glyph
15295 at the end of a glyph row that the cursor can be set on under
15296 window-systems. (If there weren't such a glyph we would not know
15297 how wide and tall a box cursor should be displayed).
15299 At the same time this space let's a nicely handle clearing to the
15300 end of the line if the row ends in italic text. */
15302 static int
15303 append_space_for_newline (it, default_face_p)
15304 struct it *it;
15305 int default_face_p;
15307 if (FRAME_WINDOW_P (it->f))
15309 int n = it->glyph_row->used[TEXT_AREA];
15311 if (it->glyph_row->glyphs[TEXT_AREA] + n
15312 < it->glyph_row->glyphs[1 + TEXT_AREA])
15314 /* Save some values that must not be changed.
15315 Must save IT->c and IT->len because otherwise
15316 ITERATOR_AT_END_P wouldn't work anymore after
15317 append_space_for_newline has been called. */
15318 enum display_element_type saved_what = it->what;
15319 int saved_c = it->c, saved_len = it->len;
15320 int saved_x = it->current_x;
15321 int saved_face_id = it->face_id;
15322 struct text_pos saved_pos;
15323 Lisp_Object saved_object;
15324 struct face *face;
15326 saved_object = it->object;
15327 saved_pos = it->position;
15329 it->what = IT_CHARACTER;
15330 bzero (&it->position, sizeof it->position);
15331 it->object = make_number (0);
15332 it->c = ' ';
15333 it->len = 1;
15335 if (default_face_p)
15336 it->face_id = DEFAULT_FACE_ID;
15337 else if (it->face_before_selective_p)
15338 it->face_id = it->saved_face_id;
15339 face = FACE_FROM_ID (it->f, it->face_id);
15340 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15342 PRODUCE_GLYPHS (it);
15344 it->override_ascent = -1;
15345 it->constrain_row_ascent_descent_p = 0;
15346 it->current_x = saved_x;
15347 it->object = saved_object;
15348 it->position = saved_pos;
15349 it->what = saved_what;
15350 it->face_id = saved_face_id;
15351 it->len = saved_len;
15352 it->c = saved_c;
15353 return 1;
15357 return 0;
15361 /* Extend the face of the last glyph in the text area of IT->glyph_row
15362 to the end of the display line. Called from display_line.
15363 If the glyph row is empty, add a space glyph to it so that we
15364 know the face to draw. Set the glyph row flag fill_line_p. */
15366 static void
15367 extend_face_to_end_of_line (it)
15368 struct it *it;
15370 struct face *face;
15371 struct frame *f = it->f;
15373 /* If line is already filled, do nothing. */
15374 if (it->current_x >= it->last_visible_x)
15375 return;
15377 /* Face extension extends the background and box of IT->face_id
15378 to the end of the line. If the background equals the background
15379 of the frame, we don't have to do anything. */
15380 if (it->face_before_selective_p)
15381 face = FACE_FROM_ID (it->f, it->saved_face_id);
15382 else
15383 face = FACE_FROM_ID (f, it->face_id);
15385 if (FRAME_WINDOW_P (f)
15386 && it->glyph_row->displays_text_p
15387 && face->box == FACE_NO_BOX
15388 && face->background == FRAME_BACKGROUND_PIXEL (f)
15389 && !face->stipple)
15390 return;
15392 /* Set the glyph row flag indicating that the face of the last glyph
15393 in the text area has to be drawn to the end of the text area. */
15394 it->glyph_row->fill_line_p = 1;
15396 /* If current character of IT is not ASCII, make sure we have the
15397 ASCII face. This will be automatically undone the next time
15398 get_next_display_element returns a multibyte character. Note
15399 that the character will always be single byte in unibyte text. */
15400 if (!SINGLE_BYTE_CHAR_P (it->c))
15402 it->face_id = FACE_FOR_CHAR (f, face, 0);
15405 if (FRAME_WINDOW_P (f))
15407 /* If the row is empty, add a space with the current face of IT,
15408 so that we know which face to draw. */
15409 if (it->glyph_row->used[TEXT_AREA] == 0)
15411 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15412 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15413 it->glyph_row->used[TEXT_AREA] = 1;
15416 else
15418 /* Save some values that must not be changed. */
15419 int saved_x = it->current_x;
15420 struct text_pos saved_pos;
15421 Lisp_Object saved_object;
15422 enum display_element_type saved_what = it->what;
15423 int saved_face_id = it->face_id;
15425 saved_object = it->object;
15426 saved_pos = it->position;
15428 it->what = IT_CHARACTER;
15429 bzero (&it->position, sizeof it->position);
15430 it->object = make_number (0);
15431 it->c = ' ';
15432 it->len = 1;
15433 it->face_id = face->id;
15435 PRODUCE_GLYPHS (it);
15437 while (it->current_x <= it->last_visible_x)
15438 PRODUCE_GLYPHS (it);
15440 /* Don't count these blanks really. It would let us insert a left
15441 truncation glyph below and make us set the cursor on them, maybe. */
15442 it->current_x = saved_x;
15443 it->object = saved_object;
15444 it->position = saved_pos;
15445 it->what = saved_what;
15446 it->face_id = saved_face_id;
15451 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15452 trailing whitespace. */
15454 static int
15455 trailing_whitespace_p (charpos)
15456 int charpos;
15458 int bytepos = CHAR_TO_BYTE (charpos);
15459 int c = 0;
15461 while (bytepos < ZV_BYTE
15462 && (c = FETCH_CHAR (bytepos),
15463 c == ' ' || c == '\t'))
15464 ++bytepos;
15466 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15468 if (bytepos != PT_BYTE)
15469 return 1;
15471 return 0;
15475 /* Highlight trailing whitespace, if any, in ROW. */
15477 void
15478 highlight_trailing_whitespace (f, row)
15479 struct frame *f;
15480 struct glyph_row *row;
15482 int used = row->used[TEXT_AREA];
15484 if (used)
15486 struct glyph *start = row->glyphs[TEXT_AREA];
15487 struct glyph *glyph = start + used - 1;
15489 /* Skip over glyphs inserted to display the cursor at the
15490 end of a line, for extending the face of the last glyph
15491 to the end of the line on terminals, and for truncation
15492 and continuation glyphs. */
15493 while (glyph >= start
15494 && glyph->type == CHAR_GLYPH
15495 && INTEGERP (glyph->object))
15496 --glyph;
15498 /* If last glyph is a space or stretch, and it's trailing
15499 whitespace, set the face of all trailing whitespace glyphs in
15500 IT->glyph_row to `trailing-whitespace'. */
15501 if (glyph >= start
15502 && BUFFERP (glyph->object)
15503 && (glyph->type == STRETCH_GLYPH
15504 || (glyph->type == CHAR_GLYPH
15505 && glyph->u.ch == ' '))
15506 && trailing_whitespace_p (glyph->charpos))
15508 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15509 if (face_id < 0)
15510 return;
15512 while (glyph >= start
15513 && BUFFERP (glyph->object)
15514 && (glyph->type == STRETCH_GLYPH
15515 || (glyph->type == CHAR_GLYPH
15516 && glyph->u.ch == ' ')))
15517 (glyph--)->face_id = face_id;
15523 /* Value is non-zero if glyph row ROW in window W should be
15524 used to hold the cursor. */
15526 static int
15527 cursor_row_p (w, row)
15528 struct window *w;
15529 struct glyph_row *row;
15531 int cursor_row_p = 1;
15533 if (PT == MATRIX_ROW_END_CHARPOS (row))
15535 /* If the row ends with a newline from a string, we don't want
15536 the cursor there, but we still want it at the start of the
15537 string if the string starts in this row.
15538 If the row is continued it doesn't end in a newline. */
15539 if (CHARPOS (row->end.string_pos) >= 0)
15540 cursor_row_p = (row->continued_p
15541 || PT >= MATRIX_ROW_START_CHARPOS (row));
15542 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15544 /* If the row ends in middle of a real character,
15545 and the line is continued, we want the cursor here.
15546 That's because MATRIX_ROW_END_CHARPOS would equal
15547 PT if PT is before the character. */
15548 if (!row->ends_in_ellipsis_p)
15549 cursor_row_p = row->continued_p;
15550 else
15551 /* If the row ends in an ellipsis, then
15552 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15553 We want that position to be displayed after the ellipsis. */
15554 cursor_row_p = 0;
15556 /* If the row ends at ZV, display the cursor at the end of that
15557 row instead of at the start of the row below. */
15558 else if (row->ends_at_zv_p)
15559 cursor_row_p = 1;
15560 else
15561 cursor_row_p = 0;
15564 return cursor_row_p;
15568 /* Construct the glyph row IT->glyph_row in the desired matrix of
15569 IT->w from text at the current position of IT. See dispextern.h
15570 for an overview of struct it. Value is non-zero if
15571 IT->glyph_row displays text, as opposed to a line displaying ZV
15572 only. */
15574 static int
15575 display_line (it)
15576 struct it *it;
15578 struct glyph_row *row = it->glyph_row;
15579 Lisp_Object overlay_arrow_string;
15581 /* We always start displaying at hpos zero even if hscrolled. */
15582 xassert (it->hpos == 0 && it->current_x == 0);
15584 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15585 >= it->w->desired_matrix->nrows)
15587 it->w->nrows_scale_factor++;
15588 fonts_changed_p = 1;
15589 return 0;
15592 /* Is IT->w showing the region? */
15593 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15595 /* Clear the result glyph row and enable it. */
15596 prepare_desired_row (row);
15598 row->y = it->current_y;
15599 row->start = it->start;
15600 row->continuation_lines_width = it->continuation_lines_width;
15601 row->displays_text_p = 1;
15602 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15603 it->starts_in_middle_of_char_p = 0;
15605 /* Arrange the overlays nicely for our purposes. Usually, we call
15606 display_line on only one line at a time, in which case this
15607 can't really hurt too much, or we call it on lines which appear
15608 one after another in the buffer, in which case all calls to
15609 recenter_overlay_lists but the first will be pretty cheap. */
15610 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15612 /* Move over display elements that are not visible because we are
15613 hscrolled. This may stop at an x-position < IT->first_visible_x
15614 if the first glyph is partially visible or if we hit a line end. */
15615 if (it->current_x < it->first_visible_x)
15617 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15618 MOVE_TO_POS | MOVE_TO_X);
15621 /* Get the initial row height. This is either the height of the
15622 text hscrolled, if there is any, or zero. */
15623 row->ascent = it->max_ascent;
15624 row->height = it->max_ascent + it->max_descent;
15625 row->phys_ascent = it->max_phys_ascent;
15626 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15627 row->extra_line_spacing = it->max_extra_line_spacing;
15629 /* Loop generating characters. The loop is left with IT on the next
15630 character to display. */
15631 while (1)
15633 int n_glyphs_before, hpos_before, x_before;
15634 int x, i, nglyphs;
15635 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15637 /* Retrieve the next thing to display. Value is zero if end of
15638 buffer reached. */
15639 if (!get_next_display_element (it))
15641 /* Maybe add a space at the end of this line that is used to
15642 display the cursor there under X. Set the charpos of the
15643 first glyph of blank lines not corresponding to any text
15644 to -1. */
15645 #ifdef HAVE_WINDOW_SYSTEM
15646 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15647 row->exact_window_width_line_p = 1;
15648 else
15649 #endif /* HAVE_WINDOW_SYSTEM */
15650 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15651 || row->used[TEXT_AREA] == 0)
15653 row->glyphs[TEXT_AREA]->charpos = -1;
15654 row->displays_text_p = 0;
15656 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15657 && (!MINI_WINDOW_P (it->w)
15658 || (minibuf_level && EQ (it->window, minibuf_window))))
15659 row->indicate_empty_line_p = 1;
15662 it->continuation_lines_width = 0;
15663 row->ends_at_zv_p = 1;
15664 break;
15667 /* Now, get the metrics of what we want to display. This also
15668 generates glyphs in `row' (which is IT->glyph_row). */
15669 n_glyphs_before = row->used[TEXT_AREA];
15670 x = it->current_x;
15672 /* Remember the line height so far in case the next element doesn't
15673 fit on the line. */
15674 if (!it->truncate_lines_p)
15676 ascent = it->max_ascent;
15677 descent = it->max_descent;
15678 phys_ascent = it->max_phys_ascent;
15679 phys_descent = it->max_phys_descent;
15682 PRODUCE_GLYPHS (it);
15684 /* If this display element was in marginal areas, continue with
15685 the next one. */
15686 if (it->area != TEXT_AREA)
15688 row->ascent = max (row->ascent, it->max_ascent);
15689 row->height = max (row->height, it->max_ascent + it->max_descent);
15690 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15691 row->phys_height = max (row->phys_height,
15692 it->max_phys_ascent + it->max_phys_descent);
15693 row->extra_line_spacing = max (row->extra_line_spacing,
15694 it->max_extra_line_spacing);
15695 set_iterator_to_next (it, 1);
15696 continue;
15699 /* Does the display element fit on the line? If we truncate
15700 lines, we should draw past the right edge of the window. If
15701 we don't truncate, we want to stop so that we can display the
15702 continuation glyph before the right margin. If lines are
15703 continued, there are two possible strategies for characters
15704 resulting in more than 1 glyph (e.g. tabs): Display as many
15705 glyphs as possible in this line and leave the rest for the
15706 continuation line, or display the whole element in the next
15707 line. Original redisplay did the former, so we do it also. */
15708 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15709 hpos_before = it->hpos;
15710 x_before = x;
15712 if (/* Not a newline. */
15713 nglyphs > 0
15714 /* Glyphs produced fit entirely in the line. */
15715 && it->current_x < it->last_visible_x)
15717 it->hpos += nglyphs;
15718 row->ascent = max (row->ascent, it->max_ascent);
15719 row->height = max (row->height, it->max_ascent + it->max_descent);
15720 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15721 row->phys_height = max (row->phys_height,
15722 it->max_phys_ascent + it->max_phys_descent);
15723 row->extra_line_spacing = max (row->extra_line_spacing,
15724 it->max_extra_line_spacing);
15725 if (it->current_x - it->pixel_width < it->first_visible_x)
15726 row->x = x - it->first_visible_x;
15728 else
15730 int new_x;
15731 struct glyph *glyph;
15733 for (i = 0; i < nglyphs; ++i, x = new_x)
15735 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15736 new_x = x + glyph->pixel_width;
15738 if (/* Lines are continued. */
15739 !it->truncate_lines_p
15740 && (/* Glyph doesn't fit on the line. */
15741 new_x > it->last_visible_x
15742 /* Or it fits exactly on a window system frame. */
15743 || (new_x == it->last_visible_x
15744 && FRAME_WINDOW_P (it->f))))
15746 /* End of a continued line. */
15748 if (it->hpos == 0
15749 || (new_x == it->last_visible_x
15750 && FRAME_WINDOW_P (it->f)))
15752 /* Current glyph is the only one on the line or
15753 fits exactly on the line. We must continue
15754 the line because we can't draw the cursor
15755 after the glyph. */
15756 row->continued_p = 1;
15757 it->current_x = new_x;
15758 it->continuation_lines_width += new_x;
15759 ++it->hpos;
15760 if (i == nglyphs - 1)
15762 set_iterator_to_next (it, 1);
15763 #ifdef HAVE_WINDOW_SYSTEM
15764 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15766 if (!get_next_display_element (it))
15768 row->exact_window_width_line_p = 1;
15769 it->continuation_lines_width = 0;
15770 row->continued_p = 0;
15771 row->ends_at_zv_p = 1;
15773 else if (ITERATOR_AT_END_OF_LINE_P (it))
15775 row->continued_p = 0;
15776 row->exact_window_width_line_p = 1;
15779 #endif /* HAVE_WINDOW_SYSTEM */
15782 else if (CHAR_GLYPH_PADDING_P (*glyph)
15783 && !FRAME_WINDOW_P (it->f))
15785 /* A padding glyph that doesn't fit on this line.
15786 This means the whole character doesn't fit
15787 on the line. */
15788 row->used[TEXT_AREA] = n_glyphs_before;
15790 /* Fill the rest of the row with continuation
15791 glyphs like in 20.x. */
15792 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15793 < row->glyphs[1 + TEXT_AREA])
15794 produce_special_glyphs (it, IT_CONTINUATION);
15796 row->continued_p = 1;
15797 it->current_x = x_before;
15798 it->continuation_lines_width += x_before;
15800 /* Restore the height to what it was before the
15801 element not fitting on the line. */
15802 it->max_ascent = ascent;
15803 it->max_descent = descent;
15804 it->max_phys_ascent = phys_ascent;
15805 it->max_phys_descent = phys_descent;
15807 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15809 /* A TAB that extends past the right edge of the
15810 window. This produces a single glyph on
15811 window system frames. We leave the glyph in
15812 this row and let it fill the row, but don't
15813 consume the TAB. */
15814 it->continuation_lines_width += it->last_visible_x;
15815 row->ends_in_middle_of_char_p = 1;
15816 row->continued_p = 1;
15817 glyph->pixel_width = it->last_visible_x - x;
15818 it->starts_in_middle_of_char_p = 1;
15820 else
15822 /* Something other than a TAB that draws past
15823 the right edge of the window. Restore
15824 positions to values before the element. */
15825 row->used[TEXT_AREA] = n_glyphs_before + i;
15827 /* Display continuation glyphs. */
15828 if (!FRAME_WINDOW_P (it->f))
15829 produce_special_glyphs (it, IT_CONTINUATION);
15830 row->continued_p = 1;
15832 it->current_x = x_before;
15833 it->continuation_lines_width += x;
15834 extend_face_to_end_of_line (it);
15836 if (nglyphs > 1 && i > 0)
15838 row->ends_in_middle_of_char_p = 1;
15839 it->starts_in_middle_of_char_p = 1;
15842 /* Restore the height to what it was before the
15843 element not fitting on the line. */
15844 it->max_ascent = ascent;
15845 it->max_descent = descent;
15846 it->max_phys_ascent = phys_ascent;
15847 it->max_phys_descent = phys_descent;
15850 break;
15852 else if (new_x > it->first_visible_x)
15854 /* Increment number of glyphs actually displayed. */
15855 ++it->hpos;
15857 if (x < it->first_visible_x)
15858 /* Glyph is partially visible, i.e. row starts at
15859 negative X position. */
15860 row->x = x - it->first_visible_x;
15862 else
15864 /* Glyph is completely off the left margin of the
15865 window. This should not happen because of the
15866 move_it_in_display_line at the start of this
15867 function, unless the text display area of the
15868 window is empty. */
15869 xassert (it->first_visible_x <= it->last_visible_x);
15873 row->ascent = max (row->ascent, it->max_ascent);
15874 row->height = max (row->height, it->max_ascent + it->max_descent);
15875 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15876 row->phys_height = max (row->phys_height,
15877 it->max_phys_ascent + it->max_phys_descent);
15878 row->extra_line_spacing = max (row->extra_line_spacing,
15879 it->max_extra_line_spacing);
15881 /* End of this display line if row is continued. */
15882 if (row->continued_p || row->ends_at_zv_p)
15883 break;
15886 at_end_of_line:
15887 /* Is this a line end? If yes, we're also done, after making
15888 sure that a non-default face is extended up to the right
15889 margin of the window. */
15890 if (ITERATOR_AT_END_OF_LINE_P (it))
15892 int used_before = row->used[TEXT_AREA];
15894 row->ends_in_newline_from_string_p = STRINGP (it->object);
15896 #ifdef HAVE_WINDOW_SYSTEM
15897 /* Add a space at the end of the line that is used to
15898 display the cursor there. */
15899 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15900 append_space_for_newline (it, 0);
15901 #endif /* HAVE_WINDOW_SYSTEM */
15903 /* Extend the face to the end of the line. */
15904 extend_face_to_end_of_line (it);
15906 /* Make sure we have the position. */
15907 if (used_before == 0)
15908 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15910 /* Consume the line end. This skips over invisible lines. */
15911 set_iterator_to_next (it, 1);
15912 it->continuation_lines_width = 0;
15913 break;
15916 /* Proceed with next display element. Note that this skips
15917 over lines invisible because of selective display. */
15918 set_iterator_to_next (it, 1);
15920 /* If we truncate lines, we are done when the last displayed
15921 glyphs reach past the right margin of the window. */
15922 if (it->truncate_lines_p
15923 && (FRAME_WINDOW_P (it->f)
15924 ? (it->current_x >= it->last_visible_x)
15925 : (it->current_x > it->last_visible_x)))
15927 /* Maybe add truncation glyphs. */
15928 if (!FRAME_WINDOW_P (it->f))
15930 int i, n;
15932 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15933 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15934 break;
15936 for (n = row->used[TEXT_AREA]; i < n; ++i)
15938 row->used[TEXT_AREA] = i;
15939 produce_special_glyphs (it, IT_TRUNCATION);
15942 #ifdef HAVE_WINDOW_SYSTEM
15943 else
15945 /* Don't truncate if we can overflow newline into fringe. */
15946 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15948 if (!get_next_display_element (it))
15950 it->continuation_lines_width = 0;
15951 row->ends_at_zv_p = 1;
15952 row->exact_window_width_line_p = 1;
15953 break;
15955 if (ITERATOR_AT_END_OF_LINE_P (it))
15957 row->exact_window_width_line_p = 1;
15958 goto at_end_of_line;
15962 #endif /* HAVE_WINDOW_SYSTEM */
15964 row->truncated_on_right_p = 1;
15965 it->continuation_lines_width = 0;
15966 reseat_at_next_visible_line_start (it, 0);
15967 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15968 it->hpos = hpos_before;
15969 it->current_x = x_before;
15970 break;
15974 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15975 at the left window margin. */
15976 if (it->first_visible_x
15977 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15979 if (!FRAME_WINDOW_P (it->f))
15980 insert_left_trunc_glyphs (it);
15981 row->truncated_on_left_p = 1;
15984 /* If the start of this line is the overlay arrow-position, then
15985 mark this glyph row as the one containing the overlay arrow.
15986 This is clearly a mess with variable size fonts. It would be
15987 better to let it be displayed like cursors under X. */
15988 if ((row->displays_text_p || !overlay_arrow_seen)
15989 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15990 !NILP (overlay_arrow_string)))
15992 /* Overlay arrow in window redisplay is a fringe bitmap. */
15993 if (STRINGP (overlay_arrow_string))
15995 struct glyph_row *arrow_row
15996 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15997 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15998 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15999 struct glyph *p = row->glyphs[TEXT_AREA];
16000 struct glyph *p2, *end;
16002 /* Copy the arrow glyphs. */
16003 while (glyph < arrow_end)
16004 *p++ = *glyph++;
16006 /* Throw away padding glyphs. */
16007 p2 = p;
16008 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16009 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16010 ++p2;
16011 if (p2 > p)
16013 while (p2 < end)
16014 *p++ = *p2++;
16015 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16018 else
16020 xassert (INTEGERP (overlay_arrow_string));
16021 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16023 overlay_arrow_seen = 1;
16026 /* Compute pixel dimensions of this line. */
16027 compute_line_metrics (it);
16029 /* Remember the position at which this line ends. */
16030 row->end = it->current;
16032 /* Record whether this row ends inside an ellipsis. */
16033 row->ends_in_ellipsis_p
16034 = (it->method == GET_FROM_DISPLAY_VECTOR
16035 && it->ellipsis_p);
16037 /* Save fringe bitmaps in this row. */
16038 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16039 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16040 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16041 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16043 it->left_user_fringe_bitmap = 0;
16044 it->left_user_fringe_face_id = 0;
16045 it->right_user_fringe_bitmap = 0;
16046 it->right_user_fringe_face_id = 0;
16048 /* Maybe set the cursor. */
16049 if (it->w->cursor.vpos < 0
16050 && PT >= MATRIX_ROW_START_CHARPOS (row)
16051 && PT <= MATRIX_ROW_END_CHARPOS (row)
16052 && cursor_row_p (it->w, row))
16053 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16055 /* Highlight trailing whitespace. */
16056 if (!NILP (Vshow_trailing_whitespace))
16057 highlight_trailing_whitespace (it->f, it->glyph_row);
16059 /* Prepare for the next line. This line starts horizontally at (X
16060 HPOS) = (0 0). Vertical positions are incremented. As a
16061 convenience for the caller, IT->glyph_row is set to the next
16062 row to be used. */
16063 it->current_x = it->hpos = 0;
16064 it->current_y += row->height;
16065 ++it->vpos;
16066 ++it->glyph_row;
16067 it->start = it->current;
16068 return row->displays_text_p;
16073 /***********************************************************************
16074 Menu Bar
16075 ***********************************************************************/
16077 /* Redisplay the menu bar in the frame for window W.
16079 The menu bar of X frames that don't have X toolkit support is
16080 displayed in a special window W->frame->menu_bar_window.
16082 The menu bar of terminal frames is treated specially as far as
16083 glyph matrices are concerned. Menu bar lines are not part of
16084 windows, so the update is done directly on the frame matrix rows
16085 for the menu bar. */
16087 static void
16088 display_menu_bar (w)
16089 struct window *w;
16091 struct frame *f = XFRAME (WINDOW_FRAME (w));
16092 struct it it;
16093 Lisp_Object items;
16094 int i;
16096 /* Don't do all this for graphical frames. */
16097 #ifdef HAVE_NTGUI
16098 if (!NILP (Vwindow_system))
16099 return;
16100 #endif
16101 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16102 if (FRAME_X_P (f))
16103 return;
16104 #endif
16105 #ifdef MAC_OS
16106 if (FRAME_MAC_P (f))
16107 return;
16108 #endif
16110 #ifdef USE_X_TOOLKIT
16111 xassert (!FRAME_WINDOW_P (f));
16112 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16113 it.first_visible_x = 0;
16114 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16115 #else /* not USE_X_TOOLKIT */
16116 if (FRAME_WINDOW_P (f))
16118 /* Menu bar lines are displayed in the desired matrix of the
16119 dummy window menu_bar_window. */
16120 struct window *menu_w;
16121 xassert (WINDOWP (f->menu_bar_window));
16122 menu_w = XWINDOW (f->menu_bar_window);
16123 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16124 MENU_FACE_ID);
16125 it.first_visible_x = 0;
16126 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16128 else
16130 /* This is a TTY frame, i.e. character hpos/vpos are used as
16131 pixel x/y. */
16132 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16133 MENU_FACE_ID);
16134 it.first_visible_x = 0;
16135 it.last_visible_x = FRAME_COLS (f);
16137 #endif /* not USE_X_TOOLKIT */
16139 if (! mode_line_inverse_video)
16140 /* Force the menu-bar to be displayed in the default face. */
16141 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16143 /* Clear all rows of the menu bar. */
16144 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16146 struct glyph_row *row = it.glyph_row + i;
16147 clear_glyph_row (row);
16148 row->enabled_p = 1;
16149 row->full_width_p = 1;
16152 /* Display all items of the menu bar. */
16153 items = FRAME_MENU_BAR_ITEMS (it.f);
16154 for (i = 0; i < XVECTOR (items)->size; i += 4)
16156 Lisp_Object string;
16158 /* Stop at nil string. */
16159 string = AREF (items, i + 1);
16160 if (NILP (string))
16161 break;
16163 /* Remember where item was displayed. */
16164 AREF (items, i + 3) = make_number (it.hpos);
16166 /* Display the item, pad with one space. */
16167 if (it.current_x < it.last_visible_x)
16168 display_string (NULL, string, Qnil, 0, 0, &it,
16169 SCHARS (string) + 1, 0, 0, -1);
16172 /* Fill out the line with spaces. */
16173 if (it.current_x < it.last_visible_x)
16174 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16176 /* Compute the total height of the lines. */
16177 compute_line_metrics (&it);
16182 /***********************************************************************
16183 Mode Line
16184 ***********************************************************************/
16186 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16187 FORCE is non-zero, redisplay mode lines unconditionally.
16188 Otherwise, redisplay only mode lines that are garbaged. Value is
16189 the number of windows whose mode lines were redisplayed. */
16191 static int
16192 redisplay_mode_lines (window, force)
16193 Lisp_Object window;
16194 int force;
16196 int nwindows = 0;
16198 while (!NILP (window))
16200 struct window *w = XWINDOW (window);
16202 if (WINDOWP (w->hchild))
16203 nwindows += redisplay_mode_lines (w->hchild, force);
16204 else if (WINDOWP (w->vchild))
16205 nwindows += redisplay_mode_lines (w->vchild, force);
16206 else if (force
16207 || FRAME_GARBAGED_P (XFRAME (w->frame))
16208 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16210 struct text_pos lpoint;
16211 struct buffer *old = current_buffer;
16213 /* Set the window's buffer for the mode line display. */
16214 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16215 set_buffer_internal_1 (XBUFFER (w->buffer));
16217 /* Point refers normally to the selected window. For any
16218 other window, set up appropriate value. */
16219 if (!EQ (window, selected_window))
16221 struct text_pos pt;
16223 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16224 if (CHARPOS (pt) < BEGV)
16225 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16226 else if (CHARPOS (pt) > (ZV - 1))
16227 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16228 else
16229 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16232 /* Display mode lines. */
16233 clear_glyph_matrix (w->desired_matrix);
16234 if (display_mode_lines (w))
16236 ++nwindows;
16237 w->must_be_updated_p = 1;
16240 /* Restore old settings. */
16241 set_buffer_internal_1 (old);
16242 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16245 window = w->next;
16248 return nwindows;
16252 /* Display the mode and/or top line of window W. Value is the number
16253 of mode lines displayed. */
16255 static int
16256 display_mode_lines (w)
16257 struct window *w;
16259 Lisp_Object old_selected_window, old_selected_frame;
16260 int n = 0;
16262 old_selected_frame = selected_frame;
16263 selected_frame = w->frame;
16264 old_selected_window = selected_window;
16265 XSETWINDOW (selected_window, w);
16267 /* These will be set while the mode line specs are processed. */
16268 line_number_displayed = 0;
16269 w->column_number_displayed = Qnil;
16271 if (WINDOW_WANTS_MODELINE_P (w))
16273 struct window *sel_w = XWINDOW (old_selected_window);
16275 /* Select mode line face based on the real selected window. */
16276 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16277 current_buffer->mode_line_format);
16278 ++n;
16281 if (WINDOW_WANTS_HEADER_LINE_P (w))
16283 display_mode_line (w, HEADER_LINE_FACE_ID,
16284 current_buffer->header_line_format);
16285 ++n;
16288 selected_frame = old_selected_frame;
16289 selected_window = old_selected_window;
16290 return n;
16294 /* Display mode or top line of window W. FACE_ID specifies which line
16295 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16296 FORMAT is the mode line format to display. Value is the pixel
16297 height of the mode line displayed. */
16299 static int
16300 display_mode_line (w, face_id, format)
16301 struct window *w;
16302 enum face_id face_id;
16303 Lisp_Object format;
16305 struct it it;
16306 struct face *face;
16307 int count = SPECPDL_INDEX ();
16309 init_iterator (&it, w, -1, -1, NULL, face_id);
16310 prepare_desired_row (it.glyph_row);
16312 it.glyph_row->mode_line_p = 1;
16314 if (! mode_line_inverse_video)
16315 /* Force the mode-line to be displayed in the default face. */
16316 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16318 record_unwind_protect (unwind_format_mode_line,
16319 format_mode_line_unwind_data (NULL, 0));
16321 mode_line_target = MODE_LINE_DISPLAY;
16323 /* Temporarily make frame's keyboard the current kboard so that
16324 kboard-local variables in the mode_line_format will get the right
16325 values. */
16326 push_kboard (FRAME_KBOARD (it.f));
16327 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16328 pop_kboard ();
16330 unbind_to (count, Qnil);
16332 /* Fill up with spaces. */
16333 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16335 compute_line_metrics (&it);
16336 it.glyph_row->full_width_p = 1;
16337 it.glyph_row->continued_p = 0;
16338 it.glyph_row->truncated_on_left_p = 0;
16339 it.glyph_row->truncated_on_right_p = 0;
16341 /* Make a 3D mode-line have a shadow at its right end. */
16342 face = FACE_FROM_ID (it.f, face_id);
16343 extend_face_to_end_of_line (&it);
16344 if (face->box != FACE_NO_BOX)
16346 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16347 + it.glyph_row->used[TEXT_AREA] - 1);
16348 last->right_box_line_p = 1;
16351 return it.glyph_row->height;
16354 /* Move element ELT in LIST to the front of LIST.
16355 Return the updated list. */
16357 static Lisp_Object
16358 move_elt_to_front (elt, list)
16359 Lisp_Object elt, list;
16361 register Lisp_Object tail, prev;
16362 register Lisp_Object tem;
16364 tail = list;
16365 prev = Qnil;
16366 while (CONSP (tail))
16368 tem = XCAR (tail);
16370 if (EQ (elt, tem))
16372 /* Splice out the link TAIL. */
16373 if (NILP (prev))
16374 list = XCDR (tail);
16375 else
16376 Fsetcdr (prev, XCDR (tail));
16378 /* Now make it the first. */
16379 Fsetcdr (tail, list);
16380 return tail;
16382 else
16383 prev = tail;
16384 tail = XCDR (tail);
16385 QUIT;
16388 /* Not found--return unchanged LIST. */
16389 return list;
16392 /* Contribute ELT to the mode line for window IT->w. How it
16393 translates into text depends on its data type.
16395 IT describes the display environment in which we display, as usual.
16397 DEPTH is the depth in recursion. It is used to prevent
16398 infinite recursion here.
16400 FIELD_WIDTH is the number of characters the display of ELT should
16401 occupy in the mode line, and PRECISION is the maximum number of
16402 characters to display from ELT's representation. See
16403 display_string for details.
16405 Returns the hpos of the end of the text generated by ELT.
16407 PROPS is a property list to add to any string we encounter.
16409 If RISKY is nonzero, remove (disregard) any properties in any string
16410 we encounter, and ignore :eval and :propertize.
16412 The global variable `mode_line_target' determines whether the
16413 output is passed to `store_mode_line_noprop',
16414 `store_mode_line_string', or `display_string'. */
16416 static int
16417 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16418 struct it *it;
16419 int depth;
16420 int field_width, precision;
16421 Lisp_Object elt, props;
16422 int risky;
16424 int n = 0, field, prec;
16425 int literal = 0;
16427 tail_recurse:
16428 if (depth > 100)
16429 elt = build_string ("*too-deep*");
16431 depth++;
16433 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16435 case Lisp_String:
16437 /* A string: output it and check for %-constructs within it. */
16438 unsigned char c;
16439 int offset = 0;
16441 if (SCHARS (elt) > 0
16442 && (!NILP (props) || risky))
16444 Lisp_Object oprops, aelt;
16445 oprops = Ftext_properties_at (make_number (0), elt);
16447 /* If the starting string's properties are not what
16448 we want, translate the string. Also, if the string
16449 is risky, do that anyway. */
16451 if (NILP (Fequal (props, oprops)) || risky)
16453 /* If the starting string has properties,
16454 merge the specified ones onto the existing ones. */
16455 if (! NILP (oprops) && !risky)
16457 Lisp_Object tem;
16459 oprops = Fcopy_sequence (oprops);
16460 tem = props;
16461 while (CONSP (tem))
16463 oprops = Fplist_put (oprops, XCAR (tem),
16464 XCAR (XCDR (tem)));
16465 tem = XCDR (XCDR (tem));
16467 props = oprops;
16470 aelt = Fassoc (elt, mode_line_proptrans_alist);
16471 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16473 /* AELT is what we want. Move it to the front
16474 without consing. */
16475 elt = XCAR (aelt);
16476 mode_line_proptrans_alist
16477 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16479 else
16481 Lisp_Object tem;
16483 /* If AELT has the wrong props, it is useless.
16484 so get rid of it. */
16485 if (! NILP (aelt))
16486 mode_line_proptrans_alist
16487 = Fdelq (aelt, mode_line_proptrans_alist);
16489 elt = Fcopy_sequence (elt);
16490 Fset_text_properties (make_number (0), Flength (elt),
16491 props, elt);
16492 /* Add this item to mode_line_proptrans_alist. */
16493 mode_line_proptrans_alist
16494 = Fcons (Fcons (elt, props),
16495 mode_line_proptrans_alist);
16496 /* Truncate mode_line_proptrans_alist
16497 to at most 50 elements. */
16498 tem = Fnthcdr (make_number (50),
16499 mode_line_proptrans_alist);
16500 if (! NILP (tem))
16501 XSETCDR (tem, Qnil);
16506 offset = 0;
16508 if (literal)
16510 prec = precision - n;
16511 switch (mode_line_target)
16513 case MODE_LINE_NOPROP:
16514 case MODE_LINE_TITLE:
16515 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16516 break;
16517 case MODE_LINE_STRING:
16518 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16519 break;
16520 case MODE_LINE_DISPLAY:
16521 n += display_string (NULL, elt, Qnil, 0, 0, it,
16522 0, prec, 0, STRING_MULTIBYTE (elt));
16523 break;
16526 break;
16529 /* Handle the non-literal case. */
16531 while ((precision <= 0 || n < precision)
16532 && SREF (elt, offset) != 0
16533 && (mode_line_target != MODE_LINE_DISPLAY
16534 || it->current_x < it->last_visible_x))
16536 int last_offset = offset;
16538 /* Advance to end of string or next format specifier. */
16539 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16542 if (offset - 1 != last_offset)
16544 int nchars, nbytes;
16546 /* Output to end of string or up to '%'. Field width
16547 is length of string. Don't output more than
16548 PRECISION allows us. */
16549 offset--;
16551 prec = c_string_width (SDATA (elt) + last_offset,
16552 offset - last_offset, precision - n,
16553 &nchars, &nbytes);
16555 switch (mode_line_target)
16557 case MODE_LINE_NOPROP:
16558 case MODE_LINE_TITLE:
16559 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16560 break;
16561 case MODE_LINE_STRING:
16563 int bytepos = last_offset;
16564 int charpos = string_byte_to_char (elt, bytepos);
16565 int endpos = (precision <= 0
16566 ? string_byte_to_char (elt, offset)
16567 : charpos + nchars);
16569 n += store_mode_line_string (NULL,
16570 Fsubstring (elt, make_number (charpos),
16571 make_number (endpos)),
16572 0, 0, 0, Qnil);
16574 break;
16575 case MODE_LINE_DISPLAY:
16577 int bytepos = last_offset;
16578 int charpos = string_byte_to_char (elt, bytepos);
16580 if (precision <= 0)
16581 nchars = string_byte_to_char (elt, offset) - charpos;
16582 n += display_string (NULL, elt, Qnil, 0, charpos,
16583 it, 0, nchars, 0,
16584 STRING_MULTIBYTE (elt));
16586 break;
16589 else /* c == '%' */
16591 int percent_position = offset;
16593 /* Get the specified minimum width. Zero means
16594 don't pad. */
16595 field = 0;
16596 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16597 field = field * 10 + c - '0';
16599 /* Don't pad beyond the total padding allowed. */
16600 if (field_width - n > 0 && field > field_width - n)
16601 field = field_width - n;
16603 /* Note that either PRECISION <= 0 or N < PRECISION. */
16604 prec = precision - n;
16606 if (c == 'M')
16607 n += display_mode_element (it, depth, field, prec,
16608 Vglobal_mode_string, props,
16609 risky);
16610 else if (c != 0)
16612 int multibyte;
16613 int bytepos, charpos;
16614 unsigned char *spec;
16616 bytepos = percent_position;
16617 charpos = (STRING_MULTIBYTE (elt)
16618 ? string_byte_to_char (elt, bytepos)
16619 : bytepos);
16621 spec
16622 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16624 switch (mode_line_target)
16626 case MODE_LINE_NOPROP:
16627 case MODE_LINE_TITLE:
16628 n += store_mode_line_noprop (spec, field, prec);
16629 break;
16630 case MODE_LINE_STRING:
16632 int len = strlen (spec);
16633 Lisp_Object tem = make_string (spec, len);
16634 props = Ftext_properties_at (make_number (charpos), elt);
16635 /* Should only keep face property in props */
16636 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16638 break;
16639 case MODE_LINE_DISPLAY:
16641 int nglyphs_before, nwritten;
16643 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16644 nwritten = display_string (spec, Qnil, elt,
16645 charpos, 0, it,
16646 field, prec, 0,
16647 multibyte);
16649 /* Assign to the glyphs written above the
16650 string where the `%x' came from, position
16651 of the `%'. */
16652 if (nwritten > 0)
16654 struct glyph *glyph
16655 = (it->glyph_row->glyphs[TEXT_AREA]
16656 + nglyphs_before);
16657 int i;
16659 for (i = 0; i < nwritten; ++i)
16661 glyph[i].object = elt;
16662 glyph[i].charpos = charpos;
16665 n += nwritten;
16668 break;
16671 else /* c == 0 */
16672 break;
16676 break;
16678 case Lisp_Symbol:
16679 /* A symbol: process the value of the symbol recursively
16680 as if it appeared here directly. Avoid error if symbol void.
16681 Special case: if value of symbol is a string, output the string
16682 literally. */
16684 register Lisp_Object tem;
16686 /* If the variable is not marked as risky to set
16687 then its contents are risky to use. */
16688 if (NILP (Fget (elt, Qrisky_local_variable)))
16689 risky = 1;
16691 tem = Fboundp (elt);
16692 if (!NILP (tem))
16694 tem = Fsymbol_value (elt);
16695 /* If value is a string, output that string literally:
16696 don't check for % within it. */
16697 if (STRINGP (tem))
16698 literal = 1;
16700 if (!EQ (tem, elt))
16702 /* Give up right away for nil or t. */
16703 elt = tem;
16704 goto tail_recurse;
16708 break;
16710 case Lisp_Cons:
16712 register Lisp_Object car, tem;
16714 /* A cons cell: five distinct cases.
16715 If first element is :eval or :propertize, do something special.
16716 If first element is a string or a cons, process all the elements
16717 and effectively concatenate them.
16718 If first element is a negative number, truncate displaying cdr to
16719 at most that many characters. If positive, pad (with spaces)
16720 to at least that many characters.
16721 If first element is a symbol, process the cadr or caddr recursively
16722 according to whether the symbol's value is non-nil or nil. */
16723 car = XCAR (elt);
16724 if (EQ (car, QCeval))
16726 /* An element of the form (:eval FORM) means evaluate FORM
16727 and use the result as mode line elements. */
16729 if (risky)
16730 break;
16732 if (CONSP (XCDR (elt)))
16734 Lisp_Object spec;
16735 spec = safe_eval (XCAR (XCDR (elt)));
16736 n += display_mode_element (it, depth, field_width - n,
16737 precision - n, spec, props,
16738 risky);
16741 else if (EQ (car, QCpropertize))
16743 /* An element of the form (:propertize ELT PROPS...)
16744 means display ELT but applying properties PROPS. */
16746 if (risky)
16747 break;
16749 if (CONSP (XCDR (elt)))
16750 n += display_mode_element (it, depth, field_width - n,
16751 precision - n, XCAR (XCDR (elt)),
16752 XCDR (XCDR (elt)), risky);
16754 else if (SYMBOLP (car))
16756 tem = Fboundp (car);
16757 elt = XCDR (elt);
16758 if (!CONSP (elt))
16759 goto invalid;
16760 /* elt is now the cdr, and we know it is a cons cell.
16761 Use its car if CAR has a non-nil value. */
16762 if (!NILP (tem))
16764 tem = Fsymbol_value (car);
16765 if (!NILP (tem))
16767 elt = XCAR (elt);
16768 goto tail_recurse;
16771 /* Symbol's value is nil (or symbol is unbound)
16772 Get the cddr of the original list
16773 and if possible find the caddr and use that. */
16774 elt = XCDR (elt);
16775 if (NILP (elt))
16776 break;
16777 else if (!CONSP (elt))
16778 goto invalid;
16779 elt = XCAR (elt);
16780 goto tail_recurse;
16782 else if (INTEGERP (car))
16784 register int lim = XINT (car);
16785 elt = XCDR (elt);
16786 if (lim < 0)
16788 /* Negative int means reduce maximum width. */
16789 if (precision <= 0)
16790 precision = -lim;
16791 else
16792 precision = min (precision, -lim);
16794 else if (lim > 0)
16796 /* Padding specified. Don't let it be more than
16797 current maximum. */
16798 if (precision > 0)
16799 lim = min (precision, lim);
16801 /* If that's more padding than already wanted, queue it.
16802 But don't reduce padding already specified even if
16803 that is beyond the current truncation point. */
16804 field_width = max (lim, field_width);
16806 goto tail_recurse;
16808 else if (STRINGP (car) || CONSP (car))
16810 register int limit = 50;
16811 /* Limit is to protect against circular lists. */
16812 while (CONSP (elt)
16813 && --limit > 0
16814 && (precision <= 0 || n < precision))
16816 n += display_mode_element (it, depth,
16817 /* Do padding only after the last
16818 element in the list. */
16819 (! CONSP (XCDR (elt))
16820 ? field_width - n
16821 : 0),
16822 precision - n, XCAR (elt),
16823 props, risky);
16824 elt = XCDR (elt);
16828 break;
16830 default:
16831 invalid:
16832 elt = build_string ("*invalid*");
16833 goto tail_recurse;
16836 /* Pad to FIELD_WIDTH. */
16837 if (field_width > 0 && n < field_width)
16839 switch (mode_line_target)
16841 case MODE_LINE_NOPROP:
16842 case MODE_LINE_TITLE:
16843 n += store_mode_line_noprop ("", field_width - n, 0);
16844 break;
16845 case MODE_LINE_STRING:
16846 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16847 break;
16848 case MODE_LINE_DISPLAY:
16849 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16850 0, 0, 0);
16851 break;
16855 return n;
16858 /* Store a mode-line string element in mode_line_string_list.
16860 If STRING is non-null, display that C string. Otherwise, the Lisp
16861 string LISP_STRING is displayed.
16863 FIELD_WIDTH is the minimum number of output glyphs to produce.
16864 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16865 with spaces. FIELD_WIDTH <= 0 means don't pad.
16867 PRECISION is the maximum number of characters to output from
16868 STRING. PRECISION <= 0 means don't truncate the string.
16870 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16871 properties to the string.
16873 PROPS are the properties to add to the string.
16874 The mode_line_string_face face property is always added to the string.
16877 static int
16878 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16879 char *string;
16880 Lisp_Object lisp_string;
16881 int copy_string;
16882 int field_width;
16883 int precision;
16884 Lisp_Object props;
16886 int len;
16887 int n = 0;
16889 if (string != NULL)
16891 len = strlen (string);
16892 if (precision > 0 && len > precision)
16893 len = precision;
16894 lisp_string = make_string (string, len);
16895 if (NILP (props))
16896 props = mode_line_string_face_prop;
16897 else if (!NILP (mode_line_string_face))
16899 Lisp_Object face = Fplist_get (props, Qface);
16900 props = Fcopy_sequence (props);
16901 if (NILP (face))
16902 face = mode_line_string_face;
16903 else
16904 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16905 props = Fplist_put (props, Qface, face);
16907 Fadd_text_properties (make_number (0), make_number (len),
16908 props, lisp_string);
16910 else
16912 len = XFASTINT (Flength (lisp_string));
16913 if (precision > 0 && len > precision)
16915 len = precision;
16916 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16917 precision = -1;
16919 if (!NILP (mode_line_string_face))
16921 Lisp_Object face;
16922 if (NILP (props))
16923 props = Ftext_properties_at (make_number (0), lisp_string);
16924 face = Fplist_get (props, Qface);
16925 if (NILP (face))
16926 face = mode_line_string_face;
16927 else
16928 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16929 props = Fcons (Qface, Fcons (face, Qnil));
16930 if (copy_string)
16931 lisp_string = Fcopy_sequence (lisp_string);
16933 if (!NILP (props))
16934 Fadd_text_properties (make_number (0), make_number (len),
16935 props, lisp_string);
16938 if (len > 0)
16940 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16941 n += len;
16944 if (field_width > len)
16946 field_width -= len;
16947 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16948 if (!NILP (props))
16949 Fadd_text_properties (make_number (0), make_number (field_width),
16950 props, lisp_string);
16951 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16952 n += field_width;
16955 return n;
16959 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16960 1, 4, 0,
16961 doc: /* Format a string out of a mode line format specification.
16962 First arg FORMAT specifies the mode line format (see `mode-line-format'
16963 for details) to use.
16965 Optional second arg FACE specifies the face property to put
16966 on all characters for which no face is specified.
16967 t means whatever face the window's mode line currently uses
16968 \(either `mode-line' or `mode-line-inactive', depending).
16969 nil means the default is no face property.
16970 If FACE is an integer, the value string has no text properties.
16972 Optional third and fourth args WINDOW and BUFFER specify the window
16973 and buffer to use as the context for the formatting (defaults
16974 are the selected window and the window's buffer). */)
16975 (format, face, window, buffer)
16976 Lisp_Object format, face, window, buffer;
16978 struct it it;
16979 int len;
16980 struct window *w;
16981 struct buffer *old_buffer = NULL;
16982 int face_id = -1;
16983 int no_props = INTEGERP (face);
16984 int count = SPECPDL_INDEX ();
16985 Lisp_Object str;
16986 int string_start = 0;
16988 if (NILP (window))
16989 window = selected_window;
16990 CHECK_WINDOW (window);
16991 w = XWINDOW (window);
16993 if (NILP (buffer))
16994 buffer = w->buffer;
16995 CHECK_BUFFER (buffer);
16997 if (NILP (format))
16998 return build_string ("");
17000 if (no_props)
17001 face = Qnil;
17003 if (!NILP (face))
17005 if (EQ (face, Qt))
17006 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17007 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17010 if (face_id < 0)
17011 face_id = DEFAULT_FACE_ID;
17013 if (XBUFFER (buffer) != current_buffer)
17014 old_buffer = current_buffer;
17016 /* Save things including mode_line_proptrans_alist,
17017 and set that to nil so that we don't alter the outer value. */
17018 record_unwind_protect (unwind_format_mode_line,
17019 format_mode_line_unwind_data (old_buffer, 1));
17020 mode_line_proptrans_alist = Qnil;
17022 if (old_buffer)
17023 set_buffer_internal_1 (XBUFFER (buffer));
17025 init_iterator (&it, w, -1, -1, NULL, face_id);
17027 if (no_props)
17029 mode_line_target = MODE_LINE_NOPROP;
17030 mode_line_string_face_prop = Qnil;
17031 mode_line_string_list = Qnil;
17032 string_start = MODE_LINE_NOPROP_LEN (0);
17034 else
17036 mode_line_target = MODE_LINE_STRING;
17037 mode_line_string_list = Qnil;
17038 mode_line_string_face = face;
17039 mode_line_string_face_prop
17040 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17043 push_kboard (FRAME_KBOARD (it.f));
17044 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17045 pop_kboard ();
17047 if (no_props)
17049 len = MODE_LINE_NOPROP_LEN (string_start);
17050 str = make_string (mode_line_noprop_buf + string_start, len);
17052 else
17054 mode_line_string_list = Fnreverse (mode_line_string_list);
17055 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17056 make_string ("", 0));
17059 unbind_to (count, Qnil);
17060 return str;
17063 /* Write a null-terminated, right justified decimal representation of
17064 the positive integer D to BUF using a minimal field width WIDTH. */
17066 static void
17067 pint2str (buf, width, d)
17068 register char *buf;
17069 register int width;
17070 register int d;
17072 register char *p = buf;
17074 if (d <= 0)
17075 *p++ = '0';
17076 else
17078 while (d > 0)
17080 *p++ = d % 10 + '0';
17081 d /= 10;
17085 for (width -= (int) (p - buf); width > 0; --width)
17086 *p++ = ' ';
17087 *p-- = '\0';
17088 while (p > buf)
17090 d = *buf;
17091 *buf++ = *p;
17092 *p-- = d;
17096 /* Write a null-terminated, right justified decimal and "human
17097 readable" representation of the nonnegative integer D to BUF using
17098 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17100 static const char power_letter[] =
17102 0, /* not used */
17103 'k', /* kilo */
17104 'M', /* mega */
17105 'G', /* giga */
17106 'T', /* tera */
17107 'P', /* peta */
17108 'E', /* exa */
17109 'Z', /* zetta */
17110 'Y' /* yotta */
17113 static void
17114 pint2hrstr (buf, width, d)
17115 char *buf;
17116 int width;
17117 int d;
17119 /* We aim to represent the nonnegative integer D as
17120 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17121 int quotient = d;
17122 int remainder = 0;
17123 /* -1 means: do not use TENTHS. */
17124 int tenths = -1;
17125 int exponent = 0;
17127 /* Length of QUOTIENT.TENTHS as a string. */
17128 int length;
17130 char * psuffix;
17131 char * p;
17133 if (1000 <= quotient)
17135 /* Scale to the appropriate EXPONENT. */
17138 remainder = quotient % 1000;
17139 quotient /= 1000;
17140 exponent++;
17142 while (1000 <= quotient);
17144 /* Round to nearest and decide whether to use TENTHS or not. */
17145 if (quotient <= 9)
17147 tenths = remainder / 100;
17148 if (50 <= remainder % 100)
17150 if (tenths < 9)
17151 tenths++;
17152 else
17154 quotient++;
17155 if (quotient == 10)
17156 tenths = -1;
17157 else
17158 tenths = 0;
17162 else
17163 if (500 <= remainder)
17165 if (quotient < 999)
17166 quotient++;
17167 else
17169 quotient = 1;
17170 exponent++;
17171 tenths = 0;
17176 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17177 if (tenths == -1 && quotient <= 99)
17178 if (quotient <= 9)
17179 length = 1;
17180 else
17181 length = 2;
17182 else
17183 length = 3;
17184 p = psuffix = buf + max (width, length);
17186 /* Print EXPONENT. */
17187 if (exponent)
17188 *psuffix++ = power_letter[exponent];
17189 *psuffix = '\0';
17191 /* Print TENTHS. */
17192 if (tenths >= 0)
17194 *--p = '0' + tenths;
17195 *--p = '.';
17198 /* Print QUOTIENT. */
17201 int digit = quotient % 10;
17202 *--p = '0' + digit;
17204 while ((quotient /= 10) != 0);
17206 /* Print leading spaces. */
17207 while (buf < p)
17208 *--p = ' ';
17211 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17212 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17213 type of CODING_SYSTEM. Return updated pointer into BUF. */
17215 static unsigned char invalid_eol_type[] = "(*invalid*)";
17217 static char *
17218 decode_mode_spec_coding (coding_system, buf, eol_flag)
17219 Lisp_Object coding_system;
17220 register char *buf;
17221 int eol_flag;
17223 Lisp_Object val;
17224 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17225 const unsigned char *eol_str;
17226 int eol_str_len;
17227 /* The EOL conversion we are using. */
17228 Lisp_Object eoltype;
17230 val = Fget (coding_system, Qcoding_system);
17231 eoltype = Qnil;
17233 if (!VECTORP (val)) /* Not yet decided. */
17235 if (multibyte)
17236 *buf++ = '-';
17237 if (eol_flag)
17238 eoltype = eol_mnemonic_undecided;
17239 /* Don't mention EOL conversion if it isn't decided. */
17241 else
17243 Lisp_Object eolvalue;
17245 eolvalue = Fget (coding_system, Qeol_type);
17247 if (multibyte)
17248 *buf++ = XFASTINT (AREF (val, 1));
17250 if (eol_flag)
17252 /* The EOL conversion that is normal on this system. */
17254 if (NILP (eolvalue)) /* Not yet decided. */
17255 eoltype = eol_mnemonic_undecided;
17256 else if (VECTORP (eolvalue)) /* Not yet decided. */
17257 eoltype = eol_mnemonic_undecided;
17258 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17259 eoltype = (XFASTINT (eolvalue) == 0
17260 ? eol_mnemonic_unix
17261 : (XFASTINT (eolvalue) == 1
17262 ? eol_mnemonic_dos : eol_mnemonic_mac));
17266 if (eol_flag)
17268 /* Mention the EOL conversion if it is not the usual one. */
17269 if (STRINGP (eoltype))
17271 eol_str = SDATA (eoltype);
17272 eol_str_len = SBYTES (eoltype);
17274 else if (INTEGERP (eoltype)
17275 && CHAR_VALID_P (XINT (eoltype), 0))
17277 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17278 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17279 eol_str = tmp;
17281 else
17283 eol_str = invalid_eol_type;
17284 eol_str_len = sizeof (invalid_eol_type) - 1;
17286 bcopy (eol_str, buf, eol_str_len);
17287 buf += eol_str_len;
17290 return buf;
17293 /* Return a string for the output of a mode line %-spec for window W,
17294 generated by character C. PRECISION >= 0 means don't return a
17295 string longer than that value. FIELD_WIDTH > 0 means pad the
17296 string returned with spaces to that value. Return 1 in *MULTIBYTE
17297 if the result is multibyte text.
17299 Note we operate on the current buffer for most purposes,
17300 the exception being w->base_line_pos. */
17302 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17304 static char *
17305 decode_mode_spec (w, c, field_width, precision, multibyte)
17306 struct window *w;
17307 register int c;
17308 int field_width, precision;
17309 int *multibyte;
17311 Lisp_Object obj;
17312 struct frame *f = XFRAME (WINDOW_FRAME (w));
17313 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17314 struct buffer *b = current_buffer;
17316 obj = Qnil;
17317 *multibyte = 0;
17319 switch (c)
17321 case '*':
17322 if (!NILP (b->read_only))
17323 return "%";
17324 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17325 return "*";
17326 return "-";
17328 case '+':
17329 /* This differs from %* only for a modified read-only buffer. */
17330 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17331 return "*";
17332 if (!NILP (b->read_only))
17333 return "%";
17334 return "-";
17336 case '&':
17337 /* This differs from %* in ignoring read-only-ness. */
17338 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17339 return "*";
17340 return "-";
17342 case '%':
17343 return "%";
17345 case '[':
17347 int i;
17348 char *p;
17350 if (command_loop_level > 5)
17351 return "[[[... ";
17352 p = decode_mode_spec_buf;
17353 for (i = 0; i < command_loop_level; i++)
17354 *p++ = '[';
17355 *p = 0;
17356 return decode_mode_spec_buf;
17359 case ']':
17361 int i;
17362 char *p;
17364 if (command_loop_level > 5)
17365 return " ...]]]";
17366 p = decode_mode_spec_buf;
17367 for (i = 0; i < command_loop_level; i++)
17368 *p++ = ']';
17369 *p = 0;
17370 return decode_mode_spec_buf;
17373 case '-':
17375 register int i;
17377 /* Let lots_of_dashes be a string of infinite length. */
17378 if (mode_line_target == MODE_LINE_NOPROP ||
17379 mode_line_target == MODE_LINE_STRING)
17380 return "--";
17381 if (field_width <= 0
17382 || field_width > sizeof (lots_of_dashes))
17384 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17385 decode_mode_spec_buf[i] = '-';
17386 decode_mode_spec_buf[i] = '\0';
17387 return decode_mode_spec_buf;
17389 else
17390 return lots_of_dashes;
17393 case 'b':
17394 obj = b->name;
17395 break;
17397 case 'c':
17399 int col = (int) current_column (); /* iftc */
17400 w->column_number_displayed = make_number (col);
17401 pint2str (decode_mode_spec_buf, field_width, col);
17402 return decode_mode_spec_buf;
17405 case 'e':
17406 #ifndef SYSTEM_MALLOC
17408 if (NILP (Vmemory_full))
17409 return "";
17410 else
17411 return "!MEM FULL! ";
17413 #else
17414 return "";
17415 #endif
17417 case 'F':
17418 /* %F displays the frame name. */
17419 if (!NILP (f->title))
17420 return (char *) SDATA (f->title);
17421 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17422 return (char *) SDATA (f->name);
17423 return "Emacs";
17425 case 'f':
17426 obj = b->filename;
17427 break;
17429 case 'i':
17431 int size = ZV - BEGV;
17432 pint2str (decode_mode_spec_buf, field_width, size);
17433 return decode_mode_spec_buf;
17436 case 'I':
17438 int size = ZV - BEGV;
17439 pint2hrstr (decode_mode_spec_buf, field_width, size);
17440 return decode_mode_spec_buf;
17443 case 'l':
17445 int startpos = XMARKER (w->start)->charpos;
17446 int startpos_byte = marker_byte_position (w->start);
17447 int line, linepos, linepos_byte, topline;
17448 int nlines, junk;
17449 int height = WINDOW_TOTAL_LINES (w);
17451 /* If we decided that this buffer isn't suitable for line numbers,
17452 don't forget that too fast. */
17453 if (EQ (w->base_line_pos, w->buffer))
17454 goto no_value;
17455 /* But do forget it, if the window shows a different buffer now. */
17456 else if (BUFFERP (w->base_line_pos))
17457 w->base_line_pos = Qnil;
17459 /* If the buffer is very big, don't waste time. */
17460 if (INTEGERP (Vline_number_display_limit)
17461 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17463 w->base_line_pos = Qnil;
17464 w->base_line_number = Qnil;
17465 goto no_value;
17468 if (!NILP (w->base_line_number)
17469 && !NILP (w->base_line_pos)
17470 && XFASTINT (w->base_line_pos) <= startpos)
17472 line = XFASTINT (w->base_line_number);
17473 linepos = XFASTINT (w->base_line_pos);
17474 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17476 else
17478 line = 1;
17479 linepos = BUF_BEGV (b);
17480 linepos_byte = BUF_BEGV_BYTE (b);
17483 /* Count lines from base line to window start position. */
17484 nlines = display_count_lines (linepos, linepos_byte,
17485 startpos_byte,
17486 startpos, &junk);
17488 topline = nlines + line;
17490 /* Determine a new base line, if the old one is too close
17491 or too far away, or if we did not have one.
17492 "Too close" means it's plausible a scroll-down would
17493 go back past it. */
17494 if (startpos == BUF_BEGV (b))
17496 w->base_line_number = make_number (topline);
17497 w->base_line_pos = make_number (BUF_BEGV (b));
17499 else if (nlines < height + 25 || nlines > height * 3 + 50
17500 || linepos == BUF_BEGV (b))
17502 int limit = BUF_BEGV (b);
17503 int limit_byte = BUF_BEGV_BYTE (b);
17504 int position;
17505 int distance = (height * 2 + 30) * line_number_display_limit_width;
17507 if (startpos - distance > limit)
17509 limit = startpos - distance;
17510 limit_byte = CHAR_TO_BYTE (limit);
17513 nlines = display_count_lines (startpos, startpos_byte,
17514 limit_byte,
17515 - (height * 2 + 30),
17516 &position);
17517 /* If we couldn't find the lines we wanted within
17518 line_number_display_limit_width chars per line,
17519 give up on line numbers for this window. */
17520 if (position == limit_byte && limit == startpos - distance)
17522 w->base_line_pos = w->buffer;
17523 w->base_line_number = Qnil;
17524 goto no_value;
17527 w->base_line_number = make_number (topline - nlines);
17528 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17531 /* Now count lines from the start pos to point. */
17532 nlines = display_count_lines (startpos, startpos_byte,
17533 PT_BYTE, PT, &junk);
17535 /* Record that we did display the line number. */
17536 line_number_displayed = 1;
17538 /* Make the string to show. */
17539 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17540 return decode_mode_spec_buf;
17541 no_value:
17543 char* p = decode_mode_spec_buf;
17544 int pad = field_width - 2;
17545 while (pad-- > 0)
17546 *p++ = ' ';
17547 *p++ = '?';
17548 *p++ = '?';
17549 *p = '\0';
17550 return decode_mode_spec_buf;
17553 break;
17555 case 'm':
17556 obj = b->mode_name;
17557 break;
17559 case 'n':
17560 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17561 return " Narrow";
17562 break;
17564 case 'p':
17566 int pos = marker_position (w->start);
17567 int total = BUF_ZV (b) - BUF_BEGV (b);
17569 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17571 if (pos <= BUF_BEGV (b))
17572 return "All";
17573 else
17574 return "Bottom";
17576 else if (pos <= BUF_BEGV (b))
17577 return "Top";
17578 else
17580 if (total > 1000000)
17581 /* Do it differently for a large value, to avoid overflow. */
17582 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17583 else
17584 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17585 /* We can't normally display a 3-digit number,
17586 so get us a 2-digit number that is close. */
17587 if (total == 100)
17588 total = 99;
17589 sprintf (decode_mode_spec_buf, "%2d%%", total);
17590 return decode_mode_spec_buf;
17594 /* Display percentage of size above the bottom of the screen. */
17595 case 'P':
17597 int toppos = marker_position (w->start);
17598 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17599 int total = BUF_ZV (b) - BUF_BEGV (b);
17601 if (botpos >= BUF_ZV (b))
17603 if (toppos <= BUF_BEGV (b))
17604 return "All";
17605 else
17606 return "Bottom";
17608 else
17610 if (total > 1000000)
17611 /* Do it differently for a large value, to avoid overflow. */
17612 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17613 else
17614 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17615 /* We can't normally display a 3-digit number,
17616 so get us a 2-digit number that is close. */
17617 if (total == 100)
17618 total = 99;
17619 if (toppos <= BUF_BEGV (b))
17620 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17621 else
17622 sprintf (decode_mode_spec_buf, "%2d%%", total);
17623 return decode_mode_spec_buf;
17627 case 's':
17628 /* status of process */
17629 obj = Fget_buffer_process (Fcurrent_buffer ());
17630 if (NILP (obj))
17631 return "no process";
17632 #ifdef subprocesses
17633 obj = Fsymbol_name (Fprocess_status (obj));
17634 #endif
17635 break;
17637 case 't': /* indicate TEXT or BINARY */
17638 #ifdef MODE_LINE_BINARY_TEXT
17639 return MODE_LINE_BINARY_TEXT (b);
17640 #else
17641 return "T";
17642 #endif
17644 case 'z':
17645 /* coding-system (not including end-of-line format) */
17646 case 'Z':
17647 /* coding-system (including end-of-line type) */
17649 int eol_flag = (c == 'Z');
17650 char *p = decode_mode_spec_buf;
17652 if (! FRAME_WINDOW_P (f))
17654 /* No need to mention EOL here--the terminal never needs
17655 to do EOL conversion. */
17656 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
17657 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
17659 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17660 p, eol_flag);
17662 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17663 #ifdef subprocesses
17664 obj = Fget_buffer_process (Fcurrent_buffer ());
17665 if (PROCESSP (obj))
17667 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17668 p, eol_flag);
17669 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17670 p, eol_flag);
17672 #endif /* subprocesses */
17673 #endif /* 0 */
17674 *p = 0;
17675 return decode_mode_spec_buf;
17679 if (STRINGP (obj))
17681 *multibyte = STRING_MULTIBYTE (obj);
17682 return (char *) SDATA (obj);
17684 else
17685 return "";
17689 /* Count up to COUNT lines starting from START / START_BYTE.
17690 But don't go beyond LIMIT_BYTE.
17691 Return the number of lines thus found (always nonnegative).
17693 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17695 static int
17696 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17697 int start, start_byte, limit_byte, count;
17698 int *byte_pos_ptr;
17700 register unsigned char *cursor;
17701 unsigned char *base;
17703 register int ceiling;
17704 register unsigned char *ceiling_addr;
17705 int orig_count = count;
17707 /* If we are not in selective display mode,
17708 check only for newlines. */
17709 int selective_display = (!NILP (current_buffer->selective_display)
17710 && !INTEGERP (current_buffer->selective_display));
17712 if (count > 0)
17714 while (start_byte < limit_byte)
17716 ceiling = BUFFER_CEILING_OF (start_byte);
17717 ceiling = min (limit_byte - 1, ceiling);
17718 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17719 base = (cursor = BYTE_POS_ADDR (start_byte));
17720 while (1)
17722 if (selective_display)
17723 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17725 else
17726 while (*cursor != '\n' && ++cursor != ceiling_addr)
17729 if (cursor != ceiling_addr)
17731 if (--count == 0)
17733 start_byte += cursor - base + 1;
17734 *byte_pos_ptr = start_byte;
17735 return orig_count;
17737 else
17738 if (++cursor == ceiling_addr)
17739 break;
17741 else
17742 break;
17744 start_byte += cursor - base;
17747 else
17749 while (start_byte > limit_byte)
17751 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17752 ceiling = max (limit_byte, ceiling);
17753 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17754 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17755 while (1)
17757 if (selective_display)
17758 while (--cursor != ceiling_addr
17759 && *cursor != '\n' && *cursor != 015)
17761 else
17762 while (--cursor != ceiling_addr && *cursor != '\n')
17765 if (cursor != ceiling_addr)
17767 if (++count == 0)
17769 start_byte += cursor - base + 1;
17770 *byte_pos_ptr = start_byte;
17771 /* When scanning backwards, we should
17772 not count the newline posterior to which we stop. */
17773 return - orig_count - 1;
17776 else
17777 break;
17779 /* Here we add 1 to compensate for the last decrement
17780 of CURSOR, which took it past the valid range. */
17781 start_byte += cursor - base + 1;
17785 *byte_pos_ptr = limit_byte;
17787 if (count < 0)
17788 return - orig_count + count;
17789 return orig_count - count;
17795 /***********************************************************************
17796 Displaying strings
17797 ***********************************************************************/
17799 /* Display a NUL-terminated string, starting with index START.
17801 If STRING is non-null, display that C string. Otherwise, the Lisp
17802 string LISP_STRING is displayed.
17804 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17805 FACE_STRING. Display STRING or LISP_STRING with the face at
17806 FACE_STRING_POS in FACE_STRING:
17808 Display the string in the environment given by IT, but use the
17809 standard display table, temporarily.
17811 FIELD_WIDTH is the minimum number of output glyphs to produce.
17812 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17813 with spaces. If STRING has more characters, more than FIELD_WIDTH
17814 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17816 PRECISION is the maximum number of characters to output from
17817 STRING. PRECISION < 0 means don't truncate the string.
17819 This is roughly equivalent to printf format specifiers:
17821 FIELD_WIDTH PRECISION PRINTF
17822 ----------------------------------------
17823 -1 -1 %s
17824 -1 10 %.10s
17825 10 -1 %10s
17826 20 10 %20.10s
17828 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17829 display them, and < 0 means obey the current buffer's value of
17830 enable_multibyte_characters.
17832 Value is the number of columns displayed. */
17834 static int
17835 display_string (string, lisp_string, face_string, face_string_pos,
17836 start, it, field_width, precision, max_x, multibyte)
17837 unsigned char *string;
17838 Lisp_Object lisp_string;
17839 Lisp_Object face_string;
17840 int face_string_pos;
17841 int start;
17842 struct it *it;
17843 int field_width, precision, max_x;
17844 int multibyte;
17846 int hpos_at_start = it->hpos;
17847 int saved_face_id = it->face_id;
17848 struct glyph_row *row = it->glyph_row;
17850 /* Initialize the iterator IT for iteration over STRING beginning
17851 with index START. */
17852 reseat_to_string (it, string, lisp_string, start,
17853 precision, field_width, multibyte);
17855 /* If displaying STRING, set up the face of the iterator
17856 from LISP_STRING, if that's given. */
17857 if (STRINGP (face_string))
17859 int endptr;
17860 struct face *face;
17862 it->face_id
17863 = face_at_string_position (it->w, face_string, face_string_pos,
17864 0, it->region_beg_charpos,
17865 it->region_end_charpos,
17866 &endptr, it->base_face_id, 0);
17867 face = FACE_FROM_ID (it->f, it->face_id);
17868 it->face_box_p = face->box != FACE_NO_BOX;
17871 /* Set max_x to the maximum allowed X position. Don't let it go
17872 beyond the right edge of the window. */
17873 if (max_x <= 0)
17874 max_x = it->last_visible_x;
17875 else
17876 max_x = min (max_x, it->last_visible_x);
17878 /* Skip over display elements that are not visible. because IT->w is
17879 hscrolled. */
17880 if (it->current_x < it->first_visible_x)
17881 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17882 MOVE_TO_POS | MOVE_TO_X);
17884 row->ascent = it->max_ascent;
17885 row->height = it->max_ascent + it->max_descent;
17886 row->phys_ascent = it->max_phys_ascent;
17887 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17888 row->extra_line_spacing = it->max_extra_line_spacing;
17890 /* This condition is for the case that we are called with current_x
17891 past last_visible_x. */
17892 while (it->current_x < max_x)
17894 int x_before, x, n_glyphs_before, i, nglyphs;
17896 /* Get the next display element. */
17897 if (!get_next_display_element (it))
17898 break;
17900 /* Produce glyphs. */
17901 x_before = it->current_x;
17902 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17903 PRODUCE_GLYPHS (it);
17905 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17906 i = 0;
17907 x = x_before;
17908 while (i < nglyphs)
17910 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17912 if (!it->truncate_lines_p
17913 && x + glyph->pixel_width > max_x)
17915 /* End of continued line or max_x reached. */
17916 if (CHAR_GLYPH_PADDING_P (*glyph))
17918 /* A wide character is unbreakable. */
17919 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17920 it->current_x = x_before;
17922 else
17924 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17925 it->current_x = x;
17927 break;
17929 else if (x + glyph->pixel_width > it->first_visible_x)
17931 /* Glyph is at least partially visible. */
17932 ++it->hpos;
17933 if (x < it->first_visible_x)
17934 it->glyph_row->x = x - it->first_visible_x;
17936 else
17938 /* Glyph is off the left margin of the display area.
17939 Should not happen. */
17940 abort ();
17943 row->ascent = max (row->ascent, it->max_ascent);
17944 row->height = max (row->height, it->max_ascent + it->max_descent);
17945 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17946 row->phys_height = max (row->phys_height,
17947 it->max_phys_ascent + it->max_phys_descent);
17948 row->extra_line_spacing = max (row->extra_line_spacing,
17949 it->max_extra_line_spacing);
17950 x += glyph->pixel_width;
17951 ++i;
17954 /* Stop if max_x reached. */
17955 if (i < nglyphs)
17956 break;
17958 /* Stop at line ends. */
17959 if (ITERATOR_AT_END_OF_LINE_P (it))
17961 it->continuation_lines_width = 0;
17962 break;
17965 set_iterator_to_next (it, 1);
17967 /* Stop if truncating at the right edge. */
17968 if (it->truncate_lines_p
17969 && it->current_x >= it->last_visible_x)
17971 /* Add truncation mark, but don't do it if the line is
17972 truncated at a padding space. */
17973 if (IT_CHARPOS (*it) < it->string_nchars)
17975 if (!FRAME_WINDOW_P (it->f))
17977 int i, n;
17979 if (it->current_x > it->last_visible_x)
17981 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17982 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17983 break;
17984 for (n = row->used[TEXT_AREA]; i < n; ++i)
17986 row->used[TEXT_AREA] = i;
17987 produce_special_glyphs (it, IT_TRUNCATION);
17990 produce_special_glyphs (it, IT_TRUNCATION);
17992 it->glyph_row->truncated_on_right_p = 1;
17994 break;
17998 /* Maybe insert a truncation at the left. */
17999 if (it->first_visible_x
18000 && IT_CHARPOS (*it) > 0)
18002 if (!FRAME_WINDOW_P (it->f))
18003 insert_left_trunc_glyphs (it);
18004 it->glyph_row->truncated_on_left_p = 1;
18007 it->face_id = saved_face_id;
18009 /* Value is number of columns displayed. */
18010 return it->hpos - hpos_at_start;
18015 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18016 appears as an element of LIST or as the car of an element of LIST.
18017 If PROPVAL is a list, compare each element against LIST in that
18018 way, and return 1/2 if any element of PROPVAL is found in LIST.
18019 Otherwise return 0. This function cannot quit.
18020 The return value is 2 if the text is invisible but with an ellipsis
18021 and 1 if it's invisible and without an ellipsis. */
18024 invisible_p (propval, list)
18025 register Lisp_Object propval;
18026 Lisp_Object list;
18028 register Lisp_Object tail, proptail;
18030 for (tail = list; CONSP (tail); tail = XCDR (tail))
18032 register Lisp_Object tem;
18033 tem = XCAR (tail);
18034 if (EQ (propval, tem))
18035 return 1;
18036 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18037 return NILP (XCDR (tem)) ? 1 : 2;
18040 if (CONSP (propval))
18042 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18044 Lisp_Object propelt;
18045 propelt = XCAR (proptail);
18046 for (tail = list; CONSP (tail); tail = XCDR (tail))
18048 register Lisp_Object tem;
18049 tem = XCAR (tail);
18050 if (EQ (propelt, tem))
18051 return 1;
18052 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18053 return NILP (XCDR (tem)) ? 1 : 2;
18058 return 0;
18061 /* Calculate a width or height in pixels from a specification using
18062 the following elements:
18064 SPEC ::=
18065 NUM - a (fractional) multiple of the default font width/height
18066 (NUM) - specifies exactly NUM pixels
18067 UNIT - a fixed number of pixels, see below.
18068 ELEMENT - size of a display element in pixels, see below.
18069 (NUM . SPEC) - equals NUM * SPEC
18070 (+ SPEC SPEC ...) - add pixel values
18071 (- SPEC SPEC ...) - subtract pixel values
18072 (- SPEC) - negate pixel value
18074 NUM ::=
18075 INT or FLOAT - a number constant
18076 SYMBOL - use symbol's (buffer local) variable binding.
18078 UNIT ::=
18079 in - pixels per inch *)
18080 mm - pixels per 1/1000 meter *)
18081 cm - pixels per 1/100 meter *)
18082 width - width of current font in pixels.
18083 height - height of current font in pixels.
18085 *) using the ratio(s) defined in display-pixels-per-inch.
18087 ELEMENT ::=
18089 left-fringe - left fringe width in pixels
18090 right-fringe - right fringe width in pixels
18092 left-margin - left margin width in pixels
18093 right-margin - right margin width in pixels
18095 scroll-bar - scroll-bar area width in pixels
18097 Examples:
18099 Pixels corresponding to 5 inches:
18100 (5 . in)
18102 Total width of non-text areas on left side of window (if scroll-bar is on left):
18103 '(space :width (+ left-fringe left-margin scroll-bar))
18105 Align to first text column (in header line):
18106 '(space :align-to 0)
18108 Align to middle of text area minus half the width of variable `my-image'
18109 containing a loaded image:
18110 '(space :align-to (0.5 . (- text my-image)))
18112 Width of left margin minus width of 1 character in the default font:
18113 '(space :width (- left-margin 1))
18115 Width of left margin minus width of 2 characters in the current font:
18116 '(space :width (- left-margin (2 . width)))
18118 Center 1 character over left-margin (in header line):
18119 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18121 Different ways to express width of left fringe plus left margin minus one pixel:
18122 '(space :width (- (+ left-fringe left-margin) (1)))
18123 '(space :width (+ left-fringe left-margin (- (1))))
18124 '(space :width (+ left-fringe left-margin (-1)))
18128 #define NUMVAL(X) \
18129 ((INTEGERP (X) || FLOATP (X)) \
18130 ? XFLOATINT (X) \
18131 : - 1)
18134 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18135 double *res;
18136 struct it *it;
18137 Lisp_Object prop;
18138 void *font;
18139 int width_p, *align_to;
18141 double pixels;
18143 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18144 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18146 if (NILP (prop))
18147 return OK_PIXELS (0);
18149 xassert (FRAME_LIVE_P (it->f));
18151 if (SYMBOLP (prop))
18153 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18155 char *unit = SDATA (SYMBOL_NAME (prop));
18157 if (unit[0] == 'i' && unit[1] == 'n')
18158 pixels = 1.0;
18159 else if (unit[0] == 'm' && unit[1] == 'm')
18160 pixels = 25.4;
18161 else if (unit[0] == 'c' && unit[1] == 'm')
18162 pixels = 2.54;
18163 else
18164 pixels = 0;
18165 if (pixels > 0)
18167 double ppi;
18168 #ifdef HAVE_WINDOW_SYSTEM
18169 if (FRAME_WINDOW_P (it->f)
18170 && (ppi = (width_p
18171 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18172 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18173 ppi > 0))
18174 return OK_PIXELS (ppi / pixels);
18175 #endif
18177 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18178 || (CONSP (Vdisplay_pixels_per_inch)
18179 && (ppi = (width_p
18180 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18181 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18182 ppi > 0)))
18183 return OK_PIXELS (ppi / pixels);
18185 return 0;
18189 #ifdef HAVE_WINDOW_SYSTEM
18190 if (EQ (prop, Qheight))
18191 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18192 if (EQ (prop, Qwidth))
18193 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18194 #else
18195 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18196 return OK_PIXELS (1);
18197 #endif
18199 if (EQ (prop, Qtext))
18200 return OK_PIXELS (width_p
18201 ? window_box_width (it->w, TEXT_AREA)
18202 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18204 if (align_to && *align_to < 0)
18206 *res = 0;
18207 if (EQ (prop, Qleft))
18208 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18209 if (EQ (prop, Qright))
18210 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18211 if (EQ (prop, Qcenter))
18212 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18213 + window_box_width (it->w, TEXT_AREA) / 2);
18214 if (EQ (prop, Qleft_fringe))
18215 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18216 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18217 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18218 if (EQ (prop, Qright_fringe))
18219 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18220 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18221 : window_box_right_offset (it->w, TEXT_AREA));
18222 if (EQ (prop, Qleft_margin))
18223 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18224 if (EQ (prop, Qright_margin))
18225 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18226 if (EQ (prop, Qscroll_bar))
18227 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18229 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18230 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18231 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18232 : 0)));
18234 else
18236 if (EQ (prop, Qleft_fringe))
18237 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18238 if (EQ (prop, Qright_fringe))
18239 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18240 if (EQ (prop, Qleft_margin))
18241 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18242 if (EQ (prop, Qright_margin))
18243 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18244 if (EQ (prop, Qscroll_bar))
18245 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18248 prop = Fbuffer_local_value (prop, it->w->buffer);
18251 if (INTEGERP (prop) || FLOATP (prop))
18253 int base_unit = (width_p
18254 ? FRAME_COLUMN_WIDTH (it->f)
18255 : FRAME_LINE_HEIGHT (it->f));
18256 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18259 if (CONSP (prop))
18261 Lisp_Object car = XCAR (prop);
18262 Lisp_Object cdr = XCDR (prop);
18264 if (SYMBOLP (car))
18266 #ifdef HAVE_WINDOW_SYSTEM
18267 if (FRAME_WINDOW_P (it->f)
18268 && valid_image_p (prop))
18270 int id = lookup_image (it->f, prop);
18271 struct image *img = IMAGE_FROM_ID (it->f, id);
18273 return OK_PIXELS (width_p ? img->width : img->height);
18275 #endif
18276 if (EQ (car, Qplus) || EQ (car, Qminus))
18278 int first = 1;
18279 double px;
18281 pixels = 0;
18282 while (CONSP (cdr))
18284 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18285 font, width_p, align_to))
18286 return 0;
18287 if (first)
18288 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18289 else
18290 pixels += px;
18291 cdr = XCDR (cdr);
18293 if (EQ (car, Qminus))
18294 pixels = -pixels;
18295 return OK_PIXELS (pixels);
18298 car = Fbuffer_local_value (car, it->w->buffer);
18301 if (INTEGERP (car) || FLOATP (car))
18303 double fact;
18304 pixels = XFLOATINT (car);
18305 if (NILP (cdr))
18306 return OK_PIXELS (pixels);
18307 if (calc_pixel_width_or_height (&fact, it, cdr,
18308 font, width_p, align_to))
18309 return OK_PIXELS (pixels * fact);
18310 return 0;
18313 return 0;
18316 return 0;
18320 /***********************************************************************
18321 Glyph Display
18322 ***********************************************************************/
18324 #ifdef HAVE_WINDOW_SYSTEM
18326 #if GLYPH_DEBUG
18328 void
18329 dump_glyph_string (s)
18330 struct glyph_string *s;
18332 fprintf (stderr, "glyph string\n");
18333 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18334 s->x, s->y, s->width, s->height);
18335 fprintf (stderr, " ybase = %d\n", s->ybase);
18336 fprintf (stderr, " hl = %d\n", s->hl);
18337 fprintf (stderr, " left overhang = %d, right = %d\n",
18338 s->left_overhang, s->right_overhang);
18339 fprintf (stderr, " nchars = %d\n", s->nchars);
18340 fprintf (stderr, " extends to end of line = %d\n",
18341 s->extends_to_end_of_line_p);
18342 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18343 fprintf (stderr, " bg width = %d\n", s->background_width);
18346 #endif /* GLYPH_DEBUG */
18348 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18349 of XChar2b structures for S; it can't be allocated in
18350 init_glyph_string because it must be allocated via `alloca'. W
18351 is the window on which S is drawn. ROW and AREA are the glyph row
18352 and area within the row from which S is constructed. START is the
18353 index of the first glyph structure covered by S. HL is a
18354 face-override for drawing S. */
18356 #ifdef HAVE_NTGUI
18357 #define OPTIONAL_HDC(hdc) hdc,
18358 #define DECLARE_HDC(hdc) HDC hdc;
18359 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18360 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18361 #endif
18363 #ifndef OPTIONAL_HDC
18364 #define OPTIONAL_HDC(hdc)
18365 #define DECLARE_HDC(hdc)
18366 #define ALLOCATE_HDC(hdc, f)
18367 #define RELEASE_HDC(hdc, f)
18368 #endif
18370 static void
18371 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18372 struct glyph_string *s;
18373 DECLARE_HDC (hdc)
18374 XChar2b *char2b;
18375 struct window *w;
18376 struct glyph_row *row;
18377 enum glyph_row_area area;
18378 int start;
18379 enum draw_glyphs_face hl;
18381 bzero (s, sizeof *s);
18382 s->w = w;
18383 s->f = XFRAME (w->frame);
18384 #ifdef HAVE_NTGUI
18385 s->hdc = hdc;
18386 #endif
18387 s->display = FRAME_X_DISPLAY (s->f);
18388 s->window = FRAME_X_WINDOW (s->f);
18389 s->char2b = char2b;
18390 s->hl = hl;
18391 s->row = row;
18392 s->area = area;
18393 s->first_glyph = row->glyphs[area] + start;
18394 s->height = row->height;
18395 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18397 /* Display the internal border below the tool-bar window. */
18398 if (WINDOWP (s->f->tool_bar_window)
18399 && s->w == XWINDOW (s->f->tool_bar_window))
18400 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18402 s->ybase = s->y + row->ascent;
18406 /* Append the list of glyph strings with head H and tail T to the list
18407 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18409 static INLINE void
18410 append_glyph_string_lists (head, tail, h, t)
18411 struct glyph_string **head, **tail;
18412 struct glyph_string *h, *t;
18414 if (h)
18416 if (*head)
18417 (*tail)->next = h;
18418 else
18419 *head = h;
18420 h->prev = *tail;
18421 *tail = t;
18426 /* Prepend the list of glyph strings with head H and tail T to the
18427 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18428 result. */
18430 static INLINE void
18431 prepend_glyph_string_lists (head, tail, h, t)
18432 struct glyph_string **head, **tail;
18433 struct glyph_string *h, *t;
18435 if (h)
18437 if (*head)
18438 (*head)->prev = t;
18439 else
18440 *tail = t;
18441 t->next = *head;
18442 *head = h;
18447 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18448 Set *HEAD and *TAIL to the resulting list. */
18450 static INLINE void
18451 append_glyph_string (head, tail, s)
18452 struct glyph_string **head, **tail;
18453 struct glyph_string *s;
18455 s->next = s->prev = NULL;
18456 append_glyph_string_lists (head, tail, s, s);
18460 /* Get face and two-byte form of character glyph GLYPH on frame F.
18461 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18462 a pointer to a realized face that is ready for display. */
18464 static INLINE struct face *
18465 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18466 struct frame *f;
18467 struct glyph *glyph;
18468 XChar2b *char2b;
18469 int *two_byte_p;
18471 struct face *face;
18473 xassert (glyph->type == CHAR_GLYPH);
18474 face = FACE_FROM_ID (f, glyph->face_id);
18476 if (two_byte_p)
18477 *two_byte_p = 0;
18479 if (!glyph->multibyte_p)
18481 /* Unibyte case. We don't have to encode, but we have to make
18482 sure to use a face suitable for unibyte. */
18483 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18485 else if (glyph->u.ch < 128
18486 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18488 /* Case of ASCII in a face known to fit ASCII. */
18489 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18491 else
18493 int c1, c2, charset;
18495 /* Split characters into bytes. If c2 is -1 afterwards, C is
18496 really a one-byte character so that byte1 is zero. */
18497 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18498 if (c2 > 0)
18499 STORE_XCHAR2B (char2b, c1, c2);
18500 else
18501 STORE_XCHAR2B (char2b, 0, c1);
18503 /* Maybe encode the character in *CHAR2B. */
18504 if (charset != CHARSET_ASCII)
18506 struct font_info *font_info
18507 = FONT_INFO_FROM_ID (f, face->font_info_id);
18508 if (font_info)
18509 glyph->font_type
18510 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18514 /* Make sure X resources of the face are allocated. */
18515 xassert (face != NULL);
18516 PREPARE_FACE_FOR_DISPLAY (f, face);
18517 return face;
18521 /* Fill glyph string S with composition components specified by S->cmp.
18523 FACES is an array of faces for all components of this composition.
18524 S->gidx is the index of the first component for S.
18526 OVERLAPS non-zero means S should draw the foreground only, and use
18527 its physical height for clipping. See also draw_glyphs.
18529 Value is the index of a component not in S. */
18531 static int
18532 fill_composite_glyph_string (s, faces, overlaps)
18533 struct glyph_string *s;
18534 struct face **faces;
18535 int overlaps;
18537 int i;
18539 xassert (s);
18541 s->for_overlaps = overlaps;
18543 s->face = faces[s->gidx];
18544 s->font = s->face->font;
18545 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18547 /* For all glyphs of this composition, starting at the offset
18548 S->gidx, until we reach the end of the definition or encounter a
18549 glyph that requires the different face, add it to S. */
18550 ++s->nchars;
18551 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18552 ++s->nchars;
18554 /* All glyph strings for the same composition has the same width,
18555 i.e. the width set for the first component of the composition. */
18557 s->width = s->first_glyph->pixel_width;
18559 /* If the specified font could not be loaded, use the frame's
18560 default font, but record the fact that we couldn't load it in
18561 the glyph string so that we can draw rectangles for the
18562 characters of the glyph string. */
18563 if (s->font == NULL)
18565 s->font_not_found_p = 1;
18566 s->font = FRAME_FONT (s->f);
18569 /* Adjust base line for subscript/superscript text. */
18570 s->ybase += s->first_glyph->voffset;
18572 xassert (s->face && s->face->gc);
18574 /* This glyph string must always be drawn with 16-bit functions. */
18575 s->two_byte_p = 1;
18577 return s->gidx + s->nchars;
18581 /* Fill glyph string S from a sequence of character glyphs.
18583 FACE_ID is the face id of the string. START is the index of the
18584 first glyph to consider, END is the index of the last + 1.
18585 OVERLAPS non-zero means S should draw the foreground only, and use
18586 its physical height for clipping. See also draw_glyphs.
18588 Value is the index of the first glyph not in S. */
18590 static int
18591 fill_glyph_string (s, face_id, start, end, overlaps)
18592 struct glyph_string *s;
18593 int face_id;
18594 int start, end, overlaps;
18596 struct glyph *glyph, *last;
18597 int voffset;
18598 int glyph_not_available_p;
18600 xassert (s->f == XFRAME (s->w->frame));
18601 xassert (s->nchars == 0);
18602 xassert (start >= 0 && end > start);
18604 s->for_overlaps = overlaps,
18605 glyph = s->row->glyphs[s->area] + start;
18606 last = s->row->glyphs[s->area] + end;
18607 voffset = glyph->voffset;
18609 glyph_not_available_p = glyph->glyph_not_available_p;
18611 while (glyph < last
18612 && glyph->type == CHAR_GLYPH
18613 && glyph->voffset == voffset
18614 /* Same face id implies same font, nowadays. */
18615 && glyph->face_id == face_id
18616 && glyph->glyph_not_available_p == glyph_not_available_p)
18618 int two_byte_p;
18620 s->face = get_glyph_face_and_encoding (s->f, glyph,
18621 s->char2b + s->nchars,
18622 &two_byte_p);
18623 s->two_byte_p = two_byte_p;
18624 ++s->nchars;
18625 xassert (s->nchars <= end - start);
18626 s->width += glyph->pixel_width;
18627 ++glyph;
18630 s->font = s->face->font;
18631 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18633 /* If the specified font could not be loaded, use the frame's font,
18634 but record the fact that we couldn't load it in
18635 S->font_not_found_p so that we can draw rectangles for the
18636 characters of the glyph string. */
18637 if (s->font == NULL || glyph_not_available_p)
18639 s->font_not_found_p = 1;
18640 s->font = FRAME_FONT (s->f);
18643 /* Adjust base line for subscript/superscript text. */
18644 s->ybase += voffset;
18646 xassert (s->face && s->face->gc);
18647 return glyph - s->row->glyphs[s->area];
18651 /* Fill glyph string S from image glyph S->first_glyph. */
18653 static void
18654 fill_image_glyph_string (s)
18655 struct glyph_string *s;
18657 xassert (s->first_glyph->type == IMAGE_GLYPH);
18658 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18659 xassert (s->img);
18660 s->slice = s->first_glyph->slice;
18661 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18662 s->font = s->face->font;
18663 s->width = s->first_glyph->pixel_width;
18665 /* Adjust base line for subscript/superscript text. */
18666 s->ybase += s->first_glyph->voffset;
18670 /* Fill glyph string S from a sequence of stretch glyphs.
18672 ROW is the glyph row in which the glyphs are found, AREA is the
18673 area within the row. START is the index of the first glyph to
18674 consider, END is the index of the last + 1.
18676 Value is the index of the first glyph not in S. */
18678 static int
18679 fill_stretch_glyph_string (s, row, area, start, end)
18680 struct glyph_string *s;
18681 struct glyph_row *row;
18682 enum glyph_row_area area;
18683 int start, end;
18685 struct glyph *glyph, *last;
18686 int voffset, face_id;
18688 xassert (s->first_glyph->type == STRETCH_GLYPH);
18690 glyph = s->row->glyphs[s->area] + start;
18691 last = s->row->glyphs[s->area] + end;
18692 face_id = glyph->face_id;
18693 s->face = FACE_FROM_ID (s->f, face_id);
18694 s->font = s->face->font;
18695 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18696 s->width = glyph->pixel_width;
18697 voffset = glyph->voffset;
18699 for (++glyph;
18700 (glyph < last
18701 && glyph->type == STRETCH_GLYPH
18702 && glyph->voffset == voffset
18703 && glyph->face_id == face_id);
18704 ++glyph)
18705 s->width += glyph->pixel_width;
18707 /* Adjust base line for subscript/superscript text. */
18708 s->ybase += voffset;
18710 /* The case that face->gc == 0 is handled when drawing the glyph
18711 string by calling PREPARE_FACE_FOR_DISPLAY. */
18712 xassert (s->face);
18713 return glyph - s->row->glyphs[s->area];
18717 /* EXPORT for RIF:
18718 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18719 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18720 assumed to be zero. */
18722 void
18723 x_get_glyph_overhangs (glyph, f, left, right)
18724 struct glyph *glyph;
18725 struct frame *f;
18726 int *left, *right;
18728 *left = *right = 0;
18730 if (glyph->type == CHAR_GLYPH)
18732 XFontStruct *font;
18733 struct face *face;
18734 struct font_info *font_info;
18735 XChar2b char2b;
18736 XCharStruct *pcm;
18738 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18739 font = face->font;
18740 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18741 if (font /* ++KFS: Should this be font_info ? */
18742 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
18744 if (pcm->rbearing > pcm->width)
18745 *right = pcm->rbearing - pcm->width;
18746 if (pcm->lbearing < 0)
18747 *left = -pcm->lbearing;
18753 /* Return the index of the first glyph preceding glyph string S that
18754 is overwritten by S because of S's left overhang. Value is -1
18755 if no glyphs are overwritten. */
18757 static int
18758 left_overwritten (s)
18759 struct glyph_string *s;
18761 int k;
18763 if (s->left_overhang)
18765 int x = 0, i;
18766 struct glyph *glyphs = s->row->glyphs[s->area];
18767 int first = s->first_glyph - glyphs;
18769 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18770 x -= glyphs[i].pixel_width;
18772 k = i + 1;
18774 else
18775 k = -1;
18777 return k;
18781 /* Return the index of the first glyph preceding glyph string S that
18782 is overwriting S because of its right overhang. Value is -1 if no
18783 glyph in front of S overwrites S. */
18785 static int
18786 left_overwriting (s)
18787 struct glyph_string *s;
18789 int i, k, x;
18790 struct glyph *glyphs = s->row->glyphs[s->area];
18791 int first = s->first_glyph - glyphs;
18793 k = -1;
18794 x = 0;
18795 for (i = first - 1; i >= 0; --i)
18797 int left, right;
18798 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18799 if (x + right > 0)
18800 k = i;
18801 x -= glyphs[i].pixel_width;
18804 return k;
18808 /* Return the index of the last glyph following glyph string S that is
18809 not overwritten by S because of S's right overhang. Value is -1 if
18810 no such glyph is found. */
18812 static int
18813 right_overwritten (s)
18814 struct glyph_string *s;
18816 int k = -1;
18818 if (s->right_overhang)
18820 int x = 0, i;
18821 struct glyph *glyphs = s->row->glyphs[s->area];
18822 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18823 int end = s->row->used[s->area];
18825 for (i = first; i < end && s->right_overhang > x; ++i)
18826 x += glyphs[i].pixel_width;
18828 k = i;
18831 return k;
18835 /* Return the index of the last glyph following glyph string S that
18836 overwrites S because of its left overhang. Value is negative
18837 if no such glyph is found. */
18839 static int
18840 right_overwriting (s)
18841 struct glyph_string *s;
18843 int i, k, x;
18844 int end = s->row->used[s->area];
18845 struct glyph *glyphs = s->row->glyphs[s->area];
18846 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18848 k = -1;
18849 x = 0;
18850 for (i = first; i < end; ++i)
18852 int left, right;
18853 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18854 if (x - left < 0)
18855 k = i;
18856 x += glyphs[i].pixel_width;
18859 return k;
18863 /* Get face and two-byte form of character C in face FACE_ID on frame
18864 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18865 means we want to display multibyte text. DISPLAY_P non-zero means
18866 make sure that X resources for the face returned are allocated.
18867 Value is a pointer to a realized face that is ready for display if
18868 DISPLAY_P is non-zero. */
18870 static INLINE struct face *
18871 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18872 struct frame *f;
18873 int c, face_id;
18874 XChar2b *char2b;
18875 int multibyte_p, display_p;
18877 struct face *face = FACE_FROM_ID (f, face_id);
18879 if (!multibyte_p)
18881 /* Unibyte case. We don't have to encode, but we have to make
18882 sure to use a face suitable for unibyte. */
18883 STORE_XCHAR2B (char2b, 0, c);
18884 face_id = FACE_FOR_CHAR (f, face, c);
18885 face = FACE_FROM_ID (f, face_id);
18887 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18889 /* Case of ASCII in a face known to fit ASCII. */
18890 STORE_XCHAR2B (char2b, 0, c);
18892 else
18894 int c1, c2, charset;
18896 /* Split characters into bytes. If c2 is -1 afterwards, C is
18897 really a one-byte character so that byte1 is zero. */
18898 SPLIT_CHAR (c, charset, c1, c2);
18899 if (c2 > 0)
18900 STORE_XCHAR2B (char2b, c1, c2);
18901 else
18902 STORE_XCHAR2B (char2b, 0, c1);
18904 /* Maybe encode the character in *CHAR2B. */
18905 if (face->font != NULL)
18907 struct font_info *font_info
18908 = FONT_INFO_FROM_ID (f, face->font_info_id);
18909 if (font_info)
18910 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
18914 /* Make sure X resources of the face are allocated. */
18915 #ifdef HAVE_X_WINDOWS
18916 if (display_p)
18917 #endif
18919 xassert (face != NULL);
18920 PREPARE_FACE_FOR_DISPLAY (f, face);
18923 return face;
18927 /* Set background width of glyph string S. START is the index of the
18928 first glyph following S. LAST_X is the right-most x-position + 1
18929 in the drawing area. */
18931 static INLINE void
18932 set_glyph_string_background_width (s, start, last_x)
18933 struct glyph_string *s;
18934 int start;
18935 int last_x;
18937 /* If the face of this glyph string has to be drawn to the end of
18938 the drawing area, set S->extends_to_end_of_line_p. */
18940 if (start == s->row->used[s->area]
18941 && s->area == TEXT_AREA
18942 && ((s->row->fill_line_p
18943 && (s->hl == DRAW_NORMAL_TEXT
18944 || s->hl == DRAW_IMAGE_RAISED
18945 || s->hl == DRAW_IMAGE_SUNKEN))
18946 || s->hl == DRAW_MOUSE_FACE))
18947 s->extends_to_end_of_line_p = 1;
18949 /* If S extends its face to the end of the line, set its
18950 background_width to the distance to the right edge of the drawing
18951 area. */
18952 if (s->extends_to_end_of_line_p)
18953 s->background_width = last_x - s->x + 1;
18954 else
18955 s->background_width = s->width;
18959 /* Compute overhangs and x-positions for glyph string S and its
18960 predecessors, or successors. X is the starting x-position for S.
18961 BACKWARD_P non-zero means process predecessors. */
18963 static void
18964 compute_overhangs_and_x (s, x, backward_p)
18965 struct glyph_string *s;
18966 int x;
18967 int backward_p;
18969 if (backward_p)
18971 while (s)
18973 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18974 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18975 x -= s->width;
18976 s->x = x;
18977 s = s->prev;
18980 else
18982 while (s)
18984 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
18985 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
18986 s->x = x;
18987 x += s->width;
18988 s = s->next;
18995 /* The following macros are only called from draw_glyphs below.
18996 They reference the following parameters of that function directly:
18997 `w', `row', `area', and `overlap_p'
18998 as well as the following local variables:
18999 `s', `f', and `hdc' (in W32) */
19001 #ifdef HAVE_NTGUI
19002 /* On W32, silently add local `hdc' variable to argument list of
19003 init_glyph_string. */
19004 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19005 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19006 #else
19007 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19008 init_glyph_string (s, char2b, w, row, area, start, hl)
19009 #endif
19011 /* Add a glyph string for a stretch glyph to the list of strings
19012 between HEAD and TAIL. START is the index of the stretch glyph in
19013 row area AREA of glyph row ROW. END is the index of the last glyph
19014 in that glyph row area. X is the current output position assigned
19015 to the new glyph string constructed. HL overrides that face of the
19016 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19017 is the right-most x-position of the drawing area. */
19019 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19020 and below -- keep them on one line. */
19021 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19022 do \
19024 s = (struct glyph_string *) alloca (sizeof *s); \
19025 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19026 START = fill_stretch_glyph_string (s, row, area, START, END); \
19027 append_glyph_string (&HEAD, &TAIL, s); \
19028 s->x = (X); \
19030 while (0)
19033 /* Add a glyph string for an image glyph to the list of strings
19034 between HEAD and TAIL. START is the index of the image glyph in
19035 row area AREA of glyph row ROW. END is the index of the last glyph
19036 in that glyph row area. X is the current output position assigned
19037 to the new glyph string constructed. HL overrides that face of the
19038 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19039 is the right-most x-position of the drawing area. */
19041 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19042 do \
19044 s = (struct glyph_string *) alloca (sizeof *s); \
19045 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19046 fill_image_glyph_string (s); \
19047 append_glyph_string (&HEAD, &TAIL, s); \
19048 ++START; \
19049 s->x = (X); \
19051 while (0)
19054 /* Add a glyph string for a sequence of character glyphs to the list
19055 of strings between HEAD and TAIL. START is the index of the first
19056 glyph in row area AREA of glyph row ROW that is part of the new
19057 glyph string. END is the index of the last glyph in that glyph row
19058 area. X is the current output position assigned to the new glyph
19059 string constructed. HL overrides that face of the glyph; e.g. it
19060 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19061 right-most x-position of the drawing area. */
19063 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19064 do \
19066 int c, face_id; \
19067 XChar2b *char2b; \
19069 c = (row)->glyphs[area][START].u.ch; \
19070 face_id = (row)->glyphs[area][START].face_id; \
19072 s = (struct glyph_string *) alloca (sizeof *s); \
19073 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19074 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19075 append_glyph_string (&HEAD, &TAIL, s); \
19076 s->x = (X); \
19077 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19079 while (0)
19082 /* Add a glyph string for a composite sequence to the list of strings
19083 between HEAD and TAIL. START is the index of the first glyph in
19084 row area AREA of glyph row ROW that is part of the new glyph
19085 string. END is the index of the last glyph in that glyph row area.
19086 X is the current output position assigned to the new glyph string
19087 constructed. HL overrides that face of the glyph; e.g. it is
19088 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19089 x-position of the drawing area. */
19091 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19092 do { \
19093 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19094 int face_id = (row)->glyphs[area][START].face_id; \
19095 struct face *base_face = FACE_FROM_ID (f, face_id); \
19096 struct composition *cmp = composition_table[cmp_id]; \
19097 int glyph_len = cmp->glyph_len; \
19098 XChar2b *char2b; \
19099 struct face **faces; \
19100 struct glyph_string *first_s = NULL; \
19101 int n; \
19103 base_face = base_face->ascii_face; \
19104 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19105 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19106 /* At first, fill in `char2b' and `faces'. */ \
19107 for (n = 0; n < glyph_len; n++) \
19109 int c = COMPOSITION_GLYPH (cmp, n); \
19110 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19111 faces[n] = FACE_FROM_ID (f, this_face_id); \
19112 get_char_face_and_encoding (f, c, this_face_id, \
19113 char2b + n, 1, 1); \
19116 /* Make glyph_strings for each glyph sequence that is drawable by \
19117 the same face, and append them to HEAD/TAIL. */ \
19118 for (n = 0; n < cmp->glyph_len;) \
19120 s = (struct glyph_string *) alloca (sizeof *s); \
19121 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19122 append_glyph_string (&(HEAD), &(TAIL), s); \
19123 s->cmp = cmp; \
19124 s->gidx = n; \
19125 s->x = (X); \
19127 if (n == 0) \
19128 first_s = s; \
19130 n = fill_composite_glyph_string (s, faces, overlaps); \
19133 ++START; \
19134 s = first_s; \
19135 } while (0)
19138 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19139 of AREA of glyph row ROW on window W between indices START and END.
19140 HL overrides the face for drawing glyph strings, e.g. it is
19141 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19142 x-positions of the drawing area.
19144 This is an ugly monster macro construct because we must use alloca
19145 to allocate glyph strings (because draw_glyphs can be called
19146 asynchronously). */
19148 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19149 do \
19151 HEAD = TAIL = NULL; \
19152 while (START < END) \
19154 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19155 switch (first_glyph->type) \
19157 case CHAR_GLYPH: \
19158 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19159 HL, X, LAST_X); \
19160 break; \
19162 case COMPOSITE_GLYPH: \
19163 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19164 HL, X, LAST_X); \
19165 break; \
19167 case STRETCH_GLYPH: \
19168 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19169 HL, X, LAST_X); \
19170 break; \
19172 case IMAGE_GLYPH: \
19173 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19174 HL, X, LAST_X); \
19175 break; \
19177 default: \
19178 abort (); \
19181 set_glyph_string_background_width (s, START, LAST_X); \
19182 (X) += s->width; \
19185 while (0)
19188 /* Draw glyphs between START and END in AREA of ROW on window W,
19189 starting at x-position X. X is relative to AREA in W. HL is a
19190 face-override with the following meaning:
19192 DRAW_NORMAL_TEXT draw normally
19193 DRAW_CURSOR draw in cursor face
19194 DRAW_MOUSE_FACE draw in mouse face.
19195 DRAW_INVERSE_VIDEO draw in mode line face
19196 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19197 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19199 If OVERLAPS is non-zero, draw only the foreground of characters and
19200 clip to the physical height of ROW. Non-zero value also defines
19201 the overlapping part to be drawn:
19203 OVERLAPS_PRED overlap with preceding rows
19204 OVERLAPS_SUCC overlap with succeeding rows
19205 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19206 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19208 Value is the x-position reached, relative to AREA of W. */
19210 static int
19211 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19212 struct window *w;
19213 int x;
19214 struct glyph_row *row;
19215 enum glyph_row_area area;
19216 int start, end;
19217 enum draw_glyphs_face hl;
19218 int overlaps;
19220 struct glyph_string *head, *tail;
19221 struct glyph_string *s;
19222 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19223 int last_x, area_width;
19224 int x_reached;
19225 int i, j;
19226 struct frame *f = XFRAME (WINDOW_FRAME (w));
19227 DECLARE_HDC (hdc);
19229 ALLOCATE_HDC (hdc, f);
19231 /* Let's rather be paranoid than getting a SEGV. */
19232 end = min (end, row->used[area]);
19233 start = max (0, start);
19234 start = min (end, start);
19236 /* Translate X to frame coordinates. Set last_x to the right
19237 end of the drawing area. */
19238 if (row->full_width_p)
19240 /* X is relative to the left edge of W, without scroll bars
19241 or fringes. */
19242 x += WINDOW_LEFT_EDGE_X (w);
19243 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19245 else
19247 int area_left = window_box_left (w, area);
19248 x += area_left;
19249 area_width = window_box_width (w, area);
19250 last_x = area_left + area_width;
19253 /* Build a doubly-linked list of glyph_string structures between
19254 head and tail from what we have to draw. Note that the macro
19255 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19256 the reason we use a separate variable `i'. */
19257 i = start;
19258 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19259 if (tail)
19260 x_reached = tail->x + tail->background_width;
19261 else
19262 x_reached = x;
19264 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19265 the row, redraw some glyphs in front or following the glyph
19266 strings built above. */
19267 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19269 int dummy_x = 0;
19270 struct glyph_string *h, *t;
19272 /* Compute overhangs for all glyph strings. */
19273 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19274 for (s = head; s; s = s->next)
19275 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19277 /* Prepend glyph strings for glyphs in front of the first glyph
19278 string that are overwritten because of the first glyph
19279 string's left overhang. The background of all strings
19280 prepended must be drawn because the first glyph string
19281 draws over it. */
19282 i = left_overwritten (head);
19283 if (i >= 0)
19285 j = i;
19286 BUILD_GLYPH_STRINGS (j, start, h, t,
19287 DRAW_NORMAL_TEXT, dummy_x, last_x);
19288 start = i;
19289 compute_overhangs_and_x (t, head->x, 1);
19290 prepend_glyph_string_lists (&head, &tail, h, t);
19291 clip_head = head;
19294 /* Prepend glyph strings for glyphs in front of the first glyph
19295 string that overwrite that glyph string because of their
19296 right overhang. For these strings, only the foreground must
19297 be drawn, because it draws over the glyph string at `head'.
19298 The background must not be drawn because this would overwrite
19299 right overhangs of preceding glyphs for which no glyph
19300 strings exist. */
19301 i = left_overwriting (head);
19302 if (i >= 0)
19304 clip_head = head;
19305 BUILD_GLYPH_STRINGS (i, start, h, t,
19306 DRAW_NORMAL_TEXT, dummy_x, last_x);
19307 for (s = h; s; s = s->next)
19308 s->background_filled_p = 1;
19309 compute_overhangs_and_x (t, head->x, 1);
19310 prepend_glyph_string_lists (&head, &tail, h, t);
19313 /* Append glyphs strings for glyphs following the last glyph
19314 string tail that are overwritten by tail. The background of
19315 these strings has to be drawn because tail's foreground draws
19316 over it. */
19317 i = right_overwritten (tail);
19318 if (i >= 0)
19320 BUILD_GLYPH_STRINGS (end, i, h, t,
19321 DRAW_NORMAL_TEXT, x, last_x);
19322 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19323 append_glyph_string_lists (&head, &tail, h, t);
19324 clip_tail = tail;
19327 /* Append glyph strings for glyphs following the last glyph
19328 string tail that overwrite tail. The foreground of such
19329 glyphs has to be drawn because it writes into the background
19330 of tail. The background must not be drawn because it could
19331 paint over the foreground of following glyphs. */
19332 i = right_overwriting (tail);
19333 if (i >= 0)
19335 clip_tail = tail;
19336 BUILD_GLYPH_STRINGS (end, i, h, t,
19337 DRAW_NORMAL_TEXT, x, last_x);
19338 for (s = h; s; s = s->next)
19339 s->background_filled_p = 1;
19340 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19341 append_glyph_string_lists (&head, &tail, h, t);
19343 if (clip_head || clip_tail)
19344 for (s = head; s; s = s->next)
19346 s->clip_head = clip_head;
19347 s->clip_tail = clip_tail;
19351 /* Draw all strings. */
19352 for (s = head; s; s = s->next)
19353 FRAME_RIF (f)->draw_glyph_string (s);
19355 if (area == TEXT_AREA
19356 && !row->full_width_p
19357 /* When drawing overlapping rows, only the glyph strings'
19358 foreground is drawn, which doesn't erase a cursor
19359 completely. */
19360 && !overlaps)
19362 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19363 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19364 : (tail ? tail->x + tail->background_width : x));
19366 int text_left = window_box_left (w, TEXT_AREA);
19367 x0 -= text_left;
19368 x1 -= text_left;
19370 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19371 row->y, MATRIX_ROW_BOTTOM_Y (row));
19374 /* Value is the x-position up to which drawn, relative to AREA of W.
19375 This doesn't include parts drawn because of overhangs. */
19376 if (row->full_width_p)
19377 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19378 else
19379 x_reached -= window_box_left (w, area);
19381 RELEASE_HDC (hdc, f);
19383 return x_reached;
19386 /* Expand row matrix if too narrow. Don't expand if area
19387 is not present. */
19389 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19391 if (!fonts_changed_p \
19392 && (it->glyph_row->glyphs[area] \
19393 < it->glyph_row->glyphs[area + 1])) \
19395 it->w->ncols_scale_factor++; \
19396 fonts_changed_p = 1; \
19400 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19401 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19403 static INLINE void
19404 append_glyph (it)
19405 struct it *it;
19407 struct glyph *glyph;
19408 enum glyph_row_area area = it->area;
19410 xassert (it->glyph_row);
19411 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19413 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19414 if (glyph < it->glyph_row->glyphs[area + 1])
19416 glyph->charpos = CHARPOS (it->position);
19417 glyph->object = it->object;
19418 glyph->pixel_width = it->pixel_width;
19419 glyph->ascent = it->ascent;
19420 glyph->descent = it->descent;
19421 glyph->voffset = it->voffset;
19422 glyph->type = CHAR_GLYPH;
19423 glyph->multibyte_p = it->multibyte_p;
19424 glyph->left_box_line_p = it->start_of_box_run_p;
19425 glyph->right_box_line_p = it->end_of_box_run_p;
19426 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19427 || it->phys_descent > it->descent);
19428 glyph->padding_p = 0;
19429 glyph->glyph_not_available_p = it->glyph_not_available_p;
19430 glyph->face_id = it->face_id;
19431 glyph->u.ch = it->char_to_display;
19432 glyph->slice = null_glyph_slice;
19433 glyph->font_type = FONT_TYPE_UNKNOWN;
19434 ++it->glyph_row->used[area];
19436 else
19437 IT_EXPAND_MATRIX_WIDTH (it, area);
19440 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19441 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19443 static INLINE void
19444 append_composite_glyph (it)
19445 struct it *it;
19447 struct glyph *glyph;
19448 enum glyph_row_area area = it->area;
19450 xassert (it->glyph_row);
19452 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19453 if (glyph < it->glyph_row->glyphs[area + 1])
19455 glyph->charpos = CHARPOS (it->position);
19456 glyph->object = it->object;
19457 glyph->pixel_width = it->pixel_width;
19458 glyph->ascent = it->ascent;
19459 glyph->descent = it->descent;
19460 glyph->voffset = it->voffset;
19461 glyph->type = COMPOSITE_GLYPH;
19462 glyph->multibyte_p = it->multibyte_p;
19463 glyph->left_box_line_p = it->start_of_box_run_p;
19464 glyph->right_box_line_p = it->end_of_box_run_p;
19465 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19466 || it->phys_descent > it->descent);
19467 glyph->padding_p = 0;
19468 glyph->glyph_not_available_p = 0;
19469 glyph->face_id = it->face_id;
19470 glyph->u.cmp_id = it->cmp_id;
19471 glyph->slice = null_glyph_slice;
19472 glyph->font_type = FONT_TYPE_UNKNOWN;
19473 ++it->glyph_row->used[area];
19475 else
19476 IT_EXPAND_MATRIX_WIDTH (it, area);
19480 /* Change IT->ascent and IT->height according to the setting of
19481 IT->voffset. */
19483 static INLINE void
19484 take_vertical_position_into_account (it)
19485 struct it *it;
19487 if (it->voffset)
19489 if (it->voffset < 0)
19490 /* Increase the ascent so that we can display the text higher
19491 in the line. */
19492 it->ascent -= it->voffset;
19493 else
19494 /* Increase the descent so that we can display the text lower
19495 in the line. */
19496 it->descent += it->voffset;
19501 /* Produce glyphs/get display metrics for the image IT is loaded with.
19502 See the description of struct display_iterator in dispextern.h for
19503 an overview of struct display_iterator. */
19505 static void
19506 produce_image_glyph (it)
19507 struct it *it;
19509 struct image *img;
19510 struct face *face;
19511 int glyph_ascent;
19512 struct glyph_slice slice;
19514 xassert (it->what == IT_IMAGE);
19516 face = FACE_FROM_ID (it->f, it->face_id);
19517 xassert (face);
19518 /* Make sure X resources of the face is loaded. */
19519 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19521 if (it->image_id < 0)
19523 /* Fringe bitmap. */
19524 it->ascent = it->phys_ascent = 0;
19525 it->descent = it->phys_descent = 0;
19526 it->pixel_width = 0;
19527 it->nglyphs = 0;
19528 return;
19531 img = IMAGE_FROM_ID (it->f, it->image_id);
19532 xassert (img);
19533 /* Make sure X resources of the image is loaded. */
19534 prepare_image_for_display (it->f, img);
19536 slice.x = slice.y = 0;
19537 slice.width = img->width;
19538 slice.height = img->height;
19540 if (INTEGERP (it->slice.x))
19541 slice.x = XINT (it->slice.x);
19542 else if (FLOATP (it->slice.x))
19543 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19545 if (INTEGERP (it->slice.y))
19546 slice.y = XINT (it->slice.y);
19547 else if (FLOATP (it->slice.y))
19548 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19550 if (INTEGERP (it->slice.width))
19551 slice.width = XINT (it->slice.width);
19552 else if (FLOATP (it->slice.width))
19553 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19555 if (INTEGERP (it->slice.height))
19556 slice.height = XINT (it->slice.height);
19557 else if (FLOATP (it->slice.height))
19558 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19560 if (slice.x >= img->width)
19561 slice.x = img->width;
19562 if (slice.y >= img->height)
19563 slice.y = img->height;
19564 if (slice.x + slice.width >= img->width)
19565 slice.width = img->width - slice.x;
19566 if (slice.y + slice.height > img->height)
19567 slice.height = img->height - slice.y;
19569 if (slice.width == 0 || slice.height == 0)
19570 return;
19572 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19574 it->descent = slice.height - glyph_ascent;
19575 if (slice.y == 0)
19576 it->descent += img->vmargin;
19577 if (slice.y + slice.height == img->height)
19578 it->descent += img->vmargin;
19579 it->phys_descent = it->descent;
19581 it->pixel_width = slice.width;
19582 if (slice.x == 0)
19583 it->pixel_width += img->hmargin;
19584 if (slice.x + slice.width == img->width)
19585 it->pixel_width += img->hmargin;
19587 /* It's quite possible for images to have an ascent greater than
19588 their height, so don't get confused in that case. */
19589 if (it->descent < 0)
19590 it->descent = 0;
19592 #if 0 /* this breaks image tiling */
19593 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19594 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19595 if (face_ascent > it->ascent)
19596 it->ascent = it->phys_ascent = face_ascent;
19597 #endif
19599 it->nglyphs = 1;
19601 if (face->box != FACE_NO_BOX)
19603 if (face->box_line_width > 0)
19605 if (slice.y == 0)
19606 it->ascent += face->box_line_width;
19607 if (slice.y + slice.height == img->height)
19608 it->descent += face->box_line_width;
19611 if (it->start_of_box_run_p && slice.x == 0)
19612 it->pixel_width += abs (face->box_line_width);
19613 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19614 it->pixel_width += abs (face->box_line_width);
19617 take_vertical_position_into_account (it);
19619 if (it->glyph_row)
19621 struct glyph *glyph;
19622 enum glyph_row_area area = it->area;
19624 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19625 if (glyph < it->glyph_row->glyphs[area + 1])
19627 glyph->charpos = CHARPOS (it->position);
19628 glyph->object = it->object;
19629 glyph->pixel_width = it->pixel_width;
19630 glyph->ascent = glyph_ascent;
19631 glyph->descent = it->descent;
19632 glyph->voffset = it->voffset;
19633 glyph->type = IMAGE_GLYPH;
19634 glyph->multibyte_p = it->multibyte_p;
19635 glyph->left_box_line_p = it->start_of_box_run_p;
19636 glyph->right_box_line_p = it->end_of_box_run_p;
19637 glyph->overlaps_vertically_p = 0;
19638 glyph->padding_p = 0;
19639 glyph->glyph_not_available_p = 0;
19640 glyph->face_id = it->face_id;
19641 glyph->u.img_id = img->id;
19642 glyph->slice = slice;
19643 glyph->font_type = FONT_TYPE_UNKNOWN;
19644 ++it->glyph_row->used[area];
19646 else
19647 IT_EXPAND_MATRIX_WIDTH (it, area);
19652 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19653 of the glyph, WIDTH and HEIGHT are the width and height of the
19654 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19656 static void
19657 append_stretch_glyph (it, object, width, height, ascent)
19658 struct it *it;
19659 Lisp_Object object;
19660 int width, height;
19661 int ascent;
19663 struct glyph *glyph;
19664 enum glyph_row_area area = it->area;
19666 xassert (ascent >= 0 && ascent <= height);
19668 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19669 if (glyph < it->glyph_row->glyphs[area + 1])
19671 glyph->charpos = CHARPOS (it->position);
19672 glyph->object = object;
19673 glyph->pixel_width = width;
19674 glyph->ascent = ascent;
19675 glyph->descent = height - ascent;
19676 glyph->voffset = it->voffset;
19677 glyph->type = STRETCH_GLYPH;
19678 glyph->multibyte_p = it->multibyte_p;
19679 glyph->left_box_line_p = it->start_of_box_run_p;
19680 glyph->right_box_line_p = it->end_of_box_run_p;
19681 glyph->overlaps_vertically_p = 0;
19682 glyph->padding_p = 0;
19683 glyph->glyph_not_available_p = 0;
19684 glyph->face_id = it->face_id;
19685 glyph->u.stretch.ascent = ascent;
19686 glyph->u.stretch.height = height;
19687 glyph->slice = null_glyph_slice;
19688 glyph->font_type = FONT_TYPE_UNKNOWN;
19689 ++it->glyph_row->used[area];
19691 else
19692 IT_EXPAND_MATRIX_WIDTH (it, area);
19696 /* Produce a stretch glyph for iterator IT. IT->object is the value
19697 of the glyph property displayed. The value must be a list
19698 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19699 being recognized:
19701 1. `:width WIDTH' specifies that the space should be WIDTH *
19702 canonical char width wide. WIDTH may be an integer or floating
19703 point number.
19705 2. `:relative-width FACTOR' specifies that the width of the stretch
19706 should be computed from the width of the first character having the
19707 `glyph' property, and should be FACTOR times that width.
19709 3. `:align-to HPOS' specifies that the space should be wide enough
19710 to reach HPOS, a value in canonical character units.
19712 Exactly one of the above pairs must be present.
19714 4. `:height HEIGHT' specifies that the height of the stretch produced
19715 should be HEIGHT, measured in canonical character units.
19717 5. `:relative-height FACTOR' specifies that the height of the
19718 stretch should be FACTOR times the height of the characters having
19719 the glyph property.
19721 Either none or exactly one of 4 or 5 must be present.
19723 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19724 of the stretch should be used for the ascent of the stretch.
19725 ASCENT must be in the range 0 <= ASCENT <= 100. */
19727 static void
19728 produce_stretch_glyph (it)
19729 struct it *it;
19731 /* (space :width WIDTH :height HEIGHT ...) */
19732 Lisp_Object prop, plist;
19733 int width = 0, height = 0, align_to = -1;
19734 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19735 int ascent = 0;
19736 double tem;
19737 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19738 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19740 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19742 /* List should start with `space'. */
19743 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19744 plist = XCDR (it->object);
19746 /* Compute the width of the stretch. */
19747 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19748 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19750 /* Absolute width `:width WIDTH' specified and valid. */
19751 zero_width_ok_p = 1;
19752 width = (int)tem;
19754 else if (prop = Fplist_get (plist, QCrelative_width),
19755 NUMVAL (prop) > 0)
19757 /* Relative width `:relative-width FACTOR' specified and valid.
19758 Compute the width of the characters having the `glyph'
19759 property. */
19760 struct it it2;
19761 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19763 it2 = *it;
19764 if (it->multibyte_p)
19766 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19767 - IT_BYTEPOS (*it));
19768 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19770 else
19771 it2.c = *p, it2.len = 1;
19773 it2.glyph_row = NULL;
19774 it2.what = IT_CHARACTER;
19775 x_produce_glyphs (&it2);
19776 width = NUMVAL (prop) * it2.pixel_width;
19778 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19779 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19781 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19782 align_to = (align_to < 0
19784 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19785 else if (align_to < 0)
19786 align_to = window_box_left_offset (it->w, TEXT_AREA);
19787 width = max (0, (int)tem + align_to - it->current_x);
19788 zero_width_ok_p = 1;
19790 else
19791 /* Nothing specified -> width defaults to canonical char width. */
19792 width = FRAME_COLUMN_WIDTH (it->f);
19794 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19795 width = 1;
19797 /* Compute height. */
19798 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19799 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19801 height = (int)tem;
19802 zero_height_ok_p = 1;
19804 else if (prop = Fplist_get (plist, QCrelative_height),
19805 NUMVAL (prop) > 0)
19806 height = FONT_HEIGHT (font) * NUMVAL (prop);
19807 else
19808 height = FONT_HEIGHT (font);
19810 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19811 height = 1;
19813 /* Compute percentage of height used for ascent. If
19814 `:ascent ASCENT' is present and valid, use that. Otherwise,
19815 derive the ascent from the font in use. */
19816 if (prop = Fplist_get (plist, QCascent),
19817 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19818 ascent = height * NUMVAL (prop) / 100.0;
19819 else if (!NILP (prop)
19820 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19821 ascent = min (max (0, (int)tem), height);
19822 else
19823 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19825 if (width > 0 && !it->truncate_lines_p
19826 && it->current_x + width > it->last_visible_x)
19827 width = it->last_visible_x - it->current_x - 1;
19829 if (width > 0 && height > 0 && it->glyph_row)
19831 Lisp_Object object = it->stack[it->sp - 1].string;
19832 if (!STRINGP (object))
19833 object = it->w->buffer;
19834 append_stretch_glyph (it, object, width, height, ascent);
19837 it->pixel_width = width;
19838 it->ascent = it->phys_ascent = ascent;
19839 it->descent = it->phys_descent = height - it->ascent;
19840 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19842 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19844 if (face->box_line_width > 0)
19846 it->ascent += face->box_line_width;
19847 it->descent += face->box_line_width;
19850 if (it->start_of_box_run_p)
19851 it->pixel_width += abs (face->box_line_width);
19852 if (it->end_of_box_run_p)
19853 it->pixel_width += abs (face->box_line_width);
19856 take_vertical_position_into_account (it);
19859 /* Get line-height and line-spacing property at point.
19860 If line-height has format (HEIGHT TOTAL), return TOTAL
19861 in TOTAL_HEIGHT. */
19863 static Lisp_Object
19864 get_line_height_property (it, prop)
19865 struct it *it;
19866 Lisp_Object prop;
19868 Lisp_Object position;
19870 if (STRINGP (it->object))
19871 position = make_number (IT_STRING_CHARPOS (*it));
19872 else if (BUFFERP (it->object))
19873 position = make_number (IT_CHARPOS (*it));
19874 else
19875 return Qnil;
19877 return Fget_char_property (position, prop, it->object);
19880 /* Calculate line-height and line-spacing properties.
19881 An integer value specifies explicit pixel value.
19882 A float value specifies relative value to current face height.
19883 A cons (float . face-name) specifies relative value to
19884 height of specified face font.
19886 Returns height in pixels, or nil. */
19889 static Lisp_Object
19890 calc_line_height_property (it, val, font, boff, override)
19891 struct it *it;
19892 Lisp_Object val;
19893 XFontStruct *font;
19894 int boff, override;
19896 Lisp_Object face_name = Qnil;
19897 int ascent, descent, height;
19899 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19900 return val;
19902 if (CONSP (val))
19904 face_name = XCAR (val);
19905 val = XCDR (val);
19906 if (!NUMBERP (val))
19907 val = make_number (1);
19908 if (NILP (face_name))
19910 height = it->ascent + it->descent;
19911 goto scale;
19915 if (NILP (face_name))
19917 font = FRAME_FONT (it->f);
19918 boff = FRAME_BASELINE_OFFSET (it->f);
19920 else if (EQ (face_name, Qt))
19922 override = 0;
19924 else
19926 int face_id;
19927 struct face *face;
19928 struct font_info *font_info;
19930 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19931 if (face_id < 0)
19932 return make_number (-1);
19934 face = FACE_FROM_ID (it->f, face_id);
19935 font = face->font;
19936 if (font == NULL)
19937 return make_number (-1);
19939 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19940 boff = font_info->baseline_offset;
19941 if (font_info->vertical_centering)
19942 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19945 ascent = FONT_BASE (font) + boff;
19946 descent = FONT_DESCENT (font) - boff;
19948 if (override)
19950 it->override_ascent = ascent;
19951 it->override_descent = descent;
19952 it->override_boff = boff;
19955 height = ascent + descent;
19957 scale:
19958 if (FLOATP (val))
19959 height = (int)(XFLOAT_DATA (val) * height);
19960 else if (INTEGERP (val))
19961 height *= XINT (val);
19963 return make_number (height);
19967 /* RIF:
19968 Produce glyphs/get display metrics for the display element IT is
19969 loaded with. See the description of struct it in dispextern.h
19970 for an overview of struct it. */
19972 void
19973 x_produce_glyphs (it)
19974 struct it *it;
19976 int extra_line_spacing = it->extra_line_spacing;
19978 it->glyph_not_available_p = 0;
19980 if (it->what == IT_CHARACTER)
19982 XChar2b char2b;
19983 XFontStruct *font;
19984 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19985 XCharStruct *pcm;
19986 int font_not_found_p;
19987 struct font_info *font_info;
19988 int boff; /* baseline offset */
19989 /* We may change it->multibyte_p upon unibyte<->multibyte
19990 conversion. So, save the current value now and restore it
19991 later.
19993 Note: It seems that we don't have to record multibyte_p in
19994 struct glyph because the character code itself tells if or
19995 not the character is multibyte. Thus, in the future, we must
19996 consider eliminating the field `multibyte_p' in the struct
19997 glyph. */
19998 int saved_multibyte_p = it->multibyte_p;
20000 /* Maybe translate single-byte characters to multibyte, or the
20001 other way. */
20002 it->char_to_display = it->c;
20003 if (!ASCII_BYTE_P (it->c))
20005 if (unibyte_display_via_language_environment
20006 && SINGLE_BYTE_CHAR_P (it->c)
20007 && (it->c >= 0240
20008 || !NILP (Vnonascii_translation_table)))
20010 it->char_to_display = unibyte_char_to_multibyte (it->c);
20011 it->multibyte_p = 1;
20012 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20013 face = FACE_FROM_ID (it->f, it->face_id);
20015 else if (!SINGLE_BYTE_CHAR_P (it->c)
20016 && !it->multibyte_p)
20018 it->multibyte_p = 1;
20019 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20020 face = FACE_FROM_ID (it->f, it->face_id);
20024 /* Get font to use. Encode IT->char_to_display. */
20025 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20026 &char2b, it->multibyte_p, 0);
20027 font = face->font;
20029 /* When no suitable font found, use the default font. */
20030 font_not_found_p = font == NULL;
20031 if (font_not_found_p)
20033 font = FRAME_FONT (it->f);
20034 boff = FRAME_BASELINE_OFFSET (it->f);
20035 font_info = NULL;
20037 else
20039 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20040 boff = font_info->baseline_offset;
20041 if (font_info->vertical_centering)
20042 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20045 if (it->char_to_display >= ' '
20046 && (!it->multibyte_p || it->char_to_display < 128))
20048 /* Either unibyte or ASCII. */
20049 int stretched_p;
20051 it->nglyphs = 1;
20053 pcm = FRAME_RIF (it->f)->per_char_metric
20054 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20056 if (it->override_ascent >= 0)
20058 it->ascent = it->override_ascent;
20059 it->descent = it->override_descent;
20060 boff = it->override_boff;
20062 else
20064 it->ascent = FONT_BASE (font) + boff;
20065 it->descent = FONT_DESCENT (font) - boff;
20068 if (pcm)
20070 it->phys_ascent = pcm->ascent + boff;
20071 it->phys_descent = pcm->descent - boff;
20072 it->pixel_width = pcm->width;
20074 else
20076 it->glyph_not_available_p = 1;
20077 it->phys_ascent = it->ascent;
20078 it->phys_descent = it->descent;
20079 it->pixel_width = FONT_WIDTH (font);
20082 if (it->constrain_row_ascent_descent_p)
20084 if (it->descent > it->max_descent)
20086 it->ascent += it->descent - it->max_descent;
20087 it->descent = it->max_descent;
20089 if (it->ascent > it->max_ascent)
20091 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20092 it->ascent = it->max_ascent;
20094 it->phys_ascent = min (it->phys_ascent, it->ascent);
20095 it->phys_descent = min (it->phys_descent, it->descent);
20096 extra_line_spacing = 0;
20099 /* If this is a space inside a region of text with
20100 `space-width' property, change its width. */
20101 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20102 if (stretched_p)
20103 it->pixel_width *= XFLOATINT (it->space_width);
20105 /* If face has a box, add the box thickness to the character
20106 height. If character has a box line to the left and/or
20107 right, add the box line width to the character's width. */
20108 if (face->box != FACE_NO_BOX)
20110 int thick = face->box_line_width;
20112 if (thick > 0)
20114 it->ascent += thick;
20115 it->descent += thick;
20117 else
20118 thick = -thick;
20120 if (it->start_of_box_run_p)
20121 it->pixel_width += thick;
20122 if (it->end_of_box_run_p)
20123 it->pixel_width += thick;
20126 /* If face has an overline, add the height of the overline
20127 (1 pixel) and a 1 pixel margin to the character height. */
20128 if (face->overline_p)
20129 it->ascent += 2;
20131 if (it->constrain_row_ascent_descent_p)
20133 if (it->ascent > it->max_ascent)
20134 it->ascent = it->max_ascent;
20135 if (it->descent > it->max_descent)
20136 it->descent = it->max_descent;
20139 take_vertical_position_into_account (it);
20141 /* If we have to actually produce glyphs, do it. */
20142 if (it->glyph_row)
20144 if (stretched_p)
20146 /* Translate a space with a `space-width' property
20147 into a stretch glyph. */
20148 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20149 / FONT_HEIGHT (font));
20150 append_stretch_glyph (it, it->object, it->pixel_width,
20151 it->ascent + it->descent, ascent);
20153 else
20154 append_glyph (it);
20156 /* If characters with lbearing or rbearing are displayed
20157 in this line, record that fact in a flag of the
20158 glyph row. This is used to optimize X output code. */
20159 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20160 it->glyph_row->contains_overlapping_glyphs_p = 1;
20163 else if (it->char_to_display == '\n')
20165 /* A newline has no width but we need the height of the line.
20166 But if previous part of the line set a height, don't
20167 increase that height */
20169 Lisp_Object height;
20170 Lisp_Object total_height = Qnil;
20172 it->override_ascent = -1;
20173 it->pixel_width = 0;
20174 it->nglyphs = 0;
20176 height = get_line_height_property(it, Qline_height);
20177 /* Split (line-height total-height) list */
20178 if (CONSP (height)
20179 && CONSP (XCDR (height))
20180 && NILP (XCDR (XCDR (height))))
20182 total_height = XCAR (XCDR (height));
20183 height = XCAR (height);
20185 height = calc_line_height_property(it, height, font, boff, 1);
20187 if (it->override_ascent >= 0)
20189 it->ascent = it->override_ascent;
20190 it->descent = it->override_descent;
20191 boff = it->override_boff;
20193 else
20195 it->ascent = FONT_BASE (font) + boff;
20196 it->descent = FONT_DESCENT (font) - boff;
20199 if (EQ (height, Qt))
20201 if (it->descent > it->max_descent)
20203 it->ascent += it->descent - it->max_descent;
20204 it->descent = it->max_descent;
20206 if (it->ascent > it->max_ascent)
20208 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20209 it->ascent = it->max_ascent;
20211 it->phys_ascent = min (it->phys_ascent, it->ascent);
20212 it->phys_descent = min (it->phys_descent, it->descent);
20213 it->constrain_row_ascent_descent_p = 1;
20214 extra_line_spacing = 0;
20216 else
20218 Lisp_Object spacing;
20220 it->phys_ascent = it->ascent;
20221 it->phys_descent = it->descent;
20223 if ((it->max_ascent > 0 || it->max_descent > 0)
20224 && face->box != FACE_NO_BOX
20225 && face->box_line_width > 0)
20227 it->ascent += face->box_line_width;
20228 it->descent += face->box_line_width;
20230 if (!NILP (height)
20231 && XINT (height) > it->ascent + it->descent)
20232 it->ascent = XINT (height) - it->descent;
20234 if (!NILP (total_height))
20235 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20236 else
20238 spacing = get_line_height_property(it, Qline_spacing);
20239 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20241 if (INTEGERP (spacing))
20243 extra_line_spacing = XINT (spacing);
20244 if (!NILP (total_height))
20245 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20249 else if (it->char_to_display == '\t')
20251 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20252 int x = it->current_x + it->continuation_lines_width;
20253 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20255 /* If the distance from the current position to the next tab
20256 stop is less than a space character width, use the
20257 tab stop after that. */
20258 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20259 next_tab_x += tab_width;
20261 it->pixel_width = next_tab_x - x;
20262 it->nglyphs = 1;
20263 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20264 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20266 if (it->glyph_row)
20268 append_stretch_glyph (it, it->object, it->pixel_width,
20269 it->ascent + it->descent, it->ascent);
20272 else
20274 /* A multi-byte character. Assume that the display width of the
20275 character is the width of the character multiplied by the
20276 width of the font. */
20278 /* If we found a font, this font should give us the right
20279 metrics. If we didn't find a font, use the frame's
20280 default font and calculate the width of the character
20281 from the charset width; this is what old redisplay code
20282 did. */
20284 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20285 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20287 if (font_not_found_p || !pcm)
20289 int charset = CHAR_CHARSET (it->char_to_display);
20291 it->glyph_not_available_p = 1;
20292 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20293 * CHARSET_WIDTH (charset));
20294 it->phys_ascent = FONT_BASE (font) + boff;
20295 it->phys_descent = FONT_DESCENT (font) - boff;
20297 else
20299 it->pixel_width = pcm->width;
20300 it->phys_ascent = pcm->ascent + boff;
20301 it->phys_descent = pcm->descent - boff;
20302 if (it->glyph_row
20303 && (pcm->lbearing < 0
20304 || pcm->rbearing > pcm->width))
20305 it->glyph_row->contains_overlapping_glyphs_p = 1;
20307 it->nglyphs = 1;
20308 it->ascent = FONT_BASE (font) + boff;
20309 it->descent = FONT_DESCENT (font) - boff;
20310 if (face->box != FACE_NO_BOX)
20312 int thick = face->box_line_width;
20314 if (thick > 0)
20316 it->ascent += thick;
20317 it->descent += thick;
20319 else
20320 thick = - thick;
20322 if (it->start_of_box_run_p)
20323 it->pixel_width += thick;
20324 if (it->end_of_box_run_p)
20325 it->pixel_width += thick;
20328 /* If face has an overline, add the height of the overline
20329 (1 pixel) and a 1 pixel margin to the character height. */
20330 if (face->overline_p)
20331 it->ascent += 2;
20333 take_vertical_position_into_account (it);
20335 if (it->glyph_row)
20336 append_glyph (it);
20338 it->multibyte_p = saved_multibyte_p;
20340 else if (it->what == IT_COMPOSITION)
20342 /* Note: A composition is represented as one glyph in the
20343 glyph matrix. There are no padding glyphs. */
20344 XChar2b char2b;
20345 XFontStruct *font;
20346 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20347 XCharStruct *pcm;
20348 int font_not_found_p;
20349 struct font_info *font_info;
20350 int boff; /* baseline offset */
20351 struct composition *cmp = composition_table[it->cmp_id];
20353 /* Maybe translate single-byte characters to multibyte. */
20354 it->char_to_display = it->c;
20355 if (unibyte_display_via_language_environment
20356 && SINGLE_BYTE_CHAR_P (it->c)
20357 && (it->c >= 0240
20358 || (it->c >= 0200
20359 && !NILP (Vnonascii_translation_table))))
20361 it->char_to_display = unibyte_char_to_multibyte (it->c);
20364 /* Get face and font to use. Encode IT->char_to_display. */
20365 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20366 face = FACE_FROM_ID (it->f, it->face_id);
20367 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20368 &char2b, it->multibyte_p, 0);
20369 font = face->font;
20371 /* When no suitable font found, use the default font. */
20372 font_not_found_p = font == NULL;
20373 if (font_not_found_p)
20375 font = FRAME_FONT (it->f);
20376 boff = FRAME_BASELINE_OFFSET (it->f);
20377 font_info = NULL;
20379 else
20381 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20382 boff = font_info->baseline_offset;
20383 if (font_info->vertical_centering)
20384 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20387 /* There are no padding glyphs, so there is only one glyph to
20388 produce for the composition. Important is that pixel_width,
20389 ascent and descent are the values of what is drawn by
20390 draw_glyphs (i.e. the values of the overall glyphs composed). */
20391 it->nglyphs = 1;
20393 /* If we have not yet calculated pixel size data of glyphs of
20394 the composition for the current face font, calculate them
20395 now. Theoretically, we have to check all fonts for the
20396 glyphs, but that requires much time and memory space. So,
20397 here we check only the font of the first glyph. This leads
20398 to incorrect display very rarely, and C-l (recenter) can
20399 correct the display anyway. */
20400 if (cmp->font != (void *) font)
20402 /* Ascent and descent of the font of the first character of
20403 this composition (adjusted by baseline offset). Ascent
20404 and descent of overall glyphs should not be less than
20405 them respectively. */
20406 int font_ascent = FONT_BASE (font) + boff;
20407 int font_descent = FONT_DESCENT (font) - boff;
20408 /* Bounding box of the overall glyphs. */
20409 int leftmost, rightmost, lowest, highest;
20410 int i, width, ascent, descent;
20412 cmp->font = (void *) font;
20414 /* Initialize the bounding box. */
20415 if (font_info
20416 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20417 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20419 width = pcm->width;
20420 ascent = pcm->ascent;
20421 descent = pcm->descent;
20423 else
20425 width = FONT_WIDTH (font);
20426 ascent = FONT_BASE (font);
20427 descent = FONT_DESCENT (font);
20430 rightmost = width;
20431 lowest = - descent + boff;
20432 highest = ascent + boff;
20433 leftmost = 0;
20435 if (font_info
20436 && font_info->default_ascent
20437 && CHAR_TABLE_P (Vuse_default_ascent)
20438 && !NILP (Faref (Vuse_default_ascent,
20439 make_number (it->char_to_display))))
20440 highest = font_info->default_ascent + boff;
20442 /* Draw the first glyph at the normal position. It may be
20443 shifted to right later if some other glyphs are drawn at
20444 the left. */
20445 cmp->offsets[0] = 0;
20446 cmp->offsets[1] = boff;
20448 /* Set cmp->offsets for the remaining glyphs. */
20449 for (i = 1; i < cmp->glyph_len; i++)
20451 int left, right, btm, top;
20452 int ch = COMPOSITION_GLYPH (cmp, i);
20453 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20455 face = FACE_FROM_ID (it->f, face_id);
20456 get_char_face_and_encoding (it->f, ch, face->id,
20457 &char2b, it->multibyte_p, 0);
20458 font = face->font;
20459 if (font == NULL)
20461 font = FRAME_FONT (it->f);
20462 boff = FRAME_BASELINE_OFFSET (it->f);
20463 font_info = NULL;
20465 else
20467 font_info
20468 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20469 boff = font_info->baseline_offset;
20470 if (font_info->vertical_centering)
20471 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20474 if (font_info
20475 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20476 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20478 width = pcm->width;
20479 ascent = pcm->ascent;
20480 descent = pcm->descent;
20482 else
20484 width = FONT_WIDTH (font);
20485 ascent = 1;
20486 descent = 0;
20489 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20491 /* Relative composition with or without
20492 alternate chars. */
20493 left = (leftmost + rightmost - width) / 2;
20494 btm = - descent + boff;
20495 if (font_info && font_info->relative_compose
20496 && (! CHAR_TABLE_P (Vignore_relative_composition)
20497 || NILP (Faref (Vignore_relative_composition,
20498 make_number (ch)))))
20501 if (- descent >= font_info->relative_compose)
20502 /* One extra pixel between two glyphs. */
20503 btm = highest + 1;
20504 else if (ascent <= 0)
20505 /* One extra pixel between two glyphs. */
20506 btm = lowest - 1 - ascent - descent;
20509 else
20511 /* A composition rule is specified by an integer
20512 value that encodes global and new reference
20513 points (GREF and NREF). GREF and NREF are
20514 specified by numbers as below:
20516 0---1---2 -- ascent
20520 9--10--11 -- center
20522 ---3---4---5--- baseline
20524 6---7---8 -- descent
20526 int rule = COMPOSITION_RULE (cmp, i);
20527 int gref, nref, grefx, grefy, nrefx, nrefy;
20529 COMPOSITION_DECODE_RULE (rule, gref, nref);
20530 grefx = gref % 3, nrefx = nref % 3;
20531 grefy = gref / 3, nrefy = nref / 3;
20533 left = (leftmost
20534 + grefx * (rightmost - leftmost) / 2
20535 - nrefx * width / 2);
20536 btm = ((grefy == 0 ? highest
20537 : grefy == 1 ? 0
20538 : grefy == 2 ? lowest
20539 : (highest + lowest) / 2)
20540 - (nrefy == 0 ? ascent + descent
20541 : nrefy == 1 ? descent - boff
20542 : nrefy == 2 ? 0
20543 : (ascent + descent) / 2));
20546 cmp->offsets[i * 2] = left;
20547 cmp->offsets[i * 2 + 1] = btm + descent;
20549 /* Update the bounding box of the overall glyphs. */
20550 right = left + width;
20551 top = btm + descent + ascent;
20552 if (left < leftmost)
20553 leftmost = left;
20554 if (right > rightmost)
20555 rightmost = right;
20556 if (top > highest)
20557 highest = top;
20558 if (btm < lowest)
20559 lowest = btm;
20562 /* If there are glyphs whose x-offsets are negative,
20563 shift all glyphs to the right and make all x-offsets
20564 non-negative. */
20565 if (leftmost < 0)
20567 for (i = 0; i < cmp->glyph_len; i++)
20568 cmp->offsets[i * 2] -= leftmost;
20569 rightmost -= leftmost;
20572 cmp->pixel_width = rightmost;
20573 cmp->ascent = highest;
20574 cmp->descent = - lowest;
20575 if (cmp->ascent < font_ascent)
20576 cmp->ascent = font_ascent;
20577 if (cmp->descent < font_descent)
20578 cmp->descent = font_descent;
20581 it->pixel_width = cmp->pixel_width;
20582 it->ascent = it->phys_ascent = cmp->ascent;
20583 it->descent = it->phys_descent = cmp->descent;
20585 if (face->box != FACE_NO_BOX)
20587 int thick = face->box_line_width;
20589 if (thick > 0)
20591 it->ascent += thick;
20592 it->descent += thick;
20594 else
20595 thick = - thick;
20597 if (it->start_of_box_run_p)
20598 it->pixel_width += thick;
20599 if (it->end_of_box_run_p)
20600 it->pixel_width += thick;
20603 /* If face has an overline, add the height of the overline
20604 (1 pixel) and a 1 pixel margin to the character height. */
20605 if (face->overline_p)
20606 it->ascent += 2;
20608 take_vertical_position_into_account (it);
20610 if (it->glyph_row)
20611 append_composite_glyph (it);
20613 else if (it->what == IT_IMAGE)
20614 produce_image_glyph (it);
20615 else if (it->what == IT_STRETCH)
20616 produce_stretch_glyph (it);
20618 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20619 because this isn't true for images with `:ascent 100'. */
20620 xassert (it->ascent >= 0 && it->descent >= 0);
20621 if (it->area == TEXT_AREA)
20622 it->current_x += it->pixel_width;
20624 if (extra_line_spacing > 0)
20626 it->descent += extra_line_spacing;
20627 if (extra_line_spacing > it->max_extra_line_spacing)
20628 it->max_extra_line_spacing = extra_line_spacing;
20631 it->max_ascent = max (it->max_ascent, it->ascent);
20632 it->max_descent = max (it->max_descent, it->descent);
20633 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20634 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20637 /* EXPORT for RIF:
20638 Output LEN glyphs starting at START at the nominal cursor position.
20639 Advance the nominal cursor over the text. The global variable
20640 updated_window contains the window being updated, updated_row is
20641 the glyph row being updated, and updated_area is the area of that
20642 row being updated. */
20644 void
20645 x_write_glyphs (start, len)
20646 struct glyph *start;
20647 int len;
20649 int x, hpos;
20651 xassert (updated_window && updated_row);
20652 BLOCK_INPUT;
20654 /* Write glyphs. */
20656 hpos = start - updated_row->glyphs[updated_area];
20657 x = draw_glyphs (updated_window, output_cursor.x,
20658 updated_row, updated_area,
20659 hpos, hpos + len,
20660 DRAW_NORMAL_TEXT, 0);
20662 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20663 if (updated_area == TEXT_AREA
20664 && updated_window->phys_cursor_on_p
20665 && updated_window->phys_cursor.vpos == output_cursor.vpos
20666 && updated_window->phys_cursor.hpos >= hpos
20667 && updated_window->phys_cursor.hpos < hpos + len)
20668 updated_window->phys_cursor_on_p = 0;
20670 UNBLOCK_INPUT;
20672 /* Advance the output cursor. */
20673 output_cursor.hpos += len;
20674 output_cursor.x = x;
20678 /* EXPORT for RIF:
20679 Insert LEN glyphs from START at the nominal cursor position. */
20681 void
20682 x_insert_glyphs (start, len)
20683 struct glyph *start;
20684 int len;
20686 struct frame *f;
20687 struct window *w;
20688 int line_height, shift_by_width, shifted_region_width;
20689 struct glyph_row *row;
20690 struct glyph *glyph;
20691 int frame_x, frame_y, hpos;
20693 xassert (updated_window && updated_row);
20694 BLOCK_INPUT;
20695 w = updated_window;
20696 f = XFRAME (WINDOW_FRAME (w));
20698 /* Get the height of the line we are in. */
20699 row = updated_row;
20700 line_height = row->height;
20702 /* Get the width of the glyphs to insert. */
20703 shift_by_width = 0;
20704 for (glyph = start; glyph < start + len; ++glyph)
20705 shift_by_width += glyph->pixel_width;
20707 /* Get the width of the region to shift right. */
20708 shifted_region_width = (window_box_width (w, updated_area)
20709 - output_cursor.x
20710 - shift_by_width);
20712 /* Shift right. */
20713 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20714 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20716 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20717 line_height, shift_by_width);
20719 /* Write the glyphs. */
20720 hpos = start - row->glyphs[updated_area];
20721 draw_glyphs (w, output_cursor.x, row, updated_area,
20722 hpos, hpos + len,
20723 DRAW_NORMAL_TEXT, 0);
20725 /* Advance the output cursor. */
20726 output_cursor.hpos += len;
20727 output_cursor.x += shift_by_width;
20728 UNBLOCK_INPUT;
20732 /* EXPORT for RIF:
20733 Erase the current text line from the nominal cursor position
20734 (inclusive) to pixel column TO_X (exclusive). The idea is that
20735 everything from TO_X onward is already erased.
20737 TO_X is a pixel position relative to updated_area of
20738 updated_window. TO_X == -1 means clear to the end of this area. */
20740 void
20741 x_clear_end_of_line (to_x)
20742 int to_x;
20744 struct frame *f;
20745 struct window *w = updated_window;
20746 int max_x, min_y, max_y;
20747 int from_x, from_y, to_y;
20749 xassert (updated_window && updated_row);
20750 f = XFRAME (w->frame);
20752 if (updated_row->full_width_p)
20753 max_x = WINDOW_TOTAL_WIDTH (w);
20754 else
20755 max_x = window_box_width (w, updated_area);
20756 max_y = window_text_bottom_y (w);
20758 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20759 of window. For TO_X > 0, truncate to end of drawing area. */
20760 if (to_x == 0)
20761 return;
20762 else if (to_x < 0)
20763 to_x = max_x;
20764 else
20765 to_x = min (to_x, max_x);
20767 to_y = min (max_y, output_cursor.y + updated_row->height);
20769 /* Notice if the cursor will be cleared by this operation. */
20770 if (!updated_row->full_width_p)
20771 notice_overwritten_cursor (w, updated_area,
20772 output_cursor.x, -1,
20773 updated_row->y,
20774 MATRIX_ROW_BOTTOM_Y (updated_row));
20776 from_x = output_cursor.x;
20778 /* Translate to frame coordinates. */
20779 if (updated_row->full_width_p)
20781 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20782 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20784 else
20786 int area_left = window_box_left (w, updated_area);
20787 from_x += area_left;
20788 to_x += area_left;
20791 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20792 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20793 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20795 /* Prevent inadvertently clearing to end of the X window. */
20796 if (to_x > from_x && to_y > from_y)
20798 BLOCK_INPUT;
20799 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
20800 to_x - from_x, to_y - from_y);
20801 UNBLOCK_INPUT;
20805 #endif /* HAVE_WINDOW_SYSTEM */
20809 /***********************************************************************
20810 Cursor types
20811 ***********************************************************************/
20813 /* Value is the internal representation of the specified cursor type
20814 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20815 of the bar cursor. */
20817 static enum text_cursor_kinds
20818 get_specified_cursor_type (arg, width)
20819 Lisp_Object arg;
20820 int *width;
20822 enum text_cursor_kinds type;
20824 if (NILP (arg))
20825 return NO_CURSOR;
20827 if (EQ (arg, Qbox))
20828 return FILLED_BOX_CURSOR;
20830 if (EQ (arg, Qhollow))
20831 return HOLLOW_BOX_CURSOR;
20833 if (EQ (arg, Qbar))
20835 *width = 2;
20836 return BAR_CURSOR;
20839 if (CONSP (arg)
20840 && EQ (XCAR (arg), Qbar)
20841 && INTEGERP (XCDR (arg))
20842 && XINT (XCDR (arg)) >= 0)
20844 *width = XINT (XCDR (arg));
20845 return BAR_CURSOR;
20848 if (EQ (arg, Qhbar))
20850 *width = 2;
20851 return HBAR_CURSOR;
20854 if (CONSP (arg)
20855 && EQ (XCAR (arg), Qhbar)
20856 && INTEGERP (XCDR (arg))
20857 && XINT (XCDR (arg)) >= 0)
20859 *width = XINT (XCDR (arg));
20860 return HBAR_CURSOR;
20863 /* Treat anything unknown as "hollow box cursor".
20864 It was bad to signal an error; people have trouble fixing
20865 .Xdefaults with Emacs, when it has something bad in it. */
20866 type = HOLLOW_BOX_CURSOR;
20868 return type;
20871 /* Set the default cursor types for specified frame. */
20872 void
20873 set_frame_cursor_types (f, arg)
20874 struct frame *f;
20875 Lisp_Object arg;
20877 int width;
20878 Lisp_Object tem;
20880 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20881 FRAME_CURSOR_WIDTH (f) = width;
20883 /* By default, set up the blink-off state depending on the on-state. */
20885 tem = Fassoc (arg, Vblink_cursor_alist);
20886 if (!NILP (tem))
20888 FRAME_BLINK_OFF_CURSOR (f)
20889 = get_specified_cursor_type (XCDR (tem), &width);
20890 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20892 else
20893 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20897 /* Return the cursor we want to be displayed in window W. Return
20898 width of bar/hbar cursor through WIDTH arg. Return with
20899 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20900 (i.e. if the `system caret' should track this cursor).
20902 In a mini-buffer window, we want the cursor only to appear if we
20903 are reading input from this window. For the selected window, we
20904 want the cursor type given by the frame parameter or buffer local
20905 setting of cursor-type. If explicitly marked off, draw no cursor.
20906 In all other cases, we want a hollow box cursor. */
20908 static enum text_cursor_kinds
20909 get_window_cursor_type (w, glyph, width, active_cursor)
20910 struct window *w;
20911 struct glyph *glyph;
20912 int *width;
20913 int *active_cursor;
20915 struct frame *f = XFRAME (w->frame);
20916 struct buffer *b = XBUFFER (w->buffer);
20917 int cursor_type = DEFAULT_CURSOR;
20918 Lisp_Object alt_cursor;
20919 int non_selected = 0;
20921 *active_cursor = 1;
20923 /* Echo area */
20924 if (cursor_in_echo_area
20925 && FRAME_HAS_MINIBUF_P (f)
20926 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20928 if (w == XWINDOW (echo_area_window))
20930 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20932 *width = FRAME_CURSOR_WIDTH (f);
20933 return FRAME_DESIRED_CURSOR (f);
20935 else
20936 return get_specified_cursor_type (b->cursor_type, width);
20939 *active_cursor = 0;
20940 non_selected = 1;
20943 /* Nonselected window or nonselected frame. */
20944 else if (w != XWINDOW (f->selected_window)
20945 #ifdef HAVE_WINDOW_SYSTEM
20946 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20947 #endif
20950 *active_cursor = 0;
20952 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20953 return NO_CURSOR;
20955 non_selected = 1;
20958 /* Never display a cursor in a window in which cursor-type is nil. */
20959 if (NILP (b->cursor_type))
20960 return NO_CURSOR;
20962 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20963 if (non_selected)
20965 alt_cursor = b->cursor_in_non_selected_windows;
20966 return get_specified_cursor_type (alt_cursor, width);
20969 /* Get the normal cursor type for this window. */
20970 if (EQ (b->cursor_type, Qt))
20972 cursor_type = FRAME_DESIRED_CURSOR (f);
20973 *width = FRAME_CURSOR_WIDTH (f);
20975 else
20976 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20978 /* Use normal cursor if not blinked off. */
20979 if (!w->cursor_off_p)
20981 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20982 if (cursor_type == FILLED_BOX_CURSOR)
20983 cursor_type = HOLLOW_BOX_CURSOR;
20985 return cursor_type;
20988 /* Cursor is blinked off, so determine how to "toggle" it. */
20990 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20991 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20992 return get_specified_cursor_type (XCDR (alt_cursor), width);
20994 /* Then see if frame has specified a specific blink off cursor type. */
20995 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20997 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20998 return FRAME_BLINK_OFF_CURSOR (f);
21001 #if 0
21002 /* Some people liked having a permanently visible blinking cursor,
21003 while others had very strong opinions against it. So it was
21004 decided to remove it. KFS 2003-09-03 */
21006 /* Finally perform built-in cursor blinking:
21007 filled box <-> hollow box
21008 wide [h]bar <-> narrow [h]bar
21009 narrow [h]bar <-> no cursor
21010 other type <-> no cursor */
21012 if (cursor_type == FILLED_BOX_CURSOR)
21013 return HOLLOW_BOX_CURSOR;
21015 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21017 *width = 1;
21018 return cursor_type;
21020 #endif
21022 return NO_CURSOR;
21026 #ifdef HAVE_WINDOW_SYSTEM
21028 /* Notice when the text cursor of window W has been completely
21029 overwritten by a drawing operation that outputs glyphs in AREA
21030 starting at X0 and ending at X1 in the line starting at Y0 and
21031 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21032 the rest of the line after X0 has been written. Y coordinates
21033 are window-relative. */
21035 static void
21036 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21037 struct window *w;
21038 enum glyph_row_area area;
21039 int x0, y0, x1, y1;
21041 int cx0, cx1, cy0, cy1;
21042 struct glyph_row *row;
21044 if (!w->phys_cursor_on_p)
21045 return;
21046 if (area != TEXT_AREA)
21047 return;
21049 if (w->phys_cursor.vpos < 0
21050 || w->phys_cursor.vpos >= w->current_matrix->nrows
21051 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21052 !(row->enabled_p && row->displays_text_p)))
21053 return;
21055 if (row->cursor_in_fringe_p)
21057 row->cursor_in_fringe_p = 0;
21058 draw_fringe_bitmap (w, row, 0);
21059 w->phys_cursor_on_p = 0;
21060 return;
21063 cx0 = w->phys_cursor.x;
21064 cx1 = cx0 + w->phys_cursor_width;
21065 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21066 return;
21068 /* The cursor image will be completely removed from the
21069 screen if the output area intersects the cursor area in
21070 y-direction. When we draw in [y0 y1[, and some part of
21071 the cursor is at y < y0, that part must have been drawn
21072 before. When scrolling, the cursor is erased before
21073 actually scrolling, so we don't come here. When not
21074 scrolling, the rows above the old cursor row must have
21075 changed, and in this case these rows must have written
21076 over the cursor image.
21078 Likewise if part of the cursor is below y1, with the
21079 exception of the cursor being in the first blank row at
21080 the buffer and window end because update_text_area
21081 doesn't draw that row. (Except when it does, but
21082 that's handled in update_text_area.) */
21084 cy0 = w->phys_cursor.y;
21085 cy1 = cy0 + w->phys_cursor_height;
21086 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21087 return;
21089 w->phys_cursor_on_p = 0;
21092 #endif /* HAVE_WINDOW_SYSTEM */
21095 /************************************************************************
21096 Mouse Face
21097 ************************************************************************/
21099 #ifdef HAVE_WINDOW_SYSTEM
21101 /* EXPORT for RIF:
21102 Fix the display of area AREA of overlapping row ROW in window W
21103 with respect to the overlapping part OVERLAPS. */
21105 void
21106 x_fix_overlapping_area (w, row, area, overlaps)
21107 struct window *w;
21108 struct glyph_row *row;
21109 enum glyph_row_area area;
21110 int overlaps;
21112 int i, x;
21114 BLOCK_INPUT;
21116 x = 0;
21117 for (i = 0; i < row->used[area];)
21119 if (row->glyphs[area][i].overlaps_vertically_p)
21121 int start = i, start_x = x;
21125 x += row->glyphs[area][i].pixel_width;
21126 ++i;
21128 while (i < row->used[area]
21129 && row->glyphs[area][i].overlaps_vertically_p);
21131 draw_glyphs (w, start_x, row, area,
21132 start, i,
21133 DRAW_NORMAL_TEXT, overlaps);
21135 else
21137 x += row->glyphs[area][i].pixel_width;
21138 ++i;
21142 UNBLOCK_INPUT;
21146 /* EXPORT:
21147 Draw the cursor glyph of window W in glyph row ROW. See the
21148 comment of draw_glyphs for the meaning of HL. */
21150 void
21151 draw_phys_cursor_glyph (w, row, hl)
21152 struct window *w;
21153 struct glyph_row *row;
21154 enum draw_glyphs_face hl;
21156 /* If cursor hpos is out of bounds, don't draw garbage. This can
21157 happen in mini-buffer windows when switching between echo area
21158 glyphs and mini-buffer. */
21159 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21161 int on_p = w->phys_cursor_on_p;
21162 int x1;
21163 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21164 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21165 hl, 0);
21166 w->phys_cursor_on_p = on_p;
21168 if (hl == DRAW_CURSOR)
21169 w->phys_cursor_width = x1 - w->phys_cursor.x;
21170 /* When we erase the cursor, and ROW is overlapped by other
21171 rows, make sure that these overlapping parts of other rows
21172 are redrawn. */
21173 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21175 w->phys_cursor_width = x1 - w->phys_cursor.x;
21177 if (row > w->current_matrix->rows
21178 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21179 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21180 OVERLAPS_ERASED_CURSOR);
21182 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21183 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21184 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21185 OVERLAPS_ERASED_CURSOR);
21191 /* EXPORT:
21192 Erase the image of a cursor of window W from the screen. */
21194 void
21195 erase_phys_cursor (w)
21196 struct window *w;
21198 struct frame *f = XFRAME (w->frame);
21199 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21200 int hpos = w->phys_cursor.hpos;
21201 int vpos = w->phys_cursor.vpos;
21202 int mouse_face_here_p = 0;
21203 struct glyph_matrix *active_glyphs = w->current_matrix;
21204 struct glyph_row *cursor_row;
21205 struct glyph *cursor_glyph;
21206 enum draw_glyphs_face hl;
21208 /* No cursor displayed or row invalidated => nothing to do on the
21209 screen. */
21210 if (w->phys_cursor_type == NO_CURSOR)
21211 goto mark_cursor_off;
21213 /* VPOS >= active_glyphs->nrows means that window has been resized.
21214 Don't bother to erase the cursor. */
21215 if (vpos >= active_glyphs->nrows)
21216 goto mark_cursor_off;
21218 /* If row containing cursor is marked invalid, there is nothing we
21219 can do. */
21220 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21221 if (!cursor_row->enabled_p)
21222 goto mark_cursor_off;
21224 /* If line spacing is > 0, old cursor may only be partially visible in
21225 window after split-window. So adjust visible height. */
21226 cursor_row->visible_height = min (cursor_row->visible_height,
21227 window_text_bottom_y (w) - cursor_row->y);
21229 /* If row is completely invisible, don't attempt to delete a cursor which
21230 isn't there. This can happen if cursor is at top of a window, and
21231 we switch to a buffer with a header line in that window. */
21232 if (cursor_row->visible_height <= 0)
21233 goto mark_cursor_off;
21235 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21236 if (cursor_row->cursor_in_fringe_p)
21238 cursor_row->cursor_in_fringe_p = 0;
21239 draw_fringe_bitmap (w, cursor_row, 0);
21240 goto mark_cursor_off;
21243 /* This can happen when the new row is shorter than the old one.
21244 In this case, either draw_glyphs or clear_end_of_line
21245 should have cleared the cursor. Note that we wouldn't be
21246 able to erase the cursor in this case because we don't have a
21247 cursor glyph at hand. */
21248 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21249 goto mark_cursor_off;
21251 /* If the cursor is in the mouse face area, redisplay that when
21252 we clear the cursor. */
21253 if (! NILP (dpyinfo->mouse_face_window)
21254 && w == XWINDOW (dpyinfo->mouse_face_window)
21255 && (vpos > dpyinfo->mouse_face_beg_row
21256 || (vpos == dpyinfo->mouse_face_beg_row
21257 && hpos >= dpyinfo->mouse_face_beg_col))
21258 && (vpos < dpyinfo->mouse_face_end_row
21259 || (vpos == dpyinfo->mouse_face_end_row
21260 && hpos < dpyinfo->mouse_face_end_col))
21261 /* Don't redraw the cursor's spot in mouse face if it is at the
21262 end of a line (on a newline). The cursor appears there, but
21263 mouse highlighting does not. */
21264 && cursor_row->used[TEXT_AREA] > hpos)
21265 mouse_face_here_p = 1;
21267 /* Maybe clear the display under the cursor. */
21268 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21270 int x, y;
21271 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21272 int width;
21274 cursor_glyph = get_phys_cursor_glyph (w);
21275 if (cursor_glyph == NULL)
21276 goto mark_cursor_off;
21278 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21279 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21280 width = min (cursor_glyph->pixel_width,
21281 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21283 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21286 /* Erase the cursor by redrawing the character underneath it. */
21287 if (mouse_face_here_p)
21288 hl = DRAW_MOUSE_FACE;
21289 else
21290 hl = DRAW_NORMAL_TEXT;
21291 draw_phys_cursor_glyph (w, cursor_row, hl);
21293 mark_cursor_off:
21294 w->phys_cursor_on_p = 0;
21295 w->phys_cursor_type = NO_CURSOR;
21299 /* EXPORT:
21300 Display or clear cursor of window W. If ON is zero, clear the
21301 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21302 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21304 void
21305 display_and_set_cursor (w, on, hpos, vpos, x, y)
21306 struct window *w;
21307 int on, hpos, vpos, x, y;
21309 struct frame *f = XFRAME (w->frame);
21310 int new_cursor_type;
21311 int new_cursor_width;
21312 int active_cursor;
21313 struct glyph_row *glyph_row;
21314 struct glyph *glyph;
21316 /* This is pointless on invisible frames, and dangerous on garbaged
21317 windows and frames; in the latter case, the frame or window may
21318 be in the midst of changing its size, and x and y may be off the
21319 window. */
21320 if (! FRAME_VISIBLE_P (f)
21321 || FRAME_GARBAGED_P (f)
21322 || vpos >= w->current_matrix->nrows
21323 || hpos >= w->current_matrix->matrix_w)
21324 return;
21326 /* If cursor is off and we want it off, return quickly. */
21327 if (!on && !w->phys_cursor_on_p)
21328 return;
21330 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21331 /* If cursor row is not enabled, we don't really know where to
21332 display the cursor. */
21333 if (!glyph_row->enabled_p)
21335 w->phys_cursor_on_p = 0;
21336 return;
21339 glyph = NULL;
21340 if (!glyph_row->exact_window_width_line_p
21341 || hpos < glyph_row->used[TEXT_AREA])
21342 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21344 xassert (interrupt_input_blocked);
21346 /* Set new_cursor_type to the cursor we want to be displayed. */
21347 new_cursor_type = get_window_cursor_type (w, glyph,
21348 &new_cursor_width, &active_cursor);
21350 /* If cursor is currently being shown and we don't want it to be or
21351 it is in the wrong place, or the cursor type is not what we want,
21352 erase it. */
21353 if (w->phys_cursor_on_p
21354 && (!on
21355 || w->phys_cursor.x != x
21356 || w->phys_cursor.y != y
21357 || new_cursor_type != w->phys_cursor_type
21358 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21359 && new_cursor_width != w->phys_cursor_width)))
21360 erase_phys_cursor (w);
21362 /* Don't check phys_cursor_on_p here because that flag is only set
21363 to zero in some cases where we know that the cursor has been
21364 completely erased, to avoid the extra work of erasing the cursor
21365 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21366 still not be visible, or it has only been partly erased. */
21367 if (on)
21369 w->phys_cursor_ascent = glyph_row->ascent;
21370 w->phys_cursor_height = glyph_row->height;
21372 /* Set phys_cursor_.* before x_draw_.* is called because some
21373 of them may need the information. */
21374 w->phys_cursor.x = x;
21375 w->phys_cursor.y = glyph_row->y;
21376 w->phys_cursor.hpos = hpos;
21377 w->phys_cursor.vpos = vpos;
21380 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21381 new_cursor_type, new_cursor_width,
21382 on, active_cursor);
21386 /* Switch the display of W's cursor on or off, according to the value
21387 of ON. */
21389 static void
21390 update_window_cursor (w, on)
21391 struct window *w;
21392 int on;
21394 /* Don't update cursor in windows whose frame is in the process
21395 of being deleted. */
21396 if (w->current_matrix)
21398 BLOCK_INPUT;
21399 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21400 w->phys_cursor.x, w->phys_cursor.y);
21401 UNBLOCK_INPUT;
21406 /* Call update_window_cursor with parameter ON_P on all leaf windows
21407 in the window tree rooted at W. */
21409 static void
21410 update_cursor_in_window_tree (w, on_p)
21411 struct window *w;
21412 int on_p;
21414 while (w)
21416 if (!NILP (w->hchild))
21417 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21418 else if (!NILP (w->vchild))
21419 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21420 else
21421 update_window_cursor (w, on_p);
21423 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21428 /* EXPORT:
21429 Display the cursor on window W, or clear it, according to ON_P.
21430 Don't change the cursor's position. */
21432 void
21433 x_update_cursor (f, on_p)
21434 struct frame *f;
21435 int on_p;
21437 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21441 /* EXPORT:
21442 Clear the cursor of window W to background color, and mark the
21443 cursor as not shown. This is used when the text where the cursor
21444 is is about to be rewritten. */
21446 void
21447 x_clear_cursor (w)
21448 struct window *w;
21450 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21451 update_window_cursor (w, 0);
21455 /* EXPORT:
21456 Display the active region described by mouse_face_* according to DRAW. */
21458 void
21459 show_mouse_face (dpyinfo, draw)
21460 Display_Info *dpyinfo;
21461 enum draw_glyphs_face draw;
21463 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21464 struct frame *f = XFRAME (WINDOW_FRAME (w));
21466 if (/* If window is in the process of being destroyed, don't bother
21467 to do anything. */
21468 w->current_matrix != NULL
21469 /* Don't update mouse highlight if hidden */
21470 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21471 /* Recognize when we are called to operate on rows that don't exist
21472 anymore. This can happen when a window is split. */
21473 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21475 int phys_cursor_on_p = w->phys_cursor_on_p;
21476 struct glyph_row *row, *first, *last;
21478 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21479 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21481 for (row = first; row <= last && row->enabled_p; ++row)
21483 int start_hpos, end_hpos, start_x;
21485 /* For all but the first row, the highlight starts at column 0. */
21486 if (row == first)
21488 start_hpos = dpyinfo->mouse_face_beg_col;
21489 start_x = dpyinfo->mouse_face_beg_x;
21491 else
21493 start_hpos = 0;
21494 start_x = 0;
21497 if (row == last)
21498 end_hpos = dpyinfo->mouse_face_end_col;
21499 else
21501 end_hpos = row->used[TEXT_AREA];
21502 if (draw == DRAW_NORMAL_TEXT)
21503 row->fill_line_p = 1; /* Clear to end of line */
21506 if (end_hpos > start_hpos)
21508 draw_glyphs (w, start_x, row, TEXT_AREA,
21509 start_hpos, end_hpos,
21510 draw, 0);
21512 row->mouse_face_p
21513 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21517 /* When we've written over the cursor, arrange for it to
21518 be displayed again. */
21519 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21521 BLOCK_INPUT;
21522 display_and_set_cursor (w, 1,
21523 w->phys_cursor.hpos, w->phys_cursor.vpos,
21524 w->phys_cursor.x, w->phys_cursor.y);
21525 UNBLOCK_INPUT;
21529 /* Change the mouse cursor. */
21530 if (draw == DRAW_NORMAL_TEXT)
21531 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21532 else if (draw == DRAW_MOUSE_FACE)
21533 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21534 else
21535 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21538 /* EXPORT:
21539 Clear out the mouse-highlighted active region.
21540 Redraw it un-highlighted first. Value is non-zero if mouse
21541 face was actually drawn unhighlighted. */
21544 clear_mouse_face (dpyinfo)
21545 Display_Info *dpyinfo;
21547 int cleared = 0;
21549 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21551 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21552 cleared = 1;
21555 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21556 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21557 dpyinfo->mouse_face_window = Qnil;
21558 dpyinfo->mouse_face_overlay = Qnil;
21559 return cleared;
21563 /* EXPORT:
21564 Non-zero if physical cursor of window W is within mouse face. */
21567 cursor_in_mouse_face_p (w)
21568 struct window *w;
21570 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21571 int in_mouse_face = 0;
21573 if (WINDOWP (dpyinfo->mouse_face_window)
21574 && XWINDOW (dpyinfo->mouse_face_window) == w)
21576 int hpos = w->phys_cursor.hpos;
21577 int vpos = w->phys_cursor.vpos;
21579 if (vpos >= dpyinfo->mouse_face_beg_row
21580 && vpos <= dpyinfo->mouse_face_end_row
21581 && (vpos > dpyinfo->mouse_face_beg_row
21582 || hpos >= dpyinfo->mouse_face_beg_col)
21583 && (vpos < dpyinfo->mouse_face_end_row
21584 || hpos < dpyinfo->mouse_face_end_col
21585 || dpyinfo->mouse_face_past_end))
21586 in_mouse_face = 1;
21589 return in_mouse_face;
21595 /* Find the glyph matrix position of buffer position CHARPOS in window
21596 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21597 current glyphs must be up to date. If CHARPOS is above window
21598 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21599 of last line in W. In the row containing CHARPOS, stop before glyphs
21600 having STOP as object. */
21602 #if 1 /* This is a version of fast_find_position that's more correct
21603 in the presence of hscrolling, for example. I didn't install
21604 it right away because the problem fixed is minor, it failed
21605 in 20.x as well, and I think it's too risky to install
21606 so near the release of 21.1. 2001-09-25 gerd. */
21608 static int
21609 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21610 struct window *w;
21611 int charpos;
21612 int *hpos, *vpos, *x, *y;
21613 Lisp_Object stop;
21615 struct glyph_row *row, *first;
21616 struct glyph *glyph, *end;
21617 int past_end = 0;
21619 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21620 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21622 *x = first->x;
21623 *y = first->y;
21624 *hpos = 0;
21625 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21626 return 1;
21629 row = row_containing_pos (w, charpos, first, NULL, 0);
21630 if (row == NULL)
21632 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21633 past_end = 1;
21636 /* If whole rows or last part of a row came from a display overlay,
21637 row_containing_pos will skip over such rows because their end pos
21638 equals the start pos of the overlay or interval.
21640 Move back if we have a STOP object and previous row's
21641 end glyph came from STOP. */
21642 if (!NILP (stop))
21644 struct glyph_row *prev;
21645 while ((prev = row - 1, prev >= first)
21646 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21647 && prev->used[TEXT_AREA] > 0)
21649 struct glyph *beg = prev->glyphs[TEXT_AREA];
21650 glyph = beg + prev->used[TEXT_AREA];
21651 while (--glyph >= beg
21652 && INTEGERP (glyph->object));
21653 if (glyph < beg
21654 || !EQ (stop, glyph->object))
21655 break;
21656 row = prev;
21660 *x = row->x;
21661 *y = row->y;
21662 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21664 glyph = row->glyphs[TEXT_AREA];
21665 end = glyph + row->used[TEXT_AREA];
21667 /* Skip over glyphs not having an object at the start of the row.
21668 These are special glyphs like truncation marks on terminal
21669 frames. */
21670 if (row->displays_text_p)
21671 while (glyph < end
21672 && INTEGERP (glyph->object)
21673 && !EQ (stop, glyph->object)
21674 && glyph->charpos < 0)
21676 *x += glyph->pixel_width;
21677 ++glyph;
21680 while (glyph < end
21681 && !INTEGERP (glyph->object)
21682 && !EQ (stop, glyph->object)
21683 && (!BUFFERP (glyph->object)
21684 || glyph->charpos < charpos))
21686 *x += glyph->pixel_width;
21687 ++glyph;
21690 *hpos = glyph - row->glyphs[TEXT_AREA];
21691 return !past_end;
21694 #else /* not 1 */
21696 static int
21697 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21698 struct window *w;
21699 int pos;
21700 int *hpos, *vpos, *x, *y;
21701 Lisp_Object stop;
21703 int i;
21704 int lastcol;
21705 int maybe_next_line_p = 0;
21706 int line_start_position;
21707 int yb = window_text_bottom_y (w);
21708 struct glyph_row *row, *best_row;
21709 int row_vpos, best_row_vpos;
21710 int current_x;
21712 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21713 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21715 while (row->y < yb)
21717 if (row->used[TEXT_AREA])
21718 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21719 else
21720 line_start_position = 0;
21722 if (line_start_position > pos)
21723 break;
21724 /* If the position sought is the end of the buffer,
21725 don't include the blank lines at the bottom of the window. */
21726 else if (line_start_position == pos
21727 && pos == BUF_ZV (XBUFFER (w->buffer)))
21729 maybe_next_line_p = 1;
21730 break;
21732 else if (line_start_position > 0)
21734 best_row = row;
21735 best_row_vpos = row_vpos;
21738 if (row->y + row->height >= yb)
21739 break;
21741 ++row;
21742 ++row_vpos;
21745 /* Find the right column within BEST_ROW. */
21746 lastcol = 0;
21747 current_x = best_row->x;
21748 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21750 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21751 int charpos = glyph->charpos;
21753 if (BUFFERP (glyph->object))
21755 if (charpos == pos)
21757 *hpos = i;
21758 *vpos = best_row_vpos;
21759 *x = current_x;
21760 *y = best_row->y;
21761 return 1;
21763 else if (charpos > pos)
21764 break;
21766 else if (EQ (glyph->object, stop))
21767 break;
21769 if (charpos > 0)
21770 lastcol = i;
21771 current_x += glyph->pixel_width;
21774 /* If we're looking for the end of the buffer,
21775 and we didn't find it in the line we scanned,
21776 use the start of the following line. */
21777 if (maybe_next_line_p)
21779 ++best_row;
21780 ++best_row_vpos;
21781 lastcol = 0;
21782 current_x = best_row->x;
21785 *vpos = best_row_vpos;
21786 *hpos = lastcol + 1;
21787 *x = current_x;
21788 *y = best_row->y;
21789 return 0;
21792 #endif /* not 1 */
21795 /* Find the position of the glyph for position POS in OBJECT in
21796 window W's current matrix, and return in *X, *Y the pixel
21797 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21799 RIGHT_P non-zero means return the position of the right edge of the
21800 glyph, RIGHT_P zero means return the left edge position.
21802 If no glyph for POS exists in the matrix, return the position of
21803 the glyph with the next smaller position that is in the matrix, if
21804 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21805 exists in the matrix, return the position of the glyph with the
21806 next larger position in OBJECT.
21808 Value is non-zero if a glyph was found. */
21810 static int
21811 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21812 struct window *w;
21813 int pos;
21814 Lisp_Object object;
21815 int *hpos, *vpos, *x, *y;
21816 int right_p;
21818 int yb = window_text_bottom_y (w);
21819 struct glyph_row *r;
21820 struct glyph *best_glyph = NULL;
21821 struct glyph_row *best_row = NULL;
21822 int best_x = 0;
21824 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21825 r->enabled_p && r->y < yb;
21826 ++r)
21828 struct glyph *g = r->glyphs[TEXT_AREA];
21829 struct glyph *e = g + r->used[TEXT_AREA];
21830 int gx;
21832 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21833 if (EQ (g->object, object))
21835 if (g->charpos == pos)
21837 best_glyph = g;
21838 best_x = gx;
21839 best_row = r;
21840 goto found;
21842 else if (best_glyph == NULL
21843 || ((abs (g->charpos - pos)
21844 < abs (best_glyph->charpos - pos))
21845 && (right_p
21846 ? g->charpos < pos
21847 : g->charpos > pos)))
21849 best_glyph = g;
21850 best_x = gx;
21851 best_row = r;
21856 found:
21858 if (best_glyph)
21860 *x = best_x;
21861 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21863 if (right_p)
21865 *x += best_glyph->pixel_width;
21866 ++*hpos;
21869 *y = best_row->y;
21870 *vpos = best_row - w->current_matrix->rows;
21873 return best_glyph != NULL;
21877 /* See if position X, Y is within a hot-spot of an image. */
21879 static int
21880 on_hot_spot_p (hot_spot, x, y)
21881 Lisp_Object hot_spot;
21882 int x, y;
21884 if (!CONSP (hot_spot))
21885 return 0;
21887 if (EQ (XCAR (hot_spot), Qrect))
21889 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21890 Lisp_Object rect = XCDR (hot_spot);
21891 Lisp_Object tem;
21892 if (!CONSP (rect))
21893 return 0;
21894 if (!CONSP (XCAR (rect)))
21895 return 0;
21896 if (!CONSP (XCDR (rect)))
21897 return 0;
21898 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21899 return 0;
21900 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21901 return 0;
21902 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21903 return 0;
21904 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21905 return 0;
21906 return 1;
21908 else if (EQ (XCAR (hot_spot), Qcircle))
21910 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21911 Lisp_Object circ = XCDR (hot_spot);
21912 Lisp_Object lr, lx0, ly0;
21913 if (CONSP (circ)
21914 && CONSP (XCAR (circ))
21915 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21916 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21917 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21919 double r = XFLOATINT (lr);
21920 double dx = XINT (lx0) - x;
21921 double dy = XINT (ly0) - y;
21922 return (dx * dx + dy * dy <= r * r);
21925 else if (EQ (XCAR (hot_spot), Qpoly))
21927 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21928 if (VECTORP (XCDR (hot_spot)))
21930 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21931 Lisp_Object *poly = v->contents;
21932 int n = v->size;
21933 int i;
21934 int inside = 0;
21935 Lisp_Object lx, ly;
21936 int x0, y0;
21938 /* Need an even number of coordinates, and at least 3 edges. */
21939 if (n < 6 || n & 1)
21940 return 0;
21942 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21943 If count is odd, we are inside polygon. Pixels on edges
21944 may or may not be included depending on actual geometry of the
21945 polygon. */
21946 if ((lx = poly[n-2], !INTEGERP (lx))
21947 || (ly = poly[n-1], !INTEGERP (lx)))
21948 return 0;
21949 x0 = XINT (lx), y0 = XINT (ly);
21950 for (i = 0; i < n; i += 2)
21952 int x1 = x0, y1 = y0;
21953 if ((lx = poly[i], !INTEGERP (lx))
21954 || (ly = poly[i+1], !INTEGERP (ly)))
21955 return 0;
21956 x0 = XINT (lx), y0 = XINT (ly);
21958 /* Does this segment cross the X line? */
21959 if (x0 >= x)
21961 if (x1 >= x)
21962 continue;
21964 else if (x1 < x)
21965 continue;
21966 if (y > y0 && y > y1)
21967 continue;
21968 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21969 inside = !inside;
21971 return inside;
21974 return 0;
21977 Lisp_Object
21978 find_hot_spot (map, x, y)
21979 Lisp_Object map;
21980 int x, y;
21982 while (CONSP (map))
21984 if (CONSP (XCAR (map))
21985 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21986 return XCAR (map);
21987 map = XCDR (map);
21990 return Qnil;
21993 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21994 3, 3, 0,
21995 doc: /* Lookup in image map MAP coordinates X and Y.
21996 An image map is an alist where each element has the format (AREA ID PLIST).
21997 An AREA is specified as either a rectangle, a circle, or a polygon:
21998 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21999 pixel coordinates of the upper left and bottom right corners.
22000 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22001 and the radius of the circle; r may be a float or integer.
22002 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22003 vector describes one corner in the polygon.
22004 Returns the alist element for the first matching AREA in MAP. */)
22005 (map, x, y)
22006 Lisp_Object map;
22007 Lisp_Object x, y;
22009 if (NILP (map))
22010 return Qnil;
22012 CHECK_NUMBER (x);
22013 CHECK_NUMBER (y);
22015 return find_hot_spot (map, XINT (x), XINT (y));
22019 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22020 static void
22021 define_frame_cursor1 (f, cursor, pointer)
22022 struct frame *f;
22023 Cursor cursor;
22024 Lisp_Object pointer;
22026 /* Do not change cursor shape while dragging mouse. */
22027 if (!NILP (do_mouse_tracking))
22028 return;
22030 if (!NILP (pointer))
22032 if (EQ (pointer, Qarrow))
22033 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22034 else if (EQ (pointer, Qhand))
22035 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22036 else if (EQ (pointer, Qtext))
22037 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22038 else if (EQ (pointer, intern ("hdrag")))
22039 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22040 #ifdef HAVE_X_WINDOWS
22041 else if (EQ (pointer, intern ("vdrag")))
22042 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22043 #endif
22044 else if (EQ (pointer, intern ("hourglass")))
22045 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22046 else if (EQ (pointer, Qmodeline))
22047 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22048 else
22049 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22052 if (cursor != No_Cursor)
22053 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22056 /* Take proper action when mouse has moved to the mode or header line
22057 or marginal area AREA of window W, x-position X and y-position Y.
22058 X is relative to the start of the text display area of W, so the
22059 width of bitmap areas and scroll bars must be subtracted to get a
22060 position relative to the start of the mode line. */
22062 static void
22063 note_mode_line_or_margin_highlight (window, x, y, area)
22064 Lisp_Object window;
22065 int x, y;
22066 enum window_part area;
22068 struct window *w = XWINDOW (window);
22069 struct frame *f = XFRAME (w->frame);
22070 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22071 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22072 Lisp_Object pointer = Qnil;
22073 int charpos, dx, dy, width, height;
22074 Lisp_Object string, object = Qnil;
22075 Lisp_Object pos, help;
22077 Lisp_Object mouse_face;
22078 int original_x_pixel = x;
22079 struct glyph * glyph = NULL;
22080 struct glyph_row *row;
22082 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22084 int x0;
22085 struct glyph *end;
22087 string = mode_line_string (w, area, &x, &y, &charpos,
22088 &object, &dx, &dy, &width, &height);
22090 row = (area == ON_MODE_LINE
22091 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22092 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22094 /* Find glyph */
22095 if (row->mode_line_p && row->enabled_p)
22097 glyph = row->glyphs[TEXT_AREA];
22098 end = glyph + row->used[TEXT_AREA];
22100 for (x0 = original_x_pixel;
22101 glyph < end && x0 >= glyph->pixel_width;
22102 ++glyph)
22103 x0 -= glyph->pixel_width;
22105 if (glyph >= end)
22106 glyph = NULL;
22109 else
22111 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22112 string = marginal_area_string (w, area, &x, &y, &charpos,
22113 &object, &dx, &dy, &width, &height);
22116 help = Qnil;
22118 if (IMAGEP (object))
22120 Lisp_Object image_map, hotspot;
22121 if ((image_map = Fplist_get (XCDR (object), QCmap),
22122 !NILP (image_map))
22123 && (hotspot = find_hot_spot (image_map, dx, dy),
22124 CONSP (hotspot))
22125 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22127 Lisp_Object area_id, plist;
22129 area_id = XCAR (hotspot);
22130 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22131 If so, we could look for mouse-enter, mouse-leave
22132 properties in PLIST (and do something...). */
22133 hotspot = XCDR (hotspot);
22134 if (CONSP (hotspot)
22135 && (plist = XCAR (hotspot), CONSP (plist)))
22137 pointer = Fplist_get (plist, Qpointer);
22138 if (NILP (pointer))
22139 pointer = Qhand;
22140 help = Fplist_get (plist, Qhelp_echo);
22141 if (!NILP (help))
22143 help_echo_string = help;
22144 /* Is this correct? ++kfs */
22145 XSETWINDOW (help_echo_window, w);
22146 help_echo_object = w->buffer;
22147 help_echo_pos = charpos;
22151 if (NILP (pointer))
22152 pointer = Fplist_get (XCDR (object), QCpointer);
22155 if (STRINGP (string))
22157 pos = make_number (charpos);
22158 /* If we're on a string with `help-echo' text property, arrange
22159 for the help to be displayed. This is done by setting the
22160 global variable help_echo_string to the help string. */
22161 if (NILP (help))
22163 help = Fget_text_property (pos, Qhelp_echo, string);
22164 if (!NILP (help))
22166 help_echo_string = help;
22167 XSETWINDOW (help_echo_window, w);
22168 help_echo_object = string;
22169 help_echo_pos = charpos;
22173 if (NILP (pointer))
22174 pointer = Fget_text_property (pos, Qpointer, string);
22176 /* Change the mouse pointer according to what is under X/Y. */
22177 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22179 Lisp_Object map;
22180 map = Fget_text_property (pos, Qlocal_map, string);
22181 if (!KEYMAPP (map))
22182 map = Fget_text_property (pos, Qkeymap, string);
22183 if (!KEYMAPP (map))
22184 cursor = dpyinfo->vertical_scroll_bar_cursor;
22187 /* Change the mouse face according to what is under X/Y. */
22188 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22189 if (!NILP (mouse_face)
22190 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22191 && glyph)
22193 Lisp_Object b, e;
22195 struct glyph * tmp_glyph;
22197 int gpos;
22198 int gseq_length;
22199 int total_pixel_width;
22200 int ignore;
22202 int vpos, hpos;
22204 b = Fprevious_single_property_change (make_number (charpos + 1),
22205 Qmouse_face, string, Qnil);
22206 if (NILP (b))
22207 b = make_number (0);
22209 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22210 if (NILP (e))
22211 e = make_number (SCHARS (string));
22213 /* Calculate the position(glyph position: GPOS) of GLYPH in
22214 displayed string. GPOS is different from CHARPOS.
22216 CHARPOS is the position of glyph in internal string
22217 object. A mode line string format has structures which
22218 is converted to a flatten by emacs lisp interpreter.
22219 The internal string is an element of the structures.
22220 The displayed string is the flatten string. */
22221 for (tmp_glyph = glyph - 1, gpos = 0;
22222 tmp_glyph->charpos >= XINT (b);
22223 tmp_glyph--, gpos++)
22225 if (!EQ (tmp_glyph->object, glyph->object))
22226 break;
22229 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22230 displayed string holding GLYPH.
22232 GSEQ_LENGTH is different from SCHARS (STRING).
22233 SCHARS (STRING) returns the length of the internal string. */
22234 for (tmp_glyph = glyph, gseq_length = gpos;
22235 tmp_glyph->charpos < XINT (e);
22236 tmp_glyph++, gseq_length++)
22238 if (!EQ (tmp_glyph->object, glyph->object))
22239 break;
22242 total_pixel_width = 0;
22243 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22244 total_pixel_width += tmp_glyph->pixel_width;
22246 /* Pre calculation of re-rendering position */
22247 vpos = (x - gpos);
22248 hpos = (area == ON_MODE_LINE
22249 ? (w->current_matrix)->nrows - 1
22250 : 0);
22252 /* If the re-rendering position is included in the last
22253 re-rendering area, we should do nothing. */
22254 if ( EQ (window, dpyinfo->mouse_face_window)
22255 && dpyinfo->mouse_face_beg_col <= vpos
22256 && vpos < dpyinfo->mouse_face_end_col
22257 && dpyinfo->mouse_face_beg_row == hpos )
22258 return;
22260 if (clear_mouse_face (dpyinfo))
22261 cursor = No_Cursor;
22263 dpyinfo->mouse_face_beg_col = vpos;
22264 dpyinfo->mouse_face_beg_row = hpos;
22266 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22267 dpyinfo->mouse_face_beg_y = 0;
22269 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22270 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22272 dpyinfo->mouse_face_end_x = 0;
22273 dpyinfo->mouse_face_end_y = 0;
22275 dpyinfo->mouse_face_past_end = 0;
22276 dpyinfo->mouse_face_window = window;
22278 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22279 charpos,
22280 0, 0, 0, &ignore,
22281 glyph->face_id, 1);
22282 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22284 if (NILP (pointer))
22285 pointer = Qhand;
22287 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22288 clear_mouse_face (dpyinfo);
22290 define_frame_cursor1 (f, cursor, pointer);
22294 /* EXPORT:
22295 Take proper action when the mouse has moved to position X, Y on
22296 frame F as regards highlighting characters that have mouse-face
22297 properties. Also de-highlighting chars where the mouse was before.
22298 X and Y can be negative or out of range. */
22300 void
22301 note_mouse_highlight (f, x, y)
22302 struct frame *f;
22303 int x, y;
22305 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22306 enum window_part part;
22307 Lisp_Object window;
22308 struct window *w;
22309 Cursor cursor = No_Cursor;
22310 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22311 struct buffer *b;
22313 /* When a menu is active, don't highlight because this looks odd. */
22314 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22315 if (popup_activated ())
22316 return;
22317 #endif
22319 if (NILP (Vmouse_highlight)
22320 || !f->glyphs_initialized_p)
22321 return;
22323 dpyinfo->mouse_face_mouse_x = x;
22324 dpyinfo->mouse_face_mouse_y = y;
22325 dpyinfo->mouse_face_mouse_frame = f;
22327 if (dpyinfo->mouse_face_defer)
22328 return;
22330 if (gc_in_progress)
22332 dpyinfo->mouse_face_deferred_gc = 1;
22333 return;
22336 /* Which window is that in? */
22337 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22339 /* If we were displaying active text in another window, clear that.
22340 Also clear if we move out of text area in same window. */
22341 if (! EQ (window, dpyinfo->mouse_face_window)
22342 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22343 && !NILP (dpyinfo->mouse_face_window)))
22344 clear_mouse_face (dpyinfo);
22346 /* Not on a window -> return. */
22347 if (!WINDOWP (window))
22348 return;
22350 /* Reset help_echo_string. It will get recomputed below. */
22351 help_echo_string = Qnil;
22353 /* Convert to window-relative pixel coordinates. */
22354 w = XWINDOW (window);
22355 frame_to_window_pixel_xy (w, &x, &y);
22357 /* Handle tool-bar window differently since it doesn't display a
22358 buffer. */
22359 if (EQ (window, f->tool_bar_window))
22361 note_tool_bar_highlight (f, x, y);
22362 return;
22365 /* Mouse is on the mode, header line or margin? */
22366 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22367 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22369 note_mode_line_or_margin_highlight (window, x, y, part);
22370 return;
22373 if (part == ON_VERTICAL_BORDER)
22375 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22376 help_echo_string = make_string ("drag-mouse-1: resize", 20);
22378 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22379 || part == ON_SCROLL_BAR)
22380 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22381 else
22382 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22384 /* Are we in a window whose display is up to date?
22385 And verify the buffer's text has not changed. */
22386 b = XBUFFER (w->buffer);
22387 if (part == ON_TEXT
22388 && EQ (w->window_end_valid, w->buffer)
22389 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22390 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22392 int hpos, vpos, pos, i, dx, dy, area;
22393 struct glyph *glyph;
22394 Lisp_Object object;
22395 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22396 Lisp_Object *overlay_vec = NULL;
22397 int noverlays;
22398 struct buffer *obuf;
22399 int obegv, ozv, same_region;
22401 /* Find the glyph under X/Y. */
22402 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22404 /* Look for :pointer property on image. */
22405 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22407 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22408 if (img != NULL && IMAGEP (img->spec))
22410 Lisp_Object image_map, hotspot;
22411 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22412 !NILP (image_map))
22413 && (hotspot = find_hot_spot (image_map,
22414 glyph->slice.x + dx,
22415 glyph->slice.y + dy),
22416 CONSP (hotspot))
22417 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22419 Lisp_Object area_id, plist;
22421 area_id = XCAR (hotspot);
22422 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22423 If so, we could look for mouse-enter, mouse-leave
22424 properties in PLIST (and do something...). */
22425 hotspot = XCDR (hotspot);
22426 if (CONSP (hotspot)
22427 && (plist = XCAR (hotspot), CONSP (plist)))
22429 pointer = Fplist_get (plist, Qpointer);
22430 if (NILP (pointer))
22431 pointer = Qhand;
22432 help_echo_string = Fplist_get (plist, Qhelp_echo);
22433 if (!NILP (help_echo_string))
22435 help_echo_window = window;
22436 help_echo_object = glyph->object;
22437 help_echo_pos = glyph->charpos;
22441 if (NILP (pointer))
22442 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22446 /* Clear mouse face if X/Y not over text. */
22447 if (glyph == NULL
22448 || area != TEXT_AREA
22449 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22451 if (clear_mouse_face (dpyinfo))
22452 cursor = No_Cursor;
22453 if (NILP (pointer))
22455 if (area != TEXT_AREA)
22456 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22457 else
22458 pointer = Vvoid_text_area_pointer;
22460 goto set_cursor;
22463 pos = glyph->charpos;
22464 object = glyph->object;
22465 if (!STRINGP (object) && !BUFFERP (object))
22466 goto set_cursor;
22468 /* If we get an out-of-range value, return now; avoid an error. */
22469 if (BUFFERP (object) && pos > BUF_Z (b))
22470 goto set_cursor;
22472 /* Make the window's buffer temporarily current for
22473 overlays_at and compute_char_face. */
22474 obuf = current_buffer;
22475 current_buffer = b;
22476 obegv = BEGV;
22477 ozv = ZV;
22478 BEGV = BEG;
22479 ZV = Z;
22481 /* Is this char mouse-active or does it have help-echo? */
22482 position = make_number (pos);
22484 if (BUFFERP (object))
22486 /* Put all the overlays we want in a vector in overlay_vec. */
22487 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22488 /* Sort overlays into increasing priority order. */
22489 noverlays = sort_overlays (overlay_vec, noverlays, w);
22491 else
22492 noverlays = 0;
22494 same_region = (EQ (window, dpyinfo->mouse_face_window)
22495 && vpos >= dpyinfo->mouse_face_beg_row
22496 && vpos <= dpyinfo->mouse_face_end_row
22497 && (vpos > dpyinfo->mouse_face_beg_row
22498 || hpos >= dpyinfo->mouse_face_beg_col)
22499 && (vpos < dpyinfo->mouse_face_end_row
22500 || hpos < dpyinfo->mouse_face_end_col
22501 || dpyinfo->mouse_face_past_end));
22503 if (same_region)
22504 cursor = No_Cursor;
22506 /* Check mouse-face highlighting. */
22507 if (! same_region
22508 /* If there exists an overlay with mouse-face overlapping
22509 the one we are currently highlighting, we have to
22510 check if we enter the overlapping overlay, and then
22511 highlight only that. */
22512 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22513 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22515 /* Find the highest priority overlay that has a mouse-face
22516 property. */
22517 overlay = Qnil;
22518 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22520 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22521 if (!NILP (mouse_face))
22522 overlay = overlay_vec[i];
22525 /* If we're actually highlighting the same overlay as
22526 before, there's no need to do that again. */
22527 if (!NILP (overlay)
22528 && EQ (overlay, dpyinfo->mouse_face_overlay))
22529 goto check_help_echo;
22531 dpyinfo->mouse_face_overlay = overlay;
22533 /* Clear the display of the old active region, if any. */
22534 if (clear_mouse_face (dpyinfo))
22535 cursor = No_Cursor;
22537 /* If no overlay applies, get a text property. */
22538 if (NILP (overlay))
22539 mouse_face = Fget_text_property (position, Qmouse_face, object);
22541 /* Handle the overlay case. */
22542 if (!NILP (overlay))
22544 /* Find the range of text around this char that
22545 should be active. */
22546 Lisp_Object before, after;
22547 int ignore;
22549 before = Foverlay_start (overlay);
22550 after = Foverlay_end (overlay);
22551 /* Record this as the current active region. */
22552 fast_find_position (w, XFASTINT (before),
22553 &dpyinfo->mouse_face_beg_col,
22554 &dpyinfo->mouse_face_beg_row,
22555 &dpyinfo->mouse_face_beg_x,
22556 &dpyinfo->mouse_face_beg_y, Qnil);
22558 dpyinfo->mouse_face_past_end
22559 = !fast_find_position (w, XFASTINT (after),
22560 &dpyinfo->mouse_face_end_col,
22561 &dpyinfo->mouse_face_end_row,
22562 &dpyinfo->mouse_face_end_x,
22563 &dpyinfo->mouse_face_end_y, Qnil);
22564 dpyinfo->mouse_face_window = window;
22566 dpyinfo->mouse_face_face_id
22567 = face_at_buffer_position (w, pos, 0, 0,
22568 &ignore, pos + 1,
22569 !dpyinfo->mouse_face_hidden);
22571 /* Display it as active. */
22572 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22573 cursor = No_Cursor;
22575 /* Handle the text property case. */
22576 else if (!NILP (mouse_face) && BUFFERP (object))
22578 /* Find the range of text around this char that
22579 should be active. */
22580 Lisp_Object before, after, beginning, end;
22581 int ignore;
22583 beginning = Fmarker_position (w->start);
22584 end = make_number (BUF_Z (XBUFFER (object))
22585 - XFASTINT (w->window_end_pos));
22586 before
22587 = Fprevious_single_property_change (make_number (pos + 1),
22588 Qmouse_face,
22589 object, beginning);
22590 after
22591 = Fnext_single_property_change (position, Qmouse_face,
22592 object, end);
22594 /* Record this as the current active region. */
22595 fast_find_position (w, XFASTINT (before),
22596 &dpyinfo->mouse_face_beg_col,
22597 &dpyinfo->mouse_face_beg_row,
22598 &dpyinfo->mouse_face_beg_x,
22599 &dpyinfo->mouse_face_beg_y, Qnil);
22600 dpyinfo->mouse_face_past_end
22601 = !fast_find_position (w, XFASTINT (after),
22602 &dpyinfo->mouse_face_end_col,
22603 &dpyinfo->mouse_face_end_row,
22604 &dpyinfo->mouse_face_end_x,
22605 &dpyinfo->mouse_face_end_y, Qnil);
22606 dpyinfo->mouse_face_window = window;
22608 if (BUFFERP (object))
22609 dpyinfo->mouse_face_face_id
22610 = face_at_buffer_position (w, pos, 0, 0,
22611 &ignore, pos + 1,
22612 !dpyinfo->mouse_face_hidden);
22614 /* Display it as active. */
22615 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22616 cursor = No_Cursor;
22618 else if (!NILP (mouse_face) && STRINGP (object))
22620 Lisp_Object b, e;
22621 int ignore;
22623 b = Fprevious_single_property_change (make_number (pos + 1),
22624 Qmouse_face,
22625 object, Qnil);
22626 e = Fnext_single_property_change (position, Qmouse_face,
22627 object, Qnil);
22628 if (NILP (b))
22629 b = make_number (0);
22630 if (NILP (e))
22631 e = make_number (SCHARS (object) - 1);
22633 fast_find_string_pos (w, XINT (b), object,
22634 &dpyinfo->mouse_face_beg_col,
22635 &dpyinfo->mouse_face_beg_row,
22636 &dpyinfo->mouse_face_beg_x,
22637 &dpyinfo->mouse_face_beg_y, 0);
22638 fast_find_string_pos (w, XINT (e), object,
22639 &dpyinfo->mouse_face_end_col,
22640 &dpyinfo->mouse_face_end_row,
22641 &dpyinfo->mouse_face_end_x,
22642 &dpyinfo->mouse_face_end_y, 1);
22643 dpyinfo->mouse_face_past_end = 0;
22644 dpyinfo->mouse_face_window = window;
22645 dpyinfo->mouse_face_face_id
22646 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22647 glyph->face_id, 1);
22648 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22649 cursor = No_Cursor;
22651 else if (STRINGP (object) && NILP (mouse_face))
22653 /* A string which doesn't have mouse-face, but
22654 the text ``under'' it might have. */
22655 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22656 int start = MATRIX_ROW_START_CHARPOS (r);
22658 pos = string_buffer_position (w, object, start);
22659 if (pos > 0)
22660 mouse_face = get_char_property_and_overlay (make_number (pos),
22661 Qmouse_face,
22662 w->buffer,
22663 &overlay);
22664 if (!NILP (mouse_face) && !NILP (overlay))
22666 Lisp_Object before = Foverlay_start (overlay);
22667 Lisp_Object after = Foverlay_end (overlay);
22668 int ignore;
22670 /* Note that we might not be able to find position
22671 BEFORE in the glyph matrix if the overlay is
22672 entirely covered by a `display' property. In
22673 this case, we overshoot. So let's stop in
22674 the glyph matrix before glyphs for OBJECT. */
22675 fast_find_position (w, XFASTINT (before),
22676 &dpyinfo->mouse_face_beg_col,
22677 &dpyinfo->mouse_face_beg_row,
22678 &dpyinfo->mouse_face_beg_x,
22679 &dpyinfo->mouse_face_beg_y,
22680 object);
22682 dpyinfo->mouse_face_past_end
22683 = !fast_find_position (w, XFASTINT (after),
22684 &dpyinfo->mouse_face_end_col,
22685 &dpyinfo->mouse_face_end_row,
22686 &dpyinfo->mouse_face_end_x,
22687 &dpyinfo->mouse_face_end_y,
22688 Qnil);
22689 dpyinfo->mouse_face_window = window;
22690 dpyinfo->mouse_face_face_id
22691 = face_at_buffer_position (w, pos, 0, 0,
22692 &ignore, pos + 1,
22693 !dpyinfo->mouse_face_hidden);
22695 /* Display it as active. */
22696 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22697 cursor = No_Cursor;
22702 check_help_echo:
22704 /* Look for a `help-echo' property. */
22705 if (NILP (help_echo_string)) {
22706 Lisp_Object help, overlay;
22708 /* Check overlays first. */
22709 help = overlay = Qnil;
22710 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22712 overlay = overlay_vec[i];
22713 help = Foverlay_get (overlay, Qhelp_echo);
22716 if (!NILP (help))
22718 help_echo_string = help;
22719 help_echo_window = window;
22720 help_echo_object = overlay;
22721 help_echo_pos = pos;
22723 else
22725 Lisp_Object object = glyph->object;
22726 int charpos = glyph->charpos;
22728 /* Try text properties. */
22729 if (STRINGP (object)
22730 && charpos >= 0
22731 && charpos < SCHARS (object))
22733 help = Fget_text_property (make_number (charpos),
22734 Qhelp_echo, object);
22735 if (NILP (help))
22737 /* If the string itself doesn't specify a help-echo,
22738 see if the buffer text ``under'' it does. */
22739 struct glyph_row *r
22740 = MATRIX_ROW (w->current_matrix, vpos);
22741 int start = MATRIX_ROW_START_CHARPOS (r);
22742 int pos = string_buffer_position (w, object, start);
22743 if (pos > 0)
22745 help = Fget_char_property (make_number (pos),
22746 Qhelp_echo, w->buffer);
22747 if (!NILP (help))
22749 charpos = pos;
22750 object = w->buffer;
22755 else if (BUFFERP (object)
22756 && charpos >= BEGV
22757 && charpos < ZV)
22758 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22759 object);
22761 if (!NILP (help))
22763 help_echo_string = help;
22764 help_echo_window = window;
22765 help_echo_object = object;
22766 help_echo_pos = charpos;
22771 /* Look for a `pointer' property. */
22772 if (NILP (pointer))
22774 /* Check overlays first. */
22775 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22776 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22778 if (NILP (pointer))
22780 Lisp_Object object = glyph->object;
22781 int charpos = glyph->charpos;
22783 /* Try text properties. */
22784 if (STRINGP (object)
22785 && charpos >= 0
22786 && charpos < SCHARS (object))
22788 pointer = Fget_text_property (make_number (charpos),
22789 Qpointer, object);
22790 if (NILP (pointer))
22792 /* If the string itself doesn't specify a pointer,
22793 see if the buffer text ``under'' it does. */
22794 struct glyph_row *r
22795 = MATRIX_ROW (w->current_matrix, vpos);
22796 int start = MATRIX_ROW_START_CHARPOS (r);
22797 int pos = string_buffer_position (w, object, start);
22798 if (pos > 0)
22799 pointer = Fget_char_property (make_number (pos),
22800 Qpointer, w->buffer);
22803 else if (BUFFERP (object)
22804 && charpos >= BEGV
22805 && charpos < ZV)
22806 pointer = Fget_text_property (make_number (charpos),
22807 Qpointer, object);
22811 BEGV = obegv;
22812 ZV = ozv;
22813 current_buffer = obuf;
22816 set_cursor:
22818 define_frame_cursor1 (f, cursor, pointer);
22822 /* EXPORT for RIF:
22823 Clear any mouse-face on window W. This function is part of the
22824 redisplay interface, and is called from try_window_id and similar
22825 functions to ensure the mouse-highlight is off. */
22827 void
22828 x_clear_window_mouse_face (w)
22829 struct window *w;
22831 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22832 Lisp_Object window;
22834 BLOCK_INPUT;
22835 XSETWINDOW (window, w);
22836 if (EQ (window, dpyinfo->mouse_face_window))
22837 clear_mouse_face (dpyinfo);
22838 UNBLOCK_INPUT;
22842 /* EXPORT:
22843 Just discard the mouse face information for frame F, if any.
22844 This is used when the size of F is changed. */
22846 void
22847 cancel_mouse_face (f)
22848 struct frame *f;
22850 Lisp_Object window;
22851 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22853 window = dpyinfo->mouse_face_window;
22854 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22856 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22857 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22858 dpyinfo->mouse_face_window = Qnil;
22863 #endif /* HAVE_WINDOW_SYSTEM */
22866 /***********************************************************************
22867 Exposure Events
22868 ***********************************************************************/
22870 #ifdef HAVE_WINDOW_SYSTEM
22872 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22873 which intersects rectangle R. R is in window-relative coordinates. */
22875 static void
22876 expose_area (w, row, r, area)
22877 struct window *w;
22878 struct glyph_row *row;
22879 XRectangle *r;
22880 enum glyph_row_area area;
22882 struct glyph *first = row->glyphs[area];
22883 struct glyph *end = row->glyphs[area] + row->used[area];
22884 struct glyph *last;
22885 int first_x, start_x, x;
22887 if (area == TEXT_AREA && row->fill_line_p)
22888 /* If row extends face to end of line write the whole line. */
22889 draw_glyphs (w, 0, row, area,
22890 0, row->used[area],
22891 DRAW_NORMAL_TEXT, 0);
22892 else
22894 /* Set START_X to the window-relative start position for drawing glyphs of
22895 AREA. The first glyph of the text area can be partially visible.
22896 The first glyphs of other areas cannot. */
22897 start_x = window_box_left_offset (w, area);
22898 x = start_x;
22899 if (area == TEXT_AREA)
22900 x += row->x;
22902 /* Find the first glyph that must be redrawn. */
22903 while (first < end
22904 && x + first->pixel_width < r->x)
22906 x += first->pixel_width;
22907 ++first;
22910 /* Find the last one. */
22911 last = first;
22912 first_x = x;
22913 while (last < end
22914 && x < r->x + r->width)
22916 x += last->pixel_width;
22917 ++last;
22920 /* Repaint. */
22921 if (last > first)
22922 draw_glyphs (w, first_x - start_x, row, area,
22923 first - row->glyphs[area], last - row->glyphs[area],
22924 DRAW_NORMAL_TEXT, 0);
22929 /* Redraw the parts of the glyph row ROW on window W intersecting
22930 rectangle R. R is in window-relative coordinates. Value is
22931 non-zero if mouse-face was overwritten. */
22933 static int
22934 expose_line (w, row, r)
22935 struct window *w;
22936 struct glyph_row *row;
22937 XRectangle *r;
22939 xassert (row->enabled_p);
22941 if (row->mode_line_p || w->pseudo_window_p)
22942 draw_glyphs (w, 0, row, TEXT_AREA,
22943 0, row->used[TEXT_AREA],
22944 DRAW_NORMAL_TEXT, 0);
22945 else
22947 if (row->used[LEFT_MARGIN_AREA])
22948 expose_area (w, row, r, LEFT_MARGIN_AREA);
22949 if (row->used[TEXT_AREA])
22950 expose_area (w, row, r, TEXT_AREA);
22951 if (row->used[RIGHT_MARGIN_AREA])
22952 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22953 draw_row_fringe_bitmaps (w, row);
22956 return row->mouse_face_p;
22960 /* Redraw those parts of glyphs rows during expose event handling that
22961 overlap other rows. Redrawing of an exposed line writes over parts
22962 of lines overlapping that exposed line; this function fixes that.
22964 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22965 row in W's current matrix that is exposed and overlaps other rows.
22966 LAST_OVERLAPPING_ROW is the last such row. */
22968 static void
22969 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22970 struct window *w;
22971 struct glyph_row *first_overlapping_row;
22972 struct glyph_row *last_overlapping_row;
22974 struct glyph_row *row;
22976 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22977 if (row->overlapping_p)
22979 xassert (row->enabled_p && !row->mode_line_p);
22981 if (row->used[LEFT_MARGIN_AREA])
22982 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22984 if (row->used[TEXT_AREA])
22985 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22987 if (row->used[RIGHT_MARGIN_AREA])
22988 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22993 /* Return non-zero if W's cursor intersects rectangle R. */
22995 static int
22996 phys_cursor_in_rect_p (w, r)
22997 struct window *w;
22998 XRectangle *r;
23000 XRectangle cr, result;
23001 struct glyph *cursor_glyph;
23003 cursor_glyph = get_phys_cursor_glyph (w);
23004 if (cursor_glyph)
23006 /* r is relative to W's box, but w->phys_cursor.x is relative
23007 to left edge of W's TEXT area. Adjust it. */
23008 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23009 cr.y = w->phys_cursor.y;
23010 cr.width = cursor_glyph->pixel_width;
23011 cr.height = w->phys_cursor_height;
23012 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23013 I assume the effect is the same -- and this is portable. */
23014 return x_intersect_rectangles (&cr, r, &result);
23016 /* If we don't understand the format, pretend we're not in the hot-spot. */
23017 return 0;
23021 /* EXPORT:
23022 Draw a vertical window border to the right of window W if W doesn't
23023 have vertical scroll bars. */
23025 void
23026 x_draw_vertical_border (w)
23027 struct window *w;
23029 struct frame *f = XFRAME (WINDOW_FRAME (w));
23031 /* We could do better, if we knew what type of scroll-bar the adjacent
23032 windows (on either side) have... But we don't :-(
23033 However, I think this works ok. ++KFS 2003-04-25 */
23035 /* Redraw borders between horizontally adjacent windows. Don't
23036 do it for frames with vertical scroll bars because either the
23037 right scroll bar of a window, or the left scroll bar of its
23038 neighbor will suffice as a border. */
23039 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23040 return;
23042 if (!WINDOW_RIGHTMOST_P (w)
23043 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23045 int x0, x1, y0, y1;
23047 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23048 y1 -= 1;
23050 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23051 x1 -= 1;
23053 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23055 else if (!WINDOW_LEFTMOST_P (w)
23056 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23058 int x0, x1, y0, y1;
23060 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23061 y1 -= 1;
23063 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23064 x0 -= 1;
23066 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23071 /* Redraw the part of window W intersection rectangle FR. Pixel
23072 coordinates in FR are frame-relative. Call this function with
23073 input blocked. Value is non-zero if the exposure overwrites
23074 mouse-face. */
23076 static int
23077 expose_window (w, fr)
23078 struct window *w;
23079 XRectangle *fr;
23081 struct frame *f = XFRAME (w->frame);
23082 XRectangle wr, r;
23083 int mouse_face_overwritten_p = 0;
23085 /* If window is not yet fully initialized, do nothing. This can
23086 happen when toolkit scroll bars are used and a window is split.
23087 Reconfiguring the scroll bar will generate an expose for a newly
23088 created window. */
23089 if (w->current_matrix == NULL)
23090 return 0;
23092 /* When we're currently updating the window, display and current
23093 matrix usually don't agree. Arrange for a thorough display
23094 later. */
23095 if (w == updated_window)
23097 SET_FRAME_GARBAGED (f);
23098 return 0;
23101 /* Frame-relative pixel rectangle of W. */
23102 wr.x = WINDOW_LEFT_EDGE_X (w);
23103 wr.y = WINDOW_TOP_EDGE_Y (w);
23104 wr.width = WINDOW_TOTAL_WIDTH (w);
23105 wr.height = WINDOW_TOTAL_HEIGHT (w);
23107 if (x_intersect_rectangles (fr, &wr, &r))
23109 int yb = window_text_bottom_y (w);
23110 struct glyph_row *row;
23111 int cursor_cleared_p;
23112 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23114 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23115 r.x, r.y, r.width, r.height));
23117 /* Convert to window coordinates. */
23118 r.x -= WINDOW_LEFT_EDGE_X (w);
23119 r.y -= WINDOW_TOP_EDGE_Y (w);
23121 /* Turn off the cursor. */
23122 if (!w->pseudo_window_p
23123 && phys_cursor_in_rect_p (w, &r))
23125 x_clear_cursor (w);
23126 cursor_cleared_p = 1;
23128 else
23129 cursor_cleared_p = 0;
23131 /* Update lines intersecting rectangle R. */
23132 first_overlapping_row = last_overlapping_row = NULL;
23133 for (row = w->current_matrix->rows;
23134 row->enabled_p;
23135 ++row)
23137 int y0 = row->y;
23138 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23140 if ((y0 >= r.y && y0 < r.y + r.height)
23141 || (y1 > r.y && y1 < r.y + r.height)
23142 || (r.y >= y0 && r.y < y1)
23143 || (r.y + r.height > y0 && r.y + r.height < y1))
23145 /* A header line may be overlapping, but there is no need
23146 to fix overlapping areas for them. KFS 2005-02-12 */
23147 if (row->overlapping_p && !row->mode_line_p)
23149 if (first_overlapping_row == NULL)
23150 first_overlapping_row = row;
23151 last_overlapping_row = row;
23154 if (expose_line (w, row, &r))
23155 mouse_face_overwritten_p = 1;
23158 if (y1 >= yb)
23159 break;
23162 /* Display the mode line if there is one. */
23163 if (WINDOW_WANTS_MODELINE_P (w)
23164 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23165 row->enabled_p)
23166 && row->y < r.y + r.height)
23168 if (expose_line (w, row, &r))
23169 mouse_face_overwritten_p = 1;
23172 if (!w->pseudo_window_p)
23174 /* Fix the display of overlapping rows. */
23175 if (first_overlapping_row)
23176 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23178 /* Draw border between windows. */
23179 x_draw_vertical_border (w);
23181 /* Turn the cursor on again. */
23182 if (cursor_cleared_p)
23183 update_window_cursor (w, 1);
23187 return mouse_face_overwritten_p;
23192 /* Redraw (parts) of all windows in the window tree rooted at W that
23193 intersect R. R contains frame pixel coordinates. Value is
23194 non-zero if the exposure overwrites mouse-face. */
23196 static int
23197 expose_window_tree (w, r)
23198 struct window *w;
23199 XRectangle *r;
23201 struct frame *f = XFRAME (w->frame);
23202 int mouse_face_overwritten_p = 0;
23204 while (w && !FRAME_GARBAGED_P (f))
23206 if (!NILP (w->hchild))
23207 mouse_face_overwritten_p
23208 |= expose_window_tree (XWINDOW (w->hchild), r);
23209 else if (!NILP (w->vchild))
23210 mouse_face_overwritten_p
23211 |= expose_window_tree (XWINDOW (w->vchild), r);
23212 else
23213 mouse_face_overwritten_p |= expose_window (w, r);
23215 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23218 return mouse_face_overwritten_p;
23222 /* EXPORT:
23223 Redisplay an exposed area of frame F. X and Y are the upper-left
23224 corner of the exposed rectangle. W and H are width and height of
23225 the exposed area. All are pixel values. W or H zero means redraw
23226 the entire frame. */
23228 void
23229 expose_frame (f, x, y, w, h)
23230 struct frame *f;
23231 int x, y, w, h;
23233 XRectangle r;
23234 int mouse_face_overwritten_p = 0;
23236 TRACE ((stderr, "expose_frame "));
23238 /* No need to redraw if frame will be redrawn soon. */
23239 if (FRAME_GARBAGED_P (f))
23241 TRACE ((stderr, " garbaged\n"));
23242 return;
23245 /* If basic faces haven't been realized yet, there is no point in
23246 trying to redraw anything. This can happen when we get an expose
23247 event while Emacs is starting, e.g. by moving another window. */
23248 if (FRAME_FACE_CACHE (f) == NULL
23249 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23251 TRACE ((stderr, " no faces\n"));
23252 return;
23255 if (w == 0 || h == 0)
23257 r.x = r.y = 0;
23258 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23259 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23261 else
23263 r.x = x;
23264 r.y = y;
23265 r.width = w;
23266 r.height = h;
23269 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23270 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23272 if (WINDOWP (f->tool_bar_window))
23273 mouse_face_overwritten_p
23274 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23276 #ifdef HAVE_X_WINDOWS
23277 #ifndef MSDOS
23278 #ifndef USE_X_TOOLKIT
23279 if (WINDOWP (f->menu_bar_window))
23280 mouse_face_overwritten_p
23281 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23282 #endif /* not USE_X_TOOLKIT */
23283 #endif
23284 #endif
23286 /* Some window managers support a focus-follows-mouse style with
23287 delayed raising of frames. Imagine a partially obscured frame,
23288 and moving the mouse into partially obscured mouse-face on that
23289 frame. The visible part of the mouse-face will be highlighted,
23290 then the WM raises the obscured frame. With at least one WM, KDE
23291 2.1, Emacs is not getting any event for the raising of the frame
23292 (even tried with SubstructureRedirectMask), only Expose events.
23293 These expose events will draw text normally, i.e. not
23294 highlighted. Which means we must redo the highlight here.
23295 Subsume it under ``we love X''. --gerd 2001-08-15 */
23296 /* Included in Windows version because Windows most likely does not
23297 do the right thing if any third party tool offers
23298 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23299 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23301 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23302 if (f == dpyinfo->mouse_face_mouse_frame)
23304 int x = dpyinfo->mouse_face_mouse_x;
23305 int y = dpyinfo->mouse_face_mouse_y;
23306 clear_mouse_face (dpyinfo);
23307 note_mouse_highlight (f, x, y);
23313 /* EXPORT:
23314 Determine the intersection of two rectangles R1 and R2. Return
23315 the intersection in *RESULT. Value is non-zero if RESULT is not
23316 empty. */
23319 x_intersect_rectangles (r1, r2, result)
23320 XRectangle *r1, *r2, *result;
23322 XRectangle *left, *right;
23323 XRectangle *upper, *lower;
23324 int intersection_p = 0;
23326 /* Rearrange so that R1 is the left-most rectangle. */
23327 if (r1->x < r2->x)
23328 left = r1, right = r2;
23329 else
23330 left = r2, right = r1;
23332 /* X0 of the intersection is right.x0, if this is inside R1,
23333 otherwise there is no intersection. */
23334 if (right->x <= left->x + left->width)
23336 result->x = right->x;
23338 /* The right end of the intersection is the minimum of the
23339 the right ends of left and right. */
23340 result->width = (min (left->x + left->width, right->x + right->width)
23341 - result->x);
23343 /* Same game for Y. */
23344 if (r1->y < r2->y)
23345 upper = r1, lower = r2;
23346 else
23347 upper = r2, lower = r1;
23349 /* The upper end of the intersection is lower.y0, if this is inside
23350 of upper. Otherwise, there is no intersection. */
23351 if (lower->y <= upper->y + upper->height)
23353 result->y = lower->y;
23355 /* The lower end of the intersection is the minimum of the lower
23356 ends of upper and lower. */
23357 result->height = (min (lower->y + lower->height,
23358 upper->y + upper->height)
23359 - result->y);
23360 intersection_p = 1;
23364 return intersection_p;
23367 #endif /* HAVE_WINDOW_SYSTEM */
23370 /***********************************************************************
23371 Initialization
23372 ***********************************************************************/
23374 void
23375 syms_of_xdisp ()
23377 Vwith_echo_area_save_vector = Qnil;
23378 staticpro (&Vwith_echo_area_save_vector);
23380 Vmessage_stack = Qnil;
23381 staticpro (&Vmessage_stack);
23383 Qinhibit_redisplay = intern ("inhibit-redisplay");
23384 staticpro (&Qinhibit_redisplay);
23386 message_dolog_marker1 = Fmake_marker ();
23387 staticpro (&message_dolog_marker1);
23388 message_dolog_marker2 = Fmake_marker ();
23389 staticpro (&message_dolog_marker2);
23390 message_dolog_marker3 = Fmake_marker ();
23391 staticpro (&message_dolog_marker3);
23393 #if GLYPH_DEBUG
23394 defsubr (&Sdump_frame_glyph_matrix);
23395 defsubr (&Sdump_glyph_matrix);
23396 defsubr (&Sdump_glyph_row);
23397 defsubr (&Sdump_tool_bar_row);
23398 defsubr (&Strace_redisplay);
23399 defsubr (&Strace_to_stderr);
23400 #endif
23401 #ifdef HAVE_WINDOW_SYSTEM
23402 defsubr (&Stool_bar_lines_needed);
23403 defsubr (&Slookup_image_map);
23404 #endif
23405 defsubr (&Sformat_mode_line);
23407 staticpro (&Qmenu_bar_update_hook);
23408 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23410 staticpro (&Qoverriding_terminal_local_map);
23411 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23413 staticpro (&Qoverriding_local_map);
23414 Qoverriding_local_map = intern ("overriding-local-map");
23416 staticpro (&Qwindow_scroll_functions);
23417 Qwindow_scroll_functions = intern ("window-scroll-functions");
23419 staticpro (&Qredisplay_end_trigger_functions);
23420 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23422 staticpro (&Qinhibit_point_motion_hooks);
23423 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23425 QCdata = intern (":data");
23426 staticpro (&QCdata);
23427 Qdisplay = intern ("display");
23428 staticpro (&Qdisplay);
23429 Qspace_width = intern ("space-width");
23430 staticpro (&Qspace_width);
23431 Qraise = intern ("raise");
23432 staticpro (&Qraise);
23433 Qslice = intern ("slice");
23434 staticpro (&Qslice);
23435 Qspace = intern ("space");
23436 staticpro (&Qspace);
23437 Qmargin = intern ("margin");
23438 staticpro (&Qmargin);
23439 Qpointer = intern ("pointer");
23440 staticpro (&Qpointer);
23441 Qleft_margin = intern ("left-margin");
23442 staticpro (&Qleft_margin);
23443 Qright_margin = intern ("right-margin");
23444 staticpro (&Qright_margin);
23445 Qcenter = intern ("center");
23446 staticpro (&Qcenter);
23447 Qline_height = intern ("line-height");
23448 staticpro (&Qline_height);
23449 QCalign_to = intern (":align-to");
23450 staticpro (&QCalign_to);
23451 QCrelative_width = intern (":relative-width");
23452 staticpro (&QCrelative_width);
23453 QCrelative_height = intern (":relative-height");
23454 staticpro (&QCrelative_height);
23455 QCeval = intern (":eval");
23456 staticpro (&QCeval);
23457 QCpropertize = intern (":propertize");
23458 staticpro (&QCpropertize);
23459 QCfile = intern (":file");
23460 staticpro (&QCfile);
23461 Qfontified = intern ("fontified");
23462 staticpro (&Qfontified);
23463 Qfontification_functions = intern ("fontification-functions");
23464 staticpro (&Qfontification_functions);
23465 Qtrailing_whitespace = intern ("trailing-whitespace");
23466 staticpro (&Qtrailing_whitespace);
23467 Qescape_glyph = intern ("escape-glyph");
23468 staticpro (&Qescape_glyph);
23469 Qnobreak_space = intern ("nobreak-space");
23470 staticpro (&Qnobreak_space);
23471 Qimage = intern ("image");
23472 staticpro (&Qimage);
23473 QCmap = intern (":map");
23474 staticpro (&QCmap);
23475 QCpointer = intern (":pointer");
23476 staticpro (&QCpointer);
23477 Qrect = intern ("rect");
23478 staticpro (&Qrect);
23479 Qcircle = intern ("circle");
23480 staticpro (&Qcircle);
23481 Qpoly = intern ("poly");
23482 staticpro (&Qpoly);
23483 Qmessage_truncate_lines = intern ("message-truncate-lines");
23484 staticpro (&Qmessage_truncate_lines);
23485 Qgrow_only = intern ("grow-only");
23486 staticpro (&Qgrow_only);
23487 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23488 staticpro (&Qinhibit_menubar_update);
23489 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23490 staticpro (&Qinhibit_eval_during_redisplay);
23491 Qposition = intern ("position");
23492 staticpro (&Qposition);
23493 Qbuffer_position = intern ("buffer-position");
23494 staticpro (&Qbuffer_position);
23495 Qobject = intern ("object");
23496 staticpro (&Qobject);
23497 Qbar = intern ("bar");
23498 staticpro (&Qbar);
23499 Qhbar = intern ("hbar");
23500 staticpro (&Qhbar);
23501 Qbox = intern ("box");
23502 staticpro (&Qbox);
23503 Qhollow = intern ("hollow");
23504 staticpro (&Qhollow);
23505 Qhand = intern ("hand");
23506 staticpro (&Qhand);
23507 Qarrow = intern ("arrow");
23508 staticpro (&Qarrow);
23509 Qtext = intern ("text");
23510 staticpro (&Qtext);
23511 Qrisky_local_variable = intern ("risky-local-variable");
23512 staticpro (&Qrisky_local_variable);
23513 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23514 staticpro (&Qinhibit_free_realized_faces);
23516 list_of_error = Fcons (Fcons (intern ("error"),
23517 Fcons (intern ("void-variable"), Qnil)),
23518 Qnil);
23519 staticpro (&list_of_error);
23521 Qlast_arrow_position = intern ("last-arrow-position");
23522 staticpro (&Qlast_arrow_position);
23523 Qlast_arrow_string = intern ("last-arrow-string");
23524 staticpro (&Qlast_arrow_string);
23526 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23527 staticpro (&Qoverlay_arrow_string);
23528 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23529 staticpro (&Qoverlay_arrow_bitmap);
23531 echo_buffer[0] = echo_buffer[1] = Qnil;
23532 staticpro (&echo_buffer[0]);
23533 staticpro (&echo_buffer[1]);
23535 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23536 staticpro (&echo_area_buffer[0]);
23537 staticpro (&echo_area_buffer[1]);
23539 Vmessages_buffer_name = build_string ("*Messages*");
23540 staticpro (&Vmessages_buffer_name);
23542 mode_line_proptrans_alist = Qnil;
23543 staticpro (&mode_line_proptrans_alist);
23544 mode_line_string_list = Qnil;
23545 staticpro (&mode_line_string_list);
23546 mode_line_string_face = Qnil;
23547 staticpro (&mode_line_string_face);
23548 mode_line_string_face_prop = Qnil;
23549 staticpro (&mode_line_string_face_prop);
23550 Vmode_line_unwind_vector = Qnil;
23551 staticpro (&Vmode_line_unwind_vector);
23553 help_echo_string = Qnil;
23554 staticpro (&help_echo_string);
23555 help_echo_object = Qnil;
23556 staticpro (&help_echo_object);
23557 help_echo_window = Qnil;
23558 staticpro (&help_echo_window);
23559 previous_help_echo_string = Qnil;
23560 staticpro (&previous_help_echo_string);
23561 help_echo_pos = -1;
23563 #ifdef HAVE_WINDOW_SYSTEM
23564 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23565 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23566 For example, if a block cursor is over a tab, it will be drawn as
23567 wide as that tab on the display. */);
23568 x_stretch_cursor_p = 0;
23569 #endif
23571 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23572 doc: /* *Non-nil means highlight trailing whitespace.
23573 The face used for trailing whitespace is `trailing-whitespace'. */);
23574 Vshow_trailing_whitespace = Qnil;
23576 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23577 doc: /* *Control highlighting of nobreak space and soft hyphen.
23578 A value of t means highlight the character itself (for nobreak space,
23579 use face `nobreak-space').
23580 A value of nil means no highlighting.
23581 Other values mean display the escape glyph followed by an ordinary
23582 space or ordinary hyphen. */);
23583 Vnobreak_char_display = Qt;
23585 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23586 doc: /* *The pointer shape to show in void text areas.
23587 A value of nil means to show the text pointer. Other options are `arrow',
23588 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23589 Vvoid_text_area_pointer = Qarrow;
23591 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23592 doc: /* Non-nil means don't actually do any redisplay.
23593 This is used for internal purposes. */);
23594 Vinhibit_redisplay = Qnil;
23596 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23597 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23598 Vglobal_mode_string = Qnil;
23600 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23601 doc: /* Marker for where to display an arrow on top of the buffer text.
23602 This must be the beginning of a line in order to work.
23603 See also `overlay-arrow-string'. */);
23604 Voverlay_arrow_position = Qnil;
23606 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23607 doc: /* String to display as an arrow in non-window frames.
23608 See also `overlay-arrow-position'. */);
23609 Voverlay_arrow_string = build_string ("=>");
23611 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23612 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23613 The symbols on this list are examined during redisplay to determine
23614 where to display overlay arrows. */);
23615 Voverlay_arrow_variable_list
23616 = Fcons (intern ("overlay-arrow-position"), Qnil);
23618 DEFVAR_INT ("scroll-step", &scroll_step,
23619 doc: /* *The number of lines to try scrolling a window by when point moves out.
23620 If that fails to bring point back on frame, point is centered instead.
23621 If this is zero, point is always centered after it moves off frame.
23622 If you want scrolling to always be a line at a time, you should set
23623 `scroll-conservatively' to a large value rather than set this to 1. */);
23625 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23626 doc: /* *Scroll up to this many lines, to bring point back on screen.
23627 A value of zero means to scroll the text to center point vertically
23628 in the window. */);
23629 scroll_conservatively = 0;
23631 DEFVAR_INT ("scroll-margin", &scroll_margin,
23632 doc: /* *Number of lines of margin at the top and bottom of a window.
23633 Recenter the window whenever point gets within this many lines
23634 of the top or bottom of the window. */);
23635 scroll_margin = 0;
23637 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23638 doc: /* Pixels per inch value for non-window system displays.
23639 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23640 Vdisplay_pixels_per_inch = make_float (72.0);
23642 #if GLYPH_DEBUG
23643 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23644 #endif
23646 DEFVAR_BOOL ("truncate-partial-width-windows",
23647 &truncate_partial_width_windows,
23648 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23649 truncate_partial_width_windows = 1;
23651 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23652 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23653 Any other value means to use the appropriate face, `mode-line',
23654 `header-line', or `menu' respectively. */);
23655 mode_line_inverse_video = 1;
23657 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23658 doc: /* *Maximum buffer size for which line number should be displayed.
23659 If the buffer is bigger than this, the line number does not appear
23660 in the mode line. A value of nil means no limit. */);
23661 Vline_number_display_limit = Qnil;
23663 DEFVAR_INT ("line-number-display-limit-width",
23664 &line_number_display_limit_width,
23665 doc: /* *Maximum line width (in characters) for line number display.
23666 If the average length of the lines near point is bigger than this, then the
23667 line number may be omitted from the mode line. */);
23668 line_number_display_limit_width = 200;
23670 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23671 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23672 highlight_nonselected_windows = 0;
23674 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23675 doc: /* Non-nil if more than one frame is visible on this display.
23676 Minibuffer-only frames don't count, but iconified frames do.
23677 This variable is not guaranteed to be accurate except while processing
23678 `frame-title-format' and `icon-title-format'. */);
23680 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23681 doc: /* Template for displaying the title bar of visible frames.
23682 \(Assuming the window manager supports this feature.)
23683 This variable has the same structure as `mode-line-format' (which see),
23684 and is used only on frames for which no explicit name has been set
23685 \(see `modify-frame-parameters'). */);
23687 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23688 doc: /* Template for displaying the title bar of an iconified frame.
23689 \(Assuming the window manager supports this feature.)
23690 This variable has the same structure as `mode-line-format' (which see),
23691 and is used only on frames for which no explicit name has been set
23692 \(see `modify-frame-parameters'). */);
23693 Vicon_title_format
23694 = Vframe_title_format
23695 = Fcons (intern ("multiple-frames"),
23696 Fcons (build_string ("%b"),
23697 Fcons (Fcons (empty_string,
23698 Fcons (intern ("invocation-name"),
23699 Fcons (build_string ("@"),
23700 Fcons (intern ("system-name"),
23701 Qnil)))),
23702 Qnil)));
23704 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23705 doc: /* Maximum number of lines to keep in the message log buffer.
23706 If nil, disable message logging. If t, log messages but don't truncate
23707 the buffer when it becomes large. */);
23708 Vmessage_log_max = make_number (50);
23710 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23711 doc: /* Functions called before redisplay, if window sizes have changed.
23712 The value should be a list of functions that take one argument.
23713 Just before redisplay, for each frame, if any of its windows have changed
23714 size since the last redisplay, or have been split or deleted,
23715 all the functions in the list are called, with the frame as argument. */);
23716 Vwindow_size_change_functions = Qnil;
23718 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23719 doc: /* List of functions to call before redisplaying a window with scrolling.
23720 Each function is called with two arguments, the window
23721 and its new display-start position. Note that the value of `window-end'
23722 is not valid when these functions are called. */);
23723 Vwindow_scroll_functions = Qnil;
23725 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23726 doc: /* Functions called when redisplay of a window reaches the end trigger.
23727 Each function is called with two arguments, the window and the end trigger value.
23728 See `set-window-redisplay-end-trigger'. */);
23729 Vredisplay_end_trigger_functions = Qnil;
23731 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23732 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23733 mouse_autoselect_window = 0;
23735 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23736 doc: /* *Non-nil means automatically resize tool-bars.
23737 This increases a tool-bar's height if not all tool-bar items are visible.
23738 It decreases a tool-bar's height when it would display blank lines
23739 otherwise. */);
23740 auto_resize_tool_bars_p = 1;
23742 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23743 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23744 auto_raise_tool_bar_buttons_p = 1;
23746 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23747 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23748 make_cursor_line_fully_visible_p = 1;
23750 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23751 doc: /* *Border below tool-bar in pixels.
23752 If an integer, use it as the height of the border.
23753 If it is one of `internal-border-width' or `border-width', use the
23754 value of the corresponding frame parameter.
23755 Otherwise, no border is added below the tool-bar. */);
23756 Vtool_bar_border = Qinternal_border_width;
23758 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23759 doc: /* *Margin around tool-bar buttons in pixels.
23760 If an integer, use that for both horizontal and vertical margins.
23761 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23762 HORZ specifying the horizontal margin, and VERT specifying the
23763 vertical margin. */);
23764 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23766 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23767 doc: /* *Relief thickness of tool-bar buttons. */);
23768 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23770 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23771 doc: /* List of functions to call to fontify regions of text.
23772 Each function is called with one argument POS. Functions must
23773 fontify a region starting at POS in the current buffer, and give
23774 fontified regions the property `fontified'. */);
23775 Vfontification_functions = Qnil;
23776 Fmake_variable_buffer_local (Qfontification_functions);
23778 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23779 &unibyte_display_via_language_environment,
23780 doc: /* *Non-nil means display unibyte text according to language environment.
23781 Specifically this means that unibyte non-ASCII characters
23782 are displayed by converting them to the equivalent multibyte characters
23783 according to the current language environment. As a result, they are
23784 displayed according to the current fontset. */);
23785 unibyte_display_via_language_environment = 0;
23787 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23788 doc: /* *Maximum height for resizing mini-windows.
23789 If a float, it specifies a fraction of the mini-window frame's height.
23790 If an integer, it specifies a number of lines. */);
23791 Vmax_mini_window_height = make_float (0.25);
23793 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23794 doc: /* *How to resize mini-windows.
23795 A value of nil means don't automatically resize mini-windows.
23796 A value of t means resize them to fit the text displayed in them.
23797 A value of `grow-only', the default, means let mini-windows grow
23798 only, until their display becomes empty, at which point the windows
23799 go back to their normal size. */);
23800 Vresize_mini_windows = Qgrow_only;
23802 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23803 doc: /* Alist specifying how to blink the cursor off.
23804 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23805 `cursor-type' frame-parameter or variable equals ON-STATE,
23806 comparing using `equal', Emacs uses OFF-STATE to specify
23807 how to blink it off. ON-STATE and OFF-STATE are values for
23808 the `cursor-type' frame parameter.
23810 If a frame's ON-STATE has no entry in this list,
23811 the frame's other specifications determine how to blink the cursor off. */);
23812 Vblink_cursor_alist = Qnil;
23814 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23815 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23816 automatic_hscrolling_p = 1;
23818 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23819 doc: /* *How many columns away from the window edge point is allowed to get
23820 before automatic hscrolling will horizontally scroll the window. */);
23821 hscroll_margin = 5;
23823 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23824 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23825 When point is less than `hscroll-margin' columns from the window
23826 edge, automatic hscrolling will scroll the window by the amount of columns
23827 determined by this variable. If its value is a positive integer, scroll that
23828 many columns. If it's a positive floating-point number, it specifies the
23829 fraction of the window's width to scroll. If it's nil or zero, point will be
23830 centered horizontally after the scroll. Any other value, including negative
23831 numbers, are treated as if the value were zero.
23833 Automatic hscrolling always moves point outside the scroll margin, so if
23834 point was more than scroll step columns inside the margin, the window will
23835 scroll more than the value given by the scroll step.
23837 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23838 and `scroll-right' overrides this variable's effect. */);
23839 Vhscroll_step = make_number (0);
23841 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23842 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23843 Bind this around calls to `message' to let it take effect. */);
23844 message_truncate_lines = 0;
23846 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23847 doc: /* Normal hook run to update the menu bar definitions.
23848 Redisplay runs this hook before it redisplays the menu bar.
23849 This is used to update submenus such as Buffers,
23850 whose contents depend on various data. */);
23851 Vmenu_bar_update_hook = Qnil;
23853 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23854 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23855 inhibit_menubar_update = 0;
23857 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23858 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23859 inhibit_eval_during_redisplay = 0;
23861 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23862 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23863 inhibit_free_realized_faces = 0;
23865 #if GLYPH_DEBUG
23866 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23867 doc: /* Inhibit try_window_id display optimization. */);
23868 inhibit_try_window_id = 0;
23870 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23871 doc: /* Inhibit try_window_reusing display optimization. */);
23872 inhibit_try_window_reusing = 0;
23874 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23875 doc: /* Inhibit try_cursor_movement display optimization. */);
23876 inhibit_try_cursor_movement = 0;
23877 #endif /* GLYPH_DEBUG */
23881 /* Initialize this module when Emacs starts. */
23883 void
23884 init_xdisp ()
23886 Lisp_Object root_window;
23887 struct window *mini_w;
23889 current_header_line_height = current_mode_line_height = -1;
23891 CHARPOS (this_line_start_pos) = 0;
23893 mini_w = XWINDOW (minibuf_window);
23894 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23896 if (!noninteractive)
23898 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23899 int i;
23901 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23902 set_window_height (root_window,
23903 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23905 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23906 set_window_height (minibuf_window, 1, 0);
23908 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23909 mini_w->total_cols = make_number (FRAME_COLS (f));
23911 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23912 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23913 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23915 /* The default ellipsis glyphs `...'. */
23916 for (i = 0; i < 3; ++i)
23917 default_invis_vector[i] = make_number ('.');
23921 /* Allocate the buffer for frame titles.
23922 Also used for `format-mode-line'. */
23923 int size = 100;
23924 mode_line_noprop_buf = (char *) xmalloc (size);
23925 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23926 mode_line_noprop_ptr = mode_line_noprop_buf;
23927 mode_line_target = MODE_LINE_DISPLAY;
23930 help_echo_showing_p = 0;
23934 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23935 (do not change this comment) */