(syms_of_ntterm) <w32-use-full-screen-buffer>: Doc fix - default value changed.
[emacs.git] / src / xdisp.c
blob55ee9fc20e75cd65bdde48f519e59191986f481a
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
289 int auto_resize_tool_bars_p;
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
295 int x_stretch_cursor_p;
297 /* Non-nil means don't actually do any redisplay. */
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
303 int inhibit_eval_during_redisplay;
305 /* Names of text properties relevant for redisplay. */
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
310 /* Symbols used in text property values. */
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
324 /* Non-nil means highlight trailing whitespace. */
326 Lisp_Object Vshow_trailing_whitespace;
328 /* Non-nil means escape non-break space and hyphens. */
330 Lisp_Object Vnobreak_char_display;
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
344 #endif /* HAVE_WINDOW_SYSTEM */
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
350 Lisp_Object Vvoid_text_area_pointer;
352 /* Name of the face used to highlight trailing whitespace. */
354 Lisp_Object Qtrailing_whitespace;
356 /* Name and number of the face used to highlight escape glyphs. */
358 Lisp_Object Qescape_glyph;
360 /* Name and number of the face used to highlight non-breaking spaces. */
362 Lisp_Object Qnobreak_space;
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
367 Lisp_Object Qimage;
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
376 int noninteractive_need_newline;
378 /* Non-zero means print newline to message log before next message. */
380 static int message_log_need_newline;
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
394 static struct text_pos this_line_start_pos;
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
399 static struct text_pos this_line_end_pos;
401 /* The vertical positions and the height of this line. */
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
410 static int this_line_start_x;
412 /* Buffer that this_line_.* variables are referring to. */
414 static struct buffer *this_line_buffer;
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
419 int truncate_partial_width_windows;
421 /* A flag to control how to display unibyte 8-bit character. */
423 int unibyte_display_via_language_environment;
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
429 int multiple_frames;
431 Lisp_Object Vglobal_mode_string;
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
438 Lisp_Object Voverlay_arrow_variable_list;
440 /* Marker for where to display an arrow on top of the buffer text. */
442 Lisp_Object Voverlay_arrow_position;
444 /* String to display for the arrow. Only used on terminal frames. */
446 Lisp_Object Voverlay_arrow_string;
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
460 /* Like mode-line-format, but for the title bar on a visible frame. */
462 Lisp_Object Vframe_title_format;
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
466 Lisp_Object Vicon_title_format;
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
472 static Lisp_Object Vwindow_size_change_functions;
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
476 /* Nonzero if an overlay arrow has been displayed in this window. */
478 static int overlay_arrow_seen;
480 /* Nonzero means highlight the region even in nonselected windows. */
482 int highlight_nonselected_windows;
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
487 static EMACS_INT scroll_step;
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
492 static EMACS_INT scroll_conservatively;
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
499 EMACS_INT scroll_margin;
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
505 int buffer_shared;
507 /* Vector containing glyphs for an ellipsis `...'. */
509 static Lisp_Object default_invis_vector[3];
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
515 This variable is deprecated. */
517 int mode_line_inverse_video;
519 /* Prompt to display in front of the mini-buffer contents. */
521 Lisp_Object minibuf_prompt;
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
526 int minibuf_prompt_width;
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
532 Lisp_Object echo_area_window;
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
539 Lisp_Object Vmessage_stack;
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
544 int message_enable_multibyte;
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548 int update_mode_lines;
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
553 int windows_or_buffers_changed;
555 /* Nonzero means a frame's cursor type has been changed. */
557 int cursor_type_changed;
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
562 int line_number_displayed;
564 /* Maximum buffer size for which to display line numbers. */
566 Lisp_Object Vline_number_display_limit;
568 /* Line width to consider when repositioning for line number display. */
570 static EMACS_INT line_number_display_limit_width;
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
575 Lisp_Object Vmessage_log_max;
577 /* The name of the *Messages* buffer, a string. */
579 static Lisp_Object Vmessages_buffer_name;
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
590 Lisp_Object echo_area_buffer[2];
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
597 static Lisp_Object echo_buffer[2];
599 /* A vector saved used in with_area_buffer to reduce consing. */
601 static Lisp_Object Vwith_echo_area_save_vector;
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
606 static int display_last_displayed_message_p;
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
611 int message_buf_print;
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
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;
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
707 Lisp_Object Vresize_mini_windows;
709 /* Buffer being redisplayed -- for redisplay_window_error. */
711 struct buffer *displayed_buffer;
713 /* Value returned from text property handlers (see below). */
715 enum prop_handled
717 HANDLED_NORMALLY,
718 HANDLED_RECOMPUTE_PROPS,
719 HANDLED_OVERLAY_STRING_CONSUMED,
720 HANDLED_RETURN
723 /* A description of text properties that redisplay is interested
724 in. */
726 struct props
728 /* The name of the property. */
729 Lisp_Object *name;
731 /* A unique index for the property. */
732 enum prop_idx idx;
734 /* A handler function called to set up iterator IT from the property
735 at IT's current position. Value is used to steer handle_stop. */
736 enum prop_handled (*handler) P_ ((struct it *it));
739 static enum prop_handled handle_face_prop P_ ((struct it *));
740 static enum prop_handled handle_invisible_prop P_ ((struct it *));
741 static enum prop_handled handle_display_prop P_ ((struct it *));
742 static enum prop_handled handle_composition_prop P_ ((struct it *));
743 static enum prop_handled handle_overlay_change P_ ((struct it *));
744 static enum prop_handled handle_fontified_prop P_ ((struct it *));
746 /* Properties handled by iterators. */
748 static struct props it_props[] =
750 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
751 /* Handle `face' before `display' because some sub-properties of
752 `display' need to know the face. */
753 {&Qface, FACE_PROP_IDX, handle_face_prop},
754 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
755 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
756 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
757 {NULL, 0, NULL}
760 /* Value is the position described by X. If X is a marker, value is
761 the marker_position of X. Otherwise, value is X. */
763 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
765 /* Enumeration returned by some move_it_.* functions internally. */
767 enum move_it_result
769 /* Not used. Undefined value. */
770 MOVE_UNDEFINED,
772 /* Move ended at the requested buffer position or ZV. */
773 MOVE_POS_MATCH_OR_ZV,
775 /* Move ended at the requested X pixel position. */
776 MOVE_X_REACHED,
778 /* Move within a line ended at the end of a line that must be
779 continued. */
780 MOVE_LINE_CONTINUED,
782 /* Move within a line ended at the end of a line that would
783 be displayed truncated. */
784 MOVE_LINE_TRUNCATED,
786 /* Move within a line ended at a line end. */
787 MOVE_NEWLINE_OR_CR
790 /* This counter is used to clear the face cache every once in a while
791 in redisplay_internal. It is incremented for each redisplay.
792 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
793 cleared. */
795 #define CLEAR_FACE_CACHE_COUNT 500
796 static int clear_face_cache_count;
798 /* Similarly for the image cache. */
800 #ifdef HAVE_WINDOW_SYSTEM
801 #define CLEAR_IMAGE_CACHE_COUNT 101
802 static int clear_image_cache_count;
803 #endif
805 /* Record the previous terminal frame we displayed. */
807 static struct frame *previous_terminal_frame;
809 /* Non-zero while redisplay_internal is in progress. */
811 int redisplaying_p;
813 /* Non-zero means don't free realized faces. Bound while freeing
814 realized faces is dangerous because glyph matrices might still
815 reference them. */
817 int inhibit_free_realized_faces;
818 Lisp_Object Qinhibit_free_realized_faces;
820 /* If a string, XTread_socket generates an event to display that string.
821 (The display is done in read_char.) */
823 Lisp_Object help_echo_string;
824 Lisp_Object help_echo_window;
825 Lisp_Object help_echo_object;
826 int help_echo_pos;
828 /* Temporary variable for XTread_socket. */
830 Lisp_Object previous_help_echo_string;
832 /* Null glyph slice */
834 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
837 /* Function prototypes. */
839 static void setup_for_ellipsis P_ ((struct it *, int));
840 static void mark_window_display_accurate_1 P_ ((struct window *, int));
841 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
843 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
844 static int redisplay_mode_lines P_ ((Lisp_Object, int));
845 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
847 #if 0
848 static int invisible_text_between_p P_ ((struct it *, int, int));
849 #endif
851 static void pint2str P_ ((char *, int, int));
852 static void pint2hrstr P_ ((char *, int, int));
853 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
854 struct text_pos));
855 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
856 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
857 static void store_mode_line_noprop_char P_ ((char));
858 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
859 static void x_consider_frame_title P_ ((Lisp_Object));
860 static void handle_stop P_ ((struct it *));
861 static int tool_bar_lines_needed P_ ((struct frame *, int *));
862 static int single_display_spec_intangible_p P_ ((Lisp_Object));
863 static void ensure_echo_area_buffers P_ ((void));
864 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
865 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
866 static int with_echo_area_buffer P_ ((struct window *, int,
867 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
868 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static void clear_garbaged_frames P_ ((void));
870 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int display_echo_area P_ ((struct window *));
874 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
877 static int string_char_and_length P_ ((const unsigned char *, int, int *));
878 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
879 struct text_pos));
880 static int compute_window_start_on_continuation_line P_ ((struct window *));
881 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
882 static void insert_left_trunc_glyphs P_ ((struct it *));
883 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
884 Lisp_Object));
885 static void extend_face_to_end_of_line P_ ((struct it *));
886 static int append_space_for_newline P_ ((struct it *, int));
887 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
888 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
889 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
890 static int trailing_whitespace_p P_ ((int));
891 static int message_log_check_duplicate P_ ((int, int, int, int));
892 static void push_it P_ ((struct it *));
893 static void pop_it P_ ((struct it *));
894 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
895 static void select_frame_for_redisplay P_ ((Lisp_Object));
896 static void redisplay_internal P_ ((int));
897 static int echo_area_display P_ ((int));
898 static void redisplay_windows P_ ((Lisp_Object));
899 static void redisplay_window P_ ((Lisp_Object, int));
900 static Lisp_Object redisplay_window_error ();
901 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
902 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
903 static void update_menu_bar P_ ((struct frame *, int));
904 static int try_window_reusing_current_matrix P_ ((struct window *));
905 static int try_window_id P_ ((struct window *));
906 static int display_line P_ ((struct it *));
907 static int display_mode_lines P_ ((struct window *));
908 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
909 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
910 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
911 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
912 static void display_menu_bar P_ ((struct window *));
913 static int display_count_lines P_ ((int, int, int, int, int *));
914 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
915 int, int, struct it *, int, int, int, int));
916 static void compute_line_metrics P_ ((struct it *));
917 static void run_redisplay_end_trigger_hook P_ ((struct it *));
918 static int get_overlay_strings P_ ((struct it *, int));
919 static int get_overlay_strings_1 P_ ((struct it *, int, int));
920 static void next_overlay_string P_ ((struct it *));
921 static void reseat P_ ((struct it *, struct text_pos, int));
922 static void reseat_1 P_ ((struct it *, struct text_pos, int));
923 static void back_to_previous_visible_line_start P_ ((struct it *));
924 void reseat_at_previous_visible_line_start P_ ((struct it *));
925 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
926 static int next_element_from_ellipsis P_ ((struct it *));
927 static int next_element_from_display_vector P_ ((struct it *));
928 static int next_element_from_string P_ ((struct it *));
929 static int next_element_from_c_string P_ ((struct it *));
930 static int next_element_from_buffer P_ ((struct it *));
931 static int next_element_from_composition P_ ((struct it *));
932 static int next_element_from_image P_ ((struct it *));
933 static int next_element_from_stretch P_ ((struct it *));
934 static void load_overlay_strings P_ ((struct it *, int));
935 static int init_from_display_pos P_ ((struct it *, struct window *,
936 struct display_pos *));
937 static void reseat_to_string P_ ((struct it *, unsigned char *,
938 Lisp_Object, int, int, int, int));
939 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
940 int, int, int));
941 void move_it_vertically_backward P_ ((struct it *, int));
942 static void init_to_row_start P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static int init_to_row_end P_ ((struct it *, struct window *,
945 struct glyph_row *));
946 static void back_to_previous_line_start P_ ((struct it *));
947 static int forward_to_next_line_start P_ ((struct it *, int *));
948 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
949 Lisp_Object, int));
950 static struct text_pos string_pos P_ ((int, Lisp_Object));
951 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
952 static int number_of_chars P_ ((unsigned char *, int));
953 static void compute_stop_pos P_ ((struct it *));
954 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
955 Lisp_Object));
956 static int face_before_or_after_it_pos P_ ((struct it *, int));
957 static int next_overlay_change P_ ((int));
958 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
959 Lisp_Object, struct text_pos *,
960 int));
961 static int underlying_face_id P_ ((struct it *));
962 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
963 struct window *));
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
968 #ifdef HAVE_WINDOW_SYSTEM
970 static void update_tool_bar P_ ((struct frame *, int));
971 static void build_desired_tool_bar_string P_ ((struct frame *f));
972 static int redisplay_tool_bar P_ ((struct frame *));
973 static void display_tool_bar_line P_ ((struct it *, int));
974 static void notice_overwritten_cursor P_ ((struct window *,
975 enum glyph_row_area,
976 int, int, int, int));
980 #endif /* HAVE_WINDOW_SYSTEM */
983 /***********************************************************************
984 Window display dimensions
985 ***********************************************************************/
987 /* Return the bottom boundary y-position for text lines in window W.
988 This is the first y position at which a line cannot start.
989 It is relative to the top of the window.
991 This is the height of W minus the height of a mode line, if any. */
993 INLINE int
994 window_text_bottom_y (w)
995 struct window *w;
997 int height = WINDOW_TOTAL_HEIGHT (w);
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1008 INLINE int
1009 window_box_width (w, area)
1010 struct window *w;
1011 int area;
1013 int cols = XFASTINT (w->total_cols);
1014 int pixels = 0;
1016 if (!w->pseudo_window_p)
1018 cols -= WINDOW_SCROLL_BAR_COLS (w);
1020 if (area == TEXT_AREA)
1022 if (INTEGERP (w->left_margin_cols))
1023 cols -= XFASTINT (w->left_margin_cols);
1024 if (INTEGERP (w->right_margin_cols))
1025 cols -= XFASTINT (w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1028 else if (area == LEFT_MARGIN_AREA)
1030 cols = (INTEGERP (w->left_margin_cols)
1031 ? XFASTINT (w->left_margin_cols) : 0);
1032 pixels = 0;
1034 else if (area == RIGHT_MARGIN_AREA)
1036 cols = (INTEGERP (w->right_margin_cols)
1037 ? XFASTINT (w->right_margin_cols) : 0);
1038 pixels = 0;
1042 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1046 /* Return the pixel height of the display area of window W, not
1047 including mode lines of W, if any. */
1049 INLINE int
1050 window_box_height (w)
1051 struct window *w;
1053 struct frame *f = XFRAME (w->frame);
1054 int height = WINDOW_TOTAL_HEIGHT (w);
1056 xassert (height >= 0);
1058 /* Note: the code below that determines the mode-line/header-line
1059 height is essentially the same as that contained in the macro
1060 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1061 the appropriate glyph row has its `mode_line_p' flag set,
1062 and if it doesn't, uses estimate_mode_line_height instead. */
1064 if (WINDOW_WANTS_MODELINE_P (w))
1066 struct glyph_row *ml_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (ml_row && ml_row->mode_line_p)
1071 height -= ml_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1076 if (WINDOW_WANTS_HEADER_LINE_P (w))
1078 struct glyph_row *hl_row
1079 = (w->current_matrix && w->current_matrix->rows
1080 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1081 : 0);
1082 if (hl_row && hl_row->mode_line_p)
1083 height -= hl_row->height;
1084 else
1085 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1088 /* With a very small font and a mode-line that's taller than
1089 default, we might end up with a negative height. */
1090 return max (0, height);
1093 /* Return the window-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1097 INLINE int
1098 window_box_left_offset (w, area)
1099 struct window *w;
1100 int area;
1102 int x;
1104 if (w->pseudo_window_p)
1105 return 0;
1107 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1109 if (area == TEXT_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA));
1112 else if (area == RIGHT_MARGIN_AREA)
1113 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1114 + window_box_width (w, LEFT_MARGIN_AREA)
1115 + window_box_width (w, TEXT_AREA)
1116 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1118 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1119 else if (area == LEFT_MARGIN_AREA
1120 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1121 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1123 return x;
1127 /* Return the window-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1131 INLINE int
1132 window_box_right_offset (w, area)
1133 struct window *w;
1134 int area;
1136 return window_box_left_offset (w, area) + window_box_width (w, area);
1139 /* Return the frame-relative coordinate of the left edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the right of the left fringe of W. */
1143 INLINE int
1144 window_box_left (w, area)
1145 struct window *w;
1146 int area;
1148 struct frame *f = XFRAME (w->frame);
1149 int x;
1151 if (w->pseudo_window_p)
1152 return FRAME_INTERNAL_BORDER_WIDTH (f);
1154 x = (WINDOW_LEFT_EDGE_X (w)
1155 + window_box_left_offset (w, area));
1157 return x;
1161 /* Return the frame-relative coordinate of the right edge of display
1162 area AREA of window W. AREA < 0 means return the left edge of the
1163 whole window, to the left of the right fringe of W. */
1165 INLINE int
1166 window_box_right (w, area)
1167 struct window *w;
1168 int area;
1170 return window_box_left (w, area) + window_box_width (w, area);
1173 /* Get the bounding box of the display area AREA of window W, without
1174 mode lines, in frame-relative coordinates. AREA < 0 means the
1175 whole window, not including the left and right fringes of
1176 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1177 coordinates of the upper-left corner of the box. Return in
1178 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1180 INLINE void
1181 window_box (w, area, box_x, box_y, box_width, box_height)
1182 struct window *w;
1183 int area;
1184 int *box_x, *box_y, *box_width, *box_height;
1186 if (box_width)
1187 *box_width = window_box_width (w, area);
1188 if (box_height)
1189 *box_height = window_box_height (w);
1190 if (box_x)
1191 *box_x = window_box_left (w, area);
1192 if (box_y)
1194 *box_y = WINDOW_TOP_EDGE_Y (w);
1195 if (WINDOW_WANTS_HEADER_LINE_P (w))
1196 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1201 /* Get the bounding box of the display area AREA of window W, without
1202 mode lines. AREA < 0 means the whole window, not including the
1203 left and right fringe of the window. Return in *TOP_LEFT_X
1204 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1205 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1206 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1207 box. */
1209 INLINE void
1210 window_box_edges (w, area, top_left_x, top_left_y,
1211 bottom_right_x, bottom_right_y)
1212 struct window *w;
1213 int area;
1214 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1216 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1217 bottom_right_y);
1218 *bottom_right_x += *top_left_x;
1219 *bottom_right_y += *top_left_y;
1224 /***********************************************************************
1225 Utilities
1226 ***********************************************************************/
1228 /* Return the bottom y-position of the line the iterator IT is in.
1229 This can modify IT's settings. */
1232 line_bottom_y (it)
1233 struct it *it;
1235 int line_height = it->max_ascent + it->max_descent;
1236 int line_top_y = it->current_y;
1238 if (line_height == 0)
1240 if (last_height)
1241 line_height = last_height;
1242 else if (IT_CHARPOS (*it) < ZV)
1244 move_it_by_lines (it, 1, 1);
1245 line_height = (it->max_ascent || it->max_descent
1246 ? it->max_ascent + it->max_descent
1247 : last_height);
1249 else
1251 struct glyph_row *row = it->glyph_row;
1253 /* Use the default character height. */
1254 it->glyph_row = NULL;
1255 it->what = IT_CHARACTER;
1256 it->c = ' ';
1257 it->len = 1;
1258 PRODUCE_GLYPHS (it);
1259 line_height = it->ascent + it->descent;
1260 it->glyph_row = row;
1264 return line_top_y + line_height;
1268 /* Return 1 if position CHARPOS is visible in window W.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1272 and header-lines heights. */
1275 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1276 struct window *w;
1277 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1279 struct it it;
1280 struct text_pos top;
1281 int visible_p = 0;
1282 struct buffer *old_buffer = NULL;
1284 if (noninteractive)
1285 return visible_p;
1287 if (XBUFFER (w->buffer) != current_buffer)
1289 old_buffer = current_buffer;
1290 set_buffer_internal_1 (XBUFFER (w->buffer));
1293 SET_TEXT_POS_FROM_MARKER (top, w->start);
1295 /* Compute exact mode line heights, if requested. */
1296 if (exact_mode_line_heights_p)
1298 if (WINDOW_WANTS_MODELINE_P (w))
1299 current_mode_line_height
1300 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1301 current_buffer->mode_line_format);
1303 if (WINDOW_WANTS_HEADER_LINE_P (w))
1304 current_header_line_height
1305 = display_mode_line (w, HEADER_LINE_FACE_ID,
1306 current_buffer->header_line_format);
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1311 MOVE_TO_POS | MOVE_TO_Y);
1313 /* Note that we may overshoot because of invisible text. */
1314 if (IT_CHARPOS (it) >= charpos)
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 int bottom_y = (last_height = 0, line_bottom_y (&it));
1319 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1321 if (top_y < window_top_y)
1322 visible_p = bottom_y > window_top_y;
1323 else if (top_y < it.last_visible_y)
1324 visible_p = 1;
1325 if (visible_p)
1327 *x = top_x;
1328 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1329 *rtop = max (0, window_top_y - top_y);
1330 *rbot = max (0, bottom_y - it.last_visible_y);
1333 else
1335 struct it it2;
1337 it2 = it;
1338 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1339 move_it_by_lines (&it, 1, 0);
1340 if (charpos < IT_CHARPOS (it))
1342 visible_p = 1;
1343 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1344 *x = it2.current_x;
1345 *y = it2.current_y + it2.max_ascent - it2.ascent;
1346 *rtop = max (0, -it2.current_y);
1347 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1348 - it.last_visible_y));
1352 if (old_buffer)
1353 set_buffer_internal_1 (old_buffer);
1355 current_header_line_height = current_mode_line_height = -1;
1357 if (visible_p && XFASTINT (w->hscroll) > 0)
1358 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1360 return visible_p;
1364 /* Return the next character from STR which is MAXLEN bytes long.
1365 Return in *LEN the length of the character. This is like
1366 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1367 we find one, we return a `?', but with the length of the invalid
1368 character. */
1370 static INLINE int
1371 string_char_and_length (str, maxlen, len)
1372 const unsigned char *str;
1373 int maxlen, *len;
1375 int c;
1377 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1378 if (!CHAR_VALID_P (c, 1))
1379 /* We may not change the length here because other places in Emacs
1380 don't use this function, i.e. they silently accept invalid
1381 characters. */
1382 c = '?';
1384 return c;
1389 /* Given a position POS containing a valid character and byte position
1390 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1392 static struct text_pos
1393 string_pos_nchars_ahead (pos, string, nchars)
1394 struct text_pos pos;
1395 Lisp_Object string;
1396 int nchars;
1398 xassert (STRINGP (string) && nchars >= 0);
1400 if (STRING_MULTIBYTE (string))
1402 int rest = SBYTES (string) - BYTEPOS (pos);
1403 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1404 int len;
1406 while (nchars--)
1408 string_char_and_length (p, rest, &len);
1409 p += len, rest -= len;
1410 xassert (rest >= 0);
1411 CHARPOS (pos) += 1;
1412 BYTEPOS (pos) += len;
1415 else
1416 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1418 return pos;
1422 /* Value is the text position, i.e. character and byte position,
1423 for character position CHARPOS in STRING. */
1425 static INLINE struct text_pos
1426 string_pos (charpos, string)
1427 int charpos;
1428 Lisp_Object string;
1430 struct text_pos pos;
1431 xassert (STRINGP (string));
1432 xassert (charpos >= 0);
1433 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1434 return pos;
1438 /* Value is a text position, i.e. character and byte position, for
1439 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1440 means recognize multibyte characters. */
1442 static struct text_pos
1443 c_string_pos (charpos, s, multibyte_p)
1444 int charpos;
1445 unsigned char *s;
1446 int multibyte_p;
1448 struct text_pos pos;
1450 xassert (s != NULL);
1451 xassert (charpos >= 0);
1453 if (multibyte_p)
1455 int rest = strlen (s), len;
1457 SET_TEXT_POS (pos, 0, 0);
1458 while (charpos--)
1460 string_char_and_length (s, rest, &len);
1461 s += len, rest -= len;
1462 xassert (rest >= 0);
1463 CHARPOS (pos) += 1;
1464 BYTEPOS (pos) += len;
1467 else
1468 SET_TEXT_POS (pos, charpos, charpos);
1470 return pos;
1474 /* Value is the number of characters in C string S. MULTIBYTE_P
1475 non-zero means recognize multibyte characters. */
1477 static int
1478 number_of_chars (s, multibyte_p)
1479 unsigned char *s;
1480 int multibyte_p;
1482 int nchars;
1484 if (multibyte_p)
1486 int rest = strlen (s), len;
1487 unsigned char *p = (unsigned char *) s;
1489 for (nchars = 0; rest > 0; ++nchars)
1491 string_char_and_length (p, rest, &len);
1492 rest -= len, p += len;
1495 else
1496 nchars = strlen (s);
1498 return nchars;
1502 /* Compute byte position NEWPOS->bytepos corresponding to
1503 NEWPOS->charpos. POS is a known position in string STRING.
1504 NEWPOS->charpos must be >= POS.charpos. */
1506 static void
1507 compute_string_pos (newpos, pos, string)
1508 struct text_pos *newpos, pos;
1509 Lisp_Object string;
1511 xassert (STRINGP (string));
1512 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1514 if (STRING_MULTIBYTE (string))
1515 *newpos = string_pos_nchars_ahead (pos, string,
1516 CHARPOS (*newpos) - CHARPOS (pos));
1517 else
1518 BYTEPOS (*newpos) = CHARPOS (*newpos);
1521 /* EXPORT:
1522 Return an estimation of the pixel height of mode or top lines on
1523 frame F. FACE_ID specifies what line's height to estimate. */
1526 estimate_mode_line_height (f, face_id)
1527 struct frame *f;
1528 enum face_id face_id;
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 if (FRAME_WINDOW_P (f))
1533 int height = FONT_HEIGHT (FRAME_FONT (f));
1535 /* This function is called so early when Emacs starts that the face
1536 cache and mode line face are not yet initialized. */
1537 if (FRAME_FACE_CACHE (f))
1539 struct face *face = FACE_FROM_ID (f, face_id);
1540 if (face)
1542 if (face->font)
1543 height = FONT_HEIGHT (face->font);
1544 if (face->box_line_width > 0)
1545 height += 2 * face->box_line_width;
1549 return height;
1551 #endif
1553 return 1;
1556 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1557 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1558 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1559 not force the value into range. */
1561 void
1562 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1563 FRAME_PTR f;
1564 register int pix_x, pix_y;
1565 int *x, *y;
1566 NativeRectangle *bounds;
1567 int noclip;
1570 #ifdef HAVE_WINDOW_SYSTEM
1571 if (FRAME_WINDOW_P (f))
1573 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1574 even for negative values. */
1575 if (pix_x < 0)
1576 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1577 if (pix_y < 0)
1578 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1580 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1581 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1583 if (bounds)
1584 STORE_NATIVE_RECT (*bounds,
1585 FRAME_COL_TO_PIXEL_X (f, pix_x),
1586 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1587 FRAME_COLUMN_WIDTH (f) - 1,
1588 FRAME_LINE_HEIGHT (f) - 1);
1590 if (!noclip)
1592 if (pix_x < 0)
1593 pix_x = 0;
1594 else if (pix_x > FRAME_TOTAL_COLS (f))
1595 pix_x = FRAME_TOTAL_COLS (f);
1597 if (pix_y < 0)
1598 pix_y = 0;
1599 else if (pix_y > FRAME_LINES (f))
1600 pix_y = FRAME_LINES (f);
1603 #endif
1605 *x = pix_x;
1606 *y = pix_y;
1610 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1611 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1612 can't tell the positions because W's display is not up to date,
1613 return 0. */
1616 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1617 struct window *w;
1618 int hpos, vpos;
1619 int *frame_x, *frame_y;
1621 #ifdef HAVE_WINDOW_SYSTEM
1622 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1624 int success_p;
1626 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1627 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1629 if (display_completed)
1631 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1632 struct glyph *glyph = row->glyphs[TEXT_AREA];
1633 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1635 hpos = row->x;
1636 vpos = row->y;
1637 while (glyph < end)
1639 hpos += glyph->pixel_width;
1640 ++glyph;
1643 /* If first glyph is partially visible, its first visible position is still 0. */
1644 if (hpos < 0)
1645 hpos = 0;
1647 success_p = 1;
1649 else
1651 hpos = vpos = 0;
1652 success_p = 0;
1655 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1656 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1657 return success_p;
1659 #endif
1661 *frame_x = hpos;
1662 *frame_y = vpos;
1663 return 1;
1667 #ifdef HAVE_WINDOW_SYSTEM
1669 /* Find the glyph under window-relative coordinates X/Y in window W.
1670 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1671 strings. Return in *HPOS and *VPOS the row and column number of
1672 the glyph found. Return in *AREA the glyph area containing X.
1673 Value is a pointer to the glyph found or null if X/Y is not on
1674 text, or we can't tell because W's current matrix is not up to
1675 date. */
1677 static struct glyph *
1678 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1679 struct window *w;
1680 int x, y;
1681 int *hpos, *vpos, *dx, *dy, *area;
1683 struct glyph *glyph, *end;
1684 struct glyph_row *row = NULL;
1685 int x0, i;
1687 /* Find row containing Y. Give up if some row is not enabled. */
1688 for (i = 0; i < w->current_matrix->nrows; ++i)
1690 row = MATRIX_ROW (w->current_matrix, i);
1691 if (!row->enabled_p)
1692 return NULL;
1693 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1694 break;
1697 *vpos = i;
1698 *hpos = 0;
1700 /* Give up if Y is not in the window. */
1701 if (i == w->current_matrix->nrows)
1702 return NULL;
1704 /* Get the glyph area containing X. */
1705 if (w->pseudo_window_p)
1707 *area = TEXT_AREA;
1708 x0 = 0;
1710 else
1712 if (x < window_box_left_offset (w, TEXT_AREA))
1714 *area = LEFT_MARGIN_AREA;
1715 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1717 else if (x < window_box_right_offset (w, TEXT_AREA))
1719 *area = TEXT_AREA;
1720 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1722 else
1724 *area = RIGHT_MARGIN_AREA;
1725 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1729 /* Find glyph containing X. */
1730 glyph = row->glyphs[*area];
1731 end = glyph + row->used[*area];
1732 x -= x0;
1733 while (glyph < end && x >= glyph->pixel_width)
1735 x -= glyph->pixel_width;
1736 ++glyph;
1739 if (glyph == end)
1740 return NULL;
1742 if (dx)
1744 *dx = x;
1745 *dy = y - (row->y + row->ascent - glyph->ascent);
1748 *hpos = glyph - row->glyphs[*area];
1749 return glyph;
1753 /* EXPORT:
1754 Convert frame-relative x/y to coordinates relative to window W.
1755 Takes pseudo-windows into account. */
1757 void
1758 frame_to_window_pixel_xy (w, x, y)
1759 struct window *w;
1760 int *x, *y;
1762 if (w->pseudo_window_p)
1764 /* A pseudo-window is always full-width, and starts at the
1765 left edge of the frame, plus a frame border. */
1766 struct frame *f = XFRAME (w->frame);
1767 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1768 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1770 else
1772 *x -= WINDOW_LEFT_EDGE_X (w);
1773 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1777 /* EXPORT:
1778 Return in RECTS[] at most N clipping rectangles for glyph string S.
1779 Return the number of stored rectangles. */
1782 get_glyph_string_clip_rects (s, rects, n)
1783 struct glyph_string *s;
1784 NativeRectangle *rects;
1785 int n;
1787 XRectangle r;
1789 if (n <= 0)
1790 return 0;
1792 if (s->row->full_width_p)
1794 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1795 r.x = WINDOW_LEFT_EDGE_X (s->w);
1796 r.width = WINDOW_TOTAL_WIDTH (s->w);
1798 /* Unless displaying a mode or menu bar line, which are always
1799 fully visible, clip to the visible part of the row. */
1800 if (s->w->pseudo_window_p)
1801 r.height = s->row->visible_height;
1802 else
1803 r.height = s->height;
1805 else
1807 /* This is a text line that may be partially visible. */
1808 r.x = window_box_left (s->w, s->area);
1809 r.width = window_box_width (s->w, s->area);
1810 r.height = s->row->visible_height;
1813 if (s->clip_head)
1814 if (r.x < s->clip_head->x)
1816 if (r.width >= s->clip_head->x - r.x)
1817 r.width -= s->clip_head->x - r.x;
1818 else
1819 r.width = 0;
1820 r.x = s->clip_head->x;
1822 if (s->clip_tail)
1823 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1825 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1826 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1827 else
1828 r.width = 0;
1831 /* If S draws overlapping rows, it's sufficient to use the top and
1832 bottom of the window for clipping because this glyph string
1833 intentionally draws over other lines. */
1834 if (s->for_overlaps)
1836 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1837 r.height = window_text_bottom_y (s->w) - r.y;
1839 /* Alas, the above simple strategy does not work for the
1840 environments with anti-aliased text: if the same text is
1841 drawn onto the same place multiple times, it gets thicker.
1842 If the overlap we are processing is for the erased cursor, we
1843 take the intersection with the rectagle of the cursor. */
1844 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1846 XRectangle rc, r_save = r;
1848 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1849 rc.y = s->w->phys_cursor.y;
1850 rc.width = s->w->phys_cursor_width;
1851 rc.height = s->w->phys_cursor_height;
1853 x_intersect_rectangles (&r_save, &rc, &r);
1856 else
1858 /* Don't use S->y for clipping because it doesn't take partially
1859 visible lines into account. For example, it can be negative for
1860 partially visible lines at the top of a window. */
1861 if (!s->row->full_width_p
1862 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1863 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1864 else
1865 r.y = max (0, s->row->y);
1867 /* If drawing a tool-bar window, draw it over the internal border
1868 at the top of the window. */
1869 if (WINDOWP (s->f->tool_bar_window)
1870 && s->w == XWINDOW (s->f->tool_bar_window))
1871 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1874 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1876 /* If drawing the cursor, don't let glyph draw outside its
1877 advertised boundaries. Cleartype does this under some circumstances. */
1878 if (s->hl == DRAW_CURSOR)
1880 struct glyph *glyph = s->first_glyph;
1881 int height, max_y;
1883 if (s->x > r.x)
1885 r.width -= s->x - r.x;
1886 r.x = s->x;
1888 r.width = min (r.width, glyph->pixel_width);
1890 /* If r.y is below window bottom, ensure that we still see a cursor. */
1891 height = min (glyph->ascent + glyph->descent,
1892 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1893 max_y = window_text_bottom_y (s->w) - height;
1894 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1895 if (s->ybase - glyph->ascent > max_y)
1897 r.y = max_y;
1898 r.height = height;
1900 else
1902 /* Don't draw cursor glyph taller than our actual glyph. */
1903 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1904 if (height < r.height)
1906 max_y = r.y + r.height;
1907 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1908 r.height = min (max_y - r.y, height);
1913 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1914 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1916 #ifdef CONVERT_FROM_XRECT
1917 CONVERT_FROM_XRECT (r, *rects);
1918 #else
1919 *rects = r;
1920 #endif
1921 return 1;
1923 else
1925 /* If we are processing overlapping and allowed to return
1926 multiple clipping rectangles, we exclude the row of the glyph
1927 string from the clipping rectangle. This is to avoid drawing
1928 the same text on the environment with anti-aliasing. */
1929 #ifdef CONVERT_FROM_XRECT
1930 XRectangle rs[2];
1931 #else
1932 XRectangle *rs = rects;
1933 #endif
1934 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1936 if (s->for_overlaps & OVERLAPS_PRED)
1938 rs[i] = r;
1939 if (r.y + r.height > row_y)
1941 if (r.y < row_y)
1942 rs[i].height = row_y - r.y;
1943 else
1944 rs[i].height = 0;
1946 i++;
1948 if (s->for_overlaps & OVERLAPS_SUCC)
1950 rs[i] = r;
1951 if (r.y < row_y + s->row->visible_height)
1953 if (r.y + r.height > row_y + s->row->visible_height)
1955 rs[i].y = row_y + s->row->visible_height;
1956 rs[i].height = r.y + r.height - rs[i].y;
1958 else
1959 rs[i].height = 0;
1961 i++;
1964 n = i;
1965 #ifdef CONVERT_FROM_XRECT
1966 for (i = 0; i < n; i++)
1967 CONVERT_FROM_XRECT (rs[i], rects[i]);
1968 #endif
1969 return n;
1973 /* EXPORT:
1974 Return in *NR the clipping rectangle for glyph string S. */
1976 void
1977 get_glyph_string_clip_rect (s, nr)
1978 struct glyph_string *s;
1979 NativeRectangle *nr;
1981 get_glyph_string_clip_rects (s, nr, 1);
1985 /* EXPORT:
1986 Return the position and height of the phys cursor in window W.
1987 Set w->phys_cursor_width to width of phys cursor.
1990 void
1991 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
1992 struct window *w;
1993 struct glyph_row *row;
1994 struct glyph *glyph;
1995 int *xp, *yp, *heightp;
1997 struct frame *f = XFRAME (WINDOW_FRAME (w));
1998 int x, y, wd, h, h0, y0;
2000 /* Compute the width of the rectangle to draw. If on a stretch
2001 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2002 rectangle as wide as the glyph, but use a canonical character
2003 width instead. */
2004 wd = glyph->pixel_width - 1;
2005 #ifdef HAVE_NTGUI
2006 wd++; /* Why? */
2007 #endif
2009 x = w->phys_cursor.x;
2010 if (x < 0)
2012 wd += x;
2013 x = 0;
2016 if (glyph->type == STRETCH_GLYPH
2017 && !x_stretch_cursor_p)
2018 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2019 w->phys_cursor_width = wd;
2021 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2023 /* If y is below window bottom, ensure that we still see a cursor. */
2024 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2026 h = max (h0, glyph->ascent + glyph->descent);
2027 h0 = min (h0, glyph->ascent + glyph->descent);
2029 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2030 if (y < y0)
2032 h = max (h - (y0 - y) + 1, h0);
2033 y = y0 - 1;
2035 else
2037 y0 = window_text_bottom_y (w) - h0;
2038 if (y > y0)
2040 h += y - y0;
2041 y = y0;
2045 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2046 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2047 *heightp = h;
2051 * Remember which glyph the mouse is over.
2054 void
2055 remember_mouse_glyph (f, gx, gy, rect)
2056 struct frame *f;
2057 int gx, gy;
2058 NativeRectangle *rect;
2060 Lisp_Object window;
2061 struct window *w;
2062 struct glyph_row *r, *gr, *end_row;
2063 enum window_part part;
2064 enum glyph_row_area area;
2065 int x, y, width, height;
2067 /* Try to determine frame pixel position and size of the glyph under
2068 frame pixel coordinates X/Y on frame F. */
2070 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2071 if (NILP (window))
2073 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2074 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2075 goto virtual_glyph;
2078 w = XWINDOW (window);
2079 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2080 height = WINDOW_FRAME_LINE_HEIGHT (w);
2082 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2083 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2085 if (w->pseudo_window_p)
2087 area = TEXT_AREA;
2088 part = ON_MODE_LINE; /* Don't adjust margin. */
2089 goto text_glyph;
2092 switch (part)
2094 case ON_LEFT_MARGIN:
2095 area = LEFT_MARGIN_AREA;
2096 goto text_glyph;
2098 case ON_RIGHT_MARGIN:
2099 area = RIGHT_MARGIN_AREA;
2100 goto text_glyph;
2102 case ON_HEADER_LINE:
2103 case ON_MODE_LINE:
2104 gr = (part == ON_HEADER_LINE
2105 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2106 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2107 gy = gr->y;
2108 area = TEXT_AREA;
2109 goto text_glyph_row_found;
2111 case ON_TEXT:
2112 area = TEXT_AREA;
2114 text_glyph:
2115 gr = 0; gy = 0;
2116 for (; r <= end_row && r->enabled_p; ++r)
2117 if (r->y + r->height > y)
2119 gr = r; gy = r->y;
2120 break;
2123 text_glyph_row_found:
2124 if (gr && gy <= y)
2126 struct glyph *g = gr->glyphs[area];
2127 struct glyph *end = g + gr->used[area];
2129 height = gr->height;
2130 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2131 if (gx + g->pixel_width > x)
2132 break;
2134 if (g < end)
2136 if (g->type == IMAGE_GLYPH)
2138 /* Don't remember when mouse is over image, as
2139 image may have hot-spots. */
2140 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2141 return;
2143 width = g->pixel_width;
2145 else
2147 /* Use nominal char spacing at end of line. */
2148 x -= gx;
2149 gx += (x / width) * width;
2152 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2153 gx += window_box_left_offset (w, area);
2155 else
2157 /* Use nominal line height at end of window. */
2158 gx = (x / width) * width;
2159 y -= gy;
2160 gy += (y / height) * height;
2162 break;
2164 case ON_LEFT_FRINGE:
2165 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2166 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2167 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2168 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2169 goto row_glyph;
2171 case ON_RIGHT_FRINGE:
2172 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2173 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2174 : window_box_right_offset (w, TEXT_AREA));
2175 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2176 goto row_glyph;
2178 case ON_SCROLL_BAR:
2179 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2181 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2182 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2183 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2184 : 0)));
2185 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2187 row_glyph:
2188 gr = 0, gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2192 gr = r; gy = r->y;
2193 break;
2196 if (gr && gy <= y)
2197 height = gr->height;
2198 else
2200 /* Use nominal line height at end of window. */
2201 y -= gy;
2202 gy += (y / height) * height;
2204 break;
2206 default:
2208 virtual_glyph:
2209 /* If there is no glyph under the mouse, then we divide the screen
2210 into a grid of the smallest glyph in the frame, and use that
2211 as our "glyph". */
2213 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2214 round down even for negative values. */
2215 if (gx < 0)
2216 gx -= width - 1;
2217 if (gy < 0)
2218 gy -= height - 1;
2220 gx = (gx / width) * width;
2221 gy = (gy / height) * height;
2223 goto store_rect;
2226 gx += WINDOW_LEFT_EDGE_X (w);
2227 gy += WINDOW_TOP_EDGE_Y (w);
2229 store_rect:
2230 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2232 /* Visible feedback for debugging. */
2233 #if 0
2234 #if HAVE_X_WINDOWS
2235 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2236 f->output_data.x->normal_gc,
2237 gx, gy, width, height);
2238 #endif
2239 #endif
2243 #endif /* HAVE_WINDOW_SYSTEM */
2246 /***********************************************************************
2247 Lisp form evaluation
2248 ***********************************************************************/
2250 /* Error handler for safe_eval and safe_call. */
2252 static Lisp_Object
2253 safe_eval_handler (arg)
2254 Lisp_Object arg;
2256 add_to_log ("Error during redisplay: %s", arg, Qnil);
2257 return Qnil;
2261 /* Evaluate SEXPR and return the result, or nil if something went
2262 wrong. Prevent redisplay during the evaluation. */
2264 Lisp_Object
2265 safe_eval (sexpr)
2266 Lisp_Object sexpr;
2268 Lisp_Object val;
2270 if (inhibit_eval_during_redisplay)
2271 val = Qnil;
2272 else
2274 int count = SPECPDL_INDEX ();
2275 struct gcpro gcpro1;
2277 GCPRO1 (sexpr);
2278 specbind (Qinhibit_redisplay, Qt);
2279 /* Use Qt to ensure debugger does not run,
2280 so there is no possibility of wanting to redisplay. */
2281 val = internal_condition_case_1 (Feval, sexpr, Qt,
2282 safe_eval_handler);
2283 UNGCPRO;
2284 val = unbind_to (count, val);
2287 return val;
2291 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2292 Return the result, or nil if something went wrong. Prevent
2293 redisplay during the evaluation. */
2295 Lisp_Object
2296 safe_call (nargs, args)
2297 int nargs;
2298 Lisp_Object *args;
2300 Lisp_Object val;
2302 if (inhibit_eval_during_redisplay)
2303 val = Qnil;
2304 else
2306 int count = SPECPDL_INDEX ();
2307 struct gcpro gcpro1;
2309 GCPRO1 (args[0]);
2310 gcpro1.nvars = nargs;
2311 specbind (Qinhibit_redisplay, Qt);
2312 /* Use Qt to ensure debugger does not run,
2313 so there is no possibility of wanting to redisplay. */
2314 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2315 safe_eval_handler);
2316 UNGCPRO;
2317 val = unbind_to (count, val);
2320 return val;
2324 /* Call function FN with one argument ARG.
2325 Return the result, or nil if something went wrong. */
2327 Lisp_Object
2328 safe_call1 (fn, arg)
2329 Lisp_Object fn, arg;
2331 Lisp_Object args[2];
2332 args[0] = fn;
2333 args[1] = arg;
2334 return safe_call (2, args);
2339 /***********************************************************************
2340 Debugging
2341 ***********************************************************************/
2343 #if 0
2345 /* Define CHECK_IT to perform sanity checks on iterators.
2346 This is for debugging. It is too slow to do unconditionally. */
2348 static void
2349 check_it (it)
2350 struct it *it;
2352 if (it->method == GET_FROM_STRING)
2354 xassert (STRINGP (it->string));
2355 xassert (IT_STRING_CHARPOS (*it) >= 0);
2357 else
2359 xassert (IT_STRING_CHARPOS (*it) < 0);
2360 if (it->method == GET_FROM_BUFFER)
2362 /* Check that character and byte positions agree. */
2363 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2367 if (it->dpvec)
2368 xassert (it->current.dpvec_index >= 0);
2369 else
2370 xassert (it->current.dpvec_index < 0);
2373 #define CHECK_IT(IT) check_it ((IT))
2375 #else /* not 0 */
2377 #define CHECK_IT(IT) (void) 0
2379 #endif /* not 0 */
2382 #if GLYPH_DEBUG
2384 /* Check that the window end of window W is what we expect it
2385 to be---the last row in the current matrix displaying text. */
2387 static void
2388 check_window_end (w)
2389 struct window *w;
2391 if (!MINI_WINDOW_P (w)
2392 && !NILP (w->window_end_valid))
2394 struct glyph_row *row;
2395 xassert ((row = MATRIX_ROW (w->current_matrix,
2396 XFASTINT (w->window_end_vpos)),
2397 !row->enabled_p
2398 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2399 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2403 #define CHECK_WINDOW_END(W) check_window_end ((W))
2405 #else /* not GLYPH_DEBUG */
2407 #define CHECK_WINDOW_END(W) (void) 0
2409 #endif /* not GLYPH_DEBUG */
2413 /***********************************************************************
2414 Iterator initialization
2415 ***********************************************************************/
2417 /* Initialize IT for displaying current_buffer in window W, starting
2418 at character position CHARPOS. CHARPOS < 0 means that no buffer
2419 position is specified which is useful when the iterator is assigned
2420 a position later. BYTEPOS is the byte position corresponding to
2421 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2423 If ROW is not null, calls to produce_glyphs with IT as parameter
2424 will produce glyphs in that row.
2426 BASE_FACE_ID is the id of a base face to use. It must be one of
2427 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2428 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2429 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2431 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2432 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2433 will be initialized to use the corresponding mode line glyph row of
2434 the desired matrix of W. */
2436 void
2437 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2438 struct it *it;
2439 struct window *w;
2440 int charpos, bytepos;
2441 struct glyph_row *row;
2442 enum face_id base_face_id;
2444 int highlight_region_p;
2446 /* Some precondition checks. */
2447 xassert (w != NULL && it != NULL);
2448 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2449 && charpos <= ZV));
2451 /* If face attributes have been changed since the last redisplay,
2452 free realized faces now because they depend on face definitions
2453 that might have changed. Don't free faces while there might be
2454 desired matrices pending which reference these faces. */
2455 if (face_change_count && !inhibit_free_realized_faces)
2457 face_change_count = 0;
2458 free_all_realized_faces (Qnil);
2461 /* Use one of the mode line rows of W's desired matrix if
2462 appropriate. */
2463 if (row == NULL)
2465 if (base_face_id == MODE_LINE_FACE_ID
2466 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2467 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2468 else if (base_face_id == HEADER_LINE_FACE_ID)
2469 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2472 /* Clear IT. */
2473 bzero (it, sizeof *it);
2474 it->current.overlay_string_index = -1;
2475 it->current.dpvec_index = -1;
2476 it->base_face_id = base_face_id;
2477 it->string = Qnil;
2478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2480 /* The window in which we iterate over current_buffer: */
2481 XSETWINDOW (it->window, w);
2482 it->w = w;
2483 it->f = XFRAME (w->frame);
2485 /* Extra space between lines (on window systems only). */
2486 if (base_face_id == DEFAULT_FACE_ID
2487 && FRAME_WINDOW_P (it->f))
2489 if (NATNUMP (current_buffer->extra_line_spacing))
2490 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2491 else if (FLOATP (current_buffer->extra_line_spacing))
2492 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2493 * FRAME_LINE_HEIGHT (it->f));
2494 else if (it->f->extra_line_spacing > 0)
2495 it->extra_line_spacing = it->f->extra_line_spacing;
2496 it->max_extra_line_spacing = 0;
2499 /* If realized faces have been removed, e.g. because of face
2500 attribute changes of named faces, recompute them. When running
2501 in batch mode, the face cache of Vterminal_frame is null. If
2502 we happen to get called, make a dummy face cache. */
2503 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2504 init_frame_faces (it->f);
2505 if (FRAME_FACE_CACHE (it->f)->used == 0)
2506 recompute_basic_faces (it->f);
2508 /* Current value of the `slice', `space-width', and 'height' properties. */
2509 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2510 it->space_width = Qnil;
2511 it->font_height = Qnil;
2512 it->override_ascent = -1;
2514 /* Are control characters displayed as `^C'? */
2515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2517 /* -1 means everything between a CR and the following line end
2518 is invisible. >0 means lines indented more than this value are
2519 invisible. */
2520 it->selective = (INTEGERP (current_buffer->selective_display)
2521 ? XFASTINT (current_buffer->selective_display)
2522 : (!NILP (current_buffer->selective_display)
2523 ? -1 : 0));
2524 it->selective_display_ellipsis_p
2525 = !NILP (current_buffer->selective_display_ellipses);
2527 /* Display table to use. */
2528 it->dp = window_display_table (w);
2530 /* Are multibyte characters enabled in current_buffer? */
2531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2533 /* Non-zero if we should highlight the region. */
2534 highlight_region_p
2535 = (!NILP (Vtransient_mark_mode)
2536 && !NILP (current_buffer->mark_active)
2537 && XMARKER (current_buffer->mark)->buffer != 0);
2539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2540 start and end of a visible region in window IT->w. Set both to
2541 -1 to indicate no region. */
2542 if (highlight_region_p
2543 /* Maybe highlight only in selected window. */
2544 && (/* Either show region everywhere. */
2545 highlight_nonselected_windows
2546 /* Or show region in the selected window. */
2547 || w == XWINDOW (selected_window)
2548 /* Or show the region if we are in the mini-buffer and W is
2549 the window the mini-buffer refers to. */
2550 || (MINI_WINDOW_P (XWINDOW (selected_window))
2551 && WINDOWP (minibuf_selected_window)
2552 && w == XWINDOW (minibuf_selected_window))))
2554 int charpos = marker_position (current_buffer->mark);
2555 it->region_beg_charpos = min (PT, charpos);
2556 it->region_end_charpos = max (PT, charpos);
2558 else
2559 it->region_beg_charpos = it->region_end_charpos = -1;
2561 /* Get the position at which the redisplay_end_trigger hook should
2562 be run, if it is to be run at all. */
2563 if (MARKERP (w->redisplay_end_trigger)
2564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2565 it->redisplay_end_trigger_charpos
2566 = marker_position (w->redisplay_end_trigger);
2567 else if (INTEGERP (w->redisplay_end_trigger))
2568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2570 /* Correct bogus values of tab_width. */
2571 it->tab_width = XINT (current_buffer->tab_width);
2572 if (it->tab_width <= 0 || it->tab_width > 1000)
2573 it->tab_width = 8;
2575 /* Are lines in the display truncated? */
2576 it->truncate_lines_p
2577 = (base_face_id != DEFAULT_FACE_ID
2578 || XINT (it->w->hscroll)
2579 || (truncate_partial_width_windows
2580 && !WINDOW_FULL_WIDTH_P (it->w))
2581 || !NILP (current_buffer->truncate_lines));
2583 /* Get dimensions of truncation and continuation glyphs. These are
2584 displayed as fringe bitmaps under X, so we don't need them for such
2585 frames. */
2586 if (!FRAME_WINDOW_P (it->f))
2588 if (it->truncate_lines_p)
2590 /* We will need the truncation glyph. */
2591 xassert (it->glyph_row == NULL);
2592 produce_special_glyphs (it, IT_TRUNCATION);
2593 it->truncation_pixel_width = it->pixel_width;
2595 else
2597 /* We will need the continuation glyph. */
2598 xassert (it->glyph_row == NULL);
2599 produce_special_glyphs (it, IT_CONTINUATION);
2600 it->continuation_pixel_width = it->pixel_width;
2603 /* Reset these values to zero because the produce_special_glyphs
2604 above has changed them. */
2605 it->pixel_width = it->ascent = it->descent = 0;
2606 it->phys_ascent = it->phys_descent = 0;
2609 /* Set this after getting the dimensions of truncation and
2610 continuation glyphs, so that we don't produce glyphs when calling
2611 produce_special_glyphs, above. */
2612 it->glyph_row = row;
2613 it->area = TEXT_AREA;
2615 /* Get the dimensions of the display area. The display area
2616 consists of the visible window area plus a horizontally scrolled
2617 part to the left of the window. All x-values are relative to the
2618 start of this total display area. */
2619 if (base_face_id != DEFAULT_FACE_ID)
2621 /* Mode lines, menu bar in terminal frames. */
2622 it->first_visible_x = 0;
2623 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2625 else
2627 it->first_visible_x
2628 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2629 it->last_visible_x = (it->first_visible_x
2630 + window_box_width (w, TEXT_AREA));
2632 /* If we truncate lines, leave room for the truncator glyph(s) at
2633 the right margin. Otherwise, leave room for the continuation
2634 glyph(s). Truncation and continuation glyphs are not inserted
2635 for window-based redisplay. */
2636 if (!FRAME_WINDOW_P (it->f))
2638 if (it->truncate_lines_p)
2639 it->last_visible_x -= it->truncation_pixel_width;
2640 else
2641 it->last_visible_x -= it->continuation_pixel_width;
2644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2645 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2648 /* Leave room for a border glyph. */
2649 if (!FRAME_WINDOW_P (it->f)
2650 && !WINDOW_RIGHTMOST_P (it->w))
2651 it->last_visible_x -= 1;
2653 it->last_visible_y = window_text_bottom_y (w);
2655 /* For mode lines and alike, arrange for the first glyph having a
2656 left box line if the face specifies a box. */
2657 if (base_face_id != DEFAULT_FACE_ID)
2659 struct face *face;
2661 it->face_id = base_face_id;
2663 /* If we have a boxed mode line, make the first character appear
2664 with a left box line. */
2665 face = FACE_FROM_ID (it->f, base_face_id);
2666 if (face->box != FACE_NO_BOX)
2667 it->start_of_box_run_p = 1;
2670 /* If a buffer position was specified, set the iterator there,
2671 getting overlays and face properties from that position. */
2672 if (charpos >= BUF_BEG (current_buffer))
2674 it->end_charpos = ZV;
2675 it->face_id = -1;
2676 IT_CHARPOS (*it) = charpos;
2678 /* Compute byte position if not specified. */
2679 if (bytepos < charpos)
2680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2681 else
2682 IT_BYTEPOS (*it) = bytepos;
2684 it->start = it->current;
2686 /* Compute faces etc. */
2687 reseat (it, it->current.pos, 1);
2690 CHECK_IT (it);
2694 /* Initialize IT for the display of window W with window start POS. */
2696 void
2697 start_display (it, w, pos)
2698 struct it *it;
2699 struct window *w;
2700 struct text_pos pos;
2702 struct glyph_row *row;
2703 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2705 row = w->desired_matrix->rows + first_vpos;
2706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2707 it->first_vpos = first_vpos;
2709 /* Don't reseat to previous visible line start if current start
2710 position is in a string or image. */
2711 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2713 int start_at_line_beg_p;
2714 int first_y = it->current_y;
2716 /* If window start is not at a line start, skip forward to POS to
2717 get the correct continuation lines width. */
2718 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2719 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2720 if (!start_at_line_beg_p)
2722 int new_x;
2724 reseat_at_previous_visible_line_start (it);
2725 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2727 new_x = it->current_x + it->pixel_width;
2729 /* If lines are continued, this line may end in the middle
2730 of a multi-glyph character (e.g. a control character
2731 displayed as \003, or in the middle of an overlay
2732 string). In this case move_it_to above will not have
2733 taken us to the start of the continuation line but to the
2734 end of the continued line. */
2735 if (it->current_x > 0
2736 && !it->truncate_lines_p /* Lines are continued. */
2737 && (/* And glyph doesn't fit on the line. */
2738 new_x > it->last_visible_x
2739 /* Or it fits exactly and we're on a window
2740 system frame. */
2741 || (new_x == it->last_visible_x
2742 && FRAME_WINDOW_P (it->f))))
2744 if (it->current.dpvec_index >= 0
2745 || it->current.overlay_string_index >= 0)
2747 set_iterator_to_next (it, 1);
2748 move_it_in_display_line_to (it, -1, -1, 0);
2751 it->continuation_lines_width += it->current_x;
2754 /* We're starting a new display line, not affected by the
2755 height of the continued line, so clear the appropriate
2756 fields in the iterator structure. */
2757 it->max_ascent = it->max_descent = 0;
2758 it->max_phys_ascent = it->max_phys_descent = 0;
2760 it->current_y = first_y;
2761 it->vpos = 0;
2762 it->current_x = it->hpos = 0;
2766 #if 0 /* Don't assert the following because start_display is sometimes
2767 called intentionally with a window start that is not at a
2768 line start. Please leave this code in as a comment. */
2770 /* Window start should be on a line start, now. */
2771 xassert (it->continuation_lines_width
2772 || IT_CHARPOS (it) == BEGV
2773 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2774 #endif /* 0 */
2778 /* Return 1 if POS is a position in ellipses displayed for invisible
2779 text. W is the window we display, for text property lookup. */
2781 static int
2782 in_ellipses_for_invisible_text_p (pos, w)
2783 struct display_pos *pos;
2784 struct window *w;
2786 Lisp_Object prop, window;
2787 int ellipses_p = 0;
2788 int charpos = CHARPOS (pos->pos);
2790 /* If POS specifies a position in a display vector, this might
2791 be for an ellipsis displayed for invisible text. We won't
2792 get the iterator set up for delivering that ellipsis unless
2793 we make sure that it gets aware of the invisible text. */
2794 if (pos->dpvec_index >= 0
2795 && pos->overlay_string_index < 0
2796 && CHARPOS (pos->string_pos) < 0
2797 && charpos > BEGV
2798 && (XSETWINDOW (window, w),
2799 prop = Fget_char_property (make_number (charpos),
2800 Qinvisible, window),
2801 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2803 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2804 window);
2805 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2808 return ellipses_p;
2812 /* Initialize IT for stepping through current_buffer in window W,
2813 starting at position POS that includes overlay string and display
2814 vector/ control character translation position information. Value
2815 is zero if there are overlay strings with newlines at POS. */
2817 static int
2818 init_from_display_pos (it, w, pos)
2819 struct it *it;
2820 struct window *w;
2821 struct display_pos *pos;
2823 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2824 int i, overlay_strings_with_newlines = 0;
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (in_ellipses_for_invisible_text_p (pos, w))
2832 --charpos;
2833 bytepos = 0;
2836 /* Keep in mind: the call to reseat in init_iterator skips invisible
2837 text, so we might end up at a position different from POS. This
2838 is only a problem when POS is a row start after a newline and an
2839 overlay starts there with an after-string, and the overlay has an
2840 invisible property. Since we don't skip invisible text in
2841 display_line and elsewhere immediately after consuming the
2842 newline before the row start, such a POS will not be in a string,
2843 but the call to init_iterator below will move us to the
2844 after-string. */
2845 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2847 /* This only scans the current chunk -- it should scan all chunks.
2848 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2849 to 16 in 22.1 to make this a lesser problem. */
2850 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2852 const char *s = SDATA (it->overlay_strings[i]);
2853 const char *e = s + SBYTES (it->overlay_strings[i]);
2855 while (s < e && *s != '\n')
2856 ++s;
2858 if (s < e)
2860 overlay_strings_with_newlines = 1;
2861 break;
2865 /* If position is within an overlay string, set up IT to the right
2866 overlay string. */
2867 if (pos->overlay_string_index >= 0)
2869 int relative_index;
2871 /* If the first overlay string happens to have a `display'
2872 property for an image, the iterator will be set up for that
2873 image, and we have to undo that setup first before we can
2874 correct the overlay string index. */
2875 if (it->method == GET_FROM_IMAGE)
2876 pop_it (it);
2878 /* We already have the first chunk of overlay strings in
2879 IT->overlay_strings. Load more until the one for
2880 pos->overlay_string_index is in IT->overlay_strings. */
2881 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2883 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2884 it->current.overlay_string_index = 0;
2885 while (n--)
2887 load_overlay_strings (it, 0);
2888 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2892 it->current.overlay_string_index = pos->overlay_string_index;
2893 relative_index = (it->current.overlay_string_index
2894 % OVERLAY_STRING_CHUNK_SIZE);
2895 it->string = it->overlay_strings[relative_index];
2896 xassert (STRINGP (it->string));
2897 it->current.string_pos = pos->string_pos;
2898 it->method = GET_FROM_STRING;
2901 #if 0 /* This is bogus because POS not having an overlay string
2902 position does not mean it's after the string. Example: A
2903 line starting with a before-string and initialization of IT
2904 to the previous row's end position. */
2905 else if (it->current.overlay_string_index >= 0)
2907 /* If POS says we're already after an overlay string ending at
2908 POS, make sure to pop the iterator because it will be in
2909 front of that overlay string. When POS is ZV, we've thereby
2910 also ``processed'' overlay strings at ZV. */
2911 while (it->sp)
2912 pop_it (it);
2913 xassert (it->current.overlay_string_index == -1);
2914 xassert (it->method == GET_FROM_BUFFER);
2915 if (CHARPOS (pos->pos) == ZV)
2916 it->overlay_strings_at_end_processed_p = 1;
2918 #endif /* 0 */
2920 if (CHARPOS (pos->string_pos) >= 0)
2922 /* Recorded position is not in an overlay string, but in another
2923 string. This can only be a string from a `display' property.
2924 IT should already be filled with that string. */
2925 it->current.string_pos = pos->string_pos;
2926 xassert (STRINGP (it->string));
2929 /* Restore position in display vector translations, control
2930 character translations or ellipses. */
2931 if (pos->dpvec_index >= 0)
2933 if (it->dpvec == NULL)
2934 get_next_display_element (it);
2935 xassert (it->dpvec && it->current.dpvec_index == 0);
2936 it->current.dpvec_index = pos->dpvec_index;
2939 CHECK_IT (it);
2940 return !overlay_strings_with_newlines;
2944 /* Initialize IT for stepping through current_buffer in window W
2945 starting at ROW->start. */
2947 static void
2948 init_to_row_start (it, w, row)
2949 struct it *it;
2950 struct window *w;
2951 struct glyph_row *row;
2953 init_from_display_pos (it, w, &row->start);
2954 it->start = row->start;
2955 it->continuation_lines_width = row->continuation_lines_width;
2956 CHECK_IT (it);
2960 /* Initialize IT for stepping through current_buffer in window W
2961 starting in the line following ROW, i.e. starting at ROW->end.
2962 Value is zero if there are overlay strings with newlines at ROW's
2963 end position. */
2965 static int
2966 init_to_row_end (it, w, row)
2967 struct it *it;
2968 struct window *w;
2969 struct glyph_row *row;
2971 int success = 0;
2973 if (init_from_display_pos (it, w, &row->end))
2975 if (row->continued_p)
2976 it->continuation_lines_width
2977 = row->continuation_lines_width + row->pixel_width;
2978 CHECK_IT (it);
2979 success = 1;
2982 return success;
2988 /***********************************************************************
2989 Text properties
2990 ***********************************************************************/
2992 /* Called when IT reaches IT->stop_charpos. Handle text property and
2993 overlay changes. Set IT->stop_charpos to the next position where
2994 to stop. */
2996 static void
2997 handle_stop (it)
2998 struct it *it;
3000 enum prop_handled handled;
3001 int handle_overlay_change_p;
3002 struct props *p;
3004 it->dpvec = NULL;
3005 it->current.dpvec_index = -1;
3006 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3007 it->ignore_overlay_strings_at_pos_p = 0;
3009 /* Use face of preceding text for ellipsis (if invisible) */
3010 if (it->selective_display_ellipsis_p)
3011 it->saved_face_id = it->face_id;
3015 handled = HANDLED_NORMALLY;
3017 /* Call text property handlers. */
3018 for (p = it_props; p->handler; ++p)
3020 handled = p->handler (it);
3022 if (handled == HANDLED_RECOMPUTE_PROPS)
3023 break;
3024 else if (handled == HANDLED_RETURN)
3026 /* We still want to show before and after strings from
3027 overlays even if the actual buffer text is replaced. */
3028 if (!handle_overlay_change_p || it->sp > 1)
3029 return;
3030 if (!get_overlay_strings_1 (it, 0, 0))
3031 return;
3032 it->ignore_overlay_strings_at_pos_p = 1;
3033 it->string_from_display_prop_p = 0;
3034 handle_overlay_change_p = 0;
3035 handled = HANDLED_RECOMPUTE_PROPS;
3036 break;
3038 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3039 handle_overlay_change_p = 0;
3042 if (handled != HANDLED_RECOMPUTE_PROPS)
3044 /* Don't check for overlay strings below when set to deliver
3045 characters from a display vector. */
3046 if (it->method == GET_FROM_DISPLAY_VECTOR)
3047 handle_overlay_change_p = 0;
3049 /* Handle overlay changes. */
3050 if (handle_overlay_change_p)
3051 handled = handle_overlay_change (it);
3053 /* Determine where to stop next. */
3054 if (handled == HANDLED_NORMALLY)
3055 compute_stop_pos (it);
3058 while (handled == HANDLED_RECOMPUTE_PROPS);
3062 /* Compute IT->stop_charpos from text property and overlay change
3063 information for IT's current position. */
3065 static void
3066 compute_stop_pos (it)
3067 struct it *it;
3069 register INTERVAL iv, next_iv;
3070 Lisp_Object object, limit, position;
3072 /* If nowhere else, stop at the end. */
3073 it->stop_charpos = it->end_charpos;
3075 if (STRINGP (it->string))
3077 /* Strings are usually short, so don't limit the search for
3078 properties. */
3079 object = it->string;
3080 limit = Qnil;
3081 position = make_number (IT_STRING_CHARPOS (*it));
3083 else
3085 int charpos;
3087 /* If next overlay change is in front of the current stop pos
3088 (which is IT->end_charpos), stop there. Note: value of
3089 next_overlay_change is point-max if no overlay change
3090 follows. */
3091 charpos = next_overlay_change (IT_CHARPOS (*it));
3092 if (charpos < it->stop_charpos)
3093 it->stop_charpos = charpos;
3095 /* If showing the region, we have to stop at the region
3096 start or end because the face might change there. */
3097 if (it->region_beg_charpos > 0)
3099 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3100 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3101 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3102 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3105 /* Set up variables for computing the stop position from text
3106 property changes. */
3107 XSETBUFFER (object, current_buffer);
3108 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3109 position = make_number (IT_CHARPOS (*it));
3113 /* Get the interval containing IT's position. Value is a null
3114 interval if there isn't such an interval. */
3115 iv = validate_interval_range (object, &position, &position, 0);
3116 if (!NULL_INTERVAL_P (iv))
3118 Lisp_Object values_here[LAST_PROP_IDX];
3119 struct props *p;
3121 /* Get properties here. */
3122 for (p = it_props; p->handler; ++p)
3123 values_here[p->idx] = textget (iv->plist, *p->name);
3125 /* Look for an interval following iv that has different
3126 properties. */
3127 for (next_iv = next_interval (iv);
3128 (!NULL_INTERVAL_P (next_iv)
3129 && (NILP (limit)
3130 || XFASTINT (limit) > next_iv->position));
3131 next_iv = next_interval (next_iv))
3133 for (p = it_props; p->handler; ++p)
3135 Lisp_Object new_value;
3137 new_value = textget (next_iv->plist, *p->name);
3138 if (!EQ (values_here[p->idx], new_value))
3139 break;
3142 if (p->handler)
3143 break;
3146 if (!NULL_INTERVAL_P (next_iv))
3148 if (INTEGERP (limit)
3149 && next_iv->position >= XFASTINT (limit))
3150 /* No text property change up to limit. */
3151 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3152 else
3153 /* Text properties change in next_iv. */
3154 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3158 xassert (STRINGP (it->string)
3159 || (it->stop_charpos >= BEGV
3160 && it->stop_charpos >= IT_CHARPOS (*it)));
3164 /* Return the position of the next overlay change after POS in
3165 current_buffer. Value is point-max if no overlay change
3166 follows. This is like `next-overlay-change' but doesn't use
3167 xmalloc. */
3169 static int
3170 next_overlay_change (pos)
3171 int pos;
3173 int noverlays;
3174 int endpos;
3175 Lisp_Object *overlays;
3176 int i;
3178 /* Get all overlays at the given position. */
3179 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3181 /* If any of these overlays ends before endpos,
3182 use its ending point instead. */
3183 for (i = 0; i < noverlays; ++i)
3185 Lisp_Object oend;
3186 int oendpos;
3188 oend = OVERLAY_END (overlays[i]);
3189 oendpos = OVERLAY_POSITION (oend);
3190 endpos = min (endpos, oendpos);
3193 return endpos;
3198 /***********************************************************************
3199 Fontification
3200 ***********************************************************************/
3202 /* Handle changes in the `fontified' property of the current buffer by
3203 calling hook functions from Qfontification_functions to fontify
3204 regions of text. */
3206 static enum prop_handled
3207 handle_fontified_prop (it)
3208 struct it *it;
3210 Lisp_Object prop, pos;
3211 enum prop_handled handled = HANDLED_NORMALLY;
3213 if (!NILP (Vmemory_full))
3214 return handled;
3216 /* Get the value of the `fontified' property at IT's current buffer
3217 position. (The `fontified' property doesn't have a special
3218 meaning in strings.) If the value is nil, call functions from
3219 Qfontification_functions. */
3220 if (!STRINGP (it->string)
3221 && it->s == NULL
3222 && !NILP (Vfontification_functions)
3223 && !NILP (Vrun_hooks)
3224 && (pos = make_number (IT_CHARPOS (*it)),
3225 prop = Fget_char_property (pos, Qfontified, Qnil),
3226 NILP (prop)))
3228 int count = SPECPDL_INDEX ();
3229 Lisp_Object val;
3231 val = Vfontification_functions;
3232 specbind (Qfontification_functions, Qnil);
3234 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3235 safe_call1 (val, pos);
3236 else
3238 Lisp_Object globals, fn;
3239 struct gcpro gcpro1, gcpro2;
3241 globals = Qnil;
3242 GCPRO2 (val, globals);
3244 for (; CONSP (val); val = XCDR (val))
3246 fn = XCAR (val);
3248 if (EQ (fn, Qt))
3250 /* A value of t indicates this hook has a local
3251 binding; it means to run the global binding too.
3252 In a global value, t should not occur. If it
3253 does, we must ignore it to avoid an endless
3254 loop. */
3255 for (globals = Fdefault_value (Qfontification_functions);
3256 CONSP (globals);
3257 globals = XCDR (globals))
3259 fn = XCAR (globals);
3260 if (!EQ (fn, Qt))
3261 safe_call1 (fn, pos);
3264 else
3265 safe_call1 (fn, pos);
3268 UNGCPRO;
3271 unbind_to (count, Qnil);
3273 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3274 something. This avoids an endless loop if they failed to
3275 fontify the text for which reason ever. */
3276 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3277 handled = HANDLED_RECOMPUTE_PROPS;
3280 return handled;
3285 /***********************************************************************
3286 Faces
3287 ***********************************************************************/
3289 /* Set up iterator IT from face properties at its current position.
3290 Called from handle_stop. */
3292 static enum prop_handled
3293 handle_face_prop (it)
3294 struct it *it;
3296 int new_face_id, next_stop;
3298 if (!STRINGP (it->string))
3300 new_face_id
3301 = face_at_buffer_position (it->w,
3302 IT_CHARPOS (*it),
3303 it->region_beg_charpos,
3304 it->region_end_charpos,
3305 &next_stop,
3306 (IT_CHARPOS (*it)
3307 + TEXT_PROP_DISTANCE_LIMIT),
3310 /* Is this a start of a run of characters with box face?
3311 Caveat: this can be called for a freshly initialized
3312 iterator; face_id is -1 in this case. We know that the new
3313 face will not change until limit, i.e. if the new face has a
3314 box, all characters up to limit will have one. But, as
3315 usual, we don't know whether limit is really the end. */
3316 if (new_face_id != it->face_id)
3318 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3320 /* If new face has a box but old face has not, this is
3321 the start of a run of characters with box, i.e. it has
3322 a shadow on the left side. The value of face_id of the
3323 iterator will be -1 if this is the initial call that gets
3324 the face. In this case, we have to look in front of IT's
3325 position and see whether there is a face != new_face_id. */
3326 it->start_of_box_run_p
3327 = (new_face->box != FACE_NO_BOX
3328 && (it->face_id >= 0
3329 || IT_CHARPOS (*it) == BEG
3330 || new_face_id != face_before_it_pos (it)));
3331 it->face_box_p = new_face->box != FACE_NO_BOX;
3334 else
3336 int base_face_id, bufpos;
3338 if (it->current.overlay_string_index >= 0)
3339 bufpos = IT_CHARPOS (*it);
3340 else
3341 bufpos = 0;
3343 /* For strings from a buffer, i.e. overlay strings or strings
3344 from a `display' property, use the face at IT's current
3345 buffer position as the base face to merge with, so that
3346 overlay strings appear in the same face as surrounding
3347 text, unless they specify their own faces. */
3348 base_face_id = underlying_face_id (it);
3350 new_face_id = face_at_string_position (it->w,
3351 it->string,
3352 IT_STRING_CHARPOS (*it),
3353 bufpos,
3354 it->region_beg_charpos,
3355 it->region_end_charpos,
3356 &next_stop,
3357 base_face_id, 0);
3359 #if 0 /* This shouldn't be neccessary. Let's check it. */
3360 /* If IT is used to display a mode line we would really like to
3361 use the mode line face instead of the frame's default face. */
3362 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3363 && new_face_id == DEFAULT_FACE_ID)
3364 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3365 #endif
3367 /* Is this a start of a run of characters with box? Caveat:
3368 this can be called for a freshly allocated iterator; face_id
3369 is -1 is this case. We know that the new face will not
3370 change until the next check pos, i.e. if the new face has a
3371 box, all characters up to that position will have a
3372 box. But, as usual, we don't know whether that position
3373 is really the end. */
3374 if (new_face_id != it->face_id)
3376 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3377 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3379 /* If new face has a box but old face hasn't, this is the
3380 start of a run of characters with box, i.e. it has a
3381 shadow on the left side. */
3382 it->start_of_box_run_p
3383 = new_face->box && (old_face == NULL || !old_face->box);
3384 it->face_box_p = new_face->box != FACE_NO_BOX;
3388 it->face_id = new_face_id;
3389 return HANDLED_NORMALLY;
3393 /* Return the ID of the face ``underlying'' IT's current position,
3394 which is in a string. If the iterator is associated with a
3395 buffer, return the face at IT's current buffer position.
3396 Otherwise, use the iterator's base_face_id. */
3398 static int
3399 underlying_face_id (it)
3400 struct it *it;
3402 int face_id = it->base_face_id, i;
3404 xassert (STRINGP (it->string));
3406 for (i = it->sp - 1; i >= 0; --i)
3407 if (NILP (it->stack[i].string))
3408 face_id = it->stack[i].face_id;
3410 return face_id;
3414 /* Compute the face one character before or after the current position
3415 of IT. BEFORE_P non-zero means get the face in front of IT's
3416 position. Value is the id of the face. */
3418 static int
3419 face_before_or_after_it_pos (it, before_p)
3420 struct it *it;
3421 int before_p;
3423 int face_id, limit;
3424 int next_check_charpos;
3425 struct text_pos pos;
3427 xassert (it->s == NULL);
3429 if (STRINGP (it->string))
3431 int bufpos, base_face_id;
3433 /* No face change past the end of the string (for the case
3434 we are padding with spaces). No face change before the
3435 string start. */
3436 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3437 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3438 return it->face_id;
3440 /* Set pos to the position before or after IT's current position. */
3441 if (before_p)
3442 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3443 else
3444 /* For composition, we must check the character after the
3445 composition. */
3446 pos = (it->what == IT_COMPOSITION
3447 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3448 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3450 if (it->current.overlay_string_index >= 0)
3451 bufpos = IT_CHARPOS (*it);
3452 else
3453 bufpos = 0;
3455 base_face_id = underlying_face_id (it);
3457 /* Get the face for ASCII, or unibyte. */
3458 face_id = face_at_string_position (it->w,
3459 it->string,
3460 CHARPOS (pos),
3461 bufpos,
3462 it->region_beg_charpos,
3463 it->region_end_charpos,
3464 &next_check_charpos,
3465 base_face_id, 0);
3467 /* Correct the face for charsets different from ASCII. Do it
3468 for the multibyte case only. The face returned above is
3469 suitable for unibyte text if IT->string is unibyte. */
3470 if (STRING_MULTIBYTE (it->string))
3472 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3473 int rest = SBYTES (it->string) - BYTEPOS (pos);
3474 int c, len;
3475 struct face *face = FACE_FROM_ID (it->f, face_id);
3477 c = string_char_and_length (p, rest, &len);
3478 face_id = FACE_FOR_CHAR (it->f, face, c);
3481 else
3483 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3484 || (IT_CHARPOS (*it) <= BEGV && before_p))
3485 return it->face_id;
3487 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3488 pos = it->current.pos;
3490 if (before_p)
3491 DEC_TEXT_POS (pos, it->multibyte_p);
3492 else
3494 if (it->what == IT_COMPOSITION)
3495 /* For composition, we must check the position after the
3496 composition. */
3497 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3498 else
3499 INC_TEXT_POS (pos, it->multibyte_p);
3502 /* Determine face for CHARSET_ASCII, or unibyte. */
3503 face_id = face_at_buffer_position (it->w,
3504 CHARPOS (pos),
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_check_charpos,
3508 limit, 0);
3510 /* Correct the face for charsets different from ASCII. Do it
3511 for the multibyte case only. The face returned above is
3512 suitable for unibyte text if current_buffer is unibyte. */
3513 if (it->multibyte_p)
3515 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3516 struct face *face = FACE_FROM_ID (it->f, face_id);
3517 face_id = FACE_FOR_CHAR (it->f, face, c);
3521 return face_id;
3526 /***********************************************************************
3527 Invisible text
3528 ***********************************************************************/
3530 /* Set up iterator IT from invisible properties at its current
3531 position. Called from handle_stop. */
3533 static enum prop_handled
3534 handle_invisible_prop (it)
3535 struct it *it;
3537 enum prop_handled handled = HANDLED_NORMALLY;
3539 if (STRINGP (it->string))
3541 extern Lisp_Object Qinvisible;
3542 Lisp_Object prop, end_charpos, limit, charpos;
3544 /* Get the value of the invisible text property at the
3545 current position. Value will be nil if there is no such
3546 property. */
3547 charpos = make_number (IT_STRING_CHARPOS (*it));
3548 prop = Fget_text_property (charpos, Qinvisible, it->string);
3550 if (!NILP (prop)
3551 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3553 handled = HANDLED_RECOMPUTE_PROPS;
3555 /* Get the position at which the next change of the
3556 invisible text property can be found in IT->string.
3557 Value will be nil if the property value is the same for
3558 all the rest of IT->string. */
3559 XSETINT (limit, SCHARS (it->string));
3560 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3561 it->string, limit);
3563 /* Text at current position is invisible. The next
3564 change in the property is at position end_charpos.
3565 Move IT's current position to that position. */
3566 if (INTEGERP (end_charpos)
3567 && XFASTINT (end_charpos) < XFASTINT (limit))
3569 struct text_pos old;
3570 old = it->current.string_pos;
3571 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3572 compute_string_pos (&it->current.string_pos, old, it->string);
3574 else
3576 /* The rest of the string is invisible. If this is an
3577 overlay string, proceed with the next overlay string
3578 or whatever comes and return a character from there. */
3579 if (it->current.overlay_string_index >= 0)
3581 next_overlay_string (it);
3582 /* Don't check for overlay strings when we just
3583 finished processing them. */
3584 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3586 else
3588 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3589 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3594 else
3596 int invis_p, newpos, next_stop, start_charpos;
3597 Lisp_Object pos, prop, overlay;
3599 /* First of all, is there invisible text at this position? */
3600 start_charpos = IT_CHARPOS (*it);
3601 pos = make_number (IT_CHARPOS (*it));
3602 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3603 &overlay);
3604 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3606 /* If we are on invisible text, skip over it. */
3607 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3609 /* Record whether we have to display an ellipsis for the
3610 invisible text. */
3611 int display_ellipsis_p = invis_p == 2;
3613 handled = HANDLED_RECOMPUTE_PROPS;
3615 /* Loop skipping over invisible text. The loop is left at
3616 ZV or with IT on the first char being visible again. */
3619 /* Try to skip some invisible text. Return value is the
3620 position reached which can be equal to IT's position
3621 if there is nothing invisible here. This skips both
3622 over invisible text properties and overlays with
3623 invisible property. */
3624 newpos = skip_invisible (IT_CHARPOS (*it),
3625 &next_stop, ZV, it->window);
3627 /* If we skipped nothing at all we weren't at invisible
3628 text in the first place. If everything to the end of
3629 the buffer was skipped, end the loop. */
3630 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3631 invis_p = 0;
3632 else
3634 /* We skipped some characters but not necessarily
3635 all there are. Check if we ended up on visible
3636 text. Fget_char_property returns the property of
3637 the char before the given position, i.e. if we
3638 get invis_p = 0, this means that the char at
3639 newpos is visible. */
3640 pos = make_number (newpos);
3641 prop = Fget_char_property (pos, Qinvisible, it->window);
3642 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3645 /* If we ended up on invisible text, proceed to
3646 skip starting with next_stop. */
3647 if (invis_p)
3648 IT_CHARPOS (*it) = next_stop;
3650 /* If there are adjacent invisible texts, don't lose the
3651 second one's ellipsis. */
3652 if (invis_p == 2)
3653 display_ellipsis_p = 1;
3655 while (invis_p);
3657 /* The position newpos is now either ZV or on visible text. */
3658 IT_CHARPOS (*it) = newpos;
3659 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3661 /* If there are before-strings at the start of invisible
3662 text, and the text is invisible because of a text
3663 property, arrange to show before-strings because 20.x did
3664 it that way. (If the text is invisible because of an
3665 overlay property instead of a text property, this is
3666 already handled in the overlay code.) */
3667 if (NILP (overlay)
3668 && get_overlay_strings (it, start_charpos))
3670 handled = HANDLED_RECOMPUTE_PROPS;
3671 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3673 else if (display_ellipsis_p)
3675 /* Make sure that the glyphs of the ellipsis will get
3676 correct `charpos' values. If we would not update
3677 it->position here, the glyphs would belong to the
3678 last visible character _before_ the invisible
3679 text, which confuses `set_cursor_from_row'.
3681 We use the last invisible position instead of the
3682 first because this way the cursor is always drawn on
3683 the first "." of the ellipsis, whenever PT is inside
3684 the invisible text. Otherwise the cursor would be
3685 placed _after_ the ellipsis when the point is after the
3686 first invisible character. */
3687 if (!STRINGP (it->object))
3689 it->position.charpos = IT_CHARPOS (*it) - 1;
3690 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3692 setup_for_ellipsis (it, 0);
3697 return handled;
3701 /* Make iterator IT return `...' next.
3702 Replaces LEN characters from buffer. */
3704 static void
3705 setup_for_ellipsis (it, len)
3706 struct it *it;
3707 int len;
3709 /* Use the display table definition for `...'. Invalid glyphs
3710 will be handled by the method returning elements from dpvec. */
3711 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3713 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3714 it->dpvec = v->contents;
3715 it->dpend = v->contents + v->size;
3717 else
3719 /* Default `...'. */
3720 it->dpvec = default_invis_vector;
3721 it->dpend = default_invis_vector + 3;
3724 it->dpvec_char_len = len;
3725 it->current.dpvec_index = 0;
3726 it->dpvec_face_id = -1;
3728 /* Remember the current face id in case glyphs specify faces.
3729 IT's face is restored in set_iterator_to_next.
3730 saved_face_id was set to preceding char's face in handle_stop. */
3731 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3732 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3734 it->method = GET_FROM_DISPLAY_VECTOR;
3735 it->ellipsis_p = 1;
3740 /***********************************************************************
3741 'display' property
3742 ***********************************************************************/
3744 /* Set up iterator IT from `display' property at its current position.
3745 Called from handle_stop.
3746 We return HANDLED_RETURN if some part of the display property
3747 overrides the display of the buffer text itself.
3748 Otherwise we return HANDLED_NORMALLY. */
3750 static enum prop_handled
3751 handle_display_prop (it)
3752 struct it *it;
3754 Lisp_Object prop, object;
3755 struct text_pos *position;
3756 /* Nonzero if some property replaces the display of the text itself. */
3757 int display_replaced_p = 0;
3759 if (STRINGP (it->string))
3761 object = it->string;
3762 position = &it->current.string_pos;
3764 else
3766 XSETWINDOW (object, it->w);
3767 position = &it->current.pos;
3770 /* Reset those iterator values set from display property values. */
3771 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3772 it->space_width = Qnil;
3773 it->font_height = Qnil;
3774 it->voffset = 0;
3776 /* We don't support recursive `display' properties, i.e. string
3777 values that have a string `display' property, that have a string
3778 `display' property etc. */
3779 if (!it->string_from_display_prop_p)
3780 it->area = TEXT_AREA;
3782 prop = Fget_char_property (make_number (position->charpos),
3783 Qdisplay, object);
3784 if (NILP (prop))
3785 return HANDLED_NORMALLY;
3787 if (!STRINGP (it->string))
3788 object = it->w->buffer;
3790 if (CONSP (prop)
3791 /* Simple properties. */
3792 && !EQ (XCAR (prop), Qimage)
3793 && !EQ (XCAR (prop), Qspace)
3794 && !EQ (XCAR (prop), Qwhen)
3795 && !EQ (XCAR (prop), Qslice)
3796 && !EQ (XCAR (prop), Qspace_width)
3797 && !EQ (XCAR (prop), Qheight)
3798 && !EQ (XCAR (prop), Qraise)
3799 /* Marginal area specifications. */
3800 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3801 && !EQ (XCAR (prop), Qleft_fringe)
3802 && !EQ (XCAR (prop), Qright_fringe)
3803 && !NILP (XCAR (prop)))
3805 for (; CONSP (prop); prop = XCDR (prop))
3807 if (handle_single_display_spec (it, XCAR (prop), object,
3808 position, display_replaced_p))
3809 display_replaced_p = 1;
3812 else if (VECTORP (prop))
3814 int i;
3815 for (i = 0; i < ASIZE (prop); ++i)
3816 if (handle_single_display_spec (it, AREF (prop, i), object,
3817 position, display_replaced_p))
3818 display_replaced_p = 1;
3820 else
3822 int ret = handle_single_display_spec (it, prop, object, position, 0);
3823 if (ret < 0) /* Replaced by "", i.e. nothing. */
3824 return HANDLED_RECOMPUTE_PROPS;
3825 if (ret)
3826 display_replaced_p = 1;
3829 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3833 /* Value is the position of the end of the `display' property starting
3834 at START_POS in OBJECT. */
3836 static struct text_pos
3837 display_prop_end (it, object, start_pos)
3838 struct it *it;
3839 Lisp_Object object;
3840 struct text_pos start_pos;
3842 Lisp_Object end;
3843 struct text_pos end_pos;
3845 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3846 Qdisplay, object, Qnil);
3847 CHARPOS (end_pos) = XFASTINT (end);
3848 if (STRINGP (object))
3849 compute_string_pos (&end_pos, start_pos, it->string);
3850 else
3851 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3853 return end_pos;
3857 /* Set up IT from a single `display' specification PROP. OBJECT
3858 is the object in which the `display' property was found. *POSITION
3859 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3860 means that we previously saw a display specification which already
3861 replaced text display with something else, for example an image;
3862 we ignore such properties after the first one has been processed.
3864 If PROP is a `space' or `image' specification, and in some other
3865 cases too, set *POSITION to the position where the `display'
3866 property ends.
3868 Value is non-zero if something was found which replaces the display
3869 of buffer or string text. Specifically, the value is -1 if that
3870 "something" is "nothing". */
3872 static int
3873 handle_single_display_spec (it, spec, object, position,
3874 display_replaced_before_p)
3875 struct it *it;
3876 Lisp_Object spec;
3877 Lisp_Object object;
3878 struct text_pos *position;
3879 int display_replaced_before_p;
3881 Lisp_Object form;
3882 Lisp_Object location, value;
3883 struct text_pos start_pos;
3884 int valid_p;
3886 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3887 If the result is non-nil, use VALUE instead of SPEC. */
3888 form = Qt;
3889 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3891 spec = XCDR (spec);
3892 if (!CONSP (spec))
3893 return 0;
3894 form = XCAR (spec);
3895 spec = XCDR (spec);
3898 if (!NILP (form) && !EQ (form, Qt))
3900 int count = SPECPDL_INDEX ();
3901 struct gcpro gcpro1;
3903 /* Bind `object' to the object having the `display' property, a
3904 buffer or string. Bind `position' to the position in the
3905 object where the property was found, and `buffer-position'
3906 to the current position in the buffer. */
3907 specbind (Qobject, object);
3908 specbind (Qposition, make_number (CHARPOS (*position)));
3909 specbind (Qbuffer_position,
3910 make_number (STRINGP (object)
3911 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3912 GCPRO1 (form);
3913 form = safe_eval (form);
3914 UNGCPRO;
3915 unbind_to (count, Qnil);
3918 if (NILP (form))
3919 return 0;
3921 /* Handle `(height HEIGHT)' specifications. */
3922 if (CONSP (spec)
3923 && EQ (XCAR (spec), Qheight)
3924 && CONSP (XCDR (spec)))
3926 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3927 return 0;
3929 it->font_height = XCAR (XCDR (spec));
3930 if (!NILP (it->font_height))
3932 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3933 int new_height = -1;
3935 if (CONSP (it->font_height)
3936 && (EQ (XCAR (it->font_height), Qplus)
3937 || EQ (XCAR (it->font_height), Qminus))
3938 && CONSP (XCDR (it->font_height))
3939 && INTEGERP (XCAR (XCDR (it->font_height))))
3941 /* `(+ N)' or `(- N)' where N is an integer. */
3942 int steps = XINT (XCAR (XCDR (it->font_height)));
3943 if (EQ (XCAR (it->font_height), Qplus))
3944 steps = - steps;
3945 it->face_id = smaller_face (it->f, it->face_id, steps);
3947 else if (FUNCTIONP (it->font_height))
3949 /* Call function with current height as argument.
3950 Value is the new height. */
3951 Lisp_Object height;
3952 height = safe_call1 (it->font_height,
3953 face->lface[LFACE_HEIGHT_INDEX]);
3954 if (NUMBERP (height))
3955 new_height = XFLOATINT (height);
3957 else if (NUMBERP (it->font_height))
3959 /* Value is a multiple of the canonical char height. */
3960 struct face *face;
3962 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3963 new_height = (XFLOATINT (it->font_height)
3964 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3966 else
3968 /* Evaluate IT->font_height with `height' bound to the
3969 current specified height to get the new height. */
3970 int count = SPECPDL_INDEX ();
3972 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3973 value = safe_eval (it->font_height);
3974 unbind_to (count, Qnil);
3976 if (NUMBERP (value))
3977 new_height = XFLOATINT (value);
3980 if (new_height > 0)
3981 it->face_id = face_with_height (it->f, it->face_id, new_height);
3984 return 0;
3987 /* Handle `(space_width WIDTH)'. */
3988 if (CONSP (spec)
3989 && EQ (XCAR (spec), Qspace_width)
3990 && CONSP (XCDR (spec)))
3992 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3993 return 0;
3995 value = XCAR (XCDR (spec));
3996 if (NUMBERP (value) && XFLOATINT (value) > 0)
3997 it->space_width = value;
3999 return 0;
4002 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4003 if (CONSP (spec)
4004 && EQ (XCAR (spec), Qslice))
4006 Lisp_Object tem;
4008 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4009 return 0;
4011 if (tem = XCDR (spec), CONSP (tem))
4013 it->slice.x = XCAR (tem);
4014 if (tem = XCDR (tem), CONSP (tem))
4016 it->slice.y = XCAR (tem);
4017 if (tem = XCDR (tem), CONSP (tem))
4019 it->slice.width = XCAR (tem);
4020 if (tem = XCDR (tem), CONSP (tem))
4021 it->slice.height = XCAR (tem);
4026 return 0;
4029 /* Handle `(raise FACTOR)'. */
4030 if (CONSP (spec)
4031 && EQ (XCAR (spec), Qraise)
4032 && CONSP (XCDR (spec)))
4034 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4035 return 0;
4037 #ifdef HAVE_WINDOW_SYSTEM
4038 value = XCAR (XCDR (spec));
4039 if (NUMBERP (value))
4041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4042 it->voffset = - (XFLOATINT (value)
4043 * (FONT_HEIGHT (face->font)));
4045 #endif /* HAVE_WINDOW_SYSTEM */
4047 return 0;
4050 /* Don't handle the other kinds of display specifications
4051 inside a string that we got from a `display' property. */
4052 if (it->string_from_display_prop_p)
4053 return 0;
4055 /* Characters having this form of property are not displayed, so
4056 we have to find the end of the property. */
4057 start_pos = *position;
4058 *position = display_prop_end (it, object, start_pos);
4059 value = Qnil;
4061 /* Stop the scan at that end position--we assume that all
4062 text properties change there. */
4063 it->stop_charpos = position->charpos;
4065 /* Handle `(left-fringe BITMAP [FACE])'
4066 and `(right-fringe BITMAP [FACE])'. */
4067 if (CONSP (spec)
4068 && (EQ (XCAR (spec), Qleft_fringe)
4069 || EQ (XCAR (spec), Qright_fringe))
4070 && CONSP (XCDR (spec)))
4072 int face_id = DEFAULT_FACE_ID;
4073 int fringe_bitmap;
4075 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4076 /* If we return here, POSITION has been advanced
4077 across the text with this property. */
4078 return 0;
4080 #ifdef HAVE_WINDOW_SYSTEM
4081 value = XCAR (XCDR (spec));
4082 if (!SYMBOLP (value)
4083 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4084 /* If we return here, POSITION has been advanced
4085 across the text with this property. */
4086 return 0;
4088 if (CONSP (XCDR (XCDR (spec))))
4090 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4091 int face_id2 = lookup_derived_face (it->f, face_name,
4092 'A', FRINGE_FACE_ID, 0);
4093 if (face_id2 >= 0)
4094 face_id = face_id2;
4097 /* Save current settings of IT so that we can restore them
4098 when we are finished with the glyph property value. */
4100 push_it (it);
4102 it->area = TEXT_AREA;
4103 it->what = IT_IMAGE;
4104 it->image_id = -1; /* no image */
4105 it->position = start_pos;
4106 it->object = NILP (object) ? it->w->buffer : object;
4107 it->method = GET_FROM_IMAGE;
4108 it->face_id = face_id;
4110 /* Say that we haven't consumed the characters with
4111 `display' property yet. The call to pop_it in
4112 set_iterator_to_next will clean this up. */
4113 *position = start_pos;
4115 if (EQ (XCAR (spec), Qleft_fringe))
4117 it->left_user_fringe_bitmap = fringe_bitmap;
4118 it->left_user_fringe_face_id = face_id;
4120 else
4122 it->right_user_fringe_bitmap = fringe_bitmap;
4123 it->right_user_fringe_face_id = face_id;
4125 #endif /* HAVE_WINDOW_SYSTEM */
4126 return 1;
4129 /* Prepare to handle `((margin left-margin) ...)',
4130 `((margin right-margin) ...)' and `((margin nil) ...)'
4131 prefixes for display specifications. */
4132 location = Qunbound;
4133 if (CONSP (spec) && CONSP (XCAR (spec)))
4135 Lisp_Object tem;
4137 value = XCDR (spec);
4138 if (CONSP (value))
4139 value = XCAR (value);
4141 tem = XCAR (spec);
4142 if (EQ (XCAR (tem), Qmargin)
4143 && (tem = XCDR (tem),
4144 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4145 (NILP (tem)
4146 || EQ (tem, Qleft_margin)
4147 || EQ (tem, Qright_margin))))
4148 location = tem;
4151 if (EQ (location, Qunbound))
4153 location = Qnil;
4154 value = spec;
4157 /* After this point, VALUE is the property after any
4158 margin prefix has been stripped. It must be a string,
4159 an image specification, or `(space ...)'.
4161 LOCATION specifies where to display: `left-margin',
4162 `right-margin' or nil. */
4164 valid_p = (STRINGP (value)
4165 #ifdef HAVE_WINDOW_SYSTEM
4166 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4167 #endif /* not HAVE_WINDOW_SYSTEM */
4168 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4170 if (valid_p && !display_replaced_before_p)
4172 /* Save current settings of IT so that we can restore them
4173 when we are finished with the glyph property value. */
4174 push_it (it);
4176 if (NILP (location))
4177 it->area = TEXT_AREA;
4178 else if (EQ (location, Qleft_margin))
4179 it->area = LEFT_MARGIN_AREA;
4180 else
4181 it->area = RIGHT_MARGIN_AREA;
4183 if (STRINGP (value))
4185 if (SCHARS (value) == 0)
4187 pop_it (it);
4188 return -1; /* Replaced by "", i.e. nothing. */
4190 it->string = value;
4191 it->multibyte_p = STRING_MULTIBYTE (it->string);
4192 it->current.overlay_string_index = -1;
4193 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4194 it->end_charpos = it->string_nchars = SCHARS (it->string);
4195 it->method = GET_FROM_STRING;
4196 it->stop_charpos = 0;
4197 it->string_from_display_prop_p = 1;
4198 /* Say that we haven't consumed the characters with
4199 `display' property yet. The call to pop_it in
4200 set_iterator_to_next will clean this up. */
4201 *position = start_pos;
4203 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4205 it->method = GET_FROM_STRETCH;
4206 it->object = value;
4207 *position = it->position = start_pos;
4209 #ifdef HAVE_WINDOW_SYSTEM
4210 else
4212 it->what = IT_IMAGE;
4213 it->image_id = lookup_image (it->f, value);
4214 it->position = start_pos;
4215 it->object = NILP (object) ? it->w->buffer : object;
4216 it->method = GET_FROM_IMAGE;
4218 /* Say that we haven't consumed the characters with
4219 `display' property yet. The call to pop_it in
4220 set_iterator_to_next will clean this up. */
4221 *position = start_pos;
4223 #endif /* HAVE_WINDOW_SYSTEM */
4225 return 1;
4228 /* Invalid property or property not supported. Restore
4229 POSITION to what it was before. */
4230 *position = start_pos;
4231 return 0;
4235 /* Check if SPEC is a display specification value whose text should be
4236 treated as intangible. */
4238 static int
4239 single_display_spec_intangible_p (prop)
4240 Lisp_Object prop;
4242 /* Skip over `when FORM'. */
4243 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4245 prop = XCDR (prop);
4246 if (!CONSP (prop))
4247 return 0;
4248 prop = XCDR (prop);
4251 if (STRINGP (prop))
4252 return 1;
4254 if (!CONSP (prop))
4255 return 0;
4257 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4258 we don't need to treat text as intangible. */
4259 if (EQ (XCAR (prop), Qmargin))
4261 prop = XCDR (prop);
4262 if (!CONSP (prop))
4263 return 0;
4265 prop = XCDR (prop);
4266 if (!CONSP (prop)
4267 || EQ (XCAR (prop), Qleft_margin)
4268 || EQ (XCAR (prop), Qright_margin))
4269 return 0;
4272 return (CONSP (prop)
4273 && (EQ (XCAR (prop), Qimage)
4274 || EQ (XCAR (prop), Qspace)));
4278 /* Check if PROP is a display property value whose text should be
4279 treated as intangible. */
4282 display_prop_intangible_p (prop)
4283 Lisp_Object prop;
4285 if (CONSP (prop)
4286 && CONSP (XCAR (prop))
4287 && !EQ (Qmargin, XCAR (XCAR (prop))))
4289 /* A list of sub-properties. */
4290 while (CONSP (prop))
4292 if (single_display_spec_intangible_p (XCAR (prop)))
4293 return 1;
4294 prop = XCDR (prop);
4297 else if (VECTORP (prop))
4299 /* A vector of sub-properties. */
4300 int i;
4301 for (i = 0; i < ASIZE (prop); ++i)
4302 if (single_display_spec_intangible_p (AREF (prop, i)))
4303 return 1;
4305 else
4306 return single_display_spec_intangible_p (prop);
4308 return 0;
4312 /* Return 1 if PROP is a display sub-property value containing STRING. */
4314 static int
4315 single_display_spec_string_p (prop, string)
4316 Lisp_Object prop, string;
4318 if (EQ (string, prop))
4319 return 1;
4321 /* Skip over `when FORM'. */
4322 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4324 prop = XCDR (prop);
4325 if (!CONSP (prop))
4326 return 0;
4327 prop = XCDR (prop);
4330 if (CONSP (prop))
4331 /* Skip over `margin LOCATION'. */
4332 if (EQ (XCAR (prop), Qmargin))
4334 prop = XCDR (prop);
4335 if (!CONSP (prop))
4336 return 0;
4338 prop = XCDR (prop);
4339 if (!CONSP (prop))
4340 return 0;
4343 return CONSP (prop) && EQ (XCAR (prop), string);
4347 /* Return 1 if STRING appears in the `display' property PROP. */
4349 static int
4350 display_prop_string_p (prop, string)
4351 Lisp_Object prop, string;
4353 if (CONSP (prop)
4354 && CONSP (XCAR (prop))
4355 && !EQ (Qmargin, XCAR (XCAR (prop))))
4357 /* A list of sub-properties. */
4358 while (CONSP (prop))
4360 if (single_display_spec_string_p (XCAR (prop), string))
4361 return 1;
4362 prop = XCDR (prop);
4365 else if (VECTORP (prop))
4367 /* A vector of sub-properties. */
4368 int i;
4369 for (i = 0; i < ASIZE (prop); ++i)
4370 if (single_display_spec_string_p (AREF (prop, i), string))
4371 return 1;
4373 else
4374 return single_display_spec_string_p (prop, string);
4376 return 0;
4380 /* Determine from which buffer position in W's buffer STRING comes
4381 from. AROUND_CHARPOS is an approximate position where it could
4382 be from. Value is the buffer position or 0 if it couldn't be
4383 determined.
4385 W's buffer must be current.
4387 This function is necessary because we don't record buffer positions
4388 in glyphs generated from strings (to keep struct glyph small).
4389 This function may only use code that doesn't eval because it is
4390 called asynchronously from note_mouse_highlight. */
4393 string_buffer_position (w, string, around_charpos)
4394 struct window *w;
4395 Lisp_Object string;
4396 int around_charpos;
4398 Lisp_Object limit, prop, pos;
4399 const int MAX_DISTANCE = 1000;
4400 int found = 0;
4402 pos = make_number (around_charpos);
4403 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4404 while (!found && !EQ (pos, limit))
4406 prop = Fget_char_property (pos, Qdisplay, Qnil);
4407 if (!NILP (prop) && display_prop_string_p (prop, string))
4408 found = 1;
4409 else
4410 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4413 if (!found)
4415 pos = make_number (around_charpos);
4416 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4417 while (!found && !EQ (pos, limit))
4419 prop = Fget_char_property (pos, Qdisplay, Qnil);
4420 if (!NILP (prop) && display_prop_string_p (prop, string))
4421 found = 1;
4422 else
4423 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4424 limit);
4428 return found ? XINT (pos) : 0;
4433 /***********************************************************************
4434 `composition' property
4435 ***********************************************************************/
4437 /* Set up iterator IT from `composition' property at its current
4438 position. Called from handle_stop. */
4440 static enum prop_handled
4441 handle_composition_prop (it)
4442 struct it *it;
4444 Lisp_Object prop, string;
4445 int pos, pos_byte, end;
4446 enum prop_handled handled = HANDLED_NORMALLY;
4448 if (STRINGP (it->string))
4450 pos = IT_STRING_CHARPOS (*it);
4451 pos_byte = IT_STRING_BYTEPOS (*it);
4452 string = it->string;
4454 else
4456 pos = IT_CHARPOS (*it);
4457 pos_byte = IT_BYTEPOS (*it);
4458 string = Qnil;
4461 /* If there's a valid composition and point is not inside of the
4462 composition (in the case that the composition is from the current
4463 buffer), draw a glyph composed from the composition components. */
4464 if (find_composition (pos, -1, &pos, &end, &prop, string)
4465 && COMPOSITION_VALID_P (pos, end, prop)
4466 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4468 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4470 if (id >= 0)
4472 struct composition *cmp = composition_table[id];
4474 if (cmp->glyph_len == 0)
4476 /* No glyph. */
4477 if (STRINGP (it->string))
4479 IT_STRING_CHARPOS (*it) = end;
4480 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4481 end);
4483 else
4485 IT_CHARPOS (*it) = end;
4486 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4488 return HANDLED_RECOMPUTE_PROPS;
4491 it->stop_charpos = end;
4492 push_it (it);
4494 it->method = GET_FROM_COMPOSITION;
4495 it->cmp_id = id;
4496 it->cmp_len = COMPOSITION_LENGTH (prop);
4497 /* For a terminal, draw only the first character of the
4498 components. */
4499 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4500 it->len = (STRINGP (it->string)
4501 ? string_char_to_byte (it->string, end)
4502 : CHAR_TO_BYTE (end)) - pos_byte;
4503 handled = HANDLED_RETURN;
4507 return handled;
4512 /***********************************************************************
4513 Overlay strings
4514 ***********************************************************************/
4516 /* The following structure is used to record overlay strings for
4517 later sorting in load_overlay_strings. */
4519 struct overlay_entry
4521 Lisp_Object overlay;
4522 Lisp_Object string;
4523 int priority;
4524 int after_string_p;
4528 /* Set up iterator IT from overlay strings at its current position.
4529 Called from handle_stop. */
4531 static enum prop_handled
4532 handle_overlay_change (it)
4533 struct it *it;
4535 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4536 return HANDLED_RECOMPUTE_PROPS;
4537 else
4538 return HANDLED_NORMALLY;
4542 /* Set up the next overlay string for delivery by IT, if there is an
4543 overlay string to deliver. Called by set_iterator_to_next when the
4544 end of the current overlay string is reached. If there are more
4545 overlay strings to display, IT->string and
4546 IT->current.overlay_string_index are set appropriately here.
4547 Otherwise IT->string is set to nil. */
4549 static void
4550 next_overlay_string (it)
4551 struct it *it;
4553 ++it->current.overlay_string_index;
4554 if (it->current.overlay_string_index == it->n_overlay_strings)
4556 /* No more overlay strings. Restore IT's settings to what
4557 they were before overlay strings were processed, and
4558 continue to deliver from current_buffer. */
4559 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4561 pop_it (it);
4562 xassert (it->sp > 0
4563 || it->method == GET_FROM_COMPOSITION
4564 || (NILP (it->string)
4565 && it->method == GET_FROM_BUFFER
4566 && it->stop_charpos >= BEGV
4567 && it->stop_charpos <= it->end_charpos));
4568 it->current.overlay_string_index = -1;
4569 it->n_overlay_strings = 0;
4571 /* If we're at the end of the buffer, record that we have
4572 processed the overlay strings there already, so that
4573 next_element_from_buffer doesn't try it again. */
4574 if (IT_CHARPOS (*it) >= it->end_charpos)
4575 it->overlay_strings_at_end_processed_p = 1;
4577 /* If we have to display `...' for invisible text, set
4578 the iterator up for that. */
4579 if (display_ellipsis_p)
4580 setup_for_ellipsis (it, 0);
4582 else
4584 /* There are more overlay strings to process. If
4585 IT->current.overlay_string_index has advanced to a position
4586 where we must load IT->overlay_strings with more strings, do
4587 it. */
4588 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4590 if (it->current.overlay_string_index && i == 0)
4591 load_overlay_strings (it, 0);
4593 /* Initialize IT to deliver display elements from the overlay
4594 string. */
4595 it->string = it->overlay_strings[i];
4596 it->multibyte_p = STRING_MULTIBYTE (it->string);
4597 SET_TEXT_POS (it->current.string_pos, 0, 0);
4598 it->method = GET_FROM_STRING;
4599 it->stop_charpos = 0;
4602 CHECK_IT (it);
4606 /* Compare two overlay_entry structures E1 and E2. Used as a
4607 comparison function for qsort in load_overlay_strings. Overlay
4608 strings for the same position are sorted so that
4610 1. All after-strings come in front of before-strings, except
4611 when they come from the same overlay.
4613 2. Within after-strings, strings are sorted so that overlay strings
4614 from overlays with higher priorities come first.
4616 2. Within before-strings, strings are sorted so that overlay
4617 strings from overlays with higher priorities come last.
4619 Value is analogous to strcmp. */
4622 static int
4623 compare_overlay_entries (e1, e2)
4624 void *e1, *e2;
4626 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4627 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4628 int result;
4630 if (entry1->after_string_p != entry2->after_string_p)
4632 /* Let after-strings appear in front of before-strings if
4633 they come from different overlays. */
4634 if (EQ (entry1->overlay, entry2->overlay))
4635 result = entry1->after_string_p ? 1 : -1;
4636 else
4637 result = entry1->after_string_p ? -1 : 1;
4639 else if (entry1->after_string_p)
4640 /* After-strings sorted in order of decreasing priority. */
4641 result = entry2->priority - entry1->priority;
4642 else
4643 /* Before-strings sorted in order of increasing priority. */
4644 result = entry1->priority - entry2->priority;
4646 return result;
4650 /* Load the vector IT->overlay_strings with overlay strings from IT's
4651 current buffer position, or from CHARPOS if that is > 0. Set
4652 IT->n_overlays to the total number of overlay strings found.
4654 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4655 a time. On entry into load_overlay_strings,
4656 IT->current.overlay_string_index gives the number of overlay
4657 strings that have already been loaded by previous calls to this
4658 function.
4660 IT->add_overlay_start contains an additional overlay start
4661 position to consider for taking overlay strings from, if non-zero.
4662 This position comes into play when the overlay has an `invisible'
4663 property, and both before and after-strings. When we've skipped to
4664 the end of the overlay, because of its `invisible' property, we
4665 nevertheless want its before-string to appear.
4666 IT->add_overlay_start will contain the overlay start position
4667 in this case.
4669 Overlay strings are sorted so that after-string strings come in
4670 front of before-string strings. Within before and after-strings,
4671 strings are sorted by overlay priority. See also function
4672 compare_overlay_entries. */
4674 static void
4675 load_overlay_strings (it, charpos)
4676 struct it *it;
4677 int charpos;
4679 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4680 Lisp_Object overlay, window, str, invisible;
4681 struct Lisp_Overlay *ov;
4682 int start, end;
4683 int size = 20;
4684 int n = 0, i, j, invis_p;
4685 struct overlay_entry *entries
4686 = (struct overlay_entry *) alloca (size * sizeof *entries);
4688 if (charpos <= 0)
4689 charpos = IT_CHARPOS (*it);
4691 /* Append the overlay string STRING of overlay OVERLAY to vector
4692 `entries' which has size `size' and currently contains `n'
4693 elements. AFTER_P non-zero means STRING is an after-string of
4694 OVERLAY. */
4695 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4696 do \
4698 Lisp_Object priority; \
4700 if (n == size) \
4702 int new_size = 2 * size; \
4703 struct overlay_entry *old = entries; \
4704 entries = \
4705 (struct overlay_entry *) alloca (new_size \
4706 * sizeof *entries); \
4707 bcopy (old, entries, size * sizeof *entries); \
4708 size = new_size; \
4711 entries[n].string = (STRING); \
4712 entries[n].overlay = (OVERLAY); \
4713 priority = Foverlay_get ((OVERLAY), Qpriority); \
4714 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4715 entries[n].after_string_p = (AFTER_P); \
4716 ++n; \
4718 while (0)
4720 /* Process overlay before the overlay center. */
4721 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4723 XSETMISC (overlay, ov);
4724 xassert (OVERLAYP (overlay));
4725 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4726 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4728 if (end < charpos)
4729 break;
4731 /* Skip this overlay if it doesn't start or end at IT's current
4732 position. */
4733 if (end != charpos && start != charpos)
4734 continue;
4736 /* Skip this overlay if it doesn't apply to IT->w. */
4737 window = Foverlay_get (overlay, Qwindow);
4738 if (WINDOWP (window) && XWINDOW (window) != it->w)
4739 continue;
4741 /* If the text ``under'' the overlay is invisible, both before-
4742 and after-strings from this overlay are visible; start and
4743 end position are indistinguishable. */
4744 invisible = Foverlay_get (overlay, Qinvisible);
4745 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4747 /* If overlay has a non-empty before-string, record it. */
4748 if ((start == charpos || (end == charpos && invis_p))
4749 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4750 && SCHARS (str))
4751 RECORD_OVERLAY_STRING (overlay, str, 0);
4753 /* If overlay has a non-empty after-string, record it. */
4754 if ((end == charpos || (start == charpos && invis_p))
4755 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4756 && SCHARS (str))
4757 RECORD_OVERLAY_STRING (overlay, str, 1);
4760 /* Process overlays after the overlay center. */
4761 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4763 XSETMISC (overlay, ov);
4764 xassert (OVERLAYP (overlay));
4765 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4766 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4768 if (start > charpos)
4769 break;
4771 /* Skip this overlay if it doesn't start or end at IT's current
4772 position. */
4773 if (end != charpos && start != charpos)
4774 continue;
4776 /* Skip this overlay if it doesn't apply to IT->w. */
4777 window = Foverlay_get (overlay, Qwindow);
4778 if (WINDOWP (window) && XWINDOW (window) != it->w)
4779 continue;
4781 /* If the text ``under'' the overlay is invisible, it has a zero
4782 dimension, and both before- and after-strings apply. */
4783 invisible = Foverlay_get (overlay, Qinvisible);
4784 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4786 /* If overlay has a non-empty before-string, record it. */
4787 if ((start == charpos || (end == charpos && invis_p))
4788 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4789 && SCHARS (str))
4790 RECORD_OVERLAY_STRING (overlay, str, 0);
4792 /* If overlay has a non-empty after-string, record it. */
4793 if ((end == charpos || (start == charpos && invis_p))
4794 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4795 && SCHARS (str))
4796 RECORD_OVERLAY_STRING (overlay, str, 1);
4799 #undef RECORD_OVERLAY_STRING
4801 /* Sort entries. */
4802 if (n > 1)
4803 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4805 /* Record the total number of strings to process. */
4806 it->n_overlay_strings = n;
4808 /* IT->current.overlay_string_index is the number of overlay strings
4809 that have already been consumed by IT. Copy some of the
4810 remaining overlay strings to IT->overlay_strings. */
4811 i = 0;
4812 j = it->current.overlay_string_index;
4813 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4814 it->overlay_strings[i++] = entries[j++].string;
4816 CHECK_IT (it);
4820 /* Get the first chunk of overlay strings at IT's current buffer
4821 position, or at CHARPOS if that is > 0. Value is non-zero if at
4822 least one overlay string was found. */
4824 static int
4825 get_overlay_strings_1 (it, charpos, compute_stop_p)
4826 struct it *it;
4827 int charpos;
4829 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4830 process. This fills IT->overlay_strings with strings, and sets
4831 IT->n_overlay_strings to the total number of strings to process.
4832 IT->pos.overlay_string_index has to be set temporarily to zero
4833 because load_overlay_strings needs this; it must be set to -1
4834 when no overlay strings are found because a zero value would
4835 indicate a position in the first overlay string. */
4836 it->current.overlay_string_index = 0;
4837 load_overlay_strings (it, charpos);
4839 /* If we found overlay strings, set up IT to deliver display
4840 elements from the first one. Otherwise set up IT to deliver
4841 from current_buffer. */
4842 if (it->n_overlay_strings)
4844 /* Make sure we know settings in current_buffer, so that we can
4845 restore meaningful values when we're done with the overlay
4846 strings. */
4847 if (compute_stop_p)
4848 compute_stop_pos (it);
4849 xassert (it->face_id >= 0);
4851 /* Save IT's settings. They are restored after all overlay
4852 strings have been processed. */
4853 xassert (!compute_stop_p || it->sp == 0);
4854 push_it (it);
4856 /* Set up IT to deliver display elements from the first overlay
4857 string. */
4858 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4859 it->string = it->overlay_strings[0];
4860 it->stop_charpos = 0;
4861 xassert (STRINGP (it->string));
4862 it->end_charpos = SCHARS (it->string);
4863 it->multibyte_p = STRING_MULTIBYTE (it->string);
4864 it->method = GET_FROM_STRING;
4865 return 1;
4868 it->current.overlay_string_index = -1;
4869 return 0;
4872 static int
4873 get_overlay_strings (it, charpos)
4874 struct it *it;
4875 int charpos;
4877 it->string = Qnil;
4878 it->method = GET_FROM_BUFFER;
4880 (void) get_overlay_strings_1 (it, charpos, 1);
4882 CHECK_IT (it);
4884 /* Value is non-zero if we found at least one overlay string. */
4885 return STRINGP (it->string);
4890 /***********************************************************************
4891 Saving and restoring state
4892 ***********************************************************************/
4894 /* Save current settings of IT on IT->stack. Called, for example,
4895 before setting up IT for an overlay string, to be able to restore
4896 IT's settings to what they were after the overlay string has been
4897 processed. */
4899 static void
4900 push_it (it)
4901 struct it *it;
4903 struct iterator_stack_entry *p;
4905 xassert (it->sp < IT_STACK_SIZE);
4906 p = it->stack + it->sp;
4908 p->stop_charpos = it->stop_charpos;
4909 xassert (it->face_id >= 0);
4910 p->face_id = it->face_id;
4911 p->string = it->string;
4912 p->method = it->method;
4913 switch (p->method)
4915 case GET_FROM_IMAGE:
4916 p->u.image.object = it->object;
4917 p->u.image.image_id = it->image_id;
4918 p->u.image.slice = it->slice;
4919 break;
4920 case GET_FROM_COMPOSITION:
4921 p->u.comp.object = it->object;
4922 p->u.comp.c = it->c;
4923 p->u.comp.len = it->len;
4924 p->u.comp.cmp_id = it->cmp_id;
4925 p->u.comp.cmp_len = it->cmp_len;
4926 break;
4927 case GET_FROM_STRETCH:
4928 p->u.stretch.object = it->object;
4929 break;
4931 p->position = it->position;
4932 p->current = it->current;
4933 p->end_charpos = it->end_charpos;
4934 p->string_nchars = it->string_nchars;
4935 p->area = it->area;
4936 p->multibyte_p = it->multibyte_p;
4937 p->space_width = it->space_width;
4938 p->font_height = it->font_height;
4939 p->voffset = it->voffset;
4940 p->string_from_display_prop_p = it->string_from_display_prop_p;
4941 p->display_ellipsis_p = 0;
4942 ++it->sp;
4946 /* Restore IT's settings from IT->stack. Called, for example, when no
4947 more overlay strings must be processed, and we return to delivering
4948 display elements from a buffer, or when the end of a string from a
4949 `display' property is reached and we return to delivering display
4950 elements from an overlay string, or from a buffer. */
4952 static void
4953 pop_it (it)
4954 struct it *it;
4956 struct iterator_stack_entry *p;
4958 xassert (it->sp > 0);
4959 --it->sp;
4960 p = it->stack + it->sp;
4961 it->stop_charpos = p->stop_charpos;
4962 it->face_id = p->face_id;
4963 it->current = p->current;
4964 it->position = p->position;
4965 it->string = p->string;
4966 if (NILP (it->string))
4967 SET_TEXT_POS (it->current.string_pos, -1, -1);
4968 it->method = p->method;
4969 switch (it->method)
4971 case GET_FROM_IMAGE:
4972 it->image_id = p->u.image.image_id;
4973 it->object = p->u.image.object;
4974 it->slice = p->u.image.slice;
4975 break;
4976 case GET_FROM_COMPOSITION:
4977 it->object = p->u.comp.object;
4978 it->c = p->u.comp.c;
4979 it->len = p->u.comp.len;
4980 it->cmp_id = p->u.comp.cmp_id;
4981 it->cmp_len = p->u.comp.cmp_len;
4982 break;
4983 case GET_FROM_STRETCH:
4984 it->object = p->u.comp.object;
4985 break;
4987 it->end_charpos = p->end_charpos;
4988 it->string_nchars = p->string_nchars;
4989 it->area = p->area;
4990 it->multibyte_p = p->multibyte_p;
4991 it->space_width = p->space_width;
4992 it->font_height = p->font_height;
4993 it->voffset = p->voffset;
4994 it->string_from_display_prop_p = p->string_from_display_prop_p;
4999 /***********************************************************************
5000 Moving over lines
5001 ***********************************************************************/
5003 /* Set IT's current position to the previous line start. */
5005 static void
5006 back_to_previous_line_start (it)
5007 struct it *it;
5009 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5010 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5014 /* Move IT to the next line start.
5016 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5017 we skipped over part of the text (as opposed to moving the iterator
5018 continuously over the text). Otherwise, don't change the value
5019 of *SKIPPED_P.
5021 Newlines may come from buffer text, overlay strings, or strings
5022 displayed via the `display' property. That's the reason we can't
5023 simply use find_next_newline_no_quit.
5025 Note that this function may not skip over invisible text that is so
5026 because of text properties and immediately follows a newline. If
5027 it would, function reseat_at_next_visible_line_start, when called
5028 from set_iterator_to_next, would effectively make invisible
5029 characters following a newline part of the wrong glyph row, which
5030 leads to wrong cursor motion. */
5032 static int
5033 forward_to_next_line_start (it, skipped_p)
5034 struct it *it;
5035 int *skipped_p;
5037 int old_selective, newline_found_p, n;
5038 const int MAX_NEWLINE_DISTANCE = 500;
5040 /* If already on a newline, just consume it to avoid unintended
5041 skipping over invisible text below. */
5042 if (it->what == IT_CHARACTER
5043 && it->c == '\n'
5044 && CHARPOS (it->position) == IT_CHARPOS (*it))
5046 set_iterator_to_next (it, 0);
5047 it->c = 0;
5048 return 1;
5051 /* Don't handle selective display in the following. It's (a)
5052 unnecessary because it's done by the caller, and (b) leads to an
5053 infinite recursion because next_element_from_ellipsis indirectly
5054 calls this function. */
5055 old_selective = it->selective;
5056 it->selective = 0;
5058 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5059 from buffer text. */
5060 for (n = newline_found_p = 0;
5061 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5062 n += STRINGP (it->string) ? 0 : 1)
5064 if (!get_next_display_element (it))
5065 return 0;
5066 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5067 set_iterator_to_next (it, 0);
5070 /* If we didn't find a newline near enough, see if we can use a
5071 short-cut. */
5072 if (!newline_found_p)
5074 int start = IT_CHARPOS (*it);
5075 int limit = find_next_newline_no_quit (start, 1);
5076 Lisp_Object pos;
5078 xassert (!STRINGP (it->string));
5080 /* If there isn't any `display' property in sight, and no
5081 overlays, we can just use the position of the newline in
5082 buffer text. */
5083 if (it->stop_charpos >= limit
5084 || ((pos = Fnext_single_property_change (make_number (start),
5085 Qdisplay,
5086 Qnil, make_number (limit)),
5087 NILP (pos))
5088 && next_overlay_change (start) == ZV))
5090 IT_CHARPOS (*it) = limit;
5091 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5092 *skipped_p = newline_found_p = 1;
5094 else
5096 while (get_next_display_element (it)
5097 && !newline_found_p)
5099 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5100 set_iterator_to_next (it, 0);
5105 it->selective = old_selective;
5106 return newline_found_p;
5110 /* Set IT's current position to the previous visible line start. Skip
5111 invisible text that is so either due to text properties or due to
5112 selective display. Caution: this does not change IT->current_x and
5113 IT->hpos. */
5115 static void
5116 back_to_previous_visible_line_start (it)
5117 struct it *it;
5119 while (IT_CHARPOS (*it) > BEGV)
5121 back_to_previous_line_start (it);
5123 if (IT_CHARPOS (*it) <= BEGV)
5124 break;
5126 /* If selective > 0, then lines indented more than that values
5127 are invisible. */
5128 if (it->selective > 0
5129 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5130 (double) it->selective)) /* iftc */
5131 continue;
5133 /* Check the newline before point for invisibility. */
5135 Lisp_Object prop;
5136 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5137 Qinvisible, it->window);
5138 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5139 continue;
5142 if (IT_CHARPOS (*it) <= BEGV)
5143 break;
5146 struct it it2;
5147 int pos;
5148 int beg, end;
5149 Lisp_Object val, overlay;
5151 /* If newline is part of a composition, continue from start of composition */
5152 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5153 && beg < IT_CHARPOS (*it))
5154 goto replaced;
5156 /* If newline is replaced by a display property, find start of overlay
5157 or interval and continue search from that point. */
5158 it2 = *it;
5159 pos = --IT_CHARPOS (it2);
5160 --IT_BYTEPOS (it2);
5161 it2.sp = 0;
5162 if (handle_display_prop (&it2) == HANDLED_RETURN
5163 && !NILP (val = get_char_property_and_overlay
5164 (make_number (pos), Qdisplay, Qnil, &overlay))
5165 && (OVERLAYP (overlay)
5166 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5167 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5168 goto replaced;
5170 /* Newline is not replaced by anything -- so we are done. */
5171 break;
5173 replaced:
5174 if (beg < BEGV)
5175 beg = BEGV;
5176 IT_CHARPOS (*it) = beg;
5177 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5181 it->continuation_lines_width = 0;
5183 xassert (IT_CHARPOS (*it) >= BEGV);
5184 xassert (IT_CHARPOS (*it) == BEGV
5185 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5186 CHECK_IT (it);
5190 /* Reseat iterator IT at the previous visible line start. Skip
5191 invisible text that is so either due to text properties or due to
5192 selective display. At the end, update IT's overlay information,
5193 face information etc. */
5195 void
5196 reseat_at_previous_visible_line_start (it)
5197 struct it *it;
5199 back_to_previous_visible_line_start (it);
5200 reseat (it, it->current.pos, 1);
5201 CHECK_IT (it);
5205 /* Reseat iterator IT on the next visible line start in the current
5206 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5207 preceding the line start. Skip over invisible text that is so
5208 because of selective display. Compute faces, overlays etc at the
5209 new position. Note that this function does not skip over text that
5210 is invisible because of text properties. */
5212 static void
5213 reseat_at_next_visible_line_start (it, on_newline_p)
5214 struct it *it;
5215 int on_newline_p;
5217 int newline_found_p, skipped_p = 0;
5219 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5221 /* Skip over lines that are invisible because they are indented
5222 more than the value of IT->selective. */
5223 if (it->selective > 0)
5224 while (IT_CHARPOS (*it) < ZV
5225 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5226 (double) it->selective)) /* iftc */
5228 xassert (IT_BYTEPOS (*it) == BEGV
5229 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5230 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5233 /* Position on the newline if that's what's requested. */
5234 if (on_newline_p && newline_found_p)
5236 if (STRINGP (it->string))
5238 if (IT_STRING_CHARPOS (*it) > 0)
5240 --IT_STRING_CHARPOS (*it);
5241 --IT_STRING_BYTEPOS (*it);
5244 else if (IT_CHARPOS (*it) > BEGV)
5246 --IT_CHARPOS (*it);
5247 --IT_BYTEPOS (*it);
5248 reseat (it, it->current.pos, 0);
5251 else if (skipped_p)
5252 reseat (it, it->current.pos, 0);
5254 CHECK_IT (it);
5259 /***********************************************************************
5260 Changing an iterator's position
5261 ***********************************************************************/
5263 /* Change IT's current position to POS in current_buffer. If FORCE_P
5264 is non-zero, always check for text properties at the new position.
5265 Otherwise, text properties are only looked up if POS >=
5266 IT->check_charpos of a property. */
5268 static void
5269 reseat (it, pos, force_p)
5270 struct it *it;
5271 struct text_pos pos;
5272 int force_p;
5274 int original_pos = IT_CHARPOS (*it);
5276 reseat_1 (it, pos, 0);
5278 /* Determine where to check text properties. Avoid doing it
5279 where possible because text property lookup is very expensive. */
5280 if (force_p
5281 || CHARPOS (pos) > it->stop_charpos
5282 || CHARPOS (pos) < original_pos)
5283 handle_stop (it);
5285 CHECK_IT (it);
5289 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5290 IT->stop_pos to POS, also. */
5292 static void
5293 reseat_1 (it, pos, set_stop_p)
5294 struct it *it;
5295 struct text_pos pos;
5296 int set_stop_p;
5298 /* Don't call this function when scanning a C string. */
5299 xassert (it->s == NULL);
5301 /* POS must be a reasonable value. */
5302 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5304 it->current.pos = it->position = pos;
5305 XSETBUFFER (it->object, current_buffer);
5306 it->end_charpos = ZV;
5307 it->dpvec = NULL;
5308 it->current.dpvec_index = -1;
5309 it->current.overlay_string_index = -1;
5310 IT_STRING_CHARPOS (*it) = -1;
5311 IT_STRING_BYTEPOS (*it) = -1;
5312 it->string = Qnil;
5313 it->method = GET_FROM_BUFFER;
5314 it->object = it->w->buffer;
5315 it->area = TEXT_AREA;
5316 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5317 it->sp = 0;
5318 it->string_from_display_prop_p = 0;
5319 it->face_before_selective_p = 0;
5321 if (set_stop_p)
5322 it->stop_charpos = CHARPOS (pos);
5326 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5327 If S is non-null, it is a C string to iterate over. Otherwise,
5328 STRING gives a Lisp string to iterate over.
5330 If PRECISION > 0, don't return more then PRECISION number of
5331 characters from the string.
5333 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5334 characters have been returned. FIELD_WIDTH < 0 means an infinite
5335 field width.
5337 MULTIBYTE = 0 means disable processing of multibyte characters,
5338 MULTIBYTE > 0 means enable it,
5339 MULTIBYTE < 0 means use IT->multibyte_p.
5341 IT must be initialized via a prior call to init_iterator before
5342 calling this function. */
5344 static void
5345 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5346 struct it *it;
5347 unsigned char *s;
5348 Lisp_Object string;
5349 int charpos;
5350 int precision, field_width, multibyte;
5352 /* No region in strings. */
5353 it->region_beg_charpos = it->region_end_charpos = -1;
5355 /* No text property checks performed by default, but see below. */
5356 it->stop_charpos = -1;
5358 /* Set iterator position and end position. */
5359 bzero (&it->current, sizeof it->current);
5360 it->current.overlay_string_index = -1;
5361 it->current.dpvec_index = -1;
5362 xassert (charpos >= 0);
5364 /* If STRING is specified, use its multibyteness, otherwise use the
5365 setting of MULTIBYTE, if specified. */
5366 if (multibyte >= 0)
5367 it->multibyte_p = multibyte > 0;
5369 if (s == NULL)
5371 xassert (STRINGP (string));
5372 it->string = string;
5373 it->s = NULL;
5374 it->end_charpos = it->string_nchars = SCHARS (string);
5375 it->method = GET_FROM_STRING;
5376 it->current.string_pos = string_pos (charpos, string);
5378 else
5380 it->s = s;
5381 it->string = Qnil;
5383 /* Note that we use IT->current.pos, not it->current.string_pos,
5384 for displaying C strings. */
5385 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5386 if (it->multibyte_p)
5388 it->current.pos = c_string_pos (charpos, s, 1);
5389 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5391 else
5393 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5394 it->end_charpos = it->string_nchars = strlen (s);
5397 it->method = GET_FROM_C_STRING;
5400 /* PRECISION > 0 means don't return more than PRECISION characters
5401 from the string. */
5402 if (precision > 0 && it->end_charpos - charpos > precision)
5403 it->end_charpos = it->string_nchars = charpos + precision;
5405 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5406 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5407 FIELD_WIDTH < 0 means infinite field width. This is useful for
5408 padding with `-' at the end of a mode line. */
5409 if (field_width < 0)
5410 field_width = INFINITY;
5411 if (field_width > it->end_charpos - charpos)
5412 it->end_charpos = charpos + field_width;
5414 /* Use the standard display table for displaying strings. */
5415 if (DISP_TABLE_P (Vstandard_display_table))
5416 it->dp = XCHAR_TABLE (Vstandard_display_table);
5418 it->stop_charpos = charpos;
5419 CHECK_IT (it);
5424 /***********************************************************************
5425 Iteration
5426 ***********************************************************************/
5428 /* Map enum it_method value to corresponding next_element_from_* function. */
5430 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5432 next_element_from_buffer,
5433 next_element_from_display_vector,
5434 next_element_from_composition,
5435 next_element_from_string,
5436 next_element_from_c_string,
5437 next_element_from_image,
5438 next_element_from_stretch
5442 /* Load IT's display element fields with information about the next
5443 display element from the current position of IT. Value is zero if
5444 end of buffer (or C string) is reached. */
5446 static struct frame *last_escape_glyph_frame = NULL;
5447 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5448 static int last_escape_glyph_merged_face_id = 0;
5451 get_next_display_element (it)
5452 struct it *it;
5454 /* Non-zero means that we found a display element. Zero means that
5455 we hit the end of what we iterate over. Performance note: the
5456 function pointer `method' used here turns out to be faster than
5457 using a sequence of if-statements. */
5458 int success_p;
5460 get_next:
5461 success_p = (*get_next_element[it->method]) (it);
5463 if (it->what == IT_CHARACTER)
5465 /* Map via display table or translate control characters.
5466 IT->c, IT->len etc. have been set to the next character by
5467 the function call above. If we have a display table, and it
5468 contains an entry for IT->c, translate it. Don't do this if
5469 IT->c itself comes from a display table, otherwise we could
5470 end up in an infinite recursion. (An alternative could be to
5471 count the recursion depth of this function and signal an
5472 error when a certain maximum depth is reached.) Is it worth
5473 it? */
5474 if (success_p && it->dpvec == NULL)
5476 Lisp_Object dv;
5478 if (it->dp
5479 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5480 VECTORP (dv)))
5482 struct Lisp_Vector *v = XVECTOR (dv);
5484 /* Return the first character from the display table
5485 entry, if not empty. If empty, don't display the
5486 current character. */
5487 if (v->size)
5489 it->dpvec_char_len = it->len;
5490 it->dpvec = v->contents;
5491 it->dpend = v->contents + v->size;
5492 it->current.dpvec_index = 0;
5493 it->dpvec_face_id = -1;
5494 it->saved_face_id = it->face_id;
5495 it->method = GET_FROM_DISPLAY_VECTOR;
5496 it->ellipsis_p = 0;
5498 else
5500 set_iterator_to_next (it, 0);
5502 goto get_next;
5505 /* Translate control characters into `\003' or `^C' form.
5506 Control characters coming from a display table entry are
5507 currently not translated because we use IT->dpvec to hold
5508 the translation. This could easily be changed but I
5509 don't believe that it is worth doing.
5511 If it->multibyte_p is nonzero, eight-bit characters and
5512 non-printable multibyte characters are also translated to
5513 octal form.
5515 If it->multibyte_p is zero, eight-bit characters that
5516 don't have corresponding multibyte char code are also
5517 translated to octal form. */
5518 else if ((it->c < ' '
5519 && (it->area != TEXT_AREA
5520 /* In mode line, treat \n like other crl chars. */
5521 || (it->c != '\t'
5522 && it->glyph_row && it->glyph_row->mode_line_p)
5523 || (it->c != '\n' && it->c != '\t')))
5524 || (it->multibyte_p
5525 ? ((it->c >= 127
5526 && it->len == 1)
5527 || !CHAR_PRINTABLE_P (it->c)
5528 || (!NILP (Vnobreak_char_display)
5529 && (it->c == 0x8a0 || it->c == 0x8ad
5530 || it->c == 0x920 || it->c == 0x92d
5531 || it->c == 0xe20 || it->c == 0xe2d
5532 || it->c == 0xf20 || it->c == 0xf2d)))
5533 : (it->c >= 127
5534 && (!unibyte_display_via_language_environment
5535 || it->c == unibyte_char_to_multibyte (it->c)))))
5537 /* IT->c is a control character which must be displayed
5538 either as '\003' or as `^C' where the '\\' and '^'
5539 can be defined in the display table. Fill
5540 IT->ctl_chars with glyphs for what we have to
5541 display. Then, set IT->dpvec to these glyphs. */
5542 GLYPH g;
5543 int ctl_len;
5544 int face_id, lface_id = 0 ;
5545 GLYPH escape_glyph;
5547 /* Handle control characters with ^. */
5549 if (it->c < 128 && it->ctl_arrow_p)
5551 g = '^'; /* default glyph for Control */
5552 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5553 if (it->dp
5554 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5555 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5557 g = XINT (DISP_CTRL_GLYPH (it->dp));
5558 lface_id = FAST_GLYPH_FACE (g);
5560 if (lface_id)
5562 g = FAST_GLYPH_CHAR (g);
5563 face_id = merge_faces (it->f, Qt, lface_id,
5564 it->face_id);
5566 else if (it->f == last_escape_glyph_frame
5567 && it->face_id == last_escape_glyph_face_id)
5569 face_id = last_escape_glyph_merged_face_id;
5571 else
5573 /* Merge the escape-glyph face into the current face. */
5574 face_id = merge_faces (it->f, Qescape_glyph, 0,
5575 it->face_id);
5576 last_escape_glyph_frame = it->f;
5577 last_escape_glyph_face_id = it->face_id;
5578 last_escape_glyph_merged_face_id = face_id;
5581 XSETINT (it->ctl_chars[0], g);
5582 g = it->c ^ 0100;
5583 XSETINT (it->ctl_chars[1], g);
5584 ctl_len = 2;
5585 goto display_control;
5588 /* Handle non-break space in the mode where it only gets
5589 highlighting. */
5591 if (EQ (Vnobreak_char_display, Qt)
5592 && (it->c == 0x8a0 || it->c == 0x920
5593 || it->c == 0xe20 || it->c == 0xf20))
5595 /* Merge the no-break-space face into the current face. */
5596 face_id = merge_faces (it->f, Qnobreak_space, 0,
5597 it->face_id);
5599 g = it->c = ' ';
5600 XSETINT (it->ctl_chars[0], g);
5601 ctl_len = 1;
5602 goto display_control;
5605 /* Handle sequences that start with the "escape glyph". */
5607 /* the default escape glyph is \. */
5608 escape_glyph = '\\';
5610 if (it->dp
5611 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5612 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5614 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5615 lface_id = FAST_GLYPH_FACE (escape_glyph);
5617 if (lface_id)
5619 /* The display table specified a face.
5620 Merge it into face_id and also into escape_glyph. */
5621 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5622 face_id = merge_faces (it->f, Qt, lface_id,
5623 it->face_id);
5625 else if (it->f == last_escape_glyph_frame
5626 && it->face_id == last_escape_glyph_face_id)
5628 face_id = last_escape_glyph_merged_face_id;
5630 else
5632 /* Merge the escape-glyph face into the current face. */
5633 face_id = merge_faces (it->f, Qescape_glyph, 0,
5634 it->face_id);
5635 last_escape_glyph_frame = it->f;
5636 last_escape_glyph_face_id = it->face_id;
5637 last_escape_glyph_merged_face_id = face_id;
5640 /* Handle soft hyphens in the mode where they only get
5641 highlighting. */
5643 if (EQ (Vnobreak_char_display, Qt)
5644 && (it->c == 0x8ad || it->c == 0x92d
5645 || it->c == 0xe2d || it->c == 0xf2d))
5647 g = it->c = '-';
5648 XSETINT (it->ctl_chars[0], g);
5649 ctl_len = 1;
5650 goto display_control;
5653 /* Handle non-break space and soft hyphen
5654 with the escape glyph. */
5656 if (it->c == 0x8a0 || it->c == 0x8ad
5657 || it->c == 0x920 || it->c == 0x92d
5658 || it->c == 0xe20 || it->c == 0xe2d
5659 || it->c == 0xf20 || it->c == 0xf2d)
5661 XSETINT (it->ctl_chars[0], escape_glyph);
5662 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5663 XSETINT (it->ctl_chars[1], g);
5664 ctl_len = 2;
5665 goto display_control;
5669 unsigned char str[MAX_MULTIBYTE_LENGTH];
5670 int len;
5671 int i;
5673 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5674 if (SINGLE_BYTE_CHAR_P (it->c))
5675 str[0] = it->c, len = 1;
5676 else
5678 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5679 if (len < 0)
5681 /* It's an invalid character, which shouldn't
5682 happen actually, but due to bugs it may
5683 happen. Let's print the char as is, there's
5684 not much meaningful we can do with it. */
5685 str[0] = it->c;
5686 str[1] = it->c >> 8;
5687 str[2] = it->c >> 16;
5688 str[3] = it->c >> 24;
5689 len = 4;
5693 for (i = 0; i < len; i++)
5695 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5696 /* Insert three more glyphs into IT->ctl_chars for
5697 the octal display of the character. */
5698 g = ((str[i] >> 6) & 7) + '0';
5699 XSETINT (it->ctl_chars[i * 4 + 1], g);
5700 g = ((str[i] >> 3) & 7) + '0';
5701 XSETINT (it->ctl_chars[i * 4 + 2], g);
5702 g = (str[i] & 7) + '0';
5703 XSETINT (it->ctl_chars[i * 4 + 3], g);
5705 ctl_len = len * 4;
5708 display_control:
5709 /* Set up IT->dpvec and return first character from it. */
5710 it->dpvec_char_len = it->len;
5711 it->dpvec = it->ctl_chars;
5712 it->dpend = it->dpvec + ctl_len;
5713 it->current.dpvec_index = 0;
5714 it->dpvec_face_id = face_id;
5715 it->saved_face_id = it->face_id;
5716 it->method = GET_FROM_DISPLAY_VECTOR;
5717 it->ellipsis_p = 0;
5718 goto get_next;
5722 /* Adjust face id for a multibyte character. There are no
5723 multibyte character in unibyte text. */
5724 if (it->multibyte_p
5725 && success_p
5726 && FRAME_WINDOW_P (it->f))
5728 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5729 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5733 /* Is this character the last one of a run of characters with
5734 box? If yes, set IT->end_of_box_run_p to 1. */
5735 if (it->face_box_p
5736 && it->s == NULL)
5738 int face_id;
5739 struct face *face;
5741 it->end_of_box_run_p
5742 = ((face_id = face_after_it_pos (it),
5743 face_id != it->face_id)
5744 && (face = FACE_FROM_ID (it->f, face_id),
5745 face->box == FACE_NO_BOX));
5748 /* Value is 0 if end of buffer or string reached. */
5749 return success_p;
5753 /* Move IT to the next display element.
5755 RESEAT_P non-zero means if called on a newline in buffer text,
5756 skip to the next visible line start.
5758 Functions get_next_display_element and set_iterator_to_next are
5759 separate because I find this arrangement easier to handle than a
5760 get_next_display_element function that also increments IT's
5761 position. The way it is we can first look at an iterator's current
5762 display element, decide whether it fits on a line, and if it does,
5763 increment the iterator position. The other way around we probably
5764 would either need a flag indicating whether the iterator has to be
5765 incremented the next time, or we would have to implement a
5766 decrement position function which would not be easy to write. */
5768 void
5769 set_iterator_to_next (it, reseat_p)
5770 struct it *it;
5771 int reseat_p;
5773 /* Reset flags indicating start and end of a sequence of characters
5774 with box. Reset them at the start of this function because
5775 moving the iterator to a new position might set them. */
5776 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5778 switch (it->method)
5780 case GET_FROM_BUFFER:
5781 /* The current display element of IT is a character from
5782 current_buffer. Advance in the buffer, and maybe skip over
5783 invisible lines that are so because of selective display. */
5784 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5785 reseat_at_next_visible_line_start (it, 0);
5786 else
5788 xassert (it->len != 0);
5789 IT_BYTEPOS (*it) += it->len;
5790 IT_CHARPOS (*it) += 1;
5791 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5793 break;
5795 case GET_FROM_COMPOSITION:
5796 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5797 xassert (it->sp > 0);
5798 pop_it (it);
5799 if (it->method == GET_FROM_STRING)
5801 IT_STRING_BYTEPOS (*it) += it->len;
5802 IT_STRING_CHARPOS (*it) += it->cmp_len;
5803 it->object = it->string;
5804 goto consider_string_end;
5806 else if (it->method == GET_FROM_BUFFER)
5808 IT_BYTEPOS (*it) += it->len;
5809 IT_CHARPOS (*it) += it->cmp_len;
5810 it->object = it->w->buffer;
5812 break;
5814 case GET_FROM_C_STRING:
5815 /* Current display element of IT is from a C string. */
5816 IT_BYTEPOS (*it) += it->len;
5817 IT_CHARPOS (*it) += 1;
5818 break;
5820 case GET_FROM_DISPLAY_VECTOR:
5821 /* Current display element of IT is from a display table entry.
5822 Advance in the display table definition. Reset it to null if
5823 end reached, and continue with characters from buffers/
5824 strings. */
5825 ++it->current.dpvec_index;
5827 /* Restore face of the iterator to what they were before the
5828 display vector entry (these entries may contain faces). */
5829 it->face_id = it->saved_face_id;
5831 if (it->dpvec + it->current.dpvec_index == it->dpend)
5833 int recheck_faces = it->ellipsis_p;
5835 if (it->s)
5836 it->method = GET_FROM_C_STRING;
5837 else if (STRINGP (it->string))
5838 it->method = GET_FROM_STRING;
5839 else
5841 it->method = GET_FROM_BUFFER;
5842 it->object = it->w->buffer;
5845 it->dpvec = NULL;
5846 it->current.dpvec_index = -1;
5848 /* Skip over characters which were displayed via IT->dpvec. */
5849 if (it->dpvec_char_len < 0)
5850 reseat_at_next_visible_line_start (it, 1);
5851 else if (it->dpvec_char_len > 0)
5853 if (it->method == GET_FROM_STRING
5854 && it->n_overlay_strings > 0)
5855 it->ignore_overlay_strings_at_pos_p = 1;
5856 it->len = it->dpvec_char_len;
5857 set_iterator_to_next (it, reseat_p);
5860 /* Maybe recheck faces after display vector */
5861 if (recheck_faces)
5862 it->stop_charpos = IT_CHARPOS (*it);
5864 break;
5866 case GET_FROM_STRING:
5867 /* Current display element is a character from a Lisp string. */
5868 xassert (it->s == NULL && STRINGP (it->string));
5869 IT_STRING_BYTEPOS (*it) += it->len;
5870 IT_STRING_CHARPOS (*it) += 1;
5872 consider_string_end:
5874 if (it->current.overlay_string_index >= 0)
5876 /* IT->string is an overlay string. Advance to the
5877 next, if there is one. */
5878 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5879 next_overlay_string (it);
5881 else
5883 /* IT->string is not an overlay string. If we reached
5884 its end, and there is something on IT->stack, proceed
5885 with what is on the stack. This can be either another
5886 string, this time an overlay string, or a buffer. */
5887 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5888 && it->sp > 0)
5890 pop_it (it);
5891 if (it->method == GET_FROM_STRING)
5892 goto consider_string_end;
5895 break;
5897 case GET_FROM_IMAGE:
5898 case GET_FROM_STRETCH:
5899 /* The position etc with which we have to proceed are on
5900 the stack. The position may be at the end of a string,
5901 if the `display' property takes up the whole string. */
5902 xassert (it->sp > 0);
5903 pop_it (it);
5904 if (it->method == GET_FROM_STRING)
5905 goto consider_string_end;
5906 break;
5908 default:
5909 /* There are no other methods defined, so this should be a bug. */
5910 abort ();
5913 xassert (it->method != GET_FROM_STRING
5914 || (STRINGP (it->string)
5915 && IT_STRING_CHARPOS (*it) >= 0));
5918 /* Load IT's display element fields with information about the next
5919 display element which comes from a display table entry or from the
5920 result of translating a control character to one of the forms `^C'
5921 or `\003'.
5923 IT->dpvec holds the glyphs to return as characters.
5924 IT->saved_face_id holds the face id before the display vector--
5925 it is restored into IT->face_idin set_iterator_to_next. */
5927 static int
5928 next_element_from_display_vector (it)
5929 struct it *it;
5931 /* Precondition. */
5932 xassert (it->dpvec && it->current.dpvec_index >= 0);
5934 it->face_id = it->saved_face_id;
5936 if (INTEGERP (*it->dpvec)
5937 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5939 GLYPH g;
5941 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5942 it->c = FAST_GLYPH_CHAR (g);
5943 it->len = CHAR_BYTES (it->c);
5945 /* The entry may contain a face id to use. Such a face id is
5946 the id of a Lisp face, not a realized face. A face id of
5947 zero means no face is specified. */
5948 if (it->dpvec_face_id >= 0)
5949 it->face_id = it->dpvec_face_id;
5950 else
5952 int lface_id = FAST_GLYPH_FACE (g);
5953 if (lface_id > 0)
5954 it->face_id = merge_faces (it->f, Qt, lface_id,
5955 it->saved_face_id);
5958 else
5959 /* Display table entry is invalid. Return a space. */
5960 it->c = ' ', it->len = 1;
5962 /* Don't change position and object of the iterator here. They are
5963 still the values of the character that had this display table
5964 entry or was translated, and that's what we want. */
5965 it->what = IT_CHARACTER;
5966 return 1;
5970 /* Load IT with the next display element from Lisp string IT->string.
5971 IT->current.string_pos is the current position within the string.
5972 If IT->current.overlay_string_index >= 0, the Lisp string is an
5973 overlay string. */
5975 static int
5976 next_element_from_string (it)
5977 struct it *it;
5979 struct text_pos position;
5981 xassert (STRINGP (it->string));
5982 xassert (IT_STRING_CHARPOS (*it) >= 0);
5983 position = it->current.string_pos;
5985 /* Time to check for invisible text? */
5986 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5987 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5989 handle_stop (it);
5991 /* Since a handler may have changed IT->method, we must
5992 recurse here. */
5993 return get_next_display_element (it);
5996 if (it->current.overlay_string_index >= 0)
5998 /* Get the next character from an overlay string. In overlay
5999 strings, There is no field width or padding with spaces to
6000 do. */
6001 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6003 it->what = IT_EOB;
6004 return 0;
6006 else if (STRING_MULTIBYTE (it->string))
6008 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6009 const unsigned char *s = (SDATA (it->string)
6010 + IT_STRING_BYTEPOS (*it));
6011 it->c = string_char_and_length (s, remaining, &it->len);
6013 else
6015 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6016 it->len = 1;
6019 else
6021 /* Get the next character from a Lisp string that is not an
6022 overlay string. Such strings come from the mode line, for
6023 example. We may have to pad with spaces, or truncate the
6024 string. See also next_element_from_c_string. */
6025 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6027 it->what = IT_EOB;
6028 return 0;
6030 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6032 /* Pad with spaces. */
6033 it->c = ' ', it->len = 1;
6034 CHARPOS (position) = BYTEPOS (position) = -1;
6036 else if (STRING_MULTIBYTE (it->string))
6038 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6039 const unsigned char *s = (SDATA (it->string)
6040 + IT_STRING_BYTEPOS (*it));
6041 it->c = string_char_and_length (s, maxlen, &it->len);
6043 else
6045 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6046 it->len = 1;
6050 /* Record what we have and where it came from. Note that we store a
6051 buffer position in IT->position although it could arguably be a
6052 string position. */
6053 it->what = IT_CHARACTER;
6054 it->object = it->string;
6055 it->position = position;
6056 return 1;
6060 /* Load IT with next display element from C string IT->s.
6061 IT->string_nchars is the maximum number of characters to return
6062 from the string. IT->end_charpos may be greater than
6063 IT->string_nchars when this function is called, in which case we
6064 may have to return padding spaces. Value is zero if end of string
6065 reached, including padding spaces. */
6067 static int
6068 next_element_from_c_string (it)
6069 struct it *it;
6071 int success_p = 1;
6073 xassert (it->s);
6074 it->what = IT_CHARACTER;
6075 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6076 it->object = Qnil;
6078 /* IT's position can be greater IT->string_nchars in case a field
6079 width or precision has been specified when the iterator was
6080 initialized. */
6081 if (IT_CHARPOS (*it) >= it->end_charpos)
6083 /* End of the game. */
6084 it->what = IT_EOB;
6085 success_p = 0;
6087 else if (IT_CHARPOS (*it) >= it->string_nchars)
6089 /* Pad with spaces. */
6090 it->c = ' ', it->len = 1;
6091 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6093 else if (it->multibyte_p)
6095 /* Implementation note: The calls to strlen apparently aren't a
6096 performance problem because there is no noticeable performance
6097 difference between Emacs running in unibyte or multibyte mode. */
6098 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6099 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6100 maxlen, &it->len);
6102 else
6103 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6105 return success_p;
6109 /* Set up IT to return characters from an ellipsis, if appropriate.
6110 The definition of the ellipsis glyphs may come from a display table
6111 entry. This function Fills IT with the first glyph from the
6112 ellipsis if an ellipsis is to be displayed. */
6114 static int
6115 next_element_from_ellipsis (it)
6116 struct it *it;
6118 if (it->selective_display_ellipsis_p)
6119 setup_for_ellipsis (it, it->len);
6120 else
6122 /* The face at the current position may be different from the
6123 face we find after the invisible text. Remember what it
6124 was in IT->saved_face_id, and signal that it's there by
6125 setting face_before_selective_p. */
6126 it->saved_face_id = it->face_id;
6127 it->method = GET_FROM_BUFFER;
6128 it->object = it->w->buffer;
6129 reseat_at_next_visible_line_start (it, 1);
6130 it->face_before_selective_p = 1;
6133 return get_next_display_element (it);
6137 /* Deliver an image display element. The iterator IT is already
6138 filled with image information (done in handle_display_prop). Value
6139 is always 1. */
6142 static int
6143 next_element_from_image (it)
6144 struct it *it;
6146 it->what = IT_IMAGE;
6147 return 1;
6151 /* Fill iterator IT with next display element from a stretch glyph
6152 property. IT->object is the value of the text property. Value is
6153 always 1. */
6155 static int
6156 next_element_from_stretch (it)
6157 struct it *it;
6159 it->what = IT_STRETCH;
6160 return 1;
6164 /* Load IT with the next display element from current_buffer. Value
6165 is zero if end of buffer reached. IT->stop_charpos is the next
6166 position at which to stop and check for text properties or buffer
6167 end. */
6169 static int
6170 next_element_from_buffer (it)
6171 struct it *it;
6173 int success_p = 1;
6175 /* Check this assumption, otherwise, we would never enter the
6176 if-statement, below. */
6177 xassert (IT_CHARPOS (*it) >= BEGV
6178 && IT_CHARPOS (*it) <= it->stop_charpos);
6180 if (IT_CHARPOS (*it) >= it->stop_charpos)
6182 if (IT_CHARPOS (*it) >= it->end_charpos)
6184 int overlay_strings_follow_p;
6186 /* End of the game, except when overlay strings follow that
6187 haven't been returned yet. */
6188 if (it->overlay_strings_at_end_processed_p)
6189 overlay_strings_follow_p = 0;
6190 else
6192 it->overlay_strings_at_end_processed_p = 1;
6193 overlay_strings_follow_p = get_overlay_strings (it, 0);
6196 if (overlay_strings_follow_p)
6197 success_p = get_next_display_element (it);
6198 else
6200 it->what = IT_EOB;
6201 it->position = it->current.pos;
6202 success_p = 0;
6205 else
6207 handle_stop (it);
6208 return get_next_display_element (it);
6211 else
6213 /* No face changes, overlays etc. in sight, so just return a
6214 character from current_buffer. */
6215 unsigned char *p;
6217 /* Maybe run the redisplay end trigger hook. Performance note:
6218 This doesn't seem to cost measurable time. */
6219 if (it->redisplay_end_trigger_charpos
6220 && it->glyph_row
6221 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6222 run_redisplay_end_trigger_hook (it);
6224 /* Get the next character, maybe multibyte. */
6225 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6226 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6228 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6229 - IT_BYTEPOS (*it));
6230 it->c = string_char_and_length (p, maxlen, &it->len);
6232 else
6233 it->c = *p, it->len = 1;
6235 /* Record what we have and where it came from. */
6236 it->what = IT_CHARACTER;;
6237 it->object = it->w->buffer;
6238 it->position = it->current.pos;
6240 /* Normally we return the character found above, except when we
6241 really want to return an ellipsis for selective display. */
6242 if (it->selective)
6244 if (it->c == '\n')
6246 /* A value of selective > 0 means hide lines indented more
6247 than that number of columns. */
6248 if (it->selective > 0
6249 && IT_CHARPOS (*it) + 1 < ZV
6250 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6251 IT_BYTEPOS (*it) + 1,
6252 (double) it->selective)) /* iftc */
6254 success_p = next_element_from_ellipsis (it);
6255 it->dpvec_char_len = -1;
6258 else if (it->c == '\r' && it->selective == -1)
6260 /* A value of selective == -1 means that everything from the
6261 CR to the end of the line is invisible, with maybe an
6262 ellipsis displayed for it. */
6263 success_p = next_element_from_ellipsis (it);
6264 it->dpvec_char_len = -1;
6269 /* Value is zero if end of buffer reached. */
6270 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6271 return success_p;
6275 /* Run the redisplay end trigger hook for IT. */
6277 static void
6278 run_redisplay_end_trigger_hook (it)
6279 struct it *it;
6281 Lisp_Object args[3];
6283 /* IT->glyph_row should be non-null, i.e. we should be actually
6284 displaying something, or otherwise we should not run the hook. */
6285 xassert (it->glyph_row);
6287 /* Set up hook arguments. */
6288 args[0] = Qredisplay_end_trigger_functions;
6289 args[1] = it->window;
6290 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6291 it->redisplay_end_trigger_charpos = 0;
6293 /* Since we are *trying* to run these functions, don't try to run
6294 them again, even if they get an error. */
6295 it->w->redisplay_end_trigger = Qnil;
6296 Frun_hook_with_args (3, args);
6298 /* Notice if it changed the face of the character we are on. */
6299 handle_face_prop (it);
6303 /* Deliver a composition display element. The iterator IT is already
6304 filled with composition information (done in
6305 handle_composition_prop). Value is always 1. */
6307 static int
6308 next_element_from_composition (it)
6309 struct it *it;
6311 it->what = IT_COMPOSITION;
6312 it->position = (STRINGP (it->string)
6313 ? it->current.string_pos
6314 : it->current.pos);
6315 if (STRINGP (it->string))
6316 it->object = it->string;
6317 else
6318 it->object = it->w->buffer;
6319 return 1;
6324 /***********************************************************************
6325 Moving an iterator without producing glyphs
6326 ***********************************************************************/
6328 /* Check if iterator is at a position corresponding to a valid buffer
6329 position after some move_it_ call. */
6331 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6332 ((it)->method == GET_FROM_STRING \
6333 ? IT_STRING_CHARPOS (*it) == 0 \
6334 : 1)
6337 /* Move iterator IT to a specified buffer or X position within one
6338 line on the display without producing glyphs.
6340 OP should be a bit mask including some or all of these bits:
6341 MOVE_TO_X: Stop on reaching x-position TO_X.
6342 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6343 Regardless of OP's value, stop in reaching the end of the display line.
6345 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6346 This means, in particular, that TO_X includes window's horizontal
6347 scroll amount.
6349 The return value has several possible values that
6350 say what condition caused the scan to stop:
6352 MOVE_POS_MATCH_OR_ZV
6353 - when TO_POS or ZV was reached.
6355 MOVE_X_REACHED
6356 -when TO_X was reached before TO_POS or ZV were reached.
6358 MOVE_LINE_CONTINUED
6359 - when we reached the end of the display area and the line must
6360 be continued.
6362 MOVE_LINE_TRUNCATED
6363 - when we reached the end of the display area and the line is
6364 truncated.
6366 MOVE_NEWLINE_OR_CR
6367 - when we stopped at a line end, i.e. a newline or a CR and selective
6368 display is on. */
6370 static enum move_it_result
6371 move_it_in_display_line_to (it, to_charpos, to_x, op)
6372 struct it *it;
6373 int to_charpos, to_x, op;
6375 enum move_it_result result = MOVE_UNDEFINED;
6376 struct glyph_row *saved_glyph_row;
6378 /* Don't produce glyphs in produce_glyphs. */
6379 saved_glyph_row = it->glyph_row;
6380 it->glyph_row = NULL;
6382 #define BUFFER_POS_REACHED_P() \
6383 ((op & MOVE_TO_POS) != 0 \
6384 && BUFFERP (it->object) \
6385 && IT_CHARPOS (*it) >= to_charpos \
6386 && (it->method == GET_FROM_BUFFER \
6387 || (it->method == GET_FROM_DISPLAY_VECTOR \
6388 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6391 while (1)
6393 int x, i, ascent = 0, descent = 0;
6395 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6396 if ((op & MOVE_TO_POS) != 0
6397 && BUFFERP (it->object)
6398 && it->method == GET_FROM_BUFFER
6399 && IT_CHARPOS (*it) > to_charpos)
6401 result = MOVE_POS_MATCH_OR_ZV;
6402 break;
6405 /* Stop when ZV reached.
6406 We used to stop here when TO_CHARPOS reached as well, but that is
6407 too soon if this glyph does not fit on this line. So we handle it
6408 explicitly below. */
6409 if (!get_next_display_element (it)
6410 || (it->truncate_lines_p
6411 && BUFFER_POS_REACHED_P ()))
6413 result = MOVE_POS_MATCH_OR_ZV;
6414 break;
6417 /* The call to produce_glyphs will get the metrics of the
6418 display element IT is loaded with. We record in x the
6419 x-position before this display element in case it does not
6420 fit on the line. */
6421 x = it->current_x;
6423 /* Remember the line height so far in case the next element doesn't
6424 fit on the line. */
6425 if (!it->truncate_lines_p)
6427 ascent = it->max_ascent;
6428 descent = it->max_descent;
6431 PRODUCE_GLYPHS (it);
6433 if (it->area != TEXT_AREA)
6435 set_iterator_to_next (it, 1);
6436 continue;
6439 /* The number of glyphs we get back in IT->nglyphs will normally
6440 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6441 character on a terminal frame, or (iii) a line end. For the
6442 second case, IT->nglyphs - 1 padding glyphs will be present
6443 (on X frames, there is only one glyph produced for a
6444 composite character.
6446 The behavior implemented below means, for continuation lines,
6447 that as many spaces of a TAB as fit on the current line are
6448 displayed there. For terminal frames, as many glyphs of a
6449 multi-glyph character are displayed in the current line, too.
6450 This is what the old redisplay code did, and we keep it that
6451 way. Under X, the whole shape of a complex character must
6452 fit on the line or it will be completely displayed in the
6453 next line.
6455 Note that both for tabs and padding glyphs, all glyphs have
6456 the same width. */
6457 if (it->nglyphs)
6459 /* More than one glyph or glyph doesn't fit on line. All
6460 glyphs have the same width. */
6461 int single_glyph_width = it->pixel_width / it->nglyphs;
6462 int new_x;
6463 int x_before_this_char = x;
6464 int hpos_before_this_char = it->hpos;
6466 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6468 new_x = x + single_glyph_width;
6470 /* We want to leave anything reaching TO_X to the caller. */
6471 if ((op & MOVE_TO_X) && new_x > to_x)
6473 if (BUFFER_POS_REACHED_P ())
6474 goto buffer_pos_reached;
6475 it->current_x = x;
6476 result = MOVE_X_REACHED;
6477 break;
6479 else if (/* Lines are continued. */
6480 !it->truncate_lines_p
6481 && (/* And glyph doesn't fit on the line. */
6482 new_x > it->last_visible_x
6483 /* Or it fits exactly and we're on a window
6484 system frame. */
6485 || (new_x == it->last_visible_x
6486 && FRAME_WINDOW_P (it->f))))
6488 if (/* IT->hpos == 0 means the very first glyph
6489 doesn't fit on the line, e.g. a wide image. */
6490 it->hpos == 0
6491 || (new_x == it->last_visible_x
6492 && FRAME_WINDOW_P (it->f)))
6494 ++it->hpos;
6495 it->current_x = new_x;
6497 /* The character's last glyph just barely fits
6498 in this row. */
6499 if (i == it->nglyphs - 1)
6501 /* If this is the destination position,
6502 return a position *before* it in this row,
6503 now that we know it fits in this row. */
6504 if (BUFFER_POS_REACHED_P ())
6506 it->hpos = hpos_before_this_char;
6507 it->current_x = x_before_this_char;
6508 result = MOVE_POS_MATCH_OR_ZV;
6509 break;
6512 set_iterator_to_next (it, 1);
6513 #ifdef HAVE_WINDOW_SYSTEM
6514 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6516 if (!get_next_display_element (it))
6518 result = MOVE_POS_MATCH_OR_ZV;
6519 break;
6521 if (BUFFER_POS_REACHED_P ())
6523 if (ITERATOR_AT_END_OF_LINE_P (it))
6524 result = MOVE_POS_MATCH_OR_ZV;
6525 else
6526 result = MOVE_LINE_CONTINUED;
6527 break;
6529 if (ITERATOR_AT_END_OF_LINE_P (it))
6531 result = MOVE_NEWLINE_OR_CR;
6532 break;
6535 #endif /* HAVE_WINDOW_SYSTEM */
6538 else
6540 it->current_x = x;
6541 it->max_ascent = ascent;
6542 it->max_descent = descent;
6545 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6546 IT_CHARPOS (*it)));
6547 result = MOVE_LINE_CONTINUED;
6548 break;
6550 else if (BUFFER_POS_REACHED_P ())
6551 goto buffer_pos_reached;
6552 else if (new_x > it->first_visible_x)
6554 /* Glyph is visible. Increment number of glyphs that
6555 would be displayed. */
6556 ++it->hpos;
6558 else
6560 /* Glyph is completely off the left margin of the display
6561 area. Nothing to do. */
6565 if (result != MOVE_UNDEFINED)
6566 break;
6568 else if (BUFFER_POS_REACHED_P ())
6570 buffer_pos_reached:
6571 it->current_x = x;
6572 it->max_ascent = ascent;
6573 it->max_descent = descent;
6574 result = MOVE_POS_MATCH_OR_ZV;
6575 break;
6577 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6579 /* Stop when TO_X specified and reached. This check is
6580 necessary here because of lines consisting of a line end,
6581 only. The line end will not produce any glyphs and we
6582 would never get MOVE_X_REACHED. */
6583 xassert (it->nglyphs == 0);
6584 result = MOVE_X_REACHED;
6585 break;
6588 /* Is this a line end? If yes, we're done. */
6589 if (ITERATOR_AT_END_OF_LINE_P (it))
6591 result = MOVE_NEWLINE_OR_CR;
6592 break;
6595 /* The current display element has been consumed. Advance
6596 to the next. */
6597 set_iterator_to_next (it, 1);
6599 /* Stop if lines are truncated and IT's current x-position is
6600 past the right edge of the window now. */
6601 if (it->truncate_lines_p
6602 && it->current_x >= it->last_visible_x)
6604 #ifdef HAVE_WINDOW_SYSTEM
6605 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6607 if (!get_next_display_element (it)
6608 || BUFFER_POS_REACHED_P ())
6610 result = MOVE_POS_MATCH_OR_ZV;
6611 break;
6613 if (ITERATOR_AT_END_OF_LINE_P (it))
6615 result = MOVE_NEWLINE_OR_CR;
6616 break;
6619 #endif /* HAVE_WINDOW_SYSTEM */
6620 result = MOVE_LINE_TRUNCATED;
6621 break;
6625 #undef BUFFER_POS_REACHED_P
6627 /* Restore the iterator settings altered at the beginning of this
6628 function. */
6629 it->glyph_row = saved_glyph_row;
6630 return result;
6634 /* Move IT forward until it satisfies one or more of the criteria in
6635 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6637 OP is a bit-mask that specifies where to stop, and in particular,
6638 which of those four position arguments makes a difference. See the
6639 description of enum move_operation_enum.
6641 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6642 screen line, this function will set IT to the next position >
6643 TO_CHARPOS. */
6645 void
6646 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6647 struct it *it;
6648 int to_charpos, to_x, to_y, to_vpos;
6649 int op;
6651 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6652 int line_height;
6653 int reached = 0;
6655 for (;;)
6657 if (op & MOVE_TO_VPOS)
6659 /* If no TO_CHARPOS and no TO_X specified, stop at the
6660 start of the line TO_VPOS. */
6661 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6663 if (it->vpos == to_vpos)
6665 reached = 1;
6666 break;
6668 else
6669 skip = move_it_in_display_line_to (it, -1, -1, 0);
6671 else
6673 /* TO_VPOS >= 0 means stop at TO_X in the line at
6674 TO_VPOS, or at TO_POS, whichever comes first. */
6675 if (it->vpos == to_vpos)
6677 reached = 2;
6678 break;
6681 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6683 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6685 reached = 3;
6686 break;
6688 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6690 /* We have reached TO_X but not in the line we want. */
6691 skip = move_it_in_display_line_to (it, to_charpos,
6692 -1, MOVE_TO_POS);
6693 if (skip == MOVE_POS_MATCH_OR_ZV)
6695 reached = 4;
6696 break;
6701 else if (op & MOVE_TO_Y)
6703 struct it it_backup;
6705 /* TO_Y specified means stop at TO_X in the line containing
6706 TO_Y---or at TO_CHARPOS if this is reached first. The
6707 problem is that we can't really tell whether the line
6708 contains TO_Y before we have completely scanned it, and
6709 this may skip past TO_X. What we do is to first scan to
6710 TO_X.
6712 If TO_X is not specified, use a TO_X of zero. The reason
6713 is to make the outcome of this function more predictable.
6714 If we didn't use TO_X == 0, we would stop at the end of
6715 the line which is probably not what a caller would expect
6716 to happen. */
6717 skip = move_it_in_display_line_to (it, to_charpos,
6718 ((op & MOVE_TO_X)
6719 ? to_x : 0),
6720 (MOVE_TO_X
6721 | (op & MOVE_TO_POS)));
6723 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6724 if (skip == MOVE_POS_MATCH_OR_ZV)
6726 reached = 5;
6727 break;
6730 /* If TO_X was reached, we would like to know whether TO_Y
6731 is in the line. This can only be said if we know the
6732 total line height which requires us to scan the rest of
6733 the line. */
6734 if (skip == MOVE_X_REACHED)
6736 it_backup = *it;
6737 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6738 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6739 op & MOVE_TO_POS);
6740 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6743 /* Now, decide whether TO_Y is in this line. */
6744 line_height = it->max_ascent + it->max_descent;
6745 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6747 if (to_y >= it->current_y
6748 && to_y < it->current_y + line_height)
6750 if (skip == MOVE_X_REACHED)
6751 /* If TO_Y is in this line and TO_X was reached above,
6752 we scanned too far. We have to restore IT's settings
6753 to the ones before skipping. */
6754 *it = it_backup;
6755 reached = 6;
6757 else if (skip == MOVE_X_REACHED)
6759 skip = skip2;
6760 if (skip == MOVE_POS_MATCH_OR_ZV)
6761 reached = 7;
6764 if (reached)
6765 break;
6767 else
6768 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6770 switch (skip)
6772 case MOVE_POS_MATCH_OR_ZV:
6773 reached = 8;
6774 goto out;
6776 case MOVE_NEWLINE_OR_CR:
6777 set_iterator_to_next (it, 1);
6778 it->continuation_lines_width = 0;
6779 break;
6781 case MOVE_LINE_TRUNCATED:
6782 it->continuation_lines_width = 0;
6783 reseat_at_next_visible_line_start (it, 0);
6784 if ((op & MOVE_TO_POS) != 0
6785 && IT_CHARPOS (*it) > to_charpos)
6787 reached = 9;
6788 goto out;
6790 break;
6792 case MOVE_LINE_CONTINUED:
6793 it->continuation_lines_width += it->current_x;
6794 break;
6796 default:
6797 abort ();
6800 /* Reset/increment for the next run. */
6801 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6802 it->current_x = it->hpos = 0;
6803 it->current_y += it->max_ascent + it->max_descent;
6804 ++it->vpos;
6805 last_height = it->max_ascent + it->max_descent;
6806 last_max_ascent = it->max_ascent;
6807 it->max_ascent = it->max_descent = 0;
6810 out:
6812 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6816 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6818 If DY > 0, move IT backward at least that many pixels. DY = 0
6819 means move IT backward to the preceding line start or BEGV. This
6820 function may move over more than DY pixels if IT->current_y - DY
6821 ends up in the middle of a line; in this case IT->current_y will be
6822 set to the top of the line moved to. */
6824 void
6825 move_it_vertically_backward (it, dy)
6826 struct it *it;
6827 int dy;
6829 int nlines, h;
6830 struct it it2, it3;
6831 int start_pos;
6833 move_further_back:
6834 xassert (dy >= 0);
6836 start_pos = IT_CHARPOS (*it);
6838 /* Estimate how many newlines we must move back. */
6839 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6841 /* Set the iterator's position that many lines back. */
6842 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6843 back_to_previous_visible_line_start (it);
6845 /* Reseat the iterator here. When moving backward, we don't want
6846 reseat to skip forward over invisible text, set up the iterator
6847 to deliver from overlay strings at the new position etc. So,
6848 use reseat_1 here. */
6849 reseat_1 (it, it->current.pos, 1);
6851 /* We are now surely at a line start. */
6852 it->current_x = it->hpos = 0;
6853 it->continuation_lines_width = 0;
6855 /* Move forward and see what y-distance we moved. First move to the
6856 start of the next line so that we get its height. We need this
6857 height to be able to tell whether we reached the specified
6858 y-distance. */
6859 it2 = *it;
6860 it2.max_ascent = it2.max_descent = 0;
6863 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6864 MOVE_TO_POS | MOVE_TO_VPOS);
6866 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6867 xassert (IT_CHARPOS (*it) >= BEGV);
6868 it3 = it2;
6870 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6871 xassert (IT_CHARPOS (*it) >= BEGV);
6872 /* H is the actual vertical distance from the position in *IT
6873 and the starting position. */
6874 h = it2.current_y - it->current_y;
6875 /* NLINES is the distance in number of lines. */
6876 nlines = it2.vpos - it->vpos;
6878 /* Correct IT's y and vpos position
6879 so that they are relative to the starting point. */
6880 it->vpos -= nlines;
6881 it->current_y -= h;
6883 if (dy == 0)
6885 /* DY == 0 means move to the start of the screen line. The
6886 value of nlines is > 0 if continuation lines were involved. */
6887 if (nlines > 0)
6888 move_it_by_lines (it, nlines, 1);
6889 #if 0
6890 /* I think this assert is bogus if buffer contains
6891 invisible text or images. KFS. */
6892 xassert (IT_CHARPOS (*it) <= start_pos);
6893 #endif
6895 else
6897 /* The y-position we try to reach, relative to *IT.
6898 Note that H has been subtracted in front of the if-statement. */
6899 int target_y = it->current_y + h - dy;
6900 int y0 = it3.current_y;
6901 int y1 = line_bottom_y (&it3);
6902 int line_height = y1 - y0;
6904 /* If we did not reach target_y, try to move further backward if
6905 we can. If we moved too far backward, try to move forward. */
6906 if (target_y < it->current_y
6907 /* This is heuristic. In a window that's 3 lines high, with
6908 a line height of 13 pixels each, recentering with point
6909 on the bottom line will try to move -39/2 = 19 pixels
6910 backward. Try to avoid moving into the first line. */
6911 && (it->current_y - target_y
6912 > min (window_box_height (it->w), line_height * 2 / 3))
6913 && IT_CHARPOS (*it) > BEGV)
6915 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6916 target_y - it->current_y));
6917 dy = it->current_y - target_y;
6918 goto move_further_back;
6920 else if (target_y >= it->current_y + line_height
6921 && IT_CHARPOS (*it) < ZV)
6923 /* Should move forward by at least one line, maybe more.
6925 Note: Calling move_it_by_lines can be expensive on
6926 terminal frames, where compute_motion is used (via
6927 vmotion) to do the job, when there are very long lines
6928 and truncate-lines is nil. That's the reason for
6929 treating terminal frames specially here. */
6931 if (!FRAME_WINDOW_P (it->f))
6932 move_it_vertically (it, target_y - (it->current_y + line_height));
6933 else
6937 move_it_by_lines (it, 1, 1);
6939 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6942 #if 0
6943 /* I think this assert is bogus if buffer contains
6944 invisible text or images. KFS. */
6945 xassert (IT_CHARPOS (*it) >= BEGV);
6946 #endif
6952 /* Move IT by a specified amount of pixel lines DY. DY negative means
6953 move backwards. DY = 0 means move to start of screen line. At the
6954 end, IT will be on the start of a screen line. */
6956 void
6957 move_it_vertically (it, dy)
6958 struct it *it;
6959 int dy;
6961 if (dy <= 0)
6962 move_it_vertically_backward (it, -dy);
6963 else
6965 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6966 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6967 MOVE_TO_POS | MOVE_TO_Y);
6968 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6970 /* If buffer ends in ZV without a newline, move to the start of
6971 the line to satisfy the post-condition. */
6972 if (IT_CHARPOS (*it) == ZV
6973 && ZV > BEGV
6974 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6975 move_it_by_lines (it, 0, 0);
6980 /* Move iterator IT past the end of the text line it is in. */
6982 void
6983 move_it_past_eol (it)
6984 struct it *it;
6986 enum move_it_result rc;
6988 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6989 if (rc == MOVE_NEWLINE_OR_CR)
6990 set_iterator_to_next (it, 0);
6994 #if 0 /* Currently not used. */
6996 /* Return non-zero if some text between buffer positions START_CHARPOS
6997 and END_CHARPOS is invisible. IT->window is the window for text
6998 property lookup. */
7000 static int
7001 invisible_text_between_p (it, start_charpos, end_charpos)
7002 struct it *it;
7003 int start_charpos, end_charpos;
7005 Lisp_Object prop, limit;
7006 int invisible_found_p;
7008 xassert (it != NULL && start_charpos <= end_charpos);
7010 /* Is text at START invisible? */
7011 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7012 it->window);
7013 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7014 invisible_found_p = 1;
7015 else
7017 limit = Fnext_single_char_property_change (make_number (start_charpos),
7018 Qinvisible, Qnil,
7019 make_number (end_charpos));
7020 invisible_found_p = XFASTINT (limit) < end_charpos;
7023 return invisible_found_p;
7026 #endif /* 0 */
7029 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7030 negative means move up. DVPOS == 0 means move to the start of the
7031 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7032 NEED_Y_P is zero, IT->current_y will be left unchanged.
7034 Further optimization ideas: If we would know that IT->f doesn't use
7035 a face with proportional font, we could be faster for
7036 truncate-lines nil. */
7038 void
7039 move_it_by_lines (it, dvpos, need_y_p)
7040 struct it *it;
7041 int dvpos, need_y_p;
7043 struct position pos;
7045 if (!FRAME_WINDOW_P (it->f))
7047 struct text_pos textpos;
7049 /* We can use vmotion on frames without proportional fonts. */
7050 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7051 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7052 reseat (it, textpos, 1);
7053 it->vpos += pos.vpos;
7054 it->current_y += pos.vpos;
7056 else if (dvpos == 0)
7058 /* DVPOS == 0 means move to the start of the screen line. */
7059 move_it_vertically_backward (it, 0);
7060 xassert (it->current_x == 0 && it->hpos == 0);
7061 /* Let next call to line_bottom_y calculate real line height */
7062 last_height = 0;
7064 else if (dvpos > 0)
7066 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7067 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7068 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7070 else
7072 struct it it2;
7073 int start_charpos, i;
7075 /* Start at the beginning of the screen line containing IT's
7076 position. This may actually move vertically backwards,
7077 in case of overlays, so adjust dvpos accordingly. */
7078 dvpos += it->vpos;
7079 move_it_vertically_backward (it, 0);
7080 dvpos -= it->vpos;
7082 /* Go back -DVPOS visible lines and reseat the iterator there. */
7083 start_charpos = IT_CHARPOS (*it);
7084 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7085 back_to_previous_visible_line_start (it);
7086 reseat (it, it->current.pos, 1);
7088 /* Move further back if we end up in a string or an image. */
7089 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7091 /* First try to move to start of display line. */
7092 dvpos += it->vpos;
7093 move_it_vertically_backward (it, 0);
7094 dvpos -= it->vpos;
7095 if (IT_POS_VALID_AFTER_MOVE_P (it))
7096 break;
7097 /* If start of line is still in string or image,
7098 move further back. */
7099 back_to_previous_visible_line_start (it);
7100 reseat (it, it->current.pos, 1);
7101 dvpos--;
7104 it->current_x = it->hpos = 0;
7106 /* Above call may have moved too far if continuation lines
7107 are involved. Scan forward and see if it did. */
7108 it2 = *it;
7109 it2.vpos = it2.current_y = 0;
7110 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7111 it->vpos -= it2.vpos;
7112 it->current_y -= it2.current_y;
7113 it->current_x = it->hpos = 0;
7115 /* If we moved too far back, move IT some lines forward. */
7116 if (it2.vpos > -dvpos)
7118 int delta = it2.vpos + dvpos;
7119 it2 = *it;
7120 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7121 /* Move back again if we got too far ahead. */
7122 if (IT_CHARPOS (*it) >= start_charpos)
7123 *it = it2;
7128 /* Return 1 if IT points into the middle of a display vector. */
7131 in_display_vector_p (it)
7132 struct it *it;
7134 return (it->method == GET_FROM_DISPLAY_VECTOR
7135 && it->current.dpvec_index > 0
7136 && it->dpvec + it->current.dpvec_index != it->dpend);
7140 /***********************************************************************
7141 Messages
7142 ***********************************************************************/
7145 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7146 to *Messages*. */
7148 void
7149 add_to_log (format, arg1, arg2)
7150 char *format;
7151 Lisp_Object arg1, arg2;
7153 Lisp_Object args[3];
7154 Lisp_Object msg, fmt;
7155 char *buffer;
7156 int len;
7157 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7158 USE_SAFE_ALLOCA;
7160 /* Do nothing if called asynchronously. Inserting text into
7161 a buffer may call after-change-functions and alike and
7162 that would means running Lisp asynchronously. */
7163 if (handling_signal)
7164 return;
7166 fmt = msg = Qnil;
7167 GCPRO4 (fmt, msg, arg1, arg2);
7169 args[0] = fmt = build_string (format);
7170 args[1] = arg1;
7171 args[2] = arg2;
7172 msg = Fformat (3, args);
7174 len = SBYTES (msg) + 1;
7175 SAFE_ALLOCA (buffer, char *, len);
7176 bcopy (SDATA (msg), buffer, len);
7178 message_dolog (buffer, len - 1, 1, 0);
7179 SAFE_FREE ();
7181 UNGCPRO;
7185 /* Output a newline in the *Messages* buffer if "needs" one. */
7187 void
7188 message_log_maybe_newline ()
7190 if (message_log_need_newline)
7191 message_dolog ("", 0, 1, 0);
7195 /* Add a string M of length NBYTES to the message log, optionally
7196 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7197 nonzero, means interpret the contents of M as multibyte. This
7198 function calls low-level routines in order to bypass text property
7199 hooks, etc. which might not be safe to run.
7201 This may GC (insert may run before/after change hooks),
7202 so the buffer M must NOT point to a Lisp string. */
7204 void
7205 message_dolog (m, nbytes, nlflag, multibyte)
7206 const char *m;
7207 int nbytes, nlflag, multibyte;
7209 if (!NILP (Vmemory_full))
7210 return;
7212 if (!NILP (Vmessage_log_max))
7214 struct buffer *oldbuf;
7215 Lisp_Object oldpoint, oldbegv, oldzv;
7216 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7217 int point_at_end = 0;
7218 int zv_at_end = 0;
7219 Lisp_Object old_deactivate_mark, tem;
7220 struct gcpro gcpro1;
7222 old_deactivate_mark = Vdeactivate_mark;
7223 oldbuf = current_buffer;
7224 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7225 current_buffer->undo_list = Qt;
7227 oldpoint = message_dolog_marker1;
7228 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7229 oldbegv = message_dolog_marker2;
7230 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7231 oldzv = message_dolog_marker3;
7232 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7233 GCPRO1 (old_deactivate_mark);
7235 if (PT == Z)
7236 point_at_end = 1;
7237 if (ZV == Z)
7238 zv_at_end = 1;
7240 BEGV = BEG;
7241 BEGV_BYTE = BEG_BYTE;
7242 ZV = Z;
7243 ZV_BYTE = Z_BYTE;
7244 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7246 /* Insert the string--maybe converting multibyte to single byte
7247 or vice versa, so that all the text fits the buffer. */
7248 if (multibyte
7249 && NILP (current_buffer->enable_multibyte_characters))
7251 int i, c, char_bytes;
7252 unsigned char work[1];
7254 /* Convert a multibyte string to single-byte
7255 for the *Message* buffer. */
7256 for (i = 0; i < nbytes; i += char_bytes)
7258 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7259 work[0] = (SINGLE_BYTE_CHAR_P (c)
7261 : multibyte_char_to_unibyte (c, Qnil));
7262 insert_1_both (work, 1, 1, 1, 0, 0);
7265 else if (! multibyte
7266 && ! NILP (current_buffer->enable_multibyte_characters))
7268 int i, c, char_bytes;
7269 unsigned char *msg = (unsigned char *) m;
7270 unsigned char str[MAX_MULTIBYTE_LENGTH];
7271 /* Convert a single-byte string to multibyte
7272 for the *Message* buffer. */
7273 for (i = 0; i < nbytes; i++)
7275 c = unibyte_char_to_multibyte (msg[i]);
7276 char_bytes = CHAR_STRING (c, str);
7277 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7280 else if (nbytes)
7281 insert_1 (m, nbytes, 1, 0, 0);
7283 if (nlflag)
7285 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7286 insert_1 ("\n", 1, 1, 0, 0);
7288 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7289 this_bol = PT;
7290 this_bol_byte = PT_BYTE;
7292 /* See if this line duplicates the previous one.
7293 If so, combine duplicates. */
7294 if (this_bol > BEG)
7296 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7297 prev_bol = PT;
7298 prev_bol_byte = PT_BYTE;
7300 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7301 this_bol, this_bol_byte);
7302 if (dup)
7304 del_range_both (prev_bol, prev_bol_byte,
7305 this_bol, this_bol_byte, 0);
7306 if (dup > 1)
7308 char dupstr[40];
7309 int duplen;
7311 /* If you change this format, don't forget to also
7312 change message_log_check_duplicate. */
7313 sprintf (dupstr, " [%d times]", dup);
7314 duplen = strlen (dupstr);
7315 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7316 insert_1 (dupstr, duplen, 1, 0, 1);
7321 /* If we have more than the desired maximum number of lines
7322 in the *Messages* buffer now, delete the oldest ones.
7323 This is safe because we don't have undo in this buffer. */
7325 if (NATNUMP (Vmessage_log_max))
7327 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7328 -XFASTINT (Vmessage_log_max) - 1, 0);
7329 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7332 BEGV = XMARKER (oldbegv)->charpos;
7333 BEGV_BYTE = marker_byte_position (oldbegv);
7335 if (zv_at_end)
7337 ZV = Z;
7338 ZV_BYTE = Z_BYTE;
7340 else
7342 ZV = XMARKER (oldzv)->charpos;
7343 ZV_BYTE = marker_byte_position (oldzv);
7346 if (point_at_end)
7347 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7348 else
7349 /* We can't do Fgoto_char (oldpoint) because it will run some
7350 Lisp code. */
7351 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7352 XMARKER (oldpoint)->bytepos);
7354 UNGCPRO;
7355 unchain_marker (XMARKER (oldpoint));
7356 unchain_marker (XMARKER (oldbegv));
7357 unchain_marker (XMARKER (oldzv));
7359 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7360 set_buffer_internal (oldbuf);
7361 if (NILP (tem))
7362 windows_or_buffers_changed = old_windows_or_buffers_changed;
7363 message_log_need_newline = !nlflag;
7364 Vdeactivate_mark = old_deactivate_mark;
7369 /* We are at the end of the buffer after just having inserted a newline.
7370 (Note: We depend on the fact we won't be crossing the gap.)
7371 Check to see if the most recent message looks a lot like the previous one.
7372 Return 0 if different, 1 if the new one should just replace it, or a
7373 value N > 1 if we should also append " [N times]". */
7375 static int
7376 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7377 int prev_bol, this_bol;
7378 int prev_bol_byte, this_bol_byte;
7380 int i;
7381 int len = Z_BYTE - 1 - this_bol_byte;
7382 int seen_dots = 0;
7383 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7384 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7386 for (i = 0; i < len; i++)
7388 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7389 seen_dots = 1;
7390 if (p1[i] != p2[i])
7391 return seen_dots;
7393 p1 += len;
7394 if (*p1 == '\n')
7395 return 2;
7396 if (*p1++ == ' ' && *p1++ == '[')
7398 int n = 0;
7399 while (*p1 >= '0' && *p1 <= '9')
7400 n = n * 10 + *p1++ - '0';
7401 if (strncmp (p1, " times]\n", 8) == 0)
7402 return n+1;
7404 return 0;
7408 /* Display an echo area message M with a specified length of NBYTES
7409 bytes. The string may include null characters. If M is 0, clear
7410 out any existing message, and let the mini-buffer text show
7411 through.
7413 This may GC, so the buffer M must NOT point to a Lisp string. */
7415 void
7416 message2 (m, nbytes, multibyte)
7417 const char *m;
7418 int nbytes;
7419 int multibyte;
7421 /* First flush out any partial line written with print. */
7422 message_log_maybe_newline ();
7423 if (m)
7424 message_dolog (m, nbytes, 1, multibyte);
7425 message2_nolog (m, nbytes, multibyte);
7429 /* The non-logging counterpart of message2. */
7431 void
7432 message2_nolog (m, nbytes, multibyte)
7433 const char *m;
7434 int nbytes, multibyte;
7436 struct frame *sf = SELECTED_FRAME ();
7437 message_enable_multibyte = multibyte;
7439 if (noninteractive)
7441 if (noninteractive_need_newline)
7442 putc ('\n', stderr);
7443 noninteractive_need_newline = 0;
7444 if (m)
7445 fwrite (m, nbytes, 1, stderr);
7446 if (cursor_in_echo_area == 0)
7447 fprintf (stderr, "\n");
7448 fflush (stderr);
7450 /* A null message buffer means that the frame hasn't really been
7451 initialized yet. Error messages get reported properly by
7452 cmd_error, so this must be just an informative message; toss it. */
7453 else if (INTERACTIVE
7454 && sf->glyphs_initialized_p
7455 && FRAME_MESSAGE_BUF (sf))
7457 Lisp_Object mini_window;
7458 struct frame *f;
7460 /* Get the frame containing the mini-buffer
7461 that the selected frame is using. */
7462 mini_window = FRAME_MINIBUF_WINDOW (sf);
7463 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7465 FRAME_SAMPLE_VISIBILITY (f);
7466 if (FRAME_VISIBLE_P (sf)
7467 && ! FRAME_VISIBLE_P (f))
7468 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7470 if (m)
7472 set_message (m, Qnil, nbytes, multibyte);
7473 if (minibuffer_auto_raise)
7474 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7476 else
7477 clear_message (1, 1);
7479 do_pending_window_change (0);
7480 echo_area_display (1);
7481 do_pending_window_change (0);
7482 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7483 (*frame_up_to_date_hook) (f);
7488 /* Display an echo area message M with a specified length of NBYTES
7489 bytes. The string may include null characters. If M is not a
7490 string, clear out any existing message, and let the mini-buffer
7491 text show through.
7493 This function cancels echoing. */
7495 void
7496 message3 (m, nbytes, multibyte)
7497 Lisp_Object m;
7498 int nbytes;
7499 int multibyte;
7501 struct gcpro gcpro1;
7503 GCPRO1 (m);
7504 clear_message (1,1);
7505 cancel_echoing ();
7507 /* First flush out any partial line written with print. */
7508 message_log_maybe_newline ();
7509 if (STRINGP (m))
7511 char *buffer;
7512 USE_SAFE_ALLOCA;
7514 SAFE_ALLOCA (buffer, char *, nbytes);
7515 bcopy (SDATA (m), buffer, nbytes);
7516 message_dolog (buffer, nbytes, 1, multibyte);
7517 SAFE_FREE ();
7519 message3_nolog (m, nbytes, multibyte);
7521 UNGCPRO;
7525 /* The non-logging version of message3.
7526 This does not cancel echoing, because it is used for echoing.
7527 Perhaps we need to make a separate function for echoing
7528 and make this cancel echoing. */
7530 void
7531 message3_nolog (m, nbytes, multibyte)
7532 Lisp_Object m;
7533 int nbytes, multibyte;
7535 struct frame *sf = SELECTED_FRAME ();
7536 message_enable_multibyte = multibyte;
7538 if (noninteractive)
7540 if (noninteractive_need_newline)
7541 putc ('\n', stderr);
7542 noninteractive_need_newline = 0;
7543 if (STRINGP (m))
7544 fwrite (SDATA (m), nbytes, 1, stderr);
7545 if (cursor_in_echo_area == 0)
7546 fprintf (stderr, "\n");
7547 fflush (stderr);
7549 /* A null message buffer means that the frame hasn't really been
7550 initialized yet. Error messages get reported properly by
7551 cmd_error, so this must be just an informative message; toss it. */
7552 else if (INTERACTIVE
7553 && sf->glyphs_initialized_p
7554 && FRAME_MESSAGE_BUF (sf))
7556 Lisp_Object mini_window;
7557 Lisp_Object frame;
7558 struct frame *f;
7560 /* Get the frame containing the mini-buffer
7561 that the selected frame is using. */
7562 mini_window = FRAME_MINIBUF_WINDOW (sf);
7563 frame = XWINDOW (mini_window)->frame;
7564 f = XFRAME (frame);
7566 FRAME_SAMPLE_VISIBILITY (f);
7567 if (FRAME_VISIBLE_P (sf)
7568 && !FRAME_VISIBLE_P (f))
7569 Fmake_frame_visible (frame);
7571 if (STRINGP (m) && SCHARS (m) > 0)
7573 set_message (NULL, m, nbytes, multibyte);
7574 if (minibuffer_auto_raise)
7575 Fraise_frame (frame);
7576 /* Assume we are not echoing.
7577 (If we are, echo_now will override this.) */
7578 echo_message_buffer = Qnil;
7580 else
7581 clear_message (1, 1);
7583 do_pending_window_change (0);
7584 echo_area_display (1);
7585 do_pending_window_change (0);
7586 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7587 (*frame_up_to_date_hook) (f);
7592 /* Display a null-terminated echo area message M. If M is 0, clear
7593 out any existing message, and let the mini-buffer text show through.
7595 The buffer M must continue to exist until after the echo area gets
7596 cleared or some other message gets displayed there. Do not pass
7597 text that is stored in a Lisp string. Do not pass text in a buffer
7598 that was alloca'd. */
7600 void
7601 message1 (m)
7602 char *m;
7604 message2 (m, (m ? strlen (m) : 0), 0);
7608 /* The non-logging counterpart of message1. */
7610 void
7611 message1_nolog (m)
7612 char *m;
7614 message2_nolog (m, (m ? strlen (m) : 0), 0);
7617 /* Display a message M which contains a single %s
7618 which gets replaced with STRING. */
7620 void
7621 message_with_string (m, string, log)
7622 char *m;
7623 Lisp_Object string;
7624 int log;
7626 CHECK_STRING (string);
7628 if (noninteractive)
7630 if (m)
7632 if (noninteractive_need_newline)
7633 putc ('\n', stderr);
7634 noninteractive_need_newline = 0;
7635 fprintf (stderr, m, SDATA (string));
7636 if (cursor_in_echo_area == 0)
7637 fprintf (stderr, "\n");
7638 fflush (stderr);
7641 else if (INTERACTIVE)
7643 /* The frame whose minibuffer we're going to display the message on.
7644 It may be larger than the selected frame, so we need
7645 to use its buffer, not the selected frame's buffer. */
7646 Lisp_Object mini_window;
7647 struct frame *f, *sf = SELECTED_FRAME ();
7649 /* Get the frame containing the minibuffer
7650 that the selected frame is using. */
7651 mini_window = FRAME_MINIBUF_WINDOW (sf);
7652 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7654 /* A null message buffer means that the frame hasn't really been
7655 initialized yet. Error messages get reported properly by
7656 cmd_error, so this must be just an informative message; toss it. */
7657 if (FRAME_MESSAGE_BUF (f))
7659 Lisp_Object args[2], message;
7660 struct gcpro gcpro1, gcpro2;
7662 args[0] = build_string (m);
7663 args[1] = message = string;
7664 GCPRO2 (args[0], message);
7665 gcpro1.nvars = 2;
7667 message = Fformat (2, args);
7669 if (log)
7670 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7671 else
7672 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7674 UNGCPRO;
7676 /* Print should start at the beginning of the message
7677 buffer next time. */
7678 message_buf_print = 0;
7684 /* Dump an informative message to the minibuf. If M is 0, clear out
7685 any existing message, and let the mini-buffer text show through. */
7687 /* VARARGS 1 */
7688 void
7689 message (m, a1, a2, a3)
7690 char *m;
7691 EMACS_INT a1, a2, a3;
7693 if (noninteractive)
7695 if (m)
7697 if (noninteractive_need_newline)
7698 putc ('\n', stderr);
7699 noninteractive_need_newline = 0;
7700 fprintf (stderr, m, a1, a2, a3);
7701 if (cursor_in_echo_area == 0)
7702 fprintf (stderr, "\n");
7703 fflush (stderr);
7706 else if (INTERACTIVE)
7708 /* The frame whose mini-buffer we're going to display the message
7709 on. It may be larger than the selected frame, so we need to
7710 use its buffer, not the selected frame's buffer. */
7711 Lisp_Object mini_window;
7712 struct frame *f, *sf = SELECTED_FRAME ();
7714 /* Get the frame containing the mini-buffer
7715 that the selected frame is using. */
7716 mini_window = FRAME_MINIBUF_WINDOW (sf);
7717 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7719 /* A null message buffer means that the frame hasn't really been
7720 initialized yet. Error messages get reported properly by
7721 cmd_error, so this must be just an informative message; toss
7722 it. */
7723 if (FRAME_MESSAGE_BUF (f))
7725 if (m)
7727 int len;
7728 #ifdef NO_ARG_ARRAY
7729 char *a[3];
7730 a[0] = (char *) a1;
7731 a[1] = (char *) a2;
7732 a[2] = (char *) a3;
7734 len = doprnt (FRAME_MESSAGE_BUF (f),
7735 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7736 #else
7737 len = doprnt (FRAME_MESSAGE_BUF (f),
7738 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7739 (char **) &a1);
7740 #endif /* NO_ARG_ARRAY */
7742 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7744 else
7745 message1 (0);
7747 /* Print should start at the beginning of the message
7748 buffer next time. */
7749 message_buf_print = 0;
7755 /* The non-logging version of message. */
7757 void
7758 message_nolog (m, a1, a2, a3)
7759 char *m;
7760 EMACS_INT a1, a2, a3;
7762 Lisp_Object old_log_max;
7763 old_log_max = Vmessage_log_max;
7764 Vmessage_log_max = Qnil;
7765 message (m, a1, a2, a3);
7766 Vmessage_log_max = old_log_max;
7770 /* Display the current message in the current mini-buffer. This is
7771 only called from error handlers in process.c, and is not time
7772 critical. */
7774 void
7775 update_echo_area ()
7777 if (!NILP (echo_area_buffer[0]))
7779 Lisp_Object string;
7780 string = Fcurrent_message ();
7781 message3 (string, SBYTES (string),
7782 !NILP (current_buffer->enable_multibyte_characters));
7787 /* Make sure echo area buffers in `echo_buffers' are live.
7788 If they aren't, make new ones. */
7790 static void
7791 ensure_echo_area_buffers ()
7793 int i;
7795 for (i = 0; i < 2; ++i)
7796 if (!BUFFERP (echo_buffer[i])
7797 || NILP (XBUFFER (echo_buffer[i])->name))
7799 char name[30];
7800 Lisp_Object old_buffer;
7801 int j;
7803 old_buffer = echo_buffer[i];
7804 sprintf (name, " *Echo Area %d*", i);
7805 echo_buffer[i] = Fget_buffer_create (build_string (name));
7806 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7808 for (j = 0; j < 2; ++j)
7809 if (EQ (old_buffer, echo_area_buffer[j]))
7810 echo_area_buffer[j] = echo_buffer[i];
7815 /* Call FN with args A1..A4 with either the current or last displayed
7816 echo_area_buffer as current buffer.
7818 WHICH zero means use the current message buffer
7819 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7820 from echo_buffer[] and clear it.
7822 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7823 suitable buffer from echo_buffer[] and clear it.
7825 Value is what FN returns. */
7827 static int
7828 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7829 struct window *w;
7830 int which;
7831 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7832 EMACS_INT a1;
7833 Lisp_Object a2;
7834 EMACS_INT a3, a4;
7836 Lisp_Object buffer;
7837 int this_one, the_other, clear_buffer_p, rc;
7838 int count = SPECPDL_INDEX ();
7840 /* If buffers aren't live, make new ones. */
7841 ensure_echo_area_buffers ();
7843 clear_buffer_p = 0;
7845 if (which == 0)
7846 this_one = 0, the_other = 1;
7847 else if (which > 0)
7848 this_one = 1, the_other = 0;
7850 /* Choose a suitable buffer from echo_buffer[] is we don't
7851 have one. */
7852 if (NILP (echo_area_buffer[this_one]))
7854 echo_area_buffer[this_one]
7855 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7856 ? echo_buffer[the_other]
7857 : echo_buffer[this_one]);
7858 clear_buffer_p = 1;
7861 buffer = echo_area_buffer[this_one];
7863 /* Don't get confused by reusing the buffer used for echoing
7864 for a different purpose. */
7865 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7866 cancel_echoing ();
7868 record_unwind_protect (unwind_with_echo_area_buffer,
7869 with_echo_area_buffer_unwind_data (w));
7871 /* Make the echo area buffer current. Note that for display
7872 purposes, it is not necessary that the displayed window's buffer
7873 == current_buffer, except for text property lookup. So, let's
7874 only set that buffer temporarily here without doing a full
7875 Fset_window_buffer. We must also change w->pointm, though,
7876 because otherwise an assertions in unshow_buffer fails, and Emacs
7877 aborts. */
7878 set_buffer_internal_1 (XBUFFER (buffer));
7879 if (w)
7881 w->buffer = buffer;
7882 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7885 current_buffer->undo_list = Qt;
7886 current_buffer->read_only = Qnil;
7887 specbind (Qinhibit_read_only, Qt);
7888 specbind (Qinhibit_modification_hooks, Qt);
7890 if (clear_buffer_p && Z > BEG)
7891 del_range (BEG, Z);
7893 xassert (BEGV >= BEG);
7894 xassert (ZV <= Z && ZV >= BEGV);
7896 rc = fn (a1, a2, a3, a4);
7898 xassert (BEGV >= BEG);
7899 xassert (ZV <= Z && ZV >= BEGV);
7901 unbind_to (count, Qnil);
7902 return rc;
7906 /* Save state that should be preserved around the call to the function
7907 FN called in with_echo_area_buffer. */
7909 static Lisp_Object
7910 with_echo_area_buffer_unwind_data (w)
7911 struct window *w;
7913 int i = 0;
7914 Lisp_Object vector;
7916 /* Reduce consing by keeping one vector in
7917 Vwith_echo_area_save_vector. */
7918 vector = Vwith_echo_area_save_vector;
7919 Vwith_echo_area_save_vector = Qnil;
7921 if (NILP (vector))
7922 vector = Fmake_vector (make_number (7), Qnil);
7924 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7925 AREF (vector, i) = Vdeactivate_mark, ++i;
7926 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7928 if (w)
7930 XSETWINDOW (AREF (vector, i), w); ++i;
7931 AREF (vector, i) = w->buffer; ++i;
7932 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7933 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7935 else
7937 int end = i + 4;
7938 for (; i < end; ++i)
7939 AREF (vector, i) = Qnil;
7942 xassert (i == ASIZE (vector));
7943 return vector;
7947 /* Restore global state from VECTOR which was created by
7948 with_echo_area_buffer_unwind_data. */
7950 static Lisp_Object
7951 unwind_with_echo_area_buffer (vector)
7952 Lisp_Object vector;
7954 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7955 Vdeactivate_mark = AREF (vector, 1);
7956 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7958 if (WINDOWP (AREF (vector, 3)))
7960 struct window *w;
7961 Lisp_Object buffer, charpos, bytepos;
7963 w = XWINDOW (AREF (vector, 3));
7964 buffer = AREF (vector, 4);
7965 charpos = AREF (vector, 5);
7966 bytepos = AREF (vector, 6);
7968 w->buffer = buffer;
7969 set_marker_both (w->pointm, buffer,
7970 XFASTINT (charpos), XFASTINT (bytepos));
7973 Vwith_echo_area_save_vector = vector;
7974 return Qnil;
7978 /* Set up the echo area for use by print functions. MULTIBYTE_P
7979 non-zero means we will print multibyte. */
7981 void
7982 setup_echo_area_for_printing (multibyte_p)
7983 int multibyte_p;
7985 /* If we can't find an echo area any more, exit. */
7986 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7987 Fkill_emacs (Qnil);
7989 ensure_echo_area_buffers ();
7991 if (!message_buf_print)
7993 /* A message has been output since the last time we printed.
7994 Choose a fresh echo area buffer. */
7995 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7996 echo_area_buffer[0] = echo_buffer[1];
7997 else
7998 echo_area_buffer[0] = echo_buffer[0];
8000 /* Switch to that buffer and clear it. */
8001 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8002 current_buffer->truncate_lines = Qnil;
8004 if (Z > BEG)
8006 int count = SPECPDL_INDEX ();
8007 specbind (Qinhibit_read_only, Qt);
8008 /* Note that undo recording is always disabled. */
8009 del_range (BEG, Z);
8010 unbind_to (count, Qnil);
8012 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8014 /* Set up the buffer for the multibyteness we need. */
8015 if (multibyte_p
8016 != !NILP (current_buffer->enable_multibyte_characters))
8017 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8019 /* Raise the frame containing the echo area. */
8020 if (minibuffer_auto_raise)
8022 struct frame *sf = SELECTED_FRAME ();
8023 Lisp_Object mini_window;
8024 mini_window = FRAME_MINIBUF_WINDOW (sf);
8025 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8028 message_log_maybe_newline ();
8029 message_buf_print = 1;
8031 else
8033 if (NILP (echo_area_buffer[0]))
8035 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8036 echo_area_buffer[0] = echo_buffer[1];
8037 else
8038 echo_area_buffer[0] = echo_buffer[0];
8041 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8043 /* Someone switched buffers between print requests. */
8044 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8045 current_buffer->truncate_lines = Qnil;
8051 /* Display an echo area message in window W. Value is non-zero if W's
8052 height is changed. If display_last_displayed_message_p is
8053 non-zero, display the message that was last displayed, otherwise
8054 display the current message. */
8056 static int
8057 display_echo_area (w)
8058 struct window *w;
8060 int i, no_message_p, window_height_changed_p, count;
8062 /* Temporarily disable garbage collections while displaying the echo
8063 area. This is done because a GC can print a message itself.
8064 That message would modify the echo area buffer's contents while a
8065 redisplay of the buffer is going on, and seriously confuse
8066 redisplay. */
8067 count = inhibit_garbage_collection ();
8069 /* If there is no message, we must call display_echo_area_1
8070 nevertheless because it resizes the window. But we will have to
8071 reset the echo_area_buffer in question to nil at the end because
8072 with_echo_area_buffer will sets it to an empty buffer. */
8073 i = display_last_displayed_message_p ? 1 : 0;
8074 no_message_p = NILP (echo_area_buffer[i]);
8076 window_height_changed_p
8077 = with_echo_area_buffer (w, display_last_displayed_message_p,
8078 display_echo_area_1,
8079 (EMACS_INT) w, Qnil, 0, 0);
8081 if (no_message_p)
8082 echo_area_buffer[i] = Qnil;
8084 unbind_to (count, Qnil);
8085 return window_height_changed_p;
8089 /* Helper for display_echo_area. Display the current buffer which
8090 contains the current echo area message in window W, a mini-window,
8091 a pointer to which is passed in A1. A2..A4 are currently not used.
8092 Change the height of W so that all of the message is displayed.
8093 Value is non-zero if height of W was changed. */
8095 static int
8096 display_echo_area_1 (a1, a2, a3, a4)
8097 EMACS_INT a1;
8098 Lisp_Object a2;
8099 EMACS_INT a3, a4;
8101 struct window *w = (struct window *) a1;
8102 Lisp_Object window;
8103 struct text_pos start;
8104 int window_height_changed_p = 0;
8106 /* Do this before displaying, so that we have a large enough glyph
8107 matrix for the display. If we can't get enough space for the
8108 whole text, display the last N lines. That works by setting w->start. */
8109 window_height_changed_p = resize_mini_window (w, 0);
8111 /* Use the starting position chosen by resize_mini_window. */
8112 SET_TEXT_POS_FROM_MARKER (start, w->start);
8114 /* Display. */
8115 clear_glyph_matrix (w->desired_matrix);
8116 XSETWINDOW (window, w);
8117 try_window (window, start, 0);
8119 return window_height_changed_p;
8123 /* Resize the echo area window to exactly the size needed for the
8124 currently displayed message, if there is one. If a mini-buffer
8125 is active, don't shrink it. */
8127 void
8128 resize_echo_area_exactly ()
8130 if (BUFFERP (echo_area_buffer[0])
8131 && WINDOWP (echo_area_window))
8133 struct window *w = XWINDOW (echo_area_window);
8134 int resized_p;
8135 Lisp_Object resize_exactly;
8137 if (minibuf_level == 0)
8138 resize_exactly = Qt;
8139 else
8140 resize_exactly = Qnil;
8142 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8143 (EMACS_INT) w, resize_exactly, 0, 0);
8144 if (resized_p)
8146 ++windows_or_buffers_changed;
8147 ++update_mode_lines;
8148 redisplay_internal (0);
8154 /* Callback function for with_echo_area_buffer, when used from
8155 resize_echo_area_exactly. A1 contains a pointer to the window to
8156 resize, EXACTLY non-nil means resize the mini-window exactly to the
8157 size of the text displayed. A3 and A4 are not used. Value is what
8158 resize_mini_window returns. */
8160 static int
8161 resize_mini_window_1 (a1, exactly, a3, a4)
8162 EMACS_INT a1;
8163 Lisp_Object exactly;
8164 EMACS_INT a3, a4;
8166 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8170 /* Resize mini-window W to fit the size of its contents. EXACT:P
8171 means size the window exactly to the size needed. Otherwise, it's
8172 only enlarged until W's buffer is empty.
8174 Set W->start to the right place to begin display. If the whole
8175 contents fit, start at the beginning. Otherwise, start so as
8176 to make the end of the contents appear. This is particularly
8177 important for y-or-n-p, but seems desirable generally.
8179 Value is non-zero if the window height has been changed. */
8182 resize_mini_window (w, exact_p)
8183 struct window *w;
8184 int exact_p;
8186 struct frame *f = XFRAME (w->frame);
8187 int window_height_changed_p = 0;
8189 xassert (MINI_WINDOW_P (w));
8191 /* By default, start display at the beginning. */
8192 set_marker_both (w->start, w->buffer,
8193 BUF_BEGV (XBUFFER (w->buffer)),
8194 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8196 /* Don't resize windows while redisplaying a window; it would
8197 confuse redisplay functions when the size of the window they are
8198 displaying changes from under them. Such a resizing can happen,
8199 for instance, when which-func prints a long message while
8200 we are running fontification-functions. We're running these
8201 functions with safe_call which binds inhibit-redisplay to t. */
8202 if (!NILP (Vinhibit_redisplay))
8203 return 0;
8205 /* Nil means don't try to resize. */
8206 if (NILP (Vresize_mini_windows)
8207 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8208 return 0;
8210 if (!FRAME_MINIBUF_ONLY_P (f))
8212 struct it it;
8213 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8214 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8215 int height, max_height;
8216 int unit = FRAME_LINE_HEIGHT (f);
8217 struct text_pos start;
8218 struct buffer *old_current_buffer = NULL;
8220 if (current_buffer != XBUFFER (w->buffer))
8222 old_current_buffer = current_buffer;
8223 set_buffer_internal (XBUFFER (w->buffer));
8226 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8228 /* Compute the max. number of lines specified by the user. */
8229 if (FLOATP (Vmax_mini_window_height))
8230 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8231 else if (INTEGERP (Vmax_mini_window_height))
8232 max_height = XINT (Vmax_mini_window_height);
8233 else
8234 max_height = total_height / 4;
8236 /* Correct that max. height if it's bogus. */
8237 max_height = max (1, max_height);
8238 max_height = min (total_height, max_height);
8240 /* Find out the height of the text in the window. */
8241 if (it.truncate_lines_p)
8242 height = 1;
8243 else
8245 last_height = 0;
8246 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8247 if (it.max_ascent == 0 && it.max_descent == 0)
8248 height = it.current_y + last_height;
8249 else
8250 height = it.current_y + it.max_ascent + it.max_descent;
8251 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8252 height = (height + unit - 1) / unit;
8255 /* Compute a suitable window start. */
8256 if (height > max_height)
8258 height = max_height;
8259 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8260 move_it_vertically_backward (&it, (height - 1) * unit);
8261 start = it.current.pos;
8263 else
8264 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8265 SET_MARKER_FROM_TEXT_POS (w->start, start);
8267 if (EQ (Vresize_mini_windows, Qgrow_only))
8269 /* Let it grow only, until we display an empty message, in which
8270 case the window shrinks again. */
8271 if (height > WINDOW_TOTAL_LINES (w))
8273 int old_height = WINDOW_TOTAL_LINES (w);
8274 freeze_window_starts (f, 1);
8275 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8276 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8278 else if (height < WINDOW_TOTAL_LINES (w)
8279 && (exact_p || BEGV == ZV))
8281 int old_height = WINDOW_TOTAL_LINES (w);
8282 freeze_window_starts (f, 0);
8283 shrink_mini_window (w);
8284 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8287 else
8289 /* Always resize to exact size needed. */
8290 if (height > WINDOW_TOTAL_LINES (w))
8292 int old_height = WINDOW_TOTAL_LINES (w);
8293 freeze_window_starts (f, 1);
8294 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8295 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8297 else if (height < WINDOW_TOTAL_LINES (w))
8299 int old_height = WINDOW_TOTAL_LINES (w);
8300 freeze_window_starts (f, 0);
8301 shrink_mini_window (w);
8303 if (height)
8305 freeze_window_starts (f, 1);
8306 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8309 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8313 if (old_current_buffer)
8314 set_buffer_internal (old_current_buffer);
8317 return window_height_changed_p;
8321 /* Value is the current message, a string, or nil if there is no
8322 current message. */
8324 Lisp_Object
8325 current_message ()
8327 Lisp_Object msg;
8329 if (NILP (echo_area_buffer[0]))
8330 msg = Qnil;
8331 else
8333 with_echo_area_buffer (0, 0, current_message_1,
8334 (EMACS_INT) &msg, Qnil, 0, 0);
8335 if (NILP (msg))
8336 echo_area_buffer[0] = Qnil;
8339 return msg;
8343 static int
8344 current_message_1 (a1, a2, a3, a4)
8345 EMACS_INT a1;
8346 Lisp_Object a2;
8347 EMACS_INT a3, a4;
8349 Lisp_Object *msg = (Lisp_Object *) a1;
8351 if (Z > BEG)
8352 *msg = make_buffer_string (BEG, Z, 1);
8353 else
8354 *msg = Qnil;
8355 return 0;
8359 /* Push the current message on Vmessage_stack for later restauration
8360 by restore_message. Value is non-zero if the current message isn't
8361 empty. This is a relatively infrequent operation, so it's not
8362 worth optimizing. */
8365 push_message ()
8367 Lisp_Object msg;
8368 msg = current_message ();
8369 Vmessage_stack = Fcons (msg, Vmessage_stack);
8370 return STRINGP (msg);
8374 /* Restore message display from the top of Vmessage_stack. */
8376 void
8377 restore_message ()
8379 Lisp_Object msg;
8381 xassert (CONSP (Vmessage_stack));
8382 msg = XCAR (Vmessage_stack);
8383 if (STRINGP (msg))
8384 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8385 else
8386 message3_nolog (msg, 0, 0);
8390 /* Handler for record_unwind_protect calling pop_message. */
8392 Lisp_Object
8393 pop_message_unwind (dummy)
8394 Lisp_Object dummy;
8396 pop_message ();
8397 return Qnil;
8400 /* Pop the top-most entry off Vmessage_stack. */
8402 void
8403 pop_message ()
8405 xassert (CONSP (Vmessage_stack));
8406 Vmessage_stack = XCDR (Vmessage_stack);
8410 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8411 exits. If the stack is not empty, we have a missing pop_message
8412 somewhere. */
8414 void
8415 check_message_stack ()
8417 if (!NILP (Vmessage_stack))
8418 abort ();
8422 /* Truncate to NCHARS what will be displayed in the echo area the next
8423 time we display it---but don't redisplay it now. */
8425 void
8426 truncate_echo_area (nchars)
8427 int nchars;
8429 if (nchars == 0)
8430 echo_area_buffer[0] = Qnil;
8431 /* A null message buffer means that the frame hasn't really been
8432 initialized yet. Error messages get reported properly by
8433 cmd_error, so this must be just an informative message; toss it. */
8434 else if (!noninteractive
8435 && INTERACTIVE
8436 && !NILP (echo_area_buffer[0]))
8438 struct frame *sf = SELECTED_FRAME ();
8439 if (FRAME_MESSAGE_BUF (sf))
8440 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8445 /* Helper function for truncate_echo_area. Truncate the current
8446 message to at most NCHARS characters. */
8448 static int
8449 truncate_message_1 (nchars, a2, a3, a4)
8450 EMACS_INT nchars;
8451 Lisp_Object a2;
8452 EMACS_INT a3, a4;
8454 if (BEG + nchars < Z)
8455 del_range (BEG + nchars, Z);
8456 if (Z == BEG)
8457 echo_area_buffer[0] = Qnil;
8458 return 0;
8462 /* Set the current message to a substring of S or STRING.
8464 If STRING is a Lisp string, set the message to the first NBYTES
8465 bytes from STRING. NBYTES zero means use the whole string. If
8466 STRING is multibyte, the message will be displayed multibyte.
8468 If S is not null, set the message to the first LEN bytes of S. LEN
8469 zero means use the whole string. MULTIBYTE_P non-zero means S is
8470 multibyte. Display the message multibyte in that case.
8472 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8473 to t before calling set_message_1 (which calls insert).
8476 void
8477 set_message (s, string, nbytes, multibyte_p)
8478 const char *s;
8479 Lisp_Object string;
8480 int nbytes, multibyte_p;
8482 message_enable_multibyte
8483 = ((s && multibyte_p)
8484 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8486 with_echo_area_buffer (0, 0, set_message_1,
8487 (EMACS_INT) s, string, nbytes, multibyte_p);
8488 message_buf_print = 0;
8489 help_echo_showing_p = 0;
8493 /* Helper function for set_message. Arguments have the same meaning
8494 as there, with A1 corresponding to S and A2 corresponding to STRING
8495 This function is called with the echo area buffer being
8496 current. */
8498 static int
8499 set_message_1 (a1, a2, nbytes, multibyte_p)
8500 EMACS_INT a1;
8501 Lisp_Object a2;
8502 EMACS_INT nbytes, multibyte_p;
8504 const char *s = (const char *) a1;
8505 Lisp_Object string = a2;
8507 /* Change multibyteness of the echo buffer appropriately. */
8508 if (message_enable_multibyte
8509 != !NILP (current_buffer->enable_multibyte_characters))
8510 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8512 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8514 /* Insert new message at BEG. */
8515 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8516 Ferase_buffer ();
8518 if (STRINGP (string))
8520 int nchars;
8522 if (nbytes == 0)
8523 nbytes = SBYTES (string);
8524 nchars = string_byte_to_char (string, nbytes);
8526 /* This function takes care of single/multibyte conversion. We
8527 just have to ensure that the echo area buffer has the right
8528 setting of enable_multibyte_characters. */
8529 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8531 else if (s)
8533 if (nbytes == 0)
8534 nbytes = strlen (s);
8536 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8538 /* Convert from multi-byte to single-byte. */
8539 int i, c, n;
8540 unsigned char work[1];
8542 /* Convert a multibyte string to single-byte. */
8543 for (i = 0; i < nbytes; i += n)
8545 c = string_char_and_length (s + i, nbytes - i, &n);
8546 work[0] = (SINGLE_BYTE_CHAR_P (c)
8548 : multibyte_char_to_unibyte (c, Qnil));
8549 insert_1_both (work, 1, 1, 1, 0, 0);
8552 else if (!multibyte_p
8553 && !NILP (current_buffer->enable_multibyte_characters))
8555 /* Convert from single-byte to multi-byte. */
8556 int i, c, n;
8557 const unsigned char *msg = (const unsigned char *) s;
8558 unsigned char str[MAX_MULTIBYTE_LENGTH];
8560 /* Convert a single-byte string to multibyte. */
8561 for (i = 0; i < nbytes; i++)
8563 c = unibyte_char_to_multibyte (msg[i]);
8564 n = CHAR_STRING (c, str);
8565 insert_1_both (str, 1, n, 1, 0, 0);
8568 else
8569 insert_1 (s, nbytes, 1, 0, 0);
8572 return 0;
8576 /* Clear messages. CURRENT_P non-zero means clear the current
8577 message. LAST_DISPLAYED_P non-zero means clear the message
8578 last displayed. */
8580 void
8581 clear_message (current_p, last_displayed_p)
8582 int current_p, last_displayed_p;
8584 if (current_p)
8586 echo_area_buffer[0] = Qnil;
8587 message_cleared_p = 1;
8590 if (last_displayed_p)
8591 echo_area_buffer[1] = Qnil;
8593 message_buf_print = 0;
8596 /* Clear garbaged frames.
8598 This function is used where the old redisplay called
8599 redraw_garbaged_frames which in turn called redraw_frame which in
8600 turn called clear_frame. The call to clear_frame was a source of
8601 flickering. I believe a clear_frame is not necessary. It should
8602 suffice in the new redisplay to invalidate all current matrices,
8603 and ensure a complete redisplay of all windows. */
8605 static void
8606 clear_garbaged_frames ()
8608 if (frame_garbaged)
8610 Lisp_Object tail, frame;
8611 int changed_count = 0;
8613 FOR_EACH_FRAME (tail, frame)
8615 struct frame *f = XFRAME (frame);
8617 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8619 if (f->resized_p)
8621 Fredraw_frame (frame);
8622 f->force_flush_display_p = 1;
8624 clear_current_matrices (f);
8625 changed_count++;
8626 f->garbaged = 0;
8627 f->resized_p = 0;
8631 frame_garbaged = 0;
8632 if (changed_count)
8633 ++windows_or_buffers_changed;
8638 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8639 is non-zero update selected_frame. Value is non-zero if the
8640 mini-windows height has been changed. */
8642 static int
8643 echo_area_display (update_frame_p)
8644 int update_frame_p;
8646 Lisp_Object mini_window;
8647 struct window *w;
8648 struct frame *f;
8649 int window_height_changed_p = 0;
8650 struct frame *sf = SELECTED_FRAME ();
8652 mini_window = FRAME_MINIBUF_WINDOW (sf);
8653 w = XWINDOW (mini_window);
8654 f = XFRAME (WINDOW_FRAME (w));
8656 /* Don't display if frame is invisible or not yet initialized. */
8657 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8658 return 0;
8660 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8661 #ifndef MAC_OS8
8662 #ifdef HAVE_WINDOW_SYSTEM
8663 /* When Emacs starts, selected_frame may be a visible terminal
8664 frame, even if we run under a window system. If we let this
8665 through, a message would be displayed on the terminal. */
8666 if (EQ (selected_frame, Vterminal_frame)
8667 && !NILP (Vwindow_system))
8668 return 0;
8669 #endif /* HAVE_WINDOW_SYSTEM */
8670 #endif
8672 /* Redraw garbaged frames. */
8673 if (frame_garbaged)
8674 clear_garbaged_frames ();
8676 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8678 echo_area_window = mini_window;
8679 window_height_changed_p = display_echo_area (w);
8680 w->must_be_updated_p = 1;
8682 /* Update the display, unless called from redisplay_internal.
8683 Also don't update the screen during redisplay itself. The
8684 update will happen at the end of redisplay, and an update
8685 here could cause confusion. */
8686 if (update_frame_p && !redisplaying_p)
8688 int n = 0;
8690 /* If the display update has been interrupted by pending
8691 input, update mode lines in the frame. Due to the
8692 pending input, it might have been that redisplay hasn't
8693 been called, so that mode lines above the echo area are
8694 garbaged. This looks odd, so we prevent it here. */
8695 if (!display_completed)
8696 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8698 if (window_height_changed_p
8699 /* Don't do this if Emacs is shutting down. Redisplay
8700 needs to run hooks. */
8701 && !NILP (Vrun_hooks))
8703 /* Must update other windows. Likewise as in other
8704 cases, don't let this update be interrupted by
8705 pending input. */
8706 int count = SPECPDL_INDEX ();
8707 specbind (Qredisplay_dont_pause, Qt);
8708 windows_or_buffers_changed = 1;
8709 redisplay_internal (0);
8710 unbind_to (count, Qnil);
8712 else if (FRAME_WINDOW_P (f) && n == 0)
8714 /* Window configuration is the same as before.
8715 Can do with a display update of the echo area,
8716 unless we displayed some mode lines. */
8717 update_single_window (w, 1);
8718 rif->flush_display (f);
8720 else
8721 update_frame (f, 1, 1);
8723 /* If cursor is in the echo area, make sure that the next
8724 redisplay displays the minibuffer, so that the cursor will
8725 be replaced with what the minibuffer wants. */
8726 if (cursor_in_echo_area)
8727 ++windows_or_buffers_changed;
8730 else if (!EQ (mini_window, selected_window))
8731 windows_or_buffers_changed++;
8733 /* The current message is now also the last one displayed. */
8734 echo_area_buffer[1] = echo_area_buffer[0];
8736 /* Prevent redisplay optimization in redisplay_internal by resetting
8737 this_line_start_pos. This is done because the mini-buffer now
8738 displays the message instead of its buffer text. */
8739 if (EQ (mini_window, selected_window))
8740 CHARPOS (this_line_start_pos) = 0;
8742 return window_height_changed_p;
8747 /***********************************************************************
8748 Mode Lines and Frame Titles
8749 ***********************************************************************/
8751 /* A buffer for constructing non-propertized mode-line strings and
8752 frame titles in it; allocated from the heap in init_xdisp and
8753 resized as needed in store_mode_line_noprop_char. */
8755 static char *mode_line_noprop_buf;
8757 /* The buffer's end, and a current output position in it. */
8759 static char *mode_line_noprop_buf_end;
8760 static char *mode_line_noprop_ptr;
8762 #define MODE_LINE_NOPROP_LEN(start) \
8763 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8765 static enum {
8766 MODE_LINE_DISPLAY = 0,
8767 MODE_LINE_TITLE,
8768 MODE_LINE_NOPROP,
8769 MODE_LINE_STRING
8770 } mode_line_target;
8772 /* Alist that caches the results of :propertize.
8773 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8774 static Lisp_Object mode_line_proptrans_alist;
8776 /* List of strings making up the mode-line. */
8777 static Lisp_Object mode_line_string_list;
8779 /* Base face property when building propertized mode line string. */
8780 static Lisp_Object mode_line_string_face;
8781 static Lisp_Object mode_line_string_face_prop;
8784 /* Unwind data for mode line strings */
8786 static Lisp_Object Vmode_line_unwind_vector;
8788 static Lisp_Object
8789 format_mode_line_unwind_data (obuf, save_proptrans)
8790 struct buffer *obuf;
8792 Lisp_Object vector;
8794 /* Reduce consing by keeping one vector in
8795 Vwith_echo_area_save_vector. */
8796 vector = Vmode_line_unwind_vector;
8797 Vmode_line_unwind_vector = Qnil;
8799 if (NILP (vector))
8800 vector = Fmake_vector (make_number (7), Qnil);
8802 AREF (vector, 0) = make_number (mode_line_target);
8803 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8804 AREF (vector, 2) = mode_line_string_list;
8805 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8806 AREF (vector, 4) = mode_line_string_face;
8807 AREF (vector, 5) = mode_line_string_face_prop;
8809 if (obuf)
8810 XSETBUFFER (AREF (vector, 6), obuf);
8811 else
8812 AREF (vector, 6) = Qnil;
8814 return vector;
8817 static Lisp_Object
8818 unwind_format_mode_line (vector)
8819 Lisp_Object vector;
8821 mode_line_target = XINT (AREF (vector, 0));
8822 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8823 mode_line_string_list = AREF (vector, 2);
8824 if (! EQ (AREF (vector, 3), Qt))
8825 mode_line_proptrans_alist = AREF (vector, 3);
8826 mode_line_string_face = AREF (vector, 4);
8827 mode_line_string_face_prop = AREF (vector, 5);
8829 if (!NILP (AREF (vector, 6)))
8831 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8832 AREF (vector, 6) = Qnil;
8835 Vmode_line_unwind_vector = vector;
8836 return Qnil;
8840 /* Store a single character C for the frame title in mode_line_noprop_buf.
8841 Re-allocate mode_line_noprop_buf if necessary. */
8843 static void
8844 #ifdef PROTOTYPES
8845 store_mode_line_noprop_char (char c)
8846 #else
8847 store_mode_line_noprop_char (c)
8848 char c;
8849 #endif
8851 /* If output position has reached the end of the allocated buffer,
8852 double the buffer's size. */
8853 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8855 int len = MODE_LINE_NOPROP_LEN (0);
8856 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8857 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8858 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8859 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8862 *mode_line_noprop_ptr++ = c;
8866 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8867 mode_line_noprop_ptr. STR is the string to store. Do not copy
8868 characters that yield more columns than PRECISION; PRECISION <= 0
8869 means copy the whole string. Pad with spaces until FIELD_WIDTH
8870 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8871 pad. Called from display_mode_element when it is used to build a
8872 frame title. */
8874 static int
8875 store_mode_line_noprop (str, field_width, precision)
8876 const unsigned char *str;
8877 int field_width, precision;
8879 int n = 0;
8880 int dummy, nbytes;
8882 /* Copy at most PRECISION chars from STR. */
8883 nbytes = strlen (str);
8884 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8885 while (nbytes--)
8886 store_mode_line_noprop_char (*str++);
8888 /* Fill up with spaces until FIELD_WIDTH reached. */
8889 while (field_width > 0
8890 && n < field_width)
8892 store_mode_line_noprop_char (' ');
8893 ++n;
8896 return n;
8899 /***********************************************************************
8900 Frame Titles
8901 ***********************************************************************/
8903 #ifdef HAVE_WINDOW_SYSTEM
8905 /* Set the title of FRAME, if it has changed. The title format is
8906 Vicon_title_format if FRAME is iconified, otherwise it is
8907 frame_title_format. */
8909 static void
8910 x_consider_frame_title (frame)
8911 Lisp_Object frame;
8913 struct frame *f = XFRAME (frame);
8915 if (FRAME_WINDOW_P (f)
8916 || FRAME_MINIBUF_ONLY_P (f)
8917 || f->explicit_name)
8919 /* Do we have more than one visible frame on this X display? */
8920 Lisp_Object tail;
8921 Lisp_Object fmt;
8922 int title_start;
8923 char *title;
8924 int len;
8925 struct it it;
8926 int count = SPECPDL_INDEX ();
8928 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8930 Lisp_Object other_frame = XCAR (tail);
8931 struct frame *tf = XFRAME (other_frame);
8933 if (tf != f
8934 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8935 && !FRAME_MINIBUF_ONLY_P (tf)
8936 && !EQ (other_frame, tip_frame)
8937 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8938 break;
8941 /* Set global variable indicating that multiple frames exist. */
8942 multiple_frames = CONSP (tail);
8944 /* Switch to the buffer of selected window of the frame. Set up
8945 mode_line_target so that display_mode_element will output into
8946 mode_line_noprop_buf; then display the title. */
8947 record_unwind_protect (unwind_format_mode_line,
8948 format_mode_line_unwind_data (current_buffer, 0));
8950 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8951 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8953 mode_line_target = MODE_LINE_TITLE;
8954 title_start = MODE_LINE_NOPROP_LEN (0);
8955 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8956 NULL, DEFAULT_FACE_ID);
8957 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8958 len = MODE_LINE_NOPROP_LEN (title_start);
8959 title = mode_line_noprop_buf + title_start;
8960 unbind_to (count, Qnil);
8962 /* Set the title only if it's changed. This avoids consing in
8963 the common case where it hasn't. (If it turns out that we've
8964 already wasted too much time by walking through the list with
8965 display_mode_element, then we might need to optimize at a
8966 higher level than this.) */
8967 if (! STRINGP (f->name)
8968 || SBYTES (f->name) != len
8969 || bcmp (title, SDATA (f->name), len) != 0)
8970 x_implicitly_set_name (f, make_string (title, len), Qnil);
8974 #endif /* not HAVE_WINDOW_SYSTEM */
8979 /***********************************************************************
8980 Menu Bars
8981 ***********************************************************************/
8984 /* Prepare for redisplay by updating menu-bar item lists when
8985 appropriate. This can call eval. */
8987 void
8988 prepare_menu_bars ()
8990 int all_windows;
8991 struct gcpro gcpro1, gcpro2;
8992 struct frame *f;
8993 Lisp_Object tooltip_frame;
8995 #ifdef HAVE_WINDOW_SYSTEM
8996 tooltip_frame = tip_frame;
8997 #else
8998 tooltip_frame = Qnil;
8999 #endif
9001 /* Update all frame titles based on their buffer names, etc. We do
9002 this before the menu bars so that the buffer-menu will show the
9003 up-to-date frame titles. */
9004 #ifdef HAVE_WINDOW_SYSTEM
9005 if (windows_or_buffers_changed || update_mode_lines)
9007 Lisp_Object tail, frame;
9009 FOR_EACH_FRAME (tail, frame)
9011 f = XFRAME (frame);
9012 if (!EQ (frame, tooltip_frame)
9013 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9014 x_consider_frame_title (frame);
9017 #endif /* HAVE_WINDOW_SYSTEM */
9019 /* Update the menu bar item lists, if appropriate. This has to be
9020 done before any actual redisplay or generation of display lines. */
9021 all_windows = (update_mode_lines
9022 || buffer_shared > 1
9023 || windows_or_buffers_changed);
9024 if (all_windows)
9026 Lisp_Object tail, frame;
9027 int count = SPECPDL_INDEX ();
9029 record_unwind_save_match_data ();
9031 FOR_EACH_FRAME (tail, frame)
9033 f = XFRAME (frame);
9035 /* Ignore tooltip frame. */
9036 if (EQ (frame, tooltip_frame))
9037 continue;
9039 /* If a window on this frame changed size, report that to
9040 the user and clear the size-change flag. */
9041 if (FRAME_WINDOW_SIZES_CHANGED (f))
9043 Lisp_Object functions;
9045 /* Clear flag first in case we get an error below. */
9046 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9047 functions = Vwindow_size_change_functions;
9048 GCPRO2 (tail, functions);
9050 while (CONSP (functions))
9052 call1 (XCAR (functions), frame);
9053 functions = XCDR (functions);
9055 UNGCPRO;
9058 GCPRO1 (tail);
9059 update_menu_bar (f, 0);
9060 #ifdef HAVE_WINDOW_SYSTEM
9061 update_tool_bar (f, 0);
9062 #ifdef MAC_OS
9063 mac_update_title_bar (f, 0);
9064 #endif
9065 #endif
9066 UNGCPRO;
9069 unbind_to (count, Qnil);
9071 else
9073 struct frame *sf = SELECTED_FRAME ();
9074 update_menu_bar (sf, 1);
9075 #ifdef HAVE_WINDOW_SYSTEM
9076 update_tool_bar (sf, 1);
9077 #ifdef MAC_OS
9078 mac_update_title_bar (sf, 1);
9079 #endif
9080 #endif
9083 /* Motif needs this. See comment in xmenu.c. Turn it off when
9084 pending_menu_activation is not defined. */
9085 #ifdef USE_X_TOOLKIT
9086 pending_menu_activation = 0;
9087 #endif
9091 /* Update the menu bar item list for frame F. This has to be done
9092 before we start to fill in any display lines, because it can call
9093 eval.
9095 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9097 static void
9098 update_menu_bar (f, save_match_data)
9099 struct frame *f;
9100 int save_match_data;
9102 Lisp_Object window;
9103 register struct window *w;
9105 /* If called recursively during a menu update, do nothing. This can
9106 happen when, for instance, an activate-menubar-hook causes a
9107 redisplay. */
9108 if (inhibit_menubar_update)
9109 return;
9111 window = FRAME_SELECTED_WINDOW (f);
9112 w = XWINDOW (window);
9114 #if 0 /* The if statement below this if statement used to include the
9115 condition !NILP (w->update_mode_line), rather than using
9116 update_mode_lines directly, and this if statement may have
9117 been added to make that condition work. Now the if
9118 statement below matches its comment, this isn't needed. */
9119 if (update_mode_lines)
9120 w->update_mode_line = Qt;
9121 #endif
9123 if (FRAME_WINDOW_P (f)
9125 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9126 || defined (USE_GTK)
9127 FRAME_EXTERNAL_MENU_BAR (f)
9128 #else
9129 FRAME_MENU_BAR_LINES (f) > 0
9130 #endif
9131 : FRAME_MENU_BAR_LINES (f) > 0)
9133 /* If the user has switched buffers or windows, we need to
9134 recompute to reflect the new bindings. But we'll
9135 recompute when update_mode_lines is set too; that means
9136 that people can use force-mode-line-update to request
9137 that the menu bar be recomputed. The adverse effect on
9138 the rest of the redisplay algorithm is about the same as
9139 windows_or_buffers_changed anyway. */
9140 if (windows_or_buffers_changed
9141 /* This used to test w->update_mode_line, but we believe
9142 there is no need to recompute the menu in that case. */
9143 || update_mode_lines
9144 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9145 < BUF_MODIFF (XBUFFER (w->buffer)))
9146 != !NILP (w->last_had_star))
9147 || ((!NILP (Vtransient_mark_mode)
9148 && !NILP (XBUFFER (w->buffer)->mark_active))
9149 != !NILP (w->region_showing)))
9151 struct buffer *prev = current_buffer;
9152 int count = SPECPDL_INDEX ();
9154 specbind (Qinhibit_menubar_update, Qt);
9156 set_buffer_internal_1 (XBUFFER (w->buffer));
9157 if (save_match_data)
9158 record_unwind_save_match_data ();
9159 if (NILP (Voverriding_local_map_menu_flag))
9161 specbind (Qoverriding_terminal_local_map, Qnil);
9162 specbind (Qoverriding_local_map, Qnil);
9165 /* Run the Lucid hook. */
9166 safe_run_hooks (Qactivate_menubar_hook);
9168 /* If it has changed current-menubar from previous value,
9169 really recompute the menu-bar from the value. */
9170 if (! NILP (Vlucid_menu_bar_dirty_flag))
9171 call0 (Qrecompute_lucid_menubar);
9173 safe_run_hooks (Qmenu_bar_update_hook);
9174 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9176 /* Redisplay the menu bar in case we changed it. */
9177 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9178 || defined (USE_GTK)
9179 if (FRAME_WINDOW_P (f))
9181 #ifdef MAC_OS
9182 /* All frames on Mac OS share the same menubar. So only
9183 the selected frame should be allowed to set it. */
9184 if (f == SELECTED_FRAME ())
9185 #endif
9186 set_frame_menubar (f, 0, 0);
9188 else
9189 /* On a terminal screen, the menu bar is an ordinary screen
9190 line, and this makes it get updated. */
9191 w->update_mode_line = Qt;
9192 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9193 /* In the non-toolkit version, the menu bar is an ordinary screen
9194 line, and this makes it get updated. */
9195 w->update_mode_line = Qt;
9196 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9198 unbind_to (count, Qnil);
9199 set_buffer_internal_1 (prev);
9206 /***********************************************************************
9207 Output Cursor
9208 ***********************************************************************/
9210 #ifdef HAVE_WINDOW_SYSTEM
9212 /* EXPORT:
9213 Nominal cursor position -- where to draw output.
9214 HPOS and VPOS are window relative glyph matrix coordinates.
9215 X and Y are window relative pixel coordinates. */
9217 struct cursor_pos output_cursor;
9220 /* EXPORT:
9221 Set the global variable output_cursor to CURSOR. All cursor
9222 positions are relative to updated_window. */
9224 void
9225 set_output_cursor (cursor)
9226 struct cursor_pos *cursor;
9228 output_cursor.hpos = cursor->hpos;
9229 output_cursor.vpos = cursor->vpos;
9230 output_cursor.x = cursor->x;
9231 output_cursor.y = cursor->y;
9235 /* EXPORT for RIF:
9236 Set a nominal cursor position.
9238 HPOS and VPOS are column/row positions in a window glyph matrix. X
9239 and Y are window text area relative pixel positions.
9241 If this is done during an update, updated_window will contain the
9242 window that is being updated and the position is the future output
9243 cursor position for that window. If updated_window is null, use
9244 selected_window and display the cursor at the given position. */
9246 void
9247 x_cursor_to (vpos, hpos, y, x)
9248 int vpos, hpos, y, x;
9250 struct window *w;
9252 /* If updated_window is not set, work on selected_window. */
9253 if (updated_window)
9254 w = updated_window;
9255 else
9256 w = XWINDOW (selected_window);
9258 /* Set the output cursor. */
9259 output_cursor.hpos = hpos;
9260 output_cursor.vpos = vpos;
9261 output_cursor.x = x;
9262 output_cursor.y = y;
9264 /* If not called as part of an update, really display the cursor.
9265 This will also set the cursor position of W. */
9266 if (updated_window == NULL)
9268 BLOCK_INPUT;
9269 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9270 if (rif->flush_display_optional)
9271 rif->flush_display_optional (SELECTED_FRAME ());
9272 UNBLOCK_INPUT;
9276 #endif /* HAVE_WINDOW_SYSTEM */
9279 /***********************************************************************
9280 Tool-bars
9281 ***********************************************************************/
9283 #ifdef HAVE_WINDOW_SYSTEM
9285 /* Where the mouse was last time we reported a mouse event. */
9287 FRAME_PTR last_mouse_frame;
9289 /* Tool-bar item index of the item on which a mouse button was pressed
9290 or -1. */
9292 int last_tool_bar_item;
9295 /* Update the tool-bar item list for frame F. This has to be done
9296 before we start to fill in any display lines. Called from
9297 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9298 and restore it here. */
9300 static void
9301 update_tool_bar (f, save_match_data)
9302 struct frame *f;
9303 int save_match_data;
9305 #ifdef USE_GTK
9306 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9307 #else
9308 int do_update = WINDOWP (f->tool_bar_window)
9309 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9310 #endif
9312 if (do_update)
9314 Lisp_Object window;
9315 struct window *w;
9317 window = FRAME_SELECTED_WINDOW (f);
9318 w = XWINDOW (window);
9320 /* If the user has switched buffers or windows, we need to
9321 recompute to reflect the new bindings. But we'll
9322 recompute when update_mode_lines is set too; that means
9323 that people can use force-mode-line-update to request
9324 that the menu bar be recomputed. The adverse effect on
9325 the rest of the redisplay algorithm is about the same as
9326 windows_or_buffers_changed anyway. */
9327 if (windows_or_buffers_changed
9328 || !NILP (w->update_mode_line)
9329 || update_mode_lines
9330 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9331 < BUF_MODIFF (XBUFFER (w->buffer)))
9332 != !NILP (w->last_had_star))
9333 || ((!NILP (Vtransient_mark_mode)
9334 && !NILP (XBUFFER (w->buffer)->mark_active))
9335 != !NILP (w->region_showing)))
9337 struct buffer *prev = current_buffer;
9338 int count = SPECPDL_INDEX ();
9339 Lisp_Object new_tool_bar;
9340 int new_n_tool_bar;
9341 struct gcpro gcpro1;
9343 /* Set current_buffer to the buffer of the selected
9344 window of the frame, so that we get the right local
9345 keymaps. */
9346 set_buffer_internal_1 (XBUFFER (w->buffer));
9348 /* Save match data, if we must. */
9349 if (save_match_data)
9350 record_unwind_save_match_data ();
9352 /* Make sure that we don't accidentally use bogus keymaps. */
9353 if (NILP (Voverriding_local_map_menu_flag))
9355 specbind (Qoverriding_terminal_local_map, Qnil);
9356 specbind (Qoverriding_local_map, Qnil);
9359 GCPRO1 (new_tool_bar);
9361 /* Build desired tool-bar items from keymaps. */
9362 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9363 &new_n_tool_bar);
9365 /* Redisplay the tool-bar if we changed it. */
9366 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9368 /* Redisplay that happens asynchronously due to an expose event
9369 may access f->tool_bar_items. Make sure we update both
9370 variables within BLOCK_INPUT so no such event interrupts. */
9371 BLOCK_INPUT;
9372 f->tool_bar_items = new_tool_bar;
9373 f->n_tool_bar_items = new_n_tool_bar;
9374 w->update_mode_line = Qt;
9375 UNBLOCK_INPUT;
9378 UNGCPRO;
9380 unbind_to (count, Qnil);
9381 set_buffer_internal_1 (prev);
9387 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9388 F's desired tool-bar contents. F->tool_bar_items must have
9389 been set up previously by calling prepare_menu_bars. */
9391 static void
9392 build_desired_tool_bar_string (f)
9393 struct frame *f;
9395 int i, size, size_needed;
9396 struct gcpro gcpro1, gcpro2, gcpro3;
9397 Lisp_Object image, plist, props;
9399 image = plist = props = Qnil;
9400 GCPRO3 (image, plist, props);
9402 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9403 Otherwise, make a new string. */
9405 /* The size of the string we might be able to reuse. */
9406 size = (STRINGP (f->desired_tool_bar_string)
9407 ? SCHARS (f->desired_tool_bar_string)
9408 : 0);
9410 /* We need one space in the string for each image. */
9411 size_needed = f->n_tool_bar_items;
9413 /* Reuse f->desired_tool_bar_string, if possible. */
9414 if (size < size_needed || NILP (f->desired_tool_bar_string))
9415 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9416 make_number (' '));
9417 else
9419 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9420 Fremove_text_properties (make_number (0), make_number (size),
9421 props, f->desired_tool_bar_string);
9424 /* Put a `display' property on the string for the images to display,
9425 put a `menu_item' property on tool-bar items with a value that
9426 is the index of the item in F's tool-bar item vector. */
9427 for (i = 0; i < f->n_tool_bar_items; ++i)
9429 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9431 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9432 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9433 int hmargin, vmargin, relief, idx, end;
9434 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9436 /* If image is a vector, choose the image according to the
9437 button state. */
9438 image = PROP (TOOL_BAR_ITEM_IMAGES);
9439 if (VECTORP (image))
9441 if (enabled_p)
9442 idx = (selected_p
9443 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9444 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9445 else
9446 idx = (selected_p
9447 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9448 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9450 xassert (ASIZE (image) >= idx);
9451 image = AREF (image, idx);
9453 else
9454 idx = -1;
9456 /* Ignore invalid image specifications. */
9457 if (!valid_image_p (image))
9458 continue;
9460 /* Display the tool-bar button pressed, or depressed. */
9461 plist = Fcopy_sequence (XCDR (image));
9463 /* Compute margin and relief to draw. */
9464 relief = (tool_bar_button_relief >= 0
9465 ? tool_bar_button_relief
9466 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9467 hmargin = vmargin = relief;
9469 if (INTEGERP (Vtool_bar_button_margin)
9470 && XINT (Vtool_bar_button_margin) > 0)
9472 hmargin += XFASTINT (Vtool_bar_button_margin);
9473 vmargin += XFASTINT (Vtool_bar_button_margin);
9475 else if (CONSP (Vtool_bar_button_margin))
9477 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9478 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9479 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9481 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9482 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9483 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9486 if (auto_raise_tool_bar_buttons_p)
9488 /* Add a `:relief' property to the image spec if the item is
9489 selected. */
9490 if (selected_p)
9492 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9493 hmargin -= relief;
9494 vmargin -= relief;
9497 else
9499 /* If image is selected, display it pressed, i.e. with a
9500 negative relief. If it's not selected, display it with a
9501 raised relief. */
9502 plist = Fplist_put (plist, QCrelief,
9503 (selected_p
9504 ? make_number (-relief)
9505 : make_number (relief)));
9506 hmargin -= relief;
9507 vmargin -= relief;
9510 /* Put a margin around the image. */
9511 if (hmargin || vmargin)
9513 if (hmargin == vmargin)
9514 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9515 else
9516 plist = Fplist_put (plist, QCmargin,
9517 Fcons (make_number (hmargin),
9518 make_number (vmargin)));
9521 /* If button is not enabled, and we don't have special images
9522 for the disabled state, make the image appear disabled by
9523 applying an appropriate algorithm to it. */
9524 if (!enabled_p && idx < 0)
9525 plist = Fplist_put (plist, QCconversion, Qdisabled);
9527 /* Put a `display' text property on the string for the image to
9528 display. Put a `menu-item' property on the string that gives
9529 the start of this item's properties in the tool-bar items
9530 vector. */
9531 image = Fcons (Qimage, plist);
9532 props = list4 (Qdisplay, image,
9533 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9535 /* Let the last image hide all remaining spaces in the tool bar
9536 string. The string can be longer than needed when we reuse a
9537 previous string. */
9538 if (i + 1 == f->n_tool_bar_items)
9539 end = SCHARS (f->desired_tool_bar_string);
9540 else
9541 end = i + 1;
9542 Fadd_text_properties (make_number (i), make_number (end),
9543 props, f->desired_tool_bar_string);
9544 #undef PROP
9547 UNGCPRO;
9551 /* Display one line of the tool-bar of frame IT->f.
9553 HEIGHT specifies the desired height of the tool-bar line.
9554 If the actual height of the glyph row is less than HEIGHT, the
9555 row's height is increased to HEIGHT, and the icons are centered
9556 vertically in the new height.
9558 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9559 count a final empty row in case the tool-bar width exactly matches
9560 the window width.
9563 static void
9564 display_tool_bar_line (it, height)
9565 struct it *it;
9566 int height;
9568 struct glyph_row *row = it->glyph_row;
9569 int max_x = it->last_visible_x;
9570 struct glyph *last;
9572 prepare_desired_row (row);
9573 row->y = it->current_y;
9575 /* Note that this isn't made use of if the face hasn't a box,
9576 so there's no need to check the face here. */
9577 it->start_of_box_run_p = 1;
9579 while (it->current_x < max_x)
9581 int x, n_glyphs_before, i, nglyphs;
9582 struct it it_before;
9584 /* Get the next display element. */
9585 if (!get_next_display_element (it))
9587 /* Don't count empty row if we are counting needed tool-bar lines. */
9588 if (height < 0 && !it->hpos)
9589 return;
9590 break;
9593 /* Produce glyphs. */
9594 n_glyphs_before = row->used[TEXT_AREA];
9595 it_before = *it;
9597 PRODUCE_GLYPHS (it);
9599 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9600 i = 0;
9601 x = it_before.current_x;
9602 while (i < nglyphs)
9604 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9606 if (x + glyph->pixel_width > max_x)
9608 /* Glyph doesn't fit on line. Backtrack. */
9609 row->used[TEXT_AREA] = n_glyphs_before;
9610 *it = it_before;
9611 goto out;
9614 ++it->hpos;
9615 x += glyph->pixel_width;
9616 ++i;
9619 /* Stop at line ends. */
9620 if (ITERATOR_AT_END_OF_LINE_P (it))
9621 break;
9623 set_iterator_to_next (it, 1);
9626 out:;
9628 row->displays_text_p = row->used[TEXT_AREA] != 0;
9629 /* Use default face for the border below the tool bar. */
9630 if (!row->displays_text_p)
9631 it->face_id = DEFAULT_FACE_ID;
9632 extend_face_to_end_of_line (it);
9633 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9634 last->right_box_line_p = 1;
9635 if (last == row->glyphs[TEXT_AREA])
9636 last->left_box_line_p = 1;
9638 /* Make line the desired height and center it vertically. */
9639 if ((height -= it->max_ascent + it->max_descent) > 0)
9641 /* Don't add more than one line height. */
9642 height %= FRAME_LINE_HEIGHT (it->f);
9643 it->max_ascent += height / 2;
9644 it->max_descent += (height + 1) / 2;
9647 compute_line_metrics (it);
9649 /* If line is empty, make it occupy the rest of the tool-bar. */
9650 if (!row->displays_text_p)
9652 row->height = row->phys_height = it->last_visible_y - row->y;
9653 row->ascent = row->phys_ascent = 0;
9654 row->extra_line_spacing = 0;
9657 row->full_width_p = 1;
9658 row->continued_p = 0;
9659 row->truncated_on_left_p = 0;
9660 row->truncated_on_right_p = 0;
9662 it->current_x = it->hpos = 0;
9663 it->current_y += row->height;
9664 ++it->vpos;
9665 ++it->glyph_row;
9669 /* Value is the number of screen lines needed to make all tool-bar
9670 items of frame F visible. The number of actual rows needed is
9671 returned in *N_ROWS if non-NULL. */
9673 static int
9674 tool_bar_lines_needed (f, n_rows)
9675 struct frame *f;
9676 int *n_rows;
9678 struct window *w = XWINDOW (f->tool_bar_window);
9679 struct it it;
9680 struct glyph_row *temp_row = w->desired_matrix->rows;
9682 /* Initialize an iterator for iteration over
9683 F->desired_tool_bar_string in the tool-bar window of frame F. */
9684 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9685 it.first_visible_x = 0;
9686 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9687 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9689 while (!ITERATOR_AT_END_P (&it))
9691 clear_glyph_row (temp_row);
9692 it.glyph_row = temp_row;
9693 display_tool_bar_line (&it, -1);
9695 clear_glyph_row (temp_row);
9697 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9698 if (n_rows)
9699 *n_rows = it.vpos > 0 ? it.vpos : -1;
9701 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9705 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9706 0, 1, 0,
9707 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9708 (frame)
9709 Lisp_Object frame;
9711 struct frame *f;
9712 struct window *w;
9713 int nlines = 0;
9715 if (NILP (frame))
9716 frame = selected_frame;
9717 else
9718 CHECK_FRAME (frame);
9719 f = XFRAME (frame);
9721 if (WINDOWP (f->tool_bar_window)
9722 || (w = XWINDOW (f->tool_bar_window),
9723 WINDOW_TOTAL_LINES (w) > 0))
9725 update_tool_bar (f, 1);
9726 if (f->n_tool_bar_items)
9728 build_desired_tool_bar_string (f);
9729 nlines = tool_bar_lines_needed (f, NULL);
9733 return make_number (nlines);
9737 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9738 height should be changed. */
9740 static int
9741 redisplay_tool_bar (f)
9742 struct frame *f;
9744 struct window *w;
9745 struct it it;
9746 struct glyph_row *row;
9747 int change_height_p = 0;
9749 #ifdef USE_GTK
9750 if (FRAME_EXTERNAL_TOOL_BAR (f))
9751 update_frame_tool_bar (f);
9752 return 0;
9753 #endif
9755 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9756 do anything. This means you must start with tool-bar-lines
9757 non-zero to get the auto-sizing effect. Or in other words, you
9758 can turn off tool-bars by specifying tool-bar-lines zero. */
9759 if (!WINDOWP (f->tool_bar_window)
9760 || (w = XWINDOW (f->tool_bar_window),
9761 WINDOW_TOTAL_LINES (w) == 0))
9762 return 0;
9764 /* Set up an iterator for the tool-bar window. */
9765 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9766 it.first_visible_x = 0;
9767 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9768 row = it.glyph_row;
9770 /* Build a string that represents the contents of the tool-bar. */
9771 build_desired_tool_bar_string (f);
9772 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9774 if (f->n_tool_bar_rows == 0)
9776 int nlines;
9778 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9779 nlines != WINDOW_TOTAL_LINES (w)))
9781 extern Lisp_Object Qtool_bar_lines;
9782 Lisp_Object frame;
9783 int old_height = WINDOW_TOTAL_LINES (w);
9785 XSETFRAME (frame, f);
9786 clear_glyph_matrix (w->desired_matrix);
9787 Fmodify_frame_parameters (frame,
9788 Fcons (Fcons (Qtool_bar_lines,
9789 make_number (nlines)),
9790 Qnil));
9791 if (WINDOW_TOTAL_LINES (w) != old_height)
9793 fonts_changed_p = 1;
9794 return 1;
9799 /* Display as many lines as needed to display all tool-bar items. */
9801 if (f->n_tool_bar_rows > 0)
9803 int border, rows, height, extra;
9805 if (INTEGERP (Vtool_bar_border))
9806 border = XINT (Vtool_bar_border);
9807 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9808 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9809 else if (EQ (Vtool_bar_border, Qborder_width))
9810 border = f->border_width;
9811 else
9812 border = 0;
9813 if (border < 0)
9814 border = 0;
9816 rows = f->n_tool_bar_rows;
9817 height = max (1, (it.last_visible_y - border) / rows);
9818 extra = it.last_visible_y - border - height * rows;
9820 while (it.current_y < it.last_visible_y)
9822 int h = 0;
9823 if (extra > 0 && rows-- > 0)
9825 h = (extra + rows - 1) / rows;
9826 extra -= h;
9828 display_tool_bar_line (&it, height + h);
9831 else
9833 while (it.current_y < it.last_visible_y)
9834 display_tool_bar_line (&it, 0);
9837 /* It doesn't make much sense to try scrolling in the tool-bar
9838 window, so don't do it. */
9839 w->desired_matrix->no_scrolling_p = 1;
9840 w->must_be_updated_p = 1;
9842 if (auto_resize_tool_bars_p)
9844 int nlines;
9846 /* If we couldn't display everything, change the tool-bar's
9847 height. */
9848 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9849 change_height_p = 1;
9851 /* If there are blank lines at the end, except for a partially
9852 visible blank line at the end that is smaller than
9853 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9854 row = it.glyph_row - 1;
9855 if (!row->displays_text_p
9856 && row->height >= FRAME_LINE_HEIGHT (f))
9857 change_height_p = 1;
9859 /* If row displays tool-bar items, but is partially visible,
9860 change the tool-bar's height. */
9861 if (row->displays_text_p
9862 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9863 change_height_p = 1;
9865 /* Resize windows as needed by changing the `tool-bar-lines'
9866 frame parameter. */
9867 if (change_height_p
9868 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9869 nlines != WINDOW_TOTAL_LINES (w)))
9871 extern Lisp_Object Qtool_bar_lines;
9872 Lisp_Object frame;
9873 int old_height = WINDOW_TOTAL_LINES (w);
9875 XSETFRAME (frame, f);
9876 clear_glyph_matrix (w->desired_matrix);
9877 Fmodify_frame_parameters (frame,
9878 Fcons (Fcons (Qtool_bar_lines,
9879 make_number (nlines)),
9880 Qnil));
9881 if (WINDOW_TOTAL_LINES (w) != old_height)
9882 fonts_changed_p = 1;
9886 return change_height_p;
9890 /* Get information about the tool-bar item which is displayed in GLYPH
9891 on frame F. Return in *PROP_IDX the index where tool-bar item
9892 properties start in F->tool_bar_items. Value is zero if
9893 GLYPH doesn't display a tool-bar item. */
9895 static int
9896 tool_bar_item_info (f, glyph, prop_idx)
9897 struct frame *f;
9898 struct glyph *glyph;
9899 int *prop_idx;
9901 Lisp_Object prop;
9902 int success_p;
9903 int charpos;
9905 /* This function can be called asynchronously, which means we must
9906 exclude any possibility that Fget_text_property signals an
9907 error. */
9908 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9909 charpos = max (0, charpos);
9911 /* Get the text property `menu-item' at pos. The value of that
9912 property is the start index of this item's properties in
9913 F->tool_bar_items. */
9914 prop = Fget_text_property (make_number (charpos),
9915 Qmenu_item, f->current_tool_bar_string);
9916 if (INTEGERP (prop))
9918 *prop_idx = XINT (prop);
9919 success_p = 1;
9921 else
9922 success_p = 0;
9924 return success_p;
9928 /* Get information about the tool-bar item at position X/Y on frame F.
9929 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9930 the current matrix of the tool-bar window of F, or NULL if not
9931 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9932 item in F->tool_bar_items. Value is
9934 -1 if X/Y is not on a tool-bar item
9935 0 if X/Y is on the same item that was highlighted before.
9936 1 otherwise. */
9938 static int
9939 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9940 struct frame *f;
9941 int x, y;
9942 struct glyph **glyph;
9943 int *hpos, *vpos, *prop_idx;
9945 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9946 struct window *w = XWINDOW (f->tool_bar_window);
9947 int area;
9949 /* Find the glyph under X/Y. */
9950 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9951 if (*glyph == NULL)
9952 return -1;
9954 /* Get the start of this tool-bar item's properties in
9955 f->tool_bar_items. */
9956 if (!tool_bar_item_info (f, *glyph, prop_idx))
9957 return -1;
9959 /* Is mouse on the highlighted item? */
9960 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9961 && *vpos >= dpyinfo->mouse_face_beg_row
9962 && *vpos <= dpyinfo->mouse_face_end_row
9963 && (*vpos > dpyinfo->mouse_face_beg_row
9964 || *hpos >= dpyinfo->mouse_face_beg_col)
9965 && (*vpos < dpyinfo->mouse_face_end_row
9966 || *hpos < dpyinfo->mouse_face_end_col
9967 || dpyinfo->mouse_face_past_end))
9968 return 0;
9970 return 1;
9974 /* EXPORT:
9975 Handle mouse button event on the tool-bar of frame F, at
9976 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9977 0 for button release. MODIFIERS is event modifiers for button
9978 release. */
9980 void
9981 handle_tool_bar_click (f, x, y, down_p, modifiers)
9982 struct frame *f;
9983 int x, y, down_p;
9984 unsigned int modifiers;
9986 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9987 struct window *w = XWINDOW (f->tool_bar_window);
9988 int hpos, vpos, prop_idx;
9989 struct glyph *glyph;
9990 Lisp_Object enabled_p;
9992 /* If not on the highlighted tool-bar item, return. */
9993 frame_to_window_pixel_xy (w, &x, &y);
9994 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9995 return;
9997 /* If item is disabled, do nothing. */
9998 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9999 if (NILP (enabled_p))
10000 return;
10002 if (down_p)
10004 /* Show item in pressed state. */
10005 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10006 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10007 last_tool_bar_item = prop_idx;
10009 else
10011 Lisp_Object key, frame;
10012 struct input_event event;
10013 EVENT_INIT (event);
10015 /* Show item in released state. */
10016 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10017 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10019 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10021 XSETFRAME (frame, f);
10022 event.kind = TOOL_BAR_EVENT;
10023 event.frame_or_window = frame;
10024 event.arg = frame;
10025 kbd_buffer_store_event (&event);
10027 event.kind = TOOL_BAR_EVENT;
10028 event.frame_or_window = frame;
10029 event.arg = key;
10030 event.modifiers = modifiers;
10031 kbd_buffer_store_event (&event);
10032 last_tool_bar_item = -1;
10037 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10038 tool-bar window-relative coordinates X/Y. Called from
10039 note_mouse_highlight. */
10041 static void
10042 note_tool_bar_highlight (f, x, y)
10043 struct frame *f;
10044 int x, y;
10046 Lisp_Object window = f->tool_bar_window;
10047 struct window *w = XWINDOW (window);
10048 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10049 int hpos, vpos;
10050 struct glyph *glyph;
10051 struct glyph_row *row;
10052 int i;
10053 Lisp_Object enabled_p;
10054 int prop_idx;
10055 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10056 int mouse_down_p, rc;
10058 /* Function note_mouse_highlight is called with negative x(y
10059 values when mouse moves outside of the frame. */
10060 if (x <= 0 || y <= 0)
10062 clear_mouse_face (dpyinfo);
10063 return;
10066 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10067 if (rc < 0)
10069 /* Not on tool-bar item. */
10070 clear_mouse_face (dpyinfo);
10071 return;
10073 else if (rc == 0)
10074 /* On same tool-bar item as before. */
10075 goto set_help_echo;
10077 clear_mouse_face (dpyinfo);
10079 /* Mouse is down, but on different tool-bar item? */
10080 mouse_down_p = (dpyinfo->grabbed
10081 && f == last_mouse_frame
10082 && FRAME_LIVE_P (f));
10083 if (mouse_down_p
10084 && last_tool_bar_item != prop_idx)
10085 return;
10087 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10088 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10090 /* If tool-bar item is not enabled, don't highlight it. */
10091 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10092 if (!NILP (enabled_p))
10094 /* Compute the x-position of the glyph. In front and past the
10095 image is a space. We include this in the highlighted area. */
10096 row = MATRIX_ROW (w->current_matrix, vpos);
10097 for (i = x = 0; i < hpos; ++i)
10098 x += row->glyphs[TEXT_AREA][i].pixel_width;
10100 /* Record this as the current active region. */
10101 dpyinfo->mouse_face_beg_col = hpos;
10102 dpyinfo->mouse_face_beg_row = vpos;
10103 dpyinfo->mouse_face_beg_x = x;
10104 dpyinfo->mouse_face_beg_y = row->y;
10105 dpyinfo->mouse_face_past_end = 0;
10107 dpyinfo->mouse_face_end_col = hpos + 1;
10108 dpyinfo->mouse_face_end_row = vpos;
10109 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10110 dpyinfo->mouse_face_end_y = row->y;
10111 dpyinfo->mouse_face_window = window;
10112 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10114 /* Display it as active. */
10115 show_mouse_face (dpyinfo, draw);
10116 dpyinfo->mouse_face_image_state = draw;
10119 set_help_echo:
10121 /* Set help_echo_string to a help string to display for this tool-bar item.
10122 XTread_socket does the rest. */
10123 help_echo_object = help_echo_window = Qnil;
10124 help_echo_pos = -1;
10125 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10126 if (NILP (help_echo_string))
10127 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10130 #endif /* HAVE_WINDOW_SYSTEM */
10134 /************************************************************************
10135 Horizontal scrolling
10136 ************************************************************************/
10138 static int hscroll_window_tree P_ ((Lisp_Object));
10139 static int hscroll_windows P_ ((Lisp_Object));
10141 /* For all leaf windows in the window tree rooted at WINDOW, set their
10142 hscroll value so that PT is (i) visible in the window, and (ii) so
10143 that it is not within a certain margin at the window's left and
10144 right border. Value is non-zero if any window's hscroll has been
10145 changed. */
10147 static int
10148 hscroll_window_tree (window)
10149 Lisp_Object window;
10151 int hscrolled_p = 0;
10152 int hscroll_relative_p = FLOATP (Vhscroll_step);
10153 int hscroll_step_abs = 0;
10154 double hscroll_step_rel = 0;
10156 if (hscroll_relative_p)
10158 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10159 if (hscroll_step_rel < 0)
10161 hscroll_relative_p = 0;
10162 hscroll_step_abs = 0;
10165 else if (INTEGERP (Vhscroll_step))
10167 hscroll_step_abs = XINT (Vhscroll_step);
10168 if (hscroll_step_abs < 0)
10169 hscroll_step_abs = 0;
10171 else
10172 hscroll_step_abs = 0;
10174 while (WINDOWP (window))
10176 struct window *w = XWINDOW (window);
10178 if (WINDOWP (w->hchild))
10179 hscrolled_p |= hscroll_window_tree (w->hchild);
10180 else if (WINDOWP (w->vchild))
10181 hscrolled_p |= hscroll_window_tree (w->vchild);
10182 else if (w->cursor.vpos >= 0)
10184 int h_margin;
10185 int text_area_width;
10186 struct glyph_row *current_cursor_row
10187 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10188 struct glyph_row *desired_cursor_row
10189 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10190 struct glyph_row *cursor_row
10191 = (desired_cursor_row->enabled_p
10192 ? desired_cursor_row
10193 : current_cursor_row);
10195 text_area_width = window_box_width (w, TEXT_AREA);
10197 /* Scroll when cursor is inside this scroll margin. */
10198 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10200 if ((XFASTINT (w->hscroll)
10201 && w->cursor.x <= h_margin)
10202 || (cursor_row->enabled_p
10203 && cursor_row->truncated_on_right_p
10204 && (w->cursor.x >= text_area_width - h_margin)))
10206 struct it it;
10207 int hscroll;
10208 struct buffer *saved_current_buffer;
10209 int pt;
10210 int wanted_x;
10212 /* Find point in a display of infinite width. */
10213 saved_current_buffer = current_buffer;
10214 current_buffer = XBUFFER (w->buffer);
10216 if (w == XWINDOW (selected_window))
10217 pt = BUF_PT (current_buffer);
10218 else
10220 pt = marker_position (w->pointm);
10221 pt = max (BEGV, pt);
10222 pt = min (ZV, pt);
10225 /* Move iterator to pt starting at cursor_row->start in
10226 a line with infinite width. */
10227 init_to_row_start (&it, w, cursor_row);
10228 it.last_visible_x = INFINITY;
10229 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10230 current_buffer = saved_current_buffer;
10232 /* Position cursor in window. */
10233 if (!hscroll_relative_p && hscroll_step_abs == 0)
10234 hscroll = max (0, (it.current_x
10235 - (ITERATOR_AT_END_OF_LINE_P (&it)
10236 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10237 : (text_area_width / 2))))
10238 / FRAME_COLUMN_WIDTH (it.f);
10239 else if (w->cursor.x >= text_area_width - h_margin)
10241 if (hscroll_relative_p)
10242 wanted_x = text_area_width * (1 - hscroll_step_rel)
10243 - h_margin;
10244 else
10245 wanted_x = text_area_width
10246 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10247 - h_margin;
10248 hscroll
10249 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10251 else
10253 if (hscroll_relative_p)
10254 wanted_x = text_area_width * hscroll_step_rel
10255 + h_margin;
10256 else
10257 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10258 + h_margin;
10259 hscroll
10260 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10262 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10264 /* Don't call Fset_window_hscroll if value hasn't
10265 changed because it will prevent redisplay
10266 optimizations. */
10267 if (XFASTINT (w->hscroll) != hscroll)
10269 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10270 w->hscroll = make_number (hscroll);
10271 hscrolled_p = 1;
10276 window = w->next;
10279 /* Value is non-zero if hscroll of any leaf window has been changed. */
10280 return hscrolled_p;
10284 /* Set hscroll so that cursor is visible and not inside horizontal
10285 scroll margins for all windows in the tree rooted at WINDOW. See
10286 also hscroll_window_tree above. Value is non-zero if any window's
10287 hscroll has been changed. If it has, desired matrices on the frame
10288 of WINDOW are cleared. */
10290 static int
10291 hscroll_windows (window)
10292 Lisp_Object window;
10294 int hscrolled_p;
10296 if (automatic_hscrolling_p)
10298 hscrolled_p = hscroll_window_tree (window);
10299 if (hscrolled_p)
10300 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10302 else
10303 hscrolled_p = 0;
10304 return hscrolled_p;
10309 /************************************************************************
10310 Redisplay
10311 ************************************************************************/
10313 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10314 to a non-zero value. This is sometimes handy to have in a debugger
10315 session. */
10317 #if GLYPH_DEBUG
10319 /* First and last unchanged row for try_window_id. */
10321 int debug_first_unchanged_at_end_vpos;
10322 int debug_last_unchanged_at_beg_vpos;
10324 /* Delta vpos and y. */
10326 int debug_dvpos, debug_dy;
10328 /* Delta in characters and bytes for try_window_id. */
10330 int debug_delta, debug_delta_bytes;
10332 /* Values of window_end_pos and window_end_vpos at the end of
10333 try_window_id. */
10335 EMACS_INT debug_end_pos, debug_end_vpos;
10337 /* Append a string to W->desired_matrix->method. FMT is a printf
10338 format string. A1...A9 are a supplement for a variable-length
10339 argument list. If trace_redisplay_p is non-zero also printf the
10340 resulting string to stderr. */
10342 static void
10343 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10344 struct window *w;
10345 char *fmt;
10346 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10348 char buffer[512];
10349 char *method = w->desired_matrix->method;
10350 int len = strlen (method);
10351 int size = sizeof w->desired_matrix->method;
10352 int remaining = size - len - 1;
10354 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10355 if (len && remaining)
10357 method[len] = '|';
10358 --remaining, ++len;
10361 strncpy (method + len, buffer, remaining);
10363 if (trace_redisplay_p)
10364 fprintf (stderr, "%p (%s): %s\n",
10366 ((BUFFERP (w->buffer)
10367 && STRINGP (XBUFFER (w->buffer)->name))
10368 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10369 : "no buffer"),
10370 buffer);
10373 #endif /* GLYPH_DEBUG */
10376 /* Value is non-zero if all changes in window W, which displays
10377 current_buffer, are in the text between START and END. START is a
10378 buffer position, END is given as a distance from Z. Used in
10379 redisplay_internal for display optimization. */
10381 static INLINE int
10382 text_outside_line_unchanged_p (w, start, end)
10383 struct window *w;
10384 int start, end;
10386 int unchanged_p = 1;
10388 /* If text or overlays have changed, see where. */
10389 if (XFASTINT (w->last_modified) < MODIFF
10390 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10392 /* Gap in the line? */
10393 if (GPT < start || Z - GPT < end)
10394 unchanged_p = 0;
10396 /* Changes start in front of the line, or end after it? */
10397 if (unchanged_p
10398 && (BEG_UNCHANGED < start - 1
10399 || END_UNCHANGED < end))
10400 unchanged_p = 0;
10402 /* If selective display, can't optimize if changes start at the
10403 beginning of the line. */
10404 if (unchanged_p
10405 && INTEGERP (current_buffer->selective_display)
10406 && XINT (current_buffer->selective_display) > 0
10407 && (BEG_UNCHANGED < start || GPT <= start))
10408 unchanged_p = 0;
10410 /* If there are overlays at the start or end of the line, these
10411 may have overlay strings with newlines in them. A change at
10412 START, for instance, may actually concern the display of such
10413 overlay strings as well, and they are displayed on different
10414 lines. So, quickly rule out this case. (For the future, it
10415 might be desirable to implement something more telling than
10416 just BEG/END_UNCHANGED.) */
10417 if (unchanged_p)
10419 if (BEG + BEG_UNCHANGED == start
10420 && overlay_touches_p (start))
10421 unchanged_p = 0;
10422 if (END_UNCHANGED == end
10423 && overlay_touches_p (Z - end))
10424 unchanged_p = 0;
10428 return unchanged_p;
10432 /* Do a frame update, taking possible shortcuts into account. This is
10433 the main external entry point for redisplay.
10435 If the last redisplay displayed an echo area message and that message
10436 is no longer requested, we clear the echo area or bring back the
10437 mini-buffer if that is in use. */
10439 void
10440 redisplay ()
10442 redisplay_internal (0);
10446 static Lisp_Object
10447 overlay_arrow_string_or_property (var)
10448 Lisp_Object var;
10450 Lisp_Object val;
10452 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10453 return val;
10455 return Voverlay_arrow_string;
10458 /* Return 1 if there are any overlay-arrows in current_buffer. */
10459 static int
10460 overlay_arrow_in_current_buffer_p ()
10462 Lisp_Object vlist;
10464 for (vlist = Voverlay_arrow_variable_list;
10465 CONSP (vlist);
10466 vlist = XCDR (vlist))
10468 Lisp_Object var = XCAR (vlist);
10469 Lisp_Object val;
10471 if (!SYMBOLP (var))
10472 continue;
10473 val = find_symbol_value (var);
10474 if (MARKERP (val)
10475 && current_buffer == XMARKER (val)->buffer)
10476 return 1;
10478 return 0;
10482 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10483 has changed. */
10485 static int
10486 overlay_arrows_changed_p ()
10488 Lisp_Object vlist;
10490 for (vlist = Voverlay_arrow_variable_list;
10491 CONSP (vlist);
10492 vlist = XCDR (vlist))
10494 Lisp_Object var = XCAR (vlist);
10495 Lisp_Object val, pstr;
10497 if (!SYMBOLP (var))
10498 continue;
10499 val = find_symbol_value (var);
10500 if (!MARKERP (val))
10501 continue;
10502 if (! EQ (COERCE_MARKER (val),
10503 Fget (var, Qlast_arrow_position))
10504 || ! (pstr = overlay_arrow_string_or_property (var),
10505 EQ (pstr, Fget (var, Qlast_arrow_string))))
10506 return 1;
10508 return 0;
10511 /* Mark overlay arrows to be updated on next redisplay. */
10513 static void
10514 update_overlay_arrows (up_to_date)
10515 int up_to_date;
10517 Lisp_Object vlist;
10519 for (vlist = Voverlay_arrow_variable_list;
10520 CONSP (vlist);
10521 vlist = XCDR (vlist))
10523 Lisp_Object var = XCAR (vlist);
10525 if (!SYMBOLP (var))
10526 continue;
10528 if (up_to_date > 0)
10530 Lisp_Object val = find_symbol_value (var);
10531 Fput (var, Qlast_arrow_position,
10532 COERCE_MARKER (val));
10533 Fput (var, Qlast_arrow_string,
10534 overlay_arrow_string_or_property (var));
10536 else if (up_to_date < 0
10537 || !NILP (Fget (var, Qlast_arrow_position)))
10539 Fput (var, Qlast_arrow_position, Qt);
10540 Fput (var, Qlast_arrow_string, Qt);
10546 /* Return overlay arrow string to display at row.
10547 Return integer (bitmap number) for arrow bitmap in left fringe.
10548 Return nil if no overlay arrow. */
10550 static Lisp_Object
10551 overlay_arrow_at_row (it, row)
10552 struct it *it;
10553 struct glyph_row *row;
10555 Lisp_Object vlist;
10557 for (vlist = Voverlay_arrow_variable_list;
10558 CONSP (vlist);
10559 vlist = XCDR (vlist))
10561 Lisp_Object var = XCAR (vlist);
10562 Lisp_Object val;
10564 if (!SYMBOLP (var))
10565 continue;
10567 val = find_symbol_value (var);
10569 if (MARKERP (val)
10570 && current_buffer == XMARKER (val)->buffer
10571 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10573 if (FRAME_WINDOW_P (it->f)
10574 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10576 #ifdef HAVE_WINDOW_SYSTEM
10577 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10579 int fringe_bitmap;
10580 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10581 return make_number (fringe_bitmap);
10583 #endif
10584 return make_number (-1); /* Use default arrow bitmap */
10586 return overlay_arrow_string_or_property (var);
10590 return Qnil;
10593 /* Return 1 if point moved out of or into a composition. Otherwise
10594 return 0. PREV_BUF and PREV_PT are the last point buffer and
10595 position. BUF and PT are the current point buffer and position. */
10598 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10599 struct buffer *prev_buf, *buf;
10600 int prev_pt, pt;
10602 int start, end;
10603 Lisp_Object prop;
10604 Lisp_Object buffer;
10606 XSETBUFFER (buffer, buf);
10607 /* Check a composition at the last point if point moved within the
10608 same buffer. */
10609 if (prev_buf == buf)
10611 if (prev_pt == pt)
10612 /* Point didn't move. */
10613 return 0;
10615 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10616 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10617 && COMPOSITION_VALID_P (start, end, prop)
10618 && start < prev_pt && end > prev_pt)
10619 /* The last point was within the composition. Return 1 iff
10620 point moved out of the composition. */
10621 return (pt <= start || pt >= end);
10624 /* Check a composition at the current point. */
10625 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10626 && find_composition (pt, -1, &start, &end, &prop, buffer)
10627 && COMPOSITION_VALID_P (start, end, prop)
10628 && start < pt && end > pt);
10632 /* Reconsider the setting of B->clip_changed which is displayed
10633 in window W. */
10635 static INLINE void
10636 reconsider_clip_changes (w, b)
10637 struct window *w;
10638 struct buffer *b;
10640 if (b->clip_changed
10641 && !NILP (w->window_end_valid)
10642 && w->current_matrix->buffer == b
10643 && w->current_matrix->zv == BUF_ZV (b)
10644 && w->current_matrix->begv == BUF_BEGV (b))
10645 b->clip_changed = 0;
10647 /* If display wasn't paused, and W is not a tool bar window, see if
10648 point has been moved into or out of a composition. In that case,
10649 we set b->clip_changed to 1 to force updating the screen. If
10650 b->clip_changed has already been set to 1, we can skip this
10651 check. */
10652 if (!b->clip_changed
10653 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10655 int pt;
10657 if (w == XWINDOW (selected_window))
10658 pt = BUF_PT (current_buffer);
10659 else
10660 pt = marker_position (w->pointm);
10662 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10663 || pt != XINT (w->last_point))
10664 && check_point_in_composition (w->current_matrix->buffer,
10665 XINT (w->last_point),
10666 XBUFFER (w->buffer), pt))
10667 b->clip_changed = 1;
10672 /* Select FRAME to forward the values of frame-local variables into C
10673 variables so that the redisplay routines can access those values
10674 directly. */
10676 static void
10677 select_frame_for_redisplay (frame)
10678 Lisp_Object frame;
10680 Lisp_Object tail, sym, val;
10681 Lisp_Object old = selected_frame;
10683 selected_frame = frame;
10685 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10686 if (CONSP (XCAR (tail))
10687 && (sym = XCAR (XCAR (tail)),
10688 SYMBOLP (sym))
10689 && (sym = indirect_variable (sym),
10690 val = SYMBOL_VALUE (sym),
10691 (BUFFER_LOCAL_VALUEP (val)
10692 || SOME_BUFFER_LOCAL_VALUEP (val)))
10693 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10694 /* Use find_symbol_value rather than Fsymbol_value
10695 to avoid an error if it is void. */
10696 find_symbol_value (sym);
10698 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10699 if (CONSP (XCAR (tail))
10700 && (sym = XCAR (XCAR (tail)),
10701 SYMBOLP (sym))
10702 && (sym = indirect_variable (sym),
10703 val = SYMBOL_VALUE (sym),
10704 (BUFFER_LOCAL_VALUEP (val)
10705 || SOME_BUFFER_LOCAL_VALUEP (val)))
10706 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10707 find_symbol_value (sym);
10711 #define STOP_POLLING \
10712 do { if (! polling_stopped_here) stop_polling (); \
10713 polling_stopped_here = 1; } while (0)
10715 #define RESUME_POLLING \
10716 do { if (polling_stopped_here) start_polling (); \
10717 polling_stopped_here = 0; } while (0)
10720 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10721 response to any user action; therefore, we should preserve the echo
10722 area. (Actually, our caller does that job.) Perhaps in the future
10723 avoid recentering windows if it is not necessary; currently that
10724 causes some problems. */
10726 static void
10727 redisplay_internal (preserve_echo_area)
10728 int preserve_echo_area;
10730 struct window *w = XWINDOW (selected_window);
10731 struct frame *f = XFRAME (w->frame);
10732 int pause;
10733 int must_finish = 0;
10734 struct text_pos tlbufpos, tlendpos;
10735 int number_of_visible_frames;
10736 int count;
10737 struct frame *sf = SELECTED_FRAME ();
10738 int polling_stopped_here = 0;
10740 /* Non-zero means redisplay has to consider all windows on all
10741 frames. Zero means, only selected_window is considered. */
10742 int consider_all_windows_p;
10744 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10746 /* No redisplay if running in batch mode or frame is not yet fully
10747 initialized, or redisplay is explicitly turned off by setting
10748 Vinhibit_redisplay. */
10749 if (noninteractive
10750 || !NILP (Vinhibit_redisplay)
10751 || !f->glyphs_initialized_p)
10752 return;
10754 /* The flag redisplay_performed_directly_p is set by
10755 direct_output_for_insert when it already did the whole screen
10756 update necessary. */
10757 if (redisplay_performed_directly_p)
10759 redisplay_performed_directly_p = 0;
10760 if (!hscroll_windows (selected_window))
10761 return;
10764 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10765 if (popup_activated ())
10766 return;
10767 #endif
10769 /* I don't think this happens but let's be paranoid. */
10770 if (redisplaying_p)
10771 return;
10773 /* Record a function that resets redisplaying_p to its old value
10774 when we leave this function. */
10775 count = SPECPDL_INDEX ();
10776 record_unwind_protect (unwind_redisplay,
10777 Fcons (make_number (redisplaying_p), selected_frame));
10778 ++redisplaying_p;
10779 specbind (Qinhibit_free_realized_faces, Qnil);
10782 Lisp_Object tail, frame;
10784 FOR_EACH_FRAME (tail, frame)
10786 struct frame *f = XFRAME (frame);
10787 f->already_hscrolled_p = 0;
10791 retry:
10792 pause = 0;
10793 reconsider_clip_changes (w, current_buffer);
10794 last_escape_glyph_frame = NULL;
10795 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10797 /* If new fonts have been loaded that make a glyph matrix adjustment
10798 necessary, do it. */
10799 if (fonts_changed_p)
10801 adjust_glyphs (NULL);
10802 ++windows_or_buffers_changed;
10803 fonts_changed_p = 0;
10806 /* If face_change_count is non-zero, init_iterator will free all
10807 realized faces, which includes the faces referenced from current
10808 matrices. So, we can't reuse current matrices in this case. */
10809 if (face_change_count)
10810 ++windows_or_buffers_changed;
10812 if (! FRAME_WINDOW_P (sf)
10813 && previous_terminal_frame != sf)
10815 /* Since frames on an ASCII terminal share the same display
10816 area, displaying a different frame means redisplay the whole
10817 thing. */
10818 windows_or_buffers_changed++;
10819 SET_FRAME_GARBAGED (sf);
10820 XSETFRAME (Vterminal_frame, sf);
10822 previous_terminal_frame = sf;
10824 /* Set the visible flags for all frames. Do this before checking
10825 for resized or garbaged frames; they want to know if their frames
10826 are visible. See the comment in frame.h for
10827 FRAME_SAMPLE_VISIBILITY. */
10829 Lisp_Object tail, frame;
10831 number_of_visible_frames = 0;
10833 FOR_EACH_FRAME (tail, frame)
10835 struct frame *f = XFRAME (frame);
10837 FRAME_SAMPLE_VISIBILITY (f);
10838 if (FRAME_VISIBLE_P (f))
10839 ++number_of_visible_frames;
10840 clear_desired_matrices (f);
10844 /* Notice any pending interrupt request to change frame size. */
10845 do_pending_window_change (1);
10847 /* Clear frames marked as garbaged. */
10848 if (frame_garbaged)
10849 clear_garbaged_frames ();
10851 /* Build menubar and tool-bar items. */
10852 if (NILP (Vmemory_full))
10853 prepare_menu_bars ();
10855 if (windows_or_buffers_changed)
10856 update_mode_lines++;
10858 /* Detect case that we need to write or remove a star in the mode line. */
10859 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10861 w->update_mode_line = Qt;
10862 if (buffer_shared > 1)
10863 update_mode_lines++;
10866 /* If %c is in the mode line, update it if needed. */
10867 if (!NILP (w->column_number_displayed)
10868 /* This alternative quickly identifies a common case
10869 where no change is needed. */
10870 && !(PT == XFASTINT (w->last_point)
10871 && XFASTINT (w->last_modified) >= MODIFF
10872 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10873 && (XFASTINT (w->column_number_displayed)
10874 != (int) current_column ())) /* iftc */
10875 w->update_mode_line = Qt;
10877 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10879 /* The variable buffer_shared is set in redisplay_window and
10880 indicates that we redisplay a buffer in different windows. See
10881 there. */
10882 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10883 || cursor_type_changed);
10885 /* If specs for an arrow have changed, do thorough redisplay
10886 to ensure we remove any arrow that should no longer exist. */
10887 if (overlay_arrows_changed_p ())
10888 consider_all_windows_p = windows_or_buffers_changed = 1;
10890 /* Normally the message* functions will have already displayed and
10891 updated the echo area, but the frame may have been trashed, or
10892 the update may have been preempted, so display the echo area
10893 again here. Checking message_cleared_p captures the case that
10894 the echo area should be cleared. */
10895 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10896 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10897 || (message_cleared_p
10898 && minibuf_level == 0
10899 /* If the mini-window is currently selected, this means the
10900 echo-area doesn't show through. */
10901 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10903 int window_height_changed_p = echo_area_display (0);
10904 must_finish = 1;
10906 /* If we don't display the current message, don't clear the
10907 message_cleared_p flag, because, if we did, we wouldn't clear
10908 the echo area in the next redisplay which doesn't preserve
10909 the echo area. */
10910 if (!display_last_displayed_message_p)
10911 message_cleared_p = 0;
10913 if (fonts_changed_p)
10914 goto retry;
10915 else if (window_height_changed_p)
10917 consider_all_windows_p = 1;
10918 ++update_mode_lines;
10919 ++windows_or_buffers_changed;
10921 /* If window configuration was changed, frames may have been
10922 marked garbaged. Clear them or we will experience
10923 surprises wrt scrolling. */
10924 if (frame_garbaged)
10925 clear_garbaged_frames ();
10928 else if (EQ (selected_window, minibuf_window)
10929 && (current_buffer->clip_changed
10930 || XFASTINT (w->last_modified) < MODIFF
10931 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10932 && resize_mini_window (w, 0))
10934 /* Resized active mini-window to fit the size of what it is
10935 showing if its contents might have changed. */
10936 must_finish = 1;
10937 consider_all_windows_p = 1;
10938 ++windows_or_buffers_changed;
10939 ++update_mode_lines;
10941 /* If window configuration was changed, frames may have been
10942 marked garbaged. Clear them or we will experience
10943 surprises wrt scrolling. */
10944 if (frame_garbaged)
10945 clear_garbaged_frames ();
10949 /* If showing the region, and mark has changed, we must redisplay
10950 the whole window. The assignment to this_line_start_pos prevents
10951 the optimization directly below this if-statement. */
10952 if (((!NILP (Vtransient_mark_mode)
10953 && !NILP (XBUFFER (w->buffer)->mark_active))
10954 != !NILP (w->region_showing))
10955 || (!NILP (w->region_showing)
10956 && !EQ (w->region_showing,
10957 Fmarker_position (XBUFFER (w->buffer)->mark))))
10958 CHARPOS (this_line_start_pos) = 0;
10960 /* Optimize the case that only the line containing the cursor in the
10961 selected window has changed. Variables starting with this_ are
10962 set in display_line and record information about the line
10963 containing the cursor. */
10964 tlbufpos = this_line_start_pos;
10965 tlendpos = this_line_end_pos;
10966 if (!consider_all_windows_p
10967 && CHARPOS (tlbufpos) > 0
10968 && NILP (w->update_mode_line)
10969 && !current_buffer->clip_changed
10970 && !current_buffer->prevent_redisplay_optimizations_p
10971 && FRAME_VISIBLE_P (XFRAME (w->frame))
10972 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10973 /* Make sure recorded data applies to current buffer, etc. */
10974 && this_line_buffer == current_buffer
10975 && current_buffer == XBUFFER (w->buffer)
10976 && NILP (w->force_start)
10977 && NILP (w->optional_new_start)
10978 /* Point must be on the line that we have info recorded about. */
10979 && PT >= CHARPOS (tlbufpos)
10980 && PT <= Z - CHARPOS (tlendpos)
10981 /* All text outside that line, including its final newline,
10982 must be unchanged */
10983 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10984 CHARPOS (tlendpos)))
10986 if (CHARPOS (tlbufpos) > BEGV
10987 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10988 && (CHARPOS (tlbufpos) == ZV
10989 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10990 /* Former continuation line has disappeared by becoming empty */
10991 goto cancel;
10992 else if (XFASTINT (w->last_modified) < MODIFF
10993 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10994 || MINI_WINDOW_P (w))
10996 /* We have to handle the case of continuation around a
10997 wide-column character (See the comment in indent.c around
10998 line 885).
11000 For instance, in the following case:
11002 -------- Insert --------
11003 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11004 J_I_ ==> J_I_ `^^' are cursors.
11005 ^^ ^^
11006 -------- --------
11008 As we have to redraw the line above, we should goto cancel. */
11010 struct it it;
11011 int line_height_before = this_line_pixel_height;
11013 /* Note that start_display will handle the case that the
11014 line starting at tlbufpos is a continuation lines. */
11015 start_display (&it, w, tlbufpos);
11017 /* Implementation note: It this still necessary? */
11018 if (it.current_x != this_line_start_x)
11019 goto cancel;
11021 TRACE ((stderr, "trying display optimization 1\n"));
11022 w->cursor.vpos = -1;
11023 overlay_arrow_seen = 0;
11024 it.vpos = this_line_vpos;
11025 it.current_y = this_line_y;
11026 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11027 display_line (&it);
11029 /* If line contains point, is not continued,
11030 and ends at same distance from eob as before, we win */
11031 if (w->cursor.vpos >= 0
11032 /* Line is not continued, otherwise this_line_start_pos
11033 would have been set to 0 in display_line. */
11034 && CHARPOS (this_line_start_pos)
11035 /* Line ends as before. */
11036 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11037 /* Line has same height as before. Otherwise other lines
11038 would have to be shifted up or down. */
11039 && this_line_pixel_height == line_height_before)
11041 /* If this is not the window's last line, we must adjust
11042 the charstarts of the lines below. */
11043 if (it.current_y < it.last_visible_y)
11045 struct glyph_row *row
11046 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11047 int delta, delta_bytes;
11049 if (Z - CHARPOS (tlendpos) == ZV)
11051 /* This line ends at end of (accessible part of)
11052 buffer. There is no newline to count. */
11053 delta = (Z
11054 - CHARPOS (tlendpos)
11055 - MATRIX_ROW_START_CHARPOS (row));
11056 delta_bytes = (Z_BYTE
11057 - BYTEPOS (tlendpos)
11058 - MATRIX_ROW_START_BYTEPOS (row));
11060 else
11062 /* This line ends in a newline. Must take
11063 account of the newline and the rest of the
11064 text that follows. */
11065 delta = (Z
11066 - CHARPOS (tlendpos)
11067 - MATRIX_ROW_START_CHARPOS (row));
11068 delta_bytes = (Z_BYTE
11069 - BYTEPOS (tlendpos)
11070 - MATRIX_ROW_START_BYTEPOS (row));
11073 increment_matrix_positions (w->current_matrix,
11074 this_line_vpos + 1,
11075 w->current_matrix->nrows,
11076 delta, delta_bytes);
11079 /* If this row displays text now but previously didn't,
11080 or vice versa, w->window_end_vpos may have to be
11081 adjusted. */
11082 if ((it.glyph_row - 1)->displays_text_p)
11084 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11085 XSETINT (w->window_end_vpos, this_line_vpos);
11087 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11088 && this_line_vpos > 0)
11089 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11090 w->window_end_valid = Qnil;
11092 /* Update hint: No need to try to scroll in update_window. */
11093 w->desired_matrix->no_scrolling_p = 1;
11095 #if GLYPH_DEBUG
11096 *w->desired_matrix->method = 0;
11097 debug_method_add (w, "optimization 1");
11098 #endif
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 update_window_fringes (w, 0);
11101 #endif
11102 goto update;
11104 else
11105 goto cancel;
11107 else if (/* Cursor position hasn't changed. */
11108 PT == XFASTINT (w->last_point)
11109 /* Make sure the cursor was last displayed
11110 in this window. Otherwise we have to reposition it. */
11111 && 0 <= w->cursor.vpos
11112 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11114 if (!must_finish)
11116 do_pending_window_change (1);
11118 /* We used to always goto end_of_redisplay here, but this
11119 isn't enough if we have a blinking cursor. */
11120 if (w->cursor_off_p == w->last_cursor_off_p)
11121 goto end_of_redisplay;
11123 goto update;
11125 /* If highlighting the region, or if the cursor is in the echo area,
11126 then we can't just move the cursor. */
11127 else if (! (!NILP (Vtransient_mark_mode)
11128 && !NILP (current_buffer->mark_active))
11129 && (EQ (selected_window, current_buffer->last_selected_window)
11130 || highlight_nonselected_windows)
11131 && NILP (w->region_showing)
11132 && NILP (Vshow_trailing_whitespace)
11133 && !cursor_in_echo_area)
11135 struct it it;
11136 struct glyph_row *row;
11138 /* Skip from tlbufpos to PT and see where it is. Note that
11139 PT may be in invisible text. If so, we will end at the
11140 next visible position. */
11141 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11142 NULL, DEFAULT_FACE_ID);
11143 it.current_x = this_line_start_x;
11144 it.current_y = this_line_y;
11145 it.vpos = this_line_vpos;
11147 /* The call to move_it_to stops in front of PT, but
11148 moves over before-strings. */
11149 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11151 if (it.vpos == this_line_vpos
11152 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11153 row->enabled_p))
11155 xassert (this_line_vpos == it.vpos);
11156 xassert (this_line_y == it.current_y);
11157 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11158 #if GLYPH_DEBUG
11159 *w->desired_matrix->method = 0;
11160 debug_method_add (w, "optimization 3");
11161 #endif
11162 goto update;
11164 else
11165 goto cancel;
11168 cancel:
11169 /* Text changed drastically or point moved off of line. */
11170 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11173 CHARPOS (this_line_start_pos) = 0;
11174 consider_all_windows_p |= buffer_shared > 1;
11175 ++clear_face_cache_count;
11176 #ifdef HAVE_WINDOW_SYSTEM
11177 ++clear_image_cache_count;
11178 #endif
11180 /* Build desired matrices, and update the display. If
11181 consider_all_windows_p is non-zero, do it for all windows on all
11182 frames. Otherwise do it for selected_window, only. */
11184 if (consider_all_windows_p)
11186 Lisp_Object tail, frame;
11188 FOR_EACH_FRAME (tail, frame)
11189 XFRAME (frame)->updated_p = 0;
11191 /* Recompute # windows showing selected buffer. This will be
11192 incremented each time such a window is displayed. */
11193 buffer_shared = 0;
11195 FOR_EACH_FRAME (tail, frame)
11197 struct frame *f = XFRAME (frame);
11199 if (FRAME_WINDOW_P (f) || f == sf)
11201 if (! EQ (frame, selected_frame))
11202 /* Select the frame, for the sake of frame-local
11203 variables. */
11204 select_frame_for_redisplay (frame);
11206 /* Mark all the scroll bars to be removed; we'll redeem
11207 the ones we want when we redisplay their windows. */
11208 if (condemn_scroll_bars_hook)
11209 condemn_scroll_bars_hook (f);
11211 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11212 redisplay_windows (FRAME_ROOT_WINDOW (f));
11214 /* Any scroll bars which redisplay_windows should have
11215 nuked should now go away. */
11216 if (judge_scroll_bars_hook)
11217 judge_scroll_bars_hook (f);
11219 /* If fonts changed, display again. */
11220 /* ??? rms: I suspect it is a mistake to jump all the way
11221 back to retry here. It should just retry this frame. */
11222 if (fonts_changed_p)
11223 goto retry;
11225 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11227 /* See if we have to hscroll. */
11228 if (!f->already_hscrolled_p)
11230 f->already_hscrolled_p = 1;
11231 if (hscroll_windows (f->root_window))
11232 goto retry;
11235 /* Prevent various kinds of signals during display
11236 update. stdio is not robust about handling
11237 signals, which can cause an apparent I/O
11238 error. */
11239 if (interrupt_input)
11240 unrequest_sigio ();
11241 STOP_POLLING;
11243 /* Update the display. */
11244 set_window_update_flags (XWINDOW (f->root_window), 1);
11245 pause |= update_frame (f, 0, 0);
11246 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11247 if (pause)
11248 break;
11249 #endif
11251 f->updated_p = 1;
11256 if (!pause)
11258 /* Do the mark_window_display_accurate after all windows have
11259 been redisplayed because this call resets flags in buffers
11260 which are needed for proper redisplay. */
11261 FOR_EACH_FRAME (tail, frame)
11263 struct frame *f = XFRAME (frame);
11264 if (f->updated_p)
11266 mark_window_display_accurate (f->root_window, 1);
11267 if (frame_up_to_date_hook)
11268 frame_up_to_date_hook (f);
11273 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11275 Lisp_Object mini_window;
11276 struct frame *mini_frame;
11278 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11279 /* Use list_of_error, not Qerror, so that
11280 we catch only errors and don't run the debugger. */
11281 internal_condition_case_1 (redisplay_window_1, selected_window,
11282 list_of_error,
11283 redisplay_window_error);
11285 /* Compare desired and current matrices, perform output. */
11287 update:
11288 /* If fonts changed, display again. */
11289 if (fonts_changed_p)
11290 goto retry;
11292 /* Prevent various kinds of signals during display update.
11293 stdio is not robust about handling signals,
11294 which can cause an apparent I/O error. */
11295 if (interrupt_input)
11296 unrequest_sigio ();
11297 STOP_POLLING;
11299 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11301 if (hscroll_windows (selected_window))
11302 goto retry;
11304 XWINDOW (selected_window)->must_be_updated_p = 1;
11305 pause = update_frame (sf, 0, 0);
11308 /* We may have called echo_area_display at the top of this
11309 function. If the echo area is on another frame, that may
11310 have put text on a frame other than the selected one, so the
11311 above call to update_frame would not have caught it. Catch
11312 it here. */
11313 mini_window = FRAME_MINIBUF_WINDOW (sf);
11314 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11316 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11318 XWINDOW (mini_window)->must_be_updated_p = 1;
11319 pause |= update_frame (mini_frame, 0, 0);
11320 if (!pause && hscroll_windows (mini_window))
11321 goto retry;
11325 /* If display was paused because of pending input, make sure we do a
11326 thorough update the next time. */
11327 if (pause)
11329 /* Prevent the optimization at the beginning of
11330 redisplay_internal that tries a single-line update of the
11331 line containing the cursor in the selected window. */
11332 CHARPOS (this_line_start_pos) = 0;
11334 /* Let the overlay arrow be updated the next time. */
11335 update_overlay_arrows (0);
11337 /* If we pause after scrolling, some rows in the current
11338 matrices of some windows are not valid. */
11339 if (!WINDOW_FULL_WIDTH_P (w)
11340 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11341 update_mode_lines = 1;
11343 else
11345 if (!consider_all_windows_p)
11347 /* This has already been done above if
11348 consider_all_windows_p is set. */
11349 mark_window_display_accurate_1 (w, 1);
11351 /* Say overlay arrows are up to date. */
11352 update_overlay_arrows (1);
11354 if (frame_up_to_date_hook != 0)
11355 frame_up_to_date_hook (sf);
11358 update_mode_lines = 0;
11359 windows_or_buffers_changed = 0;
11360 cursor_type_changed = 0;
11363 /* Start SIGIO interrupts coming again. Having them off during the
11364 code above makes it less likely one will discard output, but not
11365 impossible, since there might be stuff in the system buffer here.
11366 But it is much hairier to try to do anything about that. */
11367 if (interrupt_input)
11368 request_sigio ();
11369 RESUME_POLLING;
11371 /* If a frame has become visible which was not before, redisplay
11372 again, so that we display it. Expose events for such a frame
11373 (which it gets when becoming visible) don't call the parts of
11374 redisplay constructing glyphs, so simply exposing a frame won't
11375 display anything in this case. So, we have to display these
11376 frames here explicitly. */
11377 if (!pause)
11379 Lisp_Object tail, frame;
11380 int new_count = 0;
11382 FOR_EACH_FRAME (tail, frame)
11384 int this_is_visible = 0;
11386 if (XFRAME (frame)->visible)
11387 this_is_visible = 1;
11388 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11389 if (XFRAME (frame)->visible)
11390 this_is_visible = 1;
11392 if (this_is_visible)
11393 new_count++;
11396 if (new_count != number_of_visible_frames)
11397 windows_or_buffers_changed++;
11400 /* Change frame size now if a change is pending. */
11401 do_pending_window_change (1);
11403 /* If we just did a pending size change, or have additional
11404 visible frames, redisplay again. */
11405 if (windows_or_buffers_changed && !pause)
11406 goto retry;
11408 /* Clear the face cache eventually. */
11409 if (consider_all_windows_p)
11411 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11413 clear_face_cache (0);
11414 clear_face_cache_count = 0;
11416 #ifdef HAVE_WINDOW_SYSTEM
11417 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11419 Lisp_Object tail, frame;
11420 FOR_EACH_FRAME (tail, frame)
11422 struct frame *f = XFRAME (frame);
11423 if (FRAME_WINDOW_P (f))
11424 clear_image_cache (f, 0);
11426 clear_image_cache_count = 0;
11428 #endif /* HAVE_WINDOW_SYSTEM */
11431 end_of_redisplay:
11432 unbind_to (count, Qnil);
11433 RESUME_POLLING;
11437 /* Redisplay, but leave alone any recent echo area message unless
11438 another message has been requested in its place.
11440 This is useful in situations where you need to redisplay but no
11441 user action has occurred, making it inappropriate for the message
11442 area to be cleared. See tracking_off and
11443 wait_reading_process_output for examples of these situations.
11445 FROM_WHERE is an integer saying from where this function was
11446 called. This is useful for debugging. */
11448 void
11449 redisplay_preserve_echo_area (from_where)
11450 int from_where;
11452 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11454 if (!NILP (echo_area_buffer[1]))
11456 /* We have a previously displayed message, but no current
11457 message. Redisplay the previous message. */
11458 display_last_displayed_message_p = 1;
11459 redisplay_internal (1);
11460 display_last_displayed_message_p = 0;
11462 else
11463 redisplay_internal (1);
11465 if (rif != NULL && rif->flush_display_optional)
11466 rif->flush_display_optional (NULL);
11470 /* Function registered with record_unwind_protect in
11471 redisplay_internal. Reset redisplaying_p to the value it had
11472 before redisplay_internal was called, and clear
11473 prevent_freeing_realized_faces_p. It also selects the previously
11474 selected frame. */
11476 static Lisp_Object
11477 unwind_redisplay (val)
11478 Lisp_Object val;
11480 Lisp_Object old_redisplaying_p, old_frame;
11482 old_redisplaying_p = XCAR (val);
11483 redisplaying_p = XFASTINT (old_redisplaying_p);
11484 old_frame = XCDR (val);
11485 if (! EQ (old_frame, selected_frame))
11486 select_frame_for_redisplay (old_frame);
11487 return Qnil;
11491 /* Mark the display of window W as accurate or inaccurate. If
11492 ACCURATE_P is non-zero mark display of W as accurate. If
11493 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11494 redisplay_internal is called. */
11496 static void
11497 mark_window_display_accurate_1 (w, accurate_p)
11498 struct window *w;
11499 int accurate_p;
11501 if (BUFFERP (w->buffer))
11503 struct buffer *b = XBUFFER (w->buffer);
11505 w->last_modified
11506 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11507 w->last_overlay_modified
11508 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11509 w->last_had_star
11510 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11512 if (accurate_p)
11514 b->clip_changed = 0;
11515 b->prevent_redisplay_optimizations_p = 0;
11517 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11518 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11519 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11520 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11522 w->current_matrix->buffer = b;
11523 w->current_matrix->begv = BUF_BEGV (b);
11524 w->current_matrix->zv = BUF_ZV (b);
11526 w->last_cursor = w->cursor;
11527 w->last_cursor_off_p = w->cursor_off_p;
11529 if (w == XWINDOW (selected_window))
11530 w->last_point = make_number (BUF_PT (b));
11531 else
11532 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11536 if (accurate_p)
11538 w->window_end_valid = w->buffer;
11539 #if 0 /* This is incorrect with variable-height lines. */
11540 xassert (XINT (w->window_end_vpos)
11541 < (WINDOW_TOTAL_LINES (w)
11542 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11543 #endif
11544 w->update_mode_line = Qnil;
11549 /* Mark the display of windows in the window tree rooted at WINDOW as
11550 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11551 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11552 be redisplayed the next time redisplay_internal is called. */
11554 void
11555 mark_window_display_accurate (window, accurate_p)
11556 Lisp_Object window;
11557 int accurate_p;
11559 struct window *w;
11561 for (; !NILP (window); window = w->next)
11563 w = XWINDOW (window);
11564 mark_window_display_accurate_1 (w, accurate_p);
11566 if (!NILP (w->vchild))
11567 mark_window_display_accurate (w->vchild, accurate_p);
11568 if (!NILP (w->hchild))
11569 mark_window_display_accurate (w->hchild, accurate_p);
11572 if (accurate_p)
11574 update_overlay_arrows (1);
11576 else
11578 /* Force a thorough redisplay the next time by setting
11579 last_arrow_position and last_arrow_string to t, which is
11580 unequal to any useful value of Voverlay_arrow_... */
11581 update_overlay_arrows (-1);
11586 /* Return value in display table DP (Lisp_Char_Table *) for character
11587 C. Since a display table doesn't have any parent, we don't have to
11588 follow parent. Do not call this function directly but use the
11589 macro DISP_CHAR_VECTOR. */
11591 Lisp_Object
11592 disp_char_vector (dp, c)
11593 struct Lisp_Char_Table *dp;
11594 int c;
11596 int code[4], i;
11597 Lisp_Object val;
11599 if (SINGLE_BYTE_CHAR_P (c))
11600 return (dp->contents[c]);
11602 SPLIT_CHAR (c, code[0], code[1], code[2]);
11603 if (code[1] < 32)
11604 code[1] = -1;
11605 else if (code[2] < 32)
11606 code[2] = -1;
11608 /* Here, the possible range of code[0] (== charset ID) is
11609 128..max_charset. Since the top level char table contains data
11610 for multibyte characters after 256th element, we must increment
11611 code[0] by 128 to get a correct index. */
11612 code[0] += 128;
11613 code[3] = -1; /* anchor */
11615 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11617 val = dp->contents[code[i]];
11618 if (!SUB_CHAR_TABLE_P (val))
11619 return (NILP (val) ? dp->defalt : val);
11622 /* Here, val is a sub char table. We return the default value of
11623 it. */
11624 return (dp->defalt);
11629 /***********************************************************************
11630 Window Redisplay
11631 ***********************************************************************/
11633 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11635 static void
11636 redisplay_windows (window)
11637 Lisp_Object window;
11639 while (!NILP (window))
11641 struct window *w = XWINDOW (window);
11643 if (!NILP (w->hchild))
11644 redisplay_windows (w->hchild);
11645 else if (!NILP (w->vchild))
11646 redisplay_windows (w->vchild);
11647 else
11649 displayed_buffer = XBUFFER (w->buffer);
11650 /* Use list_of_error, not Qerror, so that
11651 we catch only errors and don't run the debugger. */
11652 internal_condition_case_1 (redisplay_window_0, window,
11653 list_of_error,
11654 redisplay_window_error);
11657 window = w->next;
11661 static Lisp_Object
11662 redisplay_window_error ()
11664 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11665 return Qnil;
11668 static Lisp_Object
11669 redisplay_window_0 (window)
11670 Lisp_Object window;
11672 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11673 redisplay_window (window, 0);
11674 return Qnil;
11677 static Lisp_Object
11678 redisplay_window_1 (window)
11679 Lisp_Object window;
11681 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11682 redisplay_window (window, 1);
11683 return Qnil;
11687 /* Increment GLYPH until it reaches END or CONDITION fails while
11688 adding (GLYPH)->pixel_width to X. */
11690 #define SKIP_GLYPHS(glyph, end, x, condition) \
11691 do \
11693 (x) += (glyph)->pixel_width; \
11694 ++(glyph); \
11696 while ((glyph) < (end) && (condition))
11699 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11700 DELTA is the number of bytes by which positions recorded in ROW
11701 differ from current buffer positions.
11703 Return 0 if cursor is not on this row. 1 otherwise. */
11706 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11707 struct window *w;
11708 struct glyph_row *row;
11709 struct glyph_matrix *matrix;
11710 int delta, delta_bytes, dy, dvpos;
11712 struct glyph *glyph = row->glyphs[TEXT_AREA];
11713 struct glyph *end = glyph + row->used[TEXT_AREA];
11714 struct glyph *cursor = NULL;
11715 /* The first glyph that starts a sequence of glyphs from string. */
11716 struct glyph *string_start;
11717 /* The X coordinate of string_start. */
11718 int string_start_x;
11719 /* The last known character position. */
11720 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11721 /* The last known character position before string_start. */
11722 int string_before_pos;
11723 int x = row->x;
11724 int cursor_x = x;
11725 int cursor_from_overlay_pos = 0;
11726 int pt_old = PT - delta;
11728 /* Skip over glyphs not having an object at the start of the row.
11729 These are special glyphs like truncation marks on terminal
11730 frames. */
11731 if (row->displays_text_p)
11732 while (glyph < end
11733 && INTEGERP (glyph->object)
11734 && glyph->charpos < 0)
11736 x += glyph->pixel_width;
11737 ++glyph;
11740 string_start = NULL;
11741 while (glyph < end
11742 && !INTEGERP (glyph->object)
11743 && (!BUFFERP (glyph->object)
11744 || (last_pos = glyph->charpos) < pt_old))
11746 if (! STRINGP (glyph->object))
11748 string_start = NULL;
11749 x += glyph->pixel_width;
11750 ++glyph;
11751 if (cursor_from_overlay_pos
11752 && last_pos >= cursor_from_overlay_pos)
11754 cursor_from_overlay_pos = 0;
11755 cursor = 0;
11758 else
11760 if (string_start == NULL)
11762 string_before_pos = last_pos;
11763 string_start = glyph;
11764 string_start_x = x;
11766 /* Skip all glyphs from string. */
11769 Lisp_Object cprop;
11770 int pos;
11771 if ((cursor == NULL || glyph > cursor)
11772 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11773 Qcursor, (glyph)->object),
11774 !NILP (cprop))
11775 && (pos = string_buffer_position (w, glyph->object,
11776 string_before_pos),
11777 (pos == 0 /* From overlay */
11778 || pos == pt_old)))
11780 /* Estimate overlay buffer position from the buffer
11781 positions of the glyphs before and after the overlay.
11782 Add 1 to last_pos so that if point corresponds to the
11783 glyph right after the overlay, we still use a 'cursor'
11784 property found in that overlay. */
11785 cursor_from_overlay_pos = (pos ? 0 : last_pos
11786 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11787 cursor = glyph;
11788 cursor_x = x;
11790 x += glyph->pixel_width;
11791 ++glyph;
11793 while (glyph < end && EQ (glyph->object, string_start->object));
11797 if (cursor != NULL)
11799 glyph = cursor;
11800 x = cursor_x;
11802 else if (row->ends_in_ellipsis_p && glyph == end)
11804 /* Scan back over the ellipsis glyphs, decrementing positions. */
11805 while (glyph > row->glyphs[TEXT_AREA]
11806 && (glyph - 1)->charpos == last_pos)
11807 glyph--, x -= glyph->pixel_width;
11808 /* That loop always goes one position too far,
11809 including the glyph before the ellipsis.
11810 So scan forward over that one. */
11811 x += glyph->pixel_width;
11812 glyph++;
11814 else if (string_start
11815 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11817 /* We may have skipped over point because the previous glyphs
11818 are from string. As there's no easy way to know the
11819 character position of the current glyph, find the correct
11820 glyph on point by scanning from string_start again. */
11821 Lisp_Object limit;
11822 Lisp_Object string;
11823 struct glyph *stop = glyph;
11824 int pos;
11826 limit = make_number (pt_old + 1);
11827 glyph = string_start;
11828 x = string_start_x;
11829 string = glyph->object;
11830 pos = string_buffer_position (w, string, string_before_pos);
11831 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11832 because we always put cursor after overlay strings. */
11833 while (pos == 0 && glyph < stop)
11835 string = glyph->object;
11836 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11837 if (glyph < stop)
11838 pos = string_buffer_position (w, glyph->object, string_before_pos);
11841 while (glyph < stop)
11843 pos = XINT (Fnext_single_char_property_change
11844 (make_number (pos), Qdisplay, Qnil, limit));
11845 if (pos > pt_old)
11846 break;
11847 /* Skip glyphs from the same string. */
11848 string = glyph->object;
11849 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11850 /* Skip glyphs from an overlay. */
11851 while (glyph < stop
11852 && ! string_buffer_position (w, glyph->object, pos))
11854 string = glyph->object;
11855 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11859 /* If we reached the end of the line, and end was from a string,
11860 cursor is not on this line. */
11861 if (glyph == end && row->continued_p)
11862 return 0;
11865 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11866 w->cursor.x = x;
11867 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11868 w->cursor.y = row->y + dy;
11870 if (w == XWINDOW (selected_window))
11872 if (!row->continued_p
11873 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11874 && row->x == 0)
11876 this_line_buffer = XBUFFER (w->buffer);
11878 CHARPOS (this_line_start_pos)
11879 = MATRIX_ROW_START_CHARPOS (row) + delta;
11880 BYTEPOS (this_line_start_pos)
11881 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11883 CHARPOS (this_line_end_pos)
11884 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11885 BYTEPOS (this_line_end_pos)
11886 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11888 this_line_y = w->cursor.y;
11889 this_line_pixel_height = row->height;
11890 this_line_vpos = w->cursor.vpos;
11891 this_line_start_x = row->x;
11893 else
11894 CHARPOS (this_line_start_pos) = 0;
11897 return 1;
11901 /* Run window scroll functions, if any, for WINDOW with new window
11902 start STARTP. Sets the window start of WINDOW to that position.
11904 We assume that the window's buffer is really current. */
11906 static INLINE struct text_pos
11907 run_window_scroll_functions (window, startp)
11908 Lisp_Object window;
11909 struct text_pos startp;
11911 struct window *w = XWINDOW (window);
11912 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11914 if (current_buffer != XBUFFER (w->buffer))
11915 abort ();
11917 if (!NILP (Vwindow_scroll_functions))
11919 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11920 make_number (CHARPOS (startp)));
11921 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11922 /* In case the hook functions switch buffers. */
11923 if (current_buffer != XBUFFER (w->buffer))
11924 set_buffer_internal_1 (XBUFFER (w->buffer));
11927 return startp;
11931 /* Make sure the line containing the cursor is fully visible.
11932 A value of 1 means there is nothing to be done.
11933 (Either the line is fully visible, or it cannot be made so,
11934 or we cannot tell.)
11936 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11937 is higher than window.
11939 A value of 0 means the caller should do scrolling
11940 as if point had gone off the screen. */
11942 static int
11943 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11944 struct window *w;
11945 int force_p;
11946 int current_matrix_p;
11948 struct glyph_matrix *matrix;
11949 struct glyph_row *row;
11950 int window_height;
11952 if (!make_cursor_line_fully_visible_p)
11953 return 1;
11955 /* It's not always possible to find the cursor, e.g, when a window
11956 is full of overlay strings. Don't do anything in that case. */
11957 if (w->cursor.vpos < 0)
11958 return 1;
11960 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11961 row = MATRIX_ROW (matrix, w->cursor.vpos);
11963 /* If the cursor row is not partially visible, there's nothing to do. */
11964 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11965 return 1;
11967 /* If the row the cursor is in is taller than the window's height,
11968 it's not clear what to do, so do nothing. */
11969 window_height = window_box_height (w);
11970 if (row->height >= window_height)
11972 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11973 return 1;
11975 return 0;
11977 #if 0
11978 /* This code used to try to scroll the window just enough to make
11979 the line visible. It returned 0 to say that the caller should
11980 allocate larger glyph matrices. */
11982 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11984 int dy = row->height - row->visible_height;
11985 w->vscroll = 0;
11986 w->cursor.y += dy;
11987 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11989 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11991 int dy = - (row->height - row->visible_height);
11992 w->vscroll = dy;
11993 w->cursor.y += dy;
11994 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11997 /* When we change the cursor y-position of the selected window,
11998 change this_line_y as well so that the display optimization for
11999 the cursor line of the selected window in redisplay_internal uses
12000 the correct y-position. */
12001 if (w == XWINDOW (selected_window))
12002 this_line_y = w->cursor.y;
12004 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12005 redisplay with larger matrices. */
12006 if (matrix->nrows < required_matrix_height (w))
12008 fonts_changed_p = 1;
12009 return 0;
12012 return 1;
12013 #endif /* 0 */
12017 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12018 non-zero means only WINDOW is redisplayed in redisplay_internal.
12019 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12020 in redisplay_window to bring a partially visible line into view in
12021 the case that only the cursor has moved.
12023 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12024 last screen line's vertical height extends past the end of the screen.
12026 Value is
12028 1 if scrolling succeeded
12030 0 if scrolling didn't find point.
12032 -1 if new fonts have been loaded so that we must interrupt
12033 redisplay, adjust glyph matrices, and try again. */
12035 enum
12037 SCROLLING_SUCCESS,
12038 SCROLLING_FAILED,
12039 SCROLLING_NEED_LARGER_MATRICES
12042 static int
12043 try_scrolling (window, just_this_one_p, scroll_conservatively,
12044 scroll_step, temp_scroll_step, last_line_misfit)
12045 Lisp_Object window;
12046 int just_this_one_p;
12047 EMACS_INT scroll_conservatively, scroll_step;
12048 int temp_scroll_step;
12049 int last_line_misfit;
12051 struct window *w = XWINDOW (window);
12052 struct frame *f = XFRAME (w->frame);
12053 struct text_pos scroll_margin_pos;
12054 struct text_pos pos;
12055 struct text_pos startp;
12056 struct it it;
12057 Lisp_Object window_end;
12058 int this_scroll_margin;
12059 int dy = 0;
12060 int scroll_max;
12061 int rc;
12062 int amount_to_scroll = 0;
12063 Lisp_Object aggressive;
12064 int height;
12065 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12067 #if GLYPH_DEBUG
12068 debug_method_add (w, "try_scrolling");
12069 #endif
12071 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12073 /* Compute scroll margin height in pixels. We scroll when point is
12074 within this distance from the top or bottom of the window. */
12075 if (scroll_margin > 0)
12077 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12078 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12080 else
12081 this_scroll_margin = 0;
12083 /* Force scroll_conservatively to have a reasonable value so it doesn't
12084 cause an overflow while computing how much to scroll. */
12085 if (scroll_conservatively)
12086 scroll_conservatively = min (scroll_conservatively,
12087 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12089 /* Compute how much we should try to scroll maximally to bring point
12090 into view. */
12091 if (scroll_step || scroll_conservatively || temp_scroll_step)
12092 scroll_max = max (scroll_step,
12093 max (scroll_conservatively, temp_scroll_step));
12094 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12095 || NUMBERP (current_buffer->scroll_up_aggressively))
12096 /* We're trying to scroll because of aggressive scrolling
12097 but no scroll_step is set. Choose an arbitrary one. Maybe
12098 there should be a variable for this. */
12099 scroll_max = 10;
12100 else
12101 scroll_max = 0;
12102 scroll_max *= FRAME_LINE_HEIGHT (f);
12104 /* Decide whether we have to scroll down. Start at the window end
12105 and move this_scroll_margin up to find the position of the scroll
12106 margin. */
12107 window_end = Fwindow_end (window, Qt);
12109 too_near_end:
12111 CHARPOS (scroll_margin_pos) = XINT (window_end);
12112 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12114 if (this_scroll_margin || extra_scroll_margin_lines)
12116 start_display (&it, w, scroll_margin_pos);
12117 if (this_scroll_margin)
12118 move_it_vertically_backward (&it, this_scroll_margin);
12119 if (extra_scroll_margin_lines)
12120 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12121 scroll_margin_pos = it.current.pos;
12124 if (PT >= CHARPOS (scroll_margin_pos))
12126 int y0;
12128 /* Point is in the scroll margin at the bottom of the window, or
12129 below. Compute a new window start that makes point visible. */
12131 /* Compute the distance from the scroll margin to PT.
12132 Give up if the distance is greater than scroll_max. */
12133 start_display (&it, w, scroll_margin_pos);
12134 y0 = it.current_y;
12135 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12136 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12138 /* To make point visible, we have to move the window start
12139 down so that the line the cursor is in is visible, which
12140 means we have to add in the height of the cursor line. */
12141 dy = line_bottom_y (&it) - y0;
12143 if (dy > scroll_max)
12144 return SCROLLING_FAILED;
12146 /* Move the window start down. If scrolling conservatively,
12147 move it just enough down to make point visible. If
12148 scroll_step is set, move it down by scroll_step. */
12149 start_display (&it, w, startp);
12151 if (scroll_conservatively)
12152 /* Set AMOUNT_TO_SCROLL to at least one line,
12153 and at most scroll_conservatively lines. */
12154 amount_to_scroll
12155 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12156 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12157 else if (scroll_step || temp_scroll_step)
12158 amount_to_scroll = scroll_max;
12159 else
12161 aggressive = current_buffer->scroll_up_aggressively;
12162 height = WINDOW_BOX_TEXT_HEIGHT (w);
12163 if (NUMBERP (aggressive))
12165 double float_amount = XFLOATINT (aggressive) * height;
12166 amount_to_scroll = float_amount;
12167 if (amount_to_scroll == 0 && float_amount > 0)
12168 amount_to_scroll = 1;
12172 if (amount_to_scroll <= 0)
12173 return SCROLLING_FAILED;
12175 /* If moving by amount_to_scroll leaves STARTP unchanged,
12176 move it down one screen line. */
12178 move_it_vertically (&it, amount_to_scroll);
12179 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12180 move_it_by_lines (&it, 1, 1);
12181 startp = it.current.pos;
12183 else
12185 /* See if point is inside the scroll margin at the top of the
12186 window. */
12187 scroll_margin_pos = startp;
12188 if (this_scroll_margin)
12190 start_display (&it, w, startp);
12191 move_it_vertically (&it, this_scroll_margin);
12192 scroll_margin_pos = it.current.pos;
12195 if (PT < CHARPOS (scroll_margin_pos))
12197 /* Point is in the scroll margin at the top of the window or
12198 above what is displayed in the window. */
12199 int y0;
12201 /* Compute the vertical distance from PT to the scroll
12202 margin position. Give up if distance is greater than
12203 scroll_max. */
12204 SET_TEXT_POS (pos, PT, PT_BYTE);
12205 start_display (&it, w, pos);
12206 y0 = it.current_y;
12207 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12208 it.last_visible_y, -1,
12209 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12210 dy = it.current_y - y0;
12211 if (dy > scroll_max)
12212 return SCROLLING_FAILED;
12214 /* Compute new window start. */
12215 start_display (&it, w, startp);
12217 if (scroll_conservatively)
12218 amount_to_scroll
12219 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12220 else if (scroll_step || temp_scroll_step)
12221 amount_to_scroll = scroll_max;
12222 else
12224 aggressive = current_buffer->scroll_down_aggressively;
12225 height = WINDOW_BOX_TEXT_HEIGHT (w);
12226 if (NUMBERP (aggressive))
12228 double float_amount = XFLOATINT (aggressive) * height;
12229 amount_to_scroll = float_amount;
12230 if (amount_to_scroll == 0 && float_amount > 0)
12231 amount_to_scroll = 1;
12235 if (amount_to_scroll <= 0)
12236 return SCROLLING_FAILED;
12238 move_it_vertically_backward (&it, amount_to_scroll);
12239 startp = it.current.pos;
12243 /* Run window scroll functions. */
12244 startp = run_window_scroll_functions (window, startp);
12246 /* Display the window. Give up if new fonts are loaded, or if point
12247 doesn't appear. */
12248 if (!try_window (window, startp, 0))
12249 rc = SCROLLING_NEED_LARGER_MATRICES;
12250 else if (w->cursor.vpos < 0)
12252 clear_glyph_matrix (w->desired_matrix);
12253 rc = SCROLLING_FAILED;
12255 else
12257 /* Maybe forget recorded base line for line number display. */
12258 if (!just_this_one_p
12259 || current_buffer->clip_changed
12260 || BEG_UNCHANGED < CHARPOS (startp))
12261 w->base_line_number = Qnil;
12263 /* If cursor ends up on a partially visible line,
12264 treat that as being off the bottom of the screen. */
12265 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12267 clear_glyph_matrix (w->desired_matrix);
12268 ++extra_scroll_margin_lines;
12269 goto too_near_end;
12271 rc = SCROLLING_SUCCESS;
12274 return rc;
12278 /* Compute a suitable window start for window W if display of W starts
12279 on a continuation line. Value is non-zero if a new window start
12280 was computed.
12282 The new window start will be computed, based on W's width, starting
12283 from the start of the continued line. It is the start of the
12284 screen line with the minimum distance from the old start W->start. */
12286 static int
12287 compute_window_start_on_continuation_line (w)
12288 struct window *w;
12290 struct text_pos pos, start_pos;
12291 int window_start_changed_p = 0;
12293 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12295 /* If window start is on a continuation line... Window start may be
12296 < BEGV in case there's invisible text at the start of the
12297 buffer (M-x rmail, for example). */
12298 if (CHARPOS (start_pos) > BEGV
12299 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12301 struct it it;
12302 struct glyph_row *row;
12304 /* Handle the case that the window start is out of range. */
12305 if (CHARPOS (start_pos) < BEGV)
12306 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12307 else if (CHARPOS (start_pos) > ZV)
12308 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12310 /* Find the start of the continued line. This should be fast
12311 because scan_buffer is fast (newline cache). */
12312 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12313 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12314 row, DEFAULT_FACE_ID);
12315 reseat_at_previous_visible_line_start (&it);
12317 /* If the line start is "too far" away from the window start,
12318 say it takes too much time to compute a new window start. */
12319 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12320 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12322 int min_distance, distance;
12324 /* Move forward by display lines to find the new window
12325 start. If window width was enlarged, the new start can
12326 be expected to be > the old start. If window width was
12327 decreased, the new window start will be < the old start.
12328 So, we're looking for the display line start with the
12329 minimum distance from the old window start. */
12330 pos = it.current.pos;
12331 min_distance = INFINITY;
12332 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12333 distance < min_distance)
12335 min_distance = distance;
12336 pos = it.current.pos;
12337 move_it_by_lines (&it, 1, 0);
12340 /* Set the window start there. */
12341 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12342 window_start_changed_p = 1;
12346 return window_start_changed_p;
12350 /* Try cursor movement in case text has not changed in window WINDOW,
12351 with window start STARTP. Value is
12353 CURSOR_MOVEMENT_SUCCESS if successful
12355 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12357 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12358 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12359 we want to scroll as if scroll-step were set to 1. See the code.
12361 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12362 which case we have to abort this redisplay, and adjust matrices
12363 first. */
12365 enum
12367 CURSOR_MOVEMENT_SUCCESS,
12368 CURSOR_MOVEMENT_CANNOT_BE_USED,
12369 CURSOR_MOVEMENT_MUST_SCROLL,
12370 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12373 static int
12374 try_cursor_movement (window, startp, scroll_step)
12375 Lisp_Object window;
12376 struct text_pos startp;
12377 int *scroll_step;
12379 struct window *w = XWINDOW (window);
12380 struct frame *f = XFRAME (w->frame);
12381 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12383 #if GLYPH_DEBUG
12384 if (inhibit_try_cursor_movement)
12385 return rc;
12386 #endif
12388 /* Handle case where text has not changed, only point, and it has
12389 not moved off the frame. */
12390 if (/* Point may be in this window. */
12391 PT >= CHARPOS (startp)
12392 /* Selective display hasn't changed. */
12393 && !current_buffer->clip_changed
12394 /* Function force-mode-line-update is used to force a thorough
12395 redisplay. It sets either windows_or_buffers_changed or
12396 update_mode_lines. So don't take a shortcut here for these
12397 cases. */
12398 && !update_mode_lines
12399 && !windows_or_buffers_changed
12400 && !cursor_type_changed
12401 /* Can't use this case if highlighting a region. When a
12402 region exists, cursor movement has to do more than just
12403 set the cursor. */
12404 && !(!NILP (Vtransient_mark_mode)
12405 && !NILP (current_buffer->mark_active))
12406 && NILP (w->region_showing)
12407 && NILP (Vshow_trailing_whitespace)
12408 /* Right after splitting windows, last_point may be nil. */
12409 && INTEGERP (w->last_point)
12410 /* This code is not used for mini-buffer for the sake of the case
12411 of redisplaying to replace an echo area message; since in
12412 that case the mini-buffer contents per se are usually
12413 unchanged. This code is of no real use in the mini-buffer
12414 since the handling of this_line_start_pos, etc., in redisplay
12415 handles the same cases. */
12416 && !EQ (window, minibuf_window)
12417 /* When splitting windows or for new windows, it happens that
12418 redisplay is called with a nil window_end_vpos or one being
12419 larger than the window. This should really be fixed in
12420 window.c. I don't have this on my list, now, so we do
12421 approximately the same as the old redisplay code. --gerd. */
12422 && INTEGERP (w->window_end_vpos)
12423 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12424 && (FRAME_WINDOW_P (f)
12425 || !overlay_arrow_in_current_buffer_p ()))
12427 int this_scroll_margin, top_scroll_margin;
12428 struct glyph_row *row = NULL;
12430 #if GLYPH_DEBUG
12431 debug_method_add (w, "cursor movement");
12432 #endif
12434 /* Scroll if point within this distance from the top or bottom
12435 of the window. This is a pixel value. */
12436 this_scroll_margin = max (0, scroll_margin);
12437 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12438 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12440 top_scroll_margin = this_scroll_margin;
12441 if (WINDOW_WANTS_HEADER_LINE_P (w))
12442 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12444 /* Start with the row the cursor was displayed during the last
12445 not paused redisplay. Give up if that row is not valid. */
12446 if (w->last_cursor.vpos < 0
12447 || w->last_cursor.vpos >= w->current_matrix->nrows)
12448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12449 else
12451 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12452 if (row->mode_line_p)
12453 ++row;
12454 if (!row->enabled_p)
12455 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12458 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12460 int scroll_p = 0;
12461 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12463 if (PT > XFASTINT (w->last_point))
12465 /* Point has moved forward. */
12466 while (MATRIX_ROW_END_CHARPOS (row) < PT
12467 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12469 xassert (row->enabled_p);
12470 ++row;
12473 /* The end position of a row equals the start position
12474 of the next row. If PT is there, we would rather
12475 display it in the next line. */
12476 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12477 && MATRIX_ROW_END_CHARPOS (row) == PT
12478 && !cursor_row_p (w, row))
12479 ++row;
12481 /* If within the scroll margin, scroll. Note that
12482 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12483 the next line would be drawn, and that
12484 this_scroll_margin can be zero. */
12485 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12486 || PT > MATRIX_ROW_END_CHARPOS (row)
12487 /* Line is completely visible last line in window
12488 and PT is to be set in the next line. */
12489 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12490 && PT == MATRIX_ROW_END_CHARPOS (row)
12491 && !row->ends_at_zv_p
12492 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12493 scroll_p = 1;
12495 else if (PT < XFASTINT (w->last_point))
12497 /* Cursor has to be moved backward. Note that PT >=
12498 CHARPOS (startp) because of the outer if-statement. */
12499 while (!row->mode_line_p
12500 && (MATRIX_ROW_START_CHARPOS (row) > PT
12501 || (MATRIX_ROW_START_CHARPOS (row) == PT
12502 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12503 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12504 row > w->current_matrix->rows
12505 && (row-1)->ends_in_newline_from_string_p))))
12506 && (row->y > top_scroll_margin
12507 || CHARPOS (startp) == BEGV))
12509 xassert (row->enabled_p);
12510 --row;
12513 /* Consider the following case: Window starts at BEGV,
12514 there is invisible, intangible text at BEGV, so that
12515 display starts at some point START > BEGV. It can
12516 happen that we are called with PT somewhere between
12517 BEGV and START. Try to handle that case. */
12518 if (row < w->current_matrix->rows
12519 || row->mode_line_p)
12521 row = w->current_matrix->rows;
12522 if (row->mode_line_p)
12523 ++row;
12526 /* Due to newlines in overlay strings, we may have to
12527 skip forward over overlay strings. */
12528 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12529 && MATRIX_ROW_END_CHARPOS (row) == PT
12530 && !cursor_row_p (w, row))
12531 ++row;
12533 /* If within the scroll margin, scroll. */
12534 if (row->y < top_scroll_margin
12535 && CHARPOS (startp) != BEGV)
12536 scroll_p = 1;
12538 else
12540 /* Cursor did not move. So don't scroll even if cursor line
12541 is partially visible, as it was so before. */
12542 rc = CURSOR_MOVEMENT_SUCCESS;
12545 if (PT < MATRIX_ROW_START_CHARPOS (row)
12546 || PT > MATRIX_ROW_END_CHARPOS (row))
12548 /* if PT is not in the glyph row, give up. */
12549 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12551 else if (rc != CURSOR_MOVEMENT_SUCCESS
12552 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12553 && make_cursor_line_fully_visible_p)
12555 if (PT == MATRIX_ROW_END_CHARPOS (row)
12556 && !row->ends_at_zv_p
12557 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12558 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12559 else if (row->height > window_box_height (w))
12561 /* If we end up in a partially visible line, let's
12562 make it fully visible, except when it's taller
12563 than the window, in which case we can't do much
12564 about it. */
12565 *scroll_step = 1;
12566 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12568 else
12570 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12571 if (!cursor_row_fully_visible_p (w, 0, 1))
12572 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12573 else
12574 rc = CURSOR_MOVEMENT_SUCCESS;
12577 else if (scroll_p)
12578 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12579 else
12583 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12585 rc = CURSOR_MOVEMENT_SUCCESS;
12586 break;
12588 ++row;
12590 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12591 && MATRIX_ROW_START_CHARPOS (row) == PT
12592 && cursor_row_p (w, row));
12597 return rc;
12600 void
12601 set_vertical_scroll_bar (w)
12602 struct window *w;
12604 int start, end, whole;
12606 /* Calculate the start and end positions for the current window.
12607 At some point, it would be nice to choose between scrollbars
12608 which reflect the whole buffer size, with special markers
12609 indicating narrowing, and scrollbars which reflect only the
12610 visible region.
12612 Note that mini-buffers sometimes aren't displaying any text. */
12613 if (!MINI_WINDOW_P (w)
12614 || (w == XWINDOW (minibuf_window)
12615 && NILP (echo_area_buffer[0])))
12617 struct buffer *buf = XBUFFER (w->buffer);
12618 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12619 start = marker_position (w->start) - BUF_BEGV (buf);
12620 /* I don't think this is guaranteed to be right. For the
12621 moment, we'll pretend it is. */
12622 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12624 if (end < start)
12625 end = start;
12626 if (whole < (end - start))
12627 whole = end - start;
12629 else
12630 start = end = whole = 0;
12632 /* Indicate what this scroll bar ought to be displaying now. */
12633 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12637 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12638 selected_window is redisplayed.
12640 We can return without actually redisplaying the window if
12641 fonts_changed_p is nonzero. In that case, redisplay_internal will
12642 retry. */
12644 static void
12645 redisplay_window (window, just_this_one_p)
12646 Lisp_Object window;
12647 int just_this_one_p;
12649 struct window *w = XWINDOW (window);
12650 struct frame *f = XFRAME (w->frame);
12651 struct buffer *buffer = XBUFFER (w->buffer);
12652 struct buffer *old = current_buffer;
12653 struct text_pos lpoint, opoint, startp;
12654 int update_mode_line;
12655 int tem;
12656 struct it it;
12657 /* Record it now because it's overwritten. */
12658 int current_matrix_up_to_date_p = 0;
12659 int used_current_matrix_p = 0;
12660 /* This is less strict than current_matrix_up_to_date_p.
12661 It indictes that the buffer contents and narrowing are unchanged. */
12662 int buffer_unchanged_p = 0;
12663 int temp_scroll_step = 0;
12664 int count = SPECPDL_INDEX ();
12665 int rc;
12666 int centering_position = -1;
12667 int last_line_misfit = 0;
12669 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12670 opoint = lpoint;
12672 /* W must be a leaf window here. */
12673 xassert (!NILP (w->buffer));
12674 #if GLYPH_DEBUG
12675 *w->desired_matrix->method = 0;
12676 #endif
12678 specbind (Qinhibit_point_motion_hooks, Qt);
12680 reconsider_clip_changes (w, buffer);
12682 /* Has the mode line to be updated? */
12683 update_mode_line = (!NILP (w->update_mode_line)
12684 || update_mode_lines
12685 || buffer->clip_changed
12686 || buffer->prevent_redisplay_optimizations_p);
12688 if (MINI_WINDOW_P (w))
12690 if (w == XWINDOW (echo_area_window)
12691 && !NILP (echo_area_buffer[0]))
12693 if (update_mode_line)
12694 /* We may have to update a tty frame's menu bar or a
12695 tool-bar. Example `M-x C-h C-h C-g'. */
12696 goto finish_menu_bars;
12697 else
12698 /* We've already displayed the echo area glyphs in this window. */
12699 goto finish_scroll_bars;
12701 else if ((w != XWINDOW (minibuf_window)
12702 || minibuf_level == 0)
12703 /* When buffer is nonempty, redisplay window normally. */
12704 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12705 /* Quail displays non-mini buffers in minibuffer window.
12706 In that case, redisplay the window normally. */
12707 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12709 /* W is a mini-buffer window, but it's not active, so clear
12710 it. */
12711 int yb = window_text_bottom_y (w);
12712 struct glyph_row *row;
12713 int y;
12715 for (y = 0, row = w->desired_matrix->rows;
12716 y < yb;
12717 y += row->height, ++row)
12718 blank_row (w, row, y);
12719 goto finish_scroll_bars;
12722 clear_glyph_matrix (w->desired_matrix);
12725 /* Otherwise set up data on this window; select its buffer and point
12726 value. */
12727 /* Really select the buffer, for the sake of buffer-local
12728 variables. */
12729 set_buffer_internal_1 (XBUFFER (w->buffer));
12730 SET_TEXT_POS (opoint, PT, PT_BYTE);
12732 current_matrix_up_to_date_p
12733 = (!NILP (w->window_end_valid)
12734 && !current_buffer->clip_changed
12735 && !current_buffer->prevent_redisplay_optimizations_p
12736 && XFASTINT (w->last_modified) >= MODIFF
12737 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12739 buffer_unchanged_p
12740 = (!NILP (w->window_end_valid)
12741 && !current_buffer->clip_changed
12742 && XFASTINT (w->last_modified) >= MODIFF
12743 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12745 /* When windows_or_buffers_changed is non-zero, we can't rely on
12746 the window end being valid, so set it to nil there. */
12747 if (windows_or_buffers_changed)
12749 /* If window starts on a continuation line, maybe adjust the
12750 window start in case the window's width changed. */
12751 if (XMARKER (w->start)->buffer == current_buffer)
12752 compute_window_start_on_continuation_line (w);
12754 w->window_end_valid = Qnil;
12757 /* Some sanity checks. */
12758 CHECK_WINDOW_END (w);
12759 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12760 abort ();
12761 if (BYTEPOS (opoint) < CHARPOS (opoint))
12762 abort ();
12764 /* If %c is in mode line, update it if needed. */
12765 if (!NILP (w->column_number_displayed)
12766 /* This alternative quickly identifies a common case
12767 where no change is needed. */
12768 && !(PT == XFASTINT (w->last_point)
12769 && XFASTINT (w->last_modified) >= MODIFF
12770 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12771 && (XFASTINT (w->column_number_displayed)
12772 != (int) current_column ())) /* iftc */
12773 update_mode_line = 1;
12775 /* Count number of windows showing the selected buffer. An indirect
12776 buffer counts as its base buffer. */
12777 if (!just_this_one_p)
12779 struct buffer *current_base, *window_base;
12780 current_base = current_buffer;
12781 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12782 if (current_base->base_buffer)
12783 current_base = current_base->base_buffer;
12784 if (window_base->base_buffer)
12785 window_base = window_base->base_buffer;
12786 if (current_base == window_base)
12787 buffer_shared++;
12790 /* Point refers normally to the selected window. For any other
12791 window, set up appropriate value. */
12792 if (!EQ (window, selected_window))
12794 int new_pt = XMARKER (w->pointm)->charpos;
12795 int new_pt_byte = marker_byte_position (w->pointm);
12796 if (new_pt < BEGV)
12798 new_pt = BEGV;
12799 new_pt_byte = BEGV_BYTE;
12800 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12802 else if (new_pt > (ZV - 1))
12804 new_pt = ZV;
12805 new_pt_byte = ZV_BYTE;
12806 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12809 /* We don't use SET_PT so that the point-motion hooks don't run. */
12810 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12813 /* If any of the character widths specified in the display table
12814 have changed, invalidate the width run cache. It's true that
12815 this may be a bit late to catch such changes, but the rest of
12816 redisplay goes (non-fatally) haywire when the display table is
12817 changed, so why should we worry about doing any better? */
12818 if (current_buffer->width_run_cache)
12820 struct Lisp_Char_Table *disptab = buffer_display_table ();
12822 if (! disptab_matches_widthtab (disptab,
12823 XVECTOR (current_buffer->width_table)))
12825 invalidate_region_cache (current_buffer,
12826 current_buffer->width_run_cache,
12827 BEG, Z);
12828 recompute_width_table (current_buffer, disptab);
12832 /* If window-start is screwed up, choose a new one. */
12833 if (XMARKER (w->start)->buffer != current_buffer)
12834 goto recenter;
12836 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12838 /* If someone specified a new starting point but did not insist,
12839 check whether it can be used. */
12840 if (!NILP (w->optional_new_start)
12841 && CHARPOS (startp) >= BEGV
12842 && CHARPOS (startp) <= ZV)
12844 w->optional_new_start = Qnil;
12845 start_display (&it, w, startp);
12846 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12847 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12848 if (IT_CHARPOS (it) == PT)
12849 w->force_start = Qt;
12850 /* IT may overshoot PT if text at PT is invisible. */
12851 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12852 w->force_start = Qt;
12855 /* Handle case where place to start displaying has been specified,
12856 unless the specified location is outside the accessible range. */
12857 if (!NILP (w->force_start)
12858 || w->frozen_window_start_p)
12860 /* We set this later on if we have to adjust point. */
12861 int new_vpos = -1;
12862 int val;
12864 w->force_start = Qnil;
12865 w->vscroll = 0;
12866 w->window_end_valid = Qnil;
12868 /* Forget any recorded base line for line number display. */
12869 if (!buffer_unchanged_p)
12870 w->base_line_number = Qnil;
12872 /* Redisplay the mode line. Select the buffer properly for that.
12873 Also, run the hook window-scroll-functions
12874 because we have scrolled. */
12875 /* Note, we do this after clearing force_start because
12876 if there's an error, it is better to forget about force_start
12877 than to get into an infinite loop calling the hook functions
12878 and having them get more errors. */
12879 if (!update_mode_line
12880 || ! NILP (Vwindow_scroll_functions))
12882 update_mode_line = 1;
12883 w->update_mode_line = Qt;
12884 startp = run_window_scroll_functions (window, startp);
12887 w->last_modified = make_number (0);
12888 w->last_overlay_modified = make_number (0);
12889 if (CHARPOS (startp) < BEGV)
12890 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12891 else if (CHARPOS (startp) > ZV)
12892 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12894 /* Redisplay, then check if cursor has been set during the
12895 redisplay. Give up if new fonts were loaded. */
12896 val = try_window (window, startp, 1);
12897 if (!val)
12899 w->force_start = Qt;
12900 clear_glyph_matrix (w->desired_matrix);
12901 goto need_larger_matrices;
12903 /* Point was outside the scroll margins. */
12904 if (val < 0)
12905 new_vpos = window_box_height (w) / 2;
12907 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12909 /* If point does not appear, try to move point so it does
12910 appear. The desired matrix has been built above, so we
12911 can use it here. */
12912 new_vpos = window_box_height (w) / 2;
12915 if (!cursor_row_fully_visible_p (w, 0, 0))
12917 /* Point does appear, but on a line partly visible at end of window.
12918 Move it back to a fully-visible line. */
12919 new_vpos = window_box_height (w);
12922 /* If we need to move point for either of the above reasons,
12923 now actually do it. */
12924 if (new_vpos >= 0)
12926 struct glyph_row *row;
12928 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12929 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12930 ++row;
12932 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12933 MATRIX_ROW_START_BYTEPOS (row));
12935 if (w != XWINDOW (selected_window))
12936 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12937 else if (current_buffer == old)
12938 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12940 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12942 /* If we are highlighting the region, then we just changed
12943 the region, so redisplay to show it. */
12944 if (!NILP (Vtransient_mark_mode)
12945 && !NILP (current_buffer->mark_active))
12947 clear_glyph_matrix (w->desired_matrix);
12948 if (!try_window (window, startp, 0))
12949 goto need_larger_matrices;
12953 #if GLYPH_DEBUG
12954 debug_method_add (w, "forced window start");
12955 #endif
12956 goto done;
12959 /* Handle case where text has not changed, only point, and it has
12960 not moved off the frame, and we are not retrying after hscroll.
12961 (current_matrix_up_to_date_p is nonzero when retrying.) */
12962 if (current_matrix_up_to_date_p
12963 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12964 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12966 switch (rc)
12968 case CURSOR_MOVEMENT_SUCCESS:
12969 used_current_matrix_p = 1;
12970 goto done;
12972 #if 0 /* try_cursor_movement never returns this value. */
12973 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12974 goto need_larger_matrices;
12975 #endif
12977 case CURSOR_MOVEMENT_MUST_SCROLL:
12978 goto try_to_scroll;
12980 default:
12981 abort ();
12984 /* If current starting point was originally the beginning of a line
12985 but no longer is, find a new starting point. */
12986 else if (!NILP (w->start_at_line_beg)
12987 && !(CHARPOS (startp) <= BEGV
12988 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12990 #if GLYPH_DEBUG
12991 debug_method_add (w, "recenter 1");
12992 #endif
12993 goto recenter;
12996 /* Try scrolling with try_window_id. Value is > 0 if update has
12997 been done, it is -1 if we know that the same window start will
12998 not work. It is 0 if unsuccessful for some other reason. */
12999 else if ((tem = try_window_id (w)) != 0)
13001 #if GLYPH_DEBUG
13002 debug_method_add (w, "try_window_id %d", tem);
13003 #endif
13005 if (fonts_changed_p)
13006 goto need_larger_matrices;
13007 if (tem > 0)
13008 goto done;
13010 /* Otherwise try_window_id has returned -1 which means that we
13011 don't want the alternative below this comment to execute. */
13013 else if (CHARPOS (startp) >= BEGV
13014 && CHARPOS (startp) <= ZV
13015 && PT >= CHARPOS (startp)
13016 && (CHARPOS (startp) < ZV
13017 /* Avoid starting at end of buffer. */
13018 || CHARPOS (startp) == BEGV
13019 || (XFASTINT (w->last_modified) >= MODIFF
13020 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13023 /* If first window line is a continuation line, and window start
13024 is inside the modified region, but the first change is before
13025 current window start, we must select a new window start.*/
13026 if (NILP (w->start_at_line_beg)
13027 && CHARPOS (startp) > BEGV)
13029 /* Make sure beg_unchanged and end_unchanged are up to date.
13030 Do it only if buffer has really changed. This may or may
13031 not have been done by try_window_id (see which) already. */
13032 if (MODIFF > SAVE_MODIFF
13033 /* This seems to happen sometimes after saving a buffer. */
13034 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13036 if (GPT - BEG < BEG_UNCHANGED)
13037 BEG_UNCHANGED = GPT - BEG;
13038 if (Z - GPT < END_UNCHANGED)
13039 END_UNCHANGED = Z - GPT;
13042 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13043 && CHARPOS (startp) <= Z - END_UNCHANGED)
13045 /* There doesn't seems to be a simple way to find a new
13046 window start that is near the old window start, so
13047 we just recenter. */
13048 goto recenter;
13052 #if GLYPH_DEBUG
13053 debug_method_add (w, "same window start");
13054 #endif
13056 /* Try to redisplay starting at same place as before.
13057 If point has not moved off frame, accept the results. */
13058 if (!current_matrix_up_to_date_p
13059 /* Don't use try_window_reusing_current_matrix in this case
13060 because a window scroll function can have changed the
13061 buffer. */
13062 || !NILP (Vwindow_scroll_functions)
13063 || MINI_WINDOW_P (w)
13064 || !(used_current_matrix_p
13065 = try_window_reusing_current_matrix (w)))
13067 IF_DEBUG (debug_method_add (w, "1"));
13068 if (try_window (window, startp, 1) < 0)
13069 /* -1 means we need to scroll.
13070 0 means we need new matrices, but fonts_changed_p
13071 is set in that case, so we will detect it below. */
13072 goto try_to_scroll;
13075 if (fonts_changed_p)
13076 goto need_larger_matrices;
13078 if (w->cursor.vpos >= 0)
13080 if (!just_this_one_p
13081 || current_buffer->clip_changed
13082 || BEG_UNCHANGED < CHARPOS (startp))
13083 /* Forget any recorded base line for line number display. */
13084 w->base_line_number = Qnil;
13086 if (!cursor_row_fully_visible_p (w, 1, 0))
13088 clear_glyph_matrix (w->desired_matrix);
13089 last_line_misfit = 1;
13091 /* Drop through and scroll. */
13092 else
13093 goto done;
13095 else
13096 clear_glyph_matrix (w->desired_matrix);
13099 try_to_scroll:
13101 w->last_modified = make_number (0);
13102 w->last_overlay_modified = make_number (0);
13104 /* Redisplay the mode line. Select the buffer properly for that. */
13105 if (!update_mode_line)
13107 update_mode_line = 1;
13108 w->update_mode_line = Qt;
13111 /* Try to scroll by specified few lines. */
13112 if ((scroll_conservatively
13113 || scroll_step
13114 || temp_scroll_step
13115 || NUMBERP (current_buffer->scroll_up_aggressively)
13116 || NUMBERP (current_buffer->scroll_down_aggressively))
13117 && !current_buffer->clip_changed
13118 && CHARPOS (startp) >= BEGV
13119 && CHARPOS (startp) <= ZV)
13121 /* The function returns -1 if new fonts were loaded, 1 if
13122 successful, 0 if not successful. */
13123 int rc = try_scrolling (window, just_this_one_p,
13124 scroll_conservatively,
13125 scroll_step,
13126 temp_scroll_step, last_line_misfit);
13127 switch (rc)
13129 case SCROLLING_SUCCESS:
13130 goto done;
13132 case SCROLLING_NEED_LARGER_MATRICES:
13133 goto need_larger_matrices;
13135 case SCROLLING_FAILED:
13136 break;
13138 default:
13139 abort ();
13143 /* Finally, just choose place to start which centers point */
13145 recenter:
13146 if (centering_position < 0)
13147 centering_position = window_box_height (w) / 2;
13149 #if GLYPH_DEBUG
13150 debug_method_add (w, "recenter");
13151 #endif
13153 /* w->vscroll = 0; */
13155 /* Forget any previously recorded base line for line number display. */
13156 if (!buffer_unchanged_p)
13157 w->base_line_number = Qnil;
13159 /* Move backward half the height of the window. */
13160 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13161 it.current_y = it.last_visible_y;
13162 move_it_vertically_backward (&it, centering_position);
13163 xassert (IT_CHARPOS (it) >= BEGV);
13165 /* The function move_it_vertically_backward may move over more
13166 than the specified y-distance. If it->w is small, e.g. a
13167 mini-buffer window, we may end up in front of the window's
13168 display area. Start displaying at the start of the line
13169 containing PT in this case. */
13170 if (it.current_y <= 0)
13172 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13173 move_it_vertically_backward (&it, 0);
13174 #if 0
13175 /* I think this assert is bogus if buffer contains
13176 invisible text or images. KFS. */
13177 xassert (IT_CHARPOS (it) <= PT);
13178 #endif
13179 it.current_y = 0;
13182 it.current_x = it.hpos = 0;
13184 /* Set startp here explicitly in case that helps avoid an infinite loop
13185 in case the window-scroll-functions functions get errors. */
13186 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13188 /* Run scroll hooks. */
13189 startp = run_window_scroll_functions (window, it.current.pos);
13191 /* Redisplay the window. */
13192 if (!current_matrix_up_to_date_p
13193 || windows_or_buffers_changed
13194 || cursor_type_changed
13195 /* Don't use try_window_reusing_current_matrix in this case
13196 because it can have changed the buffer. */
13197 || !NILP (Vwindow_scroll_functions)
13198 || !just_this_one_p
13199 || MINI_WINDOW_P (w)
13200 || !(used_current_matrix_p
13201 = try_window_reusing_current_matrix (w)))
13202 try_window (window, startp, 0);
13204 /* If new fonts have been loaded (due to fontsets), give up. We
13205 have to start a new redisplay since we need to re-adjust glyph
13206 matrices. */
13207 if (fonts_changed_p)
13208 goto need_larger_matrices;
13210 /* If cursor did not appear assume that the middle of the window is
13211 in the first line of the window. Do it again with the next line.
13212 (Imagine a window of height 100, displaying two lines of height
13213 60. Moving back 50 from it->last_visible_y will end in the first
13214 line.) */
13215 if (w->cursor.vpos < 0)
13217 if (!NILP (w->window_end_valid)
13218 && PT >= Z - XFASTINT (w->window_end_pos))
13220 clear_glyph_matrix (w->desired_matrix);
13221 move_it_by_lines (&it, 1, 0);
13222 try_window (window, it.current.pos, 0);
13224 else if (PT < IT_CHARPOS (it))
13226 clear_glyph_matrix (w->desired_matrix);
13227 move_it_by_lines (&it, -1, 0);
13228 try_window (window, it.current.pos, 0);
13230 else
13232 /* Not much we can do about it. */
13236 /* Consider the following case: Window starts at BEGV, there is
13237 invisible, intangible text at BEGV, so that display starts at
13238 some point START > BEGV. It can happen that we are called with
13239 PT somewhere between BEGV and START. Try to handle that case. */
13240 if (w->cursor.vpos < 0)
13242 struct glyph_row *row = w->current_matrix->rows;
13243 if (row->mode_line_p)
13244 ++row;
13245 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13248 if (!cursor_row_fully_visible_p (w, 0, 0))
13250 /* If vscroll is enabled, disable it and try again. */
13251 if (w->vscroll)
13253 w->vscroll = 0;
13254 clear_glyph_matrix (w->desired_matrix);
13255 goto recenter;
13258 /* If centering point failed to make the whole line visible,
13259 put point at the top instead. That has to make the whole line
13260 visible, if it can be done. */
13261 if (centering_position == 0)
13262 goto done;
13264 clear_glyph_matrix (w->desired_matrix);
13265 centering_position = 0;
13266 goto recenter;
13269 done:
13271 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13272 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13273 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13274 ? Qt : Qnil);
13276 /* Display the mode line, if we must. */
13277 if ((update_mode_line
13278 /* If window not full width, must redo its mode line
13279 if (a) the window to its side is being redone and
13280 (b) we do a frame-based redisplay. This is a consequence
13281 of how inverted lines are drawn in frame-based redisplay. */
13282 || (!just_this_one_p
13283 && !FRAME_WINDOW_P (f)
13284 && !WINDOW_FULL_WIDTH_P (w))
13285 /* Line number to display. */
13286 || INTEGERP (w->base_line_pos)
13287 /* Column number is displayed and different from the one displayed. */
13288 || (!NILP (w->column_number_displayed)
13289 && (XFASTINT (w->column_number_displayed)
13290 != (int) current_column ()))) /* iftc */
13291 /* This means that the window has a mode line. */
13292 && (WINDOW_WANTS_MODELINE_P (w)
13293 || WINDOW_WANTS_HEADER_LINE_P (w)))
13295 display_mode_lines (w);
13297 /* If mode line height has changed, arrange for a thorough
13298 immediate redisplay using the correct mode line height. */
13299 if (WINDOW_WANTS_MODELINE_P (w)
13300 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13302 fonts_changed_p = 1;
13303 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13304 = DESIRED_MODE_LINE_HEIGHT (w);
13307 /* If top line height has changed, arrange for a thorough
13308 immediate redisplay using the correct mode line height. */
13309 if (WINDOW_WANTS_HEADER_LINE_P (w)
13310 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13312 fonts_changed_p = 1;
13313 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13314 = DESIRED_HEADER_LINE_HEIGHT (w);
13317 if (fonts_changed_p)
13318 goto need_larger_matrices;
13321 if (!line_number_displayed
13322 && !BUFFERP (w->base_line_pos))
13324 w->base_line_pos = Qnil;
13325 w->base_line_number = Qnil;
13328 finish_menu_bars:
13330 /* When we reach a frame's selected window, redo the frame's menu bar. */
13331 if (update_mode_line
13332 && EQ (FRAME_SELECTED_WINDOW (f), window))
13334 int redisplay_menu_p = 0;
13335 int redisplay_tool_bar_p = 0;
13337 if (FRAME_WINDOW_P (f))
13339 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13340 || defined (USE_GTK)
13341 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13342 #else
13343 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13344 #endif
13346 else
13347 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13349 if (redisplay_menu_p)
13350 display_menu_bar (w);
13352 #ifdef HAVE_WINDOW_SYSTEM
13353 #ifdef USE_GTK
13354 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13355 #else
13356 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13357 && (FRAME_TOOL_BAR_LINES (f) > 0
13358 || auto_resize_tool_bars_p);
13360 #endif
13362 if (redisplay_tool_bar_p)
13363 redisplay_tool_bar (f);
13364 #endif
13367 #ifdef HAVE_WINDOW_SYSTEM
13368 if (FRAME_WINDOW_P (f)
13369 && update_window_fringes (w, (just_this_one_p
13370 || (!used_current_matrix_p && !overlay_arrow_seen)
13371 || w->pseudo_window_p)))
13373 update_begin (f);
13374 BLOCK_INPUT;
13375 if (draw_window_fringes (w, 1))
13376 x_draw_vertical_border (w);
13377 UNBLOCK_INPUT;
13378 update_end (f);
13380 #endif /* HAVE_WINDOW_SYSTEM */
13382 /* We go to this label, with fonts_changed_p nonzero,
13383 if it is necessary to try again using larger glyph matrices.
13384 We have to redeem the scroll bar even in this case,
13385 because the loop in redisplay_internal expects that. */
13386 need_larger_matrices:
13388 finish_scroll_bars:
13390 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13392 /* Set the thumb's position and size. */
13393 set_vertical_scroll_bar (w);
13395 /* Note that we actually used the scroll bar attached to this
13396 window, so it shouldn't be deleted at the end of redisplay. */
13397 redeem_scroll_bar_hook (w);
13400 /* Restore current_buffer and value of point in it. */
13401 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13402 set_buffer_internal_1 (old);
13403 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13405 unbind_to (count, Qnil);
13409 /* Build the complete desired matrix of WINDOW with a window start
13410 buffer position POS.
13412 Value is 1 if successful. It is zero if fonts were loaded during
13413 redisplay which makes re-adjusting glyph matrices necessary, and -1
13414 if point would appear in the scroll margins.
13415 (We check that only if CHECK_MARGINS is nonzero. */
13418 try_window (window, pos, check_margins)
13419 Lisp_Object window;
13420 struct text_pos pos;
13421 int check_margins;
13423 struct window *w = XWINDOW (window);
13424 struct it it;
13425 struct glyph_row *last_text_row = NULL;
13427 /* Make POS the new window start. */
13428 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13430 /* Mark cursor position as unknown. No overlay arrow seen. */
13431 w->cursor.vpos = -1;
13432 overlay_arrow_seen = 0;
13434 /* Initialize iterator and info to start at POS. */
13435 start_display (&it, w, pos);
13437 /* Display all lines of W. */
13438 while (it.current_y < it.last_visible_y)
13440 if (display_line (&it))
13441 last_text_row = it.glyph_row - 1;
13442 if (fonts_changed_p)
13443 return 0;
13446 /* Don't let the cursor end in the scroll margins. */
13447 if (check_margins
13448 && !MINI_WINDOW_P (w))
13450 int this_scroll_margin;
13452 this_scroll_margin = max (0, scroll_margin);
13453 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13454 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13456 if ((w->cursor.y < this_scroll_margin
13457 && CHARPOS (pos) > BEGV
13458 && IT_CHARPOS (it) < ZV)
13459 /* rms: considering make_cursor_line_fully_visible_p here
13460 seems to give wrong results. We don't want to recenter
13461 when the last line is partly visible, we want to allow
13462 that case to be handled in the usual way. */
13463 || (w->cursor.y + 1) > it.last_visible_y)
13465 w->cursor.vpos = -1;
13466 clear_glyph_matrix (w->desired_matrix);
13467 return -1;
13471 /* If bottom moved off end of frame, change mode line percentage. */
13472 if (XFASTINT (w->window_end_pos) <= 0
13473 && Z != IT_CHARPOS (it))
13474 w->update_mode_line = Qt;
13476 /* Set window_end_pos to the offset of the last character displayed
13477 on the window from the end of current_buffer. Set
13478 window_end_vpos to its row number. */
13479 if (last_text_row)
13481 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13482 w->window_end_bytepos
13483 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13484 w->window_end_pos
13485 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13486 w->window_end_vpos
13487 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13488 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13489 ->displays_text_p);
13491 else
13493 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13494 w->window_end_pos = make_number (Z - ZV);
13495 w->window_end_vpos = make_number (0);
13498 /* But that is not valid info until redisplay finishes. */
13499 w->window_end_valid = Qnil;
13500 return 1;
13505 /************************************************************************
13506 Window redisplay reusing current matrix when buffer has not changed
13507 ************************************************************************/
13509 /* Try redisplay of window W showing an unchanged buffer with a
13510 different window start than the last time it was displayed by
13511 reusing its current matrix. Value is non-zero if successful.
13512 W->start is the new window start. */
13514 static int
13515 try_window_reusing_current_matrix (w)
13516 struct window *w;
13518 struct frame *f = XFRAME (w->frame);
13519 struct glyph_row *row, *bottom_row;
13520 struct it it;
13521 struct run run;
13522 struct text_pos start, new_start;
13523 int nrows_scrolled, i;
13524 struct glyph_row *last_text_row;
13525 struct glyph_row *last_reused_text_row;
13526 struct glyph_row *start_row;
13527 int start_vpos, min_y, max_y;
13529 #if GLYPH_DEBUG
13530 if (inhibit_try_window_reusing)
13531 return 0;
13532 #endif
13534 if (/* This function doesn't handle terminal frames. */
13535 !FRAME_WINDOW_P (f)
13536 /* Don't try to reuse the display if windows have been split
13537 or such. */
13538 || windows_or_buffers_changed
13539 || cursor_type_changed)
13540 return 0;
13542 /* Can't do this if region may have changed. */
13543 if ((!NILP (Vtransient_mark_mode)
13544 && !NILP (current_buffer->mark_active))
13545 || !NILP (w->region_showing)
13546 || !NILP (Vshow_trailing_whitespace))
13547 return 0;
13549 /* If top-line visibility has changed, give up. */
13550 if (WINDOW_WANTS_HEADER_LINE_P (w)
13551 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13552 return 0;
13554 /* Give up if old or new display is scrolled vertically. We could
13555 make this function handle this, but right now it doesn't. */
13556 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13557 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13558 return 0;
13560 /* The variable new_start now holds the new window start. The old
13561 start `start' can be determined from the current matrix. */
13562 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13563 start = start_row->start.pos;
13564 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13566 /* Clear the desired matrix for the display below. */
13567 clear_glyph_matrix (w->desired_matrix);
13569 if (CHARPOS (new_start) <= CHARPOS (start))
13571 int first_row_y;
13573 /* Don't use this method if the display starts with an ellipsis
13574 displayed for invisible text. It's not easy to handle that case
13575 below, and it's certainly not worth the effort since this is
13576 not a frequent case. */
13577 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13578 return 0;
13580 IF_DEBUG (debug_method_add (w, "twu1"));
13582 /* Display up to a row that can be reused. The variable
13583 last_text_row is set to the last row displayed that displays
13584 text. Note that it.vpos == 0 if or if not there is a
13585 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13586 start_display (&it, w, new_start);
13587 first_row_y = it.current_y;
13588 w->cursor.vpos = -1;
13589 last_text_row = last_reused_text_row = NULL;
13591 while (it.current_y < it.last_visible_y
13592 && !fonts_changed_p)
13594 /* If we have reached into the characters in the START row,
13595 that means the line boundaries have changed. So we
13596 can't start copying with the row START. Maybe it will
13597 work to start copying with the following row. */
13598 while (IT_CHARPOS (it) > CHARPOS (start))
13600 /* Advance to the next row as the "start". */
13601 start_row++;
13602 start = start_row->start.pos;
13603 /* If there are no more rows to try, or just one, give up. */
13604 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13605 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13606 || CHARPOS (start) == ZV)
13608 clear_glyph_matrix (w->desired_matrix);
13609 return 0;
13612 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13614 /* If we have reached alignment,
13615 we can copy the rest of the rows. */
13616 if (IT_CHARPOS (it) == CHARPOS (start))
13617 break;
13619 if (display_line (&it))
13620 last_text_row = it.glyph_row - 1;
13623 /* A value of current_y < last_visible_y means that we stopped
13624 at the previous window start, which in turn means that we
13625 have at least one reusable row. */
13626 if (it.current_y < it.last_visible_y)
13628 /* IT.vpos always starts from 0; it counts text lines. */
13629 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13631 /* Find PT if not already found in the lines displayed. */
13632 if (w->cursor.vpos < 0)
13634 int dy = it.current_y - start_row->y;
13636 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13637 row = row_containing_pos (w, PT, row, NULL, dy);
13638 if (row)
13639 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13640 dy, nrows_scrolled);
13641 else
13643 clear_glyph_matrix (w->desired_matrix);
13644 return 0;
13648 /* Scroll the display. Do it before the current matrix is
13649 changed. The problem here is that update has not yet
13650 run, i.e. part of the current matrix is not up to date.
13651 scroll_run_hook will clear the cursor, and use the
13652 current matrix to get the height of the row the cursor is
13653 in. */
13654 run.current_y = start_row->y;
13655 run.desired_y = it.current_y;
13656 run.height = it.last_visible_y - it.current_y;
13658 if (run.height > 0 && run.current_y != run.desired_y)
13660 update_begin (f);
13661 rif->update_window_begin_hook (w);
13662 rif->clear_window_mouse_face (w);
13663 rif->scroll_run_hook (w, &run);
13664 rif->update_window_end_hook (w, 0, 0);
13665 update_end (f);
13668 /* Shift current matrix down by nrows_scrolled lines. */
13669 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13670 rotate_matrix (w->current_matrix,
13671 start_vpos,
13672 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13673 nrows_scrolled);
13675 /* Disable lines that must be updated. */
13676 for (i = 0; i < it.vpos; ++i)
13677 (start_row + i)->enabled_p = 0;
13679 /* Re-compute Y positions. */
13680 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13681 max_y = it.last_visible_y;
13682 for (row = start_row + nrows_scrolled;
13683 row < bottom_row;
13684 ++row)
13686 row->y = it.current_y;
13687 row->visible_height = row->height;
13689 if (row->y < min_y)
13690 row->visible_height -= min_y - row->y;
13691 if (row->y + row->height > max_y)
13692 row->visible_height -= row->y + row->height - max_y;
13693 row->redraw_fringe_bitmaps_p = 1;
13695 it.current_y += row->height;
13697 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13698 last_reused_text_row = row;
13699 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13700 break;
13703 /* Disable lines in the current matrix which are now
13704 below the window. */
13705 for (++row; row < bottom_row; ++row)
13706 row->enabled_p = row->mode_line_p = 0;
13709 /* Update window_end_pos etc.; last_reused_text_row is the last
13710 reused row from the current matrix containing text, if any.
13711 The value of last_text_row is the last displayed line
13712 containing text. */
13713 if (last_reused_text_row)
13715 w->window_end_bytepos
13716 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13717 w->window_end_pos
13718 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13719 w->window_end_vpos
13720 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13721 w->current_matrix));
13723 else if (last_text_row)
13725 w->window_end_bytepos
13726 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13727 w->window_end_pos
13728 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13729 w->window_end_vpos
13730 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13732 else
13734 /* This window must be completely empty. */
13735 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13736 w->window_end_pos = make_number (Z - ZV);
13737 w->window_end_vpos = make_number (0);
13739 w->window_end_valid = Qnil;
13741 /* Update hint: don't try scrolling again in update_window. */
13742 w->desired_matrix->no_scrolling_p = 1;
13744 #if GLYPH_DEBUG
13745 debug_method_add (w, "try_window_reusing_current_matrix 1");
13746 #endif
13747 return 1;
13749 else if (CHARPOS (new_start) > CHARPOS (start))
13751 struct glyph_row *pt_row, *row;
13752 struct glyph_row *first_reusable_row;
13753 struct glyph_row *first_row_to_display;
13754 int dy;
13755 int yb = window_text_bottom_y (w);
13757 /* Find the row starting at new_start, if there is one. Don't
13758 reuse a partially visible line at the end. */
13759 first_reusable_row = start_row;
13760 while (first_reusable_row->enabled_p
13761 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13762 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13763 < CHARPOS (new_start)))
13764 ++first_reusable_row;
13766 /* Give up if there is no row to reuse. */
13767 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13768 || !first_reusable_row->enabled_p
13769 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13770 != CHARPOS (new_start)))
13771 return 0;
13773 /* We can reuse fully visible rows beginning with
13774 first_reusable_row to the end of the window. Set
13775 first_row_to_display to the first row that cannot be reused.
13776 Set pt_row to the row containing point, if there is any. */
13777 pt_row = NULL;
13778 for (first_row_to_display = first_reusable_row;
13779 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13780 ++first_row_to_display)
13782 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13783 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13784 pt_row = first_row_to_display;
13787 /* Start displaying at the start of first_row_to_display. */
13788 xassert (first_row_to_display->y < yb);
13789 init_to_row_start (&it, w, first_row_to_display);
13791 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13792 - start_vpos);
13793 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13794 - nrows_scrolled);
13795 it.current_y = (first_row_to_display->y - first_reusable_row->y
13796 + WINDOW_HEADER_LINE_HEIGHT (w));
13798 /* Display lines beginning with first_row_to_display in the
13799 desired matrix. Set last_text_row to the last row displayed
13800 that displays text. */
13801 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13802 if (pt_row == NULL)
13803 w->cursor.vpos = -1;
13804 last_text_row = NULL;
13805 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13806 if (display_line (&it))
13807 last_text_row = it.glyph_row - 1;
13809 /* Give up If point isn't in a row displayed or reused. */
13810 if (w->cursor.vpos < 0)
13812 clear_glyph_matrix (w->desired_matrix);
13813 return 0;
13816 /* If point is in a reused row, adjust y and vpos of the cursor
13817 position. */
13818 if (pt_row)
13820 w->cursor.vpos -= nrows_scrolled;
13821 w->cursor.y -= first_reusable_row->y - start_row->y;
13824 /* Scroll the display. */
13825 run.current_y = first_reusable_row->y;
13826 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13827 run.height = it.last_visible_y - run.current_y;
13828 dy = run.current_y - run.desired_y;
13830 if (run.height)
13832 update_begin (f);
13833 rif->update_window_begin_hook (w);
13834 rif->clear_window_mouse_face (w);
13835 rif->scroll_run_hook (w, &run);
13836 rif->update_window_end_hook (w, 0, 0);
13837 update_end (f);
13840 /* Adjust Y positions of reused rows. */
13841 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13842 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13843 max_y = it.last_visible_y;
13844 for (row = first_reusable_row; row < first_row_to_display; ++row)
13846 row->y -= dy;
13847 row->visible_height = row->height;
13848 if (row->y < min_y)
13849 row->visible_height -= min_y - row->y;
13850 if (row->y + row->height > max_y)
13851 row->visible_height -= row->y + row->height - max_y;
13852 row->redraw_fringe_bitmaps_p = 1;
13855 /* Scroll the current matrix. */
13856 xassert (nrows_scrolled > 0);
13857 rotate_matrix (w->current_matrix,
13858 start_vpos,
13859 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13860 -nrows_scrolled);
13862 /* Disable rows not reused. */
13863 for (row -= nrows_scrolled; row < bottom_row; ++row)
13864 row->enabled_p = 0;
13866 /* Point may have moved to a different line, so we cannot assume that
13867 the previous cursor position is valid; locate the correct row. */
13868 if (pt_row)
13870 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13871 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13872 row++)
13874 w->cursor.vpos++;
13875 w->cursor.y = row->y;
13877 if (row < bottom_row)
13879 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13880 while (glyph->charpos < PT)
13882 w->cursor.hpos++;
13883 w->cursor.x += glyph->pixel_width;
13884 glyph++;
13889 /* Adjust window end. A null value of last_text_row means that
13890 the window end is in reused rows which in turn means that
13891 only its vpos can have changed. */
13892 if (last_text_row)
13894 w->window_end_bytepos
13895 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13896 w->window_end_pos
13897 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13898 w->window_end_vpos
13899 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13901 else
13903 w->window_end_vpos
13904 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13907 w->window_end_valid = Qnil;
13908 w->desired_matrix->no_scrolling_p = 1;
13910 #if GLYPH_DEBUG
13911 debug_method_add (w, "try_window_reusing_current_matrix 2");
13912 #endif
13913 return 1;
13916 return 0;
13921 /************************************************************************
13922 Window redisplay reusing current matrix when buffer has changed
13923 ************************************************************************/
13925 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13926 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13927 int *, int *));
13928 static struct glyph_row *
13929 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13930 struct glyph_row *));
13933 /* Return the last row in MATRIX displaying text. If row START is
13934 non-null, start searching with that row. IT gives the dimensions
13935 of the display. Value is null if matrix is empty; otherwise it is
13936 a pointer to the row found. */
13938 static struct glyph_row *
13939 find_last_row_displaying_text (matrix, it, start)
13940 struct glyph_matrix *matrix;
13941 struct it *it;
13942 struct glyph_row *start;
13944 struct glyph_row *row, *row_found;
13946 /* Set row_found to the last row in IT->w's current matrix
13947 displaying text. The loop looks funny but think of partially
13948 visible lines. */
13949 row_found = NULL;
13950 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13951 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13953 xassert (row->enabled_p);
13954 row_found = row;
13955 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13956 break;
13957 ++row;
13960 return row_found;
13964 /* Return the last row in the current matrix of W that is not affected
13965 by changes at the start of current_buffer that occurred since W's
13966 current matrix was built. Value is null if no such row exists.
13968 BEG_UNCHANGED us the number of characters unchanged at the start of
13969 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13970 first changed character in current_buffer. Characters at positions <
13971 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13972 when the current matrix was built. */
13974 static struct glyph_row *
13975 find_last_unchanged_at_beg_row (w)
13976 struct window *w;
13978 int first_changed_pos = BEG + BEG_UNCHANGED;
13979 struct glyph_row *row;
13980 struct glyph_row *row_found = NULL;
13981 int yb = window_text_bottom_y (w);
13983 /* Find the last row displaying unchanged text. */
13984 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13985 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13986 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13988 if (/* If row ends before first_changed_pos, it is unchanged,
13989 except in some case. */
13990 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13991 /* When row ends in ZV and we write at ZV it is not
13992 unchanged. */
13993 && !row->ends_at_zv_p
13994 /* When first_changed_pos is the end of a continued line,
13995 row is not unchanged because it may be no longer
13996 continued. */
13997 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13998 && (row->continued_p
13999 || row->exact_window_width_line_p)))
14000 row_found = row;
14002 /* Stop if last visible row. */
14003 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14004 break;
14006 ++row;
14009 return row_found;
14013 /* Find the first glyph row in the current matrix of W that is not
14014 affected by changes at the end of current_buffer since the
14015 time W's current matrix was built.
14017 Return in *DELTA the number of chars by which buffer positions in
14018 unchanged text at the end of current_buffer must be adjusted.
14020 Return in *DELTA_BYTES the corresponding number of bytes.
14022 Value is null if no such row exists, i.e. all rows are affected by
14023 changes. */
14025 static struct glyph_row *
14026 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14027 struct window *w;
14028 int *delta, *delta_bytes;
14030 struct glyph_row *row;
14031 struct glyph_row *row_found = NULL;
14033 *delta = *delta_bytes = 0;
14035 /* Display must not have been paused, otherwise the current matrix
14036 is not up to date. */
14037 if (NILP (w->window_end_valid))
14038 abort ();
14040 /* A value of window_end_pos >= END_UNCHANGED means that the window
14041 end is in the range of changed text. If so, there is no
14042 unchanged row at the end of W's current matrix. */
14043 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14044 return NULL;
14046 /* Set row to the last row in W's current matrix displaying text. */
14047 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14049 /* If matrix is entirely empty, no unchanged row exists. */
14050 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14052 /* The value of row is the last glyph row in the matrix having a
14053 meaningful buffer position in it. The end position of row
14054 corresponds to window_end_pos. This allows us to translate
14055 buffer positions in the current matrix to current buffer
14056 positions for characters not in changed text. */
14057 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14058 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14059 int last_unchanged_pos, last_unchanged_pos_old;
14060 struct glyph_row *first_text_row
14061 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14063 *delta = Z - Z_old;
14064 *delta_bytes = Z_BYTE - Z_BYTE_old;
14066 /* Set last_unchanged_pos to the buffer position of the last
14067 character in the buffer that has not been changed. Z is the
14068 index + 1 of the last character in current_buffer, i.e. by
14069 subtracting END_UNCHANGED we get the index of the last
14070 unchanged character, and we have to add BEG to get its buffer
14071 position. */
14072 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14073 last_unchanged_pos_old = last_unchanged_pos - *delta;
14075 /* Search backward from ROW for a row displaying a line that
14076 starts at a minimum position >= last_unchanged_pos_old. */
14077 for (; row > first_text_row; --row)
14079 /* This used to abort, but it can happen.
14080 It is ok to just stop the search instead here. KFS. */
14081 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14082 break;
14084 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14085 row_found = row;
14089 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14090 abort ();
14092 return row_found;
14096 /* Make sure that glyph rows in the current matrix of window W
14097 reference the same glyph memory as corresponding rows in the
14098 frame's frame matrix. This function is called after scrolling W's
14099 current matrix on a terminal frame in try_window_id and
14100 try_window_reusing_current_matrix. */
14102 static void
14103 sync_frame_with_window_matrix_rows (w)
14104 struct window *w;
14106 struct frame *f = XFRAME (w->frame);
14107 struct glyph_row *window_row, *window_row_end, *frame_row;
14109 /* Preconditions: W must be a leaf window and full-width. Its frame
14110 must have a frame matrix. */
14111 xassert (NILP (w->hchild) && NILP (w->vchild));
14112 xassert (WINDOW_FULL_WIDTH_P (w));
14113 xassert (!FRAME_WINDOW_P (f));
14115 /* If W is a full-width window, glyph pointers in W's current matrix
14116 have, by definition, to be the same as glyph pointers in the
14117 corresponding frame matrix. Note that frame matrices have no
14118 marginal areas (see build_frame_matrix). */
14119 window_row = w->current_matrix->rows;
14120 window_row_end = window_row + w->current_matrix->nrows;
14121 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14122 while (window_row < window_row_end)
14124 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14125 struct glyph *end = window_row->glyphs[LAST_AREA];
14127 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14128 frame_row->glyphs[TEXT_AREA] = start;
14129 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14130 frame_row->glyphs[LAST_AREA] = end;
14132 /* Disable frame rows whose corresponding window rows have
14133 been disabled in try_window_id. */
14134 if (!window_row->enabled_p)
14135 frame_row->enabled_p = 0;
14137 ++window_row, ++frame_row;
14142 /* Find the glyph row in window W containing CHARPOS. Consider all
14143 rows between START and END (not inclusive). END null means search
14144 all rows to the end of the display area of W. Value is the row
14145 containing CHARPOS or null. */
14147 struct glyph_row *
14148 row_containing_pos (w, charpos, start, end, dy)
14149 struct window *w;
14150 int charpos;
14151 struct glyph_row *start, *end;
14152 int dy;
14154 struct glyph_row *row = start;
14155 int last_y;
14157 /* If we happen to start on a header-line, skip that. */
14158 if (row->mode_line_p)
14159 ++row;
14161 if ((end && row >= end) || !row->enabled_p)
14162 return NULL;
14164 last_y = window_text_bottom_y (w) - dy;
14166 while (1)
14168 /* Give up if we have gone too far. */
14169 if (end && row >= end)
14170 return NULL;
14171 /* This formerly returned if they were equal.
14172 I think that both quantities are of a "last plus one" type;
14173 if so, when they are equal, the row is within the screen. -- rms. */
14174 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14175 return NULL;
14177 /* If it is in this row, return this row. */
14178 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14179 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14180 /* The end position of a row equals the start
14181 position of the next row. If CHARPOS is there, we
14182 would rather display it in the next line, except
14183 when this line ends in ZV. */
14184 && !row->ends_at_zv_p
14185 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14186 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14187 return row;
14188 ++row;
14193 /* Try to redisplay window W by reusing its existing display. W's
14194 current matrix must be up to date when this function is called,
14195 i.e. window_end_valid must not be nil.
14197 Value is
14199 1 if display has been updated
14200 0 if otherwise unsuccessful
14201 -1 if redisplay with same window start is known not to succeed
14203 The following steps are performed:
14205 1. Find the last row in the current matrix of W that is not
14206 affected by changes at the start of current_buffer. If no such row
14207 is found, give up.
14209 2. Find the first row in W's current matrix that is not affected by
14210 changes at the end of current_buffer. Maybe there is no such row.
14212 3. Display lines beginning with the row + 1 found in step 1 to the
14213 row found in step 2 or, if step 2 didn't find a row, to the end of
14214 the window.
14216 4. If cursor is not known to appear on the window, give up.
14218 5. If display stopped at the row found in step 2, scroll the
14219 display and current matrix as needed.
14221 6. Maybe display some lines at the end of W, if we must. This can
14222 happen under various circumstances, like a partially visible line
14223 becoming fully visible, or because newly displayed lines are displayed
14224 in smaller font sizes.
14226 7. Update W's window end information. */
14228 static int
14229 try_window_id (w)
14230 struct window *w;
14232 struct frame *f = XFRAME (w->frame);
14233 struct glyph_matrix *current_matrix = w->current_matrix;
14234 struct glyph_matrix *desired_matrix = w->desired_matrix;
14235 struct glyph_row *last_unchanged_at_beg_row;
14236 struct glyph_row *first_unchanged_at_end_row;
14237 struct glyph_row *row;
14238 struct glyph_row *bottom_row;
14239 int bottom_vpos;
14240 struct it it;
14241 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14242 struct text_pos start_pos;
14243 struct run run;
14244 int first_unchanged_at_end_vpos = 0;
14245 struct glyph_row *last_text_row, *last_text_row_at_end;
14246 struct text_pos start;
14247 int first_changed_charpos, last_changed_charpos;
14249 #if GLYPH_DEBUG
14250 if (inhibit_try_window_id)
14251 return 0;
14252 #endif
14254 /* This is handy for debugging. */
14255 #if 0
14256 #define GIVE_UP(X) \
14257 do { \
14258 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14259 return 0; \
14260 } while (0)
14261 #else
14262 #define GIVE_UP(X) return 0
14263 #endif
14265 SET_TEXT_POS_FROM_MARKER (start, w->start);
14267 /* Don't use this for mini-windows because these can show
14268 messages and mini-buffers, and we don't handle that here. */
14269 if (MINI_WINDOW_P (w))
14270 GIVE_UP (1);
14272 /* This flag is used to prevent redisplay optimizations. */
14273 if (windows_or_buffers_changed || cursor_type_changed)
14274 GIVE_UP (2);
14276 /* Verify that narrowing has not changed.
14277 Also verify that we were not told to prevent redisplay optimizations.
14278 It would be nice to further
14279 reduce the number of cases where this prevents try_window_id. */
14280 if (current_buffer->clip_changed
14281 || current_buffer->prevent_redisplay_optimizations_p)
14282 GIVE_UP (3);
14284 /* Window must either use window-based redisplay or be full width. */
14285 if (!FRAME_WINDOW_P (f)
14286 && (!line_ins_del_ok
14287 || !WINDOW_FULL_WIDTH_P (w)))
14288 GIVE_UP (4);
14290 /* Give up if point is not known NOT to appear in W. */
14291 if (PT < CHARPOS (start))
14292 GIVE_UP (5);
14294 /* Another way to prevent redisplay optimizations. */
14295 if (XFASTINT (w->last_modified) == 0)
14296 GIVE_UP (6);
14298 /* Verify that window is not hscrolled. */
14299 if (XFASTINT (w->hscroll) != 0)
14300 GIVE_UP (7);
14302 /* Verify that display wasn't paused. */
14303 if (NILP (w->window_end_valid))
14304 GIVE_UP (8);
14306 /* Can't use this if highlighting a region because a cursor movement
14307 will do more than just set the cursor. */
14308 if (!NILP (Vtransient_mark_mode)
14309 && !NILP (current_buffer->mark_active))
14310 GIVE_UP (9);
14312 /* Likewise if highlighting trailing whitespace. */
14313 if (!NILP (Vshow_trailing_whitespace))
14314 GIVE_UP (11);
14316 /* Likewise if showing a region. */
14317 if (!NILP (w->region_showing))
14318 GIVE_UP (10);
14320 /* Can use this if overlay arrow position and or string have changed. */
14321 if (overlay_arrows_changed_p ())
14322 GIVE_UP (12);
14325 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14326 only if buffer has really changed. The reason is that the gap is
14327 initially at Z for freshly visited files. The code below would
14328 set end_unchanged to 0 in that case. */
14329 if (MODIFF > SAVE_MODIFF
14330 /* This seems to happen sometimes after saving a buffer. */
14331 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14333 if (GPT - BEG < BEG_UNCHANGED)
14334 BEG_UNCHANGED = GPT - BEG;
14335 if (Z - GPT < END_UNCHANGED)
14336 END_UNCHANGED = Z - GPT;
14339 /* The position of the first and last character that has been changed. */
14340 first_changed_charpos = BEG + BEG_UNCHANGED;
14341 last_changed_charpos = Z - END_UNCHANGED;
14343 /* If window starts after a line end, and the last change is in
14344 front of that newline, then changes don't affect the display.
14345 This case happens with stealth-fontification. Note that although
14346 the display is unchanged, glyph positions in the matrix have to
14347 be adjusted, of course. */
14348 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14349 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14350 && ((last_changed_charpos < CHARPOS (start)
14351 && CHARPOS (start) == BEGV)
14352 || (last_changed_charpos < CHARPOS (start) - 1
14353 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14355 int Z_old, delta, Z_BYTE_old, delta_bytes;
14356 struct glyph_row *r0;
14358 /* Compute how many chars/bytes have been added to or removed
14359 from the buffer. */
14360 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14361 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14362 delta = Z - Z_old;
14363 delta_bytes = Z_BYTE - Z_BYTE_old;
14365 /* Give up if PT is not in the window. Note that it already has
14366 been checked at the start of try_window_id that PT is not in
14367 front of the window start. */
14368 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14369 GIVE_UP (13);
14371 /* If window start is unchanged, we can reuse the whole matrix
14372 as is, after adjusting glyph positions. No need to compute
14373 the window end again, since its offset from Z hasn't changed. */
14374 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14375 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14376 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14377 /* PT must not be in a partially visible line. */
14378 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14379 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14381 /* Adjust positions in the glyph matrix. */
14382 if (delta || delta_bytes)
14384 struct glyph_row *r1
14385 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14386 increment_matrix_positions (w->current_matrix,
14387 MATRIX_ROW_VPOS (r0, current_matrix),
14388 MATRIX_ROW_VPOS (r1, current_matrix),
14389 delta, delta_bytes);
14392 /* Set the cursor. */
14393 row = row_containing_pos (w, PT, r0, NULL, 0);
14394 if (row)
14395 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14396 else
14397 abort ();
14398 return 1;
14402 /* Handle the case that changes are all below what is displayed in
14403 the window, and that PT is in the window. This shortcut cannot
14404 be taken if ZV is visible in the window, and text has been added
14405 there that is visible in the window. */
14406 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14407 /* ZV is not visible in the window, or there are no
14408 changes at ZV, actually. */
14409 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14410 || first_changed_charpos == last_changed_charpos))
14412 struct glyph_row *r0;
14414 /* Give up if PT is not in the window. Note that it already has
14415 been checked at the start of try_window_id that PT is not in
14416 front of the window start. */
14417 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14418 GIVE_UP (14);
14420 /* If window start is unchanged, we can reuse the whole matrix
14421 as is, without changing glyph positions since no text has
14422 been added/removed in front of the window end. */
14423 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14424 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14425 /* PT must not be in a partially visible line. */
14426 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14427 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14429 /* We have to compute the window end anew since text
14430 can have been added/removed after it. */
14431 w->window_end_pos
14432 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14433 w->window_end_bytepos
14434 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14436 /* Set the cursor. */
14437 row = row_containing_pos (w, PT, r0, NULL, 0);
14438 if (row)
14439 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14440 else
14441 abort ();
14442 return 2;
14446 /* Give up if window start is in the changed area.
14448 The condition used to read
14450 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14452 but why that was tested escapes me at the moment. */
14453 if (CHARPOS (start) >= first_changed_charpos
14454 && CHARPOS (start) <= last_changed_charpos)
14455 GIVE_UP (15);
14457 /* Check that window start agrees with the start of the first glyph
14458 row in its current matrix. Check this after we know the window
14459 start is not in changed text, otherwise positions would not be
14460 comparable. */
14461 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14462 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14463 GIVE_UP (16);
14465 /* Give up if the window ends in strings. Overlay strings
14466 at the end are difficult to handle, so don't try. */
14467 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14468 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14469 GIVE_UP (20);
14471 /* Compute the position at which we have to start displaying new
14472 lines. Some of the lines at the top of the window might be
14473 reusable because they are not displaying changed text. Find the
14474 last row in W's current matrix not affected by changes at the
14475 start of current_buffer. Value is null if changes start in the
14476 first line of window. */
14477 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14478 if (last_unchanged_at_beg_row)
14480 /* Avoid starting to display in the moddle of a character, a TAB
14481 for instance. This is easier than to set up the iterator
14482 exactly, and it's not a frequent case, so the additional
14483 effort wouldn't really pay off. */
14484 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14485 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14486 && last_unchanged_at_beg_row > w->current_matrix->rows)
14487 --last_unchanged_at_beg_row;
14489 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14490 GIVE_UP (17);
14492 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14493 GIVE_UP (18);
14494 start_pos = it.current.pos;
14496 /* Start displaying new lines in the desired matrix at the same
14497 vpos we would use in the current matrix, i.e. below
14498 last_unchanged_at_beg_row. */
14499 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14500 current_matrix);
14501 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14502 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14504 xassert (it.hpos == 0 && it.current_x == 0);
14506 else
14508 /* There are no reusable lines at the start of the window.
14509 Start displaying in the first text line. */
14510 start_display (&it, w, start);
14511 it.vpos = it.first_vpos;
14512 start_pos = it.current.pos;
14515 /* Find the first row that is not affected by changes at the end of
14516 the buffer. Value will be null if there is no unchanged row, in
14517 which case we must redisplay to the end of the window. delta
14518 will be set to the value by which buffer positions beginning with
14519 first_unchanged_at_end_row have to be adjusted due to text
14520 changes. */
14521 first_unchanged_at_end_row
14522 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14523 IF_DEBUG (debug_delta = delta);
14524 IF_DEBUG (debug_delta_bytes = delta_bytes);
14526 /* Set stop_pos to the buffer position up to which we will have to
14527 display new lines. If first_unchanged_at_end_row != NULL, this
14528 is the buffer position of the start of the line displayed in that
14529 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14530 that we don't stop at a buffer position. */
14531 stop_pos = 0;
14532 if (first_unchanged_at_end_row)
14534 xassert (last_unchanged_at_beg_row == NULL
14535 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14537 /* If this is a continuation line, move forward to the next one
14538 that isn't. Changes in lines above affect this line.
14539 Caution: this may move first_unchanged_at_end_row to a row
14540 not displaying text. */
14541 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14542 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14543 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14544 < it.last_visible_y))
14545 ++first_unchanged_at_end_row;
14547 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14548 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14549 >= it.last_visible_y))
14550 first_unchanged_at_end_row = NULL;
14551 else
14553 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14554 + delta);
14555 first_unchanged_at_end_vpos
14556 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14557 xassert (stop_pos >= Z - END_UNCHANGED);
14560 else if (last_unchanged_at_beg_row == NULL)
14561 GIVE_UP (19);
14564 #if GLYPH_DEBUG
14566 /* Either there is no unchanged row at the end, or the one we have
14567 now displays text. This is a necessary condition for the window
14568 end pos calculation at the end of this function. */
14569 xassert (first_unchanged_at_end_row == NULL
14570 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14572 debug_last_unchanged_at_beg_vpos
14573 = (last_unchanged_at_beg_row
14574 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14575 : -1);
14576 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14578 #endif /* GLYPH_DEBUG != 0 */
14581 /* Display new lines. Set last_text_row to the last new line
14582 displayed which has text on it, i.e. might end up as being the
14583 line where the window_end_vpos is. */
14584 w->cursor.vpos = -1;
14585 last_text_row = NULL;
14586 overlay_arrow_seen = 0;
14587 while (it.current_y < it.last_visible_y
14588 && !fonts_changed_p
14589 && (first_unchanged_at_end_row == NULL
14590 || IT_CHARPOS (it) < stop_pos))
14592 if (display_line (&it))
14593 last_text_row = it.glyph_row - 1;
14596 if (fonts_changed_p)
14597 return -1;
14600 /* Compute differences in buffer positions, y-positions etc. for
14601 lines reused at the bottom of the window. Compute what we can
14602 scroll. */
14603 if (first_unchanged_at_end_row
14604 /* No lines reused because we displayed everything up to the
14605 bottom of the window. */
14606 && it.current_y < it.last_visible_y)
14608 dvpos = (it.vpos
14609 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14610 current_matrix));
14611 dy = it.current_y - first_unchanged_at_end_row->y;
14612 run.current_y = first_unchanged_at_end_row->y;
14613 run.desired_y = run.current_y + dy;
14614 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14616 else
14618 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14619 first_unchanged_at_end_row = NULL;
14621 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14624 /* Find the cursor if not already found. We have to decide whether
14625 PT will appear on this window (it sometimes doesn't, but this is
14626 not a very frequent case.) This decision has to be made before
14627 the current matrix is altered. A value of cursor.vpos < 0 means
14628 that PT is either in one of the lines beginning at
14629 first_unchanged_at_end_row or below the window. Don't care for
14630 lines that might be displayed later at the window end; as
14631 mentioned, this is not a frequent case. */
14632 if (w->cursor.vpos < 0)
14634 /* Cursor in unchanged rows at the top? */
14635 if (PT < CHARPOS (start_pos)
14636 && last_unchanged_at_beg_row)
14638 row = row_containing_pos (w, PT,
14639 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14640 last_unchanged_at_beg_row + 1, 0);
14641 if (row)
14642 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14645 /* Start from first_unchanged_at_end_row looking for PT. */
14646 else if (first_unchanged_at_end_row)
14648 row = row_containing_pos (w, PT - delta,
14649 first_unchanged_at_end_row, NULL, 0);
14650 if (row)
14651 set_cursor_from_row (w, row, w->current_matrix, delta,
14652 delta_bytes, dy, dvpos);
14655 /* Give up if cursor was not found. */
14656 if (w->cursor.vpos < 0)
14658 clear_glyph_matrix (w->desired_matrix);
14659 return -1;
14663 /* Don't let the cursor end in the scroll margins. */
14665 int this_scroll_margin, cursor_height;
14667 this_scroll_margin = max (0, scroll_margin);
14668 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14669 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14670 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14672 if ((w->cursor.y < this_scroll_margin
14673 && CHARPOS (start) > BEGV)
14674 /* Old redisplay didn't take scroll margin into account at the bottom,
14675 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14676 || (w->cursor.y + (make_cursor_line_fully_visible_p
14677 ? cursor_height + this_scroll_margin
14678 : 1)) > it.last_visible_y)
14680 w->cursor.vpos = -1;
14681 clear_glyph_matrix (w->desired_matrix);
14682 return -1;
14686 /* Scroll the display. Do it before changing the current matrix so
14687 that xterm.c doesn't get confused about where the cursor glyph is
14688 found. */
14689 if (dy && run.height)
14691 update_begin (f);
14693 if (FRAME_WINDOW_P (f))
14695 rif->update_window_begin_hook (w);
14696 rif->clear_window_mouse_face (w);
14697 rif->scroll_run_hook (w, &run);
14698 rif->update_window_end_hook (w, 0, 0);
14700 else
14702 /* Terminal frame. In this case, dvpos gives the number of
14703 lines to scroll by; dvpos < 0 means scroll up. */
14704 int first_unchanged_at_end_vpos
14705 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14706 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14707 int end = (WINDOW_TOP_EDGE_LINE (w)
14708 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14709 + window_internal_height (w));
14711 /* Perform the operation on the screen. */
14712 if (dvpos > 0)
14714 /* Scroll last_unchanged_at_beg_row to the end of the
14715 window down dvpos lines. */
14716 set_terminal_window (end);
14718 /* On dumb terminals delete dvpos lines at the end
14719 before inserting dvpos empty lines. */
14720 if (!scroll_region_ok)
14721 ins_del_lines (end - dvpos, -dvpos);
14723 /* Insert dvpos empty lines in front of
14724 last_unchanged_at_beg_row. */
14725 ins_del_lines (from, dvpos);
14727 else if (dvpos < 0)
14729 /* Scroll up last_unchanged_at_beg_vpos to the end of
14730 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14731 set_terminal_window (end);
14733 /* Delete dvpos lines in front of
14734 last_unchanged_at_beg_vpos. ins_del_lines will set
14735 the cursor to the given vpos and emit |dvpos| delete
14736 line sequences. */
14737 ins_del_lines (from + dvpos, dvpos);
14739 /* On a dumb terminal insert dvpos empty lines at the
14740 end. */
14741 if (!scroll_region_ok)
14742 ins_del_lines (end + dvpos, -dvpos);
14745 set_terminal_window (0);
14748 update_end (f);
14751 /* Shift reused rows of the current matrix to the right position.
14752 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14753 text. */
14754 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14755 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14756 if (dvpos < 0)
14758 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14759 bottom_vpos, dvpos);
14760 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14761 bottom_vpos, 0);
14763 else if (dvpos > 0)
14765 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14766 bottom_vpos, dvpos);
14767 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14768 first_unchanged_at_end_vpos + dvpos, 0);
14771 /* For frame-based redisplay, make sure that current frame and window
14772 matrix are in sync with respect to glyph memory. */
14773 if (!FRAME_WINDOW_P (f))
14774 sync_frame_with_window_matrix_rows (w);
14776 /* Adjust buffer positions in reused rows. */
14777 if (delta)
14778 increment_matrix_positions (current_matrix,
14779 first_unchanged_at_end_vpos + dvpos,
14780 bottom_vpos, delta, delta_bytes);
14782 /* Adjust Y positions. */
14783 if (dy)
14784 shift_glyph_matrix (w, current_matrix,
14785 first_unchanged_at_end_vpos + dvpos,
14786 bottom_vpos, dy);
14788 if (first_unchanged_at_end_row)
14790 first_unchanged_at_end_row += dvpos;
14791 if (first_unchanged_at_end_row->y >= it.last_visible_y
14792 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14793 first_unchanged_at_end_row = NULL;
14796 /* If scrolling up, there may be some lines to display at the end of
14797 the window. */
14798 last_text_row_at_end = NULL;
14799 if (dy < 0)
14801 /* Scrolling up can leave for example a partially visible line
14802 at the end of the window to be redisplayed. */
14803 /* Set last_row to the glyph row in the current matrix where the
14804 window end line is found. It has been moved up or down in
14805 the matrix by dvpos. */
14806 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14807 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14809 /* If last_row is the window end line, it should display text. */
14810 xassert (last_row->displays_text_p);
14812 /* If window end line was partially visible before, begin
14813 displaying at that line. Otherwise begin displaying with the
14814 line following it. */
14815 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14817 init_to_row_start (&it, w, last_row);
14818 it.vpos = last_vpos;
14819 it.current_y = last_row->y;
14821 else
14823 init_to_row_end (&it, w, last_row);
14824 it.vpos = 1 + last_vpos;
14825 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14826 ++last_row;
14829 /* We may start in a continuation line. If so, we have to
14830 get the right continuation_lines_width and current_x. */
14831 it.continuation_lines_width = last_row->continuation_lines_width;
14832 it.hpos = it.current_x = 0;
14834 /* Display the rest of the lines at the window end. */
14835 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14836 while (it.current_y < it.last_visible_y
14837 && !fonts_changed_p)
14839 /* Is it always sure that the display agrees with lines in
14840 the current matrix? I don't think so, so we mark rows
14841 displayed invalid in the current matrix by setting their
14842 enabled_p flag to zero. */
14843 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14844 if (display_line (&it))
14845 last_text_row_at_end = it.glyph_row - 1;
14849 /* Update window_end_pos and window_end_vpos. */
14850 if (first_unchanged_at_end_row
14851 && !last_text_row_at_end)
14853 /* Window end line if one of the preserved rows from the current
14854 matrix. Set row to the last row displaying text in current
14855 matrix starting at first_unchanged_at_end_row, after
14856 scrolling. */
14857 xassert (first_unchanged_at_end_row->displays_text_p);
14858 row = find_last_row_displaying_text (w->current_matrix, &it,
14859 first_unchanged_at_end_row);
14860 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14862 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14863 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14864 w->window_end_vpos
14865 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14866 xassert (w->window_end_bytepos >= 0);
14867 IF_DEBUG (debug_method_add (w, "A"));
14869 else if (last_text_row_at_end)
14871 w->window_end_pos
14872 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14873 w->window_end_bytepos
14874 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14875 w->window_end_vpos
14876 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14877 xassert (w->window_end_bytepos >= 0);
14878 IF_DEBUG (debug_method_add (w, "B"));
14880 else if (last_text_row)
14882 /* We have displayed either to the end of the window or at the
14883 end of the window, i.e. the last row with text is to be found
14884 in the desired matrix. */
14885 w->window_end_pos
14886 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14887 w->window_end_bytepos
14888 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14889 w->window_end_vpos
14890 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14891 xassert (w->window_end_bytepos >= 0);
14893 else if (first_unchanged_at_end_row == NULL
14894 && last_text_row == NULL
14895 && last_text_row_at_end == NULL)
14897 /* Displayed to end of window, but no line containing text was
14898 displayed. Lines were deleted at the end of the window. */
14899 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14900 int vpos = XFASTINT (w->window_end_vpos);
14901 struct glyph_row *current_row = current_matrix->rows + vpos;
14902 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14904 for (row = NULL;
14905 row == NULL && vpos >= first_vpos;
14906 --vpos, --current_row, --desired_row)
14908 if (desired_row->enabled_p)
14910 if (desired_row->displays_text_p)
14911 row = desired_row;
14913 else if (current_row->displays_text_p)
14914 row = current_row;
14917 xassert (row != NULL);
14918 w->window_end_vpos = make_number (vpos + 1);
14919 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14920 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14921 xassert (w->window_end_bytepos >= 0);
14922 IF_DEBUG (debug_method_add (w, "C"));
14924 else
14925 abort ();
14927 #if 0 /* This leads to problems, for instance when the cursor is
14928 at ZV, and the cursor line displays no text. */
14929 /* Disable rows below what's displayed in the window. This makes
14930 debugging easier. */
14931 enable_glyph_matrix_rows (current_matrix,
14932 XFASTINT (w->window_end_vpos) + 1,
14933 bottom_vpos, 0);
14934 #endif
14936 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14937 debug_end_vpos = XFASTINT (w->window_end_vpos));
14939 /* Record that display has not been completed. */
14940 w->window_end_valid = Qnil;
14941 w->desired_matrix->no_scrolling_p = 1;
14942 return 3;
14944 #undef GIVE_UP
14949 /***********************************************************************
14950 More debugging support
14951 ***********************************************************************/
14953 #if GLYPH_DEBUG
14955 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14956 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14957 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14960 /* Dump the contents of glyph matrix MATRIX on stderr.
14962 GLYPHS 0 means don't show glyph contents.
14963 GLYPHS 1 means show glyphs in short form
14964 GLYPHS > 1 means show glyphs in long form. */
14966 void
14967 dump_glyph_matrix (matrix, glyphs)
14968 struct glyph_matrix *matrix;
14969 int glyphs;
14971 int i;
14972 for (i = 0; i < matrix->nrows; ++i)
14973 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14977 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14978 the glyph row and area where the glyph comes from. */
14980 void
14981 dump_glyph (row, glyph, area)
14982 struct glyph_row *row;
14983 struct glyph *glyph;
14984 int area;
14986 if (glyph->type == CHAR_GLYPH)
14988 fprintf (stderr,
14989 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14990 glyph - row->glyphs[TEXT_AREA],
14991 'C',
14992 glyph->charpos,
14993 (BUFFERP (glyph->object)
14994 ? 'B'
14995 : (STRINGP (glyph->object)
14996 ? 'S'
14997 : '-')),
14998 glyph->pixel_width,
14999 glyph->u.ch,
15000 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15001 ? glyph->u.ch
15002 : '.'),
15003 glyph->face_id,
15004 glyph->left_box_line_p,
15005 glyph->right_box_line_p);
15007 else if (glyph->type == STRETCH_GLYPH)
15009 fprintf (stderr,
15010 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15011 glyph - row->glyphs[TEXT_AREA],
15012 'S',
15013 glyph->charpos,
15014 (BUFFERP (glyph->object)
15015 ? 'B'
15016 : (STRINGP (glyph->object)
15017 ? 'S'
15018 : '-')),
15019 glyph->pixel_width,
15021 '.',
15022 glyph->face_id,
15023 glyph->left_box_line_p,
15024 glyph->right_box_line_p);
15026 else if (glyph->type == IMAGE_GLYPH)
15028 fprintf (stderr,
15029 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15030 glyph - row->glyphs[TEXT_AREA],
15031 'I',
15032 glyph->charpos,
15033 (BUFFERP (glyph->object)
15034 ? 'B'
15035 : (STRINGP (glyph->object)
15036 ? 'S'
15037 : '-')),
15038 glyph->pixel_width,
15039 glyph->u.img_id,
15040 '.',
15041 glyph->face_id,
15042 glyph->left_box_line_p,
15043 glyph->right_box_line_p);
15045 else if (glyph->type == COMPOSITE_GLYPH)
15047 fprintf (stderr,
15048 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15049 glyph - row->glyphs[TEXT_AREA],
15050 '+',
15051 glyph->charpos,
15052 (BUFFERP (glyph->object)
15053 ? 'B'
15054 : (STRINGP (glyph->object)
15055 ? 'S'
15056 : '-')),
15057 glyph->pixel_width,
15058 glyph->u.cmp_id,
15059 '.',
15060 glyph->face_id,
15061 glyph->left_box_line_p,
15062 glyph->right_box_line_p);
15067 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15068 GLYPHS 0 means don't show glyph contents.
15069 GLYPHS 1 means show glyphs in short form
15070 GLYPHS > 1 means show glyphs in long form. */
15072 void
15073 dump_glyph_row (row, vpos, glyphs)
15074 struct glyph_row *row;
15075 int vpos, glyphs;
15077 if (glyphs != 1)
15079 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15080 fprintf (stderr, "======================================================================\n");
15082 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15083 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15084 vpos,
15085 MATRIX_ROW_START_CHARPOS (row),
15086 MATRIX_ROW_END_CHARPOS (row),
15087 row->used[TEXT_AREA],
15088 row->contains_overlapping_glyphs_p,
15089 row->enabled_p,
15090 row->truncated_on_left_p,
15091 row->truncated_on_right_p,
15092 row->continued_p,
15093 MATRIX_ROW_CONTINUATION_LINE_P (row),
15094 row->displays_text_p,
15095 row->ends_at_zv_p,
15096 row->fill_line_p,
15097 row->ends_in_middle_of_char_p,
15098 row->starts_in_middle_of_char_p,
15099 row->mouse_face_p,
15100 row->x,
15101 row->y,
15102 row->pixel_width,
15103 row->height,
15104 row->visible_height,
15105 row->ascent,
15106 row->phys_ascent);
15107 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15108 row->end.overlay_string_index,
15109 row->continuation_lines_width);
15110 fprintf (stderr, "%9d %5d\n",
15111 CHARPOS (row->start.string_pos),
15112 CHARPOS (row->end.string_pos));
15113 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15114 row->end.dpvec_index);
15117 if (glyphs > 1)
15119 int area;
15121 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15123 struct glyph *glyph = row->glyphs[area];
15124 struct glyph *glyph_end = glyph + row->used[area];
15126 /* Glyph for a line end in text. */
15127 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15128 ++glyph_end;
15130 if (glyph < glyph_end)
15131 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15133 for (; glyph < glyph_end; ++glyph)
15134 dump_glyph (row, glyph, area);
15137 else if (glyphs == 1)
15139 int area;
15141 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15143 char *s = (char *) alloca (row->used[area] + 1);
15144 int i;
15146 for (i = 0; i < row->used[area]; ++i)
15148 struct glyph *glyph = row->glyphs[area] + i;
15149 if (glyph->type == CHAR_GLYPH
15150 && glyph->u.ch < 0x80
15151 && glyph->u.ch >= ' ')
15152 s[i] = glyph->u.ch;
15153 else
15154 s[i] = '.';
15157 s[i] = '\0';
15158 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15164 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15165 Sdump_glyph_matrix, 0, 1, "p",
15166 doc: /* Dump the current matrix of the selected window to stderr.
15167 Shows contents of glyph row structures. With non-nil
15168 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15169 glyphs in short form, otherwise show glyphs in long form. */)
15170 (glyphs)
15171 Lisp_Object glyphs;
15173 struct window *w = XWINDOW (selected_window);
15174 struct buffer *buffer = XBUFFER (w->buffer);
15176 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15177 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15178 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15179 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15180 fprintf (stderr, "=============================================\n");
15181 dump_glyph_matrix (w->current_matrix,
15182 NILP (glyphs) ? 0 : XINT (glyphs));
15183 return Qnil;
15187 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15188 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15191 struct frame *f = XFRAME (selected_frame);
15192 dump_glyph_matrix (f->current_matrix, 1);
15193 return Qnil;
15197 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15198 doc: /* Dump glyph row ROW to stderr.
15199 GLYPH 0 means don't dump glyphs.
15200 GLYPH 1 means dump glyphs in short form.
15201 GLYPH > 1 or omitted means dump glyphs in long form. */)
15202 (row, glyphs)
15203 Lisp_Object row, glyphs;
15205 struct glyph_matrix *matrix;
15206 int vpos;
15208 CHECK_NUMBER (row);
15209 matrix = XWINDOW (selected_window)->current_matrix;
15210 vpos = XINT (row);
15211 if (vpos >= 0 && vpos < matrix->nrows)
15212 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15213 vpos,
15214 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15215 return Qnil;
15219 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15220 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15221 GLYPH 0 means don't dump glyphs.
15222 GLYPH 1 means dump glyphs in short form.
15223 GLYPH > 1 or omitted means dump glyphs in long form. */)
15224 (row, glyphs)
15225 Lisp_Object row, glyphs;
15227 struct frame *sf = SELECTED_FRAME ();
15228 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15229 int vpos;
15231 CHECK_NUMBER (row);
15232 vpos = XINT (row);
15233 if (vpos >= 0 && vpos < m->nrows)
15234 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15235 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15236 return Qnil;
15240 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15241 doc: /* Toggle tracing of redisplay.
15242 With ARG, turn tracing on if and only if ARG is positive. */)
15243 (arg)
15244 Lisp_Object arg;
15246 if (NILP (arg))
15247 trace_redisplay_p = !trace_redisplay_p;
15248 else
15250 arg = Fprefix_numeric_value (arg);
15251 trace_redisplay_p = XINT (arg) > 0;
15254 return Qnil;
15258 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15259 doc: /* Like `format', but print result to stderr.
15260 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15261 (nargs, args)
15262 int nargs;
15263 Lisp_Object *args;
15265 Lisp_Object s = Fformat (nargs, args);
15266 fprintf (stderr, "%s", SDATA (s));
15267 return Qnil;
15270 #endif /* GLYPH_DEBUG */
15274 /***********************************************************************
15275 Building Desired Matrix Rows
15276 ***********************************************************************/
15278 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15279 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15281 static struct glyph_row *
15282 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15283 struct window *w;
15284 Lisp_Object overlay_arrow_string;
15286 struct frame *f = XFRAME (WINDOW_FRAME (w));
15287 struct buffer *buffer = XBUFFER (w->buffer);
15288 struct buffer *old = current_buffer;
15289 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15290 int arrow_len = SCHARS (overlay_arrow_string);
15291 const unsigned char *arrow_end = arrow_string + arrow_len;
15292 const unsigned char *p;
15293 struct it it;
15294 int multibyte_p;
15295 int n_glyphs_before;
15297 set_buffer_temp (buffer);
15298 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15299 it.glyph_row->used[TEXT_AREA] = 0;
15300 SET_TEXT_POS (it.position, 0, 0);
15302 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15303 p = arrow_string;
15304 while (p < arrow_end)
15306 Lisp_Object face, ilisp;
15308 /* Get the next character. */
15309 if (multibyte_p)
15310 it.c = string_char_and_length (p, arrow_len, &it.len);
15311 else
15312 it.c = *p, it.len = 1;
15313 p += it.len;
15315 /* Get its face. */
15316 ilisp = make_number (p - arrow_string);
15317 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15318 it.face_id = compute_char_face (f, it.c, face);
15320 /* Compute its width, get its glyphs. */
15321 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15322 SET_TEXT_POS (it.position, -1, -1);
15323 PRODUCE_GLYPHS (&it);
15325 /* If this character doesn't fit any more in the line, we have
15326 to remove some glyphs. */
15327 if (it.current_x > it.last_visible_x)
15329 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15330 break;
15334 set_buffer_temp (old);
15335 return it.glyph_row;
15339 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15340 glyphs are only inserted for terminal frames since we can't really
15341 win with truncation glyphs when partially visible glyphs are
15342 involved. Which glyphs to insert is determined by
15343 produce_special_glyphs. */
15345 static void
15346 insert_left_trunc_glyphs (it)
15347 struct it *it;
15349 struct it truncate_it;
15350 struct glyph *from, *end, *to, *toend;
15352 xassert (!FRAME_WINDOW_P (it->f));
15354 /* Get the truncation glyphs. */
15355 truncate_it = *it;
15356 truncate_it.current_x = 0;
15357 truncate_it.face_id = DEFAULT_FACE_ID;
15358 truncate_it.glyph_row = &scratch_glyph_row;
15359 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15360 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15361 truncate_it.object = make_number (0);
15362 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15364 /* Overwrite glyphs from IT with truncation glyphs. */
15365 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15366 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15367 to = it->glyph_row->glyphs[TEXT_AREA];
15368 toend = to + it->glyph_row->used[TEXT_AREA];
15370 while (from < end)
15371 *to++ = *from++;
15373 /* There may be padding glyphs left over. Overwrite them too. */
15374 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15376 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15377 while (from < end)
15378 *to++ = *from++;
15381 if (to > toend)
15382 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15386 /* Compute the pixel height and width of IT->glyph_row.
15388 Most of the time, ascent and height of a display line will be equal
15389 to the max_ascent and max_height values of the display iterator
15390 structure. This is not the case if
15392 1. We hit ZV without displaying anything. In this case, max_ascent
15393 and max_height will be zero.
15395 2. We have some glyphs that don't contribute to the line height.
15396 (The glyph row flag contributes_to_line_height_p is for future
15397 pixmap extensions).
15399 The first case is easily covered by using default values because in
15400 these cases, the line height does not really matter, except that it
15401 must not be zero. */
15403 static void
15404 compute_line_metrics (it)
15405 struct it *it;
15407 struct glyph_row *row = it->glyph_row;
15408 int area, i;
15410 if (FRAME_WINDOW_P (it->f))
15412 int i, min_y, max_y;
15414 /* The line may consist of one space only, that was added to
15415 place the cursor on it. If so, the row's height hasn't been
15416 computed yet. */
15417 if (row->height == 0)
15419 if (it->max_ascent + it->max_descent == 0)
15420 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15421 row->ascent = it->max_ascent;
15422 row->height = it->max_ascent + it->max_descent;
15423 row->phys_ascent = it->max_phys_ascent;
15424 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15425 row->extra_line_spacing = it->max_extra_line_spacing;
15428 /* Compute the width of this line. */
15429 row->pixel_width = row->x;
15430 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15431 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15433 xassert (row->pixel_width >= 0);
15434 xassert (row->ascent >= 0 && row->height > 0);
15436 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15437 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15439 /* If first line's physical ascent is larger than its logical
15440 ascent, use the physical ascent, and make the row taller.
15441 This makes accented characters fully visible. */
15442 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15443 && row->phys_ascent > row->ascent)
15445 row->height += row->phys_ascent - row->ascent;
15446 row->ascent = row->phys_ascent;
15449 /* Compute how much of the line is visible. */
15450 row->visible_height = row->height;
15452 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15453 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15455 if (row->y < min_y)
15456 row->visible_height -= min_y - row->y;
15457 if (row->y + row->height > max_y)
15458 row->visible_height -= row->y + row->height - max_y;
15460 else
15462 row->pixel_width = row->used[TEXT_AREA];
15463 if (row->continued_p)
15464 row->pixel_width -= it->continuation_pixel_width;
15465 else if (row->truncated_on_right_p)
15466 row->pixel_width -= it->truncation_pixel_width;
15467 row->ascent = row->phys_ascent = 0;
15468 row->height = row->phys_height = row->visible_height = 1;
15469 row->extra_line_spacing = 0;
15472 /* Compute a hash code for this row. */
15473 row->hash = 0;
15474 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15475 for (i = 0; i < row->used[area]; ++i)
15476 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15477 + row->glyphs[area][i].u.val
15478 + row->glyphs[area][i].face_id
15479 + row->glyphs[area][i].padding_p
15480 + (row->glyphs[area][i].type << 2));
15482 it->max_ascent = it->max_descent = 0;
15483 it->max_phys_ascent = it->max_phys_descent = 0;
15487 /* Append one space to the glyph row of iterator IT if doing a
15488 window-based redisplay. The space has the same face as
15489 IT->face_id. Value is non-zero if a space was added.
15491 This function is called to make sure that there is always one glyph
15492 at the end of a glyph row that the cursor can be set on under
15493 window-systems. (If there weren't such a glyph we would not know
15494 how wide and tall a box cursor should be displayed).
15496 At the same time this space let's a nicely handle clearing to the
15497 end of the line if the row ends in italic text. */
15499 static int
15500 append_space_for_newline (it, default_face_p)
15501 struct it *it;
15502 int default_face_p;
15504 if (FRAME_WINDOW_P (it->f))
15506 int n = it->glyph_row->used[TEXT_AREA];
15508 if (it->glyph_row->glyphs[TEXT_AREA] + n
15509 < it->glyph_row->glyphs[1 + TEXT_AREA])
15511 /* Save some values that must not be changed.
15512 Must save IT->c and IT->len because otherwise
15513 ITERATOR_AT_END_P wouldn't work anymore after
15514 append_space_for_newline has been called. */
15515 enum display_element_type saved_what = it->what;
15516 int saved_c = it->c, saved_len = it->len;
15517 int saved_x = it->current_x;
15518 int saved_face_id = it->face_id;
15519 struct text_pos saved_pos;
15520 Lisp_Object saved_object;
15521 struct face *face;
15523 saved_object = it->object;
15524 saved_pos = it->position;
15526 it->what = IT_CHARACTER;
15527 bzero (&it->position, sizeof it->position);
15528 it->object = make_number (0);
15529 it->c = ' ';
15530 it->len = 1;
15532 if (default_face_p)
15533 it->face_id = DEFAULT_FACE_ID;
15534 else if (it->face_before_selective_p)
15535 it->face_id = it->saved_face_id;
15536 face = FACE_FROM_ID (it->f, it->face_id);
15537 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15539 PRODUCE_GLYPHS (it);
15541 it->override_ascent = -1;
15542 it->constrain_row_ascent_descent_p = 0;
15543 it->current_x = saved_x;
15544 it->object = saved_object;
15545 it->position = saved_pos;
15546 it->what = saved_what;
15547 it->face_id = saved_face_id;
15548 it->len = saved_len;
15549 it->c = saved_c;
15550 return 1;
15554 return 0;
15558 /* Extend the face of the last glyph in the text area of IT->glyph_row
15559 to the end of the display line. Called from display_line.
15560 If the glyph row is empty, add a space glyph to it so that we
15561 know the face to draw. Set the glyph row flag fill_line_p. */
15563 static void
15564 extend_face_to_end_of_line (it)
15565 struct it *it;
15567 struct face *face;
15568 struct frame *f = it->f;
15570 /* If line is already filled, do nothing. */
15571 if (it->current_x >= it->last_visible_x)
15572 return;
15574 /* Face extension extends the background and box of IT->face_id
15575 to the end of the line. If the background equals the background
15576 of the frame, we don't have to do anything. */
15577 if (it->face_before_selective_p)
15578 face = FACE_FROM_ID (it->f, it->saved_face_id);
15579 else
15580 face = FACE_FROM_ID (f, it->face_id);
15582 if (FRAME_WINDOW_P (f)
15583 && it->glyph_row->displays_text_p
15584 && face->box == FACE_NO_BOX
15585 && face->background == FRAME_BACKGROUND_PIXEL (f)
15586 && !face->stipple)
15587 return;
15589 /* Set the glyph row flag indicating that the face of the last glyph
15590 in the text area has to be drawn to the end of the text area. */
15591 it->glyph_row->fill_line_p = 1;
15593 /* If current character of IT is not ASCII, make sure we have the
15594 ASCII face. This will be automatically undone the next time
15595 get_next_display_element returns a multibyte character. Note
15596 that the character will always be single byte in unibyte text. */
15597 if (!SINGLE_BYTE_CHAR_P (it->c))
15599 it->face_id = FACE_FOR_CHAR (f, face, 0);
15602 if (FRAME_WINDOW_P (f))
15604 /* If the row is empty, add a space with the current face of IT,
15605 so that we know which face to draw. */
15606 if (it->glyph_row->used[TEXT_AREA] == 0)
15608 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15609 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15610 it->glyph_row->used[TEXT_AREA] = 1;
15613 else
15615 /* Save some values that must not be changed. */
15616 int saved_x = it->current_x;
15617 struct text_pos saved_pos;
15618 Lisp_Object saved_object;
15619 enum display_element_type saved_what = it->what;
15620 int saved_face_id = it->face_id;
15622 saved_object = it->object;
15623 saved_pos = it->position;
15625 it->what = IT_CHARACTER;
15626 bzero (&it->position, sizeof it->position);
15627 it->object = make_number (0);
15628 it->c = ' ';
15629 it->len = 1;
15630 it->face_id = face->id;
15632 PRODUCE_GLYPHS (it);
15634 while (it->current_x <= it->last_visible_x)
15635 PRODUCE_GLYPHS (it);
15637 /* Don't count these blanks really. It would let us insert a left
15638 truncation glyph below and make us set the cursor on them, maybe. */
15639 it->current_x = saved_x;
15640 it->object = saved_object;
15641 it->position = saved_pos;
15642 it->what = saved_what;
15643 it->face_id = saved_face_id;
15648 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15649 trailing whitespace. */
15651 static int
15652 trailing_whitespace_p (charpos)
15653 int charpos;
15655 int bytepos = CHAR_TO_BYTE (charpos);
15656 int c = 0;
15658 while (bytepos < ZV_BYTE
15659 && (c = FETCH_CHAR (bytepos),
15660 c == ' ' || c == '\t'))
15661 ++bytepos;
15663 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15665 if (bytepos != PT_BYTE)
15666 return 1;
15668 return 0;
15672 /* Highlight trailing whitespace, if any, in ROW. */
15674 void
15675 highlight_trailing_whitespace (f, row)
15676 struct frame *f;
15677 struct glyph_row *row;
15679 int used = row->used[TEXT_AREA];
15681 if (used)
15683 struct glyph *start = row->glyphs[TEXT_AREA];
15684 struct glyph *glyph = start + used - 1;
15686 /* Skip over glyphs inserted to display the cursor at the
15687 end of a line, for extending the face of the last glyph
15688 to the end of the line on terminals, and for truncation
15689 and continuation glyphs. */
15690 while (glyph >= start
15691 && glyph->type == CHAR_GLYPH
15692 && INTEGERP (glyph->object))
15693 --glyph;
15695 /* If last glyph is a space or stretch, and it's trailing
15696 whitespace, set the face of all trailing whitespace glyphs in
15697 IT->glyph_row to `trailing-whitespace'. */
15698 if (glyph >= start
15699 && BUFFERP (glyph->object)
15700 && (glyph->type == STRETCH_GLYPH
15701 || (glyph->type == CHAR_GLYPH
15702 && glyph->u.ch == ' '))
15703 && trailing_whitespace_p (glyph->charpos))
15705 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15706 if (face_id < 0)
15707 return;
15709 while (glyph >= start
15710 && BUFFERP (glyph->object)
15711 && (glyph->type == STRETCH_GLYPH
15712 || (glyph->type == CHAR_GLYPH
15713 && glyph->u.ch == ' ')))
15714 (glyph--)->face_id = face_id;
15720 /* Value is non-zero if glyph row ROW in window W should be
15721 used to hold the cursor. */
15723 static int
15724 cursor_row_p (w, row)
15725 struct window *w;
15726 struct glyph_row *row;
15728 int cursor_row_p = 1;
15730 if (PT == MATRIX_ROW_END_CHARPOS (row))
15732 /* If the row ends with a newline from a string, we don't want
15733 the cursor there, but we still want it at the start of the
15734 string if the string starts in this row.
15735 If the row is continued it doesn't end in a newline. */
15736 if (CHARPOS (row->end.string_pos) >= 0)
15737 cursor_row_p = (row->continued_p
15738 || PT >= MATRIX_ROW_START_CHARPOS (row));
15739 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15741 /* If the row ends in middle of a real character,
15742 and the line is continued, we want the cursor here.
15743 That's because MATRIX_ROW_END_CHARPOS would equal
15744 PT if PT is before the character. */
15745 if (!row->ends_in_ellipsis_p)
15746 cursor_row_p = row->continued_p;
15747 else
15748 /* If the row ends in an ellipsis, then
15749 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15750 We want that position to be displayed after the ellipsis. */
15751 cursor_row_p = 0;
15753 /* If the row ends at ZV, display the cursor at the end of that
15754 row instead of at the start of the row below. */
15755 else if (row->ends_at_zv_p)
15756 cursor_row_p = 1;
15757 else
15758 cursor_row_p = 0;
15761 return cursor_row_p;
15765 /* Construct the glyph row IT->glyph_row in the desired matrix of
15766 IT->w from text at the current position of IT. See dispextern.h
15767 for an overview of struct it. Value is non-zero if
15768 IT->glyph_row displays text, as opposed to a line displaying ZV
15769 only. */
15771 static int
15772 display_line (it)
15773 struct it *it;
15775 struct glyph_row *row = it->glyph_row;
15776 Lisp_Object overlay_arrow_string;
15778 /* We always start displaying at hpos zero even if hscrolled. */
15779 xassert (it->hpos == 0 && it->current_x == 0);
15781 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15782 >= it->w->desired_matrix->nrows)
15784 it->w->nrows_scale_factor++;
15785 fonts_changed_p = 1;
15786 return 0;
15789 /* Is IT->w showing the region? */
15790 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15792 /* Clear the result glyph row and enable it. */
15793 prepare_desired_row (row);
15795 row->y = it->current_y;
15796 row->start = it->start;
15797 row->continuation_lines_width = it->continuation_lines_width;
15798 row->displays_text_p = 1;
15799 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15800 it->starts_in_middle_of_char_p = 0;
15802 /* Arrange the overlays nicely for our purposes. Usually, we call
15803 display_line on only one line at a time, in which case this
15804 can't really hurt too much, or we call it on lines which appear
15805 one after another in the buffer, in which case all calls to
15806 recenter_overlay_lists but the first will be pretty cheap. */
15807 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15809 /* Move over display elements that are not visible because we are
15810 hscrolled. This may stop at an x-position < IT->first_visible_x
15811 if the first glyph is partially visible or if we hit a line end. */
15812 if (it->current_x < it->first_visible_x)
15814 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15815 MOVE_TO_POS | MOVE_TO_X);
15818 /* Get the initial row height. This is either the height of the
15819 text hscrolled, if there is any, or zero. */
15820 row->ascent = it->max_ascent;
15821 row->height = it->max_ascent + it->max_descent;
15822 row->phys_ascent = it->max_phys_ascent;
15823 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15824 row->extra_line_spacing = it->max_extra_line_spacing;
15826 /* Loop generating characters. The loop is left with IT on the next
15827 character to display. */
15828 while (1)
15830 int n_glyphs_before, hpos_before, x_before;
15831 int x, i, nglyphs;
15832 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15834 /* Retrieve the next thing to display. Value is zero if end of
15835 buffer reached. */
15836 if (!get_next_display_element (it))
15838 /* Maybe add a space at the end of this line that is used to
15839 display the cursor there under X. Set the charpos of the
15840 first glyph of blank lines not corresponding to any text
15841 to -1. */
15842 #ifdef HAVE_WINDOW_SYSTEM
15843 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15844 row->exact_window_width_line_p = 1;
15845 else
15846 #endif /* HAVE_WINDOW_SYSTEM */
15847 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15848 || row->used[TEXT_AREA] == 0)
15850 row->glyphs[TEXT_AREA]->charpos = -1;
15851 row->displays_text_p = 0;
15853 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15854 && (!MINI_WINDOW_P (it->w)
15855 || (minibuf_level && EQ (it->window, minibuf_window))))
15856 row->indicate_empty_line_p = 1;
15859 it->continuation_lines_width = 0;
15860 row->ends_at_zv_p = 1;
15861 break;
15864 /* Now, get the metrics of what we want to display. This also
15865 generates glyphs in `row' (which is IT->glyph_row). */
15866 n_glyphs_before = row->used[TEXT_AREA];
15867 x = it->current_x;
15869 /* Remember the line height so far in case the next element doesn't
15870 fit on the line. */
15871 if (!it->truncate_lines_p)
15873 ascent = it->max_ascent;
15874 descent = it->max_descent;
15875 phys_ascent = it->max_phys_ascent;
15876 phys_descent = it->max_phys_descent;
15879 PRODUCE_GLYPHS (it);
15881 /* If this display element was in marginal areas, continue with
15882 the next one. */
15883 if (it->area != TEXT_AREA)
15885 row->ascent = max (row->ascent, it->max_ascent);
15886 row->height = max (row->height, it->max_ascent + it->max_descent);
15887 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15888 row->phys_height = max (row->phys_height,
15889 it->max_phys_ascent + it->max_phys_descent);
15890 row->extra_line_spacing = max (row->extra_line_spacing,
15891 it->max_extra_line_spacing);
15892 set_iterator_to_next (it, 1);
15893 continue;
15896 /* Does the display element fit on the line? If we truncate
15897 lines, we should draw past the right edge of the window. If
15898 we don't truncate, we want to stop so that we can display the
15899 continuation glyph before the right margin. If lines are
15900 continued, there are two possible strategies for characters
15901 resulting in more than 1 glyph (e.g. tabs): Display as many
15902 glyphs as possible in this line and leave the rest for the
15903 continuation line, or display the whole element in the next
15904 line. Original redisplay did the former, so we do it also. */
15905 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15906 hpos_before = it->hpos;
15907 x_before = x;
15909 if (/* Not a newline. */
15910 nglyphs > 0
15911 /* Glyphs produced fit entirely in the line. */
15912 && it->current_x < it->last_visible_x)
15914 it->hpos += nglyphs;
15915 row->ascent = max (row->ascent, it->max_ascent);
15916 row->height = max (row->height, it->max_ascent + it->max_descent);
15917 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15918 row->phys_height = max (row->phys_height,
15919 it->max_phys_ascent + it->max_phys_descent);
15920 row->extra_line_spacing = max (row->extra_line_spacing,
15921 it->max_extra_line_spacing);
15922 if (it->current_x - it->pixel_width < it->first_visible_x)
15923 row->x = x - it->first_visible_x;
15925 else
15927 int new_x;
15928 struct glyph *glyph;
15930 for (i = 0; i < nglyphs; ++i, x = new_x)
15932 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15933 new_x = x + glyph->pixel_width;
15935 if (/* Lines are continued. */
15936 !it->truncate_lines_p
15937 && (/* Glyph doesn't fit on the line. */
15938 new_x > it->last_visible_x
15939 /* Or it fits exactly on a window system frame. */
15940 || (new_x == it->last_visible_x
15941 && FRAME_WINDOW_P (it->f))))
15943 /* End of a continued line. */
15945 if (it->hpos == 0
15946 || (new_x == it->last_visible_x
15947 && FRAME_WINDOW_P (it->f)))
15949 /* Current glyph is the only one on the line or
15950 fits exactly on the line. We must continue
15951 the line because we can't draw the cursor
15952 after the glyph. */
15953 row->continued_p = 1;
15954 it->current_x = new_x;
15955 it->continuation_lines_width += new_x;
15956 ++it->hpos;
15957 if (i == nglyphs - 1)
15959 set_iterator_to_next (it, 1);
15960 #ifdef HAVE_WINDOW_SYSTEM
15961 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15963 if (!get_next_display_element (it))
15965 row->exact_window_width_line_p = 1;
15966 it->continuation_lines_width = 0;
15967 row->continued_p = 0;
15968 row->ends_at_zv_p = 1;
15970 else if (ITERATOR_AT_END_OF_LINE_P (it))
15972 row->continued_p = 0;
15973 row->exact_window_width_line_p = 1;
15976 #endif /* HAVE_WINDOW_SYSTEM */
15979 else if (CHAR_GLYPH_PADDING_P (*glyph)
15980 && !FRAME_WINDOW_P (it->f))
15982 /* A padding glyph that doesn't fit on this line.
15983 This means the whole character doesn't fit
15984 on the line. */
15985 row->used[TEXT_AREA] = n_glyphs_before;
15987 /* Fill the rest of the row with continuation
15988 glyphs like in 20.x. */
15989 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15990 < row->glyphs[1 + TEXT_AREA])
15991 produce_special_glyphs (it, IT_CONTINUATION);
15993 row->continued_p = 1;
15994 it->current_x = x_before;
15995 it->continuation_lines_width += x_before;
15997 /* Restore the height to what it was before the
15998 element not fitting on the line. */
15999 it->max_ascent = ascent;
16000 it->max_descent = descent;
16001 it->max_phys_ascent = phys_ascent;
16002 it->max_phys_descent = phys_descent;
16004 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16006 /* A TAB that extends past the right edge of the
16007 window. This produces a single glyph on
16008 window system frames. We leave the glyph in
16009 this row and let it fill the row, but don't
16010 consume the TAB. */
16011 it->continuation_lines_width += it->last_visible_x;
16012 row->ends_in_middle_of_char_p = 1;
16013 row->continued_p = 1;
16014 glyph->pixel_width = it->last_visible_x - x;
16015 it->starts_in_middle_of_char_p = 1;
16017 else
16019 /* Something other than a TAB that draws past
16020 the right edge of the window. Restore
16021 positions to values before the element. */
16022 row->used[TEXT_AREA] = n_glyphs_before + i;
16024 /* Display continuation glyphs. */
16025 if (!FRAME_WINDOW_P (it->f))
16026 produce_special_glyphs (it, IT_CONTINUATION);
16027 row->continued_p = 1;
16029 it->current_x = x_before;
16030 it->continuation_lines_width += x;
16031 extend_face_to_end_of_line (it);
16033 if (nglyphs > 1 && i > 0)
16035 row->ends_in_middle_of_char_p = 1;
16036 it->starts_in_middle_of_char_p = 1;
16039 /* Restore the height to what it was before the
16040 element not fitting on the line. */
16041 it->max_ascent = ascent;
16042 it->max_descent = descent;
16043 it->max_phys_ascent = phys_ascent;
16044 it->max_phys_descent = phys_descent;
16047 break;
16049 else if (new_x > it->first_visible_x)
16051 /* Increment number of glyphs actually displayed. */
16052 ++it->hpos;
16054 if (x < it->first_visible_x)
16055 /* Glyph is partially visible, i.e. row starts at
16056 negative X position. */
16057 row->x = x - it->first_visible_x;
16059 else
16061 /* Glyph is completely off the left margin of the
16062 window. This should not happen because of the
16063 move_it_in_display_line at the start of this
16064 function, unless the text display area of the
16065 window is empty. */
16066 xassert (it->first_visible_x <= it->last_visible_x);
16070 row->ascent = max (row->ascent, it->max_ascent);
16071 row->height = max (row->height, it->max_ascent + it->max_descent);
16072 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16073 row->phys_height = max (row->phys_height,
16074 it->max_phys_ascent + it->max_phys_descent);
16075 row->extra_line_spacing = max (row->extra_line_spacing,
16076 it->max_extra_line_spacing);
16078 /* End of this display line if row is continued. */
16079 if (row->continued_p || row->ends_at_zv_p)
16080 break;
16083 at_end_of_line:
16084 /* Is this a line end? If yes, we're also done, after making
16085 sure that a non-default face is extended up to the right
16086 margin of the window. */
16087 if (ITERATOR_AT_END_OF_LINE_P (it))
16089 int used_before = row->used[TEXT_AREA];
16091 row->ends_in_newline_from_string_p = STRINGP (it->object);
16093 #ifdef HAVE_WINDOW_SYSTEM
16094 /* Add a space at the end of the line that is used to
16095 display the cursor there. */
16096 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16097 append_space_for_newline (it, 0);
16098 #endif /* HAVE_WINDOW_SYSTEM */
16100 /* Extend the face to the end of the line. */
16101 extend_face_to_end_of_line (it);
16103 /* Make sure we have the position. */
16104 if (used_before == 0)
16105 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16107 /* Consume the line end. This skips over invisible lines. */
16108 set_iterator_to_next (it, 1);
16109 it->continuation_lines_width = 0;
16110 break;
16113 /* Proceed with next display element. Note that this skips
16114 over lines invisible because of selective display. */
16115 set_iterator_to_next (it, 1);
16117 /* If we truncate lines, we are done when the last displayed
16118 glyphs reach past the right margin of the window. */
16119 if (it->truncate_lines_p
16120 && (FRAME_WINDOW_P (it->f)
16121 ? (it->current_x >= it->last_visible_x)
16122 : (it->current_x > it->last_visible_x)))
16124 /* Maybe add truncation glyphs. */
16125 if (!FRAME_WINDOW_P (it->f))
16127 int i, n;
16129 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16130 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16131 break;
16133 for (n = row->used[TEXT_AREA]; i < n; ++i)
16135 row->used[TEXT_AREA] = i;
16136 produce_special_glyphs (it, IT_TRUNCATION);
16139 #ifdef HAVE_WINDOW_SYSTEM
16140 else
16142 /* Don't truncate if we can overflow newline into fringe. */
16143 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16145 if (!get_next_display_element (it))
16147 it->continuation_lines_width = 0;
16148 row->ends_at_zv_p = 1;
16149 row->exact_window_width_line_p = 1;
16150 break;
16152 if (ITERATOR_AT_END_OF_LINE_P (it))
16154 row->exact_window_width_line_p = 1;
16155 goto at_end_of_line;
16159 #endif /* HAVE_WINDOW_SYSTEM */
16161 row->truncated_on_right_p = 1;
16162 it->continuation_lines_width = 0;
16163 reseat_at_next_visible_line_start (it, 0);
16164 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16165 it->hpos = hpos_before;
16166 it->current_x = x_before;
16167 break;
16171 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16172 at the left window margin. */
16173 if (it->first_visible_x
16174 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16176 if (!FRAME_WINDOW_P (it->f))
16177 insert_left_trunc_glyphs (it);
16178 row->truncated_on_left_p = 1;
16181 /* If the start of this line is the overlay arrow-position, then
16182 mark this glyph row as the one containing the overlay arrow.
16183 This is clearly a mess with variable size fonts. It would be
16184 better to let it be displayed like cursors under X. */
16185 if ((row->displays_text_p || !overlay_arrow_seen)
16186 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16187 !NILP (overlay_arrow_string)))
16189 /* Overlay arrow in window redisplay is a fringe bitmap. */
16190 if (STRINGP (overlay_arrow_string))
16192 struct glyph_row *arrow_row
16193 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16194 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16195 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16196 struct glyph *p = row->glyphs[TEXT_AREA];
16197 struct glyph *p2, *end;
16199 /* Copy the arrow glyphs. */
16200 while (glyph < arrow_end)
16201 *p++ = *glyph++;
16203 /* Throw away padding glyphs. */
16204 p2 = p;
16205 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16206 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16207 ++p2;
16208 if (p2 > p)
16210 while (p2 < end)
16211 *p++ = *p2++;
16212 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16215 else
16217 xassert (INTEGERP (overlay_arrow_string));
16218 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16220 overlay_arrow_seen = 1;
16223 /* Compute pixel dimensions of this line. */
16224 compute_line_metrics (it);
16226 /* Remember the position at which this line ends. */
16227 row->end = it->current;
16229 /* Record whether this row ends inside an ellipsis. */
16230 row->ends_in_ellipsis_p
16231 = (it->method == GET_FROM_DISPLAY_VECTOR
16232 && it->ellipsis_p);
16234 /* Save fringe bitmaps in this row. */
16235 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16236 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16237 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16238 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16240 it->left_user_fringe_bitmap = 0;
16241 it->left_user_fringe_face_id = 0;
16242 it->right_user_fringe_bitmap = 0;
16243 it->right_user_fringe_face_id = 0;
16245 /* Maybe set the cursor. */
16246 if (it->w->cursor.vpos < 0
16247 && PT >= MATRIX_ROW_START_CHARPOS (row)
16248 && PT <= MATRIX_ROW_END_CHARPOS (row)
16249 && cursor_row_p (it->w, row))
16250 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16252 /* Highlight trailing whitespace. */
16253 if (!NILP (Vshow_trailing_whitespace))
16254 highlight_trailing_whitespace (it->f, it->glyph_row);
16256 /* Prepare for the next line. This line starts horizontally at (X
16257 HPOS) = (0 0). Vertical positions are incremented. As a
16258 convenience for the caller, IT->glyph_row is set to the next
16259 row to be used. */
16260 it->current_x = it->hpos = 0;
16261 it->current_y += row->height;
16262 ++it->vpos;
16263 ++it->glyph_row;
16264 it->start = it->current;
16265 return row->displays_text_p;
16270 /***********************************************************************
16271 Menu Bar
16272 ***********************************************************************/
16274 /* Redisplay the menu bar in the frame for window W.
16276 The menu bar of X frames that don't have X toolkit support is
16277 displayed in a special window W->frame->menu_bar_window.
16279 The menu bar of terminal frames is treated specially as far as
16280 glyph matrices are concerned. Menu bar lines are not part of
16281 windows, so the update is done directly on the frame matrix rows
16282 for the menu bar. */
16284 static void
16285 display_menu_bar (w)
16286 struct window *w;
16288 struct frame *f = XFRAME (WINDOW_FRAME (w));
16289 struct it it;
16290 Lisp_Object items;
16291 int i;
16293 /* Don't do all this for graphical frames. */
16294 #ifdef HAVE_NTGUI
16295 if (!NILP (Vwindow_system))
16296 return;
16297 #endif
16298 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16299 if (FRAME_X_P (f))
16300 return;
16301 #endif
16302 #ifdef MAC_OS
16303 if (FRAME_MAC_P (f))
16304 return;
16305 #endif
16307 #ifdef USE_X_TOOLKIT
16308 xassert (!FRAME_WINDOW_P (f));
16309 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16310 it.first_visible_x = 0;
16311 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16312 #else /* not USE_X_TOOLKIT */
16313 if (FRAME_WINDOW_P (f))
16315 /* Menu bar lines are displayed in the desired matrix of the
16316 dummy window menu_bar_window. */
16317 struct window *menu_w;
16318 xassert (WINDOWP (f->menu_bar_window));
16319 menu_w = XWINDOW (f->menu_bar_window);
16320 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16321 MENU_FACE_ID);
16322 it.first_visible_x = 0;
16323 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16325 else
16327 /* This is a TTY frame, i.e. character hpos/vpos are used as
16328 pixel x/y. */
16329 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16330 MENU_FACE_ID);
16331 it.first_visible_x = 0;
16332 it.last_visible_x = FRAME_COLS (f);
16334 #endif /* not USE_X_TOOLKIT */
16336 if (! mode_line_inverse_video)
16337 /* Force the menu-bar to be displayed in the default face. */
16338 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16340 /* Clear all rows of the menu bar. */
16341 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16343 struct glyph_row *row = it.glyph_row + i;
16344 clear_glyph_row (row);
16345 row->enabled_p = 1;
16346 row->full_width_p = 1;
16349 /* Display all items of the menu bar. */
16350 items = FRAME_MENU_BAR_ITEMS (it.f);
16351 for (i = 0; i < XVECTOR (items)->size; i += 4)
16353 Lisp_Object string;
16355 /* Stop at nil string. */
16356 string = AREF (items, i + 1);
16357 if (NILP (string))
16358 break;
16360 /* Remember where item was displayed. */
16361 AREF (items, i + 3) = make_number (it.hpos);
16363 /* Display the item, pad with one space. */
16364 if (it.current_x < it.last_visible_x)
16365 display_string (NULL, string, Qnil, 0, 0, &it,
16366 SCHARS (string) + 1, 0, 0, -1);
16369 /* Fill out the line with spaces. */
16370 if (it.current_x < it.last_visible_x)
16371 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16373 /* Compute the total height of the lines. */
16374 compute_line_metrics (&it);
16379 /***********************************************************************
16380 Mode Line
16381 ***********************************************************************/
16383 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16384 FORCE is non-zero, redisplay mode lines unconditionally.
16385 Otherwise, redisplay only mode lines that are garbaged. Value is
16386 the number of windows whose mode lines were redisplayed. */
16388 static int
16389 redisplay_mode_lines (window, force)
16390 Lisp_Object window;
16391 int force;
16393 int nwindows = 0;
16395 while (!NILP (window))
16397 struct window *w = XWINDOW (window);
16399 if (WINDOWP (w->hchild))
16400 nwindows += redisplay_mode_lines (w->hchild, force);
16401 else if (WINDOWP (w->vchild))
16402 nwindows += redisplay_mode_lines (w->vchild, force);
16403 else if (force
16404 || FRAME_GARBAGED_P (XFRAME (w->frame))
16405 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16407 struct text_pos lpoint;
16408 struct buffer *old = current_buffer;
16410 /* Set the window's buffer for the mode line display. */
16411 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16412 set_buffer_internal_1 (XBUFFER (w->buffer));
16414 /* Point refers normally to the selected window. For any
16415 other window, set up appropriate value. */
16416 if (!EQ (window, selected_window))
16418 struct text_pos pt;
16420 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16421 if (CHARPOS (pt) < BEGV)
16422 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16423 else if (CHARPOS (pt) > (ZV - 1))
16424 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16425 else
16426 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16429 /* Display mode lines. */
16430 clear_glyph_matrix (w->desired_matrix);
16431 if (display_mode_lines (w))
16433 ++nwindows;
16434 w->must_be_updated_p = 1;
16437 /* Restore old settings. */
16438 set_buffer_internal_1 (old);
16439 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16442 window = w->next;
16445 return nwindows;
16449 /* Display the mode and/or top line of window W. Value is the number
16450 of mode lines displayed. */
16452 static int
16453 display_mode_lines (w)
16454 struct window *w;
16456 Lisp_Object old_selected_window, old_selected_frame;
16457 int n = 0;
16459 old_selected_frame = selected_frame;
16460 selected_frame = w->frame;
16461 old_selected_window = selected_window;
16462 XSETWINDOW (selected_window, w);
16464 /* These will be set while the mode line specs are processed. */
16465 line_number_displayed = 0;
16466 w->column_number_displayed = Qnil;
16468 if (WINDOW_WANTS_MODELINE_P (w))
16470 struct window *sel_w = XWINDOW (old_selected_window);
16472 /* Select mode line face based on the real selected window. */
16473 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16474 current_buffer->mode_line_format);
16475 ++n;
16478 if (WINDOW_WANTS_HEADER_LINE_P (w))
16480 display_mode_line (w, HEADER_LINE_FACE_ID,
16481 current_buffer->header_line_format);
16482 ++n;
16485 selected_frame = old_selected_frame;
16486 selected_window = old_selected_window;
16487 return n;
16491 /* Display mode or top line of window W. FACE_ID specifies which line
16492 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16493 FORMAT is the mode line format to display. Value is the pixel
16494 height of the mode line displayed. */
16496 static int
16497 display_mode_line (w, face_id, format)
16498 struct window *w;
16499 enum face_id face_id;
16500 Lisp_Object format;
16502 struct it it;
16503 struct face *face;
16504 int count = SPECPDL_INDEX ();
16506 init_iterator (&it, w, -1, -1, NULL, face_id);
16507 prepare_desired_row (it.glyph_row);
16509 it.glyph_row->mode_line_p = 1;
16511 if (! mode_line_inverse_video)
16512 /* Force the mode-line to be displayed in the default face. */
16513 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16515 record_unwind_protect (unwind_format_mode_line,
16516 format_mode_line_unwind_data (NULL, 0));
16518 mode_line_target = MODE_LINE_DISPLAY;
16520 /* Temporarily make frame's keyboard the current kboard so that
16521 kboard-local variables in the mode_line_format will get the right
16522 values. */
16523 push_frame_kboard (it.f);
16524 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16525 pop_frame_kboard ();
16527 unbind_to (count, Qnil);
16529 /* Fill up with spaces. */
16530 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16532 compute_line_metrics (&it);
16533 it.glyph_row->full_width_p = 1;
16534 it.glyph_row->continued_p = 0;
16535 it.glyph_row->truncated_on_left_p = 0;
16536 it.glyph_row->truncated_on_right_p = 0;
16538 /* Make a 3D mode-line have a shadow at its right end. */
16539 face = FACE_FROM_ID (it.f, face_id);
16540 extend_face_to_end_of_line (&it);
16541 if (face->box != FACE_NO_BOX)
16543 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16544 + it.glyph_row->used[TEXT_AREA] - 1);
16545 last->right_box_line_p = 1;
16548 return it.glyph_row->height;
16551 /* Move element ELT in LIST to the front of LIST.
16552 Return the updated list. */
16554 static Lisp_Object
16555 move_elt_to_front (elt, list)
16556 Lisp_Object elt, list;
16558 register Lisp_Object tail, prev;
16559 register Lisp_Object tem;
16561 tail = list;
16562 prev = Qnil;
16563 while (CONSP (tail))
16565 tem = XCAR (tail);
16567 if (EQ (elt, tem))
16569 /* Splice out the link TAIL. */
16570 if (NILP (prev))
16571 list = XCDR (tail);
16572 else
16573 Fsetcdr (prev, XCDR (tail));
16575 /* Now make it the first. */
16576 Fsetcdr (tail, list);
16577 return tail;
16579 else
16580 prev = tail;
16581 tail = XCDR (tail);
16582 QUIT;
16585 /* Not found--return unchanged LIST. */
16586 return list;
16589 /* Contribute ELT to the mode line for window IT->w. How it
16590 translates into text depends on its data type.
16592 IT describes the display environment in which we display, as usual.
16594 DEPTH is the depth in recursion. It is used to prevent
16595 infinite recursion here.
16597 FIELD_WIDTH is the number of characters the display of ELT should
16598 occupy in the mode line, and PRECISION is the maximum number of
16599 characters to display from ELT's representation. See
16600 display_string for details.
16602 Returns the hpos of the end of the text generated by ELT.
16604 PROPS is a property list to add to any string we encounter.
16606 If RISKY is nonzero, remove (disregard) any properties in any string
16607 we encounter, and ignore :eval and :propertize.
16609 The global variable `mode_line_target' determines whether the
16610 output is passed to `store_mode_line_noprop',
16611 `store_mode_line_string', or `display_string'. */
16613 static int
16614 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16615 struct it *it;
16616 int depth;
16617 int field_width, precision;
16618 Lisp_Object elt, props;
16619 int risky;
16621 int n = 0, field, prec;
16622 int literal = 0;
16624 tail_recurse:
16625 if (depth > 100)
16626 elt = build_string ("*too-deep*");
16628 depth++;
16630 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16632 case Lisp_String:
16634 /* A string: output it and check for %-constructs within it. */
16635 unsigned char c;
16636 int offset = 0;
16638 if (SCHARS (elt) > 0
16639 && (!NILP (props) || risky))
16641 Lisp_Object oprops, aelt;
16642 oprops = Ftext_properties_at (make_number (0), elt);
16644 /* If the starting string's properties are not what
16645 we want, translate the string. Also, if the string
16646 is risky, do that anyway. */
16648 if (NILP (Fequal (props, oprops)) || risky)
16650 /* If the starting string has properties,
16651 merge the specified ones onto the existing ones. */
16652 if (! NILP (oprops) && !risky)
16654 Lisp_Object tem;
16656 oprops = Fcopy_sequence (oprops);
16657 tem = props;
16658 while (CONSP (tem))
16660 oprops = Fplist_put (oprops, XCAR (tem),
16661 XCAR (XCDR (tem)));
16662 tem = XCDR (XCDR (tem));
16664 props = oprops;
16667 aelt = Fassoc (elt, mode_line_proptrans_alist);
16668 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16670 /* AELT is what we want. Move it to the front
16671 without consing. */
16672 elt = XCAR (aelt);
16673 mode_line_proptrans_alist
16674 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16676 else
16678 Lisp_Object tem;
16680 /* If AELT has the wrong props, it is useless.
16681 so get rid of it. */
16682 if (! NILP (aelt))
16683 mode_line_proptrans_alist
16684 = Fdelq (aelt, mode_line_proptrans_alist);
16686 elt = Fcopy_sequence (elt);
16687 Fset_text_properties (make_number (0), Flength (elt),
16688 props, elt);
16689 /* Add this item to mode_line_proptrans_alist. */
16690 mode_line_proptrans_alist
16691 = Fcons (Fcons (elt, props),
16692 mode_line_proptrans_alist);
16693 /* Truncate mode_line_proptrans_alist
16694 to at most 50 elements. */
16695 tem = Fnthcdr (make_number (50),
16696 mode_line_proptrans_alist);
16697 if (! NILP (tem))
16698 XSETCDR (tem, Qnil);
16703 offset = 0;
16705 if (literal)
16707 prec = precision - n;
16708 switch (mode_line_target)
16710 case MODE_LINE_NOPROP:
16711 case MODE_LINE_TITLE:
16712 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16713 break;
16714 case MODE_LINE_STRING:
16715 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16716 break;
16717 case MODE_LINE_DISPLAY:
16718 n += display_string (NULL, elt, Qnil, 0, 0, it,
16719 0, prec, 0, STRING_MULTIBYTE (elt));
16720 break;
16723 break;
16726 /* Handle the non-literal case. */
16728 while ((precision <= 0 || n < precision)
16729 && SREF (elt, offset) != 0
16730 && (mode_line_target != MODE_LINE_DISPLAY
16731 || it->current_x < it->last_visible_x))
16733 int last_offset = offset;
16735 /* Advance to end of string or next format specifier. */
16736 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16739 if (offset - 1 != last_offset)
16741 int nchars, nbytes;
16743 /* Output to end of string or up to '%'. Field width
16744 is length of string. Don't output more than
16745 PRECISION allows us. */
16746 offset--;
16748 prec = c_string_width (SDATA (elt) + last_offset,
16749 offset - last_offset, precision - n,
16750 &nchars, &nbytes);
16752 switch (mode_line_target)
16754 case MODE_LINE_NOPROP:
16755 case MODE_LINE_TITLE:
16756 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16757 break;
16758 case MODE_LINE_STRING:
16760 int bytepos = last_offset;
16761 int charpos = string_byte_to_char (elt, bytepos);
16762 int endpos = (precision <= 0
16763 ? string_byte_to_char (elt, offset)
16764 : charpos + nchars);
16766 n += store_mode_line_string (NULL,
16767 Fsubstring (elt, make_number (charpos),
16768 make_number (endpos)),
16769 0, 0, 0, Qnil);
16771 break;
16772 case MODE_LINE_DISPLAY:
16774 int bytepos = last_offset;
16775 int charpos = string_byte_to_char (elt, bytepos);
16777 if (precision <= 0)
16778 nchars = string_byte_to_char (elt, offset) - charpos;
16779 n += display_string (NULL, elt, Qnil, 0, charpos,
16780 it, 0, nchars, 0,
16781 STRING_MULTIBYTE (elt));
16783 break;
16786 else /* c == '%' */
16788 int percent_position = offset;
16790 /* Get the specified minimum width. Zero means
16791 don't pad. */
16792 field = 0;
16793 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16794 field = field * 10 + c - '0';
16796 /* Don't pad beyond the total padding allowed. */
16797 if (field_width - n > 0 && field > field_width - n)
16798 field = field_width - n;
16800 /* Note that either PRECISION <= 0 or N < PRECISION. */
16801 prec = precision - n;
16803 if (c == 'M')
16804 n += display_mode_element (it, depth, field, prec,
16805 Vglobal_mode_string, props,
16806 risky);
16807 else if (c != 0)
16809 int multibyte;
16810 int bytepos, charpos;
16811 unsigned char *spec;
16813 bytepos = percent_position;
16814 charpos = (STRING_MULTIBYTE (elt)
16815 ? string_byte_to_char (elt, bytepos)
16816 : bytepos);
16818 spec
16819 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16821 switch (mode_line_target)
16823 case MODE_LINE_NOPROP:
16824 case MODE_LINE_TITLE:
16825 n += store_mode_line_noprop (spec, field, prec);
16826 break;
16827 case MODE_LINE_STRING:
16829 int len = strlen (spec);
16830 Lisp_Object tem = make_string (spec, len);
16831 props = Ftext_properties_at (make_number (charpos), elt);
16832 /* Should only keep face property in props */
16833 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16835 break;
16836 case MODE_LINE_DISPLAY:
16838 int nglyphs_before, nwritten;
16840 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16841 nwritten = display_string (spec, Qnil, elt,
16842 charpos, 0, it,
16843 field, prec, 0,
16844 multibyte);
16846 /* Assign to the glyphs written above the
16847 string where the `%x' came from, position
16848 of the `%'. */
16849 if (nwritten > 0)
16851 struct glyph *glyph
16852 = (it->glyph_row->glyphs[TEXT_AREA]
16853 + nglyphs_before);
16854 int i;
16856 for (i = 0; i < nwritten; ++i)
16858 glyph[i].object = elt;
16859 glyph[i].charpos = charpos;
16862 n += nwritten;
16865 break;
16868 else /* c == 0 */
16869 break;
16873 break;
16875 case Lisp_Symbol:
16876 /* A symbol: process the value of the symbol recursively
16877 as if it appeared here directly. Avoid error if symbol void.
16878 Special case: if value of symbol is a string, output the string
16879 literally. */
16881 register Lisp_Object tem;
16883 /* If the variable is not marked as risky to set
16884 then its contents are risky to use. */
16885 if (NILP (Fget (elt, Qrisky_local_variable)))
16886 risky = 1;
16888 tem = Fboundp (elt);
16889 if (!NILP (tem))
16891 tem = Fsymbol_value (elt);
16892 /* If value is a string, output that string literally:
16893 don't check for % within it. */
16894 if (STRINGP (tem))
16895 literal = 1;
16897 if (!EQ (tem, elt))
16899 /* Give up right away for nil or t. */
16900 elt = tem;
16901 goto tail_recurse;
16905 break;
16907 case Lisp_Cons:
16909 register Lisp_Object car, tem;
16911 /* A cons cell: five distinct cases.
16912 If first element is :eval or :propertize, do something special.
16913 If first element is a string or a cons, process all the elements
16914 and effectively concatenate them.
16915 If first element is a negative number, truncate displaying cdr to
16916 at most that many characters. If positive, pad (with spaces)
16917 to at least that many characters.
16918 If first element is a symbol, process the cadr or caddr recursively
16919 according to whether the symbol's value is non-nil or nil. */
16920 car = XCAR (elt);
16921 if (EQ (car, QCeval))
16923 /* An element of the form (:eval FORM) means evaluate FORM
16924 and use the result as mode line elements. */
16926 if (risky)
16927 break;
16929 if (CONSP (XCDR (elt)))
16931 Lisp_Object spec;
16932 spec = safe_eval (XCAR (XCDR (elt)));
16933 n += display_mode_element (it, depth, field_width - n,
16934 precision - n, spec, props,
16935 risky);
16938 else if (EQ (car, QCpropertize))
16940 /* An element of the form (:propertize ELT PROPS...)
16941 means display ELT but applying properties PROPS. */
16943 if (risky)
16944 break;
16946 if (CONSP (XCDR (elt)))
16947 n += display_mode_element (it, depth, field_width - n,
16948 precision - n, XCAR (XCDR (elt)),
16949 XCDR (XCDR (elt)), risky);
16951 else if (SYMBOLP (car))
16953 tem = Fboundp (car);
16954 elt = XCDR (elt);
16955 if (!CONSP (elt))
16956 goto invalid;
16957 /* elt is now the cdr, and we know it is a cons cell.
16958 Use its car if CAR has a non-nil value. */
16959 if (!NILP (tem))
16961 tem = Fsymbol_value (car);
16962 if (!NILP (tem))
16964 elt = XCAR (elt);
16965 goto tail_recurse;
16968 /* Symbol's value is nil (or symbol is unbound)
16969 Get the cddr of the original list
16970 and if possible find the caddr and use that. */
16971 elt = XCDR (elt);
16972 if (NILP (elt))
16973 break;
16974 else if (!CONSP (elt))
16975 goto invalid;
16976 elt = XCAR (elt);
16977 goto tail_recurse;
16979 else if (INTEGERP (car))
16981 register int lim = XINT (car);
16982 elt = XCDR (elt);
16983 if (lim < 0)
16985 /* Negative int means reduce maximum width. */
16986 if (precision <= 0)
16987 precision = -lim;
16988 else
16989 precision = min (precision, -lim);
16991 else if (lim > 0)
16993 /* Padding specified. Don't let it be more than
16994 current maximum. */
16995 if (precision > 0)
16996 lim = min (precision, lim);
16998 /* If that's more padding than already wanted, queue it.
16999 But don't reduce padding already specified even if
17000 that is beyond the current truncation point. */
17001 field_width = max (lim, field_width);
17003 goto tail_recurse;
17005 else if (STRINGP (car) || CONSP (car))
17007 register int limit = 50;
17008 /* Limit is to protect against circular lists. */
17009 while (CONSP (elt)
17010 && --limit > 0
17011 && (precision <= 0 || n < precision))
17013 n += display_mode_element (it, depth,
17014 /* Do padding only after the last
17015 element in the list. */
17016 (! CONSP (XCDR (elt))
17017 ? field_width - n
17018 : 0),
17019 precision - n, XCAR (elt),
17020 props, risky);
17021 elt = XCDR (elt);
17025 break;
17027 default:
17028 invalid:
17029 elt = build_string ("*invalid*");
17030 goto tail_recurse;
17033 /* Pad to FIELD_WIDTH. */
17034 if (field_width > 0 && n < field_width)
17036 switch (mode_line_target)
17038 case MODE_LINE_NOPROP:
17039 case MODE_LINE_TITLE:
17040 n += store_mode_line_noprop ("", field_width - n, 0);
17041 break;
17042 case MODE_LINE_STRING:
17043 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17044 break;
17045 case MODE_LINE_DISPLAY:
17046 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17047 0, 0, 0);
17048 break;
17052 return n;
17055 /* Store a mode-line string element in mode_line_string_list.
17057 If STRING is non-null, display that C string. Otherwise, the Lisp
17058 string LISP_STRING is displayed.
17060 FIELD_WIDTH is the minimum number of output glyphs to produce.
17061 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17062 with spaces. FIELD_WIDTH <= 0 means don't pad.
17064 PRECISION is the maximum number of characters to output from
17065 STRING. PRECISION <= 0 means don't truncate the string.
17067 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17068 properties to the string.
17070 PROPS are the properties to add to the string.
17071 The mode_line_string_face face property is always added to the string.
17074 static int
17075 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17076 char *string;
17077 Lisp_Object lisp_string;
17078 int copy_string;
17079 int field_width;
17080 int precision;
17081 Lisp_Object props;
17083 int len;
17084 int n = 0;
17086 if (string != NULL)
17088 len = strlen (string);
17089 if (precision > 0 && len > precision)
17090 len = precision;
17091 lisp_string = make_string (string, len);
17092 if (NILP (props))
17093 props = mode_line_string_face_prop;
17094 else if (!NILP (mode_line_string_face))
17096 Lisp_Object face = Fplist_get (props, Qface);
17097 props = Fcopy_sequence (props);
17098 if (NILP (face))
17099 face = mode_line_string_face;
17100 else
17101 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17102 props = Fplist_put (props, Qface, face);
17104 Fadd_text_properties (make_number (0), make_number (len),
17105 props, lisp_string);
17107 else
17109 len = XFASTINT (Flength (lisp_string));
17110 if (precision > 0 && len > precision)
17112 len = precision;
17113 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17114 precision = -1;
17116 if (!NILP (mode_line_string_face))
17118 Lisp_Object face;
17119 if (NILP (props))
17120 props = Ftext_properties_at (make_number (0), lisp_string);
17121 face = Fplist_get (props, Qface);
17122 if (NILP (face))
17123 face = mode_line_string_face;
17124 else
17125 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17126 props = Fcons (Qface, Fcons (face, Qnil));
17127 if (copy_string)
17128 lisp_string = Fcopy_sequence (lisp_string);
17130 if (!NILP (props))
17131 Fadd_text_properties (make_number (0), make_number (len),
17132 props, lisp_string);
17135 if (len > 0)
17137 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17138 n += len;
17141 if (field_width > len)
17143 field_width -= len;
17144 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17145 if (!NILP (props))
17146 Fadd_text_properties (make_number (0), make_number (field_width),
17147 props, lisp_string);
17148 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17149 n += field_width;
17152 return n;
17156 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17157 1, 4, 0,
17158 doc: /* Format a string out of a mode line format specification.
17159 First arg FORMAT specifies the mode line format (see `mode-line-format'
17160 for details) to use.
17162 Optional second arg FACE specifies the face property to put
17163 on all characters for which no face is specified.
17164 t means whatever face the window's mode line currently uses
17165 \(either `mode-line' or `mode-line-inactive', depending).
17166 nil means the default is no face property.
17167 If FACE is an integer, the value string has no text properties.
17169 Optional third and fourth args WINDOW and BUFFER specify the window
17170 and buffer to use as the context for the formatting (defaults
17171 are the selected window and the window's buffer). */)
17172 (format, face, window, buffer)
17173 Lisp_Object format, face, window, buffer;
17175 struct it it;
17176 int len;
17177 struct window *w;
17178 struct buffer *old_buffer = NULL;
17179 int face_id = -1;
17180 int no_props = INTEGERP (face);
17181 int count = SPECPDL_INDEX ();
17182 Lisp_Object str;
17183 int string_start = 0;
17185 if (NILP (window))
17186 window = selected_window;
17187 CHECK_WINDOW (window);
17188 w = XWINDOW (window);
17190 if (NILP (buffer))
17191 buffer = w->buffer;
17192 CHECK_BUFFER (buffer);
17194 if (NILP (format))
17195 return build_string ("");
17197 if (no_props)
17198 face = Qnil;
17200 if (!NILP (face))
17202 if (EQ (face, Qt))
17203 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17204 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17207 if (face_id < 0)
17208 face_id = DEFAULT_FACE_ID;
17210 if (XBUFFER (buffer) != current_buffer)
17211 old_buffer = current_buffer;
17213 /* Save things including mode_line_proptrans_alist,
17214 and set that to nil so that we don't alter the outer value. */
17215 record_unwind_protect (unwind_format_mode_line,
17216 format_mode_line_unwind_data (old_buffer, 1));
17217 mode_line_proptrans_alist = Qnil;
17219 if (old_buffer)
17220 set_buffer_internal_1 (XBUFFER (buffer));
17222 init_iterator (&it, w, -1, -1, NULL, face_id);
17224 if (no_props)
17226 mode_line_target = MODE_LINE_NOPROP;
17227 mode_line_string_face_prop = Qnil;
17228 mode_line_string_list = Qnil;
17229 string_start = MODE_LINE_NOPROP_LEN (0);
17231 else
17233 mode_line_target = MODE_LINE_STRING;
17234 mode_line_string_list = Qnil;
17235 mode_line_string_face = face;
17236 mode_line_string_face_prop
17237 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17240 push_frame_kboard (it.f);
17241 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17242 pop_frame_kboard ();
17244 if (no_props)
17246 len = MODE_LINE_NOPROP_LEN (string_start);
17247 str = make_string (mode_line_noprop_buf + string_start, len);
17249 else
17251 mode_line_string_list = Fnreverse (mode_line_string_list);
17252 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17253 make_string ("", 0));
17256 unbind_to (count, Qnil);
17257 return str;
17260 /* Write a null-terminated, right justified decimal representation of
17261 the positive integer D to BUF using a minimal field width WIDTH. */
17263 static void
17264 pint2str (buf, width, d)
17265 register char *buf;
17266 register int width;
17267 register int d;
17269 register char *p = buf;
17271 if (d <= 0)
17272 *p++ = '0';
17273 else
17275 while (d > 0)
17277 *p++ = d % 10 + '0';
17278 d /= 10;
17282 for (width -= (int) (p - buf); width > 0; --width)
17283 *p++ = ' ';
17284 *p-- = '\0';
17285 while (p > buf)
17287 d = *buf;
17288 *buf++ = *p;
17289 *p-- = d;
17293 /* Write a null-terminated, right justified decimal and "human
17294 readable" representation of the nonnegative integer D to BUF using
17295 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17297 static const char power_letter[] =
17299 0, /* not used */
17300 'k', /* kilo */
17301 'M', /* mega */
17302 'G', /* giga */
17303 'T', /* tera */
17304 'P', /* peta */
17305 'E', /* exa */
17306 'Z', /* zetta */
17307 'Y' /* yotta */
17310 static void
17311 pint2hrstr (buf, width, d)
17312 char *buf;
17313 int width;
17314 int d;
17316 /* We aim to represent the nonnegative integer D as
17317 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17318 int quotient = d;
17319 int remainder = 0;
17320 /* -1 means: do not use TENTHS. */
17321 int tenths = -1;
17322 int exponent = 0;
17324 /* Length of QUOTIENT.TENTHS as a string. */
17325 int length;
17327 char * psuffix;
17328 char * p;
17330 if (1000 <= quotient)
17332 /* Scale to the appropriate EXPONENT. */
17335 remainder = quotient % 1000;
17336 quotient /= 1000;
17337 exponent++;
17339 while (1000 <= quotient);
17341 /* Round to nearest and decide whether to use TENTHS or not. */
17342 if (quotient <= 9)
17344 tenths = remainder / 100;
17345 if (50 <= remainder % 100)
17347 if (tenths < 9)
17348 tenths++;
17349 else
17351 quotient++;
17352 if (quotient == 10)
17353 tenths = -1;
17354 else
17355 tenths = 0;
17359 else
17360 if (500 <= remainder)
17362 if (quotient < 999)
17363 quotient++;
17364 else
17366 quotient = 1;
17367 exponent++;
17368 tenths = 0;
17373 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17374 if (tenths == -1 && quotient <= 99)
17375 if (quotient <= 9)
17376 length = 1;
17377 else
17378 length = 2;
17379 else
17380 length = 3;
17381 p = psuffix = buf + max (width, length);
17383 /* Print EXPONENT. */
17384 if (exponent)
17385 *psuffix++ = power_letter[exponent];
17386 *psuffix = '\0';
17388 /* Print TENTHS. */
17389 if (tenths >= 0)
17391 *--p = '0' + tenths;
17392 *--p = '.';
17395 /* Print QUOTIENT. */
17398 int digit = quotient % 10;
17399 *--p = '0' + digit;
17401 while ((quotient /= 10) != 0);
17403 /* Print leading spaces. */
17404 while (buf < p)
17405 *--p = ' ';
17408 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17409 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17410 type of CODING_SYSTEM. Return updated pointer into BUF. */
17412 static unsigned char invalid_eol_type[] = "(*invalid*)";
17414 static char *
17415 decode_mode_spec_coding (coding_system, buf, eol_flag)
17416 Lisp_Object coding_system;
17417 register char *buf;
17418 int eol_flag;
17420 Lisp_Object val;
17421 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17422 const unsigned char *eol_str;
17423 int eol_str_len;
17424 /* The EOL conversion we are using. */
17425 Lisp_Object eoltype;
17427 val = Fget (coding_system, Qcoding_system);
17428 eoltype = Qnil;
17430 if (!VECTORP (val)) /* Not yet decided. */
17432 if (multibyte)
17433 *buf++ = '-';
17434 if (eol_flag)
17435 eoltype = eol_mnemonic_undecided;
17436 /* Don't mention EOL conversion if it isn't decided. */
17438 else
17440 Lisp_Object eolvalue;
17442 eolvalue = Fget (coding_system, Qeol_type);
17444 if (multibyte)
17445 *buf++ = XFASTINT (AREF (val, 1));
17447 if (eol_flag)
17449 /* The EOL conversion that is normal on this system. */
17451 if (NILP (eolvalue)) /* Not yet decided. */
17452 eoltype = eol_mnemonic_undecided;
17453 else if (VECTORP (eolvalue)) /* Not yet decided. */
17454 eoltype = eol_mnemonic_undecided;
17455 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17456 eoltype = (XFASTINT (eolvalue) == 0
17457 ? eol_mnemonic_unix
17458 : (XFASTINT (eolvalue) == 1
17459 ? eol_mnemonic_dos : eol_mnemonic_mac));
17463 if (eol_flag)
17465 /* Mention the EOL conversion if it is not the usual one. */
17466 if (STRINGP (eoltype))
17468 eol_str = SDATA (eoltype);
17469 eol_str_len = SBYTES (eoltype);
17471 else if (INTEGERP (eoltype)
17472 && CHAR_VALID_P (XINT (eoltype), 0))
17474 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17475 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17476 eol_str = tmp;
17478 else
17480 eol_str = invalid_eol_type;
17481 eol_str_len = sizeof (invalid_eol_type) - 1;
17483 bcopy (eol_str, buf, eol_str_len);
17484 buf += eol_str_len;
17487 return buf;
17490 /* Return a string for the output of a mode line %-spec for window W,
17491 generated by character C. PRECISION >= 0 means don't return a
17492 string longer than that value. FIELD_WIDTH > 0 means pad the
17493 string returned with spaces to that value. Return 1 in *MULTIBYTE
17494 if the result is multibyte text.
17496 Note we operate on the current buffer for most purposes,
17497 the exception being w->base_line_pos. */
17499 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17501 static char *
17502 decode_mode_spec (w, c, field_width, precision, multibyte)
17503 struct window *w;
17504 register int c;
17505 int field_width, precision;
17506 int *multibyte;
17508 Lisp_Object obj;
17509 struct frame *f = XFRAME (WINDOW_FRAME (w));
17510 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17511 struct buffer *b = current_buffer;
17513 obj = Qnil;
17514 *multibyte = 0;
17516 switch (c)
17518 case '*':
17519 if (!NILP (b->read_only))
17520 return "%";
17521 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17522 return "*";
17523 return "-";
17525 case '+':
17526 /* This differs from %* only for a modified read-only buffer. */
17527 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17528 return "*";
17529 if (!NILP (b->read_only))
17530 return "%";
17531 return "-";
17533 case '&':
17534 /* This differs from %* in ignoring read-only-ness. */
17535 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17536 return "*";
17537 return "-";
17539 case '%':
17540 return "%";
17542 case '[':
17544 int i;
17545 char *p;
17547 if (command_loop_level > 5)
17548 return "[[[... ";
17549 p = decode_mode_spec_buf;
17550 for (i = 0; i < command_loop_level; i++)
17551 *p++ = '[';
17552 *p = 0;
17553 return decode_mode_spec_buf;
17556 case ']':
17558 int i;
17559 char *p;
17561 if (command_loop_level > 5)
17562 return " ...]]]";
17563 p = decode_mode_spec_buf;
17564 for (i = 0; i < command_loop_level; i++)
17565 *p++ = ']';
17566 *p = 0;
17567 return decode_mode_spec_buf;
17570 case '-':
17572 register int i;
17574 /* Let lots_of_dashes be a string of infinite length. */
17575 if (mode_line_target == MODE_LINE_NOPROP ||
17576 mode_line_target == MODE_LINE_STRING)
17577 return "--";
17578 if (field_width <= 0
17579 || field_width > sizeof (lots_of_dashes))
17581 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17582 decode_mode_spec_buf[i] = '-';
17583 decode_mode_spec_buf[i] = '\0';
17584 return decode_mode_spec_buf;
17586 else
17587 return lots_of_dashes;
17590 case 'b':
17591 obj = b->name;
17592 break;
17594 case 'c':
17596 int col = (int) current_column (); /* iftc */
17597 w->column_number_displayed = make_number (col);
17598 pint2str (decode_mode_spec_buf, field_width, col);
17599 return decode_mode_spec_buf;
17602 case 'e':
17603 #ifndef SYSTEM_MALLOC
17605 if (NILP (Vmemory_full))
17606 return "";
17607 else
17608 return "!MEM FULL! ";
17610 #else
17611 return "";
17612 #endif
17614 case 'F':
17615 /* %F displays the frame name. */
17616 if (!NILP (f->title))
17617 return (char *) SDATA (f->title);
17618 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17619 return (char *) SDATA (f->name);
17620 return "Emacs";
17622 case 'f':
17623 obj = b->filename;
17624 break;
17626 case 'i':
17628 int size = ZV - BEGV;
17629 pint2str (decode_mode_spec_buf, field_width, size);
17630 return decode_mode_spec_buf;
17633 case 'I':
17635 int size = ZV - BEGV;
17636 pint2hrstr (decode_mode_spec_buf, field_width, size);
17637 return decode_mode_spec_buf;
17640 case 'l':
17642 int startpos = XMARKER (w->start)->charpos;
17643 int startpos_byte = marker_byte_position (w->start);
17644 int line, linepos, linepos_byte, topline;
17645 int nlines, junk;
17646 int height = WINDOW_TOTAL_LINES (w);
17648 /* If we decided that this buffer isn't suitable for line numbers,
17649 don't forget that too fast. */
17650 if (EQ (w->base_line_pos, w->buffer))
17651 goto no_value;
17652 /* But do forget it, if the window shows a different buffer now. */
17653 else if (BUFFERP (w->base_line_pos))
17654 w->base_line_pos = Qnil;
17656 /* If the buffer is very big, don't waste time. */
17657 if (INTEGERP (Vline_number_display_limit)
17658 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17660 w->base_line_pos = Qnil;
17661 w->base_line_number = Qnil;
17662 goto no_value;
17665 if (!NILP (w->base_line_number)
17666 && !NILP (w->base_line_pos)
17667 && XFASTINT (w->base_line_pos) <= startpos)
17669 line = XFASTINT (w->base_line_number);
17670 linepos = XFASTINT (w->base_line_pos);
17671 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17673 else
17675 line = 1;
17676 linepos = BUF_BEGV (b);
17677 linepos_byte = BUF_BEGV_BYTE (b);
17680 /* Count lines from base line to window start position. */
17681 nlines = display_count_lines (linepos, linepos_byte,
17682 startpos_byte,
17683 startpos, &junk);
17685 topline = nlines + line;
17687 /* Determine a new base line, if the old one is too close
17688 or too far away, or if we did not have one.
17689 "Too close" means it's plausible a scroll-down would
17690 go back past it. */
17691 if (startpos == BUF_BEGV (b))
17693 w->base_line_number = make_number (topline);
17694 w->base_line_pos = make_number (BUF_BEGV (b));
17696 else if (nlines < height + 25 || nlines > height * 3 + 50
17697 || linepos == BUF_BEGV (b))
17699 int limit = BUF_BEGV (b);
17700 int limit_byte = BUF_BEGV_BYTE (b);
17701 int position;
17702 int distance = (height * 2 + 30) * line_number_display_limit_width;
17704 if (startpos - distance > limit)
17706 limit = startpos - distance;
17707 limit_byte = CHAR_TO_BYTE (limit);
17710 nlines = display_count_lines (startpos, startpos_byte,
17711 limit_byte,
17712 - (height * 2 + 30),
17713 &position);
17714 /* If we couldn't find the lines we wanted within
17715 line_number_display_limit_width chars per line,
17716 give up on line numbers for this window. */
17717 if (position == limit_byte && limit == startpos - distance)
17719 w->base_line_pos = w->buffer;
17720 w->base_line_number = Qnil;
17721 goto no_value;
17724 w->base_line_number = make_number (topline - nlines);
17725 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17728 /* Now count lines from the start pos to point. */
17729 nlines = display_count_lines (startpos, startpos_byte,
17730 PT_BYTE, PT, &junk);
17732 /* Record that we did display the line number. */
17733 line_number_displayed = 1;
17735 /* Make the string to show. */
17736 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17737 return decode_mode_spec_buf;
17738 no_value:
17740 char* p = decode_mode_spec_buf;
17741 int pad = field_width - 2;
17742 while (pad-- > 0)
17743 *p++ = ' ';
17744 *p++ = '?';
17745 *p++ = '?';
17746 *p = '\0';
17747 return decode_mode_spec_buf;
17750 break;
17752 case 'm':
17753 obj = b->mode_name;
17754 break;
17756 case 'n':
17757 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17758 return " Narrow";
17759 break;
17761 case 'p':
17763 int pos = marker_position (w->start);
17764 int total = BUF_ZV (b) - BUF_BEGV (b);
17766 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17768 if (pos <= BUF_BEGV (b))
17769 return "All";
17770 else
17771 return "Bottom";
17773 else if (pos <= BUF_BEGV (b))
17774 return "Top";
17775 else
17777 if (total > 1000000)
17778 /* Do it differently for a large value, to avoid overflow. */
17779 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17780 else
17781 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17782 /* We can't normally display a 3-digit number,
17783 so get us a 2-digit number that is close. */
17784 if (total == 100)
17785 total = 99;
17786 sprintf (decode_mode_spec_buf, "%2d%%", total);
17787 return decode_mode_spec_buf;
17791 /* Display percentage of size above the bottom of the screen. */
17792 case 'P':
17794 int toppos = marker_position (w->start);
17795 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17796 int total = BUF_ZV (b) - BUF_BEGV (b);
17798 if (botpos >= BUF_ZV (b))
17800 if (toppos <= BUF_BEGV (b))
17801 return "All";
17802 else
17803 return "Bottom";
17805 else
17807 if (total > 1000000)
17808 /* Do it differently for a large value, to avoid overflow. */
17809 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17810 else
17811 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17812 /* We can't normally display a 3-digit number,
17813 so get us a 2-digit number that is close. */
17814 if (total == 100)
17815 total = 99;
17816 if (toppos <= BUF_BEGV (b))
17817 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17818 else
17819 sprintf (decode_mode_spec_buf, "%2d%%", total);
17820 return decode_mode_spec_buf;
17824 case 's':
17825 /* status of process */
17826 obj = Fget_buffer_process (Fcurrent_buffer ());
17827 if (NILP (obj))
17828 return "no process";
17829 #ifdef subprocesses
17830 obj = Fsymbol_name (Fprocess_status (obj));
17831 #endif
17832 break;
17834 case 't': /* indicate TEXT or BINARY */
17835 #ifdef MODE_LINE_BINARY_TEXT
17836 return MODE_LINE_BINARY_TEXT (b);
17837 #else
17838 return "T";
17839 #endif
17841 case 'z':
17842 /* coding-system (not including end-of-line format) */
17843 case 'Z':
17844 /* coding-system (including end-of-line type) */
17846 int eol_flag = (c == 'Z');
17847 char *p = decode_mode_spec_buf;
17849 if (! FRAME_WINDOW_P (f))
17851 /* No need to mention EOL here--the terminal never needs
17852 to do EOL conversion. */
17853 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17854 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17856 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17857 p, eol_flag);
17859 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17860 #ifdef subprocesses
17861 obj = Fget_buffer_process (Fcurrent_buffer ());
17862 if (PROCESSP (obj))
17864 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17865 p, eol_flag);
17866 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17867 p, eol_flag);
17869 #endif /* subprocesses */
17870 #endif /* 0 */
17871 *p = 0;
17872 return decode_mode_spec_buf;
17876 if (STRINGP (obj))
17878 *multibyte = STRING_MULTIBYTE (obj);
17879 return (char *) SDATA (obj);
17881 else
17882 return "";
17886 /* Count up to COUNT lines starting from START / START_BYTE.
17887 But don't go beyond LIMIT_BYTE.
17888 Return the number of lines thus found (always nonnegative).
17890 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17892 static int
17893 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17894 int start, start_byte, limit_byte, count;
17895 int *byte_pos_ptr;
17897 register unsigned char *cursor;
17898 unsigned char *base;
17900 register int ceiling;
17901 register unsigned char *ceiling_addr;
17902 int orig_count = count;
17904 /* If we are not in selective display mode,
17905 check only for newlines. */
17906 int selective_display = (!NILP (current_buffer->selective_display)
17907 && !INTEGERP (current_buffer->selective_display));
17909 if (count > 0)
17911 while (start_byte < limit_byte)
17913 ceiling = BUFFER_CEILING_OF (start_byte);
17914 ceiling = min (limit_byte - 1, ceiling);
17915 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17916 base = (cursor = BYTE_POS_ADDR (start_byte));
17917 while (1)
17919 if (selective_display)
17920 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17922 else
17923 while (*cursor != '\n' && ++cursor != ceiling_addr)
17926 if (cursor != ceiling_addr)
17928 if (--count == 0)
17930 start_byte += cursor - base + 1;
17931 *byte_pos_ptr = start_byte;
17932 return orig_count;
17934 else
17935 if (++cursor == ceiling_addr)
17936 break;
17938 else
17939 break;
17941 start_byte += cursor - base;
17944 else
17946 while (start_byte > limit_byte)
17948 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17949 ceiling = max (limit_byte, ceiling);
17950 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17951 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17952 while (1)
17954 if (selective_display)
17955 while (--cursor != ceiling_addr
17956 && *cursor != '\n' && *cursor != 015)
17958 else
17959 while (--cursor != ceiling_addr && *cursor != '\n')
17962 if (cursor != ceiling_addr)
17964 if (++count == 0)
17966 start_byte += cursor - base + 1;
17967 *byte_pos_ptr = start_byte;
17968 /* When scanning backwards, we should
17969 not count the newline posterior to which we stop. */
17970 return - orig_count - 1;
17973 else
17974 break;
17976 /* Here we add 1 to compensate for the last decrement
17977 of CURSOR, which took it past the valid range. */
17978 start_byte += cursor - base + 1;
17982 *byte_pos_ptr = limit_byte;
17984 if (count < 0)
17985 return - orig_count + count;
17986 return orig_count - count;
17992 /***********************************************************************
17993 Displaying strings
17994 ***********************************************************************/
17996 /* Display a NUL-terminated string, starting with index START.
17998 If STRING is non-null, display that C string. Otherwise, the Lisp
17999 string LISP_STRING is displayed.
18001 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18002 FACE_STRING. Display STRING or LISP_STRING with the face at
18003 FACE_STRING_POS in FACE_STRING:
18005 Display the string in the environment given by IT, but use the
18006 standard display table, temporarily.
18008 FIELD_WIDTH is the minimum number of output glyphs to produce.
18009 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18010 with spaces. If STRING has more characters, more than FIELD_WIDTH
18011 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18013 PRECISION is the maximum number of characters to output from
18014 STRING. PRECISION < 0 means don't truncate the string.
18016 This is roughly equivalent to printf format specifiers:
18018 FIELD_WIDTH PRECISION PRINTF
18019 ----------------------------------------
18020 -1 -1 %s
18021 -1 10 %.10s
18022 10 -1 %10s
18023 20 10 %20.10s
18025 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18026 display them, and < 0 means obey the current buffer's value of
18027 enable_multibyte_characters.
18029 Value is the number of columns displayed. */
18031 static int
18032 display_string (string, lisp_string, face_string, face_string_pos,
18033 start, it, field_width, precision, max_x, multibyte)
18034 unsigned char *string;
18035 Lisp_Object lisp_string;
18036 Lisp_Object face_string;
18037 int face_string_pos;
18038 int start;
18039 struct it *it;
18040 int field_width, precision, max_x;
18041 int multibyte;
18043 int hpos_at_start = it->hpos;
18044 int saved_face_id = it->face_id;
18045 struct glyph_row *row = it->glyph_row;
18047 /* Initialize the iterator IT for iteration over STRING beginning
18048 with index START. */
18049 reseat_to_string (it, string, lisp_string, start,
18050 precision, field_width, multibyte);
18052 /* If displaying STRING, set up the face of the iterator
18053 from LISP_STRING, if that's given. */
18054 if (STRINGP (face_string))
18056 int endptr;
18057 struct face *face;
18059 it->face_id
18060 = face_at_string_position (it->w, face_string, face_string_pos,
18061 0, it->region_beg_charpos,
18062 it->region_end_charpos,
18063 &endptr, it->base_face_id, 0);
18064 face = FACE_FROM_ID (it->f, it->face_id);
18065 it->face_box_p = face->box != FACE_NO_BOX;
18068 /* Set max_x to the maximum allowed X position. Don't let it go
18069 beyond the right edge of the window. */
18070 if (max_x <= 0)
18071 max_x = it->last_visible_x;
18072 else
18073 max_x = min (max_x, it->last_visible_x);
18075 /* Skip over display elements that are not visible. because IT->w is
18076 hscrolled. */
18077 if (it->current_x < it->first_visible_x)
18078 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18079 MOVE_TO_POS | MOVE_TO_X);
18081 row->ascent = it->max_ascent;
18082 row->height = it->max_ascent + it->max_descent;
18083 row->phys_ascent = it->max_phys_ascent;
18084 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18085 row->extra_line_spacing = it->max_extra_line_spacing;
18087 /* This condition is for the case that we are called with current_x
18088 past last_visible_x. */
18089 while (it->current_x < max_x)
18091 int x_before, x, n_glyphs_before, i, nglyphs;
18093 /* Get the next display element. */
18094 if (!get_next_display_element (it))
18095 break;
18097 /* Produce glyphs. */
18098 x_before = it->current_x;
18099 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18100 PRODUCE_GLYPHS (it);
18102 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18103 i = 0;
18104 x = x_before;
18105 while (i < nglyphs)
18107 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18109 if (!it->truncate_lines_p
18110 && x + glyph->pixel_width > max_x)
18112 /* End of continued line or max_x reached. */
18113 if (CHAR_GLYPH_PADDING_P (*glyph))
18115 /* A wide character is unbreakable. */
18116 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18117 it->current_x = x_before;
18119 else
18121 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18122 it->current_x = x;
18124 break;
18126 else if (x + glyph->pixel_width > it->first_visible_x)
18128 /* Glyph is at least partially visible. */
18129 ++it->hpos;
18130 if (x < it->first_visible_x)
18131 it->glyph_row->x = x - it->first_visible_x;
18133 else
18135 /* Glyph is off the left margin of the display area.
18136 Should not happen. */
18137 abort ();
18140 row->ascent = max (row->ascent, it->max_ascent);
18141 row->height = max (row->height, it->max_ascent + it->max_descent);
18142 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18143 row->phys_height = max (row->phys_height,
18144 it->max_phys_ascent + it->max_phys_descent);
18145 row->extra_line_spacing = max (row->extra_line_spacing,
18146 it->max_extra_line_spacing);
18147 x += glyph->pixel_width;
18148 ++i;
18151 /* Stop if max_x reached. */
18152 if (i < nglyphs)
18153 break;
18155 /* Stop at line ends. */
18156 if (ITERATOR_AT_END_OF_LINE_P (it))
18158 it->continuation_lines_width = 0;
18159 break;
18162 set_iterator_to_next (it, 1);
18164 /* Stop if truncating at the right edge. */
18165 if (it->truncate_lines_p
18166 && it->current_x >= it->last_visible_x)
18168 /* Add truncation mark, but don't do it if the line is
18169 truncated at a padding space. */
18170 if (IT_CHARPOS (*it) < it->string_nchars)
18172 if (!FRAME_WINDOW_P (it->f))
18174 int i, n;
18176 if (it->current_x > it->last_visible_x)
18178 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18179 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18180 break;
18181 for (n = row->used[TEXT_AREA]; i < n; ++i)
18183 row->used[TEXT_AREA] = i;
18184 produce_special_glyphs (it, IT_TRUNCATION);
18187 produce_special_glyphs (it, IT_TRUNCATION);
18189 it->glyph_row->truncated_on_right_p = 1;
18191 break;
18195 /* Maybe insert a truncation at the left. */
18196 if (it->first_visible_x
18197 && IT_CHARPOS (*it) > 0)
18199 if (!FRAME_WINDOW_P (it->f))
18200 insert_left_trunc_glyphs (it);
18201 it->glyph_row->truncated_on_left_p = 1;
18204 it->face_id = saved_face_id;
18206 /* Value is number of columns displayed. */
18207 return it->hpos - hpos_at_start;
18212 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18213 appears as an element of LIST or as the car of an element of LIST.
18214 If PROPVAL is a list, compare each element against LIST in that
18215 way, and return 1/2 if any element of PROPVAL is found in LIST.
18216 Otherwise return 0. This function cannot quit.
18217 The return value is 2 if the text is invisible but with an ellipsis
18218 and 1 if it's invisible and without an ellipsis. */
18221 invisible_p (propval, list)
18222 register Lisp_Object propval;
18223 Lisp_Object list;
18225 register Lisp_Object tail, proptail;
18227 for (tail = list; CONSP (tail); tail = XCDR (tail))
18229 register Lisp_Object tem;
18230 tem = XCAR (tail);
18231 if (EQ (propval, tem))
18232 return 1;
18233 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18234 return NILP (XCDR (tem)) ? 1 : 2;
18237 if (CONSP (propval))
18239 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18241 Lisp_Object propelt;
18242 propelt = XCAR (proptail);
18243 for (tail = list; CONSP (tail); tail = XCDR (tail))
18245 register Lisp_Object tem;
18246 tem = XCAR (tail);
18247 if (EQ (propelt, tem))
18248 return 1;
18249 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18250 return NILP (XCDR (tem)) ? 1 : 2;
18255 return 0;
18258 /* Calculate a width or height in pixels from a specification using
18259 the following elements:
18261 SPEC ::=
18262 NUM - a (fractional) multiple of the default font width/height
18263 (NUM) - specifies exactly NUM pixels
18264 UNIT - a fixed number of pixels, see below.
18265 ELEMENT - size of a display element in pixels, see below.
18266 (NUM . SPEC) - equals NUM * SPEC
18267 (+ SPEC SPEC ...) - add pixel values
18268 (- SPEC SPEC ...) - subtract pixel values
18269 (- SPEC) - negate pixel value
18271 NUM ::=
18272 INT or FLOAT - a number constant
18273 SYMBOL - use symbol's (buffer local) variable binding.
18275 UNIT ::=
18276 in - pixels per inch *)
18277 mm - pixels per 1/1000 meter *)
18278 cm - pixels per 1/100 meter *)
18279 width - width of current font in pixels.
18280 height - height of current font in pixels.
18282 *) using the ratio(s) defined in display-pixels-per-inch.
18284 ELEMENT ::=
18286 left-fringe - left fringe width in pixels
18287 right-fringe - right fringe width in pixels
18289 left-margin - left margin width in pixels
18290 right-margin - right margin width in pixels
18292 scroll-bar - scroll-bar area width in pixels
18294 Examples:
18296 Pixels corresponding to 5 inches:
18297 (5 . in)
18299 Total width of non-text areas on left side of window (if scroll-bar is on left):
18300 '(space :width (+ left-fringe left-margin scroll-bar))
18302 Align to first text column (in header line):
18303 '(space :align-to 0)
18305 Align to middle of text area minus half the width of variable `my-image'
18306 containing a loaded image:
18307 '(space :align-to (0.5 . (- text my-image)))
18309 Width of left margin minus width of 1 character in the default font:
18310 '(space :width (- left-margin 1))
18312 Width of left margin minus width of 2 characters in the current font:
18313 '(space :width (- left-margin (2 . width)))
18315 Center 1 character over left-margin (in header line):
18316 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18318 Different ways to express width of left fringe plus left margin minus one pixel:
18319 '(space :width (- (+ left-fringe left-margin) (1)))
18320 '(space :width (+ left-fringe left-margin (- (1))))
18321 '(space :width (+ left-fringe left-margin (-1)))
18325 #define NUMVAL(X) \
18326 ((INTEGERP (X) || FLOATP (X)) \
18327 ? XFLOATINT (X) \
18328 : - 1)
18331 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18332 double *res;
18333 struct it *it;
18334 Lisp_Object prop;
18335 void *font;
18336 int width_p, *align_to;
18338 double pixels;
18340 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18341 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18343 if (NILP (prop))
18344 return OK_PIXELS (0);
18346 if (SYMBOLP (prop))
18348 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18350 char *unit = SDATA (SYMBOL_NAME (prop));
18352 if (unit[0] == 'i' && unit[1] == 'n')
18353 pixels = 1.0;
18354 else if (unit[0] == 'm' && unit[1] == 'm')
18355 pixels = 25.4;
18356 else if (unit[0] == 'c' && unit[1] == 'm')
18357 pixels = 2.54;
18358 else
18359 pixels = 0;
18360 if (pixels > 0)
18362 double ppi;
18363 #ifdef HAVE_WINDOW_SYSTEM
18364 if (FRAME_WINDOW_P (it->f)
18365 && (ppi = (width_p
18366 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18367 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18368 ppi > 0))
18369 return OK_PIXELS (ppi / pixels);
18370 #endif
18372 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18373 || (CONSP (Vdisplay_pixels_per_inch)
18374 && (ppi = (width_p
18375 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18376 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18377 ppi > 0)))
18378 return OK_PIXELS (ppi / pixels);
18380 return 0;
18384 #ifdef HAVE_WINDOW_SYSTEM
18385 if (EQ (prop, Qheight))
18386 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18387 if (EQ (prop, Qwidth))
18388 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18389 #else
18390 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18391 return OK_PIXELS (1);
18392 #endif
18394 if (EQ (prop, Qtext))
18395 return OK_PIXELS (width_p
18396 ? window_box_width (it->w, TEXT_AREA)
18397 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18399 if (align_to && *align_to < 0)
18401 *res = 0;
18402 if (EQ (prop, Qleft))
18403 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18404 if (EQ (prop, Qright))
18405 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18406 if (EQ (prop, Qcenter))
18407 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18408 + window_box_width (it->w, TEXT_AREA) / 2);
18409 if (EQ (prop, Qleft_fringe))
18410 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18411 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18412 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18413 if (EQ (prop, Qright_fringe))
18414 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18415 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18416 : window_box_right_offset (it->w, TEXT_AREA));
18417 if (EQ (prop, Qleft_margin))
18418 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18419 if (EQ (prop, Qright_margin))
18420 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18421 if (EQ (prop, Qscroll_bar))
18422 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18424 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18425 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18426 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18427 : 0)));
18429 else
18431 if (EQ (prop, Qleft_fringe))
18432 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18433 if (EQ (prop, Qright_fringe))
18434 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18435 if (EQ (prop, Qleft_margin))
18436 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18437 if (EQ (prop, Qright_margin))
18438 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18439 if (EQ (prop, Qscroll_bar))
18440 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18443 prop = Fbuffer_local_value (prop, it->w->buffer);
18446 if (INTEGERP (prop) || FLOATP (prop))
18448 int base_unit = (width_p
18449 ? FRAME_COLUMN_WIDTH (it->f)
18450 : FRAME_LINE_HEIGHT (it->f));
18451 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18454 if (CONSP (prop))
18456 Lisp_Object car = XCAR (prop);
18457 Lisp_Object cdr = XCDR (prop);
18459 if (SYMBOLP (car))
18461 #ifdef HAVE_WINDOW_SYSTEM
18462 if (valid_image_p (prop))
18464 int id = lookup_image (it->f, prop);
18465 struct image *img = IMAGE_FROM_ID (it->f, id);
18467 return OK_PIXELS (width_p ? img->width : img->height);
18469 #endif
18470 if (EQ (car, Qplus) || EQ (car, Qminus))
18472 int first = 1;
18473 double px;
18475 pixels = 0;
18476 while (CONSP (cdr))
18478 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18479 font, width_p, align_to))
18480 return 0;
18481 if (first)
18482 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18483 else
18484 pixels += px;
18485 cdr = XCDR (cdr);
18487 if (EQ (car, Qminus))
18488 pixels = -pixels;
18489 return OK_PIXELS (pixels);
18492 car = Fbuffer_local_value (car, it->w->buffer);
18495 if (INTEGERP (car) || FLOATP (car))
18497 double fact;
18498 pixels = XFLOATINT (car);
18499 if (NILP (cdr))
18500 return OK_PIXELS (pixels);
18501 if (calc_pixel_width_or_height (&fact, it, cdr,
18502 font, width_p, align_to))
18503 return OK_PIXELS (pixels * fact);
18504 return 0;
18507 return 0;
18510 return 0;
18514 /***********************************************************************
18515 Glyph Display
18516 ***********************************************************************/
18518 #ifdef HAVE_WINDOW_SYSTEM
18520 #if GLYPH_DEBUG
18522 void
18523 dump_glyph_string (s)
18524 struct glyph_string *s;
18526 fprintf (stderr, "glyph string\n");
18527 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18528 s->x, s->y, s->width, s->height);
18529 fprintf (stderr, " ybase = %d\n", s->ybase);
18530 fprintf (stderr, " hl = %d\n", s->hl);
18531 fprintf (stderr, " left overhang = %d, right = %d\n",
18532 s->left_overhang, s->right_overhang);
18533 fprintf (stderr, " nchars = %d\n", s->nchars);
18534 fprintf (stderr, " extends to end of line = %d\n",
18535 s->extends_to_end_of_line_p);
18536 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18537 fprintf (stderr, " bg width = %d\n", s->background_width);
18540 #endif /* GLYPH_DEBUG */
18542 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18543 of XChar2b structures for S; it can't be allocated in
18544 init_glyph_string because it must be allocated via `alloca'. W
18545 is the window on which S is drawn. ROW and AREA are the glyph row
18546 and area within the row from which S is constructed. START is the
18547 index of the first glyph structure covered by S. HL is a
18548 face-override for drawing S. */
18550 #ifdef HAVE_NTGUI
18551 #define OPTIONAL_HDC(hdc) hdc,
18552 #define DECLARE_HDC(hdc) HDC hdc;
18553 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18554 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18555 #endif
18557 #ifndef OPTIONAL_HDC
18558 #define OPTIONAL_HDC(hdc)
18559 #define DECLARE_HDC(hdc)
18560 #define ALLOCATE_HDC(hdc, f)
18561 #define RELEASE_HDC(hdc, f)
18562 #endif
18564 static void
18565 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18566 struct glyph_string *s;
18567 DECLARE_HDC (hdc)
18568 XChar2b *char2b;
18569 struct window *w;
18570 struct glyph_row *row;
18571 enum glyph_row_area area;
18572 int start;
18573 enum draw_glyphs_face hl;
18575 bzero (s, sizeof *s);
18576 s->w = w;
18577 s->f = XFRAME (w->frame);
18578 #ifdef HAVE_NTGUI
18579 s->hdc = hdc;
18580 #endif
18581 s->display = FRAME_X_DISPLAY (s->f);
18582 s->window = FRAME_X_WINDOW (s->f);
18583 s->char2b = char2b;
18584 s->hl = hl;
18585 s->row = row;
18586 s->area = area;
18587 s->first_glyph = row->glyphs[area] + start;
18588 s->height = row->height;
18589 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18591 /* Display the internal border below the tool-bar window. */
18592 if (WINDOWP (s->f->tool_bar_window)
18593 && s->w == XWINDOW (s->f->tool_bar_window))
18594 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18596 s->ybase = s->y + row->ascent;
18600 /* Append the list of glyph strings with head H and tail T to the list
18601 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18603 static INLINE void
18604 append_glyph_string_lists (head, tail, h, t)
18605 struct glyph_string **head, **tail;
18606 struct glyph_string *h, *t;
18608 if (h)
18610 if (*head)
18611 (*tail)->next = h;
18612 else
18613 *head = h;
18614 h->prev = *tail;
18615 *tail = t;
18620 /* Prepend the list of glyph strings with head H and tail T to the
18621 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18622 result. */
18624 static INLINE void
18625 prepend_glyph_string_lists (head, tail, h, t)
18626 struct glyph_string **head, **tail;
18627 struct glyph_string *h, *t;
18629 if (h)
18631 if (*head)
18632 (*head)->prev = t;
18633 else
18634 *tail = t;
18635 t->next = *head;
18636 *head = h;
18641 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18642 Set *HEAD and *TAIL to the resulting list. */
18644 static INLINE void
18645 append_glyph_string (head, tail, s)
18646 struct glyph_string **head, **tail;
18647 struct glyph_string *s;
18649 s->next = s->prev = NULL;
18650 append_glyph_string_lists (head, tail, s, s);
18654 /* Get face and two-byte form of character glyph GLYPH on frame F.
18655 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18656 a pointer to a realized face that is ready for display. */
18658 static INLINE struct face *
18659 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18660 struct frame *f;
18661 struct glyph *glyph;
18662 XChar2b *char2b;
18663 int *two_byte_p;
18665 struct face *face;
18667 xassert (glyph->type == CHAR_GLYPH);
18668 face = FACE_FROM_ID (f, glyph->face_id);
18670 if (two_byte_p)
18671 *two_byte_p = 0;
18673 if (!glyph->multibyte_p)
18675 /* Unibyte case. We don't have to encode, but we have to make
18676 sure to use a face suitable for unibyte. */
18677 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18679 else if (glyph->u.ch < 128)
18681 /* Case of ASCII in a face known to fit ASCII. */
18682 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18684 else
18686 int c1, c2, charset;
18688 /* Split characters into bytes. If c2 is -1 afterwards, C is
18689 really a one-byte character so that byte1 is zero. */
18690 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18691 if (c2 > 0)
18692 STORE_XCHAR2B (char2b, c1, c2);
18693 else
18694 STORE_XCHAR2B (char2b, 0, c1);
18696 /* Maybe encode the character in *CHAR2B. */
18697 if (charset != CHARSET_ASCII)
18699 struct font_info *font_info
18700 = FONT_INFO_FROM_ID (f, face->font_info_id);
18701 if (font_info)
18702 glyph->font_type
18703 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18707 /* Make sure X resources of the face are allocated. */
18708 xassert (face != NULL);
18709 PREPARE_FACE_FOR_DISPLAY (f, face);
18710 return face;
18714 /* Fill glyph string S with composition components specified by S->cmp.
18716 FACES is an array of faces for all components of this composition.
18717 S->gidx is the index of the first component for S.
18719 OVERLAPS non-zero means S should draw the foreground only, and use
18720 its physical height for clipping. See also draw_glyphs.
18722 Value is the index of a component not in S. */
18724 static int
18725 fill_composite_glyph_string (s, faces, overlaps)
18726 struct glyph_string *s;
18727 struct face **faces;
18728 int overlaps;
18730 int i;
18732 xassert (s);
18734 s->for_overlaps = overlaps;
18736 s->face = faces[s->gidx];
18737 s->font = s->face->font;
18738 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18740 /* For all glyphs of this composition, starting at the offset
18741 S->gidx, until we reach the end of the definition or encounter a
18742 glyph that requires the different face, add it to S. */
18743 ++s->nchars;
18744 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18745 ++s->nchars;
18747 /* All glyph strings for the same composition has the same width,
18748 i.e. the width set for the first component of the composition. */
18750 s->width = s->first_glyph->pixel_width;
18752 /* If the specified font could not be loaded, use the frame's
18753 default font, but record the fact that we couldn't load it in
18754 the glyph string so that we can draw rectangles for the
18755 characters of the glyph string. */
18756 if (s->font == NULL)
18758 s->font_not_found_p = 1;
18759 s->font = FRAME_FONT (s->f);
18762 /* Adjust base line for subscript/superscript text. */
18763 s->ybase += s->first_glyph->voffset;
18765 xassert (s->face && s->face->gc);
18767 /* This glyph string must always be drawn with 16-bit functions. */
18768 s->two_byte_p = 1;
18770 return s->gidx + s->nchars;
18774 /* Fill glyph string S from a sequence of character glyphs.
18776 FACE_ID is the face id of the string. START is the index of the
18777 first glyph to consider, END is the index of the last + 1.
18778 OVERLAPS non-zero means S should draw the foreground only, and use
18779 its physical height for clipping. See also draw_glyphs.
18781 Value is the index of the first glyph not in S. */
18783 static int
18784 fill_glyph_string (s, face_id, start, end, overlaps)
18785 struct glyph_string *s;
18786 int face_id;
18787 int start, end, overlaps;
18789 struct glyph *glyph, *last;
18790 int voffset;
18791 int glyph_not_available_p;
18793 xassert (s->f == XFRAME (s->w->frame));
18794 xassert (s->nchars == 0);
18795 xassert (start >= 0 && end > start);
18797 s->for_overlaps = overlaps,
18798 glyph = s->row->glyphs[s->area] + start;
18799 last = s->row->glyphs[s->area] + end;
18800 voffset = glyph->voffset;
18802 glyph_not_available_p = glyph->glyph_not_available_p;
18804 while (glyph < last
18805 && glyph->type == CHAR_GLYPH
18806 && glyph->voffset == voffset
18807 /* Same face id implies same font, nowadays. */
18808 && glyph->face_id == face_id
18809 && glyph->glyph_not_available_p == glyph_not_available_p)
18811 int two_byte_p;
18813 s->face = get_glyph_face_and_encoding (s->f, glyph,
18814 s->char2b + s->nchars,
18815 &two_byte_p);
18816 s->two_byte_p = two_byte_p;
18817 ++s->nchars;
18818 xassert (s->nchars <= end - start);
18819 s->width += glyph->pixel_width;
18820 ++glyph;
18823 s->font = s->face->font;
18824 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18826 /* If the specified font could not be loaded, use the frame's font,
18827 but record the fact that we couldn't load it in
18828 S->font_not_found_p so that we can draw rectangles for the
18829 characters of the glyph string. */
18830 if (s->font == NULL || glyph_not_available_p)
18832 s->font_not_found_p = 1;
18833 s->font = FRAME_FONT (s->f);
18836 /* Adjust base line for subscript/superscript text. */
18837 s->ybase += voffset;
18839 xassert (s->face && s->face->gc);
18840 return glyph - s->row->glyphs[s->area];
18844 /* Fill glyph string S from image glyph S->first_glyph. */
18846 static void
18847 fill_image_glyph_string (s)
18848 struct glyph_string *s;
18850 xassert (s->first_glyph->type == IMAGE_GLYPH);
18851 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18852 xassert (s->img);
18853 s->slice = s->first_glyph->slice;
18854 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18855 s->font = s->face->font;
18856 s->width = s->first_glyph->pixel_width;
18858 /* Adjust base line for subscript/superscript text. */
18859 s->ybase += s->first_glyph->voffset;
18863 /* Fill glyph string S from a sequence of stretch glyphs.
18865 ROW is the glyph row in which the glyphs are found, AREA is the
18866 area within the row. START is the index of the first glyph to
18867 consider, END is the index of the last + 1.
18869 Value is the index of the first glyph not in S. */
18871 static int
18872 fill_stretch_glyph_string (s, row, area, start, end)
18873 struct glyph_string *s;
18874 struct glyph_row *row;
18875 enum glyph_row_area area;
18876 int start, end;
18878 struct glyph *glyph, *last;
18879 int voffset, face_id;
18881 xassert (s->first_glyph->type == STRETCH_GLYPH);
18883 glyph = s->row->glyphs[s->area] + start;
18884 last = s->row->glyphs[s->area] + end;
18885 face_id = glyph->face_id;
18886 s->face = FACE_FROM_ID (s->f, face_id);
18887 s->font = s->face->font;
18888 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18889 s->width = glyph->pixel_width;
18890 s->nchars = 1;
18891 voffset = glyph->voffset;
18893 for (++glyph;
18894 (glyph < last
18895 && glyph->type == STRETCH_GLYPH
18896 && glyph->voffset == voffset
18897 && glyph->face_id == face_id);
18898 ++glyph)
18899 s->width += glyph->pixel_width;
18901 /* Adjust base line for subscript/superscript text. */
18902 s->ybase += voffset;
18904 /* The case that face->gc == 0 is handled when drawing the glyph
18905 string by calling PREPARE_FACE_FOR_DISPLAY. */
18906 xassert (s->face);
18907 return glyph - s->row->glyphs[s->area];
18911 /* EXPORT for RIF:
18912 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18913 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18914 assumed to be zero. */
18916 void
18917 x_get_glyph_overhangs (glyph, f, left, right)
18918 struct glyph *glyph;
18919 struct frame *f;
18920 int *left, *right;
18922 *left = *right = 0;
18924 if (glyph->type == CHAR_GLYPH)
18926 XFontStruct *font;
18927 struct face *face;
18928 struct font_info *font_info;
18929 XChar2b char2b;
18930 XCharStruct *pcm;
18932 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18933 font = face->font;
18934 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18935 if (font /* ++KFS: Should this be font_info ? */
18936 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18938 if (pcm->rbearing > pcm->width)
18939 *right = pcm->rbearing - pcm->width;
18940 if (pcm->lbearing < 0)
18941 *left = -pcm->lbearing;
18947 /* Return the index of the first glyph preceding glyph string S that
18948 is overwritten by S because of S's left overhang. Value is -1
18949 if no glyphs are overwritten. */
18951 static int
18952 left_overwritten (s)
18953 struct glyph_string *s;
18955 int k;
18957 if (s->left_overhang)
18959 int x = 0, i;
18960 struct glyph *glyphs = s->row->glyphs[s->area];
18961 int first = s->first_glyph - glyphs;
18963 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18964 x -= glyphs[i].pixel_width;
18966 k = i + 1;
18968 else
18969 k = -1;
18971 return k;
18975 /* Return the index of the first glyph preceding glyph string S that
18976 is overwriting S because of its right overhang. Value is -1 if no
18977 glyph in front of S overwrites S. */
18979 static int
18980 left_overwriting (s)
18981 struct glyph_string *s;
18983 int i, k, x;
18984 struct glyph *glyphs = s->row->glyphs[s->area];
18985 int first = s->first_glyph - glyphs;
18987 k = -1;
18988 x = 0;
18989 for (i = first - 1; i >= 0; --i)
18991 int left, right;
18992 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18993 if (x + right > 0)
18994 k = i;
18995 x -= glyphs[i].pixel_width;
18998 return k;
19002 /* Return the index of the last glyph following glyph string S that is
19003 not overwritten by S because of S's right overhang. Value is -1 if
19004 no such glyph is found. */
19006 static int
19007 right_overwritten (s)
19008 struct glyph_string *s;
19010 int k = -1;
19012 if (s->right_overhang)
19014 int x = 0, i;
19015 struct glyph *glyphs = s->row->glyphs[s->area];
19016 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19017 int end = s->row->used[s->area];
19019 for (i = first; i < end && s->right_overhang > x; ++i)
19020 x += glyphs[i].pixel_width;
19022 k = i;
19025 return k;
19029 /* Return the index of the last glyph following glyph string S that
19030 overwrites S because of its left overhang. Value is negative
19031 if no such glyph is found. */
19033 static int
19034 right_overwriting (s)
19035 struct glyph_string *s;
19037 int i, k, x;
19038 int end = s->row->used[s->area];
19039 struct glyph *glyphs = s->row->glyphs[s->area];
19040 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19042 k = -1;
19043 x = 0;
19044 for (i = first; i < end; ++i)
19046 int left, right;
19047 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19048 if (x - left < 0)
19049 k = i;
19050 x += glyphs[i].pixel_width;
19053 return k;
19057 /* Get face and two-byte form of character C in face FACE_ID on frame
19058 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19059 means we want to display multibyte text. DISPLAY_P non-zero means
19060 make sure that X resources for the face returned are allocated.
19061 Value is a pointer to a realized face that is ready for display if
19062 DISPLAY_P is non-zero. */
19064 static INLINE struct face *
19065 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19066 struct frame *f;
19067 int c, face_id;
19068 XChar2b *char2b;
19069 int multibyte_p, display_p;
19071 struct face *face = FACE_FROM_ID (f, face_id);
19073 if (!multibyte_p)
19075 /* Unibyte case. We don't have to encode, but we have to make
19076 sure to use a face suitable for unibyte. */
19077 STORE_XCHAR2B (char2b, 0, c);
19078 face_id = FACE_FOR_CHAR (f, face, c);
19079 face = FACE_FROM_ID (f, face_id);
19081 else if (c < 128)
19083 /* Case of ASCII in a face known to fit ASCII. */
19084 STORE_XCHAR2B (char2b, 0, c);
19086 else
19088 int c1, c2, charset;
19090 /* Split characters into bytes. If c2 is -1 afterwards, C is
19091 really a one-byte character so that byte1 is zero. */
19092 SPLIT_CHAR (c, charset, c1, c2);
19093 if (c2 > 0)
19094 STORE_XCHAR2B (char2b, c1, c2);
19095 else
19096 STORE_XCHAR2B (char2b, 0, c1);
19098 /* Maybe encode the character in *CHAR2B. */
19099 if (face->font != NULL)
19101 struct font_info *font_info
19102 = FONT_INFO_FROM_ID (f, face->font_info_id);
19103 if (font_info)
19104 rif->encode_char (c, char2b, font_info, 0);
19108 /* Make sure X resources of the face are allocated. */
19109 #ifdef HAVE_X_WINDOWS
19110 if (display_p)
19111 #endif
19113 xassert (face != NULL);
19114 PREPARE_FACE_FOR_DISPLAY (f, face);
19117 return face;
19121 /* Set background width of glyph string S. START is the index of the
19122 first glyph following S. LAST_X is the right-most x-position + 1
19123 in the drawing area. */
19125 static INLINE void
19126 set_glyph_string_background_width (s, start, last_x)
19127 struct glyph_string *s;
19128 int start;
19129 int last_x;
19131 /* If the face of this glyph string has to be drawn to the end of
19132 the drawing area, set S->extends_to_end_of_line_p. */
19134 if (start == s->row->used[s->area]
19135 && s->area == TEXT_AREA
19136 && ((s->row->fill_line_p
19137 && (s->hl == DRAW_NORMAL_TEXT
19138 || s->hl == DRAW_IMAGE_RAISED
19139 || s->hl == DRAW_IMAGE_SUNKEN))
19140 || s->hl == DRAW_MOUSE_FACE))
19141 s->extends_to_end_of_line_p = 1;
19143 /* If S extends its face to the end of the line, set its
19144 background_width to the distance to the right edge of the drawing
19145 area. */
19146 if (s->extends_to_end_of_line_p)
19147 s->background_width = last_x - s->x + 1;
19148 else
19149 s->background_width = s->width;
19153 /* Compute overhangs and x-positions for glyph string S and its
19154 predecessors, or successors. X is the starting x-position for S.
19155 BACKWARD_P non-zero means process predecessors. */
19157 static void
19158 compute_overhangs_and_x (s, x, backward_p)
19159 struct glyph_string *s;
19160 int x;
19161 int backward_p;
19163 if (backward_p)
19165 while (s)
19167 if (rif->compute_glyph_string_overhangs)
19168 rif->compute_glyph_string_overhangs (s);
19169 x -= s->width;
19170 s->x = x;
19171 s = s->prev;
19174 else
19176 while (s)
19178 if (rif->compute_glyph_string_overhangs)
19179 rif->compute_glyph_string_overhangs (s);
19180 s->x = x;
19181 x += s->width;
19182 s = s->next;
19189 /* The following macros are only called from draw_glyphs below.
19190 They reference the following parameters of that function directly:
19191 `w', `row', `area', and `overlap_p'
19192 as well as the following local variables:
19193 `s', `f', and `hdc' (in W32) */
19195 #ifdef HAVE_NTGUI
19196 /* On W32, silently add local `hdc' variable to argument list of
19197 init_glyph_string. */
19198 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19199 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19200 #else
19201 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19202 init_glyph_string (s, char2b, w, row, area, start, hl)
19203 #endif
19205 /* Add a glyph string for a stretch glyph to the list of strings
19206 between HEAD and TAIL. START is the index of the stretch glyph in
19207 row area AREA of glyph row ROW. END is the index of the last glyph
19208 in that glyph row area. X is the current output position assigned
19209 to the new glyph string constructed. HL overrides that face of the
19210 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19211 is the right-most x-position of the drawing area. */
19213 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19214 and below -- keep them on one line. */
19215 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19216 do \
19218 s = (struct glyph_string *) alloca (sizeof *s); \
19219 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19220 START = fill_stretch_glyph_string (s, row, area, START, END); \
19221 append_glyph_string (&HEAD, &TAIL, s); \
19222 s->x = (X); \
19224 while (0)
19227 /* Add a glyph string for an image glyph to the list of strings
19228 between HEAD and TAIL. START is the index of the image glyph in
19229 row area AREA of glyph row ROW. END is the index of the last glyph
19230 in that glyph row area. X is the current output position assigned
19231 to the new glyph string constructed. HL overrides that face of the
19232 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19233 is the right-most x-position of the drawing area. */
19235 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19236 do \
19238 s = (struct glyph_string *) alloca (sizeof *s); \
19239 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19240 fill_image_glyph_string (s); \
19241 append_glyph_string (&HEAD, &TAIL, s); \
19242 ++START; \
19243 s->x = (X); \
19245 while (0)
19248 /* Add a glyph string for a sequence of character glyphs to the list
19249 of strings between HEAD and TAIL. START is the index of the first
19250 glyph in row area AREA of glyph row ROW that is part of the new
19251 glyph string. END is the index of the last glyph in that glyph row
19252 area. X is the current output position assigned to the new glyph
19253 string constructed. HL overrides that face of the glyph; e.g. it
19254 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19255 right-most x-position of the drawing area. */
19257 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19258 do \
19260 int c, face_id; \
19261 XChar2b *char2b; \
19263 c = (row)->glyphs[area][START].u.ch; \
19264 face_id = (row)->glyphs[area][START].face_id; \
19266 s = (struct glyph_string *) alloca (sizeof *s); \
19267 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19268 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19269 append_glyph_string (&HEAD, &TAIL, s); \
19270 s->x = (X); \
19271 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19273 while (0)
19276 /* Add a glyph string for a composite sequence to the list of strings
19277 between HEAD and TAIL. START is the index of the first glyph in
19278 row area AREA of glyph row ROW that is part of the new glyph
19279 string. END is the index of the last glyph in that glyph row area.
19280 X is the current output position assigned to the new glyph string
19281 constructed. HL overrides that face of the glyph; e.g. it is
19282 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19283 x-position of the drawing area. */
19285 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19286 do { \
19287 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19288 int face_id = (row)->glyphs[area][START].face_id; \
19289 struct face *base_face = FACE_FROM_ID (f, face_id); \
19290 struct composition *cmp = composition_table[cmp_id]; \
19291 int glyph_len = cmp->glyph_len; \
19292 XChar2b *char2b; \
19293 struct face **faces; \
19294 struct glyph_string *first_s = NULL; \
19295 int n; \
19297 base_face = base_face->ascii_face; \
19298 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19299 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19300 /* At first, fill in `char2b' and `faces'. */ \
19301 for (n = 0; n < glyph_len; n++) \
19303 int c = COMPOSITION_GLYPH (cmp, n); \
19304 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19305 faces[n] = FACE_FROM_ID (f, this_face_id); \
19306 get_char_face_and_encoding (f, c, this_face_id, \
19307 char2b + n, 1, 1); \
19310 /* Make glyph_strings for each glyph sequence that is drawable by \
19311 the same face, and append them to HEAD/TAIL. */ \
19312 for (n = 0; n < cmp->glyph_len;) \
19314 s = (struct glyph_string *) alloca (sizeof *s); \
19315 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19316 append_glyph_string (&(HEAD), &(TAIL), s); \
19317 s->cmp = cmp; \
19318 s->gidx = n; \
19319 s->x = (X); \
19321 if (n == 0) \
19322 first_s = s; \
19324 n = fill_composite_glyph_string (s, faces, overlaps); \
19327 ++START; \
19328 s = first_s; \
19329 } while (0)
19332 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19333 of AREA of glyph row ROW on window W between indices START and END.
19334 HL overrides the face for drawing glyph strings, e.g. it is
19335 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19336 x-positions of the drawing area.
19338 This is an ugly monster macro construct because we must use alloca
19339 to allocate glyph strings (because draw_glyphs can be called
19340 asynchronously). */
19342 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19343 do \
19345 HEAD = TAIL = NULL; \
19346 while (START < END) \
19348 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19349 switch (first_glyph->type) \
19351 case CHAR_GLYPH: \
19352 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19353 HL, X, LAST_X); \
19354 break; \
19356 case COMPOSITE_GLYPH: \
19357 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19358 HL, X, LAST_X); \
19359 break; \
19361 case STRETCH_GLYPH: \
19362 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19363 HL, X, LAST_X); \
19364 break; \
19366 case IMAGE_GLYPH: \
19367 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19368 HL, X, LAST_X); \
19369 break; \
19371 default: \
19372 abort (); \
19375 set_glyph_string_background_width (s, START, LAST_X); \
19376 (X) += s->width; \
19379 while (0)
19382 /* Draw glyphs between START and END in AREA of ROW on window W,
19383 starting at x-position X. X is relative to AREA in W. HL is a
19384 face-override with the following meaning:
19386 DRAW_NORMAL_TEXT draw normally
19387 DRAW_CURSOR draw in cursor face
19388 DRAW_MOUSE_FACE draw in mouse face.
19389 DRAW_INVERSE_VIDEO draw in mode line face
19390 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19391 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19393 If OVERLAPS is non-zero, draw only the foreground of characters and
19394 clip to the physical height of ROW. Non-zero value also defines
19395 the overlapping part to be drawn:
19397 OVERLAPS_PRED overlap with preceding rows
19398 OVERLAPS_SUCC overlap with succeeding rows
19399 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19400 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19402 Value is the x-position reached, relative to AREA of W. */
19404 static int
19405 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19406 struct window *w;
19407 int x;
19408 struct glyph_row *row;
19409 enum glyph_row_area area;
19410 int start, end;
19411 enum draw_glyphs_face hl;
19412 int overlaps;
19414 struct glyph_string *head, *tail;
19415 struct glyph_string *s;
19416 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19417 int last_x, area_width;
19418 int x_reached;
19419 int i, j;
19420 struct frame *f = XFRAME (WINDOW_FRAME (w));
19421 DECLARE_HDC (hdc);
19423 ALLOCATE_HDC (hdc, f);
19425 /* Let's rather be paranoid than getting a SEGV. */
19426 end = min (end, row->used[area]);
19427 start = max (0, start);
19428 start = min (end, start);
19430 /* Translate X to frame coordinates. Set last_x to the right
19431 end of the drawing area. */
19432 if (row->full_width_p)
19434 /* X is relative to the left edge of W, without scroll bars
19435 or fringes. */
19436 x += WINDOW_LEFT_EDGE_X (w);
19437 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19439 else
19441 int area_left = window_box_left (w, area);
19442 x += area_left;
19443 area_width = window_box_width (w, area);
19444 last_x = area_left + area_width;
19447 /* Build a doubly-linked list of glyph_string structures between
19448 head and tail from what we have to draw. Note that the macro
19449 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19450 the reason we use a separate variable `i'. */
19451 i = start;
19452 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19453 if (tail)
19454 x_reached = tail->x + tail->background_width;
19455 else
19456 x_reached = x;
19458 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19459 the row, redraw some glyphs in front or following the glyph
19460 strings built above. */
19461 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19463 int dummy_x = 0;
19464 struct glyph_string *h, *t;
19466 /* Compute overhangs for all glyph strings. */
19467 if (rif->compute_glyph_string_overhangs)
19468 for (s = head; s; s = s->next)
19469 rif->compute_glyph_string_overhangs (s);
19471 /* Prepend glyph strings for glyphs in front of the first glyph
19472 string that are overwritten because of the first glyph
19473 string's left overhang. The background of all strings
19474 prepended must be drawn because the first glyph string
19475 draws over it. */
19476 i = left_overwritten (head);
19477 if (i >= 0)
19479 j = i;
19480 BUILD_GLYPH_STRINGS (j, start, h, t,
19481 DRAW_NORMAL_TEXT, dummy_x, last_x);
19482 start = i;
19483 compute_overhangs_and_x (t, head->x, 1);
19484 prepend_glyph_string_lists (&head, &tail, h, t);
19485 clip_head = head;
19488 /* Prepend glyph strings for glyphs in front of the first glyph
19489 string that overwrite that glyph string because of their
19490 right overhang. For these strings, only the foreground must
19491 be drawn, because it draws over the glyph string at `head'.
19492 The background must not be drawn because this would overwrite
19493 right overhangs of preceding glyphs for which no glyph
19494 strings exist. */
19495 i = left_overwriting (head);
19496 if (i >= 0)
19498 clip_head = head;
19499 BUILD_GLYPH_STRINGS (i, start, h, t,
19500 DRAW_NORMAL_TEXT, dummy_x, last_x);
19501 for (s = h; s; s = s->next)
19502 s->background_filled_p = 1;
19503 compute_overhangs_and_x (t, head->x, 1);
19504 prepend_glyph_string_lists (&head, &tail, h, t);
19507 /* Append glyphs strings for glyphs following the last glyph
19508 string tail that are overwritten by tail. The background of
19509 these strings has to be drawn because tail's foreground draws
19510 over it. */
19511 i = right_overwritten (tail);
19512 if (i >= 0)
19514 BUILD_GLYPH_STRINGS (end, i, h, t,
19515 DRAW_NORMAL_TEXT, x, last_x);
19516 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19517 append_glyph_string_lists (&head, &tail, h, t);
19518 clip_tail = tail;
19521 /* Append glyph strings for glyphs following the last glyph
19522 string tail that overwrite tail. The foreground of such
19523 glyphs has to be drawn because it writes into the background
19524 of tail. The background must not be drawn because it could
19525 paint over the foreground of following glyphs. */
19526 i = right_overwriting (tail);
19527 if (i >= 0)
19529 clip_tail = tail;
19530 BUILD_GLYPH_STRINGS (end, i, h, t,
19531 DRAW_NORMAL_TEXT, x, last_x);
19532 for (s = h; s; s = s->next)
19533 s->background_filled_p = 1;
19534 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19535 append_glyph_string_lists (&head, &tail, h, t);
19537 if (clip_head || clip_tail)
19538 for (s = head; s; s = s->next)
19540 s->clip_head = clip_head;
19541 s->clip_tail = clip_tail;
19545 /* Draw all strings. */
19546 for (s = head; s; s = s->next)
19547 rif->draw_glyph_string (s);
19549 if (area == TEXT_AREA
19550 && !row->full_width_p
19551 /* When drawing overlapping rows, only the glyph strings'
19552 foreground is drawn, which doesn't erase a cursor
19553 completely. */
19554 && !overlaps)
19556 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19557 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19558 : (tail ? tail->x + tail->background_width : x));
19560 int text_left = window_box_left (w, TEXT_AREA);
19561 x0 -= text_left;
19562 x1 -= text_left;
19564 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19565 row->y, MATRIX_ROW_BOTTOM_Y (row));
19568 /* Value is the x-position up to which drawn, relative to AREA of W.
19569 This doesn't include parts drawn because of overhangs. */
19570 if (row->full_width_p)
19571 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19572 else
19573 x_reached -= window_box_left (w, area);
19575 RELEASE_HDC (hdc, f);
19577 return x_reached;
19580 /* Expand row matrix if too narrow. Don't expand if area
19581 is not present. */
19583 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19585 if (!fonts_changed_p \
19586 && (it->glyph_row->glyphs[area] \
19587 < it->glyph_row->glyphs[area + 1])) \
19589 it->w->ncols_scale_factor++; \
19590 fonts_changed_p = 1; \
19594 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19595 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19597 static INLINE void
19598 append_glyph (it)
19599 struct it *it;
19601 struct glyph *glyph;
19602 enum glyph_row_area area = it->area;
19604 xassert (it->glyph_row);
19605 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19607 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19608 if (glyph < it->glyph_row->glyphs[area + 1])
19610 glyph->charpos = CHARPOS (it->position);
19611 glyph->object = it->object;
19612 glyph->pixel_width = it->pixel_width;
19613 glyph->ascent = it->ascent;
19614 glyph->descent = it->descent;
19615 glyph->voffset = it->voffset;
19616 glyph->type = CHAR_GLYPH;
19617 glyph->multibyte_p = it->multibyte_p;
19618 glyph->left_box_line_p = it->start_of_box_run_p;
19619 glyph->right_box_line_p = it->end_of_box_run_p;
19620 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19621 || it->phys_descent > it->descent);
19622 glyph->padding_p = 0;
19623 glyph->glyph_not_available_p = it->glyph_not_available_p;
19624 glyph->face_id = it->face_id;
19625 glyph->u.ch = it->char_to_display;
19626 glyph->slice = null_glyph_slice;
19627 glyph->font_type = FONT_TYPE_UNKNOWN;
19628 ++it->glyph_row->used[area];
19630 else
19631 IT_EXPAND_MATRIX_WIDTH (it, area);
19634 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19635 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19637 static INLINE void
19638 append_composite_glyph (it)
19639 struct it *it;
19641 struct glyph *glyph;
19642 enum glyph_row_area area = it->area;
19644 xassert (it->glyph_row);
19646 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19647 if (glyph < it->glyph_row->glyphs[area + 1])
19649 glyph->charpos = CHARPOS (it->position);
19650 glyph->object = it->object;
19651 glyph->pixel_width = it->pixel_width;
19652 glyph->ascent = it->ascent;
19653 glyph->descent = it->descent;
19654 glyph->voffset = it->voffset;
19655 glyph->type = COMPOSITE_GLYPH;
19656 glyph->multibyte_p = it->multibyte_p;
19657 glyph->left_box_line_p = it->start_of_box_run_p;
19658 glyph->right_box_line_p = it->end_of_box_run_p;
19659 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19660 || it->phys_descent > it->descent);
19661 glyph->padding_p = 0;
19662 glyph->glyph_not_available_p = 0;
19663 glyph->face_id = it->face_id;
19664 glyph->u.cmp_id = it->cmp_id;
19665 glyph->slice = null_glyph_slice;
19666 glyph->font_type = FONT_TYPE_UNKNOWN;
19667 ++it->glyph_row->used[area];
19669 else
19670 IT_EXPAND_MATRIX_WIDTH (it, area);
19674 /* Change IT->ascent and IT->height according to the setting of
19675 IT->voffset. */
19677 static INLINE void
19678 take_vertical_position_into_account (it)
19679 struct it *it;
19681 if (it->voffset)
19683 if (it->voffset < 0)
19684 /* Increase the ascent so that we can display the text higher
19685 in the line. */
19686 it->ascent -= it->voffset;
19687 else
19688 /* Increase the descent so that we can display the text lower
19689 in the line. */
19690 it->descent += it->voffset;
19695 /* Produce glyphs/get display metrics for the image IT is loaded with.
19696 See the description of struct display_iterator in dispextern.h for
19697 an overview of struct display_iterator. */
19699 static void
19700 produce_image_glyph (it)
19701 struct it *it;
19703 struct image *img;
19704 struct face *face;
19705 int glyph_ascent;
19706 struct glyph_slice slice;
19708 xassert (it->what == IT_IMAGE);
19710 face = FACE_FROM_ID (it->f, it->face_id);
19711 xassert (face);
19712 /* Make sure X resources of the face is loaded. */
19713 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19715 if (it->image_id < 0)
19717 /* Fringe bitmap. */
19718 it->ascent = it->phys_ascent = 0;
19719 it->descent = it->phys_descent = 0;
19720 it->pixel_width = 0;
19721 it->nglyphs = 0;
19722 return;
19725 img = IMAGE_FROM_ID (it->f, it->image_id);
19726 xassert (img);
19727 /* Make sure X resources of the image is loaded. */
19728 prepare_image_for_display (it->f, img);
19730 slice.x = slice.y = 0;
19731 slice.width = img->width;
19732 slice.height = img->height;
19734 if (INTEGERP (it->slice.x))
19735 slice.x = XINT (it->slice.x);
19736 else if (FLOATP (it->slice.x))
19737 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19739 if (INTEGERP (it->slice.y))
19740 slice.y = XINT (it->slice.y);
19741 else if (FLOATP (it->slice.y))
19742 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19744 if (INTEGERP (it->slice.width))
19745 slice.width = XINT (it->slice.width);
19746 else if (FLOATP (it->slice.width))
19747 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19749 if (INTEGERP (it->slice.height))
19750 slice.height = XINT (it->slice.height);
19751 else if (FLOATP (it->slice.height))
19752 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19754 if (slice.x >= img->width)
19755 slice.x = img->width;
19756 if (slice.y >= img->height)
19757 slice.y = img->height;
19758 if (slice.x + slice.width >= img->width)
19759 slice.width = img->width - slice.x;
19760 if (slice.y + slice.height > img->height)
19761 slice.height = img->height - slice.y;
19763 if (slice.width == 0 || slice.height == 0)
19764 return;
19766 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19768 it->descent = slice.height - glyph_ascent;
19769 if (slice.y == 0)
19770 it->descent += img->vmargin;
19771 if (slice.y + slice.height == img->height)
19772 it->descent += img->vmargin;
19773 it->phys_descent = it->descent;
19775 it->pixel_width = slice.width;
19776 if (slice.x == 0)
19777 it->pixel_width += img->hmargin;
19778 if (slice.x + slice.width == img->width)
19779 it->pixel_width += img->hmargin;
19781 /* It's quite possible for images to have an ascent greater than
19782 their height, so don't get confused in that case. */
19783 if (it->descent < 0)
19784 it->descent = 0;
19786 #if 0 /* this breaks image tiling */
19787 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19788 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19789 if (face_ascent > it->ascent)
19790 it->ascent = it->phys_ascent = face_ascent;
19791 #endif
19793 it->nglyphs = 1;
19795 if (face->box != FACE_NO_BOX)
19797 if (face->box_line_width > 0)
19799 if (slice.y == 0)
19800 it->ascent += face->box_line_width;
19801 if (slice.y + slice.height == img->height)
19802 it->descent += face->box_line_width;
19805 if (it->start_of_box_run_p && slice.x == 0)
19806 it->pixel_width += abs (face->box_line_width);
19807 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19808 it->pixel_width += abs (face->box_line_width);
19811 take_vertical_position_into_account (it);
19813 if (it->glyph_row)
19815 struct glyph *glyph;
19816 enum glyph_row_area area = it->area;
19818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19819 if (glyph < it->glyph_row->glyphs[area + 1])
19821 glyph->charpos = CHARPOS (it->position);
19822 glyph->object = it->object;
19823 glyph->pixel_width = it->pixel_width;
19824 glyph->ascent = glyph_ascent;
19825 glyph->descent = it->descent;
19826 glyph->voffset = it->voffset;
19827 glyph->type = IMAGE_GLYPH;
19828 glyph->multibyte_p = it->multibyte_p;
19829 glyph->left_box_line_p = it->start_of_box_run_p;
19830 glyph->right_box_line_p = it->end_of_box_run_p;
19831 glyph->overlaps_vertically_p = 0;
19832 glyph->padding_p = 0;
19833 glyph->glyph_not_available_p = 0;
19834 glyph->face_id = it->face_id;
19835 glyph->u.img_id = img->id;
19836 glyph->slice = slice;
19837 glyph->font_type = FONT_TYPE_UNKNOWN;
19838 ++it->glyph_row->used[area];
19840 else
19841 IT_EXPAND_MATRIX_WIDTH (it, area);
19846 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19847 of the glyph, WIDTH and HEIGHT are the width and height of the
19848 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19850 static void
19851 append_stretch_glyph (it, object, width, height, ascent)
19852 struct it *it;
19853 Lisp_Object object;
19854 int width, height;
19855 int ascent;
19857 struct glyph *glyph;
19858 enum glyph_row_area area = it->area;
19860 xassert (ascent >= 0 && ascent <= height);
19862 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19863 if (glyph < it->glyph_row->glyphs[area + 1])
19865 glyph->charpos = CHARPOS (it->position);
19866 glyph->object = object;
19867 glyph->pixel_width = width;
19868 glyph->ascent = ascent;
19869 glyph->descent = height - ascent;
19870 glyph->voffset = it->voffset;
19871 glyph->type = STRETCH_GLYPH;
19872 glyph->multibyte_p = it->multibyte_p;
19873 glyph->left_box_line_p = it->start_of_box_run_p;
19874 glyph->right_box_line_p = it->end_of_box_run_p;
19875 glyph->overlaps_vertically_p = 0;
19876 glyph->padding_p = 0;
19877 glyph->glyph_not_available_p = 0;
19878 glyph->face_id = it->face_id;
19879 glyph->u.stretch.ascent = ascent;
19880 glyph->u.stretch.height = height;
19881 glyph->slice = null_glyph_slice;
19882 glyph->font_type = FONT_TYPE_UNKNOWN;
19883 ++it->glyph_row->used[area];
19885 else
19886 IT_EXPAND_MATRIX_WIDTH (it, area);
19890 /* Produce a stretch glyph for iterator IT. IT->object is the value
19891 of the glyph property displayed. The value must be a list
19892 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19893 being recognized:
19895 1. `:width WIDTH' specifies that the space should be WIDTH *
19896 canonical char width wide. WIDTH may be an integer or floating
19897 point number.
19899 2. `:relative-width FACTOR' specifies that the width of the stretch
19900 should be computed from the width of the first character having the
19901 `glyph' property, and should be FACTOR times that width.
19903 3. `:align-to HPOS' specifies that the space should be wide enough
19904 to reach HPOS, a value in canonical character units.
19906 Exactly one of the above pairs must be present.
19908 4. `:height HEIGHT' specifies that the height of the stretch produced
19909 should be HEIGHT, measured in canonical character units.
19911 5. `:relative-height FACTOR' specifies that the height of the
19912 stretch should be FACTOR times the height of the characters having
19913 the glyph property.
19915 Either none or exactly one of 4 or 5 must be present.
19917 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19918 of the stretch should be used for the ascent of the stretch.
19919 ASCENT must be in the range 0 <= ASCENT <= 100. */
19921 static void
19922 produce_stretch_glyph (it)
19923 struct it *it;
19925 /* (space :width WIDTH :height HEIGHT ...) */
19926 Lisp_Object prop, plist;
19927 int width = 0, height = 0, align_to = -1;
19928 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19929 int ascent = 0;
19930 double tem;
19931 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19932 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19934 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19936 /* List should start with `space'. */
19937 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19938 plist = XCDR (it->object);
19940 /* Compute the width of the stretch. */
19941 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19942 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19944 /* Absolute width `:width WIDTH' specified and valid. */
19945 zero_width_ok_p = 1;
19946 width = (int)tem;
19948 else if (prop = Fplist_get (plist, QCrelative_width),
19949 NUMVAL (prop) > 0)
19951 /* Relative width `:relative-width FACTOR' specified and valid.
19952 Compute the width of the characters having the `glyph'
19953 property. */
19954 struct it it2;
19955 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19957 it2 = *it;
19958 if (it->multibyte_p)
19960 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19961 - IT_BYTEPOS (*it));
19962 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19964 else
19965 it2.c = *p, it2.len = 1;
19967 it2.glyph_row = NULL;
19968 it2.what = IT_CHARACTER;
19969 x_produce_glyphs (&it2);
19970 width = NUMVAL (prop) * it2.pixel_width;
19972 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19973 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19975 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19976 align_to = (align_to < 0
19978 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19979 else if (align_to < 0)
19980 align_to = window_box_left_offset (it->w, TEXT_AREA);
19981 width = max (0, (int)tem + align_to - it->current_x);
19982 zero_width_ok_p = 1;
19984 else
19985 /* Nothing specified -> width defaults to canonical char width. */
19986 width = FRAME_COLUMN_WIDTH (it->f);
19988 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19989 width = 1;
19991 /* Compute height. */
19992 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19993 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19995 height = (int)tem;
19996 zero_height_ok_p = 1;
19998 else if (prop = Fplist_get (plist, QCrelative_height),
19999 NUMVAL (prop) > 0)
20000 height = FONT_HEIGHT (font) * NUMVAL (prop);
20001 else
20002 height = FONT_HEIGHT (font);
20004 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20005 height = 1;
20007 /* Compute percentage of height used for ascent. If
20008 `:ascent ASCENT' is present and valid, use that. Otherwise,
20009 derive the ascent from the font in use. */
20010 if (prop = Fplist_get (plist, QCascent),
20011 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20012 ascent = height * NUMVAL (prop) / 100.0;
20013 else if (!NILP (prop)
20014 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20015 ascent = min (max (0, (int)tem), height);
20016 else
20017 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20019 if (width > 0 && !it->truncate_lines_p
20020 && it->current_x + width > it->last_visible_x)
20021 width = it->last_visible_x - it->current_x - 1;
20023 if (width > 0 && height > 0 && it->glyph_row)
20025 Lisp_Object object = it->stack[it->sp - 1].string;
20026 if (!STRINGP (object))
20027 object = it->w->buffer;
20028 append_stretch_glyph (it, object, width, height, ascent);
20031 it->pixel_width = width;
20032 it->ascent = it->phys_ascent = ascent;
20033 it->descent = it->phys_descent = height - it->ascent;
20034 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20036 take_vertical_position_into_account (it);
20039 /* Get line-height and line-spacing property at point.
20040 If line-height has format (HEIGHT TOTAL), return TOTAL
20041 in TOTAL_HEIGHT. */
20043 static Lisp_Object
20044 get_line_height_property (it, prop)
20045 struct it *it;
20046 Lisp_Object prop;
20048 Lisp_Object position;
20050 if (STRINGP (it->object))
20051 position = make_number (IT_STRING_CHARPOS (*it));
20052 else if (BUFFERP (it->object))
20053 position = make_number (IT_CHARPOS (*it));
20054 else
20055 return Qnil;
20057 return Fget_char_property (position, prop, it->object);
20060 /* Calculate line-height and line-spacing properties.
20061 An integer value specifies explicit pixel value.
20062 A float value specifies relative value to current face height.
20063 A cons (float . face-name) specifies relative value to
20064 height of specified face font.
20066 Returns height in pixels, or nil. */
20069 static Lisp_Object
20070 calc_line_height_property (it, val, font, boff, override)
20071 struct it *it;
20072 Lisp_Object val;
20073 XFontStruct *font;
20074 int boff, override;
20076 Lisp_Object face_name = Qnil;
20077 int ascent, descent, height;
20079 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20080 return val;
20082 if (CONSP (val))
20084 face_name = XCAR (val);
20085 val = XCDR (val);
20086 if (!NUMBERP (val))
20087 val = make_number (1);
20088 if (NILP (face_name))
20090 height = it->ascent + it->descent;
20091 goto scale;
20095 if (NILP (face_name))
20097 font = FRAME_FONT (it->f);
20098 boff = FRAME_BASELINE_OFFSET (it->f);
20100 else if (EQ (face_name, Qt))
20102 override = 0;
20104 else
20106 int face_id;
20107 struct face *face;
20108 struct font_info *font_info;
20110 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20111 if (face_id < 0)
20112 return make_number (-1);
20114 face = FACE_FROM_ID (it->f, face_id);
20115 font = face->font;
20116 if (font == NULL)
20117 return make_number (-1);
20119 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20120 boff = font_info->baseline_offset;
20121 if (font_info->vertical_centering)
20122 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20125 ascent = FONT_BASE (font) + boff;
20126 descent = FONT_DESCENT (font) - boff;
20128 if (override)
20130 it->override_ascent = ascent;
20131 it->override_descent = descent;
20132 it->override_boff = boff;
20135 height = ascent + descent;
20137 scale:
20138 if (FLOATP (val))
20139 height = (int)(XFLOAT_DATA (val) * height);
20140 else if (INTEGERP (val))
20141 height *= XINT (val);
20143 return make_number (height);
20147 /* RIF:
20148 Produce glyphs/get display metrics for the display element IT is
20149 loaded with. See the description of struct it in dispextern.h
20150 for an overview of struct it. */
20152 void
20153 x_produce_glyphs (it)
20154 struct it *it;
20156 int extra_line_spacing = it->extra_line_spacing;
20158 it->glyph_not_available_p = 0;
20160 if (it->what == IT_CHARACTER)
20162 XChar2b char2b;
20163 XFontStruct *font;
20164 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20165 XCharStruct *pcm;
20166 int font_not_found_p;
20167 struct font_info *font_info;
20168 int boff; /* baseline offset */
20169 /* We may change it->multibyte_p upon unibyte<->multibyte
20170 conversion. So, save the current value now and restore it
20171 later.
20173 Note: It seems that we don't have to record multibyte_p in
20174 struct glyph because the character code itself tells if or
20175 not the character is multibyte. Thus, in the future, we must
20176 consider eliminating the field `multibyte_p' in the struct
20177 glyph. */
20178 int saved_multibyte_p = it->multibyte_p;
20180 /* Maybe translate single-byte characters to multibyte, or the
20181 other way. */
20182 it->char_to_display = it->c;
20183 if (!ASCII_BYTE_P (it->c))
20185 if (unibyte_display_via_language_environment
20186 && SINGLE_BYTE_CHAR_P (it->c)
20187 && (it->c >= 0240
20188 || !NILP (Vnonascii_translation_table)))
20190 it->char_to_display = unibyte_char_to_multibyte (it->c);
20191 it->multibyte_p = 1;
20192 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20193 face = FACE_FROM_ID (it->f, it->face_id);
20195 else if (!SINGLE_BYTE_CHAR_P (it->c)
20196 && !it->multibyte_p)
20198 it->multibyte_p = 1;
20199 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20200 face = FACE_FROM_ID (it->f, it->face_id);
20204 /* Get font to use. Encode IT->char_to_display. */
20205 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20206 &char2b, it->multibyte_p, 0);
20207 font = face->font;
20209 /* When no suitable font found, use the default font. */
20210 font_not_found_p = font == NULL;
20211 if (font_not_found_p)
20213 font = FRAME_FONT (it->f);
20214 boff = FRAME_BASELINE_OFFSET (it->f);
20215 font_info = NULL;
20217 else
20219 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20220 boff = font_info->baseline_offset;
20221 if (font_info->vertical_centering)
20222 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20225 if (it->char_to_display >= ' '
20226 && (!it->multibyte_p || it->char_to_display < 128))
20228 /* Either unibyte or ASCII. */
20229 int stretched_p;
20231 it->nglyphs = 1;
20233 pcm = rif->per_char_metric (font, &char2b,
20234 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20236 if (it->override_ascent >= 0)
20238 it->ascent = it->override_ascent;
20239 it->descent = it->override_descent;
20240 boff = it->override_boff;
20242 else
20244 it->ascent = FONT_BASE (font) + boff;
20245 it->descent = FONT_DESCENT (font) - boff;
20248 if (pcm)
20250 it->phys_ascent = pcm->ascent + boff;
20251 it->phys_descent = pcm->descent - boff;
20252 it->pixel_width = pcm->width;
20254 else
20256 it->glyph_not_available_p = 1;
20257 it->phys_ascent = it->ascent;
20258 it->phys_descent = it->descent;
20259 it->pixel_width = FONT_WIDTH (font);
20262 if (it->constrain_row_ascent_descent_p)
20264 if (it->descent > it->max_descent)
20266 it->ascent += it->descent - it->max_descent;
20267 it->descent = it->max_descent;
20269 if (it->ascent > it->max_ascent)
20271 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20272 it->ascent = it->max_ascent;
20274 it->phys_ascent = min (it->phys_ascent, it->ascent);
20275 it->phys_descent = min (it->phys_descent, it->descent);
20276 extra_line_spacing = 0;
20279 /* If this is a space inside a region of text with
20280 `space-width' property, change its width. */
20281 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20282 if (stretched_p)
20283 it->pixel_width *= XFLOATINT (it->space_width);
20285 /* If face has a box, add the box thickness to the character
20286 height. If character has a box line to the left and/or
20287 right, add the box line width to the character's width. */
20288 if (face->box != FACE_NO_BOX)
20290 int thick = face->box_line_width;
20292 if (thick > 0)
20294 it->ascent += thick;
20295 it->descent += thick;
20297 else
20298 thick = -thick;
20300 if (it->start_of_box_run_p)
20301 it->pixel_width += thick;
20302 if (it->end_of_box_run_p)
20303 it->pixel_width += thick;
20306 /* If face has an overline, add the height of the overline
20307 (1 pixel) and a 1 pixel margin to the character height. */
20308 if (face->overline_p)
20309 it->ascent += 2;
20311 if (it->constrain_row_ascent_descent_p)
20313 if (it->ascent > it->max_ascent)
20314 it->ascent = it->max_ascent;
20315 if (it->descent > it->max_descent)
20316 it->descent = it->max_descent;
20319 take_vertical_position_into_account (it);
20321 /* If we have to actually produce glyphs, do it. */
20322 if (it->glyph_row)
20324 if (stretched_p)
20326 /* Translate a space with a `space-width' property
20327 into a stretch glyph. */
20328 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20329 / FONT_HEIGHT (font));
20330 append_stretch_glyph (it, it->object, it->pixel_width,
20331 it->ascent + it->descent, ascent);
20333 else
20334 append_glyph (it);
20336 /* If characters with lbearing or rbearing are displayed
20337 in this line, record that fact in a flag of the
20338 glyph row. This is used to optimize X output code. */
20339 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20340 it->glyph_row->contains_overlapping_glyphs_p = 1;
20343 else if (it->char_to_display == '\n')
20345 /* A newline has no width but we need the height of the line.
20346 But if previous part of the line set a height, don't
20347 increase that height */
20349 Lisp_Object height;
20350 Lisp_Object total_height = Qnil;
20352 it->override_ascent = -1;
20353 it->pixel_width = 0;
20354 it->nglyphs = 0;
20356 height = get_line_height_property(it, Qline_height);
20357 /* Split (line-height total-height) list */
20358 if (CONSP (height)
20359 && CONSP (XCDR (height))
20360 && NILP (XCDR (XCDR (height))))
20362 total_height = XCAR (XCDR (height));
20363 height = XCAR (height);
20365 height = calc_line_height_property(it, height, font, boff, 1);
20367 if (it->override_ascent >= 0)
20369 it->ascent = it->override_ascent;
20370 it->descent = it->override_descent;
20371 boff = it->override_boff;
20373 else
20375 it->ascent = FONT_BASE (font) + boff;
20376 it->descent = FONT_DESCENT (font) - boff;
20379 if (EQ (height, Qt))
20381 if (it->descent > it->max_descent)
20383 it->ascent += it->descent - it->max_descent;
20384 it->descent = it->max_descent;
20386 if (it->ascent > it->max_ascent)
20388 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20389 it->ascent = it->max_ascent;
20391 it->phys_ascent = min (it->phys_ascent, it->ascent);
20392 it->phys_descent = min (it->phys_descent, it->descent);
20393 it->constrain_row_ascent_descent_p = 1;
20394 extra_line_spacing = 0;
20396 else
20398 Lisp_Object spacing;
20400 it->phys_ascent = it->ascent;
20401 it->phys_descent = it->descent;
20403 if ((it->max_ascent > 0 || it->max_descent > 0)
20404 && face->box != FACE_NO_BOX
20405 && face->box_line_width > 0)
20407 it->ascent += face->box_line_width;
20408 it->descent += face->box_line_width;
20410 if (!NILP (height)
20411 && XINT (height) > it->ascent + it->descent)
20412 it->ascent = XINT (height) - it->descent;
20414 if (!NILP (total_height))
20415 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20416 else
20418 spacing = get_line_height_property(it, Qline_spacing);
20419 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20421 if (INTEGERP (spacing))
20423 extra_line_spacing = XINT (spacing);
20424 if (!NILP (total_height))
20425 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20429 else if (it->char_to_display == '\t')
20431 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20432 int x = it->current_x + it->continuation_lines_width;
20433 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20435 /* If the distance from the current position to the next tab
20436 stop is less than a space character width, use the
20437 tab stop after that. */
20438 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20439 next_tab_x += tab_width;
20441 it->pixel_width = next_tab_x - x;
20442 it->nglyphs = 1;
20443 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20444 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20446 if (it->glyph_row)
20448 append_stretch_glyph (it, it->object, it->pixel_width,
20449 it->ascent + it->descent, it->ascent);
20452 else
20454 /* A multi-byte character. Assume that the display width of the
20455 character is the width of the character multiplied by the
20456 width of the font. */
20458 /* If we found a font, this font should give us the right
20459 metrics. If we didn't find a font, use the frame's
20460 default font and calculate the width of the character
20461 from the charset width; this is what old redisplay code
20462 did. */
20464 pcm = rif->per_char_metric (font, &char2b,
20465 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20467 if (font_not_found_p || !pcm)
20469 int charset = CHAR_CHARSET (it->char_to_display);
20471 it->glyph_not_available_p = 1;
20472 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20473 * CHARSET_WIDTH (charset));
20474 it->phys_ascent = FONT_BASE (font) + boff;
20475 it->phys_descent = FONT_DESCENT (font) - boff;
20477 else
20479 it->pixel_width = pcm->width;
20480 it->phys_ascent = pcm->ascent + boff;
20481 it->phys_descent = pcm->descent - boff;
20482 if (it->glyph_row
20483 && (pcm->lbearing < 0
20484 || pcm->rbearing > pcm->width))
20485 it->glyph_row->contains_overlapping_glyphs_p = 1;
20487 it->nglyphs = 1;
20488 it->ascent = FONT_BASE (font) + boff;
20489 it->descent = FONT_DESCENT (font) - boff;
20490 if (face->box != FACE_NO_BOX)
20492 int thick = face->box_line_width;
20494 if (thick > 0)
20496 it->ascent += thick;
20497 it->descent += thick;
20499 else
20500 thick = - thick;
20502 if (it->start_of_box_run_p)
20503 it->pixel_width += thick;
20504 if (it->end_of_box_run_p)
20505 it->pixel_width += thick;
20508 /* If face has an overline, add the height of the overline
20509 (1 pixel) and a 1 pixel margin to the character height. */
20510 if (face->overline_p)
20511 it->ascent += 2;
20513 take_vertical_position_into_account (it);
20515 if (it->glyph_row)
20516 append_glyph (it);
20518 it->multibyte_p = saved_multibyte_p;
20520 else if (it->what == IT_COMPOSITION)
20522 /* Note: A composition is represented as one glyph in the
20523 glyph matrix. There are no padding glyphs. */
20524 XChar2b char2b;
20525 XFontStruct *font;
20526 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20527 XCharStruct *pcm;
20528 int font_not_found_p;
20529 struct font_info *font_info;
20530 int boff; /* baseline offset */
20531 struct composition *cmp = composition_table[it->cmp_id];
20533 /* Maybe translate single-byte characters to multibyte. */
20534 it->char_to_display = it->c;
20535 if (unibyte_display_via_language_environment
20536 && SINGLE_BYTE_CHAR_P (it->c)
20537 && (it->c >= 0240
20538 || (it->c >= 0200
20539 && !NILP (Vnonascii_translation_table))))
20541 it->char_to_display = unibyte_char_to_multibyte (it->c);
20544 /* Get face and font to use. Encode IT->char_to_display. */
20545 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20546 face = FACE_FROM_ID (it->f, it->face_id);
20547 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20548 &char2b, it->multibyte_p, 0);
20549 font = face->font;
20551 /* When no suitable font found, use the default font. */
20552 font_not_found_p = font == NULL;
20553 if (font_not_found_p)
20555 font = FRAME_FONT (it->f);
20556 boff = FRAME_BASELINE_OFFSET (it->f);
20557 font_info = NULL;
20559 else
20561 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20562 boff = font_info->baseline_offset;
20563 if (font_info->vertical_centering)
20564 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20567 /* There are no padding glyphs, so there is only one glyph to
20568 produce for the composition. Important is that pixel_width,
20569 ascent and descent are the values of what is drawn by
20570 draw_glyphs (i.e. the values of the overall glyphs composed). */
20571 it->nglyphs = 1;
20573 /* If we have not yet calculated pixel size data of glyphs of
20574 the composition for the current face font, calculate them
20575 now. Theoretically, we have to check all fonts for the
20576 glyphs, but that requires much time and memory space. So,
20577 here we check only the font of the first glyph. This leads
20578 to incorrect display very rarely, and C-l (recenter) can
20579 correct the display anyway. */
20580 if (cmp->font != (void *) font)
20582 /* Ascent and descent of the font of the first character of
20583 this composition (adjusted by baseline offset). Ascent
20584 and descent of overall glyphs should not be less than
20585 them respectively. */
20586 int font_ascent = FONT_BASE (font) + boff;
20587 int font_descent = FONT_DESCENT (font) - boff;
20588 /* Bounding box of the overall glyphs. */
20589 int leftmost, rightmost, lowest, highest;
20590 int i, width, ascent, descent;
20592 cmp->font = (void *) font;
20594 /* Initialize the bounding box. */
20595 if (font_info
20596 && (pcm = rif->per_char_metric (font, &char2b,
20597 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20599 width = pcm->width;
20600 ascent = pcm->ascent;
20601 descent = pcm->descent;
20603 else
20605 width = FONT_WIDTH (font);
20606 ascent = FONT_BASE (font);
20607 descent = FONT_DESCENT (font);
20610 rightmost = width;
20611 lowest = - descent + boff;
20612 highest = ascent + boff;
20613 leftmost = 0;
20615 if (font_info
20616 && font_info->default_ascent
20617 && CHAR_TABLE_P (Vuse_default_ascent)
20618 && !NILP (Faref (Vuse_default_ascent,
20619 make_number (it->char_to_display))))
20620 highest = font_info->default_ascent + boff;
20622 /* Draw the first glyph at the normal position. It may be
20623 shifted to right later if some other glyphs are drawn at
20624 the left. */
20625 cmp->offsets[0] = 0;
20626 cmp->offsets[1] = boff;
20628 /* Set cmp->offsets for the remaining glyphs. */
20629 for (i = 1; i < cmp->glyph_len; i++)
20631 int left, right, btm, top;
20632 int ch = COMPOSITION_GLYPH (cmp, i);
20633 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20635 face = FACE_FROM_ID (it->f, face_id);
20636 get_char_face_and_encoding (it->f, ch, face->id,
20637 &char2b, it->multibyte_p, 0);
20638 font = face->font;
20639 if (font == NULL)
20641 font = FRAME_FONT (it->f);
20642 boff = FRAME_BASELINE_OFFSET (it->f);
20643 font_info = NULL;
20645 else
20647 font_info
20648 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20649 boff = font_info->baseline_offset;
20650 if (font_info->vertical_centering)
20651 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20654 if (font_info
20655 && (pcm = rif->per_char_metric (font, &char2b,
20656 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20658 width = pcm->width;
20659 ascent = pcm->ascent;
20660 descent = pcm->descent;
20662 else
20664 width = FONT_WIDTH (font);
20665 ascent = 1;
20666 descent = 0;
20669 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20671 /* Relative composition with or without
20672 alternate chars. */
20673 left = (leftmost + rightmost - width) / 2;
20674 btm = - descent + boff;
20675 if (font_info && font_info->relative_compose
20676 && (! CHAR_TABLE_P (Vignore_relative_composition)
20677 || NILP (Faref (Vignore_relative_composition,
20678 make_number (ch)))))
20681 if (- descent >= font_info->relative_compose)
20682 /* One extra pixel between two glyphs. */
20683 btm = highest + 1;
20684 else if (ascent <= 0)
20685 /* One extra pixel between two glyphs. */
20686 btm = lowest - 1 - ascent - descent;
20689 else
20691 /* A composition rule is specified by an integer
20692 value that encodes global and new reference
20693 points (GREF and NREF). GREF and NREF are
20694 specified by numbers as below:
20696 0---1---2 -- ascent
20700 9--10--11 -- center
20702 ---3---4---5--- baseline
20704 6---7---8 -- descent
20706 int rule = COMPOSITION_RULE (cmp, i);
20707 int gref, nref, grefx, grefy, nrefx, nrefy;
20709 COMPOSITION_DECODE_RULE (rule, gref, nref);
20710 grefx = gref % 3, nrefx = nref % 3;
20711 grefy = gref / 3, nrefy = nref / 3;
20713 left = (leftmost
20714 + grefx * (rightmost - leftmost) / 2
20715 - nrefx * width / 2);
20716 btm = ((grefy == 0 ? highest
20717 : grefy == 1 ? 0
20718 : grefy == 2 ? lowest
20719 : (highest + lowest) / 2)
20720 - (nrefy == 0 ? ascent + descent
20721 : nrefy == 1 ? descent - boff
20722 : nrefy == 2 ? 0
20723 : (ascent + descent) / 2));
20726 cmp->offsets[i * 2] = left;
20727 cmp->offsets[i * 2 + 1] = btm + descent;
20729 /* Update the bounding box of the overall glyphs. */
20730 right = left + width;
20731 top = btm + descent + ascent;
20732 if (left < leftmost)
20733 leftmost = left;
20734 if (right > rightmost)
20735 rightmost = right;
20736 if (top > highest)
20737 highest = top;
20738 if (btm < lowest)
20739 lowest = btm;
20742 /* If there are glyphs whose x-offsets are negative,
20743 shift all glyphs to the right and make all x-offsets
20744 non-negative. */
20745 if (leftmost < 0)
20747 for (i = 0; i < cmp->glyph_len; i++)
20748 cmp->offsets[i * 2] -= leftmost;
20749 rightmost -= leftmost;
20752 cmp->pixel_width = rightmost;
20753 cmp->ascent = highest;
20754 cmp->descent = - lowest;
20755 if (cmp->ascent < font_ascent)
20756 cmp->ascent = font_ascent;
20757 if (cmp->descent < font_descent)
20758 cmp->descent = font_descent;
20761 it->pixel_width = cmp->pixel_width;
20762 it->ascent = it->phys_ascent = cmp->ascent;
20763 it->descent = it->phys_descent = cmp->descent;
20765 if (face->box != FACE_NO_BOX)
20767 int thick = face->box_line_width;
20769 if (thick > 0)
20771 it->ascent += thick;
20772 it->descent += thick;
20774 else
20775 thick = - thick;
20777 if (it->start_of_box_run_p)
20778 it->pixel_width += thick;
20779 if (it->end_of_box_run_p)
20780 it->pixel_width += thick;
20783 /* If face has an overline, add the height of the overline
20784 (1 pixel) and a 1 pixel margin to the character height. */
20785 if (face->overline_p)
20786 it->ascent += 2;
20788 take_vertical_position_into_account (it);
20790 if (it->glyph_row)
20791 append_composite_glyph (it);
20793 else if (it->what == IT_IMAGE)
20794 produce_image_glyph (it);
20795 else if (it->what == IT_STRETCH)
20796 produce_stretch_glyph (it);
20798 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20799 because this isn't true for images with `:ascent 100'. */
20800 xassert (it->ascent >= 0 && it->descent >= 0);
20801 if (it->area == TEXT_AREA)
20802 it->current_x += it->pixel_width;
20804 if (extra_line_spacing > 0)
20806 it->descent += extra_line_spacing;
20807 if (extra_line_spacing > it->max_extra_line_spacing)
20808 it->max_extra_line_spacing = extra_line_spacing;
20811 it->max_ascent = max (it->max_ascent, it->ascent);
20812 it->max_descent = max (it->max_descent, it->descent);
20813 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20814 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20817 /* EXPORT for RIF:
20818 Output LEN glyphs starting at START at the nominal cursor position.
20819 Advance the nominal cursor over the text. The global variable
20820 updated_window contains the window being updated, updated_row is
20821 the glyph row being updated, and updated_area is the area of that
20822 row being updated. */
20824 void
20825 x_write_glyphs (start, len)
20826 struct glyph *start;
20827 int len;
20829 int x, hpos;
20831 xassert (updated_window && updated_row);
20832 BLOCK_INPUT;
20834 /* Write glyphs. */
20836 hpos = start - updated_row->glyphs[updated_area];
20837 x = draw_glyphs (updated_window, output_cursor.x,
20838 updated_row, updated_area,
20839 hpos, hpos + len,
20840 DRAW_NORMAL_TEXT, 0);
20842 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20843 if (updated_area == TEXT_AREA
20844 && updated_window->phys_cursor_on_p
20845 && updated_window->phys_cursor.vpos == output_cursor.vpos
20846 && updated_window->phys_cursor.hpos >= hpos
20847 && updated_window->phys_cursor.hpos < hpos + len)
20848 updated_window->phys_cursor_on_p = 0;
20850 UNBLOCK_INPUT;
20852 /* Advance the output cursor. */
20853 output_cursor.hpos += len;
20854 output_cursor.x = x;
20858 /* EXPORT for RIF:
20859 Insert LEN glyphs from START at the nominal cursor position. */
20861 void
20862 x_insert_glyphs (start, len)
20863 struct glyph *start;
20864 int len;
20866 struct frame *f;
20867 struct window *w;
20868 int line_height, shift_by_width, shifted_region_width;
20869 struct glyph_row *row;
20870 struct glyph *glyph;
20871 int frame_x, frame_y, hpos;
20873 xassert (updated_window && updated_row);
20874 BLOCK_INPUT;
20875 w = updated_window;
20876 f = XFRAME (WINDOW_FRAME (w));
20878 /* Get the height of the line we are in. */
20879 row = updated_row;
20880 line_height = row->height;
20882 /* Get the width of the glyphs to insert. */
20883 shift_by_width = 0;
20884 for (glyph = start; glyph < start + len; ++glyph)
20885 shift_by_width += glyph->pixel_width;
20887 /* Get the width of the region to shift right. */
20888 shifted_region_width = (window_box_width (w, updated_area)
20889 - output_cursor.x
20890 - shift_by_width);
20892 /* Shift right. */
20893 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20894 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20896 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20897 line_height, shift_by_width);
20899 /* Write the glyphs. */
20900 hpos = start - row->glyphs[updated_area];
20901 draw_glyphs (w, output_cursor.x, row, updated_area,
20902 hpos, hpos + len,
20903 DRAW_NORMAL_TEXT, 0);
20905 /* Advance the output cursor. */
20906 output_cursor.hpos += len;
20907 output_cursor.x += shift_by_width;
20908 UNBLOCK_INPUT;
20912 /* EXPORT for RIF:
20913 Erase the current text line from the nominal cursor position
20914 (inclusive) to pixel column TO_X (exclusive). The idea is that
20915 everything from TO_X onward is already erased.
20917 TO_X is a pixel position relative to updated_area of
20918 updated_window. TO_X == -1 means clear to the end of this area. */
20920 void
20921 x_clear_end_of_line (to_x)
20922 int to_x;
20924 struct frame *f;
20925 struct window *w = updated_window;
20926 int max_x, min_y, max_y;
20927 int from_x, from_y, to_y;
20929 xassert (updated_window && updated_row);
20930 f = XFRAME (w->frame);
20932 if (updated_row->full_width_p)
20933 max_x = WINDOW_TOTAL_WIDTH (w);
20934 else
20935 max_x = window_box_width (w, updated_area);
20936 max_y = window_text_bottom_y (w);
20938 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20939 of window. For TO_X > 0, truncate to end of drawing area. */
20940 if (to_x == 0)
20941 return;
20942 else if (to_x < 0)
20943 to_x = max_x;
20944 else
20945 to_x = min (to_x, max_x);
20947 to_y = min (max_y, output_cursor.y + updated_row->height);
20949 /* Notice if the cursor will be cleared by this operation. */
20950 if (!updated_row->full_width_p)
20951 notice_overwritten_cursor (w, updated_area,
20952 output_cursor.x, -1,
20953 updated_row->y,
20954 MATRIX_ROW_BOTTOM_Y (updated_row));
20956 from_x = output_cursor.x;
20958 /* Translate to frame coordinates. */
20959 if (updated_row->full_width_p)
20961 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20962 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20964 else
20966 int area_left = window_box_left (w, updated_area);
20967 from_x += area_left;
20968 to_x += area_left;
20971 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20972 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20973 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20975 /* Prevent inadvertently clearing to end of the X window. */
20976 if (to_x > from_x && to_y > from_y)
20978 BLOCK_INPUT;
20979 rif->clear_frame_area (f, from_x, from_y,
20980 to_x - from_x, to_y - from_y);
20981 UNBLOCK_INPUT;
20985 #endif /* HAVE_WINDOW_SYSTEM */
20989 /***********************************************************************
20990 Cursor types
20991 ***********************************************************************/
20993 /* Value is the internal representation of the specified cursor type
20994 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20995 of the bar cursor. */
20997 static enum text_cursor_kinds
20998 get_specified_cursor_type (arg, width)
20999 Lisp_Object arg;
21000 int *width;
21002 enum text_cursor_kinds type;
21004 if (NILP (arg))
21005 return NO_CURSOR;
21007 if (EQ (arg, Qbox))
21008 return FILLED_BOX_CURSOR;
21010 if (EQ (arg, Qhollow))
21011 return HOLLOW_BOX_CURSOR;
21013 if (EQ (arg, Qbar))
21015 *width = 2;
21016 return BAR_CURSOR;
21019 if (CONSP (arg)
21020 && EQ (XCAR (arg), Qbar)
21021 && INTEGERP (XCDR (arg))
21022 && XINT (XCDR (arg)) >= 0)
21024 *width = XINT (XCDR (arg));
21025 return BAR_CURSOR;
21028 if (EQ (arg, Qhbar))
21030 *width = 2;
21031 return HBAR_CURSOR;
21034 if (CONSP (arg)
21035 && EQ (XCAR (arg), Qhbar)
21036 && INTEGERP (XCDR (arg))
21037 && XINT (XCDR (arg)) >= 0)
21039 *width = XINT (XCDR (arg));
21040 return HBAR_CURSOR;
21043 /* Treat anything unknown as "hollow box cursor".
21044 It was bad to signal an error; people have trouble fixing
21045 .Xdefaults with Emacs, when it has something bad in it. */
21046 type = HOLLOW_BOX_CURSOR;
21048 return type;
21051 /* Set the default cursor types for specified frame. */
21052 void
21053 set_frame_cursor_types (f, arg)
21054 struct frame *f;
21055 Lisp_Object arg;
21057 int width;
21058 Lisp_Object tem;
21060 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21061 FRAME_CURSOR_WIDTH (f) = width;
21063 /* By default, set up the blink-off state depending on the on-state. */
21065 tem = Fassoc (arg, Vblink_cursor_alist);
21066 if (!NILP (tem))
21068 FRAME_BLINK_OFF_CURSOR (f)
21069 = get_specified_cursor_type (XCDR (tem), &width);
21070 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21072 else
21073 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21077 /* Return the cursor we want to be displayed in window W. Return
21078 width of bar/hbar cursor through WIDTH arg. Return with
21079 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21080 (i.e. if the `system caret' should track this cursor).
21082 In a mini-buffer window, we want the cursor only to appear if we
21083 are reading input from this window. For the selected window, we
21084 want the cursor type given by the frame parameter or buffer local
21085 setting of cursor-type. If explicitly marked off, draw no cursor.
21086 In all other cases, we want a hollow box cursor. */
21088 static enum text_cursor_kinds
21089 get_window_cursor_type (w, glyph, width, active_cursor)
21090 struct window *w;
21091 struct glyph *glyph;
21092 int *width;
21093 int *active_cursor;
21095 struct frame *f = XFRAME (w->frame);
21096 struct buffer *b = XBUFFER (w->buffer);
21097 int cursor_type = DEFAULT_CURSOR;
21098 Lisp_Object alt_cursor;
21099 int non_selected = 0;
21101 *active_cursor = 1;
21103 /* Echo area */
21104 if (cursor_in_echo_area
21105 && FRAME_HAS_MINIBUF_P (f)
21106 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21108 if (w == XWINDOW (echo_area_window))
21110 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21112 *width = FRAME_CURSOR_WIDTH (f);
21113 return FRAME_DESIRED_CURSOR (f);
21115 else
21116 return get_specified_cursor_type (b->cursor_type, width);
21119 *active_cursor = 0;
21120 non_selected = 1;
21123 /* Nonselected window or nonselected frame. */
21124 else if (w != XWINDOW (f->selected_window)
21125 #ifdef HAVE_WINDOW_SYSTEM
21126 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21127 #endif
21130 *active_cursor = 0;
21132 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21133 return NO_CURSOR;
21135 non_selected = 1;
21138 /* Never display a cursor in a window in which cursor-type is nil. */
21139 if (NILP (b->cursor_type))
21140 return NO_CURSOR;
21142 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21143 if (non_selected)
21145 alt_cursor = b->cursor_in_non_selected_windows;
21146 return get_specified_cursor_type (alt_cursor, width);
21149 /* Get the normal cursor type for this window. */
21150 if (EQ (b->cursor_type, Qt))
21152 cursor_type = FRAME_DESIRED_CURSOR (f);
21153 *width = FRAME_CURSOR_WIDTH (f);
21155 else
21156 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21158 /* Use normal cursor if not blinked off. */
21159 if (!w->cursor_off_p)
21161 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21162 if (cursor_type == FILLED_BOX_CURSOR)
21163 cursor_type = HOLLOW_BOX_CURSOR;
21165 return cursor_type;
21168 /* Cursor is blinked off, so determine how to "toggle" it. */
21170 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21171 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21172 return get_specified_cursor_type (XCDR (alt_cursor), width);
21174 /* Then see if frame has specified a specific blink off cursor type. */
21175 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21177 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21178 return FRAME_BLINK_OFF_CURSOR (f);
21181 #if 0
21182 /* Some people liked having a permanently visible blinking cursor,
21183 while others had very strong opinions against it. So it was
21184 decided to remove it. KFS 2003-09-03 */
21186 /* Finally perform built-in cursor blinking:
21187 filled box <-> hollow box
21188 wide [h]bar <-> narrow [h]bar
21189 narrow [h]bar <-> no cursor
21190 other type <-> no cursor */
21192 if (cursor_type == FILLED_BOX_CURSOR)
21193 return HOLLOW_BOX_CURSOR;
21195 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21197 *width = 1;
21198 return cursor_type;
21200 #endif
21202 return NO_CURSOR;
21206 #ifdef HAVE_WINDOW_SYSTEM
21208 /* Notice when the text cursor of window W has been completely
21209 overwritten by a drawing operation that outputs glyphs in AREA
21210 starting at X0 and ending at X1 in the line starting at Y0 and
21211 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21212 the rest of the line after X0 has been written. Y coordinates
21213 are window-relative. */
21215 static void
21216 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21217 struct window *w;
21218 enum glyph_row_area area;
21219 int x0, y0, x1, y1;
21221 int cx0, cx1, cy0, cy1;
21222 struct glyph_row *row;
21224 if (!w->phys_cursor_on_p)
21225 return;
21226 if (area != TEXT_AREA)
21227 return;
21229 if (w->phys_cursor.vpos < 0
21230 || w->phys_cursor.vpos >= w->current_matrix->nrows
21231 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21232 !(row->enabled_p && row->displays_text_p)))
21233 return;
21235 if (row->cursor_in_fringe_p)
21237 row->cursor_in_fringe_p = 0;
21238 draw_fringe_bitmap (w, row, 0);
21239 w->phys_cursor_on_p = 0;
21240 return;
21243 cx0 = w->phys_cursor.x;
21244 cx1 = cx0 + w->phys_cursor_width;
21245 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21246 return;
21248 /* The cursor image will be completely removed from the
21249 screen if the output area intersects the cursor area in
21250 y-direction. When we draw in [y0 y1[, and some part of
21251 the cursor is at y < y0, that part must have been drawn
21252 before. When scrolling, the cursor is erased before
21253 actually scrolling, so we don't come here. When not
21254 scrolling, the rows above the old cursor row must have
21255 changed, and in this case these rows must have written
21256 over the cursor image.
21258 Likewise if part of the cursor is below y1, with the
21259 exception of the cursor being in the first blank row at
21260 the buffer and window end because update_text_area
21261 doesn't draw that row. (Except when it does, but
21262 that's handled in update_text_area.) */
21264 cy0 = w->phys_cursor.y;
21265 cy1 = cy0 + w->phys_cursor_height;
21266 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21267 return;
21269 w->phys_cursor_on_p = 0;
21272 #endif /* HAVE_WINDOW_SYSTEM */
21275 /************************************************************************
21276 Mouse Face
21277 ************************************************************************/
21279 #ifdef HAVE_WINDOW_SYSTEM
21281 /* EXPORT for RIF:
21282 Fix the display of area AREA of overlapping row ROW in window W
21283 with respect to the overlapping part OVERLAPS. */
21285 void
21286 x_fix_overlapping_area (w, row, area, overlaps)
21287 struct window *w;
21288 struct glyph_row *row;
21289 enum glyph_row_area area;
21290 int overlaps;
21292 int i, x;
21294 BLOCK_INPUT;
21296 x = 0;
21297 for (i = 0; i < row->used[area];)
21299 if (row->glyphs[area][i].overlaps_vertically_p)
21301 int start = i, start_x = x;
21305 x += row->glyphs[area][i].pixel_width;
21306 ++i;
21308 while (i < row->used[area]
21309 && row->glyphs[area][i].overlaps_vertically_p);
21311 draw_glyphs (w, start_x, row, area,
21312 start, i,
21313 DRAW_NORMAL_TEXT, overlaps);
21315 else
21317 x += row->glyphs[area][i].pixel_width;
21318 ++i;
21322 UNBLOCK_INPUT;
21326 /* EXPORT:
21327 Draw the cursor glyph of window W in glyph row ROW. See the
21328 comment of draw_glyphs for the meaning of HL. */
21330 void
21331 draw_phys_cursor_glyph (w, row, hl)
21332 struct window *w;
21333 struct glyph_row *row;
21334 enum draw_glyphs_face hl;
21336 /* If cursor hpos is out of bounds, don't draw garbage. This can
21337 happen in mini-buffer windows when switching between echo area
21338 glyphs and mini-buffer. */
21339 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21341 int on_p = w->phys_cursor_on_p;
21342 int x1;
21343 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21344 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21345 hl, 0);
21346 w->phys_cursor_on_p = on_p;
21348 if (hl == DRAW_CURSOR)
21349 w->phys_cursor_width = x1 - w->phys_cursor.x;
21350 /* When we erase the cursor, and ROW is overlapped by other
21351 rows, make sure that these overlapping parts of other rows
21352 are redrawn. */
21353 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21355 w->phys_cursor_width = x1 - w->phys_cursor.x;
21357 if (row > w->current_matrix->rows
21358 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21359 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21360 OVERLAPS_ERASED_CURSOR);
21362 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21363 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21364 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21365 OVERLAPS_ERASED_CURSOR);
21371 /* EXPORT:
21372 Erase the image of a cursor of window W from the screen. */
21374 void
21375 erase_phys_cursor (w)
21376 struct window *w;
21378 struct frame *f = XFRAME (w->frame);
21379 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21380 int hpos = w->phys_cursor.hpos;
21381 int vpos = w->phys_cursor.vpos;
21382 int mouse_face_here_p = 0;
21383 struct glyph_matrix *active_glyphs = w->current_matrix;
21384 struct glyph_row *cursor_row;
21385 struct glyph *cursor_glyph;
21386 enum draw_glyphs_face hl;
21388 /* No cursor displayed or row invalidated => nothing to do on the
21389 screen. */
21390 if (w->phys_cursor_type == NO_CURSOR)
21391 goto mark_cursor_off;
21393 /* VPOS >= active_glyphs->nrows means that window has been resized.
21394 Don't bother to erase the cursor. */
21395 if (vpos >= active_glyphs->nrows)
21396 goto mark_cursor_off;
21398 /* If row containing cursor is marked invalid, there is nothing we
21399 can do. */
21400 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21401 if (!cursor_row->enabled_p)
21402 goto mark_cursor_off;
21404 /* If line spacing is > 0, old cursor may only be partially visible in
21405 window after split-window. So adjust visible height. */
21406 cursor_row->visible_height = min (cursor_row->visible_height,
21407 window_text_bottom_y (w) - cursor_row->y);
21409 /* If row is completely invisible, don't attempt to delete a cursor which
21410 isn't there. This can happen if cursor is at top of a window, and
21411 we switch to a buffer with a header line in that window. */
21412 if (cursor_row->visible_height <= 0)
21413 goto mark_cursor_off;
21415 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21416 if (cursor_row->cursor_in_fringe_p)
21418 cursor_row->cursor_in_fringe_p = 0;
21419 draw_fringe_bitmap (w, cursor_row, 0);
21420 goto mark_cursor_off;
21423 /* This can happen when the new row is shorter than the old one.
21424 In this case, either draw_glyphs or clear_end_of_line
21425 should have cleared the cursor. Note that we wouldn't be
21426 able to erase the cursor in this case because we don't have a
21427 cursor glyph at hand. */
21428 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21429 goto mark_cursor_off;
21431 /* If the cursor is in the mouse face area, redisplay that when
21432 we clear the cursor. */
21433 if (! NILP (dpyinfo->mouse_face_window)
21434 && w == XWINDOW (dpyinfo->mouse_face_window)
21435 && (vpos > dpyinfo->mouse_face_beg_row
21436 || (vpos == dpyinfo->mouse_face_beg_row
21437 && hpos >= dpyinfo->mouse_face_beg_col))
21438 && (vpos < dpyinfo->mouse_face_end_row
21439 || (vpos == dpyinfo->mouse_face_end_row
21440 && hpos < dpyinfo->mouse_face_end_col))
21441 /* Don't redraw the cursor's spot in mouse face if it is at the
21442 end of a line (on a newline). The cursor appears there, but
21443 mouse highlighting does not. */
21444 && cursor_row->used[TEXT_AREA] > hpos)
21445 mouse_face_here_p = 1;
21447 /* Maybe clear the display under the cursor. */
21448 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21450 int x, y, left_x;
21451 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21452 int width;
21454 cursor_glyph = get_phys_cursor_glyph (w);
21455 if (cursor_glyph == NULL)
21456 goto mark_cursor_off;
21458 width = cursor_glyph->pixel_width;
21459 left_x = window_box_left_offset (w, TEXT_AREA);
21460 x = w->phys_cursor.x;
21461 if (x < left_x)
21462 width -= left_x - x;
21463 width = min (width, window_box_width (w, TEXT_AREA) - x);
21464 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21465 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21467 if (width > 0)
21468 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21471 /* Erase the cursor by redrawing the character underneath it. */
21472 if (mouse_face_here_p)
21473 hl = DRAW_MOUSE_FACE;
21474 else
21475 hl = DRAW_NORMAL_TEXT;
21476 draw_phys_cursor_glyph (w, cursor_row, hl);
21478 mark_cursor_off:
21479 w->phys_cursor_on_p = 0;
21480 w->phys_cursor_type = NO_CURSOR;
21484 /* EXPORT:
21485 Display or clear cursor of window W. If ON is zero, clear the
21486 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21487 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21489 void
21490 display_and_set_cursor (w, on, hpos, vpos, x, y)
21491 struct window *w;
21492 int on, hpos, vpos, x, y;
21494 struct frame *f = XFRAME (w->frame);
21495 int new_cursor_type;
21496 int new_cursor_width;
21497 int active_cursor;
21498 struct glyph_row *glyph_row;
21499 struct glyph *glyph;
21501 /* This is pointless on invisible frames, and dangerous on garbaged
21502 windows and frames; in the latter case, the frame or window may
21503 be in the midst of changing its size, and x and y may be off the
21504 window. */
21505 if (! FRAME_VISIBLE_P (f)
21506 || FRAME_GARBAGED_P (f)
21507 || vpos >= w->current_matrix->nrows
21508 || hpos >= w->current_matrix->matrix_w)
21509 return;
21511 /* If cursor is off and we want it off, return quickly. */
21512 if (!on && !w->phys_cursor_on_p)
21513 return;
21515 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21516 /* If cursor row is not enabled, we don't really know where to
21517 display the cursor. */
21518 if (!glyph_row->enabled_p)
21520 w->phys_cursor_on_p = 0;
21521 return;
21524 glyph = NULL;
21525 if (!glyph_row->exact_window_width_line_p
21526 || hpos < glyph_row->used[TEXT_AREA])
21527 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21529 xassert (interrupt_input_blocked);
21531 /* Set new_cursor_type to the cursor we want to be displayed. */
21532 new_cursor_type = get_window_cursor_type (w, glyph,
21533 &new_cursor_width, &active_cursor);
21535 /* If cursor is currently being shown and we don't want it to be or
21536 it is in the wrong place, or the cursor type is not what we want,
21537 erase it. */
21538 if (w->phys_cursor_on_p
21539 && (!on
21540 || w->phys_cursor.x != x
21541 || w->phys_cursor.y != y
21542 || new_cursor_type != w->phys_cursor_type
21543 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21544 && new_cursor_width != w->phys_cursor_width)))
21545 erase_phys_cursor (w);
21547 /* Don't check phys_cursor_on_p here because that flag is only set
21548 to zero in some cases where we know that the cursor has been
21549 completely erased, to avoid the extra work of erasing the cursor
21550 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21551 still not be visible, or it has only been partly erased. */
21552 if (on)
21554 w->phys_cursor_ascent = glyph_row->ascent;
21555 w->phys_cursor_height = glyph_row->height;
21557 /* Set phys_cursor_.* before x_draw_.* is called because some
21558 of them may need the information. */
21559 w->phys_cursor.x = x;
21560 w->phys_cursor.y = glyph_row->y;
21561 w->phys_cursor.hpos = hpos;
21562 w->phys_cursor.vpos = vpos;
21565 rif->draw_window_cursor (w, glyph_row, x, y,
21566 new_cursor_type, new_cursor_width,
21567 on, active_cursor);
21571 /* Switch the display of W's cursor on or off, according to the value
21572 of ON. */
21574 static void
21575 update_window_cursor (w, on)
21576 struct window *w;
21577 int on;
21579 /* Don't update cursor in windows whose frame is in the process
21580 of being deleted. */
21581 if (w->current_matrix)
21583 BLOCK_INPUT;
21584 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21585 w->phys_cursor.x, w->phys_cursor.y);
21586 UNBLOCK_INPUT;
21591 /* Call update_window_cursor with parameter ON_P on all leaf windows
21592 in the window tree rooted at W. */
21594 static void
21595 update_cursor_in_window_tree (w, on_p)
21596 struct window *w;
21597 int on_p;
21599 while (w)
21601 if (!NILP (w->hchild))
21602 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21603 else if (!NILP (w->vchild))
21604 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21605 else
21606 update_window_cursor (w, on_p);
21608 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21613 /* EXPORT:
21614 Display the cursor on window W, or clear it, according to ON_P.
21615 Don't change the cursor's position. */
21617 void
21618 x_update_cursor (f, on_p)
21619 struct frame *f;
21620 int on_p;
21622 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21626 /* EXPORT:
21627 Clear the cursor of window W to background color, and mark the
21628 cursor as not shown. This is used when the text where the cursor
21629 is is about to be rewritten. */
21631 void
21632 x_clear_cursor (w)
21633 struct window *w;
21635 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21636 update_window_cursor (w, 0);
21640 /* EXPORT:
21641 Display the active region described by mouse_face_* according to DRAW. */
21643 void
21644 show_mouse_face (dpyinfo, draw)
21645 Display_Info *dpyinfo;
21646 enum draw_glyphs_face draw;
21648 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21649 struct frame *f = XFRAME (WINDOW_FRAME (w));
21651 if (/* If window is in the process of being destroyed, don't bother
21652 to do anything. */
21653 w->current_matrix != NULL
21654 /* Don't update mouse highlight if hidden */
21655 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21656 /* Recognize when we are called to operate on rows that don't exist
21657 anymore. This can happen when a window is split. */
21658 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21660 int phys_cursor_on_p = w->phys_cursor_on_p;
21661 struct glyph_row *row, *first, *last;
21663 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21664 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21666 for (row = first; row <= last && row->enabled_p; ++row)
21668 int start_hpos, end_hpos, start_x;
21670 /* For all but the first row, the highlight starts at column 0. */
21671 if (row == first)
21673 start_hpos = dpyinfo->mouse_face_beg_col;
21674 start_x = dpyinfo->mouse_face_beg_x;
21676 else
21678 start_hpos = 0;
21679 start_x = 0;
21682 if (row == last)
21683 end_hpos = dpyinfo->mouse_face_end_col;
21684 else
21686 end_hpos = row->used[TEXT_AREA];
21687 if (draw == DRAW_NORMAL_TEXT)
21688 row->fill_line_p = 1; /* Clear to end of line */
21691 if (end_hpos > start_hpos)
21693 draw_glyphs (w, start_x, row, TEXT_AREA,
21694 start_hpos, end_hpos,
21695 draw, 0);
21697 row->mouse_face_p
21698 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21702 /* When we've written over the cursor, arrange for it to
21703 be displayed again. */
21704 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21706 BLOCK_INPUT;
21707 display_and_set_cursor (w, 1,
21708 w->phys_cursor.hpos, w->phys_cursor.vpos,
21709 w->phys_cursor.x, w->phys_cursor.y);
21710 UNBLOCK_INPUT;
21714 /* Change the mouse cursor. */
21715 if (draw == DRAW_NORMAL_TEXT)
21716 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21717 else if (draw == DRAW_MOUSE_FACE)
21718 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21719 else
21720 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21723 /* EXPORT:
21724 Clear out the mouse-highlighted active region.
21725 Redraw it un-highlighted first. Value is non-zero if mouse
21726 face was actually drawn unhighlighted. */
21729 clear_mouse_face (dpyinfo)
21730 Display_Info *dpyinfo;
21732 int cleared = 0;
21734 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21736 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21737 cleared = 1;
21740 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21741 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21742 dpyinfo->mouse_face_window = Qnil;
21743 dpyinfo->mouse_face_overlay = Qnil;
21744 return cleared;
21748 /* EXPORT:
21749 Non-zero if physical cursor of window W is within mouse face. */
21752 cursor_in_mouse_face_p (w)
21753 struct window *w;
21755 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21756 int in_mouse_face = 0;
21758 if (WINDOWP (dpyinfo->mouse_face_window)
21759 && XWINDOW (dpyinfo->mouse_face_window) == w)
21761 int hpos = w->phys_cursor.hpos;
21762 int vpos = w->phys_cursor.vpos;
21764 if (vpos >= dpyinfo->mouse_face_beg_row
21765 && vpos <= dpyinfo->mouse_face_end_row
21766 && (vpos > dpyinfo->mouse_face_beg_row
21767 || hpos >= dpyinfo->mouse_face_beg_col)
21768 && (vpos < dpyinfo->mouse_face_end_row
21769 || hpos < dpyinfo->mouse_face_end_col
21770 || dpyinfo->mouse_face_past_end))
21771 in_mouse_face = 1;
21774 return in_mouse_face;
21780 /* Find the glyph matrix position of buffer position CHARPOS in window
21781 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21782 current glyphs must be up to date. If CHARPOS is above window
21783 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21784 of last line in W. In the row containing CHARPOS, stop before glyphs
21785 having STOP as object. */
21787 #if 1 /* This is a version of fast_find_position that's more correct
21788 in the presence of hscrolling, for example. I didn't install
21789 it right away because the problem fixed is minor, it failed
21790 in 20.x as well, and I think it's too risky to install
21791 so near the release of 21.1. 2001-09-25 gerd. */
21793 static int
21794 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21795 struct window *w;
21796 int charpos;
21797 int *hpos, *vpos, *x, *y;
21798 Lisp_Object stop;
21800 struct glyph_row *row, *first;
21801 struct glyph *glyph, *end;
21802 int past_end = 0;
21804 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21805 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21807 *x = first->x;
21808 *y = first->y;
21809 *hpos = 0;
21810 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21811 return 1;
21814 row = row_containing_pos (w, charpos, first, NULL, 0);
21815 if (row == NULL)
21817 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21818 past_end = 1;
21821 /* If whole rows or last part of a row came from a display overlay,
21822 row_containing_pos will skip over such rows because their end pos
21823 equals the start pos of the overlay or interval.
21825 Move back if we have a STOP object and previous row's
21826 end glyph came from STOP. */
21827 if (!NILP (stop))
21829 struct glyph_row *prev;
21830 while ((prev = row - 1, prev >= first)
21831 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21832 && prev->used[TEXT_AREA] > 0)
21834 struct glyph *beg = prev->glyphs[TEXT_AREA];
21835 glyph = beg + prev->used[TEXT_AREA];
21836 while (--glyph >= beg
21837 && INTEGERP (glyph->object));
21838 if (glyph < beg
21839 || !EQ (stop, glyph->object))
21840 break;
21841 row = prev;
21845 *x = row->x;
21846 *y = row->y;
21847 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21849 glyph = row->glyphs[TEXT_AREA];
21850 end = glyph + row->used[TEXT_AREA];
21852 /* Skip over glyphs not having an object at the start of the row.
21853 These are special glyphs like truncation marks on terminal
21854 frames. */
21855 if (row->displays_text_p)
21856 while (glyph < end
21857 && INTEGERP (glyph->object)
21858 && !EQ (stop, glyph->object)
21859 && glyph->charpos < 0)
21861 *x += glyph->pixel_width;
21862 ++glyph;
21865 while (glyph < end
21866 && !INTEGERP (glyph->object)
21867 && !EQ (stop, glyph->object)
21868 && (!BUFFERP (glyph->object)
21869 || glyph->charpos < charpos))
21871 *x += glyph->pixel_width;
21872 ++glyph;
21875 *hpos = glyph - row->glyphs[TEXT_AREA];
21876 return !past_end;
21879 #else /* not 1 */
21881 static int
21882 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21883 struct window *w;
21884 int pos;
21885 int *hpos, *vpos, *x, *y;
21886 Lisp_Object stop;
21888 int i;
21889 int lastcol;
21890 int maybe_next_line_p = 0;
21891 int line_start_position;
21892 int yb = window_text_bottom_y (w);
21893 struct glyph_row *row, *best_row;
21894 int row_vpos, best_row_vpos;
21895 int current_x;
21897 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21898 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21900 while (row->y < yb)
21902 if (row->used[TEXT_AREA])
21903 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21904 else
21905 line_start_position = 0;
21907 if (line_start_position > pos)
21908 break;
21909 /* If the position sought is the end of the buffer,
21910 don't include the blank lines at the bottom of the window. */
21911 else if (line_start_position == pos
21912 && pos == BUF_ZV (XBUFFER (w->buffer)))
21914 maybe_next_line_p = 1;
21915 break;
21917 else if (line_start_position > 0)
21919 best_row = row;
21920 best_row_vpos = row_vpos;
21923 if (row->y + row->height >= yb)
21924 break;
21926 ++row;
21927 ++row_vpos;
21930 /* Find the right column within BEST_ROW. */
21931 lastcol = 0;
21932 current_x = best_row->x;
21933 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21935 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21936 int charpos = glyph->charpos;
21938 if (BUFFERP (glyph->object))
21940 if (charpos == pos)
21942 *hpos = i;
21943 *vpos = best_row_vpos;
21944 *x = current_x;
21945 *y = best_row->y;
21946 return 1;
21948 else if (charpos > pos)
21949 break;
21951 else if (EQ (glyph->object, stop))
21952 break;
21954 if (charpos > 0)
21955 lastcol = i;
21956 current_x += glyph->pixel_width;
21959 /* If we're looking for the end of the buffer,
21960 and we didn't find it in the line we scanned,
21961 use the start of the following line. */
21962 if (maybe_next_line_p)
21964 ++best_row;
21965 ++best_row_vpos;
21966 lastcol = 0;
21967 current_x = best_row->x;
21970 *vpos = best_row_vpos;
21971 *hpos = lastcol + 1;
21972 *x = current_x;
21973 *y = best_row->y;
21974 return 0;
21977 #endif /* not 1 */
21980 /* Find the position of the glyph for position POS in OBJECT in
21981 window W's current matrix, and return in *X, *Y the pixel
21982 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21984 RIGHT_P non-zero means return the position of the right edge of the
21985 glyph, RIGHT_P zero means return the left edge position.
21987 If no glyph for POS exists in the matrix, return the position of
21988 the glyph with the next smaller position that is in the matrix, if
21989 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21990 exists in the matrix, return the position of the glyph with the
21991 next larger position in OBJECT.
21993 Value is non-zero if a glyph was found. */
21995 static int
21996 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21997 struct window *w;
21998 int pos;
21999 Lisp_Object object;
22000 int *hpos, *vpos, *x, *y;
22001 int right_p;
22003 int yb = window_text_bottom_y (w);
22004 struct glyph_row *r;
22005 struct glyph *best_glyph = NULL;
22006 struct glyph_row *best_row = NULL;
22007 int best_x = 0;
22009 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22010 r->enabled_p && r->y < yb;
22011 ++r)
22013 struct glyph *g = r->glyphs[TEXT_AREA];
22014 struct glyph *e = g + r->used[TEXT_AREA];
22015 int gx;
22017 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22018 if (EQ (g->object, object))
22020 if (g->charpos == pos)
22022 best_glyph = g;
22023 best_x = gx;
22024 best_row = r;
22025 goto found;
22027 else if (best_glyph == NULL
22028 || ((abs (g->charpos - pos)
22029 < abs (best_glyph->charpos - pos))
22030 && (right_p
22031 ? g->charpos < pos
22032 : g->charpos > pos)))
22034 best_glyph = g;
22035 best_x = gx;
22036 best_row = r;
22041 found:
22043 if (best_glyph)
22045 *x = best_x;
22046 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22048 if (right_p)
22050 *x += best_glyph->pixel_width;
22051 ++*hpos;
22054 *y = best_row->y;
22055 *vpos = best_row - w->current_matrix->rows;
22058 return best_glyph != NULL;
22062 /* See if position X, Y is within a hot-spot of an image. */
22064 static int
22065 on_hot_spot_p (hot_spot, x, y)
22066 Lisp_Object hot_spot;
22067 int x, y;
22069 if (!CONSP (hot_spot))
22070 return 0;
22072 if (EQ (XCAR (hot_spot), Qrect))
22074 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22075 Lisp_Object rect = XCDR (hot_spot);
22076 Lisp_Object tem;
22077 if (!CONSP (rect))
22078 return 0;
22079 if (!CONSP (XCAR (rect)))
22080 return 0;
22081 if (!CONSP (XCDR (rect)))
22082 return 0;
22083 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22084 return 0;
22085 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22086 return 0;
22087 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22088 return 0;
22089 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22090 return 0;
22091 return 1;
22093 else if (EQ (XCAR (hot_spot), Qcircle))
22095 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22096 Lisp_Object circ = XCDR (hot_spot);
22097 Lisp_Object lr, lx0, ly0;
22098 if (CONSP (circ)
22099 && CONSP (XCAR (circ))
22100 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22101 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22102 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22104 double r = XFLOATINT (lr);
22105 double dx = XINT (lx0) - x;
22106 double dy = XINT (ly0) - y;
22107 return (dx * dx + dy * dy <= r * r);
22110 else if (EQ (XCAR (hot_spot), Qpoly))
22112 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22113 if (VECTORP (XCDR (hot_spot)))
22115 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22116 Lisp_Object *poly = v->contents;
22117 int n = v->size;
22118 int i;
22119 int inside = 0;
22120 Lisp_Object lx, ly;
22121 int x0, y0;
22123 /* Need an even number of coordinates, and at least 3 edges. */
22124 if (n < 6 || n & 1)
22125 return 0;
22127 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22128 If count is odd, we are inside polygon. Pixels on edges
22129 may or may not be included depending on actual geometry of the
22130 polygon. */
22131 if ((lx = poly[n-2], !INTEGERP (lx))
22132 || (ly = poly[n-1], !INTEGERP (lx)))
22133 return 0;
22134 x0 = XINT (lx), y0 = XINT (ly);
22135 for (i = 0; i < n; i += 2)
22137 int x1 = x0, y1 = y0;
22138 if ((lx = poly[i], !INTEGERP (lx))
22139 || (ly = poly[i+1], !INTEGERP (ly)))
22140 return 0;
22141 x0 = XINT (lx), y0 = XINT (ly);
22143 /* Does this segment cross the X line? */
22144 if (x0 >= x)
22146 if (x1 >= x)
22147 continue;
22149 else if (x1 < x)
22150 continue;
22151 if (y > y0 && y > y1)
22152 continue;
22153 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22154 inside = !inside;
22156 return inside;
22159 /* If we don't understand the format, pretend we're not in the hot-spot. */
22160 return 0;
22163 Lisp_Object
22164 find_hot_spot (map, x, y)
22165 Lisp_Object map;
22166 int x, y;
22168 while (CONSP (map))
22170 if (CONSP (XCAR (map))
22171 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22172 return XCAR (map);
22173 map = XCDR (map);
22176 return Qnil;
22179 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22180 3, 3, 0,
22181 doc: /* Lookup in image map MAP coordinates X and Y.
22182 An image map is an alist where each element has the format (AREA ID PLIST).
22183 An AREA is specified as either a rectangle, a circle, or a polygon:
22184 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22185 pixel coordinates of the upper left and bottom right corners.
22186 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22187 and the radius of the circle; r may be a float or integer.
22188 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22189 vector describes one corner in the polygon.
22190 Returns the alist element for the first matching AREA in MAP. */)
22191 (map, x, y)
22192 Lisp_Object map;
22193 Lisp_Object x, y;
22195 if (NILP (map))
22196 return Qnil;
22198 CHECK_NUMBER (x);
22199 CHECK_NUMBER (y);
22201 return find_hot_spot (map, XINT (x), XINT (y));
22205 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22206 static void
22207 define_frame_cursor1 (f, cursor, pointer)
22208 struct frame *f;
22209 Cursor cursor;
22210 Lisp_Object pointer;
22212 /* Do not change cursor shape while dragging mouse. */
22213 if (!NILP (do_mouse_tracking))
22214 return;
22216 if (!NILP (pointer))
22218 if (EQ (pointer, Qarrow))
22219 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22220 else if (EQ (pointer, Qhand))
22221 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22222 else if (EQ (pointer, Qtext))
22223 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22224 else if (EQ (pointer, intern ("hdrag")))
22225 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22226 #ifdef HAVE_X_WINDOWS
22227 else if (EQ (pointer, intern ("vdrag")))
22228 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22229 #endif
22230 else if (EQ (pointer, intern ("hourglass")))
22231 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22232 else if (EQ (pointer, Qmodeline))
22233 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22234 else
22235 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22238 if (cursor != No_Cursor)
22239 rif->define_frame_cursor (f, cursor);
22242 /* Take proper action when mouse has moved to the mode or header line
22243 or marginal area AREA of window W, x-position X and y-position Y.
22244 X is relative to the start of the text display area of W, so the
22245 width of bitmap areas and scroll bars must be subtracted to get a
22246 position relative to the start of the mode line. */
22248 static void
22249 note_mode_line_or_margin_highlight (window, x, y, area)
22250 Lisp_Object window;
22251 int x, y;
22252 enum window_part area;
22254 struct window *w = XWINDOW (window);
22255 struct frame *f = XFRAME (w->frame);
22256 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22257 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22258 Lisp_Object pointer = Qnil;
22259 int charpos, dx, dy, width, height;
22260 Lisp_Object string, object = Qnil;
22261 Lisp_Object pos, help;
22263 Lisp_Object mouse_face;
22264 int original_x_pixel = x;
22265 struct glyph * glyph = NULL;
22266 struct glyph_row *row;
22268 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22270 int x0;
22271 struct glyph *end;
22273 string = mode_line_string (w, area, &x, &y, &charpos,
22274 &object, &dx, &dy, &width, &height);
22276 row = (area == ON_MODE_LINE
22277 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22278 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22280 /* Find glyph */
22281 if (row->mode_line_p && row->enabled_p)
22283 glyph = row->glyphs[TEXT_AREA];
22284 end = glyph + row->used[TEXT_AREA];
22286 for (x0 = original_x_pixel;
22287 glyph < end && x0 >= glyph->pixel_width;
22288 ++glyph)
22289 x0 -= glyph->pixel_width;
22291 if (glyph >= end)
22292 glyph = NULL;
22295 else
22297 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22298 string = marginal_area_string (w, area, &x, &y, &charpos,
22299 &object, &dx, &dy, &width, &height);
22302 help = Qnil;
22304 if (IMAGEP (object))
22306 Lisp_Object image_map, hotspot;
22307 if ((image_map = Fplist_get (XCDR (object), QCmap),
22308 !NILP (image_map))
22309 && (hotspot = find_hot_spot (image_map, dx, dy),
22310 CONSP (hotspot))
22311 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22313 Lisp_Object area_id, plist;
22315 area_id = XCAR (hotspot);
22316 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22317 If so, we could look for mouse-enter, mouse-leave
22318 properties in PLIST (and do something...). */
22319 hotspot = XCDR (hotspot);
22320 if (CONSP (hotspot)
22321 && (plist = XCAR (hotspot), CONSP (plist)))
22323 pointer = Fplist_get (plist, Qpointer);
22324 if (NILP (pointer))
22325 pointer = Qhand;
22326 help = Fplist_get (plist, Qhelp_echo);
22327 if (!NILP (help))
22329 help_echo_string = help;
22330 /* Is this correct? ++kfs */
22331 XSETWINDOW (help_echo_window, w);
22332 help_echo_object = w->buffer;
22333 help_echo_pos = charpos;
22337 if (NILP (pointer))
22338 pointer = Fplist_get (XCDR (object), QCpointer);
22341 if (STRINGP (string))
22343 pos = make_number (charpos);
22344 /* If we're on a string with `help-echo' text property, arrange
22345 for the help to be displayed. This is done by setting the
22346 global variable help_echo_string to the help string. */
22347 if (NILP (help))
22349 help = Fget_text_property (pos, Qhelp_echo, string);
22350 if (!NILP (help))
22352 help_echo_string = help;
22353 XSETWINDOW (help_echo_window, w);
22354 help_echo_object = string;
22355 help_echo_pos = charpos;
22359 if (NILP (pointer))
22360 pointer = Fget_text_property (pos, Qpointer, string);
22362 /* Change the mouse pointer according to what is under X/Y. */
22363 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22365 Lisp_Object map;
22366 map = Fget_text_property (pos, Qlocal_map, string);
22367 if (!KEYMAPP (map))
22368 map = Fget_text_property (pos, Qkeymap, string);
22369 if (!KEYMAPP (map))
22370 cursor = dpyinfo->vertical_scroll_bar_cursor;
22373 /* Change the mouse face according to what is under X/Y. */
22374 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22375 if (!NILP (mouse_face)
22376 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22377 && glyph)
22379 Lisp_Object b, e;
22381 struct glyph * tmp_glyph;
22383 int gpos;
22384 int gseq_length;
22385 int total_pixel_width;
22386 int ignore;
22388 int vpos, hpos;
22390 b = Fprevious_single_property_change (make_number (charpos + 1),
22391 Qmouse_face, string, Qnil);
22392 if (NILP (b))
22393 b = make_number (0);
22395 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22396 if (NILP (e))
22397 e = make_number (SCHARS (string));
22399 /* Calculate the position(glyph position: GPOS) of GLYPH in
22400 displayed string. GPOS is different from CHARPOS.
22402 CHARPOS is the position of glyph in internal string
22403 object. A mode line string format has structures which
22404 is converted to a flatten by emacs lisp interpreter.
22405 The internal string is an element of the structures.
22406 The displayed string is the flatten string. */
22407 for (tmp_glyph = glyph - 1, gpos = 0;
22408 tmp_glyph->charpos >= XINT (b);
22409 tmp_glyph--, gpos++)
22411 if (!EQ (tmp_glyph->object, glyph->object))
22412 break;
22415 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22416 displayed string holding GLYPH.
22418 GSEQ_LENGTH is different from SCHARS (STRING).
22419 SCHARS (STRING) returns the length of the internal string. */
22420 for (tmp_glyph = glyph, gseq_length = gpos;
22421 tmp_glyph->charpos < XINT (e);
22422 tmp_glyph++, gseq_length++)
22424 if (!EQ (tmp_glyph->object, glyph->object))
22425 break;
22428 total_pixel_width = 0;
22429 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22430 total_pixel_width += tmp_glyph->pixel_width;
22432 /* Pre calculation of re-rendering position */
22433 vpos = (x - gpos);
22434 hpos = (area == ON_MODE_LINE
22435 ? (w->current_matrix)->nrows - 1
22436 : 0);
22438 /* If the re-rendering position is included in the last
22439 re-rendering area, we should do nothing. */
22440 if ( EQ (window, dpyinfo->mouse_face_window)
22441 && dpyinfo->mouse_face_beg_col <= vpos
22442 && vpos < dpyinfo->mouse_face_end_col
22443 && dpyinfo->mouse_face_beg_row == hpos )
22444 return;
22446 if (clear_mouse_face (dpyinfo))
22447 cursor = No_Cursor;
22449 dpyinfo->mouse_face_beg_col = vpos;
22450 dpyinfo->mouse_face_beg_row = hpos;
22452 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22453 dpyinfo->mouse_face_beg_y = 0;
22455 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22456 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22458 dpyinfo->mouse_face_end_x = 0;
22459 dpyinfo->mouse_face_end_y = 0;
22461 dpyinfo->mouse_face_past_end = 0;
22462 dpyinfo->mouse_face_window = window;
22464 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22465 charpos,
22466 0, 0, 0, &ignore,
22467 glyph->face_id, 1);
22468 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22470 if (NILP (pointer))
22471 pointer = Qhand;
22473 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22474 clear_mouse_face (dpyinfo);
22476 define_frame_cursor1 (f, cursor, pointer);
22480 /* EXPORT:
22481 Take proper action when the mouse has moved to position X, Y on
22482 frame F as regards highlighting characters that have mouse-face
22483 properties. Also de-highlighting chars where the mouse was before.
22484 X and Y can be negative or out of range. */
22486 void
22487 note_mouse_highlight (f, x, y)
22488 struct frame *f;
22489 int x, y;
22491 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22492 enum window_part part;
22493 Lisp_Object window;
22494 struct window *w;
22495 Cursor cursor = No_Cursor;
22496 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22497 struct buffer *b;
22499 /* When a menu is active, don't highlight because this looks odd. */
22500 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22501 if (popup_activated ())
22502 return;
22503 #endif
22505 if (NILP (Vmouse_highlight)
22506 || !f->glyphs_initialized_p)
22507 return;
22509 dpyinfo->mouse_face_mouse_x = x;
22510 dpyinfo->mouse_face_mouse_y = y;
22511 dpyinfo->mouse_face_mouse_frame = f;
22513 if (dpyinfo->mouse_face_defer)
22514 return;
22516 if (gc_in_progress)
22518 dpyinfo->mouse_face_deferred_gc = 1;
22519 return;
22522 /* Which window is that in? */
22523 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22525 /* If we were displaying active text in another window, clear that.
22526 Also clear if we move out of text area in same window. */
22527 if (! EQ (window, dpyinfo->mouse_face_window)
22528 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22529 && !NILP (dpyinfo->mouse_face_window)))
22530 clear_mouse_face (dpyinfo);
22532 /* Not on a window -> return. */
22533 if (!WINDOWP (window))
22534 return;
22536 /* Reset help_echo_string. It will get recomputed below. */
22537 help_echo_string = Qnil;
22539 /* Convert to window-relative pixel coordinates. */
22540 w = XWINDOW (window);
22541 frame_to_window_pixel_xy (w, &x, &y);
22543 /* Handle tool-bar window differently since it doesn't display a
22544 buffer. */
22545 if (EQ (window, f->tool_bar_window))
22547 note_tool_bar_highlight (f, x, y);
22548 return;
22551 /* Mouse is on the mode, header line or margin? */
22552 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22553 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22555 note_mode_line_or_margin_highlight (window, x, y, part);
22556 return;
22559 if (part == ON_VERTICAL_BORDER)
22561 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22562 help_echo_string = build_string ("drag-mouse-1: resize");
22564 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22565 || part == ON_SCROLL_BAR)
22566 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22567 else
22568 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22570 /* Are we in a window whose display is up to date?
22571 And verify the buffer's text has not changed. */
22572 b = XBUFFER (w->buffer);
22573 if (part == ON_TEXT
22574 && EQ (w->window_end_valid, w->buffer)
22575 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22576 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22578 int hpos, vpos, pos, i, dx, dy, area;
22579 struct glyph *glyph;
22580 Lisp_Object object;
22581 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22582 Lisp_Object *overlay_vec = NULL;
22583 int noverlays;
22584 struct buffer *obuf;
22585 int obegv, ozv, same_region;
22587 /* Find the glyph under X/Y. */
22588 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22590 /* Look for :pointer property on image. */
22591 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22593 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22594 if (img != NULL && IMAGEP (img->spec))
22596 Lisp_Object image_map, hotspot;
22597 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22598 !NILP (image_map))
22599 && (hotspot = find_hot_spot (image_map,
22600 glyph->slice.x + dx,
22601 glyph->slice.y + dy),
22602 CONSP (hotspot))
22603 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22605 Lisp_Object area_id, plist;
22607 area_id = XCAR (hotspot);
22608 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22609 If so, we could look for mouse-enter, mouse-leave
22610 properties in PLIST (and do something...). */
22611 hotspot = XCDR (hotspot);
22612 if (CONSP (hotspot)
22613 && (plist = XCAR (hotspot), CONSP (plist)))
22615 pointer = Fplist_get (plist, Qpointer);
22616 if (NILP (pointer))
22617 pointer = Qhand;
22618 help_echo_string = Fplist_get (plist, Qhelp_echo);
22619 if (!NILP (help_echo_string))
22621 help_echo_window = window;
22622 help_echo_object = glyph->object;
22623 help_echo_pos = glyph->charpos;
22627 if (NILP (pointer))
22628 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22632 /* Clear mouse face if X/Y not over text. */
22633 if (glyph == NULL
22634 || area != TEXT_AREA
22635 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22637 if (clear_mouse_face (dpyinfo))
22638 cursor = No_Cursor;
22639 if (NILP (pointer))
22641 if (area != TEXT_AREA)
22642 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22643 else
22644 pointer = Vvoid_text_area_pointer;
22646 goto set_cursor;
22649 pos = glyph->charpos;
22650 object = glyph->object;
22651 if (!STRINGP (object) && !BUFFERP (object))
22652 goto set_cursor;
22654 /* If we get an out-of-range value, return now; avoid an error. */
22655 if (BUFFERP (object) && pos > BUF_Z (b))
22656 goto set_cursor;
22658 /* Make the window's buffer temporarily current for
22659 overlays_at and compute_char_face. */
22660 obuf = current_buffer;
22661 current_buffer = b;
22662 obegv = BEGV;
22663 ozv = ZV;
22664 BEGV = BEG;
22665 ZV = Z;
22667 /* Is this char mouse-active or does it have help-echo? */
22668 position = make_number (pos);
22670 if (BUFFERP (object))
22672 /* Put all the overlays we want in a vector in overlay_vec. */
22673 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22674 /* Sort overlays into increasing priority order. */
22675 noverlays = sort_overlays (overlay_vec, noverlays, w);
22677 else
22678 noverlays = 0;
22680 same_region = (EQ (window, dpyinfo->mouse_face_window)
22681 && vpos >= dpyinfo->mouse_face_beg_row
22682 && vpos <= dpyinfo->mouse_face_end_row
22683 && (vpos > dpyinfo->mouse_face_beg_row
22684 || hpos >= dpyinfo->mouse_face_beg_col)
22685 && (vpos < dpyinfo->mouse_face_end_row
22686 || hpos < dpyinfo->mouse_face_end_col
22687 || dpyinfo->mouse_face_past_end));
22689 if (same_region)
22690 cursor = No_Cursor;
22692 /* Check mouse-face highlighting. */
22693 if (! same_region
22694 /* If there exists an overlay with mouse-face overlapping
22695 the one we are currently highlighting, we have to
22696 check if we enter the overlapping overlay, and then
22697 highlight only that. */
22698 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22699 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22701 /* Find the highest priority overlay that has a mouse-face
22702 property. */
22703 overlay = Qnil;
22704 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22706 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22707 if (!NILP (mouse_face))
22708 overlay = overlay_vec[i];
22711 /* If we're actually highlighting the same overlay as
22712 before, there's no need to do that again. */
22713 if (!NILP (overlay)
22714 && EQ (overlay, dpyinfo->mouse_face_overlay))
22715 goto check_help_echo;
22717 dpyinfo->mouse_face_overlay = overlay;
22719 /* Clear the display of the old active region, if any. */
22720 if (clear_mouse_face (dpyinfo))
22721 cursor = No_Cursor;
22723 /* If no overlay applies, get a text property. */
22724 if (NILP (overlay))
22725 mouse_face = Fget_text_property (position, Qmouse_face, object);
22727 /* Handle the overlay case. */
22728 if (!NILP (overlay))
22730 /* Find the range of text around this char that
22731 should be active. */
22732 Lisp_Object before, after;
22733 int ignore;
22735 before = Foverlay_start (overlay);
22736 after = Foverlay_end (overlay);
22737 /* Record this as the current active region. */
22738 fast_find_position (w, XFASTINT (before),
22739 &dpyinfo->mouse_face_beg_col,
22740 &dpyinfo->mouse_face_beg_row,
22741 &dpyinfo->mouse_face_beg_x,
22742 &dpyinfo->mouse_face_beg_y, Qnil);
22744 dpyinfo->mouse_face_past_end
22745 = !fast_find_position (w, XFASTINT (after),
22746 &dpyinfo->mouse_face_end_col,
22747 &dpyinfo->mouse_face_end_row,
22748 &dpyinfo->mouse_face_end_x,
22749 &dpyinfo->mouse_face_end_y, Qnil);
22750 dpyinfo->mouse_face_window = window;
22752 dpyinfo->mouse_face_face_id
22753 = face_at_buffer_position (w, pos, 0, 0,
22754 &ignore, pos + 1,
22755 !dpyinfo->mouse_face_hidden);
22757 /* Display it as active. */
22758 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22759 cursor = No_Cursor;
22761 /* Handle the text property case. */
22762 else if (!NILP (mouse_face) && BUFFERP (object))
22764 /* Find the range of text around this char that
22765 should be active. */
22766 Lisp_Object before, after, beginning, end;
22767 int ignore;
22769 beginning = Fmarker_position (w->start);
22770 end = make_number (BUF_Z (XBUFFER (object))
22771 - XFASTINT (w->window_end_pos));
22772 before
22773 = Fprevious_single_property_change (make_number (pos + 1),
22774 Qmouse_face,
22775 object, beginning);
22776 after
22777 = Fnext_single_property_change (position, Qmouse_face,
22778 object, end);
22780 /* Record this as the current active region. */
22781 fast_find_position (w, XFASTINT (before),
22782 &dpyinfo->mouse_face_beg_col,
22783 &dpyinfo->mouse_face_beg_row,
22784 &dpyinfo->mouse_face_beg_x,
22785 &dpyinfo->mouse_face_beg_y, Qnil);
22786 dpyinfo->mouse_face_past_end
22787 = !fast_find_position (w, XFASTINT (after),
22788 &dpyinfo->mouse_face_end_col,
22789 &dpyinfo->mouse_face_end_row,
22790 &dpyinfo->mouse_face_end_x,
22791 &dpyinfo->mouse_face_end_y, Qnil);
22792 dpyinfo->mouse_face_window = window;
22794 if (BUFFERP (object))
22795 dpyinfo->mouse_face_face_id
22796 = face_at_buffer_position (w, pos, 0, 0,
22797 &ignore, pos + 1,
22798 !dpyinfo->mouse_face_hidden);
22800 /* Display it as active. */
22801 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22802 cursor = No_Cursor;
22804 else if (!NILP (mouse_face) && STRINGP (object))
22806 Lisp_Object b, e;
22807 int ignore;
22809 b = Fprevious_single_property_change (make_number (pos + 1),
22810 Qmouse_face,
22811 object, Qnil);
22812 e = Fnext_single_property_change (position, Qmouse_face,
22813 object, Qnil);
22814 if (NILP (b))
22815 b = make_number (0);
22816 if (NILP (e))
22817 e = make_number (SCHARS (object) - 1);
22819 fast_find_string_pos (w, XINT (b), object,
22820 &dpyinfo->mouse_face_beg_col,
22821 &dpyinfo->mouse_face_beg_row,
22822 &dpyinfo->mouse_face_beg_x,
22823 &dpyinfo->mouse_face_beg_y, 0);
22824 fast_find_string_pos (w, XINT (e), object,
22825 &dpyinfo->mouse_face_end_col,
22826 &dpyinfo->mouse_face_end_row,
22827 &dpyinfo->mouse_face_end_x,
22828 &dpyinfo->mouse_face_end_y, 1);
22829 dpyinfo->mouse_face_past_end = 0;
22830 dpyinfo->mouse_face_window = window;
22831 dpyinfo->mouse_face_face_id
22832 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22833 glyph->face_id, 1);
22834 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22835 cursor = No_Cursor;
22837 else if (STRINGP (object) && NILP (mouse_face))
22839 /* A string which doesn't have mouse-face, but
22840 the text ``under'' it might have. */
22841 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22842 int start = MATRIX_ROW_START_CHARPOS (r);
22844 pos = string_buffer_position (w, object, start);
22845 if (pos > 0)
22846 mouse_face = get_char_property_and_overlay (make_number (pos),
22847 Qmouse_face,
22848 w->buffer,
22849 &overlay);
22850 if (!NILP (mouse_face) && !NILP (overlay))
22852 Lisp_Object before = Foverlay_start (overlay);
22853 Lisp_Object after = Foverlay_end (overlay);
22854 int ignore;
22856 /* Note that we might not be able to find position
22857 BEFORE in the glyph matrix if the overlay is
22858 entirely covered by a `display' property. In
22859 this case, we overshoot. So let's stop in
22860 the glyph matrix before glyphs for OBJECT. */
22861 fast_find_position (w, XFASTINT (before),
22862 &dpyinfo->mouse_face_beg_col,
22863 &dpyinfo->mouse_face_beg_row,
22864 &dpyinfo->mouse_face_beg_x,
22865 &dpyinfo->mouse_face_beg_y,
22866 object);
22868 dpyinfo->mouse_face_past_end
22869 = !fast_find_position (w, XFASTINT (after),
22870 &dpyinfo->mouse_face_end_col,
22871 &dpyinfo->mouse_face_end_row,
22872 &dpyinfo->mouse_face_end_x,
22873 &dpyinfo->mouse_face_end_y,
22874 Qnil);
22875 dpyinfo->mouse_face_window = window;
22876 dpyinfo->mouse_face_face_id
22877 = face_at_buffer_position (w, pos, 0, 0,
22878 &ignore, pos + 1,
22879 !dpyinfo->mouse_face_hidden);
22881 /* Display it as active. */
22882 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22883 cursor = No_Cursor;
22888 check_help_echo:
22890 /* Look for a `help-echo' property. */
22891 if (NILP (help_echo_string)) {
22892 Lisp_Object help, overlay;
22894 /* Check overlays first. */
22895 help = overlay = Qnil;
22896 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22898 overlay = overlay_vec[i];
22899 help = Foverlay_get (overlay, Qhelp_echo);
22902 if (!NILP (help))
22904 help_echo_string = help;
22905 help_echo_window = window;
22906 help_echo_object = overlay;
22907 help_echo_pos = pos;
22909 else
22911 Lisp_Object object = glyph->object;
22912 int charpos = glyph->charpos;
22914 /* Try text properties. */
22915 if (STRINGP (object)
22916 && charpos >= 0
22917 && charpos < SCHARS (object))
22919 help = Fget_text_property (make_number (charpos),
22920 Qhelp_echo, object);
22921 if (NILP (help))
22923 /* If the string itself doesn't specify a help-echo,
22924 see if the buffer text ``under'' it does. */
22925 struct glyph_row *r
22926 = MATRIX_ROW (w->current_matrix, vpos);
22927 int start = MATRIX_ROW_START_CHARPOS (r);
22928 int pos = string_buffer_position (w, object, start);
22929 if (pos > 0)
22931 help = Fget_char_property (make_number (pos),
22932 Qhelp_echo, w->buffer);
22933 if (!NILP (help))
22935 charpos = pos;
22936 object = w->buffer;
22941 else if (BUFFERP (object)
22942 && charpos >= BEGV
22943 && charpos < ZV)
22944 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22945 object);
22947 if (!NILP (help))
22949 help_echo_string = help;
22950 help_echo_window = window;
22951 help_echo_object = object;
22952 help_echo_pos = charpos;
22957 /* Look for a `pointer' property. */
22958 if (NILP (pointer))
22960 /* Check overlays first. */
22961 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22962 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22964 if (NILP (pointer))
22966 Lisp_Object object = glyph->object;
22967 int charpos = glyph->charpos;
22969 /* Try text properties. */
22970 if (STRINGP (object)
22971 && charpos >= 0
22972 && charpos < SCHARS (object))
22974 pointer = Fget_text_property (make_number (charpos),
22975 Qpointer, object);
22976 if (NILP (pointer))
22978 /* If the string itself doesn't specify a pointer,
22979 see if the buffer text ``under'' it does. */
22980 struct glyph_row *r
22981 = MATRIX_ROW (w->current_matrix, vpos);
22982 int start = MATRIX_ROW_START_CHARPOS (r);
22983 int pos = string_buffer_position (w, object, start);
22984 if (pos > 0)
22985 pointer = Fget_char_property (make_number (pos),
22986 Qpointer, w->buffer);
22989 else if (BUFFERP (object)
22990 && charpos >= BEGV
22991 && charpos < ZV)
22992 pointer = Fget_text_property (make_number (charpos),
22993 Qpointer, object);
22997 BEGV = obegv;
22998 ZV = ozv;
22999 current_buffer = obuf;
23002 set_cursor:
23004 define_frame_cursor1 (f, cursor, pointer);
23008 /* EXPORT for RIF:
23009 Clear any mouse-face on window W. This function is part of the
23010 redisplay interface, and is called from try_window_id and similar
23011 functions to ensure the mouse-highlight is off. */
23013 void
23014 x_clear_window_mouse_face (w)
23015 struct window *w;
23017 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23018 Lisp_Object window;
23020 BLOCK_INPUT;
23021 XSETWINDOW (window, w);
23022 if (EQ (window, dpyinfo->mouse_face_window))
23023 clear_mouse_face (dpyinfo);
23024 UNBLOCK_INPUT;
23028 /* EXPORT:
23029 Just discard the mouse face information for frame F, if any.
23030 This is used when the size of F is changed. */
23032 void
23033 cancel_mouse_face (f)
23034 struct frame *f;
23036 Lisp_Object window;
23037 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23039 window = dpyinfo->mouse_face_window;
23040 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23042 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23043 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23044 dpyinfo->mouse_face_window = Qnil;
23049 #endif /* HAVE_WINDOW_SYSTEM */
23052 /***********************************************************************
23053 Exposure Events
23054 ***********************************************************************/
23056 #ifdef HAVE_WINDOW_SYSTEM
23058 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23059 which intersects rectangle R. R is in window-relative coordinates. */
23061 static void
23062 expose_area (w, row, r, area)
23063 struct window *w;
23064 struct glyph_row *row;
23065 XRectangle *r;
23066 enum glyph_row_area area;
23068 struct glyph *first = row->glyphs[area];
23069 struct glyph *end = row->glyphs[area] + row->used[area];
23070 struct glyph *last;
23071 int first_x, start_x, x;
23073 if (area == TEXT_AREA && row->fill_line_p)
23074 /* If row extends face to end of line write the whole line. */
23075 draw_glyphs (w, 0, row, area,
23076 0, row->used[area],
23077 DRAW_NORMAL_TEXT, 0);
23078 else
23080 /* Set START_X to the window-relative start position for drawing glyphs of
23081 AREA. The first glyph of the text area can be partially visible.
23082 The first glyphs of other areas cannot. */
23083 start_x = window_box_left_offset (w, area);
23084 x = start_x;
23085 if (area == TEXT_AREA)
23086 x += row->x;
23088 /* Find the first glyph that must be redrawn. */
23089 while (first < end
23090 && x + first->pixel_width < r->x)
23092 x += first->pixel_width;
23093 ++first;
23096 /* Find the last one. */
23097 last = first;
23098 first_x = x;
23099 while (last < end
23100 && x < r->x + r->width)
23102 x += last->pixel_width;
23103 ++last;
23106 /* Repaint. */
23107 if (last > first)
23108 draw_glyphs (w, first_x - start_x, row, area,
23109 first - row->glyphs[area], last - row->glyphs[area],
23110 DRAW_NORMAL_TEXT, 0);
23115 /* Redraw the parts of the glyph row ROW on window W intersecting
23116 rectangle R. R is in window-relative coordinates. Value is
23117 non-zero if mouse-face was overwritten. */
23119 static int
23120 expose_line (w, row, r)
23121 struct window *w;
23122 struct glyph_row *row;
23123 XRectangle *r;
23125 xassert (row->enabled_p);
23127 if (row->mode_line_p || w->pseudo_window_p)
23128 draw_glyphs (w, 0, row, TEXT_AREA,
23129 0, row->used[TEXT_AREA],
23130 DRAW_NORMAL_TEXT, 0);
23131 else
23133 if (row->used[LEFT_MARGIN_AREA])
23134 expose_area (w, row, r, LEFT_MARGIN_AREA);
23135 if (row->used[TEXT_AREA])
23136 expose_area (w, row, r, TEXT_AREA);
23137 if (row->used[RIGHT_MARGIN_AREA])
23138 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23139 draw_row_fringe_bitmaps (w, row);
23142 return row->mouse_face_p;
23146 /* Redraw those parts of glyphs rows during expose event handling that
23147 overlap other rows. Redrawing of an exposed line writes over parts
23148 of lines overlapping that exposed line; this function fixes that.
23150 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23151 row in W's current matrix that is exposed and overlaps other rows.
23152 LAST_OVERLAPPING_ROW is the last such row. */
23154 static void
23155 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23156 struct window *w;
23157 struct glyph_row *first_overlapping_row;
23158 struct glyph_row *last_overlapping_row;
23160 struct glyph_row *row;
23162 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23163 if (row->overlapping_p)
23165 xassert (row->enabled_p && !row->mode_line_p);
23167 if (row->used[LEFT_MARGIN_AREA])
23168 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23170 if (row->used[TEXT_AREA])
23171 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23173 if (row->used[RIGHT_MARGIN_AREA])
23174 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23179 /* Return non-zero if W's cursor intersects rectangle R. */
23181 static int
23182 phys_cursor_in_rect_p (w, r)
23183 struct window *w;
23184 XRectangle *r;
23186 XRectangle cr, result;
23187 struct glyph *cursor_glyph;
23189 cursor_glyph = get_phys_cursor_glyph (w);
23190 if (cursor_glyph)
23192 /* r is relative to W's box, but w->phys_cursor.x is relative
23193 to left edge of W's TEXT area. Adjust it. */
23194 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23195 cr.y = w->phys_cursor.y;
23196 cr.width = cursor_glyph->pixel_width;
23197 cr.height = w->phys_cursor_height;
23198 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23199 I assume the effect is the same -- and this is portable. */
23200 return x_intersect_rectangles (&cr, r, &result);
23202 else
23203 return 0;
23207 /* EXPORT:
23208 Draw a vertical window border to the right of window W if W doesn't
23209 have vertical scroll bars. */
23211 void
23212 x_draw_vertical_border (w)
23213 struct window *w;
23215 /* We could do better, if we knew what type of scroll-bar the adjacent
23216 windows (on either side) have... But we don't :-(
23217 However, I think this works ok. ++KFS 2003-04-25 */
23219 /* Redraw borders between horizontally adjacent windows. Don't
23220 do it for frames with vertical scroll bars because either the
23221 right scroll bar of a window, or the left scroll bar of its
23222 neighbor will suffice as a border. */
23223 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23224 return;
23226 if (!WINDOW_RIGHTMOST_P (w)
23227 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23229 int x0, x1, y0, y1;
23231 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23232 y1 -= 1;
23234 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23235 x1 -= 1;
23237 rif->draw_vertical_window_border (w, x1, y0, y1);
23239 else if (!WINDOW_LEFTMOST_P (w)
23240 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23242 int x0, x1, y0, y1;
23244 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23245 y1 -= 1;
23247 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23248 x0 -= 1;
23250 rif->draw_vertical_window_border (w, x0, y0, y1);
23255 /* Redraw the part of window W intersection rectangle FR. Pixel
23256 coordinates in FR are frame-relative. Call this function with
23257 input blocked. Value is non-zero if the exposure overwrites
23258 mouse-face. */
23260 static int
23261 expose_window (w, fr)
23262 struct window *w;
23263 XRectangle *fr;
23265 struct frame *f = XFRAME (w->frame);
23266 XRectangle wr, r;
23267 int mouse_face_overwritten_p = 0;
23269 /* If window is not yet fully initialized, do nothing. This can
23270 happen when toolkit scroll bars are used and a window is split.
23271 Reconfiguring the scroll bar will generate an expose for a newly
23272 created window. */
23273 if (w->current_matrix == NULL)
23274 return 0;
23276 /* When we're currently updating the window, display and current
23277 matrix usually don't agree. Arrange for a thorough display
23278 later. */
23279 if (w == updated_window)
23281 SET_FRAME_GARBAGED (f);
23282 return 0;
23285 /* Frame-relative pixel rectangle of W. */
23286 wr.x = WINDOW_LEFT_EDGE_X (w);
23287 wr.y = WINDOW_TOP_EDGE_Y (w);
23288 wr.width = WINDOW_TOTAL_WIDTH (w);
23289 wr.height = WINDOW_TOTAL_HEIGHT (w);
23291 if (x_intersect_rectangles (fr, &wr, &r))
23293 int yb = window_text_bottom_y (w);
23294 struct glyph_row *row;
23295 int cursor_cleared_p;
23296 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23298 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23299 r.x, r.y, r.width, r.height));
23301 /* Convert to window coordinates. */
23302 r.x -= WINDOW_LEFT_EDGE_X (w);
23303 r.y -= WINDOW_TOP_EDGE_Y (w);
23305 /* Turn off the cursor. */
23306 if (!w->pseudo_window_p
23307 && phys_cursor_in_rect_p (w, &r))
23309 x_clear_cursor (w);
23310 cursor_cleared_p = 1;
23312 else
23313 cursor_cleared_p = 0;
23315 /* Update lines intersecting rectangle R. */
23316 first_overlapping_row = last_overlapping_row = NULL;
23317 for (row = w->current_matrix->rows;
23318 row->enabled_p;
23319 ++row)
23321 int y0 = row->y;
23322 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23324 if ((y0 >= r.y && y0 < r.y + r.height)
23325 || (y1 > r.y && y1 < r.y + r.height)
23326 || (r.y >= y0 && r.y < y1)
23327 || (r.y + r.height > y0 && r.y + r.height < y1))
23329 /* A header line may be overlapping, but there is no need
23330 to fix overlapping areas for them. KFS 2005-02-12 */
23331 if (row->overlapping_p && !row->mode_line_p)
23333 if (first_overlapping_row == NULL)
23334 first_overlapping_row = row;
23335 last_overlapping_row = row;
23338 if (expose_line (w, row, &r))
23339 mouse_face_overwritten_p = 1;
23342 if (y1 >= yb)
23343 break;
23346 /* Display the mode line if there is one. */
23347 if (WINDOW_WANTS_MODELINE_P (w)
23348 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23349 row->enabled_p)
23350 && row->y < r.y + r.height)
23352 if (expose_line (w, row, &r))
23353 mouse_face_overwritten_p = 1;
23356 if (!w->pseudo_window_p)
23358 /* Fix the display of overlapping rows. */
23359 if (first_overlapping_row)
23360 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23362 /* Draw border between windows. */
23363 x_draw_vertical_border (w);
23365 /* Turn the cursor on again. */
23366 if (cursor_cleared_p)
23367 update_window_cursor (w, 1);
23371 return mouse_face_overwritten_p;
23376 /* Redraw (parts) of all windows in the window tree rooted at W that
23377 intersect R. R contains frame pixel coordinates. Value is
23378 non-zero if the exposure overwrites mouse-face. */
23380 static int
23381 expose_window_tree (w, r)
23382 struct window *w;
23383 XRectangle *r;
23385 struct frame *f = XFRAME (w->frame);
23386 int mouse_face_overwritten_p = 0;
23388 while (w && !FRAME_GARBAGED_P (f))
23390 if (!NILP (w->hchild))
23391 mouse_face_overwritten_p
23392 |= expose_window_tree (XWINDOW (w->hchild), r);
23393 else if (!NILP (w->vchild))
23394 mouse_face_overwritten_p
23395 |= expose_window_tree (XWINDOW (w->vchild), r);
23396 else
23397 mouse_face_overwritten_p |= expose_window (w, r);
23399 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23402 return mouse_face_overwritten_p;
23406 /* EXPORT:
23407 Redisplay an exposed area of frame F. X and Y are the upper-left
23408 corner of the exposed rectangle. W and H are width and height of
23409 the exposed area. All are pixel values. W or H zero means redraw
23410 the entire frame. */
23412 void
23413 expose_frame (f, x, y, w, h)
23414 struct frame *f;
23415 int x, y, w, h;
23417 XRectangle r;
23418 int mouse_face_overwritten_p = 0;
23420 TRACE ((stderr, "expose_frame "));
23422 /* No need to redraw if frame will be redrawn soon. */
23423 if (FRAME_GARBAGED_P (f))
23425 TRACE ((stderr, " garbaged\n"));
23426 return;
23429 /* If basic faces haven't been realized yet, there is no point in
23430 trying to redraw anything. This can happen when we get an expose
23431 event while Emacs is starting, e.g. by moving another window. */
23432 if (FRAME_FACE_CACHE (f) == NULL
23433 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23435 TRACE ((stderr, " no faces\n"));
23436 return;
23439 if (w == 0 || h == 0)
23441 r.x = r.y = 0;
23442 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23443 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23445 else
23447 r.x = x;
23448 r.y = y;
23449 r.width = w;
23450 r.height = h;
23453 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23454 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23456 if (WINDOWP (f->tool_bar_window))
23457 mouse_face_overwritten_p
23458 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23460 #ifdef HAVE_X_WINDOWS
23461 #ifndef MSDOS
23462 #ifndef USE_X_TOOLKIT
23463 if (WINDOWP (f->menu_bar_window))
23464 mouse_face_overwritten_p
23465 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23466 #endif /* not USE_X_TOOLKIT */
23467 #endif
23468 #endif
23470 /* Some window managers support a focus-follows-mouse style with
23471 delayed raising of frames. Imagine a partially obscured frame,
23472 and moving the mouse into partially obscured mouse-face on that
23473 frame. The visible part of the mouse-face will be highlighted,
23474 then the WM raises the obscured frame. With at least one WM, KDE
23475 2.1, Emacs is not getting any event for the raising of the frame
23476 (even tried with SubstructureRedirectMask), only Expose events.
23477 These expose events will draw text normally, i.e. not
23478 highlighted. Which means we must redo the highlight here.
23479 Subsume it under ``we love X''. --gerd 2001-08-15 */
23480 /* Included in Windows version because Windows most likely does not
23481 do the right thing if any third party tool offers
23482 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23483 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23485 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23486 if (f == dpyinfo->mouse_face_mouse_frame)
23488 int x = dpyinfo->mouse_face_mouse_x;
23489 int y = dpyinfo->mouse_face_mouse_y;
23490 clear_mouse_face (dpyinfo);
23491 note_mouse_highlight (f, x, y);
23497 /* EXPORT:
23498 Determine the intersection of two rectangles R1 and R2. Return
23499 the intersection in *RESULT. Value is non-zero if RESULT is not
23500 empty. */
23503 x_intersect_rectangles (r1, r2, result)
23504 XRectangle *r1, *r2, *result;
23506 XRectangle *left, *right;
23507 XRectangle *upper, *lower;
23508 int intersection_p = 0;
23510 /* Rearrange so that R1 is the left-most rectangle. */
23511 if (r1->x < r2->x)
23512 left = r1, right = r2;
23513 else
23514 left = r2, right = r1;
23516 /* X0 of the intersection is right.x0, if this is inside R1,
23517 otherwise there is no intersection. */
23518 if (right->x <= left->x + left->width)
23520 result->x = right->x;
23522 /* The right end of the intersection is the minimum of the
23523 the right ends of left and right. */
23524 result->width = (min (left->x + left->width, right->x + right->width)
23525 - result->x);
23527 /* Same game for Y. */
23528 if (r1->y < r2->y)
23529 upper = r1, lower = r2;
23530 else
23531 upper = r2, lower = r1;
23533 /* The upper end of the intersection is lower.y0, if this is inside
23534 of upper. Otherwise, there is no intersection. */
23535 if (lower->y <= upper->y + upper->height)
23537 result->y = lower->y;
23539 /* The lower end of the intersection is the minimum of the lower
23540 ends of upper and lower. */
23541 result->height = (min (lower->y + lower->height,
23542 upper->y + upper->height)
23543 - result->y);
23544 intersection_p = 1;
23548 return intersection_p;
23551 #endif /* HAVE_WINDOW_SYSTEM */
23554 /***********************************************************************
23555 Initialization
23556 ***********************************************************************/
23558 void
23559 syms_of_xdisp ()
23561 Vwith_echo_area_save_vector = Qnil;
23562 staticpro (&Vwith_echo_area_save_vector);
23564 Vmessage_stack = Qnil;
23565 staticpro (&Vmessage_stack);
23567 Qinhibit_redisplay = intern ("inhibit-redisplay");
23568 staticpro (&Qinhibit_redisplay);
23570 message_dolog_marker1 = Fmake_marker ();
23571 staticpro (&message_dolog_marker1);
23572 message_dolog_marker2 = Fmake_marker ();
23573 staticpro (&message_dolog_marker2);
23574 message_dolog_marker3 = Fmake_marker ();
23575 staticpro (&message_dolog_marker3);
23577 #if GLYPH_DEBUG
23578 defsubr (&Sdump_frame_glyph_matrix);
23579 defsubr (&Sdump_glyph_matrix);
23580 defsubr (&Sdump_glyph_row);
23581 defsubr (&Sdump_tool_bar_row);
23582 defsubr (&Strace_redisplay);
23583 defsubr (&Strace_to_stderr);
23584 #endif
23585 #ifdef HAVE_WINDOW_SYSTEM
23586 defsubr (&Stool_bar_lines_needed);
23587 defsubr (&Slookup_image_map);
23588 #endif
23589 defsubr (&Sformat_mode_line);
23591 staticpro (&Qmenu_bar_update_hook);
23592 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23594 staticpro (&Qoverriding_terminal_local_map);
23595 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23597 staticpro (&Qoverriding_local_map);
23598 Qoverriding_local_map = intern ("overriding-local-map");
23600 staticpro (&Qwindow_scroll_functions);
23601 Qwindow_scroll_functions = intern ("window-scroll-functions");
23603 staticpro (&Qredisplay_end_trigger_functions);
23604 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23606 staticpro (&Qinhibit_point_motion_hooks);
23607 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23609 QCdata = intern (":data");
23610 staticpro (&QCdata);
23611 Qdisplay = intern ("display");
23612 staticpro (&Qdisplay);
23613 Qspace_width = intern ("space-width");
23614 staticpro (&Qspace_width);
23615 Qraise = intern ("raise");
23616 staticpro (&Qraise);
23617 Qslice = intern ("slice");
23618 staticpro (&Qslice);
23619 Qspace = intern ("space");
23620 staticpro (&Qspace);
23621 Qmargin = intern ("margin");
23622 staticpro (&Qmargin);
23623 Qpointer = intern ("pointer");
23624 staticpro (&Qpointer);
23625 Qleft_margin = intern ("left-margin");
23626 staticpro (&Qleft_margin);
23627 Qright_margin = intern ("right-margin");
23628 staticpro (&Qright_margin);
23629 Qcenter = intern ("center");
23630 staticpro (&Qcenter);
23631 Qline_height = intern ("line-height");
23632 staticpro (&Qline_height);
23633 QCalign_to = intern (":align-to");
23634 staticpro (&QCalign_to);
23635 QCrelative_width = intern (":relative-width");
23636 staticpro (&QCrelative_width);
23637 QCrelative_height = intern (":relative-height");
23638 staticpro (&QCrelative_height);
23639 QCeval = intern (":eval");
23640 staticpro (&QCeval);
23641 QCpropertize = intern (":propertize");
23642 staticpro (&QCpropertize);
23643 QCfile = intern (":file");
23644 staticpro (&QCfile);
23645 Qfontified = intern ("fontified");
23646 staticpro (&Qfontified);
23647 Qfontification_functions = intern ("fontification-functions");
23648 staticpro (&Qfontification_functions);
23649 Qtrailing_whitespace = intern ("trailing-whitespace");
23650 staticpro (&Qtrailing_whitespace);
23651 Qescape_glyph = intern ("escape-glyph");
23652 staticpro (&Qescape_glyph);
23653 Qnobreak_space = intern ("nobreak-space");
23654 staticpro (&Qnobreak_space);
23655 Qimage = intern ("image");
23656 staticpro (&Qimage);
23657 QCmap = intern (":map");
23658 staticpro (&QCmap);
23659 QCpointer = intern (":pointer");
23660 staticpro (&QCpointer);
23661 Qrect = intern ("rect");
23662 staticpro (&Qrect);
23663 Qcircle = intern ("circle");
23664 staticpro (&Qcircle);
23665 Qpoly = intern ("poly");
23666 staticpro (&Qpoly);
23667 Qmessage_truncate_lines = intern ("message-truncate-lines");
23668 staticpro (&Qmessage_truncate_lines);
23669 Qgrow_only = intern ("grow-only");
23670 staticpro (&Qgrow_only);
23671 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23672 staticpro (&Qinhibit_menubar_update);
23673 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23674 staticpro (&Qinhibit_eval_during_redisplay);
23675 Qposition = intern ("position");
23676 staticpro (&Qposition);
23677 Qbuffer_position = intern ("buffer-position");
23678 staticpro (&Qbuffer_position);
23679 Qobject = intern ("object");
23680 staticpro (&Qobject);
23681 Qbar = intern ("bar");
23682 staticpro (&Qbar);
23683 Qhbar = intern ("hbar");
23684 staticpro (&Qhbar);
23685 Qbox = intern ("box");
23686 staticpro (&Qbox);
23687 Qhollow = intern ("hollow");
23688 staticpro (&Qhollow);
23689 Qhand = intern ("hand");
23690 staticpro (&Qhand);
23691 Qarrow = intern ("arrow");
23692 staticpro (&Qarrow);
23693 Qtext = intern ("text");
23694 staticpro (&Qtext);
23695 Qrisky_local_variable = intern ("risky-local-variable");
23696 staticpro (&Qrisky_local_variable);
23697 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23698 staticpro (&Qinhibit_free_realized_faces);
23700 list_of_error = Fcons (Fcons (intern ("error"),
23701 Fcons (intern ("void-variable"), Qnil)),
23702 Qnil);
23703 staticpro (&list_of_error);
23705 Qlast_arrow_position = intern ("last-arrow-position");
23706 staticpro (&Qlast_arrow_position);
23707 Qlast_arrow_string = intern ("last-arrow-string");
23708 staticpro (&Qlast_arrow_string);
23710 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23711 staticpro (&Qoverlay_arrow_string);
23712 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23713 staticpro (&Qoverlay_arrow_bitmap);
23715 echo_buffer[0] = echo_buffer[1] = Qnil;
23716 staticpro (&echo_buffer[0]);
23717 staticpro (&echo_buffer[1]);
23719 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23720 staticpro (&echo_area_buffer[0]);
23721 staticpro (&echo_area_buffer[1]);
23723 Vmessages_buffer_name = build_string ("*Messages*");
23724 staticpro (&Vmessages_buffer_name);
23726 mode_line_proptrans_alist = Qnil;
23727 staticpro (&mode_line_proptrans_alist);
23728 mode_line_string_list = Qnil;
23729 staticpro (&mode_line_string_list);
23730 mode_line_string_face = Qnil;
23731 staticpro (&mode_line_string_face);
23732 mode_line_string_face_prop = Qnil;
23733 staticpro (&mode_line_string_face_prop);
23734 Vmode_line_unwind_vector = Qnil;
23735 staticpro (&Vmode_line_unwind_vector);
23737 help_echo_string = Qnil;
23738 staticpro (&help_echo_string);
23739 help_echo_object = Qnil;
23740 staticpro (&help_echo_object);
23741 help_echo_window = Qnil;
23742 staticpro (&help_echo_window);
23743 previous_help_echo_string = Qnil;
23744 staticpro (&previous_help_echo_string);
23745 help_echo_pos = -1;
23747 #ifdef HAVE_WINDOW_SYSTEM
23748 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23749 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23750 For example, if a block cursor is over a tab, it will be drawn as
23751 wide as that tab on the display. */);
23752 x_stretch_cursor_p = 0;
23753 #endif
23755 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23756 doc: /* *Non-nil means highlight trailing whitespace.
23757 The face used for trailing whitespace is `trailing-whitespace'. */);
23758 Vshow_trailing_whitespace = Qnil;
23760 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23761 doc: /* *Control highlighting of nobreak space and soft hyphen.
23762 A value of t means highlight the character itself (for nobreak space,
23763 use face `nobreak-space').
23764 A value of nil means no highlighting.
23765 Other values mean display the escape glyph followed by an ordinary
23766 space or ordinary hyphen. */);
23767 Vnobreak_char_display = Qt;
23769 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23770 doc: /* *The pointer shape to show in void text areas.
23771 A value of nil means to show the text pointer. Other options are `arrow',
23772 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23773 Vvoid_text_area_pointer = Qarrow;
23775 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23776 doc: /* Non-nil means don't actually do any redisplay.
23777 This is used for internal purposes. */);
23778 Vinhibit_redisplay = Qnil;
23780 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23781 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23782 Vglobal_mode_string = Qnil;
23784 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23785 doc: /* Marker for where to display an arrow on top of the buffer text.
23786 This must be the beginning of a line in order to work.
23787 See also `overlay-arrow-string'. */);
23788 Voverlay_arrow_position = Qnil;
23790 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23791 doc: /* String to display as an arrow in non-window frames.
23792 See also `overlay-arrow-position'. */);
23793 Voverlay_arrow_string = build_string ("=>");
23795 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23796 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23797 The symbols on this list are examined during redisplay to determine
23798 where to display overlay arrows. */);
23799 Voverlay_arrow_variable_list
23800 = Fcons (intern ("overlay-arrow-position"), Qnil);
23802 DEFVAR_INT ("scroll-step", &scroll_step,
23803 doc: /* *The number of lines to try scrolling a window by when point moves out.
23804 If that fails to bring point back on frame, point is centered instead.
23805 If this is zero, point is always centered after it moves off frame.
23806 If you want scrolling to always be a line at a time, you should set
23807 `scroll-conservatively' to a large value rather than set this to 1. */);
23809 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23810 doc: /* *Scroll up to this many lines, to bring point back on screen.
23811 A value of zero means to scroll the text to center point vertically
23812 in the window. */);
23813 scroll_conservatively = 0;
23815 DEFVAR_INT ("scroll-margin", &scroll_margin,
23816 doc: /* *Number of lines of margin at the top and bottom of a window.
23817 Recenter the window whenever point gets within this many lines
23818 of the top or bottom of the window. */);
23819 scroll_margin = 0;
23821 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23822 doc: /* Pixels per inch value for non-window system displays.
23823 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23824 Vdisplay_pixels_per_inch = make_float (72.0);
23826 #if GLYPH_DEBUG
23827 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23828 #endif
23830 DEFVAR_BOOL ("truncate-partial-width-windows",
23831 &truncate_partial_width_windows,
23832 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23833 truncate_partial_width_windows = 1;
23835 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23836 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23837 Any other value means to use the appropriate face, `mode-line',
23838 `header-line', or `menu' respectively. */);
23839 mode_line_inverse_video = 1;
23841 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23842 doc: /* *Maximum buffer size for which line number should be displayed.
23843 If the buffer is bigger than this, the line number does not appear
23844 in the mode line. A value of nil means no limit. */);
23845 Vline_number_display_limit = Qnil;
23847 DEFVAR_INT ("line-number-display-limit-width",
23848 &line_number_display_limit_width,
23849 doc: /* *Maximum line width (in characters) for line number display.
23850 If the average length of the lines near point is bigger than this, then the
23851 line number may be omitted from the mode line. */);
23852 line_number_display_limit_width = 200;
23854 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23855 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23856 highlight_nonselected_windows = 0;
23858 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23859 doc: /* Non-nil if more than one frame is visible on this display.
23860 Minibuffer-only frames don't count, but iconified frames do.
23861 This variable is not guaranteed to be accurate except while processing
23862 `frame-title-format' and `icon-title-format'. */);
23864 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23865 doc: /* Template for displaying the title bar of visible frames.
23866 \(Assuming the window manager supports this feature.)
23867 This variable has the same structure as `mode-line-format' (which see),
23868 and is used only on frames for which no explicit name has been set
23869 \(see `modify-frame-parameters'). */);
23871 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23872 doc: /* Template for displaying the title bar of an iconified frame.
23873 \(Assuming the window manager supports this feature.)
23874 This variable has the same structure as `mode-line-format' (which see),
23875 and is used only on frames for which no explicit name has been set
23876 \(see `modify-frame-parameters'). */);
23877 Vicon_title_format
23878 = Vframe_title_format
23879 = Fcons (intern ("multiple-frames"),
23880 Fcons (build_string ("%b"),
23881 Fcons (Fcons (empty_string,
23882 Fcons (intern ("invocation-name"),
23883 Fcons (build_string ("@"),
23884 Fcons (intern ("system-name"),
23885 Qnil)))),
23886 Qnil)));
23888 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23889 doc: /* Maximum number of lines to keep in the message log buffer.
23890 If nil, disable message logging. If t, log messages but don't truncate
23891 the buffer when it becomes large. */);
23892 Vmessage_log_max = make_number (50);
23894 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23895 doc: /* Functions called before redisplay, if window sizes have changed.
23896 The value should be a list of functions that take one argument.
23897 Just before redisplay, for each frame, if any of its windows have changed
23898 size since the last redisplay, or have been split or deleted,
23899 all the functions in the list are called, with the frame as argument. */);
23900 Vwindow_size_change_functions = Qnil;
23902 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23903 doc: /* List of functions to call before redisplaying a window with scrolling.
23904 Each function is called with two arguments, the window
23905 and its new display-start position. Note that the value of `window-end'
23906 is not valid when these functions are called. */);
23907 Vwindow_scroll_functions = Qnil;
23909 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23910 doc: /* Functions called when redisplay of a window reaches the end trigger.
23911 Each function is called with two arguments, the window and the end trigger value.
23912 See `set-window-redisplay-end-trigger'. */);
23913 Vredisplay_end_trigger_functions = Qnil;
23915 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23916 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23917 mouse_autoselect_window = 0;
23919 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23920 doc: /* *Non-nil means automatically resize tool-bars.
23921 This increases a tool-bar's height if not all tool-bar items are visible.
23922 It decreases a tool-bar's height when it would display blank lines
23923 otherwise. */);
23924 auto_resize_tool_bars_p = 1;
23926 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23927 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23928 auto_raise_tool_bar_buttons_p = 1;
23930 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23931 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23932 make_cursor_line_fully_visible_p = 1;
23934 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23935 doc: /* *Border below tool-bar in pixels.
23936 If an integer, use it as the height of the border.
23937 If it is one of `internal-border-width' or `border-width', use the
23938 value of the corresponding frame parameter.
23939 Otherwise, no border is added below the tool-bar. */);
23940 Vtool_bar_border = Qinternal_border_width;
23942 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23943 doc: /* *Margin around tool-bar buttons in pixels.
23944 If an integer, use that for both horizontal and vertical margins.
23945 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23946 HORZ specifying the horizontal margin, and VERT specifying the
23947 vertical margin. */);
23948 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23950 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23951 doc: /* *Relief thickness of tool-bar buttons. */);
23952 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23954 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23955 doc: /* List of functions to call to fontify regions of text.
23956 Each function is called with one argument POS. Functions must
23957 fontify a region starting at POS in the current buffer, and give
23958 fontified regions the property `fontified'. */);
23959 Vfontification_functions = Qnil;
23960 Fmake_variable_buffer_local (Qfontification_functions);
23962 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23963 &unibyte_display_via_language_environment,
23964 doc: /* *Non-nil means display unibyte text according to language environment.
23965 Specifically this means that unibyte non-ASCII characters
23966 are displayed by converting them to the equivalent multibyte characters
23967 according to the current language environment. As a result, they are
23968 displayed according to the current fontset. */);
23969 unibyte_display_via_language_environment = 0;
23971 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23972 doc: /* *Maximum height for resizing mini-windows.
23973 If a float, it specifies a fraction of the mini-window frame's height.
23974 If an integer, it specifies a number of lines. */);
23975 Vmax_mini_window_height = make_float (0.25);
23977 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23978 doc: /* *How to resize mini-windows.
23979 A value of nil means don't automatically resize mini-windows.
23980 A value of t means resize them to fit the text displayed in them.
23981 A value of `grow-only', the default, means let mini-windows grow
23982 only, until their display becomes empty, at which point the windows
23983 go back to their normal size. */);
23984 Vresize_mini_windows = Qgrow_only;
23986 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23987 doc: /* Alist specifying how to blink the cursor off.
23988 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23989 `cursor-type' frame-parameter or variable equals ON-STATE,
23990 comparing using `equal', Emacs uses OFF-STATE to specify
23991 how to blink it off. ON-STATE and OFF-STATE are values for
23992 the `cursor-type' frame parameter.
23994 If a frame's ON-STATE has no entry in this list,
23995 the frame's other specifications determine how to blink the cursor off. */);
23996 Vblink_cursor_alist = Qnil;
23998 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23999 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24000 automatic_hscrolling_p = 1;
24002 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24003 doc: /* *How many columns away from the window edge point is allowed to get
24004 before automatic hscrolling will horizontally scroll the window. */);
24005 hscroll_margin = 5;
24007 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24008 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24009 When point is less than `hscroll-margin' columns from the window
24010 edge, automatic hscrolling will scroll the window by the amount of columns
24011 determined by this variable. If its value is a positive integer, scroll that
24012 many columns. If it's a positive floating-point number, it specifies the
24013 fraction of the window's width to scroll. If it's nil or zero, point will be
24014 centered horizontally after the scroll. Any other value, including negative
24015 numbers, are treated as if the value were zero.
24017 Automatic hscrolling always moves point outside the scroll margin, so if
24018 point was more than scroll step columns inside the margin, the window will
24019 scroll more than the value given by the scroll step.
24021 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24022 and `scroll-right' overrides this variable's effect. */);
24023 Vhscroll_step = make_number (0);
24025 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24026 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24027 Bind this around calls to `message' to let it take effect. */);
24028 message_truncate_lines = 0;
24030 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24031 doc: /* Normal hook run to update the menu bar definitions.
24032 Redisplay runs this hook before it redisplays the menu bar.
24033 This is used to update submenus such as Buffers,
24034 whose contents depend on various data. */);
24035 Vmenu_bar_update_hook = Qnil;
24037 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24038 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24039 inhibit_menubar_update = 0;
24041 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24042 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24043 inhibit_eval_during_redisplay = 0;
24045 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24046 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24047 inhibit_free_realized_faces = 0;
24049 #if GLYPH_DEBUG
24050 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24051 doc: /* Inhibit try_window_id display optimization. */);
24052 inhibit_try_window_id = 0;
24054 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24055 doc: /* Inhibit try_window_reusing display optimization. */);
24056 inhibit_try_window_reusing = 0;
24058 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24059 doc: /* Inhibit try_cursor_movement display optimization. */);
24060 inhibit_try_cursor_movement = 0;
24061 #endif /* GLYPH_DEBUG */
24065 /* Initialize this module when Emacs starts. */
24067 void
24068 init_xdisp ()
24070 Lisp_Object root_window;
24071 struct window *mini_w;
24073 current_header_line_height = current_mode_line_height = -1;
24075 CHARPOS (this_line_start_pos) = 0;
24077 mini_w = XWINDOW (minibuf_window);
24078 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24080 if (!noninteractive)
24082 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24083 int i;
24085 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24086 set_window_height (root_window,
24087 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24089 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24090 set_window_height (minibuf_window, 1, 0);
24092 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24093 mini_w->total_cols = make_number (FRAME_COLS (f));
24095 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24096 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24097 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24099 /* The default ellipsis glyphs `...'. */
24100 for (i = 0; i < 3; ++i)
24101 default_invis_vector[i] = make_number ('.');
24105 /* Allocate the buffer for frame titles.
24106 Also used for `format-mode-line'. */
24107 int size = 100;
24108 mode_line_noprop_buf = (char *) xmalloc (size);
24109 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24110 mode_line_noprop_ptr = mode_line_noprop_buf;
24111 mode_line_target = MODE_LINE_DISPLAY;
24114 help_echo_showing_p = 0;
24118 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24119 (do not change this comment) */