Merged from emacs@sv.gnu.org
[emacs.git] / src / xdisp.c
blobd05b1c5293d1cc1366545e38997f0880a2c9103f
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 #ifdef MAC_OS
8959 mac_update_title_bar (f, 0);
8960 #endif
8961 #endif
8962 UNGCPRO;
8965 unbind_to (count, Qnil);
8967 else
8969 struct frame *sf = SELECTED_FRAME ();
8970 update_menu_bar (sf, 1);
8971 #ifdef HAVE_WINDOW_SYSTEM
8972 update_tool_bar (sf, 1);
8973 #ifdef MAC_OS
8974 mac_update_title_bar (sf, 1);
8975 #endif
8976 #endif
8979 /* Motif needs this. See comment in xmenu.c. Turn it off when
8980 pending_menu_activation is not defined. */
8981 #ifdef USE_X_TOOLKIT
8982 pending_menu_activation = 0;
8983 #endif
8987 /* Update the menu bar item list for frame F. This has to be done
8988 before we start to fill in any display lines, because it can call
8989 eval.
8991 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8993 static void
8994 update_menu_bar (f, save_match_data)
8995 struct frame *f;
8996 int save_match_data;
8998 Lisp_Object window;
8999 register struct window *w;
9001 /* If called recursively during a menu update, do nothing. This can
9002 happen when, for instance, an activate-menubar-hook causes a
9003 redisplay. */
9004 if (inhibit_menubar_update)
9005 return;
9007 window = FRAME_SELECTED_WINDOW (f);
9008 w = XWINDOW (window);
9010 #if 0 /* The if statement below this if statement used to include the
9011 condition !NILP (w->update_mode_line), rather than using
9012 update_mode_lines directly, and this if statement may have
9013 been added to make that condition work. Now the if
9014 statement below matches its comment, this isn't needed. */
9015 if (update_mode_lines)
9016 w->update_mode_line = Qt;
9017 #endif
9019 if (FRAME_WINDOW_P (f)
9021 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9022 || defined (USE_GTK)
9023 FRAME_EXTERNAL_MENU_BAR (f)
9024 #else
9025 FRAME_MENU_BAR_LINES (f) > 0
9026 #endif
9027 : FRAME_MENU_BAR_LINES (f) > 0)
9029 /* If the user has switched buffers or windows, we need to
9030 recompute to reflect the new bindings. But we'll
9031 recompute when update_mode_lines is set too; that means
9032 that people can use force-mode-line-update to request
9033 that the menu bar be recomputed. The adverse effect on
9034 the rest of the redisplay algorithm is about the same as
9035 windows_or_buffers_changed anyway. */
9036 if (windows_or_buffers_changed
9037 /* This used to test w->update_mode_line, but we believe
9038 there is no need to recompute the menu in that case. */
9039 || update_mode_lines
9040 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9041 < BUF_MODIFF (XBUFFER (w->buffer)))
9042 != !NILP (w->last_had_star))
9043 || ((!NILP (Vtransient_mark_mode)
9044 && !NILP (XBUFFER (w->buffer)->mark_active))
9045 != !NILP (w->region_showing)))
9047 struct buffer *prev = current_buffer;
9048 int count = SPECPDL_INDEX ();
9050 specbind (Qinhibit_menubar_update, Qt);
9052 set_buffer_internal_1 (XBUFFER (w->buffer));
9053 if (save_match_data)
9054 record_unwind_save_match_data ();
9055 if (NILP (Voverriding_local_map_menu_flag))
9057 specbind (Qoverriding_terminal_local_map, Qnil);
9058 specbind (Qoverriding_local_map, Qnil);
9061 /* Run the Lucid hook. */
9062 safe_run_hooks (Qactivate_menubar_hook);
9064 /* If it has changed current-menubar from previous value,
9065 really recompute the menu-bar from the value. */
9066 if (! NILP (Vlucid_menu_bar_dirty_flag))
9067 call0 (Qrecompute_lucid_menubar);
9069 safe_run_hooks (Qmenu_bar_update_hook);
9070 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9072 /* Redisplay the menu bar in case we changed it. */
9073 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9074 || defined (USE_GTK)
9075 if (FRAME_WINDOW_P (f))
9077 #ifdef MAC_OS
9078 /* All frames on Mac OS share the same menubar. So only
9079 the selected frame should be allowed to set it. */
9080 if (f == SELECTED_FRAME ())
9081 #endif
9082 set_frame_menubar (f, 0, 0);
9084 else
9085 /* On a terminal screen, the menu bar is an ordinary screen
9086 line, and this makes it get updated. */
9087 w->update_mode_line = Qt;
9088 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9089 /* In the non-toolkit version, the menu bar is an ordinary screen
9090 line, and this makes it get updated. */
9091 w->update_mode_line = Qt;
9092 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9094 unbind_to (count, Qnil);
9095 set_buffer_internal_1 (prev);
9102 /***********************************************************************
9103 Output Cursor
9104 ***********************************************************************/
9106 #ifdef HAVE_WINDOW_SYSTEM
9108 /* EXPORT:
9109 Nominal cursor position -- where to draw output.
9110 HPOS and VPOS are window relative glyph matrix coordinates.
9111 X and Y are window relative pixel coordinates. */
9113 struct cursor_pos output_cursor;
9116 /* EXPORT:
9117 Set the global variable output_cursor to CURSOR. All cursor
9118 positions are relative to updated_window. */
9120 void
9121 set_output_cursor (cursor)
9122 struct cursor_pos *cursor;
9124 output_cursor.hpos = cursor->hpos;
9125 output_cursor.vpos = cursor->vpos;
9126 output_cursor.x = cursor->x;
9127 output_cursor.y = cursor->y;
9131 /* EXPORT for RIF:
9132 Set a nominal cursor position.
9134 HPOS and VPOS are column/row positions in a window glyph matrix. X
9135 and Y are window text area relative pixel positions.
9137 If this is done during an update, updated_window will contain the
9138 window that is being updated and the position is the future output
9139 cursor position for that window. If updated_window is null, use
9140 selected_window and display the cursor at the given position. */
9142 void
9143 x_cursor_to (vpos, hpos, y, x)
9144 int vpos, hpos, y, x;
9146 struct window *w;
9148 /* If updated_window is not set, work on selected_window. */
9149 if (updated_window)
9150 w = updated_window;
9151 else
9152 w = XWINDOW (selected_window);
9154 /* Set the output cursor. */
9155 output_cursor.hpos = hpos;
9156 output_cursor.vpos = vpos;
9157 output_cursor.x = x;
9158 output_cursor.y = y;
9160 /* If not called as part of an update, really display the cursor.
9161 This will also set the cursor position of W. */
9162 if (updated_window == NULL)
9164 BLOCK_INPUT;
9165 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9166 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9167 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9168 UNBLOCK_INPUT;
9172 #endif /* HAVE_WINDOW_SYSTEM */
9175 /***********************************************************************
9176 Tool-bars
9177 ***********************************************************************/
9179 #ifdef HAVE_WINDOW_SYSTEM
9181 /* Where the mouse was last time we reported a mouse event. */
9183 FRAME_PTR last_mouse_frame;
9185 /* Tool-bar item index of the item on which a mouse button was pressed
9186 or -1. */
9188 int last_tool_bar_item;
9191 /* Update the tool-bar item list for frame F. This has to be done
9192 before we start to fill in any display lines. Called from
9193 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9194 and restore it here. */
9196 static void
9197 update_tool_bar (f, save_match_data)
9198 struct frame *f;
9199 int save_match_data;
9201 #ifdef USE_GTK
9202 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9203 #else
9204 int do_update = WINDOWP (f->tool_bar_window)
9205 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9206 #endif
9208 if (do_update)
9210 Lisp_Object window;
9211 struct window *w;
9213 window = FRAME_SELECTED_WINDOW (f);
9214 w = XWINDOW (window);
9216 /* If the user has switched buffers or windows, we need to
9217 recompute to reflect the new bindings. But we'll
9218 recompute when update_mode_lines is set too; that means
9219 that people can use force-mode-line-update to request
9220 that the menu bar be recomputed. The adverse effect on
9221 the rest of the redisplay algorithm is about the same as
9222 windows_or_buffers_changed anyway. */
9223 if (windows_or_buffers_changed
9224 || !NILP (w->update_mode_line)
9225 || update_mode_lines
9226 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9227 < BUF_MODIFF (XBUFFER (w->buffer)))
9228 != !NILP (w->last_had_star))
9229 || ((!NILP (Vtransient_mark_mode)
9230 && !NILP (XBUFFER (w->buffer)->mark_active))
9231 != !NILP (w->region_showing)))
9233 struct buffer *prev = current_buffer;
9234 int count = SPECPDL_INDEX ();
9235 Lisp_Object new_tool_bar;
9236 int new_n_tool_bar;
9237 struct gcpro gcpro1;
9239 /* Set current_buffer to the buffer of the selected
9240 window of the frame, so that we get the right local
9241 keymaps. */
9242 set_buffer_internal_1 (XBUFFER (w->buffer));
9244 /* Save match data, if we must. */
9245 if (save_match_data)
9246 record_unwind_save_match_data ();
9248 /* Make sure that we don't accidentally use bogus keymaps. */
9249 if (NILP (Voverriding_local_map_menu_flag))
9251 specbind (Qoverriding_terminal_local_map, Qnil);
9252 specbind (Qoverriding_local_map, Qnil);
9255 GCPRO1 (new_tool_bar);
9257 /* Build desired tool-bar items from keymaps. */
9258 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9259 &new_n_tool_bar);
9261 /* Redisplay the tool-bar if we changed it. */
9262 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9264 /* Redisplay that happens asynchronously due to an expose event
9265 may access f->tool_bar_items. Make sure we update both
9266 variables within BLOCK_INPUT so no such event interrupts. */
9267 BLOCK_INPUT;
9268 f->tool_bar_items = new_tool_bar;
9269 f->n_tool_bar_items = new_n_tool_bar;
9270 w->update_mode_line = Qt;
9271 UNBLOCK_INPUT;
9274 UNGCPRO;
9276 unbind_to (count, Qnil);
9277 set_buffer_internal_1 (prev);
9283 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9284 F's desired tool-bar contents. F->tool_bar_items must have
9285 been set up previously by calling prepare_menu_bars. */
9287 static void
9288 build_desired_tool_bar_string (f)
9289 struct frame *f;
9291 int i, size, size_needed;
9292 struct gcpro gcpro1, gcpro2, gcpro3;
9293 Lisp_Object image, plist, props;
9295 image = plist = props = Qnil;
9296 GCPRO3 (image, plist, props);
9298 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9299 Otherwise, make a new string. */
9301 /* The size of the string we might be able to reuse. */
9302 size = (STRINGP (f->desired_tool_bar_string)
9303 ? SCHARS (f->desired_tool_bar_string)
9304 : 0);
9306 /* We need one space in the string for each image. */
9307 size_needed = f->n_tool_bar_items;
9309 /* Reuse f->desired_tool_bar_string, if possible. */
9310 if (size < size_needed || NILP (f->desired_tool_bar_string))
9311 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9312 make_number (' '));
9313 else
9315 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9316 Fremove_text_properties (make_number (0), make_number (size),
9317 props, f->desired_tool_bar_string);
9320 /* Put a `display' property on the string for the images to display,
9321 put a `menu_item' property on tool-bar items with a value that
9322 is the index of the item in F's tool-bar item vector. */
9323 for (i = 0; i < f->n_tool_bar_items; ++i)
9325 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9327 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9328 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9329 int hmargin, vmargin, relief, idx, end;
9330 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9332 /* If image is a vector, choose the image according to the
9333 button state. */
9334 image = PROP (TOOL_BAR_ITEM_IMAGES);
9335 if (VECTORP (image))
9337 if (enabled_p)
9338 idx = (selected_p
9339 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9340 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9341 else
9342 idx = (selected_p
9343 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9344 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9346 xassert (ASIZE (image) >= idx);
9347 image = AREF (image, idx);
9349 else
9350 idx = -1;
9352 /* Ignore invalid image specifications. */
9353 if (!valid_image_p (image))
9354 continue;
9356 /* Display the tool-bar button pressed, or depressed. */
9357 plist = Fcopy_sequence (XCDR (image));
9359 /* Compute margin and relief to draw. */
9360 relief = (tool_bar_button_relief >= 0
9361 ? tool_bar_button_relief
9362 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9363 hmargin = vmargin = relief;
9365 if (INTEGERP (Vtool_bar_button_margin)
9366 && XINT (Vtool_bar_button_margin) > 0)
9368 hmargin += XFASTINT (Vtool_bar_button_margin);
9369 vmargin += XFASTINT (Vtool_bar_button_margin);
9371 else if (CONSP (Vtool_bar_button_margin))
9373 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9374 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9375 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9377 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9378 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9379 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9382 if (auto_raise_tool_bar_buttons_p)
9384 /* Add a `:relief' property to the image spec if the item is
9385 selected. */
9386 if (selected_p)
9388 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9389 hmargin -= relief;
9390 vmargin -= relief;
9393 else
9395 /* If image is selected, display it pressed, i.e. with a
9396 negative relief. If it's not selected, display it with a
9397 raised relief. */
9398 plist = Fplist_put (plist, QCrelief,
9399 (selected_p
9400 ? make_number (-relief)
9401 : make_number (relief)));
9402 hmargin -= relief;
9403 vmargin -= relief;
9406 /* Put a margin around the image. */
9407 if (hmargin || vmargin)
9409 if (hmargin == vmargin)
9410 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9411 else
9412 plist = Fplist_put (plist, QCmargin,
9413 Fcons (make_number (hmargin),
9414 make_number (vmargin)));
9417 /* If button is not enabled, and we don't have special images
9418 for the disabled state, make the image appear disabled by
9419 applying an appropriate algorithm to it. */
9420 if (!enabled_p && idx < 0)
9421 plist = Fplist_put (plist, QCconversion, Qdisabled);
9423 /* Put a `display' text property on the string for the image to
9424 display. Put a `menu-item' property on the string that gives
9425 the start of this item's properties in the tool-bar items
9426 vector. */
9427 image = Fcons (Qimage, plist);
9428 props = list4 (Qdisplay, image,
9429 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9431 /* Let the last image hide all remaining spaces in the tool bar
9432 string. The string can be longer than needed when we reuse a
9433 previous string. */
9434 if (i + 1 == f->n_tool_bar_items)
9435 end = SCHARS (f->desired_tool_bar_string);
9436 else
9437 end = i + 1;
9438 Fadd_text_properties (make_number (i), make_number (end),
9439 props, f->desired_tool_bar_string);
9440 #undef PROP
9443 UNGCPRO;
9447 /* Display one line of the tool-bar of frame IT->f.
9449 HEIGHT specifies the desired height of the tool-bar line.
9450 If the actual height of the glyph row is less than HEIGHT, the
9451 row's height is increased to HEIGHT, and the icons are centered
9452 vertically in the new height.
9454 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9455 count a final empty row in case the tool-bar width exactly matches
9456 the window width.
9459 static void
9460 display_tool_bar_line (it, height)
9461 struct it *it;
9462 int height;
9464 struct glyph_row *row = it->glyph_row;
9465 int max_x = it->last_visible_x;
9466 struct glyph *last;
9468 prepare_desired_row (row);
9469 row->y = it->current_y;
9471 /* Note that this isn't made use of if the face hasn't a box,
9472 so there's no need to check the face here. */
9473 it->start_of_box_run_p = 1;
9475 while (it->current_x < max_x)
9477 int x_before, x, n_glyphs_before, i, nglyphs;
9479 /* Get the next display element. */
9480 if (!get_next_display_element (it))
9482 /* Don't count empty row if we are counting needed tool-bar lines. */
9483 if (height < 0 && !it->hpos)
9484 return;
9485 break;
9488 /* Produce glyphs. */
9489 x_before = it->current_x;
9490 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9491 PRODUCE_GLYPHS (it);
9493 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9494 i = 0;
9495 x = x_before;
9496 while (i < nglyphs)
9498 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9500 if (x + glyph->pixel_width > max_x)
9502 /* Glyph doesn't fit on line. */
9503 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9504 it->current_x = x;
9505 goto out;
9508 ++it->hpos;
9509 x += glyph->pixel_width;
9510 ++i;
9513 /* Stop at line ends. */
9514 if (ITERATOR_AT_END_OF_LINE_P (it))
9515 break;
9517 set_iterator_to_next (it, 1);
9520 out:;
9522 row->displays_text_p = row->used[TEXT_AREA] != 0;
9523 /* Use default face for the border below the tool bar. */
9524 if (!row->displays_text_p)
9525 it->face_id = DEFAULT_FACE_ID;
9526 extend_face_to_end_of_line (it);
9527 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9528 last->right_box_line_p = 1;
9529 if (last == row->glyphs[TEXT_AREA])
9530 last->left_box_line_p = 1;
9532 /* Make line the desired height and center it vertically. */
9533 if ((height -= it->max_ascent + it->max_descent) > 0)
9535 it->max_ascent += height / 2;
9536 it->max_descent += (height + 1) / 2;
9539 compute_line_metrics (it);
9541 /* If line is empty, make it occupy the rest of the tool-bar. */
9542 if (!row->displays_text_p)
9544 row->height = row->phys_height = it->last_visible_y - row->y;
9545 row->ascent = row->phys_ascent = 0;
9546 row->extra_line_spacing = 0;
9549 row->full_width_p = 1;
9550 row->continued_p = 0;
9551 row->truncated_on_left_p = 0;
9552 row->truncated_on_right_p = 0;
9554 it->current_x = it->hpos = 0;
9555 it->current_y += row->height;
9556 ++it->vpos;
9557 ++it->glyph_row;
9561 /* Value is the number of screen lines needed to make all tool-bar
9562 items of frame F visible. The number of actual rows needed is
9563 returned in *N_ROWS if non-NULL. */
9565 static int
9566 tool_bar_lines_needed (f, n_rows)
9567 struct frame *f;
9568 int *n_rows;
9570 struct window *w = XWINDOW (f->tool_bar_window);
9571 struct it it;
9572 struct glyph_row *temp_row = w->desired_matrix->rows;
9574 /* Initialize an iterator for iteration over
9575 F->desired_tool_bar_string in the tool-bar window of frame F. */
9576 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9577 it.first_visible_x = 0;
9578 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9579 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9581 while (!ITERATOR_AT_END_P (&it))
9583 clear_glyph_row (temp_row);
9584 it.glyph_row = temp_row;
9585 display_tool_bar_line (&it, -1);
9587 clear_glyph_row (temp_row);
9589 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9590 if (n_rows)
9591 *n_rows = it.vpos > 0 ? it.vpos : -1;
9593 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9597 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9598 0, 1, 0,
9599 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9600 (frame)
9601 Lisp_Object frame;
9603 struct frame *f;
9604 struct window *w;
9605 int nlines = 0;
9607 if (NILP (frame))
9608 frame = selected_frame;
9609 else
9610 CHECK_FRAME (frame);
9611 f = XFRAME (frame);
9613 if (WINDOWP (f->tool_bar_window)
9614 || (w = XWINDOW (f->tool_bar_window),
9615 WINDOW_TOTAL_LINES (w) > 0))
9617 update_tool_bar (f, 1);
9618 if (f->n_tool_bar_items)
9620 build_desired_tool_bar_string (f);
9621 nlines = tool_bar_lines_needed (f, NULL);
9625 return make_number (nlines);
9629 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9630 height should be changed. */
9632 static int
9633 redisplay_tool_bar (f)
9634 struct frame *f;
9636 struct window *w;
9637 struct it it;
9638 struct glyph_row *row;
9639 int change_height_p = 0;
9641 #ifdef USE_GTK
9642 if (FRAME_EXTERNAL_TOOL_BAR (f))
9643 update_frame_tool_bar (f);
9644 return 0;
9645 #endif
9647 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9648 do anything. This means you must start with tool-bar-lines
9649 non-zero to get the auto-sizing effect. Or in other words, you
9650 can turn off tool-bars by specifying tool-bar-lines zero. */
9651 if (!WINDOWP (f->tool_bar_window)
9652 || (w = XWINDOW (f->tool_bar_window),
9653 WINDOW_TOTAL_LINES (w) == 0))
9654 return 0;
9656 /* Set up an iterator for the tool-bar window. */
9657 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9658 it.first_visible_x = 0;
9659 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9660 row = it.glyph_row;
9662 /* Build a string that represents the contents of the tool-bar. */
9663 build_desired_tool_bar_string (f);
9664 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9666 if (f->n_tool_bar_rows == 0)
9668 int nlines;
9670 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9671 nlines != WINDOW_TOTAL_LINES (w)))
9673 extern Lisp_Object Qtool_bar_lines;
9674 Lisp_Object frame;
9675 int old_height = WINDOW_TOTAL_LINES (w);
9677 XSETFRAME (frame, f);
9678 clear_glyph_matrix (w->desired_matrix);
9679 Fmodify_frame_parameters (frame,
9680 Fcons (Fcons (Qtool_bar_lines,
9681 make_number (nlines)),
9682 Qnil));
9683 if (WINDOW_TOTAL_LINES (w) != old_height)
9685 fonts_changed_p = 1;
9686 return 1;
9691 /* Display as many lines as needed to display all tool-bar items. */
9693 if (f->n_tool_bar_rows > 0)
9695 int border, rows, height, extra;
9697 if (INTEGERP (Vtool_bar_border))
9698 border = XINT (Vtool_bar_border);
9699 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9700 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9701 else if (EQ (Vtool_bar_border, Qborder_width))
9702 border = f->border_width;
9703 else
9704 border = 0;
9705 if (border < 0)
9706 border = 0;
9708 rows = f->n_tool_bar_rows;
9709 height = (it.last_visible_y - border) / rows;
9710 extra = it.last_visible_y - border - height * rows;
9712 while (it.current_y < it.last_visible_y)
9714 int h = 0;
9715 if (extra > 0 && rows-- > 0)
9717 h = (extra + rows - 1) / rows;
9718 extra -= h;
9720 display_tool_bar_line (&it, height + h);
9723 else
9725 while (it.current_y < it.last_visible_y)
9726 display_tool_bar_line (&it, 0);
9729 /* It doesn't make much sense to try scrolling in the tool-bar
9730 window, so don't do it. */
9731 w->desired_matrix->no_scrolling_p = 1;
9732 w->must_be_updated_p = 1;
9734 if (auto_resize_tool_bars_p)
9736 int nlines;
9738 /* If we couldn't display everything, change the tool-bar's
9739 height. */
9740 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9741 change_height_p = 1;
9743 /* If there are blank lines at the end, except for a partially
9744 visible blank line at the end that is smaller than
9745 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9746 row = it.glyph_row - 1;
9747 if (!row->displays_text_p
9748 && row->height >= FRAME_LINE_HEIGHT (f))
9749 change_height_p = 1;
9751 /* If row displays tool-bar items, but is partially visible,
9752 change the tool-bar's height. */
9753 if (row->displays_text_p
9754 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9755 change_height_p = 1;
9757 /* Resize windows as needed by changing the `tool-bar-lines'
9758 frame parameter. */
9759 if (change_height_p
9760 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9761 nlines != WINDOW_TOTAL_LINES (w)))
9763 extern Lisp_Object Qtool_bar_lines;
9764 Lisp_Object frame;
9765 int old_height = WINDOW_TOTAL_LINES (w);
9767 XSETFRAME (frame, f);
9768 clear_glyph_matrix (w->desired_matrix);
9769 Fmodify_frame_parameters (frame,
9770 Fcons (Fcons (Qtool_bar_lines,
9771 make_number (nlines)),
9772 Qnil));
9773 if (WINDOW_TOTAL_LINES (w) != old_height)
9774 fonts_changed_p = 1;
9778 return change_height_p;
9782 /* Get information about the tool-bar item which is displayed in GLYPH
9783 on frame F. Return in *PROP_IDX the index where tool-bar item
9784 properties start in F->tool_bar_items. Value is zero if
9785 GLYPH doesn't display a tool-bar item. */
9787 static int
9788 tool_bar_item_info (f, glyph, prop_idx)
9789 struct frame *f;
9790 struct glyph *glyph;
9791 int *prop_idx;
9793 Lisp_Object prop;
9794 int success_p;
9795 int charpos;
9797 /* This function can be called asynchronously, which means we must
9798 exclude any possibility that Fget_text_property signals an
9799 error. */
9800 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9801 charpos = max (0, charpos);
9803 /* Get the text property `menu-item' at pos. The value of that
9804 property is the start index of this item's properties in
9805 F->tool_bar_items. */
9806 prop = Fget_text_property (make_number (charpos),
9807 Qmenu_item, f->current_tool_bar_string);
9808 if (INTEGERP (prop))
9810 *prop_idx = XINT (prop);
9811 success_p = 1;
9813 else
9814 success_p = 0;
9816 return success_p;
9820 /* Get information about the tool-bar item at position X/Y on frame F.
9821 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9822 the current matrix of the tool-bar window of F, or NULL if not
9823 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9824 item in F->tool_bar_items. Value is
9826 -1 if X/Y is not on a tool-bar item
9827 0 if X/Y is on the same item that was highlighted before.
9828 1 otherwise. */
9830 static int
9831 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9832 struct frame *f;
9833 int x, y;
9834 struct glyph **glyph;
9835 int *hpos, *vpos, *prop_idx;
9837 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9838 struct window *w = XWINDOW (f->tool_bar_window);
9839 int area;
9841 /* Find the glyph under X/Y. */
9842 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9843 if (*glyph == NULL)
9844 return -1;
9846 /* Get the start of this tool-bar item's properties in
9847 f->tool_bar_items. */
9848 if (!tool_bar_item_info (f, *glyph, prop_idx))
9849 return -1;
9851 /* Is mouse on the highlighted item? */
9852 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9853 && *vpos >= dpyinfo->mouse_face_beg_row
9854 && *vpos <= dpyinfo->mouse_face_end_row
9855 && (*vpos > dpyinfo->mouse_face_beg_row
9856 || *hpos >= dpyinfo->mouse_face_beg_col)
9857 && (*vpos < dpyinfo->mouse_face_end_row
9858 || *hpos < dpyinfo->mouse_face_end_col
9859 || dpyinfo->mouse_face_past_end))
9860 return 0;
9862 return 1;
9866 /* EXPORT:
9867 Handle mouse button event on the tool-bar of frame F, at
9868 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9869 0 for button release. MODIFIERS is event modifiers for button
9870 release. */
9872 void
9873 handle_tool_bar_click (f, x, y, down_p, modifiers)
9874 struct frame *f;
9875 int x, y, down_p;
9876 unsigned int modifiers;
9878 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9879 struct window *w = XWINDOW (f->tool_bar_window);
9880 int hpos, vpos, prop_idx;
9881 struct glyph *glyph;
9882 Lisp_Object enabled_p;
9884 /* If not on the highlighted tool-bar item, return. */
9885 frame_to_window_pixel_xy (w, &x, &y);
9886 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9887 return;
9889 /* If item is disabled, do nothing. */
9890 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9891 if (NILP (enabled_p))
9892 return;
9894 if (down_p)
9896 /* Show item in pressed state. */
9897 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9898 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9899 last_tool_bar_item = prop_idx;
9901 else
9903 Lisp_Object key, frame;
9904 struct input_event event;
9905 EVENT_INIT (event);
9907 /* Show item in released state. */
9908 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9909 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9911 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9913 XSETFRAME (frame, f);
9914 event.kind = TOOL_BAR_EVENT;
9915 event.frame_or_window = frame;
9916 event.arg = frame;
9917 kbd_buffer_store_event (&event);
9919 event.kind = TOOL_BAR_EVENT;
9920 event.frame_or_window = frame;
9921 event.arg = key;
9922 event.modifiers = modifiers;
9923 kbd_buffer_store_event (&event);
9924 last_tool_bar_item = -1;
9929 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9930 tool-bar window-relative coordinates X/Y. Called from
9931 note_mouse_highlight. */
9933 static void
9934 note_tool_bar_highlight (f, x, y)
9935 struct frame *f;
9936 int x, y;
9938 Lisp_Object window = f->tool_bar_window;
9939 struct window *w = XWINDOW (window);
9940 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9941 int hpos, vpos;
9942 struct glyph *glyph;
9943 struct glyph_row *row;
9944 int i;
9945 Lisp_Object enabled_p;
9946 int prop_idx;
9947 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9948 int mouse_down_p, rc;
9950 /* Function note_mouse_highlight is called with negative x(y
9951 values when mouse moves outside of the frame. */
9952 if (x <= 0 || y <= 0)
9954 clear_mouse_face (dpyinfo);
9955 return;
9958 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9959 if (rc < 0)
9961 /* Not on tool-bar item. */
9962 clear_mouse_face (dpyinfo);
9963 return;
9965 else if (rc == 0)
9966 /* On same tool-bar item as before. */
9967 goto set_help_echo;
9969 clear_mouse_face (dpyinfo);
9971 /* Mouse is down, but on different tool-bar item? */
9972 mouse_down_p = (dpyinfo->grabbed
9973 && f == last_mouse_frame
9974 && FRAME_LIVE_P (f));
9975 if (mouse_down_p
9976 && last_tool_bar_item != prop_idx)
9977 return;
9979 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9980 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9982 /* If tool-bar item is not enabled, don't highlight it. */
9983 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9984 if (!NILP (enabled_p))
9986 /* Compute the x-position of the glyph. In front and past the
9987 image is a space. We include this in the highlighted area. */
9988 row = MATRIX_ROW (w->current_matrix, vpos);
9989 for (i = x = 0; i < hpos; ++i)
9990 x += row->glyphs[TEXT_AREA][i].pixel_width;
9992 /* Record this as the current active region. */
9993 dpyinfo->mouse_face_beg_col = hpos;
9994 dpyinfo->mouse_face_beg_row = vpos;
9995 dpyinfo->mouse_face_beg_x = x;
9996 dpyinfo->mouse_face_beg_y = row->y;
9997 dpyinfo->mouse_face_past_end = 0;
9999 dpyinfo->mouse_face_end_col = hpos + 1;
10000 dpyinfo->mouse_face_end_row = vpos;
10001 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10002 dpyinfo->mouse_face_end_y = row->y;
10003 dpyinfo->mouse_face_window = window;
10004 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10006 /* Display it as active. */
10007 show_mouse_face (dpyinfo, draw);
10008 dpyinfo->mouse_face_image_state = draw;
10011 set_help_echo:
10013 /* Set help_echo_string to a help string to display for this tool-bar item.
10014 XTread_socket does the rest. */
10015 help_echo_object = help_echo_window = Qnil;
10016 help_echo_pos = -1;
10017 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10018 if (NILP (help_echo_string))
10019 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10022 #endif /* HAVE_WINDOW_SYSTEM */
10026 /************************************************************************
10027 Horizontal scrolling
10028 ************************************************************************/
10030 static int hscroll_window_tree P_ ((Lisp_Object));
10031 static int hscroll_windows P_ ((Lisp_Object));
10033 /* For all leaf windows in the window tree rooted at WINDOW, set their
10034 hscroll value so that PT is (i) visible in the window, and (ii) so
10035 that it is not within a certain margin at the window's left and
10036 right border. Value is non-zero if any window's hscroll has been
10037 changed. */
10039 static int
10040 hscroll_window_tree (window)
10041 Lisp_Object window;
10043 int hscrolled_p = 0;
10044 int hscroll_relative_p = FLOATP (Vhscroll_step);
10045 int hscroll_step_abs = 0;
10046 double hscroll_step_rel = 0;
10048 if (hscroll_relative_p)
10050 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10051 if (hscroll_step_rel < 0)
10053 hscroll_relative_p = 0;
10054 hscroll_step_abs = 0;
10057 else if (INTEGERP (Vhscroll_step))
10059 hscroll_step_abs = XINT (Vhscroll_step);
10060 if (hscroll_step_abs < 0)
10061 hscroll_step_abs = 0;
10063 else
10064 hscroll_step_abs = 0;
10066 while (WINDOWP (window))
10068 struct window *w = XWINDOW (window);
10070 if (WINDOWP (w->hchild))
10071 hscrolled_p |= hscroll_window_tree (w->hchild);
10072 else if (WINDOWP (w->vchild))
10073 hscrolled_p |= hscroll_window_tree (w->vchild);
10074 else if (w->cursor.vpos >= 0)
10076 int h_margin;
10077 int text_area_width;
10078 struct glyph_row *current_cursor_row
10079 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10080 struct glyph_row *desired_cursor_row
10081 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10082 struct glyph_row *cursor_row
10083 = (desired_cursor_row->enabled_p
10084 ? desired_cursor_row
10085 : current_cursor_row);
10087 text_area_width = window_box_width (w, TEXT_AREA);
10089 /* Scroll when cursor is inside this scroll margin. */
10090 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10092 if ((XFASTINT (w->hscroll)
10093 && w->cursor.x <= h_margin)
10094 || (cursor_row->enabled_p
10095 && cursor_row->truncated_on_right_p
10096 && (w->cursor.x >= text_area_width - h_margin)))
10098 struct it it;
10099 int hscroll;
10100 struct buffer *saved_current_buffer;
10101 int pt;
10102 int wanted_x;
10104 /* Find point in a display of infinite width. */
10105 saved_current_buffer = current_buffer;
10106 current_buffer = XBUFFER (w->buffer);
10108 if (w == XWINDOW (selected_window))
10109 pt = BUF_PT (current_buffer);
10110 else
10112 pt = marker_position (w->pointm);
10113 pt = max (BEGV, pt);
10114 pt = min (ZV, pt);
10117 /* Move iterator to pt starting at cursor_row->start in
10118 a line with infinite width. */
10119 init_to_row_start (&it, w, cursor_row);
10120 it.last_visible_x = INFINITY;
10121 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10122 current_buffer = saved_current_buffer;
10124 /* Position cursor in window. */
10125 if (!hscroll_relative_p && hscroll_step_abs == 0)
10126 hscroll = max (0, (it.current_x
10127 - (ITERATOR_AT_END_OF_LINE_P (&it)
10128 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10129 : (text_area_width / 2))))
10130 / FRAME_COLUMN_WIDTH (it.f);
10131 else if (w->cursor.x >= text_area_width - h_margin)
10133 if (hscroll_relative_p)
10134 wanted_x = text_area_width * (1 - hscroll_step_rel)
10135 - h_margin;
10136 else
10137 wanted_x = text_area_width
10138 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10139 - h_margin;
10140 hscroll
10141 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10143 else
10145 if (hscroll_relative_p)
10146 wanted_x = text_area_width * hscroll_step_rel
10147 + h_margin;
10148 else
10149 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10150 + h_margin;
10151 hscroll
10152 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10154 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10156 /* Don't call Fset_window_hscroll if value hasn't
10157 changed because it will prevent redisplay
10158 optimizations. */
10159 if (XFASTINT (w->hscroll) != hscroll)
10161 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10162 w->hscroll = make_number (hscroll);
10163 hscrolled_p = 1;
10168 window = w->next;
10171 /* Value is non-zero if hscroll of any leaf window has been changed. */
10172 return hscrolled_p;
10176 /* Set hscroll so that cursor is visible and not inside horizontal
10177 scroll margins for all windows in the tree rooted at WINDOW. See
10178 also hscroll_window_tree above. Value is non-zero if any window's
10179 hscroll has been changed. If it has, desired matrices on the frame
10180 of WINDOW are cleared. */
10182 static int
10183 hscroll_windows (window)
10184 Lisp_Object window;
10186 int hscrolled_p;
10188 if (automatic_hscrolling_p)
10190 hscrolled_p = hscroll_window_tree (window);
10191 if (hscrolled_p)
10192 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10194 else
10195 hscrolled_p = 0;
10196 return hscrolled_p;
10201 /************************************************************************
10202 Redisplay
10203 ************************************************************************/
10205 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10206 to a non-zero value. This is sometimes handy to have in a debugger
10207 session. */
10209 #if GLYPH_DEBUG
10211 /* First and last unchanged row for try_window_id. */
10213 int debug_first_unchanged_at_end_vpos;
10214 int debug_last_unchanged_at_beg_vpos;
10216 /* Delta vpos and y. */
10218 int debug_dvpos, debug_dy;
10220 /* Delta in characters and bytes for try_window_id. */
10222 int debug_delta, debug_delta_bytes;
10224 /* Values of window_end_pos and window_end_vpos at the end of
10225 try_window_id. */
10227 EMACS_INT debug_end_pos, debug_end_vpos;
10229 /* Append a string to W->desired_matrix->method. FMT is a printf
10230 format string. A1...A9 are a supplement for a variable-length
10231 argument list. If trace_redisplay_p is non-zero also printf the
10232 resulting string to stderr. */
10234 static void
10235 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10236 struct window *w;
10237 char *fmt;
10238 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10240 char buffer[512];
10241 char *method = w->desired_matrix->method;
10242 int len = strlen (method);
10243 int size = sizeof w->desired_matrix->method;
10244 int remaining = size - len - 1;
10246 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10247 if (len && remaining)
10249 method[len] = '|';
10250 --remaining, ++len;
10253 strncpy (method + len, buffer, remaining);
10255 if (trace_redisplay_p)
10256 fprintf (stderr, "%p (%s): %s\n",
10258 ((BUFFERP (w->buffer)
10259 && STRINGP (XBUFFER (w->buffer)->name))
10260 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10261 : "no buffer"),
10262 buffer);
10265 #endif /* GLYPH_DEBUG */
10268 /* Value is non-zero if all changes in window W, which displays
10269 current_buffer, are in the text between START and END. START is a
10270 buffer position, END is given as a distance from Z. Used in
10271 redisplay_internal for display optimization. */
10273 static INLINE int
10274 text_outside_line_unchanged_p (w, start, end)
10275 struct window *w;
10276 int start, end;
10278 int unchanged_p = 1;
10280 /* If text or overlays have changed, see where. */
10281 if (XFASTINT (w->last_modified) < MODIFF
10282 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10284 /* Gap in the line? */
10285 if (GPT < start || Z - GPT < end)
10286 unchanged_p = 0;
10288 /* Changes start in front of the line, or end after it? */
10289 if (unchanged_p
10290 && (BEG_UNCHANGED < start - 1
10291 || END_UNCHANGED < end))
10292 unchanged_p = 0;
10294 /* If selective display, can't optimize if changes start at the
10295 beginning of the line. */
10296 if (unchanged_p
10297 && INTEGERP (current_buffer->selective_display)
10298 && XINT (current_buffer->selective_display) > 0
10299 && (BEG_UNCHANGED < start || GPT <= start))
10300 unchanged_p = 0;
10302 /* If there are overlays at the start or end of the line, these
10303 may have overlay strings with newlines in them. A change at
10304 START, for instance, may actually concern the display of such
10305 overlay strings as well, and they are displayed on different
10306 lines. So, quickly rule out this case. (For the future, it
10307 might be desirable to implement something more telling than
10308 just BEG/END_UNCHANGED.) */
10309 if (unchanged_p)
10311 if (BEG + BEG_UNCHANGED == start
10312 && overlay_touches_p (start))
10313 unchanged_p = 0;
10314 if (END_UNCHANGED == end
10315 && overlay_touches_p (Z - end))
10316 unchanged_p = 0;
10320 return unchanged_p;
10324 /* Do a frame update, taking possible shortcuts into account. This is
10325 the main external entry point for redisplay.
10327 If the last redisplay displayed an echo area message and that message
10328 is no longer requested, we clear the echo area or bring back the
10329 mini-buffer if that is in use. */
10331 void
10332 redisplay ()
10334 redisplay_internal (0);
10338 static Lisp_Object
10339 overlay_arrow_string_or_property (var)
10340 Lisp_Object var;
10342 Lisp_Object val;
10344 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10345 return val;
10347 return Voverlay_arrow_string;
10350 /* Return 1 if there are any overlay-arrows in current_buffer. */
10351 static int
10352 overlay_arrow_in_current_buffer_p ()
10354 Lisp_Object vlist;
10356 for (vlist = Voverlay_arrow_variable_list;
10357 CONSP (vlist);
10358 vlist = XCDR (vlist))
10360 Lisp_Object var = XCAR (vlist);
10361 Lisp_Object val;
10363 if (!SYMBOLP (var))
10364 continue;
10365 val = find_symbol_value (var);
10366 if (MARKERP (val)
10367 && current_buffer == XMARKER (val)->buffer)
10368 return 1;
10370 return 0;
10374 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10375 has changed. */
10377 static int
10378 overlay_arrows_changed_p ()
10380 Lisp_Object vlist;
10382 for (vlist = Voverlay_arrow_variable_list;
10383 CONSP (vlist);
10384 vlist = XCDR (vlist))
10386 Lisp_Object var = XCAR (vlist);
10387 Lisp_Object val, pstr;
10389 if (!SYMBOLP (var))
10390 continue;
10391 val = find_symbol_value (var);
10392 if (!MARKERP (val))
10393 continue;
10394 if (! EQ (COERCE_MARKER (val),
10395 Fget (var, Qlast_arrow_position))
10396 || ! (pstr = overlay_arrow_string_or_property (var),
10397 EQ (pstr, Fget (var, Qlast_arrow_string))))
10398 return 1;
10400 return 0;
10403 /* Mark overlay arrows to be updated on next redisplay. */
10405 static void
10406 update_overlay_arrows (up_to_date)
10407 int up_to_date;
10409 Lisp_Object vlist;
10411 for (vlist = Voverlay_arrow_variable_list;
10412 CONSP (vlist);
10413 vlist = XCDR (vlist))
10415 Lisp_Object var = XCAR (vlist);
10417 if (!SYMBOLP (var))
10418 continue;
10420 if (up_to_date > 0)
10422 Lisp_Object val = find_symbol_value (var);
10423 Fput (var, Qlast_arrow_position,
10424 COERCE_MARKER (val));
10425 Fput (var, Qlast_arrow_string,
10426 overlay_arrow_string_or_property (var));
10428 else if (up_to_date < 0
10429 || !NILP (Fget (var, Qlast_arrow_position)))
10431 Fput (var, Qlast_arrow_position, Qt);
10432 Fput (var, Qlast_arrow_string, Qt);
10438 /* Return overlay arrow string to display at row.
10439 Return integer (bitmap number) for arrow bitmap in left fringe.
10440 Return nil if no overlay arrow. */
10442 static Lisp_Object
10443 overlay_arrow_at_row (it, row)
10444 struct it *it;
10445 struct glyph_row *row;
10447 Lisp_Object vlist;
10449 for (vlist = Voverlay_arrow_variable_list;
10450 CONSP (vlist);
10451 vlist = XCDR (vlist))
10453 Lisp_Object var = XCAR (vlist);
10454 Lisp_Object val;
10456 if (!SYMBOLP (var))
10457 continue;
10459 val = find_symbol_value (var);
10461 if (MARKERP (val)
10462 && current_buffer == XMARKER (val)->buffer
10463 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10465 if (FRAME_WINDOW_P (it->f)
10466 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10468 #ifdef HAVE_WINDOW_SYSTEM
10469 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10471 int fringe_bitmap;
10472 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10473 return make_number (fringe_bitmap);
10475 #endif
10476 return make_number (-1); /* Use default arrow bitmap */
10478 return overlay_arrow_string_or_property (var);
10482 return Qnil;
10485 /* Return 1 if point moved out of or into a composition. Otherwise
10486 return 0. PREV_BUF and PREV_PT are the last point buffer and
10487 position. BUF and PT are the current point buffer and position. */
10490 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10491 struct buffer *prev_buf, *buf;
10492 int prev_pt, pt;
10494 int start, end;
10495 Lisp_Object prop;
10496 Lisp_Object buffer;
10498 XSETBUFFER (buffer, buf);
10499 /* Check a composition at the last point if point moved within the
10500 same buffer. */
10501 if (prev_buf == buf)
10503 if (prev_pt == pt)
10504 /* Point didn't move. */
10505 return 0;
10507 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10508 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10509 && COMPOSITION_VALID_P (start, end, prop)
10510 && start < prev_pt && end > prev_pt)
10511 /* The last point was within the composition. Return 1 iff
10512 point moved out of the composition. */
10513 return (pt <= start || pt >= end);
10516 /* Check a composition at the current point. */
10517 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10518 && find_composition (pt, -1, &start, &end, &prop, buffer)
10519 && COMPOSITION_VALID_P (start, end, prop)
10520 && start < pt && end > pt);
10524 /* Reconsider the setting of B->clip_changed which is displayed
10525 in window W. */
10527 static INLINE void
10528 reconsider_clip_changes (w, b)
10529 struct window *w;
10530 struct buffer *b;
10532 if (b->clip_changed
10533 && !NILP (w->window_end_valid)
10534 && w->current_matrix->buffer == b
10535 && w->current_matrix->zv == BUF_ZV (b)
10536 && w->current_matrix->begv == BUF_BEGV (b))
10537 b->clip_changed = 0;
10539 /* If display wasn't paused, and W is not a tool bar window, see if
10540 point has been moved into or out of a composition. In that case,
10541 we set b->clip_changed to 1 to force updating the screen. If
10542 b->clip_changed has already been set to 1, we can skip this
10543 check. */
10544 if (!b->clip_changed
10545 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10547 int pt;
10549 if (w == XWINDOW (selected_window))
10550 pt = BUF_PT (current_buffer);
10551 else
10552 pt = marker_position (w->pointm);
10554 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10555 || pt != XINT (w->last_point))
10556 && check_point_in_composition (w->current_matrix->buffer,
10557 XINT (w->last_point),
10558 XBUFFER (w->buffer), pt))
10559 b->clip_changed = 1;
10564 /* Select FRAME to forward the values of frame-local variables into C
10565 variables so that the redisplay routines can access those values
10566 directly. */
10568 static void
10569 select_frame_for_redisplay (frame)
10570 Lisp_Object frame;
10572 Lisp_Object tail, sym, val;
10573 Lisp_Object old = selected_frame;
10575 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10577 selected_frame = frame;
10579 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10580 if (CONSP (XCAR (tail))
10581 && (sym = XCAR (XCAR (tail)),
10582 SYMBOLP (sym))
10583 && (sym = indirect_variable (sym),
10584 val = SYMBOL_VALUE (sym),
10585 (BUFFER_LOCAL_VALUEP (val)
10586 || SOME_BUFFER_LOCAL_VALUEP (val)))
10587 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10588 /* Use find_symbol_value rather than Fsymbol_value
10589 to avoid an error if it is void. */
10590 find_symbol_value (sym);
10592 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10593 if (CONSP (XCAR (tail))
10594 && (sym = XCAR (XCAR (tail)),
10595 SYMBOLP (sym))
10596 && (sym = indirect_variable (sym),
10597 val = SYMBOL_VALUE (sym),
10598 (BUFFER_LOCAL_VALUEP (val)
10599 || SOME_BUFFER_LOCAL_VALUEP (val)))
10600 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10601 find_symbol_value (sym);
10605 #define STOP_POLLING \
10606 do { if (! polling_stopped_here) stop_polling (); \
10607 polling_stopped_here = 1; } while (0)
10609 #define RESUME_POLLING \
10610 do { if (polling_stopped_here) start_polling (); \
10611 polling_stopped_here = 0; } while (0)
10614 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10615 response to any user action; therefore, we should preserve the echo
10616 area. (Actually, our caller does that job.) Perhaps in the future
10617 avoid recentering windows if it is not necessary; currently that
10618 causes some problems. */
10620 static void
10621 redisplay_internal (preserve_echo_area)
10622 int preserve_echo_area;
10624 struct window *w = XWINDOW (selected_window);
10625 struct frame *f = XFRAME (w->frame);
10626 int pause;
10627 int must_finish = 0;
10628 struct text_pos tlbufpos, tlendpos;
10629 int number_of_visible_frames;
10630 int count;
10631 struct frame *sf = SELECTED_FRAME ();
10632 int polling_stopped_here = 0;
10634 /* Non-zero means redisplay has to consider all windows on all
10635 frames. Zero means, only selected_window is considered. */
10636 int consider_all_windows_p;
10638 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10640 /* No redisplay if running in batch mode or frame is not yet fully
10641 initialized, or redisplay is explicitly turned off by setting
10642 Vinhibit_redisplay. */
10643 if (noninteractive
10644 || !NILP (Vinhibit_redisplay)
10645 || !f->glyphs_initialized_p)
10646 return;
10648 /* The flag redisplay_performed_directly_p is set by
10649 direct_output_for_insert when it already did the whole screen
10650 update necessary. */
10651 if (redisplay_performed_directly_p)
10653 redisplay_performed_directly_p = 0;
10654 if (!hscroll_windows (selected_window))
10655 return;
10658 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10659 if (popup_activated ())
10660 return;
10661 #endif
10663 /* I don't think this happens but let's be paranoid. */
10664 if (redisplaying_p)
10665 return;
10667 /* Record a function that resets redisplaying_p to its old value
10668 when we leave this function. */
10669 count = SPECPDL_INDEX ();
10670 record_unwind_protect (unwind_redisplay,
10671 Fcons (make_number (redisplaying_p), selected_frame));
10672 ++redisplaying_p;
10673 specbind (Qinhibit_free_realized_faces, Qnil);
10676 Lisp_Object tail, frame;
10678 FOR_EACH_FRAME (tail, frame)
10680 struct frame *f = XFRAME (frame);
10681 f->already_hscrolled_p = 0;
10685 retry:
10686 pause = 0;
10687 reconsider_clip_changes (w, current_buffer);
10688 last_escape_glyph_frame = NULL;
10689 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10691 /* If new fonts have been loaded that make a glyph matrix adjustment
10692 necessary, do it. */
10693 if (fonts_changed_p)
10695 adjust_glyphs (NULL);
10696 ++windows_or_buffers_changed;
10697 fonts_changed_p = 0;
10700 /* If face_change_count is non-zero, init_iterator will free all
10701 realized faces, which includes the faces referenced from current
10702 matrices. So, we can't reuse current matrices in this case. */
10703 if (face_change_count)
10704 ++windows_or_buffers_changed;
10706 if (FRAME_TERMCAP_P (sf)
10707 && FRAME_TTY (sf)->previous_frame != sf)
10709 /* Since frames on a single ASCII terminal share the same
10710 display area, displaying a different frame means redisplay
10711 the whole thing. */
10712 windows_or_buffers_changed++;
10713 SET_FRAME_GARBAGED (sf);
10714 FRAME_TTY (sf)->previous_frame = sf;
10717 /* Set the visible flags for all frames. Do this before checking
10718 for resized or garbaged frames; they want to know if their frames
10719 are visible. See the comment in frame.h for
10720 FRAME_SAMPLE_VISIBILITY. */
10722 Lisp_Object tail, frame;
10724 number_of_visible_frames = 0;
10726 FOR_EACH_FRAME (tail, frame)
10728 struct frame *f = XFRAME (frame);
10730 FRAME_SAMPLE_VISIBILITY (f);
10731 if (FRAME_VISIBLE_P (f))
10732 ++number_of_visible_frames;
10733 clear_desired_matrices (f);
10738 /* Notice any pending interrupt request to change frame size. */
10739 do_pending_window_change (1);
10741 /* Clear frames marked as garbaged. */
10742 if (frame_garbaged)
10743 clear_garbaged_frames ();
10745 /* Build menubar and tool-bar items. */
10746 if (NILP (Vmemory_full))
10747 prepare_menu_bars ();
10749 if (windows_or_buffers_changed)
10750 update_mode_lines++;
10752 /* Detect case that we need to write or remove a star in the mode line. */
10753 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10755 w->update_mode_line = Qt;
10756 if (buffer_shared > 1)
10757 update_mode_lines++;
10760 /* If %c is in the mode line, update it if needed. */
10761 if (!NILP (w->column_number_displayed)
10762 /* This alternative quickly identifies a common case
10763 where no change is needed. */
10764 && !(PT == XFASTINT (w->last_point)
10765 && XFASTINT (w->last_modified) >= MODIFF
10766 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10767 && (XFASTINT (w->column_number_displayed)
10768 != (int) current_column ())) /* iftc */
10769 w->update_mode_line = Qt;
10771 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10773 /* The variable buffer_shared is set in redisplay_window and
10774 indicates that we redisplay a buffer in different windows. See
10775 there. */
10776 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10777 || cursor_type_changed);
10779 /* If specs for an arrow have changed, do thorough redisplay
10780 to ensure we remove any arrow that should no longer exist. */
10781 if (overlay_arrows_changed_p ())
10782 consider_all_windows_p = windows_or_buffers_changed = 1;
10784 /* Normally the message* functions will have already displayed and
10785 updated the echo area, but the frame may have been trashed, or
10786 the update may have been preempted, so display the echo area
10787 again here. Checking message_cleared_p captures the case that
10788 the echo area should be cleared. */
10789 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10790 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10791 || (message_cleared_p
10792 && minibuf_level == 0
10793 /* If the mini-window is currently selected, this means the
10794 echo-area doesn't show through. */
10795 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10797 int window_height_changed_p = echo_area_display (0);
10798 must_finish = 1;
10800 /* If we don't display the current message, don't clear the
10801 message_cleared_p flag, because, if we did, we wouldn't clear
10802 the echo area in the next redisplay which doesn't preserve
10803 the echo area. */
10804 if (!display_last_displayed_message_p)
10805 message_cleared_p = 0;
10807 if (fonts_changed_p)
10808 goto retry;
10809 else if (window_height_changed_p)
10811 consider_all_windows_p = 1;
10812 ++update_mode_lines;
10813 ++windows_or_buffers_changed;
10815 /* If window configuration was changed, frames may have been
10816 marked garbaged. Clear them or we will experience
10817 surprises wrt scrolling. */
10818 if (frame_garbaged)
10819 clear_garbaged_frames ();
10822 else if (EQ (selected_window, minibuf_window)
10823 && (current_buffer->clip_changed
10824 || XFASTINT (w->last_modified) < MODIFF
10825 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10826 && resize_mini_window (w, 0))
10828 /* Resized active mini-window to fit the size of what it is
10829 showing if its contents might have changed. */
10830 must_finish = 1;
10831 consider_all_windows_p = 1;
10832 ++windows_or_buffers_changed;
10833 ++update_mode_lines;
10835 /* If window configuration was changed, frames may have been
10836 marked garbaged. Clear them or we will experience
10837 surprises wrt scrolling. */
10838 if (frame_garbaged)
10839 clear_garbaged_frames ();
10843 /* If showing the region, and mark has changed, we must redisplay
10844 the whole window. The assignment to this_line_start_pos prevents
10845 the optimization directly below this if-statement. */
10846 if (((!NILP (Vtransient_mark_mode)
10847 && !NILP (XBUFFER (w->buffer)->mark_active))
10848 != !NILP (w->region_showing))
10849 || (!NILP (w->region_showing)
10850 && !EQ (w->region_showing,
10851 Fmarker_position (XBUFFER (w->buffer)->mark))))
10852 CHARPOS (this_line_start_pos) = 0;
10854 /* Optimize the case that only the line containing the cursor in the
10855 selected window has changed. Variables starting with this_ are
10856 set in display_line and record information about the line
10857 containing the cursor. */
10858 tlbufpos = this_line_start_pos;
10859 tlendpos = this_line_end_pos;
10860 if (!consider_all_windows_p
10861 && CHARPOS (tlbufpos) > 0
10862 && NILP (w->update_mode_line)
10863 && !current_buffer->clip_changed
10864 && !current_buffer->prevent_redisplay_optimizations_p
10865 && FRAME_VISIBLE_P (XFRAME (w->frame))
10866 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10867 /* Make sure recorded data applies to current buffer, etc. */
10868 && this_line_buffer == current_buffer
10869 && current_buffer == XBUFFER (w->buffer)
10870 && NILP (w->force_start)
10871 && NILP (w->optional_new_start)
10872 /* Point must be on the line that we have info recorded about. */
10873 && PT >= CHARPOS (tlbufpos)
10874 && PT <= Z - CHARPOS (tlendpos)
10875 /* All text outside that line, including its final newline,
10876 must be unchanged */
10877 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10878 CHARPOS (tlendpos)))
10880 if (CHARPOS (tlbufpos) > BEGV
10881 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10882 && (CHARPOS (tlbufpos) == ZV
10883 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10884 /* Former continuation line has disappeared by becoming empty */
10885 goto cancel;
10886 else if (XFASTINT (w->last_modified) < MODIFF
10887 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10888 || MINI_WINDOW_P (w))
10890 /* We have to handle the case of continuation around a
10891 wide-column character (See the comment in indent.c around
10892 line 885).
10894 For instance, in the following case:
10896 -------- Insert --------
10897 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10898 J_I_ ==> J_I_ `^^' are cursors.
10899 ^^ ^^
10900 -------- --------
10902 As we have to redraw the line above, we should goto cancel. */
10904 struct it it;
10905 int line_height_before = this_line_pixel_height;
10907 /* Note that start_display will handle the case that the
10908 line starting at tlbufpos is a continuation lines. */
10909 start_display (&it, w, tlbufpos);
10911 /* Implementation note: It this still necessary? */
10912 if (it.current_x != this_line_start_x)
10913 goto cancel;
10915 TRACE ((stderr, "trying display optimization 1\n"));
10916 w->cursor.vpos = -1;
10917 overlay_arrow_seen = 0;
10918 it.vpos = this_line_vpos;
10919 it.current_y = this_line_y;
10920 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10921 display_line (&it);
10923 /* If line contains point, is not continued,
10924 and ends at same distance from eob as before, we win */
10925 if (w->cursor.vpos >= 0
10926 /* Line is not continued, otherwise this_line_start_pos
10927 would have been set to 0 in display_line. */
10928 && CHARPOS (this_line_start_pos)
10929 /* Line ends as before. */
10930 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10931 /* Line has same height as before. Otherwise other lines
10932 would have to be shifted up or down. */
10933 && this_line_pixel_height == line_height_before)
10935 /* If this is not the window's last line, we must adjust
10936 the charstarts of the lines below. */
10937 if (it.current_y < it.last_visible_y)
10939 struct glyph_row *row
10940 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10941 int delta, delta_bytes;
10943 if (Z - CHARPOS (tlendpos) == ZV)
10945 /* This line ends at end of (accessible part of)
10946 buffer. There is no newline to count. */
10947 delta = (Z
10948 - CHARPOS (tlendpos)
10949 - MATRIX_ROW_START_CHARPOS (row));
10950 delta_bytes = (Z_BYTE
10951 - BYTEPOS (tlendpos)
10952 - MATRIX_ROW_START_BYTEPOS (row));
10954 else
10956 /* This line ends in a newline. Must take
10957 account of the newline and the rest of the
10958 text that follows. */
10959 delta = (Z
10960 - CHARPOS (tlendpos)
10961 - MATRIX_ROW_START_CHARPOS (row));
10962 delta_bytes = (Z_BYTE
10963 - BYTEPOS (tlendpos)
10964 - MATRIX_ROW_START_BYTEPOS (row));
10967 increment_matrix_positions (w->current_matrix,
10968 this_line_vpos + 1,
10969 w->current_matrix->nrows,
10970 delta, delta_bytes);
10973 /* If this row displays text now but previously didn't,
10974 or vice versa, w->window_end_vpos may have to be
10975 adjusted. */
10976 if ((it.glyph_row - 1)->displays_text_p)
10978 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10979 XSETINT (w->window_end_vpos, this_line_vpos);
10981 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10982 && this_line_vpos > 0)
10983 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10984 w->window_end_valid = Qnil;
10986 /* Update hint: No need to try to scroll in update_window. */
10987 w->desired_matrix->no_scrolling_p = 1;
10989 #if GLYPH_DEBUG
10990 *w->desired_matrix->method = 0;
10991 debug_method_add (w, "optimization 1");
10992 #endif
10993 #ifdef HAVE_WINDOW_SYSTEM
10994 update_window_fringes (w, 0);
10995 #endif
10996 goto update;
10998 else
10999 goto cancel;
11001 else if (/* Cursor position hasn't changed. */
11002 PT == XFASTINT (w->last_point)
11003 /* Make sure the cursor was last displayed
11004 in this window. Otherwise we have to reposition it. */
11005 && 0 <= w->cursor.vpos
11006 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11008 if (!must_finish)
11010 do_pending_window_change (1);
11012 /* We used to always goto end_of_redisplay here, but this
11013 isn't enough if we have a blinking cursor. */
11014 if (w->cursor_off_p == w->last_cursor_off_p)
11015 goto end_of_redisplay;
11017 goto update;
11019 /* If highlighting the region, or if the cursor is in the echo area,
11020 then we can't just move the cursor. */
11021 else if (! (!NILP (Vtransient_mark_mode)
11022 && !NILP (current_buffer->mark_active))
11023 && (EQ (selected_window, current_buffer->last_selected_window)
11024 || highlight_nonselected_windows)
11025 && NILP (w->region_showing)
11026 && NILP (Vshow_trailing_whitespace)
11027 && !cursor_in_echo_area)
11029 struct it it;
11030 struct glyph_row *row;
11032 /* Skip from tlbufpos to PT and see where it is. Note that
11033 PT may be in invisible text. If so, we will end at the
11034 next visible position. */
11035 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11036 NULL, DEFAULT_FACE_ID);
11037 it.current_x = this_line_start_x;
11038 it.current_y = this_line_y;
11039 it.vpos = this_line_vpos;
11041 /* The call to move_it_to stops in front of PT, but
11042 moves over before-strings. */
11043 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11045 if (it.vpos == this_line_vpos
11046 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11047 row->enabled_p))
11049 xassert (this_line_vpos == it.vpos);
11050 xassert (this_line_y == it.current_y);
11051 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11052 #if GLYPH_DEBUG
11053 *w->desired_matrix->method = 0;
11054 debug_method_add (w, "optimization 3");
11055 #endif
11056 goto update;
11058 else
11059 goto cancel;
11062 cancel:
11063 /* Text changed drastically or point moved off of line. */
11064 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11067 CHARPOS (this_line_start_pos) = 0;
11068 consider_all_windows_p |= buffer_shared > 1;
11069 ++clear_face_cache_count;
11070 #ifdef HAVE_WINDOW_SYSTEM
11071 ++clear_image_cache_count;
11072 #endif
11074 /* Build desired matrices, and update the display. If
11075 consider_all_windows_p is non-zero, do it for all windows on all
11076 frames. Otherwise do it for selected_window, only. */
11078 if (consider_all_windows_p)
11080 Lisp_Object tail, frame;
11082 FOR_EACH_FRAME (tail, frame)
11083 XFRAME (frame)->updated_p = 0;
11085 /* Recompute # windows showing selected buffer. This will be
11086 incremented each time such a window is displayed. */
11087 buffer_shared = 0;
11089 FOR_EACH_FRAME (tail, frame)
11091 struct frame *f = XFRAME (frame);
11093 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11095 if (! EQ (frame, selected_frame))
11096 /* Select the frame, for the sake of frame-local
11097 variables. */
11098 select_frame_for_redisplay (frame);
11100 /* Mark all the scroll bars to be removed; we'll redeem
11101 the ones we want when we redisplay their windows. */
11102 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11103 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11105 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11106 redisplay_windows (FRAME_ROOT_WINDOW (f));
11108 /* Any scroll bars which redisplay_windows should have
11109 nuked should now go away. */
11110 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11111 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11113 /* If fonts changed, display again. */
11114 /* ??? rms: I suspect it is a mistake to jump all the way
11115 back to retry here. It should just retry this frame. */
11116 if (fonts_changed_p)
11117 goto retry;
11119 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11121 /* See if we have to hscroll. */
11122 if (!f->already_hscrolled_p)
11124 f->already_hscrolled_p = 1;
11125 if (hscroll_windows (f->root_window))
11126 goto retry;
11129 /* Prevent various kinds of signals during display
11130 update. stdio is not robust about handling
11131 signals, which can cause an apparent I/O
11132 error. */
11133 if (interrupt_input)
11134 unrequest_sigio ();
11135 STOP_POLLING;
11137 /* Update the display. */
11138 set_window_update_flags (XWINDOW (f->root_window), 1);
11139 pause |= update_frame (f, 0, 0);
11140 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11141 if (pause)
11142 break;
11143 #endif
11145 f->updated_p = 1;
11150 if (!pause)
11152 /* Do the mark_window_display_accurate after all windows have
11153 been redisplayed because this call resets flags in buffers
11154 which are needed for proper redisplay. */
11155 FOR_EACH_FRAME (tail, frame)
11157 struct frame *f = XFRAME (frame);
11158 if (f->updated_p)
11160 mark_window_display_accurate (f->root_window, 1);
11161 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11162 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11167 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11169 Lisp_Object mini_window;
11170 struct frame *mini_frame;
11172 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11173 /* Use list_of_error, not Qerror, so that
11174 we catch only errors and don't run the debugger. */
11175 internal_condition_case_1 (redisplay_window_1, selected_window,
11176 list_of_error,
11177 redisplay_window_error);
11179 /* Compare desired and current matrices, perform output. */
11181 update:
11182 /* If fonts changed, display again. */
11183 if (fonts_changed_p)
11184 goto retry;
11186 /* Prevent various kinds of signals during display update.
11187 stdio is not robust about handling signals,
11188 which can cause an apparent I/O error. */
11189 if (interrupt_input)
11190 unrequest_sigio ();
11191 STOP_POLLING;
11193 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11195 if (hscroll_windows (selected_window))
11196 goto retry;
11198 XWINDOW (selected_window)->must_be_updated_p = 1;
11199 pause = update_frame (sf, 0, 0);
11202 /* We may have called echo_area_display at the top of this
11203 function. If the echo area is on another frame, that may
11204 have put text on a frame other than the selected one, so the
11205 above call to update_frame would not have caught it. Catch
11206 it here. */
11207 mini_window = FRAME_MINIBUF_WINDOW (sf);
11208 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11210 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11212 XWINDOW (mini_window)->must_be_updated_p = 1;
11213 pause |= update_frame (mini_frame, 0, 0);
11214 if (!pause && hscroll_windows (mini_window))
11215 goto retry;
11219 /* If display was paused because of pending input, make sure we do a
11220 thorough update the next time. */
11221 if (pause)
11223 /* Prevent the optimization at the beginning of
11224 redisplay_internal that tries a single-line update of the
11225 line containing the cursor in the selected window. */
11226 CHARPOS (this_line_start_pos) = 0;
11228 /* Let the overlay arrow be updated the next time. */
11229 update_overlay_arrows (0);
11231 /* If we pause after scrolling, some rows in the current
11232 matrices of some windows are not valid. */
11233 if (!WINDOW_FULL_WIDTH_P (w)
11234 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11235 update_mode_lines = 1;
11237 else
11239 if (!consider_all_windows_p)
11241 /* This has already been done above if
11242 consider_all_windows_p is set. */
11243 mark_window_display_accurate_1 (w, 1);
11245 /* Say overlay arrows are up to date. */
11246 update_overlay_arrows (1);
11248 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11249 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11252 update_mode_lines = 0;
11253 windows_or_buffers_changed = 0;
11254 cursor_type_changed = 0;
11257 /* Start SIGIO interrupts coming again. Having them off during the
11258 code above makes it less likely one will discard output, but not
11259 impossible, since there might be stuff in the system buffer here.
11260 But it is much hairier to try to do anything about that. */
11261 if (interrupt_input)
11262 request_sigio ();
11263 RESUME_POLLING;
11265 /* If a frame has become visible which was not before, redisplay
11266 again, so that we display it. Expose events for such a frame
11267 (which it gets when becoming visible) don't call the parts of
11268 redisplay constructing glyphs, so simply exposing a frame won't
11269 display anything in this case. So, we have to display these
11270 frames here explicitly. */
11271 if (!pause)
11273 Lisp_Object tail, frame;
11274 int new_count = 0;
11276 FOR_EACH_FRAME (tail, frame)
11278 int this_is_visible = 0;
11280 if (XFRAME (frame)->visible)
11281 this_is_visible = 1;
11282 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11283 if (XFRAME (frame)->visible)
11284 this_is_visible = 1;
11286 if (this_is_visible)
11287 new_count++;
11290 if (new_count != number_of_visible_frames)
11291 windows_or_buffers_changed++;
11294 /* Change frame size now if a change is pending. */
11295 do_pending_window_change (1);
11297 /* If we just did a pending size change, or have additional
11298 visible frames, redisplay again. */
11299 if (windows_or_buffers_changed && !pause)
11300 goto retry;
11302 /* Clear the face cache eventually. */
11303 if (consider_all_windows_p)
11305 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11307 clear_face_cache (0);
11308 clear_face_cache_count = 0;
11310 #ifdef HAVE_WINDOW_SYSTEM
11311 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11313 Lisp_Object tail, frame;
11314 FOR_EACH_FRAME (tail, frame)
11316 struct frame *f = XFRAME (frame);
11317 if (FRAME_WINDOW_P (f))
11318 clear_image_cache (f, 0);
11320 clear_image_cache_count = 0;
11322 #endif /* HAVE_WINDOW_SYSTEM */
11325 end_of_redisplay:
11326 unbind_to (count, Qnil);
11327 RESUME_POLLING;
11331 /* Redisplay, but leave alone any recent echo area message unless
11332 another message has been requested in its place.
11334 This is useful in situations where you need to redisplay but no
11335 user action has occurred, making it inappropriate for the message
11336 area to be cleared. See tracking_off and
11337 wait_reading_process_output for examples of these situations.
11339 FROM_WHERE is an integer saying from where this function was
11340 called. This is useful for debugging. */
11342 void
11343 redisplay_preserve_echo_area (from_where)
11344 int from_where;
11346 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11348 if (!NILP (echo_area_buffer[1]))
11350 /* We have a previously displayed message, but no current
11351 message. Redisplay the previous message. */
11352 display_last_displayed_message_p = 1;
11353 redisplay_internal (1);
11354 display_last_displayed_message_p = 0;
11356 else
11357 redisplay_internal (1);
11359 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11360 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11361 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11365 /* Function registered with record_unwind_protect in
11366 redisplay_internal. Reset redisplaying_p to the value it had
11367 before redisplay_internal was called, and clear
11368 prevent_freeing_realized_faces_p. It also selects the previously
11369 selected frame, unless it has been deleted (by an X connection
11370 failure during redisplay, for example). */
11372 static Lisp_Object
11373 unwind_redisplay (val)
11374 Lisp_Object val;
11376 Lisp_Object old_redisplaying_p, old_frame;
11378 old_redisplaying_p = XCAR (val);
11379 redisplaying_p = XFASTINT (old_redisplaying_p);
11380 old_frame = XCDR (val);
11381 if (! EQ (old_frame, selected_frame)
11382 && FRAME_LIVE_P (XFRAME (old_frame)))
11383 select_frame_for_redisplay (old_frame);
11384 return Qnil;
11388 /* Mark the display of window W as accurate or inaccurate. If
11389 ACCURATE_P is non-zero mark display of W as accurate. If
11390 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11391 redisplay_internal is called. */
11393 static void
11394 mark_window_display_accurate_1 (w, accurate_p)
11395 struct window *w;
11396 int accurate_p;
11398 if (BUFFERP (w->buffer))
11400 struct buffer *b = XBUFFER (w->buffer);
11402 w->last_modified
11403 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11404 w->last_overlay_modified
11405 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11406 w->last_had_star
11407 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11409 if (accurate_p)
11411 b->clip_changed = 0;
11412 b->prevent_redisplay_optimizations_p = 0;
11414 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11415 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11416 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11417 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11419 w->current_matrix->buffer = b;
11420 w->current_matrix->begv = BUF_BEGV (b);
11421 w->current_matrix->zv = BUF_ZV (b);
11423 w->last_cursor = w->cursor;
11424 w->last_cursor_off_p = w->cursor_off_p;
11426 if (w == XWINDOW (selected_window))
11427 w->last_point = make_number (BUF_PT (b));
11428 else
11429 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11433 if (accurate_p)
11435 w->window_end_valid = w->buffer;
11436 #if 0 /* This is incorrect with variable-height lines. */
11437 xassert (XINT (w->window_end_vpos)
11438 < (WINDOW_TOTAL_LINES (w)
11439 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11440 #endif
11441 w->update_mode_line = Qnil;
11446 /* Mark the display of windows in the window tree rooted at WINDOW as
11447 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11448 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11449 be redisplayed the next time redisplay_internal is called. */
11451 void
11452 mark_window_display_accurate (window, accurate_p)
11453 Lisp_Object window;
11454 int accurate_p;
11456 struct window *w;
11458 for (; !NILP (window); window = w->next)
11460 w = XWINDOW (window);
11461 mark_window_display_accurate_1 (w, accurate_p);
11463 if (!NILP (w->vchild))
11464 mark_window_display_accurate (w->vchild, accurate_p);
11465 if (!NILP (w->hchild))
11466 mark_window_display_accurate (w->hchild, accurate_p);
11469 if (accurate_p)
11471 update_overlay_arrows (1);
11473 else
11475 /* Force a thorough redisplay the next time by setting
11476 last_arrow_position and last_arrow_string to t, which is
11477 unequal to any useful value of Voverlay_arrow_... */
11478 update_overlay_arrows (-1);
11483 /* Return value in display table DP (Lisp_Char_Table *) for character
11484 C. Since a display table doesn't have any parent, we don't have to
11485 follow parent. Do not call this function directly but use the
11486 macro DISP_CHAR_VECTOR. */
11488 Lisp_Object
11489 disp_char_vector (dp, c)
11490 struct Lisp_Char_Table *dp;
11491 int c;
11493 int code[4], i;
11494 Lisp_Object val;
11496 if (SINGLE_BYTE_CHAR_P (c))
11497 return (dp->contents[c]);
11499 SPLIT_CHAR (c, code[0], code[1], code[2]);
11500 if (code[1] < 32)
11501 code[1] = -1;
11502 else if (code[2] < 32)
11503 code[2] = -1;
11505 /* Here, the possible range of code[0] (== charset ID) is
11506 128..max_charset. Since the top level char table contains data
11507 for multibyte characters after 256th element, we must increment
11508 code[0] by 128 to get a correct index. */
11509 code[0] += 128;
11510 code[3] = -1; /* anchor */
11512 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11514 val = dp->contents[code[i]];
11515 if (!SUB_CHAR_TABLE_P (val))
11516 return (NILP (val) ? dp->defalt : val);
11519 /* Here, val is a sub char table. We return the default value of
11520 it. */
11521 return (dp->defalt);
11526 /***********************************************************************
11527 Window Redisplay
11528 ***********************************************************************/
11530 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11532 static void
11533 redisplay_windows (window)
11534 Lisp_Object window;
11536 while (!NILP (window))
11538 struct window *w = XWINDOW (window);
11540 if (!NILP (w->hchild))
11541 redisplay_windows (w->hchild);
11542 else if (!NILP (w->vchild))
11543 redisplay_windows (w->vchild);
11544 else
11546 displayed_buffer = XBUFFER (w->buffer);
11547 /* Use list_of_error, not Qerror, so that
11548 we catch only errors and don't run the debugger. */
11549 internal_condition_case_1 (redisplay_window_0, window,
11550 list_of_error,
11551 redisplay_window_error);
11554 window = w->next;
11558 static Lisp_Object
11559 redisplay_window_error ()
11561 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11562 return Qnil;
11565 static Lisp_Object
11566 redisplay_window_0 (window)
11567 Lisp_Object window;
11569 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11570 redisplay_window (window, 0);
11571 return Qnil;
11574 static Lisp_Object
11575 redisplay_window_1 (window)
11576 Lisp_Object window;
11578 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11579 redisplay_window (window, 1);
11580 return Qnil;
11584 /* Increment GLYPH until it reaches END or CONDITION fails while
11585 adding (GLYPH)->pixel_width to X. */
11587 #define SKIP_GLYPHS(glyph, end, x, condition) \
11588 do \
11590 (x) += (glyph)->pixel_width; \
11591 ++(glyph); \
11593 while ((glyph) < (end) && (condition))
11596 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11597 DELTA is the number of bytes by which positions recorded in ROW
11598 differ from current buffer positions. */
11600 void
11601 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11602 struct window *w;
11603 struct glyph_row *row;
11604 struct glyph_matrix *matrix;
11605 int delta, delta_bytes, dy, dvpos;
11607 struct glyph *glyph = row->glyphs[TEXT_AREA];
11608 struct glyph *end = glyph + row->used[TEXT_AREA];
11609 struct glyph *cursor = NULL;
11610 /* The first glyph that starts a sequence of glyphs from string. */
11611 struct glyph *string_start;
11612 /* The X coordinate of string_start. */
11613 int string_start_x;
11614 /* The last known character position. */
11615 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11616 /* The last known character position before string_start. */
11617 int string_before_pos;
11618 int x = row->x;
11619 int cursor_x = x;
11620 int cursor_from_overlay_pos = 0;
11621 int pt_old = PT - delta;
11623 /* Skip over glyphs not having an object at the start of the row.
11624 These are special glyphs like truncation marks on terminal
11625 frames. */
11626 if (row->displays_text_p)
11627 while (glyph < end
11628 && INTEGERP (glyph->object)
11629 && glyph->charpos < 0)
11631 x += glyph->pixel_width;
11632 ++glyph;
11635 string_start = NULL;
11636 while (glyph < end
11637 && !INTEGERP (glyph->object)
11638 && (!BUFFERP (glyph->object)
11639 || (last_pos = glyph->charpos) < pt_old))
11641 if (! STRINGP (glyph->object))
11643 string_start = NULL;
11644 x += glyph->pixel_width;
11645 ++glyph;
11646 if (cursor_from_overlay_pos
11647 && last_pos >= cursor_from_overlay_pos)
11649 cursor_from_overlay_pos = 0;
11650 cursor = 0;
11653 else
11655 string_before_pos = last_pos;
11656 string_start = glyph;
11657 string_start_x = x;
11658 /* Skip all glyphs from string. */
11661 Lisp_Object cprop;
11662 int pos;
11663 if ((cursor == NULL || glyph > cursor)
11664 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11665 Qcursor, (glyph)->object),
11666 !NILP (cprop))
11667 && (pos = string_buffer_position (w, glyph->object,
11668 string_before_pos),
11669 (pos == 0 /* From overlay */
11670 || pos == pt_old)))
11672 /* Estimate overlay buffer position from the buffer
11673 positions of the glyphs before and after the overlay.
11674 Add 1 to last_pos so that if point corresponds to the
11675 glyph right after the overlay, we still use a 'cursor'
11676 property found in that overlay. */
11677 cursor_from_overlay_pos = (pos ? 0 : last_pos
11678 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11679 cursor = glyph;
11680 cursor_x = x;
11682 x += glyph->pixel_width;
11683 ++glyph;
11685 while (glyph < end && EQ (glyph->object, string_start->object));
11689 if (cursor != NULL)
11691 glyph = cursor;
11692 x = cursor_x;
11694 else if (row->ends_in_ellipsis_p && glyph == end)
11696 /* Scan back over the ellipsis glyphs, decrementing positions. */
11697 while (glyph > row->glyphs[TEXT_AREA]
11698 && (glyph - 1)->charpos == last_pos)
11699 glyph--, x -= glyph->pixel_width;
11700 /* That loop always goes one position too far,
11701 including the glyph before the ellipsis.
11702 So scan forward over that one. */
11703 x += glyph->pixel_width;
11704 glyph++;
11706 else if (string_start
11707 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11709 /* We may have skipped over point because the previous glyphs
11710 are from string. As there's no easy way to know the
11711 character position of the current glyph, find the correct
11712 glyph on point by scanning from string_start again. */
11713 Lisp_Object limit;
11714 Lisp_Object string;
11715 int pos;
11717 limit = make_number (pt_old + 1);
11718 end = glyph;
11719 glyph = string_start;
11720 x = string_start_x;
11721 string = glyph->object;
11722 pos = string_buffer_position (w, string, string_before_pos);
11723 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11724 because we always put cursor after overlay strings. */
11725 while (pos == 0 && glyph < end)
11727 string = glyph->object;
11728 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11729 if (glyph < end)
11730 pos = string_buffer_position (w, glyph->object, string_before_pos);
11733 while (glyph < end)
11735 pos = XINT (Fnext_single_char_property_change
11736 (make_number (pos), Qdisplay, Qnil, limit));
11737 if (pos > pt_old)
11738 break;
11739 /* Skip glyphs from the same string. */
11740 string = glyph->object;
11741 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11742 /* Skip glyphs from an overlay. */
11743 while (glyph < end
11744 && ! string_buffer_position (w, glyph->object, pos))
11746 string = glyph->object;
11747 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11752 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11753 w->cursor.x = x;
11754 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11755 w->cursor.y = row->y + dy;
11757 if (w == XWINDOW (selected_window))
11759 if (!row->continued_p
11760 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11761 && row->x == 0)
11763 this_line_buffer = XBUFFER (w->buffer);
11765 CHARPOS (this_line_start_pos)
11766 = MATRIX_ROW_START_CHARPOS (row) + delta;
11767 BYTEPOS (this_line_start_pos)
11768 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11770 CHARPOS (this_line_end_pos)
11771 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11772 BYTEPOS (this_line_end_pos)
11773 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11775 this_line_y = w->cursor.y;
11776 this_line_pixel_height = row->height;
11777 this_line_vpos = w->cursor.vpos;
11778 this_line_start_x = row->x;
11780 else
11781 CHARPOS (this_line_start_pos) = 0;
11786 /* Run window scroll functions, if any, for WINDOW with new window
11787 start STARTP. Sets the window start of WINDOW to that position.
11789 We assume that the window's buffer is really current. */
11791 static INLINE struct text_pos
11792 run_window_scroll_functions (window, startp)
11793 Lisp_Object window;
11794 struct text_pos startp;
11796 struct window *w = XWINDOW (window);
11797 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11799 if (current_buffer != XBUFFER (w->buffer))
11800 abort ();
11802 if (!NILP (Vwindow_scroll_functions))
11804 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11805 make_number (CHARPOS (startp)));
11806 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11807 /* In case the hook functions switch buffers. */
11808 if (current_buffer != XBUFFER (w->buffer))
11809 set_buffer_internal_1 (XBUFFER (w->buffer));
11812 return startp;
11816 /* Make sure the line containing the cursor is fully visible.
11817 A value of 1 means there is nothing to be done.
11818 (Either the line is fully visible, or it cannot be made so,
11819 or we cannot tell.)
11821 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11822 is higher than window.
11824 A value of 0 means the caller should do scrolling
11825 as if point had gone off the screen. */
11827 static int
11828 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11829 struct window *w;
11830 int force_p;
11831 int current_matrix_p;
11833 struct glyph_matrix *matrix;
11834 struct glyph_row *row;
11835 int window_height;
11837 if (!make_cursor_line_fully_visible_p)
11838 return 1;
11840 /* It's not always possible to find the cursor, e.g, when a window
11841 is full of overlay strings. Don't do anything in that case. */
11842 if (w->cursor.vpos < 0)
11843 return 1;
11845 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11846 row = MATRIX_ROW (matrix, w->cursor.vpos);
11848 /* If the cursor row is not partially visible, there's nothing to do. */
11849 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11850 return 1;
11852 /* If the row the cursor is in is taller than the window's height,
11853 it's not clear what to do, so do nothing. */
11854 window_height = window_box_height (w);
11855 if (row->height >= window_height)
11857 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11858 return 1;
11860 return 0;
11862 #if 0
11863 /* This code used to try to scroll the window just enough to make
11864 the line visible. It returned 0 to say that the caller should
11865 allocate larger glyph matrices. */
11867 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11869 int dy = row->height - row->visible_height;
11870 w->vscroll = 0;
11871 w->cursor.y += dy;
11872 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11874 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11876 int dy = - (row->height - row->visible_height);
11877 w->vscroll = dy;
11878 w->cursor.y += dy;
11879 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11882 /* When we change the cursor y-position of the selected window,
11883 change this_line_y as well so that the display optimization for
11884 the cursor line of the selected window in redisplay_internal uses
11885 the correct y-position. */
11886 if (w == XWINDOW (selected_window))
11887 this_line_y = w->cursor.y;
11889 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11890 redisplay with larger matrices. */
11891 if (matrix->nrows < required_matrix_height (w))
11893 fonts_changed_p = 1;
11894 return 0;
11897 return 1;
11898 #endif /* 0 */
11902 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11903 non-zero means only WINDOW is redisplayed in redisplay_internal.
11904 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11905 in redisplay_window to bring a partially visible line into view in
11906 the case that only the cursor has moved.
11908 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11909 last screen line's vertical height extends past the end of the screen.
11911 Value is
11913 1 if scrolling succeeded
11915 0 if scrolling didn't find point.
11917 -1 if new fonts have been loaded so that we must interrupt
11918 redisplay, adjust glyph matrices, and try again. */
11920 enum
11922 SCROLLING_SUCCESS,
11923 SCROLLING_FAILED,
11924 SCROLLING_NEED_LARGER_MATRICES
11927 static int
11928 try_scrolling (window, just_this_one_p, scroll_conservatively,
11929 scroll_step, temp_scroll_step, last_line_misfit)
11930 Lisp_Object window;
11931 int just_this_one_p;
11932 EMACS_INT scroll_conservatively, scroll_step;
11933 int temp_scroll_step;
11934 int last_line_misfit;
11936 struct window *w = XWINDOW (window);
11937 struct frame *f = XFRAME (w->frame);
11938 struct text_pos scroll_margin_pos;
11939 struct text_pos pos;
11940 struct text_pos startp;
11941 struct it it;
11942 Lisp_Object window_end;
11943 int this_scroll_margin;
11944 int dy = 0;
11945 int scroll_max;
11946 int rc;
11947 int amount_to_scroll = 0;
11948 Lisp_Object aggressive;
11949 int height;
11950 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11952 #if GLYPH_DEBUG
11953 debug_method_add (w, "try_scrolling");
11954 #endif
11956 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11958 /* Compute scroll margin height in pixels. We scroll when point is
11959 within this distance from the top or bottom of the window. */
11960 if (scroll_margin > 0)
11962 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11963 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11965 else
11966 this_scroll_margin = 0;
11968 /* Force scroll_conservatively to have a reasonable value so it doesn't
11969 cause an overflow while computing how much to scroll. */
11970 if (scroll_conservatively)
11971 scroll_conservatively = min (scroll_conservatively,
11972 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11974 /* Compute how much we should try to scroll maximally to bring point
11975 into view. */
11976 if (scroll_step || scroll_conservatively || temp_scroll_step)
11977 scroll_max = max (scroll_step,
11978 max (scroll_conservatively, temp_scroll_step));
11979 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11980 || NUMBERP (current_buffer->scroll_up_aggressively))
11981 /* We're trying to scroll because of aggressive scrolling
11982 but no scroll_step is set. Choose an arbitrary one. Maybe
11983 there should be a variable for this. */
11984 scroll_max = 10;
11985 else
11986 scroll_max = 0;
11987 scroll_max *= FRAME_LINE_HEIGHT (f);
11989 /* Decide whether we have to scroll down. Start at the window end
11990 and move this_scroll_margin up to find the position of the scroll
11991 margin. */
11992 window_end = Fwindow_end (window, Qt);
11994 too_near_end:
11996 CHARPOS (scroll_margin_pos) = XINT (window_end);
11997 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11999 if (this_scroll_margin || extra_scroll_margin_lines)
12001 start_display (&it, w, scroll_margin_pos);
12002 if (this_scroll_margin)
12003 move_it_vertically_backward (&it, this_scroll_margin);
12004 if (extra_scroll_margin_lines)
12005 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12006 scroll_margin_pos = it.current.pos;
12009 if (PT >= CHARPOS (scroll_margin_pos))
12011 int y0;
12013 /* Point is in the scroll margin at the bottom of the window, or
12014 below. Compute a new window start that makes point visible. */
12016 /* Compute the distance from the scroll margin to PT.
12017 Give up if the distance is greater than scroll_max. */
12018 start_display (&it, w, scroll_margin_pos);
12019 y0 = it.current_y;
12020 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12021 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12023 /* To make point visible, we have to move the window start
12024 down so that the line the cursor is in is visible, which
12025 means we have to add in the height of the cursor line. */
12026 dy = line_bottom_y (&it) - y0;
12028 if (dy > scroll_max)
12029 return SCROLLING_FAILED;
12031 /* Move the window start down. If scrolling conservatively,
12032 move it just enough down to make point visible. If
12033 scroll_step is set, move it down by scroll_step. */
12034 start_display (&it, w, startp);
12036 if (scroll_conservatively)
12037 /* Set AMOUNT_TO_SCROLL to at least one line,
12038 and at most scroll_conservatively lines. */
12039 amount_to_scroll
12040 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12041 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12042 else if (scroll_step || temp_scroll_step)
12043 amount_to_scroll = scroll_max;
12044 else
12046 aggressive = current_buffer->scroll_up_aggressively;
12047 height = WINDOW_BOX_TEXT_HEIGHT (w);
12048 if (NUMBERP (aggressive))
12050 double float_amount = XFLOATINT (aggressive) * height;
12051 amount_to_scroll = float_amount;
12052 if (amount_to_scroll == 0 && float_amount > 0)
12053 amount_to_scroll = 1;
12057 if (amount_to_scroll <= 0)
12058 return SCROLLING_FAILED;
12060 /* If moving by amount_to_scroll leaves STARTP unchanged,
12061 move it down one screen line. */
12063 move_it_vertically (&it, amount_to_scroll);
12064 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12065 move_it_by_lines (&it, 1, 1);
12066 startp = it.current.pos;
12068 else
12070 /* See if point is inside the scroll margin at the top of the
12071 window. */
12072 scroll_margin_pos = startp;
12073 if (this_scroll_margin)
12075 start_display (&it, w, startp);
12076 move_it_vertically (&it, this_scroll_margin);
12077 scroll_margin_pos = it.current.pos;
12080 if (PT < CHARPOS (scroll_margin_pos))
12082 /* Point is in the scroll margin at the top of the window or
12083 above what is displayed in the window. */
12084 int y0;
12086 /* Compute the vertical distance from PT to the scroll
12087 margin position. Give up if distance is greater than
12088 scroll_max. */
12089 SET_TEXT_POS (pos, PT, PT_BYTE);
12090 start_display (&it, w, pos);
12091 y0 = it.current_y;
12092 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12093 it.last_visible_y, -1,
12094 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12095 dy = it.current_y - y0;
12096 if (dy > scroll_max)
12097 return SCROLLING_FAILED;
12099 /* Compute new window start. */
12100 start_display (&it, w, startp);
12102 if (scroll_conservatively)
12103 amount_to_scroll
12104 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12105 else if (scroll_step || temp_scroll_step)
12106 amount_to_scroll = scroll_max;
12107 else
12109 aggressive = current_buffer->scroll_down_aggressively;
12110 height = WINDOW_BOX_TEXT_HEIGHT (w);
12111 if (NUMBERP (aggressive))
12113 double float_amount = XFLOATINT (aggressive) * height;
12114 amount_to_scroll = float_amount;
12115 if (amount_to_scroll == 0 && float_amount > 0)
12116 amount_to_scroll = 1;
12120 if (amount_to_scroll <= 0)
12121 return SCROLLING_FAILED;
12123 move_it_vertically_backward (&it, amount_to_scroll);
12124 startp = it.current.pos;
12128 /* Run window scroll functions. */
12129 startp = run_window_scroll_functions (window, startp);
12131 /* Display the window. Give up if new fonts are loaded, or if point
12132 doesn't appear. */
12133 if (!try_window (window, startp, 0))
12134 rc = SCROLLING_NEED_LARGER_MATRICES;
12135 else if (w->cursor.vpos < 0)
12137 clear_glyph_matrix (w->desired_matrix);
12138 rc = SCROLLING_FAILED;
12140 else
12142 /* Maybe forget recorded base line for line number display. */
12143 if (!just_this_one_p
12144 || current_buffer->clip_changed
12145 || BEG_UNCHANGED < CHARPOS (startp))
12146 w->base_line_number = Qnil;
12148 /* If cursor ends up on a partially visible line,
12149 treat that as being off the bottom of the screen. */
12150 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12152 clear_glyph_matrix (w->desired_matrix);
12153 ++extra_scroll_margin_lines;
12154 goto too_near_end;
12156 rc = SCROLLING_SUCCESS;
12159 return rc;
12163 /* Compute a suitable window start for window W if display of W starts
12164 on a continuation line. Value is non-zero if a new window start
12165 was computed.
12167 The new window start will be computed, based on W's width, starting
12168 from the start of the continued line. It is the start of the
12169 screen line with the minimum distance from the old start W->start. */
12171 static int
12172 compute_window_start_on_continuation_line (w)
12173 struct window *w;
12175 struct text_pos pos, start_pos;
12176 int window_start_changed_p = 0;
12178 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12180 /* If window start is on a continuation line... Window start may be
12181 < BEGV in case there's invisible text at the start of the
12182 buffer (M-x rmail, for example). */
12183 if (CHARPOS (start_pos) > BEGV
12184 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12186 struct it it;
12187 struct glyph_row *row;
12189 /* Handle the case that the window start is out of range. */
12190 if (CHARPOS (start_pos) < BEGV)
12191 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12192 else if (CHARPOS (start_pos) > ZV)
12193 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12195 /* Find the start of the continued line. This should be fast
12196 because scan_buffer is fast (newline cache). */
12197 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12198 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12199 row, DEFAULT_FACE_ID);
12200 reseat_at_previous_visible_line_start (&it);
12202 /* If the line start is "too far" away from the window start,
12203 say it takes too much time to compute a new window start. */
12204 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12205 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12207 int min_distance, distance;
12209 /* Move forward by display lines to find the new window
12210 start. If window width was enlarged, the new start can
12211 be expected to be > the old start. If window width was
12212 decreased, the new window start will be < the old start.
12213 So, we're looking for the display line start with the
12214 minimum distance from the old window start. */
12215 pos = it.current.pos;
12216 min_distance = INFINITY;
12217 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12218 distance < min_distance)
12220 min_distance = distance;
12221 pos = it.current.pos;
12222 move_it_by_lines (&it, 1, 0);
12225 /* Set the window start there. */
12226 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12227 window_start_changed_p = 1;
12231 return window_start_changed_p;
12235 /* Try cursor movement in case text has not changed in window WINDOW,
12236 with window start STARTP. Value is
12238 CURSOR_MOVEMENT_SUCCESS if successful
12240 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12242 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12243 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12244 we want to scroll as if scroll-step were set to 1. See the code.
12246 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12247 which case we have to abort this redisplay, and adjust matrices
12248 first. */
12250 enum
12252 CURSOR_MOVEMENT_SUCCESS,
12253 CURSOR_MOVEMENT_CANNOT_BE_USED,
12254 CURSOR_MOVEMENT_MUST_SCROLL,
12255 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12258 static int
12259 try_cursor_movement (window, startp, scroll_step)
12260 Lisp_Object window;
12261 struct text_pos startp;
12262 int *scroll_step;
12264 struct window *w = XWINDOW (window);
12265 struct frame *f = XFRAME (w->frame);
12266 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12268 #if GLYPH_DEBUG
12269 if (inhibit_try_cursor_movement)
12270 return rc;
12271 #endif
12273 /* Handle case where text has not changed, only point, and it has
12274 not moved off the frame. */
12275 if (/* Point may be in this window. */
12276 PT >= CHARPOS (startp)
12277 /* Selective display hasn't changed. */
12278 && !current_buffer->clip_changed
12279 /* Function force-mode-line-update is used to force a thorough
12280 redisplay. It sets either windows_or_buffers_changed or
12281 update_mode_lines. So don't take a shortcut here for these
12282 cases. */
12283 && !update_mode_lines
12284 && !windows_or_buffers_changed
12285 && !cursor_type_changed
12286 /* Can't use this case if highlighting a region. When a
12287 region exists, cursor movement has to do more than just
12288 set the cursor. */
12289 && !(!NILP (Vtransient_mark_mode)
12290 && !NILP (current_buffer->mark_active))
12291 && NILP (w->region_showing)
12292 && NILP (Vshow_trailing_whitespace)
12293 /* Right after splitting windows, last_point may be nil. */
12294 && INTEGERP (w->last_point)
12295 /* This code is not used for mini-buffer for the sake of the case
12296 of redisplaying to replace an echo area message; since in
12297 that case the mini-buffer contents per se are usually
12298 unchanged. This code is of no real use in the mini-buffer
12299 since the handling of this_line_start_pos, etc., in redisplay
12300 handles the same cases. */
12301 && !EQ (window, minibuf_window)
12302 /* When splitting windows or for new windows, it happens that
12303 redisplay is called with a nil window_end_vpos or one being
12304 larger than the window. This should really be fixed in
12305 window.c. I don't have this on my list, now, so we do
12306 approximately the same as the old redisplay code. --gerd. */
12307 && INTEGERP (w->window_end_vpos)
12308 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12309 && (FRAME_WINDOW_P (f)
12310 || !overlay_arrow_in_current_buffer_p ()))
12312 int this_scroll_margin, top_scroll_margin;
12313 struct glyph_row *row = NULL;
12315 #if GLYPH_DEBUG
12316 debug_method_add (w, "cursor movement");
12317 #endif
12319 /* Scroll if point within this distance from the top or bottom
12320 of the window. This is a pixel value. */
12321 this_scroll_margin = max (0, scroll_margin);
12322 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12323 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12325 top_scroll_margin = this_scroll_margin;
12326 if (WINDOW_WANTS_HEADER_LINE_P (w))
12327 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12329 /* Start with the row the cursor was displayed during the last
12330 not paused redisplay. Give up if that row is not valid. */
12331 if (w->last_cursor.vpos < 0
12332 || w->last_cursor.vpos >= w->current_matrix->nrows)
12333 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12334 else
12336 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12337 if (row->mode_line_p)
12338 ++row;
12339 if (!row->enabled_p)
12340 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12343 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12345 int scroll_p = 0;
12346 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12348 if (PT > XFASTINT (w->last_point))
12350 /* Point has moved forward. */
12351 while (MATRIX_ROW_END_CHARPOS (row) < PT
12352 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12354 xassert (row->enabled_p);
12355 ++row;
12358 /* The end position of a row equals the start position
12359 of the next row. If PT is there, we would rather
12360 display it in the next line. */
12361 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12362 && MATRIX_ROW_END_CHARPOS (row) == PT
12363 && !cursor_row_p (w, row))
12364 ++row;
12366 /* If within the scroll margin, scroll. Note that
12367 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12368 the next line would be drawn, and that
12369 this_scroll_margin can be zero. */
12370 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12371 || PT > MATRIX_ROW_END_CHARPOS (row)
12372 /* Line is completely visible last line in window
12373 and PT is to be set in the next line. */
12374 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12375 && PT == MATRIX_ROW_END_CHARPOS (row)
12376 && !row->ends_at_zv_p
12377 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12378 scroll_p = 1;
12380 else if (PT < XFASTINT (w->last_point))
12382 /* Cursor has to be moved backward. Note that PT >=
12383 CHARPOS (startp) because of the outer if-statement. */
12384 while (!row->mode_line_p
12385 && (MATRIX_ROW_START_CHARPOS (row) > PT
12386 || (MATRIX_ROW_START_CHARPOS (row) == PT
12387 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12388 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12389 row > w->current_matrix->rows
12390 && (row-1)->ends_in_newline_from_string_p))))
12391 && (row->y > top_scroll_margin
12392 || CHARPOS (startp) == BEGV))
12394 xassert (row->enabled_p);
12395 --row;
12398 /* Consider the following case: Window starts at BEGV,
12399 there is invisible, intangible text at BEGV, so that
12400 display starts at some point START > BEGV. It can
12401 happen that we are called with PT somewhere between
12402 BEGV and START. Try to handle that case. */
12403 if (row < w->current_matrix->rows
12404 || row->mode_line_p)
12406 row = w->current_matrix->rows;
12407 if (row->mode_line_p)
12408 ++row;
12411 /* Due to newlines in overlay strings, we may have to
12412 skip forward over overlay strings. */
12413 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12414 && MATRIX_ROW_END_CHARPOS (row) == PT
12415 && !cursor_row_p (w, row))
12416 ++row;
12418 /* If within the scroll margin, scroll. */
12419 if (row->y < top_scroll_margin
12420 && CHARPOS (startp) != BEGV)
12421 scroll_p = 1;
12423 else
12425 /* Cursor did not move. So don't scroll even if cursor line
12426 is partially visible, as it was so before. */
12427 rc = CURSOR_MOVEMENT_SUCCESS;
12430 if (PT < MATRIX_ROW_START_CHARPOS (row)
12431 || PT > MATRIX_ROW_END_CHARPOS (row))
12433 /* if PT is not in the glyph row, give up. */
12434 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12436 else if (rc != CURSOR_MOVEMENT_SUCCESS
12437 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12438 && make_cursor_line_fully_visible_p)
12440 if (PT == MATRIX_ROW_END_CHARPOS (row)
12441 && !row->ends_at_zv_p
12442 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12443 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12444 else if (row->height > window_box_height (w))
12446 /* If we end up in a partially visible line, let's
12447 make it fully visible, except when it's taller
12448 than the window, in which case we can't do much
12449 about it. */
12450 *scroll_step = 1;
12451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12453 else
12455 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12456 if (!cursor_row_fully_visible_p (w, 0, 1))
12457 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12458 else
12459 rc = CURSOR_MOVEMENT_SUCCESS;
12462 else if (scroll_p)
12463 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12464 else
12466 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12467 rc = CURSOR_MOVEMENT_SUCCESS;
12472 return rc;
12475 void
12476 set_vertical_scroll_bar (w)
12477 struct window *w;
12479 int start, end, whole;
12481 /* Calculate the start and end positions for the current window.
12482 At some point, it would be nice to choose between scrollbars
12483 which reflect the whole buffer size, with special markers
12484 indicating narrowing, and scrollbars which reflect only the
12485 visible region.
12487 Note that mini-buffers sometimes aren't displaying any text. */
12488 if (!MINI_WINDOW_P (w)
12489 || (w == XWINDOW (minibuf_window)
12490 && NILP (echo_area_buffer[0])))
12492 struct buffer *buf = XBUFFER (w->buffer);
12493 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12494 start = marker_position (w->start) - BUF_BEGV (buf);
12495 /* I don't think this is guaranteed to be right. For the
12496 moment, we'll pretend it is. */
12497 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12499 if (end < start)
12500 end = start;
12501 if (whole < (end - start))
12502 whole = end - start;
12504 else
12505 start = end = whole = 0;
12507 /* Indicate what this scroll bar ought to be displaying now. */
12508 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12509 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12510 (w, end - start, whole, start);
12514 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12515 selected_window is redisplayed.
12517 We can return without actually redisplaying the window if
12518 fonts_changed_p is nonzero. In that case, redisplay_internal will
12519 retry. */
12521 static void
12522 redisplay_window (window, just_this_one_p)
12523 Lisp_Object window;
12524 int just_this_one_p;
12526 struct window *w = XWINDOW (window);
12527 struct frame *f = XFRAME (w->frame);
12528 struct buffer *buffer = XBUFFER (w->buffer);
12529 struct buffer *old = current_buffer;
12530 struct text_pos lpoint, opoint, startp;
12531 int update_mode_line;
12532 int tem;
12533 struct it it;
12534 /* Record it now because it's overwritten. */
12535 int current_matrix_up_to_date_p = 0;
12536 int used_current_matrix_p = 0;
12537 /* This is less strict than current_matrix_up_to_date_p.
12538 It indictes that the buffer contents and narrowing are unchanged. */
12539 int buffer_unchanged_p = 0;
12540 int temp_scroll_step = 0;
12541 int count = SPECPDL_INDEX ();
12542 int rc;
12543 int centering_position = -1;
12544 int last_line_misfit = 0;
12546 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12547 opoint = lpoint;
12549 /* W must be a leaf window here. */
12550 xassert (!NILP (w->buffer));
12551 #if GLYPH_DEBUG
12552 *w->desired_matrix->method = 0;
12553 #endif
12555 specbind (Qinhibit_point_motion_hooks, Qt);
12557 reconsider_clip_changes (w, buffer);
12559 /* Has the mode line to be updated? */
12560 update_mode_line = (!NILP (w->update_mode_line)
12561 || update_mode_lines
12562 || buffer->clip_changed
12563 || buffer->prevent_redisplay_optimizations_p);
12565 if (MINI_WINDOW_P (w))
12567 if (w == XWINDOW (echo_area_window)
12568 && !NILP (echo_area_buffer[0]))
12570 if (update_mode_line)
12571 /* We may have to update a tty frame's menu bar or a
12572 tool-bar. Example `M-x C-h C-h C-g'. */
12573 goto finish_menu_bars;
12574 else
12575 /* We've already displayed the echo area glyphs in this window. */
12576 goto finish_scroll_bars;
12578 else if ((w != XWINDOW (minibuf_window)
12579 || minibuf_level == 0)
12580 /* When buffer is nonempty, redisplay window normally. */
12581 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12582 /* Quail displays non-mini buffers in minibuffer window.
12583 In that case, redisplay the window normally. */
12584 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12586 /* W is a mini-buffer window, but it's not active, so clear
12587 it. */
12588 int yb = window_text_bottom_y (w);
12589 struct glyph_row *row;
12590 int y;
12592 for (y = 0, row = w->desired_matrix->rows;
12593 y < yb;
12594 y += row->height, ++row)
12595 blank_row (w, row, y);
12596 goto finish_scroll_bars;
12599 clear_glyph_matrix (w->desired_matrix);
12602 /* Otherwise set up data on this window; select its buffer and point
12603 value. */
12604 /* Really select the buffer, for the sake of buffer-local
12605 variables. */
12606 set_buffer_internal_1 (XBUFFER (w->buffer));
12607 SET_TEXT_POS (opoint, PT, PT_BYTE);
12609 current_matrix_up_to_date_p
12610 = (!NILP (w->window_end_valid)
12611 && !current_buffer->clip_changed
12612 && !current_buffer->prevent_redisplay_optimizations_p
12613 && XFASTINT (w->last_modified) >= MODIFF
12614 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12616 buffer_unchanged_p
12617 = (!NILP (w->window_end_valid)
12618 && !current_buffer->clip_changed
12619 && XFASTINT (w->last_modified) >= MODIFF
12620 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12622 /* When windows_or_buffers_changed is non-zero, we can't rely on
12623 the window end being valid, so set it to nil there. */
12624 if (windows_or_buffers_changed)
12626 /* If window starts on a continuation line, maybe adjust the
12627 window start in case the window's width changed. */
12628 if (XMARKER (w->start)->buffer == current_buffer)
12629 compute_window_start_on_continuation_line (w);
12631 w->window_end_valid = Qnil;
12634 /* Some sanity checks. */
12635 CHECK_WINDOW_END (w);
12636 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12637 abort ();
12638 if (BYTEPOS (opoint) < CHARPOS (opoint))
12639 abort ();
12641 /* If %c is in mode line, update it if needed. */
12642 if (!NILP (w->column_number_displayed)
12643 /* This alternative quickly identifies a common case
12644 where no change is needed. */
12645 && !(PT == XFASTINT (w->last_point)
12646 && XFASTINT (w->last_modified) >= MODIFF
12647 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12648 && (XFASTINT (w->column_number_displayed)
12649 != (int) current_column ())) /* iftc */
12650 update_mode_line = 1;
12652 /* Count number of windows showing the selected buffer. An indirect
12653 buffer counts as its base buffer. */
12654 if (!just_this_one_p)
12656 struct buffer *current_base, *window_base;
12657 current_base = current_buffer;
12658 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12659 if (current_base->base_buffer)
12660 current_base = current_base->base_buffer;
12661 if (window_base->base_buffer)
12662 window_base = window_base->base_buffer;
12663 if (current_base == window_base)
12664 buffer_shared++;
12667 /* Point refers normally to the selected window. For any other
12668 window, set up appropriate value. */
12669 if (!EQ (window, selected_window))
12671 int new_pt = XMARKER (w->pointm)->charpos;
12672 int new_pt_byte = marker_byte_position (w->pointm);
12673 if (new_pt < BEGV)
12675 new_pt = BEGV;
12676 new_pt_byte = BEGV_BYTE;
12677 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12679 else if (new_pt > (ZV - 1))
12681 new_pt = ZV;
12682 new_pt_byte = ZV_BYTE;
12683 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12686 /* We don't use SET_PT so that the point-motion hooks don't run. */
12687 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12690 /* If any of the character widths specified in the display table
12691 have changed, invalidate the width run cache. It's true that
12692 this may be a bit late to catch such changes, but the rest of
12693 redisplay goes (non-fatally) haywire when the display table is
12694 changed, so why should we worry about doing any better? */
12695 if (current_buffer->width_run_cache)
12697 struct Lisp_Char_Table *disptab = buffer_display_table ();
12699 if (! disptab_matches_widthtab (disptab,
12700 XVECTOR (current_buffer->width_table)))
12702 invalidate_region_cache (current_buffer,
12703 current_buffer->width_run_cache,
12704 BEG, Z);
12705 recompute_width_table (current_buffer, disptab);
12709 /* If window-start is screwed up, choose a new one. */
12710 if (XMARKER (w->start)->buffer != current_buffer)
12711 goto recenter;
12713 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12715 /* If someone specified a new starting point but did not insist,
12716 check whether it can be used. */
12717 if (!NILP (w->optional_new_start)
12718 && CHARPOS (startp) >= BEGV
12719 && CHARPOS (startp) <= ZV)
12721 w->optional_new_start = Qnil;
12722 start_display (&it, w, startp);
12723 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12724 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12725 if (IT_CHARPOS (it) == PT)
12726 w->force_start = Qt;
12727 /* IT may overshoot PT if text at PT is invisible. */
12728 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12729 w->force_start = Qt;
12732 /* Handle case where place to start displaying has been specified,
12733 unless the specified location is outside the accessible range. */
12734 if (!NILP (w->force_start)
12735 || w->frozen_window_start_p)
12737 /* We set this later on if we have to adjust point. */
12738 int new_vpos = -1;
12739 int val;
12741 w->force_start = Qnil;
12742 w->vscroll = 0;
12743 w->window_end_valid = Qnil;
12745 /* Forget any recorded base line for line number display. */
12746 if (!buffer_unchanged_p)
12747 w->base_line_number = Qnil;
12749 /* Redisplay the mode line. Select the buffer properly for that.
12750 Also, run the hook window-scroll-functions
12751 because we have scrolled. */
12752 /* Note, we do this after clearing force_start because
12753 if there's an error, it is better to forget about force_start
12754 than to get into an infinite loop calling the hook functions
12755 and having them get more errors. */
12756 if (!update_mode_line
12757 || ! NILP (Vwindow_scroll_functions))
12759 update_mode_line = 1;
12760 w->update_mode_line = Qt;
12761 startp = run_window_scroll_functions (window, startp);
12764 w->last_modified = make_number (0);
12765 w->last_overlay_modified = make_number (0);
12766 if (CHARPOS (startp) < BEGV)
12767 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12768 else if (CHARPOS (startp) > ZV)
12769 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12771 /* Redisplay, then check if cursor has been set during the
12772 redisplay. Give up if new fonts were loaded. */
12773 val = try_window (window, startp, 1);
12774 if (!val)
12776 w->force_start = Qt;
12777 clear_glyph_matrix (w->desired_matrix);
12778 goto need_larger_matrices;
12780 /* Point was outside the scroll margins. */
12781 if (val < 0)
12782 new_vpos = window_box_height (w) / 2;
12784 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12786 /* If point does not appear, try to move point so it does
12787 appear. The desired matrix has been built above, so we
12788 can use it here. */
12789 new_vpos = window_box_height (w) / 2;
12792 if (!cursor_row_fully_visible_p (w, 0, 0))
12794 /* Point does appear, but on a line partly visible at end of window.
12795 Move it back to a fully-visible line. */
12796 new_vpos = window_box_height (w);
12799 /* If we need to move point for either of the above reasons,
12800 now actually do it. */
12801 if (new_vpos >= 0)
12803 struct glyph_row *row;
12805 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12806 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12807 ++row;
12809 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12810 MATRIX_ROW_START_BYTEPOS (row));
12812 if (w != XWINDOW (selected_window))
12813 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12814 else if (current_buffer == old)
12815 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12817 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12819 /* If we are highlighting the region, then we just changed
12820 the region, so redisplay to show it. */
12821 if (!NILP (Vtransient_mark_mode)
12822 && !NILP (current_buffer->mark_active))
12824 clear_glyph_matrix (w->desired_matrix);
12825 if (!try_window (window, startp, 0))
12826 goto need_larger_matrices;
12830 #if GLYPH_DEBUG
12831 debug_method_add (w, "forced window start");
12832 #endif
12833 goto done;
12836 /* Handle case where text has not changed, only point, and it has
12837 not moved off the frame, and we are not retrying after hscroll.
12838 (current_matrix_up_to_date_p is nonzero when retrying.) */
12839 if (current_matrix_up_to_date_p
12840 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12841 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12843 switch (rc)
12845 case CURSOR_MOVEMENT_SUCCESS:
12846 used_current_matrix_p = 1;
12847 goto done;
12849 #if 0 /* try_cursor_movement never returns this value. */
12850 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12851 goto need_larger_matrices;
12852 #endif
12854 case CURSOR_MOVEMENT_MUST_SCROLL:
12855 goto try_to_scroll;
12857 default:
12858 abort ();
12861 /* If current starting point was originally the beginning of a line
12862 but no longer is, find a new starting point. */
12863 else if (!NILP (w->start_at_line_beg)
12864 && !(CHARPOS (startp) <= BEGV
12865 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12867 #if GLYPH_DEBUG
12868 debug_method_add (w, "recenter 1");
12869 #endif
12870 goto recenter;
12873 /* Try scrolling with try_window_id. Value is > 0 if update has
12874 been done, it is -1 if we know that the same window start will
12875 not work. It is 0 if unsuccessful for some other reason. */
12876 else if ((tem = try_window_id (w)) != 0)
12878 #if GLYPH_DEBUG
12879 debug_method_add (w, "try_window_id %d", tem);
12880 #endif
12882 if (fonts_changed_p)
12883 goto need_larger_matrices;
12884 if (tem > 0)
12885 goto done;
12887 /* Otherwise try_window_id has returned -1 which means that we
12888 don't want the alternative below this comment to execute. */
12890 else if (CHARPOS (startp) >= BEGV
12891 && CHARPOS (startp) <= ZV
12892 && PT >= CHARPOS (startp)
12893 && (CHARPOS (startp) < ZV
12894 /* Avoid starting at end of buffer. */
12895 || CHARPOS (startp) == BEGV
12896 || (XFASTINT (w->last_modified) >= MODIFF
12897 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12900 /* If first window line is a continuation line, and window start
12901 is inside the modified region, but the first change is before
12902 current window start, we must select a new window start.*/
12903 if (NILP (w->start_at_line_beg)
12904 && CHARPOS (startp) > BEGV)
12906 /* Make sure beg_unchanged and end_unchanged are up to date.
12907 Do it only if buffer has really changed. This may or may
12908 not have been done by try_window_id (see which) already. */
12909 if (MODIFF > SAVE_MODIFF
12910 /* This seems to happen sometimes after saving a buffer. */
12911 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12913 if (GPT - BEG < BEG_UNCHANGED)
12914 BEG_UNCHANGED = GPT - BEG;
12915 if (Z - GPT < END_UNCHANGED)
12916 END_UNCHANGED = Z - GPT;
12919 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
12920 && CHARPOS (startp) <= Z - END_UNCHANGED)
12922 /* There doesn't seems to be a simple way to find a new
12923 window start that is near the old window start, so
12924 we just recenter. */
12925 goto recenter;
12929 #if GLYPH_DEBUG
12930 debug_method_add (w, "same window start");
12931 #endif
12933 /* Try to redisplay starting at same place as before.
12934 If point has not moved off frame, accept the results. */
12935 if (!current_matrix_up_to_date_p
12936 /* Don't use try_window_reusing_current_matrix in this case
12937 because a window scroll function can have changed the
12938 buffer. */
12939 || !NILP (Vwindow_scroll_functions)
12940 || MINI_WINDOW_P (w)
12941 || !(used_current_matrix_p
12942 = try_window_reusing_current_matrix (w)))
12944 IF_DEBUG (debug_method_add (w, "1"));
12945 if (try_window (window, startp, 1) < 0)
12946 /* -1 means we need to scroll.
12947 0 means we need new matrices, but fonts_changed_p
12948 is set in that case, so we will detect it below. */
12949 goto try_to_scroll;
12952 if (fonts_changed_p)
12953 goto need_larger_matrices;
12955 if (w->cursor.vpos >= 0)
12957 if (!just_this_one_p
12958 || current_buffer->clip_changed
12959 || BEG_UNCHANGED < CHARPOS (startp))
12960 /* Forget any recorded base line for line number display. */
12961 w->base_line_number = Qnil;
12963 if (!cursor_row_fully_visible_p (w, 1, 0))
12965 clear_glyph_matrix (w->desired_matrix);
12966 last_line_misfit = 1;
12968 /* Drop through and scroll. */
12969 else
12970 goto done;
12972 else
12973 clear_glyph_matrix (w->desired_matrix);
12976 try_to_scroll:
12978 w->last_modified = make_number (0);
12979 w->last_overlay_modified = make_number (0);
12981 /* Redisplay the mode line. Select the buffer properly for that. */
12982 if (!update_mode_line)
12984 update_mode_line = 1;
12985 w->update_mode_line = Qt;
12988 /* Try to scroll by specified few lines. */
12989 if ((scroll_conservatively
12990 || scroll_step
12991 || temp_scroll_step
12992 || NUMBERP (current_buffer->scroll_up_aggressively)
12993 || NUMBERP (current_buffer->scroll_down_aggressively))
12994 && !current_buffer->clip_changed
12995 && CHARPOS (startp) >= BEGV
12996 && CHARPOS (startp) <= ZV)
12998 /* The function returns -1 if new fonts were loaded, 1 if
12999 successful, 0 if not successful. */
13000 int rc = try_scrolling (window, just_this_one_p,
13001 scroll_conservatively,
13002 scroll_step,
13003 temp_scroll_step, last_line_misfit);
13004 switch (rc)
13006 case SCROLLING_SUCCESS:
13007 goto done;
13009 case SCROLLING_NEED_LARGER_MATRICES:
13010 goto need_larger_matrices;
13012 case SCROLLING_FAILED:
13013 break;
13015 default:
13016 abort ();
13020 /* Finally, just choose place to start which centers point */
13022 recenter:
13023 if (centering_position < 0)
13024 centering_position = window_box_height (w) / 2;
13026 #if GLYPH_DEBUG
13027 debug_method_add (w, "recenter");
13028 #endif
13030 /* w->vscroll = 0; */
13032 /* Forget any previously recorded base line for line number display. */
13033 if (!buffer_unchanged_p)
13034 w->base_line_number = Qnil;
13036 /* Move backward half the height of the window. */
13037 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13038 it.current_y = it.last_visible_y;
13039 move_it_vertically_backward (&it, centering_position);
13040 xassert (IT_CHARPOS (it) >= BEGV);
13042 /* The function move_it_vertically_backward may move over more
13043 than the specified y-distance. If it->w is small, e.g. a
13044 mini-buffer window, we may end up in front of the window's
13045 display area. Start displaying at the start of the line
13046 containing PT in this case. */
13047 if (it.current_y <= 0)
13049 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13050 move_it_vertically_backward (&it, 0);
13051 #if 0
13052 /* I think this assert is bogus if buffer contains
13053 invisible text or images. KFS. */
13054 xassert (IT_CHARPOS (it) <= PT);
13055 #endif
13056 it.current_y = 0;
13059 it.current_x = it.hpos = 0;
13061 /* Set startp here explicitly in case that helps avoid an infinite loop
13062 in case the window-scroll-functions functions get errors. */
13063 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13065 /* Run scroll hooks. */
13066 startp = run_window_scroll_functions (window, it.current.pos);
13068 /* Redisplay the window. */
13069 if (!current_matrix_up_to_date_p
13070 || windows_or_buffers_changed
13071 || cursor_type_changed
13072 /* Don't use try_window_reusing_current_matrix in this case
13073 because it can have changed the buffer. */
13074 || !NILP (Vwindow_scroll_functions)
13075 || !just_this_one_p
13076 || MINI_WINDOW_P (w)
13077 || !(used_current_matrix_p
13078 = try_window_reusing_current_matrix (w)))
13079 try_window (window, startp, 0);
13081 /* If new fonts have been loaded (due to fontsets), give up. We
13082 have to start a new redisplay since we need to re-adjust glyph
13083 matrices. */
13084 if (fonts_changed_p)
13085 goto need_larger_matrices;
13087 /* If cursor did not appear assume that the middle of the window is
13088 in the first line of the window. Do it again with the next line.
13089 (Imagine a window of height 100, displaying two lines of height
13090 60. Moving back 50 from it->last_visible_y will end in the first
13091 line.) */
13092 if (w->cursor.vpos < 0)
13094 if (!NILP (w->window_end_valid)
13095 && PT >= Z - XFASTINT (w->window_end_pos))
13097 clear_glyph_matrix (w->desired_matrix);
13098 move_it_by_lines (&it, 1, 0);
13099 try_window (window, it.current.pos, 0);
13101 else if (PT < IT_CHARPOS (it))
13103 clear_glyph_matrix (w->desired_matrix);
13104 move_it_by_lines (&it, -1, 0);
13105 try_window (window, it.current.pos, 0);
13107 else
13109 /* Not much we can do about it. */
13113 /* Consider the following case: Window starts at BEGV, there is
13114 invisible, intangible text at BEGV, so that display starts at
13115 some point START > BEGV. It can happen that we are called with
13116 PT somewhere between BEGV and START. Try to handle that case. */
13117 if (w->cursor.vpos < 0)
13119 struct glyph_row *row = w->current_matrix->rows;
13120 if (row->mode_line_p)
13121 ++row;
13122 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13125 if (!cursor_row_fully_visible_p (w, 0, 0))
13127 /* If vscroll is enabled, disable it and try again. */
13128 if (w->vscroll)
13130 w->vscroll = 0;
13131 clear_glyph_matrix (w->desired_matrix);
13132 goto recenter;
13135 /* If centering point failed to make the whole line visible,
13136 put point at the top instead. That has to make the whole line
13137 visible, if it can be done. */
13138 if (centering_position == 0)
13139 goto done;
13141 clear_glyph_matrix (w->desired_matrix);
13142 centering_position = 0;
13143 goto recenter;
13146 done:
13148 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13149 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13150 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13151 ? Qt : Qnil);
13153 /* Display the mode line, if we must. */
13154 if ((update_mode_line
13155 /* If window not full width, must redo its mode line
13156 if (a) the window to its side is being redone and
13157 (b) we do a frame-based redisplay. This is a consequence
13158 of how inverted lines are drawn in frame-based redisplay. */
13159 || (!just_this_one_p
13160 && !FRAME_WINDOW_P (f)
13161 && !WINDOW_FULL_WIDTH_P (w))
13162 /* Line number to display. */
13163 || INTEGERP (w->base_line_pos)
13164 /* Column number is displayed and different from the one displayed. */
13165 || (!NILP (w->column_number_displayed)
13166 && (XFASTINT (w->column_number_displayed)
13167 != (int) current_column ()))) /* iftc */
13168 /* This means that the window has a mode line. */
13169 && (WINDOW_WANTS_MODELINE_P (w)
13170 || WINDOW_WANTS_HEADER_LINE_P (w)))
13172 display_mode_lines (w);
13174 /* If mode line height has changed, arrange for a thorough
13175 immediate redisplay using the correct mode line height. */
13176 if (WINDOW_WANTS_MODELINE_P (w)
13177 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13179 fonts_changed_p = 1;
13180 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13181 = DESIRED_MODE_LINE_HEIGHT (w);
13184 /* If top line height has changed, arrange for a thorough
13185 immediate redisplay using the correct mode line height. */
13186 if (WINDOW_WANTS_HEADER_LINE_P (w)
13187 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13189 fonts_changed_p = 1;
13190 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13191 = DESIRED_HEADER_LINE_HEIGHT (w);
13194 if (fonts_changed_p)
13195 goto need_larger_matrices;
13198 if (!line_number_displayed
13199 && !BUFFERP (w->base_line_pos))
13201 w->base_line_pos = Qnil;
13202 w->base_line_number = Qnil;
13205 finish_menu_bars:
13207 /* When we reach a frame's selected window, redo the frame's menu bar. */
13208 if (update_mode_line
13209 && EQ (FRAME_SELECTED_WINDOW (f), window))
13211 int redisplay_menu_p = 0;
13212 int redisplay_tool_bar_p = 0;
13214 if (FRAME_WINDOW_P (f))
13216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13217 || defined (USE_GTK)
13218 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13219 #else
13220 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13221 #endif
13223 else
13224 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13226 if (redisplay_menu_p)
13227 display_menu_bar (w);
13229 #ifdef HAVE_WINDOW_SYSTEM
13230 if (FRAME_WINDOW_P (f))
13232 #ifdef USE_GTK
13233 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13234 #else
13235 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13236 && (FRAME_TOOL_BAR_LINES (f) > 0
13237 || auto_resize_tool_bars_p);
13238 #endif
13240 if (redisplay_tool_bar_p)
13241 redisplay_tool_bar (f);
13243 #endif
13246 #ifdef HAVE_WINDOW_SYSTEM
13247 if (FRAME_WINDOW_P (f)
13248 && update_window_fringes (w, (just_this_one_p
13249 || (!used_current_matrix_p && !overlay_arrow_seen)
13250 || w->pseudo_window_p)))
13252 update_begin (f);
13253 BLOCK_INPUT;
13254 if (draw_window_fringes (w, 1))
13255 x_draw_vertical_border (w);
13256 UNBLOCK_INPUT;
13257 update_end (f);
13259 #endif /* HAVE_WINDOW_SYSTEM */
13261 /* We go to this label, with fonts_changed_p nonzero,
13262 if it is necessary to try again using larger glyph matrices.
13263 We have to redeem the scroll bar even in this case,
13264 because the loop in redisplay_internal expects that. */
13265 need_larger_matrices:
13267 finish_scroll_bars:
13269 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13271 /* Set the thumb's position and size. */
13272 set_vertical_scroll_bar (w);
13274 /* Note that we actually used the scroll bar attached to this
13275 window, so it shouldn't be deleted at the end of redisplay. */
13276 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13277 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13280 /* Restore current_buffer and value of point in it. */
13281 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13282 set_buffer_internal_1 (old);
13283 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13285 unbind_to (count, Qnil);
13289 /* Build the complete desired matrix of WINDOW with a window start
13290 buffer position POS.
13292 Value is 1 if successful. It is zero if fonts were loaded during
13293 redisplay which makes re-adjusting glyph matrices necessary, and -1
13294 if point would appear in the scroll margins.
13295 (We check that only if CHECK_MARGINS is nonzero. */
13298 try_window (window, pos, check_margins)
13299 Lisp_Object window;
13300 struct text_pos pos;
13301 int check_margins;
13303 struct window *w = XWINDOW (window);
13304 struct it it;
13305 struct glyph_row *last_text_row = NULL;
13307 /* Make POS the new window start. */
13308 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13310 /* Mark cursor position as unknown. No overlay arrow seen. */
13311 w->cursor.vpos = -1;
13312 overlay_arrow_seen = 0;
13314 /* Initialize iterator and info to start at POS. */
13315 start_display (&it, w, pos);
13317 /* Display all lines of W. */
13318 while (it.current_y < it.last_visible_y)
13320 if (display_line (&it))
13321 last_text_row = it.glyph_row - 1;
13322 if (fonts_changed_p)
13323 return 0;
13326 /* Don't let the cursor end in the scroll margins. */
13327 if (check_margins
13328 && !MINI_WINDOW_P (w))
13330 int this_scroll_margin;
13332 this_scroll_margin = max (0, scroll_margin);
13333 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13334 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13336 if ((w->cursor.y < this_scroll_margin
13337 && CHARPOS (pos) > BEGV
13338 && IT_CHARPOS (it) < ZV)
13339 /* rms: considering make_cursor_line_fully_visible_p here
13340 seems to give wrong results. We don't want to recenter
13341 when the last line is partly visible, we want to allow
13342 that case to be handled in the usual way. */
13343 || (w->cursor.y + 1) > it.last_visible_y)
13345 w->cursor.vpos = -1;
13346 clear_glyph_matrix (w->desired_matrix);
13347 return -1;
13351 /* If bottom moved off end of frame, change mode line percentage. */
13352 if (XFASTINT (w->window_end_pos) <= 0
13353 && Z != IT_CHARPOS (it))
13354 w->update_mode_line = Qt;
13356 /* Set window_end_pos to the offset of the last character displayed
13357 on the window from the end of current_buffer. Set
13358 window_end_vpos to its row number. */
13359 if (last_text_row)
13361 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13362 w->window_end_bytepos
13363 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13364 w->window_end_pos
13365 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13366 w->window_end_vpos
13367 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13368 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13369 ->displays_text_p);
13371 else
13373 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13374 w->window_end_pos = make_number (Z - ZV);
13375 w->window_end_vpos = make_number (0);
13378 /* But that is not valid info until redisplay finishes. */
13379 w->window_end_valid = Qnil;
13380 return 1;
13385 /************************************************************************
13386 Window redisplay reusing current matrix when buffer has not changed
13387 ************************************************************************/
13389 /* Try redisplay of window W showing an unchanged buffer with a
13390 different window start than the last time it was displayed by
13391 reusing its current matrix. Value is non-zero if successful.
13392 W->start is the new window start. */
13394 static int
13395 try_window_reusing_current_matrix (w)
13396 struct window *w;
13398 struct frame *f = XFRAME (w->frame);
13399 struct glyph_row *row, *bottom_row;
13400 struct it it;
13401 struct run run;
13402 struct text_pos start, new_start;
13403 int nrows_scrolled, i;
13404 struct glyph_row *last_text_row;
13405 struct glyph_row *last_reused_text_row;
13406 struct glyph_row *start_row;
13407 int start_vpos, min_y, max_y;
13409 #if GLYPH_DEBUG
13410 if (inhibit_try_window_reusing)
13411 return 0;
13412 #endif
13414 if (/* This function doesn't handle terminal frames. */
13415 !FRAME_WINDOW_P (f)
13416 /* Don't try to reuse the display if windows have been split
13417 or such. */
13418 || windows_or_buffers_changed
13419 || cursor_type_changed)
13420 return 0;
13422 /* Can't do this if region may have changed. */
13423 if ((!NILP (Vtransient_mark_mode)
13424 && !NILP (current_buffer->mark_active))
13425 || !NILP (w->region_showing)
13426 || !NILP (Vshow_trailing_whitespace))
13427 return 0;
13429 /* If top-line visibility has changed, give up. */
13430 if (WINDOW_WANTS_HEADER_LINE_P (w)
13431 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13432 return 0;
13434 /* Give up if old or new display is scrolled vertically. We could
13435 make this function handle this, but right now it doesn't. */
13436 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13437 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13438 return 0;
13440 /* The variable new_start now holds the new window start. The old
13441 start `start' can be determined from the current matrix. */
13442 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13443 start = start_row->start.pos;
13444 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13446 /* Clear the desired matrix for the display below. */
13447 clear_glyph_matrix (w->desired_matrix);
13449 if (CHARPOS (new_start) <= CHARPOS (start))
13451 int first_row_y;
13453 /* Don't use this method if the display starts with an ellipsis
13454 displayed for invisible text. It's not easy to handle that case
13455 below, and it's certainly not worth the effort since this is
13456 not a frequent case. */
13457 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13458 return 0;
13460 IF_DEBUG (debug_method_add (w, "twu1"));
13462 /* Display up to a row that can be reused. The variable
13463 last_text_row is set to the last row displayed that displays
13464 text. Note that it.vpos == 0 if or if not there is a
13465 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13466 start_display (&it, w, new_start);
13467 first_row_y = it.current_y;
13468 w->cursor.vpos = -1;
13469 last_text_row = last_reused_text_row = NULL;
13471 while (it.current_y < it.last_visible_y
13472 && !fonts_changed_p)
13474 /* If we have reached into the characters in the START row,
13475 that means the line boundaries have changed. So we
13476 can't start copying with the row START. Maybe it will
13477 work to start copying with the following row. */
13478 while (IT_CHARPOS (it) > CHARPOS (start))
13480 /* Advance to the next row as the "start". */
13481 start_row++;
13482 start = start_row->start.pos;
13483 /* If there are no more rows to try, or just one, give up. */
13484 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13485 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13486 || CHARPOS (start) == ZV)
13488 clear_glyph_matrix (w->desired_matrix);
13489 return 0;
13492 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13494 /* If we have reached alignment,
13495 we can copy the rest of the rows. */
13496 if (IT_CHARPOS (it) == CHARPOS (start))
13497 break;
13499 if (display_line (&it))
13500 last_text_row = it.glyph_row - 1;
13503 /* A value of current_y < last_visible_y means that we stopped
13504 at the previous window start, which in turn means that we
13505 have at least one reusable row. */
13506 if (it.current_y < it.last_visible_y)
13508 /* IT.vpos always starts from 0; it counts text lines. */
13509 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13511 /* Find PT if not already found in the lines displayed. */
13512 if (w->cursor.vpos < 0)
13514 int dy = it.current_y - start_row->y;
13516 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13517 row = row_containing_pos (w, PT, row, NULL, dy);
13518 if (row)
13519 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13520 dy, nrows_scrolled);
13521 else
13523 clear_glyph_matrix (w->desired_matrix);
13524 return 0;
13528 /* Scroll the display. Do it before the current matrix is
13529 changed. The problem here is that update has not yet
13530 run, i.e. part of the current matrix is not up to date.
13531 scroll_run_hook will clear the cursor, and use the
13532 current matrix to get the height of the row the cursor is
13533 in. */
13534 run.current_y = start_row->y;
13535 run.desired_y = it.current_y;
13536 run.height = it.last_visible_y - it.current_y;
13538 if (run.height > 0 && run.current_y != run.desired_y)
13540 update_begin (f);
13541 FRAME_RIF (f)->update_window_begin_hook (w);
13542 FRAME_RIF (f)->clear_window_mouse_face (w);
13543 FRAME_RIF (f)->scroll_run_hook (w, &run);
13544 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13545 update_end (f);
13548 /* Shift current matrix down by nrows_scrolled lines. */
13549 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13550 rotate_matrix (w->current_matrix,
13551 start_vpos,
13552 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13553 nrows_scrolled);
13555 /* Disable lines that must be updated. */
13556 for (i = 0; i < it.vpos; ++i)
13557 (start_row + i)->enabled_p = 0;
13559 /* Re-compute Y positions. */
13560 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13561 max_y = it.last_visible_y;
13562 for (row = start_row + nrows_scrolled;
13563 row < bottom_row;
13564 ++row)
13566 row->y = it.current_y;
13567 row->visible_height = row->height;
13569 if (row->y < min_y)
13570 row->visible_height -= min_y - row->y;
13571 if (row->y + row->height > max_y)
13572 row->visible_height -= row->y + row->height - max_y;
13573 row->redraw_fringe_bitmaps_p = 1;
13575 it.current_y += row->height;
13577 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13578 last_reused_text_row = row;
13579 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13580 break;
13583 /* Disable lines in the current matrix which are now
13584 below the window. */
13585 for (++row; row < bottom_row; ++row)
13586 row->enabled_p = row->mode_line_p = 0;
13589 /* Update window_end_pos etc.; last_reused_text_row is the last
13590 reused row from the current matrix containing text, if any.
13591 The value of last_text_row is the last displayed line
13592 containing text. */
13593 if (last_reused_text_row)
13595 w->window_end_bytepos
13596 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13597 w->window_end_pos
13598 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13599 w->window_end_vpos
13600 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13601 w->current_matrix));
13603 else if (last_text_row)
13605 w->window_end_bytepos
13606 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13607 w->window_end_pos
13608 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13609 w->window_end_vpos
13610 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13612 else
13614 /* This window must be completely empty. */
13615 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13616 w->window_end_pos = make_number (Z - ZV);
13617 w->window_end_vpos = make_number (0);
13619 w->window_end_valid = Qnil;
13621 /* Update hint: don't try scrolling again in update_window. */
13622 w->desired_matrix->no_scrolling_p = 1;
13624 #if GLYPH_DEBUG
13625 debug_method_add (w, "try_window_reusing_current_matrix 1");
13626 #endif
13627 return 1;
13629 else if (CHARPOS (new_start) > CHARPOS (start))
13631 struct glyph_row *pt_row, *row;
13632 struct glyph_row *first_reusable_row;
13633 struct glyph_row *first_row_to_display;
13634 int dy;
13635 int yb = window_text_bottom_y (w);
13637 /* Find the row starting at new_start, if there is one. Don't
13638 reuse a partially visible line at the end. */
13639 first_reusable_row = start_row;
13640 while (first_reusable_row->enabled_p
13641 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13642 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13643 < CHARPOS (new_start)))
13644 ++first_reusable_row;
13646 /* Give up if there is no row to reuse. */
13647 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13648 || !first_reusable_row->enabled_p
13649 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13650 != CHARPOS (new_start)))
13651 return 0;
13653 /* We can reuse fully visible rows beginning with
13654 first_reusable_row to the end of the window. Set
13655 first_row_to_display to the first row that cannot be reused.
13656 Set pt_row to the row containing point, if there is any. */
13657 pt_row = NULL;
13658 for (first_row_to_display = first_reusable_row;
13659 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13660 ++first_row_to_display)
13662 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13663 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13664 pt_row = first_row_to_display;
13667 /* Start displaying at the start of first_row_to_display. */
13668 xassert (first_row_to_display->y < yb);
13669 init_to_row_start (&it, w, first_row_to_display);
13671 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13672 - start_vpos);
13673 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13674 - nrows_scrolled);
13675 it.current_y = (first_row_to_display->y - first_reusable_row->y
13676 + WINDOW_HEADER_LINE_HEIGHT (w));
13678 /* Display lines beginning with first_row_to_display in the
13679 desired matrix. Set last_text_row to the last row displayed
13680 that displays text. */
13681 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13682 if (pt_row == NULL)
13683 w->cursor.vpos = -1;
13684 last_text_row = NULL;
13685 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13686 if (display_line (&it))
13687 last_text_row = it.glyph_row - 1;
13689 /* Give up If point isn't in a row displayed or reused. */
13690 if (w->cursor.vpos < 0)
13692 clear_glyph_matrix (w->desired_matrix);
13693 return 0;
13696 /* If point is in a reused row, adjust y and vpos of the cursor
13697 position. */
13698 if (pt_row)
13700 w->cursor.vpos -= nrows_scrolled;
13701 w->cursor.y -= first_reusable_row->y - start_row->y;
13704 /* Scroll the display. */
13705 run.current_y = first_reusable_row->y;
13706 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13707 run.height = it.last_visible_y - run.current_y;
13708 dy = run.current_y - run.desired_y;
13710 if (run.height)
13712 update_begin (f);
13713 FRAME_RIF (f)->update_window_begin_hook (w);
13714 FRAME_RIF (f)->clear_window_mouse_face (w);
13715 FRAME_RIF (f)->scroll_run_hook (w, &run);
13716 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13717 update_end (f);
13720 /* Adjust Y positions of reused rows. */
13721 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13722 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13723 max_y = it.last_visible_y;
13724 for (row = first_reusable_row; row < first_row_to_display; ++row)
13726 row->y -= dy;
13727 row->visible_height = row->height;
13728 if (row->y < min_y)
13729 row->visible_height -= min_y - row->y;
13730 if (row->y + row->height > max_y)
13731 row->visible_height -= row->y + row->height - max_y;
13732 row->redraw_fringe_bitmaps_p = 1;
13735 /* Scroll the current matrix. */
13736 xassert (nrows_scrolled > 0);
13737 rotate_matrix (w->current_matrix,
13738 start_vpos,
13739 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13740 -nrows_scrolled);
13742 /* Disable rows not reused. */
13743 for (row -= nrows_scrolled; row < bottom_row; ++row)
13744 row->enabled_p = 0;
13746 /* Point may have moved to a different line, so we cannot assume that
13747 the previous cursor position is valid; locate the correct row. */
13748 if (pt_row)
13750 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13751 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13752 row++)
13754 w->cursor.vpos++;
13755 w->cursor.y = row->y;
13757 if (row < bottom_row)
13759 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13760 while (glyph->charpos < PT)
13762 w->cursor.hpos++;
13763 w->cursor.x += glyph->pixel_width;
13764 glyph++;
13769 /* Adjust window end. A null value of last_text_row means that
13770 the window end is in reused rows which in turn means that
13771 only its vpos can have changed. */
13772 if (last_text_row)
13774 w->window_end_bytepos
13775 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13776 w->window_end_pos
13777 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13778 w->window_end_vpos
13779 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13781 else
13783 w->window_end_vpos
13784 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13787 w->window_end_valid = Qnil;
13788 w->desired_matrix->no_scrolling_p = 1;
13790 #if GLYPH_DEBUG
13791 debug_method_add (w, "try_window_reusing_current_matrix 2");
13792 #endif
13793 return 1;
13796 return 0;
13801 /************************************************************************
13802 Window redisplay reusing current matrix when buffer has changed
13803 ************************************************************************/
13805 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13806 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13807 int *, int *));
13808 static struct glyph_row *
13809 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13810 struct glyph_row *));
13813 /* Return the last row in MATRIX displaying text. If row START is
13814 non-null, start searching with that row. IT gives the dimensions
13815 of the display. Value is null if matrix is empty; otherwise it is
13816 a pointer to the row found. */
13818 static struct glyph_row *
13819 find_last_row_displaying_text (matrix, it, start)
13820 struct glyph_matrix *matrix;
13821 struct it *it;
13822 struct glyph_row *start;
13824 struct glyph_row *row, *row_found;
13826 /* Set row_found to the last row in IT->w's current matrix
13827 displaying text. The loop looks funny but think of partially
13828 visible lines. */
13829 row_found = NULL;
13830 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13831 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13833 xassert (row->enabled_p);
13834 row_found = row;
13835 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13836 break;
13837 ++row;
13840 return row_found;
13844 /* Return the last row in the current matrix of W that is not affected
13845 by changes at the start of current_buffer that occurred since W's
13846 current matrix was built. Value is null if no such row exists.
13848 BEG_UNCHANGED us the number of characters unchanged at the start of
13849 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13850 first changed character in current_buffer. Characters at positions <
13851 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13852 when the current matrix was built. */
13854 static struct glyph_row *
13855 find_last_unchanged_at_beg_row (w)
13856 struct window *w;
13858 int first_changed_pos = BEG + BEG_UNCHANGED;
13859 struct glyph_row *row;
13860 struct glyph_row *row_found = NULL;
13861 int yb = window_text_bottom_y (w);
13863 /* Find the last row displaying unchanged text. */
13864 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13865 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13866 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13868 if (/* If row ends before first_changed_pos, it is unchanged,
13869 except in some case. */
13870 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13871 /* When row ends in ZV and we write at ZV it is not
13872 unchanged. */
13873 && !row->ends_at_zv_p
13874 /* When first_changed_pos is the end of a continued line,
13875 row is not unchanged because it may be no longer
13876 continued. */
13877 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13878 && (row->continued_p
13879 || row->exact_window_width_line_p)))
13880 row_found = row;
13882 /* Stop if last visible row. */
13883 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13884 break;
13886 ++row;
13889 return row_found;
13893 /* Find the first glyph row in the current matrix of W that is not
13894 affected by changes at the end of current_buffer since the
13895 time W's current matrix was built.
13897 Return in *DELTA the number of chars by which buffer positions in
13898 unchanged text at the end of current_buffer must be adjusted.
13900 Return in *DELTA_BYTES the corresponding number of bytes.
13902 Value is null if no such row exists, i.e. all rows are affected by
13903 changes. */
13905 static struct glyph_row *
13906 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13907 struct window *w;
13908 int *delta, *delta_bytes;
13910 struct glyph_row *row;
13911 struct glyph_row *row_found = NULL;
13913 *delta = *delta_bytes = 0;
13915 /* Display must not have been paused, otherwise the current matrix
13916 is not up to date. */
13917 if (NILP (w->window_end_valid))
13918 abort ();
13920 /* A value of window_end_pos >= END_UNCHANGED means that the window
13921 end is in the range of changed text. If so, there is no
13922 unchanged row at the end of W's current matrix. */
13923 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13924 return NULL;
13926 /* Set row to the last row in W's current matrix displaying text. */
13927 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13929 /* If matrix is entirely empty, no unchanged row exists. */
13930 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13932 /* The value of row is the last glyph row in the matrix having a
13933 meaningful buffer position in it. The end position of row
13934 corresponds to window_end_pos. This allows us to translate
13935 buffer positions in the current matrix to current buffer
13936 positions for characters not in changed text. */
13937 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13938 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13939 int last_unchanged_pos, last_unchanged_pos_old;
13940 struct glyph_row *first_text_row
13941 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13943 *delta = Z - Z_old;
13944 *delta_bytes = Z_BYTE - Z_BYTE_old;
13946 /* Set last_unchanged_pos to the buffer position of the last
13947 character in the buffer that has not been changed. Z is the
13948 index + 1 of the last character in current_buffer, i.e. by
13949 subtracting END_UNCHANGED we get the index of the last
13950 unchanged character, and we have to add BEG to get its buffer
13951 position. */
13952 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13953 last_unchanged_pos_old = last_unchanged_pos - *delta;
13955 /* Search backward from ROW for a row displaying a line that
13956 starts at a minimum position >= last_unchanged_pos_old. */
13957 for (; row > first_text_row; --row)
13959 /* This used to abort, but it can happen.
13960 It is ok to just stop the search instead here. KFS. */
13961 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13962 break;
13964 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13965 row_found = row;
13969 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13970 abort ();
13972 return row_found;
13976 /* Make sure that glyph rows in the current matrix of window W
13977 reference the same glyph memory as corresponding rows in the
13978 frame's frame matrix. This function is called after scrolling W's
13979 current matrix on a terminal frame in try_window_id and
13980 try_window_reusing_current_matrix. */
13982 static void
13983 sync_frame_with_window_matrix_rows (w)
13984 struct window *w;
13986 struct frame *f = XFRAME (w->frame);
13987 struct glyph_row *window_row, *window_row_end, *frame_row;
13989 /* Preconditions: W must be a leaf window and full-width. Its frame
13990 must have a frame matrix. */
13991 xassert (NILP (w->hchild) && NILP (w->vchild));
13992 xassert (WINDOW_FULL_WIDTH_P (w));
13993 xassert (!FRAME_WINDOW_P (f));
13995 /* If W is a full-width window, glyph pointers in W's current matrix
13996 have, by definition, to be the same as glyph pointers in the
13997 corresponding frame matrix. Note that frame matrices have no
13998 marginal areas (see build_frame_matrix). */
13999 window_row = w->current_matrix->rows;
14000 window_row_end = window_row + w->current_matrix->nrows;
14001 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14002 while (window_row < window_row_end)
14004 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14005 struct glyph *end = window_row->glyphs[LAST_AREA];
14007 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14008 frame_row->glyphs[TEXT_AREA] = start;
14009 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14010 frame_row->glyphs[LAST_AREA] = end;
14012 /* Disable frame rows whose corresponding window rows have
14013 been disabled in try_window_id. */
14014 if (!window_row->enabled_p)
14015 frame_row->enabled_p = 0;
14017 ++window_row, ++frame_row;
14022 /* Find the glyph row in window W containing CHARPOS. Consider all
14023 rows between START and END (not inclusive). END null means search
14024 all rows to the end of the display area of W. Value is the row
14025 containing CHARPOS or null. */
14027 struct glyph_row *
14028 row_containing_pos (w, charpos, start, end, dy)
14029 struct window *w;
14030 int charpos;
14031 struct glyph_row *start, *end;
14032 int dy;
14034 struct glyph_row *row = start;
14035 int last_y;
14037 /* If we happen to start on a header-line, skip that. */
14038 if (row->mode_line_p)
14039 ++row;
14041 if ((end && row >= end) || !row->enabled_p)
14042 return NULL;
14044 last_y = window_text_bottom_y (w) - dy;
14046 while (1)
14048 /* Give up if we have gone too far. */
14049 if (end && row >= end)
14050 return NULL;
14051 /* This formerly returned if they were equal.
14052 I think that both quantities are of a "last plus one" type;
14053 if so, when they are equal, the row is within the screen. -- rms. */
14054 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14055 return NULL;
14057 /* If it is in this row, return this row. */
14058 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14059 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14060 /* The end position of a row equals the start
14061 position of the next row. If CHARPOS is there, we
14062 would rather display it in the next line, except
14063 when this line ends in ZV. */
14064 && !row->ends_at_zv_p
14065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14066 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14067 return row;
14068 ++row;
14073 /* Try to redisplay window W by reusing its existing display. W's
14074 current matrix must be up to date when this function is called,
14075 i.e. window_end_valid must not be nil.
14077 Value is
14079 1 if display has been updated
14080 0 if otherwise unsuccessful
14081 -1 if redisplay with same window start is known not to succeed
14083 The following steps are performed:
14085 1. Find the last row in the current matrix of W that is not
14086 affected by changes at the start of current_buffer. If no such row
14087 is found, give up.
14089 2. Find the first row in W's current matrix that is not affected by
14090 changes at the end of current_buffer. Maybe there is no such row.
14092 3. Display lines beginning with the row + 1 found in step 1 to the
14093 row found in step 2 or, if step 2 didn't find a row, to the end of
14094 the window.
14096 4. If cursor is not known to appear on the window, give up.
14098 5. If display stopped at the row found in step 2, scroll the
14099 display and current matrix as needed.
14101 6. Maybe display some lines at the end of W, if we must. This can
14102 happen under various circumstances, like a partially visible line
14103 becoming fully visible, or because newly displayed lines are displayed
14104 in smaller font sizes.
14106 7. Update W's window end information. */
14108 static int
14109 try_window_id (w)
14110 struct window *w;
14112 struct frame *f = XFRAME (w->frame);
14113 struct glyph_matrix *current_matrix = w->current_matrix;
14114 struct glyph_matrix *desired_matrix = w->desired_matrix;
14115 struct glyph_row *last_unchanged_at_beg_row;
14116 struct glyph_row *first_unchanged_at_end_row;
14117 struct glyph_row *row;
14118 struct glyph_row *bottom_row;
14119 int bottom_vpos;
14120 struct it it;
14121 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14122 struct text_pos start_pos;
14123 struct run run;
14124 int first_unchanged_at_end_vpos = 0;
14125 struct glyph_row *last_text_row, *last_text_row_at_end;
14126 struct text_pos start;
14127 int first_changed_charpos, last_changed_charpos;
14129 #if GLYPH_DEBUG
14130 if (inhibit_try_window_id)
14131 return 0;
14132 #endif
14134 /* This is handy for debugging. */
14135 #if 0
14136 #define GIVE_UP(X) \
14137 do { \
14138 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14139 return 0; \
14140 } while (0)
14141 #else
14142 #define GIVE_UP(X) return 0
14143 #endif
14145 SET_TEXT_POS_FROM_MARKER (start, w->start);
14147 /* Don't use this for mini-windows because these can show
14148 messages and mini-buffers, and we don't handle that here. */
14149 if (MINI_WINDOW_P (w))
14150 GIVE_UP (1);
14152 /* This flag is used to prevent redisplay optimizations. */
14153 if (windows_or_buffers_changed || cursor_type_changed)
14154 GIVE_UP (2);
14156 /* Verify that narrowing has not changed.
14157 Also verify that we were not told to prevent redisplay optimizations.
14158 It would be nice to further
14159 reduce the number of cases where this prevents try_window_id. */
14160 if (current_buffer->clip_changed
14161 || current_buffer->prevent_redisplay_optimizations_p)
14162 GIVE_UP (3);
14164 /* Window must either use window-based redisplay or be full width. */
14165 if (!FRAME_WINDOW_P (f)
14166 && (!FRAME_LINE_INS_DEL_OK (f)
14167 || !WINDOW_FULL_WIDTH_P (w)))
14168 GIVE_UP (4);
14170 /* Give up if point is not known NOT to appear in W. */
14171 if (PT < CHARPOS (start))
14172 GIVE_UP (5);
14174 /* Another way to prevent redisplay optimizations. */
14175 if (XFASTINT (w->last_modified) == 0)
14176 GIVE_UP (6);
14178 /* Verify that window is not hscrolled. */
14179 if (XFASTINT (w->hscroll) != 0)
14180 GIVE_UP (7);
14182 /* Verify that display wasn't paused. */
14183 if (NILP (w->window_end_valid))
14184 GIVE_UP (8);
14186 /* Can't use this if highlighting a region because a cursor movement
14187 will do more than just set the cursor. */
14188 if (!NILP (Vtransient_mark_mode)
14189 && !NILP (current_buffer->mark_active))
14190 GIVE_UP (9);
14192 /* Likewise if highlighting trailing whitespace. */
14193 if (!NILP (Vshow_trailing_whitespace))
14194 GIVE_UP (11);
14196 /* Likewise if showing a region. */
14197 if (!NILP (w->region_showing))
14198 GIVE_UP (10);
14200 /* Can use this if overlay arrow position and or string have changed. */
14201 if (overlay_arrows_changed_p ())
14202 GIVE_UP (12);
14205 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14206 only if buffer has really changed. The reason is that the gap is
14207 initially at Z for freshly visited files. The code below would
14208 set end_unchanged to 0 in that case. */
14209 if (MODIFF > SAVE_MODIFF
14210 /* This seems to happen sometimes after saving a buffer. */
14211 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14213 if (GPT - BEG < BEG_UNCHANGED)
14214 BEG_UNCHANGED = GPT - BEG;
14215 if (Z - GPT < END_UNCHANGED)
14216 END_UNCHANGED = Z - GPT;
14219 /* The position of the first and last character that has been changed. */
14220 first_changed_charpos = BEG + BEG_UNCHANGED;
14221 last_changed_charpos = Z - END_UNCHANGED;
14223 /* If window starts after a line end, and the last change is in
14224 front of that newline, then changes don't affect the display.
14225 This case happens with stealth-fontification. Note that although
14226 the display is unchanged, glyph positions in the matrix have to
14227 be adjusted, of course. */
14228 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14229 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14230 && ((last_changed_charpos < CHARPOS (start)
14231 && CHARPOS (start) == BEGV)
14232 || (last_changed_charpos < CHARPOS (start) - 1
14233 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14235 int Z_old, delta, Z_BYTE_old, delta_bytes;
14236 struct glyph_row *r0;
14238 /* Compute how many chars/bytes have been added to or removed
14239 from the buffer. */
14240 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14241 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14242 delta = Z - Z_old;
14243 delta_bytes = Z_BYTE - Z_BYTE_old;
14245 /* Give up if PT is not in the window. Note that it already has
14246 been checked at the start of try_window_id that PT is not in
14247 front of the window start. */
14248 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14249 GIVE_UP (13);
14251 /* If window start is unchanged, we can reuse the whole matrix
14252 as is, after adjusting glyph positions. No need to compute
14253 the window end again, since its offset from Z hasn't changed. */
14254 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14255 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14256 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14257 /* PT must not be in a partially visible line. */
14258 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14259 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14261 /* Adjust positions in the glyph matrix. */
14262 if (delta || delta_bytes)
14264 struct glyph_row *r1
14265 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14266 increment_matrix_positions (w->current_matrix,
14267 MATRIX_ROW_VPOS (r0, current_matrix),
14268 MATRIX_ROW_VPOS (r1, current_matrix),
14269 delta, delta_bytes);
14272 /* Set the cursor. */
14273 row = row_containing_pos (w, PT, r0, NULL, 0);
14274 if (row)
14275 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14276 else
14277 abort ();
14278 return 1;
14282 /* Handle the case that changes are all below what is displayed in
14283 the window, and that PT is in the window. This shortcut cannot
14284 be taken if ZV is visible in the window, and text has been added
14285 there that is visible in the window. */
14286 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14287 /* ZV is not visible in the window, or there are no
14288 changes at ZV, actually. */
14289 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14290 || first_changed_charpos == last_changed_charpos))
14292 struct glyph_row *r0;
14294 /* Give up if PT is not in the window. Note that it already has
14295 been checked at the start of try_window_id that PT is not in
14296 front of the window start. */
14297 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14298 GIVE_UP (14);
14300 /* If window start is unchanged, we can reuse the whole matrix
14301 as is, without changing glyph positions since no text has
14302 been added/removed in front of the window end. */
14303 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14304 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14305 /* PT must not be in a partially visible line. */
14306 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14307 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14309 /* We have to compute the window end anew since text
14310 can have been added/removed after it. */
14311 w->window_end_pos
14312 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14313 w->window_end_bytepos
14314 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14316 /* Set the cursor. */
14317 row = row_containing_pos (w, PT, r0, NULL, 0);
14318 if (row)
14319 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14320 else
14321 abort ();
14322 return 2;
14326 /* Give up if window start is in the changed area.
14328 The condition used to read
14330 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14332 but why that was tested escapes me at the moment. */
14333 if (CHARPOS (start) >= first_changed_charpos
14334 && CHARPOS (start) <= last_changed_charpos)
14335 GIVE_UP (15);
14337 /* Check that window start agrees with the start of the first glyph
14338 row in its current matrix. Check this after we know the window
14339 start is not in changed text, otherwise positions would not be
14340 comparable. */
14341 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14342 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14343 GIVE_UP (16);
14345 /* Give up if the window ends in strings. Overlay strings
14346 at the end are difficult to handle, so don't try. */
14347 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14348 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14349 GIVE_UP (20);
14351 /* Compute the position at which we have to start displaying new
14352 lines. Some of the lines at the top of the window might be
14353 reusable because they are not displaying changed text. Find the
14354 last row in W's current matrix not affected by changes at the
14355 start of current_buffer. Value is null if changes start in the
14356 first line of window. */
14357 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14358 if (last_unchanged_at_beg_row)
14360 /* Avoid starting to display in the moddle of a character, a TAB
14361 for instance. This is easier than to set up the iterator
14362 exactly, and it's not a frequent case, so the additional
14363 effort wouldn't really pay off. */
14364 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14365 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14366 && last_unchanged_at_beg_row > w->current_matrix->rows)
14367 --last_unchanged_at_beg_row;
14369 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14370 GIVE_UP (17);
14372 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14373 GIVE_UP (18);
14374 start_pos = it.current.pos;
14376 /* Start displaying new lines in the desired matrix at the same
14377 vpos we would use in the current matrix, i.e. below
14378 last_unchanged_at_beg_row. */
14379 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14380 current_matrix);
14381 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14382 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14384 xassert (it.hpos == 0 && it.current_x == 0);
14386 else
14388 /* There are no reusable lines at the start of the window.
14389 Start displaying in the first text line. */
14390 start_display (&it, w, start);
14391 it.vpos = it.first_vpos;
14392 start_pos = it.current.pos;
14395 /* Find the first row that is not affected by changes at the end of
14396 the buffer. Value will be null if there is no unchanged row, in
14397 which case we must redisplay to the end of the window. delta
14398 will be set to the value by which buffer positions beginning with
14399 first_unchanged_at_end_row have to be adjusted due to text
14400 changes. */
14401 first_unchanged_at_end_row
14402 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14403 IF_DEBUG (debug_delta = delta);
14404 IF_DEBUG (debug_delta_bytes = delta_bytes);
14406 /* Set stop_pos to the buffer position up to which we will have to
14407 display new lines. If first_unchanged_at_end_row != NULL, this
14408 is the buffer position of the start of the line displayed in that
14409 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14410 that we don't stop at a buffer position. */
14411 stop_pos = 0;
14412 if (first_unchanged_at_end_row)
14414 xassert (last_unchanged_at_beg_row == NULL
14415 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14417 /* If this is a continuation line, move forward to the next one
14418 that isn't. Changes in lines above affect this line.
14419 Caution: this may move first_unchanged_at_end_row to a row
14420 not displaying text. */
14421 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14422 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14423 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14424 < it.last_visible_y))
14425 ++first_unchanged_at_end_row;
14427 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14428 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14429 >= it.last_visible_y))
14430 first_unchanged_at_end_row = NULL;
14431 else
14433 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14434 + delta);
14435 first_unchanged_at_end_vpos
14436 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14437 xassert (stop_pos >= Z - END_UNCHANGED);
14440 else if (last_unchanged_at_beg_row == NULL)
14441 GIVE_UP (19);
14444 #if GLYPH_DEBUG
14446 /* Either there is no unchanged row at the end, or the one we have
14447 now displays text. This is a necessary condition for the window
14448 end pos calculation at the end of this function. */
14449 xassert (first_unchanged_at_end_row == NULL
14450 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14452 debug_last_unchanged_at_beg_vpos
14453 = (last_unchanged_at_beg_row
14454 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14455 : -1);
14456 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14458 #endif /* GLYPH_DEBUG != 0 */
14461 /* Display new lines. Set last_text_row to the last new line
14462 displayed which has text on it, i.e. might end up as being the
14463 line where the window_end_vpos is. */
14464 w->cursor.vpos = -1;
14465 last_text_row = NULL;
14466 overlay_arrow_seen = 0;
14467 while (it.current_y < it.last_visible_y
14468 && !fonts_changed_p
14469 && (first_unchanged_at_end_row == NULL
14470 || IT_CHARPOS (it) < stop_pos))
14472 if (display_line (&it))
14473 last_text_row = it.glyph_row - 1;
14476 if (fonts_changed_p)
14477 return -1;
14480 /* Compute differences in buffer positions, y-positions etc. for
14481 lines reused at the bottom of the window. Compute what we can
14482 scroll. */
14483 if (first_unchanged_at_end_row
14484 /* No lines reused because we displayed everything up to the
14485 bottom of the window. */
14486 && it.current_y < it.last_visible_y)
14488 dvpos = (it.vpos
14489 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14490 current_matrix));
14491 dy = it.current_y - first_unchanged_at_end_row->y;
14492 run.current_y = first_unchanged_at_end_row->y;
14493 run.desired_y = run.current_y + dy;
14494 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14496 else
14498 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14499 first_unchanged_at_end_row = NULL;
14501 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14504 /* Find the cursor if not already found. We have to decide whether
14505 PT will appear on this window (it sometimes doesn't, but this is
14506 not a very frequent case.) This decision has to be made before
14507 the current matrix is altered. A value of cursor.vpos < 0 means
14508 that PT is either in one of the lines beginning at
14509 first_unchanged_at_end_row or below the window. Don't care for
14510 lines that might be displayed later at the window end; as
14511 mentioned, this is not a frequent case. */
14512 if (w->cursor.vpos < 0)
14514 /* Cursor in unchanged rows at the top? */
14515 if (PT < CHARPOS (start_pos)
14516 && last_unchanged_at_beg_row)
14518 row = row_containing_pos (w, PT,
14519 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14520 last_unchanged_at_beg_row + 1, 0);
14521 if (row)
14522 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14525 /* Start from first_unchanged_at_end_row looking for PT. */
14526 else if (first_unchanged_at_end_row)
14528 row = row_containing_pos (w, PT - delta,
14529 first_unchanged_at_end_row, NULL, 0);
14530 if (row)
14531 set_cursor_from_row (w, row, w->current_matrix, delta,
14532 delta_bytes, dy, dvpos);
14535 /* Give up if cursor was not found. */
14536 if (w->cursor.vpos < 0)
14538 clear_glyph_matrix (w->desired_matrix);
14539 return -1;
14543 /* Don't let the cursor end in the scroll margins. */
14545 int this_scroll_margin, cursor_height;
14547 this_scroll_margin = max (0, scroll_margin);
14548 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14549 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14550 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14552 if ((w->cursor.y < this_scroll_margin
14553 && CHARPOS (start) > BEGV)
14554 /* Old redisplay didn't take scroll margin into account at the bottom,
14555 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14556 || (w->cursor.y + (make_cursor_line_fully_visible_p
14557 ? cursor_height + this_scroll_margin
14558 : 1)) > it.last_visible_y)
14560 w->cursor.vpos = -1;
14561 clear_glyph_matrix (w->desired_matrix);
14562 return -1;
14566 /* Scroll the display. Do it before changing the current matrix so
14567 that xterm.c doesn't get confused about where the cursor glyph is
14568 found. */
14569 if (dy && run.height)
14571 update_begin (f);
14573 if (FRAME_WINDOW_P (f))
14575 FRAME_RIF (f)->update_window_begin_hook (w);
14576 FRAME_RIF (f)->clear_window_mouse_face (w);
14577 FRAME_RIF (f)->scroll_run_hook (w, &run);
14578 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14580 else
14582 /* Terminal frame. In this case, dvpos gives the number of
14583 lines to scroll by; dvpos < 0 means scroll up. */
14584 int first_unchanged_at_end_vpos
14585 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14586 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14587 int end = (WINDOW_TOP_EDGE_LINE (w)
14588 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14589 + window_internal_height (w));
14591 /* Perform the operation on the screen. */
14592 if (dvpos > 0)
14594 /* Scroll last_unchanged_at_beg_row to the end of the
14595 window down dvpos lines. */
14596 set_terminal_window (f, end);
14598 /* On dumb terminals delete dvpos lines at the end
14599 before inserting dvpos empty lines. */
14600 if (!FRAME_SCROLL_REGION_OK (f))
14601 ins_del_lines (f, end - dvpos, -dvpos);
14603 /* Insert dvpos empty lines in front of
14604 last_unchanged_at_beg_row. */
14605 ins_del_lines (f, from, dvpos);
14607 else if (dvpos < 0)
14609 /* Scroll up last_unchanged_at_beg_vpos to the end of
14610 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14611 set_terminal_window (f, end);
14613 /* Delete dvpos lines in front of
14614 last_unchanged_at_beg_vpos. ins_del_lines will set
14615 the cursor to the given vpos and emit |dvpos| delete
14616 line sequences. */
14617 ins_del_lines (f, from + dvpos, dvpos);
14619 /* On a dumb terminal insert dvpos empty lines at the
14620 end. */
14621 if (!FRAME_SCROLL_REGION_OK (f))
14622 ins_del_lines (f, end + dvpos, -dvpos);
14625 set_terminal_window (f, 0);
14628 update_end (f);
14631 /* Shift reused rows of the current matrix to the right position.
14632 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14633 text. */
14634 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14635 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14636 if (dvpos < 0)
14638 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14639 bottom_vpos, dvpos);
14640 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14641 bottom_vpos, 0);
14643 else if (dvpos > 0)
14645 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14646 bottom_vpos, dvpos);
14647 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14648 first_unchanged_at_end_vpos + dvpos, 0);
14651 /* For frame-based redisplay, make sure that current frame and window
14652 matrix are in sync with respect to glyph memory. */
14653 if (!FRAME_WINDOW_P (f))
14654 sync_frame_with_window_matrix_rows (w);
14656 /* Adjust buffer positions in reused rows. */
14657 if (delta)
14658 increment_matrix_positions (current_matrix,
14659 first_unchanged_at_end_vpos + dvpos,
14660 bottom_vpos, delta, delta_bytes);
14662 /* Adjust Y positions. */
14663 if (dy)
14664 shift_glyph_matrix (w, current_matrix,
14665 first_unchanged_at_end_vpos + dvpos,
14666 bottom_vpos, dy);
14668 if (first_unchanged_at_end_row)
14670 first_unchanged_at_end_row += dvpos;
14671 if (first_unchanged_at_end_row->y >= it.last_visible_y
14672 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14673 first_unchanged_at_end_row = NULL;
14676 /* If scrolling up, there may be some lines to display at the end of
14677 the window. */
14678 last_text_row_at_end = NULL;
14679 if (dy < 0)
14681 /* Scrolling up can leave for example a partially visible line
14682 at the end of the window to be redisplayed. */
14683 /* Set last_row to the glyph row in the current matrix where the
14684 window end line is found. It has been moved up or down in
14685 the matrix by dvpos. */
14686 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14687 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14689 /* If last_row is the window end line, it should display text. */
14690 xassert (last_row->displays_text_p);
14692 /* If window end line was partially visible before, begin
14693 displaying at that line. Otherwise begin displaying with the
14694 line following it. */
14695 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14697 init_to_row_start (&it, w, last_row);
14698 it.vpos = last_vpos;
14699 it.current_y = last_row->y;
14701 else
14703 init_to_row_end (&it, w, last_row);
14704 it.vpos = 1 + last_vpos;
14705 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14706 ++last_row;
14709 /* We may start in a continuation line. If so, we have to
14710 get the right continuation_lines_width and current_x. */
14711 it.continuation_lines_width = last_row->continuation_lines_width;
14712 it.hpos = it.current_x = 0;
14714 /* Display the rest of the lines at the window end. */
14715 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14716 while (it.current_y < it.last_visible_y
14717 && !fonts_changed_p)
14719 /* Is it always sure that the display agrees with lines in
14720 the current matrix? I don't think so, so we mark rows
14721 displayed invalid in the current matrix by setting their
14722 enabled_p flag to zero. */
14723 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14724 if (display_line (&it))
14725 last_text_row_at_end = it.glyph_row - 1;
14729 /* Update window_end_pos and window_end_vpos. */
14730 if (first_unchanged_at_end_row
14731 && !last_text_row_at_end)
14733 /* Window end line if one of the preserved rows from the current
14734 matrix. Set row to the last row displaying text in current
14735 matrix starting at first_unchanged_at_end_row, after
14736 scrolling. */
14737 xassert (first_unchanged_at_end_row->displays_text_p);
14738 row = find_last_row_displaying_text (w->current_matrix, &it,
14739 first_unchanged_at_end_row);
14740 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14742 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14743 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14744 w->window_end_vpos
14745 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14746 xassert (w->window_end_bytepos >= 0);
14747 IF_DEBUG (debug_method_add (w, "A"));
14749 else if (last_text_row_at_end)
14751 w->window_end_pos
14752 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14753 w->window_end_bytepos
14754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14755 w->window_end_vpos
14756 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14757 xassert (w->window_end_bytepos >= 0);
14758 IF_DEBUG (debug_method_add (w, "B"));
14760 else if (last_text_row)
14762 /* We have displayed either to the end of the window or at the
14763 end of the window, i.e. the last row with text is to be found
14764 in the desired matrix. */
14765 w->window_end_pos
14766 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14767 w->window_end_bytepos
14768 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14769 w->window_end_vpos
14770 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14771 xassert (w->window_end_bytepos >= 0);
14773 else if (first_unchanged_at_end_row == NULL
14774 && last_text_row == NULL
14775 && last_text_row_at_end == NULL)
14777 /* Displayed to end of window, but no line containing text was
14778 displayed. Lines were deleted at the end of the window. */
14779 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14780 int vpos = XFASTINT (w->window_end_vpos);
14781 struct glyph_row *current_row = current_matrix->rows + vpos;
14782 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14784 for (row = NULL;
14785 row == NULL && vpos >= first_vpos;
14786 --vpos, --current_row, --desired_row)
14788 if (desired_row->enabled_p)
14790 if (desired_row->displays_text_p)
14791 row = desired_row;
14793 else if (current_row->displays_text_p)
14794 row = current_row;
14797 xassert (row != NULL);
14798 w->window_end_vpos = make_number (vpos + 1);
14799 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14800 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14801 xassert (w->window_end_bytepos >= 0);
14802 IF_DEBUG (debug_method_add (w, "C"));
14804 else
14805 abort ();
14807 #if 0 /* This leads to problems, for instance when the cursor is
14808 at ZV, and the cursor line displays no text. */
14809 /* Disable rows below what's displayed in the window. This makes
14810 debugging easier. */
14811 enable_glyph_matrix_rows (current_matrix,
14812 XFASTINT (w->window_end_vpos) + 1,
14813 bottom_vpos, 0);
14814 #endif
14816 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14817 debug_end_vpos = XFASTINT (w->window_end_vpos));
14819 /* Record that display has not been completed. */
14820 w->window_end_valid = Qnil;
14821 w->desired_matrix->no_scrolling_p = 1;
14822 return 3;
14824 #undef GIVE_UP
14829 /***********************************************************************
14830 More debugging support
14831 ***********************************************************************/
14833 #if GLYPH_DEBUG
14835 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14836 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14837 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14840 /* Dump the contents of glyph matrix MATRIX on stderr.
14842 GLYPHS 0 means don't show glyph contents.
14843 GLYPHS 1 means show glyphs in short form
14844 GLYPHS > 1 means show glyphs in long form. */
14846 void
14847 dump_glyph_matrix (matrix, glyphs)
14848 struct glyph_matrix *matrix;
14849 int glyphs;
14851 int i;
14852 for (i = 0; i < matrix->nrows; ++i)
14853 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14857 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14858 the glyph row and area where the glyph comes from. */
14860 void
14861 dump_glyph (row, glyph, area)
14862 struct glyph_row *row;
14863 struct glyph *glyph;
14864 int area;
14866 if (glyph->type == CHAR_GLYPH)
14868 fprintf (stderr,
14869 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14870 glyph - row->glyphs[TEXT_AREA],
14871 'C',
14872 glyph->charpos,
14873 (BUFFERP (glyph->object)
14874 ? 'B'
14875 : (STRINGP (glyph->object)
14876 ? 'S'
14877 : '-')),
14878 glyph->pixel_width,
14879 glyph->u.ch,
14880 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14881 ? glyph->u.ch
14882 : '.'),
14883 glyph->face_id,
14884 glyph->left_box_line_p,
14885 glyph->right_box_line_p);
14887 else if (glyph->type == STRETCH_GLYPH)
14889 fprintf (stderr,
14890 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14891 glyph - row->glyphs[TEXT_AREA],
14892 'S',
14893 glyph->charpos,
14894 (BUFFERP (glyph->object)
14895 ? 'B'
14896 : (STRINGP (glyph->object)
14897 ? 'S'
14898 : '-')),
14899 glyph->pixel_width,
14901 '.',
14902 glyph->face_id,
14903 glyph->left_box_line_p,
14904 glyph->right_box_line_p);
14906 else if (glyph->type == IMAGE_GLYPH)
14908 fprintf (stderr,
14909 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14910 glyph - row->glyphs[TEXT_AREA],
14911 'I',
14912 glyph->charpos,
14913 (BUFFERP (glyph->object)
14914 ? 'B'
14915 : (STRINGP (glyph->object)
14916 ? 'S'
14917 : '-')),
14918 glyph->pixel_width,
14919 glyph->u.img_id,
14920 '.',
14921 glyph->face_id,
14922 glyph->left_box_line_p,
14923 glyph->right_box_line_p);
14928 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14929 GLYPHS 0 means don't show glyph contents.
14930 GLYPHS 1 means show glyphs in short form
14931 GLYPHS > 1 means show glyphs in long form. */
14933 void
14934 dump_glyph_row (row, vpos, glyphs)
14935 struct glyph_row *row;
14936 int vpos, glyphs;
14938 if (glyphs != 1)
14940 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
14941 fprintf (stderr, "======================================================================\n");
14943 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14944 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14945 vpos,
14946 MATRIX_ROW_START_CHARPOS (row),
14947 MATRIX_ROW_END_CHARPOS (row),
14948 row->used[TEXT_AREA],
14949 row->contains_overlapping_glyphs_p,
14950 row->enabled_p,
14951 row->truncated_on_left_p,
14952 row->truncated_on_right_p,
14953 row->continued_p,
14954 MATRIX_ROW_CONTINUATION_LINE_P (row),
14955 row->displays_text_p,
14956 row->ends_at_zv_p,
14957 row->fill_line_p,
14958 row->ends_in_middle_of_char_p,
14959 row->starts_in_middle_of_char_p,
14960 row->mouse_face_p,
14961 row->x,
14962 row->y,
14963 row->pixel_width,
14964 row->height,
14965 row->visible_height,
14966 row->ascent,
14967 row->phys_ascent);
14968 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14969 row->end.overlay_string_index,
14970 row->continuation_lines_width);
14971 fprintf (stderr, "%9d %5d\n",
14972 CHARPOS (row->start.string_pos),
14973 CHARPOS (row->end.string_pos));
14974 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14975 row->end.dpvec_index);
14978 if (glyphs > 1)
14980 int area;
14982 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14984 struct glyph *glyph = row->glyphs[area];
14985 struct glyph *glyph_end = glyph + row->used[area];
14987 /* Glyph for a line end in text. */
14988 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14989 ++glyph_end;
14991 if (glyph < glyph_end)
14992 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14994 for (; glyph < glyph_end; ++glyph)
14995 dump_glyph (row, glyph, area);
14998 else if (glyphs == 1)
15000 int area;
15002 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15004 char *s = (char *) alloca (row->used[area] + 1);
15005 int i;
15007 for (i = 0; i < row->used[area]; ++i)
15009 struct glyph *glyph = row->glyphs[area] + i;
15010 if (glyph->type == CHAR_GLYPH
15011 && glyph->u.ch < 0x80
15012 && glyph->u.ch >= ' ')
15013 s[i] = glyph->u.ch;
15014 else
15015 s[i] = '.';
15018 s[i] = '\0';
15019 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15025 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15026 Sdump_glyph_matrix, 0, 1, "p",
15027 doc: /* Dump the current matrix of the selected window to stderr.
15028 Shows contents of glyph row structures. With non-nil
15029 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15030 glyphs in short form, otherwise show glyphs in long form. */)
15031 (glyphs)
15032 Lisp_Object glyphs;
15034 struct window *w = XWINDOW (selected_window);
15035 struct buffer *buffer = XBUFFER (w->buffer);
15037 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15038 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15039 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15040 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15041 fprintf (stderr, "=============================================\n");
15042 dump_glyph_matrix (w->current_matrix,
15043 NILP (glyphs) ? 0 : XINT (glyphs));
15044 return Qnil;
15048 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15049 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15052 struct frame *f = XFRAME (selected_frame);
15053 dump_glyph_matrix (f->current_matrix, 1);
15054 return Qnil;
15058 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15059 doc: /* Dump glyph row ROW to stderr.
15060 GLYPH 0 means don't dump glyphs.
15061 GLYPH 1 means dump glyphs in short form.
15062 GLYPH > 1 or omitted means dump glyphs in long form. */)
15063 (row, glyphs)
15064 Lisp_Object row, glyphs;
15066 struct glyph_matrix *matrix;
15067 int vpos;
15069 CHECK_NUMBER (row);
15070 matrix = XWINDOW (selected_window)->current_matrix;
15071 vpos = XINT (row);
15072 if (vpos >= 0 && vpos < matrix->nrows)
15073 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15074 vpos,
15075 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15076 return Qnil;
15080 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15081 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15082 GLYPH 0 means don't dump glyphs.
15083 GLYPH 1 means dump glyphs in short form.
15084 GLYPH > 1 or omitted means dump glyphs in long form. */)
15085 (row, glyphs)
15086 Lisp_Object row, glyphs;
15088 struct frame *sf = SELECTED_FRAME ();
15089 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15090 int vpos;
15092 CHECK_NUMBER (row);
15093 vpos = XINT (row);
15094 if (vpos >= 0 && vpos < m->nrows)
15095 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15096 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15097 return Qnil;
15101 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15102 doc: /* Toggle tracing of redisplay.
15103 With ARG, turn tracing on if and only if ARG is positive. */)
15104 (arg)
15105 Lisp_Object arg;
15107 if (NILP (arg))
15108 trace_redisplay_p = !trace_redisplay_p;
15109 else
15111 arg = Fprefix_numeric_value (arg);
15112 trace_redisplay_p = XINT (arg) > 0;
15115 return Qnil;
15119 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15120 doc: /* Like `format', but print result to stderr.
15121 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15122 (nargs, args)
15123 int nargs;
15124 Lisp_Object *args;
15126 Lisp_Object s = Fformat (nargs, args);
15127 fprintf (stderr, "%s", SDATA (s));
15128 return Qnil;
15131 #endif /* GLYPH_DEBUG */
15135 /***********************************************************************
15136 Building Desired Matrix Rows
15137 ***********************************************************************/
15139 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15140 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15142 static struct glyph_row *
15143 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15144 struct window *w;
15145 Lisp_Object overlay_arrow_string;
15147 struct frame *f = XFRAME (WINDOW_FRAME (w));
15148 struct buffer *buffer = XBUFFER (w->buffer);
15149 struct buffer *old = current_buffer;
15150 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15151 int arrow_len = SCHARS (overlay_arrow_string);
15152 const unsigned char *arrow_end = arrow_string + arrow_len;
15153 const unsigned char *p;
15154 struct it it;
15155 int multibyte_p;
15156 int n_glyphs_before;
15158 set_buffer_temp (buffer);
15159 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15160 it.glyph_row->used[TEXT_AREA] = 0;
15161 SET_TEXT_POS (it.position, 0, 0);
15163 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15164 p = arrow_string;
15165 while (p < arrow_end)
15167 Lisp_Object face, ilisp;
15169 /* Get the next character. */
15170 if (multibyte_p)
15171 it.c = string_char_and_length (p, arrow_len, &it.len);
15172 else
15173 it.c = *p, it.len = 1;
15174 p += it.len;
15176 /* Get its face. */
15177 ilisp = make_number (p - arrow_string);
15178 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15179 it.face_id = compute_char_face (f, it.c, face);
15181 /* Compute its width, get its glyphs. */
15182 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15183 SET_TEXT_POS (it.position, -1, -1);
15184 PRODUCE_GLYPHS (&it);
15186 /* If this character doesn't fit any more in the line, we have
15187 to remove some glyphs. */
15188 if (it.current_x > it.last_visible_x)
15190 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15191 break;
15195 set_buffer_temp (old);
15196 return it.glyph_row;
15200 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15201 glyphs are only inserted for terminal frames since we can't really
15202 win with truncation glyphs when partially visible glyphs are
15203 involved. Which glyphs to insert is determined by
15204 produce_special_glyphs. */
15206 static void
15207 insert_left_trunc_glyphs (it)
15208 struct it *it;
15210 struct it truncate_it;
15211 struct glyph *from, *end, *to, *toend;
15213 xassert (!FRAME_WINDOW_P (it->f));
15215 /* Get the truncation glyphs. */
15216 truncate_it = *it;
15217 truncate_it.current_x = 0;
15218 truncate_it.face_id = DEFAULT_FACE_ID;
15219 truncate_it.glyph_row = &scratch_glyph_row;
15220 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15221 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15222 truncate_it.object = make_number (0);
15223 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15225 /* Overwrite glyphs from IT with truncation glyphs. */
15226 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15227 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15228 to = it->glyph_row->glyphs[TEXT_AREA];
15229 toend = to + it->glyph_row->used[TEXT_AREA];
15231 while (from < end)
15232 *to++ = *from++;
15234 /* There may be padding glyphs left over. Overwrite them too. */
15235 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15237 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15238 while (from < end)
15239 *to++ = *from++;
15242 if (to > toend)
15243 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15247 /* Compute the pixel height and width of IT->glyph_row.
15249 Most of the time, ascent and height of a display line will be equal
15250 to the max_ascent and max_height values of the display iterator
15251 structure. This is not the case if
15253 1. We hit ZV without displaying anything. In this case, max_ascent
15254 and max_height will be zero.
15256 2. We have some glyphs that don't contribute to the line height.
15257 (The glyph row flag contributes_to_line_height_p is for future
15258 pixmap extensions).
15260 The first case is easily covered by using default values because in
15261 these cases, the line height does not really matter, except that it
15262 must not be zero. */
15264 static void
15265 compute_line_metrics (it)
15266 struct it *it;
15268 struct glyph_row *row = it->glyph_row;
15269 int area, i;
15271 if (FRAME_WINDOW_P (it->f))
15273 int i, min_y, max_y;
15275 /* The line may consist of one space only, that was added to
15276 place the cursor on it. If so, the row's height hasn't been
15277 computed yet. */
15278 if (row->height == 0)
15280 if (it->max_ascent + it->max_descent == 0)
15281 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15282 row->ascent = it->max_ascent;
15283 row->height = it->max_ascent + it->max_descent;
15284 row->phys_ascent = it->max_phys_ascent;
15285 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15286 row->extra_line_spacing = it->max_extra_line_spacing;
15289 /* Compute the width of this line. */
15290 row->pixel_width = row->x;
15291 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15292 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15294 xassert (row->pixel_width >= 0);
15295 xassert (row->ascent >= 0 && row->height > 0);
15297 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15298 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15300 /* If first line's physical ascent is larger than its logical
15301 ascent, use the physical ascent, and make the row taller.
15302 This makes accented characters fully visible. */
15303 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15304 && row->phys_ascent > row->ascent)
15306 row->height += row->phys_ascent - row->ascent;
15307 row->ascent = row->phys_ascent;
15310 /* Compute how much of the line is visible. */
15311 row->visible_height = row->height;
15313 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15314 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15316 if (row->y < min_y)
15317 row->visible_height -= min_y - row->y;
15318 if (row->y + row->height > max_y)
15319 row->visible_height -= row->y + row->height - max_y;
15321 else
15323 row->pixel_width = row->used[TEXT_AREA];
15324 if (row->continued_p)
15325 row->pixel_width -= it->continuation_pixel_width;
15326 else if (row->truncated_on_right_p)
15327 row->pixel_width -= it->truncation_pixel_width;
15328 row->ascent = row->phys_ascent = 0;
15329 row->height = row->phys_height = row->visible_height = 1;
15330 row->extra_line_spacing = 0;
15333 /* Compute a hash code for this row. */
15334 row->hash = 0;
15335 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15336 for (i = 0; i < row->used[area]; ++i)
15337 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15338 + row->glyphs[area][i].u.val
15339 + row->glyphs[area][i].face_id
15340 + row->glyphs[area][i].padding_p
15341 + (row->glyphs[area][i].type << 2));
15343 it->max_ascent = it->max_descent = 0;
15344 it->max_phys_ascent = it->max_phys_descent = 0;
15348 /* Append one space to the glyph row of iterator IT if doing a
15349 window-based redisplay. The space has the same face as
15350 IT->face_id. Value is non-zero if a space was added.
15352 This function is called to make sure that there is always one glyph
15353 at the end of a glyph row that the cursor can be set on under
15354 window-systems. (If there weren't such a glyph we would not know
15355 how wide and tall a box cursor should be displayed).
15357 At the same time this space let's a nicely handle clearing to the
15358 end of the line if the row ends in italic text. */
15360 static int
15361 append_space_for_newline (it, default_face_p)
15362 struct it *it;
15363 int default_face_p;
15365 if (FRAME_WINDOW_P (it->f))
15367 int n = it->glyph_row->used[TEXT_AREA];
15369 if (it->glyph_row->glyphs[TEXT_AREA] + n
15370 < it->glyph_row->glyphs[1 + TEXT_AREA])
15372 /* Save some values that must not be changed.
15373 Must save IT->c and IT->len because otherwise
15374 ITERATOR_AT_END_P wouldn't work anymore after
15375 append_space_for_newline has been called. */
15376 enum display_element_type saved_what = it->what;
15377 int saved_c = it->c, saved_len = it->len;
15378 int saved_x = it->current_x;
15379 int saved_face_id = it->face_id;
15380 struct text_pos saved_pos;
15381 Lisp_Object saved_object;
15382 struct face *face;
15384 saved_object = it->object;
15385 saved_pos = it->position;
15387 it->what = IT_CHARACTER;
15388 bzero (&it->position, sizeof it->position);
15389 it->object = make_number (0);
15390 it->c = ' ';
15391 it->len = 1;
15393 if (default_face_p)
15394 it->face_id = DEFAULT_FACE_ID;
15395 else if (it->face_before_selective_p)
15396 it->face_id = it->saved_face_id;
15397 face = FACE_FROM_ID (it->f, it->face_id);
15398 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15400 PRODUCE_GLYPHS (it);
15402 it->override_ascent = -1;
15403 it->constrain_row_ascent_descent_p = 0;
15404 it->current_x = saved_x;
15405 it->object = saved_object;
15406 it->position = saved_pos;
15407 it->what = saved_what;
15408 it->face_id = saved_face_id;
15409 it->len = saved_len;
15410 it->c = saved_c;
15411 return 1;
15415 return 0;
15419 /* Extend the face of the last glyph in the text area of IT->glyph_row
15420 to the end of the display line. Called from display_line.
15421 If the glyph row is empty, add a space glyph to it so that we
15422 know the face to draw. Set the glyph row flag fill_line_p. */
15424 static void
15425 extend_face_to_end_of_line (it)
15426 struct it *it;
15428 struct face *face;
15429 struct frame *f = it->f;
15431 /* If line is already filled, do nothing. */
15432 if (it->current_x >= it->last_visible_x)
15433 return;
15435 /* Face extension extends the background and box of IT->face_id
15436 to the end of the line. If the background equals the background
15437 of the frame, we don't have to do anything. */
15438 if (it->face_before_selective_p)
15439 face = FACE_FROM_ID (it->f, it->saved_face_id);
15440 else
15441 face = FACE_FROM_ID (f, it->face_id);
15443 if (FRAME_WINDOW_P (f)
15444 && it->glyph_row->displays_text_p
15445 && face->box == FACE_NO_BOX
15446 && face->background == FRAME_BACKGROUND_PIXEL (f)
15447 && !face->stipple)
15448 return;
15450 /* Set the glyph row flag indicating that the face of the last glyph
15451 in the text area has to be drawn to the end of the text area. */
15452 it->glyph_row->fill_line_p = 1;
15454 /* If current character of IT is not ASCII, make sure we have the
15455 ASCII face. This will be automatically undone the next time
15456 get_next_display_element returns a multibyte character. Note
15457 that the character will always be single byte in unibyte text. */
15458 if (!SINGLE_BYTE_CHAR_P (it->c))
15460 it->face_id = FACE_FOR_CHAR (f, face, 0);
15463 if (FRAME_WINDOW_P (f))
15465 /* If the row is empty, add a space with the current face of IT,
15466 so that we know which face to draw. */
15467 if (it->glyph_row->used[TEXT_AREA] == 0)
15469 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15470 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15471 it->glyph_row->used[TEXT_AREA] = 1;
15474 else
15476 /* Save some values that must not be changed. */
15477 int saved_x = it->current_x;
15478 struct text_pos saved_pos;
15479 Lisp_Object saved_object;
15480 enum display_element_type saved_what = it->what;
15481 int saved_face_id = it->face_id;
15483 saved_object = it->object;
15484 saved_pos = it->position;
15486 it->what = IT_CHARACTER;
15487 bzero (&it->position, sizeof it->position);
15488 it->object = make_number (0);
15489 it->c = ' ';
15490 it->len = 1;
15491 it->face_id = face->id;
15493 PRODUCE_GLYPHS (it);
15495 while (it->current_x <= it->last_visible_x)
15496 PRODUCE_GLYPHS (it);
15498 /* Don't count these blanks really. It would let us insert a left
15499 truncation glyph below and make us set the cursor on them, maybe. */
15500 it->current_x = saved_x;
15501 it->object = saved_object;
15502 it->position = saved_pos;
15503 it->what = saved_what;
15504 it->face_id = saved_face_id;
15509 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15510 trailing whitespace. */
15512 static int
15513 trailing_whitespace_p (charpos)
15514 int charpos;
15516 int bytepos = CHAR_TO_BYTE (charpos);
15517 int c = 0;
15519 while (bytepos < ZV_BYTE
15520 && (c = FETCH_CHAR (bytepos),
15521 c == ' ' || c == '\t'))
15522 ++bytepos;
15524 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15526 if (bytepos != PT_BYTE)
15527 return 1;
15529 return 0;
15533 /* Highlight trailing whitespace, if any, in ROW. */
15535 void
15536 highlight_trailing_whitespace (f, row)
15537 struct frame *f;
15538 struct glyph_row *row;
15540 int used = row->used[TEXT_AREA];
15542 if (used)
15544 struct glyph *start = row->glyphs[TEXT_AREA];
15545 struct glyph *glyph = start + used - 1;
15547 /* Skip over glyphs inserted to display the cursor at the
15548 end of a line, for extending the face of the last glyph
15549 to the end of the line on terminals, and for truncation
15550 and continuation glyphs. */
15551 while (glyph >= start
15552 && glyph->type == CHAR_GLYPH
15553 && INTEGERP (glyph->object))
15554 --glyph;
15556 /* If last glyph is a space or stretch, and it's trailing
15557 whitespace, set the face of all trailing whitespace glyphs in
15558 IT->glyph_row to `trailing-whitespace'. */
15559 if (glyph >= start
15560 && BUFFERP (glyph->object)
15561 && (glyph->type == STRETCH_GLYPH
15562 || (glyph->type == CHAR_GLYPH
15563 && glyph->u.ch == ' '))
15564 && trailing_whitespace_p (glyph->charpos))
15566 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15567 if (face_id < 0)
15568 return;
15570 while (glyph >= start
15571 && BUFFERP (glyph->object)
15572 && (glyph->type == STRETCH_GLYPH
15573 || (glyph->type == CHAR_GLYPH
15574 && glyph->u.ch == ' ')))
15575 (glyph--)->face_id = face_id;
15581 /* Value is non-zero if glyph row ROW in window W should be
15582 used to hold the cursor. */
15584 static int
15585 cursor_row_p (w, row)
15586 struct window *w;
15587 struct glyph_row *row;
15589 int cursor_row_p = 1;
15591 if (PT == MATRIX_ROW_END_CHARPOS (row))
15593 /* If the row ends with a newline from a string, we don't want
15594 the cursor there, but we still want it at the start of the
15595 string if the string starts in this row.
15596 If the row is continued it doesn't end in a newline. */
15597 if (CHARPOS (row->end.string_pos) >= 0)
15598 cursor_row_p = (row->continued_p
15599 || PT >= MATRIX_ROW_START_CHARPOS (row));
15600 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15602 /* If the row ends in middle of a real character,
15603 and the line is continued, we want the cursor here.
15604 That's because MATRIX_ROW_END_CHARPOS would equal
15605 PT if PT is before the character. */
15606 if (!row->ends_in_ellipsis_p)
15607 cursor_row_p = row->continued_p;
15608 else
15609 /* If the row ends in an ellipsis, then
15610 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15611 We want that position to be displayed after the ellipsis. */
15612 cursor_row_p = 0;
15614 /* If the row ends at ZV, display the cursor at the end of that
15615 row instead of at the start of the row below. */
15616 else if (row->ends_at_zv_p)
15617 cursor_row_p = 1;
15618 else
15619 cursor_row_p = 0;
15622 return cursor_row_p;
15626 /* Construct the glyph row IT->glyph_row in the desired matrix of
15627 IT->w from text at the current position of IT. See dispextern.h
15628 for an overview of struct it. Value is non-zero if
15629 IT->glyph_row displays text, as opposed to a line displaying ZV
15630 only. */
15632 static int
15633 display_line (it)
15634 struct it *it;
15636 struct glyph_row *row = it->glyph_row;
15637 Lisp_Object overlay_arrow_string;
15639 /* We always start displaying at hpos zero even if hscrolled. */
15640 xassert (it->hpos == 0 && it->current_x == 0);
15642 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15643 >= it->w->desired_matrix->nrows)
15645 it->w->nrows_scale_factor++;
15646 fonts_changed_p = 1;
15647 return 0;
15650 /* Is IT->w showing the region? */
15651 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15653 /* Clear the result glyph row and enable it. */
15654 prepare_desired_row (row);
15656 row->y = it->current_y;
15657 row->start = it->start;
15658 row->continuation_lines_width = it->continuation_lines_width;
15659 row->displays_text_p = 1;
15660 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15661 it->starts_in_middle_of_char_p = 0;
15663 /* Arrange the overlays nicely for our purposes. Usually, we call
15664 display_line on only one line at a time, in which case this
15665 can't really hurt too much, or we call it on lines which appear
15666 one after another in the buffer, in which case all calls to
15667 recenter_overlay_lists but the first will be pretty cheap. */
15668 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15670 /* Move over display elements that are not visible because we are
15671 hscrolled. This may stop at an x-position < IT->first_visible_x
15672 if the first glyph is partially visible or if we hit a line end. */
15673 if (it->current_x < it->first_visible_x)
15675 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15676 MOVE_TO_POS | MOVE_TO_X);
15679 /* Get the initial row height. This is either the height of the
15680 text hscrolled, if there is any, or zero. */
15681 row->ascent = it->max_ascent;
15682 row->height = it->max_ascent + it->max_descent;
15683 row->phys_ascent = it->max_phys_ascent;
15684 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15685 row->extra_line_spacing = it->max_extra_line_spacing;
15687 /* Loop generating characters. The loop is left with IT on the next
15688 character to display. */
15689 while (1)
15691 int n_glyphs_before, hpos_before, x_before;
15692 int x, i, nglyphs;
15693 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15695 /* Retrieve the next thing to display. Value is zero if end of
15696 buffer reached. */
15697 if (!get_next_display_element (it))
15699 /* Maybe add a space at the end of this line that is used to
15700 display the cursor there under X. Set the charpos of the
15701 first glyph of blank lines not corresponding to any text
15702 to -1. */
15703 #ifdef HAVE_WINDOW_SYSTEM
15704 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15705 row->exact_window_width_line_p = 1;
15706 else
15707 #endif /* HAVE_WINDOW_SYSTEM */
15708 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15709 || row->used[TEXT_AREA] == 0)
15711 row->glyphs[TEXT_AREA]->charpos = -1;
15712 row->displays_text_p = 0;
15714 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15715 && (!MINI_WINDOW_P (it->w)
15716 || (minibuf_level && EQ (it->window, minibuf_window))))
15717 row->indicate_empty_line_p = 1;
15720 it->continuation_lines_width = 0;
15721 row->ends_at_zv_p = 1;
15722 break;
15725 /* Now, get the metrics of what we want to display. This also
15726 generates glyphs in `row' (which is IT->glyph_row). */
15727 n_glyphs_before = row->used[TEXT_AREA];
15728 x = it->current_x;
15730 /* Remember the line height so far in case the next element doesn't
15731 fit on the line. */
15732 if (!it->truncate_lines_p)
15734 ascent = it->max_ascent;
15735 descent = it->max_descent;
15736 phys_ascent = it->max_phys_ascent;
15737 phys_descent = it->max_phys_descent;
15740 PRODUCE_GLYPHS (it);
15742 /* If this display element was in marginal areas, continue with
15743 the next one. */
15744 if (it->area != TEXT_AREA)
15746 row->ascent = max (row->ascent, it->max_ascent);
15747 row->height = max (row->height, it->max_ascent + it->max_descent);
15748 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15749 row->phys_height = max (row->phys_height,
15750 it->max_phys_ascent + it->max_phys_descent);
15751 row->extra_line_spacing = max (row->extra_line_spacing,
15752 it->max_extra_line_spacing);
15753 set_iterator_to_next (it, 1);
15754 continue;
15757 /* Does the display element fit on the line? If we truncate
15758 lines, we should draw past the right edge of the window. If
15759 we don't truncate, we want to stop so that we can display the
15760 continuation glyph before the right margin. If lines are
15761 continued, there are two possible strategies for characters
15762 resulting in more than 1 glyph (e.g. tabs): Display as many
15763 glyphs as possible in this line and leave the rest for the
15764 continuation line, or display the whole element in the next
15765 line. Original redisplay did the former, so we do it also. */
15766 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15767 hpos_before = it->hpos;
15768 x_before = x;
15770 if (/* Not a newline. */
15771 nglyphs > 0
15772 /* Glyphs produced fit entirely in the line. */
15773 && it->current_x < it->last_visible_x)
15775 it->hpos += nglyphs;
15776 row->ascent = max (row->ascent, it->max_ascent);
15777 row->height = max (row->height, it->max_ascent + it->max_descent);
15778 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15779 row->phys_height = max (row->phys_height,
15780 it->max_phys_ascent + it->max_phys_descent);
15781 row->extra_line_spacing = max (row->extra_line_spacing,
15782 it->max_extra_line_spacing);
15783 if (it->current_x - it->pixel_width < it->first_visible_x)
15784 row->x = x - it->first_visible_x;
15786 else
15788 int new_x;
15789 struct glyph *glyph;
15791 for (i = 0; i < nglyphs; ++i, x = new_x)
15793 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15794 new_x = x + glyph->pixel_width;
15796 if (/* Lines are continued. */
15797 !it->truncate_lines_p
15798 && (/* Glyph doesn't fit on the line. */
15799 new_x > it->last_visible_x
15800 /* Or it fits exactly on a window system frame. */
15801 || (new_x == it->last_visible_x
15802 && FRAME_WINDOW_P (it->f))))
15804 /* End of a continued line. */
15806 if (it->hpos == 0
15807 || (new_x == it->last_visible_x
15808 && FRAME_WINDOW_P (it->f)))
15810 /* Current glyph is the only one on the line or
15811 fits exactly on the line. We must continue
15812 the line because we can't draw the cursor
15813 after the glyph. */
15814 row->continued_p = 1;
15815 it->current_x = new_x;
15816 it->continuation_lines_width += new_x;
15817 ++it->hpos;
15818 if (i == nglyphs - 1)
15820 set_iterator_to_next (it, 1);
15821 #ifdef HAVE_WINDOW_SYSTEM
15822 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15824 if (!get_next_display_element (it))
15826 row->exact_window_width_line_p = 1;
15827 it->continuation_lines_width = 0;
15828 row->continued_p = 0;
15829 row->ends_at_zv_p = 1;
15831 else if (ITERATOR_AT_END_OF_LINE_P (it))
15833 row->continued_p = 0;
15834 row->exact_window_width_line_p = 1;
15837 #endif /* HAVE_WINDOW_SYSTEM */
15840 else if (CHAR_GLYPH_PADDING_P (*glyph)
15841 && !FRAME_WINDOW_P (it->f))
15843 /* A padding glyph that doesn't fit on this line.
15844 This means the whole character doesn't fit
15845 on the line. */
15846 row->used[TEXT_AREA] = n_glyphs_before;
15848 /* Fill the rest of the row with continuation
15849 glyphs like in 20.x. */
15850 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15851 < row->glyphs[1 + TEXT_AREA])
15852 produce_special_glyphs (it, IT_CONTINUATION);
15854 row->continued_p = 1;
15855 it->current_x = x_before;
15856 it->continuation_lines_width += x_before;
15858 /* Restore the height to what it was before the
15859 element not fitting on the line. */
15860 it->max_ascent = ascent;
15861 it->max_descent = descent;
15862 it->max_phys_ascent = phys_ascent;
15863 it->max_phys_descent = phys_descent;
15865 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15867 /* A TAB that extends past the right edge of the
15868 window. This produces a single glyph on
15869 window system frames. We leave the glyph in
15870 this row and let it fill the row, but don't
15871 consume the TAB. */
15872 it->continuation_lines_width += it->last_visible_x;
15873 row->ends_in_middle_of_char_p = 1;
15874 row->continued_p = 1;
15875 glyph->pixel_width = it->last_visible_x - x;
15876 it->starts_in_middle_of_char_p = 1;
15878 else
15880 /* Something other than a TAB that draws past
15881 the right edge of the window. Restore
15882 positions to values before the element. */
15883 row->used[TEXT_AREA] = n_glyphs_before + i;
15885 /* Display continuation glyphs. */
15886 if (!FRAME_WINDOW_P (it->f))
15887 produce_special_glyphs (it, IT_CONTINUATION);
15888 row->continued_p = 1;
15890 it->current_x = x_before;
15891 it->continuation_lines_width += x;
15892 extend_face_to_end_of_line (it);
15894 if (nglyphs > 1 && i > 0)
15896 row->ends_in_middle_of_char_p = 1;
15897 it->starts_in_middle_of_char_p = 1;
15900 /* Restore the height to what it was before the
15901 element not fitting on the line. */
15902 it->max_ascent = ascent;
15903 it->max_descent = descent;
15904 it->max_phys_ascent = phys_ascent;
15905 it->max_phys_descent = phys_descent;
15908 break;
15910 else if (new_x > it->first_visible_x)
15912 /* Increment number of glyphs actually displayed. */
15913 ++it->hpos;
15915 if (x < it->first_visible_x)
15916 /* Glyph is partially visible, i.e. row starts at
15917 negative X position. */
15918 row->x = x - it->first_visible_x;
15920 else
15922 /* Glyph is completely off the left margin of the
15923 window. This should not happen because of the
15924 move_it_in_display_line at the start of this
15925 function, unless the text display area of the
15926 window is empty. */
15927 xassert (it->first_visible_x <= it->last_visible_x);
15931 row->ascent = max (row->ascent, it->max_ascent);
15932 row->height = max (row->height, it->max_ascent + it->max_descent);
15933 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15934 row->phys_height = max (row->phys_height,
15935 it->max_phys_ascent + it->max_phys_descent);
15936 row->extra_line_spacing = max (row->extra_line_spacing,
15937 it->max_extra_line_spacing);
15939 /* End of this display line if row is continued. */
15940 if (row->continued_p || row->ends_at_zv_p)
15941 break;
15944 at_end_of_line:
15945 /* Is this a line end? If yes, we're also done, after making
15946 sure that a non-default face is extended up to the right
15947 margin of the window. */
15948 if (ITERATOR_AT_END_OF_LINE_P (it))
15950 int used_before = row->used[TEXT_AREA];
15952 row->ends_in_newline_from_string_p = STRINGP (it->object);
15954 #ifdef HAVE_WINDOW_SYSTEM
15955 /* Add a space at the end of the line that is used to
15956 display the cursor there. */
15957 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15958 append_space_for_newline (it, 0);
15959 #endif /* HAVE_WINDOW_SYSTEM */
15961 /* Extend the face to the end of the line. */
15962 extend_face_to_end_of_line (it);
15964 /* Make sure we have the position. */
15965 if (used_before == 0)
15966 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15968 /* Consume the line end. This skips over invisible lines. */
15969 set_iterator_to_next (it, 1);
15970 it->continuation_lines_width = 0;
15971 break;
15974 /* Proceed with next display element. Note that this skips
15975 over lines invisible because of selective display. */
15976 set_iterator_to_next (it, 1);
15978 /* If we truncate lines, we are done when the last displayed
15979 glyphs reach past the right margin of the window. */
15980 if (it->truncate_lines_p
15981 && (FRAME_WINDOW_P (it->f)
15982 ? (it->current_x >= it->last_visible_x)
15983 : (it->current_x > it->last_visible_x)))
15985 /* Maybe add truncation glyphs. */
15986 if (!FRAME_WINDOW_P (it->f))
15988 int i, n;
15990 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15991 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15992 break;
15994 for (n = row->used[TEXT_AREA]; i < n; ++i)
15996 row->used[TEXT_AREA] = i;
15997 produce_special_glyphs (it, IT_TRUNCATION);
16000 #ifdef HAVE_WINDOW_SYSTEM
16001 else
16003 /* Don't truncate if we can overflow newline into fringe. */
16004 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16006 if (!get_next_display_element (it))
16008 it->continuation_lines_width = 0;
16009 row->ends_at_zv_p = 1;
16010 row->exact_window_width_line_p = 1;
16011 break;
16013 if (ITERATOR_AT_END_OF_LINE_P (it))
16015 row->exact_window_width_line_p = 1;
16016 goto at_end_of_line;
16020 #endif /* HAVE_WINDOW_SYSTEM */
16022 row->truncated_on_right_p = 1;
16023 it->continuation_lines_width = 0;
16024 reseat_at_next_visible_line_start (it, 0);
16025 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16026 it->hpos = hpos_before;
16027 it->current_x = x_before;
16028 break;
16032 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16033 at the left window margin. */
16034 if (it->first_visible_x
16035 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16037 if (!FRAME_WINDOW_P (it->f))
16038 insert_left_trunc_glyphs (it);
16039 row->truncated_on_left_p = 1;
16042 /* If the start of this line is the overlay arrow-position, then
16043 mark this glyph row as the one containing the overlay arrow.
16044 This is clearly a mess with variable size fonts. It would be
16045 better to let it be displayed like cursors under X. */
16046 if ((row->displays_text_p || !overlay_arrow_seen)
16047 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16048 !NILP (overlay_arrow_string)))
16050 /* Overlay arrow in window redisplay is a fringe bitmap. */
16051 if (STRINGP (overlay_arrow_string))
16053 struct glyph_row *arrow_row
16054 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16055 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16056 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16057 struct glyph *p = row->glyphs[TEXT_AREA];
16058 struct glyph *p2, *end;
16060 /* Copy the arrow glyphs. */
16061 while (glyph < arrow_end)
16062 *p++ = *glyph++;
16064 /* Throw away padding glyphs. */
16065 p2 = p;
16066 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16067 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16068 ++p2;
16069 if (p2 > p)
16071 while (p2 < end)
16072 *p++ = *p2++;
16073 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16076 else
16078 xassert (INTEGERP (overlay_arrow_string));
16079 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16081 overlay_arrow_seen = 1;
16084 /* Compute pixel dimensions of this line. */
16085 compute_line_metrics (it);
16087 /* Remember the position at which this line ends. */
16088 row->end = it->current;
16090 /* Record whether this row ends inside an ellipsis. */
16091 row->ends_in_ellipsis_p
16092 = (it->method == GET_FROM_DISPLAY_VECTOR
16093 && it->ellipsis_p);
16095 /* Save fringe bitmaps in this row. */
16096 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16097 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16098 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16099 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16101 it->left_user_fringe_bitmap = 0;
16102 it->left_user_fringe_face_id = 0;
16103 it->right_user_fringe_bitmap = 0;
16104 it->right_user_fringe_face_id = 0;
16106 /* Maybe set the cursor. */
16107 if (it->w->cursor.vpos < 0
16108 && PT >= MATRIX_ROW_START_CHARPOS (row)
16109 && PT <= MATRIX_ROW_END_CHARPOS (row)
16110 && cursor_row_p (it->w, row))
16111 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16113 /* Highlight trailing whitespace. */
16114 if (!NILP (Vshow_trailing_whitespace))
16115 highlight_trailing_whitespace (it->f, it->glyph_row);
16117 /* Prepare for the next line. This line starts horizontally at (X
16118 HPOS) = (0 0). Vertical positions are incremented. As a
16119 convenience for the caller, IT->glyph_row is set to the next
16120 row to be used. */
16121 it->current_x = it->hpos = 0;
16122 it->current_y += row->height;
16123 ++it->vpos;
16124 ++it->glyph_row;
16125 it->start = it->current;
16126 return row->displays_text_p;
16131 /***********************************************************************
16132 Menu Bar
16133 ***********************************************************************/
16135 /* Redisplay the menu bar in the frame for window W.
16137 The menu bar of X frames that don't have X toolkit support is
16138 displayed in a special window W->frame->menu_bar_window.
16140 The menu bar of terminal frames is treated specially as far as
16141 glyph matrices are concerned. Menu bar lines are not part of
16142 windows, so the update is done directly on the frame matrix rows
16143 for the menu bar. */
16145 static void
16146 display_menu_bar (w)
16147 struct window *w;
16149 struct frame *f = XFRAME (WINDOW_FRAME (w));
16150 struct it it;
16151 Lisp_Object items;
16152 int i;
16154 /* Don't do all this for graphical frames. */
16155 #ifdef HAVE_NTGUI
16156 if (!NILP (Vwindow_system))
16157 return;
16158 #endif
16159 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16160 if (FRAME_X_P (f))
16161 return;
16162 #endif
16163 #ifdef MAC_OS
16164 if (FRAME_MAC_P (f))
16165 return;
16166 #endif
16168 #ifdef USE_X_TOOLKIT
16169 xassert (!FRAME_WINDOW_P (f));
16170 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16171 it.first_visible_x = 0;
16172 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16173 #else /* not USE_X_TOOLKIT */
16174 if (FRAME_WINDOW_P (f))
16176 /* Menu bar lines are displayed in the desired matrix of the
16177 dummy window menu_bar_window. */
16178 struct window *menu_w;
16179 xassert (WINDOWP (f->menu_bar_window));
16180 menu_w = XWINDOW (f->menu_bar_window);
16181 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16182 MENU_FACE_ID);
16183 it.first_visible_x = 0;
16184 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16186 else
16188 /* This is a TTY frame, i.e. character hpos/vpos are used as
16189 pixel x/y. */
16190 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16191 MENU_FACE_ID);
16192 it.first_visible_x = 0;
16193 it.last_visible_x = FRAME_COLS (f);
16195 #endif /* not USE_X_TOOLKIT */
16197 if (! mode_line_inverse_video)
16198 /* Force the menu-bar to be displayed in the default face. */
16199 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16201 /* Clear all rows of the menu bar. */
16202 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16204 struct glyph_row *row = it.glyph_row + i;
16205 clear_glyph_row (row);
16206 row->enabled_p = 1;
16207 row->full_width_p = 1;
16210 /* Display all items of the menu bar. */
16211 items = FRAME_MENU_BAR_ITEMS (it.f);
16212 for (i = 0; i < XVECTOR (items)->size; i += 4)
16214 Lisp_Object string;
16216 /* Stop at nil string. */
16217 string = AREF (items, i + 1);
16218 if (NILP (string))
16219 break;
16221 /* Remember where item was displayed. */
16222 AREF (items, i + 3) = make_number (it.hpos);
16224 /* Display the item, pad with one space. */
16225 if (it.current_x < it.last_visible_x)
16226 display_string (NULL, string, Qnil, 0, 0, &it,
16227 SCHARS (string) + 1, 0, 0, -1);
16230 /* Fill out the line with spaces. */
16231 if (it.current_x < it.last_visible_x)
16232 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16234 /* Compute the total height of the lines. */
16235 compute_line_metrics (&it);
16240 /***********************************************************************
16241 Mode Line
16242 ***********************************************************************/
16244 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16245 FORCE is non-zero, redisplay mode lines unconditionally.
16246 Otherwise, redisplay only mode lines that are garbaged. Value is
16247 the number of windows whose mode lines were redisplayed. */
16249 static int
16250 redisplay_mode_lines (window, force)
16251 Lisp_Object window;
16252 int force;
16254 int nwindows = 0;
16256 while (!NILP (window))
16258 struct window *w = XWINDOW (window);
16260 if (WINDOWP (w->hchild))
16261 nwindows += redisplay_mode_lines (w->hchild, force);
16262 else if (WINDOWP (w->vchild))
16263 nwindows += redisplay_mode_lines (w->vchild, force);
16264 else if (force
16265 || FRAME_GARBAGED_P (XFRAME (w->frame))
16266 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16268 struct text_pos lpoint;
16269 struct buffer *old = current_buffer;
16271 /* Set the window's buffer for the mode line display. */
16272 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16273 set_buffer_internal_1 (XBUFFER (w->buffer));
16275 /* Point refers normally to the selected window. For any
16276 other window, set up appropriate value. */
16277 if (!EQ (window, selected_window))
16279 struct text_pos pt;
16281 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16282 if (CHARPOS (pt) < BEGV)
16283 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16284 else if (CHARPOS (pt) > (ZV - 1))
16285 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16286 else
16287 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16290 /* Display mode lines. */
16291 clear_glyph_matrix (w->desired_matrix);
16292 if (display_mode_lines (w))
16294 ++nwindows;
16295 w->must_be_updated_p = 1;
16298 /* Restore old settings. */
16299 set_buffer_internal_1 (old);
16300 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16303 window = w->next;
16306 return nwindows;
16310 /* Display the mode and/or top line of window W. Value is the number
16311 of mode lines displayed. */
16313 static int
16314 display_mode_lines (w)
16315 struct window *w;
16317 Lisp_Object old_selected_window, old_selected_frame;
16318 int n = 0;
16320 old_selected_frame = selected_frame;
16321 selected_frame = w->frame;
16322 old_selected_window = selected_window;
16323 XSETWINDOW (selected_window, w);
16325 /* These will be set while the mode line specs are processed. */
16326 line_number_displayed = 0;
16327 w->column_number_displayed = Qnil;
16329 if (WINDOW_WANTS_MODELINE_P (w))
16331 struct window *sel_w = XWINDOW (old_selected_window);
16333 /* Select mode line face based on the real selected window. */
16334 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16335 current_buffer->mode_line_format);
16336 ++n;
16339 if (WINDOW_WANTS_HEADER_LINE_P (w))
16341 display_mode_line (w, HEADER_LINE_FACE_ID,
16342 current_buffer->header_line_format);
16343 ++n;
16346 selected_frame = old_selected_frame;
16347 selected_window = old_selected_window;
16348 return n;
16352 /* Display mode or top line of window W. FACE_ID specifies which line
16353 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16354 FORMAT is the mode line format to display. Value is the pixel
16355 height of the mode line displayed. */
16357 static int
16358 display_mode_line (w, face_id, format)
16359 struct window *w;
16360 enum face_id face_id;
16361 Lisp_Object format;
16363 struct it it;
16364 struct face *face;
16365 int count = SPECPDL_INDEX ();
16367 init_iterator (&it, w, -1, -1, NULL, face_id);
16368 prepare_desired_row (it.glyph_row);
16370 it.glyph_row->mode_line_p = 1;
16372 if (! mode_line_inverse_video)
16373 /* Force the mode-line to be displayed in the default face. */
16374 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16376 record_unwind_protect (unwind_format_mode_line,
16377 format_mode_line_unwind_data (NULL, 0));
16379 mode_line_target = MODE_LINE_DISPLAY;
16381 /* Temporarily make frame's keyboard the current kboard so that
16382 kboard-local variables in the mode_line_format will get the right
16383 values. */
16384 push_kboard (FRAME_KBOARD (it.f));
16385 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16386 pop_kboard ();
16388 unbind_to (count, Qnil);
16390 /* Fill up with spaces. */
16391 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16393 compute_line_metrics (&it);
16394 it.glyph_row->full_width_p = 1;
16395 it.glyph_row->continued_p = 0;
16396 it.glyph_row->truncated_on_left_p = 0;
16397 it.glyph_row->truncated_on_right_p = 0;
16399 /* Make a 3D mode-line have a shadow at its right end. */
16400 face = FACE_FROM_ID (it.f, face_id);
16401 extend_face_to_end_of_line (&it);
16402 if (face->box != FACE_NO_BOX)
16404 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16405 + it.glyph_row->used[TEXT_AREA] - 1);
16406 last->right_box_line_p = 1;
16409 return it.glyph_row->height;
16412 /* Move element ELT in LIST to the front of LIST.
16413 Return the updated list. */
16415 static Lisp_Object
16416 move_elt_to_front (elt, list)
16417 Lisp_Object elt, list;
16419 register Lisp_Object tail, prev;
16420 register Lisp_Object tem;
16422 tail = list;
16423 prev = Qnil;
16424 while (CONSP (tail))
16426 tem = XCAR (tail);
16428 if (EQ (elt, tem))
16430 /* Splice out the link TAIL. */
16431 if (NILP (prev))
16432 list = XCDR (tail);
16433 else
16434 Fsetcdr (prev, XCDR (tail));
16436 /* Now make it the first. */
16437 Fsetcdr (tail, list);
16438 return tail;
16440 else
16441 prev = tail;
16442 tail = XCDR (tail);
16443 QUIT;
16446 /* Not found--return unchanged LIST. */
16447 return list;
16450 /* Contribute ELT to the mode line for window IT->w. How it
16451 translates into text depends on its data type.
16453 IT describes the display environment in which we display, as usual.
16455 DEPTH is the depth in recursion. It is used to prevent
16456 infinite recursion here.
16458 FIELD_WIDTH is the number of characters the display of ELT should
16459 occupy in the mode line, and PRECISION is the maximum number of
16460 characters to display from ELT's representation. See
16461 display_string for details.
16463 Returns the hpos of the end of the text generated by ELT.
16465 PROPS is a property list to add to any string we encounter.
16467 If RISKY is nonzero, remove (disregard) any properties in any string
16468 we encounter, and ignore :eval and :propertize.
16470 The global variable `mode_line_target' determines whether the
16471 output is passed to `store_mode_line_noprop',
16472 `store_mode_line_string', or `display_string'. */
16474 static int
16475 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16476 struct it *it;
16477 int depth;
16478 int field_width, precision;
16479 Lisp_Object elt, props;
16480 int risky;
16482 int n = 0, field, prec;
16483 int literal = 0;
16485 tail_recurse:
16486 if (depth > 100)
16487 elt = build_string ("*too-deep*");
16489 depth++;
16491 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16493 case Lisp_String:
16495 /* A string: output it and check for %-constructs within it. */
16496 unsigned char c;
16497 int offset = 0;
16499 if (SCHARS (elt) > 0
16500 && (!NILP (props) || risky))
16502 Lisp_Object oprops, aelt;
16503 oprops = Ftext_properties_at (make_number (0), elt);
16505 /* If the starting string's properties are not what
16506 we want, translate the string. Also, if the string
16507 is risky, do that anyway. */
16509 if (NILP (Fequal (props, oprops)) || risky)
16511 /* If the starting string has properties,
16512 merge the specified ones onto the existing ones. */
16513 if (! NILP (oprops) && !risky)
16515 Lisp_Object tem;
16517 oprops = Fcopy_sequence (oprops);
16518 tem = props;
16519 while (CONSP (tem))
16521 oprops = Fplist_put (oprops, XCAR (tem),
16522 XCAR (XCDR (tem)));
16523 tem = XCDR (XCDR (tem));
16525 props = oprops;
16528 aelt = Fassoc (elt, mode_line_proptrans_alist);
16529 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16531 /* AELT is what we want. Move it to the front
16532 without consing. */
16533 elt = XCAR (aelt);
16534 mode_line_proptrans_alist
16535 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16537 else
16539 Lisp_Object tem;
16541 /* If AELT has the wrong props, it is useless.
16542 so get rid of it. */
16543 if (! NILP (aelt))
16544 mode_line_proptrans_alist
16545 = Fdelq (aelt, mode_line_proptrans_alist);
16547 elt = Fcopy_sequence (elt);
16548 Fset_text_properties (make_number (0), Flength (elt),
16549 props, elt);
16550 /* Add this item to mode_line_proptrans_alist. */
16551 mode_line_proptrans_alist
16552 = Fcons (Fcons (elt, props),
16553 mode_line_proptrans_alist);
16554 /* Truncate mode_line_proptrans_alist
16555 to at most 50 elements. */
16556 tem = Fnthcdr (make_number (50),
16557 mode_line_proptrans_alist);
16558 if (! NILP (tem))
16559 XSETCDR (tem, Qnil);
16564 offset = 0;
16566 if (literal)
16568 prec = precision - n;
16569 switch (mode_line_target)
16571 case MODE_LINE_NOPROP:
16572 case MODE_LINE_TITLE:
16573 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16574 break;
16575 case MODE_LINE_STRING:
16576 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16577 break;
16578 case MODE_LINE_DISPLAY:
16579 n += display_string (NULL, elt, Qnil, 0, 0, it,
16580 0, prec, 0, STRING_MULTIBYTE (elt));
16581 break;
16584 break;
16587 /* Handle the non-literal case. */
16589 while ((precision <= 0 || n < precision)
16590 && SREF (elt, offset) != 0
16591 && (mode_line_target != MODE_LINE_DISPLAY
16592 || it->current_x < it->last_visible_x))
16594 int last_offset = offset;
16596 /* Advance to end of string or next format specifier. */
16597 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16600 if (offset - 1 != last_offset)
16602 int nchars, nbytes;
16604 /* Output to end of string or up to '%'. Field width
16605 is length of string. Don't output more than
16606 PRECISION allows us. */
16607 offset--;
16609 prec = c_string_width (SDATA (elt) + last_offset,
16610 offset - last_offset, precision - n,
16611 &nchars, &nbytes);
16613 switch (mode_line_target)
16615 case MODE_LINE_NOPROP:
16616 case MODE_LINE_TITLE:
16617 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16618 break;
16619 case MODE_LINE_STRING:
16621 int bytepos = last_offset;
16622 int charpos = string_byte_to_char (elt, bytepos);
16623 int endpos = (precision <= 0
16624 ? string_byte_to_char (elt, offset)
16625 : charpos + nchars);
16627 n += store_mode_line_string (NULL,
16628 Fsubstring (elt, make_number (charpos),
16629 make_number (endpos)),
16630 0, 0, 0, Qnil);
16632 break;
16633 case MODE_LINE_DISPLAY:
16635 int bytepos = last_offset;
16636 int charpos = string_byte_to_char (elt, bytepos);
16638 if (precision <= 0)
16639 nchars = string_byte_to_char (elt, offset) - charpos;
16640 n += display_string (NULL, elt, Qnil, 0, charpos,
16641 it, 0, nchars, 0,
16642 STRING_MULTIBYTE (elt));
16644 break;
16647 else /* c == '%' */
16649 int percent_position = offset;
16651 /* Get the specified minimum width. Zero means
16652 don't pad. */
16653 field = 0;
16654 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16655 field = field * 10 + c - '0';
16657 /* Don't pad beyond the total padding allowed. */
16658 if (field_width - n > 0 && field > field_width - n)
16659 field = field_width - n;
16661 /* Note that either PRECISION <= 0 or N < PRECISION. */
16662 prec = precision - n;
16664 if (c == 'M')
16665 n += display_mode_element (it, depth, field, prec,
16666 Vglobal_mode_string, props,
16667 risky);
16668 else if (c != 0)
16670 int multibyte;
16671 int bytepos, charpos;
16672 unsigned char *spec;
16674 bytepos = percent_position;
16675 charpos = (STRING_MULTIBYTE (elt)
16676 ? string_byte_to_char (elt, bytepos)
16677 : bytepos);
16679 spec
16680 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16682 switch (mode_line_target)
16684 case MODE_LINE_NOPROP:
16685 case MODE_LINE_TITLE:
16686 n += store_mode_line_noprop (spec, field, prec);
16687 break;
16688 case MODE_LINE_STRING:
16690 int len = strlen (spec);
16691 Lisp_Object tem = make_string (spec, len);
16692 props = Ftext_properties_at (make_number (charpos), elt);
16693 /* Should only keep face property in props */
16694 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16696 break;
16697 case MODE_LINE_DISPLAY:
16699 int nglyphs_before, nwritten;
16701 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16702 nwritten = display_string (spec, Qnil, elt,
16703 charpos, 0, it,
16704 field, prec, 0,
16705 multibyte);
16707 /* Assign to the glyphs written above the
16708 string where the `%x' came from, position
16709 of the `%'. */
16710 if (nwritten > 0)
16712 struct glyph *glyph
16713 = (it->glyph_row->glyphs[TEXT_AREA]
16714 + nglyphs_before);
16715 int i;
16717 for (i = 0; i < nwritten; ++i)
16719 glyph[i].object = elt;
16720 glyph[i].charpos = charpos;
16723 n += nwritten;
16726 break;
16729 else /* c == 0 */
16730 break;
16734 break;
16736 case Lisp_Symbol:
16737 /* A symbol: process the value of the symbol recursively
16738 as if it appeared here directly. Avoid error if symbol void.
16739 Special case: if value of symbol is a string, output the string
16740 literally. */
16742 register Lisp_Object tem;
16744 /* If the variable is not marked as risky to set
16745 then its contents are risky to use. */
16746 if (NILP (Fget (elt, Qrisky_local_variable)))
16747 risky = 1;
16749 tem = Fboundp (elt);
16750 if (!NILP (tem))
16752 tem = Fsymbol_value (elt);
16753 /* If value is a string, output that string literally:
16754 don't check for % within it. */
16755 if (STRINGP (tem))
16756 literal = 1;
16758 if (!EQ (tem, elt))
16760 /* Give up right away for nil or t. */
16761 elt = tem;
16762 goto tail_recurse;
16766 break;
16768 case Lisp_Cons:
16770 register Lisp_Object car, tem;
16772 /* A cons cell: five distinct cases.
16773 If first element is :eval or :propertize, do something special.
16774 If first element is a string or a cons, process all the elements
16775 and effectively concatenate them.
16776 If first element is a negative number, truncate displaying cdr to
16777 at most that many characters. If positive, pad (with spaces)
16778 to at least that many characters.
16779 If first element is a symbol, process the cadr or caddr recursively
16780 according to whether the symbol's value is non-nil or nil. */
16781 car = XCAR (elt);
16782 if (EQ (car, QCeval))
16784 /* An element of the form (:eval FORM) means evaluate FORM
16785 and use the result as mode line elements. */
16787 if (risky)
16788 break;
16790 if (CONSP (XCDR (elt)))
16792 Lisp_Object spec;
16793 spec = safe_eval (XCAR (XCDR (elt)));
16794 n += display_mode_element (it, depth, field_width - n,
16795 precision - n, spec, props,
16796 risky);
16799 else if (EQ (car, QCpropertize))
16801 /* An element of the form (:propertize ELT PROPS...)
16802 means display ELT but applying properties PROPS. */
16804 if (risky)
16805 break;
16807 if (CONSP (XCDR (elt)))
16808 n += display_mode_element (it, depth, field_width - n,
16809 precision - n, XCAR (XCDR (elt)),
16810 XCDR (XCDR (elt)), risky);
16812 else if (SYMBOLP (car))
16814 tem = Fboundp (car);
16815 elt = XCDR (elt);
16816 if (!CONSP (elt))
16817 goto invalid;
16818 /* elt is now the cdr, and we know it is a cons cell.
16819 Use its car if CAR has a non-nil value. */
16820 if (!NILP (tem))
16822 tem = Fsymbol_value (car);
16823 if (!NILP (tem))
16825 elt = XCAR (elt);
16826 goto tail_recurse;
16829 /* Symbol's value is nil (or symbol is unbound)
16830 Get the cddr of the original list
16831 and if possible find the caddr and use that. */
16832 elt = XCDR (elt);
16833 if (NILP (elt))
16834 break;
16835 else if (!CONSP (elt))
16836 goto invalid;
16837 elt = XCAR (elt);
16838 goto tail_recurse;
16840 else if (INTEGERP (car))
16842 register int lim = XINT (car);
16843 elt = XCDR (elt);
16844 if (lim < 0)
16846 /* Negative int means reduce maximum width. */
16847 if (precision <= 0)
16848 precision = -lim;
16849 else
16850 precision = min (precision, -lim);
16852 else if (lim > 0)
16854 /* Padding specified. Don't let it be more than
16855 current maximum. */
16856 if (precision > 0)
16857 lim = min (precision, lim);
16859 /* If that's more padding than already wanted, queue it.
16860 But don't reduce padding already specified even if
16861 that is beyond the current truncation point. */
16862 field_width = max (lim, field_width);
16864 goto tail_recurse;
16866 else if (STRINGP (car) || CONSP (car))
16868 register int limit = 50;
16869 /* Limit is to protect against circular lists. */
16870 while (CONSP (elt)
16871 && --limit > 0
16872 && (precision <= 0 || n < precision))
16874 n += display_mode_element (it, depth,
16875 /* Do padding only after the last
16876 element in the list. */
16877 (! CONSP (XCDR (elt))
16878 ? field_width - n
16879 : 0),
16880 precision - n, XCAR (elt),
16881 props, risky);
16882 elt = XCDR (elt);
16886 break;
16888 default:
16889 invalid:
16890 elt = build_string ("*invalid*");
16891 goto tail_recurse;
16894 /* Pad to FIELD_WIDTH. */
16895 if (field_width > 0 && n < field_width)
16897 switch (mode_line_target)
16899 case MODE_LINE_NOPROP:
16900 case MODE_LINE_TITLE:
16901 n += store_mode_line_noprop ("", field_width - n, 0);
16902 break;
16903 case MODE_LINE_STRING:
16904 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16905 break;
16906 case MODE_LINE_DISPLAY:
16907 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16908 0, 0, 0);
16909 break;
16913 return n;
16916 /* Store a mode-line string element in mode_line_string_list.
16918 If STRING is non-null, display that C string. Otherwise, the Lisp
16919 string LISP_STRING is displayed.
16921 FIELD_WIDTH is the minimum number of output glyphs to produce.
16922 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16923 with spaces. FIELD_WIDTH <= 0 means don't pad.
16925 PRECISION is the maximum number of characters to output from
16926 STRING. PRECISION <= 0 means don't truncate the string.
16928 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16929 properties to the string.
16931 PROPS are the properties to add to the string.
16932 The mode_line_string_face face property is always added to the string.
16935 static int
16936 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16937 char *string;
16938 Lisp_Object lisp_string;
16939 int copy_string;
16940 int field_width;
16941 int precision;
16942 Lisp_Object props;
16944 int len;
16945 int n = 0;
16947 if (string != NULL)
16949 len = strlen (string);
16950 if (precision > 0 && len > precision)
16951 len = precision;
16952 lisp_string = make_string (string, len);
16953 if (NILP (props))
16954 props = mode_line_string_face_prop;
16955 else if (!NILP (mode_line_string_face))
16957 Lisp_Object face = Fplist_get (props, Qface);
16958 props = Fcopy_sequence (props);
16959 if (NILP (face))
16960 face = mode_line_string_face;
16961 else
16962 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16963 props = Fplist_put (props, Qface, face);
16965 Fadd_text_properties (make_number (0), make_number (len),
16966 props, lisp_string);
16968 else
16970 len = XFASTINT (Flength (lisp_string));
16971 if (precision > 0 && len > precision)
16973 len = precision;
16974 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16975 precision = -1;
16977 if (!NILP (mode_line_string_face))
16979 Lisp_Object face;
16980 if (NILP (props))
16981 props = Ftext_properties_at (make_number (0), lisp_string);
16982 face = Fplist_get (props, Qface);
16983 if (NILP (face))
16984 face = mode_line_string_face;
16985 else
16986 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16987 props = Fcons (Qface, Fcons (face, Qnil));
16988 if (copy_string)
16989 lisp_string = Fcopy_sequence (lisp_string);
16991 if (!NILP (props))
16992 Fadd_text_properties (make_number (0), make_number (len),
16993 props, lisp_string);
16996 if (len > 0)
16998 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16999 n += len;
17002 if (field_width > len)
17004 field_width -= len;
17005 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17006 if (!NILP (props))
17007 Fadd_text_properties (make_number (0), make_number (field_width),
17008 props, lisp_string);
17009 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17010 n += field_width;
17013 return n;
17017 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17018 1, 4, 0,
17019 doc: /* Format a string out of a mode line format specification.
17020 First arg FORMAT specifies the mode line format (see `mode-line-format'
17021 for details) to use.
17023 Optional second arg FACE specifies the face property to put
17024 on all characters for which no face is specified.
17025 t means whatever face the window's mode line currently uses
17026 \(either `mode-line' or `mode-line-inactive', depending).
17027 nil means the default is no face property.
17028 If FACE is an integer, the value string has no text properties.
17030 Optional third and fourth args WINDOW and BUFFER specify the window
17031 and buffer to use as the context for the formatting (defaults
17032 are the selected window and the window's buffer). */)
17033 (format, face, window, buffer)
17034 Lisp_Object format, face, window, buffer;
17036 struct it it;
17037 int len;
17038 struct window *w;
17039 struct buffer *old_buffer = NULL;
17040 int face_id = -1;
17041 int no_props = INTEGERP (face);
17042 int count = SPECPDL_INDEX ();
17043 Lisp_Object str;
17044 int string_start = 0;
17046 if (NILP (window))
17047 window = selected_window;
17048 CHECK_WINDOW (window);
17049 w = XWINDOW (window);
17051 if (NILP (buffer))
17052 buffer = w->buffer;
17053 CHECK_BUFFER (buffer);
17055 if (NILP (format))
17056 return build_string ("");
17058 if (no_props)
17059 face = Qnil;
17061 if (!NILP (face))
17063 if (EQ (face, Qt))
17064 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17065 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17068 if (face_id < 0)
17069 face_id = DEFAULT_FACE_ID;
17071 if (XBUFFER (buffer) != current_buffer)
17072 old_buffer = current_buffer;
17074 /* Save things including mode_line_proptrans_alist,
17075 and set that to nil so that we don't alter the outer value. */
17076 record_unwind_protect (unwind_format_mode_line,
17077 format_mode_line_unwind_data (old_buffer, 1));
17078 mode_line_proptrans_alist = Qnil;
17080 if (old_buffer)
17081 set_buffer_internal_1 (XBUFFER (buffer));
17083 init_iterator (&it, w, -1, -1, NULL, face_id);
17085 if (no_props)
17087 mode_line_target = MODE_LINE_NOPROP;
17088 mode_line_string_face_prop = Qnil;
17089 mode_line_string_list = Qnil;
17090 string_start = MODE_LINE_NOPROP_LEN (0);
17092 else
17094 mode_line_target = MODE_LINE_STRING;
17095 mode_line_string_list = Qnil;
17096 mode_line_string_face = face;
17097 mode_line_string_face_prop
17098 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17101 push_kboard (FRAME_KBOARD (it.f));
17102 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17103 pop_kboard ();
17105 if (no_props)
17107 len = MODE_LINE_NOPROP_LEN (string_start);
17108 str = make_string (mode_line_noprop_buf + string_start, len);
17110 else
17112 mode_line_string_list = Fnreverse (mode_line_string_list);
17113 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17114 make_string ("", 0));
17117 unbind_to (count, Qnil);
17118 return str;
17121 /* Write a null-terminated, right justified decimal representation of
17122 the positive integer D to BUF using a minimal field width WIDTH. */
17124 static void
17125 pint2str (buf, width, d)
17126 register char *buf;
17127 register int width;
17128 register int d;
17130 register char *p = buf;
17132 if (d <= 0)
17133 *p++ = '0';
17134 else
17136 while (d > 0)
17138 *p++ = d % 10 + '0';
17139 d /= 10;
17143 for (width -= (int) (p - buf); width > 0; --width)
17144 *p++ = ' ';
17145 *p-- = '\0';
17146 while (p > buf)
17148 d = *buf;
17149 *buf++ = *p;
17150 *p-- = d;
17154 /* Write a null-terminated, right justified decimal and "human
17155 readable" representation of the nonnegative integer D to BUF using
17156 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17158 static const char power_letter[] =
17160 0, /* not used */
17161 'k', /* kilo */
17162 'M', /* mega */
17163 'G', /* giga */
17164 'T', /* tera */
17165 'P', /* peta */
17166 'E', /* exa */
17167 'Z', /* zetta */
17168 'Y' /* yotta */
17171 static void
17172 pint2hrstr (buf, width, d)
17173 char *buf;
17174 int width;
17175 int d;
17177 /* We aim to represent the nonnegative integer D as
17178 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17179 int quotient = d;
17180 int remainder = 0;
17181 /* -1 means: do not use TENTHS. */
17182 int tenths = -1;
17183 int exponent = 0;
17185 /* Length of QUOTIENT.TENTHS as a string. */
17186 int length;
17188 char * psuffix;
17189 char * p;
17191 if (1000 <= quotient)
17193 /* Scale to the appropriate EXPONENT. */
17196 remainder = quotient % 1000;
17197 quotient /= 1000;
17198 exponent++;
17200 while (1000 <= quotient);
17202 /* Round to nearest and decide whether to use TENTHS or not. */
17203 if (quotient <= 9)
17205 tenths = remainder / 100;
17206 if (50 <= remainder % 100)
17208 if (tenths < 9)
17209 tenths++;
17210 else
17212 quotient++;
17213 if (quotient == 10)
17214 tenths = -1;
17215 else
17216 tenths = 0;
17220 else
17221 if (500 <= remainder)
17223 if (quotient < 999)
17224 quotient++;
17225 else
17227 quotient = 1;
17228 exponent++;
17229 tenths = 0;
17234 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17235 if (tenths == -1 && quotient <= 99)
17236 if (quotient <= 9)
17237 length = 1;
17238 else
17239 length = 2;
17240 else
17241 length = 3;
17242 p = psuffix = buf + max (width, length);
17244 /* Print EXPONENT. */
17245 if (exponent)
17246 *psuffix++ = power_letter[exponent];
17247 *psuffix = '\0';
17249 /* Print TENTHS. */
17250 if (tenths >= 0)
17252 *--p = '0' + tenths;
17253 *--p = '.';
17256 /* Print QUOTIENT. */
17259 int digit = quotient % 10;
17260 *--p = '0' + digit;
17262 while ((quotient /= 10) != 0);
17264 /* Print leading spaces. */
17265 while (buf < p)
17266 *--p = ' ';
17269 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17270 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17271 type of CODING_SYSTEM. Return updated pointer into BUF. */
17273 static unsigned char invalid_eol_type[] = "(*invalid*)";
17275 static char *
17276 decode_mode_spec_coding (coding_system, buf, eol_flag)
17277 Lisp_Object coding_system;
17278 register char *buf;
17279 int eol_flag;
17281 Lisp_Object val;
17282 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17283 const unsigned char *eol_str;
17284 int eol_str_len;
17285 /* The EOL conversion we are using. */
17286 Lisp_Object eoltype;
17288 val = Fget (coding_system, Qcoding_system);
17289 eoltype = Qnil;
17291 if (!VECTORP (val)) /* Not yet decided. */
17293 if (multibyte)
17294 *buf++ = '-';
17295 if (eol_flag)
17296 eoltype = eol_mnemonic_undecided;
17297 /* Don't mention EOL conversion if it isn't decided. */
17299 else
17301 Lisp_Object eolvalue;
17303 eolvalue = Fget (coding_system, Qeol_type);
17305 if (multibyte)
17306 *buf++ = XFASTINT (AREF (val, 1));
17308 if (eol_flag)
17310 /* The EOL conversion that is normal on this system. */
17312 if (NILP (eolvalue)) /* Not yet decided. */
17313 eoltype = eol_mnemonic_undecided;
17314 else if (VECTORP (eolvalue)) /* Not yet decided. */
17315 eoltype = eol_mnemonic_undecided;
17316 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17317 eoltype = (XFASTINT (eolvalue) == 0
17318 ? eol_mnemonic_unix
17319 : (XFASTINT (eolvalue) == 1
17320 ? eol_mnemonic_dos : eol_mnemonic_mac));
17324 if (eol_flag)
17326 /* Mention the EOL conversion if it is not the usual one. */
17327 if (STRINGP (eoltype))
17329 eol_str = SDATA (eoltype);
17330 eol_str_len = SBYTES (eoltype);
17332 else if (INTEGERP (eoltype)
17333 && CHAR_VALID_P (XINT (eoltype), 0))
17335 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17336 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17337 eol_str = tmp;
17339 else
17341 eol_str = invalid_eol_type;
17342 eol_str_len = sizeof (invalid_eol_type) - 1;
17344 bcopy (eol_str, buf, eol_str_len);
17345 buf += eol_str_len;
17348 return buf;
17351 /* Return a string for the output of a mode line %-spec for window W,
17352 generated by character C. PRECISION >= 0 means don't return a
17353 string longer than that value. FIELD_WIDTH > 0 means pad the
17354 string returned with spaces to that value. Return 1 in *MULTIBYTE
17355 if the result is multibyte text.
17357 Note we operate on the current buffer for most purposes,
17358 the exception being w->base_line_pos. */
17360 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17362 static char *
17363 decode_mode_spec (w, c, field_width, precision, multibyte)
17364 struct window *w;
17365 register int c;
17366 int field_width, precision;
17367 int *multibyte;
17369 Lisp_Object obj;
17370 struct frame *f = XFRAME (WINDOW_FRAME (w));
17371 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17372 struct buffer *b = current_buffer;
17374 obj = Qnil;
17375 *multibyte = 0;
17377 switch (c)
17379 case '*':
17380 if (!NILP (b->read_only))
17381 return "%";
17382 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17383 return "*";
17384 return "-";
17386 case '+':
17387 /* This differs from %* only for a modified read-only buffer. */
17388 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17389 return "*";
17390 if (!NILP (b->read_only))
17391 return "%";
17392 return "-";
17394 case '&':
17395 /* This differs from %* in ignoring read-only-ness. */
17396 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17397 return "*";
17398 return "-";
17400 case '%':
17401 return "%";
17403 case '[':
17405 int i;
17406 char *p;
17408 if (command_loop_level > 5)
17409 return "[[[... ";
17410 p = decode_mode_spec_buf;
17411 for (i = 0; i < command_loop_level; i++)
17412 *p++ = '[';
17413 *p = 0;
17414 return decode_mode_spec_buf;
17417 case ']':
17419 int i;
17420 char *p;
17422 if (command_loop_level > 5)
17423 return " ...]]]";
17424 p = decode_mode_spec_buf;
17425 for (i = 0; i < command_loop_level; i++)
17426 *p++ = ']';
17427 *p = 0;
17428 return decode_mode_spec_buf;
17431 case '-':
17433 register int i;
17435 /* Let lots_of_dashes be a string of infinite length. */
17436 if (mode_line_target == MODE_LINE_NOPROP ||
17437 mode_line_target == MODE_LINE_STRING)
17438 return "--";
17439 if (field_width <= 0
17440 || field_width > sizeof (lots_of_dashes))
17442 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17443 decode_mode_spec_buf[i] = '-';
17444 decode_mode_spec_buf[i] = '\0';
17445 return decode_mode_spec_buf;
17447 else
17448 return lots_of_dashes;
17451 case 'b':
17452 obj = b->name;
17453 break;
17455 case 'c':
17457 int col = (int) current_column (); /* iftc */
17458 w->column_number_displayed = make_number (col);
17459 pint2str (decode_mode_spec_buf, field_width, col);
17460 return decode_mode_spec_buf;
17463 case 'e':
17464 #ifndef SYSTEM_MALLOC
17466 if (NILP (Vmemory_full))
17467 return "";
17468 else
17469 return "!MEM FULL! ";
17471 #else
17472 return "";
17473 #endif
17475 case 'F':
17476 /* %F displays the frame name. */
17477 if (!NILP (f->title))
17478 return (char *) SDATA (f->title);
17479 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17480 return (char *) SDATA (f->name);
17481 return "Emacs";
17483 case 'f':
17484 obj = b->filename;
17485 break;
17487 case 'i':
17489 int size = ZV - BEGV;
17490 pint2str (decode_mode_spec_buf, field_width, size);
17491 return decode_mode_spec_buf;
17494 case 'I':
17496 int size = ZV - BEGV;
17497 pint2hrstr (decode_mode_spec_buf, field_width, size);
17498 return decode_mode_spec_buf;
17501 case 'l':
17503 int startpos = XMARKER (w->start)->charpos;
17504 int startpos_byte = marker_byte_position (w->start);
17505 int line, linepos, linepos_byte, topline;
17506 int nlines, junk;
17507 int height = WINDOW_TOTAL_LINES (w);
17509 /* If we decided that this buffer isn't suitable for line numbers,
17510 don't forget that too fast. */
17511 if (EQ (w->base_line_pos, w->buffer))
17512 goto no_value;
17513 /* But do forget it, if the window shows a different buffer now. */
17514 else if (BUFFERP (w->base_line_pos))
17515 w->base_line_pos = Qnil;
17517 /* If the buffer is very big, don't waste time. */
17518 if (INTEGERP (Vline_number_display_limit)
17519 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17521 w->base_line_pos = Qnil;
17522 w->base_line_number = Qnil;
17523 goto no_value;
17526 if (!NILP (w->base_line_number)
17527 && !NILP (w->base_line_pos)
17528 && XFASTINT (w->base_line_pos) <= startpos)
17530 line = XFASTINT (w->base_line_number);
17531 linepos = XFASTINT (w->base_line_pos);
17532 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17534 else
17536 line = 1;
17537 linepos = BUF_BEGV (b);
17538 linepos_byte = BUF_BEGV_BYTE (b);
17541 /* Count lines from base line to window start position. */
17542 nlines = display_count_lines (linepos, linepos_byte,
17543 startpos_byte,
17544 startpos, &junk);
17546 topline = nlines + line;
17548 /* Determine a new base line, if the old one is too close
17549 or too far away, or if we did not have one.
17550 "Too close" means it's plausible a scroll-down would
17551 go back past it. */
17552 if (startpos == BUF_BEGV (b))
17554 w->base_line_number = make_number (topline);
17555 w->base_line_pos = make_number (BUF_BEGV (b));
17557 else if (nlines < height + 25 || nlines > height * 3 + 50
17558 || linepos == BUF_BEGV (b))
17560 int limit = BUF_BEGV (b);
17561 int limit_byte = BUF_BEGV_BYTE (b);
17562 int position;
17563 int distance = (height * 2 + 30) * line_number_display_limit_width;
17565 if (startpos - distance > limit)
17567 limit = startpos - distance;
17568 limit_byte = CHAR_TO_BYTE (limit);
17571 nlines = display_count_lines (startpos, startpos_byte,
17572 limit_byte,
17573 - (height * 2 + 30),
17574 &position);
17575 /* If we couldn't find the lines we wanted within
17576 line_number_display_limit_width chars per line,
17577 give up on line numbers for this window. */
17578 if (position == limit_byte && limit == startpos - distance)
17580 w->base_line_pos = w->buffer;
17581 w->base_line_number = Qnil;
17582 goto no_value;
17585 w->base_line_number = make_number (topline - nlines);
17586 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17589 /* Now count lines from the start pos to point. */
17590 nlines = display_count_lines (startpos, startpos_byte,
17591 PT_BYTE, PT, &junk);
17593 /* Record that we did display the line number. */
17594 line_number_displayed = 1;
17596 /* Make the string to show. */
17597 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17598 return decode_mode_spec_buf;
17599 no_value:
17601 char* p = decode_mode_spec_buf;
17602 int pad = field_width - 2;
17603 while (pad-- > 0)
17604 *p++ = ' ';
17605 *p++ = '?';
17606 *p++ = '?';
17607 *p = '\0';
17608 return decode_mode_spec_buf;
17611 break;
17613 case 'm':
17614 obj = b->mode_name;
17615 break;
17617 case 'n':
17618 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17619 return " Narrow";
17620 break;
17622 case 'p':
17624 int pos = marker_position (w->start);
17625 int total = BUF_ZV (b) - BUF_BEGV (b);
17627 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17629 if (pos <= BUF_BEGV (b))
17630 return "All";
17631 else
17632 return "Bottom";
17634 else if (pos <= BUF_BEGV (b))
17635 return "Top";
17636 else
17638 if (total > 1000000)
17639 /* Do it differently for a large value, to avoid overflow. */
17640 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17641 else
17642 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17643 /* We can't normally display a 3-digit number,
17644 so get us a 2-digit number that is close. */
17645 if (total == 100)
17646 total = 99;
17647 sprintf (decode_mode_spec_buf, "%2d%%", total);
17648 return decode_mode_spec_buf;
17652 /* Display percentage of size above the bottom of the screen. */
17653 case 'P':
17655 int toppos = marker_position (w->start);
17656 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17657 int total = BUF_ZV (b) - BUF_BEGV (b);
17659 if (botpos >= BUF_ZV (b))
17661 if (toppos <= BUF_BEGV (b))
17662 return "All";
17663 else
17664 return "Bottom";
17666 else
17668 if (total > 1000000)
17669 /* Do it differently for a large value, to avoid overflow. */
17670 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17671 else
17672 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17673 /* We can't normally display a 3-digit number,
17674 so get us a 2-digit number that is close. */
17675 if (total == 100)
17676 total = 99;
17677 if (toppos <= BUF_BEGV (b))
17678 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17679 else
17680 sprintf (decode_mode_spec_buf, "%2d%%", total);
17681 return decode_mode_spec_buf;
17685 case 's':
17686 /* status of process */
17687 obj = Fget_buffer_process (Fcurrent_buffer ());
17688 if (NILP (obj))
17689 return "no process";
17690 #ifdef subprocesses
17691 obj = Fsymbol_name (Fprocess_status (obj));
17692 #endif
17693 break;
17695 case 't': /* indicate TEXT or BINARY */
17696 #ifdef MODE_LINE_BINARY_TEXT
17697 return MODE_LINE_BINARY_TEXT (b);
17698 #else
17699 return "T";
17700 #endif
17702 case 'z':
17703 /* coding-system (not including end-of-line format) */
17704 case 'Z':
17705 /* coding-system (including end-of-line type) */
17707 int eol_flag = (c == 'Z');
17708 char *p = decode_mode_spec_buf;
17710 if (! FRAME_WINDOW_P (f))
17712 /* No need to mention EOL here--the terminal never needs
17713 to do EOL conversion. */
17714 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
17715 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
17717 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17718 p, eol_flag);
17720 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17721 #ifdef subprocesses
17722 obj = Fget_buffer_process (Fcurrent_buffer ());
17723 if (PROCESSP (obj))
17725 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17726 p, eol_flag);
17727 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17728 p, eol_flag);
17730 #endif /* subprocesses */
17731 #endif /* 0 */
17732 *p = 0;
17733 return decode_mode_spec_buf;
17737 if (STRINGP (obj))
17739 *multibyte = STRING_MULTIBYTE (obj);
17740 return (char *) SDATA (obj);
17742 else
17743 return "";
17747 /* Count up to COUNT lines starting from START / START_BYTE.
17748 But don't go beyond LIMIT_BYTE.
17749 Return the number of lines thus found (always nonnegative).
17751 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17753 static int
17754 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17755 int start, start_byte, limit_byte, count;
17756 int *byte_pos_ptr;
17758 register unsigned char *cursor;
17759 unsigned char *base;
17761 register int ceiling;
17762 register unsigned char *ceiling_addr;
17763 int orig_count = count;
17765 /* If we are not in selective display mode,
17766 check only for newlines. */
17767 int selective_display = (!NILP (current_buffer->selective_display)
17768 && !INTEGERP (current_buffer->selective_display));
17770 if (count > 0)
17772 while (start_byte < limit_byte)
17774 ceiling = BUFFER_CEILING_OF (start_byte);
17775 ceiling = min (limit_byte - 1, ceiling);
17776 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17777 base = (cursor = BYTE_POS_ADDR (start_byte));
17778 while (1)
17780 if (selective_display)
17781 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17783 else
17784 while (*cursor != '\n' && ++cursor != ceiling_addr)
17787 if (cursor != ceiling_addr)
17789 if (--count == 0)
17791 start_byte += cursor - base + 1;
17792 *byte_pos_ptr = start_byte;
17793 return orig_count;
17795 else
17796 if (++cursor == ceiling_addr)
17797 break;
17799 else
17800 break;
17802 start_byte += cursor - base;
17805 else
17807 while (start_byte > limit_byte)
17809 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17810 ceiling = max (limit_byte, ceiling);
17811 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17812 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17813 while (1)
17815 if (selective_display)
17816 while (--cursor != ceiling_addr
17817 && *cursor != '\n' && *cursor != 015)
17819 else
17820 while (--cursor != ceiling_addr && *cursor != '\n')
17823 if (cursor != ceiling_addr)
17825 if (++count == 0)
17827 start_byte += cursor - base + 1;
17828 *byte_pos_ptr = start_byte;
17829 /* When scanning backwards, we should
17830 not count the newline posterior to which we stop. */
17831 return - orig_count - 1;
17834 else
17835 break;
17837 /* Here we add 1 to compensate for the last decrement
17838 of CURSOR, which took it past the valid range. */
17839 start_byte += cursor - base + 1;
17843 *byte_pos_ptr = limit_byte;
17845 if (count < 0)
17846 return - orig_count + count;
17847 return orig_count - count;
17853 /***********************************************************************
17854 Displaying strings
17855 ***********************************************************************/
17857 /* Display a NUL-terminated string, starting with index START.
17859 If STRING is non-null, display that C string. Otherwise, the Lisp
17860 string LISP_STRING is displayed.
17862 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17863 FACE_STRING. Display STRING or LISP_STRING with the face at
17864 FACE_STRING_POS in FACE_STRING:
17866 Display the string in the environment given by IT, but use the
17867 standard display table, temporarily.
17869 FIELD_WIDTH is the minimum number of output glyphs to produce.
17870 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17871 with spaces. If STRING has more characters, more than FIELD_WIDTH
17872 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17874 PRECISION is the maximum number of characters to output from
17875 STRING. PRECISION < 0 means don't truncate the string.
17877 This is roughly equivalent to printf format specifiers:
17879 FIELD_WIDTH PRECISION PRINTF
17880 ----------------------------------------
17881 -1 -1 %s
17882 -1 10 %.10s
17883 10 -1 %10s
17884 20 10 %20.10s
17886 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17887 display them, and < 0 means obey the current buffer's value of
17888 enable_multibyte_characters.
17890 Value is the number of columns displayed. */
17892 static int
17893 display_string (string, lisp_string, face_string, face_string_pos,
17894 start, it, field_width, precision, max_x, multibyte)
17895 unsigned char *string;
17896 Lisp_Object lisp_string;
17897 Lisp_Object face_string;
17898 int face_string_pos;
17899 int start;
17900 struct it *it;
17901 int field_width, precision, max_x;
17902 int multibyte;
17904 int hpos_at_start = it->hpos;
17905 int saved_face_id = it->face_id;
17906 struct glyph_row *row = it->glyph_row;
17908 /* Initialize the iterator IT for iteration over STRING beginning
17909 with index START. */
17910 reseat_to_string (it, string, lisp_string, start,
17911 precision, field_width, multibyte);
17913 /* If displaying STRING, set up the face of the iterator
17914 from LISP_STRING, if that's given. */
17915 if (STRINGP (face_string))
17917 int endptr;
17918 struct face *face;
17920 it->face_id
17921 = face_at_string_position (it->w, face_string, face_string_pos,
17922 0, it->region_beg_charpos,
17923 it->region_end_charpos,
17924 &endptr, it->base_face_id, 0);
17925 face = FACE_FROM_ID (it->f, it->face_id);
17926 it->face_box_p = face->box != FACE_NO_BOX;
17929 /* Set max_x to the maximum allowed X position. Don't let it go
17930 beyond the right edge of the window. */
17931 if (max_x <= 0)
17932 max_x = it->last_visible_x;
17933 else
17934 max_x = min (max_x, it->last_visible_x);
17936 /* Skip over display elements that are not visible. because IT->w is
17937 hscrolled. */
17938 if (it->current_x < it->first_visible_x)
17939 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17940 MOVE_TO_POS | MOVE_TO_X);
17942 row->ascent = it->max_ascent;
17943 row->height = it->max_ascent + it->max_descent;
17944 row->phys_ascent = it->max_phys_ascent;
17945 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17946 row->extra_line_spacing = it->max_extra_line_spacing;
17948 /* This condition is for the case that we are called with current_x
17949 past last_visible_x. */
17950 while (it->current_x < max_x)
17952 int x_before, x, n_glyphs_before, i, nglyphs;
17954 /* Get the next display element. */
17955 if (!get_next_display_element (it))
17956 break;
17958 /* Produce glyphs. */
17959 x_before = it->current_x;
17960 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17961 PRODUCE_GLYPHS (it);
17963 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17964 i = 0;
17965 x = x_before;
17966 while (i < nglyphs)
17968 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17970 if (!it->truncate_lines_p
17971 && x + glyph->pixel_width > max_x)
17973 /* End of continued line or max_x reached. */
17974 if (CHAR_GLYPH_PADDING_P (*glyph))
17976 /* A wide character is unbreakable. */
17977 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17978 it->current_x = x_before;
17980 else
17982 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17983 it->current_x = x;
17985 break;
17987 else if (x + glyph->pixel_width > it->first_visible_x)
17989 /* Glyph is at least partially visible. */
17990 ++it->hpos;
17991 if (x < it->first_visible_x)
17992 it->glyph_row->x = x - it->first_visible_x;
17994 else
17996 /* Glyph is off the left margin of the display area.
17997 Should not happen. */
17998 abort ();
18001 row->ascent = max (row->ascent, it->max_ascent);
18002 row->height = max (row->height, it->max_ascent + it->max_descent);
18003 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18004 row->phys_height = max (row->phys_height,
18005 it->max_phys_ascent + it->max_phys_descent);
18006 row->extra_line_spacing = max (row->extra_line_spacing,
18007 it->max_extra_line_spacing);
18008 x += glyph->pixel_width;
18009 ++i;
18012 /* Stop if max_x reached. */
18013 if (i < nglyphs)
18014 break;
18016 /* Stop at line ends. */
18017 if (ITERATOR_AT_END_OF_LINE_P (it))
18019 it->continuation_lines_width = 0;
18020 break;
18023 set_iterator_to_next (it, 1);
18025 /* Stop if truncating at the right edge. */
18026 if (it->truncate_lines_p
18027 && it->current_x >= it->last_visible_x)
18029 /* Add truncation mark, but don't do it if the line is
18030 truncated at a padding space. */
18031 if (IT_CHARPOS (*it) < it->string_nchars)
18033 if (!FRAME_WINDOW_P (it->f))
18035 int i, n;
18037 if (it->current_x > it->last_visible_x)
18039 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18040 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18041 break;
18042 for (n = row->used[TEXT_AREA]; i < n; ++i)
18044 row->used[TEXT_AREA] = i;
18045 produce_special_glyphs (it, IT_TRUNCATION);
18048 produce_special_glyphs (it, IT_TRUNCATION);
18050 it->glyph_row->truncated_on_right_p = 1;
18052 break;
18056 /* Maybe insert a truncation at the left. */
18057 if (it->first_visible_x
18058 && IT_CHARPOS (*it) > 0)
18060 if (!FRAME_WINDOW_P (it->f))
18061 insert_left_trunc_glyphs (it);
18062 it->glyph_row->truncated_on_left_p = 1;
18065 it->face_id = saved_face_id;
18067 /* Value is number of columns displayed. */
18068 return it->hpos - hpos_at_start;
18073 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18074 appears as an element of LIST or as the car of an element of LIST.
18075 If PROPVAL is a list, compare each element against LIST in that
18076 way, and return 1/2 if any element of PROPVAL is found in LIST.
18077 Otherwise return 0. This function cannot quit.
18078 The return value is 2 if the text is invisible but with an ellipsis
18079 and 1 if it's invisible and without an ellipsis. */
18082 invisible_p (propval, list)
18083 register Lisp_Object propval;
18084 Lisp_Object list;
18086 register Lisp_Object tail, proptail;
18088 for (tail = list; CONSP (tail); tail = XCDR (tail))
18090 register Lisp_Object tem;
18091 tem = XCAR (tail);
18092 if (EQ (propval, tem))
18093 return 1;
18094 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18095 return NILP (XCDR (tem)) ? 1 : 2;
18098 if (CONSP (propval))
18100 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18102 Lisp_Object propelt;
18103 propelt = XCAR (proptail);
18104 for (tail = list; CONSP (tail); tail = XCDR (tail))
18106 register Lisp_Object tem;
18107 tem = XCAR (tail);
18108 if (EQ (propelt, tem))
18109 return 1;
18110 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18111 return NILP (XCDR (tem)) ? 1 : 2;
18116 return 0;
18119 /* Calculate a width or height in pixels from a specification using
18120 the following elements:
18122 SPEC ::=
18123 NUM - a (fractional) multiple of the default font width/height
18124 (NUM) - specifies exactly NUM pixels
18125 UNIT - a fixed number of pixels, see below.
18126 ELEMENT - size of a display element in pixels, see below.
18127 (NUM . SPEC) - equals NUM * SPEC
18128 (+ SPEC SPEC ...) - add pixel values
18129 (- SPEC SPEC ...) - subtract pixel values
18130 (- SPEC) - negate pixel value
18132 NUM ::=
18133 INT or FLOAT - a number constant
18134 SYMBOL - use symbol's (buffer local) variable binding.
18136 UNIT ::=
18137 in - pixels per inch *)
18138 mm - pixels per 1/1000 meter *)
18139 cm - pixels per 1/100 meter *)
18140 width - width of current font in pixels.
18141 height - height of current font in pixels.
18143 *) using the ratio(s) defined in display-pixels-per-inch.
18145 ELEMENT ::=
18147 left-fringe - left fringe width in pixels
18148 right-fringe - right fringe width in pixels
18150 left-margin - left margin width in pixels
18151 right-margin - right margin width in pixels
18153 scroll-bar - scroll-bar area width in pixels
18155 Examples:
18157 Pixels corresponding to 5 inches:
18158 (5 . in)
18160 Total width of non-text areas on left side of window (if scroll-bar is on left):
18161 '(space :width (+ left-fringe left-margin scroll-bar))
18163 Align to first text column (in header line):
18164 '(space :align-to 0)
18166 Align to middle of text area minus half the width of variable `my-image'
18167 containing a loaded image:
18168 '(space :align-to (0.5 . (- text my-image)))
18170 Width of left margin minus width of 1 character in the default font:
18171 '(space :width (- left-margin 1))
18173 Width of left margin minus width of 2 characters in the current font:
18174 '(space :width (- left-margin (2 . width)))
18176 Center 1 character over left-margin (in header line):
18177 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18179 Different ways to express width of left fringe plus left margin minus one pixel:
18180 '(space :width (- (+ left-fringe left-margin) (1)))
18181 '(space :width (+ left-fringe left-margin (- (1))))
18182 '(space :width (+ left-fringe left-margin (-1)))
18186 #define NUMVAL(X) \
18187 ((INTEGERP (X) || FLOATP (X)) \
18188 ? XFLOATINT (X) \
18189 : - 1)
18192 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18193 double *res;
18194 struct it *it;
18195 Lisp_Object prop;
18196 void *font;
18197 int width_p, *align_to;
18199 double pixels;
18201 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18202 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18204 if (NILP (prop))
18205 return OK_PIXELS (0);
18207 xassert (FRAME_LIVE_P (it->f));
18209 if (SYMBOLP (prop))
18211 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18213 char *unit = SDATA (SYMBOL_NAME (prop));
18215 if (unit[0] == 'i' && unit[1] == 'n')
18216 pixels = 1.0;
18217 else if (unit[0] == 'm' && unit[1] == 'm')
18218 pixels = 25.4;
18219 else if (unit[0] == 'c' && unit[1] == 'm')
18220 pixels = 2.54;
18221 else
18222 pixels = 0;
18223 if (pixels > 0)
18225 double ppi;
18226 #ifdef HAVE_WINDOW_SYSTEM
18227 if (FRAME_WINDOW_P (it->f)
18228 && (ppi = (width_p
18229 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18230 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18231 ppi > 0))
18232 return OK_PIXELS (ppi / pixels);
18233 #endif
18235 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18236 || (CONSP (Vdisplay_pixels_per_inch)
18237 && (ppi = (width_p
18238 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18239 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18240 ppi > 0)))
18241 return OK_PIXELS (ppi / pixels);
18243 return 0;
18247 #ifdef HAVE_WINDOW_SYSTEM
18248 if (EQ (prop, Qheight))
18249 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18250 if (EQ (prop, Qwidth))
18251 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18252 #else
18253 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18254 return OK_PIXELS (1);
18255 #endif
18257 if (EQ (prop, Qtext))
18258 return OK_PIXELS (width_p
18259 ? window_box_width (it->w, TEXT_AREA)
18260 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18262 if (align_to && *align_to < 0)
18264 *res = 0;
18265 if (EQ (prop, Qleft))
18266 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18267 if (EQ (prop, Qright))
18268 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18269 if (EQ (prop, Qcenter))
18270 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18271 + window_box_width (it->w, TEXT_AREA) / 2);
18272 if (EQ (prop, Qleft_fringe))
18273 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18274 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18275 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18276 if (EQ (prop, Qright_fringe))
18277 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18278 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18279 : window_box_right_offset (it->w, TEXT_AREA));
18280 if (EQ (prop, Qleft_margin))
18281 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18282 if (EQ (prop, Qright_margin))
18283 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18284 if (EQ (prop, Qscroll_bar))
18285 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18287 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18288 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18289 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18290 : 0)));
18292 else
18294 if (EQ (prop, Qleft_fringe))
18295 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18296 if (EQ (prop, Qright_fringe))
18297 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18298 if (EQ (prop, Qleft_margin))
18299 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18300 if (EQ (prop, Qright_margin))
18301 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18302 if (EQ (prop, Qscroll_bar))
18303 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18306 prop = Fbuffer_local_value (prop, it->w->buffer);
18309 if (INTEGERP (prop) || FLOATP (prop))
18311 int base_unit = (width_p
18312 ? FRAME_COLUMN_WIDTH (it->f)
18313 : FRAME_LINE_HEIGHT (it->f));
18314 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18317 if (CONSP (prop))
18319 Lisp_Object car = XCAR (prop);
18320 Lisp_Object cdr = XCDR (prop);
18322 if (SYMBOLP (car))
18324 #ifdef HAVE_WINDOW_SYSTEM
18325 if (FRAME_WINDOW_P (it->f)
18326 && valid_image_p (prop))
18328 int id = lookup_image (it->f, prop);
18329 struct image *img = IMAGE_FROM_ID (it->f, id);
18331 return OK_PIXELS (width_p ? img->width : img->height);
18333 #endif
18334 if (EQ (car, Qplus) || EQ (car, Qminus))
18336 int first = 1;
18337 double px;
18339 pixels = 0;
18340 while (CONSP (cdr))
18342 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18343 font, width_p, align_to))
18344 return 0;
18345 if (first)
18346 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18347 else
18348 pixels += px;
18349 cdr = XCDR (cdr);
18351 if (EQ (car, Qminus))
18352 pixels = -pixels;
18353 return OK_PIXELS (pixels);
18356 car = Fbuffer_local_value (car, it->w->buffer);
18359 if (INTEGERP (car) || FLOATP (car))
18361 double fact;
18362 pixels = XFLOATINT (car);
18363 if (NILP (cdr))
18364 return OK_PIXELS (pixels);
18365 if (calc_pixel_width_or_height (&fact, it, cdr,
18366 font, width_p, align_to))
18367 return OK_PIXELS (pixels * fact);
18368 return 0;
18371 return 0;
18374 return 0;
18378 /***********************************************************************
18379 Glyph Display
18380 ***********************************************************************/
18382 #ifdef HAVE_WINDOW_SYSTEM
18384 #if GLYPH_DEBUG
18386 void
18387 dump_glyph_string (s)
18388 struct glyph_string *s;
18390 fprintf (stderr, "glyph string\n");
18391 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18392 s->x, s->y, s->width, s->height);
18393 fprintf (stderr, " ybase = %d\n", s->ybase);
18394 fprintf (stderr, " hl = %d\n", s->hl);
18395 fprintf (stderr, " left overhang = %d, right = %d\n",
18396 s->left_overhang, s->right_overhang);
18397 fprintf (stderr, " nchars = %d\n", s->nchars);
18398 fprintf (stderr, " extends to end of line = %d\n",
18399 s->extends_to_end_of_line_p);
18400 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18401 fprintf (stderr, " bg width = %d\n", s->background_width);
18404 #endif /* GLYPH_DEBUG */
18406 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18407 of XChar2b structures for S; it can't be allocated in
18408 init_glyph_string because it must be allocated via `alloca'. W
18409 is the window on which S is drawn. ROW and AREA are the glyph row
18410 and area within the row from which S is constructed. START is the
18411 index of the first glyph structure covered by S. HL is a
18412 face-override for drawing S. */
18414 #ifdef HAVE_NTGUI
18415 #define OPTIONAL_HDC(hdc) hdc,
18416 #define DECLARE_HDC(hdc) HDC hdc;
18417 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18418 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18419 #endif
18421 #ifndef OPTIONAL_HDC
18422 #define OPTIONAL_HDC(hdc)
18423 #define DECLARE_HDC(hdc)
18424 #define ALLOCATE_HDC(hdc, f)
18425 #define RELEASE_HDC(hdc, f)
18426 #endif
18428 static void
18429 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18430 struct glyph_string *s;
18431 DECLARE_HDC (hdc)
18432 XChar2b *char2b;
18433 struct window *w;
18434 struct glyph_row *row;
18435 enum glyph_row_area area;
18436 int start;
18437 enum draw_glyphs_face hl;
18439 bzero (s, sizeof *s);
18440 s->w = w;
18441 s->f = XFRAME (w->frame);
18442 #ifdef HAVE_NTGUI
18443 s->hdc = hdc;
18444 #endif
18445 s->display = FRAME_X_DISPLAY (s->f);
18446 s->window = FRAME_X_WINDOW (s->f);
18447 s->char2b = char2b;
18448 s->hl = hl;
18449 s->row = row;
18450 s->area = area;
18451 s->first_glyph = row->glyphs[area] + start;
18452 s->height = row->height;
18453 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18455 /* Display the internal border below the tool-bar window. */
18456 if (WINDOWP (s->f->tool_bar_window)
18457 && s->w == XWINDOW (s->f->tool_bar_window))
18458 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18460 s->ybase = s->y + row->ascent;
18464 /* Append the list of glyph strings with head H and tail T to the list
18465 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18467 static INLINE void
18468 append_glyph_string_lists (head, tail, h, t)
18469 struct glyph_string **head, **tail;
18470 struct glyph_string *h, *t;
18472 if (h)
18474 if (*head)
18475 (*tail)->next = h;
18476 else
18477 *head = h;
18478 h->prev = *tail;
18479 *tail = t;
18484 /* Prepend the list of glyph strings with head H and tail T to the
18485 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18486 result. */
18488 static INLINE void
18489 prepend_glyph_string_lists (head, tail, h, t)
18490 struct glyph_string **head, **tail;
18491 struct glyph_string *h, *t;
18493 if (h)
18495 if (*head)
18496 (*head)->prev = t;
18497 else
18498 *tail = t;
18499 t->next = *head;
18500 *head = h;
18505 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18506 Set *HEAD and *TAIL to the resulting list. */
18508 static INLINE void
18509 append_glyph_string (head, tail, s)
18510 struct glyph_string **head, **tail;
18511 struct glyph_string *s;
18513 s->next = s->prev = NULL;
18514 append_glyph_string_lists (head, tail, s, s);
18518 /* Get face and two-byte form of character glyph GLYPH on frame F.
18519 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18520 a pointer to a realized face that is ready for display. */
18522 static INLINE struct face *
18523 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18524 struct frame *f;
18525 struct glyph *glyph;
18526 XChar2b *char2b;
18527 int *two_byte_p;
18529 struct face *face;
18531 xassert (glyph->type == CHAR_GLYPH);
18532 face = FACE_FROM_ID (f, glyph->face_id);
18534 if (two_byte_p)
18535 *two_byte_p = 0;
18537 if (!glyph->multibyte_p)
18539 /* Unibyte case. We don't have to encode, but we have to make
18540 sure to use a face suitable for unibyte. */
18541 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18543 else if (glyph->u.ch < 128)
18545 /* Case of ASCII in a face known to fit ASCII. */
18546 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18548 else
18550 int c1, c2, charset;
18552 /* Split characters into bytes. If c2 is -1 afterwards, C is
18553 really a one-byte character so that byte1 is zero. */
18554 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18555 if (c2 > 0)
18556 STORE_XCHAR2B (char2b, c1, c2);
18557 else
18558 STORE_XCHAR2B (char2b, 0, c1);
18560 /* Maybe encode the character in *CHAR2B. */
18561 if (charset != CHARSET_ASCII)
18563 struct font_info *font_info
18564 = FONT_INFO_FROM_ID (f, face->font_info_id);
18565 if (font_info)
18566 glyph->font_type
18567 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18571 /* Make sure X resources of the face are allocated. */
18572 xassert (face != NULL);
18573 PREPARE_FACE_FOR_DISPLAY (f, face);
18574 return face;
18578 /* Fill glyph string S with composition components specified by S->cmp.
18580 FACES is an array of faces for all components of this composition.
18581 S->gidx is the index of the first component for S.
18583 OVERLAPS non-zero means S should draw the foreground only, and use
18584 its physical height for clipping. See also draw_glyphs.
18586 Value is the index of a component not in S. */
18588 static int
18589 fill_composite_glyph_string (s, faces, overlaps)
18590 struct glyph_string *s;
18591 struct face **faces;
18592 int overlaps;
18594 int i;
18596 xassert (s);
18598 s->for_overlaps = overlaps;
18600 s->face = faces[s->gidx];
18601 s->font = s->face->font;
18602 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18604 /* For all glyphs of this composition, starting at the offset
18605 S->gidx, until we reach the end of the definition or encounter a
18606 glyph that requires the different face, add it to S. */
18607 ++s->nchars;
18608 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18609 ++s->nchars;
18611 /* All glyph strings for the same composition has the same width,
18612 i.e. the width set for the first component of the composition. */
18614 s->width = s->first_glyph->pixel_width;
18616 /* If the specified font could not be loaded, use the frame's
18617 default font, but record the fact that we couldn't load it in
18618 the glyph string so that we can draw rectangles for the
18619 characters of the glyph string. */
18620 if (s->font == NULL)
18622 s->font_not_found_p = 1;
18623 s->font = FRAME_FONT (s->f);
18626 /* Adjust base line for subscript/superscript text. */
18627 s->ybase += s->first_glyph->voffset;
18629 xassert (s->face && s->face->gc);
18631 /* This glyph string must always be drawn with 16-bit functions. */
18632 s->two_byte_p = 1;
18634 return s->gidx + s->nchars;
18638 /* Fill glyph string S from a sequence of character glyphs.
18640 FACE_ID is the face id of the string. START is the index of the
18641 first glyph to consider, END is the index of the last + 1.
18642 OVERLAPS non-zero means S should draw the foreground only, and use
18643 its physical height for clipping. See also draw_glyphs.
18645 Value is the index of the first glyph not in S. */
18647 static int
18648 fill_glyph_string (s, face_id, start, end, overlaps)
18649 struct glyph_string *s;
18650 int face_id;
18651 int start, end, overlaps;
18653 struct glyph *glyph, *last;
18654 int voffset;
18655 int glyph_not_available_p;
18657 xassert (s->f == XFRAME (s->w->frame));
18658 xassert (s->nchars == 0);
18659 xassert (start >= 0 && end > start);
18661 s->for_overlaps = overlaps,
18662 glyph = s->row->glyphs[s->area] + start;
18663 last = s->row->glyphs[s->area] + end;
18664 voffset = glyph->voffset;
18666 glyph_not_available_p = glyph->glyph_not_available_p;
18668 while (glyph < last
18669 && glyph->type == CHAR_GLYPH
18670 && glyph->voffset == voffset
18671 /* Same face id implies same font, nowadays. */
18672 && glyph->face_id == face_id
18673 && glyph->glyph_not_available_p == glyph_not_available_p)
18675 int two_byte_p;
18677 s->face = get_glyph_face_and_encoding (s->f, glyph,
18678 s->char2b + s->nchars,
18679 &two_byte_p);
18680 s->two_byte_p = two_byte_p;
18681 ++s->nchars;
18682 xassert (s->nchars <= end - start);
18683 s->width += glyph->pixel_width;
18684 ++glyph;
18687 s->font = s->face->font;
18688 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18690 /* If the specified font could not be loaded, use the frame's font,
18691 but record the fact that we couldn't load it in
18692 S->font_not_found_p so that we can draw rectangles for the
18693 characters of the glyph string. */
18694 if (s->font == NULL || glyph_not_available_p)
18696 s->font_not_found_p = 1;
18697 s->font = FRAME_FONT (s->f);
18700 /* Adjust base line for subscript/superscript text. */
18701 s->ybase += voffset;
18703 xassert (s->face && s->face->gc);
18704 return glyph - s->row->glyphs[s->area];
18708 /* Fill glyph string S from image glyph S->first_glyph. */
18710 static void
18711 fill_image_glyph_string (s)
18712 struct glyph_string *s;
18714 xassert (s->first_glyph->type == IMAGE_GLYPH);
18715 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18716 xassert (s->img);
18717 s->slice = s->first_glyph->slice;
18718 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18719 s->font = s->face->font;
18720 s->width = s->first_glyph->pixel_width;
18722 /* Adjust base line for subscript/superscript text. */
18723 s->ybase += s->first_glyph->voffset;
18727 /* Fill glyph string S from a sequence of stretch glyphs.
18729 ROW is the glyph row in which the glyphs are found, AREA is the
18730 area within the row. START is the index of the first glyph to
18731 consider, END is the index of the last + 1.
18733 Value is the index of the first glyph not in S. */
18735 static int
18736 fill_stretch_glyph_string (s, row, area, start, end)
18737 struct glyph_string *s;
18738 struct glyph_row *row;
18739 enum glyph_row_area area;
18740 int start, end;
18742 struct glyph *glyph, *last;
18743 int voffset, face_id;
18745 xassert (s->first_glyph->type == STRETCH_GLYPH);
18747 glyph = s->row->glyphs[s->area] + start;
18748 last = s->row->glyphs[s->area] + end;
18749 face_id = glyph->face_id;
18750 s->face = FACE_FROM_ID (s->f, face_id);
18751 s->font = s->face->font;
18752 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18753 s->width = glyph->pixel_width;
18754 s->nchars = 1;
18755 voffset = glyph->voffset;
18757 for (++glyph;
18758 (glyph < last
18759 && glyph->type == STRETCH_GLYPH
18760 && glyph->voffset == voffset
18761 && glyph->face_id == face_id);
18762 ++glyph)
18763 s->width += glyph->pixel_width;
18765 /* Adjust base line for subscript/superscript text. */
18766 s->ybase += voffset;
18768 /* The case that face->gc == 0 is handled when drawing the glyph
18769 string by calling PREPARE_FACE_FOR_DISPLAY. */
18770 xassert (s->face);
18771 return glyph - s->row->glyphs[s->area];
18775 /* EXPORT for RIF:
18776 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18777 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18778 assumed to be zero. */
18780 void
18781 x_get_glyph_overhangs (glyph, f, left, right)
18782 struct glyph *glyph;
18783 struct frame *f;
18784 int *left, *right;
18786 *left = *right = 0;
18788 if (glyph->type == CHAR_GLYPH)
18790 XFontStruct *font;
18791 struct face *face;
18792 struct font_info *font_info;
18793 XChar2b char2b;
18794 XCharStruct *pcm;
18796 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18797 font = face->font;
18798 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18799 if (font /* ++KFS: Should this be font_info ? */
18800 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
18802 if (pcm->rbearing > pcm->width)
18803 *right = pcm->rbearing - pcm->width;
18804 if (pcm->lbearing < 0)
18805 *left = -pcm->lbearing;
18811 /* Return the index of the first glyph preceding glyph string S that
18812 is overwritten by S because of S's left overhang. Value is -1
18813 if no glyphs are overwritten. */
18815 static int
18816 left_overwritten (s)
18817 struct glyph_string *s;
18819 int k;
18821 if (s->left_overhang)
18823 int x = 0, i;
18824 struct glyph *glyphs = s->row->glyphs[s->area];
18825 int first = s->first_glyph - glyphs;
18827 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18828 x -= glyphs[i].pixel_width;
18830 k = i + 1;
18832 else
18833 k = -1;
18835 return k;
18839 /* Return the index of the first glyph preceding glyph string S that
18840 is overwriting S because of its right overhang. Value is -1 if no
18841 glyph in front of S overwrites S. */
18843 static int
18844 left_overwriting (s)
18845 struct glyph_string *s;
18847 int i, k, x;
18848 struct glyph *glyphs = s->row->glyphs[s->area];
18849 int first = s->first_glyph - glyphs;
18851 k = -1;
18852 x = 0;
18853 for (i = first - 1; i >= 0; --i)
18855 int left, right;
18856 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18857 if (x + right > 0)
18858 k = i;
18859 x -= glyphs[i].pixel_width;
18862 return k;
18866 /* Return the index of the last glyph following glyph string S that is
18867 not overwritten by S because of S's right overhang. Value is -1 if
18868 no such glyph is found. */
18870 static int
18871 right_overwritten (s)
18872 struct glyph_string *s;
18874 int k = -1;
18876 if (s->right_overhang)
18878 int x = 0, i;
18879 struct glyph *glyphs = s->row->glyphs[s->area];
18880 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18881 int end = s->row->used[s->area];
18883 for (i = first; i < end && s->right_overhang > x; ++i)
18884 x += glyphs[i].pixel_width;
18886 k = i;
18889 return k;
18893 /* Return the index of the last glyph following glyph string S that
18894 overwrites S because of its left overhang. Value is negative
18895 if no such glyph is found. */
18897 static int
18898 right_overwriting (s)
18899 struct glyph_string *s;
18901 int i, k, x;
18902 int end = s->row->used[s->area];
18903 struct glyph *glyphs = s->row->glyphs[s->area];
18904 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18906 k = -1;
18907 x = 0;
18908 for (i = first; i < end; ++i)
18910 int left, right;
18911 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18912 if (x - left < 0)
18913 k = i;
18914 x += glyphs[i].pixel_width;
18917 return k;
18921 /* Get face and two-byte form of character C in face FACE_ID on frame
18922 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18923 means we want to display multibyte text. DISPLAY_P non-zero means
18924 make sure that X resources for the face returned are allocated.
18925 Value is a pointer to a realized face that is ready for display if
18926 DISPLAY_P is non-zero. */
18928 static INLINE struct face *
18929 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18930 struct frame *f;
18931 int c, face_id;
18932 XChar2b *char2b;
18933 int multibyte_p, display_p;
18935 struct face *face = FACE_FROM_ID (f, face_id);
18937 if (!multibyte_p)
18939 /* Unibyte case. We don't have to encode, but we have to make
18940 sure to use a face suitable for unibyte. */
18941 STORE_XCHAR2B (char2b, 0, c);
18942 face_id = FACE_FOR_CHAR (f, face, c);
18943 face = FACE_FROM_ID (f, face_id);
18945 else if (c < 128)
18947 /* Case of ASCII in a face known to fit ASCII. */
18948 STORE_XCHAR2B (char2b, 0, c);
18950 else
18952 int c1, c2, charset;
18954 /* Split characters into bytes. If c2 is -1 afterwards, C is
18955 really a one-byte character so that byte1 is zero. */
18956 SPLIT_CHAR (c, charset, c1, c2);
18957 if (c2 > 0)
18958 STORE_XCHAR2B (char2b, c1, c2);
18959 else
18960 STORE_XCHAR2B (char2b, 0, c1);
18962 /* Maybe encode the character in *CHAR2B. */
18963 if (face->font != NULL)
18965 struct font_info *font_info
18966 = FONT_INFO_FROM_ID (f, face->font_info_id);
18967 if (font_info)
18968 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
18972 /* Make sure X resources of the face are allocated. */
18973 #ifdef HAVE_X_WINDOWS
18974 if (display_p)
18975 #endif
18977 xassert (face != NULL);
18978 PREPARE_FACE_FOR_DISPLAY (f, face);
18981 return face;
18985 /* Set background width of glyph string S. START is the index of the
18986 first glyph following S. LAST_X is the right-most x-position + 1
18987 in the drawing area. */
18989 static INLINE void
18990 set_glyph_string_background_width (s, start, last_x)
18991 struct glyph_string *s;
18992 int start;
18993 int last_x;
18995 /* If the face of this glyph string has to be drawn to the end of
18996 the drawing area, set S->extends_to_end_of_line_p. */
18998 if (start == s->row->used[s->area]
18999 && s->area == TEXT_AREA
19000 && ((s->row->fill_line_p
19001 && (s->hl == DRAW_NORMAL_TEXT
19002 || s->hl == DRAW_IMAGE_RAISED
19003 || s->hl == DRAW_IMAGE_SUNKEN))
19004 || s->hl == DRAW_MOUSE_FACE))
19005 s->extends_to_end_of_line_p = 1;
19007 /* If S extends its face to the end of the line, set its
19008 background_width to the distance to the right edge of the drawing
19009 area. */
19010 if (s->extends_to_end_of_line_p)
19011 s->background_width = last_x - s->x + 1;
19012 else
19013 s->background_width = s->width;
19017 /* Compute overhangs and x-positions for glyph string S and its
19018 predecessors, or successors. X is the starting x-position for S.
19019 BACKWARD_P non-zero means process predecessors. */
19021 static void
19022 compute_overhangs_and_x (s, x, backward_p)
19023 struct glyph_string *s;
19024 int x;
19025 int backward_p;
19027 if (backward_p)
19029 while (s)
19031 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19032 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19033 x -= s->width;
19034 s->x = x;
19035 s = s->prev;
19038 else
19040 while (s)
19042 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19043 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19044 s->x = x;
19045 x += s->width;
19046 s = s->next;
19053 /* The following macros are only called from draw_glyphs below.
19054 They reference the following parameters of that function directly:
19055 `w', `row', `area', and `overlap_p'
19056 as well as the following local variables:
19057 `s', `f', and `hdc' (in W32) */
19059 #ifdef HAVE_NTGUI
19060 /* On W32, silently add local `hdc' variable to argument list of
19061 init_glyph_string. */
19062 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19063 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19064 #else
19065 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19066 init_glyph_string (s, char2b, w, row, area, start, hl)
19067 #endif
19069 /* Add a glyph string for a stretch glyph to the list of strings
19070 between HEAD and TAIL. START is the index of the stretch glyph in
19071 row area AREA of glyph row ROW. END is the index of the last glyph
19072 in that glyph row area. X is the current output position assigned
19073 to the new glyph string constructed. HL overrides that face of the
19074 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19075 is the right-most x-position of the drawing area. */
19077 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19078 and below -- keep them on one line. */
19079 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19080 do \
19082 s = (struct glyph_string *) alloca (sizeof *s); \
19083 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19084 START = fill_stretch_glyph_string (s, row, area, START, END); \
19085 append_glyph_string (&HEAD, &TAIL, s); \
19086 s->x = (X); \
19088 while (0)
19091 /* Add a glyph string for an image glyph to the list of strings
19092 between HEAD and TAIL. START is the index of the image glyph in
19093 row area AREA of glyph row ROW. END is the index of the last glyph
19094 in that glyph row area. X is the current output position assigned
19095 to the new glyph string constructed. HL overrides that face of the
19096 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19097 is the right-most x-position of the drawing area. */
19099 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19100 do \
19102 s = (struct glyph_string *) alloca (sizeof *s); \
19103 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19104 fill_image_glyph_string (s); \
19105 append_glyph_string (&HEAD, &TAIL, s); \
19106 ++START; \
19107 s->x = (X); \
19109 while (0)
19112 /* Add a glyph string for a sequence of character glyphs to the list
19113 of strings between HEAD and TAIL. START is the index of the first
19114 glyph in row area AREA of glyph row ROW that is part of the new
19115 glyph string. END is the index of the last glyph in that glyph row
19116 area. X is the current output position assigned to the new glyph
19117 string constructed. HL overrides that face of the glyph; e.g. it
19118 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19119 right-most x-position of the drawing area. */
19121 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19122 do \
19124 int c, face_id; \
19125 XChar2b *char2b; \
19127 c = (row)->glyphs[area][START].u.ch; \
19128 face_id = (row)->glyphs[area][START].face_id; \
19130 s = (struct glyph_string *) alloca (sizeof *s); \
19131 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19132 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19133 append_glyph_string (&HEAD, &TAIL, s); \
19134 s->x = (X); \
19135 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19137 while (0)
19140 /* Add a glyph string for a composite sequence to the list of strings
19141 between HEAD and TAIL. START is the index of the first glyph in
19142 row area AREA of glyph row ROW that is part of the new glyph
19143 string. END is the index of the last glyph in that glyph row area.
19144 X is the current output position assigned to the new glyph string
19145 constructed. HL overrides that face of the glyph; e.g. it is
19146 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19147 x-position of the drawing area. */
19149 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19150 do { \
19151 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19152 int face_id = (row)->glyphs[area][START].face_id; \
19153 struct face *base_face = FACE_FROM_ID (f, face_id); \
19154 struct composition *cmp = composition_table[cmp_id]; \
19155 int glyph_len = cmp->glyph_len; \
19156 XChar2b *char2b; \
19157 struct face **faces; \
19158 struct glyph_string *first_s = NULL; \
19159 int n; \
19161 base_face = base_face->ascii_face; \
19162 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19163 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19164 /* At first, fill in `char2b' and `faces'. */ \
19165 for (n = 0; n < glyph_len; n++) \
19167 int c = COMPOSITION_GLYPH (cmp, n); \
19168 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19169 faces[n] = FACE_FROM_ID (f, this_face_id); \
19170 get_char_face_and_encoding (f, c, this_face_id, \
19171 char2b + n, 1, 1); \
19174 /* Make glyph_strings for each glyph sequence that is drawable by \
19175 the same face, and append them to HEAD/TAIL. */ \
19176 for (n = 0; n < cmp->glyph_len;) \
19178 s = (struct glyph_string *) alloca (sizeof *s); \
19179 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19180 append_glyph_string (&(HEAD), &(TAIL), s); \
19181 s->cmp = cmp; \
19182 s->gidx = n; \
19183 s->x = (X); \
19185 if (n == 0) \
19186 first_s = s; \
19188 n = fill_composite_glyph_string (s, faces, overlaps); \
19191 ++START; \
19192 s = first_s; \
19193 } while (0)
19196 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19197 of AREA of glyph row ROW on window W between indices START and END.
19198 HL overrides the face for drawing glyph strings, e.g. it is
19199 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19200 x-positions of the drawing area.
19202 This is an ugly monster macro construct because we must use alloca
19203 to allocate glyph strings (because draw_glyphs can be called
19204 asynchronously). */
19206 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19207 do \
19209 HEAD = TAIL = NULL; \
19210 while (START < END) \
19212 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19213 switch (first_glyph->type) \
19215 case CHAR_GLYPH: \
19216 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19217 HL, X, LAST_X); \
19218 break; \
19220 case COMPOSITE_GLYPH: \
19221 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19222 HL, X, LAST_X); \
19223 break; \
19225 case STRETCH_GLYPH: \
19226 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19227 HL, X, LAST_X); \
19228 break; \
19230 case IMAGE_GLYPH: \
19231 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19232 HL, X, LAST_X); \
19233 break; \
19235 default: \
19236 abort (); \
19239 set_glyph_string_background_width (s, START, LAST_X); \
19240 (X) += s->width; \
19243 while (0)
19246 /* Draw glyphs between START and END in AREA of ROW on window W,
19247 starting at x-position X. X is relative to AREA in W. HL is a
19248 face-override with the following meaning:
19250 DRAW_NORMAL_TEXT draw normally
19251 DRAW_CURSOR draw in cursor face
19252 DRAW_MOUSE_FACE draw in mouse face.
19253 DRAW_INVERSE_VIDEO draw in mode line face
19254 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19255 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19257 If OVERLAPS is non-zero, draw only the foreground of characters and
19258 clip to the physical height of ROW. Non-zero value also defines
19259 the overlapping part to be drawn:
19261 OVERLAPS_PRED overlap with preceding rows
19262 OVERLAPS_SUCC overlap with succeeding rows
19263 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19264 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19266 Value is the x-position reached, relative to AREA of W. */
19268 static int
19269 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19270 struct window *w;
19271 int x;
19272 struct glyph_row *row;
19273 enum glyph_row_area area;
19274 int start, end;
19275 enum draw_glyphs_face hl;
19276 int overlaps;
19278 struct glyph_string *head, *tail;
19279 struct glyph_string *s;
19280 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19281 int last_x, area_width;
19282 int x_reached;
19283 int i, j;
19284 struct frame *f = XFRAME (WINDOW_FRAME (w));
19285 DECLARE_HDC (hdc);
19287 ALLOCATE_HDC (hdc, f);
19289 /* Let's rather be paranoid than getting a SEGV. */
19290 end = min (end, row->used[area]);
19291 start = max (0, start);
19292 start = min (end, start);
19294 /* Translate X to frame coordinates. Set last_x to the right
19295 end of the drawing area. */
19296 if (row->full_width_p)
19298 /* X is relative to the left edge of W, without scroll bars
19299 or fringes. */
19300 x += WINDOW_LEFT_EDGE_X (w);
19301 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19303 else
19305 int area_left = window_box_left (w, area);
19306 x += area_left;
19307 area_width = window_box_width (w, area);
19308 last_x = area_left + area_width;
19311 /* Build a doubly-linked list of glyph_string structures between
19312 head and tail from what we have to draw. Note that the macro
19313 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19314 the reason we use a separate variable `i'. */
19315 i = start;
19316 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19317 if (tail)
19318 x_reached = tail->x + tail->background_width;
19319 else
19320 x_reached = x;
19322 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19323 the row, redraw some glyphs in front or following the glyph
19324 strings built above. */
19325 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19327 int dummy_x = 0;
19328 struct glyph_string *h, *t;
19330 /* Compute overhangs for all glyph strings. */
19331 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19332 for (s = head; s; s = s->next)
19333 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19335 /* Prepend glyph strings for glyphs in front of the first glyph
19336 string that are overwritten because of the first glyph
19337 string's left overhang. The background of all strings
19338 prepended must be drawn because the first glyph string
19339 draws over it. */
19340 i = left_overwritten (head);
19341 if (i >= 0)
19343 j = i;
19344 BUILD_GLYPH_STRINGS (j, start, h, t,
19345 DRAW_NORMAL_TEXT, dummy_x, last_x);
19346 start = i;
19347 compute_overhangs_and_x (t, head->x, 1);
19348 prepend_glyph_string_lists (&head, &tail, h, t);
19349 clip_head = head;
19352 /* Prepend glyph strings for glyphs in front of the first glyph
19353 string that overwrite that glyph string because of their
19354 right overhang. For these strings, only the foreground must
19355 be drawn, because it draws over the glyph string at `head'.
19356 The background must not be drawn because this would overwrite
19357 right overhangs of preceding glyphs for which no glyph
19358 strings exist. */
19359 i = left_overwriting (head);
19360 if (i >= 0)
19362 clip_head = head;
19363 BUILD_GLYPH_STRINGS (i, start, h, t,
19364 DRAW_NORMAL_TEXT, dummy_x, last_x);
19365 for (s = h; s; s = s->next)
19366 s->background_filled_p = 1;
19367 compute_overhangs_and_x (t, head->x, 1);
19368 prepend_glyph_string_lists (&head, &tail, h, t);
19371 /* Append glyphs strings for glyphs following the last glyph
19372 string tail that are overwritten by tail. The background of
19373 these strings has to be drawn because tail's foreground draws
19374 over it. */
19375 i = right_overwritten (tail);
19376 if (i >= 0)
19378 BUILD_GLYPH_STRINGS (end, i, h, t,
19379 DRAW_NORMAL_TEXT, x, last_x);
19380 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19381 append_glyph_string_lists (&head, &tail, h, t);
19382 clip_tail = tail;
19385 /* Append glyph strings for glyphs following the last glyph
19386 string tail that overwrite tail. The foreground of such
19387 glyphs has to be drawn because it writes into the background
19388 of tail. The background must not be drawn because it could
19389 paint over the foreground of following glyphs. */
19390 i = right_overwriting (tail);
19391 if (i >= 0)
19393 clip_tail = tail;
19394 BUILD_GLYPH_STRINGS (end, i, h, t,
19395 DRAW_NORMAL_TEXT, x, last_x);
19396 for (s = h; s; s = s->next)
19397 s->background_filled_p = 1;
19398 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19399 append_glyph_string_lists (&head, &tail, h, t);
19401 if (clip_head || clip_tail)
19402 for (s = head; s; s = s->next)
19404 s->clip_head = clip_head;
19405 s->clip_tail = clip_tail;
19409 /* Draw all strings. */
19410 for (s = head; s; s = s->next)
19411 FRAME_RIF (f)->draw_glyph_string (s);
19413 if (area == TEXT_AREA
19414 && !row->full_width_p
19415 /* When drawing overlapping rows, only the glyph strings'
19416 foreground is drawn, which doesn't erase a cursor
19417 completely. */
19418 && !overlaps)
19420 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19421 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19422 : (tail ? tail->x + tail->background_width : x));
19424 int text_left = window_box_left (w, TEXT_AREA);
19425 x0 -= text_left;
19426 x1 -= text_left;
19428 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19429 row->y, MATRIX_ROW_BOTTOM_Y (row));
19432 /* Value is the x-position up to which drawn, relative to AREA of W.
19433 This doesn't include parts drawn because of overhangs. */
19434 if (row->full_width_p)
19435 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19436 else
19437 x_reached -= window_box_left (w, area);
19439 RELEASE_HDC (hdc, f);
19441 return x_reached;
19444 /* Expand row matrix if too narrow. Don't expand if area
19445 is not present. */
19447 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19449 if (!fonts_changed_p \
19450 && (it->glyph_row->glyphs[area] \
19451 < it->glyph_row->glyphs[area + 1])) \
19453 it->w->ncols_scale_factor++; \
19454 fonts_changed_p = 1; \
19458 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19459 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19461 static INLINE void
19462 append_glyph (it)
19463 struct it *it;
19465 struct glyph *glyph;
19466 enum glyph_row_area area = it->area;
19468 xassert (it->glyph_row);
19469 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19471 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19472 if (glyph < it->glyph_row->glyphs[area + 1])
19474 glyph->charpos = CHARPOS (it->position);
19475 glyph->object = it->object;
19476 glyph->pixel_width = it->pixel_width;
19477 glyph->ascent = it->ascent;
19478 glyph->descent = it->descent;
19479 glyph->voffset = it->voffset;
19480 glyph->type = CHAR_GLYPH;
19481 glyph->multibyte_p = it->multibyte_p;
19482 glyph->left_box_line_p = it->start_of_box_run_p;
19483 glyph->right_box_line_p = it->end_of_box_run_p;
19484 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19485 || it->phys_descent > it->descent);
19486 glyph->padding_p = 0;
19487 glyph->glyph_not_available_p = it->glyph_not_available_p;
19488 glyph->face_id = it->face_id;
19489 glyph->u.ch = it->char_to_display;
19490 glyph->slice = null_glyph_slice;
19491 glyph->font_type = FONT_TYPE_UNKNOWN;
19492 ++it->glyph_row->used[area];
19494 else
19495 IT_EXPAND_MATRIX_WIDTH (it, area);
19498 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19499 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19501 static INLINE void
19502 append_composite_glyph (it)
19503 struct it *it;
19505 struct glyph *glyph;
19506 enum glyph_row_area area = it->area;
19508 xassert (it->glyph_row);
19510 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19511 if (glyph < it->glyph_row->glyphs[area + 1])
19513 glyph->charpos = CHARPOS (it->position);
19514 glyph->object = it->object;
19515 glyph->pixel_width = it->pixel_width;
19516 glyph->ascent = it->ascent;
19517 glyph->descent = it->descent;
19518 glyph->voffset = it->voffset;
19519 glyph->type = COMPOSITE_GLYPH;
19520 glyph->multibyte_p = it->multibyte_p;
19521 glyph->left_box_line_p = it->start_of_box_run_p;
19522 glyph->right_box_line_p = it->end_of_box_run_p;
19523 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19524 || it->phys_descent > it->descent);
19525 glyph->padding_p = 0;
19526 glyph->glyph_not_available_p = 0;
19527 glyph->face_id = it->face_id;
19528 glyph->u.cmp_id = it->cmp_id;
19529 glyph->slice = null_glyph_slice;
19530 glyph->font_type = FONT_TYPE_UNKNOWN;
19531 ++it->glyph_row->used[area];
19533 else
19534 IT_EXPAND_MATRIX_WIDTH (it, area);
19538 /* Change IT->ascent and IT->height according to the setting of
19539 IT->voffset. */
19541 static INLINE void
19542 take_vertical_position_into_account (it)
19543 struct it *it;
19545 if (it->voffset)
19547 if (it->voffset < 0)
19548 /* Increase the ascent so that we can display the text higher
19549 in the line. */
19550 it->ascent -= it->voffset;
19551 else
19552 /* Increase the descent so that we can display the text lower
19553 in the line. */
19554 it->descent += it->voffset;
19559 /* Produce glyphs/get display metrics for the image IT is loaded with.
19560 See the description of struct display_iterator in dispextern.h for
19561 an overview of struct display_iterator. */
19563 static void
19564 produce_image_glyph (it)
19565 struct it *it;
19567 struct image *img;
19568 struct face *face;
19569 int glyph_ascent;
19570 struct glyph_slice slice;
19572 xassert (it->what == IT_IMAGE);
19574 face = FACE_FROM_ID (it->f, it->face_id);
19575 xassert (face);
19576 /* Make sure X resources of the face is loaded. */
19577 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19579 if (it->image_id < 0)
19581 /* Fringe bitmap. */
19582 it->ascent = it->phys_ascent = 0;
19583 it->descent = it->phys_descent = 0;
19584 it->pixel_width = 0;
19585 it->nglyphs = 0;
19586 return;
19589 img = IMAGE_FROM_ID (it->f, it->image_id);
19590 xassert (img);
19591 /* Make sure X resources of the image is loaded. */
19592 prepare_image_for_display (it->f, img);
19594 slice.x = slice.y = 0;
19595 slice.width = img->width;
19596 slice.height = img->height;
19598 if (INTEGERP (it->slice.x))
19599 slice.x = XINT (it->slice.x);
19600 else if (FLOATP (it->slice.x))
19601 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19603 if (INTEGERP (it->slice.y))
19604 slice.y = XINT (it->slice.y);
19605 else if (FLOATP (it->slice.y))
19606 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19608 if (INTEGERP (it->slice.width))
19609 slice.width = XINT (it->slice.width);
19610 else if (FLOATP (it->slice.width))
19611 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19613 if (INTEGERP (it->slice.height))
19614 slice.height = XINT (it->slice.height);
19615 else if (FLOATP (it->slice.height))
19616 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19618 if (slice.x >= img->width)
19619 slice.x = img->width;
19620 if (slice.y >= img->height)
19621 slice.y = img->height;
19622 if (slice.x + slice.width >= img->width)
19623 slice.width = img->width - slice.x;
19624 if (slice.y + slice.height > img->height)
19625 slice.height = img->height - slice.y;
19627 if (slice.width == 0 || slice.height == 0)
19628 return;
19630 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19632 it->descent = slice.height - glyph_ascent;
19633 if (slice.y == 0)
19634 it->descent += img->vmargin;
19635 if (slice.y + slice.height == img->height)
19636 it->descent += img->vmargin;
19637 it->phys_descent = it->descent;
19639 it->pixel_width = slice.width;
19640 if (slice.x == 0)
19641 it->pixel_width += img->hmargin;
19642 if (slice.x + slice.width == img->width)
19643 it->pixel_width += img->hmargin;
19645 /* It's quite possible for images to have an ascent greater than
19646 their height, so don't get confused in that case. */
19647 if (it->descent < 0)
19648 it->descent = 0;
19650 #if 0 /* this breaks image tiling */
19651 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19652 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19653 if (face_ascent > it->ascent)
19654 it->ascent = it->phys_ascent = face_ascent;
19655 #endif
19657 it->nglyphs = 1;
19659 if (face->box != FACE_NO_BOX)
19661 if (face->box_line_width > 0)
19663 if (slice.y == 0)
19664 it->ascent += face->box_line_width;
19665 if (slice.y + slice.height == img->height)
19666 it->descent += face->box_line_width;
19669 if (it->start_of_box_run_p && slice.x == 0)
19670 it->pixel_width += abs (face->box_line_width);
19671 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19672 it->pixel_width += abs (face->box_line_width);
19675 take_vertical_position_into_account (it);
19677 if (it->glyph_row)
19679 struct glyph *glyph;
19680 enum glyph_row_area area = it->area;
19682 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19683 if (glyph < it->glyph_row->glyphs[area + 1])
19685 glyph->charpos = CHARPOS (it->position);
19686 glyph->object = it->object;
19687 glyph->pixel_width = it->pixel_width;
19688 glyph->ascent = glyph_ascent;
19689 glyph->descent = it->descent;
19690 glyph->voffset = it->voffset;
19691 glyph->type = IMAGE_GLYPH;
19692 glyph->multibyte_p = it->multibyte_p;
19693 glyph->left_box_line_p = it->start_of_box_run_p;
19694 glyph->right_box_line_p = it->end_of_box_run_p;
19695 glyph->overlaps_vertically_p = 0;
19696 glyph->padding_p = 0;
19697 glyph->glyph_not_available_p = 0;
19698 glyph->face_id = it->face_id;
19699 glyph->u.img_id = img->id;
19700 glyph->slice = slice;
19701 glyph->font_type = FONT_TYPE_UNKNOWN;
19702 ++it->glyph_row->used[area];
19704 else
19705 IT_EXPAND_MATRIX_WIDTH (it, area);
19710 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19711 of the glyph, WIDTH and HEIGHT are the width and height of the
19712 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19714 static void
19715 append_stretch_glyph (it, object, width, height, ascent)
19716 struct it *it;
19717 Lisp_Object object;
19718 int width, height;
19719 int ascent;
19721 struct glyph *glyph;
19722 enum glyph_row_area area = it->area;
19724 xassert (ascent >= 0 && ascent <= height);
19726 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19727 if (glyph < it->glyph_row->glyphs[area + 1])
19729 glyph->charpos = CHARPOS (it->position);
19730 glyph->object = object;
19731 glyph->pixel_width = width;
19732 glyph->ascent = ascent;
19733 glyph->descent = height - ascent;
19734 glyph->voffset = it->voffset;
19735 glyph->type = STRETCH_GLYPH;
19736 glyph->multibyte_p = it->multibyte_p;
19737 glyph->left_box_line_p = it->start_of_box_run_p;
19738 glyph->right_box_line_p = it->end_of_box_run_p;
19739 glyph->overlaps_vertically_p = 0;
19740 glyph->padding_p = 0;
19741 glyph->glyph_not_available_p = 0;
19742 glyph->face_id = it->face_id;
19743 glyph->u.stretch.ascent = ascent;
19744 glyph->u.stretch.height = height;
19745 glyph->slice = null_glyph_slice;
19746 glyph->font_type = FONT_TYPE_UNKNOWN;
19747 ++it->glyph_row->used[area];
19749 else
19750 IT_EXPAND_MATRIX_WIDTH (it, area);
19754 /* Produce a stretch glyph for iterator IT. IT->object is the value
19755 of the glyph property displayed. The value must be a list
19756 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19757 being recognized:
19759 1. `:width WIDTH' specifies that the space should be WIDTH *
19760 canonical char width wide. WIDTH may be an integer or floating
19761 point number.
19763 2. `:relative-width FACTOR' specifies that the width of the stretch
19764 should be computed from the width of the first character having the
19765 `glyph' property, and should be FACTOR times that width.
19767 3. `:align-to HPOS' specifies that the space should be wide enough
19768 to reach HPOS, a value in canonical character units.
19770 Exactly one of the above pairs must be present.
19772 4. `:height HEIGHT' specifies that the height of the stretch produced
19773 should be HEIGHT, measured in canonical character units.
19775 5. `:relative-height FACTOR' specifies that the height of the
19776 stretch should be FACTOR times the height of the characters having
19777 the glyph property.
19779 Either none or exactly one of 4 or 5 must be present.
19781 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19782 of the stretch should be used for the ascent of the stretch.
19783 ASCENT must be in the range 0 <= ASCENT <= 100. */
19785 static void
19786 produce_stretch_glyph (it)
19787 struct it *it;
19789 /* (space :width WIDTH :height HEIGHT ...) */
19790 Lisp_Object prop, plist;
19791 int width = 0, height = 0, align_to = -1;
19792 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19793 int ascent = 0;
19794 double tem;
19795 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19796 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19798 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19800 /* List should start with `space'. */
19801 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19802 plist = XCDR (it->object);
19804 /* Compute the width of the stretch. */
19805 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19806 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19808 /* Absolute width `:width WIDTH' specified and valid. */
19809 zero_width_ok_p = 1;
19810 width = (int)tem;
19812 else if (prop = Fplist_get (plist, QCrelative_width),
19813 NUMVAL (prop) > 0)
19815 /* Relative width `:relative-width FACTOR' specified and valid.
19816 Compute the width of the characters having the `glyph'
19817 property. */
19818 struct it it2;
19819 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19821 it2 = *it;
19822 if (it->multibyte_p)
19824 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19825 - IT_BYTEPOS (*it));
19826 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19828 else
19829 it2.c = *p, it2.len = 1;
19831 it2.glyph_row = NULL;
19832 it2.what = IT_CHARACTER;
19833 x_produce_glyphs (&it2);
19834 width = NUMVAL (prop) * it2.pixel_width;
19836 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19837 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19839 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19840 align_to = (align_to < 0
19842 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19843 else if (align_to < 0)
19844 align_to = window_box_left_offset (it->w, TEXT_AREA);
19845 width = max (0, (int)tem + align_to - it->current_x);
19846 zero_width_ok_p = 1;
19848 else
19849 /* Nothing specified -> width defaults to canonical char width. */
19850 width = FRAME_COLUMN_WIDTH (it->f);
19852 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19853 width = 1;
19855 /* Compute height. */
19856 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19857 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19859 height = (int)tem;
19860 zero_height_ok_p = 1;
19862 else if (prop = Fplist_get (plist, QCrelative_height),
19863 NUMVAL (prop) > 0)
19864 height = FONT_HEIGHT (font) * NUMVAL (prop);
19865 else
19866 height = FONT_HEIGHT (font);
19868 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19869 height = 1;
19871 /* Compute percentage of height used for ascent. If
19872 `:ascent ASCENT' is present and valid, use that. Otherwise,
19873 derive the ascent from the font in use. */
19874 if (prop = Fplist_get (plist, QCascent),
19875 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19876 ascent = height * NUMVAL (prop) / 100.0;
19877 else if (!NILP (prop)
19878 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19879 ascent = min (max (0, (int)tem), height);
19880 else
19881 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19883 if (width > 0 && !it->truncate_lines_p
19884 && it->current_x + width > it->last_visible_x)
19885 width = it->last_visible_x - it->current_x - 1;
19887 if (width > 0 && height > 0 && it->glyph_row)
19889 Lisp_Object object = it->stack[it->sp - 1].string;
19890 if (!STRINGP (object))
19891 object = it->w->buffer;
19892 append_stretch_glyph (it, object, width, height, ascent);
19895 it->pixel_width = width;
19896 it->ascent = it->phys_ascent = ascent;
19897 it->descent = it->phys_descent = height - it->ascent;
19898 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19900 take_vertical_position_into_account (it);
19903 /* Get line-height and line-spacing property at point.
19904 If line-height has format (HEIGHT TOTAL), return TOTAL
19905 in TOTAL_HEIGHT. */
19907 static Lisp_Object
19908 get_line_height_property (it, prop)
19909 struct it *it;
19910 Lisp_Object prop;
19912 Lisp_Object position;
19914 if (STRINGP (it->object))
19915 position = make_number (IT_STRING_CHARPOS (*it));
19916 else if (BUFFERP (it->object))
19917 position = make_number (IT_CHARPOS (*it));
19918 else
19919 return Qnil;
19921 return Fget_char_property (position, prop, it->object);
19924 /* Calculate line-height and line-spacing properties.
19925 An integer value specifies explicit pixel value.
19926 A float value specifies relative value to current face height.
19927 A cons (float . face-name) specifies relative value to
19928 height of specified face font.
19930 Returns height in pixels, or nil. */
19933 static Lisp_Object
19934 calc_line_height_property (it, val, font, boff, override)
19935 struct it *it;
19936 Lisp_Object val;
19937 XFontStruct *font;
19938 int boff, override;
19940 Lisp_Object face_name = Qnil;
19941 int ascent, descent, height;
19943 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19944 return val;
19946 if (CONSP (val))
19948 face_name = XCAR (val);
19949 val = XCDR (val);
19950 if (!NUMBERP (val))
19951 val = make_number (1);
19952 if (NILP (face_name))
19954 height = it->ascent + it->descent;
19955 goto scale;
19959 if (NILP (face_name))
19961 font = FRAME_FONT (it->f);
19962 boff = FRAME_BASELINE_OFFSET (it->f);
19964 else if (EQ (face_name, Qt))
19966 override = 0;
19968 else
19970 int face_id;
19971 struct face *face;
19972 struct font_info *font_info;
19974 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19975 if (face_id < 0)
19976 return make_number (-1);
19978 face = FACE_FROM_ID (it->f, face_id);
19979 font = face->font;
19980 if (font == NULL)
19981 return make_number (-1);
19983 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19984 boff = font_info->baseline_offset;
19985 if (font_info->vertical_centering)
19986 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19989 ascent = FONT_BASE (font) + boff;
19990 descent = FONT_DESCENT (font) - boff;
19992 if (override)
19994 it->override_ascent = ascent;
19995 it->override_descent = descent;
19996 it->override_boff = boff;
19999 height = ascent + descent;
20001 scale:
20002 if (FLOATP (val))
20003 height = (int)(XFLOAT_DATA (val) * height);
20004 else if (INTEGERP (val))
20005 height *= XINT (val);
20007 return make_number (height);
20011 /* RIF:
20012 Produce glyphs/get display metrics for the display element IT is
20013 loaded with. See the description of struct it in dispextern.h
20014 for an overview of struct it. */
20016 void
20017 x_produce_glyphs (it)
20018 struct it *it;
20020 int extra_line_spacing = it->extra_line_spacing;
20022 it->glyph_not_available_p = 0;
20024 if (it->what == IT_CHARACTER)
20026 XChar2b char2b;
20027 XFontStruct *font;
20028 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20029 XCharStruct *pcm;
20030 int font_not_found_p;
20031 struct font_info *font_info;
20032 int boff; /* baseline offset */
20033 /* We may change it->multibyte_p upon unibyte<->multibyte
20034 conversion. So, save the current value now and restore it
20035 later.
20037 Note: It seems that we don't have to record multibyte_p in
20038 struct glyph because the character code itself tells if or
20039 not the character is multibyte. Thus, in the future, we must
20040 consider eliminating the field `multibyte_p' in the struct
20041 glyph. */
20042 int saved_multibyte_p = it->multibyte_p;
20044 /* Maybe translate single-byte characters to multibyte, or the
20045 other way. */
20046 it->char_to_display = it->c;
20047 if (!ASCII_BYTE_P (it->c))
20049 if (unibyte_display_via_language_environment
20050 && SINGLE_BYTE_CHAR_P (it->c)
20051 && (it->c >= 0240
20052 || !NILP (Vnonascii_translation_table)))
20054 it->char_to_display = unibyte_char_to_multibyte (it->c);
20055 it->multibyte_p = 1;
20056 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20057 face = FACE_FROM_ID (it->f, it->face_id);
20059 else if (!SINGLE_BYTE_CHAR_P (it->c)
20060 && !it->multibyte_p)
20062 it->multibyte_p = 1;
20063 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20064 face = FACE_FROM_ID (it->f, it->face_id);
20068 /* Get font to use. Encode IT->char_to_display. */
20069 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20070 &char2b, it->multibyte_p, 0);
20071 font = face->font;
20073 /* When no suitable font found, use the default font. */
20074 font_not_found_p = font == NULL;
20075 if (font_not_found_p)
20077 font = FRAME_FONT (it->f);
20078 boff = FRAME_BASELINE_OFFSET (it->f);
20079 font_info = NULL;
20081 else
20083 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20084 boff = font_info->baseline_offset;
20085 if (font_info->vertical_centering)
20086 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20089 if (it->char_to_display >= ' '
20090 && (!it->multibyte_p || it->char_to_display < 128))
20092 /* Either unibyte or ASCII. */
20093 int stretched_p;
20095 it->nglyphs = 1;
20097 pcm = FRAME_RIF (it->f)->per_char_metric
20098 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20100 if (it->override_ascent >= 0)
20102 it->ascent = it->override_ascent;
20103 it->descent = it->override_descent;
20104 boff = it->override_boff;
20106 else
20108 it->ascent = FONT_BASE (font) + boff;
20109 it->descent = FONT_DESCENT (font) - boff;
20112 if (pcm)
20114 it->phys_ascent = pcm->ascent + boff;
20115 it->phys_descent = pcm->descent - boff;
20116 it->pixel_width = pcm->width;
20118 else
20120 it->glyph_not_available_p = 1;
20121 it->phys_ascent = it->ascent;
20122 it->phys_descent = it->descent;
20123 it->pixel_width = FONT_WIDTH (font);
20126 if (it->constrain_row_ascent_descent_p)
20128 if (it->descent > it->max_descent)
20130 it->ascent += it->descent - it->max_descent;
20131 it->descent = it->max_descent;
20133 if (it->ascent > it->max_ascent)
20135 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20136 it->ascent = it->max_ascent;
20138 it->phys_ascent = min (it->phys_ascent, it->ascent);
20139 it->phys_descent = min (it->phys_descent, it->descent);
20140 extra_line_spacing = 0;
20143 /* If this is a space inside a region of text with
20144 `space-width' property, change its width. */
20145 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20146 if (stretched_p)
20147 it->pixel_width *= XFLOATINT (it->space_width);
20149 /* If face has a box, add the box thickness to the character
20150 height. If character has a box line to the left and/or
20151 right, add the box line width to the character's width. */
20152 if (face->box != FACE_NO_BOX)
20154 int thick = face->box_line_width;
20156 if (thick > 0)
20158 it->ascent += thick;
20159 it->descent += thick;
20161 else
20162 thick = -thick;
20164 if (it->start_of_box_run_p)
20165 it->pixel_width += thick;
20166 if (it->end_of_box_run_p)
20167 it->pixel_width += thick;
20170 /* If face has an overline, add the height of the overline
20171 (1 pixel) and a 1 pixel margin to the character height. */
20172 if (face->overline_p)
20173 it->ascent += 2;
20175 if (it->constrain_row_ascent_descent_p)
20177 if (it->ascent > it->max_ascent)
20178 it->ascent = it->max_ascent;
20179 if (it->descent > it->max_descent)
20180 it->descent = it->max_descent;
20183 take_vertical_position_into_account (it);
20185 /* If we have to actually produce glyphs, do it. */
20186 if (it->glyph_row)
20188 if (stretched_p)
20190 /* Translate a space with a `space-width' property
20191 into a stretch glyph. */
20192 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20193 / FONT_HEIGHT (font));
20194 append_stretch_glyph (it, it->object, it->pixel_width,
20195 it->ascent + it->descent, ascent);
20197 else
20198 append_glyph (it);
20200 /* If characters with lbearing or rbearing are displayed
20201 in this line, record that fact in a flag of the
20202 glyph row. This is used to optimize X output code. */
20203 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20204 it->glyph_row->contains_overlapping_glyphs_p = 1;
20207 else if (it->char_to_display == '\n')
20209 /* A newline has no width but we need the height of the line.
20210 But if previous part of the line set a height, don't
20211 increase that height */
20213 Lisp_Object height;
20214 Lisp_Object total_height = Qnil;
20216 it->override_ascent = -1;
20217 it->pixel_width = 0;
20218 it->nglyphs = 0;
20220 height = get_line_height_property(it, Qline_height);
20221 /* Split (line-height total-height) list */
20222 if (CONSP (height)
20223 && CONSP (XCDR (height))
20224 && NILP (XCDR (XCDR (height))))
20226 total_height = XCAR (XCDR (height));
20227 height = XCAR (height);
20229 height = calc_line_height_property(it, height, font, boff, 1);
20231 if (it->override_ascent >= 0)
20233 it->ascent = it->override_ascent;
20234 it->descent = it->override_descent;
20235 boff = it->override_boff;
20237 else
20239 it->ascent = FONT_BASE (font) + boff;
20240 it->descent = FONT_DESCENT (font) - boff;
20243 if (EQ (height, Qt))
20245 if (it->descent > it->max_descent)
20247 it->ascent += it->descent - it->max_descent;
20248 it->descent = it->max_descent;
20250 if (it->ascent > it->max_ascent)
20252 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20253 it->ascent = it->max_ascent;
20255 it->phys_ascent = min (it->phys_ascent, it->ascent);
20256 it->phys_descent = min (it->phys_descent, it->descent);
20257 it->constrain_row_ascent_descent_p = 1;
20258 extra_line_spacing = 0;
20260 else
20262 Lisp_Object spacing;
20264 it->phys_ascent = it->ascent;
20265 it->phys_descent = it->descent;
20267 if ((it->max_ascent > 0 || it->max_descent > 0)
20268 && face->box != FACE_NO_BOX
20269 && face->box_line_width > 0)
20271 it->ascent += face->box_line_width;
20272 it->descent += face->box_line_width;
20274 if (!NILP (height)
20275 && XINT (height) > it->ascent + it->descent)
20276 it->ascent = XINT (height) - it->descent;
20278 if (!NILP (total_height))
20279 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20280 else
20282 spacing = get_line_height_property(it, Qline_spacing);
20283 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20285 if (INTEGERP (spacing))
20287 extra_line_spacing = XINT (spacing);
20288 if (!NILP (total_height))
20289 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20293 else if (it->char_to_display == '\t')
20295 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20296 int x = it->current_x + it->continuation_lines_width;
20297 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20299 /* If the distance from the current position to the next tab
20300 stop is less than a space character width, use the
20301 tab stop after that. */
20302 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20303 next_tab_x += tab_width;
20305 it->pixel_width = next_tab_x - x;
20306 it->nglyphs = 1;
20307 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20308 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20310 if (it->glyph_row)
20312 append_stretch_glyph (it, it->object, it->pixel_width,
20313 it->ascent + it->descent, it->ascent);
20316 else
20318 /* A multi-byte character. Assume that the display width of the
20319 character is the width of the character multiplied by the
20320 width of the font. */
20322 /* If we found a font, this font should give us the right
20323 metrics. If we didn't find a font, use the frame's
20324 default font and calculate the width of the character
20325 from the charset width; this is what old redisplay code
20326 did. */
20328 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20329 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20331 if (font_not_found_p || !pcm)
20333 int charset = CHAR_CHARSET (it->char_to_display);
20335 it->glyph_not_available_p = 1;
20336 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20337 * CHARSET_WIDTH (charset));
20338 it->phys_ascent = FONT_BASE (font) + boff;
20339 it->phys_descent = FONT_DESCENT (font) - boff;
20341 else
20343 it->pixel_width = pcm->width;
20344 it->phys_ascent = pcm->ascent + boff;
20345 it->phys_descent = pcm->descent - boff;
20346 if (it->glyph_row
20347 && (pcm->lbearing < 0
20348 || pcm->rbearing > pcm->width))
20349 it->glyph_row->contains_overlapping_glyphs_p = 1;
20351 it->nglyphs = 1;
20352 it->ascent = FONT_BASE (font) + boff;
20353 it->descent = FONT_DESCENT (font) - boff;
20354 if (face->box != FACE_NO_BOX)
20356 int thick = face->box_line_width;
20358 if (thick > 0)
20360 it->ascent += thick;
20361 it->descent += thick;
20363 else
20364 thick = - thick;
20366 if (it->start_of_box_run_p)
20367 it->pixel_width += thick;
20368 if (it->end_of_box_run_p)
20369 it->pixel_width += thick;
20372 /* If face has an overline, add the height of the overline
20373 (1 pixel) and a 1 pixel margin to the character height. */
20374 if (face->overline_p)
20375 it->ascent += 2;
20377 take_vertical_position_into_account (it);
20379 if (it->glyph_row)
20380 append_glyph (it);
20382 it->multibyte_p = saved_multibyte_p;
20384 else if (it->what == IT_COMPOSITION)
20386 /* Note: A composition is represented as one glyph in the
20387 glyph matrix. There are no padding glyphs. */
20388 XChar2b char2b;
20389 XFontStruct *font;
20390 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20391 XCharStruct *pcm;
20392 int font_not_found_p;
20393 struct font_info *font_info;
20394 int boff; /* baseline offset */
20395 struct composition *cmp = composition_table[it->cmp_id];
20397 /* Maybe translate single-byte characters to multibyte. */
20398 it->char_to_display = it->c;
20399 if (unibyte_display_via_language_environment
20400 && SINGLE_BYTE_CHAR_P (it->c)
20401 && (it->c >= 0240
20402 || (it->c >= 0200
20403 && !NILP (Vnonascii_translation_table))))
20405 it->char_to_display = unibyte_char_to_multibyte (it->c);
20408 /* Get face and font to use. Encode IT->char_to_display. */
20409 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20410 face = FACE_FROM_ID (it->f, it->face_id);
20411 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20412 &char2b, it->multibyte_p, 0);
20413 font = face->font;
20415 /* When no suitable font found, use the default font. */
20416 font_not_found_p = font == NULL;
20417 if (font_not_found_p)
20419 font = FRAME_FONT (it->f);
20420 boff = FRAME_BASELINE_OFFSET (it->f);
20421 font_info = NULL;
20423 else
20425 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20426 boff = font_info->baseline_offset;
20427 if (font_info->vertical_centering)
20428 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20431 /* There are no padding glyphs, so there is only one glyph to
20432 produce for the composition. Important is that pixel_width,
20433 ascent and descent are the values of what is drawn by
20434 draw_glyphs (i.e. the values of the overall glyphs composed). */
20435 it->nglyphs = 1;
20437 /* If we have not yet calculated pixel size data of glyphs of
20438 the composition for the current face font, calculate them
20439 now. Theoretically, we have to check all fonts for the
20440 glyphs, but that requires much time and memory space. So,
20441 here we check only the font of the first glyph. This leads
20442 to incorrect display very rarely, and C-l (recenter) can
20443 correct the display anyway. */
20444 if (cmp->font != (void *) font)
20446 /* Ascent and descent of the font of the first character of
20447 this composition (adjusted by baseline offset). Ascent
20448 and descent of overall glyphs should not be less than
20449 them respectively. */
20450 int font_ascent = FONT_BASE (font) + boff;
20451 int font_descent = FONT_DESCENT (font) - boff;
20452 /* Bounding box of the overall glyphs. */
20453 int leftmost, rightmost, lowest, highest;
20454 int i, width, ascent, descent;
20456 cmp->font = (void *) font;
20458 /* Initialize the bounding box. */
20459 if (font_info
20460 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20461 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20463 width = pcm->width;
20464 ascent = pcm->ascent;
20465 descent = pcm->descent;
20467 else
20469 width = FONT_WIDTH (font);
20470 ascent = FONT_BASE (font);
20471 descent = FONT_DESCENT (font);
20474 rightmost = width;
20475 lowest = - descent + boff;
20476 highest = ascent + boff;
20477 leftmost = 0;
20479 if (font_info
20480 && font_info->default_ascent
20481 && CHAR_TABLE_P (Vuse_default_ascent)
20482 && !NILP (Faref (Vuse_default_ascent,
20483 make_number (it->char_to_display))))
20484 highest = font_info->default_ascent + boff;
20486 /* Draw the first glyph at the normal position. It may be
20487 shifted to right later if some other glyphs are drawn at
20488 the left. */
20489 cmp->offsets[0] = 0;
20490 cmp->offsets[1] = boff;
20492 /* Set cmp->offsets for the remaining glyphs. */
20493 for (i = 1; i < cmp->glyph_len; i++)
20495 int left, right, btm, top;
20496 int ch = COMPOSITION_GLYPH (cmp, i);
20497 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20499 face = FACE_FROM_ID (it->f, face_id);
20500 get_char_face_and_encoding (it->f, ch, face->id,
20501 &char2b, it->multibyte_p, 0);
20502 font = face->font;
20503 if (font == NULL)
20505 font = FRAME_FONT (it->f);
20506 boff = FRAME_BASELINE_OFFSET (it->f);
20507 font_info = NULL;
20509 else
20511 font_info
20512 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20513 boff = font_info->baseline_offset;
20514 if (font_info->vertical_centering)
20515 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20518 if (font_info
20519 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20520 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20522 width = pcm->width;
20523 ascent = pcm->ascent;
20524 descent = pcm->descent;
20526 else
20528 width = FONT_WIDTH (font);
20529 ascent = 1;
20530 descent = 0;
20533 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20535 /* Relative composition with or without
20536 alternate chars. */
20537 left = (leftmost + rightmost - width) / 2;
20538 btm = - descent + boff;
20539 if (font_info && font_info->relative_compose
20540 && (! CHAR_TABLE_P (Vignore_relative_composition)
20541 || NILP (Faref (Vignore_relative_composition,
20542 make_number (ch)))))
20545 if (- descent >= font_info->relative_compose)
20546 /* One extra pixel between two glyphs. */
20547 btm = highest + 1;
20548 else if (ascent <= 0)
20549 /* One extra pixel between two glyphs. */
20550 btm = lowest - 1 - ascent - descent;
20553 else
20555 /* A composition rule is specified by an integer
20556 value that encodes global and new reference
20557 points (GREF and NREF). GREF and NREF are
20558 specified by numbers as below:
20560 0---1---2 -- ascent
20564 9--10--11 -- center
20566 ---3---4---5--- baseline
20568 6---7---8 -- descent
20570 int rule = COMPOSITION_RULE (cmp, i);
20571 int gref, nref, grefx, grefy, nrefx, nrefy;
20573 COMPOSITION_DECODE_RULE (rule, gref, nref);
20574 grefx = gref % 3, nrefx = nref % 3;
20575 grefy = gref / 3, nrefy = nref / 3;
20577 left = (leftmost
20578 + grefx * (rightmost - leftmost) / 2
20579 - nrefx * width / 2);
20580 btm = ((grefy == 0 ? highest
20581 : grefy == 1 ? 0
20582 : grefy == 2 ? lowest
20583 : (highest + lowest) / 2)
20584 - (nrefy == 0 ? ascent + descent
20585 : nrefy == 1 ? descent - boff
20586 : nrefy == 2 ? 0
20587 : (ascent + descent) / 2));
20590 cmp->offsets[i * 2] = left;
20591 cmp->offsets[i * 2 + 1] = btm + descent;
20593 /* Update the bounding box of the overall glyphs. */
20594 right = left + width;
20595 top = btm + descent + ascent;
20596 if (left < leftmost)
20597 leftmost = left;
20598 if (right > rightmost)
20599 rightmost = right;
20600 if (top > highest)
20601 highest = top;
20602 if (btm < lowest)
20603 lowest = btm;
20606 /* If there are glyphs whose x-offsets are negative,
20607 shift all glyphs to the right and make all x-offsets
20608 non-negative. */
20609 if (leftmost < 0)
20611 for (i = 0; i < cmp->glyph_len; i++)
20612 cmp->offsets[i * 2] -= leftmost;
20613 rightmost -= leftmost;
20616 cmp->pixel_width = rightmost;
20617 cmp->ascent = highest;
20618 cmp->descent = - lowest;
20619 if (cmp->ascent < font_ascent)
20620 cmp->ascent = font_ascent;
20621 if (cmp->descent < font_descent)
20622 cmp->descent = font_descent;
20625 it->pixel_width = cmp->pixel_width;
20626 it->ascent = it->phys_ascent = cmp->ascent;
20627 it->descent = it->phys_descent = cmp->descent;
20629 if (face->box != FACE_NO_BOX)
20631 int thick = face->box_line_width;
20633 if (thick > 0)
20635 it->ascent += thick;
20636 it->descent += thick;
20638 else
20639 thick = - thick;
20641 if (it->start_of_box_run_p)
20642 it->pixel_width += thick;
20643 if (it->end_of_box_run_p)
20644 it->pixel_width += thick;
20647 /* If face has an overline, add the height of the overline
20648 (1 pixel) and a 1 pixel margin to the character height. */
20649 if (face->overline_p)
20650 it->ascent += 2;
20652 take_vertical_position_into_account (it);
20654 if (it->glyph_row)
20655 append_composite_glyph (it);
20657 else if (it->what == IT_IMAGE)
20658 produce_image_glyph (it);
20659 else if (it->what == IT_STRETCH)
20660 produce_stretch_glyph (it);
20662 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20663 because this isn't true for images with `:ascent 100'. */
20664 xassert (it->ascent >= 0 && it->descent >= 0);
20665 if (it->area == TEXT_AREA)
20666 it->current_x += it->pixel_width;
20668 if (extra_line_spacing > 0)
20670 it->descent += extra_line_spacing;
20671 if (extra_line_spacing > it->max_extra_line_spacing)
20672 it->max_extra_line_spacing = extra_line_spacing;
20675 it->max_ascent = max (it->max_ascent, it->ascent);
20676 it->max_descent = max (it->max_descent, it->descent);
20677 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20678 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20681 /* EXPORT for RIF:
20682 Output LEN glyphs starting at START at the nominal cursor position.
20683 Advance the nominal cursor over the text. The global variable
20684 updated_window contains the window being updated, updated_row is
20685 the glyph row being updated, and updated_area is the area of that
20686 row being updated. */
20688 void
20689 x_write_glyphs (start, len)
20690 struct glyph *start;
20691 int len;
20693 int x, hpos;
20695 xassert (updated_window && updated_row);
20696 BLOCK_INPUT;
20698 /* Write glyphs. */
20700 hpos = start - updated_row->glyphs[updated_area];
20701 x = draw_glyphs (updated_window, output_cursor.x,
20702 updated_row, updated_area,
20703 hpos, hpos + len,
20704 DRAW_NORMAL_TEXT, 0);
20706 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20707 if (updated_area == TEXT_AREA
20708 && updated_window->phys_cursor_on_p
20709 && updated_window->phys_cursor.vpos == output_cursor.vpos
20710 && updated_window->phys_cursor.hpos >= hpos
20711 && updated_window->phys_cursor.hpos < hpos + len)
20712 updated_window->phys_cursor_on_p = 0;
20714 UNBLOCK_INPUT;
20716 /* Advance the output cursor. */
20717 output_cursor.hpos += len;
20718 output_cursor.x = x;
20722 /* EXPORT for RIF:
20723 Insert LEN glyphs from START at the nominal cursor position. */
20725 void
20726 x_insert_glyphs (start, len)
20727 struct glyph *start;
20728 int len;
20730 struct frame *f;
20731 struct window *w;
20732 int line_height, shift_by_width, shifted_region_width;
20733 struct glyph_row *row;
20734 struct glyph *glyph;
20735 int frame_x, frame_y, hpos;
20737 xassert (updated_window && updated_row);
20738 BLOCK_INPUT;
20739 w = updated_window;
20740 f = XFRAME (WINDOW_FRAME (w));
20742 /* Get the height of the line we are in. */
20743 row = updated_row;
20744 line_height = row->height;
20746 /* Get the width of the glyphs to insert. */
20747 shift_by_width = 0;
20748 for (glyph = start; glyph < start + len; ++glyph)
20749 shift_by_width += glyph->pixel_width;
20751 /* Get the width of the region to shift right. */
20752 shifted_region_width = (window_box_width (w, updated_area)
20753 - output_cursor.x
20754 - shift_by_width);
20756 /* Shift right. */
20757 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20758 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20760 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20761 line_height, shift_by_width);
20763 /* Write the glyphs. */
20764 hpos = start - row->glyphs[updated_area];
20765 draw_glyphs (w, output_cursor.x, row, updated_area,
20766 hpos, hpos + len,
20767 DRAW_NORMAL_TEXT, 0);
20769 /* Advance the output cursor. */
20770 output_cursor.hpos += len;
20771 output_cursor.x += shift_by_width;
20772 UNBLOCK_INPUT;
20776 /* EXPORT for RIF:
20777 Erase the current text line from the nominal cursor position
20778 (inclusive) to pixel column TO_X (exclusive). The idea is that
20779 everything from TO_X onward is already erased.
20781 TO_X is a pixel position relative to updated_area of
20782 updated_window. TO_X == -1 means clear to the end of this area. */
20784 void
20785 x_clear_end_of_line (to_x)
20786 int to_x;
20788 struct frame *f;
20789 struct window *w = updated_window;
20790 int max_x, min_y, max_y;
20791 int from_x, from_y, to_y;
20793 xassert (updated_window && updated_row);
20794 f = XFRAME (w->frame);
20796 if (updated_row->full_width_p)
20797 max_x = WINDOW_TOTAL_WIDTH (w);
20798 else
20799 max_x = window_box_width (w, updated_area);
20800 max_y = window_text_bottom_y (w);
20802 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20803 of window. For TO_X > 0, truncate to end of drawing area. */
20804 if (to_x == 0)
20805 return;
20806 else if (to_x < 0)
20807 to_x = max_x;
20808 else
20809 to_x = min (to_x, max_x);
20811 to_y = min (max_y, output_cursor.y + updated_row->height);
20813 /* Notice if the cursor will be cleared by this operation. */
20814 if (!updated_row->full_width_p)
20815 notice_overwritten_cursor (w, updated_area,
20816 output_cursor.x, -1,
20817 updated_row->y,
20818 MATRIX_ROW_BOTTOM_Y (updated_row));
20820 from_x = output_cursor.x;
20822 /* Translate to frame coordinates. */
20823 if (updated_row->full_width_p)
20825 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20826 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20828 else
20830 int area_left = window_box_left (w, updated_area);
20831 from_x += area_left;
20832 to_x += area_left;
20835 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20836 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20837 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20839 /* Prevent inadvertently clearing to end of the X window. */
20840 if (to_x > from_x && to_y > from_y)
20842 BLOCK_INPUT;
20843 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
20844 to_x - from_x, to_y - from_y);
20845 UNBLOCK_INPUT;
20849 #endif /* HAVE_WINDOW_SYSTEM */
20853 /***********************************************************************
20854 Cursor types
20855 ***********************************************************************/
20857 /* Value is the internal representation of the specified cursor type
20858 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20859 of the bar cursor. */
20861 static enum text_cursor_kinds
20862 get_specified_cursor_type (arg, width)
20863 Lisp_Object arg;
20864 int *width;
20866 enum text_cursor_kinds type;
20868 if (NILP (arg))
20869 return NO_CURSOR;
20871 if (EQ (arg, Qbox))
20872 return FILLED_BOX_CURSOR;
20874 if (EQ (arg, Qhollow))
20875 return HOLLOW_BOX_CURSOR;
20877 if (EQ (arg, Qbar))
20879 *width = 2;
20880 return BAR_CURSOR;
20883 if (CONSP (arg)
20884 && EQ (XCAR (arg), Qbar)
20885 && INTEGERP (XCDR (arg))
20886 && XINT (XCDR (arg)) >= 0)
20888 *width = XINT (XCDR (arg));
20889 return BAR_CURSOR;
20892 if (EQ (arg, Qhbar))
20894 *width = 2;
20895 return HBAR_CURSOR;
20898 if (CONSP (arg)
20899 && EQ (XCAR (arg), Qhbar)
20900 && INTEGERP (XCDR (arg))
20901 && XINT (XCDR (arg)) >= 0)
20903 *width = XINT (XCDR (arg));
20904 return HBAR_CURSOR;
20907 /* Treat anything unknown as "hollow box cursor".
20908 It was bad to signal an error; people have trouble fixing
20909 .Xdefaults with Emacs, when it has something bad in it. */
20910 type = HOLLOW_BOX_CURSOR;
20912 return type;
20915 /* Set the default cursor types for specified frame. */
20916 void
20917 set_frame_cursor_types (f, arg)
20918 struct frame *f;
20919 Lisp_Object arg;
20921 int width;
20922 Lisp_Object tem;
20924 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20925 FRAME_CURSOR_WIDTH (f) = width;
20927 /* By default, set up the blink-off state depending on the on-state. */
20929 tem = Fassoc (arg, Vblink_cursor_alist);
20930 if (!NILP (tem))
20932 FRAME_BLINK_OFF_CURSOR (f)
20933 = get_specified_cursor_type (XCDR (tem), &width);
20934 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20936 else
20937 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20941 /* Return the cursor we want to be displayed in window W. Return
20942 width of bar/hbar cursor through WIDTH arg. Return with
20943 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20944 (i.e. if the `system caret' should track this cursor).
20946 In a mini-buffer window, we want the cursor only to appear if we
20947 are reading input from this window. For the selected window, we
20948 want the cursor type given by the frame parameter or buffer local
20949 setting of cursor-type. If explicitly marked off, draw no cursor.
20950 In all other cases, we want a hollow box cursor. */
20952 static enum text_cursor_kinds
20953 get_window_cursor_type (w, glyph, width, active_cursor)
20954 struct window *w;
20955 struct glyph *glyph;
20956 int *width;
20957 int *active_cursor;
20959 struct frame *f = XFRAME (w->frame);
20960 struct buffer *b = XBUFFER (w->buffer);
20961 int cursor_type = DEFAULT_CURSOR;
20962 Lisp_Object alt_cursor;
20963 int non_selected = 0;
20965 *active_cursor = 1;
20967 /* Echo area */
20968 if (cursor_in_echo_area
20969 && FRAME_HAS_MINIBUF_P (f)
20970 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20972 if (w == XWINDOW (echo_area_window))
20974 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20976 *width = FRAME_CURSOR_WIDTH (f);
20977 return FRAME_DESIRED_CURSOR (f);
20979 else
20980 return get_specified_cursor_type (b->cursor_type, width);
20983 *active_cursor = 0;
20984 non_selected = 1;
20987 /* Nonselected window or nonselected frame. */
20988 else if (w != XWINDOW (f->selected_window)
20989 #ifdef HAVE_WINDOW_SYSTEM
20990 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20991 #endif
20994 *active_cursor = 0;
20996 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20997 return NO_CURSOR;
20999 non_selected = 1;
21002 /* Never display a cursor in a window in which cursor-type is nil. */
21003 if (NILP (b->cursor_type))
21004 return NO_CURSOR;
21006 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21007 if (non_selected)
21009 alt_cursor = b->cursor_in_non_selected_windows;
21010 return get_specified_cursor_type (alt_cursor, width);
21013 /* Get the normal cursor type for this window. */
21014 if (EQ (b->cursor_type, Qt))
21016 cursor_type = FRAME_DESIRED_CURSOR (f);
21017 *width = FRAME_CURSOR_WIDTH (f);
21019 else
21020 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21022 /* Use normal cursor if not blinked off. */
21023 if (!w->cursor_off_p)
21025 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21026 if (cursor_type == FILLED_BOX_CURSOR)
21027 cursor_type = HOLLOW_BOX_CURSOR;
21029 return cursor_type;
21032 /* Cursor is blinked off, so determine how to "toggle" it. */
21034 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21035 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21036 return get_specified_cursor_type (XCDR (alt_cursor), width);
21038 /* Then see if frame has specified a specific blink off cursor type. */
21039 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21041 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21042 return FRAME_BLINK_OFF_CURSOR (f);
21045 #if 0
21046 /* Some people liked having a permanently visible blinking cursor,
21047 while others had very strong opinions against it. So it was
21048 decided to remove it. KFS 2003-09-03 */
21050 /* Finally perform built-in cursor blinking:
21051 filled box <-> hollow box
21052 wide [h]bar <-> narrow [h]bar
21053 narrow [h]bar <-> no cursor
21054 other type <-> no cursor */
21056 if (cursor_type == FILLED_BOX_CURSOR)
21057 return HOLLOW_BOX_CURSOR;
21059 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21061 *width = 1;
21062 return cursor_type;
21064 #endif
21066 return NO_CURSOR;
21070 #ifdef HAVE_WINDOW_SYSTEM
21072 /* Notice when the text cursor of window W has been completely
21073 overwritten by a drawing operation that outputs glyphs in AREA
21074 starting at X0 and ending at X1 in the line starting at Y0 and
21075 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21076 the rest of the line after X0 has been written. Y coordinates
21077 are window-relative. */
21079 static void
21080 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21081 struct window *w;
21082 enum glyph_row_area area;
21083 int x0, y0, x1, y1;
21085 int cx0, cx1, cy0, cy1;
21086 struct glyph_row *row;
21088 if (!w->phys_cursor_on_p)
21089 return;
21090 if (area != TEXT_AREA)
21091 return;
21093 if (w->phys_cursor.vpos < 0
21094 || w->phys_cursor.vpos >= w->current_matrix->nrows
21095 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21096 !(row->enabled_p && row->displays_text_p)))
21097 return;
21099 if (row->cursor_in_fringe_p)
21101 row->cursor_in_fringe_p = 0;
21102 draw_fringe_bitmap (w, row, 0);
21103 w->phys_cursor_on_p = 0;
21104 return;
21107 cx0 = w->phys_cursor.x;
21108 cx1 = cx0 + w->phys_cursor_width;
21109 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21110 return;
21112 /* The cursor image will be completely removed from the
21113 screen if the output area intersects the cursor area in
21114 y-direction. When we draw in [y0 y1[, and some part of
21115 the cursor is at y < y0, that part must have been drawn
21116 before. When scrolling, the cursor is erased before
21117 actually scrolling, so we don't come here. When not
21118 scrolling, the rows above the old cursor row must have
21119 changed, and in this case these rows must have written
21120 over the cursor image.
21122 Likewise if part of the cursor is below y1, with the
21123 exception of the cursor being in the first blank row at
21124 the buffer and window end because update_text_area
21125 doesn't draw that row. (Except when it does, but
21126 that's handled in update_text_area.) */
21128 cy0 = w->phys_cursor.y;
21129 cy1 = cy0 + w->phys_cursor_height;
21130 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21131 return;
21133 w->phys_cursor_on_p = 0;
21136 #endif /* HAVE_WINDOW_SYSTEM */
21139 /************************************************************************
21140 Mouse Face
21141 ************************************************************************/
21143 #ifdef HAVE_WINDOW_SYSTEM
21145 /* EXPORT for RIF:
21146 Fix the display of area AREA of overlapping row ROW in window W
21147 with respect to the overlapping part OVERLAPS. */
21149 void
21150 x_fix_overlapping_area (w, row, area, overlaps)
21151 struct window *w;
21152 struct glyph_row *row;
21153 enum glyph_row_area area;
21154 int overlaps;
21156 int i, x;
21158 BLOCK_INPUT;
21160 x = 0;
21161 for (i = 0; i < row->used[area];)
21163 if (row->glyphs[area][i].overlaps_vertically_p)
21165 int start = i, start_x = x;
21169 x += row->glyphs[area][i].pixel_width;
21170 ++i;
21172 while (i < row->used[area]
21173 && row->glyphs[area][i].overlaps_vertically_p);
21175 draw_glyphs (w, start_x, row, area,
21176 start, i,
21177 DRAW_NORMAL_TEXT, overlaps);
21179 else
21181 x += row->glyphs[area][i].pixel_width;
21182 ++i;
21186 UNBLOCK_INPUT;
21190 /* EXPORT:
21191 Draw the cursor glyph of window W in glyph row ROW. See the
21192 comment of draw_glyphs for the meaning of HL. */
21194 void
21195 draw_phys_cursor_glyph (w, row, hl)
21196 struct window *w;
21197 struct glyph_row *row;
21198 enum draw_glyphs_face hl;
21200 /* If cursor hpos is out of bounds, don't draw garbage. This can
21201 happen in mini-buffer windows when switching between echo area
21202 glyphs and mini-buffer. */
21203 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21205 int on_p = w->phys_cursor_on_p;
21206 int x1;
21207 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21208 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21209 hl, 0);
21210 w->phys_cursor_on_p = on_p;
21212 if (hl == DRAW_CURSOR)
21213 w->phys_cursor_width = x1 - w->phys_cursor.x;
21214 /* When we erase the cursor, and ROW is overlapped by other
21215 rows, make sure that these overlapping parts of other rows
21216 are redrawn. */
21217 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21219 w->phys_cursor_width = x1 - w->phys_cursor.x;
21221 if (row > w->current_matrix->rows
21222 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21223 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21224 OVERLAPS_ERASED_CURSOR);
21226 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21227 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21228 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21229 OVERLAPS_ERASED_CURSOR);
21235 /* EXPORT:
21236 Erase the image of a cursor of window W from the screen. */
21238 void
21239 erase_phys_cursor (w)
21240 struct window *w;
21242 struct frame *f = XFRAME (w->frame);
21243 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21244 int hpos = w->phys_cursor.hpos;
21245 int vpos = w->phys_cursor.vpos;
21246 int mouse_face_here_p = 0;
21247 struct glyph_matrix *active_glyphs = w->current_matrix;
21248 struct glyph_row *cursor_row;
21249 struct glyph *cursor_glyph;
21250 enum draw_glyphs_face hl;
21252 /* No cursor displayed or row invalidated => nothing to do on the
21253 screen. */
21254 if (w->phys_cursor_type == NO_CURSOR)
21255 goto mark_cursor_off;
21257 /* VPOS >= active_glyphs->nrows means that window has been resized.
21258 Don't bother to erase the cursor. */
21259 if (vpos >= active_glyphs->nrows)
21260 goto mark_cursor_off;
21262 /* If row containing cursor is marked invalid, there is nothing we
21263 can do. */
21264 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21265 if (!cursor_row->enabled_p)
21266 goto mark_cursor_off;
21268 /* If line spacing is > 0, old cursor may only be partially visible in
21269 window after split-window. So adjust visible height. */
21270 cursor_row->visible_height = min (cursor_row->visible_height,
21271 window_text_bottom_y (w) - cursor_row->y);
21273 /* If row is completely invisible, don't attempt to delete a cursor which
21274 isn't there. This can happen if cursor is at top of a window, and
21275 we switch to a buffer with a header line in that window. */
21276 if (cursor_row->visible_height <= 0)
21277 goto mark_cursor_off;
21279 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21280 if (cursor_row->cursor_in_fringe_p)
21282 cursor_row->cursor_in_fringe_p = 0;
21283 draw_fringe_bitmap (w, cursor_row, 0);
21284 goto mark_cursor_off;
21287 /* This can happen when the new row is shorter than the old one.
21288 In this case, either draw_glyphs or clear_end_of_line
21289 should have cleared the cursor. Note that we wouldn't be
21290 able to erase the cursor in this case because we don't have a
21291 cursor glyph at hand. */
21292 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21293 goto mark_cursor_off;
21295 /* If the cursor is in the mouse face area, redisplay that when
21296 we clear the cursor. */
21297 if (! NILP (dpyinfo->mouse_face_window)
21298 && w == XWINDOW (dpyinfo->mouse_face_window)
21299 && (vpos > dpyinfo->mouse_face_beg_row
21300 || (vpos == dpyinfo->mouse_face_beg_row
21301 && hpos >= dpyinfo->mouse_face_beg_col))
21302 && (vpos < dpyinfo->mouse_face_end_row
21303 || (vpos == dpyinfo->mouse_face_end_row
21304 && hpos < dpyinfo->mouse_face_end_col))
21305 /* Don't redraw the cursor's spot in mouse face if it is at the
21306 end of a line (on a newline). The cursor appears there, but
21307 mouse highlighting does not. */
21308 && cursor_row->used[TEXT_AREA] > hpos)
21309 mouse_face_here_p = 1;
21311 /* Maybe clear the display under the cursor. */
21312 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21314 int x, y;
21315 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21316 int width;
21318 cursor_glyph = get_phys_cursor_glyph (w);
21319 if (cursor_glyph == NULL)
21320 goto mark_cursor_off;
21322 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21323 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21324 width = min (cursor_glyph->pixel_width,
21325 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21327 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21330 /* Erase the cursor by redrawing the character underneath it. */
21331 if (mouse_face_here_p)
21332 hl = DRAW_MOUSE_FACE;
21333 else
21334 hl = DRAW_NORMAL_TEXT;
21335 draw_phys_cursor_glyph (w, cursor_row, hl);
21337 mark_cursor_off:
21338 w->phys_cursor_on_p = 0;
21339 w->phys_cursor_type = NO_CURSOR;
21343 /* EXPORT:
21344 Display or clear cursor of window W. If ON is zero, clear the
21345 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21346 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21348 void
21349 display_and_set_cursor (w, on, hpos, vpos, x, y)
21350 struct window *w;
21351 int on, hpos, vpos, x, y;
21353 struct frame *f = XFRAME (w->frame);
21354 int new_cursor_type;
21355 int new_cursor_width;
21356 int active_cursor;
21357 struct glyph_row *glyph_row;
21358 struct glyph *glyph;
21360 /* This is pointless on invisible frames, and dangerous on garbaged
21361 windows and frames; in the latter case, the frame or window may
21362 be in the midst of changing its size, and x and y may be off the
21363 window. */
21364 if (! FRAME_VISIBLE_P (f)
21365 || FRAME_GARBAGED_P (f)
21366 || vpos >= w->current_matrix->nrows
21367 || hpos >= w->current_matrix->matrix_w)
21368 return;
21370 /* If cursor is off and we want it off, return quickly. */
21371 if (!on && !w->phys_cursor_on_p)
21372 return;
21374 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21375 /* If cursor row is not enabled, we don't really know where to
21376 display the cursor. */
21377 if (!glyph_row->enabled_p)
21379 w->phys_cursor_on_p = 0;
21380 return;
21383 glyph = NULL;
21384 if (!glyph_row->exact_window_width_line_p
21385 || hpos < glyph_row->used[TEXT_AREA])
21386 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21388 xassert (interrupt_input_blocked);
21390 /* Set new_cursor_type to the cursor we want to be displayed. */
21391 new_cursor_type = get_window_cursor_type (w, glyph,
21392 &new_cursor_width, &active_cursor);
21394 /* If cursor is currently being shown and we don't want it to be or
21395 it is in the wrong place, or the cursor type is not what we want,
21396 erase it. */
21397 if (w->phys_cursor_on_p
21398 && (!on
21399 || w->phys_cursor.x != x
21400 || w->phys_cursor.y != y
21401 || new_cursor_type != w->phys_cursor_type
21402 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21403 && new_cursor_width != w->phys_cursor_width)))
21404 erase_phys_cursor (w);
21406 /* Don't check phys_cursor_on_p here because that flag is only set
21407 to zero in some cases where we know that the cursor has been
21408 completely erased, to avoid the extra work of erasing the cursor
21409 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21410 still not be visible, or it has only been partly erased. */
21411 if (on)
21413 w->phys_cursor_ascent = glyph_row->ascent;
21414 w->phys_cursor_height = glyph_row->height;
21416 /* Set phys_cursor_.* before x_draw_.* is called because some
21417 of them may need the information. */
21418 w->phys_cursor.x = x;
21419 w->phys_cursor.y = glyph_row->y;
21420 w->phys_cursor.hpos = hpos;
21421 w->phys_cursor.vpos = vpos;
21424 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21425 new_cursor_type, new_cursor_width,
21426 on, active_cursor);
21430 /* Switch the display of W's cursor on or off, according to the value
21431 of ON. */
21433 static void
21434 update_window_cursor (w, on)
21435 struct window *w;
21436 int on;
21438 /* Don't update cursor in windows whose frame is in the process
21439 of being deleted. */
21440 if (w->current_matrix)
21442 BLOCK_INPUT;
21443 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21444 w->phys_cursor.x, w->phys_cursor.y);
21445 UNBLOCK_INPUT;
21450 /* Call update_window_cursor with parameter ON_P on all leaf windows
21451 in the window tree rooted at W. */
21453 static void
21454 update_cursor_in_window_tree (w, on_p)
21455 struct window *w;
21456 int on_p;
21458 while (w)
21460 if (!NILP (w->hchild))
21461 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21462 else if (!NILP (w->vchild))
21463 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21464 else
21465 update_window_cursor (w, on_p);
21467 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21472 /* EXPORT:
21473 Display the cursor on window W, or clear it, according to ON_P.
21474 Don't change the cursor's position. */
21476 void
21477 x_update_cursor (f, on_p)
21478 struct frame *f;
21479 int on_p;
21481 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21485 /* EXPORT:
21486 Clear the cursor of window W to background color, and mark the
21487 cursor as not shown. This is used when the text where the cursor
21488 is is about to be rewritten. */
21490 void
21491 x_clear_cursor (w)
21492 struct window *w;
21494 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21495 update_window_cursor (w, 0);
21499 /* EXPORT:
21500 Display the active region described by mouse_face_* according to DRAW. */
21502 void
21503 show_mouse_face (dpyinfo, draw)
21504 Display_Info *dpyinfo;
21505 enum draw_glyphs_face draw;
21507 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21508 struct frame *f = XFRAME (WINDOW_FRAME (w));
21510 if (/* If window is in the process of being destroyed, don't bother
21511 to do anything. */
21512 w->current_matrix != NULL
21513 /* Don't update mouse highlight if hidden */
21514 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21515 /* Recognize when we are called to operate on rows that don't exist
21516 anymore. This can happen when a window is split. */
21517 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21519 int phys_cursor_on_p = w->phys_cursor_on_p;
21520 struct glyph_row *row, *first, *last;
21522 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21523 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21525 for (row = first; row <= last && row->enabled_p; ++row)
21527 int start_hpos, end_hpos, start_x;
21529 /* For all but the first row, the highlight starts at column 0. */
21530 if (row == first)
21532 start_hpos = dpyinfo->mouse_face_beg_col;
21533 start_x = dpyinfo->mouse_face_beg_x;
21535 else
21537 start_hpos = 0;
21538 start_x = 0;
21541 if (row == last)
21542 end_hpos = dpyinfo->mouse_face_end_col;
21543 else
21545 end_hpos = row->used[TEXT_AREA];
21546 if (draw == DRAW_NORMAL_TEXT)
21547 row->fill_line_p = 1; /* Clear to end of line */
21550 if (end_hpos > start_hpos)
21552 draw_glyphs (w, start_x, row, TEXT_AREA,
21553 start_hpos, end_hpos,
21554 draw, 0);
21556 row->mouse_face_p
21557 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21561 /* When we've written over the cursor, arrange for it to
21562 be displayed again. */
21563 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21565 BLOCK_INPUT;
21566 display_and_set_cursor (w, 1,
21567 w->phys_cursor.hpos, w->phys_cursor.vpos,
21568 w->phys_cursor.x, w->phys_cursor.y);
21569 UNBLOCK_INPUT;
21573 /* Change the mouse cursor. */
21574 if (draw == DRAW_NORMAL_TEXT)
21575 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21576 else if (draw == DRAW_MOUSE_FACE)
21577 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21578 else
21579 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21582 /* EXPORT:
21583 Clear out the mouse-highlighted active region.
21584 Redraw it un-highlighted first. Value is non-zero if mouse
21585 face was actually drawn unhighlighted. */
21588 clear_mouse_face (dpyinfo)
21589 Display_Info *dpyinfo;
21591 int cleared = 0;
21593 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21595 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21596 cleared = 1;
21599 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21600 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21601 dpyinfo->mouse_face_window = Qnil;
21602 dpyinfo->mouse_face_overlay = Qnil;
21603 return cleared;
21607 /* EXPORT:
21608 Non-zero if physical cursor of window W is within mouse face. */
21611 cursor_in_mouse_face_p (w)
21612 struct window *w;
21614 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21615 int in_mouse_face = 0;
21617 if (WINDOWP (dpyinfo->mouse_face_window)
21618 && XWINDOW (dpyinfo->mouse_face_window) == w)
21620 int hpos = w->phys_cursor.hpos;
21621 int vpos = w->phys_cursor.vpos;
21623 if (vpos >= dpyinfo->mouse_face_beg_row
21624 && vpos <= dpyinfo->mouse_face_end_row
21625 && (vpos > dpyinfo->mouse_face_beg_row
21626 || hpos >= dpyinfo->mouse_face_beg_col)
21627 && (vpos < dpyinfo->mouse_face_end_row
21628 || hpos < dpyinfo->mouse_face_end_col
21629 || dpyinfo->mouse_face_past_end))
21630 in_mouse_face = 1;
21633 return in_mouse_face;
21639 /* Find the glyph matrix position of buffer position CHARPOS in window
21640 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21641 current glyphs must be up to date. If CHARPOS is above window
21642 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21643 of last line in W. In the row containing CHARPOS, stop before glyphs
21644 having STOP as object. */
21646 #if 1 /* This is a version of fast_find_position that's more correct
21647 in the presence of hscrolling, for example. I didn't install
21648 it right away because the problem fixed is minor, it failed
21649 in 20.x as well, and I think it's too risky to install
21650 so near the release of 21.1. 2001-09-25 gerd. */
21652 static int
21653 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21654 struct window *w;
21655 int charpos;
21656 int *hpos, *vpos, *x, *y;
21657 Lisp_Object stop;
21659 struct glyph_row *row, *first;
21660 struct glyph *glyph, *end;
21661 int past_end = 0;
21663 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21664 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21666 *x = first->x;
21667 *y = first->y;
21668 *hpos = 0;
21669 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21670 return 1;
21673 row = row_containing_pos (w, charpos, first, NULL, 0);
21674 if (row == NULL)
21676 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21677 past_end = 1;
21680 /* If whole rows or last part of a row came from a display overlay,
21681 row_containing_pos will skip over such rows because their end pos
21682 equals the start pos of the overlay or interval.
21684 Move back if we have a STOP object and previous row's
21685 end glyph came from STOP. */
21686 if (!NILP (stop))
21688 struct glyph_row *prev;
21689 while ((prev = row - 1, prev >= first)
21690 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21691 && prev->used[TEXT_AREA] > 0)
21693 struct glyph *beg = prev->glyphs[TEXT_AREA];
21694 glyph = beg + prev->used[TEXT_AREA];
21695 while (--glyph >= beg
21696 && INTEGERP (glyph->object));
21697 if (glyph < beg
21698 || !EQ (stop, glyph->object))
21699 break;
21700 row = prev;
21704 *x = row->x;
21705 *y = row->y;
21706 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21708 glyph = row->glyphs[TEXT_AREA];
21709 end = glyph + row->used[TEXT_AREA];
21711 /* Skip over glyphs not having an object at the start of the row.
21712 These are special glyphs like truncation marks on terminal
21713 frames. */
21714 if (row->displays_text_p)
21715 while (glyph < end
21716 && INTEGERP (glyph->object)
21717 && !EQ (stop, glyph->object)
21718 && glyph->charpos < 0)
21720 *x += glyph->pixel_width;
21721 ++glyph;
21724 while (glyph < end
21725 && !INTEGERP (glyph->object)
21726 && !EQ (stop, glyph->object)
21727 && (!BUFFERP (glyph->object)
21728 || glyph->charpos < charpos))
21730 *x += glyph->pixel_width;
21731 ++glyph;
21734 *hpos = glyph - row->glyphs[TEXT_AREA];
21735 return !past_end;
21738 #else /* not 1 */
21740 static int
21741 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21742 struct window *w;
21743 int pos;
21744 int *hpos, *vpos, *x, *y;
21745 Lisp_Object stop;
21747 int i;
21748 int lastcol;
21749 int maybe_next_line_p = 0;
21750 int line_start_position;
21751 int yb = window_text_bottom_y (w);
21752 struct glyph_row *row, *best_row;
21753 int row_vpos, best_row_vpos;
21754 int current_x;
21756 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21757 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21759 while (row->y < yb)
21761 if (row->used[TEXT_AREA])
21762 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21763 else
21764 line_start_position = 0;
21766 if (line_start_position > pos)
21767 break;
21768 /* If the position sought is the end of the buffer,
21769 don't include the blank lines at the bottom of the window. */
21770 else if (line_start_position == pos
21771 && pos == BUF_ZV (XBUFFER (w->buffer)))
21773 maybe_next_line_p = 1;
21774 break;
21776 else if (line_start_position > 0)
21778 best_row = row;
21779 best_row_vpos = row_vpos;
21782 if (row->y + row->height >= yb)
21783 break;
21785 ++row;
21786 ++row_vpos;
21789 /* Find the right column within BEST_ROW. */
21790 lastcol = 0;
21791 current_x = best_row->x;
21792 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21794 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21795 int charpos = glyph->charpos;
21797 if (BUFFERP (glyph->object))
21799 if (charpos == pos)
21801 *hpos = i;
21802 *vpos = best_row_vpos;
21803 *x = current_x;
21804 *y = best_row->y;
21805 return 1;
21807 else if (charpos > pos)
21808 break;
21810 else if (EQ (glyph->object, stop))
21811 break;
21813 if (charpos > 0)
21814 lastcol = i;
21815 current_x += glyph->pixel_width;
21818 /* If we're looking for the end of the buffer,
21819 and we didn't find it in the line we scanned,
21820 use the start of the following line. */
21821 if (maybe_next_line_p)
21823 ++best_row;
21824 ++best_row_vpos;
21825 lastcol = 0;
21826 current_x = best_row->x;
21829 *vpos = best_row_vpos;
21830 *hpos = lastcol + 1;
21831 *x = current_x;
21832 *y = best_row->y;
21833 return 0;
21836 #endif /* not 1 */
21839 /* Find the position of the glyph for position POS in OBJECT in
21840 window W's current matrix, and return in *X, *Y the pixel
21841 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21843 RIGHT_P non-zero means return the position of the right edge of the
21844 glyph, RIGHT_P zero means return the left edge position.
21846 If no glyph for POS exists in the matrix, return the position of
21847 the glyph with the next smaller position that is in the matrix, if
21848 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21849 exists in the matrix, return the position of the glyph with the
21850 next larger position in OBJECT.
21852 Value is non-zero if a glyph was found. */
21854 static int
21855 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21856 struct window *w;
21857 int pos;
21858 Lisp_Object object;
21859 int *hpos, *vpos, *x, *y;
21860 int right_p;
21862 int yb = window_text_bottom_y (w);
21863 struct glyph_row *r;
21864 struct glyph *best_glyph = NULL;
21865 struct glyph_row *best_row = NULL;
21866 int best_x = 0;
21868 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21869 r->enabled_p && r->y < yb;
21870 ++r)
21872 struct glyph *g = r->glyphs[TEXT_AREA];
21873 struct glyph *e = g + r->used[TEXT_AREA];
21874 int gx;
21876 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21877 if (EQ (g->object, object))
21879 if (g->charpos == pos)
21881 best_glyph = g;
21882 best_x = gx;
21883 best_row = r;
21884 goto found;
21886 else if (best_glyph == NULL
21887 || ((abs (g->charpos - pos)
21888 < abs (best_glyph->charpos - pos))
21889 && (right_p
21890 ? g->charpos < pos
21891 : g->charpos > pos)))
21893 best_glyph = g;
21894 best_x = gx;
21895 best_row = r;
21900 found:
21902 if (best_glyph)
21904 *x = best_x;
21905 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21907 if (right_p)
21909 *x += best_glyph->pixel_width;
21910 ++*hpos;
21913 *y = best_row->y;
21914 *vpos = best_row - w->current_matrix->rows;
21917 return best_glyph != NULL;
21921 /* See if position X, Y is within a hot-spot of an image. */
21923 static int
21924 on_hot_spot_p (hot_spot, x, y)
21925 Lisp_Object hot_spot;
21926 int x, y;
21928 if (!CONSP (hot_spot))
21929 return 0;
21931 if (EQ (XCAR (hot_spot), Qrect))
21933 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21934 Lisp_Object rect = XCDR (hot_spot);
21935 Lisp_Object tem;
21936 if (!CONSP (rect))
21937 return 0;
21938 if (!CONSP (XCAR (rect)))
21939 return 0;
21940 if (!CONSP (XCDR (rect)))
21941 return 0;
21942 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21943 return 0;
21944 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21945 return 0;
21946 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21947 return 0;
21948 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21949 return 0;
21950 return 1;
21952 else if (EQ (XCAR (hot_spot), Qcircle))
21954 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21955 Lisp_Object circ = XCDR (hot_spot);
21956 Lisp_Object lr, lx0, ly0;
21957 if (CONSP (circ)
21958 && CONSP (XCAR (circ))
21959 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21960 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21961 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21963 double r = XFLOATINT (lr);
21964 double dx = XINT (lx0) - x;
21965 double dy = XINT (ly0) - y;
21966 return (dx * dx + dy * dy <= r * r);
21969 else if (EQ (XCAR (hot_spot), Qpoly))
21971 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21972 if (VECTORP (XCDR (hot_spot)))
21974 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21975 Lisp_Object *poly = v->contents;
21976 int n = v->size;
21977 int i;
21978 int inside = 0;
21979 Lisp_Object lx, ly;
21980 int x0, y0;
21982 /* Need an even number of coordinates, and at least 3 edges. */
21983 if (n < 6 || n & 1)
21984 return 0;
21986 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21987 If count is odd, we are inside polygon. Pixels on edges
21988 may or may not be included depending on actual geometry of the
21989 polygon. */
21990 if ((lx = poly[n-2], !INTEGERP (lx))
21991 || (ly = poly[n-1], !INTEGERP (lx)))
21992 return 0;
21993 x0 = XINT (lx), y0 = XINT (ly);
21994 for (i = 0; i < n; i += 2)
21996 int x1 = x0, y1 = y0;
21997 if ((lx = poly[i], !INTEGERP (lx))
21998 || (ly = poly[i+1], !INTEGERP (ly)))
21999 return 0;
22000 x0 = XINT (lx), y0 = XINT (ly);
22002 /* Does this segment cross the X line? */
22003 if (x0 >= x)
22005 if (x1 >= x)
22006 continue;
22008 else if (x1 < x)
22009 continue;
22010 if (y > y0 && y > y1)
22011 continue;
22012 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22013 inside = !inside;
22015 return inside;
22018 return 0;
22021 Lisp_Object
22022 find_hot_spot (map, x, y)
22023 Lisp_Object map;
22024 int x, y;
22026 while (CONSP (map))
22028 if (CONSP (XCAR (map))
22029 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22030 return XCAR (map);
22031 map = XCDR (map);
22034 return Qnil;
22037 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22038 3, 3, 0,
22039 doc: /* Lookup in image map MAP coordinates X and Y.
22040 An image map is an alist where each element has the format (AREA ID PLIST).
22041 An AREA is specified as either a rectangle, a circle, or a polygon:
22042 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22043 pixel coordinates of the upper left and bottom right corners.
22044 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22045 and the radius of the circle; r may be a float or integer.
22046 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22047 vector describes one corner in the polygon.
22048 Returns the alist element for the first matching AREA in MAP. */)
22049 (map, x, y)
22050 Lisp_Object map;
22051 Lisp_Object x, y;
22053 if (NILP (map))
22054 return Qnil;
22056 CHECK_NUMBER (x);
22057 CHECK_NUMBER (y);
22059 return find_hot_spot (map, XINT (x), XINT (y));
22063 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22064 static void
22065 define_frame_cursor1 (f, cursor, pointer)
22066 struct frame *f;
22067 Cursor cursor;
22068 Lisp_Object pointer;
22070 /* Do not change cursor shape while dragging mouse. */
22071 if (!NILP (do_mouse_tracking))
22072 return;
22074 if (!NILP (pointer))
22076 if (EQ (pointer, Qarrow))
22077 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22078 else if (EQ (pointer, Qhand))
22079 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22080 else if (EQ (pointer, Qtext))
22081 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22082 else if (EQ (pointer, intern ("hdrag")))
22083 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22084 #ifdef HAVE_X_WINDOWS
22085 else if (EQ (pointer, intern ("vdrag")))
22086 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22087 #endif
22088 else if (EQ (pointer, intern ("hourglass")))
22089 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22090 else if (EQ (pointer, Qmodeline))
22091 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22092 else
22093 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22096 if (cursor != No_Cursor)
22097 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22100 /* Take proper action when mouse has moved to the mode or header line
22101 or marginal area AREA of window W, x-position X and y-position Y.
22102 X is relative to the start of the text display area of W, so the
22103 width of bitmap areas and scroll bars must be subtracted to get a
22104 position relative to the start of the mode line. */
22106 static void
22107 note_mode_line_or_margin_highlight (window, x, y, area)
22108 Lisp_Object window;
22109 int x, y;
22110 enum window_part area;
22112 struct window *w = XWINDOW (window);
22113 struct frame *f = XFRAME (w->frame);
22114 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22115 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22116 Lisp_Object pointer = Qnil;
22117 int charpos, dx, dy, width, height;
22118 Lisp_Object string, object = Qnil;
22119 Lisp_Object pos, help;
22121 Lisp_Object mouse_face;
22122 int original_x_pixel = x;
22123 struct glyph * glyph = NULL;
22124 struct glyph_row *row;
22126 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22128 int x0;
22129 struct glyph *end;
22131 string = mode_line_string (w, area, &x, &y, &charpos,
22132 &object, &dx, &dy, &width, &height);
22134 row = (area == ON_MODE_LINE
22135 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22136 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22138 /* Find glyph */
22139 if (row->mode_line_p && row->enabled_p)
22141 glyph = row->glyphs[TEXT_AREA];
22142 end = glyph + row->used[TEXT_AREA];
22144 for (x0 = original_x_pixel;
22145 glyph < end && x0 >= glyph->pixel_width;
22146 ++glyph)
22147 x0 -= glyph->pixel_width;
22149 if (glyph >= end)
22150 glyph = NULL;
22153 else
22155 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22156 string = marginal_area_string (w, area, &x, &y, &charpos,
22157 &object, &dx, &dy, &width, &height);
22160 help = Qnil;
22162 if (IMAGEP (object))
22164 Lisp_Object image_map, hotspot;
22165 if ((image_map = Fplist_get (XCDR (object), QCmap),
22166 !NILP (image_map))
22167 && (hotspot = find_hot_spot (image_map, dx, dy),
22168 CONSP (hotspot))
22169 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22171 Lisp_Object area_id, plist;
22173 area_id = XCAR (hotspot);
22174 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22175 If so, we could look for mouse-enter, mouse-leave
22176 properties in PLIST (and do something...). */
22177 hotspot = XCDR (hotspot);
22178 if (CONSP (hotspot)
22179 && (plist = XCAR (hotspot), CONSP (plist)))
22181 pointer = Fplist_get (plist, Qpointer);
22182 if (NILP (pointer))
22183 pointer = Qhand;
22184 help = Fplist_get (plist, Qhelp_echo);
22185 if (!NILP (help))
22187 help_echo_string = help;
22188 /* Is this correct? ++kfs */
22189 XSETWINDOW (help_echo_window, w);
22190 help_echo_object = w->buffer;
22191 help_echo_pos = charpos;
22195 if (NILP (pointer))
22196 pointer = Fplist_get (XCDR (object), QCpointer);
22199 if (STRINGP (string))
22201 pos = make_number (charpos);
22202 /* If we're on a string with `help-echo' text property, arrange
22203 for the help to be displayed. This is done by setting the
22204 global variable help_echo_string to the help string. */
22205 if (NILP (help))
22207 help = Fget_text_property (pos, Qhelp_echo, string);
22208 if (!NILP (help))
22210 help_echo_string = help;
22211 XSETWINDOW (help_echo_window, w);
22212 help_echo_object = string;
22213 help_echo_pos = charpos;
22217 if (NILP (pointer))
22218 pointer = Fget_text_property (pos, Qpointer, string);
22220 /* Change the mouse pointer according to what is under X/Y. */
22221 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22223 Lisp_Object map;
22224 map = Fget_text_property (pos, Qlocal_map, string);
22225 if (!KEYMAPP (map))
22226 map = Fget_text_property (pos, Qkeymap, string);
22227 if (!KEYMAPP (map))
22228 cursor = dpyinfo->vertical_scroll_bar_cursor;
22231 /* Change the mouse face according to what is under X/Y. */
22232 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22233 if (!NILP (mouse_face)
22234 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22235 && glyph)
22237 Lisp_Object b, e;
22239 struct glyph * tmp_glyph;
22241 int gpos;
22242 int gseq_length;
22243 int total_pixel_width;
22244 int ignore;
22246 int vpos, hpos;
22248 b = Fprevious_single_property_change (make_number (charpos + 1),
22249 Qmouse_face, string, Qnil);
22250 if (NILP (b))
22251 b = make_number (0);
22253 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22254 if (NILP (e))
22255 e = make_number (SCHARS (string));
22257 /* Calculate the position(glyph position: GPOS) of GLYPH in
22258 displayed string. GPOS is different from CHARPOS.
22260 CHARPOS is the position of glyph in internal string
22261 object. A mode line string format has structures which
22262 is converted to a flatten by emacs lisp interpreter.
22263 The internal string is an element of the structures.
22264 The displayed string is the flatten string. */
22265 for (tmp_glyph = glyph - 1, gpos = 0;
22266 tmp_glyph->charpos >= XINT (b);
22267 tmp_glyph--, gpos++)
22269 if (!EQ (tmp_glyph->object, glyph->object))
22270 break;
22273 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22274 displayed string holding GLYPH.
22276 GSEQ_LENGTH is different from SCHARS (STRING).
22277 SCHARS (STRING) returns the length of the internal string. */
22278 for (tmp_glyph = glyph, gseq_length = gpos;
22279 tmp_glyph->charpos < XINT (e);
22280 tmp_glyph++, gseq_length++)
22282 if (!EQ (tmp_glyph->object, glyph->object))
22283 break;
22286 total_pixel_width = 0;
22287 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22288 total_pixel_width += tmp_glyph->pixel_width;
22290 /* Pre calculation of re-rendering position */
22291 vpos = (x - gpos);
22292 hpos = (area == ON_MODE_LINE
22293 ? (w->current_matrix)->nrows - 1
22294 : 0);
22296 /* If the re-rendering position is included in the last
22297 re-rendering area, we should do nothing. */
22298 if ( EQ (window, dpyinfo->mouse_face_window)
22299 && dpyinfo->mouse_face_beg_col <= vpos
22300 && vpos < dpyinfo->mouse_face_end_col
22301 && dpyinfo->mouse_face_beg_row == hpos )
22302 return;
22304 if (clear_mouse_face (dpyinfo))
22305 cursor = No_Cursor;
22307 dpyinfo->mouse_face_beg_col = vpos;
22308 dpyinfo->mouse_face_beg_row = hpos;
22310 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22311 dpyinfo->mouse_face_beg_y = 0;
22313 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22314 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22316 dpyinfo->mouse_face_end_x = 0;
22317 dpyinfo->mouse_face_end_y = 0;
22319 dpyinfo->mouse_face_past_end = 0;
22320 dpyinfo->mouse_face_window = window;
22322 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22323 charpos,
22324 0, 0, 0, &ignore,
22325 glyph->face_id, 1);
22326 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22328 if (NILP (pointer))
22329 pointer = Qhand;
22331 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22332 clear_mouse_face (dpyinfo);
22334 define_frame_cursor1 (f, cursor, pointer);
22338 /* EXPORT:
22339 Take proper action when the mouse has moved to position X, Y on
22340 frame F as regards highlighting characters that have mouse-face
22341 properties. Also de-highlighting chars where the mouse was before.
22342 X and Y can be negative or out of range. */
22344 void
22345 note_mouse_highlight (f, x, y)
22346 struct frame *f;
22347 int x, y;
22349 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22350 enum window_part part;
22351 Lisp_Object window;
22352 struct window *w;
22353 Cursor cursor = No_Cursor;
22354 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22355 struct buffer *b;
22357 /* When a menu is active, don't highlight because this looks odd. */
22358 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22359 if (popup_activated ())
22360 return;
22361 #endif
22363 if (NILP (Vmouse_highlight)
22364 || !f->glyphs_initialized_p)
22365 return;
22367 dpyinfo->mouse_face_mouse_x = x;
22368 dpyinfo->mouse_face_mouse_y = y;
22369 dpyinfo->mouse_face_mouse_frame = f;
22371 if (dpyinfo->mouse_face_defer)
22372 return;
22374 if (gc_in_progress)
22376 dpyinfo->mouse_face_deferred_gc = 1;
22377 return;
22380 /* Which window is that in? */
22381 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22383 /* If we were displaying active text in another window, clear that.
22384 Also clear if we move out of text area in same window. */
22385 if (! EQ (window, dpyinfo->mouse_face_window)
22386 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22387 && !NILP (dpyinfo->mouse_face_window)))
22388 clear_mouse_face (dpyinfo);
22390 /* Not on a window -> return. */
22391 if (!WINDOWP (window))
22392 return;
22394 /* Reset help_echo_string. It will get recomputed below. */
22395 help_echo_string = Qnil;
22397 /* Convert to window-relative pixel coordinates. */
22398 w = XWINDOW (window);
22399 frame_to_window_pixel_xy (w, &x, &y);
22401 /* Handle tool-bar window differently since it doesn't display a
22402 buffer. */
22403 if (EQ (window, f->tool_bar_window))
22405 note_tool_bar_highlight (f, x, y);
22406 return;
22409 /* Mouse is on the mode, header line or margin? */
22410 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22411 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22413 note_mode_line_or_margin_highlight (window, x, y, part);
22414 return;
22417 if (part == ON_VERTICAL_BORDER)
22419 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22420 help_echo_string = build_string ("drag-mouse-1: resize");
22422 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22423 || part == ON_SCROLL_BAR)
22424 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22425 else
22426 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22428 /* Are we in a window whose display is up to date?
22429 And verify the buffer's text has not changed. */
22430 b = XBUFFER (w->buffer);
22431 if (part == ON_TEXT
22432 && EQ (w->window_end_valid, w->buffer)
22433 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22434 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22436 int hpos, vpos, pos, i, dx, dy, area;
22437 struct glyph *glyph;
22438 Lisp_Object object;
22439 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22440 Lisp_Object *overlay_vec = NULL;
22441 int noverlays;
22442 struct buffer *obuf;
22443 int obegv, ozv, same_region;
22445 /* Find the glyph under X/Y. */
22446 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22448 /* Look for :pointer property on image. */
22449 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22451 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22452 if (img != NULL && IMAGEP (img->spec))
22454 Lisp_Object image_map, hotspot;
22455 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22456 !NILP (image_map))
22457 && (hotspot = find_hot_spot (image_map,
22458 glyph->slice.x + dx,
22459 glyph->slice.y + dy),
22460 CONSP (hotspot))
22461 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22463 Lisp_Object area_id, plist;
22465 area_id = XCAR (hotspot);
22466 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22467 If so, we could look for mouse-enter, mouse-leave
22468 properties in PLIST (and do something...). */
22469 hotspot = XCDR (hotspot);
22470 if (CONSP (hotspot)
22471 && (plist = XCAR (hotspot), CONSP (plist)))
22473 pointer = Fplist_get (plist, Qpointer);
22474 if (NILP (pointer))
22475 pointer = Qhand;
22476 help_echo_string = Fplist_get (plist, Qhelp_echo);
22477 if (!NILP (help_echo_string))
22479 help_echo_window = window;
22480 help_echo_object = glyph->object;
22481 help_echo_pos = glyph->charpos;
22485 if (NILP (pointer))
22486 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22490 /* Clear mouse face if X/Y not over text. */
22491 if (glyph == NULL
22492 || area != TEXT_AREA
22493 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22495 if (clear_mouse_face (dpyinfo))
22496 cursor = No_Cursor;
22497 if (NILP (pointer))
22499 if (area != TEXT_AREA)
22500 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22501 else
22502 pointer = Vvoid_text_area_pointer;
22504 goto set_cursor;
22507 pos = glyph->charpos;
22508 object = glyph->object;
22509 if (!STRINGP (object) && !BUFFERP (object))
22510 goto set_cursor;
22512 /* If we get an out-of-range value, return now; avoid an error. */
22513 if (BUFFERP (object) && pos > BUF_Z (b))
22514 goto set_cursor;
22516 /* Make the window's buffer temporarily current for
22517 overlays_at and compute_char_face. */
22518 obuf = current_buffer;
22519 current_buffer = b;
22520 obegv = BEGV;
22521 ozv = ZV;
22522 BEGV = BEG;
22523 ZV = Z;
22525 /* Is this char mouse-active or does it have help-echo? */
22526 position = make_number (pos);
22528 if (BUFFERP (object))
22530 /* Put all the overlays we want in a vector in overlay_vec. */
22531 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22532 /* Sort overlays into increasing priority order. */
22533 noverlays = sort_overlays (overlay_vec, noverlays, w);
22535 else
22536 noverlays = 0;
22538 same_region = (EQ (window, dpyinfo->mouse_face_window)
22539 && vpos >= dpyinfo->mouse_face_beg_row
22540 && vpos <= dpyinfo->mouse_face_end_row
22541 && (vpos > dpyinfo->mouse_face_beg_row
22542 || hpos >= dpyinfo->mouse_face_beg_col)
22543 && (vpos < dpyinfo->mouse_face_end_row
22544 || hpos < dpyinfo->mouse_face_end_col
22545 || dpyinfo->mouse_face_past_end));
22547 if (same_region)
22548 cursor = No_Cursor;
22550 /* Check mouse-face highlighting. */
22551 if (! same_region
22552 /* If there exists an overlay with mouse-face overlapping
22553 the one we are currently highlighting, we have to
22554 check if we enter the overlapping overlay, and then
22555 highlight only that. */
22556 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22557 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22559 /* Find the highest priority overlay that has a mouse-face
22560 property. */
22561 overlay = Qnil;
22562 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22564 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22565 if (!NILP (mouse_face))
22566 overlay = overlay_vec[i];
22569 /* If we're actually highlighting the same overlay as
22570 before, there's no need to do that again. */
22571 if (!NILP (overlay)
22572 && EQ (overlay, dpyinfo->mouse_face_overlay))
22573 goto check_help_echo;
22575 dpyinfo->mouse_face_overlay = overlay;
22577 /* Clear the display of the old active region, if any. */
22578 if (clear_mouse_face (dpyinfo))
22579 cursor = No_Cursor;
22581 /* If no overlay applies, get a text property. */
22582 if (NILP (overlay))
22583 mouse_face = Fget_text_property (position, Qmouse_face, object);
22585 /* Handle the overlay case. */
22586 if (!NILP (overlay))
22588 /* Find the range of text around this char that
22589 should be active. */
22590 Lisp_Object before, after;
22591 int ignore;
22593 before = Foverlay_start (overlay);
22594 after = Foverlay_end (overlay);
22595 /* Record this as the current active region. */
22596 fast_find_position (w, XFASTINT (before),
22597 &dpyinfo->mouse_face_beg_col,
22598 &dpyinfo->mouse_face_beg_row,
22599 &dpyinfo->mouse_face_beg_x,
22600 &dpyinfo->mouse_face_beg_y, Qnil);
22602 dpyinfo->mouse_face_past_end
22603 = !fast_find_position (w, XFASTINT (after),
22604 &dpyinfo->mouse_face_end_col,
22605 &dpyinfo->mouse_face_end_row,
22606 &dpyinfo->mouse_face_end_x,
22607 &dpyinfo->mouse_face_end_y, Qnil);
22608 dpyinfo->mouse_face_window = window;
22610 dpyinfo->mouse_face_face_id
22611 = face_at_buffer_position (w, pos, 0, 0,
22612 &ignore, pos + 1,
22613 !dpyinfo->mouse_face_hidden);
22615 /* Display it as active. */
22616 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22617 cursor = No_Cursor;
22619 /* Handle the text property case. */
22620 else if (!NILP (mouse_face) && BUFFERP (object))
22622 /* Find the range of text around this char that
22623 should be active. */
22624 Lisp_Object before, after, beginning, end;
22625 int ignore;
22627 beginning = Fmarker_position (w->start);
22628 end = make_number (BUF_Z (XBUFFER (object))
22629 - XFASTINT (w->window_end_pos));
22630 before
22631 = Fprevious_single_property_change (make_number (pos + 1),
22632 Qmouse_face,
22633 object, beginning);
22634 after
22635 = Fnext_single_property_change (position, Qmouse_face,
22636 object, end);
22638 /* Record this as the current active region. */
22639 fast_find_position (w, XFASTINT (before),
22640 &dpyinfo->mouse_face_beg_col,
22641 &dpyinfo->mouse_face_beg_row,
22642 &dpyinfo->mouse_face_beg_x,
22643 &dpyinfo->mouse_face_beg_y, Qnil);
22644 dpyinfo->mouse_face_past_end
22645 = !fast_find_position (w, XFASTINT (after),
22646 &dpyinfo->mouse_face_end_col,
22647 &dpyinfo->mouse_face_end_row,
22648 &dpyinfo->mouse_face_end_x,
22649 &dpyinfo->mouse_face_end_y, Qnil);
22650 dpyinfo->mouse_face_window = window;
22652 if (BUFFERP (object))
22653 dpyinfo->mouse_face_face_id
22654 = face_at_buffer_position (w, pos, 0, 0,
22655 &ignore, pos + 1,
22656 !dpyinfo->mouse_face_hidden);
22658 /* Display it as active. */
22659 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22660 cursor = No_Cursor;
22662 else if (!NILP (mouse_face) && STRINGP (object))
22664 Lisp_Object b, e;
22665 int ignore;
22667 b = Fprevious_single_property_change (make_number (pos + 1),
22668 Qmouse_face,
22669 object, Qnil);
22670 e = Fnext_single_property_change (position, Qmouse_face,
22671 object, Qnil);
22672 if (NILP (b))
22673 b = make_number (0);
22674 if (NILP (e))
22675 e = make_number (SCHARS (object) - 1);
22677 fast_find_string_pos (w, XINT (b), object,
22678 &dpyinfo->mouse_face_beg_col,
22679 &dpyinfo->mouse_face_beg_row,
22680 &dpyinfo->mouse_face_beg_x,
22681 &dpyinfo->mouse_face_beg_y, 0);
22682 fast_find_string_pos (w, XINT (e), object,
22683 &dpyinfo->mouse_face_end_col,
22684 &dpyinfo->mouse_face_end_row,
22685 &dpyinfo->mouse_face_end_x,
22686 &dpyinfo->mouse_face_end_y, 1);
22687 dpyinfo->mouse_face_past_end = 0;
22688 dpyinfo->mouse_face_window = window;
22689 dpyinfo->mouse_face_face_id
22690 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22691 glyph->face_id, 1);
22692 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22693 cursor = No_Cursor;
22695 else if (STRINGP (object) && NILP (mouse_face))
22697 /* A string which doesn't have mouse-face, but
22698 the text ``under'' it might have. */
22699 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22700 int start = MATRIX_ROW_START_CHARPOS (r);
22702 pos = string_buffer_position (w, object, start);
22703 if (pos > 0)
22704 mouse_face = get_char_property_and_overlay (make_number (pos),
22705 Qmouse_face,
22706 w->buffer,
22707 &overlay);
22708 if (!NILP (mouse_face) && !NILP (overlay))
22710 Lisp_Object before = Foverlay_start (overlay);
22711 Lisp_Object after = Foverlay_end (overlay);
22712 int ignore;
22714 /* Note that we might not be able to find position
22715 BEFORE in the glyph matrix if the overlay is
22716 entirely covered by a `display' property. In
22717 this case, we overshoot. So let's stop in
22718 the glyph matrix before glyphs for OBJECT. */
22719 fast_find_position (w, XFASTINT (before),
22720 &dpyinfo->mouse_face_beg_col,
22721 &dpyinfo->mouse_face_beg_row,
22722 &dpyinfo->mouse_face_beg_x,
22723 &dpyinfo->mouse_face_beg_y,
22724 object);
22726 dpyinfo->mouse_face_past_end
22727 = !fast_find_position (w, XFASTINT (after),
22728 &dpyinfo->mouse_face_end_col,
22729 &dpyinfo->mouse_face_end_row,
22730 &dpyinfo->mouse_face_end_x,
22731 &dpyinfo->mouse_face_end_y,
22732 Qnil);
22733 dpyinfo->mouse_face_window = window;
22734 dpyinfo->mouse_face_face_id
22735 = face_at_buffer_position (w, pos, 0, 0,
22736 &ignore, pos + 1,
22737 !dpyinfo->mouse_face_hidden);
22739 /* Display it as active. */
22740 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22741 cursor = No_Cursor;
22746 check_help_echo:
22748 /* Look for a `help-echo' property. */
22749 if (NILP (help_echo_string)) {
22750 Lisp_Object help, overlay;
22752 /* Check overlays first. */
22753 help = overlay = Qnil;
22754 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22756 overlay = overlay_vec[i];
22757 help = Foverlay_get (overlay, Qhelp_echo);
22760 if (!NILP (help))
22762 help_echo_string = help;
22763 help_echo_window = window;
22764 help_echo_object = overlay;
22765 help_echo_pos = pos;
22767 else
22769 Lisp_Object object = glyph->object;
22770 int charpos = glyph->charpos;
22772 /* Try text properties. */
22773 if (STRINGP (object)
22774 && charpos >= 0
22775 && charpos < SCHARS (object))
22777 help = Fget_text_property (make_number (charpos),
22778 Qhelp_echo, object);
22779 if (NILP (help))
22781 /* If the string itself doesn't specify a help-echo,
22782 see if the buffer text ``under'' it does. */
22783 struct glyph_row *r
22784 = MATRIX_ROW (w->current_matrix, vpos);
22785 int start = MATRIX_ROW_START_CHARPOS (r);
22786 int pos = string_buffer_position (w, object, start);
22787 if (pos > 0)
22789 help = Fget_char_property (make_number (pos),
22790 Qhelp_echo, w->buffer);
22791 if (!NILP (help))
22793 charpos = pos;
22794 object = w->buffer;
22799 else if (BUFFERP (object)
22800 && charpos >= BEGV
22801 && charpos < ZV)
22802 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22803 object);
22805 if (!NILP (help))
22807 help_echo_string = help;
22808 help_echo_window = window;
22809 help_echo_object = object;
22810 help_echo_pos = charpos;
22815 /* Look for a `pointer' property. */
22816 if (NILP (pointer))
22818 /* Check overlays first. */
22819 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22820 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22822 if (NILP (pointer))
22824 Lisp_Object object = glyph->object;
22825 int charpos = glyph->charpos;
22827 /* Try text properties. */
22828 if (STRINGP (object)
22829 && charpos >= 0
22830 && charpos < SCHARS (object))
22832 pointer = Fget_text_property (make_number (charpos),
22833 Qpointer, object);
22834 if (NILP (pointer))
22836 /* If the string itself doesn't specify a pointer,
22837 see if the buffer text ``under'' it does. */
22838 struct glyph_row *r
22839 = MATRIX_ROW (w->current_matrix, vpos);
22840 int start = MATRIX_ROW_START_CHARPOS (r);
22841 int pos = string_buffer_position (w, object, start);
22842 if (pos > 0)
22843 pointer = Fget_char_property (make_number (pos),
22844 Qpointer, w->buffer);
22847 else if (BUFFERP (object)
22848 && charpos >= BEGV
22849 && charpos < ZV)
22850 pointer = Fget_text_property (make_number (charpos),
22851 Qpointer, object);
22855 BEGV = obegv;
22856 ZV = ozv;
22857 current_buffer = obuf;
22860 set_cursor:
22862 define_frame_cursor1 (f, cursor, pointer);
22866 /* EXPORT for RIF:
22867 Clear any mouse-face on window W. This function is part of the
22868 redisplay interface, and is called from try_window_id and similar
22869 functions to ensure the mouse-highlight is off. */
22871 void
22872 x_clear_window_mouse_face (w)
22873 struct window *w;
22875 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22876 Lisp_Object window;
22878 BLOCK_INPUT;
22879 XSETWINDOW (window, w);
22880 if (EQ (window, dpyinfo->mouse_face_window))
22881 clear_mouse_face (dpyinfo);
22882 UNBLOCK_INPUT;
22886 /* EXPORT:
22887 Just discard the mouse face information for frame F, if any.
22888 This is used when the size of F is changed. */
22890 void
22891 cancel_mouse_face (f)
22892 struct frame *f;
22894 Lisp_Object window;
22895 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22897 window = dpyinfo->mouse_face_window;
22898 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22900 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22901 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22902 dpyinfo->mouse_face_window = Qnil;
22907 #endif /* HAVE_WINDOW_SYSTEM */
22910 /***********************************************************************
22911 Exposure Events
22912 ***********************************************************************/
22914 #ifdef HAVE_WINDOW_SYSTEM
22916 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22917 which intersects rectangle R. R is in window-relative coordinates. */
22919 static void
22920 expose_area (w, row, r, area)
22921 struct window *w;
22922 struct glyph_row *row;
22923 XRectangle *r;
22924 enum glyph_row_area area;
22926 struct glyph *first = row->glyphs[area];
22927 struct glyph *end = row->glyphs[area] + row->used[area];
22928 struct glyph *last;
22929 int first_x, start_x, x;
22931 if (area == TEXT_AREA && row->fill_line_p)
22932 /* If row extends face to end of line write the whole line. */
22933 draw_glyphs (w, 0, row, area,
22934 0, row->used[area],
22935 DRAW_NORMAL_TEXT, 0);
22936 else
22938 /* Set START_X to the window-relative start position for drawing glyphs of
22939 AREA. The first glyph of the text area can be partially visible.
22940 The first glyphs of other areas cannot. */
22941 start_x = window_box_left_offset (w, area);
22942 x = start_x;
22943 if (area == TEXT_AREA)
22944 x += row->x;
22946 /* Find the first glyph that must be redrawn. */
22947 while (first < end
22948 && x + first->pixel_width < r->x)
22950 x += first->pixel_width;
22951 ++first;
22954 /* Find the last one. */
22955 last = first;
22956 first_x = x;
22957 while (last < end
22958 && x < r->x + r->width)
22960 x += last->pixel_width;
22961 ++last;
22964 /* Repaint. */
22965 if (last > first)
22966 draw_glyphs (w, first_x - start_x, row, area,
22967 first - row->glyphs[area], last - row->glyphs[area],
22968 DRAW_NORMAL_TEXT, 0);
22973 /* Redraw the parts of the glyph row ROW on window W intersecting
22974 rectangle R. R is in window-relative coordinates. Value is
22975 non-zero if mouse-face was overwritten. */
22977 static int
22978 expose_line (w, row, r)
22979 struct window *w;
22980 struct glyph_row *row;
22981 XRectangle *r;
22983 xassert (row->enabled_p);
22985 if (row->mode_line_p || w->pseudo_window_p)
22986 draw_glyphs (w, 0, row, TEXT_AREA,
22987 0, row->used[TEXT_AREA],
22988 DRAW_NORMAL_TEXT, 0);
22989 else
22991 if (row->used[LEFT_MARGIN_AREA])
22992 expose_area (w, row, r, LEFT_MARGIN_AREA);
22993 if (row->used[TEXT_AREA])
22994 expose_area (w, row, r, TEXT_AREA);
22995 if (row->used[RIGHT_MARGIN_AREA])
22996 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22997 draw_row_fringe_bitmaps (w, row);
23000 return row->mouse_face_p;
23004 /* Redraw those parts of glyphs rows during expose event handling that
23005 overlap other rows. Redrawing of an exposed line writes over parts
23006 of lines overlapping that exposed line; this function fixes that.
23008 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23009 row in W's current matrix that is exposed and overlaps other rows.
23010 LAST_OVERLAPPING_ROW is the last such row. */
23012 static void
23013 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23014 struct window *w;
23015 struct glyph_row *first_overlapping_row;
23016 struct glyph_row *last_overlapping_row;
23018 struct glyph_row *row;
23020 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23021 if (row->overlapping_p)
23023 xassert (row->enabled_p && !row->mode_line_p);
23025 if (row->used[LEFT_MARGIN_AREA])
23026 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23028 if (row->used[TEXT_AREA])
23029 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23031 if (row->used[RIGHT_MARGIN_AREA])
23032 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23037 /* Return non-zero if W's cursor intersects rectangle R. */
23039 static int
23040 phys_cursor_in_rect_p (w, r)
23041 struct window *w;
23042 XRectangle *r;
23044 XRectangle cr, result;
23045 struct glyph *cursor_glyph;
23047 cursor_glyph = get_phys_cursor_glyph (w);
23048 if (cursor_glyph)
23050 /* r is relative to W's box, but w->phys_cursor.x is relative
23051 to left edge of W's TEXT area. Adjust it. */
23052 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23053 cr.y = w->phys_cursor.y;
23054 cr.width = cursor_glyph->pixel_width;
23055 cr.height = w->phys_cursor_height;
23056 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23057 I assume the effect is the same -- and this is portable. */
23058 return x_intersect_rectangles (&cr, r, &result);
23060 /* If we don't understand the format, pretend we're not in the hot-spot. */
23061 return 0;
23065 /* EXPORT:
23066 Draw a vertical window border to the right of window W if W doesn't
23067 have vertical scroll bars. */
23069 void
23070 x_draw_vertical_border (w)
23071 struct window *w;
23073 struct frame *f = XFRAME (WINDOW_FRAME (w));
23075 /* We could do better, if we knew what type of scroll-bar the adjacent
23076 windows (on either side) have... But we don't :-(
23077 However, I think this works ok. ++KFS 2003-04-25 */
23079 /* Redraw borders between horizontally adjacent windows. Don't
23080 do it for frames with vertical scroll bars because either the
23081 right scroll bar of a window, or the left scroll bar of its
23082 neighbor will suffice as a border. */
23083 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23084 return;
23086 if (!WINDOW_RIGHTMOST_P (w)
23087 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23089 int x0, x1, y0, y1;
23091 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23092 y1 -= 1;
23094 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23095 x1 -= 1;
23097 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23099 else if (!WINDOW_LEFTMOST_P (w)
23100 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23102 int x0, x1, y0, y1;
23104 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23105 y1 -= 1;
23107 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23108 x0 -= 1;
23110 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23115 /* Redraw the part of window W intersection rectangle FR. Pixel
23116 coordinates in FR are frame-relative. Call this function with
23117 input blocked. Value is non-zero if the exposure overwrites
23118 mouse-face. */
23120 static int
23121 expose_window (w, fr)
23122 struct window *w;
23123 XRectangle *fr;
23125 struct frame *f = XFRAME (w->frame);
23126 XRectangle wr, r;
23127 int mouse_face_overwritten_p = 0;
23129 /* If window is not yet fully initialized, do nothing. This can
23130 happen when toolkit scroll bars are used and a window is split.
23131 Reconfiguring the scroll bar will generate an expose for a newly
23132 created window. */
23133 if (w->current_matrix == NULL)
23134 return 0;
23136 /* When we're currently updating the window, display and current
23137 matrix usually don't agree. Arrange for a thorough display
23138 later. */
23139 if (w == updated_window)
23141 SET_FRAME_GARBAGED (f);
23142 return 0;
23145 /* Frame-relative pixel rectangle of W. */
23146 wr.x = WINDOW_LEFT_EDGE_X (w);
23147 wr.y = WINDOW_TOP_EDGE_Y (w);
23148 wr.width = WINDOW_TOTAL_WIDTH (w);
23149 wr.height = WINDOW_TOTAL_HEIGHT (w);
23151 if (x_intersect_rectangles (fr, &wr, &r))
23153 int yb = window_text_bottom_y (w);
23154 struct glyph_row *row;
23155 int cursor_cleared_p;
23156 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23158 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23159 r.x, r.y, r.width, r.height));
23161 /* Convert to window coordinates. */
23162 r.x -= WINDOW_LEFT_EDGE_X (w);
23163 r.y -= WINDOW_TOP_EDGE_Y (w);
23165 /* Turn off the cursor. */
23166 if (!w->pseudo_window_p
23167 && phys_cursor_in_rect_p (w, &r))
23169 x_clear_cursor (w);
23170 cursor_cleared_p = 1;
23172 else
23173 cursor_cleared_p = 0;
23175 /* Update lines intersecting rectangle R. */
23176 first_overlapping_row = last_overlapping_row = NULL;
23177 for (row = w->current_matrix->rows;
23178 row->enabled_p;
23179 ++row)
23181 int y0 = row->y;
23182 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23184 if ((y0 >= r.y && y0 < r.y + r.height)
23185 || (y1 > r.y && y1 < r.y + r.height)
23186 || (r.y >= y0 && r.y < y1)
23187 || (r.y + r.height > y0 && r.y + r.height < y1))
23189 /* A header line may be overlapping, but there is no need
23190 to fix overlapping areas for them. KFS 2005-02-12 */
23191 if (row->overlapping_p && !row->mode_line_p)
23193 if (first_overlapping_row == NULL)
23194 first_overlapping_row = row;
23195 last_overlapping_row = row;
23198 if (expose_line (w, row, &r))
23199 mouse_face_overwritten_p = 1;
23202 if (y1 >= yb)
23203 break;
23206 /* Display the mode line if there is one. */
23207 if (WINDOW_WANTS_MODELINE_P (w)
23208 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23209 row->enabled_p)
23210 && row->y < r.y + r.height)
23212 if (expose_line (w, row, &r))
23213 mouse_face_overwritten_p = 1;
23216 if (!w->pseudo_window_p)
23218 /* Fix the display of overlapping rows. */
23219 if (first_overlapping_row)
23220 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23222 /* Draw border between windows. */
23223 x_draw_vertical_border (w);
23225 /* Turn the cursor on again. */
23226 if (cursor_cleared_p)
23227 update_window_cursor (w, 1);
23231 return mouse_face_overwritten_p;
23236 /* Redraw (parts) of all windows in the window tree rooted at W that
23237 intersect R. R contains frame pixel coordinates. Value is
23238 non-zero if the exposure overwrites mouse-face. */
23240 static int
23241 expose_window_tree (w, r)
23242 struct window *w;
23243 XRectangle *r;
23245 struct frame *f = XFRAME (w->frame);
23246 int mouse_face_overwritten_p = 0;
23248 while (w && !FRAME_GARBAGED_P (f))
23250 if (!NILP (w->hchild))
23251 mouse_face_overwritten_p
23252 |= expose_window_tree (XWINDOW (w->hchild), r);
23253 else if (!NILP (w->vchild))
23254 mouse_face_overwritten_p
23255 |= expose_window_tree (XWINDOW (w->vchild), r);
23256 else
23257 mouse_face_overwritten_p |= expose_window (w, r);
23259 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23262 return mouse_face_overwritten_p;
23266 /* EXPORT:
23267 Redisplay an exposed area of frame F. X and Y are the upper-left
23268 corner of the exposed rectangle. W and H are width and height of
23269 the exposed area. All are pixel values. W or H zero means redraw
23270 the entire frame. */
23272 void
23273 expose_frame (f, x, y, w, h)
23274 struct frame *f;
23275 int x, y, w, h;
23277 XRectangle r;
23278 int mouse_face_overwritten_p = 0;
23280 TRACE ((stderr, "expose_frame "));
23282 /* No need to redraw if frame will be redrawn soon. */
23283 if (FRAME_GARBAGED_P (f))
23285 TRACE ((stderr, " garbaged\n"));
23286 return;
23289 /* If basic faces haven't been realized yet, there is no point in
23290 trying to redraw anything. This can happen when we get an expose
23291 event while Emacs is starting, e.g. by moving another window. */
23292 if (FRAME_FACE_CACHE (f) == NULL
23293 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23295 TRACE ((stderr, " no faces\n"));
23296 return;
23299 if (w == 0 || h == 0)
23301 r.x = r.y = 0;
23302 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23303 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23305 else
23307 r.x = x;
23308 r.y = y;
23309 r.width = w;
23310 r.height = h;
23313 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23314 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23316 if (WINDOWP (f->tool_bar_window))
23317 mouse_face_overwritten_p
23318 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23320 #ifdef HAVE_X_WINDOWS
23321 #ifndef MSDOS
23322 #ifndef USE_X_TOOLKIT
23323 if (WINDOWP (f->menu_bar_window))
23324 mouse_face_overwritten_p
23325 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23326 #endif /* not USE_X_TOOLKIT */
23327 #endif
23328 #endif
23330 /* Some window managers support a focus-follows-mouse style with
23331 delayed raising of frames. Imagine a partially obscured frame,
23332 and moving the mouse into partially obscured mouse-face on that
23333 frame. The visible part of the mouse-face will be highlighted,
23334 then the WM raises the obscured frame. With at least one WM, KDE
23335 2.1, Emacs is not getting any event for the raising of the frame
23336 (even tried with SubstructureRedirectMask), only Expose events.
23337 These expose events will draw text normally, i.e. not
23338 highlighted. Which means we must redo the highlight here.
23339 Subsume it under ``we love X''. --gerd 2001-08-15 */
23340 /* Included in Windows version because Windows most likely does not
23341 do the right thing if any third party tool offers
23342 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23343 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23345 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23346 if (f == dpyinfo->mouse_face_mouse_frame)
23348 int x = dpyinfo->mouse_face_mouse_x;
23349 int y = dpyinfo->mouse_face_mouse_y;
23350 clear_mouse_face (dpyinfo);
23351 note_mouse_highlight (f, x, y);
23357 /* EXPORT:
23358 Determine the intersection of two rectangles R1 and R2. Return
23359 the intersection in *RESULT. Value is non-zero if RESULT is not
23360 empty. */
23363 x_intersect_rectangles (r1, r2, result)
23364 XRectangle *r1, *r2, *result;
23366 XRectangle *left, *right;
23367 XRectangle *upper, *lower;
23368 int intersection_p = 0;
23370 /* Rearrange so that R1 is the left-most rectangle. */
23371 if (r1->x < r2->x)
23372 left = r1, right = r2;
23373 else
23374 left = r2, right = r1;
23376 /* X0 of the intersection is right.x0, if this is inside R1,
23377 otherwise there is no intersection. */
23378 if (right->x <= left->x + left->width)
23380 result->x = right->x;
23382 /* The right end of the intersection is the minimum of the
23383 the right ends of left and right. */
23384 result->width = (min (left->x + left->width, right->x + right->width)
23385 - result->x);
23387 /* Same game for Y. */
23388 if (r1->y < r2->y)
23389 upper = r1, lower = r2;
23390 else
23391 upper = r2, lower = r1;
23393 /* The upper end of the intersection is lower.y0, if this is inside
23394 of upper. Otherwise, there is no intersection. */
23395 if (lower->y <= upper->y + upper->height)
23397 result->y = lower->y;
23399 /* The lower end of the intersection is the minimum of the lower
23400 ends of upper and lower. */
23401 result->height = (min (lower->y + lower->height,
23402 upper->y + upper->height)
23403 - result->y);
23404 intersection_p = 1;
23408 return intersection_p;
23411 #endif /* HAVE_WINDOW_SYSTEM */
23414 /***********************************************************************
23415 Initialization
23416 ***********************************************************************/
23418 void
23419 syms_of_xdisp ()
23421 Vwith_echo_area_save_vector = Qnil;
23422 staticpro (&Vwith_echo_area_save_vector);
23424 Vmessage_stack = Qnil;
23425 staticpro (&Vmessage_stack);
23427 Qinhibit_redisplay = intern ("inhibit-redisplay");
23428 staticpro (&Qinhibit_redisplay);
23430 message_dolog_marker1 = Fmake_marker ();
23431 staticpro (&message_dolog_marker1);
23432 message_dolog_marker2 = Fmake_marker ();
23433 staticpro (&message_dolog_marker2);
23434 message_dolog_marker3 = Fmake_marker ();
23435 staticpro (&message_dolog_marker3);
23437 #if GLYPH_DEBUG
23438 defsubr (&Sdump_frame_glyph_matrix);
23439 defsubr (&Sdump_glyph_matrix);
23440 defsubr (&Sdump_glyph_row);
23441 defsubr (&Sdump_tool_bar_row);
23442 defsubr (&Strace_redisplay);
23443 defsubr (&Strace_to_stderr);
23444 #endif
23445 #ifdef HAVE_WINDOW_SYSTEM
23446 defsubr (&Stool_bar_lines_needed);
23447 defsubr (&Slookup_image_map);
23448 #endif
23449 defsubr (&Sformat_mode_line);
23451 staticpro (&Qmenu_bar_update_hook);
23452 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23454 staticpro (&Qoverriding_terminal_local_map);
23455 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23457 staticpro (&Qoverriding_local_map);
23458 Qoverriding_local_map = intern ("overriding-local-map");
23460 staticpro (&Qwindow_scroll_functions);
23461 Qwindow_scroll_functions = intern ("window-scroll-functions");
23463 staticpro (&Qredisplay_end_trigger_functions);
23464 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23466 staticpro (&Qinhibit_point_motion_hooks);
23467 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23469 QCdata = intern (":data");
23470 staticpro (&QCdata);
23471 Qdisplay = intern ("display");
23472 staticpro (&Qdisplay);
23473 Qspace_width = intern ("space-width");
23474 staticpro (&Qspace_width);
23475 Qraise = intern ("raise");
23476 staticpro (&Qraise);
23477 Qslice = intern ("slice");
23478 staticpro (&Qslice);
23479 Qspace = intern ("space");
23480 staticpro (&Qspace);
23481 Qmargin = intern ("margin");
23482 staticpro (&Qmargin);
23483 Qpointer = intern ("pointer");
23484 staticpro (&Qpointer);
23485 Qleft_margin = intern ("left-margin");
23486 staticpro (&Qleft_margin);
23487 Qright_margin = intern ("right-margin");
23488 staticpro (&Qright_margin);
23489 Qcenter = intern ("center");
23490 staticpro (&Qcenter);
23491 Qline_height = intern ("line-height");
23492 staticpro (&Qline_height);
23493 QCalign_to = intern (":align-to");
23494 staticpro (&QCalign_to);
23495 QCrelative_width = intern (":relative-width");
23496 staticpro (&QCrelative_width);
23497 QCrelative_height = intern (":relative-height");
23498 staticpro (&QCrelative_height);
23499 QCeval = intern (":eval");
23500 staticpro (&QCeval);
23501 QCpropertize = intern (":propertize");
23502 staticpro (&QCpropertize);
23503 QCfile = intern (":file");
23504 staticpro (&QCfile);
23505 Qfontified = intern ("fontified");
23506 staticpro (&Qfontified);
23507 Qfontification_functions = intern ("fontification-functions");
23508 staticpro (&Qfontification_functions);
23509 Qtrailing_whitespace = intern ("trailing-whitespace");
23510 staticpro (&Qtrailing_whitespace);
23511 Qescape_glyph = intern ("escape-glyph");
23512 staticpro (&Qescape_glyph);
23513 Qnobreak_space = intern ("nobreak-space");
23514 staticpro (&Qnobreak_space);
23515 Qimage = intern ("image");
23516 staticpro (&Qimage);
23517 QCmap = intern (":map");
23518 staticpro (&QCmap);
23519 QCpointer = intern (":pointer");
23520 staticpro (&QCpointer);
23521 Qrect = intern ("rect");
23522 staticpro (&Qrect);
23523 Qcircle = intern ("circle");
23524 staticpro (&Qcircle);
23525 Qpoly = intern ("poly");
23526 staticpro (&Qpoly);
23527 Qmessage_truncate_lines = intern ("message-truncate-lines");
23528 staticpro (&Qmessage_truncate_lines);
23529 Qgrow_only = intern ("grow-only");
23530 staticpro (&Qgrow_only);
23531 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23532 staticpro (&Qinhibit_menubar_update);
23533 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23534 staticpro (&Qinhibit_eval_during_redisplay);
23535 Qposition = intern ("position");
23536 staticpro (&Qposition);
23537 Qbuffer_position = intern ("buffer-position");
23538 staticpro (&Qbuffer_position);
23539 Qobject = intern ("object");
23540 staticpro (&Qobject);
23541 Qbar = intern ("bar");
23542 staticpro (&Qbar);
23543 Qhbar = intern ("hbar");
23544 staticpro (&Qhbar);
23545 Qbox = intern ("box");
23546 staticpro (&Qbox);
23547 Qhollow = intern ("hollow");
23548 staticpro (&Qhollow);
23549 Qhand = intern ("hand");
23550 staticpro (&Qhand);
23551 Qarrow = intern ("arrow");
23552 staticpro (&Qarrow);
23553 Qtext = intern ("text");
23554 staticpro (&Qtext);
23555 Qrisky_local_variable = intern ("risky-local-variable");
23556 staticpro (&Qrisky_local_variable);
23557 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23558 staticpro (&Qinhibit_free_realized_faces);
23560 list_of_error = Fcons (Fcons (intern ("error"),
23561 Fcons (intern ("void-variable"), Qnil)),
23562 Qnil);
23563 staticpro (&list_of_error);
23565 Qlast_arrow_position = intern ("last-arrow-position");
23566 staticpro (&Qlast_arrow_position);
23567 Qlast_arrow_string = intern ("last-arrow-string");
23568 staticpro (&Qlast_arrow_string);
23570 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23571 staticpro (&Qoverlay_arrow_string);
23572 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23573 staticpro (&Qoverlay_arrow_bitmap);
23575 echo_buffer[0] = echo_buffer[1] = Qnil;
23576 staticpro (&echo_buffer[0]);
23577 staticpro (&echo_buffer[1]);
23579 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23580 staticpro (&echo_area_buffer[0]);
23581 staticpro (&echo_area_buffer[1]);
23583 Vmessages_buffer_name = build_string ("*Messages*");
23584 staticpro (&Vmessages_buffer_name);
23586 mode_line_proptrans_alist = Qnil;
23587 staticpro (&mode_line_proptrans_alist);
23588 mode_line_string_list = Qnil;
23589 staticpro (&mode_line_string_list);
23590 mode_line_string_face = Qnil;
23591 staticpro (&mode_line_string_face);
23592 mode_line_string_face_prop = Qnil;
23593 staticpro (&mode_line_string_face_prop);
23594 Vmode_line_unwind_vector = Qnil;
23595 staticpro (&Vmode_line_unwind_vector);
23597 help_echo_string = Qnil;
23598 staticpro (&help_echo_string);
23599 help_echo_object = Qnil;
23600 staticpro (&help_echo_object);
23601 help_echo_window = Qnil;
23602 staticpro (&help_echo_window);
23603 previous_help_echo_string = Qnil;
23604 staticpro (&previous_help_echo_string);
23605 help_echo_pos = -1;
23607 #ifdef HAVE_WINDOW_SYSTEM
23608 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23609 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23610 For example, if a block cursor is over a tab, it will be drawn as
23611 wide as that tab on the display. */);
23612 x_stretch_cursor_p = 0;
23613 #endif
23615 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23616 doc: /* *Non-nil means highlight trailing whitespace.
23617 The face used for trailing whitespace is `trailing-whitespace'. */);
23618 Vshow_trailing_whitespace = Qnil;
23620 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23621 doc: /* *Control highlighting of nobreak space and soft hyphen.
23622 A value of t means highlight the character itself (for nobreak space,
23623 use face `nobreak-space').
23624 A value of nil means no highlighting.
23625 Other values mean display the escape glyph followed by an ordinary
23626 space or ordinary hyphen. */);
23627 Vnobreak_char_display = Qt;
23629 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23630 doc: /* *The pointer shape to show in void text areas.
23631 A value of nil means to show the text pointer. Other options are `arrow',
23632 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23633 Vvoid_text_area_pointer = Qarrow;
23635 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23636 doc: /* Non-nil means don't actually do any redisplay.
23637 This is used for internal purposes. */);
23638 Vinhibit_redisplay = Qnil;
23640 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23641 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23642 Vglobal_mode_string = Qnil;
23644 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23645 doc: /* Marker for where to display an arrow on top of the buffer text.
23646 This must be the beginning of a line in order to work.
23647 See also `overlay-arrow-string'. */);
23648 Voverlay_arrow_position = Qnil;
23650 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23651 doc: /* String to display as an arrow in non-window frames.
23652 See also `overlay-arrow-position'. */);
23653 Voverlay_arrow_string = build_string ("=>");
23655 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23656 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23657 The symbols on this list are examined during redisplay to determine
23658 where to display overlay arrows. */);
23659 Voverlay_arrow_variable_list
23660 = Fcons (intern ("overlay-arrow-position"), Qnil);
23662 DEFVAR_INT ("scroll-step", &scroll_step,
23663 doc: /* *The number of lines to try scrolling a window by when point moves out.
23664 If that fails to bring point back on frame, point is centered instead.
23665 If this is zero, point is always centered after it moves off frame.
23666 If you want scrolling to always be a line at a time, you should set
23667 `scroll-conservatively' to a large value rather than set this to 1. */);
23669 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23670 doc: /* *Scroll up to this many lines, to bring point back on screen.
23671 A value of zero means to scroll the text to center point vertically
23672 in the window. */);
23673 scroll_conservatively = 0;
23675 DEFVAR_INT ("scroll-margin", &scroll_margin,
23676 doc: /* *Number of lines of margin at the top and bottom of a window.
23677 Recenter the window whenever point gets within this many lines
23678 of the top or bottom of the window. */);
23679 scroll_margin = 0;
23681 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23682 doc: /* Pixels per inch value for non-window system displays.
23683 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23684 Vdisplay_pixels_per_inch = make_float (72.0);
23686 #if GLYPH_DEBUG
23687 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23688 #endif
23690 DEFVAR_BOOL ("truncate-partial-width-windows",
23691 &truncate_partial_width_windows,
23692 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23693 truncate_partial_width_windows = 1;
23695 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23696 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23697 Any other value means to use the appropriate face, `mode-line',
23698 `header-line', or `menu' respectively. */);
23699 mode_line_inverse_video = 1;
23701 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23702 doc: /* *Maximum buffer size for which line number should be displayed.
23703 If the buffer is bigger than this, the line number does not appear
23704 in the mode line. A value of nil means no limit. */);
23705 Vline_number_display_limit = Qnil;
23707 DEFVAR_INT ("line-number-display-limit-width",
23708 &line_number_display_limit_width,
23709 doc: /* *Maximum line width (in characters) for line number display.
23710 If the average length of the lines near point is bigger than this, then the
23711 line number may be omitted from the mode line. */);
23712 line_number_display_limit_width = 200;
23714 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23715 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23716 highlight_nonselected_windows = 0;
23718 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23719 doc: /* Non-nil if more than one frame is visible on this display.
23720 Minibuffer-only frames don't count, but iconified frames do.
23721 This variable is not guaranteed to be accurate except while processing
23722 `frame-title-format' and `icon-title-format'. */);
23724 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23725 doc: /* Template for displaying the title bar of visible frames.
23726 \(Assuming the window manager supports this feature.)
23727 This variable has the same structure as `mode-line-format' (which see),
23728 and is used only on frames for which no explicit name has been set
23729 \(see `modify-frame-parameters'). */);
23731 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23732 doc: /* Template for displaying the title bar of an iconified frame.
23733 \(Assuming the window manager supports this feature.)
23734 This variable has the same structure as `mode-line-format' (which see),
23735 and is used only on frames for which no explicit name has been set
23736 \(see `modify-frame-parameters'). */);
23737 Vicon_title_format
23738 = Vframe_title_format
23739 = Fcons (intern ("multiple-frames"),
23740 Fcons (build_string ("%b"),
23741 Fcons (Fcons (empty_string,
23742 Fcons (intern ("invocation-name"),
23743 Fcons (build_string ("@"),
23744 Fcons (intern ("system-name"),
23745 Qnil)))),
23746 Qnil)));
23748 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23749 doc: /* Maximum number of lines to keep in the message log buffer.
23750 If nil, disable message logging. If t, log messages but don't truncate
23751 the buffer when it becomes large. */);
23752 Vmessage_log_max = make_number (50);
23754 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23755 doc: /* Functions called before redisplay, if window sizes have changed.
23756 The value should be a list of functions that take one argument.
23757 Just before redisplay, for each frame, if any of its windows have changed
23758 size since the last redisplay, or have been split or deleted,
23759 all the functions in the list are called, with the frame as argument. */);
23760 Vwindow_size_change_functions = Qnil;
23762 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23763 doc: /* List of functions to call before redisplaying a window with scrolling.
23764 Each function is called with two arguments, the window
23765 and its new display-start position. Note that the value of `window-end'
23766 is not valid when these functions are called. */);
23767 Vwindow_scroll_functions = Qnil;
23769 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23770 doc: /* Functions called when redisplay of a window reaches the end trigger.
23771 Each function is called with two arguments, the window and the end trigger value.
23772 See `set-window-redisplay-end-trigger'. */);
23773 Vredisplay_end_trigger_functions = Qnil;
23775 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23776 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23777 mouse_autoselect_window = 0;
23779 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23780 doc: /* *Non-nil means automatically resize tool-bars.
23781 This increases a tool-bar's height if not all tool-bar items are visible.
23782 It decreases a tool-bar's height when it would display blank lines
23783 otherwise. */);
23784 auto_resize_tool_bars_p = 1;
23786 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23787 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23788 auto_raise_tool_bar_buttons_p = 1;
23790 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23791 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23792 make_cursor_line_fully_visible_p = 1;
23794 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23795 doc: /* *Border below tool-bar in pixels.
23796 If an integer, use it as the height of the border.
23797 If it is one of `internal-border-width' or `border-width', use the
23798 value of the corresponding frame parameter.
23799 Otherwise, no border is added below the tool-bar. */);
23800 Vtool_bar_border = Qinternal_border_width;
23802 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23803 doc: /* *Margin around tool-bar buttons in pixels.
23804 If an integer, use that for both horizontal and vertical margins.
23805 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23806 HORZ specifying the horizontal margin, and VERT specifying the
23807 vertical margin. */);
23808 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23810 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23811 doc: /* *Relief thickness of tool-bar buttons. */);
23812 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23814 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23815 doc: /* List of functions to call to fontify regions of text.
23816 Each function is called with one argument POS. Functions must
23817 fontify a region starting at POS in the current buffer, and give
23818 fontified regions the property `fontified'. */);
23819 Vfontification_functions = Qnil;
23820 Fmake_variable_buffer_local (Qfontification_functions);
23822 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23823 &unibyte_display_via_language_environment,
23824 doc: /* *Non-nil means display unibyte text according to language environment.
23825 Specifically this means that unibyte non-ASCII characters
23826 are displayed by converting them to the equivalent multibyte characters
23827 according to the current language environment. As a result, they are
23828 displayed according to the current fontset. */);
23829 unibyte_display_via_language_environment = 0;
23831 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23832 doc: /* *Maximum height for resizing mini-windows.
23833 If a float, it specifies a fraction of the mini-window frame's height.
23834 If an integer, it specifies a number of lines. */);
23835 Vmax_mini_window_height = make_float (0.25);
23837 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23838 doc: /* *How to resize mini-windows.
23839 A value of nil means don't automatically resize mini-windows.
23840 A value of t means resize them to fit the text displayed in them.
23841 A value of `grow-only', the default, means let mini-windows grow
23842 only, until their display becomes empty, at which point the windows
23843 go back to their normal size. */);
23844 Vresize_mini_windows = Qgrow_only;
23846 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23847 doc: /* Alist specifying how to blink the cursor off.
23848 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23849 `cursor-type' frame-parameter or variable equals ON-STATE,
23850 comparing using `equal', Emacs uses OFF-STATE to specify
23851 how to blink it off. ON-STATE and OFF-STATE are values for
23852 the `cursor-type' frame parameter.
23854 If a frame's ON-STATE has no entry in this list,
23855 the frame's other specifications determine how to blink the cursor off. */);
23856 Vblink_cursor_alist = Qnil;
23858 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23859 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23860 automatic_hscrolling_p = 1;
23862 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23863 doc: /* *How many columns away from the window edge point is allowed to get
23864 before automatic hscrolling will horizontally scroll the window. */);
23865 hscroll_margin = 5;
23867 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23868 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23869 When point is less than `hscroll-margin' columns from the window
23870 edge, automatic hscrolling will scroll the window by the amount of columns
23871 determined by this variable. If its value is a positive integer, scroll that
23872 many columns. If it's a positive floating-point number, it specifies the
23873 fraction of the window's width to scroll. If it's nil or zero, point will be
23874 centered horizontally after the scroll. Any other value, including negative
23875 numbers, are treated as if the value were zero.
23877 Automatic hscrolling always moves point outside the scroll margin, so if
23878 point was more than scroll step columns inside the margin, the window will
23879 scroll more than the value given by the scroll step.
23881 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23882 and `scroll-right' overrides this variable's effect. */);
23883 Vhscroll_step = make_number (0);
23885 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23886 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23887 Bind this around calls to `message' to let it take effect. */);
23888 message_truncate_lines = 0;
23890 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23891 doc: /* Normal hook run to update the menu bar definitions.
23892 Redisplay runs this hook before it redisplays the menu bar.
23893 This is used to update submenus such as Buffers,
23894 whose contents depend on various data. */);
23895 Vmenu_bar_update_hook = Qnil;
23897 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23898 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23899 inhibit_menubar_update = 0;
23901 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23902 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23903 inhibit_eval_during_redisplay = 0;
23905 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23906 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23907 inhibit_free_realized_faces = 0;
23909 #if GLYPH_DEBUG
23910 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23911 doc: /* Inhibit try_window_id display optimization. */);
23912 inhibit_try_window_id = 0;
23914 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23915 doc: /* Inhibit try_window_reusing display optimization. */);
23916 inhibit_try_window_reusing = 0;
23918 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23919 doc: /* Inhibit try_cursor_movement display optimization. */);
23920 inhibit_try_cursor_movement = 0;
23921 #endif /* GLYPH_DEBUG */
23925 /* Initialize this module when Emacs starts. */
23927 void
23928 init_xdisp ()
23930 Lisp_Object root_window;
23931 struct window *mini_w;
23933 current_header_line_height = current_mode_line_height = -1;
23935 CHARPOS (this_line_start_pos) = 0;
23937 mini_w = XWINDOW (minibuf_window);
23938 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23940 if (!noninteractive)
23942 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23943 int i;
23945 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23946 set_window_height (root_window,
23947 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23949 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23950 set_window_height (minibuf_window, 1, 0);
23952 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23953 mini_w->total_cols = make_number (FRAME_COLS (f));
23955 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23956 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23957 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23959 /* The default ellipsis glyphs `...'. */
23960 for (i = 0; i < 3; ++i)
23961 default_invis_vector[i] = make_number ('.');
23965 /* Allocate the buffer for frame titles.
23966 Also used for `format-mode-line'. */
23967 int size = 100;
23968 mode_line_noprop_buf = (char *) xmalloc (size);
23969 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23970 mode_line_noprop_ptr = mode_line_noprop_buf;
23971 mode_line_target = MODE_LINE_DISPLAY;
23974 help_echo_showing_p = 0;
23978 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23979 (do not change this comment) */