(set-version): Add doc/lispref/vol1.texi,vol2.texi.
[emacs.git] / src / xdisp.c
blob18f176e2c0674e8a8bbf8a5bb1828d3b06a09d2d
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, 2007, 2008 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 3 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
171 #include "lisp.h"
172 #include "keyboard.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "character.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #include "font.h"
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
209 #define INFINITY 10000000
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
217 extern int interrupt_input;
218 extern int command_loop_level;
220 extern Lisp_Object do_mouse_tracking;
222 extern int minibuffer_auto_raise;
223 extern Lisp_Object Vminibuffer_list;
225 extern Lisp_Object Qface;
226 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
228 extern Lisp_Object Voverriding_local_map;
229 extern Lisp_Object Voverriding_local_map_menu_flag;
230 extern Lisp_Object Qmenu_item;
231 extern Lisp_Object Qwhen;
232 extern Lisp_Object Qhelp_echo;
234 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
235 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
236 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
237 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
238 Lisp_Object Qinhibit_point_motion_hooks;
239 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
240 Lisp_Object Qfontified;
241 Lisp_Object Qgrow_only;
242 Lisp_Object Qinhibit_eval_during_redisplay;
243 Lisp_Object Qbuffer_position, Qposition, Qobject;
245 /* Cursor shapes */
246 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
248 /* Pointer shapes */
249 Lisp_Object Qarrow, Qhand, Qtext;
251 Lisp_Object Qrisky_local_variable;
253 /* Holds the list (error). */
254 Lisp_Object list_of_error;
256 /* Functions called to fontify regions of text. */
258 Lisp_Object Vfontification_functions;
259 Lisp_Object Qfontification_functions;
261 /* Non-nil means automatically select any window when the mouse
262 cursor moves into it. */
263 Lisp_Object Vmouse_autoselect_window;
265 /* Non-zero means draw tool bar buttons raised when the mouse moves
266 over them. */
268 int auto_raise_tool_bar_buttons_p;
270 /* Non-zero means to reposition window if cursor line is only partially visible. */
272 int make_cursor_line_fully_visible_p;
274 /* Margin below tool bar in pixels. 0 or nil means no margin.
275 If value is `internal-border-width' or `border-width',
276 the corresponding frame parameter is used. */
278 Lisp_Object Vtool_bar_border;
280 /* Margin around tool bar buttons in pixels. */
282 Lisp_Object Vtool_bar_button_margin;
284 /* Thickness of shadow to draw around tool bar buttons. */
286 EMACS_INT tool_bar_button_relief;
288 /* Non-nil means automatically resize tool-bars so that all tool-bar
289 items are visible, and no blank lines remain.
291 If value is `grow-only', only make tool-bar bigger. */
293 Lisp_Object Vauto_resize_tool_bars;
295 /* Non-zero means draw block and hollow cursor as wide as the glyph
296 under it. For example, if a block cursor is over a tab, it will be
297 drawn as wide as that tab on the display. */
299 int x_stretch_cursor_p;
301 /* Non-nil means don't actually do any redisplay. */
303 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
305 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
307 int inhibit_eval_during_redisplay;
309 /* Names of text properties relevant for redisplay. */
311 Lisp_Object Qdisplay;
312 extern Lisp_Object Qface, Qinvisible, Qwidth;
314 /* Symbols used in text property values. */
316 Lisp_Object Vdisplay_pixels_per_inch;
317 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
318 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
319 Lisp_Object Qslice;
320 Lisp_Object Qcenter;
321 Lisp_Object Qmargin, Qpointer;
322 Lisp_Object Qline_height;
323 extern Lisp_Object Qheight;
324 extern Lisp_Object QCwidth, QCheight, QCascent;
325 extern Lisp_Object Qscroll_bar;
326 extern Lisp_Object Qcursor;
328 /* Non-nil means highlight trailing whitespace. */
330 Lisp_Object Vshow_trailing_whitespace;
332 /* Non-nil means escape non-break space and hyphens. */
334 Lisp_Object Vnobreak_char_display;
336 #ifdef HAVE_WINDOW_SYSTEM
337 extern Lisp_Object Voverflow_newline_into_fringe;
339 /* Test if overflow newline into fringe. Called with iterator IT
340 at or past right window margin, and with IT->current_x set. */
342 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
343 (!NILP (Voverflow_newline_into_fringe) \
344 && FRAME_WINDOW_P (it->f) \
345 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
346 && it->current_x == it->last_visible_x)
348 #endif /* HAVE_WINDOW_SYSTEM */
350 /* Non-nil means show the text cursor in void text areas
351 i.e. in blank areas after eol and eob. This used to be
352 the default in 21.3. */
354 Lisp_Object Vvoid_text_area_pointer;
356 /* Name of the face used to highlight trailing whitespace. */
358 Lisp_Object Qtrailing_whitespace;
360 /* Name and number of the face used to highlight escape glyphs. */
362 Lisp_Object Qescape_glyph;
364 /* Name and number of the face used to highlight non-breaking spaces. */
366 Lisp_Object Qnobreak_space;
368 /* The symbol `image' which is the car of the lists used to represent
369 images in Lisp. */
371 Lisp_Object Qimage;
373 /* The image map types. */
374 Lisp_Object QCmap, QCpointer;
375 Lisp_Object Qrect, Qcircle, Qpoly;
377 /* Non-zero means print newline to stdout before next mini-buffer
378 message. */
380 int noninteractive_need_newline;
382 /* Non-zero means print newline to message log before next message. */
384 static int message_log_need_newline;
386 /* Three markers that message_dolog uses.
387 It could allocate them itself, but that causes trouble
388 in handling memory-full errors. */
389 static Lisp_Object message_dolog_marker1;
390 static Lisp_Object message_dolog_marker2;
391 static Lisp_Object message_dolog_marker3;
393 /* The buffer position of the first character appearing entirely or
394 partially on the line of the selected window which contains the
395 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
396 redisplay optimization in redisplay_internal. */
398 static struct text_pos this_line_start_pos;
400 /* Number of characters past the end of the line above, including the
401 terminating newline. */
403 static struct text_pos this_line_end_pos;
405 /* The vertical positions and the height of this line. */
407 static int this_line_vpos;
408 static int this_line_y;
409 static int this_line_pixel_height;
411 /* X position at which this display line starts. Usually zero;
412 negative if first character is partially visible. */
414 static int this_line_start_x;
416 /* Buffer that this_line_.* variables are referring to. */
418 static struct buffer *this_line_buffer;
420 /* Nonzero means truncate lines in all windows less wide than the
421 frame. */
423 int truncate_partial_width_windows;
425 /* A flag to control how to display unibyte 8-bit character. */
427 int unibyte_display_via_language_environment;
429 /* Nonzero means we have more than one non-mini-buffer-only frame.
430 Not guaranteed to be accurate except while parsing
431 frame-title-format. */
433 int multiple_frames;
435 Lisp_Object Vglobal_mode_string;
438 /* List of variables (symbols) which hold markers for overlay arrows.
439 The symbols on this list are examined during redisplay to determine
440 where to display overlay arrows. */
442 Lisp_Object Voverlay_arrow_variable_list;
444 /* Marker for where to display an arrow on top of the buffer text. */
446 Lisp_Object Voverlay_arrow_position;
448 /* String to display for the arrow. Only used on terminal frames. */
450 Lisp_Object Voverlay_arrow_string;
452 /* Values of those variables at last redisplay are stored as
453 properties on `overlay-arrow-position' symbol. However, if
454 Voverlay_arrow_position is a marker, last-arrow-position is its
455 numerical position. */
457 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
459 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
460 properties on a symbol in overlay-arrow-variable-list. */
462 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
464 /* Like mode-line-format, but for the title bar on a visible frame. */
466 Lisp_Object Vframe_title_format;
468 /* Like mode-line-format, but for the title bar on an iconified frame. */
470 Lisp_Object Vicon_title_format;
472 /* List of functions to call when a window's size changes. These
473 functions get one arg, a frame on which one or more windows' sizes
474 have changed. */
476 static Lisp_Object Vwindow_size_change_functions;
478 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
480 /* Nonzero if an overlay arrow has been displayed in this window. */
482 static int overlay_arrow_seen;
484 /* Nonzero means highlight the region even in nonselected windows. */
486 int highlight_nonselected_windows;
488 /* If cursor motion alone moves point off frame, try scrolling this
489 many lines up or down if that will bring it back. */
491 static EMACS_INT scroll_step;
493 /* Nonzero means scroll just far enough to bring point back on the
494 screen, when appropriate. */
496 static EMACS_INT scroll_conservatively;
498 /* Recenter the window whenever point gets within this many lines of
499 the top or bottom of the window. This value is translated into a
500 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
501 that there is really a fixed pixel height scroll margin. */
503 EMACS_INT scroll_margin;
505 /* Number of windows showing the buffer of the selected window (or
506 another buffer with the same base buffer). keyboard.c refers to
507 this. */
509 int buffer_shared;
511 /* Vector containing glyphs for an ellipsis `...'. */
513 static Lisp_Object default_invis_vector[3];
515 /* Zero means display the mode-line/header-line/menu-bar in the default face
516 (this slightly odd definition is for compatibility with previous versions
517 of emacs), non-zero means display them using their respective faces.
519 This variable is deprecated. */
521 int mode_line_inverse_video;
523 /* Prompt to display in front of the mini-buffer contents. */
525 Lisp_Object minibuf_prompt;
527 /* Width of current mini-buffer prompt. Only set after display_line
528 of the line that contains the prompt. */
530 int minibuf_prompt_width;
532 /* This is the window where the echo area message was displayed. It
533 is always a mini-buffer window, but it may not be the same window
534 currently active as a mini-buffer. */
536 Lisp_Object echo_area_window;
538 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
539 pushes the current message and the value of
540 message_enable_multibyte on the stack, the function restore_message
541 pops the stack and displays MESSAGE again. */
543 Lisp_Object Vmessage_stack;
545 /* Nonzero means multibyte characters were enabled when the echo area
546 message was specified. */
548 int message_enable_multibyte;
550 /* Nonzero if we should redraw the mode lines on the next redisplay. */
552 int update_mode_lines;
554 /* Nonzero if window sizes or contents have changed since last
555 redisplay that finished. */
557 int windows_or_buffers_changed;
559 /* Nonzero means a frame's cursor type has been changed. */
561 int cursor_type_changed;
563 /* Nonzero after display_mode_line if %l was used and it displayed a
564 line number. */
566 int line_number_displayed;
568 /* Maximum buffer size for which to display line numbers. */
570 Lisp_Object Vline_number_display_limit;
572 /* Line width to consider when repositioning for line number display. */
574 static EMACS_INT line_number_display_limit_width;
576 /* Number of lines to keep in the message log buffer. t means
577 infinite. nil means don't log at all. */
579 Lisp_Object Vmessage_log_max;
581 /* The name of the *Messages* buffer, a string. */
583 static Lisp_Object Vmessages_buffer_name;
585 /* Current, index 0, and last displayed echo area message. Either
586 buffers from echo_buffers, or nil to indicate no message. */
588 Lisp_Object echo_area_buffer[2];
590 /* The buffers referenced from echo_area_buffer. */
592 static Lisp_Object echo_buffer[2];
594 /* A vector saved used in with_area_buffer to reduce consing. */
596 static Lisp_Object Vwith_echo_area_save_vector;
598 /* Non-zero means display_echo_area should display the last echo area
599 message again. Set by redisplay_preserve_echo_area. */
601 static int display_last_displayed_message_p;
603 /* Nonzero if echo area is being used by print; zero if being used by
604 message. */
606 int message_buf_print;
608 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
610 Lisp_Object Qinhibit_menubar_update;
611 int inhibit_menubar_update;
613 /* When evaluating expressions from menu bar items (enable conditions,
614 for instance), this is the frame they are being processed for. */
616 Lisp_Object Vmenu_updating_frame;
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
622 Lisp_Object Vmax_mini_window_height;
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
633 static int message_cleared_p;
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
645 /* Ascent and height of the last line processed by move_it_to. */
647 static int last_max_ascent, last_height;
649 /* Non-zero if there's a help-echo in the echo area. */
651 int help_echo_showing_p;
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
657 int current_mode_line_height, current_header_line_height;
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
665 #define TEXT_PROP_DISTANCE_LIMIT 100
667 #if GLYPH_DEBUG
669 /* Variables to turn off display optimizations from Lisp. */
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
677 int trace_redisplay_p;
679 #endif /* GLYPH_DEBUG */
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
693 int automatic_hscrolling_p;
694 Lisp_Object Qauto_hscroll_mode;
696 /* How close to the margin can point get before the window is scrolled
697 horizontally. */
698 EMACS_INT hscroll_margin;
700 /* How much to scroll horizontally when point is inside the above margin. */
701 Lisp_Object Vhscroll_step;
703 /* The variable `resize-mini-windows'. If nil, don't resize
704 mini-windows. If t, always resize them to fit the text they
705 display. If `grow-only', let mini-windows grow only until they
706 become empty. */
708 Lisp_Object Vresize_mini_windows;
710 /* Buffer being redisplayed -- for redisplay_window_error. */
712 struct buffer *displayed_buffer;
714 /* Space between overline and text. */
716 EMACS_INT overline_margin;
718 /* Require underline to be at least this many screen pixels below baseline
719 This to avoid underline "merging" with the base of letters at small
720 font sizes, particularly when x_use_underline_position_properties is on. */
722 EMACS_INT underline_minimum_offset;
724 /* Value returned from text property handlers (see below). */
726 enum prop_handled
728 HANDLED_NORMALLY,
729 HANDLED_RECOMPUTE_PROPS,
730 HANDLED_OVERLAY_STRING_CONSUMED,
731 HANDLED_RETURN
734 /* A description of text properties that redisplay is interested
735 in. */
737 struct props
739 /* The name of the property. */
740 Lisp_Object *name;
742 /* A unique index for the property. */
743 enum prop_idx idx;
745 /* A handler function called to set up iterator IT from the property
746 at IT's current position. Value is used to steer handle_stop. */
747 enum prop_handled (*handler) P_ ((struct it *it));
750 static enum prop_handled handle_face_prop P_ ((struct it *));
751 static enum prop_handled handle_invisible_prop P_ ((struct it *));
752 static enum prop_handled handle_display_prop P_ ((struct it *));
753 static enum prop_handled handle_composition_prop P_ ((struct it *));
754 static enum prop_handled handle_overlay_change P_ ((struct it *));
755 static enum prop_handled handle_fontified_prop P_ ((struct it *));
756 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
758 /* Properties handled by iterators. */
760 static struct props it_props[] =
762 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
763 /* Handle `face' before `display' because some sub-properties of
764 `display' need to know the face. */
765 {&Qface, FACE_PROP_IDX, handle_face_prop},
766 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
767 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
768 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
769 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
770 {NULL, 0, NULL}
773 /* Value is the position described by X. If X is a marker, value is
774 the marker_position of X. Otherwise, value is X. */
776 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
778 /* Enumeration returned by some move_it_.* functions internally. */
780 enum move_it_result
782 /* Not used. Undefined value. */
783 MOVE_UNDEFINED,
785 /* Move ended at the requested buffer position or ZV. */
786 MOVE_POS_MATCH_OR_ZV,
788 /* Move ended at the requested X pixel position. */
789 MOVE_X_REACHED,
791 /* Move within a line ended at the end of a line that must be
792 continued. */
793 MOVE_LINE_CONTINUED,
795 /* Move within a line ended at the end of a line that would
796 be displayed truncated. */
797 MOVE_LINE_TRUNCATED,
799 /* Move within a line ended at a line end. */
800 MOVE_NEWLINE_OR_CR
803 /* This counter is used to clear the face cache every once in a while
804 in redisplay_internal. It is incremented for each redisplay.
805 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
806 cleared. */
808 #define CLEAR_FACE_CACHE_COUNT 500
809 static int clear_face_cache_count;
811 /* Similarly for the image cache. */
813 #ifdef HAVE_WINDOW_SYSTEM
814 #define CLEAR_IMAGE_CACHE_COUNT 101
815 static int clear_image_cache_count;
816 #endif
818 /* Non-zero while redisplay_internal is in progress. */
820 int redisplaying_p;
822 /* Non-zero means don't free realized faces. Bound while freeing
823 realized faces is dangerous because glyph matrices might still
824 reference them. */
826 int inhibit_free_realized_faces;
827 Lisp_Object Qinhibit_free_realized_faces;
829 /* If a string, XTread_socket generates an event to display that string.
830 (The display is done in read_char.) */
832 Lisp_Object help_echo_string;
833 Lisp_Object help_echo_window;
834 Lisp_Object help_echo_object;
835 int help_echo_pos;
837 /* Temporary variable for XTread_socket. */
839 Lisp_Object previous_help_echo_string;
841 /* Null glyph slice */
843 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
846 /* Function prototypes. */
848 static void setup_for_ellipsis P_ ((struct it *, int));
849 static void mark_window_display_accurate_1 P_ ((struct window *, int));
850 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
851 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
852 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
853 static int redisplay_mode_lines P_ ((Lisp_Object, int));
854 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
856 #if 0
857 static int invisible_text_between_p P_ ((struct it *, int, int));
858 #endif
860 static void pint2str P_ ((char *, int, int));
861 static void pint2hrstr P_ ((char *, int, int));
862 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
863 struct text_pos));
864 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
865 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
866 static void store_mode_line_noprop_char P_ ((char));
867 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
868 static void x_consider_frame_title P_ ((Lisp_Object));
869 static void handle_stop P_ ((struct it *));
870 static int tool_bar_lines_needed P_ ((struct frame *, int *));
871 static int single_display_spec_intangible_p P_ ((Lisp_Object));
872 static void ensure_echo_area_buffers P_ ((void));
873 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
874 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
875 static int with_echo_area_buffer P_ ((struct window *, int,
876 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
877 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static void clear_garbaged_frames P_ ((void));
879 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int display_echo_area P_ ((struct window *));
883 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
885 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
886 static int string_char_and_length P_ ((const unsigned char *, int, int *));
887 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
888 struct text_pos));
889 static int compute_window_start_on_continuation_line P_ ((struct window *));
890 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
891 static void insert_left_trunc_glyphs P_ ((struct it *));
892 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
893 Lisp_Object));
894 static void extend_face_to_end_of_line P_ ((struct it *));
895 static int append_space_for_newline P_ ((struct it *, int));
896 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
897 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
898 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
899 static int trailing_whitespace_p P_ ((int));
900 static int message_log_check_duplicate P_ ((int, int, int, int));
901 static void push_it P_ ((struct it *));
902 static void pop_it P_ ((struct it *));
903 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
904 static void select_frame_for_redisplay P_ ((Lisp_Object));
905 static void redisplay_internal P_ ((int));
906 static int echo_area_display P_ ((int));
907 static void redisplay_windows P_ ((Lisp_Object));
908 static void redisplay_window P_ ((Lisp_Object, int));
909 static Lisp_Object redisplay_window_error ();
910 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
911 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
912 static int update_menu_bar P_ ((struct frame *, int, int));
913 static int try_window_reusing_current_matrix P_ ((struct window *));
914 static int try_window_id P_ ((struct window *));
915 static int display_line P_ ((struct it *));
916 static int display_mode_lines P_ ((struct window *));
917 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
918 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
919 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
920 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
921 static void display_menu_bar P_ ((struct window *));
922 static int display_count_lines P_ ((int, int, int, int, int *));
923 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
924 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
925 static void compute_line_metrics P_ ((struct it *));
926 static void run_redisplay_end_trigger_hook P_ ((struct it *));
927 static int get_overlay_strings P_ ((struct it *, int));
928 static int get_overlay_strings_1 P_ ((struct it *, int, int));
929 static void next_overlay_string P_ ((struct it *));
930 static void reseat P_ ((struct it *, struct text_pos, int));
931 static void reseat_1 P_ ((struct it *, struct text_pos, int));
932 static void back_to_previous_visible_line_start P_ ((struct it *));
933 void reseat_at_previous_visible_line_start P_ ((struct it *));
934 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
935 static int next_element_from_ellipsis P_ ((struct it *));
936 static int next_element_from_display_vector P_ ((struct it *));
937 static int next_element_from_string P_ ((struct it *));
938 static int next_element_from_c_string P_ ((struct it *));
939 static int next_element_from_buffer P_ ((struct it *));
940 static int next_element_from_composition P_ ((struct it *));
941 static int next_element_from_image P_ ((struct it *));
942 static int next_element_from_stretch P_ ((struct it *));
943 static void load_overlay_strings P_ ((struct it *, int));
944 static int init_from_display_pos P_ ((struct it *, struct window *,
945 struct display_pos *));
946 static void reseat_to_string P_ ((struct it *, unsigned char *,
947 Lisp_Object, int, int, int, int));
948 static enum move_it_result
949 move_it_in_display_line_to (struct it *, EMACS_INT, int,
950 enum move_operation_enum);
951 void move_it_vertically_backward P_ ((struct it *, int));
952 static void init_to_row_start P_ ((struct it *, struct window *,
953 struct glyph_row *));
954 static int init_to_row_end P_ ((struct it *, struct window *,
955 struct glyph_row *));
956 static void back_to_previous_line_start P_ ((struct it *));
957 static int forward_to_next_line_start P_ ((struct it *, int *));
958 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
959 Lisp_Object, int));
960 static struct text_pos string_pos P_ ((int, Lisp_Object));
961 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
962 static int number_of_chars P_ ((unsigned char *, int));
963 static void compute_stop_pos P_ ((struct it *));
964 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
965 Lisp_Object));
966 static int face_before_or_after_it_pos P_ ((struct it *, int));
967 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
968 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
969 Lisp_Object, Lisp_Object,
970 struct text_pos *, int));
971 static int underlying_face_id P_ ((struct it *));
972 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
973 struct window *));
975 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
976 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
978 #ifdef HAVE_WINDOW_SYSTEM
980 static void update_tool_bar P_ ((struct frame *, int));
981 static void build_desired_tool_bar_string P_ ((struct frame *f));
982 static int redisplay_tool_bar P_ ((struct frame *));
983 static void display_tool_bar_line P_ ((struct it *, int));
984 static void notice_overwritten_cursor P_ ((struct window *,
985 enum glyph_row_area,
986 int, int, int, int));
990 #endif /* HAVE_WINDOW_SYSTEM */
993 /***********************************************************************
994 Window display dimensions
995 ***********************************************************************/
997 /* Return the bottom boundary y-position for text lines in window W.
998 This is the first y position at which a line cannot start.
999 It is relative to the top of the window.
1001 This is the height of W minus the height of a mode line, if any. */
1003 INLINE int
1004 window_text_bottom_y (w)
1005 struct window *w;
1007 int height = WINDOW_TOTAL_HEIGHT (w);
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011 return height;
1014 /* Return the pixel width of display area AREA of window W. AREA < 0
1015 means return the total width of W, not including fringes to
1016 the left and right of the window. */
1018 INLINE int
1019 window_box_width (w, area)
1020 struct window *w;
1021 int area;
1023 int cols = XFASTINT (w->total_cols);
1024 int pixels = 0;
1026 if (!w->pseudo_window_p)
1028 cols -= WINDOW_SCROLL_BAR_COLS (w);
1030 if (area == TEXT_AREA)
1032 if (INTEGERP (w->left_margin_cols))
1033 cols -= XFASTINT (w->left_margin_cols);
1034 if (INTEGERP (w->right_margin_cols))
1035 cols -= XFASTINT (w->right_margin_cols);
1036 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1038 else if (area == LEFT_MARGIN_AREA)
1040 cols = (INTEGERP (w->left_margin_cols)
1041 ? XFASTINT (w->left_margin_cols) : 0);
1042 pixels = 0;
1044 else if (area == RIGHT_MARGIN_AREA)
1046 cols = (INTEGERP (w->right_margin_cols)
1047 ? XFASTINT (w->right_margin_cols) : 0);
1048 pixels = 0;
1052 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1056 /* Return the pixel height of the display area of window W, not
1057 including mode lines of W, if any. */
1059 INLINE int
1060 window_box_height (w)
1061 struct window *w;
1063 struct frame *f = XFRAME (w->frame);
1064 int height = WINDOW_TOTAL_HEIGHT (w);
1066 xassert (height >= 0);
1068 /* Note: the code below that determines the mode-line/header-line
1069 height is essentially the same as that contained in the macro
1070 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1071 the appropriate glyph row has its `mode_line_p' flag set,
1072 and if it doesn't, uses estimate_mode_line_height instead. */
1074 if (WINDOW_WANTS_MODELINE_P (w))
1076 struct glyph_row *ml_row
1077 = (w->current_matrix && w->current_matrix->rows
1078 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1079 : 0);
1080 if (ml_row && ml_row->mode_line_p)
1081 height -= ml_row->height;
1082 else
1083 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1086 if (WINDOW_WANTS_HEADER_LINE_P (w))
1088 struct glyph_row *hl_row
1089 = (w->current_matrix && w->current_matrix->rows
1090 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1091 : 0);
1092 if (hl_row && hl_row->mode_line_p)
1093 height -= hl_row->height;
1094 else
1095 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1098 /* With a very small font and a mode-line that's taller than
1099 default, we might end up with a negative height. */
1100 return max (0, height);
1103 /* Return the window-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1107 INLINE int
1108 window_box_left_offset (w, area)
1109 struct window *w;
1110 int area;
1112 int x;
1114 if (w->pseudo_window_p)
1115 return 0;
1117 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1119 if (area == TEXT_AREA)
1120 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1121 + window_box_width (w, LEFT_MARGIN_AREA));
1122 else if (area == RIGHT_MARGIN_AREA)
1123 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1124 + window_box_width (w, LEFT_MARGIN_AREA)
1125 + window_box_width (w, TEXT_AREA)
1126 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1128 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1129 else if (area == LEFT_MARGIN_AREA
1130 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1131 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1133 return x;
1137 /* Return the window-relative coordinate of the right edge of display
1138 area AREA of window W. AREA < 0 means return the left edge of the
1139 whole window, to the left of the right fringe of W. */
1141 INLINE int
1142 window_box_right_offset (w, area)
1143 struct window *w;
1144 int area;
1146 return window_box_left_offset (w, area) + window_box_width (w, area);
1149 /* Return the frame-relative coordinate of the left edge of display
1150 area AREA of window W. AREA < 0 means return the left edge of the
1151 whole window, to the right of the left fringe of W. */
1153 INLINE int
1154 window_box_left (w, area)
1155 struct window *w;
1156 int area;
1158 struct frame *f = XFRAME (w->frame);
1159 int x;
1161 if (w->pseudo_window_p)
1162 return FRAME_INTERNAL_BORDER_WIDTH (f);
1164 x = (WINDOW_LEFT_EDGE_X (w)
1165 + window_box_left_offset (w, area));
1167 return x;
1171 /* Return the frame-relative coordinate of the right edge of display
1172 area AREA of window W. AREA < 0 means return the left edge of the
1173 whole window, to the left of the right fringe of W. */
1175 INLINE int
1176 window_box_right (w, area)
1177 struct window *w;
1178 int area;
1180 return window_box_left (w, area) + window_box_width (w, area);
1183 /* Get the bounding box of the display area AREA of window W, without
1184 mode lines, in frame-relative coordinates. AREA < 0 means the
1185 whole window, not including the left and right fringes of
1186 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1187 coordinates of the upper-left corner of the box. Return in
1188 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1190 INLINE void
1191 window_box (w, area, box_x, box_y, box_width, box_height)
1192 struct window *w;
1193 int area;
1194 int *box_x, *box_y, *box_width, *box_height;
1196 if (box_width)
1197 *box_width = window_box_width (w, area);
1198 if (box_height)
1199 *box_height = window_box_height (w);
1200 if (box_x)
1201 *box_x = window_box_left (w, area);
1202 if (box_y)
1204 *box_y = WINDOW_TOP_EDGE_Y (w);
1205 if (WINDOW_WANTS_HEADER_LINE_P (w))
1206 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1211 /* Get the bounding box of the display area AREA of window W, without
1212 mode lines. AREA < 0 means the whole window, not including the
1213 left and right fringe of the window. Return in *TOP_LEFT_X
1214 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1215 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1216 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1217 box. */
1219 INLINE void
1220 window_box_edges (w, area, top_left_x, top_left_y,
1221 bottom_right_x, bottom_right_y)
1222 struct window *w;
1223 int area;
1224 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1226 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1227 bottom_right_y);
1228 *bottom_right_x += *top_left_x;
1229 *bottom_right_y += *top_left_y;
1234 /***********************************************************************
1235 Utilities
1236 ***********************************************************************/
1238 /* Return the bottom y-position of the line the iterator IT is in.
1239 This can modify IT's settings. */
1242 line_bottom_y (it)
1243 struct it *it;
1245 int line_height = it->max_ascent + it->max_descent;
1246 int line_top_y = it->current_y;
1248 if (line_height == 0)
1250 if (last_height)
1251 line_height = last_height;
1252 else if (IT_CHARPOS (*it) < ZV)
1254 move_it_by_lines (it, 1, 1);
1255 line_height = (it->max_ascent || it->max_descent
1256 ? it->max_ascent + it->max_descent
1257 : last_height);
1259 else
1261 struct glyph_row *row = it->glyph_row;
1263 /* Use the default character height. */
1264 it->glyph_row = NULL;
1265 it->what = IT_CHARACTER;
1266 it->c = ' ';
1267 it->len = 1;
1268 PRODUCE_GLYPHS (it);
1269 line_height = it->ascent + it->descent;
1270 it->glyph_row = row;
1274 return line_top_y + line_height;
1278 /* Return 1 if position CHARPOS is visible in window W.
1279 CHARPOS < 0 means return info about WINDOW_END position.
1280 If visible, set *X and *Y to pixel coordinates of top left corner.
1281 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1282 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1285 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1286 struct window *w;
1287 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1289 struct it it;
1290 struct text_pos top;
1291 int visible_p = 0;
1292 struct buffer *old_buffer = NULL;
1294 if (noninteractive)
1295 return visible_p;
1297 if (XBUFFER (w->buffer) != current_buffer)
1299 old_buffer = current_buffer;
1300 set_buffer_internal_1 (XBUFFER (w->buffer));
1303 SET_TEXT_POS_FROM_MARKER (top, w->start);
1305 /* Compute exact mode line heights. */
1306 if (WINDOW_WANTS_MODELINE_P (w))
1307 current_mode_line_height
1308 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1309 current_buffer->mode_line_format);
1311 if (WINDOW_WANTS_HEADER_LINE_P (w))
1312 current_header_line_height
1313 = display_mode_line (w, HEADER_LINE_FACE_ID,
1314 current_buffer->header_line_format);
1316 start_display (&it, w, top);
1317 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1318 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1320 /* Note that we may overshoot because of invisible text. */
1321 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1323 int top_x = it.current_x;
1324 int top_y = it.current_y;
1325 int bottom_y = (last_height = 0, line_bottom_y (&it));
1326 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1328 if (top_y < window_top_y)
1329 visible_p = bottom_y > window_top_y;
1330 else if (top_y < it.last_visible_y)
1331 visible_p = 1;
1332 if (visible_p)
1334 if (it.method == GET_FROM_BUFFER)
1336 Lisp_Object window, prop;
1338 XSETWINDOW (window, w);
1339 prop = Fget_char_property (make_number (it.position.charpos),
1340 Qinvisible, window);
1342 /* If charpos coincides with invisible text covered with an
1343 ellipsis, use the first glyph of the ellipsis to compute
1344 the pixel positions. */
1345 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1347 struct glyph_row *row = it.glyph_row;
1348 struct glyph *glyph = row->glyphs[TEXT_AREA];
1349 struct glyph *end = glyph + row->used[TEXT_AREA];
1350 int x = row->x;
1352 for (; glyph < end && glyph->charpos < charpos; glyph++)
1353 x += glyph->pixel_width;
1355 top_x = x;
1359 *x = top_x;
1360 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1361 *rtop = max (0, window_top_y - top_y);
1362 *rbot = max (0, bottom_y - it.last_visible_y);
1363 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1364 - max (top_y, window_top_y)));
1365 *vpos = it.vpos;
1368 else
1370 struct it it2;
1372 it2 = it;
1373 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1374 move_it_by_lines (&it, 1, 0);
1375 if (charpos < IT_CHARPOS (it)
1376 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1378 visible_p = 1;
1379 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1380 *x = it2.current_x;
1381 *y = it2.current_y + it2.max_ascent - it2.ascent;
1382 *rtop = max (0, -it2.current_y);
1383 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1384 - it.last_visible_y));
1385 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1386 it.last_visible_y)
1387 - max (it2.current_y,
1388 WINDOW_HEADER_LINE_HEIGHT (w))));
1389 *vpos = it2.vpos;
1393 if (old_buffer)
1394 set_buffer_internal_1 (old_buffer);
1396 current_header_line_height = current_mode_line_height = -1;
1398 if (visible_p && XFASTINT (w->hscroll) > 0)
1399 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1401 #if 0
1402 /* Debugging code. */
1403 if (visible_p)
1404 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1405 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1406 else
1407 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1408 #endif
1410 return visible_p;
1414 /* Return the next character from STR which is MAXLEN bytes long.
1415 Return in *LEN the length of the character. This is like
1416 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1417 we find one, we return a `?', but with the length of the invalid
1418 character. */
1420 static INLINE int
1421 string_char_and_length (str, maxlen, len)
1422 const unsigned char *str;
1423 int maxlen, *len;
1425 int c;
1427 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1428 if (!CHAR_VALID_P (c, 1))
1429 /* We may not change the length here because other places in Emacs
1430 don't use this function, i.e. they silently accept invalid
1431 characters. */
1432 c = '?';
1434 return c;
1439 /* Given a position POS containing a valid character and byte position
1440 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1442 static struct text_pos
1443 string_pos_nchars_ahead (pos, string, nchars)
1444 struct text_pos pos;
1445 Lisp_Object string;
1446 int nchars;
1448 xassert (STRINGP (string) && nchars >= 0);
1450 if (STRING_MULTIBYTE (string))
1452 int rest = SBYTES (string) - BYTEPOS (pos);
1453 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1454 int len;
1456 while (nchars--)
1458 string_char_and_length (p, rest, &len);
1459 p += len, rest -= len;
1460 xassert (rest >= 0);
1461 CHARPOS (pos) += 1;
1462 BYTEPOS (pos) += len;
1465 else
1466 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1468 return pos;
1472 /* Value is the text position, i.e. character and byte position,
1473 for character position CHARPOS in STRING. */
1475 static INLINE struct text_pos
1476 string_pos (charpos, string)
1477 int charpos;
1478 Lisp_Object string;
1480 struct text_pos pos;
1481 xassert (STRINGP (string));
1482 xassert (charpos >= 0);
1483 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1484 return pos;
1488 /* Value is a text position, i.e. character and byte position, for
1489 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1490 means recognize multibyte characters. */
1492 static struct text_pos
1493 c_string_pos (charpos, s, multibyte_p)
1494 int charpos;
1495 unsigned char *s;
1496 int multibyte_p;
1498 struct text_pos pos;
1500 xassert (s != NULL);
1501 xassert (charpos >= 0);
1503 if (multibyte_p)
1505 int rest = strlen (s), len;
1507 SET_TEXT_POS (pos, 0, 0);
1508 while (charpos--)
1510 string_char_and_length (s, rest, &len);
1511 s += len, rest -= len;
1512 xassert (rest >= 0);
1513 CHARPOS (pos) += 1;
1514 BYTEPOS (pos) += len;
1517 else
1518 SET_TEXT_POS (pos, charpos, charpos);
1520 return pos;
1524 /* Value is the number of characters in C string S. MULTIBYTE_P
1525 non-zero means recognize multibyte characters. */
1527 static int
1528 number_of_chars (s, multibyte_p)
1529 unsigned char *s;
1530 int multibyte_p;
1532 int nchars;
1534 if (multibyte_p)
1536 int rest = strlen (s), len;
1537 unsigned char *p = (unsigned char *) s;
1539 for (nchars = 0; rest > 0; ++nchars)
1541 string_char_and_length (p, rest, &len);
1542 rest -= len, p += len;
1545 else
1546 nchars = strlen (s);
1548 return nchars;
1552 /* Compute byte position NEWPOS->bytepos corresponding to
1553 NEWPOS->charpos. POS is a known position in string STRING.
1554 NEWPOS->charpos must be >= POS.charpos. */
1556 static void
1557 compute_string_pos (newpos, pos, string)
1558 struct text_pos *newpos, pos;
1559 Lisp_Object string;
1561 xassert (STRINGP (string));
1562 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1564 if (STRING_MULTIBYTE (string))
1565 *newpos = string_pos_nchars_ahead (pos, string,
1566 CHARPOS (*newpos) - CHARPOS (pos));
1567 else
1568 BYTEPOS (*newpos) = CHARPOS (*newpos);
1571 /* EXPORT:
1572 Return an estimation of the pixel height of mode or top lines on
1573 frame F. FACE_ID specifies what line's height to estimate. */
1576 estimate_mode_line_height (f, face_id)
1577 struct frame *f;
1578 enum face_id face_id;
1580 #ifdef HAVE_WINDOW_SYSTEM
1581 if (FRAME_WINDOW_P (f))
1583 int height = FONT_HEIGHT (FRAME_FONT (f));
1585 /* This function is called so early when Emacs starts that the face
1586 cache and mode line face are not yet initialized. */
1587 if (FRAME_FACE_CACHE (f))
1589 struct face *face = FACE_FROM_ID (f, face_id);
1590 if (face)
1592 if (face->font)
1593 height = FONT_HEIGHT (face->font);
1594 if (face->box_line_width > 0)
1595 height += 2 * face->box_line_width;
1599 return height;
1601 #endif
1603 return 1;
1606 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1607 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1608 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1609 not force the value into range. */
1611 void
1612 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1613 FRAME_PTR f;
1614 register int pix_x, pix_y;
1615 int *x, *y;
1616 NativeRectangle *bounds;
1617 int noclip;
1620 #ifdef HAVE_WINDOW_SYSTEM
1621 if (FRAME_WINDOW_P (f))
1623 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1624 even for negative values. */
1625 if (pix_x < 0)
1626 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1627 if (pix_y < 0)
1628 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1630 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1631 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1633 if (bounds)
1634 STORE_NATIVE_RECT (*bounds,
1635 FRAME_COL_TO_PIXEL_X (f, pix_x),
1636 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1637 FRAME_COLUMN_WIDTH (f) - 1,
1638 FRAME_LINE_HEIGHT (f) - 1);
1640 if (!noclip)
1642 if (pix_x < 0)
1643 pix_x = 0;
1644 else if (pix_x > FRAME_TOTAL_COLS (f))
1645 pix_x = FRAME_TOTAL_COLS (f);
1647 if (pix_y < 0)
1648 pix_y = 0;
1649 else if (pix_y > FRAME_LINES (f))
1650 pix_y = FRAME_LINES (f);
1653 #endif
1655 *x = pix_x;
1656 *y = pix_y;
1660 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1661 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1662 can't tell the positions because W's display is not up to date,
1663 return 0. */
1666 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1667 struct window *w;
1668 int hpos, vpos;
1669 int *frame_x, *frame_y;
1671 #ifdef HAVE_WINDOW_SYSTEM
1672 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1674 int success_p;
1676 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1677 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1679 if (display_completed)
1681 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1682 struct glyph *glyph = row->glyphs[TEXT_AREA];
1683 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1685 hpos = row->x;
1686 vpos = row->y;
1687 while (glyph < end)
1689 hpos += glyph->pixel_width;
1690 ++glyph;
1693 /* If first glyph is partially visible, its first visible position is still 0. */
1694 if (hpos < 0)
1695 hpos = 0;
1697 success_p = 1;
1699 else
1701 hpos = vpos = 0;
1702 success_p = 0;
1705 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1706 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1707 return success_p;
1709 #endif
1711 *frame_x = hpos;
1712 *frame_y = vpos;
1713 return 1;
1717 #ifdef HAVE_WINDOW_SYSTEM
1719 /* Find the glyph under window-relative coordinates X/Y in window W.
1720 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1721 strings. Return in *HPOS and *VPOS the row and column number of
1722 the glyph found. Return in *AREA the glyph area containing X.
1723 Value is a pointer to the glyph found or null if X/Y is not on
1724 text, or we can't tell because W's current matrix is not up to
1725 date. */
1727 #ifndef HAVE_CARBON
1728 static
1729 #endif
1730 struct glyph *
1731 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1732 struct window *w;
1733 int x, y;
1734 int *hpos, *vpos, *dx, *dy, *area;
1736 struct glyph *glyph, *end;
1737 struct glyph_row *row = NULL;
1738 int x0, i;
1740 /* Find row containing Y. Give up if some row is not enabled. */
1741 for (i = 0; i < w->current_matrix->nrows; ++i)
1743 row = MATRIX_ROW (w->current_matrix, i);
1744 if (!row->enabled_p)
1745 return NULL;
1746 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1747 break;
1750 *vpos = i;
1751 *hpos = 0;
1753 /* Give up if Y is not in the window. */
1754 if (i == w->current_matrix->nrows)
1755 return NULL;
1757 /* Get the glyph area containing X. */
1758 if (w->pseudo_window_p)
1760 *area = TEXT_AREA;
1761 x0 = 0;
1763 else
1765 if (x < window_box_left_offset (w, TEXT_AREA))
1767 *area = LEFT_MARGIN_AREA;
1768 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1770 else if (x < window_box_right_offset (w, TEXT_AREA))
1772 *area = TEXT_AREA;
1773 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1775 else
1777 *area = RIGHT_MARGIN_AREA;
1778 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1782 /* Find glyph containing X. */
1783 glyph = row->glyphs[*area];
1784 end = glyph + row->used[*area];
1785 x -= x0;
1786 while (glyph < end && x >= glyph->pixel_width)
1788 x -= glyph->pixel_width;
1789 ++glyph;
1792 if (glyph == end)
1793 return NULL;
1795 if (dx)
1797 *dx = x;
1798 *dy = y - (row->y + row->ascent - glyph->ascent);
1801 *hpos = glyph - row->glyphs[*area];
1802 return glyph;
1806 /* EXPORT:
1807 Convert frame-relative x/y to coordinates relative to window W.
1808 Takes pseudo-windows into account. */
1810 void
1811 frame_to_window_pixel_xy (w, x, y)
1812 struct window *w;
1813 int *x, *y;
1815 if (w->pseudo_window_p)
1817 /* A pseudo-window is always full-width, and starts at the
1818 left edge of the frame, plus a frame border. */
1819 struct frame *f = XFRAME (w->frame);
1820 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1821 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1823 else
1825 *x -= WINDOW_LEFT_EDGE_X (w);
1826 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1830 /* EXPORT:
1831 Return in RECTS[] at most N clipping rectangles for glyph string S.
1832 Return the number of stored rectangles. */
1835 get_glyph_string_clip_rects (s, rects, n)
1836 struct glyph_string *s;
1837 NativeRectangle *rects;
1838 int n;
1840 XRectangle r;
1842 if (n <= 0)
1843 return 0;
1845 if (s->row->full_width_p)
1847 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1848 r.x = WINDOW_LEFT_EDGE_X (s->w);
1849 r.width = WINDOW_TOTAL_WIDTH (s->w);
1851 /* Unless displaying a mode or menu bar line, which are always
1852 fully visible, clip to the visible part of the row. */
1853 if (s->w->pseudo_window_p)
1854 r.height = s->row->visible_height;
1855 else
1856 r.height = s->height;
1858 else
1860 /* This is a text line that may be partially visible. */
1861 r.x = window_box_left (s->w, s->area);
1862 r.width = window_box_width (s->w, s->area);
1863 r.height = s->row->visible_height;
1866 if (s->clip_head)
1867 if (r.x < s->clip_head->x)
1869 if (r.width >= s->clip_head->x - r.x)
1870 r.width -= s->clip_head->x - r.x;
1871 else
1872 r.width = 0;
1873 r.x = s->clip_head->x;
1875 if (s->clip_tail)
1876 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1878 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1879 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1880 else
1881 r.width = 0;
1884 /* If S draws overlapping rows, it's sufficient to use the top and
1885 bottom of the window for clipping because this glyph string
1886 intentionally draws over other lines. */
1887 if (s->for_overlaps)
1889 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1890 r.height = window_text_bottom_y (s->w) - r.y;
1892 /* Alas, the above simple strategy does not work for the
1893 environments with anti-aliased text: if the same text is
1894 drawn onto the same place multiple times, it gets thicker.
1895 If the overlap we are processing is for the erased cursor, we
1896 take the intersection with the rectagle of the cursor. */
1897 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1899 XRectangle rc, r_save = r;
1901 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1902 rc.y = s->w->phys_cursor.y;
1903 rc.width = s->w->phys_cursor_width;
1904 rc.height = s->w->phys_cursor_height;
1906 x_intersect_rectangles (&r_save, &rc, &r);
1909 else
1911 /* Don't use S->y for clipping because it doesn't take partially
1912 visible lines into account. For example, it can be negative for
1913 partially visible lines at the top of a window. */
1914 if (!s->row->full_width_p
1915 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1916 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1917 else
1918 r.y = max (0, s->row->y);
1920 /* If drawing a tool-bar window, draw it over the internal border
1921 at the top of the window. */
1922 if (WINDOWP (s->f->tool_bar_window)
1923 && s->w == XWINDOW (s->f->tool_bar_window))
1924 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1927 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1929 /* If drawing the cursor, don't let glyph draw outside its
1930 advertised boundaries. Cleartype does this under some circumstances. */
1931 if (s->hl == DRAW_CURSOR)
1933 struct glyph *glyph = s->first_glyph;
1934 int height, max_y;
1936 if (s->x > r.x)
1938 r.width -= s->x - r.x;
1939 r.x = s->x;
1941 r.width = min (r.width, glyph->pixel_width);
1943 /* If r.y is below window bottom, ensure that we still see a cursor. */
1944 height = min (glyph->ascent + glyph->descent,
1945 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1946 max_y = window_text_bottom_y (s->w) - height;
1947 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1948 if (s->ybase - glyph->ascent > max_y)
1950 r.y = max_y;
1951 r.height = height;
1953 else
1955 /* Don't draw cursor glyph taller than our actual glyph. */
1956 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1957 if (height < r.height)
1959 max_y = r.y + r.height;
1960 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1961 r.height = min (max_y - r.y, height);
1966 if (s->row->clip)
1968 XRectangle r_save = r;
1970 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1971 r.width = 0;
1974 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1975 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1977 #ifdef CONVERT_FROM_XRECT
1978 CONVERT_FROM_XRECT (r, *rects);
1979 #else
1980 *rects = r;
1981 #endif
1982 return 1;
1984 else
1986 /* If we are processing overlapping and allowed to return
1987 multiple clipping rectangles, we exclude the row of the glyph
1988 string from the clipping rectangle. This is to avoid drawing
1989 the same text on the environment with anti-aliasing. */
1990 #ifdef CONVERT_FROM_XRECT
1991 XRectangle rs[2];
1992 #else
1993 XRectangle *rs = rects;
1994 #endif
1995 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1997 if (s->for_overlaps & OVERLAPS_PRED)
1999 rs[i] = r;
2000 if (r.y + r.height > row_y)
2002 if (r.y < row_y)
2003 rs[i].height = row_y - r.y;
2004 else
2005 rs[i].height = 0;
2007 i++;
2009 if (s->for_overlaps & OVERLAPS_SUCC)
2011 rs[i] = r;
2012 if (r.y < row_y + s->row->visible_height)
2014 if (r.y + r.height > row_y + s->row->visible_height)
2016 rs[i].y = row_y + s->row->visible_height;
2017 rs[i].height = r.y + r.height - rs[i].y;
2019 else
2020 rs[i].height = 0;
2022 i++;
2025 n = i;
2026 #ifdef CONVERT_FROM_XRECT
2027 for (i = 0; i < n; i++)
2028 CONVERT_FROM_XRECT (rs[i], rects[i]);
2029 #endif
2030 return n;
2034 /* EXPORT:
2035 Return in *NR the clipping rectangle for glyph string S. */
2037 void
2038 get_glyph_string_clip_rect (s, nr)
2039 struct glyph_string *s;
2040 NativeRectangle *nr;
2042 get_glyph_string_clip_rects (s, nr, 1);
2046 /* EXPORT:
2047 Return the position and height of the phys cursor in window W.
2048 Set w->phys_cursor_width to width of phys cursor.
2051 void
2052 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2053 struct window *w;
2054 struct glyph_row *row;
2055 struct glyph *glyph;
2056 int *xp, *yp, *heightp;
2058 struct frame *f = XFRAME (WINDOW_FRAME (w));
2059 int x, y, wd, h, h0, y0;
2061 /* Compute the width of the rectangle to draw. If on a stretch
2062 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2063 rectangle as wide as the glyph, but use a canonical character
2064 width instead. */
2065 wd = glyph->pixel_width - 1;
2066 #ifdef HAVE_NTGUI
2067 wd++; /* Why? */
2068 #endif
2070 x = w->phys_cursor.x;
2071 if (x < 0)
2073 wd += x;
2074 x = 0;
2077 if (glyph->type == STRETCH_GLYPH
2078 && !x_stretch_cursor_p)
2079 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2080 w->phys_cursor_width = wd;
2082 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2084 /* If y is below window bottom, ensure that we still see a cursor. */
2085 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2087 h = max (h0, glyph->ascent + glyph->descent);
2088 h0 = min (h0, glyph->ascent + glyph->descent);
2090 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2091 if (y < y0)
2093 h = max (h - (y0 - y) + 1, h0);
2094 y = y0 - 1;
2096 else
2098 y0 = window_text_bottom_y (w) - h0;
2099 if (y > y0)
2101 h += y - y0;
2102 y = y0;
2106 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2107 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2108 *heightp = h;
2112 * Remember which glyph the mouse is over.
2115 void
2116 remember_mouse_glyph (f, gx, gy, rect)
2117 struct frame *f;
2118 int gx, gy;
2119 NativeRectangle *rect;
2121 Lisp_Object window;
2122 struct window *w;
2123 struct glyph_row *r, *gr, *end_row;
2124 enum window_part part;
2125 enum glyph_row_area area;
2126 int x, y, width, height;
2128 /* Try to determine frame pixel position and size of the glyph under
2129 frame pixel coordinates X/Y on frame F. */
2131 if (!f->glyphs_initialized_p
2132 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2133 NILP (window)))
2135 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2136 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2137 goto virtual_glyph;
2140 w = XWINDOW (window);
2141 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2142 height = WINDOW_FRAME_LINE_HEIGHT (w);
2144 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2145 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2147 if (w->pseudo_window_p)
2149 area = TEXT_AREA;
2150 part = ON_MODE_LINE; /* Don't adjust margin. */
2151 goto text_glyph;
2154 switch (part)
2156 case ON_LEFT_MARGIN:
2157 area = LEFT_MARGIN_AREA;
2158 goto text_glyph;
2160 case ON_RIGHT_MARGIN:
2161 area = RIGHT_MARGIN_AREA;
2162 goto text_glyph;
2164 case ON_HEADER_LINE:
2165 case ON_MODE_LINE:
2166 gr = (part == ON_HEADER_LINE
2167 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2168 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2169 gy = gr->y;
2170 area = TEXT_AREA;
2171 goto text_glyph_row_found;
2173 case ON_TEXT:
2174 area = TEXT_AREA;
2176 text_glyph:
2177 gr = 0; gy = 0;
2178 for (; r <= end_row && r->enabled_p; ++r)
2179 if (r->y + r->height > y)
2181 gr = r; gy = r->y;
2182 break;
2185 text_glyph_row_found:
2186 if (gr && gy <= y)
2188 struct glyph *g = gr->glyphs[area];
2189 struct glyph *end = g + gr->used[area];
2191 height = gr->height;
2192 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2193 if (gx + g->pixel_width > x)
2194 break;
2196 if (g < end)
2198 if (g->type == IMAGE_GLYPH)
2200 /* Don't remember when mouse is over image, as
2201 image may have hot-spots. */
2202 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2203 return;
2205 width = g->pixel_width;
2207 else
2209 /* Use nominal char spacing at end of line. */
2210 x -= gx;
2211 gx += (x / width) * width;
2214 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2215 gx += window_box_left_offset (w, area);
2217 else
2219 /* Use nominal line height at end of window. */
2220 gx = (x / width) * width;
2221 y -= gy;
2222 gy += (y / height) * height;
2224 break;
2226 case ON_LEFT_FRINGE:
2227 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2228 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2229 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2230 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2231 goto row_glyph;
2233 case ON_RIGHT_FRINGE:
2234 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2235 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2236 : window_box_right_offset (w, TEXT_AREA));
2237 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2238 goto row_glyph;
2240 case ON_SCROLL_BAR:
2241 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2243 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2244 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2245 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2246 : 0)));
2247 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2249 row_glyph:
2250 gr = 0, gy = 0;
2251 for (; r <= end_row && r->enabled_p; ++r)
2252 if (r->y + r->height > y)
2254 gr = r; gy = r->y;
2255 break;
2258 if (gr && gy <= y)
2259 height = gr->height;
2260 else
2262 /* Use nominal line height at end of window. */
2263 y -= gy;
2264 gy += (y / height) * height;
2266 break;
2268 default:
2270 virtual_glyph:
2271 /* If there is no glyph under the mouse, then we divide the screen
2272 into a grid of the smallest glyph in the frame, and use that
2273 as our "glyph". */
2275 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2276 round down even for negative values. */
2277 if (gx < 0)
2278 gx -= width - 1;
2279 if (gy < 0)
2280 gy -= height - 1;
2282 gx = (gx / width) * width;
2283 gy = (gy / height) * height;
2285 goto store_rect;
2288 gx += WINDOW_LEFT_EDGE_X (w);
2289 gy += WINDOW_TOP_EDGE_Y (w);
2291 store_rect:
2292 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2294 /* Visible feedback for debugging. */
2295 #if 0
2296 #if HAVE_X_WINDOWS
2297 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2298 f->output_data.x->normal_gc,
2299 gx, gy, width, height);
2300 #endif
2301 #endif
2305 #endif /* HAVE_WINDOW_SYSTEM */
2308 /***********************************************************************
2309 Lisp form evaluation
2310 ***********************************************************************/
2312 /* Error handler for safe_eval and safe_call. */
2314 static Lisp_Object
2315 safe_eval_handler (arg)
2316 Lisp_Object arg;
2318 add_to_log ("Error during redisplay: %s", arg, Qnil);
2319 return Qnil;
2323 /* Evaluate SEXPR and return the result, or nil if something went
2324 wrong. Prevent redisplay during the evaluation. */
2326 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2327 Return the result, or nil if something went wrong. Prevent
2328 redisplay during the evaluation. */
2330 Lisp_Object
2331 safe_call (nargs, args)
2332 int nargs;
2333 Lisp_Object *args;
2335 Lisp_Object val;
2337 if (inhibit_eval_during_redisplay)
2338 val = Qnil;
2339 else
2341 int count = SPECPDL_INDEX ();
2342 struct gcpro gcpro1;
2344 GCPRO1 (args[0]);
2345 gcpro1.nvars = nargs;
2346 specbind (Qinhibit_redisplay, Qt);
2347 /* Use Qt to ensure debugger does not run,
2348 so there is no possibility of wanting to redisplay. */
2349 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2350 safe_eval_handler);
2351 UNGCPRO;
2352 val = unbind_to (count, val);
2355 return val;
2359 /* Call function FN with one argument ARG.
2360 Return the result, or nil if something went wrong. */
2362 Lisp_Object
2363 safe_call1 (fn, arg)
2364 Lisp_Object fn, arg;
2366 Lisp_Object args[2];
2367 args[0] = fn;
2368 args[1] = arg;
2369 return safe_call (2, args);
2372 static Lisp_Object Qeval;
2374 Lisp_Object
2375 safe_eval (Lisp_Object sexpr)
2377 return safe_call1 (Qeval, sexpr);
2380 /* Call function FN with one argument ARG.
2381 Return the result, or nil if something went wrong. */
2383 Lisp_Object
2384 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2386 Lisp_Object args[3];
2387 args[0] = fn;
2388 args[1] = arg1;
2389 args[2] = arg2;
2390 return safe_call (3, args);
2395 /***********************************************************************
2396 Debugging
2397 ***********************************************************************/
2399 #if 0
2401 /* Define CHECK_IT to perform sanity checks on iterators.
2402 This is for debugging. It is too slow to do unconditionally. */
2404 static void
2405 check_it (it)
2406 struct it *it;
2408 if (it->method == GET_FROM_STRING)
2410 xassert (STRINGP (it->string));
2411 xassert (IT_STRING_CHARPOS (*it) >= 0);
2413 else
2415 xassert (IT_STRING_CHARPOS (*it) < 0);
2416 if (it->method == GET_FROM_BUFFER)
2418 /* Check that character and byte positions agree. */
2419 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2423 if (it->dpvec)
2424 xassert (it->current.dpvec_index >= 0);
2425 else
2426 xassert (it->current.dpvec_index < 0);
2429 #define CHECK_IT(IT) check_it ((IT))
2431 #else /* not 0 */
2433 #define CHECK_IT(IT) (void) 0
2435 #endif /* not 0 */
2438 #if GLYPH_DEBUG
2440 /* Check that the window end of window W is what we expect it
2441 to be---the last row in the current matrix displaying text. */
2443 static void
2444 check_window_end (w)
2445 struct window *w;
2447 if (!MINI_WINDOW_P (w)
2448 && !NILP (w->window_end_valid))
2450 struct glyph_row *row;
2451 xassert ((row = MATRIX_ROW (w->current_matrix,
2452 XFASTINT (w->window_end_vpos)),
2453 !row->enabled_p
2454 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2455 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2459 #define CHECK_WINDOW_END(W) check_window_end ((W))
2461 #else /* not GLYPH_DEBUG */
2463 #define CHECK_WINDOW_END(W) (void) 0
2465 #endif /* not GLYPH_DEBUG */
2469 /***********************************************************************
2470 Iterator initialization
2471 ***********************************************************************/
2473 /* Initialize IT for displaying current_buffer in window W, starting
2474 at character position CHARPOS. CHARPOS < 0 means that no buffer
2475 position is specified which is useful when the iterator is assigned
2476 a position later. BYTEPOS is the byte position corresponding to
2477 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2479 If ROW is not null, calls to produce_glyphs with IT as parameter
2480 will produce glyphs in that row.
2482 BASE_FACE_ID is the id of a base face to use. It must be one of
2483 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2484 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2485 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2487 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2488 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2489 will be initialized to use the corresponding mode line glyph row of
2490 the desired matrix of W. */
2492 void
2493 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2494 struct it *it;
2495 struct window *w;
2496 int charpos, bytepos;
2497 struct glyph_row *row;
2498 enum face_id base_face_id;
2500 int highlight_region_p;
2501 enum face_id remapped_base_face_id = base_face_id;
2503 /* Some precondition checks. */
2504 xassert (w != NULL && it != NULL);
2505 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2506 && charpos <= ZV));
2508 /* If face attributes have been changed since the last redisplay,
2509 free realized faces now because they depend on face definitions
2510 that might have changed. Don't free faces while there might be
2511 desired matrices pending which reference these faces. */
2512 if (face_change_count && !inhibit_free_realized_faces)
2514 face_change_count = 0;
2515 free_all_realized_faces (Qnil);
2518 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2519 if (! NILP (Vface_remapping_alist))
2520 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2522 /* Use one of the mode line rows of W's desired matrix if
2523 appropriate. */
2524 if (row == NULL)
2526 if (base_face_id == MODE_LINE_FACE_ID
2527 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2528 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2529 else if (base_face_id == HEADER_LINE_FACE_ID)
2530 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2533 /* Clear IT. */
2534 bzero (it, sizeof *it);
2535 it->current.overlay_string_index = -1;
2536 it->current.dpvec_index = -1;
2537 it->base_face_id = remapped_base_face_id;
2538 it->string = Qnil;
2539 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2541 /* The window in which we iterate over current_buffer: */
2542 XSETWINDOW (it->window, w);
2543 it->w = w;
2544 it->f = XFRAME (w->frame);
2546 /* Extra space between lines (on window systems only). */
2547 if (base_face_id == DEFAULT_FACE_ID
2548 && FRAME_WINDOW_P (it->f))
2550 if (NATNUMP (current_buffer->extra_line_spacing))
2551 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2552 else if (FLOATP (current_buffer->extra_line_spacing))
2553 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2554 * FRAME_LINE_HEIGHT (it->f));
2555 else if (it->f->extra_line_spacing > 0)
2556 it->extra_line_spacing = it->f->extra_line_spacing;
2557 it->max_extra_line_spacing = 0;
2560 /* If realized faces have been removed, e.g. because of face
2561 attribute changes of named faces, recompute them. When running
2562 in batch mode, the face cache of the initial frame is null. If
2563 we happen to get called, make a dummy face cache. */
2564 if (FRAME_FACE_CACHE (it->f) == NULL)
2565 init_frame_faces (it->f);
2566 if (FRAME_FACE_CACHE (it->f)->used == 0)
2567 recompute_basic_faces (it->f);
2569 /* Current value of the `slice', `space-width', and 'height' properties. */
2570 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2571 it->space_width = Qnil;
2572 it->font_height = Qnil;
2573 it->override_ascent = -1;
2575 /* Are control characters displayed as `^C'? */
2576 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2578 /* -1 means everything between a CR and the following line end
2579 is invisible. >0 means lines indented more than this value are
2580 invisible. */
2581 it->selective = (INTEGERP (current_buffer->selective_display)
2582 ? XFASTINT (current_buffer->selective_display)
2583 : (!NILP (current_buffer->selective_display)
2584 ? -1 : 0));
2585 it->selective_display_ellipsis_p
2586 = !NILP (current_buffer->selective_display_ellipses);
2588 /* Display table to use. */
2589 it->dp = window_display_table (w);
2591 /* Are multibyte characters enabled in current_buffer? */
2592 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2594 /* Non-zero if we should highlight the region. */
2595 highlight_region_p
2596 = (!NILP (Vtransient_mark_mode)
2597 && !NILP (current_buffer->mark_active)
2598 && XMARKER (current_buffer->mark)->buffer != 0);
2600 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2601 start and end of a visible region in window IT->w. Set both to
2602 -1 to indicate no region. */
2603 if (highlight_region_p
2604 /* Maybe highlight only in selected window. */
2605 && (/* Either show region everywhere. */
2606 highlight_nonselected_windows
2607 /* Or show region in the selected window. */
2608 || w == XWINDOW (selected_window)
2609 /* Or show the region if we are in the mini-buffer and W is
2610 the window the mini-buffer refers to. */
2611 || (MINI_WINDOW_P (XWINDOW (selected_window))
2612 && WINDOWP (minibuf_selected_window)
2613 && w == XWINDOW (minibuf_selected_window))))
2615 int charpos = marker_position (current_buffer->mark);
2616 it->region_beg_charpos = min (PT, charpos);
2617 it->region_end_charpos = max (PT, charpos);
2619 else
2620 it->region_beg_charpos = it->region_end_charpos = -1;
2622 /* Get the position at which the redisplay_end_trigger hook should
2623 be run, if it is to be run at all. */
2624 if (MARKERP (w->redisplay_end_trigger)
2625 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2626 it->redisplay_end_trigger_charpos
2627 = marker_position (w->redisplay_end_trigger);
2628 else if (INTEGERP (w->redisplay_end_trigger))
2629 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2631 /* Correct bogus values of tab_width. */
2632 it->tab_width = XINT (current_buffer->tab_width);
2633 if (it->tab_width <= 0 || it->tab_width > 1000)
2634 it->tab_width = 8;
2636 /* Are lines in the display truncated? */
2637 it->truncate_lines_p
2638 = (base_face_id != DEFAULT_FACE_ID
2639 || XINT (it->w->hscroll)
2640 || (truncate_partial_width_windows
2641 && !WINDOW_FULL_WIDTH_P (it->w))
2642 || !NILP (current_buffer->truncate_lines));
2644 /* Get dimensions of truncation and continuation glyphs. These are
2645 displayed as fringe bitmaps under X, so we don't need them for such
2646 frames. */
2647 if (!FRAME_WINDOW_P (it->f))
2649 if (it->truncate_lines_p)
2651 /* We will need the truncation glyph. */
2652 xassert (it->glyph_row == NULL);
2653 produce_special_glyphs (it, IT_TRUNCATION);
2654 it->truncation_pixel_width = it->pixel_width;
2656 else
2658 /* We will need the continuation glyph. */
2659 xassert (it->glyph_row == NULL);
2660 produce_special_glyphs (it, IT_CONTINUATION);
2661 it->continuation_pixel_width = it->pixel_width;
2664 /* Reset these values to zero because the produce_special_glyphs
2665 above has changed them. */
2666 it->pixel_width = it->ascent = it->descent = 0;
2667 it->phys_ascent = it->phys_descent = 0;
2670 /* Set this after getting the dimensions of truncation and
2671 continuation glyphs, so that we don't produce glyphs when calling
2672 produce_special_glyphs, above. */
2673 it->glyph_row = row;
2674 it->area = TEXT_AREA;
2676 /* Get the dimensions of the display area. The display area
2677 consists of the visible window area plus a horizontally scrolled
2678 part to the left of the window. All x-values are relative to the
2679 start of this total display area. */
2680 if (base_face_id != DEFAULT_FACE_ID)
2682 /* Mode lines, menu bar in terminal frames. */
2683 it->first_visible_x = 0;
2684 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2686 else
2688 it->first_visible_x
2689 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2690 it->last_visible_x = (it->first_visible_x
2691 + window_box_width (w, TEXT_AREA));
2693 /* If we truncate lines, leave room for the truncator glyph(s) at
2694 the right margin. Otherwise, leave room for the continuation
2695 glyph(s). Truncation and continuation glyphs are not inserted
2696 for window-based redisplay. */
2697 if (!FRAME_WINDOW_P (it->f))
2699 if (it->truncate_lines_p)
2700 it->last_visible_x -= it->truncation_pixel_width;
2701 else
2702 it->last_visible_x -= it->continuation_pixel_width;
2705 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2706 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2709 /* Leave room for a border glyph. */
2710 if (!FRAME_WINDOW_P (it->f)
2711 && !WINDOW_RIGHTMOST_P (it->w))
2712 it->last_visible_x -= 1;
2714 it->last_visible_y = window_text_bottom_y (w);
2716 /* For mode lines and alike, arrange for the first glyph having a
2717 left box line if the face specifies a box. */
2718 if (base_face_id != DEFAULT_FACE_ID)
2720 struct face *face;
2722 it->face_id = remapped_base_face_id;
2724 /* If we have a boxed mode line, make the first character appear
2725 with a left box line. */
2726 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2727 if (face->box != FACE_NO_BOX)
2728 it->start_of_box_run_p = 1;
2731 /* If a buffer position was specified, set the iterator there,
2732 getting overlays and face properties from that position. */
2733 if (charpos >= BUF_BEG (current_buffer))
2735 it->end_charpos = ZV;
2736 it->face_id = -1;
2737 IT_CHARPOS (*it) = charpos;
2739 /* Compute byte position if not specified. */
2740 if (bytepos < charpos)
2741 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2742 else
2743 IT_BYTEPOS (*it) = bytepos;
2745 it->start = it->current;
2747 /* Compute faces etc. */
2748 reseat (it, it->current.pos, 1);
2751 CHECK_IT (it);
2755 /* Initialize IT for the display of window W with window start POS. */
2757 void
2758 start_display (it, w, pos)
2759 struct it *it;
2760 struct window *w;
2761 struct text_pos pos;
2763 struct glyph_row *row;
2764 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2766 row = w->desired_matrix->rows + first_vpos;
2767 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2768 it->first_vpos = first_vpos;
2770 /* Don't reseat to previous visible line start if current start
2771 position is in a string or image. */
2772 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2774 int start_at_line_beg_p;
2775 int first_y = it->current_y;
2777 /* If window start is not at a line start, skip forward to POS to
2778 get the correct continuation lines width. */
2779 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2780 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2781 if (!start_at_line_beg_p)
2783 int new_x;
2785 reseat_at_previous_visible_line_start (it);
2786 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2788 new_x = it->current_x + it->pixel_width;
2790 /* If lines are continued, this line may end in the middle
2791 of a multi-glyph character (e.g. a control character
2792 displayed as \003, or in the middle of an overlay
2793 string). In this case move_it_to above will not have
2794 taken us to the start of the continuation line but to the
2795 end of the continued line. */
2796 if (it->current_x > 0
2797 && !it->truncate_lines_p /* Lines are continued. */
2798 && (/* And glyph doesn't fit on the line. */
2799 new_x > it->last_visible_x
2800 /* Or it fits exactly and we're on a window
2801 system frame. */
2802 || (new_x == it->last_visible_x
2803 && FRAME_WINDOW_P (it->f))))
2805 if (it->current.dpvec_index >= 0
2806 || it->current.overlay_string_index >= 0)
2808 set_iterator_to_next (it, 1);
2809 move_it_in_display_line_to (it, -1, -1, 0);
2812 it->continuation_lines_width += it->current_x;
2815 /* We're starting a new display line, not affected by the
2816 height of the continued line, so clear the appropriate
2817 fields in the iterator structure. */
2818 it->max_ascent = it->max_descent = 0;
2819 it->max_phys_ascent = it->max_phys_descent = 0;
2821 it->current_y = first_y;
2822 it->vpos = 0;
2823 it->current_x = it->hpos = 0;
2827 #if 0 /* Don't assert the following because start_display is sometimes
2828 called intentionally with a window start that is not at a
2829 line start. Please leave this code in as a comment. */
2831 /* Window start should be on a line start, now. */
2832 xassert (it->continuation_lines_width
2833 || IT_CHARPOS (it) == BEGV
2834 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2835 #endif /* 0 */
2839 /* Return 1 if POS is a position in ellipses displayed for invisible
2840 text. W is the window we display, for text property lookup. */
2842 static int
2843 in_ellipses_for_invisible_text_p (pos, w)
2844 struct display_pos *pos;
2845 struct window *w;
2847 Lisp_Object prop, window;
2848 int ellipses_p = 0;
2849 int charpos = CHARPOS (pos->pos);
2851 /* If POS specifies a position in a display vector, this might
2852 be for an ellipsis displayed for invisible text. We won't
2853 get the iterator set up for delivering that ellipsis unless
2854 we make sure that it gets aware of the invisible text. */
2855 if (pos->dpvec_index >= 0
2856 && pos->overlay_string_index < 0
2857 && CHARPOS (pos->string_pos) < 0
2858 && charpos > BEGV
2859 && (XSETWINDOW (window, w),
2860 prop = Fget_char_property (make_number (charpos),
2861 Qinvisible, window),
2862 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2864 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2865 window);
2866 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2869 return ellipses_p;
2873 /* Initialize IT for stepping through current_buffer in window W,
2874 starting at position POS that includes overlay string and display
2875 vector/ control character translation position information. Value
2876 is zero if there are overlay strings with newlines at POS. */
2878 static int
2879 init_from_display_pos (it, w, pos)
2880 struct it *it;
2881 struct window *w;
2882 struct display_pos *pos;
2884 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2885 int i, overlay_strings_with_newlines = 0;
2887 /* If POS specifies a position in a display vector, this might
2888 be for an ellipsis displayed for invisible text. We won't
2889 get the iterator set up for delivering that ellipsis unless
2890 we make sure that it gets aware of the invisible text. */
2891 if (in_ellipses_for_invisible_text_p (pos, w))
2893 --charpos;
2894 bytepos = 0;
2897 /* Keep in mind: the call to reseat in init_iterator skips invisible
2898 text, so we might end up at a position different from POS. This
2899 is only a problem when POS is a row start after a newline and an
2900 overlay starts there with an after-string, and the overlay has an
2901 invisible property. Since we don't skip invisible text in
2902 display_line and elsewhere immediately after consuming the
2903 newline before the row start, such a POS will not be in a string,
2904 but the call to init_iterator below will move us to the
2905 after-string. */
2906 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2908 /* This only scans the current chunk -- it should scan all chunks.
2909 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2910 to 16 in 22.1 to make this a lesser problem. */
2911 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2913 const char *s = SDATA (it->overlay_strings[i]);
2914 const char *e = s + SBYTES (it->overlay_strings[i]);
2916 while (s < e && *s != '\n')
2917 ++s;
2919 if (s < e)
2921 overlay_strings_with_newlines = 1;
2922 break;
2926 /* If position is within an overlay string, set up IT to the right
2927 overlay string. */
2928 if (pos->overlay_string_index >= 0)
2930 int relative_index;
2932 /* If the first overlay string happens to have a `display'
2933 property for an image, the iterator will be set up for that
2934 image, and we have to undo that setup first before we can
2935 correct the overlay string index. */
2936 if (it->method == GET_FROM_IMAGE)
2937 pop_it (it);
2939 /* We already have the first chunk of overlay strings in
2940 IT->overlay_strings. Load more until the one for
2941 pos->overlay_string_index is in IT->overlay_strings. */
2942 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2944 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2945 it->current.overlay_string_index = 0;
2946 while (n--)
2948 load_overlay_strings (it, 0);
2949 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2953 it->current.overlay_string_index = pos->overlay_string_index;
2954 relative_index = (it->current.overlay_string_index
2955 % OVERLAY_STRING_CHUNK_SIZE);
2956 it->string = it->overlay_strings[relative_index];
2957 xassert (STRINGP (it->string));
2958 it->current.string_pos = pos->string_pos;
2959 it->method = GET_FROM_STRING;
2962 #if 0 /* This is bogus because POS not having an overlay string
2963 position does not mean it's after the string. Example: A
2964 line starting with a before-string and initialization of IT
2965 to the previous row's end position. */
2966 else if (it->current.overlay_string_index >= 0)
2968 /* If POS says we're already after an overlay string ending at
2969 POS, make sure to pop the iterator because it will be in
2970 front of that overlay string. When POS is ZV, we've thereby
2971 also ``processed'' overlay strings at ZV. */
2972 while (it->sp)
2973 pop_it (it);
2974 xassert (it->current.overlay_string_index == -1);
2975 xassert (it->method == GET_FROM_BUFFER);
2976 if (CHARPOS (pos->pos) == ZV)
2977 it->overlay_strings_at_end_processed_p = 1;
2979 #endif /* 0 */
2981 if (CHARPOS (pos->string_pos) >= 0)
2983 /* Recorded position is not in an overlay string, but in another
2984 string. This can only be a string from a `display' property.
2985 IT should already be filled with that string. */
2986 it->current.string_pos = pos->string_pos;
2987 xassert (STRINGP (it->string));
2990 /* Restore position in display vector translations, control
2991 character translations or ellipses. */
2992 if (pos->dpvec_index >= 0)
2994 if (it->dpvec == NULL)
2995 get_next_display_element (it);
2996 xassert (it->dpvec && it->current.dpvec_index == 0);
2997 it->current.dpvec_index = pos->dpvec_index;
3000 CHECK_IT (it);
3001 return !overlay_strings_with_newlines;
3005 /* Initialize IT for stepping through current_buffer in window W
3006 starting at ROW->start. */
3008 static void
3009 init_to_row_start (it, w, row)
3010 struct it *it;
3011 struct window *w;
3012 struct glyph_row *row;
3014 init_from_display_pos (it, w, &row->start);
3015 it->start = row->start;
3016 it->continuation_lines_width = row->continuation_lines_width;
3017 CHECK_IT (it);
3021 /* Initialize IT for stepping through current_buffer in window W
3022 starting in the line following ROW, i.e. starting at ROW->end.
3023 Value is zero if there are overlay strings with newlines at ROW's
3024 end position. */
3026 static int
3027 init_to_row_end (it, w, row)
3028 struct it *it;
3029 struct window *w;
3030 struct glyph_row *row;
3032 int success = 0;
3034 if (init_from_display_pos (it, w, &row->end))
3036 if (row->continued_p)
3037 it->continuation_lines_width
3038 = row->continuation_lines_width + row->pixel_width;
3039 CHECK_IT (it);
3040 success = 1;
3043 return success;
3049 /***********************************************************************
3050 Text properties
3051 ***********************************************************************/
3053 /* Called when IT reaches IT->stop_charpos. Handle text property and
3054 overlay changes. Set IT->stop_charpos to the next position where
3055 to stop. */
3057 static void
3058 handle_stop (it)
3059 struct it *it;
3061 enum prop_handled handled;
3062 int handle_overlay_change_p;
3063 struct props *p;
3065 it->dpvec = NULL;
3066 it->current.dpvec_index = -1;
3067 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3068 it->ignore_overlay_strings_at_pos_p = 0;
3070 /* Use face of preceding text for ellipsis (if invisible) */
3071 if (it->selective_display_ellipsis_p)
3072 it->saved_face_id = it->face_id;
3076 handled = HANDLED_NORMALLY;
3078 /* Call text property handlers. */
3079 for (p = it_props; p->handler; ++p)
3081 handled = p->handler (it);
3083 if (handled == HANDLED_RECOMPUTE_PROPS)
3084 break;
3085 else if (handled == HANDLED_RETURN)
3087 /* We still want to show before and after strings from
3088 overlays even if the actual buffer text is replaced. */
3089 if (!handle_overlay_change_p || it->sp > 1)
3090 return;
3091 if (!get_overlay_strings_1 (it, 0, 0))
3092 return;
3093 it->ignore_overlay_strings_at_pos_p = 1;
3094 it->string_from_display_prop_p = 0;
3095 handle_overlay_change_p = 0;
3096 handled = HANDLED_RECOMPUTE_PROPS;
3097 break;
3099 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3100 handle_overlay_change_p = 0;
3103 if (handled != HANDLED_RECOMPUTE_PROPS)
3105 /* Don't check for overlay strings below when set to deliver
3106 characters from a display vector. */
3107 if (it->method == GET_FROM_DISPLAY_VECTOR)
3108 handle_overlay_change_p = 0;
3110 /* Handle overlay changes.
3111 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3112 if it finds overlays. */
3113 if (handle_overlay_change_p)
3114 handled = handle_overlay_change (it);
3117 while (handled == HANDLED_RECOMPUTE_PROPS);
3119 /* Determine where to stop next. */
3120 if (handled == HANDLED_NORMALLY)
3121 compute_stop_pos (it);
3125 /* Compute IT->stop_charpos from text property and overlay change
3126 information for IT's current position. */
3128 static void
3129 compute_stop_pos (it)
3130 struct it *it;
3132 register INTERVAL iv, next_iv;
3133 Lisp_Object object, limit, position;
3135 /* If nowhere else, stop at the end. */
3136 it->stop_charpos = it->end_charpos;
3138 if (STRINGP (it->string))
3140 /* Strings are usually short, so don't limit the search for
3141 properties. */
3142 object = it->string;
3143 limit = Qnil;
3144 position = make_number (IT_STRING_CHARPOS (*it));
3146 else
3148 int charpos;
3150 /* If next overlay change is in front of the current stop pos
3151 (which is IT->end_charpos), stop there. Note: value of
3152 next_overlay_change is point-max if no overlay change
3153 follows. */
3154 charpos = next_overlay_change (IT_CHARPOS (*it));
3155 if (charpos < it->stop_charpos)
3156 it->stop_charpos = charpos;
3158 /* If showing the region, we have to stop at the region
3159 start or end because the face might change there. */
3160 if (it->region_beg_charpos > 0)
3162 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3163 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3164 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3165 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3168 /* Set up variables for computing the stop position from text
3169 property changes. */
3170 XSETBUFFER (object, current_buffer);
3171 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3172 position = make_number (IT_CHARPOS (*it));
3176 /* Get the interval containing IT's position. Value is a null
3177 interval if there isn't such an interval. */
3178 iv = validate_interval_range (object, &position, &position, 0);
3179 if (!NULL_INTERVAL_P (iv))
3181 Lisp_Object values_here[LAST_PROP_IDX];
3182 struct props *p;
3184 /* Get properties here. */
3185 for (p = it_props; p->handler; ++p)
3186 values_here[p->idx] = textget (iv->plist, *p->name);
3188 /* Look for an interval following iv that has different
3189 properties. */
3190 for (next_iv = next_interval (iv);
3191 (!NULL_INTERVAL_P (next_iv)
3192 && (NILP (limit)
3193 || XFASTINT (limit) > next_iv->position));
3194 next_iv = next_interval (next_iv))
3196 for (p = it_props; p->handler; ++p)
3198 Lisp_Object new_value;
3200 new_value = textget (next_iv->plist, *p->name);
3201 if (!EQ (values_here[p->idx], new_value))
3202 break;
3205 if (p->handler)
3206 break;
3209 if (!NULL_INTERVAL_P (next_iv))
3211 if (INTEGERP (limit)
3212 && next_iv->position >= XFASTINT (limit))
3213 /* No text property change up to limit. */
3214 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3215 else
3216 /* Text properties change in next_iv. */
3217 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3221 xassert (STRINGP (it->string)
3222 || (it->stop_charpos >= BEGV
3223 && it->stop_charpos >= IT_CHARPOS (*it)));
3227 /* Return the position of the next overlay change after POS in
3228 current_buffer. Value is point-max if no overlay change
3229 follows. This is like `next-overlay-change' but doesn't use
3230 xmalloc. */
3232 static EMACS_INT
3233 next_overlay_change (pos)
3234 EMACS_INT pos;
3236 int noverlays;
3237 EMACS_INT endpos;
3238 Lisp_Object *overlays;
3239 int i;
3241 /* Get all overlays at the given position. */
3242 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3244 /* If any of these overlays ends before endpos,
3245 use its ending point instead. */
3246 for (i = 0; i < noverlays; ++i)
3248 Lisp_Object oend;
3249 EMACS_INT oendpos;
3251 oend = OVERLAY_END (overlays[i]);
3252 oendpos = OVERLAY_POSITION (oend);
3253 endpos = min (endpos, oendpos);
3256 return endpos;
3261 /***********************************************************************
3262 Fontification
3263 ***********************************************************************/
3265 /* Handle changes in the `fontified' property of the current buffer by
3266 calling hook functions from Qfontification_functions to fontify
3267 regions of text. */
3269 static enum prop_handled
3270 handle_fontified_prop (it)
3271 struct it *it;
3273 Lisp_Object prop, pos;
3274 enum prop_handled handled = HANDLED_NORMALLY;
3276 if (!NILP (Vmemory_full))
3277 return handled;
3279 /* Get the value of the `fontified' property at IT's current buffer
3280 position. (The `fontified' property doesn't have a special
3281 meaning in strings.) If the value is nil, call functions from
3282 Qfontification_functions. */
3283 if (!STRINGP (it->string)
3284 && it->s == NULL
3285 && !NILP (Vfontification_functions)
3286 && !NILP (Vrun_hooks)
3287 && (pos = make_number (IT_CHARPOS (*it)),
3288 prop = Fget_char_property (pos, Qfontified, Qnil),
3289 /* Ignore the special cased nil value always present at EOB since
3290 no amount of fontifying will be able to change it. */
3291 NILP (prop) && IT_CHARPOS (*it) < Z))
3293 int count = SPECPDL_INDEX ();
3294 Lisp_Object val;
3296 val = Vfontification_functions;
3297 specbind (Qfontification_functions, Qnil);
3299 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3300 safe_call1 (val, pos);
3301 else
3303 Lisp_Object globals, fn;
3304 struct gcpro gcpro1, gcpro2;
3306 globals = Qnil;
3307 GCPRO2 (val, globals);
3309 for (; CONSP (val); val = XCDR (val))
3311 fn = XCAR (val);
3313 if (EQ (fn, Qt))
3315 /* A value of t indicates this hook has a local
3316 binding; it means to run the global binding too.
3317 In a global value, t should not occur. If it
3318 does, we must ignore it to avoid an endless
3319 loop. */
3320 for (globals = Fdefault_value (Qfontification_functions);
3321 CONSP (globals);
3322 globals = XCDR (globals))
3324 fn = XCAR (globals);
3325 if (!EQ (fn, Qt))
3326 safe_call1 (fn, pos);
3329 else
3330 safe_call1 (fn, pos);
3333 UNGCPRO;
3336 unbind_to (count, Qnil);
3338 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3339 something. This avoids an endless loop if they failed to
3340 fontify the text for which reason ever. */
3341 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3342 handled = HANDLED_RECOMPUTE_PROPS;
3345 return handled;
3350 /***********************************************************************
3351 Faces
3352 ***********************************************************************/
3354 /* Set up iterator IT from face properties at its current position.
3355 Called from handle_stop. */
3357 static enum prop_handled
3358 handle_face_prop (it)
3359 struct it *it;
3361 int new_face_id;
3362 EMACS_INT next_stop;
3364 if (!STRINGP (it->string))
3366 new_face_id
3367 = face_at_buffer_position (it->w,
3368 IT_CHARPOS (*it),
3369 it->region_beg_charpos,
3370 it->region_end_charpos,
3371 &next_stop,
3372 (IT_CHARPOS (*it)
3373 + TEXT_PROP_DISTANCE_LIMIT),
3376 /* Is this a start of a run of characters with box face?
3377 Caveat: this can be called for a freshly initialized
3378 iterator; face_id is -1 in this case. We know that the new
3379 face will not change until limit, i.e. if the new face has a
3380 box, all characters up to limit will have one. But, as
3381 usual, we don't know whether limit is really the end. */
3382 if (new_face_id != it->face_id)
3384 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3386 /* If new face has a box but old face has not, this is
3387 the start of a run of characters with box, i.e. it has
3388 a shadow on the left side. The value of face_id of the
3389 iterator will be -1 if this is the initial call that gets
3390 the face. In this case, we have to look in front of IT's
3391 position and see whether there is a face != new_face_id. */
3392 it->start_of_box_run_p
3393 = (new_face->box != FACE_NO_BOX
3394 && (it->face_id >= 0
3395 || IT_CHARPOS (*it) == BEG
3396 || new_face_id != face_before_it_pos (it)));
3397 it->face_box_p = new_face->box != FACE_NO_BOX;
3400 else
3402 int base_face_id, bufpos;
3403 int i;
3404 Lisp_Object from_overlay
3405 = (it->current.overlay_string_index >= 0
3406 ? it->string_overlays[it->current.overlay_string_index]
3407 : Qnil);
3409 /* See if we got to this string directly or indirectly from
3410 an overlay property. That includes the before-string or
3411 after-string of an overlay, strings in display properties
3412 provided by an overlay, their text properties, etc.
3414 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3415 if (! NILP (from_overlay))
3416 for (i = it->sp - 1; i >= 0; i--)
3418 if (it->stack[i].current.overlay_string_index >= 0)
3419 from_overlay
3420 = it->string_overlays[it->stack[i].current.overlay_string_index];
3421 else if (! NILP (it->stack[i].from_overlay))
3422 from_overlay = it->stack[i].from_overlay;
3424 if (!NILP (from_overlay))
3425 break;
3428 if (! NILP (from_overlay))
3430 bufpos = IT_CHARPOS (*it);
3431 /* For a string from an overlay, the base face depends
3432 only on text properties and ignores overlays. */
3433 base_face_id
3434 = face_for_overlay_string (it->w,
3435 IT_CHARPOS (*it),
3436 it->region_beg_charpos,
3437 it->region_end_charpos,
3438 &next_stop,
3439 (IT_CHARPOS (*it)
3440 + TEXT_PROP_DISTANCE_LIMIT),
3442 from_overlay);
3444 else
3446 bufpos = 0;
3448 /* For strings from a `display' property, use the face at
3449 IT's current buffer position as the base face to merge
3450 with, so that overlay strings appear in the same face as
3451 surrounding text, unless they specify their own
3452 faces. */
3453 base_face_id = underlying_face_id (it);
3456 new_face_id = face_at_string_position (it->w,
3457 it->string,
3458 IT_STRING_CHARPOS (*it),
3459 bufpos,
3460 it->region_beg_charpos,
3461 it->region_end_charpos,
3462 &next_stop,
3463 base_face_id, 0);
3465 #if 0 /* This shouldn't be neccessary. Let's check it. */
3466 /* If IT is used to display a mode line we would really like to
3467 use the mode line face instead of the frame's default face. */
3468 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3469 && new_face_id == DEFAULT_FACE_ID)
3470 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3471 #endif
3473 /* Is this a start of a run of characters with box? Caveat:
3474 this can be called for a freshly allocated iterator; face_id
3475 is -1 is this case. We know that the new face will not
3476 change until the next check pos, i.e. if the new face has a
3477 box, all characters up to that position will have a
3478 box. But, as usual, we don't know whether that position
3479 is really the end. */
3480 if (new_face_id != it->face_id)
3482 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3483 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3485 /* If new face has a box but old face hasn't, this is the
3486 start of a run of characters with box, i.e. it has a
3487 shadow on the left side. */
3488 it->start_of_box_run_p
3489 = new_face->box && (old_face == NULL || !old_face->box);
3490 it->face_box_p = new_face->box != FACE_NO_BOX;
3494 it->face_id = new_face_id;
3495 return HANDLED_NORMALLY;
3499 /* Return the ID of the face ``underlying'' IT's current position,
3500 which is in a string. If the iterator is associated with a
3501 buffer, return the face at IT's current buffer position.
3502 Otherwise, use the iterator's base_face_id. */
3504 static int
3505 underlying_face_id (it)
3506 struct it *it;
3508 int face_id = it->base_face_id, i;
3510 xassert (STRINGP (it->string));
3512 for (i = it->sp - 1; i >= 0; --i)
3513 if (NILP (it->stack[i].string))
3514 face_id = it->stack[i].face_id;
3516 return face_id;
3520 /* Compute the face one character before or after the current position
3521 of IT. BEFORE_P non-zero means get the face in front of IT's
3522 position. Value is the id of the face. */
3524 static int
3525 face_before_or_after_it_pos (it, before_p)
3526 struct it *it;
3527 int before_p;
3529 int face_id, limit;
3530 EMACS_INT next_check_charpos;
3531 struct text_pos pos;
3533 xassert (it->s == NULL);
3535 if (STRINGP (it->string))
3537 int bufpos, base_face_id;
3539 /* No face change past the end of the string (for the case
3540 we are padding with spaces). No face change before the
3541 string start. */
3542 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3543 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3544 return it->face_id;
3546 /* Set pos to the position before or after IT's current position. */
3547 if (before_p)
3548 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3549 else
3550 /* For composition, we must check the character after the
3551 composition. */
3552 pos = (it->what == IT_COMPOSITION
3553 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3554 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3556 if (it->current.overlay_string_index >= 0)
3557 bufpos = IT_CHARPOS (*it);
3558 else
3559 bufpos = 0;
3561 base_face_id = underlying_face_id (it);
3563 /* Get the face for ASCII, or unibyte. */
3564 face_id = face_at_string_position (it->w,
3565 it->string,
3566 CHARPOS (pos),
3567 bufpos,
3568 it->region_beg_charpos,
3569 it->region_end_charpos,
3570 &next_check_charpos,
3571 base_face_id, 0);
3573 /* Correct the face for charsets different from ASCII. Do it
3574 for the multibyte case only. The face returned above is
3575 suitable for unibyte text if IT->string is unibyte. */
3576 if (STRING_MULTIBYTE (it->string))
3578 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3579 int rest = SBYTES (it->string) - BYTEPOS (pos);
3580 int c, len;
3581 struct face *face = FACE_FROM_ID (it->f, face_id);
3583 c = string_char_and_length (p, rest, &len);
3584 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3587 else
3589 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3590 || (IT_CHARPOS (*it) <= BEGV && before_p))
3591 return it->face_id;
3593 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3594 pos = it->current.pos;
3596 if (before_p)
3597 DEC_TEXT_POS (pos, it->multibyte_p);
3598 else
3600 if (it->what == IT_COMPOSITION)
3601 /* For composition, we must check the position after the
3602 composition. */
3603 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3604 else
3605 INC_TEXT_POS (pos, it->multibyte_p);
3608 /* Determine face for CHARSET_ASCII, or unibyte. */
3609 face_id = face_at_buffer_position (it->w,
3610 CHARPOS (pos),
3611 it->region_beg_charpos,
3612 it->region_end_charpos,
3613 &next_check_charpos,
3614 limit, 0);
3616 /* Correct the face for charsets different from ASCII. Do it
3617 for the multibyte case only. The face returned above is
3618 suitable for unibyte text if current_buffer is unibyte. */
3619 if (it->multibyte_p)
3621 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3622 struct face *face = FACE_FROM_ID (it->f, face_id);
3623 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3627 return face_id;
3632 /***********************************************************************
3633 Invisible text
3634 ***********************************************************************/
3636 /* Set up iterator IT from invisible properties at its current
3637 position. Called from handle_stop. */
3639 static enum prop_handled
3640 handle_invisible_prop (it)
3641 struct it *it;
3643 enum prop_handled handled = HANDLED_NORMALLY;
3645 if (STRINGP (it->string))
3647 extern Lisp_Object Qinvisible;
3648 Lisp_Object prop, end_charpos, limit, charpos;
3650 /* Get the value of the invisible text property at the
3651 current position. Value will be nil if there is no such
3652 property. */
3653 charpos = make_number (IT_STRING_CHARPOS (*it));
3654 prop = Fget_text_property (charpos, Qinvisible, it->string);
3656 if (!NILP (prop)
3657 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3659 handled = HANDLED_RECOMPUTE_PROPS;
3661 /* Get the position at which the next change of the
3662 invisible text property can be found in IT->string.
3663 Value will be nil if the property value is the same for
3664 all the rest of IT->string. */
3665 XSETINT (limit, SCHARS (it->string));
3666 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3667 it->string, limit);
3669 /* Text at current position is invisible. The next
3670 change in the property is at position end_charpos.
3671 Move IT's current position to that position. */
3672 if (INTEGERP (end_charpos)
3673 && XFASTINT (end_charpos) < XFASTINT (limit))
3675 struct text_pos old;
3676 old = it->current.string_pos;
3677 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3678 compute_string_pos (&it->current.string_pos, old, it->string);
3680 else
3682 /* The rest of the string is invisible. If this is an
3683 overlay string, proceed with the next overlay string
3684 or whatever comes and return a character from there. */
3685 if (it->current.overlay_string_index >= 0)
3687 next_overlay_string (it);
3688 /* Don't check for overlay strings when we just
3689 finished processing them. */
3690 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3692 else
3694 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3695 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3700 else
3702 int invis_p;
3703 EMACS_INT newpos, next_stop, start_charpos;
3704 Lisp_Object pos, prop, overlay;
3706 /* First of all, is there invisible text at this position? */
3707 start_charpos = IT_CHARPOS (*it);
3708 pos = make_number (IT_CHARPOS (*it));
3709 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3710 &overlay);
3711 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3713 /* If we are on invisible text, skip over it. */
3714 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3716 /* Record whether we have to display an ellipsis for the
3717 invisible text. */
3718 int display_ellipsis_p = invis_p == 2;
3720 handled = HANDLED_RECOMPUTE_PROPS;
3722 /* Loop skipping over invisible text. The loop is left at
3723 ZV or with IT on the first char being visible again. */
3726 /* Try to skip some invisible text. Return value is the
3727 position reached which can be equal to IT's position
3728 if there is nothing invisible here. This skips both
3729 over invisible text properties and overlays with
3730 invisible property. */
3731 newpos = skip_invisible (IT_CHARPOS (*it),
3732 &next_stop, ZV, it->window);
3734 /* If we skipped nothing at all we weren't at invisible
3735 text in the first place. If everything to the end of
3736 the buffer was skipped, end the loop. */
3737 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3738 invis_p = 0;
3739 else
3741 /* We skipped some characters but not necessarily
3742 all there are. Check if we ended up on visible
3743 text. Fget_char_property returns the property of
3744 the char before the given position, i.e. if we
3745 get invis_p = 0, this means that the char at
3746 newpos is visible. */
3747 pos = make_number (newpos);
3748 prop = Fget_char_property (pos, Qinvisible, it->window);
3749 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3752 /* If we ended up on invisible text, proceed to
3753 skip starting with next_stop. */
3754 if (invis_p)
3755 IT_CHARPOS (*it) = next_stop;
3757 /* If there are adjacent invisible texts, don't lose the
3758 second one's ellipsis. */
3759 if (invis_p == 2)
3760 display_ellipsis_p = 1;
3762 while (invis_p);
3764 /* The position newpos is now either ZV or on visible text. */
3765 IT_CHARPOS (*it) = newpos;
3766 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3768 /* If there are before-strings at the start of invisible
3769 text, and the text is invisible because of a text
3770 property, arrange to show before-strings because 20.x did
3771 it that way. (If the text is invisible because of an
3772 overlay property instead of a text property, this is
3773 already handled in the overlay code.) */
3774 if (NILP (overlay)
3775 && get_overlay_strings (it, start_charpos))
3777 handled = HANDLED_RECOMPUTE_PROPS;
3778 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3780 else if (display_ellipsis_p)
3782 /* Make sure that the glyphs of the ellipsis will get
3783 correct `charpos' values. If we would not update
3784 it->position here, the glyphs would belong to the
3785 last visible character _before_ the invisible
3786 text, which confuses `set_cursor_from_row'.
3788 We use the last invisible position instead of the
3789 first because this way the cursor is always drawn on
3790 the first "." of the ellipsis, whenever PT is inside
3791 the invisible text. Otherwise the cursor would be
3792 placed _after_ the ellipsis when the point is after the
3793 first invisible character. */
3794 if (!STRINGP (it->object))
3796 it->position.charpos = IT_CHARPOS (*it) - 1;
3797 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3799 setup_for_ellipsis (it, 0);
3800 /* Let the ellipsis display before
3801 considering any properties of the following char.
3802 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3803 handled = HANDLED_RETURN;
3808 return handled;
3812 /* Make iterator IT return `...' next.
3813 Replaces LEN characters from buffer. */
3815 static void
3816 setup_for_ellipsis (it, len)
3817 struct it *it;
3818 int len;
3820 /* Use the display table definition for `...'. Invalid glyphs
3821 will be handled by the method returning elements from dpvec. */
3822 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3824 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3825 it->dpvec = v->contents;
3826 it->dpend = v->contents + v->size;
3828 else
3830 /* Default `...'. */
3831 it->dpvec = default_invis_vector;
3832 it->dpend = default_invis_vector + 3;
3835 it->dpvec_char_len = len;
3836 it->current.dpvec_index = 0;
3837 it->dpvec_face_id = -1;
3839 /* Remember the current face id in case glyphs specify faces.
3840 IT's face is restored in set_iterator_to_next.
3841 saved_face_id was set to preceding char's face in handle_stop. */
3842 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3843 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3845 it->method = GET_FROM_DISPLAY_VECTOR;
3846 it->ellipsis_p = 1;
3851 /***********************************************************************
3852 'display' property
3853 ***********************************************************************/
3855 /* Set up iterator IT from `display' property at its current position.
3856 Called from handle_stop.
3857 We return HANDLED_RETURN if some part of the display property
3858 overrides the display of the buffer text itself.
3859 Otherwise we return HANDLED_NORMALLY. */
3861 static enum prop_handled
3862 handle_display_prop (it)
3863 struct it *it;
3865 Lisp_Object prop, object, overlay;
3866 struct text_pos *position;
3867 /* Nonzero if some property replaces the display of the text itself. */
3868 int display_replaced_p = 0;
3870 if (STRINGP (it->string))
3872 object = it->string;
3873 position = &it->current.string_pos;
3875 else
3877 XSETWINDOW (object, it->w);
3878 position = &it->current.pos;
3881 /* Reset those iterator values set from display property values. */
3882 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3883 it->space_width = Qnil;
3884 it->font_height = Qnil;
3885 it->voffset = 0;
3887 /* We don't support recursive `display' properties, i.e. string
3888 values that have a string `display' property, that have a string
3889 `display' property etc. */
3890 if (!it->string_from_display_prop_p)
3891 it->area = TEXT_AREA;
3893 prop = get_char_property_and_overlay (make_number (position->charpos),
3894 Qdisplay, object, &overlay);
3895 if (NILP (prop))
3896 return HANDLED_NORMALLY;
3897 /* Now OVERLAY is the overlay that gave us this property, or nil
3898 if it was a text property. */
3900 if (!STRINGP (it->string))
3901 object = it->w->buffer;
3903 if (CONSP (prop)
3904 /* Simple properties. */
3905 && !EQ (XCAR (prop), Qimage)
3906 && !EQ (XCAR (prop), Qspace)
3907 && !EQ (XCAR (prop), Qwhen)
3908 && !EQ (XCAR (prop), Qslice)
3909 && !EQ (XCAR (prop), Qspace_width)
3910 && !EQ (XCAR (prop), Qheight)
3911 && !EQ (XCAR (prop), Qraise)
3912 /* Marginal area specifications. */
3913 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3914 && !EQ (XCAR (prop), Qleft_fringe)
3915 && !EQ (XCAR (prop), Qright_fringe)
3916 && !NILP (XCAR (prop)))
3918 for (; CONSP (prop); prop = XCDR (prop))
3920 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3921 position, display_replaced_p))
3923 display_replaced_p = 1;
3924 /* If some text in a string is replaced, `position' no
3925 longer points to the position of `object'. */
3926 if (STRINGP (object))
3927 break;
3931 else if (VECTORP (prop))
3933 int i;
3934 for (i = 0; i < ASIZE (prop); ++i)
3935 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3936 position, display_replaced_p))
3938 display_replaced_p = 1;
3939 /* If some text in a string is replaced, `position' no
3940 longer points to the position of `object'. */
3941 if (STRINGP (object))
3942 break;
3945 else
3947 int ret = handle_single_display_spec (it, prop, object, overlay,
3948 position, 0);
3949 if (ret < 0) /* Replaced by "", i.e. nothing. */
3950 return HANDLED_RECOMPUTE_PROPS;
3951 if (ret)
3952 display_replaced_p = 1;
3955 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3959 /* Value is the position of the end of the `display' property starting
3960 at START_POS in OBJECT. */
3962 static struct text_pos
3963 display_prop_end (it, object, start_pos)
3964 struct it *it;
3965 Lisp_Object object;
3966 struct text_pos start_pos;
3968 Lisp_Object end;
3969 struct text_pos end_pos;
3971 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3972 Qdisplay, object, Qnil);
3973 CHARPOS (end_pos) = XFASTINT (end);
3974 if (STRINGP (object))
3975 compute_string_pos (&end_pos, start_pos, it->string);
3976 else
3977 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3979 return end_pos;
3983 /* Set up IT from a single `display' specification PROP. OBJECT
3984 is the object in which the `display' property was found. *POSITION
3985 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3986 means that we previously saw a display specification which already
3987 replaced text display with something else, for example an image;
3988 we ignore such properties after the first one has been processed.
3990 OVERLAY is the overlay this `display' property came from,
3991 or nil if it was a text property.
3993 If PROP is a `space' or `image' specification, and in some other
3994 cases too, set *POSITION to the position where the `display'
3995 property ends.
3997 Value is non-zero if something was found which replaces the display
3998 of buffer or string text. Specifically, the value is -1 if that
3999 "something" is "nothing". */
4001 static int
4002 handle_single_display_spec (it, spec, object, overlay, position,
4003 display_replaced_before_p)
4004 struct it *it;
4005 Lisp_Object spec;
4006 Lisp_Object object;
4007 Lisp_Object overlay;
4008 struct text_pos *position;
4009 int display_replaced_before_p;
4011 Lisp_Object form;
4012 Lisp_Object location, value;
4013 struct text_pos start_pos, save_pos;
4014 int valid_p;
4016 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4017 If the result is non-nil, use VALUE instead of SPEC. */
4018 form = Qt;
4019 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4021 spec = XCDR (spec);
4022 if (!CONSP (spec))
4023 return 0;
4024 form = XCAR (spec);
4025 spec = XCDR (spec);
4028 if (!NILP (form) && !EQ (form, Qt))
4030 int count = SPECPDL_INDEX ();
4031 struct gcpro gcpro1;
4033 /* Bind `object' to the object having the `display' property, a
4034 buffer or string. Bind `position' to the position in the
4035 object where the property was found, and `buffer-position'
4036 to the current position in the buffer. */
4037 specbind (Qobject, object);
4038 specbind (Qposition, make_number (CHARPOS (*position)));
4039 specbind (Qbuffer_position,
4040 make_number (STRINGP (object)
4041 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4042 GCPRO1 (form);
4043 form = safe_eval (form);
4044 UNGCPRO;
4045 unbind_to (count, Qnil);
4048 if (NILP (form))
4049 return 0;
4051 /* Handle `(height HEIGHT)' specifications. */
4052 if (CONSP (spec)
4053 && EQ (XCAR (spec), Qheight)
4054 && CONSP (XCDR (spec)))
4056 if (!FRAME_WINDOW_P (it->f))
4057 return 0;
4059 it->font_height = XCAR (XCDR (spec));
4060 if (!NILP (it->font_height))
4062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4063 int new_height = -1;
4065 if (CONSP (it->font_height)
4066 && (EQ (XCAR (it->font_height), Qplus)
4067 || EQ (XCAR (it->font_height), Qminus))
4068 && CONSP (XCDR (it->font_height))
4069 && INTEGERP (XCAR (XCDR (it->font_height))))
4071 /* `(+ N)' or `(- N)' where N is an integer. */
4072 int steps = XINT (XCAR (XCDR (it->font_height)));
4073 if (EQ (XCAR (it->font_height), Qplus))
4074 steps = - steps;
4075 it->face_id = smaller_face (it->f, it->face_id, steps);
4077 else if (FUNCTIONP (it->font_height))
4079 /* Call function with current height as argument.
4080 Value is the new height. */
4081 Lisp_Object height;
4082 height = safe_call1 (it->font_height,
4083 face->lface[LFACE_HEIGHT_INDEX]);
4084 if (NUMBERP (height))
4085 new_height = XFLOATINT (height);
4087 else if (NUMBERP (it->font_height))
4089 /* Value is a multiple of the canonical char height. */
4090 struct face *face;
4092 face = FACE_FROM_ID (it->f,
4093 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4094 new_height = (XFLOATINT (it->font_height)
4095 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4097 else
4099 /* Evaluate IT->font_height with `height' bound to the
4100 current specified height to get the new height. */
4101 int count = SPECPDL_INDEX ();
4103 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4104 value = safe_eval (it->font_height);
4105 unbind_to (count, Qnil);
4107 if (NUMBERP (value))
4108 new_height = XFLOATINT (value);
4111 if (new_height > 0)
4112 it->face_id = face_with_height (it->f, it->face_id, new_height);
4115 return 0;
4118 /* Handle `(space-width WIDTH)'. */
4119 if (CONSP (spec)
4120 && EQ (XCAR (spec), Qspace_width)
4121 && CONSP (XCDR (spec)))
4123 if (!FRAME_WINDOW_P (it->f))
4124 return 0;
4126 value = XCAR (XCDR (spec));
4127 if (NUMBERP (value) && XFLOATINT (value) > 0)
4128 it->space_width = value;
4130 return 0;
4133 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4134 if (CONSP (spec)
4135 && EQ (XCAR (spec), Qslice))
4137 Lisp_Object tem;
4139 if (!FRAME_WINDOW_P (it->f))
4140 return 0;
4142 if (tem = XCDR (spec), CONSP (tem))
4144 it->slice.x = XCAR (tem);
4145 if (tem = XCDR (tem), CONSP (tem))
4147 it->slice.y = XCAR (tem);
4148 if (tem = XCDR (tem), CONSP (tem))
4150 it->slice.width = XCAR (tem);
4151 if (tem = XCDR (tem), CONSP (tem))
4152 it->slice.height = XCAR (tem);
4157 return 0;
4160 /* Handle `(raise FACTOR)'. */
4161 if (CONSP (spec)
4162 && EQ (XCAR (spec), Qraise)
4163 && CONSP (XCDR (spec)))
4165 if (!FRAME_WINDOW_P (it->f))
4166 return 0;
4168 #ifdef HAVE_WINDOW_SYSTEM
4169 value = XCAR (XCDR (spec));
4170 if (NUMBERP (value))
4172 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4173 it->voffset = - (XFLOATINT (value)
4174 * (FONT_HEIGHT (face->font)));
4176 #endif /* HAVE_WINDOW_SYSTEM */
4178 return 0;
4181 /* Don't handle the other kinds of display specifications
4182 inside a string that we got from a `display' property. */
4183 if (it->string_from_display_prop_p)
4184 return 0;
4186 /* Characters having this form of property are not displayed, so
4187 we have to find the end of the property. */
4188 start_pos = *position;
4189 *position = display_prop_end (it, object, start_pos);
4190 value = Qnil;
4192 /* Stop the scan at that end position--we assume that all
4193 text properties change there. */
4194 it->stop_charpos = position->charpos;
4196 /* Handle `(left-fringe BITMAP [FACE])'
4197 and `(right-fringe BITMAP [FACE])'. */
4198 if (CONSP (spec)
4199 && (EQ (XCAR (spec), Qleft_fringe)
4200 || EQ (XCAR (spec), Qright_fringe))
4201 && CONSP (XCDR (spec)))
4203 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4204 int fringe_bitmap;
4206 if (!FRAME_WINDOW_P (it->f))
4207 /* If we return here, POSITION has been advanced
4208 across the text with this property. */
4209 return 0;
4211 #ifdef HAVE_WINDOW_SYSTEM
4212 value = XCAR (XCDR (spec));
4213 if (!SYMBOLP (value)
4214 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4215 /* If we return here, POSITION has been advanced
4216 across the text with this property. */
4217 return 0;
4219 if (CONSP (XCDR (XCDR (spec))))
4221 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4222 int face_id2 = lookup_derived_face (it->f, face_name,
4223 FRINGE_FACE_ID, 0);
4224 if (face_id2 >= 0)
4225 face_id = face_id2;
4228 /* Save current settings of IT so that we can restore them
4229 when we are finished with the glyph property value. */
4231 save_pos = it->position;
4232 it->position = *position;
4233 push_it (it);
4234 it->position = save_pos;
4236 it->area = TEXT_AREA;
4237 it->what = IT_IMAGE;
4238 it->image_id = -1; /* no image */
4239 it->position = start_pos;
4240 it->object = NILP (object) ? it->w->buffer : object;
4241 it->method = GET_FROM_IMAGE;
4242 it->from_overlay = Qnil;
4243 it->face_id = face_id;
4245 /* Say that we haven't consumed the characters with
4246 `display' property yet. The call to pop_it in
4247 set_iterator_to_next will clean this up. */
4248 *position = start_pos;
4250 if (EQ (XCAR (spec), Qleft_fringe))
4252 it->left_user_fringe_bitmap = fringe_bitmap;
4253 it->left_user_fringe_face_id = face_id;
4255 else
4257 it->right_user_fringe_bitmap = fringe_bitmap;
4258 it->right_user_fringe_face_id = face_id;
4260 #endif /* HAVE_WINDOW_SYSTEM */
4261 return 1;
4264 /* Prepare to handle `((margin left-margin) ...)',
4265 `((margin right-margin) ...)' and `((margin nil) ...)'
4266 prefixes for display specifications. */
4267 location = Qunbound;
4268 if (CONSP (spec) && CONSP (XCAR (spec)))
4270 Lisp_Object tem;
4272 value = XCDR (spec);
4273 if (CONSP (value))
4274 value = XCAR (value);
4276 tem = XCAR (spec);
4277 if (EQ (XCAR (tem), Qmargin)
4278 && (tem = XCDR (tem),
4279 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4280 (NILP (tem)
4281 || EQ (tem, Qleft_margin)
4282 || EQ (tem, Qright_margin))))
4283 location = tem;
4286 if (EQ (location, Qunbound))
4288 location = Qnil;
4289 value = spec;
4292 /* After this point, VALUE is the property after any
4293 margin prefix has been stripped. It must be a string,
4294 an image specification, or `(space ...)'.
4296 LOCATION specifies where to display: `left-margin',
4297 `right-margin' or nil. */
4299 valid_p = (STRINGP (value)
4300 #ifdef HAVE_WINDOW_SYSTEM
4301 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4302 #endif /* not HAVE_WINDOW_SYSTEM */
4303 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4305 if (valid_p && !display_replaced_before_p)
4307 /* Save current settings of IT so that we can restore them
4308 when we are finished with the glyph property value. */
4309 save_pos = it->position;
4310 it->position = *position;
4311 push_it (it);
4312 it->position = save_pos;
4313 it->from_overlay = overlay;
4315 if (NILP (location))
4316 it->area = TEXT_AREA;
4317 else if (EQ (location, Qleft_margin))
4318 it->area = LEFT_MARGIN_AREA;
4319 else
4320 it->area = RIGHT_MARGIN_AREA;
4322 if (STRINGP (value))
4324 if (SCHARS (value) == 0)
4326 pop_it (it);
4327 return -1; /* Replaced by "", i.e. nothing. */
4329 it->string = value;
4330 it->multibyte_p = STRING_MULTIBYTE (it->string);
4331 it->current.overlay_string_index = -1;
4332 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4333 it->end_charpos = it->string_nchars = SCHARS (it->string);
4334 it->method = GET_FROM_STRING;
4335 it->stop_charpos = 0;
4336 it->string_from_display_prop_p = 1;
4337 /* Say that we haven't consumed the characters with
4338 `display' property yet. The call to pop_it in
4339 set_iterator_to_next will clean this up. */
4340 if (BUFFERP (object))
4341 *position = start_pos;
4343 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4345 it->method = GET_FROM_STRETCH;
4346 it->object = value;
4347 *position = it->position = start_pos;
4349 #ifdef HAVE_WINDOW_SYSTEM
4350 else
4352 it->what = IT_IMAGE;
4353 it->image_id = lookup_image (it->f, value);
4354 it->position = start_pos;
4355 it->object = NILP (object) ? it->w->buffer : object;
4356 it->method = GET_FROM_IMAGE;
4358 /* Say that we haven't consumed the characters with
4359 `display' property yet. The call to pop_it in
4360 set_iterator_to_next will clean this up. */
4361 *position = start_pos;
4363 #endif /* HAVE_WINDOW_SYSTEM */
4365 return 1;
4368 /* Invalid property or property not supported. Restore
4369 POSITION to what it was before. */
4370 *position = start_pos;
4371 return 0;
4375 /* Check if SPEC is a display sub-property value whose text should be
4376 treated as intangible. */
4378 static int
4379 single_display_spec_intangible_p (prop)
4380 Lisp_Object prop;
4382 /* Skip over `when FORM'. */
4383 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4385 prop = XCDR (prop);
4386 if (!CONSP (prop))
4387 return 0;
4388 prop = XCDR (prop);
4391 if (STRINGP (prop))
4392 return 1;
4394 if (!CONSP (prop))
4395 return 0;
4397 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4398 we don't need to treat text as intangible. */
4399 if (EQ (XCAR (prop), Qmargin))
4401 prop = XCDR (prop);
4402 if (!CONSP (prop))
4403 return 0;
4405 prop = XCDR (prop);
4406 if (!CONSP (prop)
4407 || EQ (XCAR (prop), Qleft_margin)
4408 || EQ (XCAR (prop), Qright_margin))
4409 return 0;
4412 return (CONSP (prop)
4413 && (EQ (XCAR (prop), Qimage)
4414 || EQ (XCAR (prop), Qspace)));
4418 /* Check if PROP is a display property value whose text should be
4419 treated as intangible. */
4422 display_prop_intangible_p (prop)
4423 Lisp_Object prop;
4425 if (CONSP (prop)
4426 && CONSP (XCAR (prop))
4427 && !EQ (Qmargin, XCAR (XCAR (prop))))
4429 /* A list of sub-properties. */
4430 while (CONSP (prop))
4432 if (single_display_spec_intangible_p (XCAR (prop)))
4433 return 1;
4434 prop = XCDR (prop);
4437 else if (VECTORP (prop))
4439 /* A vector of sub-properties. */
4440 int i;
4441 for (i = 0; i < ASIZE (prop); ++i)
4442 if (single_display_spec_intangible_p (AREF (prop, i)))
4443 return 1;
4445 else
4446 return single_display_spec_intangible_p (prop);
4448 return 0;
4452 /* Return 1 if PROP is a display sub-property value containing STRING. */
4454 static int
4455 single_display_spec_string_p (prop, string)
4456 Lisp_Object prop, string;
4458 if (EQ (string, prop))
4459 return 1;
4461 /* Skip over `when FORM'. */
4462 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4464 prop = XCDR (prop);
4465 if (!CONSP (prop))
4466 return 0;
4467 prop = XCDR (prop);
4470 if (CONSP (prop))
4471 /* Skip over `margin LOCATION'. */
4472 if (EQ (XCAR (prop), Qmargin))
4474 prop = XCDR (prop);
4475 if (!CONSP (prop))
4476 return 0;
4478 prop = XCDR (prop);
4479 if (!CONSP (prop))
4480 return 0;
4483 return CONSP (prop) && EQ (XCAR (prop), string);
4487 /* Return 1 if STRING appears in the `display' property PROP. */
4489 static int
4490 display_prop_string_p (prop, string)
4491 Lisp_Object prop, string;
4493 if (CONSP (prop)
4494 && CONSP (XCAR (prop))
4495 && !EQ (Qmargin, XCAR (XCAR (prop))))
4497 /* A list of sub-properties. */
4498 while (CONSP (prop))
4500 if (single_display_spec_string_p (XCAR (prop), string))
4501 return 1;
4502 prop = XCDR (prop);
4505 else if (VECTORP (prop))
4507 /* A vector of sub-properties. */
4508 int i;
4509 for (i = 0; i < ASIZE (prop); ++i)
4510 if (single_display_spec_string_p (AREF (prop, i), string))
4511 return 1;
4513 else
4514 return single_display_spec_string_p (prop, string);
4516 return 0;
4520 /* Determine from which buffer position in W's buffer STRING comes
4521 from. AROUND_CHARPOS is an approximate position where it could
4522 be from. Value is the buffer position or 0 if it couldn't be
4523 determined.
4525 W's buffer must be current.
4527 This function is necessary because we don't record buffer positions
4528 in glyphs generated from strings (to keep struct glyph small).
4529 This function may only use code that doesn't eval because it is
4530 called asynchronously from note_mouse_highlight. */
4533 string_buffer_position (w, string, around_charpos)
4534 struct window *w;
4535 Lisp_Object string;
4536 int around_charpos;
4538 Lisp_Object limit, prop, pos;
4539 const int MAX_DISTANCE = 1000;
4540 int found = 0;
4542 pos = make_number (around_charpos);
4543 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4544 while (!found && !EQ (pos, limit))
4546 prop = Fget_char_property (pos, Qdisplay, Qnil);
4547 if (!NILP (prop) && display_prop_string_p (prop, string))
4548 found = 1;
4549 else
4550 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4553 if (!found)
4555 pos = make_number (around_charpos);
4556 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4557 while (!found && !EQ (pos, limit))
4559 prop = Fget_char_property (pos, Qdisplay, Qnil);
4560 if (!NILP (prop) && display_prop_string_p (prop, string))
4561 found = 1;
4562 else
4563 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4564 limit);
4568 return found ? XINT (pos) : 0;
4573 /***********************************************************************
4574 `composition' property
4575 ***********************************************************************/
4577 static enum prop_handled
4578 handle_auto_composed_prop (it)
4579 struct it *it;
4581 enum prop_handled handled = HANDLED_NORMALLY;
4583 if (FRAME_WINDOW_P (it->f) && FUNCTIONP (Vauto_composition_function))
4585 Lisp_Object val = Qnil;
4586 EMACS_INT pos, limit = -1;
4588 if (STRINGP (it->string))
4589 pos = IT_STRING_CHARPOS (*it);
4590 else
4591 pos = IT_CHARPOS (*it);
4593 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4594 if (! NILP (val))
4596 Lisp_Object cmp_prop;
4597 EMACS_INT cmp_start, cmp_end;
4599 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4600 &cmp_start, &cmp_end, it->string)
4601 && cmp_start == pos
4602 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4604 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4605 Lisp_Object font_object = LGSTRING_FONT (gstring);
4607 if (! EQ (font_object,
4608 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4609 it->w, it->string)))
4610 /* We must re-compute the composition for the
4611 different font. */
4612 val = Qnil;
4615 if (! NILP (val))
4617 Lisp_Object end;
4619 /* As Fnext_single_char_property_change is very slow, we
4620 limit the search to the current line. */
4621 if (STRINGP (it->string))
4622 limit = SCHARS (it->string);
4623 else
4624 limit = find_next_newline_no_quit (pos, 1);
4625 end = Fnext_single_char_property_change (make_number (pos),
4626 Qauto_composed,
4627 it->string,
4628 make_number (limit));
4630 if (XINT (end) < limit)
4631 /* The current point is auto-composed, but there exist
4632 characters not yet composed beyond the
4633 auto-composed region. There's a possiblity that
4634 the last characters in the region may be newly
4635 composed. */
4636 val = Qnil;
4639 if (NILP (val) && ! STRINGP (it->string))
4641 if (limit < 0)
4642 limit = (STRINGP (it->string) ? SCHARS (it->string)
4643 : find_next_newline_no_quit (pos, 1));
4644 if (pos < limit)
4646 int count = SPECPDL_INDEX ();
4647 Lisp_Object args[5];
4649 limit = font_range (pos, limit, FACE_FROM_ID (it->f, it->face_id),
4650 it->f, it->string);
4651 args[0] = Vauto_composition_function;
4652 specbind (Qauto_composition_function, Qnil);
4653 args[1] = make_number (pos);
4654 args[2] = make_number (limit);
4655 args[3] = it->window;
4656 args[4] = it->string;
4657 safe_call (5, args);
4658 unbind_to (count, Qnil);
4663 return handled;
4666 /* Set up iterator IT from `composition' property at its current
4667 position. Called from handle_stop. */
4669 static enum prop_handled
4670 handle_composition_prop (it)
4671 struct it *it;
4673 Lisp_Object prop, string;
4674 EMACS_INT pos, pos_byte, start, end;
4675 enum prop_handled handled = HANDLED_NORMALLY;
4677 if (STRINGP (it->string))
4679 unsigned char *s;
4681 pos = IT_STRING_CHARPOS (*it);
4682 pos_byte = IT_STRING_BYTEPOS (*it);
4683 string = it->string;
4684 s = SDATA (string) + pos_byte;
4685 it->c = STRING_CHAR (s, 0);
4687 else
4689 pos = IT_CHARPOS (*it);
4690 pos_byte = IT_BYTEPOS (*it);
4691 string = Qnil;
4692 it->c = FETCH_CHAR (pos_byte);
4695 /* If there's a valid composition and point is not inside of the
4696 composition (in the case that the composition is from the current
4697 buffer), draw a glyph composed from the composition components. */
4698 if (find_composition (pos, -1, &start, &end, &prop, string)
4699 && COMPOSITION_VALID_P (start, end, prop)
4700 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4702 int id;
4704 if (start != pos)
4706 if (STRINGP (it->string))
4707 pos_byte = string_char_to_byte (it->string, start);
4708 else
4709 pos_byte = CHAR_TO_BYTE (start);
4711 id = get_composition_id (start, pos_byte, end - start, prop, string);
4713 if (id >= 0)
4715 struct composition *cmp = composition_table[id];
4717 if (cmp->glyph_len == 0)
4719 /* No glyph. */
4720 if (STRINGP (it->string))
4722 IT_STRING_CHARPOS (*it) = end;
4723 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4724 end);
4726 else
4728 IT_CHARPOS (*it) = end;
4729 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4731 return HANDLED_RECOMPUTE_PROPS;
4734 it->stop_charpos = end;
4735 push_it (it);
4737 it->method = GET_FROM_COMPOSITION;
4738 it->cmp_id = id;
4739 it->cmp_len = COMPOSITION_LENGTH (prop);
4740 /* For a terminal, draw only the first (non-TAB) character
4741 of the components. */
4742 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4744 /* FIXME: This doesn't do anything!?! */
4745 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4746 ->key_and_value,
4747 cmp->hash_index * 2);
4749 else
4751 int i;
4753 for (i = 0; i < cmp->glyph_len; i++)
4754 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4755 != '\t')
4756 break;
4758 if (it->c == '\t')
4759 it->c = ' ';
4760 it->len = (STRINGP (it->string)
4761 ? string_char_to_byte (it->string, end)
4762 : CHAR_TO_BYTE (end)) - pos_byte;
4763 handled = HANDLED_RETURN;
4767 return handled;
4772 /***********************************************************************
4773 Overlay strings
4774 ***********************************************************************/
4776 /* The following structure is used to record overlay strings for
4777 later sorting in load_overlay_strings. */
4779 struct overlay_entry
4781 Lisp_Object overlay;
4782 Lisp_Object string;
4783 int priority;
4784 int after_string_p;
4788 /* Set up iterator IT from overlay strings at its current position.
4789 Called from handle_stop. */
4791 static enum prop_handled
4792 handle_overlay_change (it)
4793 struct it *it;
4795 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4796 return HANDLED_RECOMPUTE_PROPS;
4797 else
4798 return HANDLED_NORMALLY;
4802 /* Set up the next overlay string for delivery by IT, if there is an
4803 overlay string to deliver. Called by set_iterator_to_next when the
4804 end of the current overlay string is reached. If there are more
4805 overlay strings to display, IT->string and
4806 IT->current.overlay_string_index are set appropriately here.
4807 Otherwise IT->string is set to nil. */
4809 static void
4810 next_overlay_string (it)
4811 struct it *it;
4813 ++it->current.overlay_string_index;
4814 if (it->current.overlay_string_index == it->n_overlay_strings)
4816 /* No more overlay strings. Restore IT's settings to what
4817 they were before overlay strings were processed, and
4818 continue to deliver from current_buffer. */
4819 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4821 pop_it (it);
4822 xassert (it->sp > 0
4823 || it->method == GET_FROM_COMPOSITION
4824 || (NILP (it->string)
4825 && it->method == GET_FROM_BUFFER
4826 && it->stop_charpos >= BEGV
4827 && it->stop_charpos <= it->end_charpos));
4828 it->current.overlay_string_index = -1;
4829 it->n_overlay_strings = 0;
4831 /* If we're at the end of the buffer, record that we have
4832 processed the overlay strings there already, so that
4833 next_element_from_buffer doesn't try it again. */
4834 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4835 it->overlay_strings_at_end_processed_p = 1;
4837 /* If we have to display `...' for invisible text, set
4838 the iterator up for that. */
4839 if (display_ellipsis_p)
4840 setup_for_ellipsis (it, 0);
4842 else
4844 /* There are more overlay strings to process. If
4845 IT->current.overlay_string_index has advanced to a position
4846 where we must load IT->overlay_strings with more strings, do
4847 it. */
4848 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4850 if (it->current.overlay_string_index && i == 0)
4851 load_overlay_strings (it, 0);
4853 /* Initialize IT to deliver display elements from the overlay
4854 string. */
4855 it->string = it->overlay_strings[i];
4856 it->multibyte_p = STRING_MULTIBYTE (it->string);
4857 SET_TEXT_POS (it->current.string_pos, 0, 0);
4858 it->method = GET_FROM_STRING;
4859 it->stop_charpos = 0;
4862 CHECK_IT (it);
4866 /* Compare two overlay_entry structures E1 and E2. Used as a
4867 comparison function for qsort in load_overlay_strings. Overlay
4868 strings for the same position are sorted so that
4870 1. All after-strings come in front of before-strings, except
4871 when they come from the same overlay.
4873 2. Within after-strings, strings are sorted so that overlay strings
4874 from overlays with higher priorities come first.
4876 2. Within before-strings, strings are sorted so that overlay
4877 strings from overlays with higher priorities come last.
4879 Value is analogous to strcmp. */
4882 static int
4883 compare_overlay_entries (e1, e2)
4884 void *e1, *e2;
4886 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4887 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4888 int result;
4890 if (entry1->after_string_p != entry2->after_string_p)
4892 /* Let after-strings appear in front of before-strings if
4893 they come from different overlays. */
4894 if (EQ (entry1->overlay, entry2->overlay))
4895 result = entry1->after_string_p ? 1 : -1;
4896 else
4897 result = entry1->after_string_p ? -1 : 1;
4899 else if (entry1->after_string_p)
4900 /* After-strings sorted in order of decreasing priority. */
4901 result = entry2->priority - entry1->priority;
4902 else
4903 /* Before-strings sorted in order of increasing priority. */
4904 result = entry1->priority - entry2->priority;
4906 return result;
4910 /* Load the vector IT->overlay_strings with overlay strings from IT's
4911 current buffer position, or from CHARPOS if that is > 0. Set
4912 IT->n_overlays to the total number of overlay strings found.
4914 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4915 a time. On entry into load_overlay_strings,
4916 IT->current.overlay_string_index gives the number of overlay
4917 strings that have already been loaded by previous calls to this
4918 function.
4920 IT->add_overlay_start contains an additional overlay start
4921 position to consider for taking overlay strings from, if non-zero.
4922 This position comes into play when the overlay has an `invisible'
4923 property, and both before and after-strings. When we've skipped to
4924 the end of the overlay, because of its `invisible' property, we
4925 nevertheless want its before-string to appear.
4926 IT->add_overlay_start will contain the overlay start position
4927 in this case.
4929 Overlay strings are sorted so that after-string strings come in
4930 front of before-string strings. Within before and after-strings,
4931 strings are sorted by overlay priority. See also function
4932 compare_overlay_entries. */
4934 static void
4935 load_overlay_strings (it, charpos)
4936 struct it *it;
4937 int charpos;
4939 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4940 Lisp_Object overlay, window, str, invisible;
4941 struct Lisp_Overlay *ov;
4942 int start, end;
4943 int size = 20;
4944 int n = 0, i, j, invis_p;
4945 struct overlay_entry *entries
4946 = (struct overlay_entry *) alloca (size * sizeof *entries);
4948 if (charpos <= 0)
4949 charpos = IT_CHARPOS (*it);
4951 /* Append the overlay string STRING of overlay OVERLAY to vector
4952 `entries' which has size `size' and currently contains `n'
4953 elements. AFTER_P non-zero means STRING is an after-string of
4954 OVERLAY. */
4955 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4956 do \
4958 Lisp_Object priority; \
4960 if (n == size) \
4962 int new_size = 2 * size; \
4963 struct overlay_entry *old = entries; \
4964 entries = \
4965 (struct overlay_entry *) alloca (new_size \
4966 * sizeof *entries); \
4967 bcopy (old, entries, size * sizeof *entries); \
4968 size = new_size; \
4971 entries[n].string = (STRING); \
4972 entries[n].overlay = (OVERLAY); \
4973 priority = Foverlay_get ((OVERLAY), Qpriority); \
4974 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4975 entries[n].after_string_p = (AFTER_P); \
4976 ++n; \
4978 while (0)
4980 /* Process overlay before the overlay center. */
4981 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4983 XSETMISC (overlay, ov);
4984 xassert (OVERLAYP (overlay));
4985 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4986 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4988 if (end < charpos)
4989 break;
4991 /* Skip this overlay if it doesn't start or end at IT's current
4992 position. */
4993 if (end != charpos && start != charpos)
4994 continue;
4996 /* Skip this overlay if it doesn't apply to IT->w. */
4997 window = Foverlay_get (overlay, Qwindow);
4998 if (WINDOWP (window) && XWINDOW (window) != it->w)
4999 continue;
5001 /* If the text ``under'' the overlay is invisible, both before-
5002 and after-strings from this overlay are visible; start and
5003 end position are indistinguishable. */
5004 invisible = Foverlay_get (overlay, Qinvisible);
5005 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5007 /* If overlay has a non-empty before-string, record it. */
5008 if ((start == charpos || (end == charpos && invis_p))
5009 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5010 && SCHARS (str))
5011 RECORD_OVERLAY_STRING (overlay, str, 0);
5013 /* If overlay has a non-empty after-string, record it. */
5014 if ((end == charpos || (start == charpos && invis_p))
5015 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5016 && SCHARS (str))
5017 RECORD_OVERLAY_STRING (overlay, str, 1);
5020 /* Process overlays after the overlay center. */
5021 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5023 XSETMISC (overlay, ov);
5024 xassert (OVERLAYP (overlay));
5025 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5026 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5028 if (start > charpos)
5029 break;
5031 /* Skip this overlay if it doesn't start or end at IT's current
5032 position. */
5033 if (end != charpos && start != charpos)
5034 continue;
5036 /* Skip this overlay if it doesn't apply to IT->w. */
5037 window = Foverlay_get (overlay, Qwindow);
5038 if (WINDOWP (window) && XWINDOW (window) != it->w)
5039 continue;
5041 /* If the text ``under'' the overlay is invisible, it has a zero
5042 dimension, and both before- and after-strings apply. */
5043 invisible = Foverlay_get (overlay, Qinvisible);
5044 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5046 /* If overlay has a non-empty before-string, record it. */
5047 if ((start == charpos || (end == charpos && invis_p))
5048 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5049 && SCHARS (str))
5050 RECORD_OVERLAY_STRING (overlay, str, 0);
5052 /* If overlay has a non-empty after-string, record it. */
5053 if ((end == charpos || (start == charpos && invis_p))
5054 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5055 && SCHARS (str))
5056 RECORD_OVERLAY_STRING (overlay, str, 1);
5059 #undef RECORD_OVERLAY_STRING
5061 /* Sort entries. */
5062 if (n > 1)
5063 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5065 /* Record the total number of strings to process. */
5066 it->n_overlay_strings = n;
5068 /* IT->current.overlay_string_index is the number of overlay strings
5069 that have already been consumed by IT. Copy some of the
5070 remaining overlay strings to IT->overlay_strings. */
5071 i = 0;
5072 j = it->current.overlay_string_index;
5073 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5075 it->overlay_strings[i] = entries[j].string;
5076 it->string_overlays[i++] = entries[j++].overlay;
5079 CHECK_IT (it);
5083 /* Get the first chunk of overlay strings at IT's current buffer
5084 position, or at CHARPOS if that is > 0. Value is non-zero if at
5085 least one overlay string was found. */
5087 static int
5088 get_overlay_strings_1 (it, charpos, compute_stop_p)
5089 struct it *it;
5090 int charpos;
5091 int compute_stop_p;
5093 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5094 process. This fills IT->overlay_strings with strings, and sets
5095 IT->n_overlay_strings to the total number of strings to process.
5096 IT->pos.overlay_string_index has to be set temporarily to zero
5097 because load_overlay_strings needs this; it must be set to -1
5098 when no overlay strings are found because a zero value would
5099 indicate a position in the first overlay string. */
5100 it->current.overlay_string_index = 0;
5101 load_overlay_strings (it, charpos);
5103 /* If we found overlay strings, set up IT to deliver display
5104 elements from the first one. Otherwise set up IT to deliver
5105 from current_buffer. */
5106 if (it->n_overlay_strings)
5108 /* Make sure we know settings in current_buffer, so that we can
5109 restore meaningful values when we're done with the overlay
5110 strings. */
5111 if (compute_stop_p)
5112 compute_stop_pos (it);
5113 xassert (it->face_id >= 0);
5115 /* Save IT's settings. They are restored after all overlay
5116 strings have been processed. */
5117 xassert (!compute_stop_p || it->sp == 0);
5118 push_it (it);
5120 /* Set up IT to deliver display elements from the first overlay
5121 string. */
5122 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5123 it->string = it->overlay_strings[0];
5124 it->from_overlay = Qnil;
5125 it->stop_charpos = 0;
5126 xassert (STRINGP (it->string));
5127 it->end_charpos = SCHARS (it->string);
5128 it->multibyte_p = STRING_MULTIBYTE (it->string);
5129 it->method = GET_FROM_STRING;
5130 return 1;
5133 it->current.overlay_string_index = -1;
5134 return 0;
5137 static int
5138 get_overlay_strings (it, charpos)
5139 struct it *it;
5140 int charpos;
5142 it->string = Qnil;
5143 it->method = GET_FROM_BUFFER;
5145 (void) get_overlay_strings_1 (it, charpos, 1);
5147 CHECK_IT (it);
5149 /* Value is non-zero if we found at least one overlay string. */
5150 return STRINGP (it->string);
5155 /***********************************************************************
5156 Saving and restoring state
5157 ***********************************************************************/
5159 /* Save current settings of IT on IT->stack. Called, for example,
5160 before setting up IT for an overlay string, to be able to restore
5161 IT's settings to what they were after the overlay string has been
5162 processed. */
5164 static void
5165 push_it (it)
5166 struct it *it;
5168 struct iterator_stack_entry *p;
5170 xassert (it->sp < IT_STACK_SIZE);
5171 p = it->stack + it->sp;
5173 p->stop_charpos = it->stop_charpos;
5174 xassert (it->face_id >= 0);
5175 p->face_id = it->face_id;
5176 p->string = it->string;
5177 p->method = it->method;
5178 p->from_overlay = it->from_overlay;
5179 switch (p->method)
5181 case GET_FROM_IMAGE:
5182 p->u.image.object = it->object;
5183 p->u.image.image_id = it->image_id;
5184 p->u.image.slice = it->slice;
5185 break;
5186 case GET_FROM_COMPOSITION:
5187 p->u.comp.object = it->object;
5188 p->u.comp.c = it->c;
5189 p->u.comp.len = it->len;
5190 p->u.comp.cmp_id = it->cmp_id;
5191 p->u.comp.cmp_len = it->cmp_len;
5192 break;
5193 case GET_FROM_STRETCH:
5194 p->u.stretch.object = it->object;
5195 break;
5197 p->position = it->position;
5198 p->current = it->current;
5199 p->end_charpos = it->end_charpos;
5200 p->string_nchars = it->string_nchars;
5201 p->area = it->area;
5202 p->multibyte_p = it->multibyte_p;
5203 p->space_width = it->space_width;
5204 p->font_height = it->font_height;
5205 p->voffset = it->voffset;
5206 p->string_from_display_prop_p = it->string_from_display_prop_p;
5207 p->display_ellipsis_p = 0;
5208 ++it->sp;
5212 /* Restore IT's settings from IT->stack. Called, for example, when no
5213 more overlay strings must be processed, and we return to delivering
5214 display elements from a buffer, or when the end of a string from a
5215 `display' property is reached and we return to delivering display
5216 elements from an overlay string, or from a buffer. */
5218 static void
5219 pop_it (it)
5220 struct it *it;
5222 struct iterator_stack_entry *p;
5224 xassert (it->sp > 0);
5225 --it->sp;
5226 p = it->stack + it->sp;
5227 it->stop_charpos = p->stop_charpos;
5228 it->face_id = p->face_id;
5229 it->current = p->current;
5230 it->position = p->position;
5231 it->string = p->string;
5232 it->from_overlay = p->from_overlay;
5233 if (NILP (it->string))
5234 SET_TEXT_POS (it->current.string_pos, -1, -1);
5235 it->method = p->method;
5236 switch (it->method)
5238 case GET_FROM_IMAGE:
5239 it->image_id = p->u.image.image_id;
5240 it->object = p->u.image.object;
5241 it->slice = p->u.image.slice;
5242 break;
5243 case GET_FROM_COMPOSITION:
5244 it->object = p->u.comp.object;
5245 it->c = p->u.comp.c;
5246 it->len = p->u.comp.len;
5247 it->cmp_id = p->u.comp.cmp_id;
5248 it->cmp_len = p->u.comp.cmp_len;
5249 break;
5250 case GET_FROM_STRETCH:
5251 it->object = p->u.comp.object;
5252 break;
5253 case GET_FROM_BUFFER:
5254 it->object = it->w->buffer;
5255 break;
5256 case GET_FROM_STRING:
5257 it->object = it->string;
5258 break;
5260 it->end_charpos = p->end_charpos;
5261 it->string_nchars = p->string_nchars;
5262 it->area = p->area;
5263 it->multibyte_p = p->multibyte_p;
5264 it->space_width = p->space_width;
5265 it->font_height = p->font_height;
5266 it->voffset = p->voffset;
5267 it->string_from_display_prop_p = p->string_from_display_prop_p;
5272 /***********************************************************************
5273 Moving over lines
5274 ***********************************************************************/
5276 /* Set IT's current position to the previous line start. */
5278 static void
5279 back_to_previous_line_start (it)
5280 struct it *it;
5282 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5283 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5287 /* Move IT to the next line start.
5289 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5290 we skipped over part of the text (as opposed to moving the iterator
5291 continuously over the text). Otherwise, don't change the value
5292 of *SKIPPED_P.
5294 Newlines may come from buffer text, overlay strings, or strings
5295 displayed via the `display' property. That's the reason we can't
5296 simply use find_next_newline_no_quit.
5298 Note that this function may not skip over invisible text that is so
5299 because of text properties and immediately follows a newline. If
5300 it would, function reseat_at_next_visible_line_start, when called
5301 from set_iterator_to_next, would effectively make invisible
5302 characters following a newline part of the wrong glyph row, which
5303 leads to wrong cursor motion. */
5305 static int
5306 forward_to_next_line_start (it, skipped_p)
5307 struct it *it;
5308 int *skipped_p;
5310 int old_selective, newline_found_p, n;
5311 const int MAX_NEWLINE_DISTANCE = 500;
5313 /* If already on a newline, just consume it to avoid unintended
5314 skipping over invisible text below. */
5315 if (it->what == IT_CHARACTER
5316 && it->c == '\n'
5317 && CHARPOS (it->position) == IT_CHARPOS (*it))
5319 set_iterator_to_next (it, 0);
5320 it->c = 0;
5321 return 1;
5324 /* Don't handle selective display in the following. It's (a)
5325 unnecessary because it's done by the caller, and (b) leads to an
5326 infinite recursion because next_element_from_ellipsis indirectly
5327 calls this function. */
5328 old_selective = it->selective;
5329 it->selective = 0;
5331 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5332 from buffer text. */
5333 for (n = newline_found_p = 0;
5334 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5335 n += STRINGP (it->string) ? 0 : 1)
5337 if (!get_next_display_element (it))
5338 return 0;
5339 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5340 set_iterator_to_next (it, 0);
5343 /* If we didn't find a newline near enough, see if we can use a
5344 short-cut. */
5345 if (!newline_found_p)
5347 int start = IT_CHARPOS (*it);
5348 int limit = find_next_newline_no_quit (start, 1);
5349 Lisp_Object pos;
5351 xassert (!STRINGP (it->string));
5353 /* If there isn't any `display' property in sight, and no
5354 overlays, we can just use the position of the newline in
5355 buffer text. */
5356 if (it->stop_charpos >= limit
5357 || ((pos = Fnext_single_property_change (make_number (start),
5358 Qdisplay,
5359 Qnil, make_number (limit)),
5360 NILP (pos))
5361 && next_overlay_change (start) == ZV))
5363 IT_CHARPOS (*it) = limit;
5364 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5365 *skipped_p = newline_found_p = 1;
5367 else
5369 while (get_next_display_element (it)
5370 && !newline_found_p)
5372 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5373 set_iterator_to_next (it, 0);
5378 it->selective = old_selective;
5379 return newline_found_p;
5383 /* Set IT's current position to the previous visible line start. Skip
5384 invisible text that is so either due to text properties or due to
5385 selective display. Caution: this does not change IT->current_x and
5386 IT->hpos. */
5388 static void
5389 back_to_previous_visible_line_start (it)
5390 struct it *it;
5392 while (IT_CHARPOS (*it) > BEGV)
5394 back_to_previous_line_start (it);
5396 if (IT_CHARPOS (*it) <= BEGV)
5397 break;
5399 /* If selective > 0, then lines indented more than that values
5400 are invisible. */
5401 if (it->selective > 0
5402 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5403 (double) it->selective)) /* iftc */
5404 continue;
5406 /* Check the newline before point for invisibility. */
5408 Lisp_Object prop;
5409 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5410 Qinvisible, it->window);
5411 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5412 continue;
5415 if (IT_CHARPOS (*it) <= BEGV)
5416 break;
5419 struct it it2;
5420 int pos;
5421 EMACS_INT beg, end;
5422 Lisp_Object val, overlay;
5424 /* If newline is part of a composition, continue from start of composition */
5425 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5426 && beg < IT_CHARPOS (*it))
5427 goto replaced;
5429 /* If newline is replaced by a display property, find start of overlay
5430 or interval and continue search from that point. */
5431 it2 = *it;
5432 pos = --IT_CHARPOS (it2);
5433 --IT_BYTEPOS (it2);
5434 it2.sp = 0;
5435 if (handle_display_prop (&it2) == HANDLED_RETURN
5436 && !NILP (val = get_char_property_and_overlay
5437 (make_number (pos), Qdisplay, Qnil, &overlay))
5438 && (OVERLAYP (overlay)
5439 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5440 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5441 goto replaced;
5443 /* Newline is not replaced by anything -- so we are done. */
5444 break;
5446 replaced:
5447 if (beg < BEGV)
5448 beg = BEGV;
5449 IT_CHARPOS (*it) = beg;
5450 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5454 it->continuation_lines_width = 0;
5456 xassert (IT_CHARPOS (*it) >= BEGV);
5457 xassert (IT_CHARPOS (*it) == BEGV
5458 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5459 CHECK_IT (it);
5463 /* Reseat iterator IT at the previous visible line start. Skip
5464 invisible text that is so either due to text properties or due to
5465 selective display. At the end, update IT's overlay information,
5466 face information etc. */
5468 void
5469 reseat_at_previous_visible_line_start (it)
5470 struct it *it;
5472 back_to_previous_visible_line_start (it);
5473 reseat (it, it->current.pos, 1);
5474 CHECK_IT (it);
5478 /* Reseat iterator IT on the next visible line start in the current
5479 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5480 preceding the line start. Skip over invisible text that is so
5481 because of selective display. Compute faces, overlays etc at the
5482 new position. Note that this function does not skip over text that
5483 is invisible because of text properties. */
5485 static void
5486 reseat_at_next_visible_line_start (it, on_newline_p)
5487 struct it *it;
5488 int on_newline_p;
5490 int newline_found_p, skipped_p = 0;
5492 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5494 /* Skip over lines that are invisible because they are indented
5495 more than the value of IT->selective. */
5496 if (it->selective > 0)
5497 while (IT_CHARPOS (*it) < ZV
5498 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5499 (double) it->selective)) /* iftc */
5501 xassert (IT_BYTEPOS (*it) == BEGV
5502 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5503 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5506 /* Position on the newline if that's what's requested. */
5507 if (on_newline_p && newline_found_p)
5509 if (STRINGP (it->string))
5511 if (IT_STRING_CHARPOS (*it) > 0)
5513 --IT_STRING_CHARPOS (*it);
5514 --IT_STRING_BYTEPOS (*it);
5517 else if (IT_CHARPOS (*it) > BEGV)
5519 --IT_CHARPOS (*it);
5520 --IT_BYTEPOS (*it);
5521 reseat (it, it->current.pos, 0);
5524 else if (skipped_p)
5525 reseat (it, it->current.pos, 0);
5527 CHECK_IT (it);
5532 /***********************************************************************
5533 Changing an iterator's position
5534 ***********************************************************************/
5536 /* Change IT's current position to POS in current_buffer. If FORCE_P
5537 is non-zero, always check for text properties at the new position.
5538 Otherwise, text properties are only looked up if POS >=
5539 IT->check_charpos of a property. */
5541 static void
5542 reseat (it, pos, force_p)
5543 struct it *it;
5544 struct text_pos pos;
5545 int force_p;
5547 int original_pos = IT_CHARPOS (*it);
5549 reseat_1 (it, pos, 0);
5551 /* Determine where to check text properties. Avoid doing it
5552 where possible because text property lookup is very expensive. */
5553 if (force_p
5554 || CHARPOS (pos) > it->stop_charpos
5555 || CHARPOS (pos) < original_pos)
5556 handle_stop (it);
5558 CHECK_IT (it);
5562 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5563 IT->stop_pos to POS, also. */
5565 static void
5566 reseat_1 (it, pos, set_stop_p)
5567 struct it *it;
5568 struct text_pos pos;
5569 int set_stop_p;
5571 /* Don't call this function when scanning a C string. */
5572 xassert (it->s == NULL);
5574 /* POS must be a reasonable value. */
5575 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5577 it->current.pos = it->position = pos;
5578 it->end_charpos = ZV;
5579 it->dpvec = NULL;
5580 it->current.dpvec_index = -1;
5581 it->current.overlay_string_index = -1;
5582 IT_STRING_CHARPOS (*it) = -1;
5583 IT_STRING_BYTEPOS (*it) = -1;
5584 it->string = Qnil;
5585 it->method = GET_FROM_BUFFER;
5586 it->object = it->w->buffer;
5587 it->area = TEXT_AREA;
5588 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5589 it->sp = 0;
5590 it->string_from_display_prop_p = 0;
5591 it->face_before_selective_p = 0;
5593 if (set_stop_p)
5594 it->stop_charpos = CHARPOS (pos);
5598 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5599 If S is non-null, it is a C string to iterate over. Otherwise,
5600 STRING gives a Lisp string to iterate over.
5602 If PRECISION > 0, don't return more then PRECISION number of
5603 characters from the string.
5605 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5606 characters have been returned. FIELD_WIDTH < 0 means an infinite
5607 field width.
5609 MULTIBYTE = 0 means disable processing of multibyte characters,
5610 MULTIBYTE > 0 means enable it,
5611 MULTIBYTE < 0 means use IT->multibyte_p.
5613 IT must be initialized via a prior call to init_iterator before
5614 calling this function. */
5616 static void
5617 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5618 struct it *it;
5619 unsigned char *s;
5620 Lisp_Object string;
5621 int charpos;
5622 int precision, field_width, multibyte;
5624 /* No region in strings. */
5625 it->region_beg_charpos = it->region_end_charpos = -1;
5627 /* No text property checks performed by default, but see below. */
5628 it->stop_charpos = -1;
5630 /* Set iterator position and end position. */
5631 bzero (&it->current, sizeof it->current);
5632 it->current.overlay_string_index = -1;
5633 it->current.dpvec_index = -1;
5634 xassert (charpos >= 0);
5636 /* If STRING is specified, use its multibyteness, otherwise use the
5637 setting of MULTIBYTE, if specified. */
5638 if (multibyte >= 0)
5639 it->multibyte_p = multibyte > 0;
5641 if (s == NULL)
5643 xassert (STRINGP (string));
5644 it->string = string;
5645 it->s = NULL;
5646 it->end_charpos = it->string_nchars = SCHARS (string);
5647 it->method = GET_FROM_STRING;
5648 it->current.string_pos = string_pos (charpos, string);
5650 else
5652 it->s = s;
5653 it->string = Qnil;
5655 /* Note that we use IT->current.pos, not it->current.string_pos,
5656 for displaying C strings. */
5657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5658 if (it->multibyte_p)
5660 it->current.pos = c_string_pos (charpos, s, 1);
5661 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5663 else
5665 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5666 it->end_charpos = it->string_nchars = strlen (s);
5669 it->method = GET_FROM_C_STRING;
5672 /* PRECISION > 0 means don't return more than PRECISION characters
5673 from the string. */
5674 if (precision > 0 && it->end_charpos - charpos > precision)
5675 it->end_charpos = it->string_nchars = charpos + precision;
5677 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5678 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5679 FIELD_WIDTH < 0 means infinite field width. This is useful for
5680 padding with `-' at the end of a mode line. */
5681 if (field_width < 0)
5682 field_width = INFINITY;
5683 if (field_width > it->end_charpos - charpos)
5684 it->end_charpos = charpos + field_width;
5686 /* Use the standard display table for displaying strings. */
5687 if (DISP_TABLE_P (Vstandard_display_table))
5688 it->dp = XCHAR_TABLE (Vstandard_display_table);
5690 it->stop_charpos = charpos;
5691 CHECK_IT (it);
5696 /***********************************************************************
5697 Iteration
5698 ***********************************************************************/
5700 /* Map enum it_method value to corresponding next_element_from_* function. */
5702 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5704 next_element_from_buffer,
5705 next_element_from_display_vector,
5706 next_element_from_composition,
5707 next_element_from_string,
5708 next_element_from_c_string,
5709 next_element_from_image,
5710 next_element_from_stretch
5713 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5715 /* Load IT's display element fields with information about the next
5716 display element from the current position of IT. Value is zero if
5717 end of buffer (or C string) is reached. */
5719 static struct frame *last_escape_glyph_frame = NULL;
5720 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5721 static int last_escape_glyph_merged_face_id = 0;
5724 get_next_display_element (it)
5725 struct it *it;
5727 /* Non-zero means that we found a display element. Zero means that
5728 we hit the end of what we iterate over. Performance note: the
5729 function pointer `method' used here turns out to be faster than
5730 using a sequence of if-statements. */
5731 int success_p;
5733 get_next:
5734 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5736 if (it->what == IT_CHARACTER)
5738 /* Map via display table or translate control characters.
5739 IT->c, IT->len etc. have been set to the next character by
5740 the function call above. If we have a display table, and it
5741 contains an entry for IT->c, translate it. Don't do this if
5742 IT->c itself comes from a display table, otherwise we could
5743 end up in an infinite recursion. (An alternative could be to
5744 count the recursion depth of this function and signal an
5745 error when a certain maximum depth is reached.) Is it worth
5746 it? */
5747 if (success_p && it->dpvec == NULL)
5749 Lisp_Object dv;
5751 if (it->dp
5752 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5753 VECTORP (dv)))
5755 struct Lisp_Vector *v = XVECTOR (dv);
5757 /* Return the first character from the display table
5758 entry, if not empty. If empty, don't display the
5759 current character. */
5760 if (v->size)
5762 it->dpvec_char_len = it->len;
5763 it->dpvec = v->contents;
5764 it->dpend = v->contents + v->size;
5765 it->current.dpvec_index = 0;
5766 it->dpvec_face_id = -1;
5767 it->saved_face_id = it->face_id;
5768 it->method = GET_FROM_DISPLAY_VECTOR;
5769 it->ellipsis_p = 0;
5771 else
5773 set_iterator_to_next (it, 0);
5775 goto get_next;
5778 /* Translate control characters into `\003' or `^C' form.
5779 Control characters coming from a display table entry are
5780 currently not translated because we use IT->dpvec to hold
5781 the translation. This could easily be changed but I
5782 don't believe that it is worth doing.
5784 If it->multibyte_p is nonzero, non-printable non-ASCII
5785 characters are also translated to octal form.
5787 If it->multibyte_p is zero, eight-bit characters that
5788 don't have corresponding multibyte char code are also
5789 translated to octal form. */
5790 else if ((it->c < ' '
5791 ? (it->area != TEXT_AREA
5792 /* In mode line, treat \n, \t like other crl chars. */
5793 || (it->c != '\t'
5794 && it->glyph_row && it->glyph_row->mode_line_p)
5795 || (it->c != '\n' && it->c != '\t'))
5796 : (it->multibyte_p
5797 ? (!CHAR_PRINTABLE_P (it->c)
5798 || (!NILP (Vnobreak_char_display)
5799 && (it->c == 0xA0 /* NO-BREAK SPACE */
5800 || it->c == 0xAD /* SOFT HYPHEN */)))
5801 : (it->c >= 127
5802 && (! unibyte_display_via_language_environment
5803 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5805 /* IT->c is a control character which must be displayed
5806 either as '\003' or as `^C' where the '\\' and '^'
5807 can be defined in the display table. Fill
5808 IT->ctl_chars with glyphs for what we have to
5809 display. Then, set IT->dpvec to these glyphs. */
5810 Lisp_Object gc;
5811 int ctl_len;
5812 int face_id, lface_id = 0 ;
5813 int escape_glyph;
5815 /* Handle control characters with ^. */
5817 if (it->c < 128 && it->ctl_arrow_p)
5819 int g;
5821 g = '^'; /* default glyph for Control */
5822 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5823 if (it->dp
5824 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5825 && GLYPH_CODE_CHAR_VALID_P (gc))
5827 g = GLYPH_CODE_CHAR (gc);
5828 lface_id = GLYPH_CODE_FACE (gc);
5830 if (lface_id)
5832 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5834 else if (it->f == last_escape_glyph_frame
5835 && it->face_id == last_escape_glyph_face_id)
5837 face_id = last_escape_glyph_merged_face_id;
5839 else
5841 /* Merge the escape-glyph face into the current face. */
5842 face_id = merge_faces (it->f, Qescape_glyph, 0,
5843 it->face_id);
5844 last_escape_glyph_frame = it->f;
5845 last_escape_glyph_face_id = it->face_id;
5846 last_escape_glyph_merged_face_id = face_id;
5849 XSETINT (it->ctl_chars[0], g);
5850 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5851 ctl_len = 2;
5852 goto display_control;
5855 /* Handle non-break space in the mode where it only gets
5856 highlighting. */
5858 if (EQ (Vnobreak_char_display, Qt)
5859 && it->c == 0xA0)
5861 /* Merge the no-break-space face into the current face. */
5862 face_id = merge_faces (it->f, Qnobreak_space, 0,
5863 it->face_id);
5865 it->c = ' ';
5866 XSETINT (it->ctl_chars[0], ' ');
5867 ctl_len = 1;
5868 goto display_control;
5871 /* Handle sequences that start with the "escape glyph". */
5873 /* the default escape glyph is \. */
5874 escape_glyph = '\\';
5876 if (it->dp
5877 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5878 && GLYPH_CODE_CHAR_VALID_P (gc))
5880 escape_glyph = GLYPH_CODE_CHAR (gc);
5881 lface_id = GLYPH_CODE_FACE (gc);
5883 if (lface_id)
5885 /* The display table specified a face.
5886 Merge it into face_id and also into escape_glyph. */
5887 face_id = merge_faces (it->f, Qt, lface_id,
5888 it->face_id);
5890 else if (it->f == last_escape_glyph_frame
5891 && it->face_id == last_escape_glyph_face_id)
5893 face_id = last_escape_glyph_merged_face_id;
5895 else
5897 /* Merge the escape-glyph face into the current face. */
5898 face_id = merge_faces (it->f, Qescape_glyph, 0,
5899 it->face_id);
5900 last_escape_glyph_frame = it->f;
5901 last_escape_glyph_face_id = it->face_id;
5902 last_escape_glyph_merged_face_id = face_id;
5905 /* Handle soft hyphens in the mode where they only get
5906 highlighting. */
5908 if (EQ (Vnobreak_char_display, Qt)
5909 && it->c == 0xAD)
5911 it->c = '-';
5912 XSETINT (it->ctl_chars[0], '-');
5913 ctl_len = 1;
5914 goto display_control;
5917 /* Handle non-break space and soft hyphen
5918 with the escape glyph. */
5920 if (it->c == 0xA0 || it->c == 0xAD)
5922 XSETINT (it->ctl_chars[0], escape_glyph);
5923 it->c = (it->c == 0xA0 ? ' ' : '-');
5924 XSETINT (it->ctl_chars[1], it->c);
5925 ctl_len = 2;
5926 goto display_control;
5930 unsigned char str[MAX_MULTIBYTE_LENGTH];
5931 int len;
5932 int i;
5934 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5935 if (CHAR_BYTE8_P (it->c))
5937 str[0] = CHAR_TO_BYTE8 (it->c);
5938 len = 1;
5940 else if (it->c < 256)
5942 str[0] = it->c;
5943 len = 1;
5945 else
5947 /* It's an invalid character, which shouldn't
5948 happen actually, but due to bugs it may
5949 happen. Let's print the char as is, there's
5950 not much meaningful we can do with it. */
5951 str[0] = it->c;
5952 str[1] = it->c >> 8;
5953 str[2] = it->c >> 16;
5954 str[3] = it->c >> 24;
5955 len = 4;
5958 for (i = 0; i < len; i++)
5960 int g;
5961 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5962 /* Insert three more glyphs into IT->ctl_chars for
5963 the octal display of the character. */
5964 g = ((str[i] >> 6) & 7) + '0';
5965 XSETINT (it->ctl_chars[i * 4 + 1], g);
5966 g = ((str[i] >> 3) & 7) + '0';
5967 XSETINT (it->ctl_chars[i * 4 + 2], g);
5968 g = (str[i] & 7) + '0';
5969 XSETINT (it->ctl_chars[i * 4 + 3], g);
5971 ctl_len = len * 4;
5974 display_control:
5975 /* Set up IT->dpvec and return first character from it. */
5976 it->dpvec_char_len = it->len;
5977 it->dpvec = it->ctl_chars;
5978 it->dpend = it->dpvec + ctl_len;
5979 it->current.dpvec_index = 0;
5980 it->dpvec_face_id = face_id;
5981 it->saved_face_id = it->face_id;
5982 it->method = GET_FROM_DISPLAY_VECTOR;
5983 it->ellipsis_p = 0;
5984 goto get_next;
5989 /* Adjust face id for a multibyte character. There are no multibyte
5990 character in unibyte text. */
5991 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5992 && it->multibyte_p
5993 && success_p
5994 && FRAME_WINDOW_P (it->f))
5996 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5997 int pos = (it->s ? -1
5998 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5999 : IT_CHARPOS (*it));
6001 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6004 /* Is this character the last one of a run of characters with
6005 box? If yes, set IT->end_of_box_run_p to 1. */
6006 if (it->face_box_p
6007 && it->s == NULL)
6009 int face_id;
6010 struct face *face;
6012 it->end_of_box_run_p
6013 = ((face_id = face_after_it_pos (it),
6014 face_id != it->face_id)
6015 && (face = FACE_FROM_ID (it->f, face_id),
6016 face->box == FACE_NO_BOX));
6019 /* Value is 0 if end of buffer or string reached. */
6020 return success_p;
6024 /* Move IT to the next display element.
6026 RESEAT_P non-zero means if called on a newline in buffer text,
6027 skip to the next visible line start.
6029 Functions get_next_display_element and set_iterator_to_next are
6030 separate because I find this arrangement easier to handle than a
6031 get_next_display_element function that also increments IT's
6032 position. The way it is we can first look at an iterator's current
6033 display element, decide whether it fits on a line, and if it does,
6034 increment the iterator position. The other way around we probably
6035 would either need a flag indicating whether the iterator has to be
6036 incremented the next time, or we would have to implement a
6037 decrement position function which would not be easy to write. */
6039 void
6040 set_iterator_to_next (it, reseat_p)
6041 struct it *it;
6042 int reseat_p;
6044 /* Reset flags indicating start and end of a sequence of characters
6045 with box. Reset them at the start of this function because
6046 moving the iterator to a new position might set them. */
6047 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6049 switch (it->method)
6051 case GET_FROM_BUFFER:
6052 /* The current display element of IT is a character from
6053 current_buffer. Advance in the buffer, and maybe skip over
6054 invisible lines that are so because of selective display. */
6055 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6056 reseat_at_next_visible_line_start (it, 0);
6057 else
6059 xassert (it->len != 0);
6060 IT_BYTEPOS (*it) += it->len;
6061 IT_CHARPOS (*it) += 1;
6062 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6064 break;
6066 case GET_FROM_COMPOSITION:
6067 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6068 xassert (it->sp > 0);
6069 pop_it (it);
6070 if (it->method == GET_FROM_STRING)
6072 IT_STRING_BYTEPOS (*it) += it->len;
6073 IT_STRING_CHARPOS (*it) += it->cmp_len;
6074 goto consider_string_end;
6076 else if (it->method == GET_FROM_BUFFER)
6078 IT_BYTEPOS (*it) += it->len;
6079 IT_CHARPOS (*it) += it->cmp_len;
6081 break;
6083 case GET_FROM_C_STRING:
6084 /* Current display element of IT is from a C string. */
6085 IT_BYTEPOS (*it) += it->len;
6086 IT_CHARPOS (*it) += 1;
6087 break;
6089 case GET_FROM_DISPLAY_VECTOR:
6090 /* Current display element of IT is from a display table entry.
6091 Advance in the display table definition. Reset it to null if
6092 end reached, and continue with characters from buffers/
6093 strings. */
6094 ++it->current.dpvec_index;
6096 /* Restore face of the iterator to what they were before the
6097 display vector entry (these entries may contain faces). */
6098 it->face_id = it->saved_face_id;
6100 if (it->dpvec + it->current.dpvec_index == it->dpend)
6102 int recheck_faces = it->ellipsis_p;
6104 if (it->s)
6105 it->method = GET_FROM_C_STRING;
6106 else if (STRINGP (it->string))
6107 it->method = GET_FROM_STRING;
6108 else
6110 it->method = GET_FROM_BUFFER;
6111 it->object = it->w->buffer;
6114 it->dpvec = NULL;
6115 it->current.dpvec_index = -1;
6117 /* Skip over characters which were displayed via IT->dpvec. */
6118 if (it->dpvec_char_len < 0)
6119 reseat_at_next_visible_line_start (it, 1);
6120 else if (it->dpvec_char_len > 0)
6122 if (it->method == GET_FROM_STRING
6123 && it->n_overlay_strings > 0)
6124 it->ignore_overlay_strings_at_pos_p = 1;
6125 it->len = it->dpvec_char_len;
6126 set_iterator_to_next (it, reseat_p);
6129 /* Maybe recheck faces after display vector */
6130 if (recheck_faces)
6131 it->stop_charpos = IT_CHARPOS (*it);
6133 break;
6135 case GET_FROM_STRING:
6136 /* Current display element is a character from a Lisp string. */
6137 xassert (it->s == NULL && STRINGP (it->string));
6138 IT_STRING_BYTEPOS (*it) += it->len;
6139 IT_STRING_CHARPOS (*it) += 1;
6141 consider_string_end:
6143 if (it->current.overlay_string_index >= 0)
6145 /* IT->string is an overlay string. Advance to the
6146 next, if there is one. */
6147 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6148 next_overlay_string (it);
6150 else
6152 /* IT->string is not an overlay string. If we reached
6153 its end, and there is something on IT->stack, proceed
6154 with what is on the stack. This can be either another
6155 string, this time an overlay string, or a buffer. */
6156 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6157 && it->sp > 0)
6159 pop_it (it);
6160 if (it->method == GET_FROM_STRING)
6161 goto consider_string_end;
6164 break;
6166 case GET_FROM_IMAGE:
6167 case GET_FROM_STRETCH:
6168 /* The position etc with which we have to proceed are on
6169 the stack. The position may be at the end of a string,
6170 if the `display' property takes up the whole string. */
6171 xassert (it->sp > 0);
6172 pop_it (it);
6173 if (it->method == GET_FROM_STRING)
6174 goto consider_string_end;
6175 break;
6177 default:
6178 /* There are no other methods defined, so this should be a bug. */
6179 abort ();
6182 xassert (it->method != GET_FROM_STRING
6183 || (STRINGP (it->string)
6184 && IT_STRING_CHARPOS (*it) >= 0));
6187 /* Load IT's display element fields with information about the next
6188 display element which comes from a display table entry or from the
6189 result of translating a control character to one of the forms `^C'
6190 or `\003'.
6192 IT->dpvec holds the glyphs to return as characters.
6193 IT->saved_face_id holds the face id before the display vector--
6194 it is restored into IT->face_idin set_iterator_to_next. */
6196 static int
6197 next_element_from_display_vector (it)
6198 struct it *it;
6200 Lisp_Object gc;
6202 /* Precondition. */
6203 xassert (it->dpvec && it->current.dpvec_index >= 0);
6205 it->face_id = it->saved_face_id;
6207 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6208 That seemed totally bogus - so I changed it... */
6210 if ((gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc))
6211 && GLYPH_CODE_CHAR_VALID_P (gc))
6213 it->c = GLYPH_CODE_CHAR (gc);
6214 it->len = CHAR_BYTES (it->c);
6216 /* The entry may contain a face id to use. Such a face id is
6217 the id of a Lisp face, not a realized face. A face id of
6218 zero means no face is specified. */
6219 if (it->dpvec_face_id >= 0)
6220 it->face_id = it->dpvec_face_id;
6221 else
6223 int lface_id = GLYPH_CODE_FACE (gc);
6224 if (lface_id > 0)
6225 it->face_id = merge_faces (it->f, Qt, lface_id,
6226 it->saved_face_id);
6229 else
6230 /* Display table entry is invalid. Return a space. */
6231 it->c = ' ', it->len = 1;
6233 /* Don't change position and object of the iterator here. They are
6234 still the values of the character that had this display table
6235 entry or was translated, and that's what we want. */
6236 it->what = IT_CHARACTER;
6237 return 1;
6241 /* Load IT with the next display element from Lisp string IT->string.
6242 IT->current.string_pos is the current position within the string.
6243 If IT->current.overlay_string_index >= 0, the Lisp string is an
6244 overlay string. */
6246 static int
6247 next_element_from_string (it)
6248 struct it *it;
6250 struct text_pos position;
6252 xassert (STRINGP (it->string));
6253 xassert (IT_STRING_CHARPOS (*it) >= 0);
6254 position = it->current.string_pos;
6256 /* Time to check for invisible text? */
6257 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6258 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6260 handle_stop (it);
6262 /* Since a handler may have changed IT->method, we must
6263 recurse here. */
6264 return GET_NEXT_DISPLAY_ELEMENT (it);
6267 if (it->current.overlay_string_index >= 0)
6269 /* Get the next character from an overlay string. In overlay
6270 strings, There is no field width or padding with spaces to
6271 do. */
6272 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6274 it->what = IT_EOB;
6275 return 0;
6277 else if (STRING_MULTIBYTE (it->string))
6279 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6280 const unsigned char *s = (SDATA (it->string)
6281 + IT_STRING_BYTEPOS (*it));
6282 it->c = string_char_and_length (s, remaining, &it->len);
6284 else
6286 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6287 it->len = 1;
6290 else
6292 /* Get the next character from a Lisp string that is not an
6293 overlay string. Such strings come from the mode line, for
6294 example. We may have to pad with spaces, or truncate the
6295 string. See also next_element_from_c_string. */
6296 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6298 it->what = IT_EOB;
6299 return 0;
6301 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6303 /* Pad with spaces. */
6304 it->c = ' ', it->len = 1;
6305 CHARPOS (position) = BYTEPOS (position) = -1;
6307 else if (STRING_MULTIBYTE (it->string))
6309 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6310 const unsigned char *s = (SDATA (it->string)
6311 + IT_STRING_BYTEPOS (*it));
6312 it->c = string_char_and_length (s, maxlen, &it->len);
6314 else
6316 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6317 it->len = 1;
6321 /* Record what we have and where it came from. */
6322 it->what = IT_CHARACTER;
6323 it->object = it->string;
6324 it->position = position;
6325 return 1;
6329 /* Load IT with next display element from C string IT->s.
6330 IT->string_nchars is the maximum number of characters to return
6331 from the string. IT->end_charpos may be greater than
6332 IT->string_nchars when this function is called, in which case we
6333 may have to return padding spaces. Value is zero if end of string
6334 reached, including padding spaces. */
6336 static int
6337 next_element_from_c_string (it)
6338 struct it *it;
6340 int success_p = 1;
6342 xassert (it->s);
6343 it->what = IT_CHARACTER;
6344 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6345 it->object = Qnil;
6347 /* IT's position can be greater IT->string_nchars in case a field
6348 width or precision has been specified when the iterator was
6349 initialized. */
6350 if (IT_CHARPOS (*it) >= it->end_charpos)
6352 /* End of the game. */
6353 it->what = IT_EOB;
6354 success_p = 0;
6356 else if (IT_CHARPOS (*it) >= it->string_nchars)
6358 /* Pad with spaces. */
6359 it->c = ' ', it->len = 1;
6360 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6362 else if (it->multibyte_p)
6364 /* Implementation note: The calls to strlen apparently aren't a
6365 performance problem because there is no noticeable performance
6366 difference between Emacs running in unibyte or multibyte mode. */
6367 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6368 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6369 maxlen, &it->len);
6371 else
6372 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6374 return success_p;
6378 /* Set up IT to return characters from an ellipsis, if appropriate.
6379 The definition of the ellipsis glyphs may come from a display table
6380 entry. This function Fills IT with the first glyph from the
6381 ellipsis if an ellipsis is to be displayed. */
6383 static int
6384 next_element_from_ellipsis (it)
6385 struct it *it;
6387 if (it->selective_display_ellipsis_p)
6388 setup_for_ellipsis (it, it->len);
6389 else
6391 /* The face at the current position may be different from the
6392 face we find after the invisible text. Remember what it
6393 was in IT->saved_face_id, and signal that it's there by
6394 setting face_before_selective_p. */
6395 it->saved_face_id = it->face_id;
6396 it->method = GET_FROM_BUFFER;
6397 it->object = it->w->buffer;
6398 reseat_at_next_visible_line_start (it, 1);
6399 it->face_before_selective_p = 1;
6402 return GET_NEXT_DISPLAY_ELEMENT (it);
6406 /* Deliver an image display element. The iterator IT is already
6407 filled with image information (done in handle_display_prop). Value
6408 is always 1. */
6411 static int
6412 next_element_from_image (it)
6413 struct it *it;
6415 it->what = IT_IMAGE;
6416 return 1;
6420 /* Fill iterator IT with next display element from a stretch glyph
6421 property. IT->object is the value of the text property. Value is
6422 always 1. */
6424 static int
6425 next_element_from_stretch (it)
6426 struct it *it;
6428 it->what = IT_STRETCH;
6429 return 1;
6433 /* Load IT with the next display element from current_buffer. Value
6434 is zero if end of buffer reached. IT->stop_charpos is the next
6435 position at which to stop and check for text properties or buffer
6436 end. */
6438 static int
6439 next_element_from_buffer (it)
6440 struct it *it;
6442 int success_p = 1;
6444 /* Check this assumption, otherwise, we would never enter the
6445 if-statement, below. */
6446 xassert (IT_CHARPOS (*it) >= BEGV
6447 && IT_CHARPOS (*it) <= it->stop_charpos);
6449 if (IT_CHARPOS (*it) >= it->stop_charpos)
6451 if (IT_CHARPOS (*it) >= it->end_charpos)
6453 int overlay_strings_follow_p;
6455 /* End of the game, except when overlay strings follow that
6456 haven't been returned yet. */
6457 if (it->overlay_strings_at_end_processed_p)
6458 overlay_strings_follow_p = 0;
6459 else
6461 it->overlay_strings_at_end_processed_p = 1;
6462 overlay_strings_follow_p = get_overlay_strings (it, 0);
6465 if (overlay_strings_follow_p)
6466 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6467 else
6469 it->what = IT_EOB;
6470 it->position = it->current.pos;
6471 success_p = 0;
6474 else
6476 handle_stop (it);
6477 return GET_NEXT_DISPLAY_ELEMENT (it);
6480 else
6482 /* No face changes, overlays etc. in sight, so just return a
6483 character from current_buffer. */
6484 unsigned char *p;
6486 /* Maybe run the redisplay end trigger hook. Performance note:
6487 This doesn't seem to cost measurable time. */
6488 if (it->redisplay_end_trigger_charpos
6489 && it->glyph_row
6490 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6491 run_redisplay_end_trigger_hook (it);
6493 /* Get the next character, maybe multibyte. */
6494 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6495 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6497 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6498 - IT_BYTEPOS (*it));
6499 it->c = string_char_and_length (p, maxlen, &it->len);
6501 else
6502 it->c = *p, it->len = 1;
6504 /* Record what we have and where it came from. */
6505 it->what = IT_CHARACTER;
6506 it->object = it->w->buffer;
6507 it->position = it->current.pos;
6509 /* Normally we return the character found above, except when we
6510 really want to return an ellipsis for selective display. */
6511 if (it->selective)
6513 if (it->c == '\n')
6515 /* A value of selective > 0 means hide lines indented more
6516 than that number of columns. */
6517 if (it->selective > 0
6518 && IT_CHARPOS (*it) + 1 < ZV
6519 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6520 IT_BYTEPOS (*it) + 1,
6521 (double) it->selective)) /* iftc */
6523 success_p = next_element_from_ellipsis (it);
6524 it->dpvec_char_len = -1;
6527 else if (it->c == '\r' && it->selective == -1)
6529 /* A value of selective == -1 means that everything from the
6530 CR to the end of the line is invisible, with maybe an
6531 ellipsis displayed for it. */
6532 success_p = next_element_from_ellipsis (it);
6533 it->dpvec_char_len = -1;
6538 /* Value is zero if end of buffer reached. */
6539 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6540 return success_p;
6544 /* Run the redisplay end trigger hook for IT. */
6546 static void
6547 run_redisplay_end_trigger_hook (it)
6548 struct it *it;
6550 Lisp_Object args[3];
6552 /* IT->glyph_row should be non-null, i.e. we should be actually
6553 displaying something, or otherwise we should not run the hook. */
6554 xassert (it->glyph_row);
6556 /* Set up hook arguments. */
6557 args[0] = Qredisplay_end_trigger_functions;
6558 args[1] = it->window;
6559 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6560 it->redisplay_end_trigger_charpos = 0;
6562 /* Since we are *trying* to run these functions, don't try to run
6563 them again, even if they get an error. */
6564 it->w->redisplay_end_trigger = Qnil;
6565 Frun_hook_with_args (3, args);
6567 /* Notice if it changed the face of the character we are on. */
6568 handle_face_prop (it);
6572 /* Deliver a composition display element. The iterator IT is already
6573 filled with composition information (done in
6574 handle_composition_prop). Value is always 1. */
6576 static int
6577 next_element_from_composition (it)
6578 struct it *it;
6580 it->what = IT_COMPOSITION;
6581 it->position = (STRINGP (it->string)
6582 ? it->current.string_pos
6583 : it->current.pos);
6584 if (STRINGP (it->string))
6585 it->object = it->string;
6586 else
6587 it->object = it->w->buffer;
6588 return 1;
6593 /***********************************************************************
6594 Moving an iterator without producing glyphs
6595 ***********************************************************************/
6597 /* Check if iterator is at a position corresponding to a valid buffer
6598 position after some move_it_ call. */
6600 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6601 ((it)->method == GET_FROM_STRING \
6602 ? IT_STRING_CHARPOS (*it) == 0 \
6603 : 1)
6606 /* Move iterator IT to a specified buffer or X position within one
6607 line on the display without producing glyphs.
6609 OP should be a bit mask including some or all of these bits:
6610 MOVE_TO_X: Stop on reaching x-position TO_X.
6611 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6612 Regardless of OP's value, stop in reaching the end of the display line.
6614 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6615 This means, in particular, that TO_X includes window's horizontal
6616 scroll amount.
6618 The return value has several possible values that
6619 say what condition caused the scan to stop:
6621 MOVE_POS_MATCH_OR_ZV
6622 - when TO_POS or ZV was reached.
6624 MOVE_X_REACHED
6625 -when TO_X was reached before TO_POS or ZV were reached.
6627 MOVE_LINE_CONTINUED
6628 - when we reached the end of the display area and the line must
6629 be continued.
6631 MOVE_LINE_TRUNCATED
6632 - when we reached the end of the display area and the line is
6633 truncated.
6635 MOVE_NEWLINE_OR_CR
6636 - when we stopped at a line end, i.e. a newline or a CR and selective
6637 display is on. */
6639 static enum move_it_result
6640 move_it_in_display_line_to (struct it *it,
6641 EMACS_INT to_charpos, int to_x,
6642 enum move_operation_enum op)
6644 enum move_it_result result = MOVE_UNDEFINED;
6645 struct glyph_row *saved_glyph_row;
6647 /* Don't produce glyphs in produce_glyphs. */
6648 saved_glyph_row = it->glyph_row;
6649 it->glyph_row = NULL;
6651 #define BUFFER_POS_REACHED_P() \
6652 ((op & MOVE_TO_POS) != 0 \
6653 && BUFFERP (it->object) \
6654 && IT_CHARPOS (*it) >= to_charpos \
6655 && (it->method == GET_FROM_BUFFER \
6656 || (it->method == GET_FROM_DISPLAY_VECTOR \
6657 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6660 while (1)
6662 int x, i, ascent = 0, descent = 0;
6664 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6665 if ((op & MOVE_TO_POS) != 0
6666 && BUFFERP (it->object)
6667 && it->method == GET_FROM_BUFFER
6668 && IT_CHARPOS (*it) > to_charpos)
6670 result = MOVE_POS_MATCH_OR_ZV;
6671 break;
6674 /* Stop when ZV reached.
6675 We used to stop here when TO_CHARPOS reached as well, but that is
6676 too soon if this glyph does not fit on this line. So we handle it
6677 explicitly below. */
6678 if (!get_next_display_element (it)
6679 || (it->truncate_lines_p
6680 && BUFFER_POS_REACHED_P ()))
6682 result = MOVE_POS_MATCH_OR_ZV;
6683 break;
6686 /* The call to produce_glyphs will get the metrics of the
6687 display element IT is loaded with. We record in x the
6688 x-position before this display element in case it does not
6689 fit on the line. */
6690 x = it->current_x;
6692 /* Remember the line height so far in case the next element doesn't
6693 fit on the line. */
6694 if (!it->truncate_lines_p)
6696 ascent = it->max_ascent;
6697 descent = it->max_descent;
6700 PRODUCE_GLYPHS (it);
6702 if (it->area != TEXT_AREA)
6704 set_iterator_to_next (it, 1);
6705 continue;
6708 /* The number of glyphs we get back in IT->nglyphs will normally
6709 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6710 character on a terminal frame, or (iii) a line end. For the
6711 second case, IT->nglyphs - 1 padding glyphs will be present
6712 (on X frames, there is only one glyph produced for a
6713 composite character.
6715 The behavior implemented below means, for continuation lines,
6716 that as many spaces of a TAB as fit on the current line are
6717 displayed there. For terminal frames, as many glyphs of a
6718 multi-glyph character are displayed in the current line, too.
6719 This is what the old redisplay code did, and we keep it that
6720 way. Under X, the whole shape of a complex character must
6721 fit on the line or it will be completely displayed in the
6722 next line.
6724 Note that both for tabs and padding glyphs, all glyphs have
6725 the same width. */
6726 if (it->nglyphs)
6728 /* More than one glyph or glyph doesn't fit on line. All
6729 glyphs have the same width. */
6730 int single_glyph_width = it->pixel_width / it->nglyphs;
6731 int new_x;
6732 int x_before_this_char = x;
6733 int hpos_before_this_char = it->hpos;
6735 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6737 new_x = x + single_glyph_width;
6739 /* We want to leave anything reaching TO_X to the caller. */
6740 if ((op & MOVE_TO_X) && new_x > to_x)
6742 if (BUFFER_POS_REACHED_P ())
6743 goto buffer_pos_reached;
6744 it->current_x = x;
6745 result = MOVE_X_REACHED;
6746 break;
6748 else if (/* Lines are continued. */
6749 !it->truncate_lines_p
6750 && (/* And glyph doesn't fit on the line. */
6751 new_x > it->last_visible_x
6752 /* Or it fits exactly and we're on a window
6753 system frame. */
6754 || (new_x == it->last_visible_x
6755 && FRAME_WINDOW_P (it->f))))
6757 if (/* IT->hpos == 0 means the very first glyph
6758 doesn't fit on the line, e.g. a wide image. */
6759 it->hpos == 0
6760 || (new_x == it->last_visible_x
6761 && FRAME_WINDOW_P (it->f)))
6763 ++it->hpos;
6764 it->current_x = new_x;
6766 /* The character's last glyph just barely fits
6767 in this row. */
6768 if (i == it->nglyphs - 1)
6770 /* If this is the destination position,
6771 return a position *before* it in this row,
6772 now that we know it fits in this row. */
6773 if (BUFFER_POS_REACHED_P ())
6775 it->hpos = hpos_before_this_char;
6776 it->current_x = x_before_this_char;
6777 result = MOVE_POS_MATCH_OR_ZV;
6778 break;
6781 set_iterator_to_next (it, 1);
6782 #ifdef HAVE_WINDOW_SYSTEM
6783 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6785 if (!get_next_display_element (it))
6787 result = MOVE_POS_MATCH_OR_ZV;
6788 break;
6790 if (BUFFER_POS_REACHED_P ())
6792 if (ITERATOR_AT_END_OF_LINE_P (it))
6793 result = MOVE_POS_MATCH_OR_ZV;
6794 else
6795 result = MOVE_LINE_CONTINUED;
6796 break;
6798 if (ITERATOR_AT_END_OF_LINE_P (it))
6800 result = MOVE_NEWLINE_OR_CR;
6801 break;
6804 #endif /* HAVE_WINDOW_SYSTEM */
6807 else
6809 it->current_x = x;
6810 it->max_ascent = ascent;
6811 it->max_descent = descent;
6814 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6815 IT_CHARPOS (*it)));
6816 result = MOVE_LINE_CONTINUED;
6817 break;
6819 else if (BUFFER_POS_REACHED_P ())
6820 goto buffer_pos_reached;
6821 else if (new_x > it->first_visible_x)
6823 /* Glyph is visible. Increment number of glyphs that
6824 would be displayed. */
6825 ++it->hpos;
6827 else
6829 /* Glyph is completely off the left margin of the display
6830 area. Nothing to do. */
6834 if (result != MOVE_UNDEFINED)
6835 break;
6837 else if (BUFFER_POS_REACHED_P ())
6839 buffer_pos_reached:
6840 it->current_x = x;
6841 it->max_ascent = ascent;
6842 it->max_descent = descent;
6843 result = MOVE_POS_MATCH_OR_ZV;
6844 break;
6846 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6848 /* Stop when TO_X specified and reached. This check is
6849 necessary here because of lines consisting of a line end,
6850 only. The line end will not produce any glyphs and we
6851 would never get MOVE_X_REACHED. */
6852 xassert (it->nglyphs == 0);
6853 result = MOVE_X_REACHED;
6854 break;
6857 /* Is this a line end? If yes, we're done. */
6858 if (ITERATOR_AT_END_OF_LINE_P (it))
6860 result = MOVE_NEWLINE_OR_CR;
6861 break;
6864 /* The current display element has been consumed. Advance
6865 to the next. */
6866 set_iterator_to_next (it, 1);
6868 /* Stop if lines are truncated and IT's current x-position is
6869 past the right edge of the window now. */
6870 if (it->truncate_lines_p
6871 && it->current_x >= it->last_visible_x)
6873 #ifdef HAVE_WINDOW_SYSTEM
6874 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6876 if (!get_next_display_element (it)
6877 || BUFFER_POS_REACHED_P ())
6879 result = MOVE_POS_MATCH_OR_ZV;
6880 break;
6882 if (ITERATOR_AT_END_OF_LINE_P (it))
6884 result = MOVE_NEWLINE_OR_CR;
6885 break;
6888 #endif /* HAVE_WINDOW_SYSTEM */
6889 result = MOVE_LINE_TRUNCATED;
6890 break;
6894 #undef BUFFER_POS_REACHED_P
6896 /* Restore the iterator settings altered at the beginning of this
6897 function. */
6898 it->glyph_row = saved_glyph_row;
6899 return result;
6902 /* For external use. */
6903 void
6904 move_it_in_display_line (struct it *it,
6905 EMACS_INT to_charpos, int to_x,
6906 enum move_operation_enum op)
6908 move_it_in_display_line_to (it, to_charpos, to_x, op);
6912 /* Move IT forward until it satisfies one or more of the criteria in
6913 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6915 OP is a bit-mask that specifies where to stop, and in particular,
6916 which of those four position arguments makes a difference. See the
6917 description of enum move_operation_enum.
6919 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6920 screen line, this function will set IT to the next position >
6921 TO_CHARPOS. */
6923 void
6924 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6925 struct it *it;
6926 int to_charpos, to_x, to_y, to_vpos;
6927 int op;
6929 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6930 int line_height;
6931 int reached = 0;
6933 for (;;)
6935 if (op & MOVE_TO_VPOS)
6937 /* If no TO_CHARPOS and no TO_X specified, stop at the
6938 start of the line TO_VPOS. */
6939 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6941 if (it->vpos == to_vpos)
6943 reached = 1;
6944 break;
6946 else
6947 skip = move_it_in_display_line_to (it, -1, -1, 0);
6949 else
6951 /* TO_VPOS >= 0 means stop at TO_X in the line at
6952 TO_VPOS, or at TO_POS, whichever comes first. */
6953 if (it->vpos == to_vpos)
6955 reached = 2;
6956 break;
6959 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6961 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6963 reached = 3;
6964 break;
6966 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6968 /* We have reached TO_X but not in the line we want. */
6969 skip = move_it_in_display_line_to (it, to_charpos,
6970 -1, MOVE_TO_POS);
6971 if (skip == MOVE_POS_MATCH_OR_ZV)
6973 reached = 4;
6974 break;
6979 else if (op & MOVE_TO_Y)
6981 struct it it_backup;
6983 /* TO_Y specified means stop at TO_X in the line containing
6984 TO_Y---or at TO_CHARPOS if this is reached first. The
6985 problem is that we can't really tell whether the line
6986 contains TO_Y before we have completely scanned it, and
6987 this may skip past TO_X. What we do is to first scan to
6988 TO_X.
6990 If TO_X is not specified, use a TO_X of zero. The reason
6991 is to make the outcome of this function more predictable.
6992 If we didn't use TO_X == 0, we would stop at the end of
6993 the line which is probably not what a caller would expect
6994 to happen. */
6995 skip = move_it_in_display_line_to (it, to_charpos,
6996 ((op & MOVE_TO_X)
6997 ? to_x : 0),
6998 (MOVE_TO_X
6999 | (op & MOVE_TO_POS)));
7001 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7002 if (skip == MOVE_POS_MATCH_OR_ZV)
7004 reached = 5;
7005 break;
7008 /* If TO_X was reached, we would like to know whether TO_Y
7009 is in the line. This can only be said if we know the
7010 total line height which requires us to scan the rest of
7011 the line. */
7012 if (skip == MOVE_X_REACHED)
7014 /* Wait! We can conclude that TO_Y is in the line if
7015 the already scanned glyphs make the line tall enough
7016 because further scanning doesn't make it shorter. */
7017 line_height = it->max_ascent + it->max_descent;
7018 if (to_y >= it->current_y
7019 && to_y < it->current_y + line_height)
7021 reached = 6;
7022 break;
7024 it_backup = *it;
7025 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7026 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7027 op & MOVE_TO_POS);
7028 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7031 /* Now, decide whether TO_Y is in this line. */
7032 line_height = it->max_ascent + it->max_descent;
7033 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7035 if (to_y >= it->current_y
7036 && to_y < it->current_y + line_height)
7038 if (skip == MOVE_X_REACHED)
7039 /* If TO_Y is in this line and TO_X was reached above,
7040 we scanned too far. We have to restore IT's settings
7041 to the ones before skipping. */
7042 *it = it_backup;
7043 reached = 6;
7045 else if (skip == MOVE_X_REACHED)
7047 skip = skip2;
7048 if (skip == MOVE_POS_MATCH_OR_ZV)
7049 reached = 7;
7052 if (reached)
7053 break;
7055 else if (BUFFERP (it->object)
7056 && it->method == GET_FROM_BUFFER
7057 && IT_CHARPOS (*it) >= to_charpos)
7058 skip = MOVE_POS_MATCH_OR_ZV;
7059 else
7060 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7062 switch (skip)
7064 case MOVE_POS_MATCH_OR_ZV:
7065 reached = 8;
7066 goto out;
7068 case MOVE_NEWLINE_OR_CR:
7069 set_iterator_to_next (it, 1);
7070 it->continuation_lines_width = 0;
7071 break;
7073 case MOVE_LINE_TRUNCATED:
7074 it->continuation_lines_width = 0;
7075 reseat_at_next_visible_line_start (it, 0);
7076 if ((op & MOVE_TO_POS) != 0
7077 && IT_CHARPOS (*it) > to_charpos)
7079 reached = 9;
7080 goto out;
7082 break;
7084 case MOVE_LINE_CONTINUED:
7085 /* For continued lines ending in a tab, some of the glyphs
7086 associated with the tab are displayed on the current
7087 line. Since it->current_x does not include these glyphs,
7088 we use it->last_visible_x instead. */
7089 it->continuation_lines_width +=
7090 (it->c == '\t') ? it->last_visible_x : it->current_x;
7091 break;
7093 default:
7094 abort ();
7097 /* Reset/increment for the next run. */
7098 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7099 it->current_x = it->hpos = 0;
7100 it->current_y += it->max_ascent + it->max_descent;
7101 ++it->vpos;
7102 last_height = it->max_ascent + it->max_descent;
7103 last_max_ascent = it->max_ascent;
7104 it->max_ascent = it->max_descent = 0;
7107 out:
7109 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7113 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7115 If DY > 0, move IT backward at least that many pixels. DY = 0
7116 means move IT backward to the preceding line start or BEGV. This
7117 function may move over more than DY pixels if IT->current_y - DY
7118 ends up in the middle of a line; in this case IT->current_y will be
7119 set to the top of the line moved to. */
7121 void
7122 move_it_vertically_backward (it, dy)
7123 struct it *it;
7124 int dy;
7126 int nlines, h;
7127 struct it it2, it3;
7128 int start_pos;
7130 move_further_back:
7131 xassert (dy >= 0);
7133 start_pos = IT_CHARPOS (*it);
7135 /* Estimate how many newlines we must move back. */
7136 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7138 /* Set the iterator's position that many lines back. */
7139 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7140 back_to_previous_visible_line_start (it);
7142 /* Reseat the iterator here. When moving backward, we don't want
7143 reseat to skip forward over invisible text, set up the iterator
7144 to deliver from overlay strings at the new position etc. So,
7145 use reseat_1 here. */
7146 reseat_1 (it, it->current.pos, 1);
7148 /* We are now surely at a line start. */
7149 it->current_x = it->hpos = 0;
7150 it->continuation_lines_width = 0;
7152 /* Move forward and see what y-distance we moved. First move to the
7153 start of the next line so that we get its height. We need this
7154 height to be able to tell whether we reached the specified
7155 y-distance. */
7156 it2 = *it;
7157 it2.max_ascent = it2.max_descent = 0;
7160 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7161 MOVE_TO_POS | MOVE_TO_VPOS);
7163 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7164 xassert (IT_CHARPOS (*it) >= BEGV);
7165 it3 = it2;
7167 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7168 xassert (IT_CHARPOS (*it) >= BEGV);
7169 /* H is the actual vertical distance from the position in *IT
7170 and the starting position. */
7171 h = it2.current_y - it->current_y;
7172 /* NLINES is the distance in number of lines. */
7173 nlines = it2.vpos - it->vpos;
7175 /* Correct IT's y and vpos position
7176 so that they are relative to the starting point. */
7177 it->vpos -= nlines;
7178 it->current_y -= h;
7180 if (dy == 0)
7182 /* DY == 0 means move to the start of the screen line. The
7183 value of nlines is > 0 if continuation lines were involved. */
7184 if (nlines > 0)
7185 move_it_by_lines (it, nlines, 1);
7186 #if 0
7187 /* I think this assert is bogus if buffer contains
7188 invisible text or images. KFS. */
7189 xassert (IT_CHARPOS (*it) <= start_pos);
7190 #endif
7192 else
7194 /* The y-position we try to reach, relative to *IT.
7195 Note that H has been subtracted in front of the if-statement. */
7196 int target_y = it->current_y + h - dy;
7197 int y0 = it3.current_y;
7198 int y1 = line_bottom_y (&it3);
7199 int line_height = y1 - y0;
7201 /* If we did not reach target_y, try to move further backward if
7202 we can. If we moved too far backward, try to move forward. */
7203 if (target_y < it->current_y
7204 /* This is heuristic. In a window that's 3 lines high, with
7205 a line height of 13 pixels each, recentering with point
7206 on the bottom line will try to move -39/2 = 19 pixels
7207 backward. Try to avoid moving into the first line. */
7208 && (it->current_y - target_y
7209 > min (window_box_height (it->w), line_height * 2 / 3))
7210 && IT_CHARPOS (*it) > BEGV)
7212 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7213 target_y - it->current_y));
7214 dy = it->current_y - target_y;
7215 goto move_further_back;
7217 else if (target_y >= it->current_y + line_height
7218 && IT_CHARPOS (*it) < ZV)
7220 /* Should move forward by at least one line, maybe more.
7222 Note: Calling move_it_by_lines can be expensive on
7223 terminal frames, where compute_motion is used (via
7224 vmotion) to do the job, when there are very long lines
7225 and truncate-lines is nil. That's the reason for
7226 treating terminal frames specially here. */
7228 if (!FRAME_WINDOW_P (it->f))
7229 move_it_vertically (it, target_y - (it->current_y + line_height));
7230 else
7234 move_it_by_lines (it, 1, 1);
7236 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7239 #if 0
7240 /* I think this assert is bogus if buffer contains
7241 invisible text or images. KFS. */
7242 xassert (IT_CHARPOS (*it) >= BEGV);
7243 #endif
7249 /* Move IT by a specified amount of pixel lines DY. DY negative means
7250 move backwards. DY = 0 means move to start of screen line. At the
7251 end, IT will be on the start of a screen line. */
7253 void
7254 move_it_vertically (it, dy)
7255 struct it *it;
7256 int dy;
7258 if (dy <= 0)
7259 move_it_vertically_backward (it, -dy);
7260 else
7262 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7263 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7264 MOVE_TO_POS | MOVE_TO_Y);
7265 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7267 /* If buffer ends in ZV without a newline, move to the start of
7268 the line to satisfy the post-condition. */
7269 if (IT_CHARPOS (*it) == ZV
7270 && ZV > BEGV
7271 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7272 move_it_by_lines (it, 0, 0);
7277 /* Move iterator IT past the end of the text line it is in. */
7279 void
7280 move_it_past_eol (it)
7281 struct it *it;
7283 enum move_it_result rc;
7285 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7286 if (rc == MOVE_NEWLINE_OR_CR)
7287 set_iterator_to_next (it, 0);
7291 #if 0 /* Currently not used. */
7293 /* Return non-zero if some text between buffer positions START_CHARPOS
7294 and END_CHARPOS is invisible. IT->window is the window for text
7295 property lookup. */
7297 static int
7298 invisible_text_between_p (it, start_charpos, end_charpos)
7299 struct it *it;
7300 int start_charpos, end_charpos;
7302 Lisp_Object prop, limit;
7303 int invisible_found_p;
7305 xassert (it != NULL && start_charpos <= end_charpos);
7307 /* Is text at START invisible? */
7308 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7309 it->window);
7310 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7311 invisible_found_p = 1;
7312 else
7314 limit = Fnext_single_char_property_change (make_number (start_charpos),
7315 Qinvisible, Qnil,
7316 make_number (end_charpos));
7317 invisible_found_p = XFASTINT (limit) < end_charpos;
7320 return invisible_found_p;
7323 #endif /* 0 */
7326 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7327 negative means move up. DVPOS == 0 means move to the start of the
7328 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7329 NEED_Y_P is zero, IT->current_y will be left unchanged.
7331 Further optimization ideas: If we would know that IT->f doesn't use
7332 a face with proportional font, we could be faster for
7333 truncate-lines nil. */
7335 void
7336 move_it_by_lines (it, dvpos, need_y_p)
7337 struct it *it;
7338 int dvpos, need_y_p;
7340 struct position pos;
7342 /* The commented-out optimization uses vmotion on terminals. This
7343 gives bad results, because elements like it->what, on which
7344 callers such as pos_visible_p rely, aren't updated. */
7345 /* if (!FRAME_WINDOW_P (it->f))
7347 struct text_pos textpos;
7349 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7350 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7351 reseat (it, textpos, 1);
7352 it->vpos += pos.vpos;
7353 it->current_y += pos.vpos;
7355 else */
7357 if (dvpos == 0)
7359 /* DVPOS == 0 means move to the start of the screen line. */
7360 move_it_vertically_backward (it, 0);
7361 xassert (it->current_x == 0 && it->hpos == 0);
7362 /* Let next call to line_bottom_y calculate real line height */
7363 last_height = 0;
7365 else if (dvpos > 0)
7367 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7368 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7369 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7371 else
7373 struct it it2;
7374 int start_charpos, i;
7376 /* Start at the beginning of the screen line containing IT's
7377 position. This may actually move vertically backwards,
7378 in case of overlays, so adjust dvpos accordingly. */
7379 dvpos += it->vpos;
7380 move_it_vertically_backward (it, 0);
7381 dvpos -= it->vpos;
7383 /* Go back -DVPOS visible lines and reseat the iterator there. */
7384 start_charpos = IT_CHARPOS (*it);
7385 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7386 back_to_previous_visible_line_start (it);
7387 reseat (it, it->current.pos, 1);
7389 /* Move further back if we end up in a string or an image. */
7390 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7392 /* First try to move to start of display line. */
7393 dvpos += it->vpos;
7394 move_it_vertically_backward (it, 0);
7395 dvpos -= it->vpos;
7396 if (IT_POS_VALID_AFTER_MOVE_P (it))
7397 break;
7398 /* If start of line is still in string or image,
7399 move further back. */
7400 back_to_previous_visible_line_start (it);
7401 reseat (it, it->current.pos, 1);
7402 dvpos--;
7405 it->current_x = it->hpos = 0;
7407 /* Above call may have moved too far if continuation lines
7408 are involved. Scan forward and see if it did. */
7409 it2 = *it;
7410 it2.vpos = it2.current_y = 0;
7411 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7412 it->vpos -= it2.vpos;
7413 it->current_y -= it2.current_y;
7414 it->current_x = it->hpos = 0;
7416 /* If we moved too far back, move IT some lines forward. */
7417 if (it2.vpos > -dvpos)
7419 int delta = it2.vpos + dvpos;
7420 it2 = *it;
7421 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7422 /* Move back again if we got too far ahead. */
7423 if (IT_CHARPOS (*it) >= start_charpos)
7424 *it = it2;
7429 /* Return 1 if IT points into the middle of a display vector. */
7432 in_display_vector_p (it)
7433 struct it *it;
7435 return (it->method == GET_FROM_DISPLAY_VECTOR
7436 && it->current.dpvec_index > 0
7437 && it->dpvec + it->current.dpvec_index != it->dpend);
7441 /***********************************************************************
7442 Messages
7443 ***********************************************************************/
7446 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7447 to *Messages*. */
7449 void
7450 add_to_log (format, arg1, arg2)
7451 char *format;
7452 Lisp_Object arg1, arg2;
7454 Lisp_Object args[3];
7455 Lisp_Object msg, fmt;
7456 char *buffer;
7457 int len;
7458 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7459 USE_SAFE_ALLOCA;
7461 /* Do nothing if called asynchronously. Inserting text into
7462 a buffer may call after-change-functions and alike and
7463 that would means running Lisp asynchronously. */
7464 if (handling_signal)
7465 return;
7467 fmt = msg = Qnil;
7468 GCPRO4 (fmt, msg, arg1, arg2);
7470 args[0] = fmt = build_string (format);
7471 args[1] = arg1;
7472 args[2] = arg2;
7473 msg = Fformat (3, args);
7475 len = SBYTES (msg) + 1;
7476 SAFE_ALLOCA (buffer, char *, len);
7477 bcopy (SDATA (msg), buffer, len);
7479 message_dolog (buffer, len - 1, 1, 0);
7480 SAFE_FREE ();
7482 UNGCPRO;
7486 /* Output a newline in the *Messages* buffer if "needs" one. */
7488 void
7489 message_log_maybe_newline ()
7491 if (message_log_need_newline)
7492 message_dolog ("", 0, 1, 0);
7496 /* Add a string M of length NBYTES to the message log, optionally
7497 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7498 nonzero, means interpret the contents of M as multibyte. This
7499 function calls low-level routines in order to bypass text property
7500 hooks, etc. which might not be safe to run.
7502 This may GC (insert may run before/after change hooks),
7503 so the buffer M must NOT point to a Lisp string. */
7505 void
7506 message_dolog (m, nbytes, nlflag, multibyte)
7507 const char *m;
7508 int nbytes, nlflag, multibyte;
7510 if (!NILP (Vmemory_full))
7511 return;
7513 if (!NILP (Vmessage_log_max))
7515 struct buffer *oldbuf;
7516 Lisp_Object oldpoint, oldbegv, oldzv;
7517 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7518 int point_at_end = 0;
7519 int zv_at_end = 0;
7520 Lisp_Object old_deactivate_mark, tem;
7521 struct gcpro gcpro1;
7523 old_deactivate_mark = Vdeactivate_mark;
7524 oldbuf = current_buffer;
7525 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7526 current_buffer->undo_list = Qt;
7528 oldpoint = message_dolog_marker1;
7529 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7530 oldbegv = message_dolog_marker2;
7531 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7532 oldzv = message_dolog_marker3;
7533 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7534 GCPRO1 (old_deactivate_mark);
7536 if (PT == Z)
7537 point_at_end = 1;
7538 if (ZV == Z)
7539 zv_at_end = 1;
7541 BEGV = BEG;
7542 BEGV_BYTE = BEG_BYTE;
7543 ZV = Z;
7544 ZV_BYTE = Z_BYTE;
7545 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7547 /* Insert the string--maybe converting multibyte to single byte
7548 or vice versa, so that all the text fits the buffer. */
7549 if (multibyte
7550 && NILP (current_buffer->enable_multibyte_characters))
7552 int i, c, char_bytes;
7553 unsigned char work[1];
7555 /* Convert a multibyte string to single-byte
7556 for the *Message* buffer. */
7557 for (i = 0; i < nbytes; i += char_bytes)
7559 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7560 work[0] = (ASCII_CHAR_P (c)
7562 : multibyte_char_to_unibyte (c, Qnil));
7563 insert_1_both (work, 1, 1, 1, 0, 0);
7566 else if (! multibyte
7567 && ! NILP (current_buffer->enable_multibyte_characters))
7569 int i, c, char_bytes;
7570 unsigned char *msg = (unsigned char *) m;
7571 unsigned char str[MAX_MULTIBYTE_LENGTH];
7572 /* Convert a single-byte string to multibyte
7573 for the *Message* buffer. */
7574 for (i = 0; i < nbytes; i++)
7576 c = msg[i];
7577 c = unibyte_char_to_multibyte (c);
7578 char_bytes = CHAR_STRING (c, str);
7579 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7582 else if (nbytes)
7583 insert_1 (m, nbytes, 1, 0, 0);
7585 if (nlflag)
7587 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7588 insert_1 ("\n", 1, 1, 0, 0);
7590 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7591 this_bol = PT;
7592 this_bol_byte = PT_BYTE;
7594 /* See if this line duplicates the previous one.
7595 If so, combine duplicates. */
7596 if (this_bol > BEG)
7598 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7599 prev_bol = PT;
7600 prev_bol_byte = PT_BYTE;
7602 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7603 this_bol, this_bol_byte);
7604 if (dup)
7606 del_range_both (prev_bol, prev_bol_byte,
7607 this_bol, this_bol_byte, 0);
7608 if (dup > 1)
7610 char dupstr[40];
7611 int duplen;
7613 /* If you change this format, don't forget to also
7614 change message_log_check_duplicate. */
7615 sprintf (dupstr, " [%d times]", dup);
7616 duplen = strlen (dupstr);
7617 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7618 insert_1 (dupstr, duplen, 1, 0, 1);
7623 /* If we have more than the desired maximum number of lines
7624 in the *Messages* buffer now, delete the oldest ones.
7625 This is safe because we don't have undo in this buffer. */
7627 if (NATNUMP (Vmessage_log_max))
7629 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7630 -XFASTINT (Vmessage_log_max) - 1, 0);
7631 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7634 BEGV = XMARKER (oldbegv)->charpos;
7635 BEGV_BYTE = marker_byte_position (oldbegv);
7637 if (zv_at_end)
7639 ZV = Z;
7640 ZV_BYTE = Z_BYTE;
7642 else
7644 ZV = XMARKER (oldzv)->charpos;
7645 ZV_BYTE = marker_byte_position (oldzv);
7648 if (point_at_end)
7649 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7650 else
7651 /* We can't do Fgoto_char (oldpoint) because it will run some
7652 Lisp code. */
7653 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7654 XMARKER (oldpoint)->bytepos);
7656 UNGCPRO;
7657 unchain_marker (XMARKER (oldpoint));
7658 unchain_marker (XMARKER (oldbegv));
7659 unchain_marker (XMARKER (oldzv));
7661 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7662 set_buffer_internal (oldbuf);
7663 if (NILP (tem))
7664 windows_or_buffers_changed = old_windows_or_buffers_changed;
7665 message_log_need_newline = !nlflag;
7666 Vdeactivate_mark = old_deactivate_mark;
7671 /* We are at the end of the buffer after just having inserted a newline.
7672 (Note: We depend on the fact we won't be crossing the gap.)
7673 Check to see if the most recent message looks a lot like the previous one.
7674 Return 0 if different, 1 if the new one should just replace it, or a
7675 value N > 1 if we should also append " [N times]". */
7677 static int
7678 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7679 int prev_bol, this_bol;
7680 int prev_bol_byte, this_bol_byte;
7682 int i;
7683 int len = Z_BYTE - 1 - this_bol_byte;
7684 int seen_dots = 0;
7685 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7686 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7688 for (i = 0; i < len; i++)
7690 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7691 seen_dots = 1;
7692 if (p1[i] != p2[i])
7693 return seen_dots;
7695 p1 += len;
7696 if (*p1 == '\n')
7697 return 2;
7698 if (*p1++ == ' ' && *p1++ == '[')
7700 int n = 0;
7701 while (*p1 >= '0' && *p1 <= '9')
7702 n = n * 10 + *p1++ - '0';
7703 if (strncmp (p1, " times]\n", 8) == 0)
7704 return n+1;
7706 return 0;
7710 /* Display an echo area message M with a specified length of NBYTES
7711 bytes. The string may include null characters. If M is 0, clear
7712 out any existing message, and let the mini-buffer text show
7713 through.
7715 This may GC, so the buffer M must NOT point to a Lisp string. */
7717 void
7718 message2 (m, nbytes, multibyte)
7719 const char *m;
7720 int nbytes;
7721 int multibyte;
7723 /* First flush out any partial line written with print. */
7724 message_log_maybe_newline ();
7725 if (m)
7726 message_dolog (m, nbytes, 1, multibyte);
7727 message2_nolog (m, nbytes, multibyte);
7731 /* The non-logging counterpart of message2. */
7733 void
7734 message2_nolog (m, nbytes, multibyte)
7735 const char *m;
7736 int nbytes, multibyte;
7738 struct frame *sf = SELECTED_FRAME ();
7739 message_enable_multibyte = multibyte;
7741 if (noninteractive)
7743 if (noninteractive_need_newline)
7744 putc ('\n', stderr);
7745 noninteractive_need_newline = 0;
7746 if (m)
7747 fwrite (m, nbytes, 1, stderr);
7748 if (cursor_in_echo_area == 0)
7749 fprintf (stderr, "\n");
7750 fflush (stderr);
7752 /* A null message buffer means that the frame hasn't really been
7753 initialized yet. Error messages get reported properly by
7754 cmd_error, so this must be just an informative message; toss it. */
7755 else if (INTERACTIVE
7756 && sf->glyphs_initialized_p
7757 && FRAME_MESSAGE_BUF (sf))
7759 Lisp_Object mini_window;
7760 struct frame *f;
7762 /* Get the frame containing the mini-buffer
7763 that the selected frame is using. */
7764 mini_window = FRAME_MINIBUF_WINDOW (sf);
7765 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7767 FRAME_SAMPLE_VISIBILITY (f);
7768 if (FRAME_VISIBLE_P (sf)
7769 && ! FRAME_VISIBLE_P (f))
7770 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7772 if (m)
7774 set_message (m, Qnil, nbytes, multibyte);
7775 if (minibuffer_auto_raise)
7776 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7778 else
7779 clear_message (1, 1);
7781 do_pending_window_change (0);
7782 echo_area_display (1);
7783 do_pending_window_change (0);
7784 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7785 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7790 /* Display an echo area message M with a specified length of NBYTES
7791 bytes. The string may include null characters. If M is not a
7792 string, clear out any existing message, and let the mini-buffer
7793 text show through.
7795 This function cancels echoing. */
7797 void
7798 message3 (m, nbytes, multibyte)
7799 Lisp_Object m;
7800 int nbytes;
7801 int multibyte;
7803 struct gcpro gcpro1;
7805 GCPRO1 (m);
7806 clear_message (1,1);
7807 cancel_echoing ();
7809 /* First flush out any partial line written with print. */
7810 message_log_maybe_newline ();
7811 if (STRINGP (m))
7813 char *buffer;
7814 USE_SAFE_ALLOCA;
7816 SAFE_ALLOCA (buffer, char *, nbytes);
7817 bcopy (SDATA (m), buffer, nbytes);
7818 message_dolog (buffer, nbytes, 1, multibyte);
7819 SAFE_FREE ();
7821 message3_nolog (m, nbytes, multibyte);
7823 UNGCPRO;
7827 /* The non-logging version of message3.
7828 This does not cancel echoing, because it is used for echoing.
7829 Perhaps we need to make a separate function for echoing
7830 and make this cancel echoing. */
7832 void
7833 message3_nolog (m, nbytes, multibyte)
7834 Lisp_Object m;
7835 int nbytes, multibyte;
7837 struct frame *sf = SELECTED_FRAME ();
7838 message_enable_multibyte = multibyte;
7840 if (noninteractive)
7842 if (noninteractive_need_newline)
7843 putc ('\n', stderr);
7844 noninteractive_need_newline = 0;
7845 if (STRINGP (m))
7846 fwrite (SDATA (m), nbytes, 1, stderr);
7847 if (cursor_in_echo_area == 0)
7848 fprintf (stderr, "\n");
7849 fflush (stderr);
7851 /* A null message buffer means that the frame hasn't really been
7852 initialized yet. Error messages get reported properly by
7853 cmd_error, so this must be just an informative message; toss it. */
7854 else if (INTERACTIVE
7855 && sf->glyphs_initialized_p
7856 && FRAME_MESSAGE_BUF (sf))
7858 Lisp_Object mini_window;
7859 Lisp_Object frame;
7860 struct frame *f;
7862 /* Get the frame containing the mini-buffer
7863 that the selected frame is using. */
7864 mini_window = FRAME_MINIBUF_WINDOW (sf);
7865 frame = XWINDOW (mini_window)->frame;
7866 f = XFRAME (frame);
7868 FRAME_SAMPLE_VISIBILITY (f);
7869 if (FRAME_VISIBLE_P (sf)
7870 && !FRAME_VISIBLE_P (f))
7871 Fmake_frame_visible (frame);
7873 if (STRINGP (m) && SCHARS (m) > 0)
7875 set_message (NULL, m, nbytes, multibyte);
7876 if (minibuffer_auto_raise)
7877 Fraise_frame (frame);
7878 /* Assume we are not echoing.
7879 (If we are, echo_now will override this.) */
7880 echo_message_buffer = Qnil;
7882 else
7883 clear_message (1, 1);
7885 do_pending_window_change (0);
7886 echo_area_display (1);
7887 do_pending_window_change (0);
7888 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7889 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7894 /* Display a null-terminated echo area message M. If M is 0, clear
7895 out any existing message, and let the mini-buffer text show through.
7897 The buffer M must continue to exist until after the echo area gets
7898 cleared or some other message gets displayed there. Do not pass
7899 text that is stored in a Lisp string. Do not pass text in a buffer
7900 that was alloca'd. */
7902 void
7903 message1 (m)
7904 char *m;
7906 message2 (m, (m ? strlen (m) : 0), 0);
7910 /* The non-logging counterpart of message1. */
7912 void
7913 message1_nolog (m)
7914 char *m;
7916 message2_nolog (m, (m ? strlen (m) : 0), 0);
7919 /* Display a message M which contains a single %s
7920 which gets replaced with STRING. */
7922 void
7923 message_with_string (m, string, log)
7924 char *m;
7925 Lisp_Object string;
7926 int log;
7928 CHECK_STRING (string);
7930 if (noninteractive)
7932 if (m)
7934 if (noninteractive_need_newline)
7935 putc ('\n', stderr);
7936 noninteractive_need_newline = 0;
7937 fprintf (stderr, m, SDATA (string));
7938 if (cursor_in_echo_area == 0)
7939 fprintf (stderr, "\n");
7940 fflush (stderr);
7943 else if (INTERACTIVE)
7945 /* The frame whose minibuffer we're going to display the message on.
7946 It may be larger than the selected frame, so we need
7947 to use its buffer, not the selected frame's buffer. */
7948 Lisp_Object mini_window;
7949 struct frame *f, *sf = SELECTED_FRAME ();
7951 /* Get the frame containing the minibuffer
7952 that the selected frame is using. */
7953 mini_window = FRAME_MINIBUF_WINDOW (sf);
7954 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7956 /* A null message buffer means that the frame hasn't really been
7957 initialized yet. Error messages get reported properly by
7958 cmd_error, so this must be just an informative message; toss it. */
7959 if (FRAME_MESSAGE_BUF (f))
7961 Lisp_Object args[2], message;
7962 struct gcpro gcpro1, gcpro2;
7964 args[0] = build_string (m);
7965 args[1] = message = string;
7966 GCPRO2 (args[0], message);
7967 gcpro1.nvars = 2;
7969 message = Fformat (2, args);
7971 if (log)
7972 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7973 else
7974 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7976 UNGCPRO;
7978 /* Print should start at the beginning of the message
7979 buffer next time. */
7980 message_buf_print = 0;
7986 /* Dump an informative message to the minibuf. If M is 0, clear out
7987 any existing message, and let the mini-buffer text show through. */
7989 /* VARARGS 1 */
7990 void
7991 message (m, a1, a2, a3)
7992 char *m;
7993 EMACS_INT a1, a2, a3;
7995 if (noninteractive)
7997 if (m)
7999 if (noninteractive_need_newline)
8000 putc ('\n', stderr);
8001 noninteractive_need_newline = 0;
8002 fprintf (stderr, m, a1, a2, a3);
8003 if (cursor_in_echo_area == 0)
8004 fprintf (stderr, "\n");
8005 fflush (stderr);
8008 else if (INTERACTIVE)
8010 /* The frame whose mini-buffer we're going to display the message
8011 on. It may be larger than the selected frame, so we need to
8012 use its buffer, not the selected frame's buffer. */
8013 Lisp_Object mini_window;
8014 struct frame *f, *sf = SELECTED_FRAME ();
8016 /* Get the frame containing the mini-buffer
8017 that the selected frame is using. */
8018 mini_window = FRAME_MINIBUF_WINDOW (sf);
8019 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8021 /* A null message buffer means that the frame hasn't really been
8022 initialized yet. Error messages get reported properly by
8023 cmd_error, so this must be just an informative message; toss
8024 it. */
8025 if (FRAME_MESSAGE_BUF (f))
8027 if (m)
8029 int len;
8030 #ifdef NO_ARG_ARRAY
8031 char *a[3];
8032 a[0] = (char *) a1;
8033 a[1] = (char *) a2;
8034 a[2] = (char *) a3;
8036 len = doprnt (FRAME_MESSAGE_BUF (f),
8037 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8038 #else
8039 len = doprnt (FRAME_MESSAGE_BUF (f),
8040 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8041 (char **) &a1);
8042 #endif /* NO_ARG_ARRAY */
8044 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8046 else
8047 message1 (0);
8049 /* Print should start at the beginning of the message
8050 buffer next time. */
8051 message_buf_print = 0;
8057 /* The non-logging version of message. */
8059 void
8060 message_nolog (m, a1, a2, a3)
8061 char *m;
8062 EMACS_INT a1, a2, a3;
8064 Lisp_Object old_log_max;
8065 old_log_max = Vmessage_log_max;
8066 Vmessage_log_max = Qnil;
8067 message (m, a1, a2, a3);
8068 Vmessage_log_max = old_log_max;
8072 /* Display the current message in the current mini-buffer. This is
8073 only called from error handlers in process.c, and is not time
8074 critical. */
8076 void
8077 update_echo_area ()
8079 if (!NILP (echo_area_buffer[0]))
8081 Lisp_Object string;
8082 string = Fcurrent_message ();
8083 message3 (string, SBYTES (string),
8084 !NILP (current_buffer->enable_multibyte_characters));
8089 /* Make sure echo area buffers in `echo_buffers' are live.
8090 If they aren't, make new ones. */
8092 static void
8093 ensure_echo_area_buffers ()
8095 int i;
8097 for (i = 0; i < 2; ++i)
8098 if (!BUFFERP (echo_buffer[i])
8099 || NILP (XBUFFER (echo_buffer[i])->name))
8101 char name[30];
8102 Lisp_Object old_buffer;
8103 int j;
8105 old_buffer = echo_buffer[i];
8106 sprintf (name, " *Echo Area %d*", i);
8107 echo_buffer[i] = Fget_buffer_create (build_string (name));
8108 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8110 for (j = 0; j < 2; ++j)
8111 if (EQ (old_buffer, echo_area_buffer[j]))
8112 echo_area_buffer[j] = echo_buffer[i];
8117 /* Call FN with args A1..A4 with either the current or last displayed
8118 echo_area_buffer as current buffer.
8120 WHICH zero means use the current message buffer
8121 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8122 from echo_buffer[] and clear it.
8124 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8125 suitable buffer from echo_buffer[] and clear it.
8127 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8128 that the current message becomes the last displayed one, make
8129 choose a suitable buffer for echo_area_buffer[0], and clear it.
8131 Value is what FN returns. */
8133 static int
8134 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8135 struct window *w;
8136 int which;
8137 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8138 EMACS_INT a1;
8139 Lisp_Object a2;
8140 EMACS_INT a3, a4;
8142 Lisp_Object buffer;
8143 int this_one, the_other, clear_buffer_p, rc;
8144 int count = SPECPDL_INDEX ();
8146 /* If buffers aren't live, make new ones. */
8147 ensure_echo_area_buffers ();
8149 clear_buffer_p = 0;
8151 if (which == 0)
8152 this_one = 0, the_other = 1;
8153 else if (which > 0)
8154 this_one = 1, the_other = 0;
8155 else
8157 this_one = 0, the_other = 1;
8158 clear_buffer_p = 1;
8160 /* We need a fresh one in case the current echo buffer equals
8161 the one containing the last displayed echo area message. */
8162 if (!NILP (echo_area_buffer[this_one])
8163 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8164 echo_area_buffer[this_one] = Qnil;
8167 /* Choose a suitable buffer from echo_buffer[] is we don't
8168 have one. */
8169 if (NILP (echo_area_buffer[this_one]))
8171 echo_area_buffer[this_one]
8172 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8173 ? echo_buffer[the_other]
8174 : echo_buffer[this_one]);
8175 clear_buffer_p = 1;
8178 buffer = echo_area_buffer[this_one];
8180 /* Don't get confused by reusing the buffer used for echoing
8181 for a different purpose. */
8182 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8183 cancel_echoing ();
8185 record_unwind_protect (unwind_with_echo_area_buffer,
8186 with_echo_area_buffer_unwind_data (w));
8188 /* Make the echo area buffer current. Note that for display
8189 purposes, it is not necessary that the displayed window's buffer
8190 == current_buffer, except for text property lookup. So, let's
8191 only set that buffer temporarily here without doing a full
8192 Fset_window_buffer. We must also change w->pointm, though,
8193 because otherwise an assertions in unshow_buffer fails, and Emacs
8194 aborts. */
8195 set_buffer_internal_1 (XBUFFER (buffer));
8196 if (w)
8198 w->buffer = buffer;
8199 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8202 current_buffer->undo_list = Qt;
8203 current_buffer->read_only = Qnil;
8204 specbind (Qinhibit_read_only, Qt);
8205 specbind (Qinhibit_modification_hooks, Qt);
8207 if (clear_buffer_p && Z > BEG)
8208 del_range (BEG, Z);
8210 xassert (BEGV >= BEG);
8211 xassert (ZV <= Z && ZV >= BEGV);
8213 rc = fn (a1, a2, a3, a4);
8215 xassert (BEGV >= BEG);
8216 xassert (ZV <= Z && ZV >= BEGV);
8218 unbind_to (count, Qnil);
8219 return rc;
8223 /* Save state that should be preserved around the call to the function
8224 FN called in with_echo_area_buffer. */
8226 static Lisp_Object
8227 with_echo_area_buffer_unwind_data (w)
8228 struct window *w;
8230 int i = 0;
8231 Lisp_Object vector, tmp;
8233 /* Reduce consing by keeping one vector in
8234 Vwith_echo_area_save_vector. */
8235 vector = Vwith_echo_area_save_vector;
8236 Vwith_echo_area_save_vector = Qnil;
8238 if (NILP (vector))
8239 vector = Fmake_vector (make_number (7), Qnil);
8241 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8242 ASET (vector, i, Vdeactivate_mark); ++i;
8243 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8245 if (w)
8247 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8248 ASET (vector, i, w->buffer); ++i;
8249 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8250 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8252 else
8254 int end = i + 4;
8255 for (; i < end; ++i)
8256 ASET (vector, i, Qnil);
8259 xassert (i == ASIZE (vector));
8260 return vector;
8264 /* Restore global state from VECTOR which was created by
8265 with_echo_area_buffer_unwind_data. */
8267 static Lisp_Object
8268 unwind_with_echo_area_buffer (vector)
8269 Lisp_Object vector;
8271 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8272 Vdeactivate_mark = AREF (vector, 1);
8273 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8275 if (WINDOWP (AREF (vector, 3)))
8277 struct window *w;
8278 Lisp_Object buffer, charpos, bytepos;
8280 w = XWINDOW (AREF (vector, 3));
8281 buffer = AREF (vector, 4);
8282 charpos = AREF (vector, 5);
8283 bytepos = AREF (vector, 6);
8285 w->buffer = buffer;
8286 set_marker_both (w->pointm, buffer,
8287 XFASTINT (charpos), XFASTINT (bytepos));
8290 Vwith_echo_area_save_vector = vector;
8291 return Qnil;
8295 /* Set up the echo area for use by print functions. MULTIBYTE_P
8296 non-zero means we will print multibyte. */
8298 void
8299 setup_echo_area_for_printing (multibyte_p)
8300 int multibyte_p;
8302 /* If we can't find an echo area any more, exit. */
8303 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8304 Fkill_emacs (Qnil);
8306 ensure_echo_area_buffers ();
8308 if (!message_buf_print)
8310 /* A message has been output since the last time we printed.
8311 Choose a fresh echo area buffer. */
8312 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8313 echo_area_buffer[0] = echo_buffer[1];
8314 else
8315 echo_area_buffer[0] = echo_buffer[0];
8317 /* Switch to that buffer and clear it. */
8318 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8319 current_buffer->truncate_lines = Qnil;
8321 if (Z > BEG)
8323 int count = SPECPDL_INDEX ();
8324 specbind (Qinhibit_read_only, Qt);
8325 /* Note that undo recording is always disabled. */
8326 del_range (BEG, Z);
8327 unbind_to (count, Qnil);
8329 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8331 /* Set up the buffer for the multibyteness we need. */
8332 if (multibyte_p
8333 != !NILP (current_buffer->enable_multibyte_characters))
8334 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8336 /* Raise the frame containing the echo area. */
8337 if (minibuffer_auto_raise)
8339 struct frame *sf = SELECTED_FRAME ();
8340 Lisp_Object mini_window;
8341 mini_window = FRAME_MINIBUF_WINDOW (sf);
8342 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8345 message_log_maybe_newline ();
8346 message_buf_print = 1;
8348 else
8350 if (NILP (echo_area_buffer[0]))
8352 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8353 echo_area_buffer[0] = echo_buffer[1];
8354 else
8355 echo_area_buffer[0] = echo_buffer[0];
8358 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8360 /* Someone switched buffers between print requests. */
8361 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8362 current_buffer->truncate_lines = Qnil;
8368 /* Display an echo area message in window W. Value is non-zero if W's
8369 height is changed. If display_last_displayed_message_p is
8370 non-zero, display the message that was last displayed, otherwise
8371 display the current message. */
8373 static int
8374 display_echo_area (w)
8375 struct window *w;
8377 int i, no_message_p, window_height_changed_p, count;
8379 /* Temporarily disable garbage collections while displaying the echo
8380 area. This is done because a GC can print a message itself.
8381 That message would modify the echo area buffer's contents while a
8382 redisplay of the buffer is going on, and seriously confuse
8383 redisplay. */
8384 count = inhibit_garbage_collection ();
8386 /* If there is no message, we must call display_echo_area_1
8387 nevertheless because it resizes the window. But we will have to
8388 reset the echo_area_buffer in question to nil at the end because
8389 with_echo_area_buffer will sets it to an empty buffer. */
8390 i = display_last_displayed_message_p ? 1 : 0;
8391 no_message_p = NILP (echo_area_buffer[i]);
8393 window_height_changed_p
8394 = with_echo_area_buffer (w, display_last_displayed_message_p,
8395 display_echo_area_1,
8396 (EMACS_INT) w, Qnil, 0, 0);
8398 if (no_message_p)
8399 echo_area_buffer[i] = Qnil;
8401 unbind_to (count, Qnil);
8402 return window_height_changed_p;
8406 /* Helper for display_echo_area. Display the current buffer which
8407 contains the current echo area message in window W, a mini-window,
8408 a pointer to which is passed in A1. A2..A4 are currently not used.
8409 Change the height of W so that all of the message is displayed.
8410 Value is non-zero if height of W was changed. */
8412 static int
8413 display_echo_area_1 (a1, a2, a3, a4)
8414 EMACS_INT a1;
8415 Lisp_Object a2;
8416 EMACS_INT a3, a4;
8418 struct window *w = (struct window *) a1;
8419 Lisp_Object window;
8420 struct text_pos start;
8421 int window_height_changed_p = 0;
8423 /* Do this before displaying, so that we have a large enough glyph
8424 matrix for the display. If we can't get enough space for the
8425 whole text, display the last N lines. That works by setting w->start. */
8426 window_height_changed_p = resize_mini_window (w, 0);
8428 /* Use the starting position chosen by resize_mini_window. */
8429 SET_TEXT_POS_FROM_MARKER (start, w->start);
8431 /* Display. */
8432 clear_glyph_matrix (w->desired_matrix);
8433 XSETWINDOW (window, w);
8434 try_window (window, start, 0);
8436 return window_height_changed_p;
8440 /* Resize the echo area window to exactly the size needed for the
8441 currently displayed message, if there is one. If a mini-buffer
8442 is active, don't shrink it. */
8444 void
8445 resize_echo_area_exactly ()
8447 if (BUFFERP (echo_area_buffer[0])
8448 && WINDOWP (echo_area_window))
8450 struct window *w = XWINDOW (echo_area_window);
8451 int resized_p;
8452 Lisp_Object resize_exactly;
8454 if (minibuf_level == 0)
8455 resize_exactly = Qt;
8456 else
8457 resize_exactly = Qnil;
8459 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8460 (EMACS_INT) w, resize_exactly, 0, 0);
8461 if (resized_p)
8463 ++windows_or_buffers_changed;
8464 ++update_mode_lines;
8465 redisplay_internal (0);
8471 /* Callback function for with_echo_area_buffer, when used from
8472 resize_echo_area_exactly. A1 contains a pointer to the window to
8473 resize, EXACTLY non-nil means resize the mini-window exactly to the
8474 size of the text displayed. A3 and A4 are not used. Value is what
8475 resize_mini_window returns. */
8477 static int
8478 resize_mini_window_1 (a1, exactly, a3, a4)
8479 EMACS_INT a1;
8480 Lisp_Object exactly;
8481 EMACS_INT a3, a4;
8483 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8487 /* Resize mini-window W to fit the size of its contents. EXACT_P
8488 means size the window exactly to the size needed. Otherwise, it's
8489 only enlarged until W's buffer is empty.
8491 Set W->start to the right place to begin display. If the whole
8492 contents fit, start at the beginning. Otherwise, start so as
8493 to make the end of the contents appear. This is particularly
8494 important for y-or-n-p, but seems desirable generally.
8496 Value is non-zero if the window height has been changed. */
8499 resize_mini_window (w, exact_p)
8500 struct window *w;
8501 int exact_p;
8503 struct frame *f = XFRAME (w->frame);
8504 int window_height_changed_p = 0;
8506 xassert (MINI_WINDOW_P (w));
8508 /* By default, start display at the beginning. */
8509 set_marker_both (w->start, w->buffer,
8510 BUF_BEGV (XBUFFER (w->buffer)),
8511 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8513 /* Don't resize windows while redisplaying a window; it would
8514 confuse redisplay functions when the size of the window they are
8515 displaying changes from under them. Such a resizing can happen,
8516 for instance, when which-func prints a long message while
8517 we are running fontification-functions. We're running these
8518 functions with safe_call which binds inhibit-redisplay to t. */
8519 if (!NILP (Vinhibit_redisplay))
8520 return 0;
8522 /* Nil means don't try to resize. */
8523 if (NILP (Vresize_mini_windows)
8524 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8525 return 0;
8527 if (!FRAME_MINIBUF_ONLY_P (f))
8529 struct it it;
8530 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8531 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8532 int height, max_height;
8533 int unit = FRAME_LINE_HEIGHT (f);
8534 struct text_pos start;
8535 struct buffer *old_current_buffer = NULL;
8537 if (current_buffer != XBUFFER (w->buffer))
8539 old_current_buffer = current_buffer;
8540 set_buffer_internal (XBUFFER (w->buffer));
8543 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8545 /* Compute the max. number of lines specified by the user. */
8546 if (FLOATP (Vmax_mini_window_height))
8547 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8548 else if (INTEGERP (Vmax_mini_window_height))
8549 max_height = XINT (Vmax_mini_window_height);
8550 else
8551 max_height = total_height / 4;
8553 /* Correct that max. height if it's bogus. */
8554 max_height = max (1, max_height);
8555 max_height = min (total_height, max_height);
8557 /* Find out the height of the text in the window. */
8558 if (it.truncate_lines_p)
8559 height = 1;
8560 else
8562 last_height = 0;
8563 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8564 if (it.max_ascent == 0 && it.max_descent == 0)
8565 height = it.current_y + last_height;
8566 else
8567 height = it.current_y + it.max_ascent + it.max_descent;
8568 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8569 height = (height + unit - 1) / unit;
8572 /* Compute a suitable window start. */
8573 if (height > max_height)
8575 height = max_height;
8576 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8577 move_it_vertically_backward (&it, (height - 1) * unit);
8578 start = it.current.pos;
8580 else
8581 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8582 SET_MARKER_FROM_TEXT_POS (w->start, start);
8584 if (EQ (Vresize_mini_windows, Qgrow_only))
8586 /* Let it grow only, until we display an empty message, in which
8587 case the window shrinks again. */
8588 if (height > WINDOW_TOTAL_LINES (w))
8590 int old_height = WINDOW_TOTAL_LINES (w);
8591 freeze_window_starts (f, 1);
8592 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8593 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8595 else if (height < WINDOW_TOTAL_LINES (w)
8596 && (exact_p || BEGV == ZV))
8598 int old_height = WINDOW_TOTAL_LINES (w);
8599 freeze_window_starts (f, 0);
8600 shrink_mini_window (w);
8601 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8604 else
8606 /* Always resize to exact size needed. */
8607 if (height > WINDOW_TOTAL_LINES (w))
8609 int old_height = WINDOW_TOTAL_LINES (w);
8610 freeze_window_starts (f, 1);
8611 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8612 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8614 else if (height < WINDOW_TOTAL_LINES (w))
8616 int old_height = WINDOW_TOTAL_LINES (w);
8617 freeze_window_starts (f, 0);
8618 shrink_mini_window (w);
8620 if (height)
8622 freeze_window_starts (f, 1);
8623 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8626 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8630 if (old_current_buffer)
8631 set_buffer_internal (old_current_buffer);
8634 return window_height_changed_p;
8638 /* Value is the current message, a string, or nil if there is no
8639 current message. */
8641 Lisp_Object
8642 current_message ()
8644 Lisp_Object msg;
8646 if (!BUFFERP (echo_area_buffer[0]))
8647 msg = Qnil;
8648 else
8650 with_echo_area_buffer (0, 0, current_message_1,
8651 (EMACS_INT) &msg, Qnil, 0, 0);
8652 if (NILP (msg))
8653 echo_area_buffer[0] = Qnil;
8656 return msg;
8660 static int
8661 current_message_1 (a1, a2, a3, a4)
8662 EMACS_INT a1;
8663 Lisp_Object a2;
8664 EMACS_INT a3, a4;
8666 Lisp_Object *msg = (Lisp_Object *) a1;
8668 if (Z > BEG)
8669 *msg = make_buffer_string (BEG, Z, 1);
8670 else
8671 *msg = Qnil;
8672 return 0;
8676 /* Push the current message on Vmessage_stack for later restauration
8677 by restore_message. Value is non-zero if the current message isn't
8678 empty. This is a relatively infrequent operation, so it's not
8679 worth optimizing. */
8682 push_message ()
8684 Lisp_Object msg;
8685 msg = current_message ();
8686 Vmessage_stack = Fcons (msg, Vmessage_stack);
8687 return STRINGP (msg);
8691 /* Restore message display from the top of Vmessage_stack. */
8693 void
8694 restore_message ()
8696 Lisp_Object msg;
8698 xassert (CONSP (Vmessage_stack));
8699 msg = XCAR (Vmessage_stack);
8700 if (STRINGP (msg))
8701 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8702 else
8703 message3_nolog (msg, 0, 0);
8707 /* Handler for record_unwind_protect calling pop_message. */
8709 Lisp_Object
8710 pop_message_unwind (dummy)
8711 Lisp_Object dummy;
8713 pop_message ();
8714 return Qnil;
8717 /* Pop the top-most entry off Vmessage_stack. */
8719 void
8720 pop_message ()
8722 xassert (CONSP (Vmessage_stack));
8723 Vmessage_stack = XCDR (Vmessage_stack);
8727 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8728 exits. If the stack is not empty, we have a missing pop_message
8729 somewhere. */
8731 void
8732 check_message_stack ()
8734 if (!NILP (Vmessage_stack))
8735 abort ();
8739 /* Truncate to NCHARS what will be displayed in the echo area the next
8740 time we display it---but don't redisplay it now. */
8742 void
8743 truncate_echo_area (nchars)
8744 int nchars;
8746 if (nchars == 0)
8747 echo_area_buffer[0] = Qnil;
8748 /* A null message buffer means that the frame hasn't really been
8749 initialized yet. Error messages get reported properly by
8750 cmd_error, so this must be just an informative message; toss it. */
8751 else if (!noninteractive
8752 && INTERACTIVE
8753 && !NILP (echo_area_buffer[0]))
8755 struct frame *sf = SELECTED_FRAME ();
8756 if (FRAME_MESSAGE_BUF (sf))
8757 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8762 /* Helper function for truncate_echo_area. Truncate the current
8763 message to at most NCHARS characters. */
8765 static int
8766 truncate_message_1 (nchars, a2, a3, a4)
8767 EMACS_INT nchars;
8768 Lisp_Object a2;
8769 EMACS_INT a3, a4;
8771 if (BEG + nchars < Z)
8772 del_range (BEG + nchars, Z);
8773 if (Z == BEG)
8774 echo_area_buffer[0] = Qnil;
8775 return 0;
8779 /* Set the current message to a substring of S or STRING.
8781 If STRING is a Lisp string, set the message to the first NBYTES
8782 bytes from STRING. NBYTES zero means use the whole string. If
8783 STRING is multibyte, the message will be displayed multibyte.
8785 If S is not null, set the message to the first LEN bytes of S. LEN
8786 zero means use the whole string. MULTIBYTE_P non-zero means S is
8787 multibyte. Display the message multibyte in that case.
8789 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8790 to t before calling set_message_1 (which calls insert).
8793 void
8794 set_message (s, string, nbytes, multibyte_p)
8795 const char *s;
8796 Lisp_Object string;
8797 int nbytes, multibyte_p;
8799 message_enable_multibyte
8800 = ((s && multibyte_p)
8801 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8803 with_echo_area_buffer (0, -1, set_message_1,
8804 (EMACS_INT) s, string, nbytes, multibyte_p);
8805 message_buf_print = 0;
8806 help_echo_showing_p = 0;
8810 /* Helper function for set_message. Arguments have the same meaning
8811 as there, with A1 corresponding to S and A2 corresponding to STRING
8812 This function is called with the echo area buffer being
8813 current. */
8815 static int
8816 set_message_1 (a1, a2, nbytes, multibyte_p)
8817 EMACS_INT a1;
8818 Lisp_Object a2;
8819 EMACS_INT nbytes, multibyte_p;
8821 const char *s = (const char *) a1;
8822 Lisp_Object string = a2;
8824 /* Change multibyteness of the echo buffer appropriately. */
8825 if (message_enable_multibyte
8826 != !NILP (current_buffer->enable_multibyte_characters))
8827 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8829 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8831 /* Insert new message at BEG. */
8832 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8834 if (STRINGP (string))
8836 int nchars;
8838 if (nbytes == 0)
8839 nbytes = SBYTES (string);
8840 nchars = string_byte_to_char (string, nbytes);
8842 /* This function takes care of single/multibyte conversion. We
8843 just have to ensure that the echo area buffer has the right
8844 setting of enable_multibyte_characters. */
8845 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8847 else if (s)
8849 if (nbytes == 0)
8850 nbytes = strlen (s);
8852 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8854 /* Convert from multi-byte to single-byte. */
8855 int i, c, n;
8856 unsigned char work[1];
8858 /* Convert a multibyte string to single-byte. */
8859 for (i = 0; i < nbytes; i += n)
8861 c = string_char_and_length (s + i, nbytes - i, &n);
8862 work[0] = (ASCII_CHAR_P (c)
8864 : multibyte_char_to_unibyte (c, Qnil));
8865 insert_1_both (work, 1, 1, 1, 0, 0);
8868 else if (!multibyte_p
8869 && !NILP (current_buffer->enable_multibyte_characters))
8871 /* Convert from single-byte to multi-byte. */
8872 int i, c, n;
8873 const unsigned char *msg = (const unsigned char *) s;
8874 unsigned char str[MAX_MULTIBYTE_LENGTH];
8876 /* Convert a single-byte string to multibyte. */
8877 for (i = 0; i < nbytes; i++)
8879 c = msg[i];
8880 c = unibyte_char_to_multibyte (c);
8881 n = CHAR_STRING (c, str);
8882 insert_1_both (str, 1, n, 1, 0, 0);
8885 else
8886 insert_1 (s, nbytes, 1, 0, 0);
8889 return 0;
8893 /* Clear messages. CURRENT_P non-zero means clear the current
8894 message. LAST_DISPLAYED_P non-zero means clear the message
8895 last displayed. */
8897 void
8898 clear_message (current_p, last_displayed_p)
8899 int current_p, last_displayed_p;
8901 if (current_p)
8903 echo_area_buffer[0] = Qnil;
8904 message_cleared_p = 1;
8907 if (last_displayed_p)
8908 echo_area_buffer[1] = Qnil;
8910 message_buf_print = 0;
8913 /* Clear garbaged frames.
8915 This function is used where the old redisplay called
8916 redraw_garbaged_frames which in turn called redraw_frame which in
8917 turn called clear_frame. The call to clear_frame was a source of
8918 flickering. I believe a clear_frame is not necessary. It should
8919 suffice in the new redisplay to invalidate all current matrices,
8920 and ensure a complete redisplay of all windows. */
8922 static void
8923 clear_garbaged_frames ()
8925 if (frame_garbaged)
8927 Lisp_Object tail, frame;
8928 int changed_count = 0;
8930 FOR_EACH_FRAME (tail, frame)
8932 struct frame *f = XFRAME (frame);
8934 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8936 if (f->resized_p)
8938 Fredraw_frame (frame);
8939 f->force_flush_display_p = 1;
8941 clear_current_matrices (f);
8942 changed_count++;
8943 f->garbaged = 0;
8944 f->resized_p = 0;
8948 frame_garbaged = 0;
8949 if (changed_count)
8950 ++windows_or_buffers_changed;
8955 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8956 is non-zero update selected_frame. Value is non-zero if the
8957 mini-windows height has been changed. */
8959 static int
8960 echo_area_display (update_frame_p)
8961 int update_frame_p;
8963 Lisp_Object mini_window;
8964 struct window *w;
8965 struct frame *f;
8966 int window_height_changed_p = 0;
8967 struct frame *sf = SELECTED_FRAME ();
8969 mini_window = FRAME_MINIBUF_WINDOW (sf);
8970 w = XWINDOW (mini_window);
8971 f = XFRAME (WINDOW_FRAME (w));
8973 /* Don't display if frame is invisible or not yet initialized. */
8974 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8975 return 0;
8977 #ifdef HAVE_WINDOW_SYSTEM
8978 /* When Emacs starts, selected_frame may be the initial terminal
8979 frame. If we let this through, a message would be displayed on
8980 the terminal. */
8981 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8982 return 0;
8983 #endif /* HAVE_WINDOW_SYSTEM */
8985 /* Redraw garbaged frames. */
8986 if (frame_garbaged)
8987 clear_garbaged_frames ();
8989 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8991 echo_area_window = mini_window;
8992 window_height_changed_p = display_echo_area (w);
8993 w->must_be_updated_p = 1;
8995 /* Update the display, unless called from redisplay_internal.
8996 Also don't update the screen during redisplay itself. The
8997 update will happen at the end of redisplay, and an update
8998 here could cause confusion. */
8999 if (update_frame_p && !redisplaying_p)
9001 int n = 0;
9003 /* If the display update has been interrupted by pending
9004 input, update mode lines in the frame. Due to the
9005 pending input, it might have been that redisplay hasn't
9006 been called, so that mode lines above the echo area are
9007 garbaged. This looks odd, so we prevent it here. */
9008 if (!display_completed)
9009 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9011 if (window_height_changed_p
9012 /* Don't do this if Emacs is shutting down. Redisplay
9013 needs to run hooks. */
9014 && !NILP (Vrun_hooks))
9016 /* Must update other windows. Likewise as in other
9017 cases, don't let this update be interrupted by
9018 pending input. */
9019 int count = SPECPDL_INDEX ();
9020 specbind (Qredisplay_dont_pause, Qt);
9021 windows_or_buffers_changed = 1;
9022 redisplay_internal (0);
9023 unbind_to (count, Qnil);
9025 else if (FRAME_WINDOW_P (f) && n == 0)
9027 /* Window configuration is the same as before.
9028 Can do with a display update of the echo area,
9029 unless we displayed some mode lines. */
9030 update_single_window (w, 1);
9031 FRAME_RIF (f)->flush_display (f);
9033 else
9034 update_frame (f, 1, 1);
9036 /* If cursor is in the echo area, make sure that the next
9037 redisplay displays the minibuffer, so that the cursor will
9038 be replaced with what the minibuffer wants. */
9039 if (cursor_in_echo_area)
9040 ++windows_or_buffers_changed;
9043 else if (!EQ (mini_window, selected_window))
9044 windows_or_buffers_changed++;
9046 /* Last displayed message is now the current message. */
9047 echo_area_buffer[1] = echo_area_buffer[0];
9048 /* Inform read_char that we're not echoing. */
9049 echo_message_buffer = Qnil;
9051 /* Prevent redisplay optimization in redisplay_internal by resetting
9052 this_line_start_pos. This is done because the mini-buffer now
9053 displays the message instead of its buffer text. */
9054 if (EQ (mini_window, selected_window))
9055 CHARPOS (this_line_start_pos) = 0;
9057 return window_height_changed_p;
9062 /***********************************************************************
9063 Mode Lines and Frame Titles
9064 ***********************************************************************/
9066 /* A buffer for constructing non-propertized mode-line strings and
9067 frame titles in it; allocated from the heap in init_xdisp and
9068 resized as needed in store_mode_line_noprop_char. */
9070 static char *mode_line_noprop_buf;
9072 /* The buffer's end, and a current output position in it. */
9074 static char *mode_line_noprop_buf_end;
9075 static char *mode_line_noprop_ptr;
9077 #define MODE_LINE_NOPROP_LEN(start) \
9078 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9080 static enum {
9081 MODE_LINE_DISPLAY = 0,
9082 MODE_LINE_TITLE,
9083 MODE_LINE_NOPROP,
9084 MODE_LINE_STRING
9085 } mode_line_target;
9087 /* Alist that caches the results of :propertize.
9088 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9089 static Lisp_Object mode_line_proptrans_alist;
9091 /* List of strings making up the mode-line. */
9092 static Lisp_Object mode_line_string_list;
9094 /* Base face property when building propertized mode line string. */
9095 static Lisp_Object mode_line_string_face;
9096 static Lisp_Object mode_line_string_face_prop;
9099 /* Unwind data for mode line strings */
9101 static Lisp_Object Vmode_line_unwind_vector;
9103 static Lisp_Object
9104 format_mode_line_unwind_data (struct buffer *obuf,
9105 Lisp_Object owin,
9106 int save_proptrans)
9108 Lisp_Object vector, tmp;
9110 /* Reduce consing by keeping one vector in
9111 Vwith_echo_area_save_vector. */
9112 vector = Vmode_line_unwind_vector;
9113 Vmode_line_unwind_vector = Qnil;
9115 if (NILP (vector))
9116 vector = Fmake_vector (make_number (8), Qnil);
9118 ASET (vector, 0, make_number (mode_line_target));
9119 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9120 ASET (vector, 2, mode_line_string_list);
9121 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9122 ASET (vector, 4, mode_line_string_face);
9123 ASET (vector, 5, mode_line_string_face_prop);
9125 if (obuf)
9126 XSETBUFFER (tmp, obuf);
9127 else
9128 tmp = Qnil;
9129 ASET (vector, 6, tmp);
9130 ASET (vector, 7, owin);
9132 return vector;
9135 static Lisp_Object
9136 unwind_format_mode_line (vector)
9137 Lisp_Object vector;
9139 mode_line_target = XINT (AREF (vector, 0));
9140 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9141 mode_line_string_list = AREF (vector, 2);
9142 if (! EQ (AREF (vector, 3), Qt))
9143 mode_line_proptrans_alist = AREF (vector, 3);
9144 mode_line_string_face = AREF (vector, 4);
9145 mode_line_string_face_prop = AREF (vector, 5);
9147 if (!NILP (AREF (vector, 7)))
9148 /* Select window before buffer, since it may change the buffer. */
9149 Fselect_window (AREF (vector, 7), Qt);
9151 if (!NILP (AREF (vector, 6)))
9153 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9154 ASET (vector, 6, Qnil);
9157 Vmode_line_unwind_vector = vector;
9158 return Qnil;
9162 /* Store a single character C for the frame title in mode_line_noprop_buf.
9163 Re-allocate mode_line_noprop_buf if necessary. */
9165 static void
9166 #ifdef PROTOTYPES
9167 store_mode_line_noprop_char (char c)
9168 #else
9169 store_mode_line_noprop_char (c)
9170 char c;
9171 #endif
9173 /* If output position has reached the end of the allocated buffer,
9174 double the buffer's size. */
9175 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9177 int len = MODE_LINE_NOPROP_LEN (0);
9178 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9179 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9180 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9181 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9184 *mode_line_noprop_ptr++ = c;
9188 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9189 mode_line_noprop_ptr. STR is the string to store. Do not copy
9190 characters that yield more columns than PRECISION; PRECISION <= 0
9191 means copy the whole string. Pad with spaces until FIELD_WIDTH
9192 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9193 pad. Called from display_mode_element when it is used to build a
9194 frame title. */
9196 static int
9197 store_mode_line_noprop (str, field_width, precision)
9198 const unsigned char *str;
9199 int field_width, precision;
9201 int n = 0;
9202 int dummy, nbytes;
9204 /* Copy at most PRECISION chars from STR. */
9205 nbytes = strlen (str);
9206 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9207 while (nbytes--)
9208 store_mode_line_noprop_char (*str++);
9210 /* Fill up with spaces until FIELD_WIDTH reached. */
9211 while (field_width > 0
9212 && n < field_width)
9214 store_mode_line_noprop_char (' ');
9215 ++n;
9218 return n;
9221 /***********************************************************************
9222 Frame Titles
9223 ***********************************************************************/
9225 #ifdef HAVE_WINDOW_SYSTEM
9227 /* Set the title of FRAME, if it has changed. The title format is
9228 Vicon_title_format if FRAME is iconified, otherwise it is
9229 frame_title_format. */
9231 static void
9232 x_consider_frame_title (frame)
9233 Lisp_Object frame;
9235 struct frame *f = XFRAME (frame);
9237 if (FRAME_WINDOW_P (f)
9238 || FRAME_MINIBUF_ONLY_P (f)
9239 || f->explicit_name)
9241 /* Do we have more than one visible frame on this X display? */
9242 Lisp_Object tail;
9243 Lisp_Object fmt;
9244 int title_start;
9245 char *title;
9246 int len;
9247 struct it it;
9248 int count = SPECPDL_INDEX ();
9250 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9252 Lisp_Object other_frame = XCAR (tail);
9253 struct frame *tf = XFRAME (other_frame);
9255 if (tf != f
9256 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9257 && !FRAME_MINIBUF_ONLY_P (tf)
9258 && !EQ (other_frame, tip_frame)
9259 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9260 break;
9263 /* Set global variable indicating that multiple frames exist. */
9264 multiple_frames = CONSP (tail);
9266 /* Switch to the buffer of selected window of the frame. Set up
9267 mode_line_target so that display_mode_element will output into
9268 mode_line_noprop_buf; then display the title. */
9269 record_unwind_protect (unwind_format_mode_line,
9270 format_mode_line_unwind_data
9271 (current_buffer, selected_window, 0));
9273 Fselect_window (f->selected_window, Qt);
9274 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9275 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9277 mode_line_target = MODE_LINE_TITLE;
9278 title_start = MODE_LINE_NOPROP_LEN (0);
9279 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9280 NULL, DEFAULT_FACE_ID);
9281 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9282 len = MODE_LINE_NOPROP_LEN (title_start);
9283 title = mode_line_noprop_buf + title_start;
9284 unbind_to (count, Qnil);
9286 /* Set the title only if it's changed. This avoids consing in
9287 the common case where it hasn't. (If it turns out that we've
9288 already wasted too much time by walking through the list with
9289 display_mode_element, then we might need to optimize at a
9290 higher level than this.) */
9291 if (! STRINGP (f->name)
9292 || SBYTES (f->name) != len
9293 || bcmp (title, SDATA (f->name), len) != 0)
9294 x_implicitly_set_name (f, make_string (title, len), Qnil);
9298 #endif /* not HAVE_WINDOW_SYSTEM */
9303 /***********************************************************************
9304 Menu Bars
9305 ***********************************************************************/
9308 /* Prepare for redisplay by updating menu-bar item lists when
9309 appropriate. This can call eval. */
9311 void
9312 prepare_menu_bars ()
9314 int all_windows;
9315 struct gcpro gcpro1, gcpro2;
9316 struct frame *f;
9317 Lisp_Object tooltip_frame;
9319 #ifdef HAVE_WINDOW_SYSTEM
9320 tooltip_frame = tip_frame;
9321 #else
9322 tooltip_frame = Qnil;
9323 #endif
9325 /* Update all frame titles based on their buffer names, etc. We do
9326 this before the menu bars so that the buffer-menu will show the
9327 up-to-date frame titles. */
9328 #ifdef HAVE_WINDOW_SYSTEM
9329 if (windows_or_buffers_changed || update_mode_lines)
9331 Lisp_Object tail, frame;
9333 FOR_EACH_FRAME (tail, frame)
9335 f = XFRAME (frame);
9336 if (!EQ (frame, tooltip_frame)
9337 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9338 x_consider_frame_title (frame);
9341 #endif /* HAVE_WINDOW_SYSTEM */
9343 /* Update the menu bar item lists, if appropriate. This has to be
9344 done before any actual redisplay or generation of display lines. */
9345 all_windows = (update_mode_lines
9346 || buffer_shared > 1
9347 || windows_or_buffers_changed);
9348 if (all_windows)
9350 Lisp_Object tail, frame;
9351 int count = SPECPDL_INDEX ();
9352 /* 1 means that update_menu_bar has run its hooks
9353 so any further calls to update_menu_bar shouldn't do so again. */
9354 int menu_bar_hooks_run = 0;
9356 record_unwind_save_match_data ();
9358 FOR_EACH_FRAME (tail, frame)
9360 f = XFRAME (frame);
9362 /* Ignore tooltip frame. */
9363 if (EQ (frame, tooltip_frame))
9364 continue;
9366 /* If a window on this frame changed size, report that to
9367 the user and clear the size-change flag. */
9368 if (FRAME_WINDOW_SIZES_CHANGED (f))
9370 Lisp_Object functions;
9372 /* Clear flag first in case we get an error below. */
9373 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9374 functions = Vwindow_size_change_functions;
9375 GCPRO2 (tail, functions);
9377 while (CONSP (functions))
9379 call1 (XCAR (functions), frame);
9380 functions = XCDR (functions);
9382 UNGCPRO;
9385 GCPRO1 (tail);
9386 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9387 #ifdef HAVE_WINDOW_SYSTEM
9388 update_tool_bar (f, 0);
9389 #ifdef MAC_OS
9390 mac_update_title_bar (f, 0);
9391 #endif
9392 #endif
9393 UNGCPRO;
9396 unbind_to (count, Qnil);
9398 else
9400 struct frame *sf = SELECTED_FRAME ();
9401 update_menu_bar (sf, 1, 0);
9402 #ifdef HAVE_WINDOW_SYSTEM
9403 update_tool_bar (sf, 1);
9404 #ifdef MAC_OS
9405 mac_update_title_bar (sf, 1);
9406 #endif
9407 #endif
9410 /* Motif needs this. See comment in xmenu.c. Turn it off when
9411 pending_menu_activation is not defined. */
9412 #ifdef USE_X_TOOLKIT
9413 pending_menu_activation = 0;
9414 #endif
9418 /* Update the menu bar item list for frame F. This has to be done
9419 before we start to fill in any display lines, because it can call
9420 eval.
9422 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9424 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9425 already ran the menu bar hooks for this redisplay, so there
9426 is no need to run them again. The return value is the
9427 updated value of this flag, to pass to the next call. */
9429 static int
9430 update_menu_bar (f, save_match_data, hooks_run)
9431 struct frame *f;
9432 int save_match_data;
9433 int hooks_run;
9435 Lisp_Object window;
9436 register struct window *w;
9438 /* If called recursively during a menu update, do nothing. This can
9439 happen when, for instance, an activate-menubar-hook causes a
9440 redisplay. */
9441 if (inhibit_menubar_update)
9442 return hooks_run;
9444 window = FRAME_SELECTED_WINDOW (f);
9445 w = XWINDOW (window);
9447 #if 0 /* The if statement below this if statement used to include the
9448 condition !NILP (w->update_mode_line), rather than using
9449 update_mode_lines directly, and this if statement may have
9450 been added to make that condition work. Now the if
9451 statement below matches its comment, this isn't needed. */
9452 if (update_mode_lines)
9453 w->update_mode_line = Qt;
9454 #endif
9456 if (FRAME_WINDOW_P (f)
9458 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9459 || defined (USE_GTK)
9460 FRAME_EXTERNAL_MENU_BAR (f)
9461 #else
9462 FRAME_MENU_BAR_LINES (f) > 0
9463 #endif
9464 : FRAME_MENU_BAR_LINES (f) > 0)
9466 /* If the user has switched buffers or windows, we need to
9467 recompute to reflect the new bindings. But we'll
9468 recompute when update_mode_lines is set too; that means
9469 that people can use force-mode-line-update to request
9470 that the menu bar be recomputed. The adverse effect on
9471 the rest of the redisplay algorithm is about the same as
9472 windows_or_buffers_changed anyway. */
9473 if (windows_or_buffers_changed
9474 /* This used to test w->update_mode_line, but we believe
9475 there is no need to recompute the menu in that case. */
9476 || update_mode_lines
9477 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9478 < BUF_MODIFF (XBUFFER (w->buffer)))
9479 != !NILP (w->last_had_star))
9480 || ((!NILP (Vtransient_mark_mode)
9481 && !NILP (XBUFFER (w->buffer)->mark_active))
9482 != !NILP (w->region_showing)))
9484 struct buffer *prev = current_buffer;
9485 int count = SPECPDL_INDEX ();
9487 specbind (Qinhibit_menubar_update, Qt);
9489 set_buffer_internal_1 (XBUFFER (w->buffer));
9490 if (save_match_data)
9491 record_unwind_save_match_data ();
9492 if (NILP (Voverriding_local_map_menu_flag))
9494 specbind (Qoverriding_terminal_local_map, Qnil);
9495 specbind (Qoverriding_local_map, Qnil);
9498 if (!hooks_run)
9500 /* Run the Lucid hook. */
9501 safe_run_hooks (Qactivate_menubar_hook);
9503 /* If it has changed current-menubar from previous value,
9504 really recompute the menu-bar from the value. */
9505 if (! NILP (Vlucid_menu_bar_dirty_flag))
9506 call0 (Qrecompute_lucid_menubar);
9508 safe_run_hooks (Qmenu_bar_update_hook);
9510 hooks_run = 1;
9513 XSETFRAME (Vmenu_updating_frame, f);
9514 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9516 /* Redisplay the menu bar in case we changed it. */
9517 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9518 || defined (USE_GTK)
9519 if (FRAME_WINDOW_P (f))
9521 #ifdef MAC_OS
9522 /* All frames on Mac OS share the same menubar. So only
9523 the selected frame should be allowed to set it. */
9524 if (f == SELECTED_FRAME ())
9525 #endif
9526 set_frame_menubar (f, 0, 0);
9528 else
9529 /* On a terminal screen, the menu bar is an ordinary screen
9530 line, and this makes it get updated. */
9531 w->update_mode_line = Qt;
9532 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9533 /* In the non-toolkit version, the menu bar is an ordinary screen
9534 line, and this makes it get updated. */
9535 w->update_mode_line = Qt;
9536 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9538 unbind_to (count, Qnil);
9539 set_buffer_internal_1 (prev);
9543 return hooks_run;
9548 /***********************************************************************
9549 Output Cursor
9550 ***********************************************************************/
9552 #ifdef HAVE_WINDOW_SYSTEM
9554 /* EXPORT:
9555 Nominal cursor position -- where to draw output.
9556 HPOS and VPOS are window relative glyph matrix coordinates.
9557 X and Y are window relative pixel coordinates. */
9559 struct cursor_pos output_cursor;
9562 /* EXPORT:
9563 Set the global variable output_cursor to CURSOR. All cursor
9564 positions are relative to updated_window. */
9566 void
9567 set_output_cursor (cursor)
9568 struct cursor_pos *cursor;
9570 output_cursor.hpos = cursor->hpos;
9571 output_cursor.vpos = cursor->vpos;
9572 output_cursor.x = cursor->x;
9573 output_cursor.y = cursor->y;
9577 /* EXPORT for RIF:
9578 Set a nominal cursor position.
9580 HPOS and VPOS are column/row positions in a window glyph matrix. X
9581 and Y are window text area relative pixel positions.
9583 If this is done during an update, updated_window will contain the
9584 window that is being updated and the position is the future output
9585 cursor position for that window. If updated_window is null, use
9586 selected_window and display the cursor at the given position. */
9588 void
9589 x_cursor_to (vpos, hpos, y, x)
9590 int vpos, hpos, y, x;
9592 struct window *w;
9594 /* If updated_window is not set, work on selected_window. */
9595 if (updated_window)
9596 w = updated_window;
9597 else
9598 w = XWINDOW (selected_window);
9600 /* Set the output cursor. */
9601 output_cursor.hpos = hpos;
9602 output_cursor.vpos = vpos;
9603 output_cursor.x = x;
9604 output_cursor.y = y;
9606 /* If not called as part of an update, really display the cursor.
9607 This will also set the cursor position of W. */
9608 if (updated_window == NULL)
9610 BLOCK_INPUT;
9611 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9612 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9613 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9614 UNBLOCK_INPUT;
9618 #endif /* HAVE_WINDOW_SYSTEM */
9621 /***********************************************************************
9622 Tool-bars
9623 ***********************************************************************/
9625 #ifdef HAVE_WINDOW_SYSTEM
9627 /* Where the mouse was last time we reported a mouse event. */
9629 FRAME_PTR last_mouse_frame;
9631 /* Tool-bar item index of the item on which a mouse button was pressed
9632 or -1. */
9634 int last_tool_bar_item;
9637 /* Update the tool-bar item list for frame F. This has to be done
9638 before we start to fill in any display lines. Called from
9639 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9640 and restore it here. */
9642 static void
9643 update_tool_bar (f, save_match_data)
9644 struct frame *f;
9645 int save_match_data;
9647 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9648 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9649 #else
9650 int do_update = WINDOWP (f->tool_bar_window)
9651 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9652 #endif
9654 if (do_update)
9656 Lisp_Object window;
9657 struct window *w;
9659 window = FRAME_SELECTED_WINDOW (f);
9660 w = XWINDOW (window);
9662 /* If the user has switched buffers or windows, we need to
9663 recompute to reflect the new bindings. But we'll
9664 recompute when update_mode_lines is set too; that means
9665 that people can use force-mode-line-update to request
9666 that the menu bar be recomputed. The adverse effect on
9667 the rest of the redisplay algorithm is about the same as
9668 windows_or_buffers_changed anyway. */
9669 if (windows_or_buffers_changed
9670 || !NILP (w->update_mode_line)
9671 || update_mode_lines
9672 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9673 < BUF_MODIFF (XBUFFER (w->buffer)))
9674 != !NILP (w->last_had_star))
9675 || ((!NILP (Vtransient_mark_mode)
9676 && !NILP (XBUFFER (w->buffer)->mark_active))
9677 != !NILP (w->region_showing)))
9679 struct buffer *prev = current_buffer;
9680 int count = SPECPDL_INDEX ();
9681 Lisp_Object new_tool_bar;
9682 int new_n_tool_bar;
9683 struct gcpro gcpro1;
9685 /* Set current_buffer to the buffer of the selected
9686 window of the frame, so that we get the right local
9687 keymaps. */
9688 set_buffer_internal_1 (XBUFFER (w->buffer));
9690 /* Save match data, if we must. */
9691 if (save_match_data)
9692 record_unwind_save_match_data ();
9694 /* Make sure that we don't accidentally use bogus keymaps. */
9695 if (NILP (Voverriding_local_map_menu_flag))
9697 specbind (Qoverriding_terminal_local_map, Qnil);
9698 specbind (Qoverriding_local_map, Qnil);
9701 GCPRO1 (new_tool_bar);
9703 /* Build desired tool-bar items from keymaps. */
9704 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9705 &new_n_tool_bar);
9707 /* Redisplay the tool-bar if we changed it. */
9708 if (new_n_tool_bar != f->n_tool_bar_items
9709 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9711 /* Redisplay that happens asynchronously due to an expose event
9712 may access f->tool_bar_items. Make sure we update both
9713 variables within BLOCK_INPUT so no such event interrupts. */
9714 BLOCK_INPUT;
9715 f->tool_bar_items = new_tool_bar;
9716 f->n_tool_bar_items = new_n_tool_bar;
9717 w->update_mode_line = Qt;
9718 UNBLOCK_INPUT;
9721 UNGCPRO;
9723 unbind_to (count, Qnil);
9724 set_buffer_internal_1 (prev);
9730 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9731 F's desired tool-bar contents. F->tool_bar_items must have
9732 been set up previously by calling prepare_menu_bars. */
9734 static void
9735 build_desired_tool_bar_string (f)
9736 struct frame *f;
9738 int i, size, size_needed;
9739 struct gcpro gcpro1, gcpro2, gcpro3;
9740 Lisp_Object image, plist, props;
9742 image = plist = props = Qnil;
9743 GCPRO3 (image, plist, props);
9745 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9746 Otherwise, make a new string. */
9748 /* The size of the string we might be able to reuse. */
9749 size = (STRINGP (f->desired_tool_bar_string)
9750 ? SCHARS (f->desired_tool_bar_string)
9751 : 0);
9753 /* We need one space in the string for each image. */
9754 size_needed = f->n_tool_bar_items;
9756 /* Reuse f->desired_tool_bar_string, if possible. */
9757 if (size < size_needed || NILP (f->desired_tool_bar_string))
9758 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9759 make_number (' '));
9760 else
9762 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9763 Fremove_text_properties (make_number (0), make_number (size),
9764 props, f->desired_tool_bar_string);
9767 /* Put a `display' property on the string for the images to display,
9768 put a `menu_item' property on tool-bar items with a value that
9769 is the index of the item in F's tool-bar item vector. */
9770 for (i = 0; i < f->n_tool_bar_items; ++i)
9772 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9774 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9775 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9776 int hmargin, vmargin, relief, idx, end;
9777 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9779 /* If image is a vector, choose the image according to the
9780 button state. */
9781 image = PROP (TOOL_BAR_ITEM_IMAGES);
9782 if (VECTORP (image))
9784 if (enabled_p)
9785 idx = (selected_p
9786 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9787 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9788 else
9789 idx = (selected_p
9790 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9791 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9793 xassert (ASIZE (image) >= idx);
9794 image = AREF (image, idx);
9796 else
9797 idx = -1;
9799 /* Ignore invalid image specifications. */
9800 if (!valid_image_p (image))
9801 continue;
9803 /* Display the tool-bar button pressed, or depressed. */
9804 plist = Fcopy_sequence (XCDR (image));
9806 /* Compute margin and relief to draw. */
9807 relief = (tool_bar_button_relief >= 0
9808 ? tool_bar_button_relief
9809 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9810 hmargin = vmargin = relief;
9812 if (INTEGERP (Vtool_bar_button_margin)
9813 && XINT (Vtool_bar_button_margin) > 0)
9815 hmargin += XFASTINT (Vtool_bar_button_margin);
9816 vmargin += XFASTINT (Vtool_bar_button_margin);
9818 else if (CONSP (Vtool_bar_button_margin))
9820 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9821 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9822 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9824 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9825 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9826 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9829 if (auto_raise_tool_bar_buttons_p)
9831 /* Add a `:relief' property to the image spec if the item is
9832 selected. */
9833 if (selected_p)
9835 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9836 hmargin -= relief;
9837 vmargin -= relief;
9840 else
9842 /* If image is selected, display it pressed, i.e. with a
9843 negative relief. If it's not selected, display it with a
9844 raised relief. */
9845 plist = Fplist_put (plist, QCrelief,
9846 (selected_p
9847 ? make_number (-relief)
9848 : make_number (relief)));
9849 hmargin -= relief;
9850 vmargin -= relief;
9853 /* Put a margin around the image. */
9854 if (hmargin || vmargin)
9856 if (hmargin == vmargin)
9857 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9858 else
9859 plist = Fplist_put (plist, QCmargin,
9860 Fcons (make_number (hmargin),
9861 make_number (vmargin)));
9864 /* If button is not enabled, and we don't have special images
9865 for the disabled state, make the image appear disabled by
9866 applying an appropriate algorithm to it. */
9867 if (!enabled_p && idx < 0)
9868 plist = Fplist_put (plist, QCconversion, Qdisabled);
9870 /* Put a `display' text property on the string for the image to
9871 display. Put a `menu-item' property on the string that gives
9872 the start of this item's properties in the tool-bar items
9873 vector. */
9874 image = Fcons (Qimage, plist);
9875 props = list4 (Qdisplay, image,
9876 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9878 /* Let the last image hide all remaining spaces in the tool bar
9879 string. The string can be longer than needed when we reuse a
9880 previous string. */
9881 if (i + 1 == f->n_tool_bar_items)
9882 end = SCHARS (f->desired_tool_bar_string);
9883 else
9884 end = i + 1;
9885 Fadd_text_properties (make_number (i), make_number (end),
9886 props, f->desired_tool_bar_string);
9887 #undef PROP
9890 UNGCPRO;
9894 /* Display one line of the tool-bar of frame IT->f.
9896 HEIGHT specifies the desired height of the tool-bar line.
9897 If the actual height of the glyph row is less than HEIGHT, the
9898 row's height is increased to HEIGHT, and the icons are centered
9899 vertically in the new height.
9901 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9902 count a final empty row in case the tool-bar width exactly matches
9903 the window width.
9906 static void
9907 display_tool_bar_line (it, height)
9908 struct it *it;
9909 int height;
9911 struct glyph_row *row = it->glyph_row;
9912 int max_x = it->last_visible_x;
9913 struct glyph *last;
9915 prepare_desired_row (row);
9916 row->y = it->current_y;
9918 /* Note that this isn't made use of if the face hasn't a box,
9919 so there's no need to check the face here. */
9920 it->start_of_box_run_p = 1;
9922 while (it->current_x < max_x)
9924 int x, n_glyphs_before, i, nglyphs;
9925 struct it it_before;
9927 /* Get the next display element. */
9928 if (!get_next_display_element (it))
9930 /* Don't count empty row if we are counting needed tool-bar lines. */
9931 if (height < 0 && !it->hpos)
9932 return;
9933 break;
9936 /* Produce glyphs. */
9937 n_glyphs_before = row->used[TEXT_AREA];
9938 it_before = *it;
9940 PRODUCE_GLYPHS (it);
9942 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9943 i = 0;
9944 x = it_before.current_x;
9945 while (i < nglyphs)
9947 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9949 if (x + glyph->pixel_width > max_x)
9951 /* Glyph doesn't fit on line. Backtrack. */
9952 row->used[TEXT_AREA] = n_glyphs_before;
9953 *it = it_before;
9954 /* If this is the only glyph on this line, it will never fit on the
9955 toolbar, so skip it. But ensure there is at least one glyph,
9956 so we don't accidentally disable the tool-bar. */
9957 if (n_glyphs_before == 0
9958 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9959 break;
9960 goto out;
9963 ++it->hpos;
9964 x += glyph->pixel_width;
9965 ++i;
9968 /* Stop at line ends. */
9969 if (ITERATOR_AT_END_OF_LINE_P (it))
9970 break;
9972 set_iterator_to_next (it, 1);
9975 out:;
9977 row->displays_text_p = row->used[TEXT_AREA] != 0;
9979 /* Use default face for the border below the tool bar.
9981 FIXME: When auto-resize-tool-bars is grow-only, there is
9982 no additional border below the possibly empty tool-bar lines.
9983 So to make the extra empty lines look "normal", we have to
9984 use the tool-bar face for the border too. */
9985 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9986 it->face_id = DEFAULT_FACE_ID;
9988 extend_face_to_end_of_line (it);
9989 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9990 last->right_box_line_p = 1;
9991 if (last == row->glyphs[TEXT_AREA])
9992 last->left_box_line_p = 1;
9994 /* Make line the desired height and center it vertically. */
9995 if ((height -= it->max_ascent + it->max_descent) > 0)
9997 /* Don't add more than one line height. */
9998 height %= FRAME_LINE_HEIGHT (it->f);
9999 it->max_ascent += height / 2;
10000 it->max_descent += (height + 1) / 2;
10003 compute_line_metrics (it);
10005 /* If line is empty, make it occupy the rest of the tool-bar. */
10006 if (!row->displays_text_p)
10008 row->height = row->phys_height = it->last_visible_y - row->y;
10009 row->visible_height = row->height;
10010 row->ascent = row->phys_ascent = 0;
10011 row->extra_line_spacing = 0;
10014 row->full_width_p = 1;
10015 row->continued_p = 0;
10016 row->truncated_on_left_p = 0;
10017 row->truncated_on_right_p = 0;
10019 it->current_x = it->hpos = 0;
10020 it->current_y += row->height;
10021 ++it->vpos;
10022 ++it->glyph_row;
10026 /* Max tool-bar height. */
10028 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10029 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10031 /* Value is the number of screen lines needed to make all tool-bar
10032 items of frame F visible. The number of actual rows needed is
10033 returned in *N_ROWS if non-NULL. */
10035 static int
10036 tool_bar_lines_needed (f, n_rows)
10037 struct frame *f;
10038 int *n_rows;
10040 struct window *w = XWINDOW (f->tool_bar_window);
10041 struct it it;
10042 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10043 the desired matrix, so use (unused) mode-line row as temporary row to
10044 avoid destroying the first tool-bar row. */
10045 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10047 /* Initialize an iterator for iteration over
10048 F->desired_tool_bar_string in the tool-bar window of frame F. */
10049 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10050 it.first_visible_x = 0;
10051 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10052 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10054 while (!ITERATOR_AT_END_P (&it))
10056 clear_glyph_row (temp_row);
10057 it.glyph_row = temp_row;
10058 display_tool_bar_line (&it, -1);
10060 clear_glyph_row (temp_row);
10062 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10063 if (n_rows)
10064 *n_rows = it.vpos > 0 ? it.vpos : -1;
10066 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10070 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10071 0, 1, 0,
10072 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10073 (frame)
10074 Lisp_Object frame;
10076 struct frame *f;
10077 struct window *w;
10078 int nlines = 0;
10080 if (NILP (frame))
10081 frame = selected_frame;
10082 else
10083 CHECK_FRAME (frame);
10084 f = XFRAME (frame);
10086 if (WINDOWP (f->tool_bar_window)
10087 || (w = XWINDOW (f->tool_bar_window),
10088 WINDOW_TOTAL_LINES (w) > 0))
10090 update_tool_bar (f, 1);
10091 if (f->n_tool_bar_items)
10093 build_desired_tool_bar_string (f);
10094 nlines = tool_bar_lines_needed (f, NULL);
10098 return make_number (nlines);
10102 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10103 height should be changed. */
10105 static int
10106 redisplay_tool_bar (f)
10107 struct frame *f;
10109 struct window *w;
10110 struct it it;
10111 struct glyph_row *row;
10113 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10114 if (FRAME_EXTERNAL_TOOL_BAR (f))
10115 update_frame_tool_bar (f);
10116 return 0;
10117 #endif
10119 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10120 do anything. This means you must start with tool-bar-lines
10121 non-zero to get the auto-sizing effect. Or in other words, you
10122 can turn off tool-bars by specifying tool-bar-lines zero. */
10123 if (!WINDOWP (f->tool_bar_window)
10124 || (w = XWINDOW (f->tool_bar_window),
10125 WINDOW_TOTAL_LINES (w) == 0))
10126 return 0;
10128 /* Set up an iterator for the tool-bar window. */
10129 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10130 it.first_visible_x = 0;
10131 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10132 row = it.glyph_row;
10134 /* Build a string that represents the contents of the tool-bar. */
10135 build_desired_tool_bar_string (f);
10136 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10138 if (f->n_tool_bar_rows == 0)
10140 int nlines;
10142 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10143 nlines != WINDOW_TOTAL_LINES (w)))
10145 extern Lisp_Object Qtool_bar_lines;
10146 Lisp_Object frame;
10147 int old_height = WINDOW_TOTAL_LINES (w);
10149 XSETFRAME (frame, f);
10150 Fmodify_frame_parameters (frame,
10151 Fcons (Fcons (Qtool_bar_lines,
10152 make_number (nlines)),
10153 Qnil));
10154 if (WINDOW_TOTAL_LINES (w) != old_height)
10156 clear_glyph_matrix (w->desired_matrix);
10157 fonts_changed_p = 1;
10158 return 1;
10163 /* Display as many lines as needed to display all tool-bar items. */
10165 if (f->n_tool_bar_rows > 0)
10167 int border, rows, height, extra;
10169 if (INTEGERP (Vtool_bar_border))
10170 border = XINT (Vtool_bar_border);
10171 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10172 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10173 else if (EQ (Vtool_bar_border, Qborder_width))
10174 border = f->border_width;
10175 else
10176 border = 0;
10177 if (border < 0)
10178 border = 0;
10180 rows = f->n_tool_bar_rows;
10181 height = max (1, (it.last_visible_y - border) / rows);
10182 extra = it.last_visible_y - border - height * rows;
10184 while (it.current_y < it.last_visible_y)
10186 int h = 0;
10187 if (extra > 0 && rows-- > 0)
10189 h = (extra + rows - 1) / rows;
10190 extra -= h;
10192 display_tool_bar_line (&it, height + h);
10195 else
10197 while (it.current_y < it.last_visible_y)
10198 display_tool_bar_line (&it, 0);
10201 /* It doesn't make much sense to try scrolling in the tool-bar
10202 window, so don't do it. */
10203 w->desired_matrix->no_scrolling_p = 1;
10204 w->must_be_updated_p = 1;
10206 if (!NILP (Vauto_resize_tool_bars))
10208 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10209 int change_height_p = 0;
10211 /* If we couldn't display everything, change the tool-bar's
10212 height if there is room for more. */
10213 if (IT_STRING_CHARPOS (it) < it.end_charpos
10214 && it.current_y < max_tool_bar_height)
10215 change_height_p = 1;
10217 row = it.glyph_row - 1;
10219 /* If there are blank lines at the end, except for a partially
10220 visible blank line at the end that is smaller than
10221 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10222 if (!row->displays_text_p
10223 && row->height >= FRAME_LINE_HEIGHT (f))
10224 change_height_p = 1;
10226 /* If row displays tool-bar items, but is partially visible,
10227 change the tool-bar's height. */
10228 if (row->displays_text_p
10229 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10230 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10231 change_height_p = 1;
10233 /* Resize windows as needed by changing the `tool-bar-lines'
10234 frame parameter. */
10235 if (change_height_p)
10237 extern Lisp_Object Qtool_bar_lines;
10238 Lisp_Object frame;
10239 int old_height = WINDOW_TOTAL_LINES (w);
10240 int nrows;
10241 int nlines = tool_bar_lines_needed (f, &nrows);
10243 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10244 && !f->minimize_tool_bar_window_p)
10245 ? (nlines > old_height)
10246 : (nlines != old_height));
10247 f->minimize_tool_bar_window_p = 0;
10249 if (change_height_p)
10251 XSETFRAME (frame, f);
10252 Fmodify_frame_parameters (frame,
10253 Fcons (Fcons (Qtool_bar_lines,
10254 make_number (nlines)),
10255 Qnil));
10256 if (WINDOW_TOTAL_LINES (w) != old_height)
10258 clear_glyph_matrix (w->desired_matrix);
10259 f->n_tool_bar_rows = nrows;
10260 fonts_changed_p = 1;
10261 return 1;
10267 f->minimize_tool_bar_window_p = 0;
10268 return 0;
10272 /* Get information about the tool-bar item which is displayed in GLYPH
10273 on frame F. Return in *PROP_IDX the index where tool-bar item
10274 properties start in F->tool_bar_items. Value is zero if
10275 GLYPH doesn't display a tool-bar item. */
10277 static int
10278 tool_bar_item_info (f, glyph, prop_idx)
10279 struct frame *f;
10280 struct glyph *glyph;
10281 int *prop_idx;
10283 Lisp_Object prop;
10284 int success_p;
10285 int charpos;
10287 /* This function can be called asynchronously, which means we must
10288 exclude any possibility that Fget_text_property signals an
10289 error. */
10290 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10291 charpos = max (0, charpos);
10293 /* Get the text property `menu-item' at pos. The value of that
10294 property is the start index of this item's properties in
10295 F->tool_bar_items. */
10296 prop = Fget_text_property (make_number (charpos),
10297 Qmenu_item, f->current_tool_bar_string);
10298 if (INTEGERP (prop))
10300 *prop_idx = XINT (prop);
10301 success_p = 1;
10303 else
10304 success_p = 0;
10306 return success_p;
10310 /* Get information about the tool-bar item at position X/Y on frame F.
10311 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10312 the current matrix of the tool-bar window of F, or NULL if not
10313 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10314 item in F->tool_bar_items. Value is
10316 -1 if X/Y is not on a tool-bar item
10317 0 if X/Y is on the same item that was highlighted before.
10318 1 otherwise. */
10320 static int
10321 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10322 struct frame *f;
10323 int x, y;
10324 struct glyph **glyph;
10325 int *hpos, *vpos, *prop_idx;
10327 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10328 struct window *w = XWINDOW (f->tool_bar_window);
10329 int area;
10331 /* Find the glyph under X/Y. */
10332 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10333 if (*glyph == NULL)
10334 return -1;
10336 /* Get the start of this tool-bar item's properties in
10337 f->tool_bar_items. */
10338 if (!tool_bar_item_info (f, *glyph, prop_idx))
10339 return -1;
10341 /* Is mouse on the highlighted item? */
10342 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10343 && *vpos >= dpyinfo->mouse_face_beg_row
10344 && *vpos <= dpyinfo->mouse_face_end_row
10345 && (*vpos > dpyinfo->mouse_face_beg_row
10346 || *hpos >= dpyinfo->mouse_face_beg_col)
10347 && (*vpos < dpyinfo->mouse_face_end_row
10348 || *hpos < dpyinfo->mouse_face_end_col
10349 || dpyinfo->mouse_face_past_end))
10350 return 0;
10352 return 1;
10356 /* EXPORT:
10357 Handle mouse button event on the tool-bar of frame F, at
10358 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10359 0 for button release. MODIFIERS is event modifiers for button
10360 release. */
10362 void
10363 handle_tool_bar_click (f, x, y, down_p, modifiers)
10364 struct frame *f;
10365 int x, y, down_p;
10366 unsigned int modifiers;
10368 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10369 struct window *w = XWINDOW (f->tool_bar_window);
10370 int hpos, vpos, prop_idx;
10371 struct glyph *glyph;
10372 Lisp_Object enabled_p;
10374 /* If not on the highlighted tool-bar item, return. */
10375 frame_to_window_pixel_xy (w, &x, &y);
10376 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10377 return;
10379 /* If item is disabled, do nothing. */
10380 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10381 if (NILP (enabled_p))
10382 return;
10384 if (down_p)
10386 /* Show item in pressed state. */
10387 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10388 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10389 last_tool_bar_item = prop_idx;
10391 else
10393 Lisp_Object key, frame;
10394 struct input_event event;
10395 EVENT_INIT (event);
10397 /* Show item in released state. */
10398 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10399 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10401 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10403 XSETFRAME (frame, f);
10404 event.kind = TOOL_BAR_EVENT;
10405 event.frame_or_window = frame;
10406 event.arg = frame;
10407 kbd_buffer_store_event (&event);
10409 event.kind = TOOL_BAR_EVENT;
10410 event.frame_or_window = frame;
10411 event.arg = key;
10412 event.modifiers = modifiers;
10413 kbd_buffer_store_event (&event);
10414 last_tool_bar_item = -1;
10419 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10420 tool-bar window-relative coordinates X/Y. Called from
10421 note_mouse_highlight. */
10423 static void
10424 note_tool_bar_highlight (f, x, y)
10425 struct frame *f;
10426 int x, y;
10428 Lisp_Object window = f->tool_bar_window;
10429 struct window *w = XWINDOW (window);
10430 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10431 int hpos, vpos;
10432 struct glyph *glyph;
10433 struct glyph_row *row;
10434 int i;
10435 Lisp_Object enabled_p;
10436 int prop_idx;
10437 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10438 int mouse_down_p, rc;
10440 /* Function note_mouse_highlight is called with negative x(y
10441 values when mouse moves outside of the frame. */
10442 if (x <= 0 || y <= 0)
10444 clear_mouse_face (dpyinfo);
10445 return;
10448 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10449 if (rc < 0)
10451 /* Not on tool-bar item. */
10452 clear_mouse_face (dpyinfo);
10453 return;
10455 else if (rc == 0)
10456 /* On same tool-bar item as before. */
10457 goto set_help_echo;
10459 clear_mouse_face (dpyinfo);
10461 /* Mouse is down, but on different tool-bar item? */
10462 mouse_down_p = (dpyinfo->grabbed
10463 && f == last_mouse_frame
10464 && FRAME_LIVE_P (f));
10465 if (mouse_down_p
10466 && last_tool_bar_item != prop_idx)
10467 return;
10469 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10470 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10472 /* If tool-bar item is not enabled, don't highlight it. */
10473 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10474 if (!NILP (enabled_p))
10476 /* Compute the x-position of the glyph. In front and past the
10477 image is a space. We include this in the highlighted area. */
10478 row = MATRIX_ROW (w->current_matrix, vpos);
10479 for (i = x = 0; i < hpos; ++i)
10480 x += row->glyphs[TEXT_AREA][i].pixel_width;
10482 /* Record this as the current active region. */
10483 dpyinfo->mouse_face_beg_col = hpos;
10484 dpyinfo->mouse_face_beg_row = vpos;
10485 dpyinfo->mouse_face_beg_x = x;
10486 dpyinfo->mouse_face_beg_y = row->y;
10487 dpyinfo->mouse_face_past_end = 0;
10489 dpyinfo->mouse_face_end_col = hpos + 1;
10490 dpyinfo->mouse_face_end_row = vpos;
10491 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10492 dpyinfo->mouse_face_end_y = row->y;
10493 dpyinfo->mouse_face_window = window;
10494 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10496 /* Display it as active. */
10497 show_mouse_face (dpyinfo, draw);
10498 dpyinfo->mouse_face_image_state = draw;
10501 set_help_echo:
10503 /* Set help_echo_string to a help string to display for this tool-bar item.
10504 XTread_socket does the rest. */
10505 help_echo_object = help_echo_window = Qnil;
10506 help_echo_pos = -1;
10507 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10508 if (NILP (help_echo_string))
10509 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10512 #endif /* HAVE_WINDOW_SYSTEM */
10516 /************************************************************************
10517 Horizontal scrolling
10518 ************************************************************************/
10520 static int hscroll_window_tree P_ ((Lisp_Object));
10521 static int hscroll_windows P_ ((Lisp_Object));
10523 /* For all leaf windows in the window tree rooted at WINDOW, set their
10524 hscroll value so that PT is (i) visible in the window, and (ii) so
10525 that it is not within a certain margin at the window's left and
10526 right border. Value is non-zero if any window's hscroll has been
10527 changed. */
10529 static int
10530 hscroll_window_tree (window)
10531 Lisp_Object window;
10533 int hscrolled_p = 0;
10534 int hscroll_relative_p = FLOATP (Vhscroll_step);
10535 int hscroll_step_abs = 0;
10536 double hscroll_step_rel = 0;
10538 if (hscroll_relative_p)
10540 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10541 if (hscroll_step_rel < 0)
10543 hscroll_relative_p = 0;
10544 hscroll_step_abs = 0;
10547 else if (INTEGERP (Vhscroll_step))
10549 hscroll_step_abs = XINT (Vhscroll_step);
10550 if (hscroll_step_abs < 0)
10551 hscroll_step_abs = 0;
10553 else
10554 hscroll_step_abs = 0;
10556 while (WINDOWP (window))
10558 struct window *w = XWINDOW (window);
10560 if (WINDOWP (w->hchild))
10561 hscrolled_p |= hscroll_window_tree (w->hchild);
10562 else if (WINDOWP (w->vchild))
10563 hscrolled_p |= hscroll_window_tree (w->vchild);
10564 else if (w->cursor.vpos >= 0)
10566 int h_margin;
10567 int text_area_width;
10568 struct glyph_row *current_cursor_row
10569 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10570 struct glyph_row *desired_cursor_row
10571 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10572 struct glyph_row *cursor_row
10573 = (desired_cursor_row->enabled_p
10574 ? desired_cursor_row
10575 : current_cursor_row);
10577 text_area_width = window_box_width (w, TEXT_AREA);
10579 /* Scroll when cursor is inside this scroll margin. */
10580 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10582 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10583 && ((XFASTINT (w->hscroll)
10584 && w->cursor.x <= h_margin)
10585 || (cursor_row->enabled_p
10586 && cursor_row->truncated_on_right_p
10587 && (w->cursor.x >= text_area_width - h_margin))))
10589 struct it it;
10590 int hscroll;
10591 struct buffer *saved_current_buffer;
10592 int pt;
10593 int wanted_x;
10595 /* Find point in a display of infinite width. */
10596 saved_current_buffer = current_buffer;
10597 current_buffer = XBUFFER (w->buffer);
10599 if (w == XWINDOW (selected_window))
10600 pt = BUF_PT (current_buffer);
10601 else
10603 pt = marker_position (w->pointm);
10604 pt = max (BEGV, pt);
10605 pt = min (ZV, pt);
10608 /* Move iterator to pt starting at cursor_row->start in
10609 a line with infinite width. */
10610 init_to_row_start (&it, w, cursor_row);
10611 it.last_visible_x = INFINITY;
10612 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10613 current_buffer = saved_current_buffer;
10615 /* Position cursor in window. */
10616 if (!hscroll_relative_p && hscroll_step_abs == 0)
10617 hscroll = max (0, (it.current_x
10618 - (ITERATOR_AT_END_OF_LINE_P (&it)
10619 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10620 : (text_area_width / 2))))
10621 / FRAME_COLUMN_WIDTH (it.f);
10622 else if (w->cursor.x >= text_area_width - h_margin)
10624 if (hscroll_relative_p)
10625 wanted_x = text_area_width * (1 - hscroll_step_rel)
10626 - h_margin;
10627 else
10628 wanted_x = text_area_width
10629 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10630 - h_margin;
10631 hscroll
10632 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10634 else
10636 if (hscroll_relative_p)
10637 wanted_x = text_area_width * hscroll_step_rel
10638 + h_margin;
10639 else
10640 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10641 + h_margin;
10642 hscroll
10643 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10645 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10647 /* Don't call Fset_window_hscroll if value hasn't
10648 changed because it will prevent redisplay
10649 optimizations. */
10650 if (XFASTINT (w->hscroll) != hscroll)
10652 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10653 w->hscroll = make_number (hscroll);
10654 hscrolled_p = 1;
10659 window = w->next;
10662 /* Value is non-zero if hscroll of any leaf window has been changed. */
10663 return hscrolled_p;
10667 /* Set hscroll so that cursor is visible and not inside horizontal
10668 scroll margins for all windows in the tree rooted at WINDOW. See
10669 also hscroll_window_tree above. Value is non-zero if any window's
10670 hscroll has been changed. If it has, desired matrices on the frame
10671 of WINDOW are cleared. */
10673 static int
10674 hscroll_windows (window)
10675 Lisp_Object window;
10677 int hscrolled_p = hscroll_window_tree (window);
10678 if (hscrolled_p)
10679 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10680 return hscrolled_p;
10685 /************************************************************************
10686 Redisplay
10687 ************************************************************************/
10689 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10690 to a non-zero value. This is sometimes handy to have in a debugger
10691 session. */
10693 #if GLYPH_DEBUG
10695 /* First and last unchanged row for try_window_id. */
10697 int debug_first_unchanged_at_end_vpos;
10698 int debug_last_unchanged_at_beg_vpos;
10700 /* Delta vpos and y. */
10702 int debug_dvpos, debug_dy;
10704 /* Delta in characters and bytes for try_window_id. */
10706 int debug_delta, debug_delta_bytes;
10708 /* Values of window_end_pos and window_end_vpos at the end of
10709 try_window_id. */
10711 EMACS_INT debug_end_pos, debug_end_vpos;
10713 /* Append a string to W->desired_matrix->method. FMT is a printf
10714 format string. A1...A9 are a supplement for a variable-length
10715 argument list. If trace_redisplay_p is non-zero also printf the
10716 resulting string to stderr. */
10718 static void
10719 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10720 struct window *w;
10721 char *fmt;
10722 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10724 char buffer[512];
10725 char *method = w->desired_matrix->method;
10726 int len = strlen (method);
10727 int size = sizeof w->desired_matrix->method;
10728 int remaining = size - len - 1;
10730 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10731 if (len && remaining)
10733 method[len] = '|';
10734 --remaining, ++len;
10737 strncpy (method + len, buffer, remaining);
10739 if (trace_redisplay_p)
10740 fprintf (stderr, "%p (%s): %s\n",
10742 ((BUFFERP (w->buffer)
10743 && STRINGP (XBUFFER (w->buffer)->name))
10744 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10745 : "no buffer"),
10746 buffer);
10749 #endif /* GLYPH_DEBUG */
10752 /* Value is non-zero if all changes in window W, which displays
10753 current_buffer, are in the text between START and END. START is a
10754 buffer position, END is given as a distance from Z. Used in
10755 redisplay_internal for display optimization. */
10757 static INLINE int
10758 text_outside_line_unchanged_p (w, start, end)
10759 struct window *w;
10760 int start, end;
10762 int unchanged_p = 1;
10764 /* If text or overlays have changed, see where. */
10765 if (XFASTINT (w->last_modified) < MODIFF
10766 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10768 /* Gap in the line? */
10769 if (GPT < start || Z - GPT < end)
10770 unchanged_p = 0;
10772 /* Changes start in front of the line, or end after it? */
10773 if (unchanged_p
10774 && (BEG_UNCHANGED < start - 1
10775 || END_UNCHANGED < end))
10776 unchanged_p = 0;
10778 /* If selective display, can't optimize if changes start at the
10779 beginning of the line. */
10780 if (unchanged_p
10781 && INTEGERP (current_buffer->selective_display)
10782 && XINT (current_buffer->selective_display) > 0
10783 && (BEG_UNCHANGED < start || GPT <= start))
10784 unchanged_p = 0;
10786 /* If there are overlays at the start or end of the line, these
10787 may have overlay strings with newlines in them. A change at
10788 START, for instance, may actually concern the display of such
10789 overlay strings as well, and they are displayed on different
10790 lines. So, quickly rule out this case. (For the future, it
10791 might be desirable to implement something more telling than
10792 just BEG/END_UNCHANGED.) */
10793 if (unchanged_p)
10795 if (BEG + BEG_UNCHANGED == start
10796 && overlay_touches_p (start))
10797 unchanged_p = 0;
10798 if (END_UNCHANGED == end
10799 && overlay_touches_p (Z - end))
10800 unchanged_p = 0;
10804 return unchanged_p;
10808 /* Do a frame update, taking possible shortcuts into account. This is
10809 the main external entry point for redisplay.
10811 If the last redisplay displayed an echo area message and that message
10812 is no longer requested, we clear the echo area or bring back the
10813 mini-buffer if that is in use. */
10815 void
10816 redisplay ()
10818 redisplay_internal (0);
10822 static Lisp_Object
10823 overlay_arrow_string_or_property (var)
10824 Lisp_Object var;
10826 Lisp_Object val;
10828 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10829 return val;
10831 return Voverlay_arrow_string;
10834 /* Return 1 if there are any overlay-arrows in current_buffer. */
10835 static int
10836 overlay_arrow_in_current_buffer_p ()
10838 Lisp_Object vlist;
10840 for (vlist = Voverlay_arrow_variable_list;
10841 CONSP (vlist);
10842 vlist = XCDR (vlist))
10844 Lisp_Object var = XCAR (vlist);
10845 Lisp_Object val;
10847 if (!SYMBOLP (var))
10848 continue;
10849 val = find_symbol_value (var);
10850 if (MARKERP (val)
10851 && current_buffer == XMARKER (val)->buffer)
10852 return 1;
10854 return 0;
10858 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10859 has changed. */
10861 static int
10862 overlay_arrows_changed_p ()
10864 Lisp_Object vlist;
10866 for (vlist = Voverlay_arrow_variable_list;
10867 CONSP (vlist);
10868 vlist = XCDR (vlist))
10870 Lisp_Object var = XCAR (vlist);
10871 Lisp_Object val, pstr;
10873 if (!SYMBOLP (var))
10874 continue;
10875 val = find_symbol_value (var);
10876 if (!MARKERP (val))
10877 continue;
10878 if (! EQ (COERCE_MARKER (val),
10879 Fget (var, Qlast_arrow_position))
10880 || ! (pstr = overlay_arrow_string_or_property (var),
10881 EQ (pstr, Fget (var, Qlast_arrow_string))))
10882 return 1;
10884 return 0;
10887 /* Mark overlay arrows to be updated on next redisplay. */
10889 static void
10890 update_overlay_arrows (up_to_date)
10891 int up_to_date;
10893 Lisp_Object vlist;
10895 for (vlist = Voverlay_arrow_variable_list;
10896 CONSP (vlist);
10897 vlist = XCDR (vlist))
10899 Lisp_Object var = XCAR (vlist);
10901 if (!SYMBOLP (var))
10902 continue;
10904 if (up_to_date > 0)
10906 Lisp_Object val = find_symbol_value (var);
10907 Fput (var, Qlast_arrow_position,
10908 COERCE_MARKER (val));
10909 Fput (var, Qlast_arrow_string,
10910 overlay_arrow_string_or_property (var));
10912 else if (up_to_date < 0
10913 || !NILP (Fget (var, Qlast_arrow_position)))
10915 Fput (var, Qlast_arrow_position, Qt);
10916 Fput (var, Qlast_arrow_string, Qt);
10922 /* Return overlay arrow string to display at row.
10923 Return integer (bitmap number) for arrow bitmap in left fringe.
10924 Return nil if no overlay arrow. */
10926 static Lisp_Object
10927 overlay_arrow_at_row (it, row)
10928 struct it *it;
10929 struct glyph_row *row;
10931 Lisp_Object vlist;
10933 for (vlist = Voverlay_arrow_variable_list;
10934 CONSP (vlist);
10935 vlist = XCDR (vlist))
10937 Lisp_Object var = XCAR (vlist);
10938 Lisp_Object val;
10940 if (!SYMBOLP (var))
10941 continue;
10943 val = find_symbol_value (var);
10945 if (MARKERP (val)
10946 && current_buffer == XMARKER (val)->buffer
10947 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10949 if (FRAME_WINDOW_P (it->f)
10950 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10952 #ifdef HAVE_WINDOW_SYSTEM
10953 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10955 int fringe_bitmap;
10956 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10957 return make_number (fringe_bitmap);
10959 #endif
10960 return make_number (-1); /* Use default arrow bitmap */
10962 return overlay_arrow_string_or_property (var);
10966 return Qnil;
10969 /* Return 1 if point moved out of or into a composition. Otherwise
10970 return 0. PREV_BUF and PREV_PT are the last point buffer and
10971 position. BUF and PT are the current point buffer and position. */
10974 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10975 struct buffer *prev_buf, *buf;
10976 int prev_pt, pt;
10978 EMACS_INT start, end;
10979 Lisp_Object prop;
10980 Lisp_Object buffer;
10982 XSETBUFFER (buffer, buf);
10983 /* Check a composition at the last point if point moved within the
10984 same buffer. */
10985 if (prev_buf == buf)
10987 if (prev_pt == pt)
10988 /* Point didn't move. */
10989 return 0;
10991 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10992 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10993 && COMPOSITION_VALID_P (start, end, prop)
10994 && start < prev_pt && end > prev_pt)
10995 /* The last point was within the composition. Return 1 iff
10996 point moved out of the composition. */
10997 return (pt <= start || pt >= end);
11000 /* Check a composition at the current point. */
11001 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11002 && find_composition (pt, -1, &start, &end, &prop, buffer)
11003 && COMPOSITION_VALID_P (start, end, prop)
11004 && start < pt && end > pt);
11008 /* Reconsider the setting of B->clip_changed which is displayed
11009 in window W. */
11011 static INLINE void
11012 reconsider_clip_changes (w, b)
11013 struct window *w;
11014 struct buffer *b;
11016 if (b->clip_changed
11017 && !NILP (w->window_end_valid)
11018 && w->current_matrix->buffer == b
11019 && w->current_matrix->zv == BUF_ZV (b)
11020 && w->current_matrix->begv == BUF_BEGV (b))
11021 b->clip_changed = 0;
11023 /* If display wasn't paused, and W is not a tool bar window, see if
11024 point has been moved into or out of a composition. In that case,
11025 we set b->clip_changed to 1 to force updating the screen. If
11026 b->clip_changed has already been set to 1, we can skip this
11027 check. */
11028 if (!b->clip_changed
11029 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11031 int pt;
11033 if (w == XWINDOW (selected_window))
11034 pt = BUF_PT (current_buffer);
11035 else
11036 pt = marker_position (w->pointm);
11038 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11039 || pt != XINT (w->last_point))
11040 && check_point_in_composition (w->current_matrix->buffer,
11041 XINT (w->last_point),
11042 XBUFFER (w->buffer), pt))
11043 b->clip_changed = 1;
11048 /* Select FRAME to forward the values of frame-local variables into C
11049 variables so that the redisplay routines can access those values
11050 directly. */
11052 static void
11053 select_frame_for_redisplay (frame)
11054 Lisp_Object frame;
11056 Lisp_Object tail, symbol, val;
11057 Lisp_Object old = selected_frame;
11058 struct Lisp_Symbol *sym;
11060 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11062 selected_frame = frame;
11066 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11067 if (CONSP (XCAR (tail))
11068 && (symbol = XCAR (XCAR (tail)),
11069 SYMBOLP (symbol))
11070 && (sym = indirect_variable (XSYMBOL (symbol)),
11071 val = sym->value,
11072 (BUFFER_LOCAL_VALUEP (val)))
11073 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11074 /* Use find_symbol_value rather than Fsymbol_value
11075 to avoid an error if it is void. */
11076 find_symbol_value (symbol);
11077 } while (!EQ (frame, old) && (frame = old, 1));
11081 #define STOP_POLLING \
11082 do { if (! polling_stopped_here) stop_polling (); \
11083 polling_stopped_here = 1; } while (0)
11085 #define RESUME_POLLING \
11086 do { if (polling_stopped_here) start_polling (); \
11087 polling_stopped_here = 0; } while (0)
11090 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11091 response to any user action; therefore, we should preserve the echo
11092 area. (Actually, our caller does that job.) Perhaps in the future
11093 avoid recentering windows if it is not necessary; currently that
11094 causes some problems. */
11096 static void
11097 redisplay_internal (preserve_echo_area)
11098 int preserve_echo_area;
11100 struct window *w = XWINDOW (selected_window);
11101 struct frame *f;
11102 int pause;
11103 int must_finish = 0;
11104 struct text_pos tlbufpos, tlendpos;
11105 int number_of_visible_frames;
11106 int count, count1;
11107 struct frame *sf;
11108 int polling_stopped_here = 0;
11109 Lisp_Object old_frame = selected_frame;
11111 /* Non-zero means redisplay has to consider all windows on all
11112 frames. Zero means, only selected_window is considered. */
11113 int consider_all_windows_p;
11115 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11117 /* No redisplay if running in batch mode or frame is not yet fully
11118 initialized, or redisplay is explicitly turned off by setting
11119 Vinhibit_redisplay. */
11120 if (noninteractive
11121 || !NILP (Vinhibit_redisplay))
11122 return;
11124 /* Don't examine these until after testing Vinhibit_redisplay.
11125 When Emacs is shutting down, perhaps because its connection to
11126 X has dropped, we should not look at them at all. */
11127 f = XFRAME (w->frame);
11128 sf = SELECTED_FRAME ();
11130 if (!f->glyphs_initialized_p)
11131 return;
11133 /* The flag redisplay_performed_directly_p is set by
11134 direct_output_for_insert when it already did the whole screen
11135 update necessary. */
11136 if (redisplay_performed_directly_p)
11138 redisplay_performed_directly_p = 0;
11139 if (!hscroll_windows (selected_window))
11140 return;
11143 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11144 if (popup_activated ())
11145 return;
11146 #endif
11148 /* I don't think this happens but let's be paranoid. */
11149 if (redisplaying_p)
11150 return;
11152 /* Record a function that resets redisplaying_p to its old value
11153 when we leave this function. */
11154 count = SPECPDL_INDEX ();
11155 record_unwind_protect (unwind_redisplay,
11156 Fcons (make_number (redisplaying_p), selected_frame));
11157 ++redisplaying_p;
11158 specbind (Qinhibit_free_realized_faces, Qnil);
11161 Lisp_Object tail, frame;
11163 FOR_EACH_FRAME (tail, frame)
11165 struct frame *f = XFRAME (frame);
11166 f->already_hscrolled_p = 0;
11170 retry:
11171 if (!EQ (old_frame, selected_frame)
11172 && FRAME_LIVE_P (XFRAME (old_frame)))
11173 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11174 selected_frame and selected_window to be temporarily out-of-sync so
11175 when we come back here via `goto retry', we need to resync because we
11176 may need to run Elisp code (via prepare_menu_bars). */
11177 select_frame_for_redisplay (old_frame);
11179 pause = 0;
11180 reconsider_clip_changes (w, current_buffer);
11181 last_escape_glyph_frame = NULL;
11182 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11184 /* If new fonts have been loaded that make a glyph matrix adjustment
11185 necessary, do it. */
11186 if (fonts_changed_p)
11188 adjust_glyphs (NULL);
11189 ++windows_or_buffers_changed;
11190 fonts_changed_p = 0;
11193 /* If face_change_count is non-zero, init_iterator will free all
11194 realized faces, which includes the faces referenced from current
11195 matrices. So, we can't reuse current matrices in this case. */
11196 if (face_change_count)
11197 ++windows_or_buffers_changed;
11199 if (FRAME_TERMCAP_P (sf)
11200 && FRAME_TTY (sf)->previous_frame != sf)
11202 /* Since frames on a single ASCII terminal share the same
11203 display area, displaying a different frame means redisplay
11204 the whole thing. */
11205 windows_or_buffers_changed++;
11206 SET_FRAME_GARBAGED (sf);
11207 #ifndef WINDOWSNT
11208 set_tty_color_mode (FRAME_TTY (sf), sf);
11209 #endif
11210 FRAME_TTY (sf)->previous_frame = sf;
11213 /* Set the visible flags for all frames. Do this before checking
11214 for resized or garbaged frames; they want to know if their frames
11215 are visible. See the comment in frame.h for
11216 FRAME_SAMPLE_VISIBILITY. */
11218 Lisp_Object tail, frame;
11220 number_of_visible_frames = 0;
11222 FOR_EACH_FRAME (tail, frame)
11224 struct frame *f = XFRAME (frame);
11226 FRAME_SAMPLE_VISIBILITY (f);
11227 if (FRAME_VISIBLE_P (f))
11228 ++number_of_visible_frames;
11229 clear_desired_matrices (f);
11234 /* Notice any pending interrupt request to change frame size. */
11235 do_pending_window_change (1);
11237 /* Clear frames marked as garbaged. */
11238 if (frame_garbaged)
11239 clear_garbaged_frames ();
11241 /* Build menubar and tool-bar items. */
11242 if (NILP (Vmemory_full))
11243 prepare_menu_bars ();
11245 if (windows_or_buffers_changed)
11246 update_mode_lines++;
11248 /* Detect case that we need to write or remove a star in the mode line. */
11249 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11251 w->update_mode_line = Qt;
11252 if (buffer_shared > 1)
11253 update_mode_lines++;
11256 /* Avoid invocation of point motion hooks by `current_column' below. */
11257 count1 = SPECPDL_INDEX ();
11258 specbind (Qinhibit_point_motion_hooks, Qt);
11260 /* If %c is in the mode line, update it if needed. */
11261 if (!NILP (w->column_number_displayed)
11262 /* This alternative quickly identifies a common case
11263 where no change is needed. */
11264 && !(PT == XFASTINT (w->last_point)
11265 && XFASTINT (w->last_modified) >= MODIFF
11266 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11267 && (XFASTINT (w->column_number_displayed)
11268 != (int) current_column ())) /* iftc */
11269 w->update_mode_line = Qt;
11271 unbind_to (count1, Qnil);
11273 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11275 /* The variable buffer_shared is set in redisplay_window and
11276 indicates that we redisplay a buffer in different windows. See
11277 there. */
11278 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11279 || cursor_type_changed);
11281 /* If specs for an arrow have changed, do thorough redisplay
11282 to ensure we remove any arrow that should no longer exist. */
11283 if (overlay_arrows_changed_p ())
11284 consider_all_windows_p = windows_or_buffers_changed = 1;
11286 /* Normally the message* functions will have already displayed and
11287 updated the echo area, but the frame may have been trashed, or
11288 the update may have been preempted, so display the echo area
11289 again here. Checking message_cleared_p captures the case that
11290 the echo area should be cleared. */
11291 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11292 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11293 || (message_cleared_p
11294 && minibuf_level == 0
11295 /* If the mini-window is currently selected, this means the
11296 echo-area doesn't show through. */
11297 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11299 int window_height_changed_p = echo_area_display (0);
11300 must_finish = 1;
11302 /* If we don't display the current message, don't clear the
11303 message_cleared_p flag, because, if we did, we wouldn't clear
11304 the echo area in the next redisplay which doesn't preserve
11305 the echo area. */
11306 if (!display_last_displayed_message_p)
11307 message_cleared_p = 0;
11309 if (fonts_changed_p)
11310 goto retry;
11311 else if (window_height_changed_p)
11313 consider_all_windows_p = 1;
11314 ++update_mode_lines;
11315 ++windows_or_buffers_changed;
11317 /* If window configuration was changed, frames may have been
11318 marked garbaged. Clear them or we will experience
11319 surprises wrt scrolling. */
11320 if (frame_garbaged)
11321 clear_garbaged_frames ();
11324 else if (EQ (selected_window, minibuf_window)
11325 && (current_buffer->clip_changed
11326 || XFASTINT (w->last_modified) < MODIFF
11327 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11328 && resize_mini_window (w, 0))
11330 /* Resized active mini-window to fit the size of what it is
11331 showing if its contents might have changed. */
11332 must_finish = 1;
11333 consider_all_windows_p = 1;
11334 ++windows_or_buffers_changed;
11335 ++update_mode_lines;
11337 /* If window configuration was changed, frames may have been
11338 marked garbaged. Clear them or we will experience
11339 surprises wrt scrolling. */
11340 if (frame_garbaged)
11341 clear_garbaged_frames ();
11345 /* If showing the region, and mark has changed, we must redisplay
11346 the whole window. The assignment to this_line_start_pos prevents
11347 the optimization directly below this if-statement. */
11348 if (((!NILP (Vtransient_mark_mode)
11349 && !NILP (XBUFFER (w->buffer)->mark_active))
11350 != !NILP (w->region_showing))
11351 || (!NILP (w->region_showing)
11352 && !EQ (w->region_showing,
11353 Fmarker_position (XBUFFER (w->buffer)->mark))))
11354 CHARPOS (this_line_start_pos) = 0;
11356 /* Optimize the case that only the line containing the cursor in the
11357 selected window has changed. Variables starting with this_ are
11358 set in display_line and record information about the line
11359 containing the cursor. */
11360 tlbufpos = this_line_start_pos;
11361 tlendpos = this_line_end_pos;
11362 if (!consider_all_windows_p
11363 && CHARPOS (tlbufpos) > 0
11364 && NILP (w->update_mode_line)
11365 && !current_buffer->clip_changed
11366 && !current_buffer->prevent_redisplay_optimizations_p
11367 && FRAME_VISIBLE_P (XFRAME (w->frame))
11368 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11369 /* Make sure recorded data applies to current buffer, etc. */
11370 && this_line_buffer == current_buffer
11371 && current_buffer == XBUFFER (w->buffer)
11372 && NILP (w->force_start)
11373 && NILP (w->optional_new_start)
11374 /* Point must be on the line that we have info recorded about. */
11375 && PT >= CHARPOS (tlbufpos)
11376 && PT <= Z - CHARPOS (tlendpos)
11377 /* All text outside that line, including its final newline,
11378 must be unchanged */
11379 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11380 CHARPOS (tlendpos)))
11382 if (CHARPOS (tlbufpos) > BEGV
11383 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11384 && (CHARPOS (tlbufpos) == ZV
11385 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11386 /* Former continuation line has disappeared by becoming empty */
11387 goto cancel;
11388 else if (XFASTINT (w->last_modified) < MODIFF
11389 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11390 || MINI_WINDOW_P (w))
11392 /* We have to handle the case of continuation around a
11393 wide-column character (See the comment in indent.c around
11394 line 885).
11396 For instance, in the following case:
11398 -------- Insert --------
11399 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11400 J_I_ ==> J_I_ `^^' are cursors.
11401 ^^ ^^
11402 -------- --------
11404 As we have to redraw the line above, we should goto cancel. */
11406 struct it it;
11407 int line_height_before = this_line_pixel_height;
11409 /* Note that start_display will handle the case that the
11410 line starting at tlbufpos is a continuation lines. */
11411 start_display (&it, w, tlbufpos);
11413 /* Implementation note: It this still necessary? */
11414 if (it.current_x != this_line_start_x)
11415 goto cancel;
11417 TRACE ((stderr, "trying display optimization 1\n"));
11418 w->cursor.vpos = -1;
11419 overlay_arrow_seen = 0;
11420 it.vpos = this_line_vpos;
11421 it.current_y = this_line_y;
11422 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11423 display_line (&it);
11425 /* If line contains point, is not continued,
11426 and ends at same distance from eob as before, we win */
11427 if (w->cursor.vpos >= 0
11428 /* Line is not continued, otherwise this_line_start_pos
11429 would have been set to 0 in display_line. */
11430 && CHARPOS (this_line_start_pos)
11431 /* Line ends as before. */
11432 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11433 /* Line has same height as before. Otherwise other lines
11434 would have to be shifted up or down. */
11435 && this_line_pixel_height == line_height_before)
11437 /* If this is not the window's last line, we must adjust
11438 the charstarts of the lines below. */
11439 if (it.current_y < it.last_visible_y)
11441 struct glyph_row *row
11442 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11443 int delta, delta_bytes;
11445 if (Z - CHARPOS (tlendpos) == ZV)
11447 /* This line ends at end of (accessible part of)
11448 buffer. There is no newline to count. */
11449 delta = (Z
11450 - CHARPOS (tlendpos)
11451 - MATRIX_ROW_START_CHARPOS (row));
11452 delta_bytes = (Z_BYTE
11453 - BYTEPOS (tlendpos)
11454 - MATRIX_ROW_START_BYTEPOS (row));
11456 else
11458 /* This line ends in a newline. Must take
11459 account of the newline and the rest of the
11460 text that follows. */
11461 delta = (Z
11462 - CHARPOS (tlendpos)
11463 - MATRIX_ROW_START_CHARPOS (row));
11464 delta_bytes = (Z_BYTE
11465 - BYTEPOS (tlendpos)
11466 - MATRIX_ROW_START_BYTEPOS (row));
11469 increment_matrix_positions (w->current_matrix,
11470 this_line_vpos + 1,
11471 w->current_matrix->nrows,
11472 delta, delta_bytes);
11475 /* If this row displays text now but previously didn't,
11476 or vice versa, w->window_end_vpos may have to be
11477 adjusted. */
11478 if ((it.glyph_row - 1)->displays_text_p)
11480 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11481 XSETINT (w->window_end_vpos, this_line_vpos);
11483 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11484 && this_line_vpos > 0)
11485 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11486 w->window_end_valid = Qnil;
11488 /* Update hint: No need to try to scroll in update_window. */
11489 w->desired_matrix->no_scrolling_p = 1;
11491 #if GLYPH_DEBUG
11492 *w->desired_matrix->method = 0;
11493 debug_method_add (w, "optimization 1");
11494 #endif
11495 #ifdef HAVE_WINDOW_SYSTEM
11496 update_window_fringes (w, 0);
11497 #endif
11498 goto update;
11500 else
11501 goto cancel;
11503 else if (/* Cursor position hasn't changed. */
11504 PT == XFASTINT (w->last_point)
11505 /* Make sure the cursor was last displayed
11506 in this window. Otherwise we have to reposition it. */
11507 && 0 <= w->cursor.vpos
11508 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11510 if (!must_finish)
11512 do_pending_window_change (1);
11514 /* We used to always goto end_of_redisplay here, but this
11515 isn't enough if we have a blinking cursor. */
11516 if (w->cursor_off_p == w->last_cursor_off_p)
11517 goto end_of_redisplay;
11519 goto update;
11521 /* If highlighting the region, or if the cursor is in the echo area,
11522 then we can't just move the cursor. */
11523 else if (! (!NILP (Vtransient_mark_mode)
11524 && !NILP (current_buffer->mark_active))
11525 && (EQ (selected_window, current_buffer->last_selected_window)
11526 || highlight_nonselected_windows)
11527 && NILP (w->region_showing)
11528 && NILP (Vshow_trailing_whitespace)
11529 && !cursor_in_echo_area)
11531 struct it it;
11532 struct glyph_row *row;
11534 /* Skip from tlbufpos to PT and see where it is. Note that
11535 PT may be in invisible text. If so, we will end at the
11536 next visible position. */
11537 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11538 NULL, DEFAULT_FACE_ID);
11539 it.current_x = this_line_start_x;
11540 it.current_y = this_line_y;
11541 it.vpos = this_line_vpos;
11543 /* The call to move_it_to stops in front of PT, but
11544 moves over before-strings. */
11545 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11547 if (it.vpos == this_line_vpos
11548 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11549 row->enabled_p))
11551 xassert (this_line_vpos == it.vpos);
11552 xassert (this_line_y == it.current_y);
11553 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11554 #if GLYPH_DEBUG
11555 *w->desired_matrix->method = 0;
11556 debug_method_add (w, "optimization 3");
11557 #endif
11558 goto update;
11560 else
11561 goto cancel;
11564 cancel:
11565 /* Text changed drastically or point moved off of line. */
11566 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11569 CHARPOS (this_line_start_pos) = 0;
11570 consider_all_windows_p |= buffer_shared > 1;
11571 ++clear_face_cache_count;
11572 #ifdef HAVE_WINDOW_SYSTEM
11573 ++clear_image_cache_count;
11574 #endif
11576 /* Build desired matrices, and update the display. If
11577 consider_all_windows_p is non-zero, do it for all windows on all
11578 frames. Otherwise do it for selected_window, only. */
11580 if (consider_all_windows_p)
11582 Lisp_Object tail, frame;
11584 FOR_EACH_FRAME (tail, frame)
11585 XFRAME (frame)->updated_p = 0;
11587 /* Recompute # windows showing selected buffer. This will be
11588 incremented each time such a window is displayed. */
11589 buffer_shared = 0;
11591 FOR_EACH_FRAME (tail, frame)
11593 struct frame *f = XFRAME (frame);
11595 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11597 if (! EQ (frame, selected_frame))
11598 /* Select the frame, for the sake of frame-local
11599 variables. */
11600 select_frame_for_redisplay (frame);
11602 /* Mark all the scroll bars to be removed; we'll redeem
11603 the ones we want when we redisplay their windows. */
11604 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11605 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11607 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11608 redisplay_windows (FRAME_ROOT_WINDOW (f));
11610 /* Any scroll bars which redisplay_windows should have
11611 nuked should now go away. */
11612 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11613 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11615 /* If fonts changed, display again. */
11616 /* ??? rms: I suspect it is a mistake to jump all the way
11617 back to retry here. It should just retry this frame. */
11618 if (fonts_changed_p)
11619 goto retry;
11621 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11623 /* See if we have to hscroll. */
11624 if (!f->already_hscrolled_p)
11626 f->already_hscrolled_p = 1;
11627 if (hscroll_windows (f->root_window))
11628 goto retry;
11631 /* Prevent various kinds of signals during display
11632 update. stdio is not robust about handling
11633 signals, which can cause an apparent I/O
11634 error. */
11635 if (interrupt_input)
11636 unrequest_sigio ();
11637 STOP_POLLING;
11639 /* Update the display. */
11640 set_window_update_flags (XWINDOW (f->root_window), 1);
11641 pause |= update_frame (f, 0, 0);
11642 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11643 if (pause)
11644 break;
11645 #endif
11647 f->updated_p = 1;
11652 if (!EQ (old_frame, selected_frame)
11653 && FRAME_LIVE_P (XFRAME (old_frame)))
11654 /* We played a bit fast-and-loose above and allowed selected_frame
11655 and selected_window to be temporarily out-of-sync but let's make
11656 sure this stays contained. */
11657 select_frame_for_redisplay (old_frame);
11658 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11660 if (!pause)
11662 /* Do the mark_window_display_accurate after all windows have
11663 been redisplayed because this call resets flags in buffers
11664 which are needed for proper redisplay. */
11665 FOR_EACH_FRAME (tail, frame)
11667 struct frame *f = XFRAME (frame);
11668 if (f->updated_p)
11670 mark_window_display_accurate (f->root_window, 1);
11671 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11672 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11677 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11679 Lisp_Object mini_window;
11680 struct frame *mini_frame;
11682 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11683 /* Use list_of_error, not Qerror, so that
11684 we catch only errors and don't run the debugger. */
11685 internal_condition_case_1 (redisplay_window_1, selected_window,
11686 list_of_error,
11687 redisplay_window_error);
11689 /* Compare desired and current matrices, perform output. */
11691 update:
11692 /* If fonts changed, display again. */
11693 if (fonts_changed_p)
11694 goto retry;
11696 /* Prevent various kinds of signals during display update.
11697 stdio is not robust about handling signals,
11698 which can cause an apparent I/O error. */
11699 if (interrupt_input)
11700 unrequest_sigio ();
11701 STOP_POLLING;
11703 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11705 if (hscroll_windows (selected_window))
11706 goto retry;
11708 XWINDOW (selected_window)->must_be_updated_p = 1;
11709 pause = update_frame (sf, 0, 0);
11712 /* We may have called echo_area_display at the top of this
11713 function. If the echo area is on another frame, that may
11714 have put text on a frame other than the selected one, so the
11715 above call to update_frame would not have caught it. Catch
11716 it here. */
11717 mini_window = FRAME_MINIBUF_WINDOW (sf);
11718 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11720 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11722 XWINDOW (mini_window)->must_be_updated_p = 1;
11723 pause |= update_frame (mini_frame, 0, 0);
11724 if (!pause && hscroll_windows (mini_window))
11725 goto retry;
11729 /* If display was paused because of pending input, make sure we do a
11730 thorough update the next time. */
11731 if (pause)
11733 /* Prevent the optimization at the beginning of
11734 redisplay_internal that tries a single-line update of the
11735 line containing the cursor in the selected window. */
11736 CHARPOS (this_line_start_pos) = 0;
11738 /* Let the overlay arrow be updated the next time. */
11739 update_overlay_arrows (0);
11741 /* If we pause after scrolling, some rows in the current
11742 matrices of some windows are not valid. */
11743 if (!WINDOW_FULL_WIDTH_P (w)
11744 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11745 update_mode_lines = 1;
11747 else
11749 if (!consider_all_windows_p)
11751 /* This has already been done above if
11752 consider_all_windows_p is set. */
11753 mark_window_display_accurate_1 (w, 1);
11755 /* Say overlay arrows are up to date. */
11756 update_overlay_arrows (1);
11758 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11759 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11762 update_mode_lines = 0;
11763 windows_or_buffers_changed = 0;
11764 cursor_type_changed = 0;
11767 /* Start SIGIO interrupts coming again. Having them off during the
11768 code above makes it less likely one will discard output, but not
11769 impossible, since there might be stuff in the system buffer here.
11770 But it is much hairier to try to do anything about that. */
11771 if (interrupt_input)
11772 request_sigio ();
11773 RESUME_POLLING;
11775 /* If a frame has become visible which was not before, redisplay
11776 again, so that we display it. Expose events for such a frame
11777 (which it gets when becoming visible) don't call the parts of
11778 redisplay constructing glyphs, so simply exposing a frame won't
11779 display anything in this case. So, we have to display these
11780 frames here explicitly. */
11781 if (!pause)
11783 Lisp_Object tail, frame;
11784 int new_count = 0;
11786 FOR_EACH_FRAME (tail, frame)
11788 int this_is_visible = 0;
11790 if (XFRAME (frame)->visible)
11791 this_is_visible = 1;
11792 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11793 if (XFRAME (frame)->visible)
11794 this_is_visible = 1;
11796 if (this_is_visible)
11797 new_count++;
11800 if (new_count != number_of_visible_frames)
11801 windows_or_buffers_changed++;
11804 /* Change frame size now if a change is pending. */
11805 do_pending_window_change (1);
11807 /* If we just did a pending size change, or have additional
11808 visible frames, redisplay again. */
11809 if (windows_or_buffers_changed && !pause)
11810 goto retry;
11812 /* Clear the face cache eventually. */
11813 if (consider_all_windows_p)
11815 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11817 clear_face_cache (0);
11818 clear_face_cache_count = 0;
11820 #ifdef HAVE_WINDOW_SYSTEM
11821 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11823 clear_image_caches (Qnil);
11824 clear_image_cache_count = 0;
11826 #endif /* HAVE_WINDOW_SYSTEM */
11829 end_of_redisplay:
11830 unbind_to (count, Qnil);
11831 RESUME_POLLING;
11835 /* Redisplay, but leave alone any recent echo area message unless
11836 another message has been requested in its place.
11838 This is useful in situations where you need to redisplay but no
11839 user action has occurred, making it inappropriate for the message
11840 area to be cleared. See tracking_off and
11841 wait_reading_process_output for examples of these situations.
11843 FROM_WHERE is an integer saying from where this function was
11844 called. This is useful for debugging. */
11846 void
11847 redisplay_preserve_echo_area (from_where)
11848 int from_where;
11850 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11852 if (!NILP (echo_area_buffer[1]))
11854 /* We have a previously displayed message, but no current
11855 message. Redisplay the previous message. */
11856 display_last_displayed_message_p = 1;
11857 redisplay_internal (1);
11858 display_last_displayed_message_p = 0;
11860 else
11861 redisplay_internal (1);
11863 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11864 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11865 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11869 /* Function registered with record_unwind_protect in
11870 redisplay_internal. Reset redisplaying_p to the value it had
11871 before redisplay_internal was called, and clear
11872 prevent_freeing_realized_faces_p. It also selects the previously
11873 selected frame, unless it has been deleted (by an X connection
11874 failure during redisplay, for example). */
11876 static Lisp_Object
11877 unwind_redisplay (val)
11878 Lisp_Object val;
11880 Lisp_Object old_redisplaying_p, old_frame;
11882 old_redisplaying_p = XCAR (val);
11883 redisplaying_p = XFASTINT (old_redisplaying_p);
11884 old_frame = XCDR (val);
11885 if (! EQ (old_frame, selected_frame)
11886 && FRAME_LIVE_P (XFRAME (old_frame)))
11887 select_frame_for_redisplay (old_frame);
11888 return Qnil;
11892 /* Mark the display of window W as accurate or inaccurate. If
11893 ACCURATE_P is non-zero mark display of W as accurate. If
11894 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11895 redisplay_internal is called. */
11897 static void
11898 mark_window_display_accurate_1 (w, accurate_p)
11899 struct window *w;
11900 int accurate_p;
11902 if (BUFFERP (w->buffer))
11904 struct buffer *b = XBUFFER (w->buffer);
11906 w->last_modified
11907 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11908 w->last_overlay_modified
11909 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11910 w->last_had_star
11911 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11913 if (accurate_p)
11915 b->clip_changed = 0;
11916 b->prevent_redisplay_optimizations_p = 0;
11918 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11919 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11920 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11921 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11923 w->current_matrix->buffer = b;
11924 w->current_matrix->begv = BUF_BEGV (b);
11925 w->current_matrix->zv = BUF_ZV (b);
11927 w->last_cursor = w->cursor;
11928 w->last_cursor_off_p = w->cursor_off_p;
11930 if (w == XWINDOW (selected_window))
11931 w->last_point = make_number (BUF_PT (b));
11932 else
11933 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11937 if (accurate_p)
11939 w->window_end_valid = w->buffer;
11940 #if 0 /* This is incorrect with variable-height lines. */
11941 xassert (XINT (w->window_end_vpos)
11942 < (WINDOW_TOTAL_LINES (w)
11943 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11944 #endif
11945 w->update_mode_line = Qnil;
11950 /* Mark the display of windows in the window tree rooted at WINDOW as
11951 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11952 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11953 be redisplayed the next time redisplay_internal is called. */
11955 void
11956 mark_window_display_accurate (window, accurate_p)
11957 Lisp_Object window;
11958 int accurate_p;
11960 struct window *w;
11962 for (; !NILP (window); window = w->next)
11964 w = XWINDOW (window);
11965 mark_window_display_accurate_1 (w, accurate_p);
11967 if (!NILP (w->vchild))
11968 mark_window_display_accurate (w->vchild, accurate_p);
11969 if (!NILP (w->hchild))
11970 mark_window_display_accurate (w->hchild, accurate_p);
11973 if (accurate_p)
11975 update_overlay_arrows (1);
11977 else
11979 /* Force a thorough redisplay the next time by setting
11980 last_arrow_position and last_arrow_string to t, which is
11981 unequal to any useful value of Voverlay_arrow_... */
11982 update_overlay_arrows (-1);
11987 /* Return value in display table DP (Lisp_Char_Table *) for character
11988 C. Since a display table doesn't have any parent, we don't have to
11989 follow parent. Do not call this function directly but use the
11990 macro DISP_CHAR_VECTOR. */
11992 Lisp_Object
11993 disp_char_vector (dp, c)
11994 struct Lisp_Char_Table *dp;
11995 int c;
11997 Lisp_Object val;
11999 if (ASCII_CHAR_P (c))
12001 val = dp->ascii;
12002 if (SUB_CHAR_TABLE_P (val))
12003 val = XSUB_CHAR_TABLE (val)->contents[c];
12005 else
12007 Lisp_Object table;
12009 XSETCHAR_TABLE (table, dp);
12010 val = char_table_ref (table, c);
12012 if (NILP (val))
12013 val = dp->defalt;
12014 return val;
12019 /***********************************************************************
12020 Window Redisplay
12021 ***********************************************************************/
12023 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12025 static void
12026 redisplay_windows (window)
12027 Lisp_Object window;
12029 while (!NILP (window))
12031 struct window *w = XWINDOW (window);
12033 if (!NILP (w->hchild))
12034 redisplay_windows (w->hchild);
12035 else if (!NILP (w->vchild))
12036 redisplay_windows (w->vchild);
12037 else
12039 displayed_buffer = XBUFFER (w->buffer);
12040 /* Use list_of_error, not Qerror, so that
12041 we catch only errors and don't run the debugger. */
12042 internal_condition_case_1 (redisplay_window_0, window,
12043 list_of_error,
12044 redisplay_window_error);
12047 window = w->next;
12051 static Lisp_Object
12052 redisplay_window_error ()
12054 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12055 return Qnil;
12058 static Lisp_Object
12059 redisplay_window_0 (window)
12060 Lisp_Object window;
12062 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12063 redisplay_window (window, 0);
12064 return Qnil;
12067 static Lisp_Object
12068 redisplay_window_1 (window)
12069 Lisp_Object window;
12071 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12072 redisplay_window (window, 1);
12073 return Qnil;
12077 /* Increment GLYPH until it reaches END or CONDITION fails while
12078 adding (GLYPH)->pixel_width to X. */
12080 #define SKIP_GLYPHS(glyph, end, x, condition) \
12081 do \
12083 (x) += (glyph)->pixel_width; \
12084 ++(glyph); \
12086 while ((glyph) < (end) && (condition))
12089 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12090 DELTA is the number of bytes by which positions recorded in ROW
12091 differ from current buffer positions.
12093 Return 0 if cursor is not on this row. 1 otherwise. */
12096 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12097 struct window *w;
12098 struct glyph_row *row;
12099 struct glyph_matrix *matrix;
12100 int delta, delta_bytes, dy, dvpos;
12102 struct glyph *glyph = row->glyphs[TEXT_AREA];
12103 struct glyph *end = glyph + row->used[TEXT_AREA];
12104 struct glyph *cursor = NULL;
12105 /* The first glyph that starts a sequence of glyphs from string. */
12106 struct glyph *string_start;
12107 /* The X coordinate of string_start. */
12108 int string_start_x;
12109 /* The last known character position. */
12110 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12111 /* The last known character position before string_start. */
12112 int string_before_pos;
12113 int x = row->x;
12114 int cursor_x = x;
12115 int cursor_from_overlay_pos = 0;
12116 int pt_old = PT - delta;
12118 /* Skip over glyphs not having an object at the start of the row.
12119 These are special glyphs like truncation marks on terminal
12120 frames. */
12121 if (row->displays_text_p)
12122 while (glyph < end
12123 && INTEGERP (glyph->object)
12124 && glyph->charpos < 0)
12126 x += glyph->pixel_width;
12127 ++glyph;
12130 string_start = NULL;
12131 while (glyph < end
12132 && !INTEGERP (glyph->object)
12133 && (!BUFFERP (glyph->object)
12134 || (last_pos = glyph->charpos) < pt_old))
12136 if (! STRINGP (glyph->object))
12138 string_start = NULL;
12139 x += glyph->pixel_width;
12140 ++glyph;
12141 if (cursor_from_overlay_pos
12142 && last_pos >= cursor_from_overlay_pos)
12144 cursor_from_overlay_pos = 0;
12145 cursor = 0;
12148 else
12150 if (string_start == NULL)
12152 string_before_pos = last_pos;
12153 string_start = glyph;
12154 string_start_x = x;
12156 /* Skip all glyphs from string. */
12159 Lisp_Object cprop;
12160 int pos;
12161 if ((cursor == NULL || glyph > cursor)
12162 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12163 Qcursor, (glyph)->object),
12164 !NILP (cprop))
12165 && (pos = string_buffer_position (w, glyph->object,
12166 string_before_pos),
12167 (pos == 0 /* From overlay */
12168 || pos == pt_old)))
12170 /* Estimate overlay buffer position from the buffer
12171 positions of the glyphs before and after the overlay.
12172 Add 1 to last_pos so that if point corresponds to the
12173 glyph right after the overlay, we still use a 'cursor'
12174 property found in that overlay. */
12175 cursor_from_overlay_pos = (pos ? 0 : last_pos
12176 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12177 cursor = glyph;
12178 cursor_x = x;
12180 x += glyph->pixel_width;
12181 ++glyph;
12183 while (glyph < end && EQ (glyph->object, string_start->object));
12187 if (cursor != NULL)
12189 glyph = cursor;
12190 x = cursor_x;
12192 else if (row->ends_in_ellipsis_p && glyph == end)
12194 /* Scan back over the ellipsis glyphs, decrementing positions. */
12195 while (glyph > row->glyphs[TEXT_AREA]
12196 && (glyph - 1)->charpos == last_pos)
12197 glyph--, x -= glyph->pixel_width;
12198 /* That loop always goes one position too far,
12199 including the glyph before the ellipsis.
12200 So scan forward over that one. */
12201 x += glyph->pixel_width;
12202 glyph++;
12204 else if (string_start
12205 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12207 /* We may have skipped over point because the previous glyphs
12208 are from string. As there's no easy way to know the
12209 character position of the current glyph, find the correct
12210 glyph on point by scanning from string_start again. */
12211 Lisp_Object limit;
12212 Lisp_Object string;
12213 struct glyph *stop = glyph;
12214 int pos;
12216 limit = make_number (pt_old + 1);
12217 glyph = string_start;
12218 x = string_start_x;
12219 string = glyph->object;
12220 pos = string_buffer_position (w, string, string_before_pos);
12221 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12222 because we always put cursor after overlay strings. */
12223 while (pos == 0 && glyph < stop)
12225 string = glyph->object;
12226 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12227 if (glyph < stop)
12228 pos = string_buffer_position (w, glyph->object, string_before_pos);
12231 while (glyph < stop)
12233 pos = XINT (Fnext_single_char_property_change
12234 (make_number (pos), Qdisplay, Qnil, limit));
12235 if (pos > pt_old)
12236 break;
12237 /* Skip glyphs from the same string. */
12238 string = glyph->object;
12239 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12240 /* Skip glyphs from an overlay. */
12241 while (glyph < stop
12242 && ! string_buffer_position (w, glyph->object, pos))
12244 string = glyph->object;
12245 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12249 /* If we reached the end of the line, and end was from a string,
12250 cursor is not on this line. */
12251 if (glyph == end && row->continued_p)
12252 return 0;
12255 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12256 w->cursor.x = x;
12257 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12258 w->cursor.y = row->y + dy;
12260 if (w == XWINDOW (selected_window))
12262 if (!row->continued_p
12263 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12264 && row->x == 0)
12266 this_line_buffer = XBUFFER (w->buffer);
12268 CHARPOS (this_line_start_pos)
12269 = MATRIX_ROW_START_CHARPOS (row) + delta;
12270 BYTEPOS (this_line_start_pos)
12271 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12273 CHARPOS (this_line_end_pos)
12274 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12275 BYTEPOS (this_line_end_pos)
12276 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12278 this_line_y = w->cursor.y;
12279 this_line_pixel_height = row->height;
12280 this_line_vpos = w->cursor.vpos;
12281 this_line_start_x = row->x;
12283 else
12284 CHARPOS (this_line_start_pos) = 0;
12287 return 1;
12291 /* Run window scroll functions, if any, for WINDOW with new window
12292 start STARTP. Sets the window start of WINDOW to that position.
12294 We assume that the window's buffer is really current. */
12296 static INLINE struct text_pos
12297 run_window_scroll_functions (window, startp)
12298 Lisp_Object window;
12299 struct text_pos startp;
12301 struct window *w = XWINDOW (window);
12302 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12304 if (current_buffer != XBUFFER (w->buffer))
12305 abort ();
12307 if (!NILP (Vwindow_scroll_functions))
12309 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12310 make_number (CHARPOS (startp)));
12311 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12312 /* In case the hook functions switch buffers. */
12313 if (current_buffer != XBUFFER (w->buffer))
12314 set_buffer_internal_1 (XBUFFER (w->buffer));
12317 return startp;
12321 /* Make sure the line containing the cursor is fully visible.
12322 A value of 1 means there is nothing to be done.
12323 (Either the line is fully visible, or it cannot be made so,
12324 or we cannot tell.)
12326 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12327 is higher than window.
12329 A value of 0 means the caller should do scrolling
12330 as if point had gone off the screen. */
12332 static int
12333 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12334 struct window *w;
12335 int force_p;
12336 int current_matrix_p;
12338 struct glyph_matrix *matrix;
12339 struct glyph_row *row;
12340 int window_height;
12342 if (!make_cursor_line_fully_visible_p)
12343 return 1;
12345 /* It's not always possible to find the cursor, e.g, when a window
12346 is full of overlay strings. Don't do anything in that case. */
12347 if (w->cursor.vpos < 0)
12348 return 1;
12350 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12351 row = MATRIX_ROW (matrix, w->cursor.vpos);
12353 /* If the cursor row is not partially visible, there's nothing to do. */
12354 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12355 return 1;
12357 /* If the row the cursor is in is taller than the window's height,
12358 it's not clear what to do, so do nothing. */
12359 window_height = window_box_height (w);
12360 if (row->height >= window_height)
12362 if (!force_p || MINI_WINDOW_P (w)
12363 || w->vscroll || w->cursor.vpos == 0)
12364 return 1;
12366 return 0;
12368 #if 0
12369 /* This code used to try to scroll the window just enough to make
12370 the line visible. It returned 0 to say that the caller should
12371 allocate larger glyph matrices. */
12373 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12375 int dy = row->height - row->visible_height;
12376 w->vscroll = 0;
12377 w->cursor.y += dy;
12378 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12380 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12382 int dy = - (row->height - row->visible_height);
12383 w->vscroll = dy;
12384 w->cursor.y += dy;
12385 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12388 /* When we change the cursor y-position of the selected window,
12389 change this_line_y as well so that the display optimization for
12390 the cursor line of the selected window in redisplay_internal uses
12391 the correct y-position. */
12392 if (w == XWINDOW (selected_window))
12393 this_line_y = w->cursor.y;
12395 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12396 redisplay with larger matrices. */
12397 if (matrix->nrows < required_matrix_height (w))
12399 fonts_changed_p = 1;
12400 return 0;
12403 return 1;
12404 #endif /* 0 */
12408 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12409 non-zero means only WINDOW is redisplayed in redisplay_internal.
12410 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12411 in redisplay_window to bring a partially visible line into view in
12412 the case that only the cursor has moved.
12414 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12415 last screen line's vertical height extends past the end of the screen.
12417 Value is
12419 1 if scrolling succeeded
12421 0 if scrolling didn't find point.
12423 -1 if new fonts have been loaded so that we must interrupt
12424 redisplay, adjust glyph matrices, and try again. */
12426 enum
12428 SCROLLING_SUCCESS,
12429 SCROLLING_FAILED,
12430 SCROLLING_NEED_LARGER_MATRICES
12433 static int
12434 try_scrolling (window, just_this_one_p, scroll_conservatively,
12435 scroll_step, temp_scroll_step, last_line_misfit)
12436 Lisp_Object window;
12437 int just_this_one_p;
12438 EMACS_INT scroll_conservatively, scroll_step;
12439 int temp_scroll_step;
12440 int last_line_misfit;
12442 struct window *w = XWINDOW (window);
12443 struct frame *f = XFRAME (w->frame);
12444 struct text_pos scroll_margin_pos;
12445 struct text_pos pos;
12446 struct text_pos startp;
12447 struct it it;
12448 Lisp_Object window_end;
12449 int this_scroll_margin;
12450 int dy = 0;
12451 int scroll_max;
12452 int rc;
12453 int amount_to_scroll = 0;
12454 Lisp_Object aggressive;
12455 int height;
12456 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12458 #if GLYPH_DEBUG
12459 debug_method_add (w, "try_scrolling");
12460 #endif
12462 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12464 /* Compute scroll margin height in pixels. We scroll when point is
12465 within this distance from the top or bottom of the window. */
12466 if (scroll_margin > 0)
12468 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12469 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12471 else
12472 this_scroll_margin = 0;
12474 /* Force scroll_conservatively to have a reasonable value so it doesn't
12475 cause an overflow while computing how much to scroll. */
12476 if (scroll_conservatively)
12477 scroll_conservatively = min (scroll_conservatively,
12478 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12480 /* Compute how much we should try to scroll maximally to bring point
12481 into view. */
12482 if (scroll_step || scroll_conservatively || temp_scroll_step)
12483 scroll_max = max (scroll_step,
12484 max (scroll_conservatively, temp_scroll_step));
12485 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12486 || NUMBERP (current_buffer->scroll_up_aggressively))
12487 /* We're trying to scroll because of aggressive scrolling
12488 but no scroll_step is set. Choose an arbitrary one. Maybe
12489 there should be a variable for this. */
12490 scroll_max = 10;
12491 else
12492 scroll_max = 0;
12493 scroll_max *= FRAME_LINE_HEIGHT (f);
12495 /* Decide whether we have to scroll down. Start at the window end
12496 and move this_scroll_margin up to find the position of the scroll
12497 margin. */
12498 window_end = Fwindow_end (window, Qt);
12500 too_near_end:
12502 CHARPOS (scroll_margin_pos) = XINT (window_end);
12503 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12505 if (this_scroll_margin || extra_scroll_margin_lines)
12507 start_display (&it, w, scroll_margin_pos);
12508 if (this_scroll_margin)
12509 move_it_vertically_backward (&it, this_scroll_margin);
12510 if (extra_scroll_margin_lines)
12511 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12512 scroll_margin_pos = it.current.pos;
12515 if (PT >= CHARPOS (scroll_margin_pos))
12517 int y0;
12519 /* Point is in the scroll margin at the bottom of the window, or
12520 below. Compute a new window start that makes point visible. */
12522 /* Compute the distance from the scroll margin to PT.
12523 Give up if the distance is greater than scroll_max. */
12524 start_display (&it, w, scroll_margin_pos);
12525 y0 = it.current_y;
12526 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12527 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12529 /* To make point visible, we have to move the window start
12530 down so that the line the cursor is in is visible, which
12531 means we have to add in the height of the cursor line. */
12532 dy = line_bottom_y (&it) - y0;
12534 if (dy > scroll_max)
12535 return SCROLLING_FAILED;
12537 /* Move the window start down. If scrolling conservatively,
12538 move it just enough down to make point visible. If
12539 scroll_step is set, move it down by scroll_step. */
12540 start_display (&it, w, startp);
12542 if (scroll_conservatively)
12543 /* Set AMOUNT_TO_SCROLL to at least one line,
12544 and at most scroll_conservatively lines. */
12545 amount_to_scroll
12546 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12547 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12548 else if (scroll_step || temp_scroll_step)
12549 amount_to_scroll = scroll_max;
12550 else
12552 aggressive = current_buffer->scroll_up_aggressively;
12553 height = WINDOW_BOX_TEXT_HEIGHT (w);
12554 if (NUMBERP (aggressive))
12556 double float_amount = XFLOATINT (aggressive) * height;
12557 amount_to_scroll = float_amount;
12558 if (amount_to_scroll == 0 && float_amount > 0)
12559 amount_to_scroll = 1;
12563 if (amount_to_scroll <= 0)
12564 return SCROLLING_FAILED;
12566 /* If moving by amount_to_scroll leaves STARTP unchanged,
12567 move it down one screen line. */
12569 move_it_vertically (&it, amount_to_scroll);
12570 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12571 move_it_by_lines (&it, 1, 1);
12572 startp = it.current.pos;
12574 else
12576 /* See if point is inside the scroll margin at the top of the
12577 window. */
12578 scroll_margin_pos = startp;
12579 if (this_scroll_margin)
12581 start_display (&it, w, startp);
12582 move_it_vertically (&it, this_scroll_margin);
12583 scroll_margin_pos = it.current.pos;
12586 if (PT < CHARPOS (scroll_margin_pos))
12588 /* Point is in the scroll margin at the top of the window or
12589 above what is displayed in the window. */
12590 int y0;
12592 /* Compute the vertical distance from PT to the scroll
12593 margin position. Give up if distance is greater than
12594 scroll_max. */
12595 SET_TEXT_POS (pos, PT, PT_BYTE);
12596 start_display (&it, w, pos);
12597 y0 = it.current_y;
12598 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12599 it.last_visible_y, -1,
12600 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12601 dy = it.current_y - y0;
12602 if (dy > scroll_max)
12603 return SCROLLING_FAILED;
12605 /* Compute new window start. */
12606 start_display (&it, w, startp);
12608 if (scroll_conservatively)
12609 amount_to_scroll
12610 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12611 else if (scroll_step || temp_scroll_step)
12612 amount_to_scroll = scroll_max;
12613 else
12615 aggressive = current_buffer->scroll_down_aggressively;
12616 height = WINDOW_BOX_TEXT_HEIGHT (w);
12617 if (NUMBERP (aggressive))
12619 double float_amount = XFLOATINT (aggressive) * height;
12620 amount_to_scroll = float_amount;
12621 if (amount_to_scroll == 0 && float_amount > 0)
12622 amount_to_scroll = 1;
12626 if (amount_to_scroll <= 0)
12627 return SCROLLING_FAILED;
12629 move_it_vertically_backward (&it, amount_to_scroll);
12630 startp = it.current.pos;
12634 /* Run window scroll functions. */
12635 startp = run_window_scroll_functions (window, startp);
12637 /* Display the window. Give up if new fonts are loaded, or if point
12638 doesn't appear. */
12639 if (!try_window (window, startp, 0))
12640 rc = SCROLLING_NEED_LARGER_MATRICES;
12641 else if (w->cursor.vpos < 0)
12643 clear_glyph_matrix (w->desired_matrix);
12644 rc = SCROLLING_FAILED;
12646 else
12648 /* Maybe forget recorded base line for line number display. */
12649 if (!just_this_one_p
12650 || current_buffer->clip_changed
12651 || BEG_UNCHANGED < CHARPOS (startp))
12652 w->base_line_number = Qnil;
12654 /* If cursor ends up on a partially visible line,
12655 treat that as being off the bottom of the screen. */
12656 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12658 clear_glyph_matrix (w->desired_matrix);
12659 ++extra_scroll_margin_lines;
12660 goto too_near_end;
12662 rc = SCROLLING_SUCCESS;
12665 return rc;
12669 /* Compute a suitable window start for window W if display of W starts
12670 on a continuation line. Value is non-zero if a new window start
12671 was computed.
12673 The new window start will be computed, based on W's width, starting
12674 from the start of the continued line. It is the start of the
12675 screen line with the minimum distance from the old start W->start. */
12677 static int
12678 compute_window_start_on_continuation_line (w)
12679 struct window *w;
12681 struct text_pos pos, start_pos;
12682 int window_start_changed_p = 0;
12684 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12686 /* If window start is on a continuation line... Window start may be
12687 < BEGV in case there's invisible text at the start of the
12688 buffer (M-x rmail, for example). */
12689 if (CHARPOS (start_pos) > BEGV
12690 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12692 struct it it;
12693 struct glyph_row *row;
12695 /* Handle the case that the window start is out of range. */
12696 if (CHARPOS (start_pos) < BEGV)
12697 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12698 else if (CHARPOS (start_pos) > ZV)
12699 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12701 /* Find the start of the continued line. This should be fast
12702 because scan_buffer is fast (newline cache). */
12703 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12704 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12705 row, DEFAULT_FACE_ID);
12706 reseat_at_previous_visible_line_start (&it);
12708 /* If the line start is "too far" away from the window start,
12709 say it takes too much time to compute a new window start. */
12710 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12711 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12713 int min_distance, distance;
12715 /* Move forward by display lines to find the new window
12716 start. If window width was enlarged, the new start can
12717 be expected to be > the old start. If window width was
12718 decreased, the new window start will be < the old start.
12719 So, we're looking for the display line start with the
12720 minimum distance from the old window start. */
12721 pos = it.current.pos;
12722 min_distance = INFINITY;
12723 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12724 distance < min_distance)
12726 min_distance = distance;
12727 pos = it.current.pos;
12728 move_it_by_lines (&it, 1, 0);
12731 /* Set the window start there. */
12732 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12733 window_start_changed_p = 1;
12737 return window_start_changed_p;
12741 /* Try cursor movement in case text has not changed in window WINDOW,
12742 with window start STARTP. Value is
12744 CURSOR_MOVEMENT_SUCCESS if successful
12746 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12748 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12749 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12750 we want to scroll as if scroll-step were set to 1. See the code.
12752 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12753 which case we have to abort this redisplay, and adjust matrices
12754 first. */
12756 enum
12758 CURSOR_MOVEMENT_SUCCESS,
12759 CURSOR_MOVEMENT_CANNOT_BE_USED,
12760 CURSOR_MOVEMENT_MUST_SCROLL,
12761 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12764 static int
12765 try_cursor_movement (window, startp, scroll_step)
12766 Lisp_Object window;
12767 struct text_pos startp;
12768 int *scroll_step;
12770 struct window *w = XWINDOW (window);
12771 struct frame *f = XFRAME (w->frame);
12772 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12774 #if GLYPH_DEBUG
12775 if (inhibit_try_cursor_movement)
12776 return rc;
12777 #endif
12779 /* Handle case where text has not changed, only point, and it has
12780 not moved off the frame. */
12781 if (/* Point may be in this window. */
12782 PT >= CHARPOS (startp)
12783 /* Selective display hasn't changed. */
12784 && !current_buffer->clip_changed
12785 /* Function force-mode-line-update is used to force a thorough
12786 redisplay. It sets either windows_or_buffers_changed or
12787 update_mode_lines. So don't take a shortcut here for these
12788 cases. */
12789 && !update_mode_lines
12790 && !windows_or_buffers_changed
12791 && !cursor_type_changed
12792 /* Can't use this case if highlighting a region. When a
12793 region exists, cursor movement has to do more than just
12794 set the cursor. */
12795 && !(!NILP (Vtransient_mark_mode)
12796 && !NILP (current_buffer->mark_active))
12797 && NILP (w->region_showing)
12798 && NILP (Vshow_trailing_whitespace)
12799 /* Right after splitting windows, last_point may be nil. */
12800 && INTEGERP (w->last_point)
12801 /* This code is not used for mini-buffer for the sake of the case
12802 of redisplaying to replace an echo area message; since in
12803 that case the mini-buffer contents per se are usually
12804 unchanged. This code is of no real use in the mini-buffer
12805 since the handling of this_line_start_pos, etc., in redisplay
12806 handles the same cases. */
12807 && !EQ (window, minibuf_window)
12808 /* When splitting windows or for new windows, it happens that
12809 redisplay is called with a nil window_end_vpos or one being
12810 larger than the window. This should really be fixed in
12811 window.c. I don't have this on my list, now, so we do
12812 approximately the same as the old redisplay code. --gerd. */
12813 && INTEGERP (w->window_end_vpos)
12814 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12815 && (FRAME_WINDOW_P (f)
12816 || !overlay_arrow_in_current_buffer_p ()))
12818 int this_scroll_margin, top_scroll_margin;
12819 struct glyph_row *row = NULL;
12821 #if GLYPH_DEBUG
12822 debug_method_add (w, "cursor movement");
12823 #endif
12825 /* Scroll if point within this distance from the top or bottom
12826 of the window. This is a pixel value. */
12827 this_scroll_margin = max (0, scroll_margin);
12828 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12829 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12831 top_scroll_margin = this_scroll_margin;
12832 if (WINDOW_WANTS_HEADER_LINE_P (w))
12833 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12835 /* Start with the row the cursor was displayed during the last
12836 not paused redisplay. Give up if that row is not valid. */
12837 if (w->last_cursor.vpos < 0
12838 || w->last_cursor.vpos >= w->current_matrix->nrows)
12839 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12840 else
12842 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12843 if (row->mode_line_p)
12844 ++row;
12845 if (!row->enabled_p)
12846 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12849 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12851 int scroll_p = 0;
12852 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12854 if (PT > XFASTINT (w->last_point))
12856 /* Point has moved forward. */
12857 while (MATRIX_ROW_END_CHARPOS (row) < PT
12858 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12860 xassert (row->enabled_p);
12861 ++row;
12864 /* The end position of a row equals the start position
12865 of the next row. If PT is there, we would rather
12866 display it in the next line. */
12867 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12868 && MATRIX_ROW_END_CHARPOS (row) == PT
12869 && !cursor_row_p (w, row))
12870 ++row;
12872 /* If within the scroll margin, scroll. Note that
12873 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12874 the next line would be drawn, and that
12875 this_scroll_margin can be zero. */
12876 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12877 || PT > MATRIX_ROW_END_CHARPOS (row)
12878 /* Line is completely visible last line in window
12879 and PT is to be set in the next line. */
12880 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12881 && PT == MATRIX_ROW_END_CHARPOS (row)
12882 && !row->ends_at_zv_p
12883 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12884 scroll_p = 1;
12886 else if (PT < XFASTINT (w->last_point))
12888 /* Cursor has to be moved backward. Note that PT >=
12889 CHARPOS (startp) because of the outer if-statement. */
12890 while (!row->mode_line_p
12891 && (MATRIX_ROW_START_CHARPOS (row) > PT
12892 || (MATRIX_ROW_START_CHARPOS (row) == PT
12893 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12894 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12895 row > w->current_matrix->rows
12896 && (row-1)->ends_in_newline_from_string_p))))
12897 && (row->y > top_scroll_margin
12898 || CHARPOS (startp) == BEGV))
12900 xassert (row->enabled_p);
12901 --row;
12904 /* Consider the following case: Window starts at BEGV,
12905 there is invisible, intangible text at BEGV, so that
12906 display starts at some point START > BEGV. It can
12907 happen that we are called with PT somewhere between
12908 BEGV and START. Try to handle that case. */
12909 if (row < w->current_matrix->rows
12910 || row->mode_line_p)
12912 row = w->current_matrix->rows;
12913 if (row->mode_line_p)
12914 ++row;
12917 /* Due to newlines in overlay strings, we may have to
12918 skip forward over overlay strings. */
12919 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12920 && MATRIX_ROW_END_CHARPOS (row) == PT
12921 && !cursor_row_p (w, row))
12922 ++row;
12924 /* If within the scroll margin, scroll. */
12925 if (row->y < top_scroll_margin
12926 && CHARPOS (startp) != BEGV)
12927 scroll_p = 1;
12929 else
12931 /* Cursor did not move. So don't scroll even if cursor line
12932 is partially visible, as it was so before. */
12933 rc = CURSOR_MOVEMENT_SUCCESS;
12936 if (PT < MATRIX_ROW_START_CHARPOS (row)
12937 || PT > MATRIX_ROW_END_CHARPOS (row))
12939 /* if PT is not in the glyph row, give up. */
12940 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12942 else if (rc != CURSOR_MOVEMENT_SUCCESS
12943 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12944 && make_cursor_line_fully_visible_p)
12946 if (PT == MATRIX_ROW_END_CHARPOS (row)
12947 && !row->ends_at_zv_p
12948 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12949 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12950 else if (row->height > window_box_height (w))
12952 /* If we end up in a partially visible line, let's
12953 make it fully visible, except when it's taller
12954 than the window, in which case we can't do much
12955 about it. */
12956 *scroll_step = 1;
12957 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12959 else
12961 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12962 if (!cursor_row_fully_visible_p (w, 0, 1))
12963 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12964 else
12965 rc = CURSOR_MOVEMENT_SUCCESS;
12968 else if (scroll_p)
12969 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12970 else
12974 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12976 rc = CURSOR_MOVEMENT_SUCCESS;
12977 break;
12979 ++row;
12981 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12982 && MATRIX_ROW_START_CHARPOS (row) == PT
12983 && cursor_row_p (w, row));
12988 return rc;
12991 void
12992 set_vertical_scroll_bar (w)
12993 struct window *w;
12995 int start, end, whole;
12997 /* Calculate the start and end positions for the current window.
12998 At some point, it would be nice to choose between scrollbars
12999 which reflect the whole buffer size, with special markers
13000 indicating narrowing, and scrollbars which reflect only the
13001 visible region.
13003 Note that mini-buffers sometimes aren't displaying any text. */
13004 if (!MINI_WINDOW_P (w)
13005 || (w == XWINDOW (minibuf_window)
13006 && NILP (echo_area_buffer[0])))
13008 struct buffer *buf = XBUFFER (w->buffer);
13009 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13010 start = marker_position (w->start) - BUF_BEGV (buf);
13011 /* I don't think this is guaranteed to be right. For the
13012 moment, we'll pretend it is. */
13013 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13015 if (end < start)
13016 end = start;
13017 if (whole < (end - start))
13018 whole = end - start;
13020 else
13021 start = end = whole = 0;
13023 /* Indicate what this scroll bar ought to be displaying now. */
13024 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13025 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13026 (w, end - start, whole, start);
13030 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13031 selected_window is redisplayed.
13033 We can return without actually redisplaying the window if
13034 fonts_changed_p is nonzero. In that case, redisplay_internal will
13035 retry. */
13037 static void
13038 redisplay_window (window, just_this_one_p)
13039 Lisp_Object window;
13040 int just_this_one_p;
13042 struct window *w = XWINDOW (window);
13043 struct frame *f = XFRAME (w->frame);
13044 struct buffer *buffer = XBUFFER (w->buffer);
13045 struct buffer *old = current_buffer;
13046 struct text_pos lpoint, opoint, startp;
13047 int update_mode_line;
13048 int tem;
13049 struct it it;
13050 /* Record it now because it's overwritten. */
13051 int current_matrix_up_to_date_p = 0;
13052 int used_current_matrix_p = 0;
13053 /* This is less strict than current_matrix_up_to_date_p.
13054 It indictes that the buffer contents and narrowing are unchanged. */
13055 int buffer_unchanged_p = 0;
13056 int temp_scroll_step = 0;
13057 int count = SPECPDL_INDEX ();
13058 int rc;
13059 int centering_position = -1;
13060 int last_line_misfit = 0;
13061 int beg_unchanged, end_unchanged;
13063 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13064 opoint = lpoint;
13066 /* W must be a leaf window here. */
13067 xassert (!NILP (w->buffer));
13068 #if GLYPH_DEBUG
13069 *w->desired_matrix->method = 0;
13070 #endif
13072 restart:
13073 reconsider_clip_changes (w, buffer);
13075 /* Has the mode line to be updated? */
13076 update_mode_line = (!NILP (w->update_mode_line)
13077 || update_mode_lines
13078 || buffer->clip_changed
13079 || buffer->prevent_redisplay_optimizations_p);
13081 if (MINI_WINDOW_P (w))
13083 if (w == XWINDOW (echo_area_window)
13084 && !NILP (echo_area_buffer[0]))
13086 if (update_mode_line)
13087 /* We may have to update a tty frame's menu bar or a
13088 tool-bar. Example `M-x C-h C-h C-g'. */
13089 goto finish_menu_bars;
13090 else
13091 /* We've already displayed the echo area glyphs in this window. */
13092 goto finish_scroll_bars;
13094 else if ((w != XWINDOW (minibuf_window)
13095 || minibuf_level == 0)
13096 /* When buffer is nonempty, redisplay window normally. */
13097 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13098 /* Quail displays non-mini buffers in minibuffer window.
13099 In that case, redisplay the window normally. */
13100 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13102 /* W is a mini-buffer window, but it's not active, so clear
13103 it. */
13104 int yb = window_text_bottom_y (w);
13105 struct glyph_row *row;
13106 int y;
13108 for (y = 0, row = w->desired_matrix->rows;
13109 y < yb;
13110 y += row->height, ++row)
13111 blank_row (w, row, y);
13112 goto finish_scroll_bars;
13115 clear_glyph_matrix (w->desired_matrix);
13118 /* Otherwise set up data on this window; select its buffer and point
13119 value. */
13120 /* Really select the buffer, for the sake of buffer-local
13121 variables. */
13122 set_buffer_internal_1 (XBUFFER (w->buffer));
13124 current_matrix_up_to_date_p
13125 = (!NILP (w->window_end_valid)
13126 && !current_buffer->clip_changed
13127 && !current_buffer->prevent_redisplay_optimizations_p
13128 && XFASTINT (w->last_modified) >= MODIFF
13129 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13131 /* Run the window-bottom-change-functions
13132 if it is possible that the text on the screen has changed
13133 (either due to modification of the text, or any other reason). */
13134 if (!current_matrix_up_to_date_p
13135 && !NILP (Vwindow_text_change_functions))
13137 safe_run_hooks (Qwindow_text_change_functions);
13138 goto restart;
13141 beg_unchanged = BEG_UNCHANGED;
13142 end_unchanged = END_UNCHANGED;
13144 SET_TEXT_POS (opoint, PT, PT_BYTE);
13146 specbind (Qinhibit_point_motion_hooks, Qt);
13148 buffer_unchanged_p
13149 = (!NILP (w->window_end_valid)
13150 && !current_buffer->clip_changed
13151 && XFASTINT (w->last_modified) >= MODIFF
13152 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13154 /* When windows_or_buffers_changed is non-zero, we can't rely on
13155 the window end being valid, so set it to nil there. */
13156 if (windows_or_buffers_changed)
13158 /* If window starts on a continuation line, maybe adjust the
13159 window start in case the window's width changed. */
13160 if (XMARKER (w->start)->buffer == current_buffer)
13161 compute_window_start_on_continuation_line (w);
13163 w->window_end_valid = Qnil;
13166 /* Some sanity checks. */
13167 CHECK_WINDOW_END (w);
13168 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13169 abort ();
13170 if (BYTEPOS (opoint) < CHARPOS (opoint))
13171 abort ();
13173 /* If %c is in mode line, update it if needed. */
13174 if (!NILP (w->column_number_displayed)
13175 /* This alternative quickly identifies a common case
13176 where no change is needed. */
13177 && !(PT == XFASTINT (w->last_point)
13178 && XFASTINT (w->last_modified) >= MODIFF
13179 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13180 && (XFASTINT (w->column_number_displayed)
13181 != (int) current_column ())) /* iftc */
13182 update_mode_line = 1;
13184 /* Count number of windows showing the selected buffer. An indirect
13185 buffer counts as its base buffer. */
13186 if (!just_this_one_p)
13188 struct buffer *current_base, *window_base;
13189 current_base = current_buffer;
13190 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13191 if (current_base->base_buffer)
13192 current_base = current_base->base_buffer;
13193 if (window_base->base_buffer)
13194 window_base = window_base->base_buffer;
13195 if (current_base == window_base)
13196 buffer_shared++;
13199 /* Point refers normally to the selected window. For any other
13200 window, set up appropriate value. */
13201 if (!EQ (window, selected_window))
13203 int new_pt = XMARKER (w->pointm)->charpos;
13204 int new_pt_byte = marker_byte_position (w->pointm);
13205 if (new_pt < BEGV)
13207 new_pt = BEGV;
13208 new_pt_byte = BEGV_BYTE;
13209 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13211 else if (new_pt > (ZV - 1))
13213 new_pt = ZV;
13214 new_pt_byte = ZV_BYTE;
13215 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13218 /* We don't use SET_PT so that the point-motion hooks don't run. */
13219 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13222 /* If any of the character widths specified in the display table
13223 have changed, invalidate the width run cache. It's true that
13224 this may be a bit late to catch such changes, but the rest of
13225 redisplay goes (non-fatally) haywire when the display table is
13226 changed, so why should we worry about doing any better? */
13227 if (current_buffer->width_run_cache)
13229 struct Lisp_Char_Table *disptab = buffer_display_table ();
13231 if (! disptab_matches_widthtab (disptab,
13232 XVECTOR (current_buffer->width_table)))
13234 invalidate_region_cache (current_buffer,
13235 current_buffer->width_run_cache,
13236 BEG, Z);
13237 recompute_width_table (current_buffer, disptab);
13241 /* If window-start is screwed up, choose a new one. */
13242 if (XMARKER (w->start)->buffer != current_buffer)
13243 goto recenter;
13245 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13247 /* If someone specified a new starting point but did not insist,
13248 check whether it can be used. */
13249 if (!NILP (w->optional_new_start)
13250 && CHARPOS (startp) >= BEGV
13251 && CHARPOS (startp) <= ZV)
13253 w->optional_new_start = Qnil;
13254 start_display (&it, w, startp);
13255 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13256 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13257 if (IT_CHARPOS (it) == PT)
13258 w->force_start = Qt;
13259 /* IT may overshoot PT if text at PT is invisible. */
13260 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13261 w->force_start = Qt;
13264 force_start:
13266 /* Handle case where place to start displaying has been specified,
13267 unless the specified location is outside the accessible range. */
13268 if (!NILP (w->force_start)
13269 || w->frozen_window_start_p)
13271 /* We set this later on if we have to adjust point. */
13272 int new_vpos = -1;
13273 int val;
13275 w->force_start = Qnil;
13276 w->vscroll = 0;
13277 w->window_end_valid = Qnil;
13279 /* Forget any recorded base line for line number display. */
13280 if (!buffer_unchanged_p)
13281 w->base_line_number = Qnil;
13283 /* Redisplay the mode line. Select the buffer properly for that.
13284 Also, run the hook window-scroll-functions
13285 because we have scrolled. */
13286 /* Note, we do this after clearing force_start because
13287 if there's an error, it is better to forget about force_start
13288 than to get into an infinite loop calling the hook functions
13289 and having them get more errors. */
13290 if (!update_mode_line
13291 || ! NILP (Vwindow_scroll_functions))
13293 update_mode_line = 1;
13294 w->update_mode_line = Qt;
13295 startp = run_window_scroll_functions (window, startp);
13298 w->last_modified = make_number (0);
13299 w->last_overlay_modified = make_number (0);
13300 if (CHARPOS (startp) < BEGV)
13301 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13302 else if (CHARPOS (startp) > ZV)
13303 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13305 /* Redisplay, then check if cursor has been set during the
13306 redisplay. Give up if new fonts were loaded. */
13307 val = try_window (window, startp, 1);
13308 if (!val)
13310 w->force_start = Qt;
13311 clear_glyph_matrix (w->desired_matrix);
13312 goto need_larger_matrices;
13314 /* Point was outside the scroll margins. */
13315 if (val < 0)
13316 new_vpos = window_box_height (w) / 2;
13318 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13320 /* If point does not appear, try to move point so it does
13321 appear. The desired matrix has been built above, so we
13322 can use it here. */
13323 new_vpos = window_box_height (w) / 2;
13326 if (!cursor_row_fully_visible_p (w, 0, 0))
13328 /* Point does appear, but on a line partly visible at end of window.
13329 Move it back to a fully-visible line. */
13330 new_vpos = window_box_height (w);
13333 /* If we need to move point for either of the above reasons,
13334 now actually do it. */
13335 if (new_vpos >= 0)
13337 struct glyph_row *row;
13339 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13340 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13341 ++row;
13343 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13344 MATRIX_ROW_START_BYTEPOS (row));
13346 if (w != XWINDOW (selected_window))
13347 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13348 else if (current_buffer == old)
13349 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13351 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13353 /* If we are highlighting the region, then we just changed
13354 the region, so redisplay to show it. */
13355 if (!NILP (Vtransient_mark_mode)
13356 && !NILP (current_buffer->mark_active))
13358 clear_glyph_matrix (w->desired_matrix);
13359 if (!try_window (window, startp, 0))
13360 goto need_larger_matrices;
13364 #if GLYPH_DEBUG
13365 debug_method_add (w, "forced window start");
13366 #endif
13367 goto done;
13370 /* Handle case where text has not changed, only point, and it has
13371 not moved off the frame, and we are not retrying after hscroll.
13372 (current_matrix_up_to_date_p is nonzero when retrying.) */
13373 if (current_matrix_up_to_date_p
13374 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13375 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13377 switch (rc)
13379 case CURSOR_MOVEMENT_SUCCESS:
13380 used_current_matrix_p = 1;
13381 goto done;
13383 #if 0 /* try_cursor_movement never returns this value. */
13384 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13385 goto need_larger_matrices;
13386 #endif
13388 case CURSOR_MOVEMENT_MUST_SCROLL:
13389 goto try_to_scroll;
13391 default:
13392 abort ();
13395 /* If current starting point was originally the beginning of a line
13396 but no longer is, find a new starting point. */
13397 else if (!NILP (w->start_at_line_beg)
13398 && !(CHARPOS (startp) <= BEGV
13399 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13401 #if GLYPH_DEBUG
13402 debug_method_add (w, "recenter 1");
13403 #endif
13404 goto recenter;
13407 /* Try scrolling with try_window_id. Value is > 0 if update has
13408 been done, it is -1 if we know that the same window start will
13409 not work. It is 0 if unsuccessful for some other reason. */
13410 else if ((tem = try_window_id (w)) != 0)
13412 #if GLYPH_DEBUG
13413 debug_method_add (w, "try_window_id %d", tem);
13414 #endif
13416 if (fonts_changed_p)
13417 goto need_larger_matrices;
13418 if (tem > 0)
13419 goto done;
13421 /* Otherwise try_window_id has returned -1 which means that we
13422 don't want the alternative below this comment to execute. */
13424 else if (CHARPOS (startp) >= BEGV
13425 && CHARPOS (startp) <= ZV
13426 && PT >= CHARPOS (startp)
13427 && (CHARPOS (startp) < ZV
13428 /* Avoid starting at end of buffer. */
13429 || CHARPOS (startp) == BEGV
13430 || (XFASTINT (w->last_modified) >= MODIFF
13431 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13434 /* If first window line is a continuation line, and window start
13435 is inside the modified region, but the first change is before
13436 current window start, we must select a new window start.
13438 However, if this is the result of a down-mouse event (e.g. by
13439 extending the mouse-drag-overlay), we don't want to select a
13440 new window start, since that would change the position under
13441 the mouse, resulting in an unwanted mouse-movement rather
13442 than a simple mouse-click. */
13443 if (NILP (w->start_at_line_beg)
13444 && NILP (do_mouse_tracking)
13445 && CHARPOS (startp) > BEGV
13446 && CHARPOS (startp) > BEG + beg_unchanged
13447 && CHARPOS (startp) <= Z - end_unchanged)
13449 w->force_start = Qt;
13450 if (XMARKER (w->start)->buffer == current_buffer)
13451 compute_window_start_on_continuation_line (w);
13452 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13453 goto force_start;
13456 #if GLYPH_DEBUG
13457 debug_method_add (w, "same window start");
13458 #endif
13460 /* Try to redisplay starting at same place as before.
13461 If point has not moved off frame, accept the results. */
13462 if (!current_matrix_up_to_date_p
13463 /* Don't use try_window_reusing_current_matrix in this case
13464 because a window scroll function can have changed the
13465 buffer. */
13466 || !NILP (Vwindow_scroll_functions)
13467 || MINI_WINDOW_P (w)
13468 || !(used_current_matrix_p
13469 = try_window_reusing_current_matrix (w)))
13471 IF_DEBUG (debug_method_add (w, "1"));
13472 if (try_window (window, startp, 1) < 0)
13473 /* -1 means we need to scroll.
13474 0 means we need new matrices, but fonts_changed_p
13475 is set in that case, so we will detect it below. */
13476 goto try_to_scroll;
13479 if (fonts_changed_p)
13480 goto need_larger_matrices;
13482 if (w->cursor.vpos >= 0)
13484 if (!just_this_one_p
13485 || current_buffer->clip_changed
13486 || BEG_UNCHANGED < CHARPOS (startp))
13487 /* Forget any recorded base line for line number display. */
13488 w->base_line_number = Qnil;
13490 if (!cursor_row_fully_visible_p (w, 1, 0))
13492 clear_glyph_matrix (w->desired_matrix);
13493 last_line_misfit = 1;
13495 /* Drop through and scroll. */
13496 else
13497 goto done;
13499 else
13500 clear_glyph_matrix (w->desired_matrix);
13503 try_to_scroll:
13505 w->last_modified = make_number (0);
13506 w->last_overlay_modified = make_number (0);
13508 /* Redisplay the mode line. Select the buffer properly for that. */
13509 if (!update_mode_line)
13511 update_mode_line = 1;
13512 w->update_mode_line = Qt;
13515 /* Try to scroll by specified few lines. */
13516 if ((scroll_conservatively
13517 || scroll_step
13518 || temp_scroll_step
13519 || NUMBERP (current_buffer->scroll_up_aggressively)
13520 || NUMBERP (current_buffer->scroll_down_aggressively))
13521 && !current_buffer->clip_changed
13522 && CHARPOS (startp) >= BEGV
13523 && CHARPOS (startp) <= ZV)
13525 /* The function returns -1 if new fonts were loaded, 1 if
13526 successful, 0 if not successful. */
13527 int rc = try_scrolling (window, just_this_one_p,
13528 scroll_conservatively,
13529 scroll_step,
13530 temp_scroll_step, last_line_misfit);
13531 switch (rc)
13533 case SCROLLING_SUCCESS:
13534 goto done;
13536 case SCROLLING_NEED_LARGER_MATRICES:
13537 goto need_larger_matrices;
13539 case SCROLLING_FAILED:
13540 break;
13542 default:
13543 abort ();
13547 /* Finally, just choose place to start which centers point */
13549 recenter:
13550 if (centering_position < 0)
13551 centering_position = window_box_height (w) / 2;
13553 #if GLYPH_DEBUG
13554 debug_method_add (w, "recenter");
13555 #endif
13557 /* w->vscroll = 0; */
13559 /* Forget any previously recorded base line for line number display. */
13560 if (!buffer_unchanged_p)
13561 w->base_line_number = Qnil;
13563 /* Move backward half the height of the window. */
13564 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13565 it.current_y = it.last_visible_y;
13566 move_it_vertically_backward (&it, centering_position);
13567 xassert (IT_CHARPOS (it) >= BEGV);
13569 /* The function move_it_vertically_backward may move over more
13570 than the specified y-distance. If it->w is small, e.g. a
13571 mini-buffer window, we may end up in front of the window's
13572 display area. Start displaying at the start of the line
13573 containing PT in this case. */
13574 if (it.current_y <= 0)
13576 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13577 move_it_vertically_backward (&it, 0);
13578 #if 0
13579 /* I think this assert is bogus if buffer contains
13580 invisible text or images. KFS. */
13581 xassert (IT_CHARPOS (it) <= PT);
13582 #endif
13583 it.current_y = 0;
13586 it.current_x = it.hpos = 0;
13588 /* Set startp here explicitly in case that helps avoid an infinite loop
13589 in case the window-scroll-functions functions get errors. */
13590 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13592 /* Run scroll hooks. */
13593 startp = run_window_scroll_functions (window, it.current.pos);
13595 /* Redisplay the window. */
13596 if (!current_matrix_up_to_date_p
13597 || windows_or_buffers_changed
13598 || cursor_type_changed
13599 /* Don't use try_window_reusing_current_matrix in this case
13600 because it can have changed the buffer. */
13601 || !NILP (Vwindow_scroll_functions)
13602 || !just_this_one_p
13603 || MINI_WINDOW_P (w)
13604 || !(used_current_matrix_p
13605 = try_window_reusing_current_matrix (w)))
13606 try_window (window, startp, 0);
13608 /* If new fonts have been loaded (due to fontsets), give up. We
13609 have to start a new redisplay since we need to re-adjust glyph
13610 matrices. */
13611 if (fonts_changed_p)
13612 goto need_larger_matrices;
13614 /* If cursor did not appear assume that the middle of the window is
13615 in the first line of the window. Do it again with the next line.
13616 (Imagine a window of height 100, displaying two lines of height
13617 60. Moving back 50 from it->last_visible_y will end in the first
13618 line.) */
13619 if (w->cursor.vpos < 0)
13621 if (!NILP (w->window_end_valid)
13622 && PT >= Z - XFASTINT (w->window_end_pos))
13624 clear_glyph_matrix (w->desired_matrix);
13625 move_it_by_lines (&it, 1, 0);
13626 try_window (window, it.current.pos, 0);
13628 else if (PT < IT_CHARPOS (it))
13630 clear_glyph_matrix (w->desired_matrix);
13631 move_it_by_lines (&it, -1, 0);
13632 try_window (window, it.current.pos, 0);
13634 else
13636 /* Not much we can do about it. */
13640 /* Consider the following case: Window starts at BEGV, there is
13641 invisible, intangible text at BEGV, so that display starts at
13642 some point START > BEGV. It can happen that we are called with
13643 PT somewhere between BEGV and START. Try to handle that case. */
13644 if (w->cursor.vpos < 0)
13646 struct glyph_row *row = w->current_matrix->rows;
13647 if (row->mode_line_p)
13648 ++row;
13649 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13652 if (!cursor_row_fully_visible_p (w, 0, 0))
13654 /* If vscroll is enabled, disable it and try again. */
13655 if (w->vscroll)
13657 w->vscroll = 0;
13658 clear_glyph_matrix (w->desired_matrix);
13659 goto recenter;
13662 /* If centering point failed to make the whole line visible,
13663 put point at the top instead. That has to make the whole line
13664 visible, if it can be done. */
13665 if (centering_position == 0)
13666 goto done;
13668 clear_glyph_matrix (w->desired_matrix);
13669 centering_position = 0;
13670 goto recenter;
13673 done:
13675 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13676 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13677 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13678 ? Qt : Qnil);
13680 /* Display the mode line, if we must. */
13681 if ((update_mode_line
13682 /* If window not full width, must redo its mode line
13683 if (a) the window to its side is being redone and
13684 (b) we do a frame-based redisplay. This is a consequence
13685 of how inverted lines are drawn in frame-based redisplay. */
13686 || (!just_this_one_p
13687 && !FRAME_WINDOW_P (f)
13688 && !WINDOW_FULL_WIDTH_P (w))
13689 /* Line number to display. */
13690 || INTEGERP (w->base_line_pos)
13691 /* Column number is displayed and different from the one displayed. */
13692 || (!NILP (w->column_number_displayed)
13693 && (XFASTINT (w->column_number_displayed)
13694 != (int) current_column ()))) /* iftc */
13695 /* This means that the window has a mode line. */
13696 && (WINDOW_WANTS_MODELINE_P (w)
13697 || WINDOW_WANTS_HEADER_LINE_P (w)))
13699 display_mode_lines (w);
13701 /* If mode line height has changed, arrange for a thorough
13702 immediate redisplay using the correct mode line height. */
13703 if (WINDOW_WANTS_MODELINE_P (w)
13704 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13706 fonts_changed_p = 1;
13707 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13708 = DESIRED_MODE_LINE_HEIGHT (w);
13711 /* If top line height has changed, arrange for a thorough
13712 immediate redisplay using the correct mode line height. */
13713 if (WINDOW_WANTS_HEADER_LINE_P (w)
13714 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13716 fonts_changed_p = 1;
13717 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13718 = DESIRED_HEADER_LINE_HEIGHT (w);
13721 if (fonts_changed_p)
13722 goto need_larger_matrices;
13725 if (!line_number_displayed
13726 && !BUFFERP (w->base_line_pos))
13728 w->base_line_pos = Qnil;
13729 w->base_line_number = Qnil;
13732 finish_menu_bars:
13734 /* When we reach a frame's selected window, redo the frame's menu bar. */
13735 if (update_mode_line
13736 && EQ (FRAME_SELECTED_WINDOW (f), window))
13738 int redisplay_menu_p = 0;
13739 int redisplay_tool_bar_p = 0;
13741 if (FRAME_WINDOW_P (f))
13743 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13744 || defined (USE_GTK)
13745 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13746 #else
13747 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13748 #endif
13750 else
13751 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13753 if (redisplay_menu_p)
13754 display_menu_bar (w);
13756 #ifdef HAVE_WINDOW_SYSTEM
13757 if (FRAME_WINDOW_P (f))
13759 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13760 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13761 #else
13762 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13763 && (FRAME_TOOL_BAR_LINES (f) > 0
13764 || !NILP (Vauto_resize_tool_bars));
13765 #endif
13767 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13769 extern int ignore_mouse_drag_p;
13770 ignore_mouse_drag_p = 1;
13773 #endif
13776 #ifdef HAVE_WINDOW_SYSTEM
13777 if (FRAME_WINDOW_P (f)
13778 && update_window_fringes (w, (just_this_one_p
13779 || (!used_current_matrix_p && !overlay_arrow_seen)
13780 || w->pseudo_window_p)))
13782 update_begin (f);
13783 BLOCK_INPUT;
13784 if (draw_window_fringes (w, 1))
13785 x_draw_vertical_border (w);
13786 UNBLOCK_INPUT;
13787 update_end (f);
13789 #endif /* HAVE_WINDOW_SYSTEM */
13791 /* We go to this label, with fonts_changed_p nonzero,
13792 if it is necessary to try again using larger glyph matrices.
13793 We have to redeem the scroll bar even in this case,
13794 because the loop in redisplay_internal expects that. */
13795 need_larger_matrices:
13797 finish_scroll_bars:
13799 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13801 /* Set the thumb's position and size. */
13802 set_vertical_scroll_bar (w);
13804 /* Note that we actually used the scroll bar attached to this
13805 window, so it shouldn't be deleted at the end of redisplay. */
13806 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13807 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13810 /* Restore current_buffer and value of point in it. */
13811 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13812 set_buffer_internal_1 (old);
13813 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13814 shorter. This can be caused by log truncation in *Messages*. */
13815 if (CHARPOS (lpoint) <= ZV)
13816 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13818 unbind_to (count, Qnil);
13822 /* Build the complete desired matrix of WINDOW with a window start
13823 buffer position POS.
13825 Value is 1 if successful. It is zero if fonts were loaded during
13826 redisplay which makes re-adjusting glyph matrices necessary, and -1
13827 if point would appear in the scroll margins.
13828 (We check that only if CHECK_MARGINS is nonzero. */
13831 try_window (window, pos, check_margins)
13832 Lisp_Object window;
13833 struct text_pos pos;
13834 int check_margins;
13836 struct window *w = XWINDOW (window);
13837 struct it it;
13838 struct glyph_row *last_text_row = NULL;
13839 struct frame *f = XFRAME (w->frame);
13841 /* Make POS the new window start. */
13842 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13844 /* Mark cursor position as unknown. No overlay arrow seen. */
13845 w->cursor.vpos = -1;
13846 overlay_arrow_seen = 0;
13848 /* Initialize iterator and info to start at POS. */
13849 start_display (&it, w, pos);
13851 /* Display all lines of W. */
13852 while (it.current_y < it.last_visible_y)
13854 if (display_line (&it))
13855 last_text_row = it.glyph_row - 1;
13856 if (fonts_changed_p)
13857 return 0;
13860 /* Don't let the cursor end in the scroll margins. */
13861 if (check_margins
13862 && !MINI_WINDOW_P (w))
13864 int this_scroll_margin;
13866 this_scroll_margin = max (0, scroll_margin);
13867 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13868 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13870 if ((w->cursor.y >= 0 /* not vscrolled */
13871 && w->cursor.y < this_scroll_margin
13872 && CHARPOS (pos) > BEGV
13873 && IT_CHARPOS (it) < ZV)
13874 /* rms: considering make_cursor_line_fully_visible_p here
13875 seems to give wrong results. We don't want to recenter
13876 when the last line is partly visible, we want to allow
13877 that case to be handled in the usual way. */
13878 || (w->cursor.y + 1) > it.last_visible_y)
13880 w->cursor.vpos = -1;
13881 clear_glyph_matrix (w->desired_matrix);
13882 return -1;
13886 /* If bottom moved off end of frame, change mode line percentage. */
13887 if (XFASTINT (w->window_end_pos) <= 0
13888 && Z != IT_CHARPOS (it))
13889 w->update_mode_line = Qt;
13891 /* Set window_end_pos to the offset of the last character displayed
13892 on the window from the end of current_buffer. Set
13893 window_end_vpos to its row number. */
13894 if (last_text_row)
13896 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13897 w->window_end_bytepos
13898 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13899 w->window_end_pos
13900 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13901 w->window_end_vpos
13902 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13903 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13904 ->displays_text_p);
13906 else
13908 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13909 w->window_end_pos = make_number (Z - ZV);
13910 w->window_end_vpos = make_number (0);
13913 /* But that is not valid info until redisplay finishes. */
13914 w->window_end_valid = Qnil;
13915 return 1;
13920 /************************************************************************
13921 Window redisplay reusing current matrix when buffer has not changed
13922 ************************************************************************/
13924 /* Try redisplay of window W showing an unchanged buffer with a
13925 different window start than the last time it was displayed by
13926 reusing its current matrix. Value is non-zero if successful.
13927 W->start is the new window start. */
13929 static int
13930 try_window_reusing_current_matrix (w)
13931 struct window *w;
13933 struct frame *f = XFRAME (w->frame);
13934 struct glyph_row *row, *bottom_row;
13935 struct it it;
13936 struct run run;
13937 struct text_pos start, new_start;
13938 int nrows_scrolled, i;
13939 struct glyph_row *last_text_row;
13940 struct glyph_row *last_reused_text_row;
13941 struct glyph_row *start_row;
13942 int start_vpos, min_y, max_y;
13944 #if GLYPH_DEBUG
13945 if (inhibit_try_window_reusing)
13946 return 0;
13947 #endif
13949 if (/* This function doesn't handle terminal frames. */
13950 !FRAME_WINDOW_P (f)
13951 /* Don't try to reuse the display if windows have been split
13952 or such. */
13953 || windows_or_buffers_changed
13954 || cursor_type_changed)
13955 return 0;
13957 /* Can't do this if region may have changed. */
13958 if ((!NILP (Vtransient_mark_mode)
13959 && !NILP (current_buffer->mark_active))
13960 || !NILP (w->region_showing)
13961 || !NILP (Vshow_trailing_whitespace))
13962 return 0;
13964 /* If top-line visibility has changed, give up. */
13965 if (WINDOW_WANTS_HEADER_LINE_P (w)
13966 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13967 return 0;
13969 /* Give up if old or new display is scrolled vertically. We could
13970 make this function handle this, but right now it doesn't. */
13971 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13972 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13973 return 0;
13975 /* The variable new_start now holds the new window start. The old
13976 start `start' can be determined from the current matrix. */
13977 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13978 start = start_row->start.pos;
13979 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13981 /* Clear the desired matrix for the display below. */
13982 clear_glyph_matrix (w->desired_matrix);
13984 if (CHARPOS (new_start) <= CHARPOS (start))
13986 int first_row_y;
13988 /* Don't use this method if the display starts with an ellipsis
13989 displayed for invisible text. It's not easy to handle that case
13990 below, and it's certainly not worth the effort since this is
13991 not a frequent case. */
13992 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13993 return 0;
13995 IF_DEBUG (debug_method_add (w, "twu1"));
13997 /* Display up to a row that can be reused. The variable
13998 last_text_row is set to the last row displayed that displays
13999 text. Note that it.vpos == 0 if or if not there is a
14000 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14001 start_display (&it, w, new_start);
14002 first_row_y = it.current_y;
14003 w->cursor.vpos = -1;
14004 last_text_row = last_reused_text_row = NULL;
14006 while (it.current_y < it.last_visible_y
14007 && !fonts_changed_p)
14009 /* If we have reached into the characters in the START row,
14010 that means the line boundaries have changed. So we
14011 can't start copying with the row START. Maybe it will
14012 work to start copying with the following row. */
14013 while (IT_CHARPOS (it) > CHARPOS (start))
14015 /* Advance to the next row as the "start". */
14016 start_row++;
14017 start = start_row->start.pos;
14018 /* If there are no more rows to try, or just one, give up. */
14019 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14020 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14021 || CHARPOS (start) == ZV)
14023 clear_glyph_matrix (w->desired_matrix);
14024 return 0;
14027 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14029 /* If we have reached alignment,
14030 we can copy the rest of the rows. */
14031 if (IT_CHARPOS (it) == CHARPOS (start))
14032 break;
14034 if (display_line (&it))
14035 last_text_row = it.glyph_row - 1;
14038 /* A value of current_y < last_visible_y means that we stopped
14039 at the previous window start, which in turn means that we
14040 have at least one reusable row. */
14041 if (it.current_y < it.last_visible_y)
14043 /* IT.vpos always starts from 0; it counts text lines. */
14044 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14046 /* Find PT if not already found in the lines displayed. */
14047 if (w->cursor.vpos < 0)
14049 int dy = it.current_y - start_row->y;
14051 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14052 row = row_containing_pos (w, PT, row, NULL, dy);
14053 if (row)
14054 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14055 dy, nrows_scrolled);
14056 else
14058 clear_glyph_matrix (w->desired_matrix);
14059 return 0;
14063 /* Scroll the display. Do it before the current matrix is
14064 changed. The problem here is that update has not yet
14065 run, i.e. part of the current matrix is not up to date.
14066 scroll_run_hook will clear the cursor, and use the
14067 current matrix to get the height of the row the cursor is
14068 in. */
14069 run.current_y = start_row->y;
14070 run.desired_y = it.current_y;
14071 run.height = it.last_visible_y - it.current_y;
14073 if (run.height > 0 && run.current_y != run.desired_y)
14075 update_begin (f);
14076 FRAME_RIF (f)->update_window_begin_hook (w);
14077 FRAME_RIF (f)->clear_window_mouse_face (w);
14078 FRAME_RIF (f)->scroll_run_hook (w, &run);
14079 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14080 update_end (f);
14083 /* Shift current matrix down by nrows_scrolled lines. */
14084 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14085 rotate_matrix (w->current_matrix,
14086 start_vpos,
14087 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14088 nrows_scrolled);
14090 /* Disable lines that must be updated. */
14091 for (i = 0; i < nrows_scrolled; ++i)
14092 (start_row + i)->enabled_p = 0;
14094 /* Re-compute Y positions. */
14095 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14096 max_y = it.last_visible_y;
14097 for (row = start_row + nrows_scrolled;
14098 row < bottom_row;
14099 ++row)
14101 row->y = it.current_y;
14102 row->visible_height = row->height;
14104 if (row->y < min_y)
14105 row->visible_height -= min_y - row->y;
14106 if (row->y + row->height > max_y)
14107 row->visible_height -= row->y + row->height - max_y;
14108 row->redraw_fringe_bitmaps_p = 1;
14110 it.current_y += row->height;
14112 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14113 last_reused_text_row = row;
14114 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14115 break;
14118 /* Disable lines in the current matrix which are now
14119 below the window. */
14120 for (++row; row < bottom_row; ++row)
14121 row->enabled_p = row->mode_line_p = 0;
14124 /* Update window_end_pos etc.; last_reused_text_row is the last
14125 reused row from the current matrix containing text, if any.
14126 The value of last_text_row is the last displayed line
14127 containing text. */
14128 if (last_reused_text_row)
14130 w->window_end_bytepos
14131 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14132 w->window_end_pos
14133 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14134 w->window_end_vpos
14135 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14136 w->current_matrix));
14138 else if (last_text_row)
14140 w->window_end_bytepos
14141 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14142 w->window_end_pos
14143 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14144 w->window_end_vpos
14145 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14147 else
14149 /* This window must be completely empty. */
14150 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14151 w->window_end_pos = make_number (Z - ZV);
14152 w->window_end_vpos = make_number (0);
14154 w->window_end_valid = Qnil;
14156 /* Update hint: don't try scrolling again in update_window. */
14157 w->desired_matrix->no_scrolling_p = 1;
14159 #if GLYPH_DEBUG
14160 debug_method_add (w, "try_window_reusing_current_matrix 1");
14161 #endif
14162 return 1;
14164 else if (CHARPOS (new_start) > CHARPOS (start))
14166 struct glyph_row *pt_row, *row;
14167 struct glyph_row *first_reusable_row;
14168 struct glyph_row *first_row_to_display;
14169 int dy;
14170 int yb = window_text_bottom_y (w);
14172 /* Find the row starting at new_start, if there is one. Don't
14173 reuse a partially visible line at the end. */
14174 first_reusable_row = start_row;
14175 while (first_reusable_row->enabled_p
14176 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14177 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14178 < CHARPOS (new_start)))
14179 ++first_reusable_row;
14181 /* Give up if there is no row to reuse. */
14182 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14183 || !first_reusable_row->enabled_p
14184 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14185 != CHARPOS (new_start)))
14186 return 0;
14188 /* We can reuse fully visible rows beginning with
14189 first_reusable_row to the end of the window. Set
14190 first_row_to_display to the first row that cannot be reused.
14191 Set pt_row to the row containing point, if there is any. */
14192 pt_row = NULL;
14193 for (first_row_to_display = first_reusable_row;
14194 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14195 ++first_row_to_display)
14197 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14198 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14199 pt_row = first_row_to_display;
14202 /* Start displaying at the start of first_row_to_display. */
14203 xassert (first_row_to_display->y < yb);
14204 init_to_row_start (&it, w, first_row_to_display);
14206 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14207 - start_vpos);
14208 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14209 - nrows_scrolled);
14210 it.current_y = (first_row_to_display->y - first_reusable_row->y
14211 + WINDOW_HEADER_LINE_HEIGHT (w));
14213 /* Display lines beginning with first_row_to_display in the
14214 desired matrix. Set last_text_row to the last row displayed
14215 that displays text. */
14216 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14217 if (pt_row == NULL)
14218 w->cursor.vpos = -1;
14219 last_text_row = NULL;
14220 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14221 if (display_line (&it))
14222 last_text_row = it.glyph_row - 1;
14224 /* Give up If point isn't in a row displayed or reused. */
14225 if (w->cursor.vpos < 0)
14227 clear_glyph_matrix (w->desired_matrix);
14228 return 0;
14231 /* If point is in a reused row, adjust y and vpos of the cursor
14232 position. */
14233 if (pt_row)
14235 w->cursor.vpos -= nrows_scrolled;
14236 w->cursor.y -= first_reusable_row->y - start_row->y;
14239 /* Scroll the display. */
14240 run.current_y = first_reusable_row->y;
14241 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14242 run.height = it.last_visible_y - run.current_y;
14243 dy = run.current_y - run.desired_y;
14245 if (run.height)
14247 update_begin (f);
14248 FRAME_RIF (f)->update_window_begin_hook (w);
14249 FRAME_RIF (f)->clear_window_mouse_face (w);
14250 FRAME_RIF (f)->scroll_run_hook (w, &run);
14251 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14252 update_end (f);
14255 /* Adjust Y positions of reused rows. */
14256 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14257 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14258 max_y = it.last_visible_y;
14259 for (row = first_reusable_row; row < first_row_to_display; ++row)
14261 row->y -= dy;
14262 row->visible_height = row->height;
14263 if (row->y < min_y)
14264 row->visible_height -= min_y - row->y;
14265 if (row->y + row->height > max_y)
14266 row->visible_height -= row->y + row->height - max_y;
14267 row->redraw_fringe_bitmaps_p = 1;
14270 /* Scroll the current matrix. */
14271 xassert (nrows_scrolled > 0);
14272 rotate_matrix (w->current_matrix,
14273 start_vpos,
14274 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14275 -nrows_scrolled);
14277 /* Disable rows not reused. */
14278 for (row -= nrows_scrolled; row < bottom_row; ++row)
14279 row->enabled_p = 0;
14281 /* Point may have moved to a different line, so we cannot assume that
14282 the previous cursor position is valid; locate the correct row. */
14283 if (pt_row)
14285 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14286 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14287 row++)
14289 w->cursor.vpos++;
14290 w->cursor.y = row->y;
14292 if (row < bottom_row)
14294 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14295 while (glyph->charpos < PT)
14297 w->cursor.hpos++;
14298 w->cursor.x += glyph->pixel_width;
14299 glyph++;
14304 /* Adjust window end. A null value of last_text_row means that
14305 the window end is in reused rows which in turn means that
14306 only its vpos can have changed. */
14307 if (last_text_row)
14309 w->window_end_bytepos
14310 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14311 w->window_end_pos
14312 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14313 w->window_end_vpos
14314 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14316 else
14318 w->window_end_vpos
14319 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14322 w->window_end_valid = Qnil;
14323 w->desired_matrix->no_scrolling_p = 1;
14325 #if GLYPH_DEBUG
14326 debug_method_add (w, "try_window_reusing_current_matrix 2");
14327 #endif
14328 return 1;
14331 return 0;
14336 /************************************************************************
14337 Window redisplay reusing current matrix when buffer has changed
14338 ************************************************************************/
14340 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14341 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14342 int *, int *));
14343 static struct glyph_row *
14344 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14345 struct glyph_row *));
14348 /* Return the last row in MATRIX displaying text. If row START is
14349 non-null, start searching with that row. IT gives the dimensions
14350 of the display. Value is null if matrix is empty; otherwise it is
14351 a pointer to the row found. */
14353 static struct glyph_row *
14354 find_last_row_displaying_text (matrix, it, start)
14355 struct glyph_matrix *matrix;
14356 struct it *it;
14357 struct glyph_row *start;
14359 struct glyph_row *row, *row_found;
14361 /* Set row_found to the last row in IT->w's current matrix
14362 displaying text. The loop looks funny but think of partially
14363 visible lines. */
14364 row_found = NULL;
14365 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14366 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14368 xassert (row->enabled_p);
14369 row_found = row;
14370 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14371 break;
14372 ++row;
14375 return row_found;
14379 /* Return the last row in the current matrix of W that is not affected
14380 by changes at the start of current_buffer that occurred since W's
14381 current matrix was built. Value is null if no such row exists.
14383 BEG_UNCHANGED us the number of characters unchanged at the start of
14384 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14385 first changed character in current_buffer. Characters at positions <
14386 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14387 when the current matrix was built. */
14389 static struct glyph_row *
14390 find_last_unchanged_at_beg_row (w)
14391 struct window *w;
14393 int first_changed_pos = BEG + BEG_UNCHANGED;
14394 struct glyph_row *row;
14395 struct glyph_row *row_found = NULL;
14396 int yb = window_text_bottom_y (w);
14398 /* Find the last row displaying unchanged text. */
14399 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14400 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14401 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14403 if (/* If row ends before first_changed_pos, it is unchanged,
14404 except in some case. */
14405 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14406 /* When row ends in ZV and we write at ZV it is not
14407 unchanged. */
14408 && !row->ends_at_zv_p
14409 /* When first_changed_pos is the end of a continued line,
14410 row is not unchanged because it may be no longer
14411 continued. */
14412 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14413 && (row->continued_p
14414 || row->exact_window_width_line_p)))
14415 row_found = row;
14417 /* Stop if last visible row. */
14418 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14419 break;
14421 ++row;
14424 return row_found;
14428 /* Find the first glyph row in the current matrix of W that is not
14429 affected by changes at the end of current_buffer since the
14430 time W's current matrix was built.
14432 Return in *DELTA the number of chars by which buffer positions in
14433 unchanged text at the end of current_buffer must be adjusted.
14435 Return in *DELTA_BYTES the corresponding number of bytes.
14437 Value is null if no such row exists, i.e. all rows are affected by
14438 changes. */
14440 static struct glyph_row *
14441 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14442 struct window *w;
14443 int *delta, *delta_bytes;
14445 struct glyph_row *row;
14446 struct glyph_row *row_found = NULL;
14448 *delta = *delta_bytes = 0;
14450 /* Display must not have been paused, otherwise the current matrix
14451 is not up to date. */
14452 eassert (!NILP (w->window_end_valid));
14454 /* A value of window_end_pos >= END_UNCHANGED means that the window
14455 end is in the range of changed text. If so, there is no
14456 unchanged row at the end of W's current matrix. */
14457 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14458 return NULL;
14460 /* Set row to the last row in W's current matrix displaying text. */
14461 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14463 /* If matrix is entirely empty, no unchanged row exists. */
14464 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14466 /* The value of row is the last glyph row in the matrix having a
14467 meaningful buffer position in it. The end position of row
14468 corresponds to window_end_pos. This allows us to translate
14469 buffer positions in the current matrix to current buffer
14470 positions for characters not in changed text. */
14471 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14472 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14473 int last_unchanged_pos, last_unchanged_pos_old;
14474 struct glyph_row *first_text_row
14475 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14477 *delta = Z - Z_old;
14478 *delta_bytes = Z_BYTE - Z_BYTE_old;
14480 /* Set last_unchanged_pos to the buffer position of the last
14481 character in the buffer that has not been changed. Z is the
14482 index + 1 of the last character in current_buffer, i.e. by
14483 subtracting END_UNCHANGED we get the index of the last
14484 unchanged character, and we have to add BEG to get its buffer
14485 position. */
14486 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14487 last_unchanged_pos_old = last_unchanged_pos - *delta;
14489 /* Search backward from ROW for a row displaying a line that
14490 starts at a minimum position >= last_unchanged_pos_old. */
14491 for (; row > first_text_row; --row)
14493 /* This used to abort, but it can happen.
14494 It is ok to just stop the search instead here. KFS. */
14495 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14496 break;
14498 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14499 row_found = row;
14503 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14505 return row_found;
14509 /* Make sure that glyph rows in the current matrix of window W
14510 reference the same glyph memory as corresponding rows in the
14511 frame's frame matrix. This function is called after scrolling W's
14512 current matrix on a terminal frame in try_window_id and
14513 try_window_reusing_current_matrix. */
14515 static void
14516 sync_frame_with_window_matrix_rows (w)
14517 struct window *w;
14519 struct frame *f = XFRAME (w->frame);
14520 struct glyph_row *window_row, *window_row_end, *frame_row;
14522 /* Preconditions: W must be a leaf window and full-width. Its frame
14523 must have a frame matrix. */
14524 xassert (NILP (w->hchild) && NILP (w->vchild));
14525 xassert (WINDOW_FULL_WIDTH_P (w));
14526 xassert (!FRAME_WINDOW_P (f));
14528 /* If W is a full-width window, glyph pointers in W's current matrix
14529 have, by definition, to be the same as glyph pointers in the
14530 corresponding frame matrix. Note that frame matrices have no
14531 marginal areas (see build_frame_matrix). */
14532 window_row = w->current_matrix->rows;
14533 window_row_end = window_row + w->current_matrix->nrows;
14534 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14535 while (window_row < window_row_end)
14537 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14538 struct glyph *end = window_row->glyphs[LAST_AREA];
14540 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14541 frame_row->glyphs[TEXT_AREA] = start;
14542 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14543 frame_row->glyphs[LAST_AREA] = end;
14545 /* Disable frame rows whose corresponding window rows have
14546 been disabled in try_window_id. */
14547 if (!window_row->enabled_p)
14548 frame_row->enabled_p = 0;
14550 ++window_row, ++frame_row;
14555 /* Find the glyph row in window W containing CHARPOS. Consider all
14556 rows between START and END (not inclusive). END null means search
14557 all rows to the end of the display area of W. Value is the row
14558 containing CHARPOS or null. */
14560 struct glyph_row *
14561 row_containing_pos (w, charpos, start, end, dy)
14562 struct window *w;
14563 int charpos;
14564 struct glyph_row *start, *end;
14565 int dy;
14567 struct glyph_row *row = start;
14568 int last_y;
14570 /* If we happen to start on a header-line, skip that. */
14571 if (row->mode_line_p)
14572 ++row;
14574 if ((end && row >= end) || !row->enabled_p)
14575 return NULL;
14577 last_y = window_text_bottom_y (w) - dy;
14579 while (1)
14581 /* Give up if we have gone too far. */
14582 if (end && row >= end)
14583 return NULL;
14584 /* This formerly returned if they were equal.
14585 I think that both quantities are of a "last plus one" type;
14586 if so, when they are equal, the row is within the screen. -- rms. */
14587 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14588 return NULL;
14590 /* If it is in this row, return this row. */
14591 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14592 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14593 /* The end position of a row equals the start
14594 position of the next row. If CHARPOS is there, we
14595 would rather display it in the next line, except
14596 when this line ends in ZV. */
14597 && !row->ends_at_zv_p
14598 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14599 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14600 return row;
14601 ++row;
14606 /* Try to redisplay window W by reusing its existing display. W's
14607 current matrix must be up to date when this function is called,
14608 i.e. window_end_valid must not be nil.
14610 Value is
14612 1 if display has been updated
14613 0 if otherwise unsuccessful
14614 -1 if redisplay with same window start is known not to succeed
14616 The following steps are performed:
14618 1. Find the last row in the current matrix of W that is not
14619 affected by changes at the start of current_buffer. If no such row
14620 is found, give up.
14622 2. Find the first row in W's current matrix that is not affected by
14623 changes at the end of current_buffer. Maybe there is no such row.
14625 3. Display lines beginning with the row + 1 found in step 1 to the
14626 row found in step 2 or, if step 2 didn't find a row, to the end of
14627 the window.
14629 4. If cursor is not known to appear on the window, give up.
14631 5. If display stopped at the row found in step 2, scroll the
14632 display and current matrix as needed.
14634 6. Maybe display some lines at the end of W, if we must. This can
14635 happen under various circumstances, like a partially visible line
14636 becoming fully visible, or because newly displayed lines are displayed
14637 in smaller font sizes.
14639 7. Update W's window end information. */
14641 static int
14642 try_window_id (w)
14643 struct window *w;
14645 struct frame *f = XFRAME (w->frame);
14646 struct glyph_matrix *current_matrix = w->current_matrix;
14647 struct glyph_matrix *desired_matrix = w->desired_matrix;
14648 struct glyph_row *last_unchanged_at_beg_row;
14649 struct glyph_row *first_unchanged_at_end_row;
14650 struct glyph_row *row;
14651 struct glyph_row *bottom_row;
14652 int bottom_vpos;
14653 struct it it;
14654 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14655 struct text_pos start_pos;
14656 struct run run;
14657 int first_unchanged_at_end_vpos = 0;
14658 struct glyph_row *last_text_row, *last_text_row_at_end;
14659 struct text_pos start;
14660 int first_changed_charpos, last_changed_charpos;
14662 #if GLYPH_DEBUG
14663 if (inhibit_try_window_id)
14664 return 0;
14665 #endif
14667 /* This is handy for debugging. */
14668 #if 0
14669 #define GIVE_UP(X) \
14670 do { \
14671 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14672 return 0; \
14673 } while (0)
14674 #else
14675 #define GIVE_UP(X) return 0
14676 #endif
14678 SET_TEXT_POS_FROM_MARKER (start, w->start);
14680 /* Don't use this for mini-windows because these can show
14681 messages and mini-buffers, and we don't handle that here. */
14682 if (MINI_WINDOW_P (w))
14683 GIVE_UP (1);
14685 /* This flag is used to prevent redisplay optimizations. */
14686 if (windows_or_buffers_changed || cursor_type_changed)
14687 GIVE_UP (2);
14689 /* Verify that narrowing has not changed.
14690 Also verify that we were not told to prevent redisplay optimizations.
14691 It would be nice to further
14692 reduce the number of cases where this prevents try_window_id. */
14693 if (current_buffer->clip_changed
14694 || current_buffer->prevent_redisplay_optimizations_p)
14695 GIVE_UP (3);
14697 /* Window must either use window-based redisplay or be full width. */
14698 if (!FRAME_WINDOW_P (f)
14699 && (!FRAME_LINE_INS_DEL_OK (f)
14700 || !WINDOW_FULL_WIDTH_P (w)))
14701 GIVE_UP (4);
14703 /* Give up if point is not known NOT to appear in W. */
14704 if (PT < CHARPOS (start))
14705 GIVE_UP (5);
14707 /* Another way to prevent redisplay optimizations. */
14708 if (XFASTINT (w->last_modified) == 0)
14709 GIVE_UP (6);
14711 /* Verify that window is not hscrolled. */
14712 if (XFASTINT (w->hscroll) != 0)
14713 GIVE_UP (7);
14715 /* Verify that display wasn't paused. */
14716 if (NILP (w->window_end_valid))
14717 GIVE_UP (8);
14719 /* Can't use this if highlighting a region because a cursor movement
14720 will do more than just set the cursor. */
14721 if (!NILP (Vtransient_mark_mode)
14722 && !NILP (current_buffer->mark_active))
14723 GIVE_UP (9);
14725 /* Likewise if highlighting trailing whitespace. */
14726 if (!NILP (Vshow_trailing_whitespace))
14727 GIVE_UP (11);
14729 /* Likewise if showing a region. */
14730 if (!NILP (w->region_showing))
14731 GIVE_UP (10);
14733 /* Can use this if overlay arrow position and or string have changed. */
14734 if (overlay_arrows_changed_p ())
14735 GIVE_UP (12);
14738 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14739 only if buffer has really changed. The reason is that the gap is
14740 initially at Z for freshly visited files. The code below would
14741 set end_unchanged to 0 in that case. */
14742 if (MODIFF > SAVE_MODIFF
14743 /* This seems to happen sometimes after saving a buffer. */
14744 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14746 if (GPT - BEG < BEG_UNCHANGED)
14747 BEG_UNCHANGED = GPT - BEG;
14748 if (Z - GPT < END_UNCHANGED)
14749 END_UNCHANGED = Z - GPT;
14752 /* The position of the first and last character that has been changed. */
14753 first_changed_charpos = BEG + BEG_UNCHANGED;
14754 last_changed_charpos = Z - END_UNCHANGED;
14756 /* If window starts after a line end, and the last change is in
14757 front of that newline, then changes don't affect the display.
14758 This case happens with stealth-fontification. Note that although
14759 the display is unchanged, glyph positions in the matrix have to
14760 be adjusted, of course. */
14761 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14762 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14763 && ((last_changed_charpos < CHARPOS (start)
14764 && CHARPOS (start) == BEGV)
14765 || (last_changed_charpos < CHARPOS (start) - 1
14766 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14768 int Z_old, delta, Z_BYTE_old, delta_bytes;
14769 struct glyph_row *r0;
14771 /* Compute how many chars/bytes have been added to or removed
14772 from the buffer. */
14773 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14774 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14775 delta = Z - Z_old;
14776 delta_bytes = Z_BYTE - Z_BYTE_old;
14778 /* Give up if PT is not in the window. Note that it already has
14779 been checked at the start of try_window_id that PT is not in
14780 front of the window start. */
14781 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14782 GIVE_UP (13);
14784 /* If window start is unchanged, we can reuse the whole matrix
14785 as is, after adjusting glyph positions. No need to compute
14786 the window end again, since its offset from Z hasn't changed. */
14787 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14788 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14789 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14790 /* PT must not be in a partially visible line. */
14791 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14792 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14794 /* Adjust positions in the glyph matrix. */
14795 if (delta || delta_bytes)
14797 struct glyph_row *r1
14798 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14799 increment_matrix_positions (w->current_matrix,
14800 MATRIX_ROW_VPOS (r0, current_matrix),
14801 MATRIX_ROW_VPOS (r1, current_matrix),
14802 delta, delta_bytes);
14805 /* Set the cursor. */
14806 row = row_containing_pos (w, PT, r0, NULL, 0);
14807 if (row)
14808 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14809 else
14810 abort ();
14811 return 1;
14815 /* Handle the case that changes are all below what is displayed in
14816 the window, and that PT is in the window. This shortcut cannot
14817 be taken if ZV is visible in the window, and text has been added
14818 there that is visible in the window. */
14819 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14820 /* ZV is not visible in the window, or there are no
14821 changes at ZV, actually. */
14822 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14823 || first_changed_charpos == last_changed_charpos))
14825 struct glyph_row *r0;
14827 /* Give up if PT is not in the window. Note that it already has
14828 been checked at the start of try_window_id that PT is not in
14829 front of the window start. */
14830 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14831 GIVE_UP (14);
14833 /* If window start is unchanged, we can reuse the whole matrix
14834 as is, without changing glyph positions since no text has
14835 been added/removed in front of the window end. */
14836 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14837 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14838 /* PT must not be in a partially visible line. */
14839 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14840 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14842 /* We have to compute the window end anew since text
14843 can have been added/removed after it. */
14844 w->window_end_pos
14845 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14846 w->window_end_bytepos
14847 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14849 /* Set the cursor. */
14850 row = row_containing_pos (w, PT, r0, NULL, 0);
14851 if (row)
14852 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14853 else
14854 abort ();
14855 return 2;
14859 /* Give up if window start is in the changed area.
14861 The condition used to read
14863 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14865 but why that was tested escapes me at the moment. */
14866 if (CHARPOS (start) >= first_changed_charpos
14867 && CHARPOS (start) <= last_changed_charpos)
14868 GIVE_UP (15);
14870 /* Check that window start agrees with the start of the first glyph
14871 row in its current matrix. Check this after we know the window
14872 start is not in changed text, otherwise positions would not be
14873 comparable. */
14874 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14875 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14876 GIVE_UP (16);
14878 /* Give up if the window ends in strings. Overlay strings
14879 at the end are difficult to handle, so don't try. */
14880 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14881 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14882 GIVE_UP (20);
14884 /* Compute the position at which we have to start displaying new
14885 lines. Some of the lines at the top of the window might be
14886 reusable because they are not displaying changed text. Find the
14887 last row in W's current matrix not affected by changes at the
14888 start of current_buffer. Value is null if changes start in the
14889 first line of window. */
14890 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14891 if (last_unchanged_at_beg_row)
14893 /* Avoid starting to display in the moddle of a character, a TAB
14894 for instance. This is easier than to set up the iterator
14895 exactly, and it's not a frequent case, so the additional
14896 effort wouldn't really pay off. */
14897 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14898 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14899 && last_unchanged_at_beg_row > w->current_matrix->rows)
14900 --last_unchanged_at_beg_row;
14902 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14903 GIVE_UP (17);
14905 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14906 GIVE_UP (18);
14907 start_pos = it.current.pos;
14909 /* Start displaying new lines in the desired matrix at the same
14910 vpos we would use in the current matrix, i.e. below
14911 last_unchanged_at_beg_row. */
14912 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14913 current_matrix);
14914 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14915 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14917 xassert (it.hpos == 0 && it.current_x == 0);
14919 else
14921 /* There are no reusable lines at the start of the window.
14922 Start displaying in the first text line. */
14923 start_display (&it, w, start);
14924 it.vpos = it.first_vpos;
14925 start_pos = it.current.pos;
14928 /* Find the first row that is not affected by changes at the end of
14929 the buffer. Value will be null if there is no unchanged row, in
14930 which case we must redisplay to the end of the window. delta
14931 will be set to the value by which buffer positions beginning with
14932 first_unchanged_at_end_row have to be adjusted due to text
14933 changes. */
14934 first_unchanged_at_end_row
14935 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14936 IF_DEBUG (debug_delta = delta);
14937 IF_DEBUG (debug_delta_bytes = delta_bytes);
14939 /* Set stop_pos to the buffer position up to which we will have to
14940 display new lines. If first_unchanged_at_end_row != NULL, this
14941 is the buffer position of the start of the line displayed in that
14942 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14943 that we don't stop at a buffer position. */
14944 stop_pos = 0;
14945 if (first_unchanged_at_end_row)
14947 xassert (last_unchanged_at_beg_row == NULL
14948 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14950 /* If this is a continuation line, move forward to the next one
14951 that isn't. Changes in lines above affect this line.
14952 Caution: this may move first_unchanged_at_end_row to a row
14953 not displaying text. */
14954 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14955 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14956 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14957 < it.last_visible_y))
14958 ++first_unchanged_at_end_row;
14960 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14961 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14962 >= it.last_visible_y))
14963 first_unchanged_at_end_row = NULL;
14964 else
14966 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14967 + delta);
14968 first_unchanged_at_end_vpos
14969 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14970 xassert (stop_pos >= Z - END_UNCHANGED);
14973 else if (last_unchanged_at_beg_row == NULL)
14974 GIVE_UP (19);
14977 #if GLYPH_DEBUG
14979 /* Either there is no unchanged row at the end, or the one we have
14980 now displays text. This is a necessary condition for the window
14981 end pos calculation at the end of this function. */
14982 xassert (first_unchanged_at_end_row == NULL
14983 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14985 debug_last_unchanged_at_beg_vpos
14986 = (last_unchanged_at_beg_row
14987 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14988 : -1);
14989 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14991 #endif /* GLYPH_DEBUG != 0 */
14994 /* Display new lines. Set last_text_row to the last new line
14995 displayed which has text on it, i.e. might end up as being the
14996 line where the window_end_vpos is. */
14997 w->cursor.vpos = -1;
14998 last_text_row = NULL;
14999 overlay_arrow_seen = 0;
15000 while (it.current_y < it.last_visible_y
15001 && !fonts_changed_p
15002 && (first_unchanged_at_end_row == NULL
15003 || IT_CHARPOS (it) < stop_pos))
15005 if (display_line (&it))
15006 last_text_row = it.glyph_row - 1;
15009 if (fonts_changed_p)
15010 return -1;
15013 /* Compute differences in buffer positions, y-positions etc. for
15014 lines reused at the bottom of the window. Compute what we can
15015 scroll. */
15016 if (first_unchanged_at_end_row
15017 /* No lines reused because we displayed everything up to the
15018 bottom of the window. */
15019 && it.current_y < it.last_visible_y)
15021 dvpos = (it.vpos
15022 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15023 current_matrix));
15024 dy = it.current_y - first_unchanged_at_end_row->y;
15025 run.current_y = first_unchanged_at_end_row->y;
15026 run.desired_y = run.current_y + dy;
15027 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15029 else
15031 delta = delta_bytes = dvpos = dy
15032 = run.current_y = run.desired_y = run.height = 0;
15033 first_unchanged_at_end_row = NULL;
15035 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15038 /* Find the cursor if not already found. We have to decide whether
15039 PT will appear on this window (it sometimes doesn't, but this is
15040 not a very frequent case.) This decision has to be made before
15041 the current matrix is altered. A value of cursor.vpos < 0 means
15042 that PT is either in one of the lines beginning at
15043 first_unchanged_at_end_row or below the window. Don't care for
15044 lines that might be displayed later at the window end; as
15045 mentioned, this is not a frequent case. */
15046 if (w->cursor.vpos < 0)
15048 /* Cursor in unchanged rows at the top? */
15049 if (PT < CHARPOS (start_pos)
15050 && last_unchanged_at_beg_row)
15052 row = row_containing_pos (w, PT,
15053 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15054 last_unchanged_at_beg_row + 1, 0);
15055 if (row)
15056 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15059 /* Start from first_unchanged_at_end_row looking for PT. */
15060 else if (first_unchanged_at_end_row)
15062 row = row_containing_pos (w, PT - delta,
15063 first_unchanged_at_end_row, NULL, 0);
15064 if (row)
15065 set_cursor_from_row (w, row, w->current_matrix, delta,
15066 delta_bytes, dy, dvpos);
15069 /* Give up if cursor was not found. */
15070 if (w->cursor.vpos < 0)
15072 clear_glyph_matrix (w->desired_matrix);
15073 return -1;
15077 /* Don't let the cursor end in the scroll margins. */
15079 int this_scroll_margin, cursor_height;
15081 this_scroll_margin = max (0, scroll_margin);
15082 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15083 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15084 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15086 if ((w->cursor.y < this_scroll_margin
15087 && CHARPOS (start) > BEGV)
15088 /* Old redisplay didn't take scroll margin into account at the bottom,
15089 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15090 || (w->cursor.y + (make_cursor_line_fully_visible_p
15091 ? cursor_height + this_scroll_margin
15092 : 1)) > it.last_visible_y)
15094 w->cursor.vpos = -1;
15095 clear_glyph_matrix (w->desired_matrix);
15096 return -1;
15100 /* Scroll the display. Do it before changing the current matrix so
15101 that xterm.c doesn't get confused about where the cursor glyph is
15102 found. */
15103 if (dy && run.height)
15105 update_begin (f);
15107 if (FRAME_WINDOW_P (f))
15109 FRAME_RIF (f)->update_window_begin_hook (w);
15110 FRAME_RIF (f)->clear_window_mouse_face (w);
15111 FRAME_RIF (f)->scroll_run_hook (w, &run);
15112 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15114 else
15116 /* Terminal frame. In this case, dvpos gives the number of
15117 lines to scroll by; dvpos < 0 means scroll up. */
15118 int first_unchanged_at_end_vpos
15119 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15120 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15121 int end = (WINDOW_TOP_EDGE_LINE (w)
15122 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15123 + window_internal_height (w));
15125 /* Perform the operation on the screen. */
15126 if (dvpos > 0)
15128 /* Scroll last_unchanged_at_beg_row to the end of the
15129 window down dvpos lines. */
15130 set_terminal_window (f, end);
15132 /* On dumb terminals delete dvpos lines at the end
15133 before inserting dvpos empty lines. */
15134 if (!FRAME_SCROLL_REGION_OK (f))
15135 ins_del_lines (f, end - dvpos, -dvpos);
15137 /* Insert dvpos empty lines in front of
15138 last_unchanged_at_beg_row. */
15139 ins_del_lines (f, from, dvpos);
15141 else if (dvpos < 0)
15143 /* Scroll up last_unchanged_at_beg_vpos to the end of
15144 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15145 set_terminal_window (f, end);
15147 /* Delete dvpos lines in front of
15148 last_unchanged_at_beg_vpos. ins_del_lines will set
15149 the cursor to the given vpos and emit |dvpos| delete
15150 line sequences. */
15151 ins_del_lines (f, from + dvpos, dvpos);
15153 /* On a dumb terminal insert dvpos empty lines at the
15154 end. */
15155 if (!FRAME_SCROLL_REGION_OK (f))
15156 ins_del_lines (f, end + dvpos, -dvpos);
15159 set_terminal_window (f, 0);
15162 update_end (f);
15165 /* Shift reused rows of the current matrix to the right position.
15166 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15167 text. */
15168 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15169 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15170 if (dvpos < 0)
15172 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15173 bottom_vpos, dvpos);
15174 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15175 bottom_vpos, 0);
15177 else if (dvpos > 0)
15179 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15180 bottom_vpos, dvpos);
15181 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15182 first_unchanged_at_end_vpos + dvpos, 0);
15185 /* For frame-based redisplay, make sure that current frame and window
15186 matrix are in sync with respect to glyph memory. */
15187 if (!FRAME_WINDOW_P (f))
15188 sync_frame_with_window_matrix_rows (w);
15190 /* Adjust buffer positions in reused rows. */
15191 if (delta || delta_bytes)
15192 increment_matrix_positions (current_matrix,
15193 first_unchanged_at_end_vpos + dvpos,
15194 bottom_vpos, delta, delta_bytes);
15196 /* Adjust Y positions. */
15197 if (dy)
15198 shift_glyph_matrix (w, current_matrix,
15199 first_unchanged_at_end_vpos + dvpos,
15200 bottom_vpos, dy);
15202 if (first_unchanged_at_end_row)
15204 first_unchanged_at_end_row += dvpos;
15205 if (first_unchanged_at_end_row->y >= it.last_visible_y
15206 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15207 first_unchanged_at_end_row = NULL;
15210 /* If scrolling up, there may be some lines to display at the end of
15211 the window. */
15212 last_text_row_at_end = NULL;
15213 if (dy < 0)
15215 /* Scrolling up can leave for example a partially visible line
15216 at the end of the window to be redisplayed. */
15217 /* Set last_row to the glyph row in the current matrix where the
15218 window end line is found. It has been moved up or down in
15219 the matrix by dvpos. */
15220 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15221 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15223 /* If last_row is the window end line, it should display text. */
15224 xassert (last_row->displays_text_p);
15226 /* If window end line was partially visible before, begin
15227 displaying at that line. Otherwise begin displaying with the
15228 line following it. */
15229 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15231 init_to_row_start (&it, w, last_row);
15232 it.vpos = last_vpos;
15233 it.current_y = last_row->y;
15235 else
15237 init_to_row_end (&it, w, last_row);
15238 it.vpos = 1 + last_vpos;
15239 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15240 ++last_row;
15243 /* We may start in a continuation line. If so, we have to
15244 get the right continuation_lines_width and current_x. */
15245 it.continuation_lines_width = last_row->continuation_lines_width;
15246 it.hpos = it.current_x = 0;
15248 /* Display the rest of the lines at the window end. */
15249 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15250 while (it.current_y < it.last_visible_y
15251 && !fonts_changed_p)
15253 /* Is it always sure that the display agrees with lines in
15254 the current matrix? I don't think so, so we mark rows
15255 displayed invalid in the current matrix by setting their
15256 enabled_p flag to zero. */
15257 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15258 if (display_line (&it))
15259 last_text_row_at_end = it.glyph_row - 1;
15263 /* Update window_end_pos and window_end_vpos. */
15264 if (first_unchanged_at_end_row
15265 && !last_text_row_at_end)
15267 /* Window end line if one of the preserved rows from the current
15268 matrix. Set row to the last row displaying text in current
15269 matrix starting at first_unchanged_at_end_row, after
15270 scrolling. */
15271 xassert (first_unchanged_at_end_row->displays_text_p);
15272 row = find_last_row_displaying_text (w->current_matrix, &it,
15273 first_unchanged_at_end_row);
15274 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15276 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15277 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15278 w->window_end_vpos
15279 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15280 xassert (w->window_end_bytepos >= 0);
15281 IF_DEBUG (debug_method_add (w, "A"));
15283 else if (last_text_row_at_end)
15285 w->window_end_pos
15286 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15287 w->window_end_bytepos
15288 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15289 w->window_end_vpos
15290 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15291 xassert (w->window_end_bytepos >= 0);
15292 IF_DEBUG (debug_method_add (w, "B"));
15294 else if (last_text_row)
15296 /* We have displayed either to the end of the window or at the
15297 end of the window, i.e. the last row with text is to be found
15298 in the desired matrix. */
15299 w->window_end_pos
15300 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15301 w->window_end_bytepos
15302 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15303 w->window_end_vpos
15304 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15305 xassert (w->window_end_bytepos >= 0);
15307 else if (first_unchanged_at_end_row == NULL
15308 && last_text_row == NULL
15309 && last_text_row_at_end == NULL)
15311 /* Displayed to end of window, but no line containing text was
15312 displayed. Lines were deleted at the end of the window. */
15313 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15314 int vpos = XFASTINT (w->window_end_vpos);
15315 struct glyph_row *current_row = current_matrix->rows + vpos;
15316 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15318 for (row = NULL;
15319 row == NULL && vpos >= first_vpos;
15320 --vpos, --current_row, --desired_row)
15322 if (desired_row->enabled_p)
15324 if (desired_row->displays_text_p)
15325 row = desired_row;
15327 else if (current_row->displays_text_p)
15328 row = current_row;
15331 xassert (row != NULL);
15332 w->window_end_vpos = make_number (vpos + 1);
15333 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15334 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15335 xassert (w->window_end_bytepos >= 0);
15336 IF_DEBUG (debug_method_add (w, "C"));
15338 else
15339 abort ();
15341 #if 0 /* This leads to problems, for instance when the cursor is
15342 at ZV, and the cursor line displays no text. */
15343 /* Disable rows below what's displayed in the window. This makes
15344 debugging easier. */
15345 enable_glyph_matrix_rows (current_matrix,
15346 XFASTINT (w->window_end_vpos) + 1,
15347 bottom_vpos, 0);
15348 #endif
15350 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15351 debug_end_vpos = XFASTINT (w->window_end_vpos));
15353 /* Record that display has not been completed. */
15354 w->window_end_valid = Qnil;
15355 w->desired_matrix->no_scrolling_p = 1;
15356 return 3;
15358 #undef GIVE_UP
15363 /***********************************************************************
15364 More debugging support
15365 ***********************************************************************/
15367 #if GLYPH_DEBUG
15369 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15370 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15371 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15374 /* Dump the contents of glyph matrix MATRIX on stderr.
15376 GLYPHS 0 means don't show glyph contents.
15377 GLYPHS 1 means show glyphs in short form
15378 GLYPHS > 1 means show glyphs in long form. */
15380 void
15381 dump_glyph_matrix (matrix, glyphs)
15382 struct glyph_matrix *matrix;
15383 int glyphs;
15385 int i;
15386 for (i = 0; i < matrix->nrows; ++i)
15387 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15391 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15392 the glyph row and area where the glyph comes from. */
15394 void
15395 dump_glyph (row, glyph, area)
15396 struct glyph_row *row;
15397 struct glyph *glyph;
15398 int area;
15400 if (glyph->type == CHAR_GLYPH)
15402 fprintf (stderr,
15403 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15404 glyph - row->glyphs[TEXT_AREA],
15405 'C',
15406 glyph->charpos,
15407 (BUFFERP (glyph->object)
15408 ? 'B'
15409 : (STRINGP (glyph->object)
15410 ? 'S'
15411 : '-')),
15412 glyph->pixel_width,
15413 glyph->u.ch,
15414 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15415 ? glyph->u.ch
15416 : '.'),
15417 glyph->face_id,
15418 glyph->left_box_line_p,
15419 glyph->right_box_line_p);
15421 else if (glyph->type == STRETCH_GLYPH)
15423 fprintf (stderr,
15424 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15425 glyph - row->glyphs[TEXT_AREA],
15426 'S',
15427 glyph->charpos,
15428 (BUFFERP (glyph->object)
15429 ? 'B'
15430 : (STRINGP (glyph->object)
15431 ? 'S'
15432 : '-')),
15433 glyph->pixel_width,
15435 '.',
15436 glyph->face_id,
15437 glyph->left_box_line_p,
15438 glyph->right_box_line_p);
15440 else if (glyph->type == IMAGE_GLYPH)
15442 fprintf (stderr,
15443 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15444 glyph - row->glyphs[TEXT_AREA],
15445 'I',
15446 glyph->charpos,
15447 (BUFFERP (glyph->object)
15448 ? 'B'
15449 : (STRINGP (glyph->object)
15450 ? 'S'
15451 : '-')),
15452 glyph->pixel_width,
15453 glyph->u.img_id,
15454 '.',
15455 glyph->face_id,
15456 glyph->left_box_line_p,
15457 glyph->right_box_line_p);
15459 else if (glyph->type == COMPOSITE_GLYPH)
15461 fprintf (stderr,
15462 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15463 glyph - row->glyphs[TEXT_AREA],
15464 '+',
15465 glyph->charpos,
15466 (BUFFERP (glyph->object)
15467 ? 'B'
15468 : (STRINGP (glyph->object)
15469 ? 'S'
15470 : '-')),
15471 glyph->pixel_width,
15472 glyph->u.cmp_id,
15473 '.',
15474 glyph->face_id,
15475 glyph->left_box_line_p,
15476 glyph->right_box_line_p);
15481 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15482 GLYPHS 0 means don't show glyph contents.
15483 GLYPHS 1 means show glyphs in short form
15484 GLYPHS > 1 means show glyphs in long form. */
15486 void
15487 dump_glyph_row (row, vpos, glyphs)
15488 struct glyph_row *row;
15489 int vpos, glyphs;
15491 if (glyphs != 1)
15493 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15494 fprintf (stderr, "======================================================================\n");
15496 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15497 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15498 vpos,
15499 MATRIX_ROW_START_CHARPOS (row),
15500 MATRIX_ROW_END_CHARPOS (row),
15501 row->used[TEXT_AREA],
15502 row->contains_overlapping_glyphs_p,
15503 row->enabled_p,
15504 row->truncated_on_left_p,
15505 row->truncated_on_right_p,
15506 row->continued_p,
15507 MATRIX_ROW_CONTINUATION_LINE_P (row),
15508 row->displays_text_p,
15509 row->ends_at_zv_p,
15510 row->fill_line_p,
15511 row->ends_in_middle_of_char_p,
15512 row->starts_in_middle_of_char_p,
15513 row->mouse_face_p,
15514 row->x,
15515 row->y,
15516 row->pixel_width,
15517 row->height,
15518 row->visible_height,
15519 row->ascent,
15520 row->phys_ascent);
15521 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15522 row->end.overlay_string_index,
15523 row->continuation_lines_width);
15524 fprintf (stderr, "%9d %5d\n",
15525 CHARPOS (row->start.string_pos),
15526 CHARPOS (row->end.string_pos));
15527 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15528 row->end.dpvec_index);
15531 if (glyphs > 1)
15533 int area;
15535 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15537 struct glyph *glyph = row->glyphs[area];
15538 struct glyph *glyph_end = glyph + row->used[area];
15540 /* Glyph for a line end in text. */
15541 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15542 ++glyph_end;
15544 if (glyph < glyph_end)
15545 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15547 for (; glyph < glyph_end; ++glyph)
15548 dump_glyph (row, glyph, area);
15551 else if (glyphs == 1)
15553 int area;
15555 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15557 char *s = (char *) alloca (row->used[area] + 1);
15558 int i;
15560 for (i = 0; i < row->used[area]; ++i)
15562 struct glyph *glyph = row->glyphs[area] + i;
15563 if (glyph->type == CHAR_GLYPH
15564 && glyph->u.ch < 0x80
15565 && glyph->u.ch >= ' ')
15566 s[i] = glyph->u.ch;
15567 else
15568 s[i] = '.';
15571 s[i] = '\0';
15572 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15578 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15579 Sdump_glyph_matrix, 0, 1, "p",
15580 doc: /* Dump the current matrix of the selected window to stderr.
15581 Shows contents of glyph row structures. With non-nil
15582 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15583 glyphs in short form, otherwise show glyphs in long form. */)
15584 (glyphs)
15585 Lisp_Object glyphs;
15587 struct window *w = XWINDOW (selected_window);
15588 struct buffer *buffer = XBUFFER (w->buffer);
15590 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15591 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15592 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15593 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15594 fprintf (stderr, "=============================================\n");
15595 dump_glyph_matrix (w->current_matrix,
15596 NILP (glyphs) ? 0 : XINT (glyphs));
15597 return Qnil;
15601 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15602 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15605 struct frame *f = XFRAME (selected_frame);
15606 dump_glyph_matrix (f->current_matrix, 1);
15607 return Qnil;
15611 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15612 doc: /* Dump glyph row ROW to stderr.
15613 GLYPH 0 means don't dump glyphs.
15614 GLYPH 1 means dump glyphs in short form.
15615 GLYPH > 1 or omitted means dump glyphs in long form. */)
15616 (row, glyphs)
15617 Lisp_Object row, glyphs;
15619 struct glyph_matrix *matrix;
15620 int vpos;
15622 CHECK_NUMBER (row);
15623 matrix = XWINDOW (selected_window)->current_matrix;
15624 vpos = XINT (row);
15625 if (vpos >= 0 && vpos < matrix->nrows)
15626 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15627 vpos,
15628 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15629 return Qnil;
15633 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15634 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15635 GLYPH 0 means don't dump glyphs.
15636 GLYPH 1 means dump glyphs in short form.
15637 GLYPH > 1 or omitted means dump glyphs in long form. */)
15638 (row, glyphs)
15639 Lisp_Object row, glyphs;
15641 struct frame *sf = SELECTED_FRAME ();
15642 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15643 int vpos;
15645 CHECK_NUMBER (row);
15646 vpos = XINT (row);
15647 if (vpos >= 0 && vpos < m->nrows)
15648 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15649 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15650 return Qnil;
15654 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15655 doc: /* Toggle tracing of redisplay.
15656 With ARG, turn tracing on if and only if ARG is positive. */)
15657 (arg)
15658 Lisp_Object arg;
15660 if (NILP (arg))
15661 trace_redisplay_p = !trace_redisplay_p;
15662 else
15664 arg = Fprefix_numeric_value (arg);
15665 trace_redisplay_p = XINT (arg) > 0;
15668 return Qnil;
15672 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15673 doc: /* Like `format', but print result to stderr.
15674 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15675 (nargs, args)
15676 int nargs;
15677 Lisp_Object *args;
15679 Lisp_Object s = Fformat (nargs, args);
15680 fprintf (stderr, "%s", SDATA (s));
15681 return Qnil;
15684 #endif /* GLYPH_DEBUG */
15688 /***********************************************************************
15689 Building Desired Matrix Rows
15690 ***********************************************************************/
15692 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15693 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15695 static struct glyph_row *
15696 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15697 struct window *w;
15698 Lisp_Object overlay_arrow_string;
15700 struct frame *f = XFRAME (WINDOW_FRAME (w));
15701 struct buffer *buffer = XBUFFER (w->buffer);
15702 struct buffer *old = current_buffer;
15703 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15704 int arrow_len = SCHARS (overlay_arrow_string);
15705 const unsigned char *arrow_end = arrow_string + arrow_len;
15706 const unsigned char *p;
15707 struct it it;
15708 int multibyte_p;
15709 int n_glyphs_before;
15711 set_buffer_temp (buffer);
15712 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15713 it.glyph_row->used[TEXT_AREA] = 0;
15714 SET_TEXT_POS (it.position, 0, 0);
15716 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15717 p = arrow_string;
15718 while (p < arrow_end)
15720 Lisp_Object face, ilisp;
15722 /* Get the next character. */
15723 if (multibyte_p)
15724 it.c = string_char_and_length (p, arrow_len, &it.len);
15725 else
15726 it.c = *p, it.len = 1;
15727 p += it.len;
15729 /* Get its face. */
15730 ilisp = make_number (p - arrow_string);
15731 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15732 it.face_id = compute_char_face (f, it.c, face);
15734 /* Compute its width, get its glyphs. */
15735 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15736 SET_TEXT_POS (it.position, -1, -1);
15737 PRODUCE_GLYPHS (&it);
15739 /* If this character doesn't fit any more in the line, we have
15740 to remove some glyphs. */
15741 if (it.current_x > it.last_visible_x)
15743 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15744 break;
15748 set_buffer_temp (old);
15749 return it.glyph_row;
15753 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15754 glyphs are only inserted for terminal frames since we can't really
15755 win with truncation glyphs when partially visible glyphs are
15756 involved. Which glyphs to insert is determined by
15757 produce_special_glyphs. */
15759 static void
15760 insert_left_trunc_glyphs (it)
15761 struct it *it;
15763 struct it truncate_it;
15764 struct glyph *from, *end, *to, *toend;
15766 xassert (!FRAME_WINDOW_P (it->f));
15768 /* Get the truncation glyphs. */
15769 truncate_it = *it;
15770 truncate_it.current_x = 0;
15771 truncate_it.face_id = DEFAULT_FACE_ID;
15772 truncate_it.glyph_row = &scratch_glyph_row;
15773 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15774 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15775 truncate_it.object = make_number (0);
15776 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15778 /* Overwrite glyphs from IT with truncation glyphs. */
15779 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15780 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15781 to = it->glyph_row->glyphs[TEXT_AREA];
15782 toend = to + it->glyph_row->used[TEXT_AREA];
15784 while (from < end)
15785 *to++ = *from++;
15787 /* There may be padding glyphs left over. Overwrite them too. */
15788 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15790 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15791 while (from < end)
15792 *to++ = *from++;
15795 if (to > toend)
15796 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15800 /* Compute the pixel height and width of IT->glyph_row.
15802 Most of the time, ascent and height of a display line will be equal
15803 to the max_ascent and max_height values of the display iterator
15804 structure. This is not the case if
15806 1. We hit ZV without displaying anything. In this case, max_ascent
15807 and max_height will be zero.
15809 2. We have some glyphs that don't contribute to the line height.
15810 (The glyph row flag contributes_to_line_height_p is for future
15811 pixmap extensions).
15813 The first case is easily covered by using default values because in
15814 these cases, the line height does not really matter, except that it
15815 must not be zero. */
15817 static void
15818 compute_line_metrics (it)
15819 struct it *it;
15821 struct glyph_row *row = it->glyph_row;
15822 int area, i;
15824 if (FRAME_WINDOW_P (it->f))
15826 int i, min_y, max_y;
15828 /* The line may consist of one space only, that was added to
15829 place the cursor on it. If so, the row's height hasn't been
15830 computed yet. */
15831 if (row->height == 0)
15833 if (it->max_ascent + it->max_descent == 0)
15834 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15835 row->ascent = it->max_ascent;
15836 row->height = it->max_ascent + it->max_descent;
15837 row->phys_ascent = it->max_phys_ascent;
15838 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15839 row->extra_line_spacing = it->max_extra_line_spacing;
15842 /* Compute the width of this line. */
15843 row->pixel_width = row->x;
15844 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15845 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15847 xassert (row->pixel_width >= 0);
15848 xassert (row->ascent >= 0 && row->height > 0);
15850 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15851 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15853 /* If first line's physical ascent is larger than its logical
15854 ascent, use the physical ascent, and make the row taller.
15855 This makes accented characters fully visible. */
15856 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15857 && row->phys_ascent > row->ascent)
15859 row->height += row->phys_ascent - row->ascent;
15860 row->ascent = row->phys_ascent;
15863 /* Compute how much of the line is visible. */
15864 row->visible_height = row->height;
15866 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15867 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15869 if (row->y < min_y)
15870 row->visible_height -= min_y - row->y;
15871 if (row->y + row->height > max_y)
15872 row->visible_height -= row->y + row->height - max_y;
15874 else
15876 row->pixel_width = row->used[TEXT_AREA];
15877 if (row->continued_p)
15878 row->pixel_width -= it->continuation_pixel_width;
15879 else if (row->truncated_on_right_p)
15880 row->pixel_width -= it->truncation_pixel_width;
15881 row->ascent = row->phys_ascent = 0;
15882 row->height = row->phys_height = row->visible_height = 1;
15883 row->extra_line_spacing = 0;
15886 /* Compute a hash code for this row. */
15887 row->hash = 0;
15888 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15889 for (i = 0; i < row->used[area]; ++i)
15890 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15891 + row->glyphs[area][i].u.val
15892 + row->glyphs[area][i].face_id
15893 + row->glyphs[area][i].padding_p
15894 + (row->glyphs[area][i].type << 2));
15896 it->max_ascent = it->max_descent = 0;
15897 it->max_phys_ascent = it->max_phys_descent = 0;
15901 /* Append one space to the glyph row of iterator IT if doing a
15902 window-based redisplay. The space has the same face as
15903 IT->face_id. Value is non-zero if a space was added.
15905 This function is called to make sure that there is always one glyph
15906 at the end of a glyph row that the cursor can be set on under
15907 window-systems. (If there weren't such a glyph we would not know
15908 how wide and tall a box cursor should be displayed).
15910 At the same time this space let's a nicely handle clearing to the
15911 end of the line if the row ends in italic text. */
15913 static int
15914 append_space_for_newline (it, default_face_p)
15915 struct it *it;
15916 int default_face_p;
15918 if (FRAME_WINDOW_P (it->f))
15920 int n = it->glyph_row->used[TEXT_AREA];
15922 if (it->glyph_row->glyphs[TEXT_AREA] + n
15923 < it->glyph_row->glyphs[1 + TEXT_AREA])
15925 /* Save some values that must not be changed.
15926 Must save IT->c and IT->len because otherwise
15927 ITERATOR_AT_END_P wouldn't work anymore after
15928 append_space_for_newline has been called. */
15929 enum display_element_type saved_what = it->what;
15930 int saved_c = it->c, saved_len = it->len;
15931 int saved_x = it->current_x;
15932 int saved_face_id = it->face_id;
15933 struct text_pos saved_pos;
15934 Lisp_Object saved_object;
15935 struct face *face;
15937 saved_object = it->object;
15938 saved_pos = it->position;
15940 it->what = IT_CHARACTER;
15941 bzero (&it->position, sizeof it->position);
15942 it->object = make_number (0);
15943 it->c = ' ';
15944 it->len = 1;
15946 if (default_face_p)
15947 it->face_id = DEFAULT_FACE_ID;
15948 else if (it->face_before_selective_p)
15949 it->face_id = it->saved_face_id;
15950 face = FACE_FROM_ID (it->f, it->face_id);
15951 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15953 PRODUCE_GLYPHS (it);
15955 it->override_ascent = -1;
15956 it->constrain_row_ascent_descent_p = 0;
15957 it->current_x = saved_x;
15958 it->object = saved_object;
15959 it->position = saved_pos;
15960 it->what = saved_what;
15961 it->face_id = saved_face_id;
15962 it->len = saved_len;
15963 it->c = saved_c;
15964 return 1;
15968 return 0;
15972 /* Extend the face of the last glyph in the text area of IT->glyph_row
15973 to the end of the display line. Called from display_line.
15974 If the glyph row is empty, add a space glyph to it so that we
15975 know the face to draw. Set the glyph row flag fill_line_p. */
15977 static void
15978 extend_face_to_end_of_line (it)
15979 struct it *it;
15981 struct face *face;
15982 struct frame *f = it->f;
15984 /* If line is already filled, do nothing. */
15985 if (it->current_x >= it->last_visible_x)
15986 return;
15988 /* Face extension extends the background and box of IT->face_id
15989 to the end of the line. If the background equals the background
15990 of the frame, we don't have to do anything. */
15991 if (it->face_before_selective_p)
15992 face = FACE_FROM_ID (it->f, it->saved_face_id);
15993 else
15994 face = FACE_FROM_ID (f, it->face_id);
15996 if (FRAME_WINDOW_P (f)
15997 && it->glyph_row->displays_text_p
15998 && face->box == FACE_NO_BOX
15999 && face->background == FRAME_BACKGROUND_PIXEL (f)
16000 && !face->stipple)
16001 return;
16003 /* Set the glyph row flag indicating that the face of the last glyph
16004 in the text area has to be drawn to the end of the text area. */
16005 it->glyph_row->fill_line_p = 1;
16007 /* If current character of IT is not ASCII, make sure we have the
16008 ASCII face. This will be automatically undone the next time
16009 get_next_display_element returns a multibyte character. Note
16010 that the character will always be single byte in unibyte text. */
16011 if (!ASCII_CHAR_P (it->c))
16013 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16016 if (FRAME_WINDOW_P (f))
16018 /* If the row is empty, add a space with the current face of IT,
16019 so that we know which face to draw. */
16020 if (it->glyph_row->used[TEXT_AREA] == 0)
16022 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16023 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16024 it->glyph_row->used[TEXT_AREA] = 1;
16027 else
16029 /* Save some values that must not be changed. */
16030 int saved_x = it->current_x;
16031 struct text_pos saved_pos;
16032 Lisp_Object saved_object;
16033 enum display_element_type saved_what = it->what;
16034 int saved_face_id = it->face_id;
16036 saved_object = it->object;
16037 saved_pos = it->position;
16039 it->what = IT_CHARACTER;
16040 bzero (&it->position, sizeof it->position);
16041 it->object = make_number (0);
16042 it->c = ' ';
16043 it->len = 1;
16044 it->face_id = face->id;
16046 PRODUCE_GLYPHS (it);
16048 while (it->current_x <= it->last_visible_x)
16049 PRODUCE_GLYPHS (it);
16051 /* Don't count these blanks really. It would let us insert a left
16052 truncation glyph below and make us set the cursor on them, maybe. */
16053 it->current_x = saved_x;
16054 it->object = saved_object;
16055 it->position = saved_pos;
16056 it->what = saved_what;
16057 it->face_id = saved_face_id;
16062 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16063 trailing whitespace. */
16065 static int
16066 trailing_whitespace_p (charpos)
16067 int charpos;
16069 int bytepos = CHAR_TO_BYTE (charpos);
16070 int c = 0;
16072 while (bytepos < ZV_BYTE
16073 && (c = FETCH_CHAR (bytepos),
16074 c == ' ' || c == '\t'))
16075 ++bytepos;
16077 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16079 if (bytepos != PT_BYTE)
16080 return 1;
16082 return 0;
16086 /* Highlight trailing whitespace, if any, in ROW. */
16088 void
16089 highlight_trailing_whitespace (f, row)
16090 struct frame *f;
16091 struct glyph_row *row;
16093 int used = row->used[TEXT_AREA];
16095 if (used)
16097 struct glyph *start = row->glyphs[TEXT_AREA];
16098 struct glyph *glyph = start + used - 1;
16100 /* Skip over glyphs inserted to display the cursor at the
16101 end of a line, for extending the face of the last glyph
16102 to the end of the line on terminals, and for truncation
16103 and continuation glyphs. */
16104 while (glyph >= start
16105 && glyph->type == CHAR_GLYPH
16106 && INTEGERP (glyph->object))
16107 --glyph;
16109 /* If last glyph is a space or stretch, and it's trailing
16110 whitespace, set the face of all trailing whitespace glyphs in
16111 IT->glyph_row to `trailing-whitespace'. */
16112 if (glyph >= start
16113 && BUFFERP (glyph->object)
16114 && (glyph->type == STRETCH_GLYPH
16115 || (glyph->type == CHAR_GLYPH
16116 && glyph->u.ch == ' '))
16117 && trailing_whitespace_p (glyph->charpos))
16119 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16120 if (face_id < 0)
16121 return;
16123 while (glyph >= start
16124 && BUFFERP (glyph->object)
16125 && (glyph->type == STRETCH_GLYPH
16126 || (glyph->type == CHAR_GLYPH
16127 && glyph->u.ch == ' ')))
16128 (glyph--)->face_id = face_id;
16134 /* Value is non-zero if glyph row ROW in window W should be
16135 used to hold the cursor. */
16137 static int
16138 cursor_row_p (w, row)
16139 struct window *w;
16140 struct glyph_row *row;
16142 int cursor_row_p = 1;
16144 if (PT == MATRIX_ROW_END_CHARPOS (row))
16146 /* Suppose the row ends on a string.
16147 Unless the row is continued, that means it ends on a newline
16148 in the string. If it's anything other than a display string
16149 (e.g. a before-string from an overlay), we don't want the
16150 cursor there. (This heuristic seems to give the optimal
16151 behavior for the various types of multi-line strings.) */
16152 if (CHARPOS (row->end.string_pos) >= 0)
16154 if (row->continued_p)
16155 cursor_row_p = 1;
16156 else
16158 /* Check for `display' property. */
16159 struct glyph *beg = row->glyphs[TEXT_AREA];
16160 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16161 struct glyph *glyph;
16163 cursor_row_p = 0;
16164 for (glyph = end; glyph >= beg; --glyph)
16165 if (STRINGP (glyph->object))
16167 Lisp_Object prop
16168 = Fget_char_property (make_number (PT),
16169 Qdisplay, Qnil);
16170 cursor_row_p =
16171 (!NILP (prop)
16172 && display_prop_string_p (prop, glyph->object));
16173 break;
16177 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16179 /* If the row ends in middle of a real character,
16180 and the line is continued, we want the cursor here.
16181 That's because MATRIX_ROW_END_CHARPOS would equal
16182 PT if PT is before the character. */
16183 if (!row->ends_in_ellipsis_p)
16184 cursor_row_p = row->continued_p;
16185 else
16186 /* If the row ends in an ellipsis, then
16187 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16188 We want that position to be displayed after the ellipsis. */
16189 cursor_row_p = 0;
16191 /* If the row ends at ZV, display the cursor at the end of that
16192 row instead of at the start of the row below. */
16193 else if (row->ends_at_zv_p)
16194 cursor_row_p = 1;
16195 else
16196 cursor_row_p = 0;
16199 return cursor_row_p;
16203 /* Construct the glyph row IT->glyph_row in the desired matrix of
16204 IT->w from text at the current position of IT. See dispextern.h
16205 for an overview of struct it. Value is non-zero if
16206 IT->glyph_row displays text, as opposed to a line displaying ZV
16207 only. */
16209 static int
16210 display_line (it)
16211 struct it *it;
16213 struct glyph_row *row = it->glyph_row;
16214 Lisp_Object overlay_arrow_string;
16216 /* We always start displaying at hpos zero even if hscrolled. */
16217 xassert (it->hpos == 0 && it->current_x == 0);
16219 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16220 >= it->w->desired_matrix->nrows)
16222 it->w->nrows_scale_factor++;
16223 fonts_changed_p = 1;
16224 return 0;
16227 /* Is IT->w showing the region? */
16228 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16230 /* Clear the result glyph row and enable it. */
16231 prepare_desired_row (row);
16233 row->y = it->current_y;
16234 row->start = it->start;
16235 row->continuation_lines_width = it->continuation_lines_width;
16236 row->displays_text_p = 1;
16237 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16238 it->starts_in_middle_of_char_p = 0;
16240 /* Arrange the overlays nicely for our purposes. Usually, we call
16241 display_line on only one line at a time, in which case this
16242 can't really hurt too much, or we call it on lines which appear
16243 one after another in the buffer, in which case all calls to
16244 recenter_overlay_lists but the first will be pretty cheap. */
16245 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16247 /* Move over display elements that are not visible because we are
16248 hscrolled. This may stop at an x-position < IT->first_visible_x
16249 if the first glyph is partially visible or if we hit a line end. */
16250 if (it->current_x < it->first_visible_x)
16252 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16253 MOVE_TO_POS | MOVE_TO_X);
16256 /* Get the initial row height. This is either the height of the
16257 text hscrolled, if there is any, or zero. */
16258 row->ascent = it->max_ascent;
16259 row->height = it->max_ascent + it->max_descent;
16260 row->phys_ascent = it->max_phys_ascent;
16261 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16262 row->extra_line_spacing = it->max_extra_line_spacing;
16264 /* Loop generating characters. The loop is left with IT on the next
16265 character to display. */
16266 while (1)
16268 int n_glyphs_before, hpos_before, x_before;
16269 int x, i, nglyphs;
16270 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16272 /* Retrieve the next thing to display. Value is zero if end of
16273 buffer reached. */
16274 if (!get_next_display_element (it))
16276 /* Maybe add a space at the end of this line that is used to
16277 display the cursor there under X. Set the charpos of the
16278 first glyph of blank lines not corresponding to any text
16279 to -1. */
16280 #ifdef HAVE_WINDOW_SYSTEM
16281 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16282 row->exact_window_width_line_p = 1;
16283 else
16284 #endif /* HAVE_WINDOW_SYSTEM */
16285 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16286 || row->used[TEXT_AREA] == 0)
16288 row->glyphs[TEXT_AREA]->charpos = -1;
16289 row->displays_text_p = 0;
16291 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16292 && (!MINI_WINDOW_P (it->w)
16293 || (minibuf_level && EQ (it->window, minibuf_window))))
16294 row->indicate_empty_line_p = 1;
16297 it->continuation_lines_width = 0;
16298 row->ends_at_zv_p = 1;
16299 break;
16302 /* Now, get the metrics of what we want to display. This also
16303 generates glyphs in `row' (which is IT->glyph_row). */
16304 n_glyphs_before = row->used[TEXT_AREA];
16305 x = it->current_x;
16307 /* Remember the line height so far in case the next element doesn't
16308 fit on the line. */
16309 if (!it->truncate_lines_p)
16311 ascent = it->max_ascent;
16312 descent = it->max_descent;
16313 phys_ascent = it->max_phys_ascent;
16314 phys_descent = it->max_phys_descent;
16317 PRODUCE_GLYPHS (it);
16319 /* If this display element was in marginal areas, continue with
16320 the next one. */
16321 if (it->area != TEXT_AREA)
16323 row->ascent = max (row->ascent, it->max_ascent);
16324 row->height = max (row->height, it->max_ascent + it->max_descent);
16325 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16326 row->phys_height = max (row->phys_height,
16327 it->max_phys_ascent + it->max_phys_descent);
16328 row->extra_line_spacing = max (row->extra_line_spacing,
16329 it->max_extra_line_spacing);
16330 set_iterator_to_next (it, 1);
16331 continue;
16334 /* Does the display element fit on the line? If we truncate
16335 lines, we should draw past the right edge of the window. If
16336 we don't truncate, we want to stop so that we can display the
16337 continuation glyph before the right margin. If lines are
16338 continued, there are two possible strategies for characters
16339 resulting in more than 1 glyph (e.g. tabs): Display as many
16340 glyphs as possible in this line and leave the rest for the
16341 continuation line, or display the whole element in the next
16342 line. Original redisplay did the former, so we do it also. */
16343 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16344 hpos_before = it->hpos;
16345 x_before = x;
16347 if (/* Not a newline. */
16348 nglyphs > 0
16349 /* Glyphs produced fit entirely in the line. */
16350 && it->current_x < it->last_visible_x)
16352 it->hpos += nglyphs;
16353 row->ascent = max (row->ascent, it->max_ascent);
16354 row->height = max (row->height, it->max_ascent + it->max_descent);
16355 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16356 row->phys_height = max (row->phys_height,
16357 it->max_phys_ascent + it->max_phys_descent);
16358 row->extra_line_spacing = max (row->extra_line_spacing,
16359 it->max_extra_line_spacing);
16360 if (it->current_x - it->pixel_width < it->first_visible_x)
16361 row->x = x - it->first_visible_x;
16363 else
16365 int new_x;
16366 struct glyph *glyph;
16368 for (i = 0; i < nglyphs; ++i, x = new_x)
16370 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16371 new_x = x + glyph->pixel_width;
16373 if (/* Lines are continued. */
16374 !it->truncate_lines_p
16375 && (/* Glyph doesn't fit on the line. */
16376 new_x > it->last_visible_x
16377 /* Or it fits exactly on a window system frame. */
16378 || (new_x == it->last_visible_x
16379 && FRAME_WINDOW_P (it->f))))
16381 /* End of a continued line. */
16383 if (it->hpos == 0
16384 || (new_x == it->last_visible_x
16385 && FRAME_WINDOW_P (it->f)))
16387 /* Current glyph is the only one on the line or
16388 fits exactly on the line. We must continue
16389 the line because we can't draw the cursor
16390 after the glyph. */
16391 row->continued_p = 1;
16392 it->current_x = new_x;
16393 it->continuation_lines_width += new_x;
16394 ++it->hpos;
16395 if (i == nglyphs - 1)
16397 set_iterator_to_next (it, 1);
16398 #ifdef HAVE_WINDOW_SYSTEM
16399 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16401 if (!get_next_display_element (it))
16403 row->exact_window_width_line_p = 1;
16404 it->continuation_lines_width = 0;
16405 row->continued_p = 0;
16406 row->ends_at_zv_p = 1;
16408 else if (ITERATOR_AT_END_OF_LINE_P (it))
16410 row->continued_p = 0;
16411 row->exact_window_width_line_p = 1;
16414 #endif /* HAVE_WINDOW_SYSTEM */
16417 else if (CHAR_GLYPH_PADDING_P (*glyph)
16418 && !FRAME_WINDOW_P (it->f))
16420 /* A padding glyph that doesn't fit on this line.
16421 This means the whole character doesn't fit
16422 on the line. */
16423 row->used[TEXT_AREA] = n_glyphs_before;
16425 /* Fill the rest of the row with continuation
16426 glyphs like in 20.x. */
16427 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16428 < row->glyphs[1 + TEXT_AREA])
16429 produce_special_glyphs (it, IT_CONTINUATION);
16431 row->continued_p = 1;
16432 it->current_x = x_before;
16433 it->continuation_lines_width += x_before;
16435 /* Restore the height to what it was before the
16436 element not fitting on the line. */
16437 it->max_ascent = ascent;
16438 it->max_descent = descent;
16439 it->max_phys_ascent = phys_ascent;
16440 it->max_phys_descent = phys_descent;
16442 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16444 /* A TAB that extends past the right edge of the
16445 window. This produces a single glyph on
16446 window system frames. We leave the glyph in
16447 this row and let it fill the row, but don't
16448 consume the TAB. */
16449 it->continuation_lines_width += it->last_visible_x;
16450 row->ends_in_middle_of_char_p = 1;
16451 row->continued_p = 1;
16452 glyph->pixel_width = it->last_visible_x - x;
16453 it->starts_in_middle_of_char_p = 1;
16455 else
16457 /* Something other than a TAB that draws past
16458 the right edge of the window. Restore
16459 positions to values before the element. */
16460 row->used[TEXT_AREA] = n_glyphs_before + i;
16462 /* Display continuation glyphs. */
16463 if (!FRAME_WINDOW_P (it->f))
16464 produce_special_glyphs (it, IT_CONTINUATION);
16465 row->continued_p = 1;
16467 it->current_x = x_before;
16468 it->continuation_lines_width += x;
16469 extend_face_to_end_of_line (it);
16471 if (nglyphs > 1 && i > 0)
16473 row->ends_in_middle_of_char_p = 1;
16474 it->starts_in_middle_of_char_p = 1;
16477 /* Restore the height to what it was before the
16478 element not fitting on the line. */
16479 it->max_ascent = ascent;
16480 it->max_descent = descent;
16481 it->max_phys_ascent = phys_ascent;
16482 it->max_phys_descent = phys_descent;
16485 break;
16487 else if (new_x > it->first_visible_x)
16489 /* Increment number of glyphs actually displayed. */
16490 ++it->hpos;
16492 if (x < it->first_visible_x)
16493 /* Glyph is partially visible, i.e. row starts at
16494 negative X position. */
16495 row->x = x - it->first_visible_x;
16497 else
16499 /* Glyph is completely off the left margin of the
16500 window. This should not happen because of the
16501 move_it_in_display_line at the start of this
16502 function, unless the text display area of the
16503 window is empty. */
16504 xassert (it->first_visible_x <= it->last_visible_x);
16508 row->ascent = max (row->ascent, it->max_ascent);
16509 row->height = max (row->height, it->max_ascent + it->max_descent);
16510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16511 row->phys_height = max (row->phys_height,
16512 it->max_phys_ascent + it->max_phys_descent);
16513 row->extra_line_spacing = max (row->extra_line_spacing,
16514 it->max_extra_line_spacing);
16516 /* End of this display line if row is continued. */
16517 if (row->continued_p || row->ends_at_zv_p)
16518 break;
16521 at_end_of_line:
16522 /* Is this a line end? If yes, we're also done, after making
16523 sure that a non-default face is extended up to the right
16524 margin of the window. */
16525 if (ITERATOR_AT_END_OF_LINE_P (it))
16527 int used_before = row->used[TEXT_AREA];
16529 row->ends_in_newline_from_string_p = STRINGP (it->object);
16531 #ifdef HAVE_WINDOW_SYSTEM
16532 /* Add a space at the end of the line that is used to
16533 display the cursor there. */
16534 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16535 append_space_for_newline (it, 0);
16536 #endif /* HAVE_WINDOW_SYSTEM */
16538 /* Extend the face to the end of the line. */
16539 extend_face_to_end_of_line (it);
16541 /* Make sure we have the position. */
16542 if (used_before == 0)
16543 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16545 /* Consume the line end. This skips over invisible lines. */
16546 set_iterator_to_next (it, 1);
16547 it->continuation_lines_width = 0;
16548 break;
16551 /* Proceed with next display element. Note that this skips
16552 over lines invisible because of selective display. */
16553 set_iterator_to_next (it, 1);
16555 /* If we truncate lines, we are done when the last displayed
16556 glyphs reach past the right margin of the window. */
16557 if (it->truncate_lines_p
16558 && (FRAME_WINDOW_P (it->f)
16559 ? (it->current_x >= it->last_visible_x)
16560 : (it->current_x > it->last_visible_x)))
16562 /* Maybe add truncation glyphs. */
16563 if (!FRAME_WINDOW_P (it->f))
16565 int i, n;
16567 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16568 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16569 break;
16571 for (n = row->used[TEXT_AREA]; i < n; ++i)
16573 row->used[TEXT_AREA] = i;
16574 produce_special_glyphs (it, IT_TRUNCATION);
16577 #ifdef HAVE_WINDOW_SYSTEM
16578 else
16580 /* Don't truncate if we can overflow newline into fringe. */
16581 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16583 if (!get_next_display_element (it))
16585 it->continuation_lines_width = 0;
16586 row->ends_at_zv_p = 1;
16587 row->exact_window_width_line_p = 1;
16588 break;
16590 if (ITERATOR_AT_END_OF_LINE_P (it))
16592 row->exact_window_width_line_p = 1;
16593 goto at_end_of_line;
16597 #endif /* HAVE_WINDOW_SYSTEM */
16599 row->truncated_on_right_p = 1;
16600 it->continuation_lines_width = 0;
16601 reseat_at_next_visible_line_start (it, 0);
16602 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16603 it->hpos = hpos_before;
16604 it->current_x = x_before;
16605 break;
16609 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16610 at the left window margin. */
16611 if (it->first_visible_x
16612 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16614 if (!FRAME_WINDOW_P (it->f))
16615 insert_left_trunc_glyphs (it);
16616 row->truncated_on_left_p = 1;
16619 /* If the start of this line is the overlay arrow-position, then
16620 mark this glyph row as the one containing the overlay arrow.
16621 This is clearly a mess with variable size fonts. It would be
16622 better to let it be displayed like cursors under X. */
16623 if ((row->displays_text_p || !overlay_arrow_seen)
16624 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16625 !NILP (overlay_arrow_string)))
16627 /* Overlay arrow in window redisplay is a fringe bitmap. */
16628 if (STRINGP (overlay_arrow_string))
16630 struct glyph_row *arrow_row
16631 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16632 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16633 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16634 struct glyph *p = row->glyphs[TEXT_AREA];
16635 struct glyph *p2, *end;
16637 /* Copy the arrow glyphs. */
16638 while (glyph < arrow_end)
16639 *p++ = *glyph++;
16641 /* Throw away padding glyphs. */
16642 p2 = p;
16643 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16644 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16645 ++p2;
16646 if (p2 > p)
16648 while (p2 < end)
16649 *p++ = *p2++;
16650 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16653 else
16655 xassert (INTEGERP (overlay_arrow_string));
16656 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16658 overlay_arrow_seen = 1;
16661 /* Compute pixel dimensions of this line. */
16662 compute_line_metrics (it);
16664 /* Remember the position at which this line ends. */
16665 row->end = it->current;
16667 /* Record whether this row ends inside an ellipsis. */
16668 row->ends_in_ellipsis_p
16669 = (it->method == GET_FROM_DISPLAY_VECTOR
16670 && it->ellipsis_p);
16672 /* Save fringe bitmaps in this row. */
16673 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16674 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16675 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16676 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16678 it->left_user_fringe_bitmap = 0;
16679 it->left_user_fringe_face_id = 0;
16680 it->right_user_fringe_bitmap = 0;
16681 it->right_user_fringe_face_id = 0;
16683 /* Maybe set the cursor. */
16684 if (it->w->cursor.vpos < 0
16685 && PT >= MATRIX_ROW_START_CHARPOS (row)
16686 && PT <= MATRIX_ROW_END_CHARPOS (row)
16687 && cursor_row_p (it->w, row))
16688 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16690 /* Highlight trailing whitespace. */
16691 if (!NILP (Vshow_trailing_whitespace))
16692 highlight_trailing_whitespace (it->f, it->glyph_row);
16694 /* Prepare for the next line. This line starts horizontally at (X
16695 HPOS) = (0 0). Vertical positions are incremented. As a
16696 convenience for the caller, IT->glyph_row is set to the next
16697 row to be used. */
16698 it->current_x = it->hpos = 0;
16699 it->current_y += row->height;
16700 ++it->vpos;
16701 ++it->glyph_row;
16702 it->start = it->current;
16703 return row->displays_text_p;
16708 /***********************************************************************
16709 Menu Bar
16710 ***********************************************************************/
16712 /* Redisplay the menu bar in the frame for window W.
16714 The menu bar of X frames that don't have X toolkit support is
16715 displayed in a special window W->frame->menu_bar_window.
16717 The menu bar of terminal frames is treated specially as far as
16718 glyph matrices are concerned. Menu bar lines are not part of
16719 windows, so the update is done directly on the frame matrix rows
16720 for the menu bar. */
16722 static void
16723 display_menu_bar (w)
16724 struct window *w;
16726 struct frame *f = XFRAME (WINDOW_FRAME (w));
16727 struct it it;
16728 Lisp_Object items;
16729 int i;
16731 /* Don't do all this for graphical frames. */
16732 #ifdef HAVE_NTGUI
16733 if (FRAME_W32_P (f))
16734 return;
16735 #endif
16736 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16737 if (FRAME_X_P (f))
16738 return;
16739 #endif
16740 #ifdef MAC_OS
16741 if (FRAME_MAC_P (f))
16742 return;
16743 #endif
16745 #ifdef USE_X_TOOLKIT
16746 xassert (!FRAME_WINDOW_P (f));
16747 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16748 it.first_visible_x = 0;
16749 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16750 #else /* not USE_X_TOOLKIT */
16751 if (FRAME_WINDOW_P (f))
16753 /* Menu bar lines are displayed in the desired matrix of the
16754 dummy window menu_bar_window. */
16755 struct window *menu_w;
16756 xassert (WINDOWP (f->menu_bar_window));
16757 menu_w = XWINDOW (f->menu_bar_window);
16758 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16759 MENU_FACE_ID);
16760 it.first_visible_x = 0;
16761 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16763 else
16765 /* This is a TTY frame, i.e. character hpos/vpos are used as
16766 pixel x/y. */
16767 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16768 MENU_FACE_ID);
16769 it.first_visible_x = 0;
16770 it.last_visible_x = FRAME_COLS (f);
16772 #endif /* not USE_X_TOOLKIT */
16774 if (! mode_line_inverse_video)
16775 /* Force the menu-bar to be displayed in the default face. */
16776 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16778 /* Clear all rows of the menu bar. */
16779 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16781 struct glyph_row *row = it.glyph_row + i;
16782 clear_glyph_row (row);
16783 row->enabled_p = 1;
16784 row->full_width_p = 1;
16787 /* Display all items of the menu bar. */
16788 items = FRAME_MENU_BAR_ITEMS (it.f);
16789 for (i = 0; i < XVECTOR (items)->size; i += 4)
16791 Lisp_Object string;
16793 /* Stop at nil string. */
16794 string = AREF (items, i + 1);
16795 if (NILP (string))
16796 break;
16798 /* Remember where item was displayed. */
16799 ASET (items, i + 3, make_number (it.hpos));
16801 /* Display the item, pad with one space. */
16802 if (it.current_x < it.last_visible_x)
16803 display_string (NULL, string, Qnil, 0, 0, &it,
16804 SCHARS (string) + 1, 0, 0, -1);
16807 /* Fill out the line with spaces. */
16808 if (it.current_x < it.last_visible_x)
16809 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16811 /* Compute the total height of the lines. */
16812 compute_line_metrics (&it);
16817 /***********************************************************************
16818 Mode Line
16819 ***********************************************************************/
16821 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16822 FORCE is non-zero, redisplay mode lines unconditionally.
16823 Otherwise, redisplay only mode lines that are garbaged. Value is
16824 the number of windows whose mode lines were redisplayed. */
16826 static int
16827 redisplay_mode_lines (window, force)
16828 Lisp_Object window;
16829 int force;
16831 int nwindows = 0;
16833 while (!NILP (window))
16835 struct window *w = XWINDOW (window);
16837 if (WINDOWP (w->hchild))
16838 nwindows += redisplay_mode_lines (w->hchild, force);
16839 else if (WINDOWP (w->vchild))
16840 nwindows += redisplay_mode_lines (w->vchild, force);
16841 else if (force
16842 || FRAME_GARBAGED_P (XFRAME (w->frame))
16843 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16845 struct text_pos lpoint;
16846 struct buffer *old = current_buffer;
16848 /* Set the window's buffer for the mode line display. */
16849 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16850 set_buffer_internal_1 (XBUFFER (w->buffer));
16852 /* Point refers normally to the selected window. For any
16853 other window, set up appropriate value. */
16854 if (!EQ (window, selected_window))
16856 struct text_pos pt;
16858 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16859 if (CHARPOS (pt) < BEGV)
16860 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16861 else if (CHARPOS (pt) > (ZV - 1))
16862 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16863 else
16864 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16867 /* Display mode lines. */
16868 clear_glyph_matrix (w->desired_matrix);
16869 if (display_mode_lines (w))
16871 ++nwindows;
16872 w->must_be_updated_p = 1;
16875 /* Restore old settings. */
16876 set_buffer_internal_1 (old);
16877 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16880 window = w->next;
16883 return nwindows;
16887 /* Display the mode and/or top line of window W. Value is the number
16888 of mode lines displayed. */
16890 static int
16891 display_mode_lines (w)
16892 struct window *w;
16894 Lisp_Object old_selected_window, old_selected_frame;
16895 int n = 0;
16897 old_selected_frame = selected_frame;
16898 selected_frame = w->frame;
16899 old_selected_window = selected_window;
16900 XSETWINDOW (selected_window, w);
16902 /* These will be set while the mode line specs are processed. */
16903 line_number_displayed = 0;
16904 w->column_number_displayed = Qnil;
16906 if (WINDOW_WANTS_MODELINE_P (w))
16908 struct window *sel_w = XWINDOW (old_selected_window);
16910 /* Select mode line face based on the real selected window. */
16911 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16912 current_buffer->mode_line_format);
16913 ++n;
16916 if (WINDOW_WANTS_HEADER_LINE_P (w))
16918 display_mode_line (w, HEADER_LINE_FACE_ID,
16919 current_buffer->header_line_format);
16920 ++n;
16923 selected_frame = old_selected_frame;
16924 selected_window = old_selected_window;
16925 return n;
16929 /* Display mode or top line of window W. FACE_ID specifies which line
16930 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16931 FORMAT is the mode line format to display. Value is the pixel
16932 height of the mode line displayed. */
16934 static int
16935 display_mode_line (w, face_id, format)
16936 struct window *w;
16937 enum face_id face_id;
16938 Lisp_Object format;
16940 struct it it;
16941 struct face *face;
16942 int count = SPECPDL_INDEX ();
16944 init_iterator (&it, w, -1, -1, NULL, face_id);
16945 /* Don't extend on a previously drawn mode-line.
16946 This may happen if called from pos_visible_p. */
16947 it.glyph_row->enabled_p = 0;
16948 prepare_desired_row (it.glyph_row);
16950 it.glyph_row->mode_line_p = 1;
16952 if (! mode_line_inverse_video)
16953 /* Force the mode-line to be displayed in the default face. */
16954 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16956 record_unwind_protect (unwind_format_mode_line,
16957 format_mode_line_unwind_data (NULL, Qnil, 0));
16959 mode_line_target = MODE_LINE_DISPLAY;
16961 /* Temporarily make frame's keyboard the current kboard so that
16962 kboard-local variables in the mode_line_format will get the right
16963 values. */
16964 push_kboard (FRAME_KBOARD (it.f));
16965 record_unwind_save_match_data ();
16966 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16967 pop_kboard ();
16969 unbind_to (count, Qnil);
16971 /* Fill up with spaces. */
16972 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16974 compute_line_metrics (&it);
16975 it.glyph_row->full_width_p = 1;
16976 it.glyph_row->continued_p = 0;
16977 it.glyph_row->truncated_on_left_p = 0;
16978 it.glyph_row->truncated_on_right_p = 0;
16980 /* Make a 3D mode-line have a shadow at its right end. */
16981 face = FACE_FROM_ID (it.f, face_id);
16982 extend_face_to_end_of_line (&it);
16983 if (face->box != FACE_NO_BOX)
16985 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16986 + it.glyph_row->used[TEXT_AREA] - 1);
16987 last->right_box_line_p = 1;
16990 return it.glyph_row->height;
16993 /* Move element ELT in LIST to the front of LIST.
16994 Return the updated list. */
16996 static Lisp_Object
16997 move_elt_to_front (elt, list)
16998 Lisp_Object elt, list;
17000 register Lisp_Object tail, prev;
17001 register Lisp_Object tem;
17003 tail = list;
17004 prev = Qnil;
17005 while (CONSP (tail))
17007 tem = XCAR (tail);
17009 if (EQ (elt, tem))
17011 /* Splice out the link TAIL. */
17012 if (NILP (prev))
17013 list = XCDR (tail);
17014 else
17015 Fsetcdr (prev, XCDR (tail));
17017 /* Now make it the first. */
17018 Fsetcdr (tail, list);
17019 return tail;
17021 else
17022 prev = tail;
17023 tail = XCDR (tail);
17024 QUIT;
17027 /* Not found--return unchanged LIST. */
17028 return list;
17031 /* Contribute ELT to the mode line for window IT->w. How it
17032 translates into text depends on its data type.
17034 IT describes the display environment in which we display, as usual.
17036 DEPTH is the depth in recursion. It is used to prevent
17037 infinite recursion here.
17039 FIELD_WIDTH is the number of characters the display of ELT should
17040 occupy in the mode line, and PRECISION is the maximum number of
17041 characters to display from ELT's representation. See
17042 display_string for details.
17044 Returns the hpos of the end of the text generated by ELT.
17046 PROPS is a property list to add to any string we encounter.
17048 If RISKY is nonzero, remove (disregard) any properties in any string
17049 we encounter, and ignore :eval and :propertize.
17051 The global variable `mode_line_target' determines whether the
17052 output is passed to `store_mode_line_noprop',
17053 `store_mode_line_string', or `display_string'. */
17055 static int
17056 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17057 struct it *it;
17058 int depth;
17059 int field_width, precision;
17060 Lisp_Object elt, props;
17061 int risky;
17063 int n = 0, field, prec;
17064 int literal = 0;
17066 tail_recurse:
17067 if (depth > 100)
17068 elt = build_string ("*too-deep*");
17070 depth++;
17072 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17074 case Lisp_String:
17076 /* A string: output it and check for %-constructs within it. */
17077 unsigned char c;
17078 int offset = 0;
17080 if (SCHARS (elt) > 0
17081 && (!NILP (props) || risky))
17083 Lisp_Object oprops, aelt;
17084 oprops = Ftext_properties_at (make_number (0), elt);
17086 /* If the starting string's properties are not what
17087 we want, translate the string. Also, if the string
17088 is risky, do that anyway. */
17090 if (NILP (Fequal (props, oprops)) || risky)
17092 /* If the starting string has properties,
17093 merge the specified ones onto the existing ones. */
17094 if (! NILP (oprops) && !risky)
17096 Lisp_Object tem;
17098 oprops = Fcopy_sequence (oprops);
17099 tem = props;
17100 while (CONSP (tem))
17102 oprops = Fplist_put (oprops, XCAR (tem),
17103 XCAR (XCDR (tem)));
17104 tem = XCDR (XCDR (tem));
17106 props = oprops;
17109 aelt = Fassoc (elt, mode_line_proptrans_alist);
17110 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17112 /* AELT is what we want. Move it to the front
17113 without consing. */
17114 elt = XCAR (aelt);
17115 mode_line_proptrans_alist
17116 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17118 else
17120 Lisp_Object tem;
17122 /* If AELT has the wrong props, it is useless.
17123 so get rid of it. */
17124 if (! NILP (aelt))
17125 mode_line_proptrans_alist
17126 = Fdelq (aelt, mode_line_proptrans_alist);
17128 elt = Fcopy_sequence (elt);
17129 Fset_text_properties (make_number (0), Flength (elt),
17130 props, elt);
17131 /* Add this item to mode_line_proptrans_alist. */
17132 mode_line_proptrans_alist
17133 = Fcons (Fcons (elt, props),
17134 mode_line_proptrans_alist);
17135 /* Truncate mode_line_proptrans_alist
17136 to at most 50 elements. */
17137 tem = Fnthcdr (make_number (50),
17138 mode_line_proptrans_alist);
17139 if (! NILP (tem))
17140 XSETCDR (tem, Qnil);
17145 offset = 0;
17147 if (literal)
17149 prec = precision - n;
17150 switch (mode_line_target)
17152 case MODE_LINE_NOPROP:
17153 case MODE_LINE_TITLE:
17154 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17155 break;
17156 case MODE_LINE_STRING:
17157 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17158 break;
17159 case MODE_LINE_DISPLAY:
17160 n += display_string (NULL, elt, Qnil, 0, 0, it,
17161 0, prec, 0, STRING_MULTIBYTE (elt));
17162 break;
17165 break;
17168 /* Handle the non-literal case. */
17170 while ((precision <= 0 || n < precision)
17171 && SREF (elt, offset) != 0
17172 && (mode_line_target != MODE_LINE_DISPLAY
17173 || it->current_x < it->last_visible_x))
17175 int last_offset = offset;
17177 /* Advance to end of string or next format specifier. */
17178 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17181 if (offset - 1 != last_offset)
17183 int nchars, nbytes;
17185 /* Output to end of string or up to '%'. Field width
17186 is length of string. Don't output more than
17187 PRECISION allows us. */
17188 offset--;
17190 prec = c_string_width (SDATA (elt) + last_offset,
17191 offset - last_offset, precision - n,
17192 &nchars, &nbytes);
17194 switch (mode_line_target)
17196 case MODE_LINE_NOPROP:
17197 case MODE_LINE_TITLE:
17198 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17199 break;
17200 case MODE_LINE_STRING:
17202 int bytepos = last_offset;
17203 int charpos = string_byte_to_char (elt, bytepos);
17204 int endpos = (precision <= 0
17205 ? string_byte_to_char (elt, offset)
17206 : charpos + nchars);
17208 n += store_mode_line_string (NULL,
17209 Fsubstring (elt, make_number (charpos),
17210 make_number (endpos)),
17211 0, 0, 0, Qnil);
17213 break;
17214 case MODE_LINE_DISPLAY:
17216 int bytepos = last_offset;
17217 int charpos = string_byte_to_char (elt, bytepos);
17219 if (precision <= 0)
17220 nchars = string_byte_to_char (elt, offset) - charpos;
17221 n += display_string (NULL, elt, Qnil, 0, charpos,
17222 it, 0, nchars, 0,
17223 STRING_MULTIBYTE (elt));
17225 break;
17228 else /* c == '%' */
17230 int percent_position = offset;
17232 /* Get the specified minimum width. Zero means
17233 don't pad. */
17234 field = 0;
17235 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17236 field = field * 10 + c - '0';
17238 /* Don't pad beyond the total padding allowed. */
17239 if (field_width - n > 0 && field > field_width - n)
17240 field = field_width - n;
17242 /* Note that either PRECISION <= 0 or N < PRECISION. */
17243 prec = precision - n;
17245 if (c == 'M')
17246 n += display_mode_element (it, depth, field, prec,
17247 Vglobal_mode_string, props,
17248 risky);
17249 else if (c != 0)
17251 int multibyte;
17252 int bytepos, charpos;
17253 unsigned char *spec;
17255 bytepos = percent_position;
17256 charpos = (STRING_MULTIBYTE (elt)
17257 ? string_byte_to_char (elt, bytepos)
17258 : bytepos);
17259 spec
17260 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17262 switch (mode_line_target)
17264 case MODE_LINE_NOPROP:
17265 case MODE_LINE_TITLE:
17266 n += store_mode_line_noprop (spec, field, prec);
17267 break;
17268 case MODE_LINE_STRING:
17270 int len = strlen (spec);
17271 Lisp_Object tem = make_string (spec, len);
17272 props = Ftext_properties_at (make_number (charpos), elt);
17273 /* Should only keep face property in props */
17274 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17276 break;
17277 case MODE_LINE_DISPLAY:
17279 int nglyphs_before, nwritten;
17281 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17282 nwritten = display_string (spec, Qnil, elt,
17283 charpos, 0, it,
17284 field, prec, 0,
17285 multibyte);
17287 /* Assign to the glyphs written above the
17288 string where the `%x' came from, position
17289 of the `%'. */
17290 if (nwritten > 0)
17292 struct glyph *glyph
17293 = (it->glyph_row->glyphs[TEXT_AREA]
17294 + nglyphs_before);
17295 int i;
17297 for (i = 0; i < nwritten; ++i)
17299 glyph[i].object = elt;
17300 glyph[i].charpos = charpos;
17303 n += nwritten;
17306 break;
17309 else /* c == 0 */
17310 break;
17314 break;
17316 case Lisp_Symbol:
17317 /* A symbol: process the value of the symbol recursively
17318 as if it appeared here directly. Avoid error if symbol void.
17319 Special case: if value of symbol is a string, output the string
17320 literally. */
17322 register Lisp_Object tem;
17324 /* If the variable is not marked as risky to set
17325 then its contents are risky to use. */
17326 if (NILP (Fget (elt, Qrisky_local_variable)))
17327 risky = 1;
17329 tem = Fboundp (elt);
17330 if (!NILP (tem))
17332 tem = Fsymbol_value (elt);
17333 /* If value is a string, output that string literally:
17334 don't check for % within it. */
17335 if (STRINGP (tem))
17336 literal = 1;
17338 if (!EQ (tem, elt))
17340 /* Give up right away for nil or t. */
17341 elt = tem;
17342 goto tail_recurse;
17346 break;
17348 case Lisp_Cons:
17350 register Lisp_Object car, tem;
17352 /* A cons cell: five distinct cases.
17353 If first element is :eval or :propertize, do something special.
17354 If first element is a string or a cons, process all the elements
17355 and effectively concatenate them.
17356 If first element is a negative number, truncate displaying cdr to
17357 at most that many characters. If positive, pad (with spaces)
17358 to at least that many characters.
17359 If first element is a symbol, process the cadr or caddr recursively
17360 according to whether the symbol's value is non-nil or nil. */
17361 car = XCAR (elt);
17362 if (EQ (car, QCeval))
17364 /* An element of the form (:eval FORM) means evaluate FORM
17365 and use the result as mode line elements. */
17367 if (risky)
17368 break;
17370 if (CONSP (XCDR (elt)))
17372 Lisp_Object spec;
17373 spec = safe_eval (XCAR (XCDR (elt)));
17374 n += display_mode_element (it, depth, field_width - n,
17375 precision - n, spec, props,
17376 risky);
17379 else if (EQ (car, QCpropertize))
17381 /* An element of the form (:propertize ELT PROPS...)
17382 means display ELT but applying properties PROPS. */
17384 if (risky)
17385 break;
17387 if (CONSP (XCDR (elt)))
17388 n += display_mode_element (it, depth, field_width - n,
17389 precision - n, XCAR (XCDR (elt)),
17390 XCDR (XCDR (elt)), risky);
17392 else if (SYMBOLP (car))
17394 tem = Fboundp (car);
17395 elt = XCDR (elt);
17396 if (!CONSP (elt))
17397 goto invalid;
17398 /* elt is now the cdr, and we know it is a cons cell.
17399 Use its car if CAR has a non-nil value. */
17400 if (!NILP (tem))
17402 tem = Fsymbol_value (car);
17403 if (!NILP (tem))
17405 elt = XCAR (elt);
17406 goto tail_recurse;
17409 /* Symbol's value is nil (or symbol is unbound)
17410 Get the cddr of the original list
17411 and if possible find the caddr and use that. */
17412 elt = XCDR (elt);
17413 if (NILP (elt))
17414 break;
17415 else if (!CONSP (elt))
17416 goto invalid;
17417 elt = XCAR (elt);
17418 goto tail_recurse;
17420 else if (INTEGERP (car))
17422 register int lim = XINT (car);
17423 elt = XCDR (elt);
17424 if (lim < 0)
17426 /* Negative int means reduce maximum width. */
17427 if (precision <= 0)
17428 precision = -lim;
17429 else
17430 precision = min (precision, -lim);
17432 else if (lim > 0)
17434 /* Padding specified. Don't let it be more than
17435 current maximum. */
17436 if (precision > 0)
17437 lim = min (precision, lim);
17439 /* If that's more padding than already wanted, queue it.
17440 But don't reduce padding already specified even if
17441 that is beyond the current truncation point. */
17442 field_width = max (lim, field_width);
17444 goto tail_recurse;
17446 else if (STRINGP (car) || CONSP (car))
17448 register int limit = 50;
17449 /* Limit is to protect against circular lists. */
17450 while (CONSP (elt)
17451 && --limit > 0
17452 && (precision <= 0 || n < precision))
17454 n += display_mode_element (it, depth,
17455 /* Do padding only after the last
17456 element in the list. */
17457 (! CONSP (XCDR (elt))
17458 ? field_width - n
17459 : 0),
17460 precision - n, XCAR (elt),
17461 props, risky);
17462 elt = XCDR (elt);
17466 break;
17468 default:
17469 invalid:
17470 elt = build_string ("*invalid*");
17471 goto tail_recurse;
17474 /* Pad to FIELD_WIDTH. */
17475 if (field_width > 0 && n < field_width)
17477 switch (mode_line_target)
17479 case MODE_LINE_NOPROP:
17480 case MODE_LINE_TITLE:
17481 n += store_mode_line_noprop ("", field_width - n, 0);
17482 break;
17483 case MODE_LINE_STRING:
17484 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17485 break;
17486 case MODE_LINE_DISPLAY:
17487 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17488 0, 0, 0);
17489 break;
17493 return n;
17496 /* Store a mode-line string element in mode_line_string_list.
17498 If STRING is non-null, display that C string. Otherwise, the Lisp
17499 string LISP_STRING is displayed.
17501 FIELD_WIDTH is the minimum number of output glyphs to produce.
17502 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17503 with spaces. FIELD_WIDTH <= 0 means don't pad.
17505 PRECISION is the maximum number of characters to output from
17506 STRING. PRECISION <= 0 means don't truncate the string.
17508 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17509 properties to the string.
17511 PROPS are the properties to add to the string.
17512 The mode_line_string_face face property is always added to the string.
17515 static int
17516 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17517 char *string;
17518 Lisp_Object lisp_string;
17519 int copy_string;
17520 int field_width;
17521 int precision;
17522 Lisp_Object props;
17524 int len;
17525 int n = 0;
17527 if (string != NULL)
17529 len = strlen (string);
17530 if (precision > 0 && len > precision)
17531 len = precision;
17532 lisp_string = make_string (string, len);
17533 if (NILP (props))
17534 props = mode_line_string_face_prop;
17535 else if (!NILP (mode_line_string_face))
17537 Lisp_Object face = Fplist_get (props, Qface);
17538 props = Fcopy_sequence (props);
17539 if (NILP (face))
17540 face = mode_line_string_face;
17541 else
17542 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17543 props = Fplist_put (props, Qface, face);
17545 Fadd_text_properties (make_number (0), make_number (len),
17546 props, lisp_string);
17548 else
17550 len = XFASTINT (Flength (lisp_string));
17551 if (precision > 0 && len > precision)
17553 len = precision;
17554 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17555 precision = -1;
17557 if (!NILP (mode_line_string_face))
17559 Lisp_Object face;
17560 if (NILP (props))
17561 props = Ftext_properties_at (make_number (0), lisp_string);
17562 face = Fplist_get (props, Qface);
17563 if (NILP (face))
17564 face = mode_line_string_face;
17565 else
17566 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17567 props = Fcons (Qface, Fcons (face, Qnil));
17568 if (copy_string)
17569 lisp_string = Fcopy_sequence (lisp_string);
17571 if (!NILP (props))
17572 Fadd_text_properties (make_number (0), make_number (len),
17573 props, lisp_string);
17576 if (len > 0)
17578 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17579 n += len;
17582 if (field_width > len)
17584 field_width -= len;
17585 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17586 if (!NILP (props))
17587 Fadd_text_properties (make_number (0), make_number (field_width),
17588 props, lisp_string);
17589 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17590 n += field_width;
17593 return n;
17597 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17598 1, 4, 0,
17599 doc: /* Format a string out of a mode line format specification.
17600 First arg FORMAT specifies the mode line format (see `mode-line-format'
17601 for details) to use.
17603 Optional second arg FACE specifies the face property to put
17604 on all characters for which no face is specified.
17605 The value t means whatever face the window's mode line currently uses
17606 \(either `mode-line' or `mode-line-inactive', depending).
17607 A value of nil means the default is no face property.
17608 If FACE is an integer, the value string has no text properties.
17610 Optional third and fourth args WINDOW and BUFFER specify the window
17611 and buffer to use as the context for the formatting (defaults
17612 are the selected window and the window's buffer). */)
17613 (format, face, window, buffer)
17614 Lisp_Object format, face, window, buffer;
17616 struct it it;
17617 int len;
17618 struct window *w;
17619 struct buffer *old_buffer = NULL;
17620 int face_id = -1;
17621 int no_props = INTEGERP (face);
17622 int count = SPECPDL_INDEX ();
17623 Lisp_Object str;
17624 int string_start = 0;
17626 if (NILP (window))
17627 window = selected_window;
17628 CHECK_WINDOW (window);
17629 w = XWINDOW (window);
17631 if (NILP (buffer))
17632 buffer = w->buffer;
17633 CHECK_BUFFER (buffer);
17635 /* Make formatting the modeline a non-op when noninteractive, otherwise
17636 there will be problems later caused by a partially initialized frame. */
17637 if (NILP (format) || noninteractive)
17638 return empty_unibyte_string;
17640 if (no_props)
17641 face = Qnil;
17643 if (!NILP (face))
17645 if (EQ (face, Qt))
17646 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17647 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17650 if (face_id < 0)
17651 face_id = DEFAULT_FACE_ID;
17653 if (XBUFFER (buffer) != current_buffer)
17654 old_buffer = current_buffer;
17656 /* Save things including mode_line_proptrans_alist,
17657 and set that to nil so that we don't alter the outer value. */
17658 record_unwind_protect (unwind_format_mode_line,
17659 format_mode_line_unwind_data
17660 (old_buffer, selected_window, 1));
17661 mode_line_proptrans_alist = Qnil;
17663 Fselect_window (window, Qt);
17664 if (old_buffer)
17665 set_buffer_internal_1 (XBUFFER (buffer));
17667 init_iterator (&it, w, -1, -1, NULL, face_id);
17669 if (no_props)
17671 mode_line_target = MODE_LINE_NOPROP;
17672 mode_line_string_face_prop = Qnil;
17673 mode_line_string_list = Qnil;
17674 string_start = MODE_LINE_NOPROP_LEN (0);
17676 else
17678 mode_line_target = MODE_LINE_STRING;
17679 mode_line_string_list = Qnil;
17680 mode_line_string_face = face;
17681 mode_line_string_face_prop
17682 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17685 push_kboard (FRAME_KBOARD (it.f));
17686 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17687 pop_kboard ();
17689 if (no_props)
17691 len = MODE_LINE_NOPROP_LEN (string_start);
17692 str = make_string (mode_line_noprop_buf + string_start, len);
17694 else
17696 mode_line_string_list = Fnreverse (mode_line_string_list);
17697 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17698 empty_unibyte_string);
17701 unbind_to (count, Qnil);
17702 return str;
17705 /* Write a null-terminated, right justified decimal representation of
17706 the positive integer D to BUF using a minimal field width WIDTH. */
17708 static void
17709 pint2str (buf, width, d)
17710 register char *buf;
17711 register int width;
17712 register int d;
17714 register char *p = buf;
17716 if (d <= 0)
17717 *p++ = '0';
17718 else
17720 while (d > 0)
17722 *p++ = d % 10 + '0';
17723 d /= 10;
17727 for (width -= (int) (p - buf); width > 0; --width)
17728 *p++ = ' ';
17729 *p-- = '\0';
17730 while (p > buf)
17732 d = *buf;
17733 *buf++ = *p;
17734 *p-- = d;
17738 /* Write a null-terminated, right justified decimal and "human
17739 readable" representation of the nonnegative integer D to BUF using
17740 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17742 static const char power_letter[] =
17744 0, /* not used */
17745 'k', /* kilo */
17746 'M', /* mega */
17747 'G', /* giga */
17748 'T', /* tera */
17749 'P', /* peta */
17750 'E', /* exa */
17751 'Z', /* zetta */
17752 'Y' /* yotta */
17755 static void
17756 pint2hrstr (buf, width, d)
17757 char *buf;
17758 int width;
17759 int d;
17761 /* We aim to represent the nonnegative integer D as
17762 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17763 int quotient = d;
17764 int remainder = 0;
17765 /* -1 means: do not use TENTHS. */
17766 int tenths = -1;
17767 int exponent = 0;
17769 /* Length of QUOTIENT.TENTHS as a string. */
17770 int length;
17772 char * psuffix;
17773 char * p;
17775 if (1000 <= quotient)
17777 /* Scale to the appropriate EXPONENT. */
17780 remainder = quotient % 1000;
17781 quotient /= 1000;
17782 exponent++;
17784 while (1000 <= quotient);
17786 /* Round to nearest and decide whether to use TENTHS or not. */
17787 if (quotient <= 9)
17789 tenths = remainder / 100;
17790 if (50 <= remainder % 100)
17792 if (tenths < 9)
17793 tenths++;
17794 else
17796 quotient++;
17797 if (quotient == 10)
17798 tenths = -1;
17799 else
17800 tenths = 0;
17804 else
17805 if (500 <= remainder)
17807 if (quotient < 999)
17808 quotient++;
17809 else
17811 quotient = 1;
17812 exponent++;
17813 tenths = 0;
17818 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17819 if (tenths == -1 && quotient <= 99)
17820 if (quotient <= 9)
17821 length = 1;
17822 else
17823 length = 2;
17824 else
17825 length = 3;
17826 p = psuffix = buf + max (width, length);
17828 /* Print EXPONENT. */
17829 if (exponent)
17830 *psuffix++ = power_letter[exponent];
17831 *psuffix = '\0';
17833 /* Print TENTHS. */
17834 if (tenths >= 0)
17836 *--p = '0' + tenths;
17837 *--p = '.';
17840 /* Print QUOTIENT. */
17843 int digit = quotient % 10;
17844 *--p = '0' + digit;
17846 while ((quotient /= 10) != 0);
17848 /* Print leading spaces. */
17849 while (buf < p)
17850 *--p = ' ';
17853 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17854 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17855 type of CODING_SYSTEM. Return updated pointer into BUF. */
17857 static unsigned char invalid_eol_type[] = "(*invalid*)";
17859 static char *
17860 decode_mode_spec_coding (coding_system, buf, eol_flag)
17861 Lisp_Object coding_system;
17862 register char *buf;
17863 int eol_flag;
17865 Lisp_Object val;
17866 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17867 const unsigned char *eol_str;
17868 int eol_str_len;
17869 /* The EOL conversion we are using. */
17870 Lisp_Object eoltype;
17872 val = CODING_SYSTEM_SPEC (coding_system);
17873 eoltype = Qnil;
17875 if (!VECTORP (val)) /* Not yet decided. */
17877 if (multibyte)
17878 *buf++ = '-';
17879 if (eol_flag)
17880 eoltype = eol_mnemonic_undecided;
17881 /* Don't mention EOL conversion if it isn't decided. */
17883 else
17885 Lisp_Object attrs;
17886 Lisp_Object eolvalue;
17888 attrs = AREF (val, 0);
17889 eolvalue = AREF (val, 2);
17891 if (multibyte)
17892 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17894 if (eol_flag)
17896 /* The EOL conversion that is normal on this system. */
17898 if (NILP (eolvalue)) /* Not yet decided. */
17899 eoltype = eol_mnemonic_undecided;
17900 else if (VECTORP (eolvalue)) /* Not yet decided. */
17901 eoltype = eol_mnemonic_undecided;
17902 else /* eolvalue is Qunix, Qdos, or Qmac. */
17903 eoltype = (EQ (eolvalue, Qunix)
17904 ? eol_mnemonic_unix
17905 : (EQ (eolvalue, Qdos) == 1
17906 ? eol_mnemonic_dos : eol_mnemonic_mac));
17910 if (eol_flag)
17912 /* Mention the EOL conversion if it is not the usual one. */
17913 if (STRINGP (eoltype))
17915 eol_str = SDATA (eoltype);
17916 eol_str_len = SBYTES (eoltype);
17918 else if (CHARACTERP (eoltype))
17920 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17921 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17922 eol_str = tmp;
17924 else
17926 eol_str = invalid_eol_type;
17927 eol_str_len = sizeof (invalid_eol_type) - 1;
17929 bcopy (eol_str, buf, eol_str_len);
17930 buf += eol_str_len;
17933 return buf;
17936 /* Return a string for the output of a mode line %-spec for window W,
17937 generated by character C. PRECISION >= 0 means don't return a
17938 string longer than that value. FIELD_WIDTH > 0 means pad the
17939 string returned with spaces to that value. Return 1 in *MULTIBYTE
17940 if the result is multibyte text.
17942 Note we operate on the current buffer for most purposes,
17943 the exception being w->base_line_pos. */
17945 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17947 static char *
17948 decode_mode_spec (w, c, field_width, precision, multibyte)
17949 struct window *w;
17950 register int c;
17951 int field_width, precision;
17952 int *multibyte;
17954 Lisp_Object obj;
17955 struct frame *f = XFRAME (WINDOW_FRAME (w));
17956 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17957 struct buffer *b = current_buffer;
17959 obj = Qnil;
17960 *multibyte = 0;
17962 switch (c)
17964 case '*':
17965 if (!NILP (b->read_only))
17966 return "%";
17967 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17968 return "*";
17969 return "-";
17971 case '+':
17972 /* This differs from %* only for a modified read-only buffer. */
17973 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17974 return "*";
17975 if (!NILP (b->read_only))
17976 return "%";
17977 return "-";
17979 case '&':
17980 /* This differs from %* in ignoring read-only-ness. */
17981 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17982 return "*";
17983 return "-";
17985 case '%':
17986 return "%";
17988 case '[':
17990 int i;
17991 char *p;
17993 if (command_loop_level > 5)
17994 return "[[[... ";
17995 p = decode_mode_spec_buf;
17996 for (i = 0; i < command_loop_level; i++)
17997 *p++ = '[';
17998 *p = 0;
17999 return decode_mode_spec_buf;
18002 case ']':
18004 int i;
18005 char *p;
18007 if (command_loop_level > 5)
18008 return " ...]]]";
18009 p = decode_mode_spec_buf;
18010 for (i = 0; i < command_loop_level; i++)
18011 *p++ = ']';
18012 *p = 0;
18013 return decode_mode_spec_buf;
18016 case '-':
18018 register int i;
18020 /* Let lots_of_dashes be a string of infinite length. */
18021 if (mode_line_target == MODE_LINE_NOPROP ||
18022 mode_line_target == MODE_LINE_STRING)
18023 return "--";
18024 if (field_width <= 0
18025 || field_width > sizeof (lots_of_dashes))
18027 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18028 decode_mode_spec_buf[i] = '-';
18029 decode_mode_spec_buf[i] = '\0';
18030 return decode_mode_spec_buf;
18032 else
18033 return lots_of_dashes;
18036 case 'b':
18037 obj = b->name;
18038 break;
18040 case 'c':
18041 /* %c and %l are ignored in `frame-title-format'.
18042 (In redisplay_internal, the frame title is drawn _before_ the
18043 windows are updated, so the stuff which depends on actual
18044 window contents (such as %l) may fail to render properly, or
18045 even crash emacs.) */
18046 if (mode_line_target == MODE_LINE_TITLE)
18047 return "";
18048 else
18050 int col = (int) current_column (); /* iftc */
18051 w->column_number_displayed = make_number (col);
18052 pint2str (decode_mode_spec_buf, field_width, col);
18053 return decode_mode_spec_buf;
18056 case 'e':
18057 #ifndef SYSTEM_MALLOC
18059 if (NILP (Vmemory_full))
18060 return "";
18061 else
18062 return "!MEM FULL! ";
18064 #else
18065 return "";
18066 #endif
18068 case 'F':
18069 /* %F displays the frame name. */
18070 if (!NILP (f->title))
18071 return (char *) SDATA (f->title);
18072 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18073 return (char *) SDATA (f->name);
18074 return "Emacs";
18076 case 'f':
18077 obj = b->filename;
18078 break;
18080 case 'i':
18082 int size = ZV - BEGV;
18083 pint2str (decode_mode_spec_buf, field_width, size);
18084 return decode_mode_spec_buf;
18087 case 'I':
18089 int size = ZV - BEGV;
18090 pint2hrstr (decode_mode_spec_buf, field_width, size);
18091 return decode_mode_spec_buf;
18094 case 'l':
18096 int startpos, startpos_byte, line, linepos, linepos_byte;
18097 int topline, nlines, junk, height;
18099 /* %c and %l are ignored in `frame-title-format'. */
18100 if (mode_line_target == MODE_LINE_TITLE)
18101 return "";
18103 startpos = XMARKER (w->start)->charpos;
18104 startpos_byte = marker_byte_position (w->start);
18105 height = WINDOW_TOTAL_LINES (w);
18107 /* If we decided that this buffer isn't suitable for line numbers,
18108 don't forget that too fast. */
18109 if (EQ (w->base_line_pos, w->buffer))
18110 goto no_value;
18111 /* But do forget it, if the window shows a different buffer now. */
18112 else if (BUFFERP (w->base_line_pos))
18113 w->base_line_pos = Qnil;
18115 /* If the buffer is very big, don't waste time. */
18116 if (INTEGERP (Vline_number_display_limit)
18117 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18119 w->base_line_pos = Qnil;
18120 w->base_line_number = Qnil;
18121 goto no_value;
18124 if (INTEGERP (w->base_line_number)
18125 && INTEGERP (w->base_line_pos)
18126 && XFASTINT (w->base_line_pos) <= startpos)
18128 line = XFASTINT (w->base_line_number);
18129 linepos = XFASTINT (w->base_line_pos);
18130 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18132 else
18134 line = 1;
18135 linepos = BUF_BEGV (b);
18136 linepos_byte = BUF_BEGV_BYTE (b);
18139 /* Count lines from base line to window start position. */
18140 nlines = display_count_lines (linepos, linepos_byte,
18141 startpos_byte,
18142 startpos, &junk);
18144 topline = nlines + line;
18146 /* Determine a new base line, if the old one is too close
18147 or too far away, or if we did not have one.
18148 "Too close" means it's plausible a scroll-down would
18149 go back past it. */
18150 if (startpos == BUF_BEGV (b))
18152 w->base_line_number = make_number (topline);
18153 w->base_line_pos = make_number (BUF_BEGV (b));
18155 else if (nlines < height + 25 || nlines > height * 3 + 50
18156 || linepos == BUF_BEGV (b))
18158 int limit = BUF_BEGV (b);
18159 int limit_byte = BUF_BEGV_BYTE (b);
18160 int position;
18161 int distance = (height * 2 + 30) * line_number_display_limit_width;
18163 if (startpos - distance > limit)
18165 limit = startpos - distance;
18166 limit_byte = CHAR_TO_BYTE (limit);
18169 nlines = display_count_lines (startpos, startpos_byte,
18170 limit_byte,
18171 - (height * 2 + 30),
18172 &position);
18173 /* If we couldn't find the lines we wanted within
18174 line_number_display_limit_width chars per line,
18175 give up on line numbers for this window. */
18176 if (position == limit_byte && limit == startpos - distance)
18178 w->base_line_pos = w->buffer;
18179 w->base_line_number = Qnil;
18180 goto no_value;
18183 w->base_line_number = make_number (topline - nlines);
18184 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18187 /* Now count lines from the start pos to point. */
18188 nlines = display_count_lines (startpos, startpos_byte,
18189 PT_BYTE, PT, &junk);
18191 /* Record that we did display the line number. */
18192 line_number_displayed = 1;
18194 /* Make the string to show. */
18195 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18196 return decode_mode_spec_buf;
18197 no_value:
18199 char* p = decode_mode_spec_buf;
18200 int pad = field_width - 2;
18201 while (pad-- > 0)
18202 *p++ = ' ';
18203 *p++ = '?';
18204 *p++ = '?';
18205 *p = '\0';
18206 return decode_mode_spec_buf;
18209 break;
18211 case 'm':
18212 obj = b->mode_name;
18213 break;
18215 case 'n':
18216 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18217 return " Narrow";
18218 break;
18220 case 'p':
18222 int pos = marker_position (w->start);
18223 int total = BUF_ZV (b) - BUF_BEGV (b);
18225 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18227 if (pos <= BUF_BEGV (b))
18228 return "All";
18229 else
18230 return "Bottom";
18232 else if (pos <= BUF_BEGV (b))
18233 return "Top";
18234 else
18236 if (total > 1000000)
18237 /* Do it differently for a large value, to avoid overflow. */
18238 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18239 else
18240 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18241 /* We can't normally display a 3-digit number,
18242 so get us a 2-digit number that is close. */
18243 if (total == 100)
18244 total = 99;
18245 sprintf (decode_mode_spec_buf, "%2d%%", total);
18246 return decode_mode_spec_buf;
18250 /* Display percentage of size above the bottom of the screen. */
18251 case 'P':
18253 int toppos = marker_position (w->start);
18254 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18255 int total = BUF_ZV (b) - BUF_BEGV (b);
18257 if (botpos >= BUF_ZV (b))
18259 if (toppos <= BUF_BEGV (b))
18260 return "All";
18261 else
18262 return "Bottom";
18264 else
18266 if (total > 1000000)
18267 /* Do it differently for a large value, to avoid overflow. */
18268 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18269 else
18270 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18271 /* We can't normally display a 3-digit number,
18272 so get us a 2-digit number that is close. */
18273 if (total == 100)
18274 total = 99;
18275 if (toppos <= BUF_BEGV (b))
18276 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18277 else
18278 sprintf (decode_mode_spec_buf, "%2d%%", total);
18279 return decode_mode_spec_buf;
18283 case 's':
18284 /* status of process */
18285 obj = Fget_buffer_process (Fcurrent_buffer ());
18286 if (NILP (obj))
18287 return "no process";
18288 #ifdef subprocesses
18289 obj = Fsymbol_name (Fprocess_status (obj));
18290 #endif
18291 break;
18293 case '@':
18295 Lisp_Object val;
18296 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18297 if (NILP (val))
18298 return "-";
18299 else
18300 return "@";
18303 case 't': /* indicate TEXT or BINARY */
18304 #ifdef MODE_LINE_BINARY_TEXT
18305 return MODE_LINE_BINARY_TEXT (b);
18306 #else
18307 return "T";
18308 #endif
18310 case 'z':
18311 /* coding-system (not including end-of-line format) */
18312 case 'Z':
18313 /* coding-system (including end-of-line type) */
18315 int eol_flag = (c == 'Z');
18316 char *p = decode_mode_spec_buf;
18318 if (! FRAME_WINDOW_P (f))
18320 /* No need to mention EOL here--the terminal never needs
18321 to do EOL conversion. */
18322 p = decode_mode_spec_coding (CODING_ID_NAME
18323 (FRAME_KEYBOARD_CODING (f)->id),
18324 p, 0);
18325 p = decode_mode_spec_coding (CODING_ID_NAME
18326 (FRAME_TERMINAL_CODING (f)->id),
18327 p, 0);
18329 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18330 p, eol_flag);
18332 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18333 #ifdef subprocesses
18334 obj = Fget_buffer_process (Fcurrent_buffer ());
18335 if (PROCESSP (obj))
18337 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18338 p, eol_flag);
18339 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18340 p, eol_flag);
18342 #endif /* subprocesses */
18343 #endif /* 0 */
18344 *p = 0;
18345 return decode_mode_spec_buf;
18349 if (STRINGP (obj))
18351 *multibyte = STRING_MULTIBYTE (obj);
18352 return (char *) SDATA (obj);
18354 else
18355 return "";
18359 /* Count up to COUNT lines starting from START / START_BYTE.
18360 But don't go beyond LIMIT_BYTE.
18361 Return the number of lines thus found (always nonnegative).
18363 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18365 static int
18366 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18367 int start, start_byte, limit_byte, count;
18368 int *byte_pos_ptr;
18370 register unsigned char *cursor;
18371 unsigned char *base;
18373 register int ceiling;
18374 register unsigned char *ceiling_addr;
18375 int orig_count = count;
18377 /* If we are not in selective display mode,
18378 check only for newlines. */
18379 int selective_display = (!NILP (current_buffer->selective_display)
18380 && !INTEGERP (current_buffer->selective_display));
18382 if (count > 0)
18384 while (start_byte < limit_byte)
18386 ceiling = BUFFER_CEILING_OF (start_byte);
18387 ceiling = min (limit_byte - 1, ceiling);
18388 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18389 base = (cursor = BYTE_POS_ADDR (start_byte));
18390 while (1)
18392 if (selective_display)
18393 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18395 else
18396 while (*cursor != '\n' && ++cursor != ceiling_addr)
18399 if (cursor != ceiling_addr)
18401 if (--count == 0)
18403 start_byte += cursor - base + 1;
18404 *byte_pos_ptr = start_byte;
18405 return orig_count;
18407 else
18408 if (++cursor == ceiling_addr)
18409 break;
18411 else
18412 break;
18414 start_byte += cursor - base;
18417 else
18419 while (start_byte > limit_byte)
18421 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18422 ceiling = max (limit_byte, ceiling);
18423 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18424 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18425 while (1)
18427 if (selective_display)
18428 while (--cursor != ceiling_addr
18429 && *cursor != '\n' && *cursor != 015)
18431 else
18432 while (--cursor != ceiling_addr && *cursor != '\n')
18435 if (cursor != ceiling_addr)
18437 if (++count == 0)
18439 start_byte += cursor - base + 1;
18440 *byte_pos_ptr = start_byte;
18441 /* When scanning backwards, we should
18442 not count the newline posterior to which we stop. */
18443 return - orig_count - 1;
18446 else
18447 break;
18449 /* Here we add 1 to compensate for the last decrement
18450 of CURSOR, which took it past the valid range. */
18451 start_byte += cursor - base + 1;
18455 *byte_pos_ptr = limit_byte;
18457 if (count < 0)
18458 return - orig_count + count;
18459 return orig_count - count;
18465 /***********************************************************************
18466 Displaying strings
18467 ***********************************************************************/
18469 /* Display a NUL-terminated string, starting with index START.
18471 If STRING is non-null, display that C string. Otherwise, the Lisp
18472 string LISP_STRING is displayed.
18474 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18475 FACE_STRING. Display STRING or LISP_STRING with the face at
18476 FACE_STRING_POS in FACE_STRING:
18478 Display the string in the environment given by IT, but use the
18479 standard display table, temporarily.
18481 FIELD_WIDTH is the minimum number of output glyphs to produce.
18482 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18483 with spaces. If STRING has more characters, more than FIELD_WIDTH
18484 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18486 PRECISION is the maximum number of characters to output from
18487 STRING. PRECISION < 0 means don't truncate the string.
18489 This is roughly equivalent to printf format specifiers:
18491 FIELD_WIDTH PRECISION PRINTF
18492 ----------------------------------------
18493 -1 -1 %s
18494 -1 10 %.10s
18495 10 -1 %10s
18496 20 10 %20.10s
18498 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18499 display them, and < 0 means obey the current buffer's value of
18500 enable_multibyte_characters.
18502 Value is the number of columns displayed. */
18504 static int
18505 display_string (string, lisp_string, face_string, face_string_pos,
18506 start, it, field_width, precision, max_x, multibyte)
18507 unsigned char *string;
18508 Lisp_Object lisp_string;
18509 Lisp_Object face_string;
18510 EMACS_INT face_string_pos;
18511 EMACS_INT start;
18512 struct it *it;
18513 int field_width, precision, max_x;
18514 int multibyte;
18516 int hpos_at_start = it->hpos;
18517 int saved_face_id = it->face_id;
18518 struct glyph_row *row = it->glyph_row;
18520 /* Initialize the iterator IT for iteration over STRING beginning
18521 with index START. */
18522 reseat_to_string (it, string, lisp_string, start,
18523 precision, field_width, multibyte);
18525 /* If displaying STRING, set up the face of the iterator
18526 from LISP_STRING, if that's given. */
18527 if (STRINGP (face_string))
18529 EMACS_INT endptr;
18530 struct face *face;
18532 it->face_id
18533 = face_at_string_position (it->w, face_string, face_string_pos,
18534 0, it->region_beg_charpos,
18535 it->region_end_charpos,
18536 &endptr, it->base_face_id, 0);
18537 face = FACE_FROM_ID (it->f, it->face_id);
18538 it->face_box_p = face->box != FACE_NO_BOX;
18541 /* Set max_x to the maximum allowed X position. Don't let it go
18542 beyond the right edge of the window. */
18543 if (max_x <= 0)
18544 max_x = it->last_visible_x;
18545 else
18546 max_x = min (max_x, it->last_visible_x);
18548 /* Skip over display elements that are not visible. because IT->w is
18549 hscrolled. */
18550 if (it->current_x < it->first_visible_x)
18551 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18552 MOVE_TO_POS | MOVE_TO_X);
18554 row->ascent = it->max_ascent;
18555 row->height = it->max_ascent + it->max_descent;
18556 row->phys_ascent = it->max_phys_ascent;
18557 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18558 row->extra_line_spacing = it->max_extra_line_spacing;
18560 /* This condition is for the case that we are called with current_x
18561 past last_visible_x. */
18562 while (it->current_x < max_x)
18564 int x_before, x, n_glyphs_before, i, nglyphs;
18566 /* Get the next display element. */
18567 if (!get_next_display_element (it))
18568 break;
18570 /* Produce glyphs. */
18571 x_before = it->current_x;
18572 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18573 PRODUCE_GLYPHS (it);
18575 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18576 i = 0;
18577 x = x_before;
18578 while (i < nglyphs)
18580 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18582 if (!it->truncate_lines_p
18583 && x + glyph->pixel_width > max_x)
18585 /* End of continued line or max_x reached. */
18586 if (CHAR_GLYPH_PADDING_P (*glyph))
18588 /* A wide character is unbreakable. */
18589 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18590 it->current_x = x_before;
18592 else
18594 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18595 it->current_x = x;
18597 break;
18599 else if (x + glyph->pixel_width >= it->first_visible_x)
18601 /* Glyph is at least partially visible. */
18602 ++it->hpos;
18603 if (x < it->first_visible_x)
18604 it->glyph_row->x = x - it->first_visible_x;
18606 else
18608 /* Glyph is off the left margin of the display area.
18609 Should not happen. */
18610 abort ();
18613 row->ascent = max (row->ascent, it->max_ascent);
18614 row->height = max (row->height, it->max_ascent + it->max_descent);
18615 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18616 row->phys_height = max (row->phys_height,
18617 it->max_phys_ascent + it->max_phys_descent);
18618 row->extra_line_spacing = max (row->extra_line_spacing,
18619 it->max_extra_line_spacing);
18620 x += glyph->pixel_width;
18621 ++i;
18624 /* Stop if max_x reached. */
18625 if (i < nglyphs)
18626 break;
18628 /* Stop at line ends. */
18629 if (ITERATOR_AT_END_OF_LINE_P (it))
18631 it->continuation_lines_width = 0;
18632 break;
18635 set_iterator_to_next (it, 1);
18637 /* Stop if truncating at the right edge. */
18638 if (it->truncate_lines_p
18639 && it->current_x >= it->last_visible_x)
18641 /* Add truncation mark, but don't do it if the line is
18642 truncated at a padding space. */
18643 if (IT_CHARPOS (*it) < it->string_nchars)
18645 if (!FRAME_WINDOW_P (it->f))
18647 int i, n;
18649 if (it->current_x > it->last_visible_x)
18651 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18652 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18653 break;
18654 for (n = row->used[TEXT_AREA]; i < n; ++i)
18656 row->used[TEXT_AREA] = i;
18657 produce_special_glyphs (it, IT_TRUNCATION);
18660 produce_special_glyphs (it, IT_TRUNCATION);
18662 it->glyph_row->truncated_on_right_p = 1;
18664 break;
18668 /* Maybe insert a truncation at the left. */
18669 if (it->first_visible_x
18670 && IT_CHARPOS (*it) > 0)
18672 if (!FRAME_WINDOW_P (it->f))
18673 insert_left_trunc_glyphs (it);
18674 it->glyph_row->truncated_on_left_p = 1;
18677 it->face_id = saved_face_id;
18679 /* Value is number of columns displayed. */
18680 return it->hpos - hpos_at_start;
18685 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18686 appears as an element of LIST or as the car of an element of LIST.
18687 If PROPVAL is a list, compare each element against LIST in that
18688 way, and return 1/2 if any element of PROPVAL is found in LIST.
18689 Otherwise return 0. This function cannot quit.
18690 The return value is 2 if the text is invisible but with an ellipsis
18691 and 1 if it's invisible and without an ellipsis. */
18694 invisible_p (propval, list)
18695 register Lisp_Object propval;
18696 Lisp_Object list;
18698 register Lisp_Object tail, proptail;
18700 for (tail = list; CONSP (tail); tail = XCDR (tail))
18702 register Lisp_Object tem;
18703 tem = XCAR (tail);
18704 if (EQ (propval, tem))
18705 return 1;
18706 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18707 return NILP (XCDR (tem)) ? 1 : 2;
18710 if (CONSP (propval))
18712 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18714 Lisp_Object propelt;
18715 propelt = XCAR (proptail);
18716 for (tail = list; CONSP (tail); tail = XCDR (tail))
18718 register Lisp_Object tem;
18719 tem = XCAR (tail);
18720 if (EQ (propelt, tem))
18721 return 1;
18722 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18723 return NILP (XCDR (tem)) ? 1 : 2;
18728 return 0;
18731 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18732 doc: /* Non-nil if the property makes the text invisible.
18733 POS-OR-PROP can be a marker or number, in which case it is taken to be
18734 a position in the current buffer and the value of the `invisible' property
18735 is checked; or it can be some other value, which is then presumed to be the
18736 value of the `invisible' property of the text of interest.
18737 The non-nil value returned can be t for truly invisible text or something
18738 else if the text is replaced by an ellipsis. */)
18739 (pos_or_prop)
18740 Lisp_Object pos_or_prop;
18742 Lisp_Object prop
18743 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18744 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18745 : pos_or_prop);
18746 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18747 return (invis == 0 ? Qnil
18748 : invis == 1 ? Qt
18749 : make_number (invis));
18752 /* Calculate a width or height in pixels from a specification using
18753 the following elements:
18755 SPEC ::=
18756 NUM - a (fractional) multiple of the default font width/height
18757 (NUM) - specifies exactly NUM pixels
18758 UNIT - a fixed number of pixels, see below.
18759 ELEMENT - size of a display element in pixels, see below.
18760 (NUM . SPEC) - equals NUM * SPEC
18761 (+ SPEC SPEC ...) - add pixel values
18762 (- SPEC SPEC ...) - subtract pixel values
18763 (- SPEC) - negate pixel value
18765 NUM ::=
18766 INT or FLOAT - a number constant
18767 SYMBOL - use symbol's (buffer local) variable binding.
18769 UNIT ::=
18770 in - pixels per inch *)
18771 mm - pixels per 1/1000 meter *)
18772 cm - pixels per 1/100 meter *)
18773 width - width of current font in pixels.
18774 height - height of current font in pixels.
18776 *) using the ratio(s) defined in display-pixels-per-inch.
18778 ELEMENT ::=
18780 left-fringe - left fringe width in pixels
18781 right-fringe - right fringe width in pixels
18783 left-margin - left margin width in pixels
18784 right-margin - right margin width in pixels
18786 scroll-bar - scroll-bar area width in pixels
18788 Examples:
18790 Pixels corresponding to 5 inches:
18791 (5 . in)
18793 Total width of non-text areas on left side of window (if scroll-bar is on left):
18794 '(space :width (+ left-fringe left-margin scroll-bar))
18796 Align to first text column (in header line):
18797 '(space :align-to 0)
18799 Align to middle of text area minus half the width of variable `my-image'
18800 containing a loaded image:
18801 '(space :align-to (0.5 . (- text my-image)))
18803 Width of left margin minus width of 1 character in the default font:
18804 '(space :width (- left-margin 1))
18806 Width of left margin minus width of 2 characters in the current font:
18807 '(space :width (- left-margin (2 . width)))
18809 Center 1 character over left-margin (in header line):
18810 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18812 Different ways to express width of left fringe plus left margin minus one pixel:
18813 '(space :width (- (+ left-fringe left-margin) (1)))
18814 '(space :width (+ left-fringe left-margin (- (1))))
18815 '(space :width (+ left-fringe left-margin (-1)))
18819 #define NUMVAL(X) \
18820 ((INTEGERP (X) || FLOATP (X)) \
18821 ? XFLOATINT (X) \
18822 : - 1)
18825 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18826 double *res;
18827 struct it *it;
18828 Lisp_Object prop;
18829 struct font *font;
18830 int width_p, *align_to;
18832 double pixels;
18834 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18835 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18837 if (NILP (prop))
18838 return OK_PIXELS (0);
18840 xassert (FRAME_LIVE_P (it->f));
18842 if (SYMBOLP (prop))
18844 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18846 char *unit = SDATA (SYMBOL_NAME (prop));
18848 if (unit[0] == 'i' && unit[1] == 'n')
18849 pixels = 1.0;
18850 else if (unit[0] == 'm' && unit[1] == 'm')
18851 pixels = 25.4;
18852 else if (unit[0] == 'c' && unit[1] == 'm')
18853 pixels = 2.54;
18854 else
18855 pixels = 0;
18856 if (pixels > 0)
18858 double ppi;
18859 #ifdef HAVE_WINDOW_SYSTEM
18860 if (FRAME_WINDOW_P (it->f)
18861 && (ppi = (width_p
18862 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18863 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18864 ppi > 0))
18865 return OK_PIXELS (ppi / pixels);
18866 #endif
18868 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18869 || (CONSP (Vdisplay_pixels_per_inch)
18870 && (ppi = (width_p
18871 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18872 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18873 ppi > 0)))
18874 return OK_PIXELS (ppi / pixels);
18876 return 0;
18880 #ifdef HAVE_WINDOW_SYSTEM
18881 if (EQ (prop, Qheight))
18882 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18883 if (EQ (prop, Qwidth))
18884 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18885 #else
18886 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18887 return OK_PIXELS (1);
18888 #endif
18890 if (EQ (prop, Qtext))
18891 return OK_PIXELS (width_p
18892 ? window_box_width (it->w, TEXT_AREA)
18893 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18895 if (align_to && *align_to < 0)
18897 *res = 0;
18898 if (EQ (prop, Qleft))
18899 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18900 if (EQ (prop, Qright))
18901 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18902 if (EQ (prop, Qcenter))
18903 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18904 + window_box_width (it->w, TEXT_AREA) / 2);
18905 if (EQ (prop, Qleft_fringe))
18906 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18907 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18908 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18909 if (EQ (prop, Qright_fringe))
18910 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18911 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18912 : window_box_right_offset (it->w, TEXT_AREA));
18913 if (EQ (prop, Qleft_margin))
18914 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18915 if (EQ (prop, Qright_margin))
18916 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18917 if (EQ (prop, Qscroll_bar))
18918 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18920 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18921 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18922 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18923 : 0)));
18925 else
18927 if (EQ (prop, Qleft_fringe))
18928 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18929 if (EQ (prop, Qright_fringe))
18930 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18931 if (EQ (prop, Qleft_margin))
18932 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18933 if (EQ (prop, Qright_margin))
18934 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18935 if (EQ (prop, Qscroll_bar))
18936 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18939 prop = Fbuffer_local_value (prop, it->w->buffer);
18942 if (INTEGERP (prop) || FLOATP (prop))
18944 int base_unit = (width_p
18945 ? FRAME_COLUMN_WIDTH (it->f)
18946 : FRAME_LINE_HEIGHT (it->f));
18947 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18950 if (CONSP (prop))
18952 Lisp_Object car = XCAR (prop);
18953 Lisp_Object cdr = XCDR (prop);
18955 if (SYMBOLP (car))
18957 #ifdef HAVE_WINDOW_SYSTEM
18958 if (FRAME_WINDOW_P (it->f)
18959 && valid_image_p (prop))
18961 int id = lookup_image (it->f, prop);
18962 struct image *img = IMAGE_FROM_ID (it->f, id);
18964 return OK_PIXELS (width_p ? img->width : img->height);
18966 #endif
18967 if (EQ (car, Qplus) || EQ (car, Qminus))
18969 int first = 1;
18970 double px;
18972 pixels = 0;
18973 while (CONSP (cdr))
18975 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18976 font, width_p, align_to))
18977 return 0;
18978 if (first)
18979 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18980 else
18981 pixels += px;
18982 cdr = XCDR (cdr);
18984 if (EQ (car, Qminus))
18985 pixels = -pixels;
18986 return OK_PIXELS (pixels);
18989 car = Fbuffer_local_value (car, it->w->buffer);
18992 if (INTEGERP (car) || FLOATP (car))
18994 double fact;
18995 pixels = XFLOATINT (car);
18996 if (NILP (cdr))
18997 return OK_PIXELS (pixels);
18998 if (calc_pixel_width_or_height (&fact, it, cdr,
18999 font, width_p, align_to))
19000 return OK_PIXELS (pixels * fact);
19001 return 0;
19004 return 0;
19007 return 0;
19011 /***********************************************************************
19012 Glyph Display
19013 ***********************************************************************/
19015 #ifdef HAVE_WINDOW_SYSTEM
19017 #if GLYPH_DEBUG
19019 void
19020 dump_glyph_string (s)
19021 struct glyph_string *s;
19023 fprintf (stderr, "glyph string\n");
19024 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19025 s->x, s->y, s->width, s->height);
19026 fprintf (stderr, " ybase = %d\n", s->ybase);
19027 fprintf (stderr, " hl = %d\n", s->hl);
19028 fprintf (stderr, " left overhang = %d, right = %d\n",
19029 s->left_overhang, s->right_overhang);
19030 fprintf (stderr, " nchars = %d\n", s->nchars);
19031 fprintf (stderr, " extends to end of line = %d\n",
19032 s->extends_to_end_of_line_p);
19033 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19034 fprintf (stderr, " bg width = %d\n", s->background_width);
19037 #endif /* GLYPH_DEBUG */
19039 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19040 of XChar2b structures for S; it can't be allocated in
19041 init_glyph_string because it must be allocated via `alloca'. W
19042 is the window on which S is drawn. ROW and AREA are the glyph row
19043 and area within the row from which S is constructed. START is the
19044 index of the first glyph structure covered by S. HL is a
19045 face-override for drawing S. */
19047 #ifdef HAVE_NTGUI
19048 #define OPTIONAL_HDC(hdc) hdc,
19049 #define DECLARE_HDC(hdc) HDC hdc;
19050 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19051 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19052 #endif
19054 #ifndef OPTIONAL_HDC
19055 #define OPTIONAL_HDC(hdc)
19056 #define DECLARE_HDC(hdc)
19057 #define ALLOCATE_HDC(hdc, f)
19058 #define RELEASE_HDC(hdc, f)
19059 #endif
19061 static void
19062 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19063 struct glyph_string *s;
19064 DECLARE_HDC (hdc)
19065 XChar2b *char2b;
19066 struct window *w;
19067 struct glyph_row *row;
19068 enum glyph_row_area area;
19069 int start;
19070 enum draw_glyphs_face hl;
19072 bzero (s, sizeof *s);
19073 s->w = w;
19074 s->f = XFRAME (w->frame);
19075 #ifdef HAVE_NTGUI
19076 s->hdc = hdc;
19077 #endif
19078 s->display = FRAME_X_DISPLAY (s->f);
19079 s->window = FRAME_X_WINDOW (s->f);
19080 s->char2b = char2b;
19081 s->hl = hl;
19082 s->row = row;
19083 s->area = area;
19084 s->first_glyph = row->glyphs[area] + start;
19085 s->height = row->height;
19086 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19088 /* Display the internal border below the tool-bar window. */
19089 if (WINDOWP (s->f->tool_bar_window)
19090 && s->w == XWINDOW (s->f->tool_bar_window))
19091 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19093 s->ybase = s->y + row->ascent;
19097 /* Append the list of glyph strings with head H and tail T to the list
19098 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19100 static INLINE void
19101 append_glyph_string_lists (head, tail, h, t)
19102 struct glyph_string **head, **tail;
19103 struct glyph_string *h, *t;
19105 if (h)
19107 if (*head)
19108 (*tail)->next = h;
19109 else
19110 *head = h;
19111 h->prev = *tail;
19112 *tail = t;
19117 /* Prepend the list of glyph strings with head H and tail T to the
19118 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19119 result. */
19121 static INLINE void
19122 prepend_glyph_string_lists (head, tail, h, t)
19123 struct glyph_string **head, **tail;
19124 struct glyph_string *h, *t;
19126 if (h)
19128 if (*head)
19129 (*head)->prev = t;
19130 else
19131 *tail = t;
19132 t->next = *head;
19133 *head = h;
19138 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19139 Set *HEAD and *TAIL to the resulting list. */
19141 static INLINE void
19142 append_glyph_string (head, tail, s)
19143 struct glyph_string **head, **tail;
19144 struct glyph_string *s;
19146 s->next = s->prev = NULL;
19147 append_glyph_string_lists (head, tail, s, s);
19151 /* Get face and two-byte form of character C in face FACE_ID on frame
19152 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19153 means we want to display multibyte text. DISPLAY_P non-zero means
19154 make sure that X resources for the face returned are allocated.
19155 Value is a pointer to a realized face that is ready for display if
19156 DISPLAY_P is non-zero. */
19158 static INLINE struct face *
19159 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19160 struct frame *f;
19161 int c, face_id;
19162 XChar2b *char2b;
19163 int multibyte_p, display_p;
19165 struct face *face = FACE_FROM_ID (f, face_id);
19167 if (face->font)
19169 unsigned code = face->font->driver->encode_char (face->font, c);
19171 if (code != FONT_INVALID_CODE)
19172 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19173 else
19174 STORE_XCHAR2B (char2b, 0, 0);
19177 /* Make sure X resources of the face are allocated. */
19178 #ifdef HAVE_X_WINDOWS
19179 if (display_p)
19180 #endif
19182 xassert (face != NULL);
19183 PREPARE_FACE_FOR_DISPLAY (f, face);
19186 return face;
19190 /* Get face and two-byte form of character glyph GLYPH on frame F.
19191 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19192 a pointer to a realized face that is ready for display. */
19194 static INLINE struct face *
19195 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19196 struct frame *f;
19197 struct glyph *glyph;
19198 XChar2b *char2b;
19199 int *two_byte_p;
19201 struct face *face;
19203 xassert (glyph->type == CHAR_GLYPH);
19204 face = FACE_FROM_ID (f, glyph->face_id);
19206 if (two_byte_p)
19207 *two_byte_p = 0;
19209 if (face->font)
19211 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19213 if (code != FONT_INVALID_CODE)
19214 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19215 else
19216 STORE_XCHAR2B (char2b, 0, code);
19219 /* Make sure X resources of the face are allocated. */
19220 xassert (face != NULL);
19221 PREPARE_FACE_FOR_DISPLAY (f, face);
19222 return face;
19226 /* Fill glyph string S with composition components specified by S->cmp.
19228 BASE_FACE is the base face of the composition.
19229 S->gidx is the index of the first component for S.
19231 OVERLAPS non-zero means S should draw the foreground only, and use
19232 its physical height for clipping. See also draw_glyphs.
19234 Value is the index of a component not in S. */
19236 static int
19237 fill_composite_glyph_string (s, base_face, overlaps)
19238 struct glyph_string *s;
19239 struct face *base_face;
19240 int overlaps;
19242 int i;
19244 xassert (s);
19246 s->for_overlaps = overlaps;
19248 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19250 Lisp_Object gstring
19251 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19252 s->cmp->hash_index * 2);
19254 s->face = base_face;
19255 s->font = base_face->font;
19256 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19258 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19259 unsigned code;
19260 XChar2b * store_pos;
19261 if (NILP (g))
19262 break;
19263 code = LGLYPH_CODE (g);
19264 store_pos = s->char2b + i;
19265 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19267 s->width = s->cmp->pixel_width;
19269 else
19271 /* For all glyphs of this composition, starting at the offset
19272 S->gidx, until we reach the end of the definition or encounter a
19273 glyph that requires the different face, add it to S. */
19274 struct face *face;
19276 s->face = NULL;
19277 s->font = NULL;
19278 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19280 int c = COMPOSITION_GLYPH (s->cmp, i);
19282 if (c != '\t')
19284 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19285 -1, Qnil);
19287 face = get_char_face_and_encoding (s->f, c, face_id,
19288 s->char2b + i, 1, 1);
19289 if (face)
19291 if (! s->face)
19293 s->face = face;
19294 s->font = s->face->font;
19296 else if (s->face != face)
19297 break;
19300 ++s->nchars;
19303 /* All glyph strings for the same composition has the same width,
19304 i.e. the width set for the first component of the composition. */
19305 s->width = s->first_glyph->pixel_width;
19308 /* If the specified font could not be loaded, use the frame's
19309 default font, but record the fact that we couldn't load it in
19310 the glyph string so that we can draw rectangles for the
19311 characters of the glyph string. */
19312 if (s->font == NULL)
19314 s->font_not_found_p = 1;
19315 s->font = FRAME_FONT (s->f);
19318 /* Adjust base line for subscript/superscript text. */
19319 s->ybase += s->first_glyph->voffset;
19321 /* This glyph string must always be drawn with 16-bit functions. */
19322 s->two_byte_p = 1;
19324 return s->gidx + s->nchars;
19328 /* Fill glyph string S from a sequence of character glyphs.
19330 FACE_ID is the face id of the string. START is the index of the
19331 first glyph to consider, END is the index of the last + 1.
19332 OVERLAPS non-zero means S should draw the foreground only, and use
19333 its physical height for clipping. See also draw_glyphs.
19335 Value is the index of the first glyph not in S. */
19337 static int
19338 fill_glyph_string (s, face_id, start, end, overlaps)
19339 struct glyph_string *s;
19340 int face_id;
19341 int start, end, overlaps;
19343 struct glyph *glyph, *last;
19344 int voffset;
19345 int glyph_not_available_p;
19347 xassert (s->f == XFRAME (s->w->frame));
19348 xassert (s->nchars == 0);
19349 xassert (start >= 0 && end > start);
19351 s->for_overlaps = overlaps,
19352 glyph = s->row->glyphs[s->area] + start;
19353 last = s->row->glyphs[s->area] + end;
19354 voffset = glyph->voffset;
19355 s->padding_p = glyph->padding_p;
19356 glyph_not_available_p = glyph->glyph_not_available_p;
19358 while (glyph < last
19359 && glyph->type == CHAR_GLYPH
19360 && glyph->voffset == voffset
19361 /* Same face id implies same font, nowadays. */
19362 && glyph->face_id == face_id
19363 && glyph->glyph_not_available_p == glyph_not_available_p)
19365 int two_byte_p;
19367 s->face = get_glyph_face_and_encoding (s->f, glyph,
19368 s->char2b + s->nchars,
19369 &two_byte_p);
19370 s->two_byte_p = two_byte_p;
19371 ++s->nchars;
19372 xassert (s->nchars <= end - start);
19373 s->width += glyph->pixel_width;
19374 if (glyph++->padding_p != s->padding_p)
19375 break;
19378 s->font = s->face->font;
19380 /* If the specified font could not be loaded, use the frame's font,
19381 but record the fact that we couldn't load it in
19382 S->font_not_found_p so that we can draw rectangles for the
19383 characters of the glyph string. */
19384 if (s->font == NULL || glyph_not_available_p)
19386 s->font_not_found_p = 1;
19387 s->font = FRAME_FONT (s->f);
19390 /* Adjust base line for subscript/superscript text. */
19391 s->ybase += voffset;
19393 xassert (s->face && s->face->gc);
19394 return glyph - s->row->glyphs[s->area];
19398 /* Fill glyph string S from image glyph S->first_glyph. */
19400 static void
19401 fill_image_glyph_string (s)
19402 struct glyph_string *s;
19404 xassert (s->first_glyph->type == IMAGE_GLYPH);
19405 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19406 xassert (s->img);
19407 s->slice = s->first_glyph->slice;
19408 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19409 s->font = s->face->font;
19410 s->width = s->first_glyph->pixel_width;
19412 /* Adjust base line for subscript/superscript text. */
19413 s->ybase += s->first_glyph->voffset;
19417 /* Fill glyph string S from a sequence of stretch glyphs.
19419 ROW is the glyph row in which the glyphs are found, AREA is the
19420 area within the row. START is the index of the first glyph to
19421 consider, END is the index of the last + 1.
19423 Value is the index of the first glyph not in S. */
19425 static int
19426 fill_stretch_glyph_string (s, row, area, start, end)
19427 struct glyph_string *s;
19428 struct glyph_row *row;
19429 enum glyph_row_area area;
19430 int start, end;
19432 struct glyph *glyph, *last;
19433 int voffset, face_id;
19435 xassert (s->first_glyph->type == STRETCH_GLYPH);
19437 glyph = s->row->glyphs[s->area] + start;
19438 last = s->row->glyphs[s->area] + end;
19439 face_id = glyph->face_id;
19440 s->face = FACE_FROM_ID (s->f, face_id);
19441 s->font = s->face->font;
19442 s->width = glyph->pixel_width;
19443 s->nchars = 1;
19444 voffset = glyph->voffset;
19446 for (++glyph;
19447 (glyph < last
19448 && glyph->type == STRETCH_GLYPH
19449 && glyph->voffset == voffset
19450 && glyph->face_id == face_id);
19451 ++glyph)
19452 s->width += glyph->pixel_width;
19454 /* Adjust base line for subscript/superscript text. */
19455 s->ybase += voffset;
19457 /* The case that face->gc == 0 is handled when drawing the glyph
19458 string by calling PREPARE_FACE_FOR_DISPLAY. */
19459 xassert (s->face);
19460 return glyph - s->row->glyphs[s->area];
19463 static struct font_metrics *
19464 get_per_char_metric (f, font, char2b)
19465 struct frame *f;
19466 struct font *font;
19467 XChar2b *char2b;
19469 static struct font_metrics metrics;
19470 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19471 struct font *fontp;
19473 if (! font || code == FONT_INVALID_CODE)
19474 return NULL;
19475 font->driver->text_extents (font, &code, 1, &metrics);
19476 return &metrics;
19479 /* EXPORT for RIF:
19480 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19481 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19482 assumed to be zero. */
19484 void
19485 x_get_glyph_overhangs (glyph, f, left, right)
19486 struct glyph *glyph;
19487 struct frame *f;
19488 int *left, *right;
19490 *left = *right = 0;
19492 if (glyph->type == CHAR_GLYPH)
19494 struct face *face;
19495 XChar2b char2b;
19496 struct font_metrics *pcm;
19498 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19499 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19501 if (pcm->rbearing > pcm->width)
19502 *right = pcm->rbearing - pcm->width;
19503 if (pcm->lbearing < 0)
19504 *left = -pcm->lbearing;
19507 else if (glyph->type == COMPOSITE_GLYPH)
19509 struct composition *cmp = composition_table[glyph->u.cmp_id];
19511 *right = cmp->rbearing - cmp->pixel_width;
19512 *left = - cmp->lbearing;
19517 /* Return the index of the first glyph preceding glyph string S that
19518 is overwritten by S because of S's left overhang. Value is -1
19519 if no glyphs are overwritten. */
19521 static int
19522 left_overwritten (s)
19523 struct glyph_string *s;
19525 int k;
19527 if (s->left_overhang)
19529 int x = 0, i;
19530 struct glyph *glyphs = s->row->glyphs[s->area];
19531 int first = s->first_glyph - glyphs;
19533 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19534 x -= glyphs[i].pixel_width;
19536 k = i + 1;
19538 else
19539 k = -1;
19541 return k;
19545 /* Return the index of the first glyph preceding glyph string S that
19546 is overwriting S because of its right overhang. Value is -1 if no
19547 glyph in front of S overwrites S. */
19549 static int
19550 left_overwriting (s)
19551 struct glyph_string *s;
19553 int i, k, x;
19554 struct glyph *glyphs = s->row->glyphs[s->area];
19555 int first = s->first_glyph - glyphs;
19557 k = -1;
19558 x = 0;
19559 for (i = first - 1; i >= 0; --i)
19561 int left, right;
19562 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19563 if (x + right > 0)
19564 k = i;
19565 x -= glyphs[i].pixel_width;
19568 return k;
19572 /* Return the index of the last glyph following glyph string S that is
19573 not overwritten by S because of S's right overhang. Value is -1 if
19574 no such glyph is found. */
19576 static int
19577 right_overwritten (s)
19578 struct glyph_string *s;
19580 int k = -1;
19582 if (s->right_overhang)
19584 int x = 0, i;
19585 struct glyph *glyphs = s->row->glyphs[s->area];
19586 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19587 int end = s->row->used[s->area];
19589 for (i = first; i < end && s->right_overhang > x; ++i)
19590 x += glyphs[i].pixel_width;
19592 k = i;
19595 return k;
19599 /* Return the index of the last glyph following glyph string S that
19600 overwrites S because of its left overhang. Value is negative
19601 if no such glyph is found. */
19603 static int
19604 right_overwriting (s)
19605 struct glyph_string *s;
19607 int i, k, x;
19608 int end = s->row->used[s->area];
19609 struct glyph *glyphs = s->row->glyphs[s->area];
19610 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19612 k = -1;
19613 x = 0;
19614 for (i = first; i < end; ++i)
19616 int left, right;
19617 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19618 if (x - left < 0)
19619 k = i;
19620 x += glyphs[i].pixel_width;
19623 return k;
19627 /* Set background width of glyph string S. START is the index of the
19628 first glyph following S. LAST_X is the right-most x-position + 1
19629 in the drawing area. */
19631 static INLINE void
19632 set_glyph_string_background_width (s, start, last_x)
19633 struct glyph_string *s;
19634 int start;
19635 int last_x;
19637 /* If the face of this glyph string has to be drawn to the end of
19638 the drawing area, set S->extends_to_end_of_line_p. */
19640 if (start == s->row->used[s->area]
19641 && s->area == TEXT_AREA
19642 && ((s->row->fill_line_p
19643 && (s->hl == DRAW_NORMAL_TEXT
19644 || s->hl == DRAW_IMAGE_RAISED
19645 || s->hl == DRAW_IMAGE_SUNKEN))
19646 || s->hl == DRAW_MOUSE_FACE))
19647 s->extends_to_end_of_line_p = 1;
19649 /* If S extends its face to the end of the line, set its
19650 background_width to the distance to the right edge of the drawing
19651 area. */
19652 if (s->extends_to_end_of_line_p)
19653 s->background_width = last_x - s->x + 1;
19654 else
19655 s->background_width = s->width;
19659 /* Compute overhangs and x-positions for glyph string S and its
19660 predecessors, or successors. X is the starting x-position for S.
19661 BACKWARD_P non-zero means process predecessors. */
19663 static void
19664 compute_overhangs_and_x (s, x, backward_p)
19665 struct glyph_string *s;
19666 int x;
19667 int backward_p;
19669 if (backward_p)
19671 while (s)
19673 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19674 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19675 x -= s->width;
19676 s->x = x;
19677 s = s->prev;
19680 else
19682 while (s)
19684 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19685 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19686 s->x = x;
19687 x += s->width;
19688 s = s->next;
19695 /* The following macros are only called from draw_glyphs below.
19696 They reference the following parameters of that function directly:
19697 `w', `row', `area', and `overlap_p'
19698 as well as the following local variables:
19699 `s', `f', and `hdc' (in W32) */
19701 #ifdef HAVE_NTGUI
19702 /* On W32, silently add local `hdc' variable to argument list of
19703 init_glyph_string. */
19704 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19705 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19706 #else
19707 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19708 init_glyph_string (s, char2b, w, row, area, start, hl)
19709 #endif
19711 /* Add a glyph string for a stretch glyph to the list of strings
19712 between HEAD and TAIL. START is the index of the stretch glyph in
19713 row area AREA of glyph row ROW. END is the index of the last glyph
19714 in that glyph row area. X is the current output position assigned
19715 to the new glyph string constructed. HL overrides that face of the
19716 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19717 is the right-most x-position of the drawing area. */
19719 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19720 and below -- keep them on one line. */
19721 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19722 do \
19724 s = (struct glyph_string *) alloca (sizeof *s); \
19725 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19726 START = fill_stretch_glyph_string (s, row, area, START, END); \
19727 append_glyph_string (&HEAD, &TAIL, s); \
19728 s->x = (X); \
19730 while (0)
19733 /* Add a glyph string for an image glyph to the list of strings
19734 between HEAD and TAIL. START is the index of the image glyph in
19735 row area AREA of glyph row ROW. END is the index of the last glyph
19736 in that glyph row area. X is the current output position assigned
19737 to the new glyph string constructed. HL overrides that face of the
19738 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19739 is the right-most x-position of the drawing area. */
19741 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19742 do \
19744 s = (struct glyph_string *) alloca (sizeof *s); \
19745 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19746 fill_image_glyph_string (s); \
19747 append_glyph_string (&HEAD, &TAIL, s); \
19748 ++START; \
19749 s->x = (X); \
19751 while (0)
19754 /* Add a glyph string for a sequence of character glyphs to the list
19755 of strings between HEAD and TAIL. START is the index of the first
19756 glyph in row area AREA of glyph row ROW that is part of the new
19757 glyph string. END is the index of the last glyph in that glyph row
19758 area. X is the current output position assigned to the new glyph
19759 string constructed. HL overrides that face of the glyph; e.g. it
19760 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19761 right-most x-position of the drawing area. */
19763 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19764 do \
19766 int face_id; \
19767 XChar2b *char2b; \
19769 face_id = (row)->glyphs[area][START].face_id; \
19771 s = (struct glyph_string *) alloca (sizeof *s); \
19772 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19773 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19774 append_glyph_string (&HEAD, &TAIL, s); \
19775 s->x = (X); \
19776 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19778 while (0)
19781 /* Add a glyph string for a composite sequence to the list of strings
19782 between HEAD and TAIL. START is the index of the first glyph in
19783 row area AREA of glyph row ROW that is part of the new glyph
19784 string. END is the index of the last glyph in that glyph row area.
19785 X is the current output position assigned to the new glyph string
19786 constructed. HL overrides that face of the glyph; e.g. it is
19787 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19788 x-position of the drawing area. */
19790 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19791 do { \
19792 int face_id = (row)->glyphs[area][START].face_id; \
19793 struct face *base_face = FACE_FROM_ID (f, face_id); \
19794 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19795 struct composition *cmp = composition_table[cmp_id]; \
19796 XChar2b *char2b; \
19797 struct glyph_string *first_s; \
19798 int n; \
19800 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19802 /* Make glyph_strings for each glyph sequence that is drawable by \
19803 the same face, and append them to HEAD/TAIL. */ \
19804 for (n = 0; n < cmp->glyph_len;) \
19806 s = (struct glyph_string *) alloca (sizeof *s); \
19807 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19808 append_glyph_string (&(HEAD), &(TAIL), s); \
19809 s->cmp = cmp; \
19810 s->gidx = n; \
19811 s->x = (X); \
19812 if (n == 0) \
19813 first_s = s; \
19814 n = fill_composite_glyph_string (s, base_face, overlaps); \
19817 ++START; \
19818 s = first_s; \
19819 } while (0)
19822 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19823 of AREA of glyph row ROW on window W between indices START and END.
19824 HL overrides the face for drawing glyph strings, e.g. it is
19825 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19826 x-positions of the drawing area.
19828 This is an ugly monster macro construct because we must use alloca
19829 to allocate glyph strings (because draw_glyphs can be called
19830 asynchronously). */
19832 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19833 do \
19835 HEAD = TAIL = NULL; \
19836 while (START < END) \
19838 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19839 switch (first_glyph->type) \
19841 case CHAR_GLYPH: \
19842 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19843 HL, X, LAST_X); \
19844 break; \
19846 case COMPOSITE_GLYPH: \
19847 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19848 HL, X, LAST_X); \
19849 break; \
19851 case STRETCH_GLYPH: \
19852 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19853 HL, X, LAST_X); \
19854 break; \
19856 case IMAGE_GLYPH: \
19857 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19858 HL, X, LAST_X); \
19859 break; \
19861 default: \
19862 abort (); \
19865 if (s) \
19867 set_glyph_string_background_width (s, START, LAST_X); \
19868 (X) += s->width; \
19872 while (0)
19875 /* Draw glyphs between START and END in AREA of ROW on window W,
19876 starting at x-position X. X is relative to AREA in W. HL is a
19877 face-override with the following meaning:
19879 DRAW_NORMAL_TEXT draw normally
19880 DRAW_CURSOR draw in cursor face
19881 DRAW_MOUSE_FACE draw in mouse face.
19882 DRAW_INVERSE_VIDEO draw in mode line face
19883 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19884 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19886 If OVERLAPS is non-zero, draw only the foreground of characters and
19887 clip to the physical height of ROW. Non-zero value also defines
19888 the overlapping part to be drawn:
19890 OVERLAPS_PRED overlap with preceding rows
19891 OVERLAPS_SUCC overlap with succeeding rows
19892 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19893 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19895 Value is the x-position reached, relative to AREA of W. */
19897 static int
19898 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19899 struct window *w;
19900 int x;
19901 struct glyph_row *row;
19902 enum glyph_row_area area;
19903 EMACS_INT start, end;
19904 enum draw_glyphs_face hl;
19905 int overlaps;
19907 struct glyph_string *head, *tail;
19908 struct glyph_string *s;
19909 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19910 int i, j, x_reached, last_x, area_left = 0;
19911 struct frame *f = XFRAME (WINDOW_FRAME (w));
19912 DECLARE_HDC (hdc);
19914 ALLOCATE_HDC (hdc, f);
19916 /* Let's rather be paranoid than getting a SEGV. */
19917 end = min (end, row->used[area]);
19918 start = max (0, start);
19919 start = min (end, start);
19921 /* Translate X to frame coordinates. Set last_x to the right
19922 end of the drawing area. */
19923 if (row->full_width_p)
19925 /* X is relative to the left edge of W, without scroll bars
19926 or fringes. */
19927 area_left = WINDOW_LEFT_EDGE_X (w);
19928 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19930 else
19932 area_left = window_box_left (w, area);
19933 last_x = area_left + window_box_width (w, area);
19935 x += area_left;
19937 /* Build a doubly-linked list of glyph_string structures between
19938 head and tail from what we have to draw. Note that the macro
19939 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19940 the reason we use a separate variable `i'. */
19941 i = start;
19942 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19943 if (tail)
19944 x_reached = tail->x + tail->background_width;
19945 else
19946 x_reached = x;
19948 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19949 the row, redraw some glyphs in front or following the glyph
19950 strings built above. */
19951 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19953 struct glyph_string *h, *t;
19954 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19955 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
19956 int dummy_x = 0;
19958 /* If mouse highlighting is on, we may need to draw adjacent
19959 glyphs using mouse-face highlighting. */
19960 if (area == TEXT_AREA && row->mouse_face_p)
19962 struct glyph_row *mouse_beg_row, *mouse_end_row;
19964 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19965 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19967 if (row >= mouse_beg_row && row <= mouse_end_row)
19969 check_mouse_face = 1;
19970 mouse_beg_col = (row == mouse_beg_row)
19971 ? dpyinfo->mouse_face_beg_col : 0;
19972 mouse_end_col = (row == mouse_end_row)
19973 ? dpyinfo->mouse_face_end_col
19974 : row->used[TEXT_AREA];
19978 /* Compute overhangs for all glyph strings. */
19979 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19980 for (s = head; s; s = s->next)
19981 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19983 /* Prepend glyph strings for glyphs in front of the first glyph
19984 string that are overwritten because of the first glyph
19985 string's left overhang. The background of all strings
19986 prepended must be drawn because the first glyph string
19987 draws over it. */
19988 i = left_overwritten (head);
19989 if (i >= 0)
19991 enum draw_glyphs_face overlap_hl;
19993 /* If this row contains mouse highlighting, attempt to draw
19994 the overlapped glyphs with the correct highlight. This
19995 code fails if the overlap encompasses more than one glyph
19996 and mouse-highlight spans only some of these glyphs.
19997 However, making it work perfectly involves a lot more
19998 code, and I don't know if the pathological case occurs in
19999 practice, so we'll stick to this for now. --- cyd */
20000 if (check_mouse_face
20001 && mouse_beg_col < start && mouse_end_col > i)
20002 overlap_hl = DRAW_MOUSE_FACE;
20003 else
20004 overlap_hl = DRAW_NORMAL_TEXT;
20006 j = i;
20007 BUILD_GLYPH_STRINGS (j, start, h, t,
20008 overlap_hl, dummy_x, last_x);
20009 compute_overhangs_and_x (t, head->x, 1);
20010 prepend_glyph_string_lists (&head, &tail, h, t);
20011 clip_head = head;
20014 /* Prepend glyph strings for glyphs in front of the first glyph
20015 string that overwrite that glyph string because of their
20016 right overhang. For these strings, only the foreground must
20017 be drawn, because it draws over the glyph string at `head'.
20018 The background must not be drawn because this would overwrite
20019 right overhangs of preceding glyphs for which no glyph
20020 strings exist. */
20021 i = left_overwriting (head);
20022 if (i >= 0)
20024 enum draw_glyphs_face overlap_hl;
20026 if (check_mouse_face
20027 && mouse_beg_col < start && mouse_end_col > i)
20028 overlap_hl = DRAW_MOUSE_FACE;
20029 else
20030 overlap_hl = DRAW_NORMAL_TEXT;
20032 clip_head = head;
20033 BUILD_GLYPH_STRINGS (i, start, h, t,
20034 overlap_hl, dummy_x, last_x);
20035 for (s = h; s; s = s->next)
20036 s->background_filled_p = 1;
20037 compute_overhangs_and_x (t, head->x, 1);
20038 prepend_glyph_string_lists (&head, &tail, h, t);
20041 /* Append glyphs strings for glyphs following the last glyph
20042 string tail that are overwritten by tail. The background of
20043 these strings has to be drawn because tail's foreground draws
20044 over it. */
20045 i = right_overwritten (tail);
20046 if (i >= 0)
20048 enum draw_glyphs_face overlap_hl;
20050 if (check_mouse_face
20051 && mouse_beg_col < i && mouse_end_col > end)
20052 overlap_hl = DRAW_MOUSE_FACE;
20053 else
20054 overlap_hl = DRAW_NORMAL_TEXT;
20056 BUILD_GLYPH_STRINGS (end, i, h, t,
20057 overlap_hl, x, last_x);
20058 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20059 append_glyph_string_lists (&head, &tail, h, t);
20060 clip_tail = tail;
20063 /* Append glyph strings for glyphs following the last glyph
20064 string tail that overwrite tail. The foreground of such
20065 glyphs has to be drawn because it writes into the background
20066 of tail. The background must not be drawn because it could
20067 paint over the foreground of following glyphs. */
20068 i = right_overwriting (tail);
20069 if (i >= 0)
20071 enum draw_glyphs_face overlap_hl;
20072 if (check_mouse_face
20073 && mouse_beg_col < i && mouse_end_col > end)
20074 overlap_hl = DRAW_MOUSE_FACE;
20075 else
20076 overlap_hl = DRAW_NORMAL_TEXT;
20078 clip_tail = tail;
20079 i++; /* We must include the Ith glyph. */
20080 BUILD_GLYPH_STRINGS (end, i, h, t,
20081 overlap_hl, x, last_x);
20082 for (s = h; s; s = s->next)
20083 s->background_filled_p = 1;
20084 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20085 append_glyph_string_lists (&head, &tail, h, t);
20087 if (clip_head || clip_tail)
20088 for (s = head; s; s = s->next)
20090 s->clip_head = clip_head;
20091 s->clip_tail = clip_tail;
20095 /* Draw all strings. */
20096 for (s = head; s; s = s->next)
20097 FRAME_RIF (f)->draw_glyph_string (s);
20099 if (area == TEXT_AREA
20100 && !row->full_width_p
20101 /* When drawing overlapping rows, only the glyph strings'
20102 foreground is drawn, which doesn't erase a cursor
20103 completely. */
20104 && !overlaps)
20106 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20107 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20108 : (tail ? tail->x + tail->background_width : x));
20109 x0 -= area_left;
20110 x1 -= area_left;
20112 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20113 row->y, MATRIX_ROW_BOTTOM_Y (row));
20116 /* Value is the x-position up to which drawn, relative to AREA of W.
20117 This doesn't include parts drawn because of overhangs. */
20118 if (row->full_width_p)
20119 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20120 else
20121 x_reached -= area_left;
20123 RELEASE_HDC (hdc, f);
20125 return x_reached;
20128 /* Expand row matrix if too narrow. Don't expand if area
20129 is not present. */
20131 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20133 if (!fonts_changed_p \
20134 && (it->glyph_row->glyphs[area] \
20135 < it->glyph_row->glyphs[area + 1])) \
20137 it->w->ncols_scale_factor++; \
20138 fonts_changed_p = 1; \
20142 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20143 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20145 static INLINE void
20146 append_glyph (it)
20147 struct it *it;
20149 struct glyph *glyph;
20150 enum glyph_row_area area = it->area;
20152 xassert (it->glyph_row);
20153 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20155 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20156 if (glyph < it->glyph_row->glyphs[area + 1])
20158 glyph->charpos = CHARPOS (it->position);
20159 glyph->object = it->object;
20160 if (it->pixel_width > 0)
20162 glyph->pixel_width = it->pixel_width;
20163 glyph->padding_p = 0;
20165 else
20167 /* Assure at least 1-pixel width. Otherwise, cursor can't
20168 be displayed correctly. */
20169 glyph->pixel_width = 1;
20170 glyph->padding_p = 1;
20172 glyph->ascent = it->ascent;
20173 glyph->descent = it->descent;
20174 glyph->voffset = it->voffset;
20175 glyph->type = CHAR_GLYPH;
20176 glyph->multibyte_p = it->multibyte_p;
20177 glyph->left_box_line_p = it->start_of_box_run_p;
20178 glyph->right_box_line_p = it->end_of_box_run_p;
20179 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20180 || it->phys_descent > it->descent);
20181 glyph->glyph_not_available_p = it->glyph_not_available_p;
20182 glyph->face_id = it->face_id;
20183 glyph->u.ch = it->char_to_display;
20184 glyph->slice = null_glyph_slice;
20185 glyph->font_type = FONT_TYPE_UNKNOWN;
20186 ++it->glyph_row->used[area];
20188 else
20189 IT_EXPAND_MATRIX_WIDTH (it, area);
20192 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20193 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20195 static INLINE void
20196 append_composite_glyph (it)
20197 struct it *it;
20199 struct glyph *glyph;
20200 enum glyph_row_area area = it->area;
20202 xassert (it->glyph_row);
20204 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20205 if (glyph < it->glyph_row->glyphs[area + 1])
20207 glyph->charpos = CHARPOS (it->position);
20208 glyph->object = it->object;
20209 glyph->pixel_width = it->pixel_width;
20210 glyph->ascent = it->ascent;
20211 glyph->descent = it->descent;
20212 glyph->voffset = it->voffset;
20213 glyph->type = COMPOSITE_GLYPH;
20214 glyph->multibyte_p = it->multibyte_p;
20215 glyph->left_box_line_p = it->start_of_box_run_p;
20216 glyph->right_box_line_p = it->end_of_box_run_p;
20217 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20218 || it->phys_descent > it->descent);
20219 glyph->padding_p = 0;
20220 glyph->glyph_not_available_p = 0;
20221 glyph->face_id = it->face_id;
20222 glyph->u.cmp_id = it->cmp_id;
20223 glyph->slice = null_glyph_slice;
20224 glyph->font_type = FONT_TYPE_UNKNOWN;
20225 ++it->glyph_row->used[area];
20227 else
20228 IT_EXPAND_MATRIX_WIDTH (it, area);
20232 /* Change IT->ascent and IT->height according to the setting of
20233 IT->voffset. */
20235 static INLINE void
20236 take_vertical_position_into_account (it)
20237 struct it *it;
20239 if (it->voffset)
20241 if (it->voffset < 0)
20242 /* Increase the ascent so that we can display the text higher
20243 in the line. */
20244 it->ascent -= it->voffset;
20245 else
20246 /* Increase the descent so that we can display the text lower
20247 in the line. */
20248 it->descent += it->voffset;
20253 /* Produce glyphs/get display metrics for the image IT is loaded with.
20254 See the description of struct display_iterator in dispextern.h for
20255 an overview of struct display_iterator. */
20257 static void
20258 produce_image_glyph (it)
20259 struct it *it;
20261 struct image *img;
20262 struct face *face;
20263 int glyph_ascent, crop;
20264 struct glyph_slice slice;
20266 xassert (it->what == IT_IMAGE);
20268 face = FACE_FROM_ID (it->f, it->face_id);
20269 xassert (face);
20270 /* Make sure X resources of the face is loaded. */
20271 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20273 if (it->image_id < 0)
20275 /* Fringe bitmap. */
20276 it->ascent = it->phys_ascent = 0;
20277 it->descent = it->phys_descent = 0;
20278 it->pixel_width = 0;
20279 it->nglyphs = 0;
20280 return;
20283 img = IMAGE_FROM_ID (it->f, it->image_id);
20284 xassert (img);
20285 /* Make sure X resources of the image is loaded. */
20286 prepare_image_for_display (it->f, img);
20288 slice.x = slice.y = 0;
20289 slice.width = img->width;
20290 slice.height = img->height;
20292 if (INTEGERP (it->slice.x))
20293 slice.x = XINT (it->slice.x);
20294 else if (FLOATP (it->slice.x))
20295 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20297 if (INTEGERP (it->slice.y))
20298 slice.y = XINT (it->slice.y);
20299 else if (FLOATP (it->slice.y))
20300 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20302 if (INTEGERP (it->slice.width))
20303 slice.width = XINT (it->slice.width);
20304 else if (FLOATP (it->slice.width))
20305 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20307 if (INTEGERP (it->slice.height))
20308 slice.height = XINT (it->slice.height);
20309 else if (FLOATP (it->slice.height))
20310 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20312 if (slice.x >= img->width)
20313 slice.x = img->width;
20314 if (slice.y >= img->height)
20315 slice.y = img->height;
20316 if (slice.x + slice.width >= img->width)
20317 slice.width = img->width - slice.x;
20318 if (slice.y + slice.height > img->height)
20319 slice.height = img->height - slice.y;
20321 if (slice.width == 0 || slice.height == 0)
20322 return;
20324 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20326 it->descent = slice.height - glyph_ascent;
20327 if (slice.y == 0)
20328 it->descent += img->vmargin;
20329 if (slice.y + slice.height == img->height)
20330 it->descent += img->vmargin;
20331 it->phys_descent = it->descent;
20333 it->pixel_width = slice.width;
20334 if (slice.x == 0)
20335 it->pixel_width += img->hmargin;
20336 if (slice.x + slice.width == img->width)
20337 it->pixel_width += img->hmargin;
20339 /* It's quite possible for images to have an ascent greater than
20340 their height, so don't get confused in that case. */
20341 if (it->descent < 0)
20342 it->descent = 0;
20344 #if 0 /* this breaks image tiling */
20345 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20346 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20347 if (face_ascent > it->ascent)
20348 it->ascent = it->phys_ascent = face_ascent;
20349 #endif
20351 it->nglyphs = 1;
20353 if (face->box != FACE_NO_BOX)
20355 if (face->box_line_width > 0)
20357 if (slice.y == 0)
20358 it->ascent += face->box_line_width;
20359 if (slice.y + slice.height == img->height)
20360 it->descent += face->box_line_width;
20363 if (it->start_of_box_run_p && slice.x == 0)
20364 it->pixel_width += eabs (face->box_line_width);
20365 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20366 it->pixel_width += eabs (face->box_line_width);
20369 take_vertical_position_into_account (it);
20371 /* Automatically crop wide image glyphs at right edge so we can
20372 draw the cursor on same display row. */
20373 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20374 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20376 it->pixel_width -= crop;
20377 slice.width -= crop;
20380 if (it->glyph_row)
20382 struct glyph *glyph;
20383 enum glyph_row_area area = it->area;
20385 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20386 if (glyph < it->glyph_row->glyphs[area + 1])
20388 glyph->charpos = CHARPOS (it->position);
20389 glyph->object = it->object;
20390 glyph->pixel_width = it->pixel_width;
20391 glyph->ascent = glyph_ascent;
20392 glyph->descent = it->descent;
20393 glyph->voffset = it->voffset;
20394 glyph->type = IMAGE_GLYPH;
20395 glyph->multibyte_p = it->multibyte_p;
20396 glyph->left_box_line_p = it->start_of_box_run_p;
20397 glyph->right_box_line_p = it->end_of_box_run_p;
20398 glyph->overlaps_vertically_p = 0;
20399 glyph->padding_p = 0;
20400 glyph->glyph_not_available_p = 0;
20401 glyph->face_id = it->face_id;
20402 glyph->u.img_id = img->id;
20403 glyph->slice = slice;
20404 glyph->font_type = FONT_TYPE_UNKNOWN;
20405 ++it->glyph_row->used[area];
20407 else
20408 IT_EXPAND_MATRIX_WIDTH (it, area);
20413 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20414 of the glyph, WIDTH and HEIGHT are the width and height of the
20415 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20417 static void
20418 append_stretch_glyph (it, object, width, height, ascent)
20419 struct it *it;
20420 Lisp_Object object;
20421 int width, height;
20422 int ascent;
20424 struct glyph *glyph;
20425 enum glyph_row_area area = it->area;
20427 xassert (ascent >= 0 && ascent <= height);
20429 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20430 if (glyph < it->glyph_row->glyphs[area + 1])
20432 glyph->charpos = CHARPOS (it->position);
20433 glyph->object = object;
20434 glyph->pixel_width = width;
20435 glyph->ascent = ascent;
20436 glyph->descent = height - ascent;
20437 glyph->voffset = it->voffset;
20438 glyph->type = STRETCH_GLYPH;
20439 glyph->multibyte_p = it->multibyte_p;
20440 glyph->left_box_line_p = it->start_of_box_run_p;
20441 glyph->right_box_line_p = it->end_of_box_run_p;
20442 glyph->overlaps_vertically_p = 0;
20443 glyph->padding_p = 0;
20444 glyph->glyph_not_available_p = 0;
20445 glyph->face_id = it->face_id;
20446 glyph->u.stretch.ascent = ascent;
20447 glyph->u.stretch.height = height;
20448 glyph->slice = null_glyph_slice;
20449 glyph->font_type = FONT_TYPE_UNKNOWN;
20450 ++it->glyph_row->used[area];
20452 else
20453 IT_EXPAND_MATRIX_WIDTH (it, area);
20457 /* Produce a stretch glyph for iterator IT. IT->object is the value
20458 of the glyph property displayed. The value must be a list
20459 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20460 being recognized:
20462 1. `:width WIDTH' specifies that the space should be WIDTH *
20463 canonical char width wide. WIDTH may be an integer or floating
20464 point number.
20466 2. `:relative-width FACTOR' specifies that the width of the stretch
20467 should be computed from the width of the first character having the
20468 `glyph' property, and should be FACTOR times that width.
20470 3. `:align-to HPOS' specifies that the space should be wide enough
20471 to reach HPOS, a value in canonical character units.
20473 Exactly one of the above pairs must be present.
20475 4. `:height HEIGHT' specifies that the height of the stretch produced
20476 should be HEIGHT, measured in canonical character units.
20478 5. `:relative-height FACTOR' specifies that the height of the
20479 stretch should be FACTOR times the height of the characters having
20480 the glyph property.
20482 Either none or exactly one of 4 or 5 must be present.
20484 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20485 of the stretch should be used for the ascent of the stretch.
20486 ASCENT must be in the range 0 <= ASCENT <= 100. */
20488 static void
20489 produce_stretch_glyph (it)
20490 struct it *it;
20492 /* (space :width WIDTH :height HEIGHT ...) */
20493 Lisp_Object prop, plist;
20494 int width = 0, height = 0, align_to = -1;
20495 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20496 int ascent = 0;
20497 double tem;
20498 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20499 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20501 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20503 /* List should start with `space'. */
20504 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20505 plist = XCDR (it->object);
20507 /* Compute the width of the stretch. */
20508 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20509 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20511 /* Absolute width `:width WIDTH' specified and valid. */
20512 zero_width_ok_p = 1;
20513 width = (int)tem;
20515 else if (prop = Fplist_get (plist, QCrelative_width),
20516 NUMVAL (prop) > 0)
20518 /* Relative width `:relative-width FACTOR' specified and valid.
20519 Compute the width of the characters having the `glyph'
20520 property. */
20521 struct it it2;
20522 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20524 it2 = *it;
20525 if (it->multibyte_p)
20527 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20528 - IT_BYTEPOS (*it));
20529 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20531 else
20532 it2.c = *p, it2.len = 1;
20534 it2.glyph_row = NULL;
20535 it2.what = IT_CHARACTER;
20536 x_produce_glyphs (&it2);
20537 width = NUMVAL (prop) * it2.pixel_width;
20539 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20540 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20542 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20543 align_to = (align_to < 0
20545 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20546 else if (align_to < 0)
20547 align_to = window_box_left_offset (it->w, TEXT_AREA);
20548 width = max (0, (int)tem + align_to - it->current_x);
20549 zero_width_ok_p = 1;
20551 else
20552 /* Nothing specified -> width defaults to canonical char width. */
20553 width = FRAME_COLUMN_WIDTH (it->f);
20555 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20556 width = 1;
20558 /* Compute height. */
20559 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20560 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20562 height = (int)tem;
20563 zero_height_ok_p = 1;
20565 else if (prop = Fplist_get (plist, QCrelative_height),
20566 NUMVAL (prop) > 0)
20567 height = FONT_HEIGHT (font) * NUMVAL (prop);
20568 else
20569 height = FONT_HEIGHT (font);
20571 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20572 height = 1;
20574 /* Compute percentage of height used for ascent. If
20575 `:ascent ASCENT' is present and valid, use that. Otherwise,
20576 derive the ascent from the font in use. */
20577 if (prop = Fplist_get (plist, QCascent),
20578 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20579 ascent = height * NUMVAL (prop) / 100.0;
20580 else if (!NILP (prop)
20581 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20582 ascent = min (max (0, (int)tem), height);
20583 else
20584 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20586 if (width > 0 && !it->truncate_lines_p
20587 && it->current_x + width > it->last_visible_x)
20588 width = it->last_visible_x - it->current_x - 1;
20590 if (width > 0 && height > 0 && it->glyph_row)
20592 Lisp_Object object = it->stack[it->sp - 1].string;
20593 if (!STRINGP (object))
20594 object = it->w->buffer;
20595 append_stretch_glyph (it, object, width, height, ascent);
20598 it->pixel_width = width;
20599 it->ascent = it->phys_ascent = ascent;
20600 it->descent = it->phys_descent = height - it->ascent;
20601 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20603 take_vertical_position_into_account (it);
20606 /* Get line-height and line-spacing property at point.
20607 If line-height has format (HEIGHT TOTAL), return TOTAL
20608 in TOTAL_HEIGHT. */
20610 static Lisp_Object
20611 get_line_height_property (it, prop)
20612 struct it *it;
20613 Lisp_Object prop;
20615 Lisp_Object position;
20617 if (STRINGP (it->object))
20618 position = make_number (IT_STRING_CHARPOS (*it));
20619 else if (BUFFERP (it->object))
20620 position = make_number (IT_CHARPOS (*it));
20621 else
20622 return Qnil;
20624 return Fget_char_property (position, prop, it->object);
20627 /* Calculate line-height and line-spacing properties.
20628 An integer value specifies explicit pixel value.
20629 A float value specifies relative value to current face height.
20630 A cons (float . face-name) specifies relative value to
20631 height of specified face font.
20633 Returns height in pixels, or nil. */
20636 static Lisp_Object
20637 calc_line_height_property (it, val, font, boff, override)
20638 struct it *it;
20639 Lisp_Object val;
20640 struct font *font;
20641 int boff, override;
20643 Lisp_Object face_name = Qnil;
20644 int ascent, descent, height;
20646 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20647 return val;
20649 if (CONSP (val))
20651 face_name = XCAR (val);
20652 val = XCDR (val);
20653 if (!NUMBERP (val))
20654 val = make_number (1);
20655 if (NILP (face_name))
20657 height = it->ascent + it->descent;
20658 goto scale;
20662 if (NILP (face_name))
20664 font = FRAME_FONT (it->f);
20665 boff = FRAME_BASELINE_OFFSET (it->f);
20667 else if (EQ (face_name, Qt))
20669 override = 0;
20671 else
20673 int face_id;
20674 struct face *face;
20676 face_id = lookup_named_face (it->f, face_name, 0);
20677 if (face_id < 0)
20678 return make_number (-1);
20680 face = FACE_FROM_ID (it->f, face_id);
20681 font = face->font;
20682 if (font == NULL)
20683 return make_number (-1);
20684 boff = font->baseline_offset;
20685 if (font->vertical_centering)
20686 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20689 ascent = FONT_BASE (font) + boff;
20690 descent = FONT_DESCENT (font) - boff;
20692 if (override)
20694 it->override_ascent = ascent;
20695 it->override_descent = descent;
20696 it->override_boff = boff;
20699 height = ascent + descent;
20701 scale:
20702 if (FLOATP (val))
20703 height = (int)(XFLOAT_DATA (val) * height);
20704 else if (INTEGERP (val))
20705 height *= XINT (val);
20707 return make_number (height);
20711 /* RIF:
20712 Produce glyphs/get display metrics for the display element IT is
20713 loaded with. See the description of struct it in dispextern.h
20714 for an overview of struct it. */
20716 void
20717 x_produce_glyphs (it)
20718 struct it *it;
20720 int extra_line_spacing = it->extra_line_spacing;
20722 it->glyph_not_available_p = 0;
20724 if (it->what == IT_CHARACTER)
20726 XChar2b char2b;
20727 struct font *font;
20728 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20729 struct font_metrics *pcm;
20730 int font_not_found_p;
20731 int boff; /* baseline offset */
20732 /* We may change it->multibyte_p upon unibyte<->multibyte
20733 conversion. So, save the current value now and restore it
20734 later.
20736 Note: It seems that we don't have to record multibyte_p in
20737 struct glyph because the character code itself tells if or
20738 not the character is multibyte. Thus, in the future, we must
20739 consider eliminating the field `multibyte_p' in the struct
20740 glyph. */
20741 int saved_multibyte_p = it->multibyte_p;
20743 /* Maybe translate single-byte characters to multibyte, or the
20744 other way. */
20745 it->char_to_display = it->c;
20746 if (!ASCII_BYTE_P (it->c)
20747 && ! it->multibyte_p)
20749 if (SINGLE_BYTE_CHAR_P (it->c)
20750 && unibyte_display_via_language_environment)
20751 it->char_to_display = unibyte_char_to_multibyte (it->c);
20752 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20754 it->multibyte_p = 1;
20755 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20756 -1, Qnil);
20757 face = FACE_FROM_ID (it->f, it->face_id);
20761 /* Get font to use. Encode IT->char_to_display. */
20762 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20763 &char2b, it->multibyte_p, 0);
20764 font = face->font;
20766 /* When no suitable font found, use the default font. */
20767 font_not_found_p = font == NULL;
20768 if (font_not_found_p)
20770 font = FRAME_FONT (it->f);
20771 boff = FRAME_BASELINE_OFFSET (it->f);
20773 else
20775 boff = font->baseline_offset;
20776 if (font->vertical_centering)
20777 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20780 if (it->char_to_display >= ' '
20781 && (!it->multibyte_p || it->char_to_display < 128))
20783 /* Either unibyte or ASCII. */
20784 int stretched_p;
20786 it->nglyphs = 1;
20788 pcm = get_per_char_metric (it->f, font, &char2b);
20790 if (it->override_ascent >= 0)
20792 it->ascent = it->override_ascent;
20793 it->descent = it->override_descent;
20794 boff = it->override_boff;
20796 else
20798 it->ascent = FONT_BASE (font) + boff;
20799 it->descent = FONT_DESCENT (font) - boff;
20802 if (pcm)
20804 it->phys_ascent = pcm->ascent + boff;
20805 it->phys_descent = pcm->descent - boff;
20806 it->pixel_width = pcm->width;
20808 else
20810 it->glyph_not_available_p = 1;
20811 it->phys_ascent = it->ascent;
20812 it->phys_descent = it->descent;
20813 it->pixel_width = FONT_WIDTH (font);
20816 if (it->constrain_row_ascent_descent_p)
20818 if (it->descent > it->max_descent)
20820 it->ascent += it->descent - it->max_descent;
20821 it->descent = it->max_descent;
20823 if (it->ascent > it->max_ascent)
20825 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20826 it->ascent = it->max_ascent;
20828 it->phys_ascent = min (it->phys_ascent, it->ascent);
20829 it->phys_descent = min (it->phys_descent, it->descent);
20830 extra_line_spacing = 0;
20833 /* If this is a space inside a region of text with
20834 `space-width' property, change its width. */
20835 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20836 if (stretched_p)
20837 it->pixel_width *= XFLOATINT (it->space_width);
20839 /* If face has a box, add the box thickness to the character
20840 height. If character has a box line to the left and/or
20841 right, add the box line width to the character's width. */
20842 if (face->box != FACE_NO_BOX)
20844 int thick = face->box_line_width;
20846 if (thick > 0)
20848 it->ascent += thick;
20849 it->descent += thick;
20851 else
20852 thick = -thick;
20854 if (it->start_of_box_run_p)
20855 it->pixel_width += thick;
20856 if (it->end_of_box_run_p)
20857 it->pixel_width += thick;
20860 /* If face has an overline, add the height of the overline
20861 (1 pixel) and a 1 pixel margin to the character height. */
20862 if (face->overline_p)
20863 it->ascent += overline_margin;
20865 if (it->constrain_row_ascent_descent_p)
20867 if (it->ascent > it->max_ascent)
20868 it->ascent = it->max_ascent;
20869 if (it->descent > it->max_descent)
20870 it->descent = it->max_descent;
20873 take_vertical_position_into_account (it);
20875 /* If we have to actually produce glyphs, do it. */
20876 if (it->glyph_row)
20878 if (stretched_p)
20880 /* Translate a space with a `space-width' property
20881 into a stretch glyph. */
20882 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20883 / FONT_HEIGHT (font));
20884 append_stretch_glyph (it, it->object, it->pixel_width,
20885 it->ascent + it->descent, ascent);
20887 else
20888 append_glyph (it);
20890 /* If characters with lbearing or rbearing are displayed
20891 in this line, record that fact in a flag of the
20892 glyph row. This is used to optimize X output code. */
20893 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20894 it->glyph_row->contains_overlapping_glyphs_p = 1;
20896 if (! stretched_p && it->pixel_width == 0)
20897 /* We assure that all visible glyphs have at least 1-pixel
20898 width. */
20899 it->pixel_width = 1;
20901 else if (it->char_to_display == '\n')
20903 /* A newline has no width but we need the height of the line.
20904 But if previous part of the line set a height, don't
20905 increase that height */
20907 Lisp_Object height;
20908 Lisp_Object total_height = Qnil;
20910 it->override_ascent = -1;
20911 it->pixel_width = 0;
20912 it->nglyphs = 0;
20914 height = get_line_height_property(it, Qline_height);
20915 /* Split (line-height total-height) list */
20916 if (CONSP (height)
20917 && CONSP (XCDR (height))
20918 && NILP (XCDR (XCDR (height))))
20920 total_height = XCAR (XCDR (height));
20921 height = XCAR (height);
20923 height = calc_line_height_property(it, height, font, boff, 1);
20925 if (it->override_ascent >= 0)
20927 it->ascent = it->override_ascent;
20928 it->descent = it->override_descent;
20929 boff = it->override_boff;
20931 else
20933 it->ascent = FONT_BASE (font) + boff;
20934 it->descent = FONT_DESCENT (font) - boff;
20937 if (EQ (height, Qt))
20939 if (it->descent > it->max_descent)
20941 it->ascent += it->descent - it->max_descent;
20942 it->descent = it->max_descent;
20944 if (it->ascent > it->max_ascent)
20946 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20947 it->ascent = it->max_ascent;
20949 it->phys_ascent = min (it->phys_ascent, it->ascent);
20950 it->phys_descent = min (it->phys_descent, it->descent);
20951 it->constrain_row_ascent_descent_p = 1;
20952 extra_line_spacing = 0;
20954 else
20956 Lisp_Object spacing;
20958 it->phys_ascent = it->ascent;
20959 it->phys_descent = it->descent;
20961 if ((it->max_ascent > 0 || it->max_descent > 0)
20962 && face->box != FACE_NO_BOX
20963 && face->box_line_width > 0)
20965 it->ascent += face->box_line_width;
20966 it->descent += face->box_line_width;
20968 if (!NILP (height)
20969 && XINT (height) > it->ascent + it->descent)
20970 it->ascent = XINT (height) - it->descent;
20972 if (!NILP (total_height))
20973 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20974 else
20976 spacing = get_line_height_property(it, Qline_spacing);
20977 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20979 if (INTEGERP (spacing))
20981 extra_line_spacing = XINT (spacing);
20982 if (!NILP (total_height))
20983 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20987 else if (it->char_to_display == '\t')
20989 int tab_width = it->tab_width * font->space_width;
20990 int x = it->current_x + it->continuation_lines_width;
20991 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20993 /* If the distance from the current position to the next tab
20994 stop is less than a space character width, use the
20995 tab stop after that. */
20996 if (next_tab_x - x < font->space_width)
20997 next_tab_x += tab_width;
20999 it->pixel_width = next_tab_x - x;
21000 it->nglyphs = 1;
21001 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21002 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21004 if (it->glyph_row)
21006 append_stretch_glyph (it, it->object, it->pixel_width,
21007 it->ascent + it->descent, it->ascent);
21010 else
21012 /* A multi-byte character. Assume that the display width of the
21013 character is the width of the character multiplied by the
21014 width of the font. */
21016 /* If we found a font, this font should give us the right
21017 metrics. If we didn't find a font, use the frame's
21018 default font and calculate the width of the character by
21019 multiplying the width of font by the width of the
21020 character. */
21022 pcm = get_per_char_metric (it->f, font, &char2b);
21024 if (font_not_found_p || !pcm)
21026 int char_width = CHAR_WIDTH (it->char_to_display);
21028 if (char_width == 0)
21029 /* This is a non spacing character. But, as we are
21030 going to display an empty box, the box must occupy
21031 at least one column. */
21032 char_width = 1;
21033 it->glyph_not_available_p = 1;
21034 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21035 it->phys_ascent = FONT_BASE (font) + boff;
21036 it->phys_descent = FONT_DESCENT (font) - boff;
21038 else
21040 it->pixel_width = pcm->width;
21041 it->phys_ascent = pcm->ascent + boff;
21042 it->phys_descent = pcm->descent - boff;
21043 if (it->glyph_row
21044 && (pcm->lbearing < 0
21045 || pcm->rbearing > pcm->width))
21046 it->glyph_row->contains_overlapping_glyphs_p = 1;
21048 it->nglyphs = 1;
21049 it->ascent = FONT_BASE (font) + boff;
21050 it->descent = FONT_DESCENT (font) - boff;
21051 if (face->box != FACE_NO_BOX)
21053 int thick = face->box_line_width;
21055 if (thick > 0)
21057 it->ascent += thick;
21058 it->descent += thick;
21060 else
21061 thick = - thick;
21063 if (it->start_of_box_run_p)
21064 it->pixel_width += thick;
21065 if (it->end_of_box_run_p)
21066 it->pixel_width += thick;
21069 /* If face has an overline, add the height of the overline
21070 (1 pixel) and a 1 pixel margin to the character height. */
21071 if (face->overline_p)
21072 it->ascent += overline_margin;
21074 take_vertical_position_into_account (it);
21076 if (it->ascent < 0)
21077 it->ascent = 0;
21078 if (it->descent < 0)
21079 it->descent = 0;
21081 if (it->glyph_row)
21082 append_glyph (it);
21083 if (it->pixel_width == 0)
21084 /* We assure that all visible glyphs have at least 1-pixel
21085 width. */
21086 it->pixel_width = 1;
21088 it->multibyte_p = saved_multibyte_p;
21090 else if (it->what == IT_COMPOSITION)
21092 /* Note: A composition is represented as one glyph in the
21093 glyph matrix. There are no padding glyphs.
21095 Important is that pixel_width, ascent, and descent are the
21096 values of what is drawn by draw_glyphs (i.e. the values of
21097 the overall glyphs composed). */
21098 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21099 int boff; /* baseline offset */
21100 struct composition *cmp = composition_table[it->cmp_id];
21101 int glyph_len = cmp->glyph_len;
21102 struct font *font = face->font;
21104 it->nglyphs = 1;
21106 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21108 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21109 font_prepare_composition (cmp, it->f);
21111 else
21112 /* If we have not yet calculated pixel size data of glyphs of
21113 the composition for the current face font, calculate them
21114 now. Theoretically, we have to check all fonts for the
21115 glyphs, but that requires much time and memory space. So,
21116 here we check only the font of the first glyph. This leads
21117 to incorrect display, but it's very rare, and C-l (recenter)
21118 can correct the display anyway. */
21119 if (! cmp->font || cmp->font != font)
21121 /* Ascent and descent of the font of the first character
21122 of this composition (adjusted by baseline offset).
21123 Ascent and descent of overall glyphs should not be less
21124 than them respectively. */
21125 int font_ascent, font_descent, font_height;
21126 /* Bounding box of the overall glyphs. */
21127 int leftmost, rightmost, lowest, highest;
21128 int lbearing, rbearing;
21129 int i, width, ascent, descent;
21130 int left_padded = 0, right_padded = 0;
21131 int face_id;
21132 int c;
21133 XChar2b char2b;
21134 struct font_metrics *pcm;
21135 int font_not_found_p;
21136 int pos;
21138 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21139 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21140 break;
21141 if (glyph_len < cmp->glyph_len)
21142 right_padded = 1;
21143 for (i = 0; i < glyph_len; i++)
21145 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21146 break;
21147 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21149 if (i > 0)
21150 left_padded = 1;
21152 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21153 : IT_CHARPOS (*it));
21154 /* When no suitable font found, use the default font. */
21155 font_not_found_p = font == NULL;
21156 if (font_not_found_p)
21158 face = face->ascii_face;
21159 font = face->font;
21161 boff = font->baseline_offset;
21162 if (font->vertical_centering)
21163 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21164 font_ascent = FONT_BASE (font) + boff;
21165 font_descent = FONT_DESCENT (font) - boff;
21166 font_height = FONT_HEIGHT (font);
21168 cmp->font = (void *) font;
21170 pcm = NULL;
21171 if (! font_not_found_p)
21173 get_char_face_and_encoding (it->f, c, it->face_id,
21174 &char2b, it->multibyte_p, 0);
21175 pcm = get_per_char_metric (it->f, font, &char2b);
21178 /* Initialize the bounding box. */
21179 if (pcm)
21181 width = pcm->width;
21182 ascent = pcm->ascent;
21183 descent = pcm->descent;
21184 lbearing = pcm->lbearing;
21185 rbearing = pcm->rbearing;
21187 else
21189 width = FONT_WIDTH (font);
21190 ascent = FONT_BASE (font);
21191 descent = FONT_DESCENT (font);
21192 lbearing = 0;
21193 rbearing = width;
21196 rightmost = width;
21197 leftmost = 0;
21198 lowest = - descent + boff;
21199 highest = ascent + boff;
21201 if (! font_not_found_p
21202 && font->default_ascent
21203 && CHAR_TABLE_P (Vuse_default_ascent)
21204 && !NILP (Faref (Vuse_default_ascent,
21205 make_number (it->char_to_display))))
21206 highest = font->default_ascent + boff;
21208 /* Draw the first glyph at the normal position. It may be
21209 shifted to right later if some other glyphs are drawn
21210 at the left. */
21211 cmp->offsets[i * 2] = 0;
21212 cmp->offsets[i * 2 + 1] = boff;
21213 cmp->lbearing = lbearing;
21214 cmp->rbearing = rbearing;
21216 /* Set cmp->offsets for the remaining glyphs. */
21217 for (i++; i < glyph_len; i++)
21219 int left, right, btm, top;
21220 int ch = COMPOSITION_GLYPH (cmp, i);
21221 int face_id;
21222 struct face *this_face;
21223 int this_boff;
21225 if (ch == '\t')
21226 ch = ' ';
21227 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21228 this_face = FACE_FROM_ID (it->f, face_id);
21229 font = this_face->font;
21231 if (font == NULL)
21232 pcm = NULL;
21233 else
21235 this_boff = font->baseline_offset;
21236 if (font->vertical_centering)
21237 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21238 get_char_face_and_encoding (it->f, ch, face_id,
21239 &char2b, it->multibyte_p, 0);
21240 pcm = get_per_char_metric (it->f, font, &char2b);
21242 if (! pcm)
21243 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21244 else
21246 width = pcm->width;
21247 ascent = pcm->ascent;
21248 descent = pcm->descent;
21249 lbearing = pcm->lbearing;
21250 rbearing = pcm->rbearing;
21251 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21253 /* Relative composition with or without
21254 alternate chars. */
21255 left = (leftmost + rightmost - width) / 2;
21256 btm = - descent + boff;
21257 if (font->relative_compose
21258 && (! CHAR_TABLE_P (Vignore_relative_composition)
21259 || NILP (Faref (Vignore_relative_composition,
21260 make_number (ch)))))
21263 if (- descent >= font->relative_compose)
21264 /* One extra pixel between two glyphs. */
21265 btm = highest + 1;
21266 else if (ascent <= 0)
21267 /* One extra pixel between two glyphs. */
21268 btm = lowest - 1 - ascent - descent;
21271 else
21273 /* A composition rule is specified by an integer
21274 value that encodes global and new reference
21275 points (GREF and NREF). GREF and NREF are
21276 specified by numbers as below:
21278 0---1---2 -- ascent
21282 9--10--11 -- center
21284 ---3---4---5--- baseline
21286 6---7---8 -- descent
21288 int rule = COMPOSITION_RULE (cmp, i);
21289 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21291 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21292 grefx = gref % 3, nrefx = nref % 3;
21293 grefy = gref / 3, nrefy = nref / 3;
21294 if (xoff)
21295 xoff = font_height * (xoff - 128) / 256;
21296 if (yoff)
21297 yoff = font_height * (yoff - 128) / 256;
21299 left = (leftmost
21300 + grefx * (rightmost - leftmost) / 2
21301 - nrefx * width / 2
21302 + xoff);
21304 btm = ((grefy == 0 ? highest
21305 : grefy == 1 ? 0
21306 : grefy == 2 ? lowest
21307 : (highest + lowest) / 2)
21308 - (nrefy == 0 ? ascent + descent
21309 : nrefy == 1 ? descent - boff
21310 : nrefy == 2 ? 0
21311 : (ascent + descent) / 2)
21312 + yoff);
21315 cmp->offsets[i * 2] = left;
21316 cmp->offsets[i * 2 + 1] = btm + descent;
21318 /* Update the bounding box of the overall glyphs. */
21319 if (width > 0)
21321 right = left + width;
21322 if (left < leftmost)
21323 leftmost = left;
21324 if (right > rightmost)
21325 rightmost = right;
21327 top = btm + descent + ascent;
21328 if (top > highest)
21329 highest = top;
21330 if (btm < lowest)
21331 lowest = btm;
21333 if (cmp->lbearing > left + lbearing)
21334 cmp->lbearing = left + lbearing;
21335 if (cmp->rbearing < left + rbearing)
21336 cmp->rbearing = left + rbearing;
21340 /* If there are glyphs whose x-offsets are negative,
21341 shift all glyphs to the right and make all x-offsets
21342 non-negative. */
21343 if (leftmost < 0)
21345 for (i = 0; i < cmp->glyph_len; i++)
21346 cmp->offsets[i * 2] -= leftmost;
21347 rightmost -= leftmost;
21348 cmp->lbearing -= leftmost;
21349 cmp->rbearing -= leftmost;
21352 if (left_padded && cmp->lbearing < 0)
21354 for (i = 0; i < cmp->glyph_len; i++)
21355 cmp->offsets[i * 2] -= cmp->lbearing;
21356 rightmost -= cmp->lbearing;
21357 cmp->rbearing -= cmp->lbearing;
21358 cmp->lbearing = 0;
21360 if (right_padded && rightmost < cmp->rbearing)
21362 rightmost = cmp->rbearing;
21365 cmp->pixel_width = rightmost;
21366 cmp->ascent = highest;
21367 cmp->descent = - lowest;
21368 if (cmp->ascent < font_ascent)
21369 cmp->ascent = font_ascent;
21370 if (cmp->descent < font_descent)
21371 cmp->descent = font_descent;
21374 if (it->glyph_row
21375 && (cmp->lbearing < 0
21376 || cmp->rbearing > cmp->pixel_width))
21377 it->glyph_row->contains_overlapping_glyphs_p = 1;
21379 it->pixel_width = cmp->pixel_width;
21380 it->ascent = it->phys_ascent = cmp->ascent;
21381 it->descent = it->phys_descent = cmp->descent;
21382 if (face->box != FACE_NO_BOX)
21384 int thick = face->box_line_width;
21386 if (thick > 0)
21388 it->ascent += thick;
21389 it->descent += thick;
21391 else
21392 thick = - thick;
21394 if (it->start_of_box_run_p)
21395 it->pixel_width += thick;
21396 if (it->end_of_box_run_p)
21397 it->pixel_width += thick;
21400 /* If face has an overline, add the height of the overline
21401 (1 pixel) and a 1 pixel margin to the character height. */
21402 if (face->overline_p)
21403 it->ascent += overline_margin;
21405 take_vertical_position_into_account (it);
21406 if (it->ascent < 0)
21407 it->ascent = 0;
21408 if (it->descent < 0)
21409 it->descent = 0;
21411 if (it->glyph_row)
21412 append_composite_glyph (it);
21414 else if (it->what == IT_IMAGE)
21415 produce_image_glyph (it);
21416 else if (it->what == IT_STRETCH)
21417 produce_stretch_glyph (it);
21419 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21420 because this isn't true for images with `:ascent 100'. */
21421 xassert (it->ascent >= 0 && it->descent >= 0);
21422 if (it->area == TEXT_AREA)
21423 it->current_x += it->pixel_width;
21425 if (extra_line_spacing > 0)
21427 it->descent += extra_line_spacing;
21428 if (extra_line_spacing > it->max_extra_line_spacing)
21429 it->max_extra_line_spacing = extra_line_spacing;
21432 it->max_ascent = max (it->max_ascent, it->ascent);
21433 it->max_descent = max (it->max_descent, it->descent);
21434 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21435 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21438 /* EXPORT for RIF:
21439 Output LEN glyphs starting at START at the nominal cursor position.
21440 Advance the nominal cursor over the text. The global variable
21441 updated_window contains the window being updated, updated_row is
21442 the glyph row being updated, and updated_area is the area of that
21443 row being updated. */
21445 void
21446 x_write_glyphs (start, len)
21447 struct glyph *start;
21448 int len;
21450 int x, hpos;
21452 xassert (updated_window && updated_row);
21453 BLOCK_INPUT;
21455 /* Write glyphs. */
21457 hpos = start - updated_row->glyphs[updated_area];
21458 x = draw_glyphs (updated_window, output_cursor.x,
21459 updated_row, updated_area,
21460 hpos, hpos + len,
21461 DRAW_NORMAL_TEXT, 0);
21463 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21464 if (updated_area == TEXT_AREA
21465 && updated_window->phys_cursor_on_p
21466 && updated_window->phys_cursor.vpos == output_cursor.vpos
21467 && updated_window->phys_cursor.hpos >= hpos
21468 && updated_window->phys_cursor.hpos < hpos + len)
21469 updated_window->phys_cursor_on_p = 0;
21471 UNBLOCK_INPUT;
21473 /* Advance the output cursor. */
21474 output_cursor.hpos += len;
21475 output_cursor.x = x;
21479 /* EXPORT for RIF:
21480 Insert LEN glyphs from START at the nominal cursor position. */
21482 void
21483 x_insert_glyphs (start, len)
21484 struct glyph *start;
21485 int len;
21487 struct frame *f;
21488 struct window *w;
21489 int line_height, shift_by_width, shifted_region_width;
21490 struct glyph_row *row;
21491 struct glyph *glyph;
21492 int frame_x, frame_y;
21493 EMACS_INT hpos;
21495 xassert (updated_window && updated_row);
21496 BLOCK_INPUT;
21497 w = updated_window;
21498 f = XFRAME (WINDOW_FRAME (w));
21500 /* Get the height of the line we are in. */
21501 row = updated_row;
21502 line_height = row->height;
21504 /* Get the width of the glyphs to insert. */
21505 shift_by_width = 0;
21506 for (glyph = start; glyph < start + len; ++glyph)
21507 shift_by_width += glyph->pixel_width;
21509 /* Get the width of the region to shift right. */
21510 shifted_region_width = (window_box_width (w, updated_area)
21511 - output_cursor.x
21512 - shift_by_width);
21514 /* Shift right. */
21515 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21516 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21518 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21519 line_height, shift_by_width);
21521 /* Write the glyphs. */
21522 hpos = start - row->glyphs[updated_area];
21523 draw_glyphs (w, output_cursor.x, row, updated_area,
21524 hpos, hpos + len,
21525 DRAW_NORMAL_TEXT, 0);
21527 /* Advance the output cursor. */
21528 output_cursor.hpos += len;
21529 output_cursor.x += shift_by_width;
21530 UNBLOCK_INPUT;
21534 /* EXPORT for RIF:
21535 Erase the current text line from the nominal cursor position
21536 (inclusive) to pixel column TO_X (exclusive). The idea is that
21537 everything from TO_X onward is already erased.
21539 TO_X is a pixel position relative to updated_area of
21540 updated_window. TO_X == -1 means clear to the end of this area. */
21542 void
21543 x_clear_end_of_line (to_x)
21544 int to_x;
21546 struct frame *f;
21547 struct window *w = updated_window;
21548 int max_x, min_y, max_y;
21549 int from_x, from_y, to_y;
21551 xassert (updated_window && updated_row);
21552 f = XFRAME (w->frame);
21554 if (updated_row->full_width_p)
21555 max_x = WINDOW_TOTAL_WIDTH (w);
21556 else
21557 max_x = window_box_width (w, updated_area);
21558 max_y = window_text_bottom_y (w);
21560 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21561 of window. For TO_X > 0, truncate to end of drawing area. */
21562 if (to_x == 0)
21563 return;
21564 else if (to_x < 0)
21565 to_x = max_x;
21566 else
21567 to_x = min (to_x, max_x);
21569 to_y = min (max_y, output_cursor.y + updated_row->height);
21571 /* Notice if the cursor will be cleared by this operation. */
21572 if (!updated_row->full_width_p)
21573 notice_overwritten_cursor (w, updated_area,
21574 output_cursor.x, -1,
21575 updated_row->y,
21576 MATRIX_ROW_BOTTOM_Y (updated_row));
21578 from_x = output_cursor.x;
21580 /* Translate to frame coordinates. */
21581 if (updated_row->full_width_p)
21583 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21584 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21586 else
21588 int area_left = window_box_left (w, updated_area);
21589 from_x += area_left;
21590 to_x += area_left;
21593 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21594 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21595 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21597 /* Prevent inadvertently clearing to end of the X window. */
21598 if (to_x > from_x && to_y > from_y)
21600 BLOCK_INPUT;
21601 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21602 to_x - from_x, to_y - from_y);
21603 UNBLOCK_INPUT;
21607 #endif /* HAVE_WINDOW_SYSTEM */
21611 /***********************************************************************
21612 Cursor types
21613 ***********************************************************************/
21615 /* Value is the internal representation of the specified cursor type
21616 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21617 of the bar cursor. */
21619 static enum text_cursor_kinds
21620 get_specified_cursor_type (arg, width)
21621 Lisp_Object arg;
21622 int *width;
21624 enum text_cursor_kinds type;
21626 if (NILP (arg))
21627 return NO_CURSOR;
21629 if (EQ (arg, Qbox))
21630 return FILLED_BOX_CURSOR;
21632 if (EQ (arg, Qhollow))
21633 return HOLLOW_BOX_CURSOR;
21635 if (EQ (arg, Qbar))
21637 *width = 2;
21638 return BAR_CURSOR;
21641 if (CONSP (arg)
21642 && EQ (XCAR (arg), Qbar)
21643 && INTEGERP (XCDR (arg))
21644 && XINT (XCDR (arg)) >= 0)
21646 *width = XINT (XCDR (arg));
21647 return BAR_CURSOR;
21650 if (EQ (arg, Qhbar))
21652 *width = 2;
21653 return HBAR_CURSOR;
21656 if (CONSP (arg)
21657 && EQ (XCAR (arg), Qhbar)
21658 && INTEGERP (XCDR (arg))
21659 && XINT (XCDR (arg)) >= 0)
21661 *width = XINT (XCDR (arg));
21662 return HBAR_CURSOR;
21665 /* Treat anything unknown as "hollow box cursor".
21666 It was bad to signal an error; people have trouble fixing
21667 .Xdefaults with Emacs, when it has something bad in it. */
21668 type = HOLLOW_BOX_CURSOR;
21670 return type;
21673 /* Set the default cursor types for specified frame. */
21674 void
21675 set_frame_cursor_types (f, arg)
21676 struct frame *f;
21677 Lisp_Object arg;
21679 int width;
21680 Lisp_Object tem;
21682 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21683 FRAME_CURSOR_WIDTH (f) = width;
21685 /* By default, set up the blink-off state depending on the on-state. */
21687 tem = Fassoc (arg, Vblink_cursor_alist);
21688 if (!NILP (tem))
21690 FRAME_BLINK_OFF_CURSOR (f)
21691 = get_specified_cursor_type (XCDR (tem), &width);
21692 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21694 else
21695 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21699 /* Return the cursor we want to be displayed in window W. Return
21700 width of bar/hbar cursor through WIDTH arg. Return with
21701 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21702 (i.e. if the `system caret' should track this cursor).
21704 In a mini-buffer window, we want the cursor only to appear if we
21705 are reading input from this window. For the selected window, we
21706 want the cursor type given by the frame parameter or buffer local
21707 setting of cursor-type. If explicitly marked off, draw no cursor.
21708 In all other cases, we want a hollow box cursor. */
21710 static enum text_cursor_kinds
21711 get_window_cursor_type (w, glyph, width, active_cursor)
21712 struct window *w;
21713 struct glyph *glyph;
21714 int *width;
21715 int *active_cursor;
21717 struct frame *f = XFRAME (w->frame);
21718 struct buffer *b = XBUFFER (w->buffer);
21719 int cursor_type = DEFAULT_CURSOR;
21720 Lisp_Object alt_cursor;
21721 int non_selected = 0;
21723 *active_cursor = 1;
21725 /* Echo area */
21726 if (cursor_in_echo_area
21727 && FRAME_HAS_MINIBUF_P (f)
21728 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21730 if (w == XWINDOW (echo_area_window))
21732 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21734 *width = FRAME_CURSOR_WIDTH (f);
21735 return FRAME_DESIRED_CURSOR (f);
21737 else
21738 return get_specified_cursor_type (b->cursor_type, width);
21741 *active_cursor = 0;
21742 non_selected = 1;
21745 /* Detect a nonselected window or nonselected frame. */
21746 else if (w != XWINDOW (f->selected_window)
21747 #ifdef HAVE_WINDOW_SYSTEM
21748 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21749 #endif
21752 *active_cursor = 0;
21754 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21755 return NO_CURSOR;
21757 non_selected = 1;
21760 /* Never display a cursor in a window in which cursor-type is nil. */
21761 if (NILP (b->cursor_type))
21762 return NO_CURSOR;
21764 /* Get the normal cursor type for this window. */
21765 if (EQ (b->cursor_type, Qt))
21767 cursor_type = FRAME_DESIRED_CURSOR (f);
21768 *width = FRAME_CURSOR_WIDTH (f);
21770 else
21771 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21773 /* Use cursor-in-non-selected-windows instead
21774 for non-selected window or frame. */
21775 if (non_selected)
21777 alt_cursor = b->cursor_in_non_selected_windows;
21778 if (!EQ (Qt, alt_cursor))
21779 return get_specified_cursor_type (alt_cursor, width);
21780 /* t means modify the normal cursor type. */
21781 if (cursor_type == FILLED_BOX_CURSOR)
21782 cursor_type = HOLLOW_BOX_CURSOR;
21783 else if (cursor_type == BAR_CURSOR && *width > 1)
21784 --*width;
21785 return cursor_type;
21788 /* Use normal cursor if not blinked off. */
21789 if (!w->cursor_off_p)
21791 #ifdef HAVE_WINDOW_SYSTEM
21792 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21794 if (cursor_type == FILLED_BOX_CURSOR)
21796 /* Using a block cursor on large images can be very annoying.
21797 So use a hollow cursor for "large" images.
21798 If image is not transparent (no mask), also use hollow cursor. */
21799 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21800 if (img != NULL && IMAGEP (img->spec))
21802 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21803 where N = size of default frame font size.
21804 This should cover most of the "tiny" icons people may use. */
21805 if (!img->mask
21806 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21807 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21808 cursor_type = HOLLOW_BOX_CURSOR;
21811 else if (cursor_type != NO_CURSOR)
21813 /* Display current only supports BOX and HOLLOW cursors for images.
21814 So for now, unconditionally use a HOLLOW cursor when cursor is
21815 not a solid box cursor. */
21816 cursor_type = HOLLOW_BOX_CURSOR;
21819 #endif
21820 return cursor_type;
21823 /* Cursor is blinked off, so determine how to "toggle" it. */
21825 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21826 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21827 return get_specified_cursor_type (XCDR (alt_cursor), width);
21829 /* Then see if frame has specified a specific blink off cursor type. */
21830 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21832 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21833 return FRAME_BLINK_OFF_CURSOR (f);
21836 #if 0
21837 /* Some people liked having a permanently visible blinking cursor,
21838 while others had very strong opinions against it. So it was
21839 decided to remove it. KFS 2003-09-03 */
21841 /* Finally perform built-in cursor blinking:
21842 filled box <-> hollow box
21843 wide [h]bar <-> narrow [h]bar
21844 narrow [h]bar <-> no cursor
21845 other type <-> no cursor */
21847 if (cursor_type == FILLED_BOX_CURSOR)
21848 return HOLLOW_BOX_CURSOR;
21850 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21852 *width = 1;
21853 return cursor_type;
21855 #endif
21857 return NO_CURSOR;
21861 #ifdef HAVE_WINDOW_SYSTEM
21863 /* Notice when the text cursor of window W has been completely
21864 overwritten by a drawing operation that outputs glyphs in AREA
21865 starting at X0 and ending at X1 in the line starting at Y0 and
21866 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21867 the rest of the line after X0 has been written. Y coordinates
21868 are window-relative. */
21870 static void
21871 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21872 struct window *w;
21873 enum glyph_row_area area;
21874 int x0, y0, x1, y1;
21876 int cx0, cx1, cy0, cy1;
21877 struct glyph_row *row;
21879 if (!w->phys_cursor_on_p)
21880 return;
21881 if (area != TEXT_AREA)
21882 return;
21884 if (w->phys_cursor.vpos < 0
21885 || w->phys_cursor.vpos >= w->current_matrix->nrows
21886 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21887 !(row->enabled_p && row->displays_text_p)))
21888 return;
21890 if (row->cursor_in_fringe_p)
21892 row->cursor_in_fringe_p = 0;
21893 draw_fringe_bitmap (w, row, 0);
21894 w->phys_cursor_on_p = 0;
21895 return;
21898 cx0 = w->phys_cursor.x;
21899 cx1 = cx0 + w->phys_cursor_width;
21900 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21901 return;
21903 /* The cursor image will be completely removed from the
21904 screen if the output area intersects the cursor area in
21905 y-direction. When we draw in [y0 y1[, and some part of
21906 the cursor is at y < y0, that part must have been drawn
21907 before. When scrolling, the cursor is erased before
21908 actually scrolling, so we don't come here. When not
21909 scrolling, the rows above the old cursor row must have
21910 changed, and in this case these rows must have written
21911 over the cursor image.
21913 Likewise if part of the cursor is below y1, with the
21914 exception of the cursor being in the first blank row at
21915 the buffer and window end because update_text_area
21916 doesn't draw that row. (Except when it does, but
21917 that's handled in update_text_area.) */
21919 cy0 = w->phys_cursor.y;
21920 cy1 = cy0 + w->phys_cursor_height;
21921 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21922 return;
21924 w->phys_cursor_on_p = 0;
21927 #endif /* HAVE_WINDOW_SYSTEM */
21930 /************************************************************************
21931 Mouse Face
21932 ************************************************************************/
21934 #ifdef HAVE_WINDOW_SYSTEM
21936 /* EXPORT for RIF:
21937 Fix the display of area AREA of overlapping row ROW in window W
21938 with respect to the overlapping part OVERLAPS. */
21940 void
21941 x_fix_overlapping_area (w, row, area, overlaps)
21942 struct window *w;
21943 struct glyph_row *row;
21944 enum glyph_row_area area;
21945 int overlaps;
21947 int i, x;
21949 BLOCK_INPUT;
21951 x = 0;
21952 for (i = 0; i < row->used[area];)
21954 if (row->glyphs[area][i].overlaps_vertically_p)
21956 int start = i, start_x = x;
21960 x += row->glyphs[area][i].pixel_width;
21961 ++i;
21963 while (i < row->used[area]
21964 && row->glyphs[area][i].overlaps_vertically_p);
21966 draw_glyphs (w, start_x, row, area,
21967 start, i,
21968 DRAW_NORMAL_TEXT, overlaps);
21970 else
21972 x += row->glyphs[area][i].pixel_width;
21973 ++i;
21977 UNBLOCK_INPUT;
21981 /* EXPORT:
21982 Draw the cursor glyph of window W in glyph row ROW. See the
21983 comment of draw_glyphs for the meaning of HL. */
21985 void
21986 draw_phys_cursor_glyph (w, row, hl)
21987 struct window *w;
21988 struct glyph_row *row;
21989 enum draw_glyphs_face hl;
21991 /* If cursor hpos is out of bounds, don't draw garbage. This can
21992 happen in mini-buffer windows when switching between echo area
21993 glyphs and mini-buffer. */
21994 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21996 int on_p = w->phys_cursor_on_p;
21997 int x1;
21998 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21999 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22000 hl, 0);
22001 w->phys_cursor_on_p = on_p;
22003 if (hl == DRAW_CURSOR)
22004 w->phys_cursor_width = x1 - w->phys_cursor.x;
22005 /* When we erase the cursor, and ROW is overlapped by other
22006 rows, make sure that these overlapping parts of other rows
22007 are redrawn. */
22008 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22010 w->phys_cursor_width = x1 - w->phys_cursor.x;
22012 if (row > w->current_matrix->rows
22013 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22014 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22015 OVERLAPS_ERASED_CURSOR);
22017 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22018 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22019 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22020 OVERLAPS_ERASED_CURSOR);
22026 /* EXPORT:
22027 Erase the image of a cursor of window W from the screen. */
22029 void
22030 erase_phys_cursor (w)
22031 struct window *w;
22033 struct frame *f = XFRAME (w->frame);
22034 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22035 int hpos = w->phys_cursor.hpos;
22036 int vpos = w->phys_cursor.vpos;
22037 int mouse_face_here_p = 0;
22038 struct glyph_matrix *active_glyphs = w->current_matrix;
22039 struct glyph_row *cursor_row;
22040 struct glyph *cursor_glyph;
22041 enum draw_glyphs_face hl;
22043 /* No cursor displayed or row invalidated => nothing to do on the
22044 screen. */
22045 if (w->phys_cursor_type == NO_CURSOR)
22046 goto mark_cursor_off;
22048 /* VPOS >= active_glyphs->nrows means that window has been resized.
22049 Don't bother to erase the cursor. */
22050 if (vpos >= active_glyphs->nrows)
22051 goto mark_cursor_off;
22053 /* If row containing cursor is marked invalid, there is nothing we
22054 can do. */
22055 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22056 if (!cursor_row->enabled_p)
22057 goto mark_cursor_off;
22059 /* If line spacing is > 0, old cursor may only be partially visible in
22060 window after split-window. So adjust visible height. */
22061 cursor_row->visible_height = min (cursor_row->visible_height,
22062 window_text_bottom_y (w) - cursor_row->y);
22064 /* If row is completely invisible, don't attempt to delete a cursor which
22065 isn't there. This can happen if cursor is at top of a window, and
22066 we switch to a buffer with a header line in that window. */
22067 if (cursor_row->visible_height <= 0)
22068 goto mark_cursor_off;
22070 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22071 if (cursor_row->cursor_in_fringe_p)
22073 cursor_row->cursor_in_fringe_p = 0;
22074 draw_fringe_bitmap (w, cursor_row, 0);
22075 goto mark_cursor_off;
22078 /* This can happen when the new row is shorter than the old one.
22079 In this case, either draw_glyphs or clear_end_of_line
22080 should have cleared the cursor. Note that we wouldn't be
22081 able to erase the cursor in this case because we don't have a
22082 cursor glyph at hand. */
22083 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22084 goto mark_cursor_off;
22086 /* If the cursor is in the mouse face area, redisplay that when
22087 we clear the cursor. */
22088 if (! NILP (dpyinfo->mouse_face_window)
22089 && w == XWINDOW (dpyinfo->mouse_face_window)
22090 && (vpos > dpyinfo->mouse_face_beg_row
22091 || (vpos == dpyinfo->mouse_face_beg_row
22092 && hpos >= dpyinfo->mouse_face_beg_col))
22093 && (vpos < dpyinfo->mouse_face_end_row
22094 || (vpos == dpyinfo->mouse_face_end_row
22095 && hpos < dpyinfo->mouse_face_end_col))
22096 /* Don't redraw the cursor's spot in mouse face if it is at the
22097 end of a line (on a newline). The cursor appears there, but
22098 mouse highlighting does not. */
22099 && cursor_row->used[TEXT_AREA] > hpos)
22100 mouse_face_here_p = 1;
22102 /* Maybe clear the display under the cursor. */
22103 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22105 int x, y, left_x;
22106 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22107 int width;
22109 cursor_glyph = get_phys_cursor_glyph (w);
22110 if (cursor_glyph == NULL)
22111 goto mark_cursor_off;
22113 width = cursor_glyph->pixel_width;
22114 left_x = window_box_left_offset (w, TEXT_AREA);
22115 x = w->phys_cursor.x;
22116 if (x < left_x)
22117 width -= left_x - x;
22118 width = min (width, window_box_width (w, TEXT_AREA) - x);
22119 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22120 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22122 if (width > 0)
22123 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22126 /* Erase the cursor by redrawing the character underneath it. */
22127 if (mouse_face_here_p)
22128 hl = DRAW_MOUSE_FACE;
22129 else
22130 hl = DRAW_NORMAL_TEXT;
22131 draw_phys_cursor_glyph (w, cursor_row, hl);
22133 mark_cursor_off:
22134 w->phys_cursor_on_p = 0;
22135 w->phys_cursor_type = NO_CURSOR;
22139 /* EXPORT:
22140 Display or clear cursor of window W. If ON is zero, clear the
22141 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22142 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22144 void
22145 display_and_set_cursor (w, on, hpos, vpos, x, y)
22146 struct window *w;
22147 int on, hpos, vpos, x, y;
22149 struct frame *f = XFRAME (w->frame);
22150 int new_cursor_type;
22151 int new_cursor_width;
22152 int active_cursor;
22153 struct glyph_row *glyph_row;
22154 struct glyph *glyph;
22156 /* This is pointless on invisible frames, and dangerous on garbaged
22157 windows and frames; in the latter case, the frame or window may
22158 be in the midst of changing its size, and x and y may be off the
22159 window. */
22160 if (! FRAME_VISIBLE_P (f)
22161 || FRAME_GARBAGED_P (f)
22162 || vpos >= w->current_matrix->nrows
22163 || hpos >= w->current_matrix->matrix_w)
22164 return;
22166 /* If cursor is off and we want it off, return quickly. */
22167 if (!on && !w->phys_cursor_on_p)
22168 return;
22170 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22171 /* If cursor row is not enabled, we don't really know where to
22172 display the cursor. */
22173 if (!glyph_row->enabled_p)
22175 w->phys_cursor_on_p = 0;
22176 return;
22179 glyph = NULL;
22180 if (!glyph_row->exact_window_width_line_p
22181 || hpos < glyph_row->used[TEXT_AREA])
22182 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22184 xassert (interrupt_input_blocked);
22186 /* Set new_cursor_type to the cursor we want to be displayed. */
22187 new_cursor_type = get_window_cursor_type (w, glyph,
22188 &new_cursor_width, &active_cursor);
22190 /* If cursor is currently being shown and we don't want it to be or
22191 it is in the wrong place, or the cursor type is not what we want,
22192 erase it. */
22193 if (w->phys_cursor_on_p
22194 && (!on
22195 || w->phys_cursor.x != x
22196 || w->phys_cursor.y != y
22197 || new_cursor_type != w->phys_cursor_type
22198 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22199 && new_cursor_width != w->phys_cursor_width)))
22200 erase_phys_cursor (w);
22202 /* Don't check phys_cursor_on_p here because that flag is only set
22203 to zero in some cases where we know that the cursor has been
22204 completely erased, to avoid the extra work of erasing the cursor
22205 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22206 still not be visible, or it has only been partly erased. */
22207 if (on)
22209 w->phys_cursor_ascent = glyph_row->ascent;
22210 w->phys_cursor_height = glyph_row->height;
22212 /* Set phys_cursor_.* before x_draw_.* is called because some
22213 of them may need the information. */
22214 w->phys_cursor.x = x;
22215 w->phys_cursor.y = glyph_row->y;
22216 w->phys_cursor.hpos = hpos;
22217 w->phys_cursor.vpos = vpos;
22220 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22221 new_cursor_type, new_cursor_width,
22222 on, active_cursor);
22226 /* Switch the display of W's cursor on or off, according to the value
22227 of ON. */
22229 static void
22230 update_window_cursor (w, on)
22231 struct window *w;
22232 int on;
22234 /* Don't update cursor in windows whose frame is in the process
22235 of being deleted. */
22236 if (w->current_matrix)
22238 BLOCK_INPUT;
22239 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22240 w->phys_cursor.x, w->phys_cursor.y);
22241 UNBLOCK_INPUT;
22246 /* Call update_window_cursor with parameter ON_P on all leaf windows
22247 in the window tree rooted at W. */
22249 static void
22250 update_cursor_in_window_tree (w, on_p)
22251 struct window *w;
22252 int on_p;
22254 while (w)
22256 if (!NILP (w->hchild))
22257 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22258 else if (!NILP (w->vchild))
22259 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22260 else
22261 update_window_cursor (w, on_p);
22263 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22268 /* EXPORT:
22269 Display the cursor on window W, or clear it, according to ON_P.
22270 Don't change the cursor's position. */
22272 void
22273 x_update_cursor (f, on_p)
22274 struct frame *f;
22275 int on_p;
22277 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22281 /* EXPORT:
22282 Clear the cursor of window W to background color, and mark the
22283 cursor as not shown. This is used when the text where the cursor
22284 is is about to be rewritten. */
22286 void
22287 x_clear_cursor (w)
22288 struct window *w;
22290 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22291 update_window_cursor (w, 0);
22295 /* EXPORT:
22296 Display the active region described by mouse_face_* according to DRAW. */
22298 void
22299 show_mouse_face (dpyinfo, draw)
22300 Display_Info *dpyinfo;
22301 enum draw_glyphs_face draw;
22303 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22304 struct frame *f = XFRAME (WINDOW_FRAME (w));
22306 if (/* If window is in the process of being destroyed, don't bother
22307 to do anything. */
22308 w->current_matrix != NULL
22309 /* Don't update mouse highlight if hidden */
22310 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22311 /* Recognize when we are called to operate on rows that don't exist
22312 anymore. This can happen when a window is split. */
22313 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22315 int phys_cursor_on_p = w->phys_cursor_on_p;
22316 struct glyph_row *row, *first, *last;
22318 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22319 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22321 for (row = first; row <= last && row->enabled_p; ++row)
22323 int start_hpos, end_hpos, start_x;
22325 /* For all but the first row, the highlight starts at column 0. */
22326 if (row == first)
22328 start_hpos = dpyinfo->mouse_face_beg_col;
22329 start_x = dpyinfo->mouse_face_beg_x;
22331 else
22333 start_hpos = 0;
22334 start_x = 0;
22337 if (row == last)
22338 end_hpos = dpyinfo->mouse_face_end_col;
22339 else
22341 end_hpos = row->used[TEXT_AREA];
22342 if (draw == DRAW_NORMAL_TEXT)
22343 row->fill_line_p = 1; /* Clear to end of line */
22346 if (end_hpos > start_hpos)
22348 draw_glyphs (w, start_x, row, TEXT_AREA,
22349 start_hpos, end_hpos,
22350 draw, 0);
22352 row->mouse_face_p
22353 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22357 /* When we've written over the cursor, arrange for it to
22358 be displayed again. */
22359 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22361 BLOCK_INPUT;
22362 display_and_set_cursor (w, 1,
22363 w->phys_cursor.hpos, w->phys_cursor.vpos,
22364 w->phys_cursor.x, w->phys_cursor.y);
22365 UNBLOCK_INPUT;
22369 /* Change the mouse cursor. */
22370 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22371 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22372 else if (draw == DRAW_MOUSE_FACE)
22373 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22374 else
22375 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22378 /* EXPORT:
22379 Clear out the mouse-highlighted active region.
22380 Redraw it un-highlighted first. Value is non-zero if mouse
22381 face was actually drawn unhighlighted. */
22384 clear_mouse_face (dpyinfo)
22385 Display_Info *dpyinfo;
22387 int cleared = 0;
22389 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22391 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22392 cleared = 1;
22395 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22396 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22397 dpyinfo->mouse_face_window = Qnil;
22398 dpyinfo->mouse_face_overlay = Qnil;
22399 return cleared;
22403 /* EXPORT:
22404 Non-zero if physical cursor of window W is within mouse face. */
22407 cursor_in_mouse_face_p (w)
22408 struct window *w;
22410 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22411 int in_mouse_face = 0;
22413 if (WINDOWP (dpyinfo->mouse_face_window)
22414 && XWINDOW (dpyinfo->mouse_face_window) == w)
22416 int hpos = w->phys_cursor.hpos;
22417 int vpos = w->phys_cursor.vpos;
22419 if (vpos >= dpyinfo->mouse_face_beg_row
22420 && vpos <= dpyinfo->mouse_face_end_row
22421 && (vpos > dpyinfo->mouse_face_beg_row
22422 || hpos >= dpyinfo->mouse_face_beg_col)
22423 && (vpos < dpyinfo->mouse_face_end_row
22424 || hpos < dpyinfo->mouse_face_end_col
22425 || dpyinfo->mouse_face_past_end))
22426 in_mouse_face = 1;
22429 return in_mouse_face;
22435 /* Find the glyph matrix position of buffer position CHARPOS in window
22436 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22437 current glyphs must be up to date. If CHARPOS is above window
22438 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22439 of last line in W. In the row containing CHARPOS, stop before glyphs
22440 having STOP as object. */
22442 #if 1 /* This is a version of fast_find_position that's more correct
22443 in the presence of hscrolling, for example. I didn't install
22444 it right away because the problem fixed is minor, it failed
22445 in 20.x as well, and I think it's too risky to install
22446 so near the release of 21.1. 2001-09-25 gerd. */
22448 #ifndef HAVE_CARBON
22449 static
22450 #endif
22452 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22453 struct window *w;
22454 EMACS_INT charpos;
22455 int *hpos, *vpos, *x, *y;
22456 Lisp_Object stop;
22458 struct glyph_row *row, *first;
22459 struct glyph *glyph, *end;
22460 int past_end = 0;
22462 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22463 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22465 *x = first->x;
22466 *y = first->y;
22467 *hpos = 0;
22468 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22469 return 1;
22472 row = row_containing_pos (w, charpos, first, NULL, 0);
22473 if (row == NULL)
22475 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22476 past_end = 1;
22479 /* If whole rows or last part of a row came from a display overlay,
22480 row_containing_pos will skip over such rows because their end pos
22481 equals the start pos of the overlay or interval.
22483 Move back if we have a STOP object and previous row's
22484 end glyph came from STOP. */
22485 if (!NILP (stop))
22487 struct glyph_row *prev;
22488 while ((prev = row - 1, prev >= first)
22489 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22490 && prev->used[TEXT_AREA] > 0)
22492 struct glyph *beg = prev->glyphs[TEXT_AREA];
22493 glyph = beg + prev->used[TEXT_AREA];
22494 while (--glyph >= beg
22495 && INTEGERP (glyph->object));
22496 if (glyph < beg
22497 || !EQ (stop, glyph->object))
22498 break;
22499 row = prev;
22503 *x = row->x;
22504 *y = row->y;
22505 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22507 glyph = row->glyphs[TEXT_AREA];
22508 end = glyph + row->used[TEXT_AREA];
22510 /* Skip over glyphs not having an object at the start of the row.
22511 These are special glyphs like truncation marks on terminal
22512 frames. */
22513 if (row->displays_text_p)
22514 while (glyph < end
22515 && INTEGERP (glyph->object)
22516 && !EQ (stop, glyph->object)
22517 && glyph->charpos < 0)
22519 *x += glyph->pixel_width;
22520 ++glyph;
22523 while (glyph < end
22524 && !INTEGERP (glyph->object)
22525 && !EQ (stop, glyph->object)
22526 && (!BUFFERP (glyph->object)
22527 || glyph->charpos < charpos))
22529 *x += glyph->pixel_width;
22530 ++glyph;
22533 *hpos = glyph - row->glyphs[TEXT_AREA];
22534 return !past_end;
22537 #else /* not 1 */
22539 static int
22540 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22541 struct window *w;
22542 EMACS_INT pos;
22543 int *hpos, *vpos, *x, *y;
22544 Lisp_Object stop;
22546 int i;
22547 int lastcol;
22548 int maybe_next_line_p = 0;
22549 int line_start_position;
22550 int yb = window_text_bottom_y (w);
22551 struct glyph_row *row, *best_row;
22552 int row_vpos, best_row_vpos;
22553 int current_x;
22555 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22556 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22558 while (row->y < yb)
22560 if (row->used[TEXT_AREA])
22561 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22562 else
22563 line_start_position = 0;
22565 if (line_start_position > pos)
22566 break;
22567 /* If the position sought is the end of the buffer,
22568 don't include the blank lines at the bottom of the window. */
22569 else if (line_start_position == pos
22570 && pos == BUF_ZV (XBUFFER (w->buffer)))
22572 maybe_next_line_p = 1;
22573 break;
22575 else if (line_start_position > 0)
22577 best_row = row;
22578 best_row_vpos = row_vpos;
22581 if (row->y + row->height >= yb)
22582 break;
22584 ++row;
22585 ++row_vpos;
22588 /* Find the right column within BEST_ROW. */
22589 lastcol = 0;
22590 current_x = best_row->x;
22591 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22593 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22594 int charpos = glyph->charpos;
22596 if (BUFFERP (glyph->object))
22598 if (charpos == pos)
22600 *hpos = i;
22601 *vpos = best_row_vpos;
22602 *x = current_x;
22603 *y = best_row->y;
22604 return 1;
22606 else if (charpos > pos)
22607 break;
22609 else if (EQ (glyph->object, stop))
22610 break;
22612 if (charpos > 0)
22613 lastcol = i;
22614 current_x += glyph->pixel_width;
22617 /* If we're looking for the end of the buffer,
22618 and we didn't find it in the line we scanned,
22619 use the start of the following line. */
22620 if (maybe_next_line_p)
22622 ++best_row;
22623 ++best_row_vpos;
22624 lastcol = 0;
22625 current_x = best_row->x;
22628 *vpos = best_row_vpos;
22629 *hpos = lastcol + 1;
22630 *x = current_x;
22631 *y = best_row->y;
22632 return 0;
22635 #endif /* not 1 */
22638 /* Find the position of the glyph for position POS in OBJECT in
22639 window W's current matrix, and return in *X, *Y the pixel
22640 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22642 RIGHT_P non-zero means return the position of the right edge of the
22643 glyph, RIGHT_P zero means return the left edge position.
22645 If no glyph for POS exists in the matrix, return the position of
22646 the glyph with the next smaller position that is in the matrix, if
22647 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22648 exists in the matrix, return the position of the glyph with the
22649 next larger position in OBJECT.
22651 Value is non-zero if a glyph was found. */
22653 static int
22654 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22655 struct window *w;
22656 EMACS_INT pos;
22657 Lisp_Object object;
22658 int *hpos, *vpos, *x, *y;
22659 int right_p;
22661 int yb = window_text_bottom_y (w);
22662 struct glyph_row *r;
22663 struct glyph *best_glyph = NULL;
22664 struct glyph_row *best_row = NULL;
22665 int best_x = 0;
22667 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22668 r->enabled_p && r->y < yb;
22669 ++r)
22671 struct glyph *g = r->glyphs[TEXT_AREA];
22672 struct glyph *e = g + r->used[TEXT_AREA];
22673 int gx;
22675 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22676 if (EQ (g->object, object))
22678 if (g->charpos == pos)
22680 best_glyph = g;
22681 best_x = gx;
22682 best_row = r;
22683 goto found;
22685 else if (best_glyph == NULL
22686 || ((eabs (g->charpos - pos)
22687 < eabs (best_glyph->charpos - pos))
22688 && (right_p
22689 ? g->charpos < pos
22690 : g->charpos > pos)))
22692 best_glyph = g;
22693 best_x = gx;
22694 best_row = r;
22699 found:
22701 if (best_glyph)
22703 *x = best_x;
22704 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22706 if (right_p)
22708 *x += best_glyph->pixel_width;
22709 ++*hpos;
22712 *y = best_row->y;
22713 *vpos = best_row - w->current_matrix->rows;
22716 return best_glyph != NULL;
22720 /* See if position X, Y is within a hot-spot of an image. */
22722 static int
22723 on_hot_spot_p (hot_spot, x, y)
22724 Lisp_Object hot_spot;
22725 int x, y;
22727 if (!CONSP (hot_spot))
22728 return 0;
22730 if (EQ (XCAR (hot_spot), Qrect))
22732 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22733 Lisp_Object rect = XCDR (hot_spot);
22734 Lisp_Object tem;
22735 if (!CONSP (rect))
22736 return 0;
22737 if (!CONSP (XCAR (rect)))
22738 return 0;
22739 if (!CONSP (XCDR (rect)))
22740 return 0;
22741 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22742 return 0;
22743 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22744 return 0;
22745 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22746 return 0;
22747 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22748 return 0;
22749 return 1;
22751 else if (EQ (XCAR (hot_spot), Qcircle))
22753 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22754 Lisp_Object circ = XCDR (hot_spot);
22755 Lisp_Object lr, lx0, ly0;
22756 if (CONSP (circ)
22757 && CONSP (XCAR (circ))
22758 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22759 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22760 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22762 double r = XFLOATINT (lr);
22763 double dx = XINT (lx0) - x;
22764 double dy = XINT (ly0) - y;
22765 return (dx * dx + dy * dy <= r * r);
22768 else if (EQ (XCAR (hot_spot), Qpoly))
22770 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22771 if (VECTORP (XCDR (hot_spot)))
22773 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22774 Lisp_Object *poly = v->contents;
22775 int n = v->size;
22776 int i;
22777 int inside = 0;
22778 Lisp_Object lx, ly;
22779 int x0, y0;
22781 /* Need an even number of coordinates, and at least 3 edges. */
22782 if (n < 6 || n & 1)
22783 return 0;
22785 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22786 If count is odd, we are inside polygon. Pixels on edges
22787 may or may not be included depending on actual geometry of the
22788 polygon. */
22789 if ((lx = poly[n-2], !INTEGERP (lx))
22790 || (ly = poly[n-1], !INTEGERP (lx)))
22791 return 0;
22792 x0 = XINT (lx), y0 = XINT (ly);
22793 for (i = 0; i < n; i += 2)
22795 int x1 = x0, y1 = y0;
22796 if ((lx = poly[i], !INTEGERP (lx))
22797 || (ly = poly[i+1], !INTEGERP (ly)))
22798 return 0;
22799 x0 = XINT (lx), y0 = XINT (ly);
22801 /* Does this segment cross the X line? */
22802 if (x0 >= x)
22804 if (x1 >= x)
22805 continue;
22807 else if (x1 < x)
22808 continue;
22809 if (y > y0 && y > y1)
22810 continue;
22811 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22812 inside = !inside;
22814 return inside;
22817 return 0;
22820 Lisp_Object
22821 find_hot_spot (map, x, y)
22822 Lisp_Object map;
22823 int x, y;
22825 while (CONSP (map))
22827 if (CONSP (XCAR (map))
22828 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22829 return XCAR (map);
22830 map = XCDR (map);
22833 return Qnil;
22836 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22837 3, 3, 0,
22838 doc: /* Lookup in image map MAP coordinates X and Y.
22839 An image map is an alist where each element has the format (AREA ID PLIST).
22840 An AREA is specified as either a rectangle, a circle, or a polygon:
22841 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22842 pixel coordinates of the upper left and bottom right corners.
22843 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22844 and the radius of the circle; r may be a float or integer.
22845 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22846 vector describes one corner in the polygon.
22847 Returns the alist element for the first matching AREA in MAP. */)
22848 (map, x, y)
22849 Lisp_Object map;
22850 Lisp_Object x, y;
22852 if (NILP (map))
22853 return Qnil;
22855 CHECK_NUMBER (x);
22856 CHECK_NUMBER (y);
22858 return find_hot_spot (map, XINT (x), XINT (y));
22862 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22863 static void
22864 define_frame_cursor1 (f, cursor, pointer)
22865 struct frame *f;
22866 Cursor cursor;
22867 Lisp_Object pointer;
22869 /* Do not change cursor shape while dragging mouse. */
22870 if (!NILP (do_mouse_tracking))
22871 return;
22873 if (!NILP (pointer))
22875 if (EQ (pointer, Qarrow))
22876 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22877 else if (EQ (pointer, Qhand))
22878 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22879 else if (EQ (pointer, Qtext))
22880 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22881 else if (EQ (pointer, intern ("hdrag")))
22882 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22883 #ifdef HAVE_X_WINDOWS
22884 else if (EQ (pointer, intern ("vdrag")))
22885 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22886 #endif
22887 else if (EQ (pointer, intern ("hourglass")))
22888 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22889 else if (EQ (pointer, Qmodeline))
22890 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22891 else
22892 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22895 if (cursor != No_Cursor)
22896 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22899 /* Take proper action when mouse has moved to the mode or header line
22900 or marginal area AREA of window W, x-position X and y-position Y.
22901 X is relative to the start of the text display area of W, so the
22902 width of bitmap areas and scroll bars must be subtracted to get a
22903 position relative to the start of the mode line. */
22905 static void
22906 note_mode_line_or_margin_highlight (window, x, y, area)
22907 Lisp_Object window;
22908 int x, y;
22909 enum window_part area;
22911 struct window *w = XWINDOW (window);
22912 struct frame *f = XFRAME (w->frame);
22913 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22914 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22915 Lisp_Object pointer = Qnil;
22916 int charpos, dx, dy, width, height;
22917 Lisp_Object string, object = Qnil;
22918 Lisp_Object pos, help;
22920 Lisp_Object mouse_face;
22921 int original_x_pixel = x;
22922 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22923 struct glyph_row *row;
22925 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22927 int x0;
22928 struct glyph *end;
22930 string = mode_line_string (w, area, &x, &y, &charpos,
22931 &object, &dx, &dy, &width, &height);
22933 row = (area == ON_MODE_LINE
22934 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22935 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22937 /* Find glyph */
22938 if (row->mode_line_p && row->enabled_p)
22940 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22941 end = glyph + row->used[TEXT_AREA];
22943 for (x0 = original_x_pixel;
22944 glyph < end && x0 >= glyph->pixel_width;
22945 ++glyph)
22946 x0 -= glyph->pixel_width;
22948 if (glyph >= end)
22949 glyph = NULL;
22952 else
22954 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22955 string = marginal_area_string (w, area, &x, &y, &charpos,
22956 &object, &dx, &dy, &width, &height);
22959 help = Qnil;
22961 if (IMAGEP (object))
22963 Lisp_Object image_map, hotspot;
22964 if ((image_map = Fplist_get (XCDR (object), QCmap),
22965 !NILP (image_map))
22966 && (hotspot = find_hot_spot (image_map, dx, dy),
22967 CONSP (hotspot))
22968 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22970 Lisp_Object area_id, plist;
22972 area_id = XCAR (hotspot);
22973 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22974 If so, we could look for mouse-enter, mouse-leave
22975 properties in PLIST (and do something...). */
22976 hotspot = XCDR (hotspot);
22977 if (CONSP (hotspot)
22978 && (plist = XCAR (hotspot), CONSP (plist)))
22980 pointer = Fplist_get (plist, Qpointer);
22981 if (NILP (pointer))
22982 pointer = Qhand;
22983 help = Fplist_get (plist, Qhelp_echo);
22984 if (!NILP (help))
22986 help_echo_string = help;
22987 /* Is this correct? ++kfs */
22988 XSETWINDOW (help_echo_window, w);
22989 help_echo_object = w->buffer;
22990 help_echo_pos = charpos;
22994 if (NILP (pointer))
22995 pointer = Fplist_get (XCDR (object), QCpointer);
22998 if (STRINGP (string))
23000 pos = make_number (charpos);
23001 /* If we're on a string with `help-echo' text property, arrange
23002 for the help to be displayed. This is done by setting the
23003 global variable help_echo_string to the help string. */
23004 if (NILP (help))
23006 help = Fget_text_property (pos, Qhelp_echo, string);
23007 if (!NILP (help))
23009 help_echo_string = help;
23010 XSETWINDOW (help_echo_window, w);
23011 help_echo_object = string;
23012 help_echo_pos = charpos;
23016 if (NILP (pointer))
23017 pointer = Fget_text_property (pos, Qpointer, string);
23019 /* Change the mouse pointer according to what is under X/Y. */
23020 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23022 Lisp_Object map;
23023 map = Fget_text_property (pos, Qlocal_map, string);
23024 if (!KEYMAPP (map))
23025 map = Fget_text_property (pos, Qkeymap, string);
23026 if (!KEYMAPP (map))
23027 cursor = dpyinfo->vertical_scroll_bar_cursor;
23030 /* Change the mouse face according to what is under X/Y. */
23031 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23032 if (!NILP (mouse_face)
23033 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23034 && glyph)
23036 Lisp_Object b, e;
23038 struct glyph * tmp_glyph;
23040 int gpos;
23041 int gseq_length;
23042 int total_pixel_width;
23043 EMACS_INT ignore;
23045 int vpos, hpos;
23047 b = Fprevious_single_property_change (make_number (charpos + 1),
23048 Qmouse_face, string, Qnil);
23049 if (NILP (b))
23050 b = make_number (0);
23052 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23053 if (NILP (e))
23054 e = make_number (SCHARS (string));
23056 /* Calculate the position(glyph position: GPOS) of GLYPH in
23057 displayed string. GPOS is different from CHARPOS.
23059 CHARPOS is the position of glyph in internal string
23060 object. A mode line string format has structures which
23061 is converted to a flatten by emacs lisp interpreter.
23062 The internal string is an element of the structures.
23063 The displayed string is the flatten string. */
23064 gpos = 0;
23065 if (glyph > row_start_glyph)
23067 tmp_glyph = glyph - 1;
23068 while (tmp_glyph >= row_start_glyph
23069 && tmp_glyph->charpos >= XINT (b)
23070 && EQ (tmp_glyph->object, glyph->object))
23072 tmp_glyph--;
23073 gpos++;
23077 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23078 displayed string holding GLYPH.
23080 GSEQ_LENGTH is different from SCHARS (STRING).
23081 SCHARS (STRING) returns the length of the internal string. */
23082 for (tmp_glyph = glyph, gseq_length = gpos;
23083 tmp_glyph->charpos < XINT (e);
23084 tmp_glyph++, gseq_length++)
23086 if (!EQ (tmp_glyph->object, glyph->object))
23087 break;
23090 total_pixel_width = 0;
23091 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23092 total_pixel_width += tmp_glyph->pixel_width;
23094 /* Pre calculation of re-rendering position */
23095 vpos = (x - gpos);
23096 hpos = (area == ON_MODE_LINE
23097 ? (w->current_matrix)->nrows - 1
23098 : 0);
23100 /* If the re-rendering position is included in the last
23101 re-rendering area, we should do nothing. */
23102 if ( EQ (window, dpyinfo->mouse_face_window)
23103 && dpyinfo->mouse_face_beg_col <= vpos
23104 && vpos < dpyinfo->mouse_face_end_col
23105 && dpyinfo->mouse_face_beg_row == hpos )
23106 return;
23108 if (clear_mouse_face (dpyinfo))
23109 cursor = No_Cursor;
23111 dpyinfo->mouse_face_beg_col = vpos;
23112 dpyinfo->mouse_face_beg_row = hpos;
23114 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23115 dpyinfo->mouse_face_beg_y = 0;
23117 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23118 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23120 dpyinfo->mouse_face_end_x = 0;
23121 dpyinfo->mouse_face_end_y = 0;
23123 dpyinfo->mouse_face_past_end = 0;
23124 dpyinfo->mouse_face_window = window;
23126 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23127 charpos,
23128 0, 0, 0, &ignore,
23129 glyph->face_id, 1);
23130 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23132 if (NILP (pointer))
23133 pointer = Qhand;
23135 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23136 clear_mouse_face (dpyinfo);
23138 define_frame_cursor1 (f, cursor, pointer);
23142 /* EXPORT:
23143 Take proper action when the mouse has moved to position X, Y on
23144 frame F as regards highlighting characters that have mouse-face
23145 properties. Also de-highlighting chars where the mouse was before.
23146 X and Y can be negative or out of range. */
23148 void
23149 note_mouse_highlight (f, x, y)
23150 struct frame *f;
23151 int x, y;
23153 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23154 enum window_part part;
23155 Lisp_Object window;
23156 struct window *w;
23157 Cursor cursor = No_Cursor;
23158 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23159 struct buffer *b;
23161 /* When a menu is active, don't highlight because this looks odd. */
23162 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23163 if (popup_activated ())
23164 return;
23165 #endif
23167 if (NILP (Vmouse_highlight)
23168 || !f->glyphs_initialized_p)
23169 return;
23171 dpyinfo->mouse_face_mouse_x = x;
23172 dpyinfo->mouse_face_mouse_y = y;
23173 dpyinfo->mouse_face_mouse_frame = f;
23175 if (dpyinfo->mouse_face_defer)
23176 return;
23178 if (gc_in_progress)
23180 dpyinfo->mouse_face_deferred_gc = 1;
23181 return;
23184 /* Which window is that in? */
23185 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23187 /* If we were displaying active text in another window, clear that.
23188 Also clear if we move out of text area in same window. */
23189 if (! EQ (window, dpyinfo->mouse_face_window)
23190 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23191 && !NILP (dpyinfo->mouse_face_window)))
23192 clear_mouse_face (dpyinfo);
23194 /* Not on a window -> return. */
23195 if (!WINDOWP (window))
23196 return;
23198 /* Reset help_echo_string. It will get recomputed below. */
23199 help_echo_string = Qnil;
23201 /* Convert to window-relative pixel coordinates. */
23202 w = XWINDOW (window);
23203 frame_to_window_pixel_xy (w, &x, &y);
23205 /* Handle tool-bar window differently since it doesn't display a
23206 buffer. */
23207 if (EQ (window, f->tool_bar_window))
23209 note_tool_bar_highlight (f, x, y);
23210 return;
23213 /* Mouse is on the mode, header line or margin? */
23214 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23215 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23217 note_mode_line_or_margin_highlight (window, x, y, part);
23218 return;
23221 if (part == ON_VERTICAL_BORDER)
23223 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23224 help_echo_string = build_string ("drag-mouse-1: resize");
23226 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23227 || part == ON_SCROLL_BAR)
23228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23229 else
23230 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23232 /* Are we in a window whose display is up to date?
23233 And verify the buffer's text has not changed. */
23234 b = XBUFFER (w->buffer);
23235 if (part == ON_TEXT
23236 && EQ (w->window_end_valid, w->buffer)
23237 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23238 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23240 int hpos, vpos, pos, i, dx, dy, area;
23241 struct glyph *glyph;
23242 Lisp_Object object;
23243 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23244 Lisp_Object *overlay_vec = NULL;
23245 int noverlays;
23246 struct buffer *obuf;
23247 int obegv, ozv, same_region;
23249 /* Find the glyph under X/Y. */
23250 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23252 /* Look for :pointer property on image. */
23253 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23255 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23256 if (img != NULL && IMAGEP (img->spec))
23258 Lisp_Object image_map, hotspot;
23259 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23260 !NILP (image_map))
23261 && (hotspot = find_hot_spot (image_map,
23262 glyph->slice.x + dx,
23263 glyph->slice.y + dy),
23264 CONSP (hotspot))
23265 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23267 Lisp_Object area_id, plist;
23269 area_id = XCAR (hotspot);
23270 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23271 If so, we could look for mouse-enter, mouse-leave
23272 properties in PLIST (and do something...). */
23273 hotspot = XCDR (hotspot);
23274 if (CONSP (hotspot)
23275 && (plist = XCAR (hotspot), CONSP (plist)))
23277 pointer = Fplist_get (plist, Qpointer);
23278 if (NILP (pointer))
23279 pointer = Qhand;
23280 help_echo_string = Fplist_get (plist, Qhelp_echo);
23281 if (!NILP (help_echo_string))
23283 help_echo_window = window;
23284 help_echo_object = glyph->object;
23285 help_echo_pos = glyph->charpos;
23289 if (NILP (pointer))
23290 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23294 /* Clear mouse face if X/Y not over text. */
23295 if (glyph == NULL
23296 || area != TEXT_AREA
23297 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23299 if (clear_mouse_face (dpyinfo))
23300 cursor = No_Cursor;
23301 if (NILP (pointer))
23303 if (area != TEXT_AREA)
23304 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23305 else
23306 pointer = Vvoid_text_area_pointer;
23308 goto set_cursor;
23311 pos = glyph->charpos;
23312 object = glyph->object;
23313 if (!STRINGP (object) && !BUFFERP (object))
23314 goto set_cursor;
23316 /* If we get an out-of-range value, return now; avoid an error. */
23317 if (BUFFERP (object) && pos > BUF_Z (b))
23318 goto set_cursor;
23320 /* Make the window's buffer temporarily current for
23321 overlays_at and compute_char_face. */
23322 obuf = current_buffer;
23323 current_buffer = b;
23324 obegv = BEGV;
23325 ozv = ZV;
23326 BEGV = BEG;
23327 ZV = Z;
23329 /* Is this char mouse-active or does it have help-echo? */
23330 position = make_number (pos);
23332 if (BUFFERP (object))
23334 /* Put all the overlays we want in a vector in overlay_vec. */
23335 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23336 /* Sort overlays into increasing priority order. */
23337 noverlays = sort_overlays (overlay_vec, noverlays, w);
23339 else
23340 noverlays = 0;
23342 same_region = (EQ (window, dpyinfo->mouse_face_window)
23343 && vpos >= dpyinfo->mouse_face_beg_row
23344 && vpos <= dpyinfo->mouse_face_end_row
23345 && (vpos > dpyinfo->mouse_face_beg_row
23346 || hpos >= dpyinfo->mouse_face_beg_col)
23347 && (vpos < dpyinfo->mouse_face_end_row
23348 || hpos < dpyinfo->mouse_face_end_col
23349 || dpyinfo->mouse_face_past_end));
23351 if (same_region)
23352 cursor = No_Cursor;
23354 /* Check mouse-face highlighting. */
23355 if (! same_region
23356 /* If there exists an overlay with mouse-face overlapping
23357 the one we are currently highlighting, we have to
23358 check if we enter the overlapping overlay, and then
23359 highlight only that. */
23360 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23361 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23363 /* Find the highest priority overlay that has a mouse-face
23364 property. */
23365 overlay = Qnil;
23366 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23368 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23369 if (!NILP (mouse_face))
23370 overlay = overlay_vec[i];
23373 /* If we're actually highlighting the same overlay as
23374 before, there's no need to do that again. */
23375 if (!NILP (overlay)
23376 && EQ (overlay, dpyinfo->mouse_face_overlay))
23377 goto check_help_echo;
23379 dpyinfo->mouse_face_overlay = overlay;
23381 /* Clear the display of the old active region, if any. */
23382 if (clear_mouse_face (dpyinfo))
23383 cursor = No_Cursor;
23385 /* If no overlay applies, get a text property. */
23386 if (NILP (overlay))
23387 mouse_face = Fget_text_property (position, Qmouse_face, object);
23389 /* Handle the overlay case. */
23390 if (!NILP (overlay))
23392 /* Find the range of text around this char that
23393 should be active. */
23394 Lisp_Object before, after;
23395 EMACS_INT ignore;
23397 before = Foverlay_start (overlay);
23398 after = Foverlay_end (overlay);
23399 /* Record this as the current active region. */
23400 fast_find_position (w, XFASTINT (before),
23401 &dpyinfo->mouse_face_beg_col,
23402 &dpyinfo->mouse_face_beg_row,
23403 &dpyinfo->mouse_face_beg_x,
23404 &dpyinfo->mouse_face_beg_y, Qnil);
23406 dpyinfo->mouse_face_past_end
23407 = !fast_find_position (w, XFASTINT (after),
23408 &dpyinfo->mouse_face_end_col,
23409 &dpyinfo->mouse_face_end_row,
23410 &dpyinfo->mouse_face_end_x,
23411 &dpyinfo->mouse_face_end_y, Qnil);
23412 dpyinfo->mouse_face_window = window;
23414 dpyinfo->mouse_face_face_id
23415 = face_at_buffer_position (w, pos, 0, 0,
23416 &ignore, pos + 1,
23417 !dpyinfo->mouse_face_hidden);
23419 /* Display it as active. */
23420 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23421 cursor = No_Cursor;
23423 /* Handle the text property case. */
23424 else if (!NILP (mouse_face) && BUFFERP (object))
23426 /* Find the range of text around this char that
23427 should be active. */
23428 Lisp_Object before, after, beginning, end;
23429 EMACS_INT ignore;
23431 beginning = Fmarker_position (w->start);
23432 end = make_number (BUF_Z (XBUFFER (object))
23433 - XFASTINT (w->window_end_pos));
23434 before
23435 = Fprevious_single_property_change (make_number (pos + 1),
23436 Qmouse_face,
23437 object, beginning);
23438 after
23439 = Fnext_single_property_change (position, Qmouse_face,
23440 object, end);
23442 /* Record this as the current active region. */
23443 fast_find_position (w, XFASTINT (before),
23444 &dpyinfo->mouse_face_beg_col,
23445 &dpyinfo->mouse_face_beg_row,
23446 &dpyinfo->mouse_face_beg_x,
23447 &dpyinfo->mouse_face_beg_y, Qnil);
23448 dpyinfo->mouse_face_past_end
23449 = !fast_find_position (w, XFASTINT (after),
23450 &dpyinfo->mouse_face_end_col,
23451 &dpyinfo->mouse_face_end_row,
23452 &dpyinfo->mouse_face_end_x,
23453 &dpyinfo->mouse_face_end_y, Qnil);
23454 dpyinfo->mouse_face_window = window;
23456 if (BUFFERP (object))
23457 dpyinfo->mouse_face_face_id
23458 = face_at_buffer_position (w, pos, 0, 0,
23459 &ignore, pos + 1,
23460 !dpyinfo->mouse_face_hidden);
23462 /* Display it as active. */
23463 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23464 cursor = No_Cursor;
23466 else if (!NILP (mouse_face) && STRINGP (object))
23468 Lisp_Object b, e;
23469 EMACS_INT ignore;
23471 b = Fprevious_single_property_change (make_number (pos + 1),
23472 Qmouse_face,
23473 object, Qnil);
23474 e = Fnext_single_property_change (position, Qmouse_face,
23475 object, Qnil);
23476 if (NILP (b))
23477 b = make_number (0);
23478 if (NILP (e))
23479 e = make_number (SCHARS (object) - 1);
23481 fast_find_string_pos (w, XINT (b), object,
23482 &dpyinfo->mouse_face_beg_col,
23483 &dpyinfo->mouse_face_beg_row,
23484 &dpyinfo->mouse_face_beg_x,
23485 &dpyinfo->mouse_face_beg_y, 0);
23486 fast_find_string_pos (w, XINT (e), object,
23487 &dpyinfo->mouse_face_end_col,
23488 &dpyinfo->mouse_face_end_row,
23489 &dpyinfo->mouse_face_end_x,
23490 &dpyinfo->mouse_face_end_y, 1);
23491 dpyinfo->mouse_face_past_end = 0;
23492 dpyinfo->mouse_face_window = window;
23493 dpyinfo->mouse_face_face_id
23494 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23495 glyph->face_id, 1);
23496 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23497 cursor = No_Cursor;
23499 else if (STRINGP (object) && NILP (mouse_face))
23501 /* A string which doesn't have mouse-face, but
23502 the text ``under'' it might have. */
23503 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23504 int start = MATRIX_ROW_START_CHARPOS (r);
23506 pos = string_buffer_position (w, object, start);
23507 if (pos > 0)
23508 mouse_face = get_char_property_and_overlay (make_number (pos),
23509 Qmouse_face,
23510 w->buffer,
23511 &overlay);
23512 if (!NILP (mouse_face) && !NILP (overlay))
23514 Lisp_Object before = Foverlay_start (overlay);
23515 Lisp_Object after = Foverlay_end (overlay);
23516 EMACS_INT ignore;
23518 /* Note that we might not be able to find position
23519 BEFORE in the glyph matrix if the overlay is
23520 entirely covered by a `display' property. In
23521 this case, we overshoot. So let's stop in
23522 the glyph matrix before glyphs for OBJECT. */
23523 fast_find_position (w, XFASTINT (before),
23524 &dpyinfo->mouse_face_beg_col,
23525 &dpyinfo->mouse_face_beg_row,
23526 &dpyinfo->mouse_face_beg_x,
23527 &dpyinfo->mouse_face_beg_y,
23528 object);
23530 dpyinfo->mouse_face_past_end
23531 = !fast_find_position (w, XFASTINT (after),
23532 &dpyinfo->mouse_face_end_col,
23533 &dpyinfo->mouse_face_end_row,
23534 &dpyinfo->mouse_face_end_x,
23535 &dpyinfo->mouse_face_end_y,
23536 Qnil);
23537 dpyinfo->mouse_face_window = window;
23538 dpyinfo->mouse_face_face_id
23539 = face_at_buffer_position (w, pos, 0, 0,
23540 &ignore, pos + 1,
23541 !dpyinfo->mouse_face_hidden);
23543 /* Display it as active. */
23544 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23545 cursor = No_Cursor;
23550 check_help_echo:
23552 /* Look for a `help-echo' property. */
23553 if (NILP (help_echo_string)) {
23554 Lisp_Object help, overlay;
23556 /* Check overlays first. */
23557 help = overlay = Qnil;
23558 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23560 overlay = overlay_vec[i];
23561 help = Foverlay_get (overlay, Qhelp_echo);
23564 if (!NILP (help))
23566 help_echo_string = help;
23567 help_echo_window = window;
23568 help_echo_object = overlay;
23569 help_echo_pos = pos;
23571 else
23573 Lisp_Object object = glyph->object;
23574 int charpos = glyph->charpos;
23576 /* Try text properties. */
23577 if (STRINGP (object)
23578 && charpos >= 0
23579 && charpos < SCHARS (object))
23581 help = Fget_text_property (make_number (charpos),
23582 Qhelp_echo, object);
23583 if (NILP (help))
23585 /* If the string itself doesn't specify a help-echo,
23586 see if the buffer text ``under'' it does. */
23587 struct glyph_row *r
23588 = MATRIX_ROW (w->current_matrix, vpos);
23589 int start = MATRIX_ROW_START_CHARPOS (r);
23590 int pos = string_buffer_position (w, object, start);
23591 if (pos > 0)
23593 help = Fget_char_property (make_number (pos),
23594 Qhelp_echo, w->buffer);
23595 if (!NILP (help))
23597 charpos = pos;
23598 object = w->buffer;
23603 else if (BUFFERP (object)
23604 && charpos >= BEGV
23605 && charpos < ZV)
23606 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23607 object);
23609 if (!NILP (help))
23611 help_echo_string = help;
23612 help_echo_window = window;
23613 help_echo_object = object;
23614 help_echo_pos = charpos;
23619 /* Look for a `pointer' property. */
23620 if (NILP (pointer))
23622 /* Check overlays first. */
23623 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23624 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23626 if (NILP (pointer))
23628 Lisp_Object object = glyph->object;
23629 int charpos = glyph->charpos;
23631 /* Try text properties. */
23632 if (STRINGP (object)
23633 && charpos >= 0
23634 && charpos < SCHARS (object))
23636 pointer = Fget_text_property (make_number (charpos),
23637 Qpointer, object);
23638 if (NILP (pointer))
23640 /* If the string itself doesn't specify a pointer,
23641 see if the buffer text ``under'' it does. */
23642 struct glyph_row *r
23643 = MATRIX_ROW (w->current_matrix, vpos);
23644 int start = MATRIX_ROW_START_CHARPOS (r);
23645 int pos = string_buffer_position (w, object, start);
23646 if (pos > 0)
23647 pointer = Fget_char_property (make_number (pos),
23648 Qpointer, w->buffer);
23651 else if (BUFFERP (object)
23652 && charpos >= BEGV
23653 && charpos < ZV)
23654 pointer = Fget_text_property (make_number (charpos),
23655 Qpointer, object);
23659 BEGV = obegv;
23660 ZV = ozv;
23661 current_buffer = obuf;
23664 set_cursor:
23666 define_frame_cursor1 (f, cursor, pointer);
23670 /* EXPORT for RIF:
23671 Clear any mouse-face on window W. This function is part of the
23672 redisplay interface, and is called from try_window_id and similar
23673 functions to ensure the mouse-highlight is off. */
23675 void
23676 x_clear_window_mouse_face (w)
23677 struct window *w;
23679 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23680 Lisp_Object window;
23682 BLOCK_INPUT;
23683 XSETWINDOW (window, w);
23684 if (EQ (window, dpyinfo->mouse_face_window))
23685 clear_mouse_face (dpyinfo);
23686 UNBLOCK_INPUT;
23690 /* EXPORT:
23691 Just discard the mouse face information for frame F, if any.
23692 This is used when the size of F is changed. */
23694 void
23695 cancel_mouse_face (f)
23696 struct frame *f;
23698 Lisp_Object window;
23699 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23701 window = dpyinfo->mouse_face_window;
23702 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23704 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23705 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23706 dpyinfo->mouse_face_window = Qnil;
23711 #endif /* HAVE_WINDOW_SYSTEM */
23714 /***********************************************************************
23715 Exposure Events
23716 ***********************************************************************/
23718 #ifdef HAVE_WINDOW_SYSTEM
23720 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23721 which intersects rectangle R. R is in window-relative coordinates. */
23723 static void
23724 expose_area (w, row, r, area)
23725 struct window *w;
23726 struct glyph_row *row;
23727 XRectangle *r;
23728 enum glyph_row_area area;
23730 struct glyph *first = row->glyphs[area];
23731 struct glyph *end = row->glyphs[area] + row->used[area];
23732 struct glyph *last;
23733 int first_x, start_x, x;
23735 if (area == TEXT_AREA && row->fill_line_p)
23736 /* If row extends face to end of line write the whole line. */
23737 draw_glyphs (w, 0, row, area,
23738 0, row->used[area],
23739 DRAW_NORMAL_TEXT, 0);
23740 else
23742 /* Set START_X to the window-relative start position for drawing glyphs of
23743 AREA. The first glyph of the text area can be partially visible.
23744 The first glyphs of other areas cannot. */
23745 start_x = window_box_left_offset (w, area);
23746 x = start_x;
23747 if (area == TEXT_AREA)
23748 x += row->x;
23750 /* Find the first glyph that must be redrawn. */
23751 while (first < end
23752 && x + first->pixel_width < r->x)
23754 x += first->pixel_width;
23755 ++first;
23758 /* Find the last one. */
23759 last = first;
23760 first_x = x;
23761 while (last < end
23762 && x < r->x + r->width)
23764 x += last->pixel_width;
23765 ++last;
23768 /* Repaint. */
23769 if (last > first)
23770 draw_glyphs (w, first_x - start_x, row, area,
23771 first - row->glyphs[area], last - row->glyphs[area],
23772 DRAW_NORMAL_TEXT, 0);
23777 /* Redraw the parts of the glyph row ROW on window W intersecting
23778 rectangle R. R is in window-relative coordinates. Value is
23779 non-zero if mouse-face was overwritten. */
23781 static int
23782 expose_line (w, row, r)
23783 struct window *w;
23784 struct glyph_row *row;
23785 XRectangle *r;
23787 xassert (row->enabled_p);
23789 if (row->mode_line_p || w->pseudo_window_p)
23790 draw_glyphs (w, 0, row, TEXT_AREA,
23791 0, row->used[TEXT_AREA],
23792 DRAW_NORMAL_TEXT, 0);
23793 else
23795 if (row->used[LEFT_MARGIN_AREA])
23796 expose_area (w, row, r, LEFT_MARGIN_AREA);
23797 if (row->used[TEXT_AREA])
23798 expose_area (w, row, r, TEXT_AREA);
23799 if (row->used[RIGHT_MARGIN_AREA])
23800 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23801 draw_row_fringe_bitmaps (w, row);
23804 return row->mouse_face_p;
23808 /* Redraw those parts of glyphs rows during expose event handling that
23809 overlap other rows. Redrawing of an exposed line writes over parts
23810 of lines overlapping that exposed line; this function fixes that.
23812 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23813 row in W's current matrix that is exposed and overlaps other rows.
23814 LAST_OVERLAPPING_ROW is the last such row. */
23816 static void
23817 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23818 struct window *w;
23819 struct glyph_row *first_overlapping_row;
23820 struct glyph_row *last_overlapping_row;
23821 XRectangle *r;
23823 struct glyph_row *row;
23825 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23826 if (row->overlapping_p)
23828 xassert (row->enabled_p && !row->mode_line_p);
23830 row->clip = r;
23831 if (row->used[LEFT_MARGIN_AREA])
23832 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23834 if (row->used[TEXT_AREA])
23835 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23837 if (row->used[RIGHT_MARGIN_AREA])
23838 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23839 row->clip = NULL;
23844 /* Return non-zero if W's cursor intersects rectangle R. */
23846 static int
23847 phys_cursor_in_rect_p (w, r)
23848 struct window *w;
23849 XRectangle *r;
23851 XRectangle cr, result;
23852 struct glyph *cursor_glyph;
23853 struct glyph_row *row;
23855 if (w->phys_cursor.vpos >= 0
23856 && w->phys_cursor.vpos < w->current_matrix->nrows
23857 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23858 row->enabled_p)
23859 && row->cursor_in_fringe_p)
23861 /* Cursor is in the fringe. */
23862 cr.x = window_box_right_offset (w,
23863 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23864 ? RIGHT_MARGIN_AREA
23865 : TEXT_AREA));
23866 cr.y = row->y;
23867 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23868 cr.height = row->height;
23869 return x_intersect_rectangles (&cr, r, &result);
23872 cursor_glyph = get_phys_cursor_glyph (w);
23873 if (cursor_glyph)
23875 /* r is relative to W's box, but w->phys_cursor.x is relative
23876 to left edge of W's TEXT area. Adjust it. */
23877 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23878 cr.y = w->phys_cursor.y;
23879 cr.width = cursor_glyph->pixel_width;
23880 cr.height = w->phys_cursor_height;
23881 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23882 I assume the effect is the same -- and this is portable. */
23883 return x_intersect_rectangles (&cr, r, &result);
23885 /* If we don't understand the format, pretend we're not in the hot-spot. */
23886 return 0;
23890 /* EXPORT:
23891 Draw a vertical window border to the right of window W if W doesn't
23892 have vertical scroll bars. */
23894 void
23895 x_draw_vertical_border (w)
23896 struct window *w;
23898 struct frame *f = XFRAME (WINDOW_FRAME (w));
23900 /* We could do better, if we knew what type of scroll-bar the adjacent
23901 windows (on either side) have... But we don't :-(
23902 However, I think this works ok. ++KFS 2003-04-25 */
23904 /* Redraw borders between horizontally adjacent windows. Don't
23905 do it for frames with vertical scroll bars because either the
23906 right scroll bar of a window, or the left scroll bar of its
23907 neighbor will suffice as a border. */
23908 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23909 return;
23911 if (!WINDOW_RIGHTMOST_P (w)
23912 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23914 int x0, x1, y0, y1;
23916 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23917 y1 -= 1;
23919 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23920 x1 -= 1;
23922 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23924 else if (!WINDOW_LEFTMOST_P (w)
23925 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23927 int x0, x1, y0, y1;
23929 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23930 y1 -= 1;
23932 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23933 x0 -= 1;
23935 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23940 /* Redraw the part of window W intersection rectangle FR. Pixel
23941 coordinates in FR are frame-relative. Call this function with
23942 input blocked. Value is non-zero if the exposure overwrites
23943 mouse-face. */
23945 static int
23946 expose_window (w, fr)
23947 struct window *w;
23948 XRectangle *fr;
23950 struct frame *f = XFRAME (w->frame);
23951 XRectangle wr, r;
23952 int mouse_face_overwritten_p = 0;
23954 /* If window is not yet fully initialized, do nothing. This can
23955 happen when toolkit scroll bars are used and a window is split.
23956 Reconfiguring the scroll bar will generate an expose for a newly
23957 created window. */
23958 if (w->current_matrix == NULL)
23959 return 0;
23961 /* When we're currently updating the window, display and current
23962 matrix usually don't agree. Arrange for a thorough display
23963 later. */
23964 if (w == updated_window)
23966 SET_FRAME_GARBAGED (f);
23967 return 0;
23970 /* Frame-relative pixel rectangle of W. */
23971 wr.x = WINDOW_LEFT_EDGE_X (w);
23972 wr.y = WINDOW_TOP_EDGE_Y (w);
23973 wr.width = WINDOW_TOTAL_WIDTH (w);
23974 wr.height = WINDOW_TOTAL_HEIGHT (w);
23976 if (x_intersect_rectangles (fr, &wr, &r))
23978 int yb = window_text_bottom_y (w);
23979 struct glyph_row *row;
23980 int cursor_cleared_p;
23981 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23983 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23984 r.x, r.y, r.width, r.height));
23986 /* Convert to window coordinates. */
23987 r.x -= WINDOW_LEFT_EDGE_X (w);
23988 r.y -= WINDOW_TOP_EDGE_Y (w);
23990 /* Turn off the cursor. */
23991 if (!w->pseudo_window_p
23992 && phys_cursor_in_rect_p (w, &r))
23994 x_clear_cursor (w);
23995 cursor_cleared_p = 1;
23997 else
23998 cursor_cleared_p = 0;
24000 /* Update lines intersecting rectangle R. */
24001 first_overlapping_row = last_overlapping_row = NULL;
24002 for (row = w->current_matrix->rows;
24003 row->enabled_p;
24004 ++row)
24006 int y0 = row->y;
24007 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24009 if ((y0 >= r.y && y0 < r.y + r.height)
24010 || (y1 > r.y && y1 < r.y + r.height)
24011 || (r.y >= y0 && r.y < y1)
24012 || (r.y + r.height > y0 && r.y + r.height < y1))
24014 /* A header line may be overlapping, but there is no need
24015 to fix overlapping areas for them. KFS 2005-02-12 */
24016 if (row->overlapping_p && !row->mode_line_p)
24018 if (first_overlapping_row == NULL)
24019 first_overlapping_row = row;
24020 last_overlapping_row = row;
24023 row->clip = fr;
24024 if (expose_line (w, row, &r))
24025 mouse_face_overwritten_p = 1;
24026 row->clip = NULL;
24028 else if (row->overlapping_p)
24030 /* We must redraw a row overlapping the exposed area. */
24031 if (y0 < r.y
24032 ? y0 + row->phys_height > r.y
24033 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24035 if (first_overlapping_row == NULL)
24036 first_overlapping_row = row;
24037 last_overlapping_row = row;
24041 if (y1 >= yb)
24042 break;
24045 /* Display the mode line if there is one. */
24046 if (WINDOW_WANTS_MODELINE_P (w)
24047 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24048 row->enabled_p)
24049 && row->y < r.y + r.height)
24051 if (expose_line (w, row, &r))
24052 mouse_face_overwritten_p = 1;
24055 if (!w->pseudo_window_p)
24057 /* Fix the display of overlapping rows. */
24058 if (first_overlapping_row)
24059 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24060 fr);
24062 /* Draw border between windows. */
24063 x_draw_vertical_border (w);
24065 /* Turn the cursor on again. */
24066 if (cursor_cleared_p)
24067 update_window_cursor (w, 1);
24071 return mouse_face_overwritten_p;
24076 /* Redraw (parts) of all windows in the window tree rooted at W that
24077 intersect R. R contains frame pixel coordinates. Value is
24078 non-zero if the exposure overwrites mouse-face. */
24080 static int
24081 expose_window_tree (w, r)
24082 struct window *w;
24083 XRectangle *r;
24085 struct frame *f = XFRAME (w->frame);
24086 int mouse_face_overwritten_p = 0;
24088 while (w && !FRAME_GARBAGED_P (f))
24090 if (!NILP (w->hchild))
24091 mouse_face_overwritten_p
24092 |= expose_window_tree (XWINDOW (w->hchild), r);
24093 else if (!NILP (w->vchild))
24094 mouse_face_overwritten_p
24095 |= expose_window_tree (XWINDOW (w->vchild), r);
24096 else
24097 mouse_face_overwritten_p |= expose_window (w, r);
24099 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24102 return mouse_face_overwritten_p;
24106 /* EXPORT:
24107 Redisplay an exposed area of frame F. X and Y are the upper-left
24108 corner of the exposed rectangle. W and H are width and height of
24109 the exposed area. All are pixel values. W or H zero means redraw
24110 the entire frame. */
24112 void
24113 expose_frame (f, x, y, w, h)
24114 struct frame *f;
24115 int x, y, w, h;
24117 XRectangle r;
24118 int mouse_face_overwritten_p = 0;
24120 TRACE ((stderr, "expose_frame "));
24122 /* No need to redraw if frame will be redrawn soon. */
24123 if (FRAME_GARBAGED_P (f))
24125 TRACE ((stderr, " garbaged\n"));
24126 return;
24129 /* If basic faces haven't been realized yet, there is no point in
24130 trying to redraw anything. This can happen when we get an expose
24131 event while Emacs is starting, e.g. by moving another window. */
24132 if (FRAME_FACE_CACHE (f) == NULL
24133 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24135 TRACE ((stderr, " no faces\n"));
24136 return;
24139 if (w == 0 || h == 0)
24141 r.x = r.y = 0;
24142 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24143 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24145 else
24147 r.x = x;
24148 r.y = y;
24149 r.width = w;
24150 r.height = h;
24153 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24154 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24156 if (WINDOWP (f->tool_bar_window))
24157 mouse_face_overwritten_p
24158 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24160 #ifdef HAVE_X_WINDOWS
24161 #ifndef MSDOS
24162 #ifndef USE_X_TOOLKIT
24163 if (WINDOWP (f->menu_bar_window))
24164 mouse_face_overwritten_p
24165 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24166 #endif /* not USE_X_TOOLKIT */
24167 #endif
24168 #endif
24170 /* Some window managers support a focus-follows-mouse style with
24171 delayed raising of frames. Imagine a partially obscured frame,
24172 and moving the mouse into partially obscured mouse-face on that
24173 frame. The visible part of the mouse-face will be highlighted,
24174 then the WM raises the obscured frame. With at least one WM, KDE
24175 2.1, Emacs is not getting any event for the raising of the frame
24176 (even tried with SubstructureRedirectMask), only Expose events.
24177 These expose events will draw text normally, i.e. not
24178 highlighted. Which means we must redo the highlight here.
24179 Subsume it under ``we love X''. --gerd 2001-08-15 */
24180 /* Included in Windows version because Windows most likely does not
24181 do the right thing if any third party tool offers
24182 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24183 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24185 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24186 if (f == dpyinfo->mouse_face_mouse_frame)
24188 int x = dpyinfo->mouse_face_mouse_x;
24189 int y = dpyinfo->mouse_face_mouse_y;
24190 clear_mouse_face (dpyinfo);
24191 note_mouse_highlight (f, x, y);
24197 /* EXPORT:
24198 Determine the intersection of two rectangles R1 and R2. Return
24199 the intersection in *RESULT. Value is non-zero if RESULT is not
24200 empty. */
24203 x_intersect_rectangles (r1, r2, result)
24204 XRectangle *r1, *r2, *result;
24206 XRectangle *left, *right;
24207 XRectangle *upper, *lower;
24208 int intersection_p = 0;
24210 /* Rearrange so that R1 is the left-most rectangle. */
24211 if (r1->x < r2->x)
24212 left = r1, right = r2;
24213 else
24214 left = r2, right = r1;
24216 /* X0 of the intersection is right.x0, if this is inside R1,
24217 otherwise there is no intersection. */
24218 if (right->x <= left->x + left->width)
24220 result->x = right->x;
24222 /* The right end of the intersection is the minimum of the
24223 the right ends of left and right. */
24224 result->width = (min (left->x + left->width, right->x + right->width)
24225 - result->x);
24227 /* Same game for Y. */
24228 if (r1->y < r2->y)
24229 upper = r1, lower = r2;
24230 else
24231 upper = r2, lower = r1;
24233 /* The upper end of the intersection is lower.y0, if this is inside
24234 of upper. Otherwise, there is no intersection. */
24235 if (lower->y <= upper->y + upper->height)
24237 result->y = lower->y;
24239 /* The lower end of the intersection is the minimum of the lower
24240 ends of upper and lower. */
24241 result->height = (min (lower->y + lower->height,
24242 upper->y + upper->height)
24243 - result->y);
24244 intersection_p = 1;
24248 return intersection_p;
24251 #endif /* HAVE_WINDOW_SYSTEM */
24254 /***********************************************************************
24255 Initialization
24256 ***********************************************************************/
24258 void
24259 syms_of_xdisp ()
24261 Vwith_echo_area_save_vector = Qnil;
24262 staticpro (&Vwith_echo_area_save_vector);
24264 Vmessage_stack = Qnil;
24265 staticpro (&Vmessage_stack);
24267 Qinhibit_redisplay = intern ("inhibit-redisplay");
24268 staticpro (&Qinhibit_redisplay);
24270 message_dolog_marker1 = Fmake_marker ();
24271 staticpro (&message_dolog_marker1);
24272 message_dolog_marker2 = Fmake_marker ();
24273 staticpro (&message_dolog_marker2);
24274 message_dolog_marker3 = Fmake_marker ();
24275 staticpro (&message_dolog_marker3);
24277 #if GLYPH_DEBUG
24278 defsubr (&Sdump_frame_glyph_matrix);
24279 defsubr (&Sdump_glyph_matrix);
24280 defsubr (&Sdump_glyph_row);
24281 defsubr (&Sdump_tool_bar_row);
24282 defsubr (&Strace_redisplay);
24283 defsubr (&Strace_to_stderr);
24284 #endif
24285 #ifdef HAVE_WINDOW_SYSTEM
24286 defsubr (&Stool_bar_lines_needed);
24287 defsubr (&Slookup_image_map);
24288 #endif
24289 defsubr (&Sformat_mode_line);
24290 defsubr (&Sinvisible_p);
24292 staticpro (&Qmenu_bar_update_hook);
24293 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24295 staticpro (&Qoverriding_terminal_local_map);
24296 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24298 staticpro (&Qoverriding_local_map);
24299 Qoverriding_local_map = intern ("overriding-local-map");
24301 staticpro (&Qwindow_scroll_functions);
24302 Qwindow_scroll_functions = intern ("window-scroll-functions");
24304 staticpro (&Qwindow_text_change_functions);
24305 Qwindow_text_change_functions = intern ("window-text-change-functions");
24307 staticpro (&Qredisplay_end_trigger_functions);
24308 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24310 staticpro (&Qinhibit_point_motion_hooks);
24311 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24313 Qeval = intern ("eval");
24314 staticpro (&Qeval);
24316 QCdata = intern (":data");
24317 staticpro (&QCdata);
24318 Qdisplay = intern ("display");
24319 staticpro (&Qdisplay);
24320 Qspace_width = intern ("space-width");
24321 staticpro (&Qspace_width);
24322 Qraise = intern ("raise");
24323 staticpro (&Qraise);
24324 Qslice = intern ("slice");
24325 staticpro (&Qslice);
24326 Qspace = intern ("space");
24327 staticpro (&Qspace);
24328 Qmargin = intern ("margin");
24329 staticpro (&Qmargin);
24330 Qpointer = intern ("pointer");
24331 staticpro (&Qpointer);
24332 Qleft_margin = intern ("left-margin");
24333 staticpro (&Qleft_margin);
24334 Qright_margin = intern ("right-margin");
24335 staticpro (&Qright_margin);
24336 Qcenter = intern ("center");
24337 staticpro (&Qcenter);
24338 Qline_height = intern ("line-height");
24339 staticpro (&Qline_height);
24340 QCalign_to = intern (":align-to");
24341 staticpro (&QCalign_to);
24342 QCrelative_width = intern (":relative-width");
24343 staticpro (&QCrelative_width);
24344 QCrelative_height = intern (":relative-height");
24345 staticpro (&QCrelative_height);
24346 QCeval = intern (":eval");
24347 staticpro (&QCeval);
24348 QCpropertize = intern (":propertize");
24349 staticpro (&QCpropertize);
24350 QCfile = intern (":file");
24351 staticpro (&QCfile);
24352 Qfontified = intern ("fontified");
24353 staticpro (&Qfontified);
24354 Qfontification_functions = intern ("fontification-functions");
24355 staticpro (&Qfontification_functions);
24356 Qtrailing_whitespace = intern ("trailing-whitespace");
24357 staticpro (&Qtrailing_whitespace);
24358 Qescape_glyph = intern ("escape-glyph");
24359 staticpro (&Qescape_glyph);
24360 Qnobreak_space = intern ("nobreak-space");
24361 staticpro (&Qnobreak_space);
24362 Qimage = intern ("image");
24363 staticpro (&Qimage);
24364 QCmap = intern (":map");
24365 staticpro (&QCmap);
24366 QCpointer = intern (":pointer");
24367 staticpro (&QCpointer);
24368 Qrect = intern ("rect");
24369 staticpro (&Qrect);
24370 Qcircle = intern ("circle");
24371 staticpro (&Qcircle);
24372 Qpoly = intern ("poly");
24373 staticpro (&Qpoly);
24374 Qmessage_truncate_lines = intern ("message-truncate-lines");
24375 staticpro (&Qmessage_truncate_lines);
24376 Qgrow_only = intern ("grow-only");
24377 staticpro (&Qgrow_only);
24378 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24379 staticpro (&Qinhibit_menubar_update);
24380 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24381 staticpro (&Qinhibit_eval_during_redisplay);
24382 Qposition = intern ("position");
24383 staticpro (&Qposition);
24384 Qbuffer_position = intern ("buffer-position");
24385 staticpro (&Qbuffer_position);
24386 Qobject = intern ("object");
24387 staticpro (&Qobject);
24388 Qbar = intern ("bar");
24389 staticpro (&Qbar);
24390 Qhbar = intern ("hbar");
24391 staticpro (&Qhbar);
24392 Qbox = intern ("box");
24393 staticpro (&Qbox);
24394 Qhollow = intern ("hollow");
24395 staticpro (&Qhollow);
24396 Qhand = intern ("hand");
24397 staticpro (&Qhand);
24398 Qarrow = intern ("arrow");
24399 staticpro (&Qarrow);
24400 Qtext = intern ("text");
24401 staticpro (&Qtext);
24402 Qrisky_local_variable = intern ("risky-local-variable");
24403 staticpro (&Qrisky_local_variable);
24404 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24405 staticpro (&Qinhibit_free_realized_faces);
24407 list_of_error = Fcons (Fcons (intern ("error"),
24408 Fcons (intern ("void-variable"), Qnil)),
24409 Qnil);
24410 staticpro (&list_of_error);
24412 Qlast_arrow_position = intern ("last-arrow-position");
24413 staticpro (&Qlast_arrow_position);
24414 Qlast_arrow_string = intern ("last-arrow-string");
24415 staticpro (&Qlast_arrow_string);
24417 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24418 staticpro (&Qoverlay_arrow_string);
24419 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24420 staticpro (&Qoverlay_arrow_bitmap);
24422 echo_buffer[0] = echo_buffer[1] = Qnil;
24423 staticpro (&echo_buffer[0]);
24424 staticpro (&echo_buffer[1]);
24426 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24427 staticpro (&echo_area_buffer[0]);
24428 staticpro (&echo_area_buffer[1]);
24430 Vmessages_buffer_name = build_string ("*Messages*");
24431 staticpro (&Vmessages_buffer_name);
24433 mode_line_proptrans_alist = Qnil;
24434 staticpro (&mode_line_proptrans_alist);
24435 mode_line_string_list = Qnil;
24436 staticpro (&mode_line_string_list);
24437 mode_line_string_face = Qnil;
24438 staticpro (&mode_line_string_face);
24439 mode_line_string_face_prop = Qnil;
24440 staticpro (&mode_line_string_face_prop);
24441 Vmode_line_unwind_vector = Qnil;
24442 staticpro (&Vmode_line_unwind_vector);
24444 help_echo_string = Qnil;
24445 staticpro (&help_echo_string);
24446 help_echo_object = Qnil;
24447 staticpro (&help_echo_object);
24448 help_echo_window = Qnil;
24449 staticpro (&help_echo_window);
24450 previous_help_echo_string = Qnil;
24451 staticpro (&previous_help_echo_string);
24452 help_echo_pos = -1;
24454 #ifdef HAVE_WINDOW_SYSTEM
24455 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24456 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24457 For example, if a block cursor is over a tab, it will be drawn as
24458 wide as that tab on the display. */);
24459 x_stretch_cursor_p = 0;
24460 #endif
24462 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24463 doc: /* *Non-nil means highlight trailing whitespace.
24464 The face used for trailing whitespace is `trailing-whitespace'. */);
24465 Vshow_trailing_whitespace = Qnil;
24467 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24468 doc: /* *Control highlighting of nobreak space and soft hyphen.
24469 A value of t means highlight the character itself (for nobreak space,
24470 use face `nobreak-space').
24471 A value of nil means no highlighting.
24472 Other values mean display the escape glyph followed by an ordinary
24473 space or ordinary hyphen. */);
24474 Vnobreak_char_display = Qt;
24476 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24477 doc: /* *The pointer shape to show in void text areas.
24478 A value of nil means to show the text pointer. Other options are `arrow',
24479 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24480 Vvoid_text_area_pointer = Qarrow;
24482 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24483 doc: /* Non-nil means don't actually do any redisplay.
24484 This is used for internal purposes. */);
24485 Vinhibit_redisplay = Qnil;
24487 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24488 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24489 Vglobal_mode_string = Qnil;
24491 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24492 doc: /* Marker for where to display an arrow on top of the buffer text.
24493 This must be the beginning of a line in order to work.
24494 See also `overlay-arrow-string'. */);
24495 Voverlay_arrow_position = Qnil;
24497 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24498 doc: /* String to display as an arrow in non-window frames.
24499 See also `overlay-arrow-position'. */);
24500 Voverlay_arrow_string = build_string ("=>");
24502 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24503 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24504 The symbols on this list are examined during redisplay to determine
24505 where to display overlay arrows. */);
24506 Voverlay_arrow_variable_list
24507 = Fcons (intern ("overlay-arrow-position"), Qnil);
24509 DEFVAR_INT ("scroll-step", &scroll_step,
24510 doc: /* *The number of lines to try scrolling a window by when point moves out.
24511 If that fails to bring point back on frame, point is centered instead.
24512 If this is zero, point is always centered after it moves off frame.
24513 If you want scrolling to always be a line at a time, you should set
24514 `scroll-conservatively' to a large value rather than set this to 1. */);
24516 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24517 doc: /* *Scroll up to this many lines, to bring point back on screen.
24518 If point moves off-screen, redisplay will scroll by up to
24519 `scroll-conservatively' lines in order to bring point just barely
24520 onto the screen again. If that cannot be done, then redisplay
24521 recenters point as usual.
24523 A value of zero means always recenter point if it moves off screen. */);
24524 scroll_conservatively = 0;
24526 DEFVAR_INT ("scroll-margin", &scroll_margin,
24527 doc: /* *Number of lines of margin at the top and bottom of a window.
24528 Recenter the window whenever point gets within this many lines
24529 of the top or bottom of the window. */);
24530 scroll_margin = 0;
24532 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24533 doc: /* Pixels per inch value for non-window system displays.
24534 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24535 Vdisplay_pixels_per_inch = make_float (72.0);
24537 #if GLYPH_DEBUG
24538 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24539 #endif
24541 DEFVAR_BOOL ("truncate-partial-width-windows",
24542 &truncate_partial_width_windows,
24543 doc: /* *Non-nil means truncate lines in all windows less than full frame wide.
24544 Nil means to respect the value of `truncate-lines'. */);
24545 truncate_partial_width_windows = 1;
24547 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24548 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24549 Any other value means to use the appropriate face, `mode-line',
24550 `header-line', or `menu' respectively. */);
24551 mode_line_inverse_video = 1;
24553 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24554 doc: /* *Maximum buffer size for which line number should be displayed.
24555 If the buffer is bigger than this, the line number does not appear
24556 in the mode line. A value of nil means no limit. */);
24557 Vline_number_display_limit = Qnil;
24559 DEFVAR_INT ("line-number-display-limit-width",
24560 &line_number_display_limit_width,
24561 doc: /* *Maximum line width (in characters) for line number display.
24562 If the average length of the lines near point is bigger than this, then the
24563 line number may be omitted from the mode line. */);
24564 line_number_display_limit_width = 200;
24566 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24567 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24568 highlight_nonselected_windows = 0;
24570 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24571 doc: /* Non-nil if more than one frame is visible on this display.
24572 Minibuffer-only frames don't count, but iconified frames do.
24573 This variable is not guaranteed to be accurate except while processing
24574 `frame-title-format' and `icon-title-format'. */);
24576 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24577 doc: /* Template for displaying the title bar of visible frames.
24578 \(Assuming the window manager supports this feature.)
24580 This variable has the same structure as `mode-line-format', except that
24581 the %c and %l constructs are ignored. It is used only on frames for
24582 which no explicit name has been set \(see `modify-frame-parameters'). */);
24584 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24585 doc: /* Template for displaying the title bar of an iconified frame.
24586 \(Assuming the window manager supports this feature.)
24587 This variable has the same structure as `mode-line-format' (which see),
24588 and is used only on frames for which no explicit name has been set
24589 \(see `modify-frame-parameters'). */);
24590 Vicon_title_format
24591 = Vframe_title_format
24592 = Fcons (intern ("multiple-frames"),
24593 Fcons (build_string ("%b"),
24594 Fcons (Fcons (empty_unibyte_string,
24595 Fcons (intern ("invocation-name"),
24596 Fcons (build_string ("@"),
24597 Fcons (intern ("system-name"),
24598 Qnil)))),
24599 Qnil)));
24601 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24602 doc: /* Maximum number of lines to keep in the message log buffer.
24603 If nil, disable message logging. If t, log messages but don't truncate
24604 the buffer when it becomes large. */);
24605 Vmessage_log_max = make_number (100);
24607 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24608 doc: /* Functions called before redisplay, if window sizes have changed.
24609 The value should be a list of functions that take one argument.
24610 Just before redisplay, for each frame, if any of its windows have changed
24611 size since the last redisplay, or have been split or deleted,
24612 all the functions in the list are called, with the frame as argument. */);
24613 Vwindow_size_change_functions = Qnil;
24615 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24616 doc: /* List of functions to call before redisplaying a window with scrolling.
24617 Each function is called with two arguments, the window
24618 and its new display-start position. Note that the value of `window-end'
24619 is not valid when these functions are called. */);
24620 Vwindow_scroll_functions = Qnil;
24622 DEFVAR_LISP ("window-text-change-functions",
24623 &Vwindow_text_change_functions,
24624 doc: /* Functions to call in redisplay when text in the window might change. */);
24625 Vwindow_text_change_functions = Qnil;
24627 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24628 doc: /* Functions called when redisplay of a window reaches the end trigger.
24629 Each function is called with two arguments, the window and the end trigger value.
24630 See `set-window-redisplay-end-trigger'. */);
24631 Vredisplay_end_trigger_functions = Qnil;
24633 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24634 doc: /* *Non-nil means autoselect window with mouse pointer.
24635 If nil, do not autoselect windows.
24636 A positive number means delay autoselection by that many seconds: a
24637 window is autoselected only after the mouse has remained in that
24638 window for the duration of the delay.
24639 A negative number has a similar effect, but causes windows to be
24640 autoselected only after the mouse has stopped moving. \(Because of
24641 the way Emacs compares mouse events, you will occasionally wait twice
24642 that time before the window gets selected.\)
24643 Any other value means to autoselect window instantaneously when the
24644 mouse pointer enters it.
24646 Autoselection selects the minibuffer only if it is active, and never
24647 unselects the minibuffer if it is active.
24649 When customizing this variable make sure that the actual value of
24650 `focus-follows-mouse' matches the behavior of your window manager. */);
24651 Vmouse_autoselect_window = Qnil;
24653 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24654 doc: /* *Non-nil means automatically resize tool-bars.
24655 This dynamically changes the tool-bar's height to the minimum height
24656 that is needed to make all tool-bar items visible.
24657 If value is `grow-only', the tool-bar's height is only increased
24658 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24659 Vauto_resize_tool_bars = Qt;
24661 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24662 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24663 auto_raise_tool_bar_buttons_p = 1;
24665 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24666 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24667 make_cursor_line_fully_visible_p = 1;
24669 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24670 doc: /* *Border below tool-bar in pixels.
24671 If an integer, use it as the height of the border.
24672 If it is one of `internal-border-width' or `border-width', use the
24673 value of the corresponding frame parameter.
24674 Otherwise, no border is added below the tool-bar. */);
24675 Vtool_bar_border = Qinternal_border_width;
24677 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24678 doc: /* *Margin around tool-bar buttons in pixels.
24679 If an integer, use that for both horizontal and vertical margins.
24680 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24681 HORZ specifying the horizontal margin, and VERT specifying the
24682 vertical margin. */);
24683 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24685 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24686 doc: /* *Relief thickness of tool-bar buttons. */);
24687 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24689 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24690 doc: /* List of functions to call to fontify regions of text.
24691 Each function is called with one argument POS. Functions must
24692 fontify a region starting at POS in the current buffer, and give
24693 fontified regions the property `fontified'. */);
24694 Vfontification_functions = Qnil;
24695 Fmake_variable_buffer_local (Qfontification_functions);
24697 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24698 &unibyte_display_via_language_environment,
24699 doc: /* *Non-nil means display unibyte text according to language environment.
24700 Specifically this means that unibyte non-ASCII characters
24701 are displayed by converting them to the equivalent multibyte characters
24702 according to the current language environment. As a result, they are
24703 displayed according to the current fontset. */);
24704 unibyte_display_via_language_environment = 0;
24706 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24707 doc: /* *Maximum height for resizing mini-windows.
24708 If a float, it specifies a fraction of the mini-window frame's height.
24709 If an integer, it specifies a number of lines. */);
24710 Vmax_mini_window_height = make_float (0.25);
24712 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24713 doc: /* *How to resize mini-windows.
24714 A value of nil means don't automatically resize mini-windows.
24715 A value of t means resize them to fit the text displayed in them.
24716 A value of `grow-only', the default, means let mini-windows grow
24717 only, until their display becomes empty, at which point the windows
24718 go back to their normal size. */);
24719 Vresize_mini_windows = Qgrow_only;
24721 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24722 doc: /* Alist specifying how to blink the cursor off.
24723 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24724 `cursor-type' frame-parameter or variable equals ON-STATE,
24725 comparing using `equal', Emacs uses OFF-STATE to specify
24726 how to blink it off. ON-STATE and OFF-STATE are values for
24727 the `cursor-type' frame parameter.
24729 If a frame's ON-STATE has no entry in this list,
24730 the frame's other specifications determine how to blink the cursor off. */);
24731 Vblink_cursor_alist = Qnil;
24733 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24734 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24735 automatic_hscrolling_p = 1;
24736 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
24737 staticpro (&Qauto_hscroll_mode);
24739 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24740 doc: /* *How many columns away from the window edge point is allowed to get
24741 before automatic hscrolling will horizontally scroll the window. */);
24742 hscroll_margin = 5;
24744 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24745 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24746 When point is less than `hscroll-margin' columns from the window
24747 edge, automatic hscrolling will scroll the window by the amount of columns
24748 determined by this variable. If its value is a positive integer, scroll that
24749 many columns. If it's a positive floating-point number, it specifies the
24750 fraction of the window's width to scroll. If it's nil or zero, point will be
24751 centered horizontally after the scroll. Any other value, including negative
24752 numbers, are treated as if the value were zero.
24754 Automatic hscrolling always moves point outside the scroll margin, so if
24755 point was more than scroll step columns inside the margin, the window will
24756 scroll more than the value given by the scroll step.
24758 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24759 and `scroll-right' overrides this variable's effect. */);
24760 Vhscroll_step = make_number (0);
24762 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24763 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24764 Bind this around calls to `message' to let it take effect. */);
24765 message_truncate_lines = 0;
24767 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24768 doc: /* Normal hook run to update the menu bar definitions.
24769 Redisplay runs this hook before it redisplays the menu bar.
24770 This is used to update submenus such as Buffers,
24771 whose contents depend on various data. */);
24772 Vmenu_bar_update_hook = Qnil;
24774 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24775 doc: /* Frame for which we are updating a menu.
24776 The enable predicate for a menu binding should check this variable. */);
24777 Vmenu_updating_frame = Qnil;
24779 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24780 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24781 inhibit_menubar_update = 0;
24783 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24784 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24785 inhibit_eval_during_redisplay = 0;
24787 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24788 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24789 inhibit_free_realized_faces = 0;
24791 #if GLYPH_DEBUG
24792 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24793 doc: /* Inhibit try_window_id display optimization. */);
24794 inhibit_try_window_id = 0;
24796 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24797 doc: /* Inhibit try_window_reusing display optimization. */);
24798 inhibit_try_window_reusing = 0;
24800 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24801 doc: /* Inhibit try_cursor_movement display optimization. */);
24802 inhibit_try_cursor_movement = 0;
24803 #endif /* GLYPH_DEBUG */
24805 DEFVAR_INT ("overline-margin", &overline_margin,
24806 doc: /* *Space between overline and text, in pixels.
24807 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24808 margin to the caracter height. */);
24809 overline_margin = 2;
24811 DEFVAR_INT ("underline-minimum-offset",
24812 &underline_minimum_offset,
24813 doc: /* Minimum distance between baseline and underline.
24814 This can improve legibility of underlined text at small font sizes,
24815 particularly when using variable `x-use-underline-position-properties'
24816 with fonts that specify an UNDERLINE_POSITION relatively close to the
24817 baseline. The default value is 1. */);
24818 underline_minimum_offset = 1;
24822 /* Initialize this module when Emacs starts. */
24824 void
24825 init_xdisp ()
24827 Lisp_Object root_window;
24828 struct window *mini_w;
24830 current_header_line_height = current_mode_line_height = -1;
24832 CHARPOS (this_line_start_pos) = 0;
24834 mini_w = XWINDOW (minibuf_window);
24835 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24837 if (!noninteractive)
24839 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24840 int i;
24842 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24843 set_window_height (root_window,
24844 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24846 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24847 set_window_height (minibuf_window, 1, 0);
24849 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24850 mini_w->total_cols = make_number (FRAME_COLS (f));
24852 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24853 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24854 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24856 /* The default ellipsis glyphs `...'. */
24857 for (i = 0; i < 3; ++i)
24858 default_invis_vector[i] = make_number ('.');
24862 /* Allocate the buffer for frame titles.
24863 Also used for `format-mode-line'. */
24864 int size = 100;
24865 mode_line_noprop_buf = (char *) xmalloc (size);
24866 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24867 mode_line_noprop_ptr = mode_line_noprop_buf;
24868 mode_line_target = MODE_LINE_DISPLAY;
24871 help_echo_showing_p = 0;
24875 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24876 (do not change this comment) */