Update to Dave Love's latest version.
[emacs.git] / src / xdisp.c
blob9911f47c2a4243a9bae7507a4c8c9be06f2f32e4
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 int update_menu_bar P_ ((struct frame *, int, 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, save_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 save_pos = it->position;
4101 it->position = *position;
4102 push_it (it);
4103 it->position = save_pos;
4105 it->area = TEXT_AREA;
4106 it->what = IT_IMAGE;
4107 it->image_id = -1; /* no image */
4108 it->position = start_pos;
4109 it->object = NILP (object) ? it->w->buffer : object;
4110 it->method = GET_FROM_IMAGE;
4111 it->face_id = face_id;
4113 /* Say that we haven't consumed the characters with
4114 `display' property yet. The call to pop_it in
4115 set_iterator_to_next will clean this up. */
4116 *position = start_pos;
4118 if (EQ (XCAR (spec), Qleft_fringe))
4120 it->left_user_fringe_bitmap = fringe_bitmap;
4121 it->left_user_fringe_face_id = face_id;
4123 else
4125 it->right_user_fringe_bitmap = fringe_bitmap;
4126 it->right_user_fringe_face_id = face_id;
4128 #endif /* HAVE_WINDOW_SYSTEM */
4129 return 1;
4132 /* Prepare to handle `((margin left-margin) ...)',
4133 `((margin right-margin) ...)' and `((margin nil) ...)'
4134 prefixes for display specifications. */
4135 location = Qunbound;
4136 if (CONSP (spec) && CONSP (XCAR (spec)))
4138 Lisp_Object tem;
4140 value = XCDR (spec);
4141 if (CONSP (value))
4142 value = XCAR (value);
4144 tem = XCAR (spec);
4145 if (EQ (XCAR (tem), Qmargin)
4146 && (tem = XCDR (tem),
4147 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4148 (NILP (tem)
4149 || EQ (tem, Qleft_margin)
4150 || EQ (tem, Qright_margin))))
4151 location = tem;
4154 if (EQ (location, Qunbound))
4156 location = Qnil;
4157 value = spec;
4160 /* After this point, VALUE is the property after any
4161 margin prefix has been stripped. It must be a string,
4162 an image specification, or `(space ...)'.
4164 LOCATION specifies where to display: `left-margin',
4165 `right-margin' or nil. */
4167 valid_p = (STRINGP (value)
4168 #ifdef HAVE_WINDOW_SYSTEM
4169 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4170 #endif /* not HAVE_WINDOW_SYSTEM */
4171 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4173 if (valid_p && !display_replaced_before_p)
4175 /* Save current settings of IT so that we can restore them
4176 when we are finished with the glyph property value. */
4177 save_pos = it->position;
4178 it->position = *position;
4179 push_it (it);
4180 it->position = save_pos;
4182 if (NILP (location))
4183 it->area = TEXT_AREA;
4184 else if (EQ (location, Qleft_margin))
4185 it->area = LEFT_MARGIN_AREA;
4186 else
4187 it->area = RIGHT_MARGIN_AREA;
4189 if (STRINGP (value))
4191 if (SCHARS (value) == 0)
4193 pop_it (it);
4194 return -1; /* Replaced by "", i.e. nothing. */
4196 it->string = value;
4197 it->multibyte_p = STRING_MULTIBYTE (it->string);
4198 it->current.overlay_string_index = -1;
4199 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4200 it->end_charpos = it->string_nchars = SCHARS (it->string);
4201 it->method = GET_FROM_STRING;
4202 it->stop_charpos = 0;
4203 it->string_from_display_prop_p = 1;
4204 /* Say that we haven't consumed the characters with
4205 `display' property yet. The call to pop_it in
4206 set_iterator_to_next will clean this up. */
4207 *position = start_pos;
4209 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4211 it->method = GET_FROM_STRETCH;
4212 it->object = value;
4213 *position = it->position = start_pos;
4215 #ifdef HAVE_WINDOW_SYSTEM
4216 else
4218 it->what = IT_IMAGE;
4219 it->image_id = lookup_image (it->f, value);
4220 it->position = start_pos;
4221 it->object = NILP (object) ? it->w->buffer : object;
4222 it->method = GET_FROM_IMAGE;
4224 /* Say that we haven't consumed the characters with
4225 `display' property yet. The call to pop_it in
4226 set_iterator_to_next will clean this up. */
4227 *position = start_pos;
4229 #endif /* HAVE_WINDOW_SYSTEM */
4231 return 1;
4234 /* Invalid property or property not supported. Restore
4235 POSITION to what it was before. */
4236 *position = start_pos;
4237 return 0;
4241 /* Check if SPEC is a display specification value whose text should be
4242 treated as intangible. */
4244 static int
4245 single_display_spec_intangible_p (prop)
4246 Lisp_Object prop;
4248 /* Skip over `when FORM'. */
4249 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4251 prop = XCDR (prop);
4252 if (!CONSP (prop))
4253 return 0;
4254 prop = XCDR (prop);
4257 if (STRINGP (prop))
4258 return 1;
4260 if (!CONSP (prop))
4261 return 0;
4263 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4264 we don't need to treat text as intangible. */
4265 if (EQ (XCAR (prop), Qmargin))
4267 prop = XCDR (prop);
4268 if (!CONSP (prop))
4269 return 0;
4271 prop = XCDR (prop);
4272 if (!CONSP (prop)
4273 || EQ (XCAR (prop), Qleft_margin)
4274 || EQ (XCAR (prop), Qright_margin))
4275 return 0;
4278 return (CONSP (prop)
4279 && (EQ (XCAR (prop), Qimage)
4280 || EQ (XCAR (prop), Qspace)));
4284 /* Check if PROP is a display property value whose text should be
4285 treated as intangible. */
4288 display_prop_intangible_p (prop)
4289 Lisp_Object prop;
4291 if (CONSP (prop)
4292 && CONSP (XCAR (prop))
4293 && !EQ (Qmargin, XCAR (XCAR (prop))))
4295 /* A list of sub-properties. */
4296 while (CONSP (prop))
4298 if (single_display_spec_intangible_p (XCAR (prop)))
4299 return 1;
4300 prop = XCDR (prop);
4303 else if (VECTORP (prop))
4305 /* A vector of sub-properties. */
4306 int i;
4307 for (i = 0; i < ASIZE (prop); ++i)
4308 if (single_display_spec_intangible_p (AREF (prop, i)))
4309 return 1;
4311 else
4312 return single_display_spec_intangible_p (prop);
4314 return 0;
4318 /* Return 1 if PROP is a display sub-property value containing STRING. */
4320 static int
4321 single_display_spec_string_p (prop, string)
4322 Lisp_Object prop, string;
4324 if (EQ (string, prop))
4325 return 1;
4327 /* Skip over `when FORM'. */
4328 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4330 prop = XCDR (prop);
4331 if (!CONSP (prop))
4332 return 0;
4333 prop = XCDR (prop);
4336 if (CONSP (prop))
4337 /* Skip over `margin LOCATION'. */
4338 if (EQ (XCAR (prop), Qmargin))
4340 prop = XCDR (prop);
4341 if (!CONSP (prop))
4342 return 0;
4344 prop = XCDR (prop);
4345 if (!CONSP (prop))
4346 return 0;
4349 return CONSP (prop) && EQ (XCAR (prop), string);
4353 /* Return 1 if STRING appears in the `display' property PROP. */
4355 static int
4356 display_prop_string_p (prop, string)
4357 Lisp_Object prop, string;
4359 if (CONSP (prop)
4360 && CONSP (XCAR (prop))
4361 && !EQ (Qmargin, XCAR (XCAR (prop))))
4363 /* A list of sub-properties. */
4364 while (CONSP (prop))
4366 if (single_display_spec_string_p (XCAR (prop), string))
4367 return 1;
4368 prop = XCDR (prop);
4371 else if (VECTORP (prop))
4373 /* A vector of sub-properties. */
4374 int i;
4375 for (i = 0; i < ASIZE (prop); ++i)
4376 if (single_display_spec_string_p (AREF (prop, i), string))
4377 return 1;
4379 else
4380 return single_display_spec_string_p (prop, string);
4382 return 0;
4386 /* Determine from which buffer position in W's buffer STRING comes
4387 from. AROUND_CHARPOS is an approximate position where it could
4388 be from. Value is the buffer position or 0 if it couldn't be
4389 determined.
4391 W's buffer must be current.
4393 This function is necessary because we don't record buffer positions
4394 in glyphs generated from strings (to keep struct glyph small).
4395 This function may only use code that doesn't eval because it is
4396 called asynchronously from note_mouse_highlight. */
4399 string_buffer_position (w, string, around_charpos)
4400 struct window *w;
4401 Lisp_Object string;
4402 int around_charpos;
4404 Lisp_Object limit, prop, pos;
4405 const int MAX_DISTANCE = 1000;
4406 int found = 0;
4408 pos = make_number (around_charpos);
4409 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4410 while (!found && !EQ (pos, limit))
4412 prop = Fget_char_property (pos, Qdisplay, Qnil);
4413 if (!NILP (prop) && display_prop_string_p (prop, string))
4414 found = 1;
4415 else
4416 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4419 if (!found)
4421 pos = make_number (around_charpos);
4422 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4423 while (!found && !EQ (pos, limit))
4425 prop = Fget_char_property (pos, Qdisplay, Qnil);
4426 if (!NILP (prop) && display_prop_string_p (prop, string))
4427 found = 1;
4428 else
4429 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4430 limit);
4434 return found ? XINT (pos) : 0;
4439 /***********************************************************************
4440 `composition' property
4441 ***********************************************************************/
4443 /* Set up iterator IT from `composition' property at its current
4444 position. Called from handle_stop. */
4446 static enum prop_handled
4447 handle_composition_prop (it)
4448 struct it *it;
4450 Lisp_Object prop, string;
4451 int pos, pos_byte, end;
4452 enum prop_handled handled = HANDLED_NORMALLY;
4454 if (STRINGP (it->string))
4456 pos = IT_STRING_CHARPOS (*it);
4457 pos_byte = IT_STRING_BYTEPOS (*it);
4458 string = it->string;
4460 else
4462 pos = IT_CHARPOS (*it);
4463 pos_byte = IT_BYTEPOS (*it);
4464 string = Qnil;
4467 /* If there's a valid composition and point is not inside of the
4468 composition (in the case that the composition is from the current
4469 buffer), draw a glyph composed from the composition components. */
4470 if (find_composition (pos, -1, &pos, &end, &prop, string)
4471 && COMPOSITION_VALID_P (pos, end, prop)
4472 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4474 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4476 if (id >= 0)
4478 struct composition *cmp = composition_table[id];
4480 if (cmp->glyph_len == 0)
4482 /* No glyph. */
4483 if (STRINGP (it->string))
4485 IT_STRING_CHARPOS (*it) = end;
4486 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4487 end);
4489 else
4491 IT_CHARPOS (*it) = end;
4492 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4494 return HANDLED_RECOMPUTE_PROPS;
4497 it->stop_charpos = end;
4498 push_it (it);
4500 it->method = GET_FROM_COMPOSITION;
4501 it->cmp_id = id;
4502 it->cmp_len = COMPOSITION_LENGTH (prop);
4503 /* For a terminal, draw only the first character of the
4504 components. */
4505 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4506 it->len = (STRINGP (it->string)
4507 ? string_char_to_byte (it->string, end)
4508 : CHAR_TO_BYTE (end)) - pos_byte;
4509 handled = HANDLED_RETURN;
4513 return handled;
4518 /***********************************************************************
4519 Overlay strings
4520 ***********************************************************************/
4522 /* The following structure is used to record overlay strings for
4523 later sorting in load_overlay_strings. */
4525 struct overlay_entry
4527 Lisp_Object overlay;
4528 Lisp_Object string;
4529 int priority;
4530 int after_string_p;
4534 /* Set up iterator IT from overlay strings at its current position.
4535 Called from handle_stop. */
4537 static enum prop_handled
4538 handle_overlay_change (it)
4539 struct it *it;
4541 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4542 return HANDLED_RECOMPUTE_PROPS;
4543 else
4544 return HANDLED_NORMALLY;
4548 /* Set up the next overlay string for delivery by IT, if there is an
4549 overlay string to deliver. Called by set_iterator_to_next when the
4550 end of the current overlay string is reached. If there are more
4551 overlay strings to display, IT->string and
4552 IT->current.overlay_string_index are set appropriately here.
4553 Otherwise IT->string is set to nil. */
4555 static void
4556 next_overlay_string (it)
4557 struct it *it;
4559 ++it->current.overlay_string_index;
4560 if (it->current.overlay_string_index == it->n_overlay_strings)
4562 /* No more overlay strings. Restore IT's settings to what
4563 they were before overlay strings were processed, and
4564 continue to deliver from current_buffer. */
4565 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4567 pop_it (it);
4568 xassert (it->sp > 0
4569 || it->method == GET_FROM_COMPOSITION
4570 || (NILP (it->string)
4571 && it->method == GET_FROM_BUFFER
4572 && it->stop_charpos >= BEGV
4573 && it->stop_charpos <= it->end_charpos));
4574 it->current.overlay_string_index = -1;
4575 it->n_overlay_strings = 0;
4577 /* If we're at the end of the buffer, record that we have
4578 processed the overlay strings there already, so that
4579 next_element_from_buffer doesn't try it again. */
4580 if (IT_CHARPOS (*it) >= it->end_charpos)
4581 it->overlay_strings_at_end_processed_p = 1;
4583 /* If we have to display `...' for invisible text, set
4584 the iterator up for that. */
4585 if (display_ellipsis_p)
4586 setup_for_ellipsis (it, 0);
4588 else
4590 /* There are more overlay strings to process. If
4591 IT->current.overlay_string_index has advanced to a position
4592 where we must load IT->overlay_strings with more strings, do
4593 it. */
4594 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4596 if (it->current.overlay_string_index && i == 0)
4597 load_overlay_strings (it, 0);
4599 /* Initialize IT to deliver display elements from the overlay
4600 string. */
4601 it->string = it->overlay_strings[i];
4602 it->multibyte_p = STRING_MULTIBYTE (it->string);
4603 SET_TEXT_POS (it->current.string_pos, 0, 0);
4604 it->method = GET_FROM_STRING;
4605 it->stop_charpos = 0;
4608 CHECK_IT (it);
4612 /* Compare two overlay_entry structures E1 and E2. Used as a
4613 comparison function for qsort in load_overlay_strings. Overlay
4614 strings for the same position are sorted so that
4616 1. All after-strings come in front of before-strings, except
4617 when they come from the same overlay.
4619 2. Within after-strings, strings are sorted so that overlay strings
4620 from overlays with higher priorities come first.
4622 2. Within before-strings, strings are sorted so that overlay
4623 strings from overlays with higher priorities come last.
4625 Value is analogous to strcmp. */
4628 static int
4629 compare_overlay_entries (e1, e2)
4630 void *e1, *e2;
4632 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4633 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4634 int result;
4636 if (entry1->after_string_p != entry2->after_string_p)
4638 /* Let after-strings appear in front of before-strings if
4639 they come from different overlays. */
4640 if (EQ (entry1->overlay, entry2->overlay))
4641 result = entry1->after_string_p ? 1 : -1;
4642 else
4643 result = entry1->after_string_p ? -1 : 1;
4645 else if (entry1->after_string_p)
4646 /* After-strings sorted in order of decreasing priority. */
4647 result = entry2->priority - entry1->priority;
4648 else
4649 /* Before-strings sorted in order of increasing priority. */
4650 result = entry1->priority - entry2->priority;
4652 return result;
4656 /* Load the vector IT->overlay_strings with overlay strings from IT's
4657 current buffer position, or from CHARPOS if that is > 0. Set
4658 IT->n_overlays to the total number of overlay strings found.
4660 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4661 a time. On entry into load_overlay_strings,
4662 IT->current.overlay_string_index gives the number of overlay
4663 strings that have already been loaded by previous calls to this
4664 function.
4666 IT->add_overlay_start contains an additional overlay start
4667 position to consider for taking overlay strings from, if non-zero.
4668 This position comes into play when the overlay has an `invisible'
4669 property, and both before and after-strings. When we've skipped to
4670 the end of the overlay, because of its `invisible' property, we
4671 nevertheless want its before-string to appear.
4672 IT->add_overlay_start will contain the overlay start position
4673 in this case.
4675 Overlay strings are sorted so that after-string strings come in
4676 front of before-string strings. Within before and after-strings,
4677 strings are sorted by overlay priority. See also function
4678 compare_overlay_entries. */
4680 static void
4681 load_overlay_strings (it, charpos)
4682 struct it *it;
4683 int charpos;
4685 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4686 Lisp_Object overlay, window, str, invisible;
4687 struct Lisp_Overlay *ov;
4688 int start, end;
4689 int size = 20;
4690 int n = 0, i, j, invis_p;
4691 struct overlay_entry *entries
4692 = (struct overlay_entry *) alloca (size * sizeof *entries);
4694 if (charpos <= 0)
4695 charpos = IT_CHARPOS (*it);
4697 /* Append the overlay string STRING of overlay OVERLAY to vector
4698 `entries' which has size `size' and currently contains `n'
4699 elements. AFTER_P non-zero means STRING is an after-string of
4700 OVERLAY. */
4701 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4702 do \
4704 Lisp_Object priority; \
4706 if (n == size) \
4708 int new_size = 2 * size; \
4709 struct overlay_entry *old = entries; \
4710 entries = \
4711 (struct overlay_entry *) alloca (new_size \
4712 * sizeof *entries); \
4713 bcopy (old, entries, size * sizeof *entries); \
4714 size = new_size; \
4717 entries[n].string = (STRING); \
4718 entries[n].overlay = (OVERLAY); \
4719 priority = Foverlay_get ((OVERLAY), Qpriority); \
4720 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4721 entries[n].after_string_p = (AFTER_P); \
4722 ++n; \
4724 while (0)
4726 /* Process overlay before the overlay center. */
4727 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4729 XSETMISC (overlay, ov);
4730 xassert (OVERLAYP (overlay));
4731 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4732 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4734 if (end < charpos)
4735 break;
4737 /* Skip this overlay if it doesn't start or end at IT's current
4738 position. */
4739 if (end != charpos && start != charpos)
4740 continue;
4742 /* Skip this overlay if it doesn't apply to IT->w. */
4743 window = Foverlay_get (overlay, Qwindow);
4744 if (WINDOWP (window) && XWINDOW (window) != it->w)
4745 continue;
4747 /* If the text ``under'' the overlay is invisible, both before-
4748 and after-strings from this overlay are visible; start and
4749 end position are indistinguishable. */
4750 invisible = Foverlay_get (overlay, Qinvisible);
4751 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4753 /* If overlay has a non-empty before-string, record it. */
4754 if ((start == charpos || (end == charpos && invis_p))
4755 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4756 && SCHARS (str))
4757 RECORD_OVERLAY_STRING (overlay, str, 0);
4759 /* If overlay has a non-empty after-string, record it. */
4760 if ((end == charpos || (start == charpos && invis_p))
4761 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4762 && SCHARS (str))
4763 RECORD_OVERLAY_STRING (overlay, str, 1);
4766 /* Process overlays after the overlay center. */
4767 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4769 XSETMISC (overlay, ov);
4770 xassert (OVERLAYP (overlay));
4771 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4772 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4774 if (start > charpos)
4775 break;
4777 /* Skip this overlay if it doesn't start or end at IT's current
4778 position. */
4779 if (end != charpos && start != charpos)
4780 continue;
4782 /* Skip this overlay if it doesn't apply to IT->w. */
4783 window = Foverlay_get (overlay, Qwindow);
4784 if (WINDOWP (window) && XWINDOW (window) != it->w)
4785 continue;
4787 /* If the text ``under'' the overlay is invisible, it has a zero
4788 dimension, and both before- and after-strings apply. */
4789 invisible = Foverlay_get (overlay, Qinvisible);
4790 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4792 /* If overlay has a non-empty before-string, record it. */
4793 if ((start == charpos || (end == charpos && invis_p))
4794 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4795 && SCHARS (str))
4796 RECORD_OVERLAY_STRING (overlay, str, 0);
4798 /* If overlay has a non-empty after-string, record it. */
4799 if ((end == charpos || (start == charpos && invis_p))
4800 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4801 && SCHARS (str))
4802 RECORD_OVERLAY_STRING (overlay, str, 1);
4805 #undef RECORD_OVERLAY_STRING
4807 /* Sort entries. */
4808 if (n > 1)
4809 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4811 /* Record the total number of strings to process. */
4812 it->n_overlay_strings = n;
4814 /* IT->current.overlay_string_index is the number of overlay strings
4815 that have already been consumed by IT. Copy some of the
4816 remaining overlay strings to IT->overlay_strings. */
4817 i = 0;
4818 j = it->current.overlay_string_index;
4819 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4820 it->overlay_strings[i++] = entries[j++].string;
4822 CHECK_IT (it);
4826 /* Get the first chunk of overlay strings at IT's current buffer
4827 position, or at CHARPOS if that is > 0. Value is non-zero if at
4828 least one overlay string was found. */
4830 static int
4831 get_overlay_strings_1 (it, charpos, compute_stop_p)
4832 struct it *it;
4833 int charpos;
4835 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4836 process. This fills IT->overlay_strings with strings, and sets
4837 IT->n_overlay_strings to the total number of strings to process.
4838 IT->pos.overlay_string_index has to be set temporarily to zero
4839 because load_overlay_strings needs this; it must be set to -1
4840 when no overlay strings are found because a zero value would
4841 indicate a position in the first overlay string. */
4842 it->current.overlay_string_index = 0;
4843 load_overlay_strings (it, charpos);
4845 /* If we found overlay strings, set up IT to deliver display
4846 elements from the first one. Otherwise set up IT to deliver
4847 from current_buffer. */
4848 if (it->n_overlay_strings)
4850 /* Make sure we know settings in current_buffer, so that we can
4851 restore meaningful values when we're done with the overlay
4852 strings. */
4853 if (compute_stop_p)
4854 compute_stop_pos (it);
4855 xassert (it->face_id >= 0);
4857 /* Save IT's settings. They are restored after all overlay
4858 strings have been processed. */
4859 xassert (!compute_stop_p || it->sp == 0);
4860 push_it (it);
4862 /* Set up IT to deliver display elements from the first overlay
4863 string. */
4864 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4865 it->string = it->overlay_strings[0];
4866 it->stop_charpos = 0;
4867 xassert (STRINGP (it->string));
4868 it->end_charpos = SCHARS (it->string);
4869 it->multibyte_p = STRING_MULTIBYTE (it->string);
4870 it->method = GET_FROM_STRING;
4871 return 1;
4874 it->current.overlay_string_index = -1;
4875 return 0;
4878 static int
4879 get_overlay_strings (it, charpos)
4880 struct it *it;
4881 int charpos;
4883 it->string = Qnil;
4884 it->method = GET_FROM_BUFFER;
4886 (void) get_overlay_strings_1 (it, charpos, 1);
4888 CHECK_IT (it);
4890 /* Value is non-zero if we found at least one overlay string. */
4891 return STRINGP (it->string);
4896 /***********************************************************************
4897 Saving and restoring state
4898 ***********************************************************************/
4900 /* Save current settings of IT on IT->stack. Called, for example,
4901 before setting up IT for an overlay string, to be able to restore
4902 IT's settings to what they were after the overlay string has been
4903 processed. */
4905 static void
4906 push_it (it)
4907 struct it *it;
4909 struct iterator_stack_entry *p;
4911 xassert (it->sp < IT_STACK_SIZE);
4912 p = it->stack + it->sp;
4914 p->stop_charpos = it->stop_charpos;
4915 xassert (it->face_id >= 0);
4916 p->face_id = it->face_id;
4917 p->string = it->string;
4918 p->method = it->method;
4919 switch (p->method)
4921 case GET_FROM_IMAGE:
4922 p->u.image.object = it->object;
4923 p->u.image.image_id = it->image_id;
4924 p->u.image.slice = it->slice;
4925 break;
4926 case GET_FROM_COMPOSITION:
4927 p->u.comp.object = it->object;
4928 p->u.comp.c = it->c;
4929 p->u.comp.len = it->len;
4930 p->u.comp.cmp_id = it->cmp_id;
4931 p->u.comp.cmp_len = it->cmp_len;
4932 break;
4933 case GET_FROM_STRETCH:
4934 p->u.stretch.object = it->object;
4935 break;
4937 p->position = it->position;
4938 p->current = it->current;
4939 p->end_charpos = it->end_charpos;
4940 p->string_nchars = it->string_nchars;
4941 p->area = it->area;
4942 p->multibyte_p = it->multibyte_p;
4943 p->space_width = it->space_width;
4944 p->font_height = it->font_height;
4945 p->voffset = it->voffset;
4946 p->string_from_display_prop_p = it->string_from_display_prop_p;
4947 p->display_ellipsis_p = 0;
4948 ++it->sp;
4952 /* Restore IT's settings from IT->stack. Called, for example, when no
4953 more overlay strings must be processed, and we return to delivering
4954 display elements from a buffer, or when the end of a string from a
4955 `display' property is reached and we return to delivering display
4956 elements from an overlay string, or from a buffer. */
4958 static void
4959 pop_it (it)
4960 struct it *it;
4962 struct iterator_stack_entry *p;
4964 xassert (it->sp > 0);
4965 --it->sp;
4966 p = it->stack + it->sp;
4967 it->stop_charpos = p->stop_charpos;
4968 it->face_id = p->face_id;
4969 it->current = p->current;
4970 it->position = p->position;
4971 it->string = p->string;
4972 if (NILP (it->string))
4973 SET_TEXT_POS (it->current.string_pos, -1, -1);
4974 it->method = p->method;
4975 switch (it->method)
4977 case GET_FROM_IMAGE:
4978 it->image_id = p->u.image.image_id;
4979 it->object = p->u.image.object;
4980 it->slice = p->u.image.slice;
4981 break;
4982 case GET_FROM_COMPOSITION:
4983 it->object = p->u.comp.object;
4984 it->c = p->u.comp.c;
4985 it->len = p->u.comp.len;
4986 it->cmp_id = p->u.comp.cmp_id;
4987 it->cmp_len = p->u.comp.cmp_len;
4988 break;
4989 case GET_FROM_STRETCH:
4990 it->object = p->u.comp.object;
4991 break;
4992 case GET_FROM_BUFFER:
4993 it->object = it->w->buffer;
4994 break;
4995 case GET_FROM_STRING:
4996 it->object = it->string;
4997 break;
4999 it->end_charpos = p->end_charpos;
5000 it->string_nchars = p->string_nchars;
5001 it->area = p->area;
5002 it->multibyte_p = p->multibyte_p;
5003 it->space_width = p->space_width;
5004 it->font_height = p->font_height;
5005 it->voffset = p->voffset;
5006 it->string_from_display_prop_p = p->string_from_display_prop_p;
5011 /***********************************************************************
5012 Moving over lines
5013 ***********************************************************************/
5015 /* Set IT's current position to the previous line start. */
5017 static void
5018 back_to_previous_line_start (it)
5019 struct it *it;
5021 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5022 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5026 /* Move IT to the next line start.
5028 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5029 we skipped over part of the text (as opposed to moving the iterator
5030 continuously over the text). Otherwise, don't change the value
5031 of *SKIPPED_P.
5033 Newlines may come from buffer text, overlay strings, or strings
5034 displayed via the `display' property. That's the reason we can't
5035 simply use find_next_newline_no_quit.
5037 Note that this function may not skip over invisible text that is so
5038 because of text properties and immediately follows a newline. If
5039 it would, function reseat_at_next_visible_line_start, when called
5040 from set_iterator_to_next, would effectively make invisible
5041 characters following a newline part of the wrong glyph row, which
5042 leads to wrong cursor motion. */
5044 static int
5045 forward_to_next_line_start (it, skipped_p)
5046 struct it *it;
5047 int *skipped_p;
5049 int old_selective, newline_found_p, n;
5050 const int MAX_NEWLINE_DISTANCE = 500;
5052 /* If already on a newline, just consume it to avoid unintended
5053 skipping over invisible text below. */
5054 if (it->what == IT_CHARACTER
5055 && it->c == '\n'
5056 && CHARPOS (it->position) == IT_CHARPOS (*it))
5058 set_iterator_to_next (it, 0);
5059 it->c = 0;
5060 return 1;
5063 /* Don't handle selective display in the following. It's (a)
5064 unnecessary because it's done by the caller, and (b) leads to an
5065 infinite recursion because next_element_from_ellipsis indirectly
5066 calls this function. */
5067 old_selective = it->selective;
5068 it->selective = 0;
5070 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5071 from buffer text. */
5072 for (n = newline_found_p = 0;
5073 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5074 n += STRINGP (it->string) ? 0 : 1)
5076 if (!get_next_display_element (it))
5077 return 0;
5078 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5079 set_iterator_to_next (it, 0);
5082 /* If we didn't find a newline near enough, see if we can use a
5083 short-cut. */
5084 if (!newline_found_p)
5086 int start = IT_CHARPOS (*it);
5087 int limit = find_next_newline_no_quit (start, 1);
5088 Lisp_Object pos;
5090 xassert (!STRINGP (it->string));
5092 /* If there isn't any `display' property in sight, and no
5093 overlays, we can just use the position of the newline in
5094 buffer text. */
5095 if (it->stop_charpos >= limit
5096 || ((pos = Fnext_single_property_change (make_number (start),
5097 Qdisplay,
5098 Qnil, make_number (limit)),
5099 NILP (pos))
5100 && next_overlay_change (start) == ZV))
5102 IT_CHARPOS (*it) = limit;
5103 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5104 *skipped_p = newline_found_p = 1;
5106 else
5108 while (get_next_display_element (it)
5109 && !newline_found_p)
5111 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5112 set_iterator_to_next (it, 0);
5117 it->selective = old_selective;
5118 return newline_found_p;
5122 /* Set IT's current position to the previous visible line start. Skip
5123 invisible text that is so either due to text properties or due to
5124 selective display. Caution: this does not change IT->current_x and
5125 IT->hpos. */
5127 static void
5128 back_to_previous_visible_line_start (it)
5129 struct it *it;
5131 while (IT_CHARPOS (*it) > BEGV)
5133 back_to_previous_line_start (it);
5135 if (IT_CHARPOS (*it) <= BEGV)
5136 break;
5138 /* If selective > 0, then lines indented more than that values
5139 are invisible. */
5140 if (it->selective > 0
5141 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5142 (double) it->selective)) /* iftc */
5143 continue;
5145 /* Check the newline before point for invisibility. */
5147 Lisp_Object prop;
5148 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5149 Qinvisible, it->window);
5150 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5151 continue;
5154 if (IT_CHARPOS (*it) <= BEGV)
5155 break;
5158 struct it it2;
5159 int pos;
5160 int beg, end;
5161 Lisp_Object val, overlay;
5163 /* If newline is part of a composition, continue from start of composition */
5164 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5165 && beg < IT_CHARPOS (*it))
5166 goto replaced;
5168 /* If newline is replaced by a display property, find start of overlay
5169 or interval and continue search from that point. */
5170 it2 = *it;
5171 pos = --IT_CHARPOS (it2);
5172 --IT_BYTEPOS (it2);
5173 it2.sp = 0;
5174 if (handle_display_prop (&it2) == HANDLED_RETURN
5175 && !NILP (val = get_char_property_and_overlay
5176 (make_number (pos), Qdisplay, Qnil, &overlay))
5177 && (OVERLAYP (overlay)
5178 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5179 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5180 goto replaced;
5182 /* Newline is not replaced by anything -- so we are done. */
5183 break;
5185 replaced:
5186 if (beg < BEGV)
5187 beg = BEGV;
5188 IT_CHARPOS (*it) = beg;
5189 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5193 it->continuation_lines_width = 0;
5195 xassert (IT_CHARPOS (*it) >= BEGV);
5196 xassert (IT_CHARPOS (*it) == BEGV
5197 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5198 CHECK_IT (it);
5202 /* Reseat iterator IT at the previous visible line start. Skip
5203 invisible text that is so either due to text properties or due to
5204 selective display. At the end, update IT's overlay information,
5205 face information etc. */
5207 void
5208 reseat_at_previous_visible_line_start (it)
5209 struct it *it;
5211 back_to_previous_visible_line_start (it);
5212 reseat (it, it->current.pos, 1);
5213 CHECK_IT (it);
5217 /* Reseat iterator IT on the next visible line start in the current
5218 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5219 preceding the line start. Skip over invisible text that is so
5220 because of selective display. Compute faces, overlays etc at the
5221 new position. Note that this function does not skip over text that
5222 is invisible because of text properties. */
5224 static void
5225 reseat_at_next_visible_line_start (it, on_newline_p)
5226 struct it *it;
5227 int on_newline_p;
5229 int newline_found_p, skipped_p = 0;
5231 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5233 /* Skip over lines that are invisible because they are indented
5234 more than the value of IT->selective. */
5235 if (it->selective > 0)
5236 while (IT_CHARPOS (*it) < ZV
5237 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5238 (double) it->selective)) /* iftc */
5240 xassert (IT_BYTEPOS (*it) == BEGV
5241 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5242 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5245 /* Position on the newline if that's what's requested. */
5246 if (on_newline_p && newline_found_p)
5248 if (STRINGP (it->string))
5250 if (IT_STRING_CHARPOS (*it) > 0)
5252 --IT_STRING_CHARPOS (*it);
5253 --IT_STRING_BYTEPOS (*it);
5256 else if (IT_CHARPOS (*it) > BEGV)
5258 --IT_CHARPOS (*it);
5259 --IT_BYTEPOS (*it);
5260 reseat (it, it->current.pos, 0);
5263 else if (skipped_p)
5264 reseat (it, it->current.pos, 0);
5266 CHECK_IT (it);
5271 /***********************************************************************
5272 Changing an iterator's position
5273 ***********************************************************************/
5275 /* Change IT's current position to POS in current_buffer. If FORCE_P
5276 is non-zero, always check for text properties at the new position.
5277 Otherwise, text properties are only looked up if POS >=
5278 IT->check_charpos of a property. */
5280 static void
5281 reseat (it, pos, force_p)
5282 struct it *it;
5283 struct text_pos pos;
5284 int force_p;
5286 int original_pos = IT_CHARPOS (*it);
5288 reseat_1 (it, pos, 0);
5290 /* Determine where to check text properties. Avoid doing it
5291 where possible because text property lookup is very expensive. */
5292 if (force_p
5293 || CHARPOS (pos) > it->stop_charpos
5294 || CHARPOS (pos) < original_pos)
5295 handle_stop (it);
5297 CHECK_IT (it);
5301 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5302 IT->stop_pos to POS, also. */
5304 static void
5305 reseat_1 (it, pos, set_stop_p)
5306 struct it *it;
5307 struct text_pos pos;
5308 int set_stop_p;
5310 /* Don't call this function when scanning a C string. */
5311 xassert (it->s == NULL);
5313 /* POS must be a reasonable value. */
5314 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5316 it->current.pos = it->position = pos;
5317 it->end_charpos = ZV;
5318 it->dpvec = NULL;
5319 it->current.dpvec_index = -1;
5320 it->current.overlay_string_index = -1;
5321 IT_STRING_CHARPOS (*it) = -1;
5322 IT_STRING_BYTEPOS (*it) = -1;
5323 it->string = Qnil;
5324 it->method = GET_FROM_BUFFER;
5325 it->object = it->w->buffer;
5326 it->area = TEXT_AREA;
5327 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5328 it->sp = 0;
5329 it->string_from_display_prop_p = 0;
5330 it->face_before_selective_p = 0;
5332 if (set_stop_p)
5333 it->stop_charpos = CHARPOS (pos);
5337 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5338 If S is non-null, it is a C string to iterate over. Otherwise,
5339 STRING gives a Lisp string to iterate over.
5341 If PRECISION > 0, don't return more then PRECISION number of
5342 characters from the string.
5344 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5345 characters have been returned. FIELD_WIDTH < 0 means an infinite
5346 field width.
5348 MULTIBYTE = 0 means disable processing of multibyte characters,
5349 MULTIBYTE > 0 means enable it,
5350 MULTIBYTE < 0 means use IT->multibyte_p.
5352 IT must be initialized via a prior call to init_iterator before
5353 calling this function. */
5355 static void
5356 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5357 struct it *it;
5358 unsigned char *s;
5359 Lisp_Object string;
5360 int charpos;
5361 int precision, field_width, multibyte;
5363 /* No region in strings. */
5364 it->region_beg_charpos = it->region_end_charpos = -1;
5366 /* No text property checks performed by default, but see below. */
5367 it->stop_charpos = -1;
5369 /* Set iterator position and end position. */
5370 bzero (&it->current, sizeof it->current);
5371 it->current.overlay_string_index = -1;
5372 it->current.dpvec_index = -1;
5373 xassert (charpos >= 0);
5375 /* If STRING is specified, use its multibyteness, otherwise use the
5376 setting of MULTIBYTE, if specified. */
5377 if (multibyte >= 0)
5378 it->multibyte_p = multibyte > 0;
5380 if (s == NULL)
5382 xassert (STRINGP (string));
5383 it->string = string;
5384 it->s = NULL;
5385 it->end_charpos = it->string_nchars = SCHARS (string);
5386 it->method = GET_FROM_STRING;
5387 it->current.string_pos = string_pos (charpos, string);
5389 else
5391 it->s = s;
5392 it->string = Qnil;
5394 /* Note that we use IT->current.pos, not it->current.string_pos,
5395 for displaying C strings. */
5396 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5397 if (it->multibyte_p)
5399 it->current.pos = c_string_pos (charpos, s, 1);
5400 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5402 else
5404 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5405 it->end_charpos = it->string_nchars = strlen (s);
5408 it->method = GET_FROM_C_STRING;
5411 /* PRECISION > 0 means don't return more than PRECISION characters
5412 from the string. */
5413 if (precision > 0 && it->end_charpos - charpos > precision)
5414 it->end_charpos = it->string_nchars = charpos + precision;
5416 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5417 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5418 FIELD_WIDTH < 0 means infinite field width. This is useful for
5419 padding with `-' at the end of a mode line. */
5420 if (field_width < 0)
5421 field_width = INFINITY;
5422 if (field_width > it->end_charpos - charpos)
5423 it->end_charpos = charpos + field_width;
5425 /* Use the standard display table for displaying strings. */
5426 if (DISP_TABLE_P (Vstandard_display_table))
5427 it->dp = XCHAR_TABLE (Vstandard_display_table);
5429 it->stop_charpos = charpos;
5430 CHECK_IT (it);
5435 /***********************************************************************
5436 Iteration
5437 ***********************************************************************/
5439 /* Map enum it_method value to corresponding next_element_from_* function. */
5441 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5443 next_element_from_buffer,
5444 next_element_from_display_vector,
5445 next_element_from_composition,
5446 next_element_from_string,
5447 next_element_from_c_string,
5448 next_element_from_image,
5449 next_element_from_stretch
5453 /* Load IT's display element fields with information about the next
5454 display element from the current position of IT. Value is zero if
5455 end of buffer (or C string) is reached. */
5457 static struct frame *last_escape_glyph_frame = NULL;
5458 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5459 static int last_escape_glyph_merged_face_id = 0;
5462 get_next_display_element (it)
5463 struct it *it;
5465 /* Non-zero means that we found a display element. Zero means that
5466 we hit the end of what we iterate over. Performance note: the
5467 function pointer `method' used here turns out to be faster than
5468 using a sequence of if-statements. */
5469 int success_p;
5471 get_next:
5472 success_p = (*get_next_element[it->method]) (it);
5474 if (it->what == IT_CHARACTER)
5476 /* Map via display table or translate control characters.
5477 IT->c, IT->len etc. have been set to the next character by
5478 the function call above. If we have a display table, and it
5479 contains an entry for IT->c, translate it. Don't do this if
5480 IT->c itself comes from a display table, otherwise we could
5481 end up in an infinite recursion. (An alternative could be to
5482 count the recursion depth of this function and signal an
5483 error when a certain maximum depth is reached.) Is it worth
5484 it? */
5485 if (success_p && it->dpvec == NULL)
5487 Lisp_Object dv;
5489 if (it->dp
5490 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5491 VECTORP (dv)))
5493 struct Lisp_Vector *v = XVECTOR (dv);
5495 /* Return the first character from the display table
5496 entry, if not empty. If empty, don't display the
5497 current character. */
5498 if (v->size)
5500 it->dpvec_char_len = it->len;
5501 it->dpvec = v->contents;
5502 it->dpend = v->contents + v->size;
5503 it->current.dpvec_index = 0;
5504 it->dpvec_face_id = -1;
5505 it->saved_face_id = it->face_id;
5506 it->method = GET_FROM_DISPLAY_VECTOR;
5507 it->ellipsis_p = 0;
5509 else
5511 set_iterator_to_next (it, 0);
5513 goto get_next;
5516 /* Translate control characters into `\003' or `^C' form.
5517 Control characters coming from a display table entry are
5518 currently not translated because we use IT->dpvec to hold
5519 the translation. This could easily be changed but I
5520 don't believe that it is worth doing.
5522 If it->multibyte_p is nonzero, eight-bit characters and
5523 non-printable multibyte characters are also translated to
5524 octal form.
5526 If it->multibyte_p is zero, eight-bit characters that
5527 don't have corresponding multibyte char code are also
5528 translated to octal form. */
5529 else if ((it->c < ' '
5530 && (it->area != TEXT_AREA
5531 /* In mode line, treat \n like other crl chars. */
5532 || (it->c != '\t'
5533 && it->glyph_row && it->glyph_row->mode_line_p)
5534 || (it->c != '\n' && it->c != '\t')))
5535 || (it->multibyte_p
5536 ? ((it->c >= 127
5537 && it->len == 1)
5538 || !CHAR_PRINTABLE_P (it->c)
5539 || (!NILP (Vnobreak_char_display)
5540 && (it->c == 0x8a0 || it->c == 0x8ad
5541 || it->c == 0x920 || it->c == 0x92d
5542 || it->c == 0xe20 || it->c == 0xe2d
5543 || it->c == 0xf20 || it->c == 0xf2d)))
5544 : (it->c >= 127
5545 && (!unibyte_display_via_language_environment
5546 || it->c == unibyte_char_to_multibyte (it->c)))))
5548 /* IT->c is a control character which must be displayed
5549 either as '\003' or as `^C' where the '\\' and '^'
5550 can be defined in the display table. Fill
5551 IT->ctl_chars with glyphs for what we have to
5552 display. Then, set IT->dpvec to these glyphs. */
5553 GLYPH g;
5554 int ctl_len;
5555 int face_id, lface_id = 0 ;
5556 GLYPH escape_glyph;
5558 /* Handle control characters with ^. */
5560 if (it->c < 128 && it->ctl_arrow_p)
5562 g = '^'; /* default glyph for Control */
5563 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5564 if (it->dp
5565 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5566 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5568 g = XINT (DISP_CTRL_GLYPH (it->dp));
5569 lface_id = FAST_GLYPH_FACE (g);
5571 if (lface_id)
5573 g = FAST_GLYPH_CHAR (g);
5574 face_id = merge_faces (it->f, Qt, lface_id,
5575 it->face_id);
5577 else if (it->f == last_escape_glyph_frame
5578 && it->face_id == last_escape_glyph_face_id)
5580 face_id = last_escape_glyph_merged_face_id;
5582 else
5584 /* Merge the escape-glyph face into the current face. */
5585 face_id = merge_faces (it->f, Qescape_glyph, 0,
5586 it->face_id);
5587 last_escape_glyph_frame = it->f;
5588 last_escape_glyph_face_id = it->face_id;
5589 last_escape_glyph_merged_face_id = face_id;
5592 XSETINT (it->ctl_chars[0], g);
5593 g = it->c ^ 0100;
5594 XSETINT (it->ctl_chars[1], g);
5595 ctl_len = 2;
5596 goto display_control;
5599 /* Handle non-break space in the mode where it only gets
5600 highlighting. */
5602 if (EQ (Vnobreak_char_display, Qt)
5603 && (it->c == 0x8a0 || it->c == 0x920
5604 || it->c == 0xe20 || it->c == 0xf20))
5606 /* Merge the no-break-space face into the current face. */
5607 face_id = merge_faces (it->f, Qnobreak_space, 0,
5608 it->face_id);
5610 g = it->c = ' ';
5611 XSETINT (it->ctl_chars[0], g);
5612 ctl_len = 1;
5613 goto display_control;
5616 /* Handle sequences that start with the "escape glyph". */
5618 /* the default escape glyph is \. */
5619 escape_glyph = '\\';
5621 if (it->dp
5622 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5623 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5625 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5626 lface_id = FAST_GLYPH_FACE (escape_glyph);
5628 if (lface_id)
5630 /* The display table specified a face.
5631 Merge it into face_id and also into escape_glyph. */
5632 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5633 face_id = merge_faces (it->f, Qt, lface_id,
5634 it->face_id);
5636 else if (it->f == last_escape_glyph_frame
5637 && it->face_id == last_escape_glyph_face_id)
5639 face_id = last_escape_glyph_merged_face_id;
5641 else
5643 /* Merge the escape-glyph face into the current face. */
5644 face_id = merge_faces (it->f, Qescape_glyph, 0,
5645 it->face_id);
5646 last_escape_glyph_frame = it->f;
5647 last_escape_glyph_face_id = it->face_id;
5648 last_escape_glyph_merged_face_id = face_id;
5651 /* Handle soft hyphens in the mode where they only get
5652 highlighting. */
5654 if (EQ (Vnobreak_char_display, Qt)
5655 && (it->c == 0x8ad || it->c == 0x92d
5656 || it->c == 0xe2d || it->c == 0xf2d))
5658 g = it->c = '-';
5659 XSETINT (it->ctl_chars[0], g);
5660 ctl_len = 1;
5661 goto display_control;
5664 /* Handle non-break space and soft hyphen
5665 with the escape glyph. */
5667 if (it->c == 0x8a0 || it->c == 0x8ad
5668 || it->c == 0x920 || it->c == 0x92d
5669 || it->c == 0xe20 || it->c == 0xe2d
5670 || it->c == 0xf20 || it->c == 0xf2d)
5672 XSETINT (it->ctl_chars[0], escape_glyph);
5673 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5674 XSETINT (it->ctl_chars[1], g);
5675 ctl_len = 2;
5676 goto display_control;
5680 unsigned char str[MAX_MULTIBYTE_LENGTH];
5681 int len;
5682 int i;
5684 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5685 if (SINGLE_BYTE_CHAR_P (it->c))
5686 str[0] = it->c, len = 1;
5687 else
5689 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5690 if (len < 0)
5692 /* It's an invalid character, which shouldn't
5693 happen actually, but due to bugs it may
5694 happen. Let's print the char as is, there's
5695 not much meaningful we can do with it. */
5696 str[0] = it->c;
5697 str[1] = it->c >> 8;
5698 str[2] = it->c >> 16;
5699 str[3] = it->c >> 24;
5700 len = 4;
5704 for (i = 0; i < len; i++)
5706 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5707 /* Insert three more glyphs into IT->ctl_chars for
5708 the octal display of the character. */
5709 g = ((str[i] >> 6) & 7) + '0';
5710 XSETINT (it->ctl_chars[i * 4 + 1], g);
5711 g = ((str[i] >> 3) & 7) + '0';
5712 XSETINT (it->ctl_chars[i * 4 + 2], g);
5713 g = (str[i] & 7) + '0';
5714 XSETINT (it->ctl_chars[i * 4 + 3], g);
5716 ctl_len = len * 4;
5719 display_control:
5720 /* Set up IT->dpvec and return first character from it. */
5721 it->dpvec_char_len = it->len;
5722 it->dpvec = it->ctl_chars;
5723 it->dpend = it->dpvec + ctl_len;
5724 it->current.dpvec_index = 0;
5725 it->dpvec_face_id = face_id;
5726 it->saved_face_id = it->face_id;
5727 it->method = GET_FROM_DISPLAY_VECTOR;
5728 it->ellipsis_p = 0;
5729 goto get_next;
5733 /* Adjust face id for a multibyte character. There are no
5734 multibyte character in unibyte text. */
5735 if (it->multibyte_p
5736 && success_p
5737 && FRAME_WINDOW_P (it->f))
5739 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5740 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5744 /* Is this character the last one of a run of characters with
5745 box? If yes, set IT->end_of_box_run_p to 1. */
5746 if (it->face_box_p
5747 && it->s == NULL)
5749 int face_id;
5750 struct face *face;
5752 it->end_of_box_run_p
5753 = ((face_id = face_after_it_pos (it),
5754 face_id != it->face_id)
5755 && (face = FACE_FROM_ID (it->f, face_id),
5756 face->box == FACE_NO_BOX));
5759 /* Value is 0 if end of buffer or string reached. */
5760 return success_p;
5764 /* Move IT to the next display element.
5766 RESEAT_P non-zero means if called on a newline in buffer text,
5767 skip to the next visible line start.
5769 Functions get_next_display_element and set_iterator_to_next are
5770 separate because I find this arrangement easier to handle than a
5771 get_next_display_element function that also increments IT's
5772 position. The way it is we can first look at an iterator's current
5773 display element, decide whether it fits on a line, and if it does,
5774 increment the iterator position. The other way around we probably
5775 would either need a flag indicating whether the iterator has to be
5776 incremented the next time, or we would have to implement a
5777 decrement position function which would not be easy to write. */
5779 void
5780 set_iterator_to_next (it, reseat_p)
5781 struct it *it;
5782 int reseat_p;
5784 /* Reset flags indicating start and end of a sequence of characters
5785 with box. Reset them at the start of this function because
5786 moving the iterator to a new position might set them. */
5787 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5789 switch (it->method)
5791 case GET_FROM_BUFFER:
5792 /* The current display element of IT is a character from
5793 current_buffer. Advance in the buffer, and maybe skip over
5794 invisible lines that are so because of selective display. */
5795 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5796 reseat_at_next_visible_line_start (it, 0);
5797 else
5799 xassert (it->len != 0);
5800 IT_BYTEPOS (*it) += it->len;
5801 IT_CHARPOS (*it) += 1;
5802 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5804 break;
5806 case GET_FROM_COMPOSITION:
5807 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5808 xassert (it->sp > 0);
5809 pop_it (it);
5810 if (it->method == GET_FROM_STRING)
5812 IT_STRING_BYTEPOS (*it) += it->len;
5813 IT_STRING_CHARPOS (*it) += it->cmp_len;
5814 goto consider_string_end;
5816 else if (it->method == GET_FROM_BUFFER)
5818 IT_BYTEPOS (*it) += it->len;
5819 IT_CHARPOS (*it) += it->cmp_len;
5821 break;
5823 case GET_FROM_C_STRING:
5824 /* Current display element of IT is from a C string. */
5825 IT_BYTEPOS (*it) += it->len;
5826 IT_CHARPOS (*it) += 1;
5827 break;
5829 case GET_FROM_DISPLAY_VECTOR:
5830 /* Current display element of IT is from a display table entry.
5831 Advance in the display table definition. Reset it to null if
5832 end reached, and continue with characters from buffers/
5833 strings. */
5834 ++it->current.dpvec_index;
5836 /* Restore face of the iterator to what they were before the
5837 display vector entry (these entries may contain faces). */
5838 it->face_id = it->saved_face_id;
5840 if (it->dpvec + it->current.dpvec_index == it->dpend)
5842 int recheck_faces = it->ellipsis_p;
5844 if (it->s)
5845 it->method = GET_FROM_C_STRING;
5846 else if (STRINGP (it->string))
5847 it->method = GET_FROM_STRING;
5848 else
5850 it->method = GET_FROM_BUFFER;
5851 it->object = it->w->buffer;
5854 it->dpvec = NULL;
5855 it->current.dpvec_index = -1;
5857 /* Skip over characters which were displayed via IT->dpvec. */
5858 if (it->dpvec_char_len < 0)
5859 reseat_at_next_visible_line_start (it, 1);
5860 else if (it->dpvec_char_len > 0)
5862 if (it->method == GET_FROM_STRING
5863 && it->n_overlay_strings > 0)
5864 it->ignore_overlay_strings_at_pos_p = 1;
5865 it->len = it->dpvec_char_len;
5866 set_iterator_to_next (it, reseat_p);
5869 /* Maybe recheck faces after display vector */
5870 if (recheck_faces)
5871 it->stop_charpos = IT_CHARPOS (*it);
5873 break;
5875 case GET_FROM_STRING:
5876 /* Current display element is a character from a Lisp string. */
5877 xassert (it->s == NULL && STRINGP (it->string));
5878 IT_STRING_BYTEPOS (*it) += it->len;
5879 IT_STRING_CHARPOS (*it) += 1;
5881 consider_string_end:
5883 if (it->current.overlay_string_index >= 0)
5885 /* IT->string is an overlay string. Advance to the
5886 next, if there is one. */
5887 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5888 next_overlay_string (it);
5890 else
5892 /* IT->string is not an overlay string. If we reached
5893 its end, and there is something on IT->stack, proceed
5894 with what is on the stack. This can be either another
5895 string, this time an overlay string, or a buffer. */
5896 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5897 && it->sp > 0)
5899 pop_it (it);
5900 if (it->method == GET_FROM_STRING)
5901 goto consider_string_end;
5904 break;
5906 case GET_FROM_IMAGE:
5907 case GET_FROM_STRETCH:
5908 /* The position etc with which we have to proceed are on
5909 the stack. The position may be at the end of a string,
5910 if the `display' property takes up the whole string. */
5911 xassert (it->sp > 0);
5912 pop_it (it);
5913 if (it->method == GET_FROM_STRING)
5914 goto consider_string_end;
5915 break;
5917 default:
5918 /* There are no other methods defined, so this should be a bug. */
5919 abort ();
5922 xassert (it->method != GET_FROM_STRING
5923 || (STRINGP (it->string)
5924 && IT_STRING_CHARPOS (*it) >= 0));
5927 /* Load IT's display element fields with information about the next
5928 display element which comes from a display table entry or from the
5929 result of translating a control character to one of the forms `^C'
5930 or `\003'.
5932 IT->dpvec holds the glyphs to return as characters.
5933 IT->saved_face_id holds the face id before the display vector--
5934 it is restored into IT->face_idin set_iterator_to_next. */
5936 static int
5937 next_element_from_display_vector (it)
5938 struct it *it;
5940 /* Precondition. */
5941 xassert (it->dpvec && it->current.dpvec_index >= 0);
5943 it->face_id = it->saved_face_id;
5945 if (INTEGERP (*it->dpvec)
5946 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5948 GLYPH g;
5950 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5951 it->c = FAST_GLYPH_CHAR (g);
5952 it->len = CHAR_BYTES (it->c);
5954 /* The entry may contain a face id to use. Such a face id is
5955 the id of a Lisp face, not a realized face. A face id of
5956 zero means no face is specified. */
5957 if (it->dpvec_face_id >= 0)
5958 it->face_id = it->dpvec_face_id;
5959 else
5961 int lface_id = FAST_GLYPH_FACE (g);
5962 if (lface_id > 0)
5963 it->face_id = merge_faces (it->f, Qt, lface_id,
5964 it->saved_face_id);
5967 else
5968 /* Display table entry is invalid. Return a space. */
5969 it->c = ' ', it->len = 1;
5971 /* Don't change position and object of the iterator here. They are
5972 still the values of the character that had this display table
5973 entry or was translated, and that's what we want. */
5974 it->what = IT_CHARACTER;
5975 return 1;
5979 /* Load IT with the next display element from Lisp string IT->string.
5980 IT->current.string_pos is the current position within the string.
5981 If IT->current.overlay_string_index >= 0, the Lisp string is an
5982 overlay string. */
5984 static int
5985 next_element_from_string (it)
5986 struct it *it;
5988 struct text_pos position;
5990 xassert (STRINGP (it->string));
5991 xassert (IT_STRING_CHARPOS (*it) >= 0);
5992 position = it->current.string_pos;
5994 /* Time to check for invisible text? */
5995 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5996 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5998 handle_stop (it);
6000 /* Since a handler may have changed IT->method, we must
6001 recurse here. */
6002 return get_next_display_element (it);
6005 if (it->current.overlay_string_index >= 0)
6007 /* Get the next character from an overlay string. In overlay
6008 strings, There is no field width or padding with spaces to
6009 do. */
6010 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6012 it->what = IT_EOB;
6013 return 0;
6015 else if (STRING_MULTIBYTE (it->string))
6017 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6018 const unsigned char *s = (SDATA (it->string)
6019 + IT_STRING_BYTEPOS (*it));
6020 it->c = string_char_and_length (s, remaining, &it->len);
6022 else
6024 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6025 it->len = 1;
6028 else
6030 /* Get the next character from a Lisp string that is not an
6031 overlay string. Such strings come from the mode line, for
6032 example. We may have to pad with spaces, or truncate the
6033 string. See also next_element_from_c_string. */
6034 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6036 it->what = IT_EOB;
6037 return 0;
6039 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6041 /* Pad with spaces. */
6042 it->c = ' ', it->len = 1;
6043 CHARPOS (position) = BYTEPOS (position) = -1;
6045 else if (STRING_MULTIBYTE (it->string))
6047 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6048 const unsigned char *s = (SDATA (it->string)
6049 + IT_STRING_BYTEPOS (*it));
6050 it->c = string_char_and_length (s, maxlen, &it->len);
6052 else
6054 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6055 it->len = 1;
6059 /* Record what we have and where it came from. */
6060 it->what = IT_CHARACTER;
6061 it->object = it->string;
6062 it->position = position;
6063 return 1;
6067 /* Load IT with next display element from C string IT->s.
6068 IT->string_nchars is the maximum number of characters to return
6069 from the string. IT->end_charpos may be greater than
6070 IT->string_nchars when this function is called, in which case we
6071 may have to return padding spaces. Value is zero if end of string
6072 reached, including padding spaces. */
6074 static int
6075 next_element_from_c_string (it)
6076 struct it *it;
6078 int success_p = 1;
6080 xassert (it->s);
6081 it->what = IT_CHARACTER;
6082 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6083 it->object = Qnil;
6085 /* IT's position can be greater IT->string_nchars in case a field
6086 width or precision has been specified when the iterator was
6087 initialized. */
6088 if (IT_CHARPOS (*it) >= it->end_charpos)
6090 /* End of the game. */
6091 it->what = IT_EOB;
6092 success_p = 0;
6094 else if (IT_CHARPOS (*it) >= it->string_nchars)
6096 /* Pad with spaces. */
6097 it->c = ' ', it->len = 1;
6098 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6100 else if (it->multibyte_p)
6102 /* Implementation note: The calls to strlen apparently aren't a
6103 performance problem because there is no noticeable performance
6104 difference between Emacs running in unibyte or multibyte mode. */
6105 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6106 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6107 maxlen, &it->len);
6109 else
6110 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6112 return success_p;
6116 /* Set up IT to return characters from an ellipsis, if appropriate.
6117 The definition of the ellipsis glyphs may come from a display table
6118 entry. This function Fills IT with the first glyph from the
6119 ellipsis if an ellipsis is to be displayed. */
6121 static int
6122 next_element_from_ellipsis (it)
6123 struct it *it;
6125 if (it->selective_display_ellipsis_p)
6126 setup_for_ellipsis (it, it->len);
6127 else
6129 /* The face at the current position may be different from the
6130 face we find after the invisible text. Remember what it
6131 was in IT->saved_face_id, and signal that it's there by
6132 setting face_before_selective_p. */
6133 it->saved_face_id = it->face_id;
6134 it->method = GET_FROM_BUFFER;
6135 it->object = it->w->buffer;
6136 reseat_at_next_visible_line_start (it, 1);
6137 it->face_before_selective_p = 1;
6140 return get_next_display_element (it);
6144 /* Deliver an image display element. The iterator IT is already
6145 filled with image information (done in handle_display_prop). Value
6146 is always 1. */
6149 static int
6150 next_element_from_image (it)
6151 struct it *it;
6153 it->what = IT_IMAGE;
6154 return 1;
6158 /* Fill iterator IT with next display element from a stretch glyph
6159 property. IT->object is the value of the text property. Value is
6160 always 1. */
6162 static int
6163 next_element_from_stretch (it)
6164 struct it *it;
6166 it->what = IT_STRETCH;
6167 return 1;
6171 /* Load IT with the next display element from current_buffer. Value
6172 is zero if end of buffer reached. IT->stop_charpos is the next
6173 position at which to stop and check for text properties or buffer
6174 end. */
6176 static int
6177 next_element_from_buffer (it)
6178 struct it *it;
6180 int success_p = 1;
6182 /* Check this assumption, otherwise, we would never enter the
6183 if-statement, below. */
6184 xassert (IT_CHARPOS (*it) >= BEGV
6185 && IT_CHARPOS (*it) <= it->stop_charpos);
6187 if (IT_CHARPOS (*it) >= it->stop_charpos)
6189 if (IT_CHARPOS (*it) >= it->end_charpos)
6191 int overlay_strings_follow_p;
6193 /* End of the game, except when overlay strings follow that
6194 haven't been returned yet. */
6195 if (it->overlay_strings_at_end_processed_p)
6196 overlay_strings_follow_p = 0;
6197 else
6199 it->overlay_strings_at_end_processed_p = 1;
6200 overlay_strings_follow_p = get_overlay_strings (it, 0);
6203 if (overlay_strings_follow_p)
6204 success_p = get_next_display_element (it);
6205 else
6207 it->what = IT_EOB;
6208 it->position = it->current.pos;
6209 success_p = 0;
6212 else
6214 handle_stop (it);
6215 return get_next_display_element (it);
6218 else
6220 /* No face changes, overlays etc. in sight, so just return a
6221 character from current_buffer. */
6222 unsigned char *p;
6224 /* Maybe run the redisplay end trigger hook. Performance note:
6225 This doesn't seem to cost measurable time. */
6226 if (it->redisplay_end_trigger_charpos
6227 && it->glyph_row
6228 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6229 run_redisplay_end_trigger_hook (it);
6231 /* Get the next character, maybe multibyte. */
6232 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6233 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6235 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6236 - IT_BYTEPOS (*it));
6237 it->c = string_char_and_length (p, maxlen, &it->len);
6239 else
6240 it->c = *p, it->len = 1;
6242 /* Record what we have and where it came from. */
6243 it->what = IT_CHARACTER;;
6244 it->object = it->w->buffer;
6245 it->position = it->current.pos;
6247 /* Normally we return the character found above, except when we
6248 really want to return an ellipsis for selective display. */
6249 if (it->selective)
6251 if (it->c == '\n')
6253 /* A value of selective > 0 means hide lines indented more
6254 than that number of columns. */
6255 if (it->selective > 0
6256 && IT_CHARPOS (*it) + 1 < ZV
6257 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6258 IT_BYTEPOS (*it) + 1,
6259 (double) it->selective)) /* iftc */
6261 success_p = next_element_from_ellipsis (it);
6262 it->dpvec_char_len = -1;
6265 else if (it->c == '\r' && it->selective == -1)
6267 /* A value of selective == -1 means that everything from the
6268 CR to the end of the line is invisible, with maybe an
6269 ellipsis displayed for it. */
6270 success_p = next_element_from_ellipsis (it);
6271 it->dpvec_char_len = -1;
6276 /* Value is zero if end of buffer reached. */
6277 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6278 return success_p;
6282 /* Run the redisplay end trigger hook for IT. */
6284 static void
6285 run_redisplay_end_trigger_hook (it)
6286 struct it *it;
6288 Lisp_Object args[3];
6290 /* IT->glyph_row should be non-null, i.e. we should be actually
6291 displaying something, or otherwise we should not run the hook. */
6292 xassert (it->glyph_row);
6294 /* Set up hook arguments. */
6295 args[0] = Qredisplay_end_trigger_functions;
6296 args[1] = it->window;
6297 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6298 it->redisplay_end_trigger_charpos = 0;
6300 /* Since we are *trying* to run these functions, don't try to run
6301 them again, even if they get an error. */
6302 it->w->redisplay_end_trigger = Qnil;
6303 Frun_hook_with_args (3, args);
6305 /* Notice if it changed the face of the character we are on. */
6306 handle_face_prop (it);
6310 /* Deliver a composition display element. The iterator IT is already
6311 filled with composition information (done in
6312 handle_composition_prop). Value is always 1. */
6314 static int
6315 next_element_from_composition (it)
6316 struct it *it;
6318 it->what = IT_COMPOSITION;
6319 it->position = (STRINGP (it->string)
6320 ? it->current.string_pos
6321 : it->current.pos);
6322 if (STRINGP (it->string))
6323 it->object = it->string;
6324 else
6325 it->object = it->w->buffer;
6326 return 1;
6331 /***********************************************************************
6332 Moving an iterator without producing glyphs
6333 ***********************************************************************/
6335 /* Check if iterator is at a position corresponding to a valid buffer
6336 position after some move_it_ call. */
6338 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6339 ((it)->method == GET_FROM_STRING \
6340 ? IT_STRING_CHARPOS (*it) == 0 \
6341 : 1)
6344 /* Move iterator IT to a specified buffer or X position within one
6345 line on the display without producing glyphs.
6347 OP should be a bit mask including some or all of these bits:
6348 MOVE_TO_X: Stop on reaching x-position TO_X.
6349 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6350 Regardless of OP's value, stop in reaching the end of the display line.
6352 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6353 This means, in particular, that TO_X includes window's horizontal
6354 scroll amount.
6356 The return value has several possible values that
6357 say what condition caused the scan to stop:
6359 MOVE_POS_MATCH_OR_ZV
6360 - when TO_POS or ZV was reached.
6362 MOVE_X_REACHED
6363 -when TO_X was reached before TO_POS or ZV were reached.
6365 MOVE_LINE_CONTINUED
6366 - when we reached the end of the display area and the line must
6367 be continued.
6369 MOVE_LINE_TRUNCATED
6370 - when we reached the end of the display area and the line is
6371 truncated.
6373 MOVE_NEWLINE_OR_CR
6374 - when we stopped at a line end, i.e. a newline or a CR and selective
6375 display is on. */
6377 static enum move_it_result
6378 move_it_in_display_line_to (it, to_charpos, to_x, op)
6379 struct it *it;
6380 int to_charpos, to_x, op;
6382 enum move_it_result result = MOVE_UNDEFINED;
6383 struct glyph_row *saved_glyph_row;
6385 /* Don't produce glyphs in produce_glyphs. */
6386 saved_glyph_row = it->glyph_row;
6387 it->glyph_row = NULL;
6389 #define BUFFER_POS_REACHED_P() \
6390 ((op & MOVE_TO_POS) != 0 \
6391 && BUFFERP (it->object) \
6392 && IT_CHARPOS (*it) >= to_charpos \
6393 && (it->method == GET_FROM_BUFFER \
6394 || (it->method == GET_FROM_DISPLAY_VECTOR \
6395 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6398 while (1)
6400 int x, i, ascent = 0, descent = 0;
6402 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6403 if ((op & MOVE_TO_POS) != 0
6404 && BUFFERP (it->object)
6405 && it->method == GET_FROM_BUFFER
6406 && IT_CHARPOS (*it) > to_charpos)
6408 result = MOVE_POS_MATCH_OR_ZV;
6409 break;
6412 /* Stop when ZV reached.
6413 We used to stop here when TO_CHARPOS reached as well, but that is
6414 too soon if this glyph does not fit on this line. So we handle it
6415 explicitly below. */
6416 if (!get_next_display_element (it)
6417 || (it->truncate_lines_p
6418 && BUFFER_POS_REACHED_P ()))
6420 result = MOVE_POS_MATCH_OR_ZV;
6421 break;
6424 /* The call to produce_glyphs will get the metrics of the
6425 display element IT is loaded with. We record in x the
6426 x-position before this display element in case it does not
6427 fit on the line. */
6428 x = it->current_x;
6430 /* Remember the line height so far in case the next element doesn't
6431 fit on the line. */
6432 if (!it->truncate_lines_p)
6434 ascent = it->max_ascent;
6435 descent = it->max_descent;
6438 PRODUCE_GLYPHS (it);
6440 if (it->area != TEXT_AREA)
6442 set_iterator_to_next (it, 1);
6443 continue;
6446 /* The number of glyphs we get back in IT->nglyphs will normally
6447 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6448 character on a terminal frame, or (iii) a line end. For the
6449 second case, IT->nglyphs - 1 padding glyphs will be present
6450 (on X frames, there is only one glyph produced for a
6451 composite character.
6453 The behavior implemented below means, for continuation lines,
6454 that as many spaces of a TAB as fit on the current line are
6455 displayed there. For terminal frames, as many glyphs of a
6456 multi-glyph character are displayed in the current line, too.
6457 This is what the old redisplay code did, and we keep it that
6458 way. Under X, the whole shape of a complex character must
6459 fit on the line or it will be completely displayed in the
6460 next line.
6462 Note that both for tabs and padding glyphs, all glyphs have
6463 the same width. */
6464 if (it->nglyphs)
6466 /* More than one glyph or glyph doesn't fit on line. All
6467 glyphs have the same width. */
6468 int single_glyph_width = it->pixel_width / it->nglyphs;
6469 int new_x;
6470 int x_before_this_char = x;
6471 int hpos_before_this_char = it->hpos;
6473 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6475 new_x = x + single_glyph_width;
6477 /* We want to leave anything reaching TO_X to the caller. */
6478 if ((op & MOVE_TO_X) && new_x > to_x)
6480 if (BUFFER_POS_REACHED_P ())
6481 goto buffer_pos_reached;
6482 it->current_x = x;
6483 result = MOVE_X_REACHED;
6484 break;
6486 else if (/* Lines are continued. */
6487 !it->truncate_lines_p
6488 && (/* And glyph doesn't fit on the line. */
6489 new_x > it->last_visible_x
6490 /* Or it fits exactly and we're on a window
6491 system frame. */
6492 || (new_x == it->last_visible_x
6493 && FRAME_WINDOW_P (it->f))))
6495 if (/* IT->hpos == 0 means the very first glyph
6496 doesn't fit on the line, e.g. a wide image. */
6497 it->hpos == 0
6498 || (new_x == it->last_visible_x
6499 && FRAME_WINDOW_P (it->f)))
6501 ++it->hpos;
6502 it->current_x = new_x;
6504 /* The character's last glyph just barely fits
6505 in this row. */
6506 if (i == it->nglyphs - 1)
6508 /* If this is the destination position,
6509 return a position *before* it in this row,
6510 now that we know it fits in this row. */
6511 if (BUFFER_POS_REACHED_P ())
6513 it->hpos = hpos_before_this_char;
6514 it->current_x = x_before_this_char;
6515 result = MOVE_POS_MATCH_OR_ZV;
6516 break;
6519 set_iterator_to_next (it, 1);
6520 #ifdef HAVE_WINDOW_SYSTEM
6521 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6523 if (!get_next_display_element (it))
6525 result = MOVE_POS_MATCH_OR_ZV;
6526 break;
6528 if (BUFFER_POS_REACHED_P ())
6530 if (ITERATOR_AT_END_OF_LINE_P (it))
6531 result = MOVE_POS_MATCH_OR_ZV;
6532 else
6533 result = MOVE_LINE_CONTINUED;
6534 break;
6536 if (ITERATOR_AT_END_OF_LINE_P (it))
6538 result = MOVE_NEWLINE_OR_CR;
6539 break;
6542 #endif /* HAVE_WINDOW_SYSTEM */
6545 else
6547 it->current_x = x;
6548 it->max_ascent = ascent;
6549 it->max_descent = descent;
6552 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6553 IT_CHARPOS (*it)));
6554 result = MOVE_LINE_CONTINUED;
6555 break;
6557 else if (BUFFER_POS_REACHED_P ())
6558 goto buffer_pos_reached;
6559 else if (new_x > it->first_visible_x)
6561 /* Glyph is visible. Increment number of glyphs that
6562 would be displayed. */
6563 ++it->hpos;
6565 else
6567 /* Glyph is completely off the left margin of the display
6568 area. Nothing to do. */
6572 if (result != MOVE_UNDEFINED)
6573 break;
6575 else if (BUFFER_POS_REACHED_P ())
6577 buffer_pos_reached:
6578 it->current_x = x;
6579 it->max_ascent = ascent;
6580 it->max_descent = descent;
6581 result = MOVE_POS_MATCH_OR_ZV;
6582 break;
6584 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6586 /* Stop when TO_X specified and reached. This check is
6587 necessary here because of lines consisting of a line end,
6588 only. The line end will not produce any glyphs and we
6589 would never get MOVE_X_REACHED. */
6590 xassert (it->nglyphs == 0);
6591 result = MOVE_X_REACHED;
6592 break;
6595 /* Is this a line end? If yes, we're done. */
6596 if (ITERATOR_AT_END_OF_LINE_P (it))
6598 result = MOVE_NEWLINE_OR_CR;
6599 break;
6602 /* The current display element has been consumed. Advance
6603 to the next. */
6604 set_iterator_to_next (it, 1);
6606 /* Stop if lines are truncated and IT's current x-position is
6607 past the right edge of the window now. */
6608 if (it->truncate_lines_p
6609 && it->current_x >= it->last_visible_x)
6611 #ifdef HAVE_WINDOW_SYSTEM
6612 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6614 if (!get_next_display_element (it)
6615 || BUFFER_POS_REACHED_P ())
6617 result = MOVE_POS_MATCH_OR_ZV;
6618 break;
6620 if (ITERATOR_AT_END_OF_LINE_P (it))
6622 result = MOVE_NEWLINE_OR_CR;
6623 break;
6626 #endif /* HAVE_WINDOW_SYSTEM */
6627 result = MOVE_LINE_TRUNCATED;
6628 break;
6632 #undef BUFFER_POS_REACHED_P
6634 /* Restore the iterator settings altered at the beginning of this
6635 function. */
6636 it->glyph_row = saved_glyph_row;
6637 return result;
6641 /* Move IT forward until it satisfies one or more of the criteria in
6642 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6644 OP is a bit-mask that specifies where to stop, and in particular,
6645 which of those four position arguments makes a difference. See the
6646 description of enum move_operation_enum.
6648 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6649 screen line, this function will set IT to the next position >
6650 TO_CHARPOS. */
6652 void
6653 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6654 struct it *it;
6655 int to_charpos, to_x, to_y, to_vpos;
6656 int op;
6658 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6659 int line_height;
6660 int reached = 0;
6662 for (;;)
6664 if (op & MOVE_TO_VPOS)
6666 /* If no TO_CHARPOS and no TO_X specified, stop at the
6667 start of the line TO_VPOS. */
6668 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6670 if (it->vpos == to_vpos)
6672 reached = 1;
6673 break;
6675 else
6676 skip = move_it_in_display_line_to (it, -1, -1, 0);
6678 else
6680 /* TO_VPOS >= 0 means stop at TO_X in the line at
6681 TO_VPOS, or at TO_POS, whichever comes first. */
6682 if (it->vpos == to_vpos)
6684 reached = 2;
6685 break;
6688 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6690 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6692 reached = 3;
6693 break;
6695 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6697 /* We have reached TO_X but not in the line we want. */
6698 skip = move_it_in_display_line_to (it, to_charpos,
6699 -1, MOVE_TO_POS);
6700 if (skip == MOVE_POS_MATCH_OR_ZV)
6702 reached = 4;
6703 break;
6708 else if (op & MOVE_TO_Y)
6710 struct it it_backup;
6712 /* TO_Y specified means stop at TO_X in the line containing
6713 TO_Y---or at TO_CHARPOS if this is reached first. The
6714 problem is that we can't really tell whether the line
6715 contains TO_Y before we have completely scanned it, and
6716 this may skip past TO_X. What we do is to first scan to
6717 TO_X.
6719 If TO_X is not specified, use a TO_X of zero. The reason
6720 is to make the outcome of this function more predictable.
6721 If we didn't use TO_X == 0, we would stop at the end of
6722 the line which is probably not what a caller would expect
6723 to happen. */
6724 skip = move_it_in_display_line_to (it, to_charpos,
6725 ((op & MOVE_TO_X)
6726 ? to_x : 0),
6727 (MOVE_TO_X
6728 | (op & MOVE_TO_POS)));
6730 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6731 if (skip == MOVE_POS_MATCH_OR_ZV)
6733 reached = 5;
6734 break;
6737 /* If TO_X was reached, we would like to know whether TO_Y
6738 is in the line. This can only be said if we know the
6739 total line height which requires us to scan the rest of
6740 the line. */
6741 if (skip == MOVE_X_REACHED)
6743 it_backup = *it;
6744 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6745 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6746 op & MOVE_TO_POS);
6747 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6750 /* Now, decide whether TO_Y is in this line. */
6751 line_height = it->max_ascent + it->max_descent;
6752 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6754 if (to_y >= it->current_y
6755 && to_y < it->current_y + line_height)
6757 if (skip == MOVE_X_REACHED)
6758 /* If TO_Y is in this line and TO_X was reached above,
6759 we scanned too far. We have to restore IT's settings
6760 to the ones before skipping. */
6761 *it = it_backup;
6762 reached = 6;
6764 else if (skip == MOVE_X_REACHED)
6766 skip = skip2;
6767 if (skip == MOVE_POS_MATCH_OR_ZV)
6768 reached = 7;
6771 if (reached)
6772 break;
6774 else if (BUFFERP (it->object)
6775 && it->method == GET_FROM_BUFFER
6776 && IT_CHARPOS (*it) >= to_charpos)
6777 skip = MOVE_POS_MATCH_OR_ZV;
6778 else
6779 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6781 switch (skip)
6783 case MOVE_POS_MATCH_OR_ZV:
6784 reached = 8;
6785 goto out;
6787 case MOVE_NEWLINE_OR_CR:
6788 set_iterator_to_next (it, 1);
6789 it->continuation_lines_width = 0;
6790 break;
6792 case MOVE_LINE_TRUNCATED:
6793 it->continuation_lines_width = 0;
6794 reseat_at_next_visible_line_start (it, 0);
6795 if ((op & MOVE_TO_POS) != 0
6796 && IT_CHARPOS (*it) > to_charpos)
6798 reached = 9;
6799 goto out;
6801 break;
6803 case MOVE_LINE_CONTINUED:
6804 it->continuation_lines_width += it->current_x;
6805 break;
6807 default:
6808 abort ();
6811 /* Reset/increment for the next run. */
6812 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6813 it->current_x = it->hpos = 0;
6814 it->current_y += it->max_ascent + it->max_descent;
6815 ++it->vpos;
6816 last_height = it->max_ascent + it->max_descent;
6817 last_max_ascent = it->max_ascent;
6818 it->max_ascent = it->max_descent = 0;
6821 out:
6823 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6827 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6829 If DY > 0, move IT backward at least that many pixels. DY = 0
6830 means move IT backward to the preceding line start or BEGV. This
6831 function may move over more than DY pixels if IT->current_y - DY
6832 ends up in the middle of a line; in this case IT->current_y will be
6833 set to the top of the line moved to. */
6835 void
6836 move_it_vertically_backward (it, dy)
6837 struct it *it;
6838 int dy;
6840 int nlines, h;
6841 struct it it2, it3;
6842 int start_pos;
6844 move_further_back:
6845 xassert (dy >= 0);
6847 start_pos = IT_CHARPOS (*it);
6849 /* Estimate how many newlines we must move back. */
6850 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6852 /* Set the iterator's position that many lines back. */
6853 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6854 back_to_previous_visible_line_start (it);
6856 /* Reseat the iterator here. When moving backward, we don't want
6857 reseat to skip forward over invisible text, set up the iterator
6858 to deliver from overlay strings at the new position etc. So,
6859 use reseat_1 here. */
6860 reseat_1 (it, it->current.pos, 1);
6862 /* We are now surely at a line start. */
6863 it->current_x = it->hpos = 0;
6864 it->continuation_lines_width = 0;
6866 /* Move forward and see what y-distance we moved. First move to the
6867 start of the next line so that we get its height. We need this
6868 height to be able to tell whether we reached the specified
6869 y-distance. */
6870 it2 = *it;
6871 it2.max_ascent = it2.max_descent = 0;
6874 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6875 MOVE_TO_POS | MOVE_TO_VPOS);
6877 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6878 xassert (IT_CHARPOS (*it) >= BEGV);
6879 it3 = it2;
6881 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6882 xassert (IT_CHARPOS (*it) >= BEGV);
6883 /* H is the actual vertical distance from the position in *IT
6884 and the starting position. */
6885 h = it2.current_y - it->current_y;
6886 /* NLINES is the distance in number of lines. */
6887 nlines = it2.vpos - it->vpos;
6889 /* Correct IT's y and vpos position
6890 so that they are relative to the starting point. */
6891 it->vpos -= nlines;
6892 it->current_y -= h;
6894 if (dy == 0)
6896 /* DY == 0 means move to the start of the screen line. The
6897 value of nlines is > 0 if continuation lines were involved. */
6898 if (nlines > 0)
6899 move_it_by_lines (it, nlines, 1);
6900 #if 0
6901 /* I think this assert is bogus if buffer contains
6902 invisible text or images. KFS. */
6903 xassert (IT_CHARPOS (*it) <= start_pos);
6904 #endif
6906 else
6908 /* The y-position we try to reach, relative to *IT.
6909 Note that H has been subtracted in front of the if-statement. */
6910 int target_y = it->current_y + h - dy;
6911 int y0 = it3.current_y;
6912 int y1 = line_bottom_y (&it3);
6913 int line_height = y1 - y0;
6915 /* If we did not reach target_y, try to move further backward if
6916 we can. If we moved too far backward, try to move forward. */
6917 if (target_y < it->current_y
6918 /* This is heuristic. In a window that's 3 lines high, with
6919 a line height of 13 pixels each, recentering with point
6920 on the bottom line will try to move -39/2 = 19 pixels
6921 backward. Try to avoid moving into the first line. */
6922 && (it->current_y - target_y
6923 > min (window_box_height (it->w), line_height * 2 / 3))
6924 && IT_CHARPOS (*it) > BEGV)
6926 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6927 target_y - it->current_y));
6928 dy = it->current_y - target_y;
6929 goto move_further_back;
6931 else if (target_y >= it->current_y + line_height
6932 && IT_CHARPOS (*it) < ZV)
6934 /* Should move forward by at least one line, maybe more.
6936 Note: Calling move_it_by_lines can be expensive on
6937 terminal frames, where compute_motion is used (via
6938 vmotion) to do the job, when there are very long lines
6939 and truncate-lines is nil. That's the reason for
6940 treating terminal frames specially here. */
6942 if (!FRAME_WINDOW_P (it->f))
6943 move_it_vertically (it, target_y - (it->current_y + line_height));
6944 else
6948 move_it_by_lines (it, 1, 1);
6950 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6953 #if 0
6954 /* I think this assert is bogus if buffer contains
6955 invisible text or images. KFS. */
6956 xassert (IT_CHARPOS (*it) >= BEGV);
6957 #endif
6963 /* Move IT by a specified amount of pixel lines DY. DY negative means
6964 move backwards. DY = 0 means move to start of screen line. At the
6965 end, IT will be on the start of a screen line. */
6967 void
6968 move_it_vertically (it, dy)
6969 struct it *it;
6970 int dy;
6972 if (dy <= 0)
6973 move_it_vertically_backward (it, -dy);
6974 else
6976 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6977 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6978 MOVE_TO_POS | MOVE_TO_Y);
6979 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6981 /* If buffer ends in ZV without a newline, move to the start of
6982 the line to satisfy the post-condition. */
6983 if (IT_CHARPOS (*it) == ZV
6984 && ZV > BEGV
6985 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6986 move_it_by_lines (it, 0, 0);
6991 /* Move iterator IT past the end of the text line it is in. */
6993 void
6994 move_it_past_eol (it)
6995 struct it *it;
6997 enum move_it_result rc;
6999 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7000 if (rc == MOVE_NEWLINE_OR_CR)
7001 set_iterator_to_next (it, 0);
7005 #if 0 /* Currently not used. */
7007 /* Return non-zero if some text between buffer positions START_CHARPOS
7008 and END_CHARPOS is invisible. IT->window is the window for text
7009 property lookup. */
7011 static int
7012 invisible_text_between_p (it, start_charpos, end_charpos)
7013 struct it *it;
7014 int start_charpos, end_charpos;
7016 Lisp_Object prop, limit;
7017 int invisible_found_p;
7019 xassert (it != NULL && start_charpos <= end_charpos);
7021 /* Is text at START invisible? */
7022 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7023 it->window);
7024 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7025 invisible_found_p = 1;
7026 else
7028 limit = Fnext_single_char_property_change (make_number (start_charpos),
7029 Qinvisible, Qnil,
7030 make_number (end_charpos));
7031 invisible_found_p = XFASTINT (limit) < end_charpos;
7034 return invisible_found_p;
7037 #endif /* 0 */
7040 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7041 negative means move up. DVPOS == 0 means move to the start of the
7042 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7043 NEED_Y_P is zero, IT->current_y will be left unchanged.
7045 Further optimization ideas: If we would know that IT->f doesn't use
7046 a face with proportional font, we could be faster for
7047 truncate-lines nil. */
7049 void
7050 move_it_by_lines (it, dvpos, need_y_p)
7051 struct it *it;
7052 int dvpos, need_y_p;
7054 struct position pos;
7056 if (!FRAME_WINDOW_P (it->f))
7058 struct text_pos textpos;
7060 /* We can use vmotion on frames without proportional fonts. */
7061 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7062 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7063 reseat (it, textpos, 1);
7064 it->vpos += pos.vpos;
7065 it->current_y += pos.vpos;
7067 else if (dvpos == 0)
7069 /* DVPOS == 0 means move to the start of the screen line. */
7070 move_it_vertically_backward (it, 0);
7071 xassert (it->current_x == 0 && it->hpos == 0);
7072 /* Let next call to line_bottom_y calculate real line height */
7073 last_height = 0;
7075 else if (dvpos > 0)
7077 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7078 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7079 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7081 else
7083 struct it it2;
7084 int start_charpos, i;
7086 /* Start at the beginning of the screen line containing IT's
7087 position. This may actually move vertically backwards,
7088 in case of overlays, so adjust dvpos accordingly. */
7089 dvpos += it->vpos;
7090 move_it_vertically_backward (it, 0);
7091 dvpos -= it->vpos;
7093 /* Go back -DVPOS visible lines and reseat the iterator there. */
7094 start_charpos = IT_CHARPOS (*it);
7095 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7096 back_to_previous_visible_line_start (it);
7097 reseat (it, it->current.pos, 1);
7099 /* Move further back if we end up in a string or an image. */
7100 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7102 /* First try to move to start of display line. */
7103 dvpos += it->vpos;
7104 move_it_vertically_backward (it, 0);
7105 dvpos -= it->vpos;
7106 if (IT_POS_VALID_AFTER_MOVE_P (it))
7107 break;
7108 /* If start of line is still in string or image,
7109 move further back. */
7110 back_to_previous_visible_line_start (it);
7111 reseat (it, it->current.pos, 1);
7112 dvpos--;
7115 it->current_x = it->hpos = 0;
7117 /* Above call may have moved too far if continuation lines
7118 are involved. Scan forward and see if it did. */
7119 it2 = *it;
7120 it2.vpos = it2.current_y = 0;
7121 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7122 it->vpos -= it2.vpos;
7123 it->current_y -= it2.current_y;
7124 it->current_x = it->hpos = 0;
7126 /* If we moved too far back, move IT some lines forward. */
7127 if (it2.vpos > -dvpos)
7129 int delta = it2.vpos + dvpos;
7130 it2 = *it;
7131 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7132 /* Move back again if we got too far ahead. */
7133 if (IT_CHARPOS (*it) >= start_charpos)
7134 *it = it2;
7139 /* Return 1 if IT points into the middle of a display vector. */
7142 in_display_vector_p (it)
7143 struct it *it;
7145 return (it->method == GET_FROM_DISPLAY_VECTOR
7146 && it->current.dpvec_index > 0
7147 && it->dpvec + it->current.dpvec_index != it->dpend);
7151 /***********************************************************************
7152 Messages
7153 ***********************************************************************/
7156 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7157 to *Messages*. */
7159 void
7160 add_to_log (format, arg1, arg2)
7161 char *format;
7162 Lisp_Object arg1, arg2;
7164 Lisp_Object args[3];
7165 Lisp_Object msg, fmt;
7166 char *buffer;
7167 int len;
7168 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7169 USE_SAFE_ALLOCA;
7171 /* Do nothing if called asynchronously. Inserting text into
7172 a buffer may call after-change-functions and alike and
7173 that would means running Lisp asynchronously. */
7174 if (handling_signal)
7175 return;
7177 fmt = msg = Qnil;
7178 GCPRO4 (fmt, msg, arg1, arg2);
7180 args[0] = fmt = build_string (format);
7181 args[1] = arg1;
7182 args[2] = arg2;
7183 msg = Fformat (3, args);
7185 len = SBYTES (msg) + 1;
7186 SAFE_ALLOCA (buffer, char *, len);
7187 bcopy (SDATA (msg), buffer, len);
7189 message_dolog (buffer, len - 1, 1, 0);
7190 SAFE_FREE ();
7192 UNGCPRO;
7196 /* Output a newline in the *Messages* buffer if "needs" one. */
7198 void
7199 message_log_maybe_newline ()
7201 if (message_log_need_newline)
7202 message_dolog ("", 0, 1, 0);
7206 /* Add a string M of length NBYTES to the message log, optionally
7207 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7208 nonzero, means interpret the contents of M as multibyte. This
7209 function calls low-level routines in order to bypass text property
7210 hooks, etc. which might not be safe to run.
7212 This may GC (insert may run before/after change hooks),
7213 so the buffer M must NOT point to a Lisp string. */
7215 void
7216 message_dolog (m, nbytes, nlflag, multibyte)
7217 const char *m;
7218 int nbytes, nlflag, multibyte;
7220 if (!NILP (Vmemory_full))
7221 return;
7223 if (!NILP (Vmessage_log_max))
7225 struct buffer *oldbuf;
7226 Lisp_Object oldpoint, oldbegv, oldzv;
7227 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7228 int point_at_end = 0;
7229 int zv_at_end = 0;
7230 Lisp_Object old_deactivate_mark, tem;
7231 struct gcpro gcpro1;
7233 old_deactivate_mark = Vdeactivate_mark;
7234 oldbuf = current_buffer;
7235 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7236 current_buffer->undo_list = Qt;
7238 oldpoint = message_dolog_marker1;
7239 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7240 oldbegv = message_dolog_marker2;
7241 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7242 oldzv = message_dolog_marker3;
7243 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7244 GCPRO1 (old_deactivate_mark);
7246 if (PT == Z)
7247 point_at_end = 1;
7248 if (ZV == Z)
7249 zv_at_end = 1;
7251 BEGV = BEG;
7252 BEGV_BYTE = BEG_BYTE;
7253 ZV = Z;
7254 ZV_BYTE = Z_BYTE;
7255 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7257 /* Insert the string--maybe converting multibyte to single byte
7258 or vice versa, so that all the text fits the buffer. */
7259 if (multibyte
7260 && NILP (current_buffer->enable_multibyte_characters))
7262 int i, c, char_bytes;
7263 unsigned char work[1];
7265 /* Convert a multibyte string to single-byte
7266 for the *Message* buffer. */
7267 for (i = 0; i < nbytes; i += char_bytes)
7269 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7270 work[0] = (SINGLE_BYTE_CHAR_P (c)
7272 : multibyte_char_to_unibyte (c, Qnil));
7273 insert_1_both (work, 1, 1, 1, 0, 0);
7276 else if (! multibyte
7277 && ! NILP (current_buffer->enable_multibyte_characters))
7279 int i, c, char_bytes;
7280 unsigned char *msg = (unsigned char *) m;
7281 unsigned char str[MAX_MULTIBYTE_LENGTH];
7282 /* Convert a single-byte string to multibyte
7283 for the *Message* buffer. */
7284 for (i = 0; i < nbytes; i++)
7286 c = unibyte_char_to_multibyte (msg[i]);
7287 char_bytes = CHAR_STRING (c, str);
7288 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7291 else if (nbytes)
7292 insert_1 (m, nbytes, 1, 0, 0);
7294 if (nlflag)
7296 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7297 insert_1 ("\n", 1, 1, 0, 0);
7299 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7300 this_bol = PT;
7301 this_bol_byte = PT_BYTE;
7303 /* See if this line duplicates the previous one.
7304 If so, combine duplicates. */
7305 if (this_bol > BEG)
7307 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7308 prev_bol = PT;
7309 prev_bol_byte = PT_BYTE;
7311 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7312 this_bol, this_bol_byte);
7313 if (dup)
7315 del_range_both (prev_bol, prev_bol_byte,
7316 this_bol, this_bol_byte, 0);
7317 if (dup > 1)
7319 char dupstr[40];
7320 int duplen;
7322 /* If you change this format, don't forget to also
7323 change message_log_check_duplicate. */
7324 sprintf (dupstr, " [%d times]", dup);
7325 duplen = strlen (dupstr);
7326 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7327 insert_1 (dupstr, duplen, 1, 0, 1);
7332 /* If we have more than the desired maximum number of lines
7333 in the *Messages* buffer now, delete the oldest ones.
7334 This is safe because we don't have undo in this buffer. */
7336 if (NATNUMP (Vmessage_log_max))
7338 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7339 -XFASTINT (Vmessage_log_max) - 1, 0);
7340 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7343 BEGV = XMARKER (oldbegv)->charpos;
7344 BEGV_BYTE = marker_byte_position (oldbegv);
7346 if (zv_at_end)
7348 ZV = Z;
7349 ZV_BYTE = Z_BYTE;
7351 else
7353 ZV = XMARKER (oldzv)->charpos;
7354 ZV_BYTE = marker_byte_position (oldzv);
7357 if (point_at_end)
7358 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7359 else
7360 /* We can't do Fgoto_char (oldpoint) because it will run some
7361 Lisp code. */
7362 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7363 XMARKER (oldpoint)->bytepos);
7365 UNGCPRO;
7366 unchain_marker (XMARKER (oldpoint));
7367 unchain_marker (XMARKER (oldbegv));
7368 unchain_marker (XMARKER (oldzv));
7370 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7371 set_buffer_internal (oldbuf);
7372 if (NILP (tem))
7373 windows_or_buffers_changed = old_windows_or_buffers_changed;
7374 message_log_need_newline = !nlflag;
7375 Vdeactivate_mark = old_deactivate_mark;
7380 /* We are at the end of the buffer after just having inserted a newline.
7381 (Note: We depend on the fact we won't be crossing the gap.)
7382 Check to see if the most recent message looks a lot like the previous one.
7383 Return 0 if different, 1 if the new one should just replace it, or a
7384 value N > 1 if we should also append " [N times]". */
7386 static int
7387 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7388 int prev_bol, this_bol;
7389 int prev_bol_byte, this_bol_byte;
7391 int i;
7392 int len = Z_BYTE - 1 - this_bol_byte;
7393 int seen_dots = 0;
7394 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7395 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7397 for (i = 0; i < len; i++)
7399 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7400 seen_dots = 1;
7401 if (p1[i] != p2[i])
7402 return seen_dots;
7404 p1 += len;
7405 if (*p1 == '\n')
7406 return 2;
7407 if (*p1++ == ' ' && *p1++ == '[')
7409 int n = 0;
7410 while (*p1 >= '0' && *p1 <= '9')
7411 n = n * 10 + *p1++ - '0';
7412 if (strncmp (p1, " times]\n", 8) == 0)
7413 return n+1;
7415 return 0;
7419 /* Display an echo area message M with a specified length of NBYTES
7420 bytes. The string may include null characters. If M is 0, clear
7421 out any existing message, and let the mini-buffer text show
7422 through.
7424 This may GC, so the buffer M must NOT point to a Lisp string. */
7426 void
7427 message2 (m, nbytes, multibyte)
7428 const char *m;
7429 int nbytes;
7430 int multibyte;
7432 /* First flush out any partial line written with print. */
7433 message_log_maybe_newline ();
7434 if (m)
7435 message_dolog (m, nbytes, 1, multibyte);
7436 message2_nolog (m, nbytes, multibyte);
7440 /* The non-logging counterpart of message2. */
7442 void
7443 message2_nolog (m, nbytes, multibyte)
7444 const char *m;
7445 int nbytes, multibyte;
7447 struct frame *sf = SELECTED_FRAME ();
7448 message_enable_multibyte = multibyte;
7450 if (noninteractive)
7452 if (noninteractive_need_newline)
7453 putc ('\n', stderr);
7454 noninteractive_need_newline = 0;
7455 if (m)
7456 fwrite (m, nbytes, 1, stderr);
7457 if (cursor_in_echo_area == 0)
7458 fprintf (stderr, "\n");
7459 fflush (stderr);
7461 /* A null message buffer means that the frame hasn't really been
7462 initialized yet. Error messages get reported properly by
7463 cmd_error, so this must be just an informative message; toss it. */
7464 else if (INTERACTIVE
7465 && sf->glyphs_initialized_p
7466 && FRAME_MESSAGE_BUF (sf))
7468 Lisp_Object mini_window;
7469 struct frame *f;
7471 /* Get the frame containing the mini-buffer
7472 that the selected frame is using. */
7473 mini_window = FRAME_MINIBUF_WINDOW (sf);
7474 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7476 FRAME_SAMPLE_VISIBILITY (f);
7477 if (FRAME_VISIBLE_P (sf)
7478 && ! FRAME_VISIBLE_P (f))
7479 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7481 if (m)
7483 set_message (m, Qnil, nbytes, multibyte);
7484 if (minibuffer_auto_raise)
7485 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7487 else
7488 clear_message (1, 1);
7490 do_pending_window_change (0);
7491 echo_area_display (1);
7492 do_pending_window_change (0);
7493 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7494 (*frame_up_to_date_hook) (f);
7499 /* Display an echo area message M with a specified length of NBYTES
7500 bytes. The string may include null characters. If M is not a
7501 string, clear out any existing message, and let the mini-buffer
7502 text show through.
7504 This function cancels echoing. */
7506 void
7507 message3 (m, nbytes, multibyte)
7508 Lisp_Object m;
7509 int nbytes;
7510 int multibyte;
7512 struct gcpro gcpro1;
7514 GCPRO1 (m);
7515 clear_message (1,1);
7516 cancel_echoing ();
7518 /* First flush out any partial line written with print. */
7519 message_log_maybe_newline ();
7520 if (STRINGP (m))
7522 char *buffer;
7523 USE_SAFE_ALLOCA;
7525 SAFE_ALLOCA (buffer, char *, nbytes);
7526 bcopy (SDATA (m), buffer, nbytes);
7527 message_dolog (buffer, nbytes, 1, multibyte);
7528 SAFE_FREE ();
7530 message3_nolog (m, nbytes, multibyte);
7532 UNGCPRO;
7536 /* The non-logging version of message3.
7537 This does not cancel echoing, because it is used for echoing.
7538 Perhaps we need to make a separate function for echoing
7539 and make this cancel echoing. */
7541 void
7542 message3_nolog (m, nbytes, multibyte)
7543 Lisp_Object m;
7544 int nbytes, multibyte;
7546 struct frame *sf = SELECTED_FRAME ();
7547 message_enable_multibyte = multibyte;
7549 if (noninteractive)
7551 if (noninteractive_need_newline)
7552 putc ('\n', stderr);
7553 noninteractive_need_newline = 0;
7554 if (STRINGP (m))
7555 fwrite (SDATA (m), nbytes, 1, stderr);
7556 if (cursor_in_echo_area == 0)
7557 fprintf (stderr, "\n");
7558 fflush (stderr);
7560 /* A null message buffer means that the frame hasn't really been
7561 initialized yet. Error messages get reported properly by
7562 cmd_error, so this must be just an informative message; toss it. */
7563 else if (INTERACTIVE
7564 && sf->glyphs_initialized_p
7565 && FRAME_MESSAGE_BUF (sf))
7567 Lisp_Object mini_window;
7568 Lisp_Object frame;
7569 struct frame *f;
7571 /* Get the frame containing the mini-buffer
7572 that the selected frame is using. */
7573 mini_window = FRAME_MINIBUF_WINDOW (sf);
7574 frame = XWINDOW (mini_window)->frame;
7575 f = XFRAME (frame);
7577 FRAME_SAMPLE_VISIBILITY (f);
7578 if (FRAME_VISIBLE_P (sf)
7579 && !FRAME_VISIBLE_P (f))
7580 Fmake_frame_visible (frame);
7582 if (STRINGP (m) && SCHARS (m) > 0)
7584 set_message (NULL, m, nbytes, multibyte);
7585 if (minibuffer_auto_raise)
7586 Fraise_frame (frame);
7587 /* Assume we are not echoing.
7588 (If we are, echo_now will override this.) */
7589 echo_message_buffer = Qnil;
7591 else
7592 clear_message (1, 1);
7594 do_pending_window_change (0);
7595 echo_area_display (1);
7596 do_pending_window_change (0);
7597 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7598 (*frame_up_to_date_hook) (f);
7603 /* Display a null-terminated echo area message M. If M is 0, clear
7604 out any existing message, and let the mini-buffer text show through.
7606 The buffer M must continue to exist until after the echo area gets
7607 cleared or some other message gets displayed there. Do not pass
7608 text that is stored in a Lisp string. Do not pass text in a buffer
7609 that was alloca'd. */
7611 void
7612 message1 (m)
7613 char *m;
7615 message2 (m, (m ? strlen (m) : 0), 0);
7619 /* The non-logging counterpart of message1. */
7621 void
7622 message1_nolog (m)
7623 char *m;
7625 message2_nolog (m, (m ? strlen (m) : 0), 0);
7628 /* Display a message M which contains a single %s
7629 which gets replaced with STRING. */
7631 void
7632 message_with_string (m, string, log)
7633 char *m;
7634 Lisp_Object string;
7635 int log;
7637 CHECK_STRING (string);
7639 if (noninteractive)
7641 if (m)
7643 if (noninteractive_need_newline)
7644 putc ('\n', stderr);
7645 noninteractive_need_newline = 0;
7646 fprintf (stderr, m, SDATA (string));
7647 if (cursor_in_echo_area == 0)
7648 fprintf (stderr, "\n");
7649 fflush (stderr);
7652 else if (INTERACTIVE)
7654 /* The frame whose minibuffer we're going to display the message on.
7655 It may be larger than the selected frame, so we need
7656 to use its buffer, not the selected frame's buffer. */
7657 Lisp_Object mini_window;
7658 struct frame *f, *sf = SELECTED_FRAME ();
7660 /* Get the frame containing the minibuffer
7661 that the selected frame is using. */
7662 mini_window = FRAME_MINIBUF_WINDOW (sf);
7663 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7665 /* A null message buffer means that the frame hasn't really been
7666 initialized yet. Error messages get reported properly by
7667 cmd_error, so this must be just an informative message; toss it. */
7668 if (FRAME_MESSAGE_BUF (f))
7670 Lisp_Object args[2], message;
7671 struct gcpro gcpro1, gcpro2;
7673 args[0] = build_string (m);
7674 args[1] = message = string;
7675 GCPRO2 (args[0], message);
7676 gcpro1.nvars = 2;
7678 message = Fformat (2, args);
7680 if (log)
7681 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7682 else
7683 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7685 UNGCPRO;
7687 /* Print should start at the beginning of the message
7688 buffer next time. */
7689 message_buf_print = 0;
7695 /* Dump an informative message to the minibuf. If M is 0, clear out
7696 any existing message, and let the mini-buffer text show through. */
7698 /* VARARGS 1 */
7699 void
7700 message (m, a1, a2, a3)
7701 char *m;
7702 EMACS_INT a1, a2, a3;
7704 if (noninteractive)
7706 if (m)
7708 if (noninteractive_need_newline)
7709 putc ('\n', stderr);
7710 noninteractive_need_newline = 0;
7711 fprintf (stderr, m, a1, a2, a3);
7712 if (cursor_in_echo_area == 0)
7713 fprintf (stderr, "\n");
7714 fflush (stderr);
7717 else if (INTERACTIVE)
7719 /* The frame whose mini-buffer we're going to display the message
7720 on. It may be larger than the selected frame, so we need to
7721 use its buffer, not the selected frame's buffer. */
7722 Lisp_Object mini_window;
7723 struct frame *f, *sf = SELECTED_FRAME ();
7725 /* Get the frame containing the mini-buffer
7726 that the selected frame is using. */
7727 mini_window = FRAME_MINIBUF_WINDOW (sf);
7728 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7730 /* A null message buffer means that the frame hasn't really been
7731 initialized yet. Error messages get reported properly by
7732 cmd_error, so this must be just an informative message; toss
7733 it. */
7734 if (FRAME_MESSAGE_BUF (f))
7736 if (m)
7738 int len;
7739 #ifdef NO_ARG_ARRAY
7740 char *a[3];
7741 a[0] = (char *) a1;
7742 a[1] = (char *) a2;
7743 a[2] = (char *) a3;
7745 len = doprnt (FRAME_MESSAGE_BUF (f),
7746 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7747 #else
7748 len = doprnt (FRAME_MESSAGE_BUF (f),
7749 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7750 (char **) &a1);
7751 #endif /* NO_ARG_ARRAY */
7753 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7755 else
7756 message1 (0);
7758 /* Print should start at the beginning of the message
7759 buffer next time. */
7760 message_buf_print = 0;
7766 /* The non-logging version of message. */
7768 void
7769 message_nolog (m, a1, a2, a3)
7770 char *m;
7771 EMACS_INT a1, a2, a3;
7773 Lisp_Object old_log_max;
7774 old_log_max = Vmessage_log_max;
7775 Vmessage_log_max = Qnil;
7776 message (m, a1, a2, a3);
7777 Vmessage_log_max = old_log_max;
7781 /* Display the current message in the current mini-buffer. This is
7782 only called from error handlers in process.c, and is not time
7783 critical. */
7785 void
7786 update_echo_area ()
7788 if (!NILP (echo_area_buffer[0]))
7790 Lisp_Object string;
7791 string = Fcurrent_message ();
7792 message3 (string, SBYTES (string),
7793 !NILP (current_buffer->enable_multibyte_characters));
7798 /* Make sure echo area buffers in `echo_buffers' are live.
7799 If they aren't, make new ones. */
7801 static void
7802 ensure_echo_area_buffers ()
7804 int i;
7806 for (i = 0; i < 2; ++i)
7807 if (!BUFFERP (echo_buffer[i])
7808 || NILP (XBUFFER (echo_buffer[i])->name))
7810 char name[30];
7811 Lisp_Object old_buffer;
7812 int j;
7814 old_buffer = echo_buffer[i];
7815 sprintf (name, " *Echo Area %d*", i);
7816 echo_buffer[i] = Fget_buffer_create (build_string (name));
7817 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7819 for (j = 0; j < 2; ++j)
7820 if (EQ (old_buffer, echo_area_buffer[j]))
7821 echo_area_buffer[j] = echo_buffer[i];
7826 /* Call FN with args A1..A4 with either the current or last displayed
7827 echo_area_buffer as current buffer.
7829 WHICH zero means use the current message buffer
7830 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7831 from echo_buffer[] and clear it.
7833 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7834 suitable buffer from echo_buffer[] and clear it.
7836 Value is what FN returns. */
7838 static int
7839 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7840 struct window *w;
7841 int which;
7842 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7843 EMACS_INT a1;
7844 Lisp_Object a2;
7845 EMACS_INT a3, a4;
7847 Lisp_Object buffer;
7848 int this_one, the_other, clear_buffer_p, rc;
7849 int count = SPECPDL_INDEX ();
7851 /* If buffers aren't live, make new ones. */
7852 ensure_echo_area_buffers ();
7854 clear_buffer_p = 0;
7856 if (which == 0)
7857 this_one = 0, the_other = 1;
7858 else if (which > 0)
7859 this_one = 1, the_other = 0;
7861 /* Choose a suitable buffer from echo_buffer[] is we don't
7862 have one. */
7863 if (NILP (echo_area_buffer[this_one]))
7865 echo_area_buffer[this_one]
7866 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7867 ? echo_buffer[the_other]
7868 : echo_buffer[this_one]);
7869 clear_buffer_p = 1;
7872 buffer = echo_area_buffer[this_one];
7874 /* Don't get confused by reusing the buffer used for echoing
7875 for a different purpose. */
7876 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7877 cancel_echoing ();
7879 record_unwind_protect (unwind_with_echo_area_buffer,
7880 with_echo_area_buffer_unwind_data (w));
7882 /* Make the echo area buffer current. Note that for display
7883 purposes, it is not necessary that the displayed window's buffer
7884 == current_buffer, except for text property lookup. So, let's
7885 only set that buffer temporarily here without doing a full
7886 Fset_window_buffer. We must also change w->pointm, though,
7887 because otherwise an assertions in unshow_buffer fails, and Emacs
7888 aborts. */
7889 set_buffer_internal_1 (XBUFFER (buffer));
7890 if (w)
7892 w->buffer = buffer;
7893 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7896 current_buffer->undo_list = Qt;
7897 current_buffer->read_only = Qnil;
7898 specbind (Qinhibit_read_only, Qt);
7899 specbind (Qinhibit_modification_hooks, Qt);
7901 if (clear_buffer_p && Z > BEG)
7902 del_range (BEG, Z);
7904 xassert (BEGV >= BEG);
7905 xassert (ZV <= Z && ZV >= BEGV);
7907 rc = fn (a1, a2, a3, a4);
7909 xassert (BEGV >= BEG);
7910 xassert (ZV <= Z && ZV >= BEGV);
7912 unbind_to (count, Qnil);
7913 return rc;
7917 /* Save state that should be preserved around the call to the function
7918 FN called in with_echo_area_buffer. */
7920 static Lisp_Object
7921 with_echo_area_buffer_unwind_data (w)
7922 struct window *w;
7924 int i = 0;
7925 Lisp_Object vector;
7927 /* Reduce consing by keeping one vector in
7928 Vwith_echo_area_save_vector. */
7929 vector = Vwith_echo_area_save_vector;
7930 Vwith_echo_area_save_vector = Qnil;
7932 if (NILP (vector))
7933 vector = Fmake_vector (make_number (7), Qnil);
7935 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7936 AREF (vector, i) = Vdeactivate_mark, ++i;
7937 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7939 if (w)
7941 XSETWINDOW (AREF (vector, i), w); ++i;
7942 AREF (vector, i) = w->buffer; ++i;
7943 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7944 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7946 else
7948 int end = i + 4;
7949 for (; i < end; ++i)
7950 AREF (vector, i) = Qnil;
7953 xassert (i == ASIZE (vector));
7954 return vector;
7958 /* Restore global state from VECTOR which was created by
7959 with_echo_area_buffer_unwind_data. */
7961 static Lisp_Object
7962 unwind_with_echo_area_buffer (vector)
7963 Lisp_Object vector;
7965 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7966 Vdeactivate_mark = AREF (vector, 1);
7967 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7969 if (WINDOWP (AREF (vector, 3)))
7971 struct window *w;
7972 Lisp_Object buffer, charpos, bytepos;
7974 w = XWINDOW (AREF (vector, 3));
7975 buffer = AREF (vector, 4);
7976 charpos = AREF (vector, 5);
7977 bytepos = AREF (vector, 6);
7979 w->buffer = buffer;
7980 set_marker_both (w->pointm, buffer,
7981 XFASTINT (charpos), XFASTINT (bytepos));
7984 Vwith_echo_area_save_vector = vector;
7985 return Qnil;
7989 /* Set up the echo area for use by print functions. MULTIBYTE_P
7990 non-zero means we will print multibyte. */
7992 void
7993 setup_echo_area_for_printing (multibyte_p)
7994 int multibyte_p;
7996 /* If we can't find an echo area any more, exit. */
7997 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7998 Fkill_emacs (Qnil);
8000 ensure_echo_area_buffers ();
8002 if (!message_buf_print)
8004 /* A message has been output since the last time we printed.
8005 Choose a fresh echo area buffer. */
8006 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8007 echo_area_buffer[0] = echo_buffer[1];
8008 else
8009 echo_area_buffer[0] = echo_buffer[0];
8011 /* Switch to that buffer and clear it. */
8012 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8013 current_buffer->truncate_lines = Qnil;
8015 if (Z > BEG)
8017 int count = SPECPDL_INDEX ();
8018 specbind (Qinhibit_read_only, Qt);
8019 /* Note that undo recording is always disabled. */
8020 del_range (BEG, Z);
8021 unbind_to (count, Qnil);
8023 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8025 /* Set up the buffer for the multibyteness we need. */
8026 if (multibyte_p
8027 != !NILP (current_buffer->enable_multibyte_characters))
8028 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8030 /* Raise the frame containing the echo area. */
8031 if (minibuffer_auto_raise)
8033 struct frame *sf = SELECTED_FRAME ();
8034 Lisp_Object mini_window;
8035 mini_window = FRAME_MINIBUF_WINDOW (sf);
8036 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8039 message_log_maybe_newline ();
8040 message_buf_print = 1;
8042 else
8044 if (NILP (echo_area_buffer[0]))
8046 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8047 echo_area_buffer[0] = echo_buffer[1];
8048 else
8049 echo_area_buffer[0] = echo_buffer[0];
8052 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8054 /* Someone switched buffers between print requests. */
8055 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8056 current_buffer->truncate_lines = Qnil;
8062 /* Display an echo area message in window W. Value is non-zero if W's
8063 height is changed. If display_last_displayed_message_p is
8064 non-zero, display the message that was last displayed, otherwise
8065 display the current message. */
8067 static int
8068 display_echo_area (w)
8069 struct window *w;
8071 int i, no_message_p, window_height_changed_p, count;
8073 /* Temporarily disable garbage collections while displaying the echo
8074 area. This is done because a GC can print a message itself.
8075 That message would modify the echo area buffer's contents while a
8076 redisplay of the buffer is going on, and seriously confuse
8077 redisplay. */
8078 count = inhibit_garbage_collection ();
8080 /* If there is no message, we must call display_echo_area_1
8081 nevertheless because it resizes the window. But we will have to
8082 reset the echo_area_buffer in question to nil at the end because
8083 with_echo_area_buffer will sets it to an empty buffer. */
8084 i = display_last_displayed_message_p ? 1 : 0;
8085 no_message_p = NILP (echo_area_buffer[i]);
8087 window_height_changed_p
8088 = with_echo_area_buffer (w, display_last_displayed_message_p,
8089 display_echo_area_1,
8090 (EMACS_INT) w, Qnil, 0, 0);
8092 if (no_message_p)
8093 echo_area_buffer[i] = Qnil;
8095 unbind_to (count, Qnil);
8096 return window_height_changed_p;
8100 /* Helper for display_echo_area. Display the current buffer which
8101 contains the current echo area message in window W, a mini-window,
8102 a pointer to which is passed in A1. A2..A4 are currently not used.
8103 Change the height of W so that all of the message is displayed.
8104 Value is non-zero if height of W was changed. */
8106 static int
8107 display_echo_area_1 (a1, a2, a3, a4)
8108 EMACS_INT a1;
8109 Lisp_Object a2;
8110 EMACS_INT a3, a4;
8112 struct window *w = (struct window *) a1;
8113 Lisp_Object window;
8114 struct text_pos start;
8115 int window_height_changed_p = 0;
8117 /* Do this before displaying, so that we have a large enough glyph
8118 matrix for the display. If we can't get enough space for the
8119 whole text, display the last N lines. That works by setting w->start. */
8120 window_height_changed_p = resize_mini_window (w, 0);
8122 /* Use the starting position chosen by resize_mini_window. */
8123 SET_TEXT_POS_FROM_MARKER (start, w->start);
8125 /* Display. */
8126 clear_glyph_matrix (w->desired_matrix);
8127 XSETWINDOW (window, w);
8128 try_window (window, start, 0);
8130 return window_height_changed_p;
8134 /* Resize the echo area window to exactly the size needed for the
8135 currently displayed message, if there is one. If a mini-buffer
8136 is active, don't shrink it. */
8138 void
8139 resize_echo_area_exactly ()
8141 if (BUFFERP (echo_area_buffer[0])
8142 && WINDOWP (echo_area_window))
8144 struct window *w = XWINDOW (echo_area_window);
8145 int resized_p;
8146 Lisp_Object resize_exactly;
8148 if (minibuf_level == 0)
8149 resize_exactly = Qt;
8150 else
8151 resize_exactly = Qnil;
8153 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8154 (EMACS_INT) w, resize_exactly, 0, 0);
8155 if (resized_p)
8157 ++windows_or_buffers_changed;
8158 ++update_mode_lines;
8159 redisplay_internal (0);
8165 /* Callback function for with_echo_area_buffer, when used from
8166 resize_echo_area_exactly. A1 contains a pointer to the window to
8167 resize, EXACTLY non-nil means resize the mini-window exactly to the
8168 size of the text displayed. A3 and A4 are not used. Value is what
8169 resize_mini_window returns. */
8171 static int
8172 resize_mini_window_1 (a1, exactly, a3, a4)
8173 EMACS_INT a1;
8174 Lisp_Object exactly;
8175 EMACS_INT a3, a4;
8177 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8181 /* Resize mini-window W to fit the size of its contents. EXACT:P
8182 means size the window exactly to the size needed. Otherwise, it's
8183 only enlarged until W's buffer is empty.
8185 Set W->start to the right place to begin display. If the whole
8186 contents fit, start at the beginning. Otherwise, start so as
8187 to make the end of the contents appear. This is particularly
8188 important for y-or-n-p, but seems desirable generally.
8190 Value is non-zero if the window height has been changed. */
8193 resize_mini_window (w, exact_p)
8194 struct window *w;
8195 int exact_p;
8197 struct frame *f = XFRAME (w->frame);
8198 int window_height_changed_p = 0;
8200 xassert (MINI_WINDOW_P (w));
8202 /* By default, start display at the beginning. */
8203 set_marker_both (w->start, w->buffer,
8204 BUF_BEGV (XBUFFER (w->buffer)),
8205 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8207 /* Don't resize windows while redisplaying a window; it would
8208 confuse redisplay functions when the size of the window they are
8209 displaying changes from under them. Such a resizing can happen,
8210 for instance, when which-func prints a long message while
8211 we are running fontification-functions. We're running these
8212 functions with safe_call which binds inhibit-redisplay to t. */
8213 if (!NILP (Vinhibit_redisplay))
8214 return 0;
8216 /* Nil means don't try to resize. */
8217 if (NILP (Vresize_mini_windows)
8218 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8219 return 0;
8221 if (!FRAME_MINIBUF_ONLY_P (f))
8223 struct it it;
8224 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8225 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8226 int height, max_height;
8227 int unit = FRAME_LINE_HEIGHT (f);
8228 struct text_pos start;
8229 struct buffer *old_current_buffer = NULL;
8231 if (current_buffer != XBUFFER (w->buffer))
8233 old_current_buffer = current_buffer;
8234 set_buffer_internal (XBUFFER (w->buffer));
8237 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8239 /* Compute the max. number of lines specified by the user. */
8240 if (FLOATP (Vmax_mini_window_height))
8241 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8242 else if (INTEGERP (Vmax_mini_window_height))
8243 max_height = XINT (Vmax_mini_window_height);
8244 else
8245 max_height = total_height / 4;
8247 /* Correct that max. height if it's bogus. */
8248 max_height = max (1, max_height);
8249 max_height = min (total_height, max_height);
8251 /* Find out the height of the text in the window. */
8252 if (it.truncate_lines_p)
8253 height = 1;
8254 else
8256 last_height = 0;
8257 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8258 if (it.max_ascent == 0 && it.max_descent == 0)
8259 height = it.current_y + last_height;
8260 else
8261 height = it.current_y + it.max_ascent + it.max_descent;
8262 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8263 height = (height + unit - 1) / unit;
8266 /* Compute a suitable window start. */
8267 if (height > max_height)
8269 height = max_height;
8270 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8271 move_it_vertically_backward (&it, (height - 1) * unit);
8272 start = it.current.pos;
8274 else
8275 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8276 SET_MARKER_FROM_TEXT_POS (w->start, start);
8278 if (EQ (Vresize_mini_windows, Qgrow_only))
8280 /* Let it grow only, until we display an empty message, in which
8281 case the window shrinks again. */
8282 if (height > WINDOW_TOTAL_LINES (w))
8284 int old_height = WINDOW_TOTAL_LINES (w);
8285 freeze_window_starts (f, 1);
8286 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8287 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8289 else if (height < WINDOW_TOTAL_LINES (w)
8290 && (exact_p || BEGV == ZV))
8292 int old_height = WINDOW_TOTAL_LINES (w);
8293 freeze_window_starts (f, 0);
8294 shrink_mini_window (w);
8295 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8298 else
8300 /* Always resize to exact size needed. */
8301 if (height > WINDOW_TOTAL_LINES (w))
8303 int old_height = WINDOW_TOTAL_LINES (w);
8304 freeze_window_starts (f, 1);
8305 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8306 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8308 else if (height < WINDOW_TOTAL_LINES (w))
8310 int old_height = WINDOW_TOTAL_LINES (w);
8311 freeze_window_starts (f, 0);
8312 shrink_mini_window (w);
8314 if (height)
8316 freeze_window_starts (f, 1);
8317 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8320 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8324 if (old_current_buffer)
8325 set_buffer_internal (old_current_buffer);
8328 return window_height_changed_p;
8332 /* Value is the current message, a string, or nil if there is no
8333 current message. */
8335 Lisp_Object
8336 current_message ()
8338 Lisp_Object msg;
8340 if (NILP (echo_area_buffer[0]))
8341 msg = Qnil;
8342 else
8344 with_echo_area_buffer (0, 0, current_message_1,
8345 (EMACS_INT) &msg, Qnil, 0, 0);
8346 if (NILP (msg))
8347 echo_area_buffer[0] = Qnil;
8350 return msg;
8354 static int
8355 current_message_1 (a1, a2, a3, a4)
8356 EMACS_INT a1;
8357 Lisp_Object a2;
8358 EMACS_INT a3, a4;
8360 Lisp_Object *msg = (Lisp_Object *) a1;
8362 if (Z > BEG)
8363 *msg = make_buffer_string (BEG, Z, 1);
8364 else
8365 *msg = Qnil;
8366 return 0;
8370 /* Push the current message on Vmessage_stack for later restauration
8371 by restore_message. Value is non-zero if the current message isn't
8372 empty. This is a relatively infrequent operation, so it's not
8373 worth optimizing. */
8376 push_message ()
8378 Lisp_Object msg;
8379 msg = current_message ();
8380 Vmessage_stack = Fcons (msg, Vmessage_stack);
8381 return STRINGP (msg);
8385 /* Restore message display from the top of Vmessage_stack. */
8387 void
8388 restore_message ()
8390 Lisp_Object msg;
8392 xassert (CONSP (Vmessage_stack));
8393 msg = XCAR (Vmessage_stack);
8394 if (STRINGP (msg))
8395 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8396 else
8397 message3_nolog (msg, 0, 0);
8401 /* Handler for record_unwind_protect calling pop_message. */
8403 Lisp_Object
8404 pop_message_unwind (dummy)
8405 Lisp_Object dummy;
8407 pop_message ();
8408 return Qnil;
8411 /* Pop the top-most entry off Vmessage_stack. */
8413 void
8414 pop_message ()
8416 xassert (CONSP (Vmessage_stack));
8417 Vmessage_stack = XCDR (Vmessage_stack);
8421 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8422 exits. If the stack is not empty, we have a missing pop_message
8423 somewhere. */
8425 void
8426 check_message_stack ()
8428 if (!NILP (Vmessage_stack))
8429 abort ();
8433 /* Truncate to NCHARS what will be displayed in the echo area the next
8434 time we display it---but don't redisplay it now. */
8436 void
8437 truncate_echo_area (nchars)
8438 int nchars;
8440 if (nchars == 0)
8441 echo_area_buffer[0] = Qnil;
8442 /* A null message buffer means that the frame hasn't really been
8443 initialized yet. Error messages get reported properly by
8444 cmd_error, so this must be just an informative message; toss it. */
8445 else if (!noninteractive
8446 && INTERACTIVE
8447 && !NILP (echo_area_buffer[0]))
8449 struct frame *sf = SELECTED_FRAME ();
8450 if (FRAME_MESSAGE_BUF (sf))
8451 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8456 /* Helper function for truncate_echo_area. Truncate the current
8457 message to at most NCHARS characters. */
8459 static int
8460 truncate_message_1 (nchars, a2, a3, a4)
8461 EMACS_INT nchars;
8462 Lisp_Object a2;
8463 EMACS_INT a3, a4;
8465 if (BEG + nchars < Z)
8466 del_range (BEG + nchars, Z);
8467 if (Z == BEG)
8468 echo_area_buffer[0] = Qnil;
8469 return 0;
8473 /* Set the current message to a substring of S or STRING.
8475 If STRING is a Lisp string, set the message to the first NBYTES
8476 bytes from STRING. NBYTES zero means use the whole string. If
8477 STRING is multibyte, the message will be displayed multibyte.
8479 If S is not null, set the message to the first LEN bytes of S. LEN
8480 zero means use the whole string. MULTIBYTE_P non-zero means S is
8481 multibyte. Display the message multibyte in that case.
8483 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8484 to t before calling set_message_1 (which calls insert).
8487 void
8488 set_message (s, string, nbytes, multibyte_p)
8489 const char *s;
8490 Lisp_Object string;
8491 int nbytes, multibyte_p;
8493 message_enable_multibyte
8494 = ((s && multibyte_p)
8495 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8497 with_echo_area_buffer (0, 0, set_message_1,
8498 (EMACS_INT) s, string, nbytes, multibyte_p);
8499 message_buf_print = 0;
8500 help_echo_showing_p = 0;
8504 /* Helper function for set_message. Arguments have the same meaning
8505 as there, with A1 corresponding to S and A2 corresponding to STRING
8506 This function is called with the echo area buffer being
8507 current. */
8509 static int
8510 set_message_1 (a1, a2, nbytes, multibyte_p)
8511 EMACS_INT a1;
8512 Lisp_Object a2;
8513 EMACS_INT nbytes, multibyte_p;
8515 const char *s = (const char *) a1;
8516 Lisp_Object string = a2;
8518 /* Change multibyteness of the echo buffer appropriately. */
8519 if (message_enable_multibyte
8520 != !NILP (current_buffer->enable_multibyte_characters))
8521 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8523 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8525 /* Insert new message at BEG. */
8526 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8527 Ferase_buffer ();
8529 if (STRINGP (string))
8531 int nchars;
8533 if (nbytes == 0)
8534 nbytes = SBYTES (string);
8535 nchars = string_byte_to_char (string, nbytes);
8537 /* This function takes care of single/multibyte conversion. We
8538 just have to ensure that the echo area buffer has the right
8539 setting of enable_multibyte_characters. */
8540 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8542 else if (s)
8544 if (nbytes == 0)
8545 nbytes = strlen (s);
8547 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8549 /* Convert from multi-byte to single-byte. */
8550 int i, c, n;
8551 unsigned char work[1];
8553 /* Convert a multibyte string to single-byte. */
8554 for (i = 0; i < nbytes; i += n)
8556 c = string_char_and_length (s + i, nbytes - i, &n);
8557 work[0] = (SINGLE_BYTE_CHAR_P (c)
8559 : multibyte_char_to_unibyte (c, Qnil));
8560 insert_1_both (work, 1, 1, 1, 0, 0);
8563 else if (!multibyte_p
8564 && !NILP (current_buffer->enable_multibyte_characters))
8566 /* Convert from single-byte to multi-byte. */
8567 int i, c, n;
8568 const unsigned char *msg = (const unsigned char *) s;
8569 unsigned char str[MAX_MULTIBYTE_LENGTH];
8571 /* Convert a single-byte string to multibyte. */
8572 for (i = 0; i < nbytes; i++)
8574 c = unibyte_char_to_multibyte (msg[i]);
8575 n = CHAR_STRING (c, str);
8576 insert_1_both (str, 1, n, 1, 0, 0);
8579 else
8580 insert_1 (s, nbytes, 1, 0, 0);
8583 return 0;
8587 /* Clear messages. CURRENT_P non-zero means clear the current
8588 message. LAST_DISPLAYED_P non-zero means clear the message
8589 last displayed. */
8591 void
8592 clear_message (current_p, last_displayed_p)
8593 int current_p, last_displayed_p;
8595 if (current_p)
8597 echo_area_buffer[0] = Qnil;
8598 message_cleared_p = 1;
8601 if (last_displayed_p)
8602 echo_area_buffer[1] = Qnil;
8604 message_buf_print = 0;
8607 /* Clear garbaged frames.
8609 This function is used where the old redisplay called
8610 redraw_garbaged_frames which in turn called redraw_frame which in
8611 turn called clear_frame. The call to clear_frame was a source of
8612 flickering. I believe a clear_frame is not necessary. It should
8613 suffice in the new redisplay to invalidate all current matrices,
8614 and ensure a complete redisplay of all windows. */
8616 static void
8617 clear_garbaged_frames ()
8619 if (frame_garbaged)
8621 Lisp_Object tail, frame;
8622 int changed_count = 0;
8624 FOR_EACH_FRAME (tail, frame)
8626 struct frame *f = XFRAME (frame);
8628 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8630 if (f->resized_p)
8632 Fredraw_frame (frame);
8633 f->force_flush_display_p = 1;
8635 clear_current_matrices (f);
8636 changed_count++;
8637 f->garbaged = 0;
8638 f->resized_p = 0;
8642 frame_garbaged = 0;
8643 if (changed_count)
8644 ++windows_or_buffers_changed;
8649 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8650 is non-zero update selected_frame. Value is non-zero if the
8651 mini-windows height has been changed. */
8653 static int
8654 echo_area_display (update_frame_p)
8655 int update_frame_p;
8657 Lisp_Object mini_window;
8658 struct window *w;
8659 struct frame *f;
8660 int window_height_changed_p = 0;
8661 struct frame *sf = SELECTED_FRAME ();
8663 mini_window = FRAME_MINIBUF_WINDOW (sf);
8664 w = XWINDOW (mini_window);
8665 f = XFRAME (WINDOW_FRAME (w));
8667 /* Don't display if frame is invisible or not yet initialized. */
8668 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8669 return 0;
8671 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8672 #ifndef MAC_OS8
8673 #ifdef HAVE_WINDOW_SYSTEM
8674 /* When Emacs starts, selected_frame may be a visible terminal
8675 frame, even if we run under a window system. If we let this
8676 through, a message would be displayed on the terminal. */
8677 if (EQ (selected_frame, Vterminal_frame)
8678 && !NILP (Vwindow_system))
8679 return 0;
8680 #endif /* HAVE_WINDOW_SYSTEM */
8681 #endif
8683 /* Redraw garbaged frames. */
8684 if (frame_garbaged)
8685 clear_garbaged_frames ();
8687 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8689 echo_area_window = mini_window;
8690 window_height_changed_p = display_echo_area (w);
8691 w->must_be_updated_p = 1;
8693 /* Update the display, unless called from redisplay_internal.
8694 Also don't update the screen during redisplay itself. The
8695 update will happen at the end of redisplay, and an update
8696 here could cause confusion. */
8697 if (update_frame_p && !redisplaying_p)
8699 int n = 0;
8701 /* If the display update has been interrupted by pending
8702 input, update mode lines in the frame. Due to the
8703 pending input, it might have been that redisplay hasn't
8704 been called, so that mode lines above the echo area are
8705 garbaged. This looks odd, so we prevent it here. */
8706 if (!display_completed)
8707 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8709 if (window_height_changed_p
8710 /* Don't do this if Emacs is shutting down. Redisplay
8711 needs to run hooks. */
8712 && !NILP (Vrun_hooks))
8714 /* Must update other windows. Likewise as in other
8715 cases, don't let this update be interrupted by
8716 pending input. */
8717 int count = SPECPDL_INDEX ();
8718 specbind (Qredisplay_dont_pause, Qt);
8719 windows_or_buffers_changed = 1;
8720 redisplay_internal (0);
8721 unbind_to (count, Qnil);
8723 else if (FRAME_WINDOW_P (f) && n == 0)
8725 /* Window configuration is the same as before.
8726 Can do with a display update of the echo area,
8727 unless we displayed some mode lines. */
8728 update_single_window (w, 1);
8729 rif->flush_display (f);
8731 else
8732 update_frame (f, 1, 1);
8734 /* If cursor is in the echo area, make sure that the next
8735 redisplay displays the minibuffer, so that the cursor will
8736 be replaced with what the minibuffer wants. */
8737 if (cursor_in_echo_area)
8738 ++windows_or_buffers_changed;
8741 else if (!EQ (mini_window, selected_window))
8742 windows_or_buffers_changed++;
8744 /* The current message is now also the last one displayed. */
8745 echo_area_buffer[1] = echo_area_buffer[0];
8747 /* Prevent redisplay optimization in redisplay_internal by resetting
8748 this_line_start_pos. This is done because the mini-buffer now
8749 displays the message instead of its buffer text. */
8750 if (EQ (mini_window, selected_window))
8751 CHARPOS (this_line_start_pos) = 0;
8753 return window_height_changed_p;
8758 /***********************************************************************
8759 Mode Lines and Frame Titles
8760 ***********************************************************************/
8762 /* A buffer for constructing non-propertized mode-line strings and
8763 frame titles in it; allocated from the heap in init_xdisp and
8764 resized as needed in store_mode_line_noprop_char. */
8766 static char *mode_line_noprop_buf;
8768 /* The buffer's end, and a current output position in it. */
8770 static char *mode_line_noprop_buf_end;
8771 static char *mode_line_noprop_ptr;
8773 #define MODE_LINE_NOPROP_LEN(start) \
8774 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8776 static enum {
8777 MODE_LINE_DISPLAY = 0,
8778 MODE_LINE_TITLE,
8779 MODE_LINE_NOPROP,
8780 MODE_LINE_STRING
8781 } mode_line_target;
8783 /* Alist that caches the results of :propertize.
8784 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8785 static Lisp_Object mode_line_proptrans_alist;
8787 /* List of strings making up the mode-line. */
8788 static Lisp_Object mode_line_string_list;
8790 /* Base face property when building propertized mode line string. */
8791 static Lisp_Object mode_line_string_face;
8792 static Lisp_Object mode_line_string_face_prop;
8795 /* Unwind data for mode line strings */
8797 static Lisp_Object Vmode_line_unwind_vector;
8799 static Lisp_Object
8800 format_mode_line_unwind_data (obuf, save_proptrans)
8801 struct buffer *obuf;
8803 Lisp_Object vector;
8805 /* Reduce consing by keeping one vector in
8806 Vwith_echo_area_save_vector. */
8807 vector = Vmode_line_unwind_vector;
8808 Vmode_line_unwind_vector = Qnil;
8810 if (NILP (vector))
8811 vector = Fmake_vector (make_number (7), Qnil);
8813 AREF (vector, 0) = make_number (mode_line_target);
8814 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8815 AREF (vector, 2) = mode_line_string_list;
8816 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8817 AREF (vector, 4) = mode_line_string_face;
8818 AREF (vector, 5) = mode_line_string_face_prop;
8820 if (obuf)
8821 XSETBUFFER (AREF (vector, 6), obuf);
8822 else
8823 AREF (vector, 6) = Qnil;
8825 return vector;
8828 static Lisp_Object
8829 unwind_format_mode_line (vector)
8830 Lisp_Object vector;
8832 mode_line_target = XINT (AREF (vector, 0));
8833 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8834 mode_line_string_list = AREF (vector, 2);
8835 if (! EQ (AREF (vector, 3), Qt))
8836 mode_line_proptrans_alist = AREF (vector, 3);
8837 mode_line_string_face = AREF (vector, 4);
8838 mode_line_string_face_prop = AREF (vector, 5);
8840 if (!NILP (AREF (vector, 6)))
8842 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8843 AREF (vector, 6) = Qnil;
8846 Vmode_line_unwind_vector = vector;
8847 return Qnil;
8851 /* Store a single character C for the frame title in mode_line_noprop_buf.
8852 Re-allocate mode_line_noprop_buf if necessary. */
8854 static void
8855 #ifdef PROTOTYPES
8856 store_mode_line_noprop_char (char c)
8857 #else
8858 store_mode_line_noprop_char (c)
8859 char c;
8860 #endif
8862 /* If output position has reached the end of the allocated buffer,
8863 double the buffer's size. */
8864 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8866 int len = MODE_LINE_NOPROP_LEN (0);
8867 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8868 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8869 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8870 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8873 *mode_line_noprop_ptr++ = c;
8877 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8878 mode_line_noprop_ptr. STR is the string to store. Do not copy
8879 characters that yield more columns than PRECISION; PRECISION <= 0
8880 means copy the whole string. Pad with spaces until FIELD_WIDTH
8881 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8882 pad. Called from display_mode_element when it is used to build a
8883 frame title. */
8885 static int
8886 store_mode_line_noprop (str, field_width, precision)
8887 const unsigned char *str;
8888 int field_width, precision;
8890 int n = 0;
8891 int dummy, nbytes;
8893 /* Copy at most PRECISION chars from STR. */
8894 nbytes = strlen (str);
8895 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8896 while (nbytes--)
8897 store_mode_line_noprop_char (*str++);
8899 /* Fill up with spaces until FIELD_WIDTH reached. */
8900 while (field_width > 0
8901 && n < field_width)
8903 store_mode_line_noprop_char (' ');
8904 ++n;
8907 return n;
8910 /***********************************************************************
8911 Frame Titles
8912 ***********************************************************************/
8914 #ifdef HAVE_WINDOW_SYSTEM
8916 /* Set the title of FRAME, if it has changed. The title format is
8917 Vicon_title_format if FRAME is iconified, otherwise it is
8918 frame_title_format. */
8920 static void
8921 x_consider_frame_title (frame)
8922 Lisp_Object frame;
8924 struct frame *f = XFRAME (frame);
8926 if (FRAME_WINDOW_P (f)
8927 || FRAME_MINIBUF_ONLY_P (f)
8928 || f->explicit_name)
8930 /* Do we have more than one visible frame on this X display? */
8931 Lisp_Object tail;
8932 Lisp_Object fmt;
8933 int title_start;
8934 char *title;
8935 int len;
8936 struct it it;
8937 int count = SPECPDL_INDEX ();
8939 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8941 Lisp_Object other_frame = XCAR (tail);
8942 struct frame *tf = XFRAME (other_frame);
8944 if (tf != f
8945 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8946 && !FRAME_MINIBUF_ONLY_P (tf)
8947 && !EQ (other_frame, tip_frame)
8948 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8949 break;
8952 /* Set global variable indicating that multiple frames exist. */
8953 multiple_frames = CONSP (tail);
8955 /* Switch to the buffer of selected window of the frame. Set up
8956 mode_line_target so that display_mode_element will output into
8957 mode_line_noprop_buf; then display the title. */
8958 record_unwind_protect (unwind_format_mode_line,
8959 format_mode_line_unwind_data (current_buffer, 0));
8961 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8962 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8964 mode_line_target = MODE_LINE_TITLE;
8965 title_start = MODE_LINE_NOPROP_LEN (0);
8966 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8967 NULL, DEFAULT_FACE_ID);
8968 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8969 len = MODE_LINE_NOPROP_LEN (title_start);
8970 title = mode_line_noprop_buf + title_start;
8971 unbind_to (count, Qnil);
8973 /* Set the title only if it's changed. This avoids consing in
8974 the common case where it hasn't. (If it turns out that we've
8975 already wasted too much time by walking through the list with
8976 display_mode_element, then we might need to optimize at a
8977 higher level than this.) */
8978 if (! STRINGP (f->name)
8979 || SBYTES (f->name) != len
8980 || bcmp (title, SDATA (f->name), len) != 0)
8981 x_implicitly_set_name (f, make_string (title, len), Qnil);
8985 #endif /* not HAVE_WINDOW_SYSTEM */
8990 /***********************************************************************
8991 Menu Bars
8992 ***********************************************************************/
8995 /* Prepare for redisplay by updating menu-bar item lists when
8996 appropriate. This can call eval. */
8998 void
8999 prepare_menu_bars ()
9001 int all_windows;
9002 struct gcpro gcpro1, gcpro2;
9003 struct frame *f;
9004 Lisp_Object tooltip_frame;
9006 #ifdef HAVE_WINDOW_SYSTEM
9007 tooltip_frame = tip_frame;
9008 #else
9009 tooltip_frame = Qnil;
9010 #endif
9012 /* Update all frame titles based on their buffer names, etc. We do
9013 this before the menu bars so that the buffer-menu will show the
9014 up-to-date frame titles. */
9015 #ifdef HAVE_WINDOW_SYSTEM
9016 if (windows_or_buffers_changed || update_mode_lines)
9018 Lisp_Object tail, frame;
9020 FOR_EACH_FRAME (tail, frame)
9022 f = XFRAME (frame);
9023 if (!EQ (frame, tooltip_frame)
9024 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9025 x_consider_frame_title (frame);
9028 #endif /* HAVE_WINDOW_SYSTEM */
9030 /* Update the menu bar item lists, if appropriate. This has to be
9031 done before any actual redisplay or generation of display lines. */
9032 all_windows = (update_mode_lines
9033 || buffer_shared > 1
9034 || windows_or_buffers_changed);
9035 if (all_windows)
9037 Lisp_Object tail, frame;
9038 int count = SPECPDL_INDEX ();
9039 /* 1 means that update_menu_bar has run its hooks
9040 so any further calls to update_menu_bar shouldn't do so again. */
9041 int menu_bar_hooks_run = 0;
9043 record_unwind_save_match_data ();
9045 FOR_EACH_FRAME (tail, frame)
9047 f = XFRAME (frame);
9049 /* Ignore tooltip frame. */
9050 if (EQ (frame, tooltip_frame))
9051 continue;
9053 /* If a window on this frame changed size, report that to
9054 the user and clear the size-change flag. */
9055 if (FRAME_WINDOW_SIZES_CHANGED (f))
9057 Lisp_Object functions;
9059 /* Clear flag first in case we get an error below. */
9060 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9061 functions = Vwindow_size_change_functions;
9062 GCPRO2 (tail, functions);
9064 while (CONSP (functions))
9066 call1 (XCAR (functions), frame);
9067 functions = XCDR (functions);
9069 UNGCPRO;
9072 GCPRO1 (tail);
9073 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9074 #ifdef HAVE_WINDOW_SYSTEM
9075 update_tool_bar (f, 0);
9076 #ifdef MAC_OS
9077 mac_update_title_bar (f, 0);
9078 #endif
9079 #endif
9080 UNGCPRO;
9083 unbind_to (count, Qnil);
9085 else
9087 struct frame *sf = SELECTED_FRAME ();
9088 update_menu_bar (sf, 1, 0);
9089 #ifdef HAVE_WINDOW_SYSTEM
9090 update_tool_bar (sf, 1);
9091 #ifdef MAC_OS
9092 mac_update_title_bar (sf, 1);
9093 #endif
9094 #endif
9097 /* Motif needs this. See comment in xmenu.c. Turn it off when
9098 pending_menu_activation is not defined. */
9099 #ifdef USE_X_TOOLKIT
9100 pending_menu_activation = 0;
9101 #endif
9105 /* Update the menu bar item list for frame F. This has to be done
9106 before we start to fill in any display lines, because it can call
9107 eval.
9109 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9111 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9112 already ran the menu bar hooks for this redisplay, so there
9113 is no need to run them again. The return value is the
9114 updated value of this flag, to pass to the next call. */
9116 static int
9117 update_menu_bar (f, save_match_data, hooks_run)
9118 struct frame *f;
9119 int save_match_data;
9120 int hooks_run;
9122 Lisp_Object window;
9123 register struct window *w;
9125 /* If called recursively during a menu update, do nothing. This can
9126 happen when, for instance, an activate-menubar-hook causes a
9127 redisplay. */
9128 if (inhibit_menubar_update)
9129 return;
9131 window = FRAME_SELECTED_WINDOW (f);
9132 w = XWINDOW (window);
9134 #if 0 /* The if statement below this if statement used to include the
9135 condition !NILP (w->update_mode_line), rather than using
9136 update_mode_lines directly, and this if statement may have
9137 been added to make that condition work. Now the if
9138 statement below matches its comment, this isn't needed. */
9139 if (update_mode_lines)
9140 w->update_mode_line = Qt;
9141 #endif
9143 if (FRAME_WINDOW_P (f)
9145 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9146 || defined (USE_GTK)
9147 FRAME_EXTERNAL_MENU_BAR (f)
9148 #else
9149 FRAME_MENU_BAR_LINES (f) > 0
9150 #endif
9151 : FRAME_MENU_BAR_LINES (f) > 0)
9153 /* If the user has switched buffers or windows, we need to
9154 recompute to reflect the new bindings. But we'll
9155 recompute when update_mode_lines is set too; that means
9156 that people can use force-mode-line-update to request
9157 that the menu bar be recomputed. The adverse effect on
9158 the rest of the redisplay algorithm is about the same as
9159 windows_or_buffers_changed anyway. */
9160 if (windows_or_buffers_changed
9161 /* This used to test w->update_mode_line, but we believe
9162 there is no need to recompute the menu in that case. */
9163 || update_mode_lines
9164 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9165 < BUF_MODIFF (XBUFFER (w->buffer)))
9166 != !NILP (w->last_had_star))
9167 || ((!NILP (Vtransient_mark_mode)
9168 && !NILP (XBUFFER (w->buffer)->mark_active))
9169 != !NILP (w->region_showing)))
9171 struct buffer *prev = current_buffer;
9172 int count = SPECPDL_INDEX ();
9174 specbind (Qinhibit_menubar_update, Qt);
9176 set_buffer_internal_1 (XBUFFER (w->buffer));
9177 if (save_match_data)
9178 record_unwind_save_match_data ();
9179 if (NILP (Voverriding_local_map_menu_flag))
9181 specbind (Qoverriding_terminal_local_map, Qnil);
9182 specbind (Qoverriding_local_map, Qnil);
9185 if (!hooks_run)
9187 /* Run the Lucid hook. */
9188 safe_run_hooks (Qactivate_menubar_hook);
9190 /* If it has changed current-menubar from previous value,
9191 really recompute the menu-bar from the value. */
9192 if (! NILP (Vlucid_menu_bar_dirty_flag))
9193 call0 (Qrecompute_lucid_menubar);
9195 safe_run_hooks (Qmenu_bar_update_hook);
9197 hooks_run = 1;
9200 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9202 /* Redisplay the menu bar in case we changed it. */
9203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9204 || defined (USE_GTK)
9205 if (FRAME_WINDOW_P (f))
9207 #ifdef MAC_OS
9208 /* All frames on Mac OS share the same menubar. So only
9209 the selected frame should be allowed to set it. */
9210 if (f == SELECTED_FRAME ())
9211 #endif
9212 set_frame_menubar (f, 0, 0);
9214 else
9215 /* On a terminal screen, the menu bar is an ordinary screen
9216 line, and this makes it get updated. */
9217 w->update_mode_line = Qt;
9218 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9219 /* In the non-toolkit version, the menu bar is an ordinary screen
9220 line, and this makes it get updated. */
9221 w->update_mode_line = Qt;
9222 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9224 unbind_to (count, Qnil);
9225 set_buffer_internal_1 (prev);
9229 return hooks_run;
9234 /***********************************************************************
9235 Output Cursor
9236 ***********************************************************************/
9238 #ifdef HAVE_WINDOW_SYSTEM
9240 /* EXPORT:
9241 Nominal cursor position -- where to draw output.
9242 HPOS and VPOS are window relative glyph matrix coordinates.
9243 X and Y are window relative pixel coordinates. */
9245 struct cursor_pos output_cursor;
9248 /* EXPORT:
9249 Set the global variable output_cursor to CURSOR. All cursor
9250 positions are relative to updated_window. */
9252 void
9253 set_output_cursor (cursor)
9254 struct cursor_pos *cursor;
9256 output_cursor.hpos = cursor->hpos;
9257 output_cursor.vpos = cursor->vpos;
9258 output_cursor.x = cursor->x;
9259 output_cursor.y = cursor->y;
9263 /* EXPORT for RIF:
9264 Set a nominal cursor position.
9266 HPOS and VPOS are column/row positions in a window glyph matrix. X
9267 and Y are window text area relative pixel positions.
9269 If this is done during an update, updated_window will contain the
9270 window that is being updated and the position is the future output
9271 cursor position for that window. If updated_window is null, use
9272 selected_window and display the cursor at the given position. */
9274 void
9275 x_cursor_to (vpos, hpos, y, x)
9276 int vpos, hpos, y, x;
9278 struct window *w;
9280 /* If updated_window is not set, work on selected_window. */
9281 if (updated_window)
9282 w = updated_window;
9283 else
9284 w = XWINDOW (selected_window);
9286 /* Set the output cursor. */
9287 output_cursor.hpos = hpos;
9288 output_cursor.vpos = vpos;
9289 output_cursor.x = x;
9290 output_cursor.y = y;
9292 /* If not called as part of an update, really display the cursor.
9293 This will also set the cursor position of W. */
9294 if (updated_window == NULL)
9296 BLOCK_INPUT;
9297 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9298 if (rif->flush_display_optional)
9299 rif->flush_display_optional (SELECTED_FRAME ());
9300 UNBLOCK_INPUT;
9304 #endif /* HAVE_WINDOW_SYSTEM */
9307 /***********************************************************************
9308 Tool-bars
9309 ***********************************************************************/
9311 #ifdef HAVE_WINDOW_SYSTEM
9313 /* Where the mouse was last time we reported a mouse event. */
9315 FRAME_PTR last_mouse_frame;
9317 /* Tool-bar item index of the item on which a mouse button was pressed
9318 or -1. */
9320 int last_tool_bar_item;
9323 /* Update the tool-bar item list for frame F. This has to be done
9324 before we start to fill in any display lines. Called from
9325 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9326 and restore it here. */
9328 static void
9329 update_tool_bar (f, save_match_data)
9330 struct frame *f;
9331 int save_match_data;
9333 #ifdef USE_GTK
9334 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9335 #else
9336 int do_update = WINDOWP (f->tool_bar_window)
9337 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9338 #endif
9340 if (do_update)
9342 Lisp_Object window;
9343 struct window *w;
9345 window = FRAME_SELECTED_WINDOW (f);
9346 w = XWINDOW (window);
9348 /* If the user has switched buffers or windows, we need to
9349 recompute to reflect the new bindings. But we'll
9350 recompute when update_mode_lines is set too; that means
9351 that people can use force-mode-line-update to request
9352 that the menu bar be recomputed. The adverse effect on
9353 the rest of the redisplay algorithm is about the same as
9354 windows_or_buffers_changed anyway. */
9355 if (windows_or_buffers_changed
9356 || !NILP (w->update_mode_line)
9357 || update_mode_lines
9358 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9359 < BUF_MODIFF (XBUFFER (w->buffer)))
9360 != !NILP (w->last_had_star))
9361 || ((!NILP (Vtransient_mark_mode)
9362 && !NILP (XBUFFER (w->buffer)->mark_active))
9363 != !NILP (w->region_showing)))
9365 struct buffer *prev = current_buffer;
9366 int count = SPECPDL_INDEX ();
9367 Lisp_Object new_tool_bar;
9368 int new_n_tool_bar;
9369 struct gcpro gcpro1;
9371 /* Set current_buffer to the buffer of the selected
9372 window of the frame, so that we get the right local
9373 keymaps. */
9374 set_buffer_internal_1 (XBUFFER (w->buffer));
9376 /* Save match data, if we must. */
9377 if (save_match_data)
9378 record_unwind_save_match_data ();
9380 /* Make sure that we don't accidentally use bogus keymaps. */
9381 if (NILP (Voverriding_local_map_menu_flag))
9383 specbind (Qoverriding_terminal_local_map, Qnil);
9384 specbind (Qoverriding_local_map, Qnil);
9387 GCPRO1 (new_tool_bar);
9389 /* Build desired tool-bar items from keymaps. */
9390 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9391 &new_n_tool_bar);
9393 /* Redisplay the tool-bar if we changed it. */
9394 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9396 /* Redisplay that happens asynchronously due to an expose event
9397 may access f->tool_bar_items. Make sure we update both
9398 variables within BLOCK_INPUT so no such event interrupts. */
9399 BLOCK_INPUT;
9400 f->tool_bar_items = new_tool_bar;
9401 f->n_tool_bar_items = new_n_tool_bar;
9402 w->update_mode_line = Qt;
9403 UNBLOCK_INPUT;
9406 UNGCPRO;
9408 unbind_to (count, Qnil);
9409 set_buffer_internal_1 (prev);
9415 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9416 F's desired tool-bar contents. F->tool_bar_items must have
9417 been set up previously by calling prepare_menu_bars. */
9419 static void
9420 build_desired_tool_bar_string (f)
9421 struct frame *f;
9423 int i, size, size_needed;
9424 struct gcpro gcpro1, gcpro2, gcpro3;
9425 Lisp_Object image, plist, props;
9427 image = plist = props = Qnil;
9428 GCPRO3 (image, plist, props);
9430 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9431 Otherwise, make a new string. */
9433 /* The size of the string we might be able to reuse. */
9434 size = (STRINGP (f->desired_tool_bar_string)
9435 ? SCHARS (f->desired_tool_bar_string)
9436 : 0);
9438 /* We need one space in the string for each image. */
9439 size_needed = f->n_tool_bar_items;
9441 /* Reuse f->desired_tool_bar_string, if possible. */
9442 if (size < size_needed || NILP (f->desired_tool_bar_string))
9443 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9444 make_number (' '));
9445 else
9447 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9448 Fremove_text_properties (make_number (0), make_number (size),
9449 props, f->desired_tool_bar_string);
9452 /* Put a `display' property on the string for the images to display,
9453 put a `menu_item' property on tool-bar items with a value that
9454 is the index of the item in F's tool-bar item vector. */
9455 for (i = 0; i < f->n_tool_bar_items; ++i)
9457 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9459 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9460 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9461 int hmargin, vmargin, relief, idx, end;
9462 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9464 /* If image is a vector, choose the image according to the
9465 button state. */
9466 image = PROP (TOOL_BAR_ITEM_IMAGES);
9467 if (VECTORP (image))
9469 if (enabled_p)
9470 idx = (selected_p
9471 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9472 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9473 else
9474 idx = (selected_p
9475 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9476 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9478 xassert (ASIZE (image) >= idx);
9479 image = AREF (image, idx);
9481 else
9482 idx = -1;
9484 /* Ignore invalid image specifications. */
9485 if (!valid_image_p (image))
9486 continue;
9488 /* Display the tool-bar button pressed, or depressed. */
9489 plist = Fcopy_sequence (XCDR (image));
9491 /* Compute margin and relief to draw. */
9492 relief = (tool_bar_button_relief >= 0
9493 ? tool_bar_button_relief
9494 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9495 hmargin = vmargin = relief;
9497 if (INTEGERP (Vtool_bar_button_margin)
9498 && XINT (Vtool_bar_button_margin) > 0)
9500 hmargin += XFASTINT (Vtool_bar_button_margin);
9501 vmargin += XFASTINT (Vtool_bar_button_margin);
9503 else if (CONSP (Vtool_bar_button_margin))
9505 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9506 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9507 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9509 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9510 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9511 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9514 if (auto_raise_tool_bar_buttons_p)
9516 /* Add a `:relief' property to the image spec if the item is
9517 selected. */
9518 if (selected_p)
9520 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9521 hmargin -= relief;
9522 vmargin -= relief;
9525 else
9527 /* If image is selected, display it pressed, i.e. with a
9528 negative relief. If it's not selected, display it with a
9529 raised relief. */
9530 plist = Fplist_put (plist, QCrelief,
9531 (selected_p
9532 ? make_number (-relief)
9533 : make_number (relief)));
9534 hmargin -= relief;
9535 vmargin -= relief;
9538 /* Put a margin around the image. */
9539 if (hmargin || vmargin)
9541 if (hmargin == vmargin)
9542 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9543 else
9544 plist = Fplist_put (plist, QCmargin,
9545 Fcons (make_number (hmargin),
9546 make_number (vmargin)));
9549 /* If button is not enabled, and we don't have special images
9550 for the disabled state, make the image appear disabled by
9551 applying an appropriate algorithm to it. */
9552 if (!enabled_p && idx < 0)
9553 plist = Fplist_put (plist, QCconversion, Qdisabled);
9555 /* Put a `display' text property on the string for the image to
9556 display. Put a `menu-item' property on the string that gives
9557 the start of this item's properties in the tool-bar items
9558 vector. */
9559 image = Fcons (Qimage, plist);
9560 props = list4 (Qdisplay, image,
9561 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9563 /* Let the last image hide all remaining spaces in the tool bar
9564 string. The string can be longer than needed when we reuse a
9565 previous string. */
9566 if (i + 1 == f->n_tool_bar_items)
9567 end = SCHARS (f->desired_tool_bar_string);
9568 else
9569 end = i + 1;
9570 Fadd_text_properties (make_number (i), make_number (end),
9571 props, f->desired_tool_bar_string);
9572 #undef PROP
9575 UNGCPRO;
9579 /* Display one line of the tool-bar of frame IT->f.
9581 HEIGHT specifies the desired height of the tool-bar line.
9582 If the actual height of the glyph row is less than HEIGHT, the
9583 row's height is increased to HEIGHT, and the icons are centered
9584 vertically in the new height.
9586 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9587 count a final empty row in case the tool-bar width exactly matches
9588 the window width.
9591 static void
9592 display_tool_bar_line (it, height)
9593 struct it *it;
9594 int height;
9596 struct glyph_row *row = it->glyph_row;
9597 int max_x = it->last_visible_x;
9598 struct glyph *last;
9600 prepare_desired_row (row);
9601 row->y = it->current_y;
9603 /* Note that this isn't made use of if the face hasn't a box,
9604 so there's no need to check the face here. */
9605 it->start_of_box_run_p = 1;
9607 while (it->current_x < max_x)
9609 int x, n_glyphs_before, i, nglyphs;
9610 struct it it_before;
9612 /* Get the next display element. */
9613 if (!get_next_display_element (it))
9615 /* Don't count empty row if we are counting needed tool-bar lines. */
9616 if (height < 0 && !it->hpos)
9617 return;
9618 break;
9621 /* Produce glyphs. */
9622 n_glyphs_before = row->used[TEXT_AREA];
9623 it_before = *it;
9625 PRODUCE_GLYPHS (it);
9627 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9628 i = 0;
9629 x = it_before.current_x;
9630 while (i < nglyphs)
9632 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9634 if (x + glyph->pixel_width > max_x)
9636 /* Glyph doesn't fit on line. Backtrack. */
9637 row->used[TEXT_AREA] = n_glyphs_before;
9638 *it = it_before;
9639 /* If this is the only glyph on this line, it will never fit on the
9640 toolbar, so skip it. But ensure there is at least one glyph,
9641 so we don't accidentally disable the tool-bar. */
9642 if (n_glyphs_before == 0
9643 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9644 break;
9645 goto out;
9648 ++it->hpos;
9649 x += glyph->pixel_width;
9650 ++i;
9653 /* Stop at line ends. */
9654 if (ITERATOR_AT_END_OF_LINE_P (it))
9655 break;
9657 set_iterator_to_next (it, 1);
9660 out:;
9662 row->displays_text_p = row->used[TEXT_AREA] != 0;
9663 /* Use default face for the border below the tool bar. */
9664 if (!row->displays_text_p)
9665 it->face_id = DEFAULT_FACE_ID;
9666 extend_face_to_end_of_line (it);
9667 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9668 last->right_box_line_p = 1;
9669 if (last == row->glyphs[TEXT_AREA])
9670 last->left_box_line_p = 1;
9672 /* Make line the desired height and center it vertically. */
9673 if ((height -= it->max_ascent + it->max_descent) > 0)
9675 /* Don't add more than one line height. */
9676 height %= FRAME_LINE_HEIGHT (it->f);
9677 it->max_ascent += height / 2;
9678 it->max_descent += (height + 1) / 2;
9681 compute_line_metrics (it);
9683 /* If line is empty, make it occupy the rest of the tool-bar. */
9684 if (!row->displays_text_p)
9686 row->height = row->phys_height = it->last_visible_y - row->y;
9687 row->ascent = row->phys_ascent = 0;
9688 row->extra_line_spacing = 0;
9691 row->full_width_p = 1;
9692 row->continued_p = 0;
9693 row->truncated_on_left_p = 0;
9694 row->truncated_on_right_p = 0;
9696 it->current_x = it->hpos = 0;
9697 it->current_y += row->height;
9698 ++it->vpos;
9699 ++it->glyph_row;
9703 /* Max tool-bar height. */
9705 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9706 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9708 /* Value is the number of screen lines needed to make all tool-bar
9709 items of frame F visible. The number of actual rows needed is
9710 returned in *N_ROWS if non-NULL. */
9712 static int
9713 tool_bar_lines_needed (f, n_rows)
9714 struct frame *f;
9715 int *n_rows;
9717 struct window *w = XWINDOW (f->tool_bar_window);
9718 struct it it;
9719 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9720 the desired matrix, so use (unused) mode-line row as temporary row to
9721 avoid destroying the first tool-bar row. */
9722 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9724 /* Initialize an iterator for iteration over
9725 F->desired_tool_bar_string in the tool-bar window of frame F. */
9726 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9727 it.first_visible_x = 0;
9728 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9729 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9731 while (!ITERATOR_AT_END_P (&it))
9733 clear_glyph_row (temp_row);
9734 it.glyph_row = temp_row;
9735 display_tool_bar_line (&it, -1);
9737 clear_glyph_row (temp_row);
9739 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9740 if (n_rows)
9741 *n_rows = it.vpos > 0 ? it.vpos : -1;
9743 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9747 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9748 0, 1, 0,
9749 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9750 (frame)
9751 Lisp_Object frame;
9753 struct frame *f;
9754 struct window *w;
9755 int nlines = 0;
9757 if (NILP (frame))
9758 frame = selected_frame;
9759 else
9760 CHECK_FRAME (frame);
9761 f = XFRAME (frame);
9763 if (WINDOWP (f->tool_bar_window)
9764 || (w = XWINDOW (f->tool_bar_window),
9765 WINDOW_TOTAL_LINES (w) > 0))
9767 update_tool_bar (f, 1);
9768 if (f->n_tool_bar_items)
9770 build_desired_tool_bar_string (f);
9771 nlines = tool_bar_lines_needed (f, NULL);
9775 return make_number (nlines);
9779 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9780 height should be changed. */
9782 static int
9783 redisplay_tool_bar (f)
9784 struct frame *f;
9786 struct window *w;
9787 struct it it;
9788 struct glyph_row *row;
9789 int change_height_p = 0;
9791 #ifdef USE_GTK
9792 if (FRAME_EXTERNAL_TOOL_BAR (f))
9793 update_frame_tool_bar (f);
9794 return 0;
9795 #endif
9797 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9798 do anything. This means you must start with tool-bar-lines
9799 non-zero to get the auto-sizing effect. Or in other words, you
9800 can turn off tool-bars by specifying tool-bar-lines zero. */
9801 if (!WINDOWP (f->tool_bar_window)
9802 || (w = XWINDOW (f->tool_bar_window),
9803 WINDOW_TOTAL_LINES (w) == 0))
9804 return 0;
9806 /* Set up an iterator for the tool-bar window. */
9807 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9808 it.first_visible_x = 0;
9809 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9810 row = it.glyph_row;
9812 /* Build a string that represents the contents of the tool-bar. */
9813 build_desired_tool_bar_string (f);
9814 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9816 if (f->n_tool_bar_rows == 0)
9818 int nlines;
9820 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9821 nlines != WINDOW_TOTAL_LINES (w)))
9823 extern Lisp_Object Qtool_bar_lines;
9824 Lisp_Object frame;
9825 int old_height = WINDOW_TOTAL_LINES (w);
9827 XSETFRAME (frame, f);
9828 Fmodify_frame_parameters (frame,
9829 Fcons (Fcons (Qtool_bar_lines,
9830 make_number (nlines)),
9831 Qnil));
9832 if (WINDOW_TOTAL_LINES (w) != old_height)
9834 clear_glyph_matrix (w->desired_matrix);
9835 fonts_changed_p = 1;
9836 return 1;
9841 /* Display as many lines as needed to display all tool-bar items. */
9843 if (f->n_tool_bar_rows > 0)
9845 int border, rows, height, extra;
9847 if (INTEGERP (Vtool_bar_border))
9848 border = XINT (Vtool_bar_border);
9849 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9850 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9851 else if (EQ (Vtool_bar_border, Qborder_width))
9852 border = f->border_width;
9853 else
9854 border = 0;
9855 if (border < 0)
9856 border = 0;
9858 rows = f->n_tool_bar_rows;
9859 height = max (1, (it.last_visible_y - border) / rows);
9860 extra = it.last_visible_y - border - height * rows;
9862 while (it.current_y < it.last_visible_y)
9864 int h = 0;
9865 if (extra > 0 && rows-- > 0)
9867 h = (extra + rows - 1) / rows;
9868 extra -= h;
9870 display_tool_bar_line (&it, height + h);
9873 else
9875 while (it.current_y < it.last_visible_y)
9876 display_tool_bar_line (&it, 0);
9879 /* It doesn't make much sense to try scrolling in the tool-bar
9880 window, so don't do it. */
9881 w->desired_matrix->no_scrolling_p = 1;
9882 w->must_be_updated_p = 1;
9884 if (auto_resize_tool_bars_p)
9886 int nlines, nrows;
9887 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9889 /* If we couldn't display everything, change the tool-bar's
9890 height if there is room for more. */
9891 if (IT_STRING_CHARPOS (it) < it.end_charpos
9892 && it.current_y < max_tool_bar_height)
9893 change_height_p = 1;
9895 row = it.glyph_row - 1;
9897 /* If there are blank lines at the end, except for a partially
9898 visible blank line at the end that is smaller than
9899 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9900 if (!row->displays_text_p
9901 && row->height >= FRAME_LINE_HEIGHT (f))
9902 change_height_p = 1;
9904 /* If row displays tool-bar items, but is partially visible,
9905 change the tool-bar's height. */
9906 if (row->displays_text_p
9907 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9908 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9909 change_height_p = 1;
9911 /* Resize windows as needed by changing the `tool-bar-lines'
9912 frame parameter. */
9913 if (change_height_p
9914 && (nlines = tool_bar_lines_needed (f, &nrows),
9915 nlines != WINDOW_TOTAL_LINES (w)))
9917 extern Lisp_Object Qtool_bar_lines;
9918 Lisp_Object frame;
9919 int old_height = WINDOW_TOTAL_LINES (w);
9921 XSETFRAME (frame, f);
9922 Fmodify_frame_parameters (frame,
9923 Fcons (Fcons (Qtool_bar_lines,
9924 make_number (nlines)),
9925 Qnil));
9926 if (WINDOW_TOTAL_LINES (w) != old_height)
9928 clear_glyph_matrix (w->desired_matrix);
9929 f->n_tool_bar_rows = nrows;
9930 fonts_changed_p = 1;
9935 return change_height_p;
9939 /* Get information about the tool-bar item which is displayed in GLYPH
9940 on frame F. Return in *PROP_IDX the index where tool-bar item
9941 properties start in F->tool_bar_items. Value is zero if
9942 GLYPH doesn't display a tool-bar item. */
9944 static int
9945 tool_bar_item_info (f, glyph, prop_idx)
9946 struct frame *f;
9947 struct glyph *glyph;
9948 int *prop_idx;
9950 Lisp_Object prop;
9951 int success_p;
9952 int charpos;
9954 /* This function can be called asynchronously, which means we must
9955 exclude any possibility that Fget_text_property signals an
9956 error. */
9957 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9958 charpos = max (0, charpos);
9960 /* Get the text property `menu-item' at pos. The value of that
9961 property is the start index of this item's properties in
9962 F->tool_bar_items. */
9963 prop = Fget_text_property (make_number (charpos),
9964 Qmenu_item, f->current_tool_bar_string);
9965 if (INTEGERP (prop))
9967 *prop_idx = XINT (prop);
9968 success_p = 1;
9970 else
9971 success_p = 0;
9973 return success_p;
9977 /* Get information about the tool-bar item at position X/Y on frame F.
9978 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9979 the current matrix of the tool-bar window of F, or NULL if not
9980 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9981 item in F->tool_bar_items. Value is
9983 -1 if X/Y is not on a tool-bar item
9984 0 if X/Y is on the same item that was highlighted before.
9985 1 otherwise. */
9987 static int
9988 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9989 struct frame *f;
9990 int x, y;
9991 struct glyph **glyph;
9992 int *hpos, *vpos, *prop_idx;
9994 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9995 struct window *w = XWINDOW (f->tool_bar_window);
9996 int area;
9998 /* Find the glyph under X/Y. */
9999 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10000 if (*glyph == NULL)
10001 return -1;
10003 /* Get the start of this tool-bar item's properties in
10004 f->tool_bar_items. */
10005 if (!tool_bar_item_info (f, *glyph, prop_idx))
10006 return -1;
10008 /* Is mouse on the highlighted item? */
10009 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10010 && *vpos >= dpyinfo->mouse_face_beg_row
10011 && *vpos <= dpyinfo->mouse_face_end_row
10012 && (*vpos > dpyinfo->mouse_face_beg_row
10013 || *hpos >= dpyinfo->mouse_face_beg_col)
10014 && (*vpos < dpyinfo->mouse_face_end_row
10015 || *hpos < dpyinfo->mouse_face_end_col
10016 || dpyinfo->mouse_face_past_end))
10017 return 0;
10019 return 1;
10023 /* EXPORT:
10024 Handle mouse button event on the tool-bar of frame F, at
10025 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10026 0 for button release. MODIFIERS is event modifiers for button
10027 release. */
10029 void
10030 handle_tool_bar_click (f, x, y, down_p, modifiers)
10031 struct frame *f;
10032 int x, y, down_p;
10033 unsigned int modifiers;
10035 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10036 struct window *w = XWINDOW (f->tool_bar_window);
10037 int hpos, vpos, prop_idx;
10038 struct glyph *glyph;
10039 Lisp_Object enabled_p;
10041 /* If not on the highlighted tool-bar item, return. */
10042 frame_to_window_pixel_xy (w, &x, &y);
10043 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10044 return;
10046 /* If item is disabled, do nothing. */
10047 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10048 if (NILP (enabled_p))
10049 return;
10051 if (down_p)
10053 /* Show item in pressed state. */
10054 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10055 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10056 last_tool_bar_item = prop_idx;
10058 else
10060 Lisp_Object key, frame;
10061 struct input_event event;
10062 EVENT_INIT (event);
10064 /* Show item in released state. */
10065 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10066 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10068 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10070 XSETFRAME (frame, f);
10071 event.kind = TOOL_BAR_EVENT;
10072 event.frame_or_window = frame;
10073 event.arg = frame;
10074 kbd_buffer_store_event (&event);
10076 event.kind = TOOL_BAR_EVENT;
10077 event.frame_or_window = frame;
10078 event.arg = key;
10079 event.modifiers = modifiers;
10080 kbd_buffer_store_event (&event);
10081 last_tool_bar_item = -1;
10086 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10087 tool-bar window-relative coordinates X/Y. Called from
10088 note_mouse_highlight. */
10090 static void
10091 note_tool_bar_highlight (f, x, y)
10092 struct frame *f;
10093 int x, y;
10095 Lisp_Object window = f->tool_bar_window;
10096 struct window *w = XWINDOW (window);
10097 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10098 int hpos, vpos;
10099 struct glyph *glyph;
10100 struct glyph_row *row;
10101 int i;
10102 Lisp_Object enabled_p;
10103 int prop_idx;
10104 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10105 int mouse_down_p, rc;
10107 /* Function note_mouse_highlight is called with negative x(y
10108 values when mouse moves outside of the frame. */
10109 if (x <= 0 || y <= 0)
10111 clear_mouse_face (dpyinfo);
10112 return;
10115 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10116 if (rc < 0)
10118 /* Not on tool-bar item. */
10119 clear_mouse_face (dpyinfo);
10120 return;
10122 else if (rc == 0)
10123 /* On same tool-bar item as before. */
10124 goto set_help_echo;
10126 clear_mouse_face (dpyinfo);
10128 /* Mouse is down, but on different tool-bar item? */
10129 mouse_down_p = (dpyinfo->grabbed
10130 && f == last_mouse_frame
10131 && FRAME_LIVE_P (f));
10132 if (mouse_down_p
10133 && last_tool_bar_item != prop_idx)
10134 return;
10136 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10137 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10139 /* If tool-bar item is not enabled, don't highlight it. */
10140 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10141 if (!NILP (enabled_p))
10143 /* Compute the x-position of the glyph. In front and past the
10144 image is a space. We include this in the highlighted area. */
10145 row = MATRIX_ROW (w->current_matrix, vpos);
10146 for (i = x = 0; i < hpos; ++i)
10147 x += row->glyphs[TEXT_AREA][i].pixel_width;
10149 /* Record this as the current active region. */
10150 dpyinfo->mouse_face_beg_col = hpos;
10151 dpyinfo->mouse_face_beg_row = vpos;
10152 dpyinfo->mouse_face_beg_x = x;
10153 dpyinfo->mouse_face_beg_y = row->y;
10154 dpyinfo->mouse_face_past_end = 0;
10156 dpyinfo->mouse_face_end_col = hpos + 1;
10157 dpyinfo->mouse_face_end_row = vpos;
10158 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10159 dpyinfo->mouse_face_end_y = row->y;
10160 dpyinfo->mouse_face_window = window;
10161 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10163 /* Display it as active. */
10164 show_mouse_face (dpyinfo, draw);
10165 dpyinfo->mouse_face_image_state = draw;
10168 set_help_echo:
10170 /* Set help_echo_string to a help string to display for this tool-bar item.
10171 XTread_socket does the rest. */
10172 help_echo_object = help_echo_window = Qnil;
10173 help_echo_pos = -1;
10174 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10175 if (NILP (help_echo_string))
10176 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10179 #endif /* HAVE_WINDOW_SYSTEM */
10183 /************************************************************************
10184 Horizontal scrolling
10185 ************************************************************************/
10187 static int hscroll_window_tree P_ ((Lisp_Object));
10188 static int hscroll_windows P_ ((Lisp_Object));
10190 /* For all leaf windows in the window tree rooted at WINDOW, set their
10191 hscroll value so that PT is (i) visible in the window, and (ii) so
10192 that it is not within a certain margin at the window's left and
10193 right border. Value is non-zero if any window's hscroll has been
10194 changed. */
10196 static int
10197 hscroll_window_tree (window)
10198 Lisp_Object window;
10200 int hscrolled_p = 0;
10201 int hscroll_relative_p = FLOATP (Vhscroll_step);
10202 int hscroll_step_abs = 0;
10203 double hscroll_step_rel = 0;
10205 if (hscroll_relative_p)
10207 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10208 if (hscroll_step_rel < 0)
10210 hscroll_relative_p = 0;
10211 hscroll_step_abs = 0;
10214 else if (INTEGERP (Vhscroll_step))
10216 hscroll_step_abs = XINT (Vhscroll_step);
10217 if (hscroll_step_abs < 0)
10218 hscroll_step_abs = 0;
10220 else
10221 hscroll_step_abs = 0;
10223 while (WINDOWP (window))
10225 struct window *w = XWINDOW (window);
10227 if (WINDOWP (w->hchild))
10228 hscrolled_p |= hscroll_window_tree (w->hchild);
10229 else if (WINDOWP (w->vchild))
10230 hscrolled_p |= hscroll_window_tree (w->vchild);
10231 else if (w->cursor.vpos >= 0)
10233 int h_margin;
10234 int text_area_width;
10235 struct glyph_row *current_cursor_row
10236 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10237 struct glyph_row *desired_cursor_row
10238 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10239 struct glyph_row *cursor_row
10240 = (desired_cursor_row->enabled_p
10241 ? desired_cursor_row
10242 : current_cursor_row);
10244 text_area_width = window_box_width (w, TEXT_AREA);
10246 /* Scroll when cursor is inside this scroll margin. */
10247 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10249 if ((XFASTINT (w->hscroll)
10250 && w->cursor.x <= h_margin)
10251 || (cursor_row->enabled_p
10252 && cursor_row->truncated_on_right_p
10253 && (w->cursor.x >= text_area_width - h_margin)))
10255 struct it it;
10256 int hscroll;
10257 struct buffer *saved_current_buffer;
10258 int pt;
10259 int wanted_x;
10261 /* Find point in a display of infinite width. */
10262 saved_current_buffer = current_buffer;
10263 current_buffer = XBUFFER (w->buffer);
10265 if (w == XWINDOW (selected_window))
10266 pt = BUF_PT (current_buffer);
10267 else
10269 pt = marker_position (w->pointm);
10270 pt = max (BEGV, pt);
10271 pt = min (ZV, pt);
10274 /* Move iterator to pt starting at cursor_row->start in
10275 a line with infinite width. */
10276 init_to_row_start (&it, w, cursor_row);
10277 it.last_visible_x = INFINITY;
10278 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10279 current_buffer = saved_current_buffer;
10281 /* Position cursor in window. */
10282 if (!hscroll_relative_p && hscroll_step_abs == 0)
10283 hscroll = max (0, (it.current_x
10284 - (ITERATOR_AT_END_OF_LINE_P (&it)
10285 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10286 : (text_area_width / 2))))
10287 / FRAME_COLUMN_WIDTH (it.f);
10288 else if (w->cursor.x >= text_area_width - h_margin)
10290 if (hscroll_relative_p)
10291 wanted_x = text_area_width * (1 - hscroll_step_rel)
10292 - h_margin;
10293 else
10294 wanted_x = text_area_width
10295 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10296 - h_margin;
10297 hscroll
10298 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10300 else
10302 if (hscroll_relative_p)
10303 wanted_x = text_area_width * hscroll_step_rel
10304 + h_margin;
10305 else
10306 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10307 + h_margin;
10308 hscroll
10309 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10311 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10313 /* Don't call Fset_window_hscroll if value hasn't
10314 changed because it will prevent redisplay
10315 optimizations. */
10316 if (XFASTINT (w->hscroll) != hscroll)
10318 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10319 w->hscroll = make_number (hscroll);
10320 hscrolled_p = 1;
10325 window = w->next;
10328 /* Value is non-zero if hscroll of any leaf window has been changed. */
10329 return hscrolled_p;
10333 /* Set hscroll so that cursor is visible and not inside horizontal
10334 scroll margins for all windows in the tree rooted at WINDOW. See
10335 also hscroll_window_tree above. Value is non-zero if any window's
10336 hscroll has been changed. If it has, desired matrices on the frame
10337 of WINDOW are cleared. */
10339 static int
10340 hscroll_windows (window)
10341 Lisp_Object window;
10343 int hscrolled_p;
10345 if (automatic_hscrolling_p)
10347 hscrolled_p = hscroll_window_tree (window);
10348 if (hscrolled_p)
10349 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10351 else
10352 hscrolled_p = 0;
10353 return hscrolled_p;
10358 /************************************************************************
10359 Redisplay
10360 ************************************************************************/
10362 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10363 to a non-zero value. This is sometimes handy to have in a debugger
10364 session. */
10366 #if GLYPH_DEBUG
10368 /* First and last unchanged row for try_window_id. */
10370 int debug_first_unchanged_at_end_vpos;
10371 int debug_last_unchanged_at_beg_vpos;
10373 /* Delta vpos and y. */
10375 int debug_dvpos, debug_dy;
10377 /* Delta in characters and bytes for try_window_id. */
10379 int debug_delta, debug_delta_bytes;
10381 /* Values of window_end_pos and window_end_vpos at the end of
10382 try_window_id. */
10384 EMACS_INT debug_end_pos, debug_end_vpos;
10386 /* Append a string to W->desired_matrix->method. FMT is a printf
10387 format string. A1...A9 are a supplement for a variable-length
10388 argument list. If trace_redisplay_p is non-zero also printf the
10389 resulting string to stderr. */
10391 static void
10392 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10393 struct window *w;
10394 char *fmt;
10395 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10397 char buffer[512];
10398 char *method = w->desired_matrix->method;
10399 int len = strlen (method);
10400 int size = sizeof w->desired_matrix->method;
10401 int remaining = size - len - 1;
10403 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10404 if (len && remaining)
10406 method[len] = '|';
10407 --remaining, ++len;
10410 strncpy (method + len, buffer, remaining);
10412 if (trace_redisplay_p)
10413 fprintf (stderr, "%p (%s): %s\n",
10415 ((BUFFERP (w->buffer)
10416 && STRINGP (XBUFFER (w->buffer)->name))
10417 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10418 : "no buffer"),
10419 buffer);
10422 #endif /* GLYPH_DEBUG */
10425 /* Value is non-zero if all changes in window W, which displays
10426 current_buffer, are in the text between START and END. START is a
10427 buffer position, END is given as a distance from Z. Used in
10428 redisplay_internal for display optimization. */
10430 static INLINE int
10431 text_outside_line_unchanged_p (w, start, end)
10432 struct window *w;
10433 int start, end;
10435 int unchanged_p = 1;
10437 /* If text or overlays have changed, see where. */
10438 if (XFASTINT (w->last_modified) < MODIFF
10439 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10441 /* Gap in the line? */
10442 if (GPT < start || Z - GPT < end)
10443 unchanged_p = 0;
10445 /* Changes start in front of the line, or end after it? */
10446 if (unchanged_p
10447 && (BEG_UNCHANGED < start - 1
10448 || END_UNCHANGED < end))
10449 unchanged_p = 0;
10451 /* If selective display, can't optimize if changes start at the
10452 beginning of the line. */
10453 if (unchanged_p
10454 && INTEGERP (current_buffer->selective_display)
10455 && XINT (current_buffer->selective_display) > 0
10456 && (BEG_UNCHANGED < start || GPT <= start))
10457 unchanged_p = 0;
10459 /* If there are overlays at the start or end of the line, these
10460 may have overlay strings with newlines in them. A change at
10461 START, for instance, may actually concern the display of such
10462 overlay strings as well, and they are displayed on different
10463 lines. So, quickly rule out this case. (For the future, it
10464 might be desirable to implement something more telling than
10465 just BEG/END_UNCHANGED.) */
10466 if (unchanged_p)
10468 if (BEG + BEG_UNCHANGED == start
10469 && overlay_touches_p (start))
10470 unchanged_p = 0;
10471 if (END_UNCHANGED == end
10472 && overlay_touches_p (Z - end))
10473 unchanged_p = 0;
10477 return unchanged_p;
10481 /* Do a frame update, taking possible shortcuts into account. This is
10482 the main external entry point for redisplay.
10484 If the last redisplay displayed an echo area message and that message
10485 is no longer requested, we clear the echo area or bring back the
10486 mini-buffer if that is in use. */
10488 void
10489 redisplay ()
10491 redisplay_internal (0);
10495 static Lisp_Object
10496 overlay_arrow_string_or_property (var)
10497 Lisp_Object var;
10499 Lisp_Object val;
10501 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10502 return val;
10504 return Voverlay_arrow_string;
10507 /* Return 1 if there are any overlay-arrows in current_buffer. */
10508 static int
10509 overlay_arrow_in_current_buffer_p ()
10511 Lisp_Object vlist;
10513 for (vlist = Voverlay_arrow_variable_list;
10514 CONSP (vlist);
10515 vlist = XCDR (vlist))
10517 Lisp_Object var = XCAR (vlist);
10518 Lisp_Object val;
10520 if (!SYMBOLP (var))
10521 continue;
10522 val = find_symbol_value (var);
10523 if (MARKERP (val)
10524 && current_buffer == XMARKER (val)->buffer)
10525 return 1;
10527 return 0;
10531 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10532 has changed. */
10534 static int
10535 overlay_arrows_changed_p ()
10537 Lisp_Object vlist;
10539 for (vlist = Voverlay_arrow_variable_list;
10540 CONSP (vlist);
10541 vlist = XCDR (vlist))
10543 Lisp_Object var = XCAR (vlist);
10544 Lisp_Object val, pstr;
10546 if (!SYMBOLP (var))
10547 continue;
10548 val = find_symbol_value (var);
10549 if (!MARKERP (val))
10550 continue;
10551 if (! EQ (COERCE_MARKER (val),
10552 Fget (var, Qlast_arrow_position))
10553 || ! (pstr = overlay_arrow_string_or_property (var),
10554 EQ (pstr, Fget (var, Qlast_arrow_string))))
10555 return 1;
10557 return 0;
10560 /* Mark overlay arrows to be updated on next redisplay. */
10562 static void
10563 update_overlay_arrows (up_to_date)
10564 int up_to_date;
10566 Lisp_Object vlist;
10568 for (vlist = Voverlay_arrow_variable_list;
10569 CONSP (vlist);
10570 vlist = XCDR (vlist))
10572 Lisp_Object var = XCAR (vlist);
10574 if (!SYMBOLP (var))
10575 continue;
10577 if (up_to_date > 0)
10579 Lisp_Object val = find_symbol_value (var);
10580 Fput (var, Qlast_arrow_position,
10581 COERCE_MARKER (val));
10582 Fput (var, Qlast_arrow_string,
10583 overlay_arrow_string_or_property (var));
10585 else if (up_to_date < 0
10586 || !NILP (Fget (var, Qlast_arrow_position)))
10588 Fput (var, Qlast_arrow_position, Qt);
10589 Fput (var, Qlast_arrow_string, Qt);
10595 /* Return overlay arrow string to display at row.
10596 Return integer (bitmap number) for arrow bitmap in left fringe.
10597 Return nil if no overlay arrow. */
10599 static Lisp_Object
10600 overlay_arrow_at_row (it, row)
10601 struct it *it;
10602 struct glyph_row *row;
10604 Lisp_Object vlist;
10606 for (vlist = Voverlay_arrow_variable_list;
10607 CONSP (vlist);
10608 vlist = XCDR (vlist))
10610 Lisp_Object var = XCAR (vlist);
10611 Lisp_Object val;
10613 if (!SYMBOLP (var))
10614 continue;
10616 val = find_symbol_value (var);
10618 if (MARKERP (val)
10619 && current_buffer == XMARKER (val)->buffer
10620 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10622 if (FRAME_WINDOW_P (it->f)
10623 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10625 #ifdef HAVE_WINDOW_SYSTEM
10626 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10628 int fringe_bitmap;
10629 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10630 return make_number (fringe_bitmap);
10632 #endif
10633 return make_number (-1); /* Use default arrow bitmap */
10635 return overlay_arrow_string_or_property (var);
10639 return Qnil;
10642 /* Return 1 if point moved out of or into a composition. Otherwise
10643 return 0. PREV_BUF and PREV_PT are the last point buffer and
10644 position. BUF and PT are the current point buffer and position. */
10647 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10648 struct buffer *prev_buf, *buf;
10649 int prev_pt, pt;
10651 int start, end;
10652 Lisp_Object prop;
10653 Lisp_Object buffer;
10655 XSETBUFFER (buffer, buf);
10656 /* Check a composition at the last point if point moved within the
10657 same buffer. */
10658 if (prev_buf == buf)
10660 if (prev_pt == pt)
10661 /* Point didn't move. */
10662 return 0;
10664 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10665 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10666 && COMPOSITION_VALID_P (start, end, prop)
10667 && start < prev_pt && end > prev_pt)
10668 /* The last point was within the composition. Return 1 iff
10669 point moved out of the composition. */
10670 return (pt <= start || pt >= end);
10673 /* Check a composition at the current point. */
10674 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10675 && find_composition (pt, -1, &start, &end, &prop, buffer)
10676 && COMPOSITION_VALID_P (start, end, prop)
10677 && start < pt && end > pt);
10681 /* Reconsider the setting of B->clip_changed which is displayed
10682 in window W. */
10684 static INLINE void
10685 reconsider_clip_changes (w, b)
10686 struct window *w;
10687 struct buffer *b;
10689 if (b->clip_changed
10690 && !NILP (w->window_end_valid)
10691 && w->current_matrix->buffer == b
10692 && w->current_matrix->zv == BUF_ZV (b)
10693 && w->current_matrix->begv == BUF_BEGV (b))
10694 b->clip_changed = 0;
10696 /* If display wasn't paused, and W is not a tool bar window, see if
10697 point has been moved into or out of a composition. In that case,
10698 we set b->clip_changed to 1 to force updating the screen. If
10699 b->clip_changed has already been set to 1, we can skip this
10700 check. */
10701 if (!b->clip_changed
10702 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10704 int pt;
10706 if (w == XWINDOW (selected_window))
10707 pt = BUF_PT (current_buffer);
10708 else
10709 pt = marker_position (w->pointm);
10711 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10712 || pt != XINT (w->last_point))
10713 && check_point_in_composition (w->current_matrix->buffer,
10714 XINT (w->last_point),
10715 XBUFFER (w->buffer), pt))
10716 b->clip_changed = 1;
10721 /* Select FRAME to forward the values of frame-local variables into C
10722 variables so that the redisplay routines can access those values
10723 directly. */
10725 static void
10726 select_frame_for_redisplay (frame)
10727 Lisp_Object frame;
10729 Lisp_Object tail, sym, val;
10730 Lisp_Object old = selected_frame;
10732 selected_frame = frame;
10734 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10735 if (CONSP (XCAR (tail))
10736 && (sym = XCAR (XCAR (tail)),
10737 SYMBOLP (sym))
10738 && (sym = indirect_variable (sym),
10739 val = SYMBOL_VALUE (sym),
10740 (BUFFER_LOCAL_VALUEP (val)
10741 || SOME_BUFFER_LOCAL_VALUEP (val)))
10742 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10743 /* Use find_symbol_value rather than Fsymbol_value
10744 to avoid an error if it is void. */
10745 find_symbol_value (sym);
10747 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10748 if (CONSP (XCAR (tail))
10749 && (sym = XCAR (XCAR (tail)),
10750 SYMBOLP (sym))
10751 && (sym = indirect_variable (sym),
10752 val = SYMBOL_VALUE (sym),
10753 (BUFFER_LOCAL_VALUEP (val)
10754 || SOME_BUFFER_LOCAL_VALUEP (val)))
10755 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10756 find_symbol_value (sym);
10760 #define STOP_POLLING \
10761 do { if (! polling_stopped_here) stop_polling (); \
10762 polling_stopped_here = 1; } while (0)
10764 #define RESUME_POLLING \
10765 do { if (polling_stopped_here) start_polling (); \
10766 polling_stopped_here = 0; } while (0)
10769 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10770 response to any user action; therefore, we should preserve the echo
10771 area. (Actually, our caller does that job.) Perhaps in the future
10772 avoid recentering windows if it is not necessary; currently that
10773 causes some problems. */
10775 static void
10776 redisplay_internal (preserve_echo_area)
10777 int preserve_echo_area;
10779 struct window *w = XWINDOW (selected_window);
10780 struct frame *f = XFRAME (w->frame);
10781 int pause;
10782 int must_finish = 0;
10783 struct text_pos tlbufpos, tlendpos;
10784 int number_of_visible_frames;
10785 int count;
10786 struct frame *sf = SELECTED_FRAME ();
10787 int polling_stopped_here = 0;
10789 /* Non-zero means redisplay has to consider all windows on all
10790 frames. Zero means, only selected_window is considered. */
10791 int consider_all_windows_p;
10793 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10795 /* No redisplay if running in batch mode or frame is not yet fully
10796 initialized, or redisplay is explicitly turned off by setting
10797 Vinhibit_redisplay. */
10798 if (noninteractive
10799 || !NILP (Vinhibit_redisplay)
10800 || !f->glyphs_initialized_p)
10801 return;
10803 /* The flag redisplay_performed_directly_p is set by
10804 direct_output_for_insert when it already did the whole screen
10805 update necessary. */
10806 if (redisplay_performed_directly_p)
10808 redisplay_performed_directly_p = 0;
10809 if (!hscroll_windows (selected_window))
10810 return;
10813 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10814 if (popup_activated ())
10815 return;
10816 #endif
10818 /* I don't think this happens but let's be paranoid. */
10819 if (redisplaying_p)
10820 return;
10822 /* Record a function that resets redisplaying_p to its old value
10823 when we leave this function. */
10824 count = SPECPDL_INDEX ();
10825 record_unwind_protect (unwind_redisplay,
10826 Fcons (make_number (redisplaying_p), selected_frame));
10827 ++redisplaying_p;
10828 specbind (Qinhibit_free_realized_faces, Qnil);
10831 Lisp_Object tail, frame;
10833 FOR_EACH_FRAME (tail, frame)
10835 struct frame *f = XFRAME (frame);
10836 f->already_hscrolled_p = 0;
10840 retry:
10841 pause = 0;
10842 reconsider_clip_changes (w, current_buffer);
10843 last_escape_glyph_frame = NULL;
10844 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10846 /* If new fonts have been loaded that make a glyph matrix adjustment
10847 necessary, do it. */
10848 if (fonts_changed_p)
10850 adjust_glyphs (NULL);
10851 ++windows_or_buffers_changed;
10852 fonts_changed_p = 0;
10855 /* If face_change_count is non-zero, init_iterator will free all
10856 realized faces, which includes the faces referenced from current
10857 matrices. So, we can't reuse current matrices in this case. */
10858 if (face_change_count)
10859 ++windows_or_buffers_changed;
10861 if (! FRAME_WINDOW_P (sf)
10862 && previous_terminal_frame != sf)
10864 /* Since frames on an ASCII terminal share the same display
10865 area, displaying a different frame means redisplay the whole
10866 thing. */
10867 windows_or_buffers_changed++;
10868 SET_FRAME_GARBAGED (sf);
10869 XSETFRAME (Vterminal_frame, sf);
10871 previous_terminal_frame = sf;
10873 /* Set the visible flags for all frames. Do this before checking
10874 for resized or garbaged frames; they want to know if their frames
10875 are visible. See the comment in frame.h for
10876 FRAME_SAMPLE_VISIBILITY. */
10878 Lisp_Object tail, frame;
10880 number_of_visible_frames = 0;
10882 FOR_EACH_FRAME (tail, frame)
10884 struct frame *f = XFRAME (frame);
10886 FRAME_SAMPLE_VISIBILITY (f);
10887 if (FRAME_VISIBLE_P (f))
10888 ++number_of_visible_frames;
10889 clear_desired_matrices (f);
10893 /* Notice any pending interrupt request to change frame size. */
10894 do_pending_window_change (1);
10896 /* Clear frames marked as garbaged. */
10897 if (frame_garbaged)
10898 clear_garbaged_frames ();
10900 /* Build menubar and tool-bar items. */
10901 if (NILP (Vmemory_full))
10902 prepare_menu_bars ();
10904 if (windows_or_buffers_changed)
10905 update_mode_lines++;
10907 /* Detect case that we need to write or remove a star in the mode line. */
10908 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10910 w->update_mode_line = Qt;
10911 if (buffer_shared > 1)
10912 update_mode_lines++;
10915 /* If %c is in the mode line, update it if needed. */
10916 if (!NILP (w->column_number_displayed)
10917 /* This alternative quickly identifies a common case
10918 where no change is needed. */
10919 && !(PT == XFASTINT (w->last_point)
10920 && XFASTINT (w->last_modified) >= MODIFF
10921 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10922 && (XFASTINT (w->column_number_displayed)
10923 != (int) current_column ())) /* iftc */
10924 w->update_mode_line = Qt;
10926 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10928 /* The variable buffer_shared is set in redisplay_window and
10929 indicates that we redisplay a buffer in different windows. See
10930 there. */
10931 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10932 || cursor_type_changed);
10934 /* If specs for an arrow have changed, do thorough redisplay
10935 to ensure we remove any arrow that should no longer exist. */
10936 if (overlay_arrows_changed_p ())
10937 consider_all_windows_p = windows_or_buffers_changed = 1;
10939 /* Normally the message* functions will have already displayed and
10940 updated the echo area, but the frame may have been trashed, or
10941 the update may have been preempted, so display the echo area
10942 again here. Checking message_cleared_p captures the case that
10943 the echo area should be cleared. */
10944 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10945 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10946 || (message_cleared_p
10947 && minibuf_level == 0
10948 /* If the mini-window is currently selected, this means the
10949 echo-area doesn't show through. */
10950 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10952 int window_height_changed_p = echo_area_display (0);
10953 must_finish = 1;
10955 /* If we don't display the current message, don't clear the
10956 message_cleared_p flag, because, if we did, we wouldn't clear
10957 the echo area in the next redisplay which doesn't preserve
10958 the echo area. */
10959 if (!display_last_displayed_message_p)
10960 message_cleared_p = 0;
10962 if (fonts_changed_p)
10963 goto retry;
10964 else if (window_height_changed_p)
10966 consider_all_windows_p = 1;
10967 ++update_mode_lines;
10968 ++windows_or_buffers_changed;
10970 /* If window configuration was changed, frames may have been
10971 marked garbaged. Clear them or we will experience
10972 surprises wrt scrolling. */
10973 if (frame_garbaged)
10974 clear_garbaged_frames ();
10977 else if (EQ (selected_window, minibuf_window)
10978 && (current_buffer->clip_changed
10979 || XFASTINT (w->last_modified) < MODIFF
10980 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10981 && resize_mini_window (w, 0))
10983 /* Resized active mini-window to fit the size of what it is
10984 showing if its contents might have changed. */
10985 must_finish = 1;
10986 consider_all_windows_p = 1;
10987 ++windows_or_buffers_changed;
10988 ++update_mode_lines;
10990 /* If window configuration was changed, frames may have been
10991 marked garbaged. Clear them or we will experience
10992 surprises wrt scrolling. */
10993 if (frame_garbaged)
10994 clear_garbaged_frames ();
10998 /* If showing the region, and mark has changed, we must redisplay
10999 the whole window. The assignment to this_line_start_pos prevents
11000 the optimization directly below this if-statement. */
11001 if (((!NILP (Vtransient_mark_mode)
11002 && !NILP (XBUFFER (w->buffer)->mark_active))
11003 != !NILP (w->region_showing))
11004 || (!NILP (w->region_showing)
11005 && !EQ (w->region_showing,
11006 Fmarker_position (XBUFFER (w->buffer)->mark))))
11007 CHARPOS (this_line_start_pos) = 0;
11009 /* Optimize the case that only the line containing the cursor in the
11010 selected window has changed. Variables starting with this_ are
11011 set in display_line and record information about the line
11012 containing the cursor. */
11013 tlbufpos = this_line_start_pos;
11014 tlendpos = this_line_end_pos;
11015 if (!consider_all_windows_p
11016 && CHARPOS (tlbufpos) > 0
11017 && NILP (w->update_mode_line)
11018 && !current_buffer->clip_changed
11019 && !current_buffer->prevent_redisplay_optimizations_p
11020 && FRAME_VISIBLE_P (XFRAME (w->frame))
11021 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11022 /* Make sure recorded data applies to current buffer, etc. */
11023 && this_line_buffer == current_buffer
11024 && current_buffer == XBUFFER (w->buffer)
11025 && NILP (w->force_start)
11026 && NILP (w->optional_new_start)
11027 /* Point must be on the line that we have info recorded about. */
11028 && PT >= CHARPOS (tlbufpos)
11029 && PT <= Z - CHARPOS (tlendpos)
11030 /* All text outside that line, including its final newline,
11031 must be unchanged */
11032 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11033 CHARPOS (tlendpos)))
11035 if (CHARPOS (tlbufpos) > BEGV
11036 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11037 && (CHARPOS (tlbufpos) == ZV
11038 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11039 /* Former continuation line has disappeared by becoming empty */
11040 goto cancel;
11041 else if (XFASTINT (w->last_modified) < MODIFF
11042 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11043 || MINI_WINDOW_P (w))
11045 /* We have to handle the case of continuation around a
11046 wide-column character (See the comment in indent.c around
11047 line 885).
11049 For instance, in the following case:
11051 -------- Insert --------
11052 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11053 J_I_ ==> J_I_ `^^' are cursors.
11054 ^^ ^^
11055 -------- --------
11057 As we have to redraw the line above, we should goto cancel. */
11059 struct it it;
11060 int line_height_before = this_line_pixel_height;
11062 /* Note that start_display will handle the case that the
11063 line starting at tlbufpos is a continuation lines. */
11064 start_display (&it, w, tlbufpos);
11066 /* Implementation note: It this still necessary? */
11067 if (it.current_x != this_line_start_x)
11068 goto cancel;
11070 TRACE ((stderr, "trying display optimization 1\n"));
11071 w->cursor.vpos = -1;
11072 overlay_arrow_seen = 0;
11073 it.vpos = this_line_vpos;
11074 it.current_y = this_line_y;
11075 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11076 display_line (&it);
11078 /* If line contains point, is not continued,
11079 and ends at same distance from eob as before, we win */
11080 if (w->cursor.vpos >= 0
11081 /* Line is not continued, otherwise this_line_start_pos
11082 would have been set to 0 in display_line. */
11083 && CHARPOS (this_line_start_pos)
11084 /* Line ends as before. */
11085 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11086 /* Line has same height as before. Otherwise other lines
11087 would have to be shifted up or down. */
11088 && this_line_pixel_height == line_height_before)
11090 /* If this is not the window's last line, we must adjust
11091 the charstarts of the lines below. */
11092 if (it.current_y < it.last_visible_y)
11094 struct glyph_row *row
11095 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11096 int delta, delta_bytes;
11098 if (Z - CHARPOS (tlendpos) == ZV)
11100 /* This line ends at end of (accessible part of)
11101 buffer. There is no newline to count. */
11102 delta = (Z
11103 - CHARPOS (tlendpos)
11104 - MATRIX_ROW_START_CHARPOS (row));
11105 delta_bytes = (Z_BYTE
11106 - BYTEPOS (tlendpos)
11107 - MATRIX_ROW_START_BYTEPOS (row));
11109 else
11111 /* This line ends in a newline. Must take
11112 account of the newline and the rest of the
11113 text that follows. */
11114 delta = (Z
11115 - CHARPOS (tlendpos)
11116 - MATRIX_ROW_START_CHARPOS (row));
11117 delta_bytes = (Z_BYTE
11118 - BYTEPOS (tlendpos)
11119 - MATRIX_ROW_START_BYTEPOS (row));
11122 increment_matrix_positions (w->current_matrix,
11123 this_line_vpos + 1,
11124 w->current_matrix->nrows,
11125 delta, delta_bytes);
11128 /* If this row displays text now but previously didn't,
11129 or vice versa, w->window_end_vpos may have to be
11130 adjusted. */
11131 if ((it.glyph_row - 1)->displays_text_p)
11133 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11134 XSETINT (w->window_end_vpos, this_line_vpos);
11136 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11137 && this_line_vpos > 0)
11138 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11139 w->window_end_valid = Qnil;
11141 /* Update hint: No need to try to scroll in update_window. */
11142 w->desired_matrix->no_scrolling_p = 1;
11144 #if GLYPH_DEBUG
11145 *w->desired_matrix->method = 0;
11146 debug_method_add (w, "optimization 1");
11147 #endif
11148 #ifdef HAVE_WINDOW_SYSTEM
11149 update_window_fringes (w, 0);
11150 #endif
11151 goto update;
11153 else
11154 goto cancel;
11156 else if (/* Cursor position hasn't changed. */
11157 PT == XFASTINT (w->last_point)
11158 /* Make sure the cursor was last displayed
11159 in this window. Otherwise we have to reposition it. */
11160 && 0 <= w->cursor.vpos
11161 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11163 if (!must_finish)
11165 do_pending_window_change (1);
11167 /* We used to always goto end_of_redisplay here, but this
11168 isn't enough if we have a blinking cursor. */
11169 if (w->cursor_off_p == w->last_cursor_off_p)
11170 goto end_of_redisplay;
11172 goto update;
11174 /* If highlighting the region, or if the cursor is in the echo area,
11175 then we can't just move the cursor. */
11176 else if (! (!NILP (Vtransient_mark_mode)
11177 && !NILP (current_buffer->mark_active))
11178 && (EQ (selected_window, current_buffer->last_selected_window)
11179 || highlight_nonselected_windows)
11180 && NILP (w->region_showing)
11181 && NILP (Vshow_trailing_whitespace)
11182 && !cursor_in_echo_area)
11184 struct it it;
11185 struct glyph_row *row;
11187 /* Skip from tlbufpos to PT and see where it is. Note that
11188 PT may be in invisible text. If so, we will end at the
11189 next visible position. */
11190 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11191 NULL, DEFAULT_FACE_ID);
11192 it.current_x = this_line_start_x;
11193 it.current_y = this_line_y;
11194 it.vpos = this_line_vpos;
11196 /* The call to move_it_to stops in front of PT, but
11197 moves over before-strings. */
11198 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11200 if (it.vpos == this_line_vpos
11201 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11202 row->enabled_p))
11204 xassert (this_line_vpos == it.vpos);
11205 xassert (this_line_y == it.current_y);
11206 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11207 #if GLYPH_DEBUG
11208 *w->desired_matrix->method = 0;
11209 debug_method_add (w, "optimization 3");
11210 #endif
11211 goto update;
11213 else
11214 goto cancel;
11217 cancel:
11218 /* Text changed drastically or point moved off of line. */
11219 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11222 CHARPOS (this_line_start_pos) = 0;
11223 consider_all_windows_p |= buffer_shared > 1;
11224 ++clear_face_cache_count;
11225 #ifdef HAVE_WINDOW_SYSTEM
11226 ++clear_image_cache_count;
11227 #endif
11229 /* Build desired matrices, and update the display. If
11230 consider_all_windows_p is non-zero, do it for all windows on all
11231 frames. Otherwise do it for selected_window, only. */
11233 if (consider_all_windows_p)
11235 Lisp_Object tail, frame;
11237 FOR_EACH_FRAME (tail, frame)
11238 XFRAME (frame)->updated_p = 0;
11240 /* Recompute # windows showing selected buffer. This will be
11241 incremented each time such a window is displayed. */
11242 buffer_shared = 0;
11244 FOR_EACH_FRAME (tail, frame)
11246 struct frame *f = XFRAME (frame);
11248 if (FRAME_WINDOW_P (f) || f == sf)
11250 if (! EQ (frame, selected_frame))
11251 /* Select the frame, for the sake of frame-local
11252 variables. */
11253 select_frame_for_redisplay (frame);
11255 /* Mark all the scroll bars to be removed; we'll redeem
11256 the ones we want when we redisplay their windows. */
11257 if (condemn_scroll_bars_hook)
11258 condemn_scroll_bars_hook (f);
11260 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11261 redisplay_windows (FRAME_ROOT_WINDOW (f));
11263 /* Any scroll bars which redisplay_windows should have
11264 nuked should now go away. */
11265 if (judge_scroll_bars_hook)
11266 judge_scroll_bars_hook (f);
11268 /* If fonts changed, display again. */
11269 /* ??? rms: I suspect it is a mistake to jump all the way
11270 back to retry here. It should just retry this frame. */
11271 if (fonts_changed_p)
11272 goto retry;
11274 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11276 /* See if we have to hscroll. */
11277 if (!f->already_hscrolled_p)
11279 f->already_hscrolled_p = 1;
11280 if (hscroll_windows (f->root_window))
11281 goto retry;
11284 /* Prevent various kinds of signals during display
11285 update. stdio is not robust about handling
11286 signals, which can cause an apparent I/O
11287 error. */
11288 if (interrupt_input)
11289 unrequest_sigio ();
11290 STOP_POLLING;
11292 /* Update the display. */
11293 set_window_update_flags (XWINDOW (f->root_window), 1);
11294 pause |= update_frame (f, 0, 0);
11295 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11296 if (pause)
11297 break;
11298 #endif
11300 f->updated_p = 1;
11305 if (!pause)
11307 /* Do the mark_window_display_accurate after all windows have
11308 been redisplayed because this call resets flags in buffers
11309 which are needed for proper redisplay. */
11310 FOR_EACH_FRAME (tail, frame)
11312 struct frame *f = XFRAME (frame);
11313 if (f->updated_p)
11315 mark_window_display_accurate (f->root_window, 1);
11316 if (frame_up_to_date_hook)
11317 frame_up_to_date_hook (f);
11322 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11324 Lisp_Object mini_window;
11325 struct frame *mini_frame;
11327 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11328 /* Use list_of_error, not Qerror, so that
11329 we catch only errors and don't run the debugger. */
11330 internal_condition_case_1 (redisplay_window_1, selected_window,
11331 list_of_error,
11332 redisplay_window_error);
11334 /* Compare desired and current matrices, perform output. */
11336 update:
11337 /* If fonts changed, display again. */
11338 if (fonts_changed_p)
11339 goto retry;
11341 /* Prevent various kinds of signals during display update.
11342 stdio is not robust about handling signals,
11343 which can cause an apparent I/O error. */
11344 if (interrupt_input)
11345 unrequest_sigio ();
11346 STOP_POLLING;
11348 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11350 if (hscroll_windows (selected_window))
11351 goto retry;
11353 XWINDOW (selected_window)->must_be_updated_p = 1;
11354 pause = update_frame (sf, 0, 0);
11357 /* We may have called echo_area_display at the top of this
11358 function. If the echo area is on another frame, that may
11359 have put text on a frame other than the selected one, so the
11360 above call to update_frame would not have caught it. Catch
11361 it here. */
11362 mini_window = FRAME_MINIBUF_WINDOW (sf);
11363 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11365 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11367 XWINDOW (mini_window)->must_be_updated_p = 1;
11368 pause |= update_frame (mini_frame, 0, 0);
11369 if (!pause && hscroll_windows (mini_window))
11370 goto retry;
11374 /* If display was paused because of pending input, make sure we do a
11375 thorough update the next time. */
11376 if (pause)
11378 /* Prevent the optimization at the beginning of
11379 redisplay_internal that tries a single-line update of the
11380 line containing the cursor in the selected window. */
11381 CHARPOS (this_line_start_pos) = 0;
11383 /* Let the overlay arrow be updated the next time. */
11384 update_overlay_arrows (0);
11386 /* If we pause after scrolling, some rows in the current
11387 matrices of some windows are not valid. */
11388 if (!WINDOW_FULL_WIDTH_P (w)
11389 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11390 update_mode_lines = 1;
11392 else
11394 if (!consider_all_windows_p)
11396 /* This has already been done above if
11397 consider_all_windows_p is set. */
11398 mark_window_display_accurate_1 (w, 1);
11400 /* Say overlay arrows are up to date. */
11401 update_overlay_arrows (1);
11403 if (frame_up_to_date_hook != 0)
11404 frame_up_to_date_hook (sf);
11407 update_mode_lines = 0;
11408 windows_or_buffers_changed = 0;
11409 cursor_type_changed = 0;
11412 /* Start SIGIO interrupts coming again. Having them off during the
11413 code above makes it less likely one will discard output, but not
11414 impossible, since there might be stuff in the system buffer here.
11415 But it is much hairier to try to do anything about that. */
11416 if (interrupt_input)
11417 request_sigio ();
11418 RESUME_POLLING;
11420 /* If a frame has become visible which was not before, redisplay
11421 again, so that we display it. Expose events for such a frame
11422 (which it gets when becoming visible) don't call the parts of
11423 redisplay constructing glyphs, so simply exposing a frame won't
11424 display anything in this case. So, we have to display these
11425 frames here explicitly. */
11426 if (!pause)
11428 Lisp_Object tail, frame;
11429 int new_count = 0;
11431 FOR_EACH_FRAME (tail, frame)
11433 int this_is_visible = 0;
11435 if (XFRAME (frame)->visible)
11436 this_is_visible = 1;
11437 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11438 if (XFRAME (frame)->visible)
11439 this_is_visible = 1;
11441 if (this_is_visible)
11442 new_count++;
11445 if (new_count != number_of_visible_frames)
11446 windows_or_buffers_changed++;
11449 /* Change frame size now if a change is pending. */
11450 do_pending_window_change (1);
11452 /* If we just did a pending size change, or have additional
11453 visible frames, redisplay again. */
11454 if (windows_or_buffers_changed && !pause)
11455 goto retry;
11457 /* Clear the face cache eventually. */
11458 if (consider_all_windows_p)
11460 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11462 clear_face_cache (0);
11463 clear_face_cache_count = 0;
11465 #ifdef HAVE_WINDOW_SYSTEM
11466 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11468 Lisp_Object tail, frame;
11469 FOR_EACH_FRAME (tail, frame)
11471 struct frame *f = XFRAME (frame);
11472 if (FRAME_WINDOW_P (f))
11473 clear_image_cache (f, 0);
11475 clear_image_cache_count = 0;
11477 #endif /* HAVE_WINDOW_SYSTEM */
11480 end_of_redisplay:
11481 unbind_to (count, Qnil);
11482 RESUME_POLLING;
11486 /* Redisplay, but leave alone any recent echo area message unless
11487 another message has been requested in its place.
11489 This is useful in situations where you need to redisplay but no
11490 user action has occurred, making it inappropriate for the message
11491 area to be cleared. See tracking_off and
11492 wait_reading_process_output for examples of these situations.
11494 FROM_WHERE is an integer saying from where this function was
11495 called. This is useful for debugging. */
11497 void
11498 redisplay_preserve_echo_area (from_where)
11499 int from_where;
11501 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11503 if (!NILP (echo_area_buffer[1]))
11505 /* We have a previously displayed message, but no current
11506 message. Redisplay the previous message. */
11507 display_last_displayed_message_p = 1;
11508 redisplay_internal (1);
11509 display_last_displayed_message_p = 0;
11511 else
11512 redisplay_internal (1);
11514 if (rif != NULL && rif->flush_display_optional)
11515 rif->flush_display_optional (NULL);
11519 /* Function registered with record_unwind_protect in
11520 redisplay_internal. Reset redisplaying_p to the value it had
11521 before redisplay_internal was called, and clear
11522 prevent_freeing_realized_faces_p. It also selects the previously
11523 selected frame. */
11525 static Lisp_Object
11526 unwind_redisplay (val)
11527 Lisp_Object val;
11529 Lisp_Object old_redisplaying_p, old_frame;
11531 old_redisplaying_p = XCAR (val);
11532 redisplaying_p = XFASTINT (old_redisplaying_p);
11533 old_frame = XCDR (val);
11534 if (! EQ (old_frame, selected_frame))
11535 select_frame_for_redisplay (old_frame);
11536 return Qnil;
11540 /* Mark the display of window W as accurate or inaccurate. If
11541 ACCURATE_P is non-zero mark display of W as accurate. If
11542 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11543 redisplay_internal is called. */
11545 static void
11546 mark_window_display_accurate_1 (w, accurate_p)
11547 struct window *w;
11548 int accurate_p;
11550 if (BUFFERP (w->buffer))
11552 struct buffer *b = XBUFFER (w->buffer);
11554 w->last_modified
11555 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11556 w->last_overlay_modified
11557 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11558 w->last_had_star
11559 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11561 if (accurate_p)
11563 b->clip_changed = 0;
11564 b->prevent_redisplay_optimizations_p = 0;
11566 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11567 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11568 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11569 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11571 w->current_matrix->buffer = b;
11572 w->current_matrix->begv = BUF_BEGV (b);
11573 w->current_matrix->zv = BUF_ZV (b);
11575 w->last_cursor = w->cursor;
11576 w->last_cursor_off_p = w->cursor_off_p;
11578 if (w == XWINDOW (selected_window))
11579 w->last_point = make_number (BUF_PT (b));
11580 else
11581 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11585 if (accurate_p)
11587 w->window_end_valid = w->buffer;
11588 #if 0 /* This is incorrect with variable-height lines. */
11589 xassert (XINT (w->window_end_vpos)
11590 < (WINDOW_TOTAL_LINES (w)
11591 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11592 #endif
11593 w->update_mode_line = Qnil;
11598 /* Mark the display of windows in the window tree rooted at WINDOW as
11599 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11600 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11601 be redisplayed the next time redisplay_internal is called. */
11603 void
11604 mark_window_display_accurate (window, accurate_p)
11605 Lisp_Object window;
11606 int accurate_p;
11608 struct window *w;
11610 for (; !NILP (window); window = w->next)
11612 w = XWINDOW (window);
11613 mark_window_display_accurate_1 (w, accurate_p);
11615 if (!NILP (w->vchild))
11616 mark_window_display_accurate (w->vchild, accurate_p);
11617 if (!NILP (w->hchild))
11618 mark_window_display_accurate (w->hchild, accurate_p);
11621 if (accurate_p)
11623 update_overlay_arrows (1);
11625 else
11627 /* Force a thorough redisplay the next time by setting
11628 last_arrow_position and last_arrow_string to t, which is
11629 unequal to any useful value of Voverlay_arrow_... */
11630 update_overlay_arrows (-1);
11635 /* Return value in display table DP (Lisp_Char_Table *) for character
11636 C. Since a display table doesn't have any parent, we don't have to
11637 follow parent. Do not call this function directly but use the
11638 macro DISP_CHAR_VECTOR. */
11640 Lisp_Object
11641 disp_char_vector (dp, c)
11642 struct Lisp_Char_Table *dp;
11643 int c;
11645 int code[4], i;
11646 Lisp_Object val;
11648 if (SINGLE_BYTE_CHAR_P (c))
11649 return (dp->contents[c]);
11651 SPLIT_CHAR (c, code[0], code[1], code[2]);
11652 if (code[1] < 32)
11653 code[1] = -1;
11654 else if (code[2] < 32)
11655 code[2] = -1;
11657 /* Here, the possible range of code[0] (== charset ID) is
11658 128..max_charset. Since the top level char table contains data
11659 for multibyte characters after 256th element, we must increment
11660 code[0] by 128 to get a correct index. */
11661 code[0] += 128;
11662 code[3] = -1; /* anchor */
11664 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11666 val = dp->contents[code[i]];
11667 if (!SUB_CHAR_TABLE_P (val))
11668 return (NILP (val) ? dp->defalt : val);
11671 /* Here, val is a sub char table. We return the default value of
11672 it. */
11673 return (dp->defalt);
11678 /***********************************************************************
11679 Window Redisplay
11680 ***********************************************************************/
11682 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11684 static void
11685 redisplay_windows (window)
11686 Lisp_Object window;
11688 while (!NILP (window))
11690 struct window *w = XWINDOW (window);
11692 if (!NILP (w->hchild))
11693 redisplay_windows (w->hchild);
11694 else if (!NILP (w->vchild))
11695 redisplay_windows (w->vchild);
11696 else
11698 displayed_buffer = XBUFFER (w->buffer);
11699 /* Use list_of_error, not Qerror, so that
11700 we catch only errors and don't run the debugger. */
11701 internal_condition_case_1 (redisplay_window_0, window,
11702 list_of_error,
11703 redisplay_window_error);
11706 window = w->next;
11710 static Lisp_Object
11711 redisplay_window_error ()
11713 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11714 return Qnil;
11717 static Lisp_Object
11718 redisplay_window_0 (window)
11719 Lisp_Object window;
11721 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11722 redisplay_window (window, 0);
11723 return Qnil;
11726 static Lisp_Object
11727 redisplay_window_1 (window)
11728 Lisp_Object window;
11730 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11731 redisplay_window (window, 1);
11732 return Qnil;
11736 /* Increment GLYPH until it reaches END or CONDITION fails while
11737 adding (GLYPH)->pixel_width to X. */
11739 #define SKIP_GLYPHS(glyph, end, x, condition) \
11740 do \
11742 (x) += (glyph)->pixel_width; \
11743 ++(glyph); \
11745 while ((glyph) < (end) && (condition))
11748 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11749 DELTA is the number of bytes by which positions recorded in ROW
11750 differ from current buffer positions.
11752 Return 0 if cursor is not on this row. 1 otherwise. */
11755 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11756 struct window *w;
11757 struct glyph_row *row;
11758 struct glyph_matrix *matrix;
11759 int delta, delta_bytes, dy, dvpos;
11761 struct glyph *glyph = row->glyphs[TEXT_AREA];
11762 struct glyph *end = glyph + row->used[TEXT_AREA];
11763 struct glyph *cursor = NULL;
11764 /* The first glyph that starts a sequence of glyphs from string. */
11765 struct glyph *string_start;
11766 /* The X coordinate of string_start. */
11767 int string_start_x;
11768 /* The last known character position. */
11769 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11770 /* The last known character position before string_start. */
11771 int string_before_pos;
11772 int x = row->x;
11773 int cursor_x = x;
11774 int cursor_from_overlay_pos = 0;
11775 int pt_old = PT - delta;
11777 /* Skip over glyphs not having an object at the start of the row.
11778 These are special glyphs like truncation marks on terminal
11779 frames. */
11780 if (row->displays_text_p)
11781 while (glyph < end
11782 && INTEGERP (glyph->object)
11783 && glyph->charpos < 0)
11785 x += glyph->pixel_width;
11786 ++glyph;
11789 string_start = NULL;
11790 while (glyph < end
11791 && !INTEGERP (glyph->object)
11792 && (!BUFFERP (glyph->object)
11793 || (last_pos = glyph->charpos) < pt_old))
11795 if (! STRINGP (glyph->object))
11797 string_start = NULL;
11798 x += glyph->pixel_width;
11799 ++glyph;
11800 if (cursor_from_overlay_pos
11801 && last_pos >= cursor_from_overlay_pos)
11803 cursor_from_overlay_pos = 0;
11804 cursor = 0;
11807 else
11809 if (string_start == NULL)
11811 string_before_pos = last_pos;
11812 string_start = glyph;
11813 string_start_x = x;
11815 /* Skip all glyphs from string. */
11818 Lisp_Object cprop;
11819 int pos;
11820 if ((cursor == NULL || glyph > cursor)
11821 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11822 Qcursor, (glyph)->object),
11823 !NILP (cprop))
11824 && (pos = string_buffer_position (w, glyph->object,
11825 string_before_pos),
11826 (pos == 0 /* From overlay */
11827 || pos == pt_old)))
11829 /* Estimate overlay buffer position from the buffer
11830 positions of the glyphs before and after the overlay.
11831 Add 1 to last_pos so that if point corresponds to the
11832 glyph right after the overlay, we still use a 'cursor'
11833 property found in that overlay. */
11834 cursor_from_overlay_pos = (pos ? 0 : last_pos
11835 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11836 cursor = glyph;
11837 cursor_x = x;
11839 x += glyph->pixel_width;
11840 ++glyph;
11842 while (glyph < end && EQ (glyph->object, string_start->object));
11846 if (cursor != NULL)
11848 glyph = cursor;
11849 x = cursor_x;
11851 else if (row->ends_in_ellipsis_p && glyph == end)
11853 /* Scan back over the ellipsis glyphs, decrementing positions. */
11854 while (glyph > row->glyphs[TEXT_AREA]
11855 && (glyph - 1)->charpos == last_pos)
11856 glyph--, x -= glyph->pixel_width;
11857 /* That loop always goes one position too far,
11858 including the glyph before the ellipsis.
11859 So scan forward over that one. */
11860 x += glyph->pixel_width;
11861 glyph++;
11863 else if (string_start
11864 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11866 /* We may have skipped over point because the previous glyphs
11867 are from string. As there's no easy way to know the
11868 character position of the current glyph, find the correct
11869 glyph on point by scanning from string_start again. */
11870 Lisp_Object limit;
11871 Lisp_Object string;
11872 struct glyph *stop = glyph;
11873 int pos;
11875 limit = make_number (pt_old + 1);
11876 glyph = string_start;
11877 x = string_start_x;
11878 string = glyph->object;
11879 pos = string_buffer_position (w, string, string_before_pos);
11880 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11881 because we always put cursor after overlay strings. */
11882 while (pos == 0 && glyph < stop)
11884 string = glyph->object;
11885 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11886 if (glyph < stop)
11887 pos = string_buffer_position (w, glyph->object, string_before_pos);
11890 while (glyph < stop)
11892 pos = XINT (Fnext_single_char_property_change
11893 (make_number (pos), Qdisplay, Qnil, limit));
11894 if (pos > pt_old)
11895 break;
11896 /* Skip glyphs from the same string. */
11897 string = glyph->object;
11898 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11899 /* Skip glyphs from an overlay. */
11900 while (glyph < stop
11901 && ! string_buffer_position (w, glyph->object, pos))
11903 string = glyph->object;
11904 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11908 /* If we reached the end of the line, and end was from a string,
11909 cursor is not on this line. */
11910 if (glyph == end && row->continued_p)
11911 return 0;
11914 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11915 w->cursor.x = x;
11916 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11917 w->cursor.y = row->y + dy;
11919 if (w == XWINDOW (selected_window))
11921 if (!row->continued_p
11922 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11923 && row->x == 0)
11925 this_line_buffer = XBUFFER (w->buffer);
11927 CHARPOS (this_line_start_pos)
11928 = MATRIX_ROW_START_CHARPOS (row) + delta;
11929 BYTEPOS (this_line_start_pos)
11930 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11932 CHARPOS (this_line_end_pos)
11933 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11934 BYTEPOS (this_line_end_pos)
11935 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11937 this_line_y = w->cursor.y;
11938 this_line_pixel_height = row->height;
11939 this_line_vpos = w->cursor.vpos;
11940 this_line_start_x = row->x;
11942 else
11943 CHARPOS (this_line_start_pos) = 0;
11946 return 1;
11950 /* Run window scroll functions, if any, for WINDOW with new window
11951 start STARTP. Sets the window start of WINDOW to that position.
11953 We assume that the window's buffer is really current. */
11955 static INLINE struct text_pos
11956 run_window_scroll_functions (window, startp)
11957 Lisp_Object window;
11958 struct text_pos startp;
11960 struct window *w = XWINDOW (window);
11961 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11963 if (current_buffer != XBUFFER (w->buffer))
11964 abort ();
11966 if (!NILP (Vwindow_scroll_functions))
11968 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11969 make_number (CHARPOS (startp)));
11970 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11971 /* In case the hook functions switch buffers. */
11972 if (current_buffer != XBUFFER (w->buffer))
11973 set_buffer_internal_1 (XBUFFER (w->buffer));
11976 return startp;
11980 /* Make sure the line containing the cursor is fully visible.
11981 A value of 1 means there is nothing to be done.
11982 (Either the line is fully visible, or it cannot be made so,
11983 or we cannot tell.)
11985 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11986 is higher than window.
11988 A value of 0 means the caller should do scrolling
11989 as if point had gone off the screen. */
11991 static int
11992 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11993 struct window *w;
11994 int force_p;
11995 int current_matrix_p;
11997 struct glyph_matrix *matrix;
11998 struct glyph_row *row;
11999 int window_height;
12001 if (!make_cursor_line_fully_visible_p)
12002 return 1;
12004 /* It's not always possible to find the cursor, e.g, when a window
12005 is full of overlay strings. Don't do anything in that case. */
12006 if (w->cursor.vpos < 0)
12007 return 1;
12009 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12010 row = MATRIX_ROW (matrix, w->cursor.vpos);
12012 /* If the cursor row is not partially visible, there's nothing to do. */
12013 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12014 return 1;
12016 /* If the row the cursor is in is taller than the window's height,
12017 it's not clear what to do, so do nothing. */
12018 window_height = window_box_height (w);
12019 if (row->height >= window_height)
12021 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
12022 return 1;
12024 return 0;
12026 #if 0
12027 /* This code used to try to scroll the window just enough to make
12028 the line visible. It returned 0 to say that the caller should
12029 allocate larger glyph matrices. */
12031 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12033 int dy = row->height - row->visible_height;
12034 w->vscroll = 0;
12035 w->cursor.y += dy;
12036 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12038 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12040 int dy = - (row->height - row->visible_height);
12041 w->vscroll = dy;
12042 w->cursor.y += dy;
12043 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12046 /* When we change the cursor y-position of the selected window,
12047 change this_line_y as well so that the display optimization for
12048 the cursor line of the selected window in redisplay_internal uses
12049 the correct y-position. */
12050 if (w == XWINDOW (selected_window))
12051 this_line_y = w->cursor.y;
12053 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12054 redisplay with larger matrices. */
12055 if (matrix->nrows < required_matrix_height (w))
12057 fonts_changed_p = 1;
12058 return 0;
12061 return 1;
12062 #endif /* 0 */
12066 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12067 non-zero means only WINDOW is redisplayed in redisplay_internal.
12068 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12069 in redisplay_window to bring a partially visible line into view in
12070 the case that only the cursor has moved.
12072 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12073 last screen line's vertical height extends past the end of the screen.
12075 Value is
12077 1 if scrolling succeeded
12079 0 if scrolling didn't find point.
12081 -1 if new fonts have been loaded so that we must interrupt
12082 redisplay, adjust glyph matrices, and try again. */
12084 enum
12086 SCROLLING_SUCCESS,
12087 SCROLLING_FAILED,
12088 SCROLLING_NEED_LARGER_MATRICES
12091 static int
12092 try_scrolling (window, just_this_one_p, scroll_conservatively,
12093 scroll_step, temp_scroll_step, last_line_misfit)
12094 Lisp_Object window;
12095 int just_this_one_p;
12096 EMACS_INT scroll_conservatively, scroll_step;
12097 int temp_scroll_step;
12098 int last_line_misfit;
12100 struct window *w = XWINDOW (window);
12101 struct frame *f = XFRAME (w->frame);
12102 struct text_pos scroll_margin_pos;
12103 struct text_pos pos;
12104 struct text_pos startp;
12105 struct it it;
12106 Lisp_Object window_end;
12107 int this_scroll_margin;
12108 int dy = 0;
12109 int scroll_max;
12110 int rc;
12111 int amount_to_scroll = 0;
12112 Lisp_Object aggressive;
12113 int height;
12114 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12116 #if GLYPH_DEBUG
12117 debug_method_add (w, "try_scrolling");
12118 #endif
12120 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12122 /* Compute scroll margin height in pixels. We scroll when point is
12123 within this distance from the top or bottom of the window. */
12124 if (scroll_margin > 0)
12126 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12127 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12129 else
12130 this_scroll_margin = 0;
12132 /* Force scroll_conservatively to have a reasonable value so it doesn't
12133 cause an overflow while computing how much to scroll. */
12134 if (scroll_conservatively)
12135 scroll_conservatively = min (scroll_conservatively,
12136 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12138 /* Compute how much we should try to scroll maximally to bring point
12139 into view. */
12140 if (scroll_step || scroll_conservatively || temp_scroll_step)
12141 scroll_max = max (scroll_step,
12142 max (scroll_conservatively, temp_scroll_step));
12143 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12144 || NUMBERP (current_buffer->scroll_up_aggressively))
12145 /* We're trying to scroll because of aggressive scrolling
12146 but no scroll_step is set. Choose an arbitrary one. Maybe
12147 there should be a variable for this. */
12148 scroll_max = 10;
12149 else
12150 scroll_max = 0;
12151 scroll_max *= FRAME_LINE_HEIGHT (f);
12153 /* Decide whether we have to scroll down. Start at the window end
12154 and move this_scroll_margin up to find the position of the scroll
12155 margin. */
12156 window_end = Fwindow_end (window, Qt);
12158 too_near_end:
12160 CHARPOS (scroll_margin_pos) = XINT (window_end);
12161 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12163 if (this_scroll_margin || extra_scroll_margin_lines)
12165 start_display (&it, w, scroll_margin_pos);
12166 if (this_scroll_margin)
12167 move_it_vertically_backward (&it, this_scroll_margin);
12168 if (extra_scroll_margin_lines)
12169 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12170 scroll_margin_pos = it.current.pos;
12173 if (PT >= CHARPOS (scroll_margin_pos))
12175 int y0;
12177 /* Point is in the scroll margin at the bottom of the window, or
12178 below. Compute a new window start that makes point visible. */
12180 /* Compute the distance from the scroll margin to PT.
12181 Give up if the distance is greater than scroll_max. */
12182 start_display (&it, w, scroll_margin_pos);
12183 y0 = it.current_y;
12184 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12185 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12187 /* To make point visible, we have to move the window start
12188 down so that the line the cursor is in is visible, which
12189 means we have to add in the height of the cursor line. */
12190 dy = line_bottom_y (&it) - y0;
12192 if (dy > scroll_max)
12193 return SCROLLING_FAILED;
12195 /* Move the window start down. If scrolling conservatively,
12196 move it just enough down to make point visible. If
12197 scroll_step is set, move it down by scroll_step. */
12198 start_display (&it, w, startp);
12200 if (scroll_conservatively)
12201 /* Set AMOUNT_TO_SCROLL to at least one line,
12202 and at most scroll_conservatively lines. */
12203 amount_to_scroll
12204 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12205 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12206 else if (scroll_step || temp_scroll_step)
12207 amount_to_scroll = scroll_max;
12208 else
12210 aggressive = current_buffer->scroll_up_aggressively;
12211 height = WINDOW_BOX_TEXT_HEIGHT (w);
12212 if (NUMBERP (aggressive))
12214 double float_amount = XFLOATINT (aggressive) * height;
12215 amount_to_scroll = float_amount;
12216 if (amount_to_scroll == 0 && float_amount > 0)
12217 amount_to_scroll = 1;
12221 if (amount_to_scroll <= 0)
12222 return SCROLLING_FAILED;
12224 /* If moving by amount_to_scroll leaves STARTP unchanged,
12225 move it down one screen line. */
12227 move_it_vertically (&it, amount_to_scroll);
12228 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12229 move_it_by_lines (&it, 1, 1);
12230 startp = it.current.pos;
12232 else
12234 /* See if point is inside the scroll margin at the top of the
12235 window. */
12236 scroll_margin_pos = startp;
12237 if (this_scroll_margin)
12239 start_display (&it, w, startp);
12240 move_it_vertically (&it, this_scroll_margin);
12241 scroll_margin_pos = it.current.pos;
12244 if (PT < CHARPOS (scroll_margin_pos))
12246 /* Point is in the scroll margin at the top of the window or
12247 above what is displayed in the window. */
12248 int y0;
12250 /* Compute the vertical distance from PT to the scroll
12251 margin position. Give up if distance is greater than
12252 scroll_max. */
12253 SET_TEXT_POS (pos, PT, PT_BYTE);
12254 start_display (&it, w, pos);
12255 y0 = it.current_y;
12256 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12257 it.last_visible_y, -1,
12258 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12259 dy = it.current_y - y0;
12260 if (dy > scroll_max)
12261 return SCROLLING_FAILED;
12263 /* Compute new window start. */
12264 start_display (&it, w, startp);
12266 if (scroll_conservatively)
12267 amount_to_scroll
12268 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12269 else if (scroll_step || temp_scroll_step)
12270 amount_to_scroll = scroll_max;
12271 else
12273 aggressive = current_buffer->scroll_down_aggressively;
12274 height = WINDOW_BOX_TEXT_HEIGHT (w);
12275 if (NUMBERP (aggressive))
12277 double float_amount = XFLOATINT (aggressive) * height;
12278 amount_to_scroll = float_amount;
12279 if (amount_to_scroll == 0 && float_amount > 0)
12280 amount_to_scroll = 1;
12284 if (amount_to_scroll <= 0)
12285 return SCROLLING_FAILED;
12287 move_it_vertically_backward (&it, amount_to_scroll);
12288 startp = it.current.pos;
12292 /* Run window scroll functions. */
12293 startp = run_window_scroll_functions (window, startp);
12295 /* Display the window. Give up if new fonts are loaded, or if point
12296 doesn't appear. */
12297 if (!try_window (window, startp, 0))
12298 rc = SCROLLING_NEED_LARGER_MATRICES;
12299 else if (w->cursor.vpos < 0)
12301 clear_glyph_matrix (w->desired_matrix);
12302 rc = SCROLLING_FAILED;
12304 else
12306 /* Maybe forget recorded base line for line number display. */
12307 if (!just_this_one_p
12308 || current_buffer->clip_changed
12309 || BEG_UNCHANGED < CHARPOS (startp))
12310 w->base_line_number = Qnil;
12312 /* If cursor ends up on a partially visible line,
12313 treat that as being off the bottom of the screen. */
12314 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12316 clear_glyph_matrix (w->desired_matrix);
12317 ++extra_scroll_margin_lines;
12318 goto too_near_end;
12320 rc = SCROLLING_SUCCESS;
12323 return rc;
12327 /* Compute a suitable window start for window W if display of W starts
12328 on a continuation line. Value is non-zero if a new window start
12329 was computed.
12331 The new window start will be computed, based on W's width, starting
12332 from the start of the continued line. It is the start of the
12333 screen line with the minimum distance from the old start W->start. */
12335 static int
12336 compute_window_start_on_continuation_line (w)
12337 struct window *w;
12339 struct text_pos pos, start_pos;
12340 int window_start_changed_p = 0;
12342 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12344 /* If window start is on a continuation line... Window start may be
12345 < BEGV in case there's invisible text at the start of the
12346 buffer (M-x rmail, for example). */
12347 if (CHARPOS (start_pos) > BEGV
12348 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12350 struct it it;
12351 struct glyph_row *row;
12353 /* Handle the case that the window start is out of range. */
12354 if (CHARPOS (start_pos) < BEGV)
12355 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12356 else if (CHARPOS (start_pos) > ZV)
12357 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12359 /* Find the start of the continued line. This should be fast
12360 because scan_buffer is fast (newline cache). */
12361 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12362 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12363 row, DEFAULT_FACE_ID);
12364 reseat_at_previous_visible_line_start (&it);
12366 /* If the line start is "too far" away from the window start,
12367 say it takes too much time to compute a new window start. */
12368 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12369 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12371 int min_distance, distance;
12373 /* Move forward by display lines to find the new window
12374 start. If window width was enlarged, the new start can
12375 be expected to be > the old start. If window width was
12376 decreased, the new window start will be < the old start.
12377 So, we're looking for the display line start with the
12378 minimum distance from the old window start. */
12379 pos = it.current.pos;
12380 min_distance = INFINITY;
12381 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12382 distance < min_distance)
12384 min_distance = distance;
12385 pos = it.current.pos;
12386 move_it_by_lines (&it, 1, 0);
12389 /* Set the window start there. */
12390 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12391 window_start_changed_p = 1;
12395 return window_start_changed_p;
12399 /* Try cursor movement in case text has not changed in window WINDOW,
12400 with window start STARTP. Value is
12402 CURSOR_MOVEMENT_SUCCESS if successful
12404 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12406 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12407 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12408 we want to scroll as if scroll-step were set to 1. See the code.
12410 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12411 which case we have to abort this redisplay, and adjust matrices
12412 first. */
12414 enum
12416 CURSOR_MOVEMENT_SUCCESS,
12417 CURSOR_MOVEMENT_CANNOT_BE_USED,
12418 CURSOR_MOVEMENT_MUST_SCROLL,
12419 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12422 static int
12423 try_cursor_movement (window, startp, scroll_step)
12424 Lisp_Object window;
12425 struct text_pos startp;
12426 int *scroll_step;
12428 struct window *w = XWINDOW (window);
12429 struct frame *f = XFRAME (w->frame);
12430 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12432 #if GLYPH_DEBUG
12433 if (inhibit_try_cursor_movement)
12434 return rc;
12435 #endif
12437 /* Handle case where text has not changed, only point, and it has
12438 not moved off the frame. */
12439 if (/* Point may be in this window. */
12440 PT >= CHARPOS (startp)
12441 /* Selective display hasn't changed. */
12442 && !current_buffer->clip_changed
12443 /* Function force-mode-line-update is used to force a thorough
12444 redisplay. It sets either windows_or_buffers_changed or
12445 update_mode_lines. So don't take a shortcut here for these
12446 cases. */
12447 && !update_mode_lines
12448 && !windows_or_buffers_changed
12449 && !cursor_type_changed
12450 /* Can't use this case if highlighting a region. When a
12451 region exists, cursor movement has to do more than just
12452 set the cursor. */
12453 && !(!NILP (Vtransient_mark_mode)
12454 && !NILP (current_buffer->mark_active))
12455 && NILP (w->region_showing)
12456 && NILP (Vshow_trailing_whitespace)
12457 /* Right after splitting windows, last_point may be nil. */
12458 && INTEGERP (w->last_point)
12459 /* This code is not used for mini-buffer for the sake of the case
12460 of redisplaying to replace an echo area message; since in
12461 that case the mini-buffer contents per se are usually
12462 unchanged. This code is of no real use in the mini-buffer
12463 since the handling of this_line_start_pos, etc., in redisplay
12464 handles the same cases. */
12465 && !EQ (window, minibuf_window)
12466 /* When splitting windows or for new windows, it happens that
12467 redisplay is called with a nil window_end_vpos or one being
12468 larger than the window. This should really be fixed in
12469 window.c. I don't have this on my list, now, so we do
12470 approximately the same as the old redisplay code. --gerd. */
12471 && INTEGERP (w->window_end_vpos)
12472 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12473 && (FRAME_WINDOW_P (f)
12474 || !overlay_arrow_in_current_buffer_p ()))
12476 int this_scroll_margin, top_scroll_margin;
12477 struct glyph_row *row = NULL;
12479 #if GLYPH_DEBUG
12480 debug_method_add (w, "cursor movement");
12481 #endif
12483 /* Scroll if point within this distance from the top or bottom
12484 of the window. This is a pixel value. */
12485 this_scroll_margin = max (0, scroll_margin);
12486 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12487 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12489 top_scroll_margin = this_scroll_margin;
12490 if (WINDOW_WANTS_HEADER_LINE_P (w))
12491 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12493 /* Start with the row the cursor was displayed during the last
12494 not paused redisplay. Give up if that row is not valid. */
12495 if (w->last_cursor.vpos < 0
12496 || w->last_cursor.vpos >= w->current_matrix->nrows)
12497 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12498 else
12500 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12501 if (row->mode_line_p)
12502 ++row;
12503 if (!row->enabled_p)
12504 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12507 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12509 int scroll_p = 0;
12510 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12512 if (PT > XFASTINT (w->last_point))
12514 /* Point has moved forward. */
12515 while (MATRIX_ROW_END_CHARPOS (row) < PT
12516 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12518 xassert (row->enabled_p);
12519 ++row;
12522 /* The end position of a row equals the start position
12523 of the next row. If PT is there, we would rather
12524 display it in the next line. */
12525 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12526 && MATRIX_ROW_END_CHARPOS (row) == PT
12527 && !cursor_row_p (w, row))
12528 ++row;
12530 /* If within the scroll margin, scroll. Note that
12531 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12532 the next line would be drawn, and that
12533 this_scroll_margin can be zero. */
12534 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12535 || PT > MATRIX_ROW_END_CHARPOS (row)
12536 /* Line is completely visible last line in window
12537 and PT is to be set in the next line. */
12538 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12539 && PT == MATRIX_ROW_END_CHARPOS (row)
12540 && !row->ends_at_zv_p
12541 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12542 scroll_p = 1;
12544 else if (PT < XFASTINT (w->last_point))
12546 /* Cursor has to be moved backward. Note that PT >=
12547 CHARPOS (startp) because of the outer if-statement. */
12548 while (!row->mode_line_p
12549 && (MATRIX_ROW_START_CHARPOS (row) > PT
12550 || (MATRIX_ROW_START_CHARPOS (row) == PT
12551 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12552 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12553 row > w->current_matrix->rows
12554 && (row-1)->ends_in_newline_from_string_p))))
12555 && (row->y > top_scroll_margin
12556 || CHARPOS (startp) == BEGV))
12558 xassert (row->enabled_p);
12559 --row;
12562 /* Consider the following case: Window starts at BEGV,
12563 there is invisible, intangible text at BEGV, so that
12564 display starts at some point START > BEGV. It can
12565 happen that we are called with PT somewhere between
12566 BEGV and START. Try to handle that case. */
12567 if (row < w->current_matrix->rows
12568 || row->mode_line_p)
12570 row = w->current_matrix->rows;
12571 if (row->mode_line_p)
12572 ++row;
12575 /* Due to newlines in overlay strings, we may have to
12576 skip forward over overlay strings. */
12577 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12578 && MATRIX_ROW_END_CHARPOS (row) == PT
12579 && !cursor_row_p (w, row))
12580 ++row;
12582 /* If within the scroll margin, scroll. */
12583 if (row->y < top_scroll_margin
12584 && CHARPOS (startp) != BEGV)
12585 scroll_p = 1;
12587 else
12589 /* Cursor did not move. So don't scroll even if cursor line
12590 is partially visible, as it was so before. */
12591 rc = CURSOR_MOVEMENT_SUCCESS;
12594 if (PT < MATRIX_ROW_START_CHARPOS (row)
12595 || PT > MATRIX_ROW_END_CHARPOS (row))
12597 /* if PT is not in the glyph row, give up. */
12598 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12600 else if (rc != CURSOR_MOVEMENT_SUCCESS
12601 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12602 && make_cursor_line_fully_visible_p)
12604 if (PT == MATRIX_ROW_END_CHARPOS (row)
12605 && !row->ends_at_zv_p
12606 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12608 else if (row->height > window_box_height (w))
12610 /* If we end up in a partially visible line, let's
12611 make it fully visible, except when it's taller
12612 than the window, in which case we can't do much
12613 about it. */
12614 *scroll_step = 1;
12615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12617 else
12619 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12620 if (!cursor_row_fully_visible_p (w, 0, 1))
12621 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12622 else
12623 rc = CURSOR_MOVEMENT_SUCCESS;
12626 else if (scroll_p)
12627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12628 else
12632 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12634 rc = CURSOR_MOVEMENT_SUCCESS;
12635 break;
12637 ++row;
12639 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12640 && MATRIX_ROW_START_CHARPOS (row) == PT
12641 && cursor_row_p (w, row));
12646 return rc;
12649 void
12650 set_vertical_scroll_bar (w)
12651 struct window *w;
12653 int start, end, whole;
12655 /* Calculate the start and end positions for the current window.
12656 At some point, it would be nice to choose between scrollbars
12657 which reflect the whole buffer size, with special markers
12658 indicating narrowing, and scrollbars which reflect only the
12659 visible region.
12661 Note that mini-buffers sometimes aren't displaying any text. */
12662 if (!MINI_WINDOW_P (w)
12663 || (w == XWINDOW (minibuf_window)
12664 && NILP (echo_area_buffer[0])))
12666 struct buffer *buf = XBUFFER (w->buffer);
12667 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12668 start = marker_position (w->start) - BUF_BEGV (buf);
12669 /* I don't think this is guaranteed to be right. For the
12670 moment, we'll pretend it is. */
12671 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12673 if (end < start)
12674 end = start;
12675 if (whole < (end - start))
12676 whole = end - start;
12678 else
12679 start = end = whole = 0;
12681 /* Indicate what this scroll bar ought to be displaying now. */
12682 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12686 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12687 selected_window is redisplayed.
12689 We can return without actually redisplaying the window if
12690 fonts_changed_p is nonzero. In that case, redisplay_internal will
12691 retry. */
12693 static void
12694 redisplay_window (window, just_this_one_p)
12695 Lisp_Object window;
12696 int just_this_one_p;
12698 struct window *w = XWINDOW (window);
12699 struct frame *f = XFRAME (w->frame);
12700 struct buffer *buffer = XBUFFER (w->buffer);
12701 struct buffer *old = current_buffer;
12702 struct text_pos lpoint, opoint, startp;
12703 int update_mode_line;
12704 int tem;
12705 struct it it;
12706 /* Record it now because it's overwritten. */
12707 int current_matrix_up_to_date_p = 0;
12708 int used_current_matrix_p = 0;
12709 /* This is less strict than current_matrix_up_to_date_p.
12710 It indictes that the buffer contents and narrowing are unchanged. */
12711 int buffer_unchanged_p = 0;
12712 int temp_scroll_step = 0;
12713 int count = SPECPDL_INDEX ();
12714 int rc;
12715 int centering_position = -1;
12716 int last_line_misfit = 0;
12718 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12719 opoint = lpoint;
12721 /* W must be a leaf window here. */
12722 xassert (!NILP (w->buffer));
12723 #if GLYPH_DEBUG
12724 *w->desired_matrix->method = 0;
12725 #endif
12727 specbind (Qinhibit_point_motion_hooks, Qt);
12729 reconsider_clip_changes (w, buffer);
12731 /* Has the mode line to be updated? */
12732 update_mode_line = (!NILP (w->update_mode_line)
12733 || update_mode_lines
12734 || buffer->clip_changed
12735 || buffer->prevent_redisplay_optimizations_p);
12737 if (MINI_WINDOW_P (w))
12739 if (w == XWINDOW (echo_area_window)
12740 && !NILP (echo_area_buffer[0]))
12742 if (update_mode_line)
12743 /* We may have to update a tty frame's menu bar or a
12744 tool-bar. Example `M-x C-h C-h C-g'. */
12745 goto finish_menu_bars;
12746 else
12747 /* We've already displayed the echo area glyphs in this window. */
12748 goto finish_scroll_bars;
12750 else if ((w != XWINDOW (minibuf_window)
12751 || minibuf_level == 0)
12752 /* When buffer is nonempty, redisplay window normally. */
12753 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12754 /* Quail displays non-mini buffers in minibuffer window.
12755 In that case, redisplay the window normally. */
12756 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12758 /* W is a mini-buffer window, but it's not active, so clear
12759 it. */
12760 int yb = window_text_bottom_y (w);
12761 struct glyph_row *row;
12762 int y;
12764 for (y = 0, row = w->desired_matrix->rows;
12765 y < yb;
12766 y += row->height, ++row)
12767 blank_row (w, row, y);
12768 goto finish_scroll_bars;
12771 clear_glyph_matrix (w->desired_matrix);
12774 /* Otherwise set up data on this window; select its buffer and point
12775 value. */
12776 /* Really select the buffer, for the sake of buffer-local
12777 variables. */
12778 set_buffer_internal_1 (XBUFFER (w->buffer));
12779 SET_TEXT_POS (opoint, PT, PT_BYTE);
12781 current_matrix_up_to_date_p
12782 = (!NILP (w->window_end_valid)
12783 && !current_buffer->clip_changed
12784 && !current_buffer->prevent_redisplay_optimizations_p
12785 && XFASTINT (w->last_modified) >= MODIFF
12786 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12788 buffer_unchanged_p
12789 = (!NILP (w->window_end_valid)
12790 && !current_buffer->clip_changed
12791 && XFASTINT (w->last_modified) >= MODIFF
12792 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12794 /* When windows_or_buffers_changed is non-zero, we can't rely on
12795 the window end being valid, so set it to nil there. */
12796 if (windows_or_buffers_changed)
12798 /* If window starts on a continuation line, maybe adjust the
12799 window start in case the window's width changed. */
12800 if (XMARKER (w->start)->buffer == current_buffer)
12801 compute_window_start_on_continuation_line (w);
12803 w->window_end_valid = Qnil;
12806 /* Some sanity checks. */
12807 CHECK_WINDOW_END (w);
12808 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12809 abort ();
12810 if (BYTEPOS (opoint) < CHARPOS (opoint))
12811 abort ();
12813 /* If %c is in mode line, update it if needed. */
12814 if (!NILP (w->column_number_displayed)
12815 /* This alternative quickly identifies a common case
12816 where no change is needed. */
12817 && !(PT == XFASTINT (w->last_point)
12818 && XFASTINT (w->last_modified) >= MODIFF
12819 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12820 && (XFASTINT (w->column_number_displayed)
12821 != (int) current_column ())) /* iftc */
12822 update_mode_line = 1;
12824 /* Count number of windows showing the selected buffer. An indirect
12825 buffer counts as its base buffer. */
12826 if (!just_this_one_p)
12828 struct buffer *current_base, *window_base;
12829 current_base = current_buffer;
12830 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12831 if (current_base->base_buffer)
12832 current_base = current_base->base_buffer;
12833 if (window_base->base_buffer)
12834 window_base = window_base->base_buffer;
12835 if (current_base == window_base)
12836 buffer_shared++;
12839 /* Point refers normally to the selected window. For any other
12840 window, set up appropriate value. */
12841 if (!EQ (window, selected_window))
12843 int new_pt = XMARKER (w->pointm)->charpos;
12844 int new_pt_byte = marker_byte_position (w->pointm);
12845 if (new_pt < BEGV)
12847 new_pt = BEGV;
12848 new_pt_byte = BEGV_BYTE;
12849 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12851 else if (new_pt > (ZV - 1))
12853 new_pt = ZV;
12854 new_pt_byte = ZV_BYTE;
12855 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12858 /* We don't use SET_PT so that the point-motion hooks don't run. */
12859 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12862 /* If any of the character widths specified in the display table
12863 have changed, invalidate the width run cache. It's true that
12864 this may be a bit late to catch such changes, but the rest of
12865 redisplay goes (non-fatally) haywire when the display table is
12866 changed, so why should we worry about doing any better? */
12867 if (current_buffer->width_run_cache)
12869 struct Lisp_Char_Table *disptab = buffer_display_table ();
12871 if (! disptab_matches_widthtab (disptab,
12872 XVECTOR (current_buffer->width_table)))
12874 invalidate_region_cache (current_buffer,
12875 current_buffer->width_run_cache,
12876 BEG, Z);
12877 recompute_width_table (current_buffer, disptab);
12881 /* If window-start is screwed up, choose a new one. */
12882 if (XMARKER (w->start)->buffer != current_buffer)
12883 goto recenter;
12885 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12887 /* If someone specified a new starting point but did not insist,
12888 check whether it can be used. */
12889 if (!NILP (w->optional_new_start)
12890 && CHARPOS (startp) >= BEGV
12891 && CHARPOS (startp) <= ZV)
12893 w->optional_new_start = Qnil;
12894 start_display (&it, w, startp);
12895 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12896 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12897 if (IT_CHARPOS (it) == PT)
12898 w->force_start = Qt;
12899 /* IT may overshoot PT if text at PT is invisible. */
12900 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12901 w->force_start = Qt;
12904 /* Handle case where place to start displaying has been specified,
12905 unless the specified location is outside the accessible range. */
12906 if (!NILP (w->force_start)
12907 || w->frozen_window_start_p)
12909 /* We set this later on if we have to adjust point. */
12910 int new_vpos = -1;
12911 int val;
12913 w->force_start = Qnil;
12914 w->vscroll = 0;
12915 w->window_end_valid = Qnil;
12917 /* Forget any recorded base line for line number display. */
12918 if (!buffer_unchanged_p)
12919 w->base_line_number = Qnil;
12921 /* Redisplay the mode line. Select the buffer properly for that.
12922 Also, run the hook window-scroll-functions
12923 because we have scrolled. */
12924 /* Note, we do this after clearing force_start because
12925 if there's an error, it is better to forget about force_start
12926 than to get into an infinite loop calling the hook functions
12927 and having them get more errors. */
12928 if (!update_mode_line
12929 || ! NILP (Vwindow_scroll_functions))
12931 update_mode_line = 1;
12932 w->update_mode_line = Qt;
12933 startp = run_window_scroll_functions (window, startp);
12936 w->last_modified = make_number (0);
12937 w->last_overlay_modified = make_number (0);
12938 if (CHARPOS (startp) < BEGV)
12939 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12940 else if (CHARPOS (startp) > ZV)
12941 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12943 /* Redisplay, then check if cursor has been set during the
12944 redisplay. Give up if new fonts were loaded. */
12945 val = try_window (window, startp, 1);
12946 if (!val)
12948 w->force_start = Qt;
12949 clear_glyph_matrix (w->desired_matrix);
12950 goto need_larger_matrices;
12952 /* Point was outside the scroll margins. */
12953 if (val < 0)
12954 new_vpos = window_box_height (w) / 2;
12956 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12958 /* If point does not appear, try to move point so it does
12959 appear. The desired matrix has been built above, so we
12960 can use it here. */
12961 new_vpos = window_box_height (w) / 2;
12964 if (!cursor_row_fully_visible_p (w, 0, 0))
12966 /* Point does appear, but on a line partly visible at end of window.
12967 Move it back to a fully-visible line. */
12968 new_vpos = window_box_height (w);
12971 /* If we need to move point for either of the above reasons,
12972 now actually do it. */
12973 if (new_vpos >= 0)
12975 struct glyph_row *row;
12977 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12978 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12979 ++row;
12981 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12982 MATRIX_ROW_START_BYTEPOS (row));
12984 if (w != XWINDOW (selected_window))
12985 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12986 else if (current_buffer == old)
12987 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12989 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12991 /* If we are highlighting the region, then we just changed
12992 the region, so redisplay to show it. */
12993 if (!NILP (Vtransient_mark_mode)
12994 && !NILP (current_buffer->mark_active))
12996 clear_glyph_matrix (w->desired_matrix);
12997 if (!try_window (window, startp, 0))
12998 goto need_larger_matrices;
13002 #if GLYPH_DEBUG
13003 debug_method_add (w, "forced window start");
13004 #endif
13005 goto done;
13008 /* Handle case where text has not changed, only point, and it has
13009 not moved off the frame, and we are not retrying after hscroll.
13010 (current_matrix_up_to_date_p is nonzero when retrying.) */
13011 if (current_matrix_up_to_date_p
13012 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13013 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13015 switch (rc)
13017 case CURSOR_MOVEMENT_SUCCESS:
13018 used_current_matrix_p = 1;
13019 goto done;
13021 #if 0 /* try_cursor_movement never returns this value. */
13022 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13023 goto need_larger_matrices;
13024 #endif
13026 case CURSOR_MOVEMENT_MUST_SCROLL:
13027 goto try_to_scroll;
13029 default:
13030 abort ();
13033 /* If current starting point was originally the beginning of a line
13034 but no longer is, find a new starting point. */
13035 else if (!NILP (w->start_at_line_beg)
13036 && !(CHARPOS (startp) <= BEGV
13037 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13039 #if GLYPH_DEBUG
13040 debug_method_add (w, "recenter 1");
13041 #endif
13042 goto recenter;
13045 /* Try scrolling with try_window_id. Value is > 0 if update has
13046 been done, it is -1 if we know that the same window start will
13047 not work. It is 0 if unsuccessful for some other reason. */
13048 else if ((tem = try_window_id (w)) != 0)
13050 #if GLYPH_DEBUG
13051 debug_method_add (w, "try_window_id %d", tem);
13052 #endif
13054 if (fonts_changed_p)
13055 goto need_larger_matrices;
13056 if (tem > 0)
13057 goto done;
13059 /* Otherwise try_window_id has returned -1 which means that we
13060 don't want the alternative below this comment to execute. */
13062 else if (CHARPOS (startp) >= BEGV
13063 && CHARPOS (startp) <= ZV
13064 && PT >= CHARPOS (startp)
13065 && (CHARPOS (startp) < ZV
13066 /* Avoid starting at end of buffer. */
13067 || CHARPOS (startp) == BEGV
13068 || (XFASTINT (w->last_modified) >= MODIFF
13069 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13072 /* If first window line is a continuation line, and window start
13073 is inside the modified region, but the first change is before
13074 current window start, we must select a new window start.*/
13075 if (NILP (w->start_at_line_beg)
13076 && CHARPOS (startp) > BEGV)
13078 /* Make sure beg_unchanged and end_unchanged are up to date.
13079 Do it only if buffer has really changed. This may or may
13080 not have been done by try_window_id (see which) already. */
13081 if (MODIFF > SAVE_MODIFF
13082 /* This seems to happen sometimes after saving a buffer. */
13083 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13085 if (GPT - BEG < BEG_UNCHANGED)
13086 BEG_UNCHANGED = GPT - BEG;
13087 if (Z - GPT < END_UNCHANGED)
13088 END_UNCHANGED = Z - GPT;
13091 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13092 && CHARPOS (startp) <= Z - END_UNCHANGED)
13094 /* There doesn't seems to be a simple way to find a new
13095 window start that is near the old window start, so
13096 we just recenter. */
13097 goto recenter;
13101 #if GLYPH_DEBUG
13102 debug_method_add (w, "same window start");
13103 #endif
13105 /* Try to redisplay starting at same place as before.
13106 If point has not moved off frame, accept the results. */
13107 if (!current_matrix_up_to_date_p
13108 /* Don't use try_window_reusing_current_matrix in this case
13109 because a window scroll function can have changed the
13110 buffer. */
13111 || !NILP (Vwindow_scroll_functions)
13112 || MINI_WINDOW_P (w)
13113 || !(used_current_matrix_p
13114 = try_window_reusing_current_matrix (w)))
13116 IF_DEBUG (debug_method_add (w, "1"));
13117 if (try_window (window, startp, 1) < 0)
13118 /* -1 means we need to scroll.
13119 0 means we need new matrices, but fonts_changed_p
13120 is set in that case, so we will detect it below. */
13121 goto try_to_scroll;
13124 if (fonts_changed_p)
13125 goto need_larger_matrices;
13127 if (w->cursor.vpos >= 0)
13129 if (!just_this_one_p
13130 || current_buffer->clip_changed
13131 || BEG_UNCHANGED < CHARPOS (startp))
13132 /* Forget any recorded base line for line number display. */
13133 w->base_line_number = Qnil;
13135 if (!cursor_row_fully_visible_p (w, 1, 0))
13137 clear_glyph_matrix (w->desired_matrix);
13138 last_line_misfit = 1;
13140 /* Drop through and scroll. */
13141 else
13142 goto done;
13144 else
13145 clear_glyph_matrix (w->desired_matrix);
13148 try_to_scroll:
13150 w->last_modified = make_number (0);
13151 w->last_overlay_modified = make_number (0);
13153 /* Redisplay the mode line. Select the buffer properly for that. */
13154 if (!update_mode_line)
13156 update_mode_line = 1;
13157 w->update_mode_line = Qt;
13160 /* Try to scroll by specified few lines. */
13161 if ((scroll_conservatively
13162 || scroll_step
13163 || temp_scroll_step
13164 || NUMBERP (current_buffer->scroll_up_aggressively)
13165 || NUMBERP (current_buffer->scroll_down_aggressively))
13166 && !current_buffer->clip_changed
13167 && CHARPOS (startp) >= BEGV
13168 && CHARPOS (startp) <= ZV)
13170 /* The function returns -1 if new fonts were loaded, 1 if
13171 successful, 0 if not successful. */
13172 int rc = try_scrolling (window, just_this_one_p,
13173 scroll_conservatively,
13174 scroll_step,
13175 temp_scroll_step, last_line_misfit);
13176 switch (rc)
13178 case SCROLLING_SUCCESS:
13179 goto done;
13181 case SCROLLING_NEED_LARGER_MATRICES:
13182 goto need_larger_matrices;
13184 case SCROLLING_FAILED:
13185 break;
13187 default:
13188 abort ();
13192 /* Finally, just choose place to start which centers point */
13194 recenter:
13195 if (centering_position < 0)
13196 centering_position = window_box_height (w) / 2;
13198 #if GLYPH_DEBUG
13199 debug_method_add (w, "recenter");
13200 #endif
13202 /* w->vscroll = 0; */
13204 /* Forget any previously recorded base line for line number display. */
13205 if (!buffer_unchanged_p)
13206 w->base_line_number = Qnil;
13208 /* Move backward half the height of the window. */
13209 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13210 it.current_y = it.last_visible_y;
13211 move_it_vertically_backward (&it, centering_position);
13212 xassert (IT_CHARPOS (it) >= BEGV);
13214 /* The function move_it_vertically_backward may move over more
13215 than the specified y-distance. If it->w is small, e.g. a
13216 mini-buffer window, we may end up in front of the window's
13217 display area. Start displaying at the start of the line
13218 containing PT in this case. */
13219 if (it.current_y <= 0)
13221 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13222 move_it_vertically_backward (&it, 0);
13223 #if 0
13224 /* I think this assert is bogus if buffer contains
13225 invisible text or images. KFS. */
13226 xassert (IT_CHARPOS (it) <= PT);
13227 #endif
13228 it.current_y = 0;
13231 it.current_x = it.hpos = 0;
13233 /* Set startp here explicitly in case that helps avoid an infinite loop
13234 in case the window-scroll-functions functions get errors. */
13235 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13237 /* Run scroll hooks. */
13238 startp = run_window_scroll_functions (window, it.current.pos);
13240 /* Redisplay the window. */
13241 if (!current_matrix_up_to_date_p
13242 || windows_or_buffers_changed
13243 || cursor_type_changed
13244 /* Don't use try_window_reusing_current_matrix in this case
13245 because it can have changed the buffer. */
13246 || !NILP (Vwindow_scroll_functions)
13247 || !just_this_one_p
13248 || MINI_WINDOW_P (w)
13249 || !(used_current_matrix_p
13250 = try_window_reusing_current_matrix (w)))
13251 try_window (window, startp, 0);
13253 /* If new fonts have been loaded (due to fontsets), give up. We
13254 have to start a new redisplay since we need to re-adjust glyph
13255 matrices. */
13256 if (fonts_changed_p)
13257 goto need_larger_matrices;
13259 /* If cursor did not appear assume that the middle of the window is
13260 in the first line of the window. Do it again with the next line.
13261 (Imagine a window of height 100, displaying two lines of height
13262 60. Moving back 50 from it->last_visible_y will end in the first
13263 line.) */
13264 if (w->cursor.vpos < 0)
13266 if (!NILP (w->window_end_valid)
13267 && PT >= Z - XFASTINT (w->window_end_pos))
13269 clear_glyph_matrix (w->desired_matrix);
13270 move_it_by_lines (&it, 1, 0);
13271 try_window (window, it.current.pos, 0);
13273 else if (PT < IT_CHARPOS (it))
13275 clear_glyph_matrix (w->desired_matrix);
13276 move_it_by_lines (&it, -1, 0);
13277 try_window (window, it.current.pos, 0);
13279 else
13281 /* Not much we can do about it. */
13285 /* Consider the following case: Window starts at BEGV, there is
13286 invisible, intangible text at BEGV, so that display starts at
13287 some point START > BEGV. It can happen that we are called with
13288 PT somewhere between BEGV and START. Try to handle that case. */
13289 if (w->cursor.vpos < 0)
13291 struct glyph_row *row = w->current_matrix->rows;
13292 if (row->mode_line_p)
13293 ++row;
13294 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13297 if (!cursor_row_fully_visible_p (w, 0, 0))
13299 /* If vscroll is enabled, disable it and try again. */
13300 if (w->vscroll)
13302 w->vscroll = 0;
13303 clear_glyph_matrix (w->desired_matrix);
13304 goto recenter;
13307 /* If centering point failed to make the whole line visible,
13308 put point at the top instead. That has to make the whole line
13309 visible, if it can be done. */
13310 if (centering_position == 0)
13311 goto done;
13313 clear_glyph_matrix (w->desired_matrix);
13314 centering_position = 0;
13315 goto recenter;
13318 done:
13320 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13321 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13322 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13323 ? Qt : Qnil);
13325 /* Display the mode line, if we must. */
13326 if ((update_mode_line
13327 /* If window not full width, must redo its mode line
13328 if (a) the window to its side is being redone and
13329 (b) we do a frame-based redisplay. This is a consequence
13330 of how inverted lines are drawn in frame-based redisplay. */
13331 || (!just_this_one_p
13332 && !FRAME_WINDOW_P (f)
13333 && !WINDOW_FULL_WIDTH_P (w))
13334 /* Line number to display. */
13335 || INTEGERP (w->base_line_pos)
13336 /* Column number is displayed and different from the one displayed. */
13337 || (!NILP (w->column_number_displayed)
13338 && (XFASTINT (w->column_number_displayed)
13339 != (int) current_column ()))) /* iftc */
13340 /* This means that the window has a mode line. */
13341 && (WINDOW_WANTS_MODELINE_P (w)
13342 || WINDOW_WANTS_HEADER_LINE_P (w)))
13344 display_mode_lines (w);
13346 /* If mode line height has changed, arrange for a thorough
13347 immediate redisplay using the correct mode line height. */
13348 if (WINDOW_WANTS_MODELINE_P (w)
13349 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13351 fonts_changed_p = 1;
13352 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13353 = DESIRED_MODE_LINE_HEIGHT (w);
13356 /* If top line height has changed, arrange for a thorough
13357 immediate redisplay using the correct mode line height. */
13358 if (WINDOW_WANTS_HEADER_LINE_P (w)
13359 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13361 fonts_changed_p = 1;
13362 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13363 = DESIRED_HEADER_LINE_HEIGHT (w);
13366 if (fonts_changed_p)
13367 goto need_larger_matrices;
13370 if (!line_number_displayed
13371 && !BUFFERP (w->base_line_pos))
13373 w->base_line_pos = Qnil;
13374 w->base_line_number = Qnil;
13377 finish_menu_bars:
13379 /* When we reach a frame's selected window, redo the frame's menu bar. */
13380 if (update_mode_line
13381 && EQ (FRAME_SELECTED_WINDOW (f), window))
13383 int redisplay_menu_p = 0;
13384 int redisplay_tool_bar_p = 0;
13386 if (FRAME_WINDOW_P (f))
13388 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13389 || defined (USE_GTK)
13390 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13391 #else
13392 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13393 #endif
13395 else
13396 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13398 if (redisplay_menu_p)
13399 display_menu_bar (w);
13401 #ifdef HAVE_WINDOW_SYSTEM
13402 #ifdef USE_GTK
13403 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13404 #else
13405 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13406 && (FRAME_TOOL_BAR_LINES (f) > 0
13407 || auto_resize_tool_bars_p);
13409 #endif
13411 if (redisplay_tool_bar_p)
13412 redisplay_tool_bar (f);
13413 #endif
13416 #ifdef HAVE_WINDOW_SYSTEM
13417 if (FRAME_WINDOW_P (f)
13418 && update_window_fringes (w, (just_this_one_p
13419 || (!used_current_matrix_p && !overlay_arrow_seen)
13420 || w->pseudo_window_p)))
13422 update_begin (f);
13423 BLOCK_INPUT;
13424 if (draw_window_fringes (w, 1))
13425 x_draw_vertical_border (w);
13426 UNBLOCK_INPUT;
13427 update_end (f);
13429 #endif /* HAVE_WINDOW_SYSTEM */
13431 /* We go to this label, with fonts_changed_p nonzero,
13432 if it is necessary to try again using larger glyph matrices.
13433 We have to redeem the scroll bar even in this case,
13434 because the loop in redisplay_internal expects that. */
13435 need_larger_matrices:
13437 finish_scroll_bars:
13439 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13441 /* Set the thumb's position and size. */
13442 set_vertical_scroll_bar (w);
13444 /* Note that we actually used the scroll bar attached to this
13445 window, so it shouldn't be deleted at the end of redisplay. */
13446 redeem_scroll_bar_hook (w);
13449 /* Restore current_buffer and value of point in it. */
13450 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13451 set_buffer_internal_1 (old);
13452 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13454 unbind_to (count, Qnil);
13458 /* Build the complete desired matrix of WINDOW with a window start
13459 buffer position POS.
13461 Value is 1 if successful. It is zero if fonts were loaded during
13462 redisplay which makes re-adjusting glyph matrices necessary, and -1
13463 if point would appear in the scroll margins.
13464 (We check that only if CHECK_MARGINS is nonzero. */
13467 try_window (window, pos, check_margins)
13468 Lisp_Object window;
13469 struct text_pos pos;
13470 int check_margins;
13472 struct window *w = XWINDOW (window);
13473 struct it it;
13474 struct glyph_row *last_text_row = NULL;
13476 /* Make POS the new window start. */
13477 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13479 /* Mark cursor position as unknown. No overlay arrow seen. */
13480 w->cursor.vpos = -1;
13481 overlay_arrow_seen = 0;
13483 /* Initialize iterator and info to start at POS. */
13484 start_display (&it, w, pos);
13486 /* Display all lines of W. */
13487 while (it.current_y < it.last_visible_y)
13489 if (display_line (&it))
13490 last_text_row = it.glyph_row - 1;
13491 if (fonts_changed_p)
13492 return 0;
13495 /* Don't let the cursor end in the scroll margins. */
13496 if (check_margins
13497 && !MINI_WINDOW_P (w))
13499 int this_scroll_margin;
13501 this_scroll_margin = max (0, scroll_margin);
13502 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13503 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13505 if ((w->cursor.y < this_scroll_margin
13506 && CHARPOS (pos) > BEGV
13507 && IT_CHARPOS (it) < ZV)
13508 /* rms: considering make_cursor_line_fully_visible_p here
13509 seems to give wrong results. We don't want to recenter
13510 when the last line is partly visible, we want to allow
13511 that case to be handled in the usual way. */
13512 || (w->cursor.y + 1) > it.last_visible_y)
13514 w->cursor.vpos = -1;
13515 clear_glyph_matrix (w->desired_matrix);
13516 return -1;
13520 /* If bottom moved off end of frame, change mode line percentage. */
13521 if (XFASTINT (w->window_end_pos) <= 0
13522 && Z != IT_CHARPOS (it))
13523 w->update_mode_line = Qt;
13525 /* Set window_end_pos to the offset of the last character displayed
13526 on the window from the end of current_buffer. Set
13527 window_end_vpos to its row number. */
13528 if (last_text_row)
13530 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13531 w->window_end_bytepos
13532 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13533 w->window_end_pos
13534 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13535 w->window_end_vpos
13536 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13537 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13538 ->displays_text_p);
13540 else
13542 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13543 w->window_end_pos = make_number (Z - ZV);
13544 w->window_end_vpos = make_number (0);
13547 /* But that is not valid info until redisplay finishes. */
13548 w->window_end_valid = Qnil;
13549 return 1;
13554 /************************************************************************
13555 Window redisplay reusing current matrix when buffer has not changed
13556 ************************************************************************/
13558 /* Try redisplay of window W showing an unchanged buffer with a
13559 different window start than the last time it was displayed by
13560 reusing its current matrix. Value is non-zero if successful.
13561 W->start is the new window start. */
13563 static int
13564 try_window_reusing_current_matrix (w)
13565 struct window *w;
13567 struct frame *f = XFRAME (w->frame);
13568 struct glyph_row *row, *bottom_row;
13569 struct it it;
13570 struct run run;
13571 struct text_pos start, new_start;
13572 int nrows_scrolled, i;
13573 struct glyph_row *last_text_row;
13574 struct glyph_row *last_reused_text_row;
13575 struct glyph_row *start_row;
13576 int start_vpos, min_y, max_y;
13578 #if GLYPH_DEBUG
13579 if (inhibit_try_window_reusing)
13580 return 0;
13581 #endif
13583 if (/* This function doesn't handle terminal frames. */
13584 !FRAME_WINDOW_P (f)
13585 /* Don't try to reuse the display if windows have been split
13586 or such. */
13587 || windows_or_buffers_changed
13588 || cursor_type_changed)
13589 return 0;
13591 /* Can't do this if region may have changed. */
13592 if ((!NILP (Vtransient_mark_mode)
13593 && !NILP (current_buffer->mark_active))
13594 || !NILP (w->region_showing)
13595 || !NILP (Vshow_trailing_whitespace))
13596 return 0;
13598 /* If top-line visibility has changed, give up. */
13599 if (WINDOW_WANTS_HEADER_LINE_P (w)
13600 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13601 return 0;
13603 /* Give up if old or new display is scrolled vertically. We could
13604 make this function handle this, but right now it doesn't. */
13605 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13606 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13607 return 0;
13609 /* The variable new_start now holds the new window start. The old
13610 start `start' can be determined from the current matrix. */
13611 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13612 start = start_row->start.pos;
13613 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13615 /* Clear the desired matrix for the display below. */
13616 clear_glyph_matrix (w->desired_matrix);
13618 if (CHARPOS (new_start) <= CHARPOS (start))
13620 int first_row_y;
13622 /* Don't use this method if the display starts with an ellipsis
13623 displayed for invisible text. It's not easy to handle that case
13624 below, and it's certainly not worth the effort since this is
13625 not a frequent case. */
13626 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13627 return 0;
13629 IF_DEBUG (debug_method_add (w, "twu1"));
13631 /* Display up to a row that can be reused. The variable
13632 last_text_row is set to the last row displayed that displays
13633 text. Note that it.vpos == 0 if or if not there is a
13634 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13635 start_display (&it, w, new_start);
13636 first_row_y = it.current_y;
13637 w->cursor.vpos = -1;
13638 last_text_row = last_reused_text_row = NULL;
13640 while (it.current_y < it.last_visible_y
13641 && !fonts_changed_p)
13643 /* If we have reached into the characters in the START row,
13644 that means the line boundaries have changed. So we
13645 can't start copying with the row START. Maybe it will
13646 work to start copying with the following row. */
13647 while (IT_CHARPOS (it) > CHARPOS (start))
13649 /* Advance to the next row as the "start". */
13650 start_row++;
13651 start = start_row->start.pos;
13652 /* If there are no more rows to try, or just one, give up. */
13653 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13654 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13655 || CHARPOS (start) == ZV)
13657 clear_glyph_matrix (w->desired_matrix);
13658 return 0;
13661 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13663 /* If we have reached alignment,
13664 we can copy the rest of the rows. */
13665 if (IT_CHARPOS (it) == CHARPOS (start))
13666 break;
13668 if (display_line (&it))
13669 last_text_row = it.glyph_row - 1;
13672 /* A value of current_y < last_visible_y means that we stopped
13673 at the previous window start, which in turn means that we
13674 have at least one reusable row. */
13675 if (it.current_y < it.last_visible_y)
13677 /* IT.vpos always starts from 0; it counts text lines. */
13678 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13680 /* Find PT if not already found in the lines displayed. */
13681 if (w->cursor.vpos < 0)
13683 int dy = it.current_y - start_row->y;
13685 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13686 row = row_containing_pos (w, PT, row, NULL, dy);
13687 if (row)
13688 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13689 dy, nrows_scrolled);
13690 else
13692 clear_glyph_matrix (w->desired_matrix);
13693 return 0;
13697 /* Scroll the display. Do it before the current matrix is
13698 changed. The problem here is that update has not yet
13699 run, i.e. part of the current matrix is not up to date.
13700 scroll_run_hook will clear the cursor, and use the
13701 current matrix to get the height of the row the cursor is
13702 in. */
13703 run.current_y = start_row->y;
13704 run.desired_y = it.current_y;
13705 run.height = it.last_visible_y - it.current_y;
13707 if (run.height > 0 && run.current_y != run.desired_y)
13709 update_begin (f);
13710 rif->update_window_begin_hook (w);
13711 rif->clear_window_mouse_face (w);
13712 rif->scroll_run_hook (w, &run);
13713 rif->update_window_end_hook (w, 0, 0);
13714 update_end (f);
13717 /* Shift current matrix down by nrows_scrolled lines. */
13718 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13719 rotate_matrix (w->current_matrix,
13720 start_vpos,
13721 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13722 nrows_scrolled);
13724 /* Disable lines that must be updated. */
13725 for (i = 0; i < it.vpos; ++i)
13726 (start_row + i)->enabled_p = 0;
13728 /* Re-compute Y positions. */
13729 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13730 max_y = it.last_visible_y;
13731 for (row = start_row + nrows_scrolled;
13732 row < bottom_row;
13733 ++row)
13735 row->y = it.current_y;
13736 row->visible_height = row->height;
13738 if (row->y < min_y)
13739 row->visible_height -= min_y - row->y;
13740 if (row->y + row->height > max_y)
13741 row->visible_height -= row->y + row->height - max_y;
13742 row->redraw_fringe_bitmaps_p = 1;
13744 it.current_y += row->height;
13746 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13747 last_reused_text_row = row;
13748 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13749 break;
13752 /* Disable lines in the current matrix which are now
13753 below the window. */
13754 for (++row; row < bottom_row; ++row)
13755 row->enabled_p = row->mode_line_p = 0;
13758 /* Update window_end_pos etc.; last_reused_text_row is the last
13759 reused row from the current matrix containing text, if any.
13760 The value of last_text_row is the last displayed line
13761 containing text. */
13762 if (last_reused_text_row)
13764 w->window_end_bytepos
13765 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13766 w->window_end_pos
13767 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13768 w->window_end_vpos
13769 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13770 w->current_matrix));
13772 else if (last_text_row)
13774 w->window_end_bytepos
13775 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13776 w->window_end_pos
13777 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13778 w->window_end_vpos
13779 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13781 else
13783 /* This window must be completely empty. */
13784 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13785 w->window_end_pos = make_number (Z - ZV);
13786 w->window_end_vpos = make_number (0);
13788 w->window_end_valid = Qnil;
13790 /* Update hint: don't try scrolling again in update_window. */
13791 w->desired_matrix->no_scrolling_p = 1;
13793 #if GLYPH_DEBUG
13794 debug_method_add (w, "try_window_reusing_current_matrix 1");
13795 #endif
13796 return 1;
13798 else if (CHARPOS (new_start) > CHARPOS (start))
13800 struct glyph_row *pt_row, *row;
13801 struct glyph_row *first_reusable_row;
13802 struct glyph_row *first_row_to_display;
13803 int dy;
13804 int yb = window_text_bottom_y (w);
13806 /* Find the row starting at new_start, if there is one. Don't
13807 reuse a partially visible line at the end. */
13808 first_reusable_row = start_row;
13809 while (first_reusable_row->enabled_p
13810 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13811 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13812 < CHARPOS (new_start)))
13813 ++first_reusable_row;
13815 /* Give up if there is no row to reuse. */
13816 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13817 || !first_reusable_row->enabled_p
13818 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13819 != CHARPOS (new_start)))
13820 return 0;
13822 /* We can reuse fully visible rows beginning with
13823 first_reusable_row to the end of the window. Set
13824 first_row_to_display to the first row that cannot be reused.
13825 Set pt_row to the row containing point, if there is any. */
13826 pt_row = NULL;
13827 for (first_row_to_display = first_reusable_row;
13828 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13829 ++first_row_to_display)
13831 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13832 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13833 pt_row = first_row_to_display;
13836 /* Start displaying at the start of first_row_to_display. */
13837 xassert (first_row_to_display->y < yb);
13838 init_to_row_start (&it, w, first_row_to_display);
13840 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13841 - start_vpos);
13842 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13843 - nrows_scrolled);
13844 it.current_y = (first_row_to_display->y - first_reusable_row->y
13845 + WINDOW_HEADER_LINE_HEIGHT (w));
13847 /* Display lines beginning with first_row_to_display in the
13848 desired matrix. Set last_text_row to the last row displayed
13849 that displays text. */
13850 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13851 if (pt_row == NULL)
13852 w->cursor.vpos = -1;
13853 last_text_row = NULL;
13854 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13855 if (display_line (&it))
13856 last_text_row = it.glyph_row - 1;
13858 /* Give up If point isn't in a row displayed or reused. */
13859 if (w->cursor.vpos < 0)
13861 clear_glyph_matrix (w->desired_matrix);
13862 return 0;
13865 /* If point is in a reused row, adjust y and vpos of the cursor
13866 position. */
13867 if (pt_row)
13869 w->cursor.vpos -= nrows_scrolled;
13870 w->cursor.y -= first_reusable_row->y - start_row->y;
13873 /* Scroll the display. */
13874 run.current_y = first_reusable_row->y;
13875 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13876 run.height = it.last_visible_y - run.current_y;
13877 dy = run.current_y - run.desired_y;
13879 if (run.height)
13881 update_begin (f);
13882 rif->update_window_begin_hook (w);
13883 rif->clear_window_mouse_face (w);
13884 rif->scroll_run_hook (w, &run);
13885 rif->update_window_end_hook (w, 0, 0);
13886 update_end (f);
13889 /* Adjust Y positions of reused rows. */
13890 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13891 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13892 max_y = it.last_visible_y;
13893 for (row = first_reusable_row; row < first_row_to_display; ++row)
13895 row->y -= dy;
13896 row->visible_height = row->height;
13897 if (row->y < min_y)
13898 row->visible_height -= min_y - row->y;
13899 if (row->y + row->height > max_y)
13900 row->visible_height -= row->y + row->height - max_y;
13901 row->redraw_fringe_bitmaps_p = 1;
13904 /* Scroll the current matrix. */
13905 xassert (nrows_scrolled > 0);
13906 rotate_matrix (w->current_matrix,
13907 start_vpos,
13908 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13909 -nrows_scrolled);
13911 /* Disable rows not reused. */
13912 for (row -= nrows_scrolled; row < bottom_row; ++row)
13913 row->enabled_p = 0;
13915 /* Point may have moved to a different line, so we cannot assume that
13916 the previous cursor position is valid; locate the correct row. */
13917 if (pt_row)
13919 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13920 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13921 row++)
13923 w->cursor.vpos++;
13924 w->cursor.y = row->y;
13926 if (row < bottom_row)
13928 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13929 while (glyph->charpos < PT)
13931 w->cursor.hpos++;
13932 w->cursor.x += glyph->pixel_width;
13933 glyph++;
13938 /* Adjust window end. A null value of last_text_row means that
13939 the window end is in reused rows which in turn means that
13940 only its vpos can have changed. */
13941 if (last_text_row)
13943 w->window_end_bytepos
13944 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13945 w->window_end_pos
13946 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13947 w->window_end_vpos
13948 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13950 else
13952 w->window_end_vpos
13953 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13956 w->window_end_valid = Qnil;
13957 w->desired_matrix->no_scrolling_p = 1;
13959 #if GLYPH_DEBUG
13960 debug_method_add (w, "try_window_reusing_current_matrix 2");
13961 #endif
13962 return 1;
13965 return 0;
13970 /************************************************************************
13971 Window redisplay reusing current matrix when buffer has changed
13972 ************************************************************************/
13974 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13975 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13976 int *, int *));
13977 static struct glyph_row *
13978 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13979 struct glyph_row *));
13982 /* Return the last row in MATRIX displaying text. If row START is
13983 non-null, start searching with that row. IT gives the dimensions
13984 of the display. Value is null if matrix is empty; otherwise it is
13985 a pointer to the row found. */
13987 static struct glyph_row *
13988 find_last_row_displaying_text (matrix, it, start)
13989 struct glyph_matrix *matrix;
13990 struct it *it;
13991 struct glyph_row *start;
13993 struct glyph_row *row, *row_found;
13995 /* Set row_found to the last row in IT->w's current matrix
13996 displaying text. The loop looks funny but think of partially
13997 visible lines. */
13998 row_found = NULL;
13999 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14000 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14002 xassert (row->enabled_p);
14003 row_found = row;
14004 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14005 break;
14006 ++row;
14009 return row_found;
14013 /* Return the last row in the current matrix of W that is not affected
14014 by changes at the start of current_buffer that occurred since W's
14015 current matrix was built. Value is null if no such row exists.
14017 BEG_UNCHANGED us the number of characters unchanged at the start of
14018 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14019 first changed character in current_buffer. Characters at positions <
14020 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14021 when the current matrix was built. */
14023 static struct glyph_row *
14024 find_last_unchanged_at_beg_row (w)
14025 struct window *w;
14027 int first_changed_pos = BEG + BEG_UNCHANGED;
14028 struct glyph_row *row;
14029 struct glyph_row *row_found = NULL;
14030 int yb = window_text_bottom_y (w);
14032 /* Find the last row displaying unchanged text. */
14033 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14034 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14035 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14037 if (/* If row ends before first_changed_pos, it is unchanged,
14038 except in some case. */
14039 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14040 /* When row ends in ZV and we write at ZV it is not
14041 unchanged. */
14042 && !row->ends_at_zv_p
14043 /* When first_changed_pos is the end of a continued line,
14044 row is not unchanged because it may be no longer
14045 continued. */
14046 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14047 && (row->continued_p
14048 || row->exact_window_width_line_p)))
14049 row_found = row;
14051 /* Stop if last visible row. */
14052 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14053 break;
14055 ++row;
14058 return row_found;
14062 /* Find the first glyph row in the current matrix of W that is not
14063 affected by changes at the end of current_buffer since the
14064 time W's current matrix was built.
14066 Return in *DELTA the number of chars by which buffer positions in
14067 unchanged text at the end of current_buffer must be adjusted.
14069 Return in *DELTA_BYTES the corresponding number of bytes.
14071 Value is null if no such row exists, i.e. all rows are affected by
14072 changes. */
14074 static struct glyph_row *
14075 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14076 struct window *w;
14077 int *delta, *delta_bytes;
14079 struct glyph_row *row;
14080 struct glyph_row *row_found = NULL;
14082 *delta = *delta_bytes = 0;
14084 /* Display must not have been paused, otherwise the current matrix
14085 is not up to date. */
14086 if (NILP (w->window_end_valid))
14087 abort ();
14089 /* A value of window_end_pos >= END_UNCHANGED means that the window
14090 end is in the range of changed text. If so, there is no
14091 unchanged row at the end of W's current matrix. */
14092 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14093 return NULL;
14095 /* Set row to the last row in W's current matrix displaying text. */
14096 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14098 /* If matrix is entirely empty, no unchanged row exists. */
14099 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14101 /* The value of row is the last glyph row in the matrix having a
14102 meaningful buffer position in it. The end position of row
14103 corresponds to window_end_pos. This allows us to translate
14104 buffer positions in the current matrix to current buffer
14105 positions for characters not in changed text. */
14106 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14107 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14108 int last_unchanged_pos, last_unchanged_pos_old;
14109 struct glyph_row *first_text_row
14110 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14112 *delta = Z - Z_old;
14113 *delta_bytes = Z_BYTE - Z_BYTE_old;
14115 /* Set last_unchanged_pos to the buffer position of the last
14116 character in the buffer that has not been changed. Z is the
14117 index + 1 of the last character in current_buffer, i.e. by
14118 subtracting END_UNCHANGED we get the index of the last
14119 unchanged character, and we have to add BEG to get its buffer
14120 position. */
14121 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14122 last_unchanged_pos_old = last_unchanged_pos - *delta;
14124 /* Search backward from ROW for a row displaying a line that
14125 starts at a minimum position >= last_unchanged_pos_old. */
14126 for (; row > first_text_row; --row)
14128 /* This used to abort, but it can happen.
14129 It is ok to just stop the search instead here. KFS. */
14130 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14131 break;
14133 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14134 row_found = row;
14138 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14139 abort ();
14141 return row_found;
14145 /* Make sure that glyph rows in the current matrix of window W
14146 reference the same glyph memory as corresponding rows in the
14147 frame's frame matrix. This function is called after scrolling W's
14148 current matrix on a terminal frame in try_window_id and
14149 try_window_reusing_current_matrix. */
14151 static void
14152 sync_frame_with_window_matrix_rows (w)
14153 struct window *w;
14155 struct frame *f = XFRAME (w->frame);
14156 struct glyph_row *window_row, *window_row_end, *frame_row;
14158 /* Preconditions: W must be a leaf window and full-width. Its frame
14159 must have a frame matrix. */
14160 xassert (NILP (w->hchild) && NILP (w->vchild));
14161 xassert (WINDOW_FULL_WIDTH_P (w));
14162 xassert (!FRAME_WINDOW_P (f));
14164 /* If W is a full-width window, glyph pointers in W's current matrix
14165 have, by definition, to be the same as glyph pointers in the
14166 corresponding frame matrix. Note that frame matrices have no
14167 marginal areas (see build_frame_matrix). */
14168 window_row = w->current_matrix->rows;
14169 window_row_end = window_row + w->current_matrix->nrows;
14170 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14171 while (window_row < window_row_end)
14173 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14174 struct glyph *end = window_row->glyphs[LAST_AREA];
14176 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14177 frame_row->glyphs[TEXT_AREA] = start;
14178 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14179 frame_row->glyphs[LAST_AREA] = end;
14181 /* Disable frame rows whose corresponding window rows have
14182 been disabled in try_window_id. */
14183 if (!window_row->enabled_p)
14184 frame_row->enabled_p = 0;
14186 ++window_row, ++frame_row;
14191 /* Find the glyph row in window W containing CHARPOS. Consider all
14192 rows between START and END (not inclusive). END null means search
14193 all rows to the end of the display area of W. Value is the row
14194 containing CHARPOS or null. */
14196 struct glyph_row *
14197 row_containing_pos (w, charpos, start, end, dy)
14198 struct window *w;
14199 int charpos;
14200 struct glyph_row *start, *end;
14201 int dy;
14203 struct glyph_row *row = start;
14204 int last_y;
14206 /* If we happen to start on a header-line, skip that. */
14207 if (row->mode_line_p)
14208 ++row;
14210 if ((end && row >= end) || !row->enabled_p)
14211 return NULL;
14213 last_y = window_text_bottom_y (w) - dy;
14215 while (1)
14217 /* Give up if we have gone too far. */
14218 if (end && row >= end)
14219 return NULL;
14220 /* This formerly returned if they were equal.
14221 I think that both quantities are of a "last plus one" type;
14222 if so, when they are equal, the row is within the screen. -- rms. */
14223 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14224 return NULL;
14226 /* If it is in this row, return this row. */
14227 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14228 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14229 /* The end position of a row equals the start
14230 position of the next row. If CHARPOS is there, we
14231 would rather display it in the next line, except
14232 when this line ends in ZV. */
14233 && !row->ends_at_zv_p
14234 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14235 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14236 return row;
14237 ++row;
14242 /* Try to redisplay window W by reusing its existing display. W's
14243 current matrix must be up to date when this function is called,
14244 i.e. window_end_valid must not be nil.
14246 Value is
14248 1 if display has been updated
14249 0 if otherwise unsuccessful
14250 -1 if redisplay with same window start is known not to succeed
14252 The following steps are performed:
14254 1. Find the last row in the current matrix of W that is not
14255 affected by changes at the start of current_buffer. If no such row
14256 is found, give up.
14258 2. Find the first row in W's current matrix that is not affected by
14259 changes at the end of current_buffer. Maybe there is no such row.
14261 3. Display lines beginning with the row + 1 found in step 1 to the
14262 row found in step 2 or, if step 2 didn't find a row, to the end of
14263 the window.
14265 4. If cursor is not known to appear on the window, give up.
14267 5. If display stopped at the row found in step 2, scroll the
14268 display and current matrix as needed.
14270 6. Maybe display some lines at the end of W, if we must. This can
14271 happen under various circumstances, like a partially visible line
14272 becoming fully visible, or because newly displayed lines are displayed
14273 in smaller font sizes.
14275 7. Update W's window end information. */
14277 static int
14278 try_window_id (w)
14279 struct window *w;
14281 struct frame *f = XFRAME (w->frame);
14282 struct glyph_matrix *current_matrix = w->current_matrix;
14283 struct glyph_matrix *desired_matrix = w->desired_matrix;
14284 struct glyph_row *last_unchanged_at_beg_row;
14285 struct glyph_row *first_unchanged_at_end_row;
14286 struct glyph_row *row;
14287 struct glyph_row *bottom_row;
14288 int bottom_vpos;
14289 struct it it;
14290 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14291 struct text_pos start_pos;
14292 struct run run;
14293 int first_unchanged_at_end_vpos = 0;
14294 struct glyph_row *last_text_row, *last_text_row_at_end;
14295 struct text_pos start;
14296 int first_changed_charpos, last_changed_charpos;
14298 #if GLYPH_DEBUG
14299 if (inhibit_try_window_id)
14300 return 0;
14301 #endif
14303 /* This is handy for debugging. */
14304 #if 0
14305 #define GIVE_UP(X) \
14306 do { \
14307 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14308 return 0; \
14309 } while (0)
14310 #else
14311 #define GIVE_UP(X) return 0
14312 #endif
14314 SET_TEXT_POS_FROM_MARKER (start, w->start);
14316 /* Don't use this for mini-windows because these can show
14317 messages and mini-buffers, and we don't handle that here. */
14318 if (MINI_WINDOW_P (w))
14319 GIVE_UP (1);
14321 /* This flag is used to prevent redisplay optimizations. */
14322 if (windows_or_buffers_changed || cursor_type_changed)
14323 GIVE_UP (2);
14325 /* Verify that narrowing has not changed.
14326 Also verify that we were not told to prevent redisplay optimizations.
14327 It would be nice to further
14328 reduce the number of cases where this prevents try_window_id. */
14329 if (current_buffer->clip_changed
14330 || current_buffer->prevent_redisplay_optimizations_p)
14331 GIVE_UP (3);
14333 /* Window must either use window-based redisplay or be full width. */
14334 if (!FRAME_WINDOW_P (f)
14335 && (!line_ins_del_ok
14336 || !WINDOW_FULL_WIDTH_P (w)))
14337 GIVE_UP (4);
14339 /* Give up if point is not known NOT to appear in W. */
14340 if (PT < CHARPOS (start))
14341 GIVE_UP (5);
14343 /* Another way to prevent redisplay optimizations. */
14344 if (XFASTINT (w->last_modified) == 0)
14345 GIVE_UP (6);
14347 /* Verify that window is not hscrolled. */
14348 if (XFASTINT (w->hscroll) != 0)
14349 GIVE_UP (7);
14351 /* Verify that display wasn't paused. */
14352 if (NILP (w->window_end_valid))
14353 GIVE_UP (8);
14355 /* Can't use this if highlighting a region because a cursor movement
14356 will do more than just set the cursor. */
14357 if (!NILP (Vtransient_mark_mode)
14358 && !NILP (current_buffer->mark_active))
14359 GIVE_UP (9);
14361 /* Likewise if highlighting trailing whitespace. */
14362 if (!NILP (Vshow_trailing_whitespace))
14363 GIVE_UP (11);
14365 /* Likewise if showing a region. */
14366 if (!NILP (w->region_showing))
14367 GIVE_UP (10);
14369 /* Can use this if overlay arrow position and or string have changed. */
14370 if (overlay_arrows_changed_p ())
14371 GIVE_UP (12);
14374 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14375 only if buffer has really changed. The reason is that the gap is
14376 initially at Z for freshly visited files. The code below would
14377 set end_unchanged to 0 in that case. */
14378 if (MODIFF > SAVE_MODIFF
14379 /* This seems to happen sometimes after saving a buffer. */
14380 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14382 if (GPT - BEG < BEG_UNCHANGED)
14383 BEG_UNCHANGED = GPT - BEG;
14384 if (Z - GPT < END_UNCHANGED)
14385 END_UNCHANGED = Z - GPT;
14388 /* The position of the first and last character that has been changed. */
14389 first_changed_charpos = BEG + BEG_UNCHANGED;
14390 last_changed_charpos = Z - END_UNCHANGED;
14392 /* If window starts after a line end, and the last change is in
14393 front of that newline, then changes don't affect the display.
14394 This case happens with stealth-fontification. Note that although
14395 the display is unchanged, glyph positions in the matrix have to
14396 be adjusted, of course. */
14397 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14398 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14399 && ((last_changed_charpos < CHARPOS (start)
14400 && CHARPOS (start) == BEGV)
14401 || (last_changed_charpos < CHARPOS (start) - 1
14402 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14404 int Z_old, delta, Z_BYTE_old, delta_bytes;
14405 struct glyph_row *r0;
14407 /* Compute how many chars/bytes have been added to or removed
14408 from the buffer. */
14409 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14410 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14411 delta = Z - Z_old;
14412 delta_bytes = Z_BYTE - Z_BYTE_old;
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) + delta)
14418 GIVE_UP (13);
14420 /* If window start is unchanged, we can reuse the whole matrix
14421 as is, after adjusting glyph positions. No need to compute
14422 the window end again, since its offset from Z hasn't changed. */
14423 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14424 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14425 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14426 /* PT must not be in a partially visible line. */
14427 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14428 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14430 /* Adjust positions in the glyph matrix. */
14431 if (delta || delta_bytes)
14433 struct glyph_row *r1
14434 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14435 increment_matrix_positions (w->current_matrix,
14436 MATRIX_ROW_VPOS (r0, current_matrix),
14437 MATRIX_ROW_VPOS (r1, current_matrix),
14438 delta, delta_bytes);
14441 /* Set the cursor. */
14442 row = row_containing_pos (w, PT, r0, NULL, 0);
14443 if (row)
14444 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14445 else
14446 abort ();
14447 return 1;
14451 /* Handle the case that changes are all below what is displayed in
14452 the window, and that PT is in the window. This shortcut cannot
14453 be taken if ZV is visible in the window, and text has been added
14454 there that is visible in the window. */
14455 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14456 /* ZV is not visible in the window, or there are no
14457 changes at ZV, actually. */
14458 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14459 || first_changed_charpos == last_changed_charpos))
14461 struct glyph_row *r0;
14463 /* Give up if PT is not in the window. Note that it already has
14464 been checked at the start of try_window_id that PT is not in
14465 front of the window start. */
14466 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14467 GIVE_UP (14);
14469 /* If window start is unchanged, we can reuse the whole matrix
14470 as is, without changing glyph positions since no text has
14471 been added/removed in front of the window end. */
14472 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14473 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14474 /* PT must not be in a partially visible line. */
14475 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14476 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14478 /* We have to compute the window end anew since text
14479 can have been added/removed after it. */
14480 w->window_end_pos
14481 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14482 w->window_end_bytepos
14483 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14485 /* Set the cursor. */
14486 row = row_containing_pos (w, PT, r0, NULL, 0);
14487 if (row)
14488 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14489 else
14490 abort ();
14491 return 2;
14495 /* Give up if window start is in the changed area.
14497 The condition used to read
14499 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14501 but why that was tested escapes me at the moment. */
14502 if (CHARPOS (start) >= first_changed_charpos
14503 && CHARPOS (start) <= last_changed_charpos)
14504 GIVE_UP (15);
14506 /* Check that window start agrees with the start of the first glyph
14507 row in its current matrix. Check this after we know the window
14508 start is not in changed text, otherwise positions would not be
14509 comparable. */
14510 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14511 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14512 GIVE_UP (16);
14514 /* Give up if the window ends in strings. Overlay strings
14515 at the end are difficult to handle, so don't try. */
14516 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14517 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14518 GIVE_UP (20);
14520 /* Compute the position at which we have to start displaying new
14521 lines. Some of the lines at the top of the window might be
14522 reusable because they are not displaying changed text. Find the
14523 last row in W's current matrix not affected by changes at the
14524 start of current_buffer. Value is null if changes start in the
14525 first line of window. */
14526 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14527 if (last_unchanged_at_beg_row)
14529 /* Avoid starting to display in the moddle of a character, a TAB
14530 for instance. This is easier than to set up the iterator
14531 exactly, and it's not a frequent case, so the additional
14532 effort wouldn't really pay off. */
14533 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14534 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14535 && last_unchanged_at_beg_row > w->current_matrix->rows)
14536 --last_unchanged_at_beg_row;
14538 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14539 GIVE_UP (17);
14541 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14542 GIVE_UP (18);
14543 start_pos = it.current.pos;
14545 /* Start displaying new lines in the desired matrix at the same
14546 vpos we would use in the current matrix, i.e. below
14547 last_unchanged_at_beg_row. */
14548 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14549 current_matrix);
14550 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14551 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14553 xassert (it.hpos == 0 && it.current_x == 0);
14555 else
14557 /* There are no reusable lines at the start of the window.
14558 Start displaying in the first text line. */
14559 start_display (&it, w, start);
14560 it.vpos = it.first_vpos;
14561 start_pos = it.current.pos;
14564 /* Find the first row that is not affected by changes at the end of
14565 the buffer. Value will be null if there is no unchanged row, in
14566 which case we must redisplay to the end of the window. delta
14567 will be set to the value by which buffer positions beginning with
14568 first_unchanged_at_end_row have to be adjusted due to text
14569 changes. */
14570 first_unchanged_at_end_row
14571 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14572 IF_DEBUG (debug_delta = delta);
14573 IF_DEBUG (debug_delta_bytes = delta_bytes);
14575 /* Set stop_pos to the buffer position up to which we will have to
14576 display new lines. If first_unchanged_at_end_row != NULL, this
14577 is the buffer position of the start of the line displayed in that
14578 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14579 that we don't stop at a buffer position. */
14580 stop_pos = 0;
14581 if (first_unchanged_at_end_row)
14583 xassert (last_unchanged_at_beg_row == NULL
14584 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14586 /* If this is a continuation line, move forward to the next one
14587 that isn't. Changes in lines above affect this line.
14588 Caution: this may move first_unchanged_at_end_row to a row
14589 not displaying text. */
14590 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14591 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14592 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14593 < it.last_visible_y))
14594 ++first_unchanged_at_end_row;
14596 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14597 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14598 >= it.last_visible_y))
14599 first_unchanged_at_end_row = NULL;
14600 else
14602 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14603 + delta);
14604 first_unchanged_at_end_vpos
14605 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14606 xassert (stop_pos >= Z - END_UNCHANGED);
14609 else if (last_unchanged_at_beg_row == NULL)
14610 GIVE_UP (19);
14613 #if GLYPH_DEBUG
14615 /* Either there is no unchanged row at the end, or the one we have
14616 now displays text. This is a necessary condition for the window
14617 end pos calculation at the end of this function. */
14618 xassert (first_unchanged_at_end_row == NULL
14619 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14621 debug_last_unchanged_at_beg_vpos
14622 = (last_unchanged_at_beg_row
14623 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14624 : -1);
14625 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14627 #endif /* GLYPH_DEBUG != 0 */
14630 /* Display new lines. Set last_text_row to the last new line
14631 displayed which has text on it, i.e. might end up as being the
14632 line where the window_end_vpos is. */
14633 w->cursor.vpos = -1;
14634 last_text_row = NULL;
14635 overlay_arrow_seen = 0;
14636 while (it.current_y < it.last_visible_y
14637 && !fonts_changed_p
14638 && (first_unchanged_at_end_row == NULL
14639 || IT_CHARPOS (it) < stop_pos))
14641 if (display_line (&it))
14642 last_text_row = it.glyph_row - 1;
14645 if (fonts_changed_p)
14646 return -1;
14649 /* Compute differences in buffer positions, y-positions etc. for
14650 lines reused at the bottom of the window. Compute what we can
14651 scroll. */
14652 if (first_unchanged_at_end_row
14653 /* No lines reused because we displayed everything up to the
14654 bottom of the window. */
14655 && it.current_y < it.last_visible_y)
14657 dvpos = (it.vpos
14658 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14659 current_matrix));
14660 dy = it.current_y - first_unchanged_at_end_row->y;
14661 run.current_y = first_unchanged_at_end_row->y;
14662 run.desired_y = run.current_y + dy;
14663 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14665 else
14667 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14668 first_unchanged_at_end_row = NULL;
14670 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14673 /* Find the cursor if not already found. We have to decide whether
14674 PT will appear on this window (it sometimes doesn't, but this is
14675 not a very frequent case.) This decision has to be made before
14676 the current matrix is altered. A value of cursor.vpos < 0 means
14677 that PT is either in one of the lines beginning at
14678 first_unchanged_at_end_row or below the window. Don't care for
14679 lines that might be displayed later at the window end; as
14680 mentioned, this is not a frequent case. */
14681 if (w->cursor.vpos < 0)
14683 /* Cursor in unchanged rows at the top? */
14684 if (PT < CHARPOS (start_pos)
14685 && last_unchanged_at_beg_row)
14687 row = row_containing_pos (w, PT,
14688 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14689 last_unchanged_at_beg_row + 1, 0);
14690 if (row)
14691 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14694 /* Start from first_unchanged_at_end_row looking for PT. */
14695 else if (first_unchanged_at_end_row)
14697 row = row_containing_pos (w, PT - delta,
14698 first_unchanged_at_end_row, NULL, 0);
14699 if (row)
14700 set_cursor_from_row (w, row, w->current_matrix, delta,
14701 delta_bytes, dy, dvpos);
14704 /* Give up if cursor was not found. */
14705 if (w->cursor.vpos < 0)
14707 clear_glyph_matrix (w->desired_matrix);
14708 return -1;
14712 /* Don't let the cursor end in the scroll margins. */
14714 int this_scroll_margin, cursor_height;
14716 this_scroll_margin = max (0, scroll_margin);
14717 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14718 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14719 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14721 if ((w->cursor.y < this_scroll_margin
14722 && CHARPOS (start) > BEGV)
14723 /* Old redisplay didn't take scroll margin into account at the bottom,
14724 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14725 || (w->cursor.y + (make_cursor_line_fully_visible_p
14726 ? cursor_height + this_scroll_margin
14727 : 1)) > it.last_visible_y)
14729 w->cursor.vpos = -1;
14730 clear_glyph_matrix (w->desired_matrix);
14731 return -1;
14735 /* Scroll the display. Do it before changing the current matrix so
14736 that xterm.c doesn't get confused about where the cursor glyph is
14737 found. */
14738 if (dy && run.height)
14740 update_begin (f);
14742 if (FRAME_WINDOW_P (f))
14744 rif->update_window_begin_hook (w);
14745 rif->clear_window_mouse_face (w);
14746 rif->scroll_run_hook (w, &run);
14747 rif->update_window_end_hook (w, 0, 0);
14749 else
14751 /* Terminal frame. In this case, dvpos gives the number of
14752 lines to scroll by; dvpos < 0 means scroll up. */
14753 int first_unchanged_at_end_vpos
14754 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14755 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14756 int end = (WINDOW_TOP_EDGE_LINE (w)
14757 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14758 + window_internal_height (w));
14760 /* Perform the operation on the screen. */
14761 if (dvpos > 0)
14763 /* Scroll last_unchanged_at_beg_row to the end of the
14764 window down dvpos lines. */
14765 set_terminal_window (end);
14767 /* On dumb terminals delete dvpos lines at the end
14768 before inserting dvpos empty lines. */
14769 if (!scroll_region_ok)
14770 ins_del_lines (end - dvpos, -dvpos);
14772 /* Insert dvpos empty lines in front of
14773 last_unchanged_at_beg_row. */
14774 ins_del_lines (from, dvpos);
14776 else if (dvpos < 0)
14778 /* Scroll up last_unchanged_at_beg_vpos to the end of
14779 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14780 set_terminal_window (end);
14782 /* Delete dvpos lines in front of
14783 last_unchanged_at_beg_vpos. ins_del_lines will set
14784 the cursor to the given vpos and emit |dvpos| delete
14785 line sequences. */
14786 ins_del_lines (from + dvpos, dvpos);
14788 /* On a dumb terminal insert dvpos empty lines at the
14789 end. */
14790 if (!scroll_region_ok)
14791 ins_del_lines (end + dvpos, -dvpos);
14794 set_terminal_window (0);
14797 update_end (f);
14800 /* Shift reused rows of the current matrix to the right position.
14801 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14802 text. */
14803 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14804 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14805 if (dvpos < 0)
14807 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14808 bottom_vpos, dvpos);
14809 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14810 bottom_vpos, 0);
14812 else if (dvpos > 0)
14814 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14815 bottom_vpos, dvpos);
14816 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14817 first_unchanged_at_end_vpos + dvpos, 0);
14820 /* For frame-based redisplay, make sure that current frame and window
14821 matrix are in sync with respect to glyph memory. */
14822 if (!FRAME_WINDOW_P (f))
14823 sync_frame_with_window_matrix_rows (w);
14825 /* Adjust buffer positions in reused rows. */
14826 if (delta)
14827 increment_matrix_positions (current_matrix,
14828 first_unchanged_at_end_vpos + dvpos,
14829 bottom_vpos, delta, delta_bytes);
14831 /* Adjust Y positions. */
14832 if (dy)
14833 shift_glyph_matrix (w, current_matrix,
14834 first_unchanged_at_end_vpos + dvpos,
14835 bottom_vpos, dy);
14837 if (first_unchanged_at_end_row)
14839 first_unchanged_at_end_row += dvpos;
14840 if (first_unchanged_at_end_row->y >= it.last_visible_y
14841 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14842 first_unchanged_at_end_row = NULL;
14845 /* If scrolling up, there may be some lines to display at the end of
14846 the window. */
14847 last_text_row_at_end = NULL;
14848 if (dy < 0)
14850 /* Scrolling up can leave for example a partially visible line
14851 at the end of the window to be redisplayed. */
14852 /* Set last_row to the glyph row in the current matrix where the
14853 window end line is found. It has been moved up or down in
14854 the matrix by dvpos. */
14855 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14856 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14858 /* If last_row is the window end line, it should display text. */
14859 xassert (last_row->displays_text_p);
14861 /* If window end line was partially visible before, begin
14862 displaying at that line. Otherwise begin displaying with the
14863 line following it. */
14864 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14866 init_to_row_start (&it, w, last_row);
14867 it.vpos = last_vpos;
14868 it.current_y = last_row->y;
14870 else
14872 init_to_row_end (&it, w, last_row);
14873 it.vpos = 1 + last_vpos;
14874 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14875 ++last_row;
14878 /* We may start in a continuation line. If so, we have to
14879 get the right continuation_lines_width and current_x. */
14880 it.continuation_lines_width = last_row->continuation_lines_width;
14881 it.hpos = it.current_x = 0;
14883 /* Display the rest of the lines at the window end. */
14884 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14885 while (it.current_y < it.last_visible_y
14886 && !fonts_changed_p)
14888 /* Is it always sure that the display agrees with lines in
14889 the current matrix? I don't think so, so we mark rows
14890 displayed invalid in the current matrix by setting their
14891 enabled_p flag to zero. */
14892 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14893 if (display_line (&it))
14894 last_text_row_at_end = it.glyph_row - 1;
14898 /* Update window_end_pos and window_end_vpos. */
14899 if (first_unchanged_at_end_row
14900 && !last_text_row_at_end)
14902 /* Window end line if one of the preserved rows from the current
14903 matrix. Set row to the last row displaying text in current
14904 matrix starting at first_unchanged_at_end_row, after
14905 scrolling. */
14906 xassert (first_unchanged_at_end_row->displays_text_p);
14907 row = find_last_row_displaying_text (w->current_matrix, &it,
14908 first_unchanged_at_end_row);
14909 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14911 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14912 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14913 w->window_end_vpos
14914 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14915 xassert (w->window_end_bytepos >= 0);
14916 IF_DEBUG (debug_method_add (w, "A"));
14918 else if (last_text_row_at_end)
14920 w->window_end_pos
14921 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14922 w->window_end_bytepos
14923 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14924 w->window_end_vpos
14925 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14926 xassert (w->window_end_bytepos >= 0);
14927 IF_DEBUG (debug_method_add (w, "B"));
14929 else if (last_text_row)
14931 /* We have displayed either to the end of the window or at the
14932 end of the window, i.e. the last row with text is to be found
14933 in the desired matrix. */
14934 w->window_end_pos
14935 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14936 w->window_end_bytepos
14937 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14938 w->window_end_vpos
14939 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14940 xassert (w->window_end_bytepos >= 0);
14942 else if (first_unchanged_at_end_row == NULL
14943 && last_text_row == NULL
14944 && last_text_row_at_end == NULL)
14946 /* Displayed to end of window, but no line containing text was
14947 displayed. Lines were deleted at the end of the window. */
14948 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14949 int vpos = XFASTINT (w->window_end_vpos);
14950 struct glyph_row *current_row = current_matrix->rows + vpos;
14951 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14953 for (row = NULL;
14954 row == NULL && vpos >= first_vpos;
14955 --vpos, --current_row, --desired_row)
14957 if (desired_row->enabled_p)
14959 if (desired_row->displays_text_p)
14960 row = desired_row;
14962 else if (current_row->displays_text_p)
14963 row = current_row;
14966 xassert (row != NULL);
14967 w->window_end_vpos = make_number (vpos + 1);
14968 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14969 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14970 xassert (w->window_end_bytepos >= 0);
14971 IF_DEBUG (debug_method_add (w, "C"));
14973 else
14974 abort ();
14976 #if 0 /* This leads to problems, for instance when the cursor is
14977 at ZV, and the cursor line displays no text. */
14978 /* Disable rows below what's displayed in the window. This makes
14979 debugging easier. */
14980 enable_glyph_matrix_rows (current_matrix,
14981 XFASTINT (w->window_end_vpos) + 1,
14982 bottom_vpos, 0);
14983 #endif
14985 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14986 debug_end_vpos = XFASTINT (w->window_end_vpos));
14988 /* Record that display has not been completed. */
14989 w->window_end_valid = Qnil;
14990 w->desired_matrix->no_scrolling_p = 1;
14991 return 3;
14993 #undef GIVE_UP
14998 /***********************************************************************
14999 More debugging support
15000 ***********************************************************************/
15002 #if GLYPH_DEBUG
15004 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15005 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15006 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15009 /* Dump the contents of glyph matrix MATRIX on stderr.
15011 GLYPHS 0 means don't show glyph contents.
15012 GLYPHS 1 means show glyphs in short form
15013 GLYPHS > 1 means show glyphs in long form. */
15015 void
15016 dump_glyph_matrix (matrix, glyphs)
15017 struct glyph_matrix *matrix;
15018 int glyphs;
15020 int i;
15021 for (i = 0; i < matrix->nrows; ++i)
15022 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15026 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15027 the glyph row and area where the glyph comes from. */
15029 void
15030 dump_glyph (row, glyph, area)
15031 struct glyph_row *row;
15032 struct glyph *glyph;
15033 int area;
15035 if (glyph->type == CHAR_GLYPH)
15037 fprintf (stderr,
15038 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15039 glyph - row->glyphs[TEXT_AREA],
15040 'C',
15041 glyph->charpos,
15042 (BUFFERP (glyph->object)
15043 ? 'B'
15044 : (STRINGP (glyph->object)
15045 ? 'S'
15046 : '-')),
15047 glyph->pixel_width,
15048 glyph->u.ch,
15049 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15050 ? glyph->u.ch
15051 : '.'),
15052 glyph->face_id,
15053 glyph->left_box_line_p,
15054 glyph->right_box_line_p);
15056 else if (glyph->type == STRETCH_GLYPH)
15058 fprintf (stderr,
15059 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15060 glyph - row->glyphs[TEXT_AREA],
15061 'S',
15062 glyph->charpos,
15063 (BUFFERP (glyph->object)
15064 ? 'B'
15065 : (STRINGP (glyph->object)
15066 ? 'S'
15067 : '-')),
15068 glyph->pixel_width,
15070 '.',
15071 glyph->face_id,
15072 glyph->left_box_line_p,
15073 glyph->right_box_line_p);
15075 else if (glyph->type == IMAGE_GLYPH)
15077 fprintf (stderr,
15078 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15079 glyph - row->glyphs[TEXT_AREA],
15080 'I',
15081 glyph->charpos,
15082 (BUFFERP (glyph->object)
15083 ? 'B'
15084 : (STRINGP (glyph->object)
15085 ? 'S'
15086 : '-')),
15087 glyph->pixel_width,
15088 glyph->u.img_id,
15089 '.',
15090 glyph->face_id,
15091 glyph->left_box_line_p,
15092 glyph->right_box_line_p);
15094 else if (glyph->type == COMPOSITE_GLYPH)
15096 fprintf (stderr,
15097 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15098 glyph - row->glyphs[TEXT_AREA],
15099 '+',
15100 glyph->charpos,
15101 (BUFFERP (glyph->object)
15102 ? 'B'
15103 : (STRINGP (glyph->object)
15104 ? 'S'
15105 : '-')),
15106 glyph->pixel_width,
15107 glyph->u.cmp_id,
15108 '.',
15109 glyph->face_id,
15110 glyph->left_box_line_p,
15111 glyph->right_box_line_p);
15116 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15117 GLYPHS 0 means don't show glyph contents.
15118 GLYPHS 1 means show glyphs in short form
15119 GLYPHS > 1 means show glyphs in long form. */
15121 void
15122 dump_glyph_row (row, vpos, glyphs)
15123 struct glyph_row *row;
15124 int vpos, glyphs;
15126 if (glyphs != 1)
15128 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15129 fprintf (stderr, "======================================================================\n");
15131 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15132 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15133 vpos,
15134 MATRIX_ROW_START_CHARPOS (row),
15135 MATRIX_ROW_END_CHARPOS (row),
15136 row->used[TEXT_AREA],
15137 row->contains_overlapping_glyphs_p,
15138 row->enabled_p,
15139 row->truncated_on_left_p,
15140 row->truncated_on_right_p,
15141 row->continued_p,
15142 MATRIX_ROW_CONTINUATION_LINE_P (row),
15143 row->displays_text_p,
15144 row->ends_at_zv_p,
15145 row->fill_line_p,
15146 row->ends_in_middle_of_char_p,
15147 row->starts_in_middle_of_char_p,
15148 row->mouse_face_p,
15149 row->x,
15150 row->y,
15151 row->pixel_width,
15152 row->height,
15153 row->visible_height,
15154 row->ascent,
15155 row->phys_ascent);
15156 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15157 row->end.overlay_string_index,
15158 row->continuation_lines_width);
15159 fprintf (stderr, "%9d %5d\n",
15160 CHARPOS (row->start.string_pos),
15161 CHARPOS (row->end.string_pos));
15162 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15163 row->end.dpvec_index);
15166 if (glyphs > 1)
15168 int area;
15170 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15172 struct glyph *glyph = row->glyphs[area];
15173 struct glyph *glyph_end = glyph + row->used[area];
15175 /* Glyph for a line end in text. */
15176 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15177 ++glyph_end;
15179 if (glyph < glyph_end)
15180 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15182 for (; glyph < glyph_end; ++glyph)
15183 dump_glyph (row, glyph, area);
15186 else if (glyphs == 1)
15188 int area;
15190 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15192 char *s = (char *) alloca (row->used[area] + 1);
15193 int i;
15195 for (i = 0; i < row->used[area]; ++i)
15197 struct glyph *glyph = row->glyphs[area] + i;
15198 if (glyph->type == CHAR_GLYPH
15199 && glyph->u.ch < 0x80
15200 && glyph->u.ch >= ' ')
15201 s[i] = glyph->u.ch;
15202 else
15203 s[i] = '.';
15206 s[i] = '\0';
15207 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15213 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15214 Sdump_glyph_matrix, 0, 1, "p",
15215 doc: /* Dump the current matrix of the selected window to stderr.
15216 Shows contents of glyph row structures. With non-nil
15217 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15218 glyphs in short form, otherwise show glyphs in long form. */)
15219 (glyphs)
15220 Lisp_Object glyphs;
15222 struct window *w = XWINDOW (selected_window);
15223 struct buffer *buffer = XBUFFER (w->buffer);
15225 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15226 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15227 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15228 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15229 fprintf (stderr, "=============================================\n");
15230 dump_glyph_matrix (w->current_matrix,
15231 NILP (glyphs) ? 0 : XINT (glyphs));
15232 return Qnil;
15236 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15237 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15240 struct frame *f = XFRAME (selected_frame);
15241 dump_glyph_matrix (f->current_matrix, 1);
15242 return Qnil;
15246 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15247 doc: /* Dump glyph row ROW to stderr.
15248 GLYPH 0 means don't dump glyphs.
15249 GLYPH 1 means dump glyphs in short form.
15250 GLYPH > 1 or omitted means dump glyphs in long form. */)
15251 (row, glyphs)
15252 Lisp_Object row, glyphs;
15254 struct glyph_matrix *matrix;
15255 int vpos;
15257 CHECK_NUMBER (row);
15258 matrix = XWINDOW (selected_window)->current_matrix;
15259 vpos = XINT (row);
15260 if (vpos >= 0 && vpos < matrix->nrows)
15261 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15262 vpos,
15263 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15264 return Qnil;
15268 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15269 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15270 GLYPH 0 means don't dump glyphs.
15271 GLYPH 1 means dump glyphs in short form.
15272 GLYPH > 1 or omitted means dump glyphs in long form. */)
15273 (row, glyphs)
15274 Lisp_Object row, glyphs;
15276 struct frame *sf = SELECTED_FRAME ();
15277 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15278 int vpos;
15280 CHECK_NUMBER (row);
15281 vpos = XINT (row);
15282 if (vpos >= 0 && vpos < m->nrows)
15283 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15284 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15285 return Qnil;
15289 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15290 doc: /* Toggle tracing of redisplay.
15291 With ARG, turn tracing on if and only if ARG is positive. */)
15292 (arg)
15293 Lisp_Object arg;
15295 if (NILP (arg))
15296 trace_redisplay_p = !trace_redisplay_p;
15297 else
15299 arg = Fprefix_numeric_value (arg);
15300 trace_redisplay_p = XINT (arg) > 0;
15303 return Qnil;
15307 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15308 doc: /* Like `format', but print result to stderr.
15309 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15310 (nargs, args)
15311 int nargs;
15312 Lisp_Object *args;
15314 Lisp_Object s = Fformat (nargs, args);
15315 fprintf (stderr, "%s", SDATA (s));
15316 return Qnil;
15319 #endif /* GLYPH_DEBUG */
15323 /***********************************************************************
15324 Building Desired Matrix Rows
15325 ***********************************************************************/
15327 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15328 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15330 static struct glyph_row *
15331 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15332 struct window *w;
15333 Lisp_Object overlay_arrow_string;
15335 struct frame *f = XFRAME (WINDOW_FRAME (w));
15336 struct buffer *buffer = XBUFFER (w->buffer);
15337 struct buffer *old = current_buffer;
15338 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15339 int arrow_len = SCHARS (overlay_arrow_string);
15340 const unsigned char *arrow_end = arrow_string + arrow_len;
15341 const unsigned char *p;
15342 struct it it;
15343 int multibyte_p;
15344 int n_glyphs_before;
15346 set_buffer_temp (buffer);
15347 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15348 it.glyph_row->used[TEXT_AREA] = 0;
15349 SET_TEXT_POS (it.position, 0, 0);
15351 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15352 p = arrow_string;
15353 while (p < arrow_end)
15355 Lisp_Object face, ilisp;
15357 /* Get the next character. */
15358 if (multibyte_p)
15359 it.c = string_char_and_length (p, arrow_len, &it.len);
15360 else
15361 it.c = *p, it.len = 1;
15362 p += it.len;
15364 /* Get its face. */
15365 ilisp = make_number (p - arrow_string);
15366 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15367 it.face_id = compute_char_face (f, it.c, face);
15369 /* Compute its width, get its glyphs. */
15370 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15371 SET_TEXT_POS (it.position, -1, -1);
15372 PRODUCE_GLYPHS (&it);
15374 /* If this character doesn't fit any more in the line, we have
15375 to remove some glyphs. */
15376 if (it.current_x > it.last_visible_x)
15378 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15379 break;
15383 set_buffer_temp (old);
15384 return it.glyph_row;
15388 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15389 glyphs are only inserted for terminal frames since we can't really
15390 win with truncation glyphs when partially visible glyphs are
15391 involved. Which glyphs to insert is determined by
15392 produce_special_glyphs. */
15394 static void
15395 insert_left_trunc_glyphs (it)
15396 struct it *it;
15398 struct it truncate_it;
15399 struct glyph *from, *end, *to, *toend;
15401 xassert (!FRAME_WINDOW_P (it->f));
15403 /* Get the truncation glyphs. */
15404 truncate_it = *it;
15405 truncate_it.current_x = 0;
15406 truncate_it.face_id = DEFAULT_FACE_ID;
15407 truncate_it.glyph_row = &scratch_glyph_row;
15408 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15409 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15410 truncate_it.object = make_number (0);
15411 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15413 /* Overwrite glyphs from IT with truncation glyphs. */
15414 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15415 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15416 to = it->glyph_row->glyphs[TEXT_AREA];
15417 toend = to + it->glyph_row->used[TEXT_AREA];
15419 while (from < end)
15420 *to++ = *from++;
15422 /* There may be padding glyphs left over. Overwrite them too. */
15423 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15425 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15426 while (from < end)
15427 *to++ = *from++;
15430 if (to > toend)
15431 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15435 /* Compute the pixel height and width of IT->glyph_row.
15437 Most of the time, ascent and height of a display line will be equal
15438 to the max_ascent and max_height values of the display iterator
15439 structure. This is not the case if
15441 1. We hit ZV without displaying anything. In this case, max_ascent
15442 and max_height will be zero.
15444 2. We have some glyphs that don't contribute to the line height.
15445 (The glyph row flag contributes_to_line_height_p is for future
15446 pixmap extensions).
15448 The first case is easily covered by using default values because in
15449 these cases, the line height does not really matter, except that it
15450 must not be zero. */
15452 static void
15453 compute_line_metrics (it)
15454 struct it *it;
15456 struct glyph_row *row = it->glyph_row;
15457 int area, i;
15459 if (FRAME_WINDOW_P (it->f))
15461 int i, min_y, max_y;
15463 /* The line may consist of one space only, that was added to
15464 place the cursor on it. If so, the row's height hasn't been
15465 computed yet. */
15466 if (row->height == 0)
15468 if (it->max_ascent + it->max_descent == 0)
15469 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15470 row->ascent = it->max_ascent;
15471 row->height = it->max_ascent + it->max_descent;
15472 row->phys_ascent = it->max_phys_ascent;
15473 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15474 row->extra_line_spacing = it->max_extra_line_spacing;
15477 /* Compute the width of this line. */
15478 row->pixel_width = row->x;
15479 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15480 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15482 xassert (row->pixel_width >= 0);
15483 xassert (row->ascent >= 0 && row->height > 0);
15485 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15486 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15488 /* If first line's physical ascent is larger than its logical
15489 ascent, use the physical ascent, and make the row taller.
15490 This makes accented characters fully visible. */
15491 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15492 && row->phys_ascent > row->ascent)
15494 row->height += row->phys_ascent - row->ascent;
15495 row->ascent = row->phys_ascent;
15498 /* Compute how much of the line is visible. */
15499 row->visible_height = row->height;
15501 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15502 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15504 if (row->y < min_y)
15505 row->visible_height -= min_y - row->y;
15506 if (row->y + row->height > max_y)
15507 row->visible_height -= row->y + row->height - max_y;
15509 else
15511 row->pixel_width = row->used[TEXT_AREA];
15512 if (row->continued_p)
15513 row->pixel_width -= it->continuation_pixel_width;
15514 else if (row->truncated_on_right_p)
15515 row->pixel_width -= it->truncation_pixel_width;
15516 row->ascent = row->phys_ascent = 0;
15517 row->height = row->phys_height = row->visible_height = 1;
15518 row->extra_line_spacing = 0;
15521 /* Compute a hash code for this row. */
15522 row->hash = 0;
15523 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15524 for (i = 0; i < row->used[area]; ++i)
15525 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15526 + row->glyphs[area][i].u.val
15527 + row->glyphs[area][i].face_id
15528 + row->glyphs[area][i].padding_p
15529 + (row->glyphs[area][i].type << 2));
15531 it->max_ascent = it->max_descent = 0;
15532 it->max_phys_ascent = it->max_phys_descent = 0;
15536 /* Append one space to the glyph row of iterator IT if doing a
15537 window-based redisplay. The space has the same face as
15538 IT->face_id. Value is non-zero if a space was added.
15540 This function is called to make sure that there is always one glyph
15541 at the end of a glyph row that the cursor can be set on under
15542 window-systems. (If there weren't such a glyph we would not know
15543 how wide and tall a box cursor should be displayed).
15545 At the same time this space let's a nicely handle clearing to the
15546 end of the line if the row ends in italic text. */
15548 static int
15549 append_space_for_newline (it, default_face_p)
15550 struct it *it;
15551 int default_face_p;
15553 if (FRAME_WINDOW_P (it->f))
15555 int n = it->glyph_row->used[TEXT_AREA];
15557 if (it->glyph_row->glyphs[TEXT_AREA] + n
15558 < it->glyph_row->glyphs[1 + TEXT_AREA])
15560 /* Save some values that must not be changed.
15561 Must save IT->c and IT->len because otherwise
15562 ITERATOR_AT_END_P wouldn't work anymore after
15563 append_space_for_newline has been called. */
15564 enum display_element_type saved_what = it->what;
15565 int saved_c = it->c, saved_len = it->len;
15566 int saved_x = it->current_x;
15567 int saved_face_id = it->face_id;
15568 struct text_pos saved_pos;
15569 Lisp_Object saved_object;
15570 struct face *face;
15572 saved_object = it->object;
15573 saved_pos = it->position;
15575 it->what = IT_CHARACTER;
15576 bzero (&it->position, sizeof it->position);
15577 it->object = make_number (0);
15578 it->c = ' ';
15579 it->len = 1;
15581 if (default_face_p)
15582 it->face_id = DEFAULT_FACE_ID;
15583 else if (it->face_before_selective_p)
15584 it->face_id = it->saved_face_id;
15585 face = FACE_FROM_ID (it->f, it->face_id);
15586 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15588 PRODUCE_GLYPHS (it);
15590 it->override_ascent = -1;
15591 it->constrain_row_ascent_descent_p = 0;
15592 it->current_x = saved_x;
15593 it->object = saved_object;
15594 it->position = saved_pos;
15595 it->what = saved_what;
15596 it->face_id = saved_face_id;
15597 it->len = saved_len;
15598 it->c = saved_c;
15599 return 1;
15603 return 0;
15607 /* Extend the face of the last glyph in the text area of IT->glyph_row
15608 to the end of the display line. Called from display_line.
15609 If the glyph row is empty, add a space glyph to it so that we
15610 know the face to draw. Set the glyph row flag fill_line_p. */
15612 static void
15613 extend_face_to_end_of_line (it)
15614 struct it *it;
15616 struct face *face;
15617 struct frame *f = it->f;
15619 /* If line is already filled, do nothing. */
15620 if (it->current_x >= it->last_visible_x)
15621 return;
15623 /* Face extension extends the background and box of IT->face_id
15624 to the end of the line. If the background equals the background
15625 of the frame, we don't have to do anything. */
15626 if (it->face_before_selective_p)
15627 face = FACE_FROM_ID (it->f, it->saved_face_id);
15628 else
15629 face = FACE_FROM_ID (f, it->face_id);
15631 if (FRAME_WINDOW_P (f)
15632 && it->glyph_row->displays_text_p
15633 && face->box == FACE_NO_BOX
15634 && face->background == FRAME_BACKGROUND_PIXEL (f)
15635 && !face->stipple)
15636 return;
15638 /* Set the glyph row flag indicating that the face of the last glyph
15639 in the text area has to be drawn to the end of the text area. */
15640 it->glyph_row->fill_line_p = 1;
15642 /* If current character of IT is not ASCII, make sure we have the
15643 ASCII face. This will be automatically undone the next time
15644 get_next_display_element returns a multibyte character. Note
15645 that the character will always be single byte in unibyte text. */
15646 if (!SINGLE_BYTE_CHAR_P (it->c))
15648 it->face_id = FACE_FOR_CHAR (f, face, 0);
15651 if (FRAME_WINDOW_P (f))
15653 /* If the row is empty, add a space with the current face of IT,
15654 so that we know which face to draw. */
15655 if (it->glyph_row->used[TEXT_AREA] == 0)
15657 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15658 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15659 it->glyph_row->used[TEXT_AREA] = 1;
15662 else
15664 /* Save some values that must not be changed. */
15665 int saved_x = it->current_x;
15666 struct text_pos saved_pos;
15667 Lisp_Object saved_object;
15668 enum display_element_type saved_what = it->what;
15669 int saved_face_id = it->face_id;
15671 saved_object = it->object;
15672 saved_pos = it->position;
15674 it->what = IT_CHARACTER;
15675 bzero (&it->position, sizeof it->position);
15676 it->object = make_number (0);
15677 it->c = ' ';
15678 it->len = 1;
15679 it->face_id = face->id;
15681 PRODUCE_GLYPHS (it);
15683 while (it->current_x <= it->last_visible_x)
15684 PRODUCE_GLYPHS (it);
15686 /* Don't count these blanks really. It would let us insert a left
15687 truncation glyph below and make us set the cursor on them, maybe. */
15688 it->current_x = saved_x;
15689 it->object = saved_object;
15690 it->position = saved_pos;
15691 it->what = saved_what;
15692 it->face_id = saved_face_id;
15697 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15698 trailing whitespace. */
15700 static int
15701 trailing_whitespace_p (charpos)
15702 int charpos;
15704 int bytepos = CHAR_TO_BYTE (charpos);
15705 int c = 0;
15707 while (bytepos < ZV_BYTE
15708 && (c = FETCH_CHAR (bytepos),
15709 c == ' ' || c == '\t'))
15710 ++bytepos;
15712 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15714 if (bytepos != PT_BYTE)
15715 return 1;
15717 return 0;
15721 /* Highlight trailing whitespace, if any, in ROW. */
15723 void
15724 highlight_trailing_whitespace (f, row)
15725 struct frame *f;
15726 struct glyph_row *row;
15728 int used = row->used[TEXT_AREA];
15730 if (used)
15732 struct glyph *start = row->glyphs[TEXT_AREA];
15733 struct glyph *glyph = start + used - 1;
15735 /* Skip over glyphs inserted to display the cursor at the
15736 end of a line, for extending the face of the last glyph
15737 to the end of the line on terminals, and for truncation
15738 and continuation glyphs. */
15739 while (glyph >= start
15740 && glyph->type == CHAR_GLYPH
15741 && INTEGERP (glyph->object))
15742 --glyph;
15744 /* If last glyph is a space or stretch, and it's trailing
15745 whitespace, set the face of all trailing whitespace glyphs in
15746 IT->glyph_row to `trailing-whitespace'. */
15747 if (glyph >= start
15748 && BUFFERP (glyph->object)
15749 && (glyph->type == STRETCH_GLYPH
15750 || (glyph->type == CHAR_GLYPH
15751 && glyph->u.ch == ' '))
15752 && trailing_whitespace_p (glyph->charpos))
15754 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15755 if (face_id < 0)
15756 return;
15758 while (glyph >= start
15759 && BUFFERP (glyph->object)
15760 && (glyph->type == STRETCH_GLYPH
15761 || (glyph->type == CHAR_GLYPH
15762 && glyph->u.ch == ' ')))
15763 (glyph--)->face_id = face_id;
15769 /* Value is non-zero if glyph row ROW in window W should be
15770 used to hold the cursor. */
15772 static int
15773 cursor_row_p (w, row)
15774 struct window *w;
15775 struct glyph_row *row;
15777 int cursor_row_p = 1;
15779 if (PT == MATRIX_ROW_END_CHARPOS (row))
15781 /* If the row ends with a newline from a string, we don't want
15782 the cursor there, but we still want it at the start of the
15783 string if the string starts in this row.
15784 If the row is continued it doesn't end in a newline. */
15785 if (CHARPOS (row->end.string_pos) >= 0)
15786 cursor_row_p = (row->continued_p
15787 || PT >= MATRIX_ROW_START_CHARPOS (row));
15788 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15790 /* If the row ends in middle of a real character,
15791 and the line is continued, we want the cursor here.
15792 That's because MATRIX_ROW_END_CHARPOS would equal
15793 PT if PT is before the character. */
15794 if (!row->ends_in_ellipsis_p)
15795 cursor_row_p = row->continued_p;
15796 else
15797 /* If the row ends in an ellipsis, then
15798 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15799 We want that position to be displayed after the ellipsis. */
15800 cursor_row_p = 0;
15802 /* If the row ends at ZV, display the cursor at the end of that
15803 row instead of at the start of the row below. */
15804 else if (row->ends_at_zv_p)
15805 cursor_row_p = 1;
15806 else
15807 cursor_row_p = 0;
15810 return cursor_row_p;
15814 /* Construct the glyph row IT->glyph_row in the desired matrix of
15815 IT->w from text at the current position of IT. See dispextern.h
15816 for an overview of struct it. Value is non-zero if
15817 IT->glyph_row displays text, as opposed to a line displaying ZV
15818 only. */
15820 static int
15821 display_line (it)
15822 struct it *it;
15824 struct glyph_row *row = it->glyph_row;
15825 Lisp_Object overlay_arrow_string;
15827 /* We always start displaying at hpos zero even if hscrolled. */
15828 xassert (it->hpos == 0 && it->current_x == 0);
15830 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15831 >= it->w->desired_matrix->nrows)
15833 it->w->nrows_scale_factor++;
15834 fonts_changed_p = 1;
15835 return 0;
15838 /* Is IT->w showing the region? */
15839 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15841 /* Clear the result glyph row and enable it. */
15842 prepare_desired_row (row);
15844 row->y = it->current_y;
15845 row->start = it->start;
15846 row->continuation_lines_width = it->continuation_lines_width;
15847 row->displays_text_p = 1;
15848 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15849 it->starts_in_middle_of_char_p = 0;
15851 /* Arrange the overlays nicely for our purposes. Usually, we call
15852 display_line on only one line at a time, in which case this
15853 can't really hurt too much, or we call it on lines which appear
15854 one after another in the buffer, in which case all calls to
15855 recenter_overlay_lists but the first will be pretty cheap. */
15856 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15858 /* Move over display elements that are not visible because we are
15859 hscrolled. This may stop at an x-position < IT->first_visible_x
15860 if the first glyph is partially visible or if we hit a line end. */
15861 if (it->current_x < it->first_visible_x)
15863 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15864 MOVE_TO_POS | MOVE_TO_X);
15867 /* Get the initial row height. This is either the height of the
15868 text hscrolled, if there is any, or zero. */
15869 row->ascent = it->max_ascent;
15870 row->height = it->max_ascent + it->max_descent;
15871 row->phys_ascent = it->max_phys_ascent;
15872 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15873 row->extra_line_spacing = it->max_extra_line_spacing;
15875 /* Loop generating characters. The loop is left with IT on the next
15876 character to display. */
15877 while (1)
15879 int n_glyphs_before, hpos_before, x_before;
15880 int x, i, nglyphs;
15881 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15883 /* Retrieve the next thing to display. Value is zero if end of
15884 buffer reached. */
15885 if (!get_next_display_element (it))
15887 /* Maybe add a space at the end of this line that is used to
15888 display the cursor there under X. Set the charpos of the
15889 first glyph of blank lines not corresponding to any text
15890 to -1. */
15891 #ifdef HAVE_WINDOW_SYSTEM
15892 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15893 row->exact_window_width_line_p = 1;
15894 else
15895 #endif /* HAVE_WINDOW_SYSTEM */
15896 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15897 || row->used[TEXT_AREA] == 0)
15899 row->glyphs[TEXT_AREA]->charpos = -1;
15900 row->displays_text_p = 0;
15902 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15903 && (!MINI_WINDOW_P (it->w)
15904 || (minibuf_level && EQ (it->window, minibuf_window))))
15905 row->indicate_empty_line_p = 1;
15908 it->continuation_lines_width = 0;
15909 row->ends_at_zv_p = 1;
15910 break;
15913 /* Now, get the metrics of what we want to display. This also
15914 generates glyphs in `row' (which is IT->glyph_row). */
15915 n_glyphs_before = row->used[TEXT_AREA];
15916 x = it->current_x;
15918 /* Remember the line height so far in case the next element doesn't
15919 fit on the line. */
15920 if (!it->truncate_lines_p)
15922 ascent = it->max_ascent;
15923 descent = it->max_descent;
15924 phys_ascent = it->max_phys_ascent;
15925 phys_descent = it->max_phys_descent;
15928 PRODUCE_GLYPHS (it);
15930 /* If this display element was in marginal areas, continue with
15931 the next one. */
15932 if (it->area != TEXT_AREA)
15934 row->ascent = max (row->ascent, it->max_ascent);
15935 row->height = max (row->height, it->max_ascent + it->max_descent);
15936 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15937 row->phys_height = max (row->phys_height,
15938 it->max_phys_ascent + it->max_phys_descent);
15939 row->extra_line_spacing = max (row->extra_line_spacing,
15940 it->max_extra_line_spacing);
15941 set_iterator_to_next (it, 1);
15942 continue;
15945 /* Does the display element fit on the line? If we truncate
15946 lines, we should draw past the right edge of the window. If
15947 we don't truncate, we want to stop so that we can display the
15948 continuation glyph before the right margin. If lines are
15949 continued, there are two possible strategies for characters
15950 resulting in more than 1 glyph (e.g. tabs): Display as many
15951 glyphs as possible in this line and leave the rest for the
15952 continuation line, or display the whole element in the next
15953 line. Original redisplay did the former, so we do it also. */
15954 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15955 hpos_before = it->hpos;
15956 x_before = x;
15958 if (/* Not a newline. */
15959 nglyphs > 0
15960 /* Glyphs produced fit entirely in the line. */
15961 && it->current_x < it->last_visible_x)
15963 it->hpos += nglyphs;
15964 row->ascent = max (row->ascent, it->max_ascent);
15965 row->height = max (row->height, it->max_ascent + it->max_descent);
15966 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15967 row->phys_height = max (row->phys_height,
15968 it->max_phys_ascent + it->max_phys_descent);
15969 row->extra_line_spacing = max (row->extra_line_spacing,
15970 it->max_extra_line_spacing);
15971 if (it->current_x - it->pixel_width < it->first_visible_x)
15972 row->x = x - it->first_visible_x;
15974 else
15976 int new_x;
15977 struct glyph *glyph;
15979 for (i = 0; i < nglyphs; ++i, x = new_x)
15981 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15982 new_x = x + glyph->pixel_width;
15984 if (/* Lines are continued. */
15985 !it->truncate_lines_p
15986 && (/* Glyph doesn't fit on the line. */
15987 new_x > it->last_visible_x
15988 /* Or it fits exactly on a window system frame. */
15989 || (new_x == it->last_visible_x
15990 && FRAME_WINDOW_P (it->f))))
15992 /* End of a continued line. */
15994 if (it->hpos == 0
15995 || (new_x == it->last_visible_x
15996 && FRAME_WINDOW_P (it->f)))
15998 /* Current glyph is the only one on the line or
15999 fits exactly on the line. We must continue
16000 the line because we can't draw the cursor
16001 after the glyph. */
16002 row->continued_p = 1;
16003 it->current_x = new_x;
16004 it->continuation_lines_width += new_x;
16005 ++it->hpos;
16006 if (i == nglyphs - 1)
16008 set_iterator_to_next (it, 1);
16009 #ifdef HAVE_WINDOW_SYSTEM
16010 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16012 if (!get_next_display_element (it))
16014 row->exact_window_width_line_p = 1;
16015 it->continuation_lines_width = 0;
16016 row->continued_p = 0;
16017 row->ends_at_zv_p = 1;
16019 else if (ITERATOR_AT_END_OF_LINE_P (it))
16021 row->continued_p = 0;
16022 row->exact_window_width_line_p = 1;
16025 #endif /* HAVE_WINDOW_SYSTEM */
16028 else if (CHAR_GLYPH_PADDING_P (*glyph)
16029 && !FRAME_WINDOW_P (it->f))
16031 /* A padding glyph that doesn't fit on this line.
16032 This means the whole character doesn't fit
16033 on the line. */
16034 row->used[TEXT_AREA] = n_glyphs_before;
16036 /* Fill the rest of the row with continuation
16037 glyphs like in 20.x. */
16038 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16039 < row->glyphs[1 + TEXT_AREA])
16040 produce_special_glyphs (it, IT_CONTINUATION);
16042 row->continued_p = 1;
16043 it->current_x = x_before;
16044 it->continuation_lines_width += x_before;
16046 /* Restore the height to what it was before the
16047 element not fitting on the line. */
16048 it->max_ascent = ascent;
16049 it->max_descent = descent;
16050 it->max_phys_ascent = phys_ascent;
16051 it->max_phys_descent = phys_descent;
16053 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16055 /* A TAB that extends past the right edge of the
16056 window. This produces a single glyph on
16057 window system frames. We leave the glyph in
16058 this row and let it fill the row, but don't
16059 consume the TAB. */
16060 it->continuation_lines_width += it->last_visible_x;
16061 row->ends_in_middle_of_char_p = 1;
16062 row->continued_p = 1;
16063 glyph->pixel_width = it->last_visible_x - x;
16064 it->starts_in_middle_of_char_p = 1;
16066 else
16068 /* Something other than a TAB that draws past
16069 the right edge of the window. Restore
16070 positions to values before the element. */
16071 row->used[TEXT_AREA] = n_glyphs_before + i;
16073 /* Display continuation glyphs. */
16074 if (!FRAME_WINDOW_P (it->f))
16075 produce_special_glyphs (it, IT_CONTINUATION);
16076 row->continued_p = 1;
16078 it->current_x = x_before;
16079 it->continuation_lines_width += x;
16080 extend_face_to_end_of_line (it);
16082 if (nglyphs > 1 && i > 0)
16084 row->ends_in_middle_of_char_p = 1;
16085 it->starts_in_middle_of_char_p = 1;
16088 /* Restore the height to what it was before the
16089 element not fitting on the line. */
16090 it->max_ascent = ascent;
16091 it->max_descent = descent;
16092 it->max_phys_ascent = phys_ascent;
16093 it->max_phys_descent = phys_descent;
16096 break;
16098 else if (new_x > it->first_visible_x)
16100 /* Increment number of glyphs actually displayed. */
16101 ++it->hpos;
16103 if (x < it->first_visible_x)
16104 /* Glyph is partially visible, i.e. row starts at
16105 negative X position. */
16106 row->x = x - it->first_visible_x;
16108 else
16110 /* Glyph is completely off the left margin of the
16111 window. This should not happen because of the
16112 move_it_in_display_line at the start of this
16113 function, unless the text display area of the
16114 window is empty. */
16115 xassert (it->first_visible_x <= it->last_visible_x);
16119 row->ascent = max (row->ascent, it->max_ascent);
16120 row->height = max (row->height, it->max_ascent + it->max_descent);
16121 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16122 row->phys_height = max (row->phys_height,
16123 it->max_phys_ascent + it->max_phys_descent);
16124 row->extra_line_spacing = max (row->extra_line_spacing,
16125 it->max_extra_line_spacing);
16127 /* End of this display line if row is continued. */
16128 if (row->continued_p || row->ends_at_zv_p)
16129 break;
16132 at_end_of_line:
16133 /* Is this a line end? If yes, we're also done, after making
16134 sure that a non-default face is extended up to the right
16135 margin of the window. */
16136 if (ITERATOR_AT_END_OF_LINE_P (it))
16138 int used_before = row->used[TEXT_AREA];
16140 row->ends_in_newline_from_string_p = STRINGP (it->object);
16142 #ifdef HAVE_WINDOW_SYSTEM
16143 /* Add a space at the end of the line that is used to
16144 display the cursor there. */
16145 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16146 append_space_for_newline (it, 0);
16147 #endif /* HAVE_WINDOW_SYSTEM */
16149 /* Extend the face to the end of the line. */
16150 extend_face_to_end_of_line (it);
16152 /* Make sure we have the position. */
16153 if (used_before == 0)
16154 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16156 /* Consume the line end. This skips over invisible lines. */
16157 set_iterator_to_next (it, 1);
16158 it->continuation_lines_width = 0;
16159 break;
16162 /* Proceed with next display element. Note that this skips
16163 over lines invisible because of selective display. */
16164 set_iterator_to_next (it, 1);
16166 /* If we truncate lines, we are done when the last displayed
16167 glyphs reach past the right margin of the window. */
16168 if (it->truncate_lines_p
16169 && (FRAME_WINDOW_P (it->f)
16170 ? (it->current_x >= it->last_visible_x)
16171 : (it->current_x > it->last_visible_x)))
16173 /* Maybe add truncation glyphs. */
16174 if (!FRAME_WINDOW_P (it->f))
16176 int i, n;
16178 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16179 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16180 break;
16182 for (n = row->used[TEXT_AREA]; i < n; ++i)
16184 row->used[TEXT_AREA] = i;
16185 produce_special_glyphs (it, IT_TRUNCATION);
16188 #ifdef HAVE_WINDOW_SYSTEM
16189 else
16191 /* Don't truncate if we can overflow newline into fringe. */
16192 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16194 if (!get_next_display_element (it))
16196 it->continuation_lines_width = 0;
16197 row->ends_at_zv_p = 1;
16198 row->exact_window_width_line_p = 1;
16199 break;
16201 if (ITERATOR_AT_END_OF_LINE_P (it))
16203 row->exact_window_width_line_p = 1;
16204 goto at_end_of_line;
16208 #endif /* HAVE_WINDOW_SYSTEM */
16210 row->truncated_on_right_p = 1;
16211 it->continuation_lines_width = 0;
16212 reseat_at_next_visible_line_start (it, 0);
16213 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16214 it->hpos = hpos_before;
16215 it->current_x = x_before;
16216 break;
16220 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16221 at the left window margin. */
16222 if (it->first_visible_x
16223 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16225 if (!FRAME_WINDOW_P (it->f))
16226 insert_left_trunc_glyphs (it);
16227 row->truncated_on_left_p = 1;
16230 /* If the start of this line is the overlay arrow-position, then
16231 mark this glyph row as the one containing the overlay arrow.
16232 This is clearly a mess with variable size fonts. It would be
16233 better to let it be displayed like cursors under X. */
16234 if ((row->displays_text_p || !overlay_arrow_seen)
16235 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16236 !NILP (overlay_arrow_string)))
16238 /* Overlay arrow in window redisplay is a fringe bitmap. */
16239 if (STRINGP (overlay_arrow_string))
16241 struct glyph_row *arrow_row
16242 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16243 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16244 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16245 struct glyph *p = row->glyphs[TEXT_AREA];
16246 struct glyph *p2, *end;
16248 /* Copy the arrow glyphs. */
16249 while (glyph < arrow_end)
16250 *p++ = *glyph++;
16252 /* Throw away padding glyphs. */
16253 p2 = p;
16254 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16255 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16256 ++p2;
16257 if (p2 > p)
16259 while (p2 < end)
16260 *p++ = *p2++;
16261 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16264 else
16266 xassert (INTEGERP (overlay_arrow_string));
16267 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16269 overlay_arrow_seen = 1;
16272 /* Compute pixel dimensions of this line. */
16273 compute_line_metrics (it);
16275 /* Remember the position at which this line ends. */
16276 row->end = it->current;
16278 /* Record whether this row ends inside an ellipsis. */
16279 row->ends_in_ellipsis_p
16280 = (it->method == GET_FROM_DISPLAY_VECTOR
16281 && it->ellipsis_p);
16283 /* Save fringe bitmaps in this row. */
16284 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16285 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16286 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16287 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16289 it->left_user_fringe_bitmap = 0;
16290 it->left_user_fringe_face_id = 0;
16291 it->right_user_fringe_bitmap = 0;
16292 it->right_user_fringe_face_id = 0;
16294 /* Maybe set the cursor. */
16295 if (it->w->cursor.vpos < 0
16296 && PT >= MATRIX_ROW_START_CHARPOS (row)
16297 && PT <= MATRIX_ROW_END_CHARPOS (row)
16298 && cursor_row_p (it->w, row))
16299 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16301 /* Highlight trailing whitespace. */
16302 if (!NILP (Vshow_trailing_whitespace))
16303 highlight_trailing_whitespace (it->f, it->glyph_row);
16305 /* Prepare for the next line. This line starts horizontally at (X
16306 HPOS) = (0 0). Vertical positions are incremented. As a
16307 convenience for the caller, IT->glyph_row is set to the next
16308 row to be used. */
16309 it->current_x = it->hpos = 0;
16310 it->current_y += row->height;
16311 ++it->vpos;
16312 ++it->glyph_row;
16313 it->start = it->current;
16314 return row->displays_text_p;
16319 /***********************************************************************
16320 Menu Bar
16321 ***********************************************************************/
16323 /* Redisplay the menu bar in the frame for window W.
16325 The menu bar of X frames that don't have X toolkit support is
16326 displayed in a special window W->frame->menu_bar_window.
16328 The menu bar of terminal frames is treated specially as far as
16329 glyph matrices are concerned. Menu bar lines are not part of
16330 windows, so the update is done directly on the frame matrix rows
16331 for the menu bar. */
16333 static void
16334 display_menu_bar (w)
16335 struct window *w;
16337 struct frame *f = XFRAME (WINDOW_FRAME (w));
16338 struct it it;
16339 Lisp_Object items;
16340 int i;
16342 /* Don't do all this for graphical frames. */
16343 #ifdef HAVE_NTGUI
16344 if (!NILP (Vwindow_system))
16345 return;
16346 #endif
16347 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16348 if (FRAME_X_P (f))
16349 return;
16350 #endif
16351 #ifdef MAC_OS
16352 if (FRAME_MAC_P (f))
16353 return;
16354 #endif
16356 #ifdef USE_X_TOOLKIT
16357 xassert (!FRAME_WINDOW_P (f));
16358 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16359 it.first_visible_x = 0;
16360 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16361 #else /* not USE_X_TOOLKIT */
16362 if (FRAME_WINDOW_P (f))
16364 /* Menu bar lines are displayed in the desired matrix of the
16365 dummy window menu_bar_window. */
16366 struct window *menu_w;
16367 xassert (WINDOWP (f->menu_bar_window));
16368 menu_w = XWINDOW (f->menu_bar_window);
16369 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16370 MENU_FACE_ID);
16371 it.first_visible_x = 0;
16372 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16374 else
16376 /* This is a TTY frame, i.e. character hpos/vpos are used as
16377 pixel x/y. */
16378 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16379 MENU_FACE_ID);
16380 it.first_visible_x = 0;
16381 it.last_visible_x = FRAME_COLS (f);
16383 #endif /* not USE_X_TOOLKIT */
16385 if (! mode_line_inverse_video)
16386 /* Force the menu-bar to be displayed in the default face. */
16387 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16389 /* Clear all rows of the menu bar. */
16390 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16392 struct glyph_row *row = it.glyph_row + i;
16393 clear_glyph_row (row);
16394 row->enabled_p = 1;
16395 row->full_width_p = 1;
16398 /* Display all items of the menu bar. */
16399 items = FRAME_MENU_BAR_ITEMS (it.f);
16400 for (i = 0; i < XVECTOR (items)->size; i += 4)
16402 Lisp_Object string;
16404 /* Stop at nil string. */
16405 string = AREF (items, i + 1);
16406 if (NILP (string))
16407 break;
16409 /* Remember where item was displayed. */
16410 AREF (items, i + 3) = make_number (it.hpos);
16412 /* Display the item, pad with one space. */
16413 if (it.current_x < it.last_visible_x)
16414 display_string (NULL, string, Qnil, 0, 0, &it,
16415 SCHARS (string) + 1, 0, 0, -1);
16418 /* Fill out the line with spaces. */
16419 if (it.current_x < it.last_visible_x)
16420 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16422 /* Compute the total height of the lines. */
16423 compute_line_metrics (&it);
16428 /***********************************************************************
16429 Mode Line
16430 ***********************************************************************/
16432 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16433 FORCE is non-zero, redisplay mode lines unconditionally.
16434 Otherwise, redisplay only mode lines that are garbaged. Value is
16435 the number of windows whose mode lines were redisplayed. */
16437 static int
16438 redisplay_mode_lines (window, force)
16439 Lisp_Object window;
16440 int force;
16442 int nwindows = 0;
16444 while (!NILP (window))
16446 struct window *w = XWINDOW (window);
16448 if (WINDOWP (w->hchild))
16449 nwindows += redisplay_mode_lines (w->hchild, force);
16450 else if (WINDOWP (w->vchild))
16451 nwindows += redisplay_mode_lines (w->vchild, force);
16452 else if (force
16453 || FRAME_GARBAGED_P (XFRAME (w->frame))
16454 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16456 struct text_pos lpoint;
16457 struct buffer *old = current_buffer;
16459 /* Set the window's buffer for the mode line display. */
16460 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16461 set_buffer_internal_1 (XBUFFER (w->buffer));
16463 /* Point refers normally to the selected window. For any
16464 other window, set up appropriate value. */
16465 if (!EQ (window, selected_window))
16467 struct text_pos pt;
16469 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16470 if (CHARPOS (pt) < BEGV)
16471 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16472 else if (CHARPOS (pt) > (ZV - 1))
16473 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16474 else
16475 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16478 /* Display mode lines. */
16479 clear_glyph_matrix (w->desired_matrix);
16480 if (display_mode_lines (w))
16482 ++nwindows;
16483 w->must_be_updated_p = 1;
16486 /* Restore old settings. */
16487 set_buffer_internal_1 (old);
16488 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16491 window = w->next;
16494 return nwindows;
16498 /* Display the mode and/or top line of window W. Value is the number
16499 of mode lines displayed. */
16501 static int
16502 display_mode_lines (w)
16503 struct window *w;
16505 Lisp_Object old_selected_window, old_selected_frame;
16506 int n = 0;
16508 old_selected_frame = selected_frame;
16509 selected_frame = w->frame;
16510 old_selected_window = selected_window;
16511 XSETWINDOW (selected_window, w);
16513 /* These will be set while the mode line specs are processed. */
16514 line_number_displayed = 0;
16515 w->column_number_displayed = Qnil;
16517 if (WINDOW_WANTS_MODELINE_P (w))
16519 struct window *sel_w = XWINDOW (old_selected_window);
16521 /* Select mode line face based on the real selected window. */
16522 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16523 current_buffer->mode_line_format);
16524 ++n;
16527 if (WINDOW_WANTS_HEADER_LINE_P (w))
16529 display_mode_line (w, HEADER_LINE_FACE_ID,
16530 current_buffer->header_line_format);
16531 ++n;
16534 selected_frame = old_selected_frame;
16535 selected_window = old_selected_window;
16536 return n;
16540 /* Display mode or top line of window W. FACE_ID specifies which line
16541 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16542 FORMAT is the mode line format to display. Value is the pixel
16543 height of the mode line displayed. */
16545 static int
16546 display_mode_line (w, face_id, format)
16547 struct window *w;
16548 enum face_id face_id;
16549 Lisp_Object format;
16551 struct it it;
16552 struct face *face;
16553 int count = SPECPDL_INDEX ();
16555 init_iterator (&it, w, -1, -1, NULL, face_id);
16556 prepare_desired_row (it.glyph_row);
16558 it.glyph_row->mode_line_p = 1;
16560 if (! mode_line_inverse_video)
16561 /* Force the mode-line to be displayed in the default face. */
16562 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16564 record_unwind_protect (unwind_format_mode_line,
16565 format_mode_line_unwind_data (NULL, 0));
16567 mode_line_target = MODE_LINE_DISPLAY;
16569 /* Temporarily make frame's keyboard the current kboard so that
16570 kboard-local variables in the mode_line_format will get the right
16571 values. */
16572 push_frame_kboard (it.f);
16573 record_unwind_save_match_data ();
16574 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16575 pop_frame_kboard ();
16577 unbind_to (count, Qnil);
16579 /* Fill up with spaces. */
16580 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16582 compute_line_metrics (&it);
16583 it.glyph_row->full_width_p = 1;
16584 it.glyph_row->continued_p = 0;
16585 it.glyph_row->truncated_on_left_p = 0;
16586 it.glyph_row->truncated_on_right_p = 0;
16588 /* Make a 3D mode-line have a shadow at its right end. */
16589 face = FACE_FROM_ID (it.f, face_id);
16590 extend_face_to_end_of_line (&it);
16591 if (face->box != FACE_NO_BOX)
16593 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16594 + it.glyph_row->used[TEXT_AREA] - 1);
16595 last->right_box_line_p = 1;
16598 return it.glyph_row->height;
16601 /* Move element ELT in LIST to the front of LIST.
16602 Return the updated list. */
16604 static Lisp_Object
16605 move_elt_to_front (elt, list)
16606 Lisp_Object elt, list;
16608 register Lisp_Object tail, prev;
16609 register Lisp_Object tem;
16611 tail = list;
16612 prev = Qnil;
16613 while (CONSP (tail))
16615 tem = XCAR (tail);
16617 if (EQ (elt, tem))
16619 /* Splice out the link TAIL. */
16620 if (NILP (prev))
16621 list = XCDR (tail);
16622 else
16623 Fsetcdr (prev, XCDR (tail));
16625 /* Now make it the first. */
16626 Fsetcdr (tail, list);
16627 return tail;
16629 else
16630 prev = tail;
16631 tail = XCDR (tail);
16632 QUIT;
16635 /* Not found--return unchanged LIST. */
16636 return list;
16639 /* Contribute ELT to the mode line for window IT->w. How it
16640 translates into text depends on its data type.
16642 IT describes the display environment in which we display, as usual.
16644 DEPTH is the depth in recursion. It is used to prevent
16645 infinite recursion here.
16647 FIELD_WIDTH is the number of characters the display of ELT should
16648 occupy in the mode line, and PRECISION is the maximum number of
16649 characters to display from ELT's representation. See
16650 display_string for details.
16652 Returns the hpos of the end of the text generated by ELT.
16654 PROPS is a property list to add to any string we encounter.
16656 If RISKY is nonzero, remove (disregard) any properties in any string
16657 we encounter, and ignore :eval and :propertize.
16659 The global variable `mode_line_target' determines whether the
16660 output is passed to `store_mode_line_noprop',
16661 `store_mode_line_string', or `display_string'. */
16663 static int
16664 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16665 struct it *it;
16666 int depth;
16667 int field_width, precision;
16668 Lisp_Object elt, props;
16669 int risky;
16671 int n = 0, field, prec;
16672 int literal = 0;
16674 tail_recurse:
16675 if (depth > 100)
16676 elt = build_string ("*too-deep*");
16678 depth++;
16680 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16682 case Lisp_String:
16684 /* A string: output it and check for %-constructs within it. */
16685 unsigned char c;
16686 int offset = 0;
16688 if (SCHARS (elt) > 0
16689 && (!NILP (props) || risky))
16691 Lisp_Object oprops, aelt;
16692 oprops = Ftext_properties_at (make_number (0), elt);
16694 /* If the starting string's properties are not what
16695 we want, translate the string. Also, if the string
16696 is risky, do that anyway. */
16698 if (NILP (Fequal (props, oprops)) || risky)
16700 /* If the starting string has properties,
16701 merge the specified ones onto the existing ones. */
16702 if (! NILP (oprops) && !risky)
16704 Lisp_Object tem;
16706 oprops = Fcopy_sequence (oprops);
16707 tem = props;
16708 while (CONSP (tem))
16710 oprops = Fplist_put (oprops, XCAR (tem),
16711 XCAR (XCDR (tem)));
16712 tem = XCDR (XCDR (tem));
16714 props = oprops;
16717 aelt = Fassoc (elt, mode_line_proptrans_alist);
16718 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16720 /* AELT is what we want. Move it to the front
16721 without consing. */
16722 elt = XCAR (aelt);
16723 mode_line_proptrans_alist
16724 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16726 else
16728 Lisp_Object tem;
16730 /* If AELT has the wrong props, it is useless.
16731 so get rid of it. */
16732 if (! NILP (aelt))
16733 mode_line_proptrans_alist
16734 = Fdelq (aelt, mode_line_proptrans_alist);
16736 elt = Fcopy_sequence (elt);
16737 Fset_text_properties (make_number (0), Flength (elt),
16738 props, elt);
16739 /* Add this item to mode_line_proptrans_alist. */
16740 mode_line_proptrans_alist
16741 = Fcons (Fcons (elt, props),
16742 mode_line_proptrans_alist);
16743 /* Truncate mode_line_proptrans_alist
16744 to at most 50 elements. */
16745 tem = Fnthcdr (make_number (50),
16746 mode_line_proptrans_alist);
16747 if (! NILP (tem))
16748 XSETCDR (tem, Qnil);
16753 offset = 0;
16755 if (literal)
16757 prec = precision - n;
16758 switch (mode_line_target)
16760 case MODE_LINE_NOPROP:
16761 case MODE_LINE_TITLE:
16762 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16763 break;
16764 case MODE_LINE_STRING:
16765 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16766 break;
16767 case MODE_LINE_DISPLAY:
16768 n += display_string (NULL, elt, Qnil, 0, 0, it,
16769 0, prec, 0, STRING_MULTIBYTE (elt));
16770 break;
16773 break;
16776 /* Handle the non-literal case. */
16778 while ((precision <= 0 || n < precision)
16779 && SREF (elt, offset) != 0
16780 && (mode_line_target != MODE_LINE_DISPLAY
16781 || it->current_x < it->last_visible_x))
16783 int last_offset = offset;
16785 /* Advance to end of string or next format specifier. */
16786 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16789 if (offset - 1 != last_offset)
16791 int nchars, nbytes;
16793 /* Output to end of string or up to '%'. Field width
16794 is length of string. Don't output more than
16795 PRECISION allows us. */
16796 offset--;
16798 prec = c_string_width (SDATA (elt) + last_offset,
16799 offset - last_offset, precision - n,
16800 &nchars, &nbytes);
16802 switch (mode_line_target)
16804 case MODE_LINE_NOPROP:
16805 case MODE_LINE_TITLE:
16806 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16807 break;
16808 case MODE_LINE_STRING:
16810 int bytepos = last_offset;
16811 int charpos = string_byte_to_char (elt, bytepos);
16812 int endpos = (precision <= 0
16813 ? string_byte_to_char (elt, offset)
16814 : charpos + nchars);
16816 n += store_mode_line_string (NULL,
16817 Fsubstring (elt, make_number (charpos),
16818 make_number (endpos)),
16819 0, 0, 0, Qnil);
16821 break;
16822 case MODE_LINE_DISPLAY:
16824 int bytepos = last_offset;
16825 int charpos = string_byte_to_char (elt, bytepos);
16827 if (precision <= 0)
16828 nchars = string_byte_to_char (elt, offset) - charpos;
16829 n += display_string (NULL, elt, Qnil, 0, charpos,
16830 it, 0, nchars, 0,
16831 STRING_MULTIBYTE (elt));
16833 break;
16836 else /* c == '%' */
16838 int percent_position = offset;
16840 /* Get the specified minimum width. Zero means
16841 don't pad. */
16842 field = 0;
16843 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16844 field = field * 10 + c - '0';
16846 /* Don't pad beyond the total padding allowed. */
16847 if (field_width - n > 0 && field > field_width - n)
16848 field = field_width - n;
16850 /* Note that either PRECISION <= 0 or N < PRECISION. */
16851 prec = precision - n;
16853 if (c == 'M')
16854 n += display_mode_element (it, depth, field, prec,
16855 Vglobal_mode_string, props,
16856 risky);
16857 else if (c != 0)
16859 int multibyte;
16860 int bytepos, charpos;
16861 unsigned char *spec;
16863 bytepos = percent_position;
16864 charpos = (STRING_MULTIBYTE (elt)
16865 ? string_byte_to_char (elt, bytepos)
16866 : bytepos);
16868 spec
16869 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16871 switch (mode_line_target)
16873 case MODE_LINE_NOPROP:
16874 case MODE_LINE_TITLE:
16875 n += store_mode_line_noprop (spec, field, prec);
16876 break;
16877 case MODE_LINE_STRING:
16879 int len = strlen (spec);
16880 Lisp_Object tem = make_string (spec, len);
16881 props = Ftext_properties_at (make_number (charpos), elt);
16882 /* Should only keep face property in props */
16883 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16885 break;
16886 case MODE_LINE_DISPLAY:
16888 int nglyphs_before, nwritten;
16890 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16891 nwritten = display_string (spec, Qnil, elt,
16892 charpos, 0, it,
16893 field, prec, 0,
16894 multibyte);
16896 /* Assign to the glyphs written above the
16897 string where the `%x' came from, position
16898 of the `%'. */
16899 if (nwritten > 0)
16901 struct glyph *glyph
16902 = (it->glyph_row->glyphs[TEXT_AREA]
16903 + nglyphs_before);
16904 int i;
16906 for (i = 0; i < nwritten; ++i)
16908 glyph[i].object = elt;
16909 glyph[i].charpos = charpos;
16912 n += nwritten;
16915 break;
16918 else /* c == 0 */
16919 break;
16923 break;
16925 case Lisp_Symbol:
16926 /* A symbol: process the value of the symbol recursively
16927 as if it appeared here directly. Avoid error if symbol void.
16928 Special case: if value of symbol is a string, output the string
16929 literally. */
16931 register Lisp_Object tem;
16933 /* If the variable is not marked as risky to set
16934 then its contents are risky to use. */
16935 if (NILP (Fget (elt, Qrisky_local_variable)))
16936 risky = 1;
16938 tem = Fboundp (elt);
16939 if (!NILP (tem))
16941 tem = Fsymbol_value (elt);
16942 /* If value is a string, output that string literally:
16943 don't check for % within it. */
16944 if (STRINGP (tem))
16945 literal = 1;
16947 if (!EQ (tem, elt))
16949 /* Give up right away for nil or t. */
16950 elt = tem;
16951 goto tail_recurse;
16955 break;
16957 case Lisp_Cons:
16959 register Lisp_Object car, tem;
16961 /* A cons cell: five distinct cases.
16962 If first element is :eval or :propertize, do something special.
16963 If first element is a string or a cons, process all the elements
16964 and effectively concatenate them.
16965 If first element is a negative number, truncate displaying cdr to
16966 at most that many characters. If positive, pad (with spaces)
16967 to at least that many characters.
16968 If first element is a symbol, process the cadr or caddr recursively
16969 according to whether the symbol's value is non-nil or nil. */
16970 car = XCAR (elt);
16971 if (EQ (car, QCeval))
16973 /* An element of the form (:eval FORM) means evaluate FORM
16974 and use the result as mode line elements. */
16976 if (risky)
16977 break;
16979 if (CONSP (XCDR (elt)))
16981 Lisp_Object spec;
16982 spec = safe_eval (XCAR (XCDR (elt)));
16983 n += display_mode_element (it, depth, field_width - n,
16984 precision - n, spec, props,
16985 risky);
16988 else if (EQ (car, QCpropertize))
16990 /* An element of the form (:propertize ELT PROPS...)
16991 means display ELT but applying properties PROPS. */
16993 if (risky)
16994 break;
16996 if (CONSP (XCDR (elt)))
16997 n += display_mode_element (it, depth, field_width - n,
16998 precision - n, XCAR (XCDR (elt)),
16999 XCDR (XCDR (elt)), risky);
17001 else if (SYMBOLP (car))
17003 tem = Fboundp (car);
17004 elt = XCDR (elt);
17005 if (!CONSP (elt))
17006 goto invalid;
17007 /* elt is now the cdr, and we know it is a cons cell.
17008 Use its car if CAR has a non-nil value. */
17009 if (!NILP (tem))
17011 tem = Fsymbol_value (car);
17012 if (!NILP (tem))
17014 elt = XCAR (elt);
17015 goto tail_recurse;
17018 /* Symbol's value is nil (or symbol is unbound)
17019 Get the cddr of the original list
17020 and if possible find the caddr and use that. */
17021 elt = XCDR (elt);
17022 if (NILP (elt))
17023 break;
17024 else if (!CONSP (elt))
17025 goto invalid;
17026 elt = XCAR (elt);
17027 goto tail_recurse;
17029 else if (INTEGERP (car))
17031 register int lim = XINT (car);
17032 elt = XCDR (elt);
17033 if (lim < 0)
17035 /* Negative int means reduce maximum width. */
17036 if (precision <= 0)
17037 precision = -lim;
17038 else
17039 precision = min (precision, -lim);
17041 else if (lim > 0)
17043 /* Padding specified. Don't let it be more than
17044 current maximum. */
17045 if (precision > 0)
17046 lim = min (precision, lim);
17048 /* If that's more padding than already wanted, queue it.
17049 But don't reduce padding already specified even if
17050 that is beyond the current truncation point. */
17051 field_width = max (lim, field_width);
17053 goto tail_recurse;
17055 else if (STRINGP (car) || CONSP (car))
17057 register int limit = 50;
17058 /* Limit is to protect against circular lists. */
17059 while (CONSP (elt)
17060 && --limit > 0
17061 && (precision <= 0 || n < precision))
17063 n += display_mode_element (it, depth,
17064 /* Do padding only after the last
17065 element in the list. */
17066 (! CONSP (XCDR (elt))
17067 ? field_width - n
17068 : 0),
17069 precision - n, XCAR (elt),
17070 props, risky);
17071 elt = XCDR (elt);
17075 break;
17077 default:
17078 invalid:
17079 elt = build_string ("*invalid*");
17080 goto tail_recurse;
17083 /* Pad to FIELD_WIDTH. */
17084 if (field_width > 0 && n < field_width)
17086 switch (mode_line_target)
17088 case MODE_LINE_NOPROP:
17089 case MODE_LINE_TITLE:
17090 n += store_mode_line_noprop ("", field_width - n, 0);
17091 break;
17092 case MODE_LINE_STRING:
17093 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17094 break;
17095 case MODE_LINE_DISPLAY:
17096 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17097 0, 0, 0);
17098 break;
17102 return n;
17105 /* Store a mode-line string element in mode_line_string_list.
17107 If STRING is non-null, display that C string. Otherwise, the Lisp
17108 string LISP_STRING is displayed.
17110 FIELD_WIDTH is the minimum number of output glyphs to produce.
17111 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17112 with spaces. FIELD_WIDTH <= 0 means don't pad.
17114 PRECISION is the maximum number of characters to output from
17115 STRING. PRECISION <= 0 means don't truncate the string.
17117 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17118 properties to the string.
17120 PROPS are the properties to add to the string.
17121 The mode_line_string_face face property is always added to the string.
17124 static int
17125 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17126 char *string;
17127 Lisp_Object lisp_string;
17128 int copy_string;
17129 int field_width;
17130 int precision;
17131 Lisp_Object props;
17133 int len;
17134 int n = 0;
17136 if (string != NULL)
17138 len = strlen (string);
17139 if (precision > 0 && len > precision)
17140 len = precision;
17141 lisp_string = make_string (string, len);
17142 if (NILP (props))
17143 props = mode_line_string_face_prop;
17144 else if (!NILP (mode_line_string_face))
17146 Lisp_Object face = Fplist_get (props, Qface);
17147 props = Fcopy_sequence (props);
17148 if (NILP (face))
17149 face = mode_line_string_face;
17150 else
17151 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17152 props = Fplist_put (props, Qface, face);
17154 Fadd_text_properties (make_number (0), make_number (len),
17155 props, lisp_string);
17157 else
17159 len = XFASTINT (Flength (lisp_string));
17160 if (precision > 0 && len > precision)
17162 len = precision;
17163 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17164 precision = -1;
17166 if (!NILP (mode_line_string_face))
17168 Lisp_Object face;
17169 if (NILP (props))
17170 props = Ftext_properties_at (make_number (0), lisp_string);
17171 face = Fplist_get (props, Qface);
17172 if (NILP (face))
17173 face = mode_line_string_face;
17174 else
17175 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17176 props = Fcons (Qface, Fcons (face, Qnil));
17177 if (copy_string)
17178 lisp_string = Fcopy_sequence (lisp_string);
17180 if (!NILP (props))
17181 Fadd_text_properties (make_number (0), make_number (len),
17182 props, lisp_string);
17185 if (len > 0)
17187 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17188 n += len;
17191 if (field_width > len)
17193 field_width -= len;
17194 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17195 if (!NILP (props))
17196 Fadd_text_properties (make_number (0), make_number (field_width),
17197 props, lisp_string);
17198 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17199 n += field_width;
17202 return n;
17206 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17207 1, 4, 0,
17208 doc: /* Format a string out of a mode line format specification.
17209 First arg FORMAT specifies the mode line format (see `mode-line-format'
17210 for details) to use.
17212 Optional second arg FACE specifies the face property to put
17213 on all characters for which no face is specified.
17214 t means whatever face the window's mode line currently uses
17215 \(either `mode-line' or `mode-line-inactive', depending).
17216 nil means the default is no face property.
17217 If FACE is an integer, the value string has no text properties.
17219 Optional third and fourth args WINDOW and BUFFER specify the window
17220 and buffer to use as the context for the formatting (defaults
17221 are the selected window and the window's buffer). */)
17222 (format, face, window, buffer)
17223 Lisp_Object format, face, window, buffer;
17225 struct it it;
17226 int len;
17227 struct window *w;
17228 struct buffer *old_buffer = NULL;
17229 int face_id = -1;
17230 int no_props = INTEGERP (face);
17231 int count = SPECPDL_INDEX ();
17232 Lisp_Object str;
17233 int string_start = 0;
17235 if (NILP (window))
17236 window = selected_window;
17237 CHECK_WINDOW (window);
17238 w = XWINDOW (window);
17240 if (NILP (buffer))
17241 buffer = w->buffer;
17242 CHECK_BUFFER (buffer);
17244 if (NILP (format))
17245 return build_string ("");
17247 if (no_props)
17248 face = Qnil;
17250 if (!NILP (face))
17252 if (EQ (face, Qt))
17253 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17254 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17257 if (face_id < 0)
17258 face_id = DEFAULT_FACE_ID;
17260 if (XBUFFER (buffer) != current_buffer)
17261 old_buffer = current_buffer;
17263 /* Save things including mode_line_proptrans_alist,
17264 and set that to nil so that we don't alter the outer value. */
17265 record_unwind_protect (unwind_format_mode_line,
17266 format_mode_line_unwind_data (old_buffer, 1));
17267 mode_line_proptrans_alist = Qnil;
17269 if (old_buffer)
17270 set_buffer_internal_1 (XBUFFER (buffer));
17272 init_iterator (&it, w, -1, -1, NULL, face_id);
17274 if (no_props)
17276 mode_line_target = MODE_LINE_NOPROP;
17277 mode_line_string_face_prop = Qnil;
17278 mode_line_string_list = Qnil;
17279 string_start = MODE_LINE_NOPROP_LEN (0);
17281 else
17283 mode_line_target = MODE_LINE_STRING;
17284 mode_line_string_list = Qnil;
17285 mode_line_string_face = face;
17286 mode_line_string_face_prop
17287 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17290 push_frame_kboard (it.f);
17291 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17292 pop_frame_kboard ();
17294 if (no_props)
17296 len = MODE_LINE_NOPROP_LEN (string_start);
17297 str = make_string (mode_line_noprop_buf + string_start, len);
17299 else
17301 mode_line_string_list = Fnreverse (mode_line_string_list);
17302 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17303 make_string ("", 0));
17306 unbind_to (count, Qnil);
17307 return str;
17310 /* Write a null-terminated, right justified decimal representation of
17311 the positive integer D to BUF using a minimal field width WIDTH. */
17313 static void
17314 pint2str (buf, width, d)
17315 register char *buf;
17316 register int width;
17317 register int d;
17319 register char *p = buf;
17321 if (d <= 0)
17322 *p++ = '0';
17323 else
17325 while (d > 0)
17327 *p++ = d % 10 + '0';
17328 d /= 10;
17332 for (width -= (int) (p - buf); width > 0; --width)
17333 *p++ = ' ';
17334 *p-- = '\0';
17335 while (p > buf)
17337 d = *buf;
17338 *buf++ = *p;
17339 *p-- = d;
17343 /* Write a null-terminated, right justified decimal and "human
17344 readable" representation of the nonnegative integer D to BUF using
17345 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17347 static const char power_letter[] =
17349 0, /* not used */
17350 'k', /* kilo */
17351 'M', /* mega */
17352 'G', /* giga */
17353 'T', /* tera */
17354 'P', /* peta */
17355 'E', /* exa */
17356 'Z', /* zetta */
17357 'Y' /* yotta */
17360 static void
17361 pint2hrstr (buf, width, d)
17362 char *buf;
17363 int width;
17364 int d;
17366 /* We aim to represent the nonnegative integer D as
17367 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17368 int quotient = d;
17369 int remainder = 0;
17370 /* -1 means: do not use TENTHS. */
17371 int tenths = -1;
17372 int exponent = 0;
17374 /* Length of QUOTIENT.TENTHS as a string. */
17375 int length;
17377 char * psuffix;
17378 char * p;
17380 if (1000 <= quotient)
17382 /* Scale to the appropriate EXPONENT. */
17385 remainder = quotient % 1000;
17386 quotient /= 1000;
17387 exponent++;
17389 while (1000 <= quotient);
17391 /* Round to nearest and decide whether to use TENTHS or not. */
17392 if (quotient <= 9)
17394 tenths = remainder / 100;
17395 if (50 <= remainder % 100)
17397 if (tenths < 9)
17398 tenths++;
17399 else
17401 quotient++;
17402 if (quotient == 10)
17403 tenths = -1;
17404 else
17405 tenths = 0;
17409 else
17410 if (500 <= remainder)
17412 if (quotient < 999)
17413 quotient++;
17414 else
17416 quotient = 1;
17417 exponent++;
17418 tenths = 0;
17423 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17424 if (tenths == -1 && quotient <= 99)
17425 if (quotient <= 9)
17426 length = 1;
17427 else
17428 length = 2;
17429 else
17430 length = 3;
17431 p = psuffix = buf + max (width, length);
17433 /* Print EXPONENT. */
17434 if (exponent)
17435 *psuffix++ = power_letter[exponent];
17436 *psuffix = '\0';
17438 /* Print TENTHS. */
17439 if (tenths >= 0)
17441 *--p = '0' + tenths;
17442 *--p = '.';
17445 /* Print QUOTIENT. */
17448 int digit = quotient % 10;
17449 *--p = '0' + digit;
17451 while ((quotient /= 10) != 0);
17453 /* Print leading spaces. */
17454 while (buf < p)
17455 *--p = ' ';
17458 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17459 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17460 type of CODING_SYSTEM. Return updated pointer into BUF. */
17462 static unsigned char invalid_eol_type[] = "(*invalid*)";
17464 static char *
17465 decode_mode_spec_coding (coding_system, buf, eol_flag)
17466 Lisp_Object coding_system;
17467 register char *buf;
17468 int eol_flag;
17470 Lisp_Object val;
17471 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17472 const unsigned char *eol_str;
17473 int eol_str_len;
17474 /* The EOL conversion we are using. */
17475 Lisp_Object eoltype;
17477 val = Fget (coding_system, Qcoding_system);
17478 eoltype = Qnil;
17480 if (!VECTORP (val)) /* Not yet decided. */
17482 if (multibyte)
17483 *buf++ = '-';
17484 if (eol_flag)
17485 eoltype = eol_mnemonic_undecided;
17486 /* Don't mention EOL conversion if it isn't decided. */
17488 else
17490 Lisp_Object eolvalue;
17492 eolvalue = Fget (coding_system, Qeol_type);
17494 if (multibyte)
17495 *buf++ = XFASTINT (AREF (val, 1));
17497 if (eol_flag)
17499 /* The EOL conversion that is normal on this system. */
17501 if (NILP (eolvalue)) /* Not yet decided. */
17502 eoltype = eol_mnemonic_undecided;
17503 else if (VECTORP (eolvalue)) /* Not yet decided. */
17504 eoltype = eol_mnemonic_undecided;
17505 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17506 eoltype = (XFASTINT (eolvalue) == 0
17507 ? eol_mnemonic_unix
17508 : (XFASTINT (eolvalue) == 1
17509 ? eol_mnemonic_dos : eol_mnemonic_mac));
17513 if (eol_flag)
17515 /* Mention the EOL conversion if it is not the usual one. */
17516 if (STRINGP (eoltype))
17518 eol_str = SDATA (eoltype);
17519 eol_str_len = SBYTES (eoltype);
17521 else if (INTEGERP (eoltype)
17522 && CHAR_VALID_P (XINT (eoltype), 0))
17524 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17525 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17526 eol_str = tmp;
17528 else
17530 eol_str = invalid_eol_type;
17531 eol_str_len = sizeof (invalid_eol_type) - 1;
17533 bcopy (eol_str, buf, eol_str_len);
17534 buf += eol_str_len;
17537 return buf;
17540 /* Return a string for the output of a mode line %-spec for window W,
17541 generated by character C. PRECISION >= 0 means don't return a
17542 string longer than that value. FIELD_WIDTH > 0 means pad the
17543 string returned with spaces to that value. Return 1 in *MULTIBYTE
17544 if the result is multibyte text.
17546 Note we operate on the current buffer for most purposes,
17547 the exception being w->base_line_pos. */
17549 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17551 static char *
17552 decode_mode_spec (w, c, field_width, precision, multibyte)
17553 struct window *w;
17554 register int c;
17555 int field_width, precision;
17556 int *multibyte;
17558 Lisp_Object obj;
17559 struct frame *f = XFRAME (WINDOW_FRAME (w));
17560 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17561 struct buffer *b = current_buffer;
17563 obj = Qnil;
17564 *multibyte = 0;
17566 switch (c)
17568 case '*':
17569 if (!NILP (b->read_only))
17570 return "%";
17571 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17572 return "*";
17573 return "-";
17575 case '+':
17576 /* This differs from %* only for a modified read-only buffer. */
17577 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17578 return "*";
17579 if (!NILP (b->read_only))
17580 return "%";
17581 return "-";
17583 case '&':
17584 /* This differs from %* in ignoring read-only-ness. */
17585 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17586 return "*";
17587 return "-";
17589 case '%':
17590 return "%";
17592 case '[':
17594 int i;
17595 char *p;
17597 if (command_loop_level > 5)
17598 return "[[[... ";
17599 p = decode_mode_spec_buf;
17600 for (i = 0; i < command_loop_level; i++)
17601 *p++ = '[';
17602 *p = 0;
17603 return decode_mode_spec_buf;
17606 case ']':
17608 int i;
17609 char *p;
17611 if (command_loop_level > 5)
17612 return " ...]]]";
17613 p = decode_mode_spec_buf;
17614 for (i = 0; i < command_loop_level; i++)
17615 *p++ = ']';
17616 *p = 0;
17617 return decode_mode_spec_buf;
17620 case '-':
17622 register int i;
17624 /* Let lots_of_dashes be a string of infinite length. */
17625 if (mode_line_target == MODE_LINE_NOPROP ||
17626 mode_line_target == MODE_LINE_STRING)
17627 return "--";
17628 if (field_width <= 0
17629 || field_width > sizeof (lots_of_dashes))
17631 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17632 decode_mode_spec_buf[i] = '-';
17633 decode_mode_spec_buf[i] = '\0';
17634 return decode_mode_spec_buf;
17636 else
17637 return lots_of_dashes;
17640 case 'b':
17641 obj = b->name;
17642 break;
17644 case 'c':
17646 int col = (int) current_column (); /* iftc */
17647 w->column_number_displayed = make_number (col);
17648 pint2str (decode_mode_spec_buf, field_width, col);
17649 return decode_mode_spec_buf;
17652 case 'e':
17653 #ifndef SYSTEM_MALLOC
17655 if (NILP (Vmemory_full))
17656 return "";
17657 else
17658 return "!MEM FULL! ";
17660 #else
17661 return "";
17662 #endif
17664 case 'F':
17665 /* %F displays the frame name. */
17666 if (!NILP (f->title))
17667 return (char *) SDATA (f->title);
17668 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17669 return (char *) SDATA (f->name);
17670 return "Emacs";
17672 case 'f':
17673 obj = b->filename;
17674 break;
17676 case 'i':
17678 int size = ZV - BEGV;
17679 pint2str (decode_mode_spec_buf, field_width, size);
17680 return decode_mode_spec_buf;
17683 case 'I':
17685 int size = ZV - BEGV;
17686 pint2hrstr (decode_mode_spec_buf, field_width, size);
17687 return decode_mode_spec_buf;
17690 case 'l':
17692 int startpos = XMARKER (w->start)->charpos;
17693 int startpos_byte = marker_byte_position (w->start);
17694 int line, linepos, linepos_byte, topline;
17695 int nlines, junk;
17696 int height = WINDOW_TOTAL_LINES (w);
17698 /* If we decided that this buffer isn't suitable for line numbers,
17699 don't forget that too fast. */
17700 if (EQ (w->base_line_pos, w->buffer))
17701 goto no_value;
17702 /* But do forget it, if the window shows a different buffer now. */
17703 else if (BUFFERP (w->base_line_pos))
17704 w->base_line_pos = Qnil;
17706 /* If the buffer is very big, don't waste time. */
17707 if (INTEGERP (Vline_number_display_limit)
17708 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17710 w->base_line_pos = Qnil;
17711 w->base_line_number = Qnil;
17712 goto no_value;
17715 if (!NILP (w->base_line_number)
17716 && !NILP (w->base_line_pos)
17717 && XFASTINT (w->base_line_pos) <= startpos)
17719 line = XFASTINT (w->base_line_number);
17720 linepos = XFASTINT (w->base_line_pos);
17721 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17723 else
17725 line = 1;
17726 linepos = BUF_BEGV (b);
17727 linepos_byte = BUF_BEGV_BYTE (b);
17730 /* Count lines from base line to window start position. */
17731 nlines = display_count_lines (linepos, linepos_byte,
17732 startpos_byte,
17733 startpos, &junk);
17735 topline = nlines + line;
17737 /* Determine a new base line, if the old one is too close
17738 or too far away, or if we did not have one.
17739 "Too close" means it's plausible a scroll-down would
17740 go back past it. */
17741 if (startpos == BUF_BEGV (b))
17743 w->base_line_number = make_number (topline);
17744 w->base_line_pos = make_number (BUF_BEGV (b));
17746 else if (nlines < height + 25 || nlines > height * 3 + 50
17747 || linepos == BUF_BEGV (b))
17749 int limit = BUF_BEGV (b);
17750 int limit_byte = BUF_BEGV_BYTE (b);
17751 int position;
17752 int distance = (height * 2 + 30) * line_number_display_limit_width;
17754 if (startpos - distance > limit)
17756 limit = startpos - distance;
17757 limit_byte = CHAR_TO_BYTE (limit);
17760 nlines = display_count_lines (startpos, startpos_byte,
17761 limit_byte,
17762 - (height * 2 + 30),
17763 &position);
17764 /* If we couldn't find the lines we wanted within
17765 line_number_display_limit_width chars per line,
17766 give up on line numbers for this window. */
17767 if (position == limit_byte && limit == startpos - distance)
17769 w->base_line_pos = w->buffer;
17770 w->base_line_number = Qnil;
17771 goto no_value;
17774 w->base_line_number = make_number (topline - nlines);
17775 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17778 /* Now count lines from the start pos to point. */
17779 nlines = display_count_lines (startpos, startpos_byte,
17780 PT_BYTE, PT, &junk);
17782 /* Record that we did display the line number. */
17783 line_number_displayed = 1;
17785 /* Make the string to show. */
17786 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17787 return decode_mode_spec_buf;
17788 no_value:
17790 char* p = decode_mode_spec_buf;
17791 int pad = field_width - 2;
17792 while (pad-- > 0)
17793 *p++ = ' ';
17794 *p++ = '?';
17795 *p++ = '?';
17796 *p = '\0';
17797 return decode_mode_spec_buf;
17800 break;
17802 case 'm':
17803 obj = b->mode_name;
17804 break;
17806 case 'n':
17807 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17808 return " Narrow";
17809 break;
17811 case 'p':
17813 int pos = marker_position (w->start);
17814 int total = BUF_ZV (b) - BUF_BEGV (b);
17816 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17818 if (pos <= BUF_BEGV (b))
17819 return "All";
17820 else
17821 return "Bottom";
17823 else if (pos <= BUF_BEGV (b))
17824 return "Top";
17825 else
17827 if (total > 1000000)
17828 /* Do it differently for a large value, to avoid overflow. */
17829 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17830 else
17831 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17832 /* We can't normally display a 3-digit number,
17833 so get us a 2-digit number that is close. */
17834 if (total == 100)
17835 total = 99;
17836 sprintf (decode_mode_spec_buf, "%2d%%", total);
17837 return decode_mode_spec_buf;
17841 /* Display percentage of size above the bottom of the screen. */
17842 case 'P':
17844 int toppos = marker_position (w->start);
17845 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17846 int total = BUF_ZV (b) - BUF_BEGV (b);
17848 if (botpos >= BUF_ZV (b))
17850 if (toppos <= BUF_BEGV (b))
17851 return "All";
17852 else
17853 return "Bottom";
17855 else
17857 if (total > 1000000)
17858 /* Do it differently for a large value, to avoid overflow. */
17859 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17860 else
17861 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17862 /* We can't normally display a 3-digit number,
17863 so get us a 2-digit number that is close. */
17864 if (total == 100)
17865 total = 99;
17866 if (toppos <= BUF_BEGV (b))
17867 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17868 else
17869 sprintf (decode_mode_spec_buf, "%2d%%", total);
17870 return decode_mode_spec_buf;
17874 case 's':
17875 /* status of process */
17876 obj = Fget_buffer_process (Fcurrent_buffer ());
17877 if (NILP (obj))
17878 return "no process";
17879 #ifdef subprocesses
17880 obj = Fsymbol_name (Fprocess_status (obj));
17881 #endif
17882 break;
17884 case 't': /* indicate TEXT or BINARY */
17885 #ifdef MODE_LINE_BINARY_TEXT
17886 return MODE_LINE_BINARY_TEXT (b);
17887 #else
17888 return "T";
17889 #endif
17891 case 'z':
17892 /* coding-system (not including end-of-line format) */
17893 case 'Z':
17894 /* coding-system (including end-of-line type) */
17896 int eol_flag = (c == 'Z');
17897 char *p = decode_mode_spec_buf;
17899 if (! FRAME_WINDOW_P (f))
17901 /* No need to mention EOL here--the terminal never needs
17902 to do EOL conversion. */
17903 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17904 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17906 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17907 p, eol_flag);
17909 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17910 #ifdef subprocesses
17911 obj = Fget_buffer_process (Fcurrent_buffer ());
17912 if (PROCESSP (obj))
17914 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17915 p, eol_flag);
17916 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17917 p, eol_flag);
17919 #endif /* subprocesses */
17920 #endif /* 0 */
17921 *p = 0;
17922 return decode_mode_spec_buf;
17926 if (STRINGP (obj))
17928 *multibyte = STRING_MULTIBYTE (obj);
17929 return (char *) SDATA (obj);
17931 else
17932 return "";
17936 /* Count up to COUNT lines starting from START / START_BYTE.
17937 But don't go beyond LIMIT_BYTE.
17938 Return the number of lines thus found (always nonnegative).
17940 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17942 static int
17943 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17944 int start, start_byte, limit_byte, count;
17945 int *byte_pos_ptr;
17947 register unsigned char *cursor;
17948 unsigned char *base;
17950 register int ceiling;
17951 register unsigned char *ceiling_addr;
17952 int orig_count = count;
17954 /* If we are not in selective display mode,
17955 check only for newlines. */
17956 int selective_display = (!NILP (current_buffer->selective_display)
17957 && !INTEGERP (current_buffer->selective_display));
17959 if (count > 0)
17961 while (start_byte < limit_byte)
17963 ceiling = BUFFER_CEILING_OF (start_byte);
17964 ceiling = min (limit_byte - 1, ceiling);
17965 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17966 base = (cursor = BYTE_POS_ADDR (start_byte));
17967 while (1)
17969 if (selective_display)
17970 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17972 else
17973 while (*cursor != '\n' && ++cursor != ceiling_addr)
17976 if (cursor != ceiling_addr)
17978 if (--count == 0)
17980 start_byte += cursor - base + 1;
17981 *byte_pos_ptr = start_byte;
17982 return orig_count;
17984 else
17985 if (++cursor == ceiling_addr)
17986 break;
17988 else
17989 break;
17991 start_byte += cursor - base;
17994 else
17996 while (start_byte > limit_byte)
17998 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17999 ceiling = max (limit_byte, ceiling);
18000 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18001 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18002 while (1)
18004 if (selective_display)
18005 while (--cursor != ceiling_addr
18006 && *cursor != '\n' && *cursor != 015)
18008 else
18009 while (--cursor != ceiling_addr && *cursor != '\n')
18012 if (cursor != ceiling_addr)
18014 if (++count == 0)
18016 start_byte += cursor - base + 1;
18017 *byte_pos_ptr = start_byte;
18018 /* When scanning backwards, we should
18019 not count the newline posterior to which we stop. */
18020 return - orig_count - 1;
18023 else
18024 break;
18026 /* Here we add 1 to compensate for the last decrement
18027 of CURSOR, which took it past the valid range. */
18028 start_byte += cursor - base + 1;
18032 *byte_pos_ptr = limit_byte;
18034 if (count < 0)
18035 return - orig_count + count;
18036 return orig_count - count;
18042 /***********************************************************************
18043 Displaying strings
18044 ***********************************************************************/
18046 /* Display a NUL-terminated string, starting with index START.
18048 If STRING is non-null, display that C string. Otherwise, the Lisp
18049 string LISP_STRING is displayed.
18051 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18052 FACE_STRING. Display STRING or LISP_STRING with the face at
18053 FACE_STRING_POS in FACE_STRING:
18055 Display the string in the environment given by IT, but use the
18056 standard display table, temporarily.
18058 FIELD_WIDTH is the minimum number of output glyphs to produce.
18059 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18060 with spaces. If STRING has more characters, more than FIELD_WIDTH
18061 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18063 PRECISION is the maximum number of characters to output from
18064 STRING. PRECISION < 0 means don't truncate the string.
18066 This is roughly equivalent to printf format specifiers:
18068 FIELD_WIDTH PRECISION PRINTF
18069 ----------------------------------------
18070 -1 -1 %s
18071 -1 10 %.10s
18072 10 -1 %10s
18073 20 10 %20.10s
18075 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18076 display them, and < 0 means obey the current buffer's value of
18077 enable_multibyte_characters.
18079 Value is the number of columns displayed. */
18081 static int
18082 display_string (string, lisp_string, face_string, face_string_pos,
18083 start, it, field_width, precision, max_x, multibyte)
18084 unsigned char *string;
18085 Lisp_Object lisp_string;
18086 Lisp_Object face_string;
18087 int face_string_pos;
18088 int start;
18089 struct it *it;
18090 int field_width, precision, max_x;
18091 int multibyte;
18093 int hpos_at_start = it->hpos;
18094 int saved_face_id = it->face_id;
18095 struct glyph_row *row = it->glyph_row;
18097 /* Initialize the iterator IT for iteration over STRING beginning
18098 with index START. */
18099 reseat_to_string (it, string, lisp_string, start,
18100 precision, field_width, multibyte);
18102 /* If displaying STRING, set up the face of the iterator
18103 from LISP_STRING, if that's given. */
18104 if (STRINGP (face_string))
18106 int endptr;
18107 struct face *face;
18109 it->face_id
18110 = face_at_string_position (it->w, face_string, face_string_pos,
18111 0, it->region_beg_charpos,
18112 it->region_end_charpos,
18113 &endptr, it->base_face_id, 0);
18114 face = FACE_FROM_ID (it->f, it->face_id);
18115 it->face_box_p = face->box != FACE_NO_BOX;
18118 /* Set max_x to the maximum allowed X position. Don't let it go
18119 beyond the right edge of the window. */
18120 if (max_x <= 0)
18121 max_x = it->last_visible_x;
18122 else
18123 max_x = min (max_x, it->last_visible_x);
18125 /* Skip over display elements that are not visible. because IT->w is
18126 hscrolled. */
18127 if (it->current_x < it->first_visible_x)
18128 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18129 MOVE_TO_POS | MOVE_TO_X);
18131 row->ascent = it->max_ascent;
18132 row->height = it->max_ascent + it->max_descent;
18133 row->phys_ascent = it->max_phys_ascent;
18134 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18135 row->extra_line_spacing = it->max_extra_line_spacing;
18137 /* This condition is for the case that we are called with current_x
18138 past last_visible_x. */
18139 while (it->current_x < max_x)
18141 int x_before, x, n_glyphs_before, i, nglyphs;
18143 /* Get the next display element. */
18144 if (!get_next_display_element (it))
18145 break;
18147 /* Produce glyphs. */
18148 x_before = it->current_x;
18149 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18150 PRODUCE_GLYPHS (it);
18152 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18153 i = 0;
18154 x = x_before;
18155 while (i < nglyphs)
18157 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18159 if (!it->truncate_lines_p
18160 && x + glyph->pixel_width > max_x)
18162 /* End of continued line or max_x reached. */
18163 if (CHAR_GLYPH_PADDING_P (*glyph))
18165 /* A wide character is unbreakable. */
18166 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18167 it->current_x = x_before;
18169 else
18171 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18172 it->current_x = x;
18174 break;
18176 else if (x + glyph->pixel_width > it->first_visible_x)
18178 /* Glyph is at least partially visible. */
18179 ++it->hpos;
18180 if (x < it->first_visible_x)
18181 it->glyph_row->x = x - it->first_visible_x;
18183 else
18185 /* Glyph is off the left margin of the display area.
18186 Should not happen. */
18187 abort ();
18190 row->ascent = max (row->ascent, it->max_ascent);
18191 row->height = max (row->height, it->max_ascent + it->max_descent);
18192 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18193 row->phys_height = max (row->phys_height,
18194 it->max_phys_ascent + it->max_phys_descent);
18195 row->extra_line_spacing = max (row->extra_line_spacing,
18196 it->max_extra_line_spacing);
18197 x += glyph->pixel_width;
18198 ++i;
18201 /* Stop if max_x reached. */
18202 if (i < nglyphs)
18203 break;
18205 /* Stop at line ends. */
18206 if (ITERATOR_AT_END_OF_LINE_P (it))
18208 it->continuation_lines_width = 0;
18209 break;
18212 set_iterator_to_next (it, 1);
18214 /* Stop if truncating at the right edge. */
18215 if (it->truncate_lines_p
18216 && it->current_x >= it->last_visible_x)
18218 /* Add truncation mark, but don't do it if the line is
18219 truncated at a padding space. */
18220 if (IT_CHARPOS (*it) < it->string_nchars)
18222 if (!FRAME_WINDOW_P (it->f))
18224 int i, n;
18226 if (it->current_x > it->last_visible_x)
18228 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18229 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18230 break;
18231 for (n = row->used[TEXT_AREA]; i < n; ++i)
18233 row->used[TEXT_AREA] = i;
18234 produce_special_glyphs (it, IT_TRUNCATION);
18237 produce_special_glyphs (it, IT_TRUNCATION);
18239 it->glyph_row->truncated_on_right_p = 1;
18241 break;
18245 /* Maybe insert a truncation at the left. */
18246 if (it->first_visible_x
18247 && IT_CHARPOS (*it) > 0)
18249 if (!FRAME_WINDOW_P (it->f))
18250 insert_left_trunc_glyphs (it);
18251 it->glyph_row->truncated_on_left_p = 1;
18254 it->face_id = saved_face_id;
18256 /* Value is number of columns displayed. */
18257 return it->hpos - hpos_at_start;
18262 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18263 appears as an element of LIST or as the car of an element of LIST.
18264 If PROPVAL is a list, compare each element against LIST in that
18265 way, and return 1/2 if any element of PROPVAL is found in LIST.
18266 Otherwise return 0. This function cannot quit.
18267 The return value is 2 if the text is invisible but with an ellipsis
18268 and 1 if it's invisible and without an ellipsis. */
18271 invisible_p (propval, list)
18272 register Lisp_Object propval;
18273 Lisp_Object list;
18275 register Lisp_Object tail, proptail;
18277 for (tail = list; CONSP (tail); tail = XCDR (tail))
18279 register Lisp_Object tem;
18280 tem = XCAR (tail);
18281 if (EQ (propval, tem))
18282 return 1;
18283 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18284 return NILP (XCDR (tem)) ? 1 : 2;
18287 if (CONSP (propval))
18289 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18291 Lisp_Object propelt;
18292 propelt = XCAR (proptail);
18293 for (tail = list; CONSP (tail); tail = XCDR (tail))
18295 register Lisp_Object tem;
18296 tem = XCAR (tail);
18297 if (EQ (propelt, tem))
18298 return 1;
18299 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18300 return NILP (XCDR (tem)) ? 1 : 2;
18305 return 0;
18308 /* Calculate a width or height in pixels from a specification using
18309 the following elements:
18311 SPEC ::=
18312 NUM - a (fractional) multiple of the default font width/height
18313 (NUM) - specifies exactly NUM pixels
18314 UNIT - a fixed number of pixels, see below.
18315 ELEMENT - size of a display element in pixels, see below.
18316 (NUM . SPEC) - equals NUM * SPEC
18317 (+ SPEC SPEC ...) - add pixel values
18318 (- SPEC SPEC ...) - subtract pixel values
18319 (- SPEC) - negate pixel value
18321 NUM ::=
18322 INT or FLOAT - a number constant
18323 SYMBOL - use symbol's (buffer local) variable binding.
18325 UNIT ::=
18326 in - pixels per inch *)
18327 mm - pixels per 1/1000 meter *)
18328 cm - pixels per 1/100 meter *)
18329 width - width of current font in pixels.
18330 height - height of current font in pixels.
18332 *) using the ratio(s) defined in display-pixels-per-inch.
18334 ELEMENT ::=
18336 left-fringe - left fringe width in pixels
18337 right-fringe - right fringe width in pixels
18339 left-margin - left margin width in pixels
18340 right-margin - right margin width in pixels
18342 scroll-bar - scroll-bar area width in pixels
18344 Examples:
18346 Pixels corresponding to 5 inches:
18347 (5 . in)
18349 Total width of non-text areas on left side of window (if scroll-bar is on left):
18350 '(space :width (+ left-fringe left-margin scroll-bar))
18352 Align to first text column (in header line):
18353 '(space :align-to 0)
18355 Align to middle of text area minus half the width of variable `my-image'
18356 containing a loaded image:
18357 '(space :align-to (0.5 . (- text my-image)))
18359 Width of left margin minus width of 1 character in the default font:
18360 '(space :width (- left-margin 1))
18362 Width of left margin minus width of 2 characters in the current font:
18363 '(space :width (- left-margin (2 . width)))
18365 Center 1 character over left-margin (in header line):
18366 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18368 Different ways to express width of left fringe plus left margin minus one pixel:
18369 '(space :width (- (+ left-fringe left-margin) (1)))
18370 '(space :width (+ left-fringe left-margin (- (1))))
18371 '(space :width (+ left-fringe left-margin (-1)))
18375 #define NUMVAL(X) \
18376 ((INTEGERP (X) || FLOATP (X)) \
18377 ? XFLOATINT (X) \
18378 : - 1)
18381 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18382 double *res;
18383 struct it *it;
18384 Lisp_Object prop;
18385 void *font;
18386 int width_p, *align_to;
18388 double pixels;
18390 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18391 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18393 if (NILP (prop))
18394 return OK_PIXELS (0);
18396 if (SYMBOLP (prop))
18398 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18400 char *unit = SDATA (SYMBOL_NAME (prop));
18402 if (unit[0] == 'i' && unit[1] == 'n')
18403 pixels = 1.0;
18404 else if (unit[0] == 'm' && unit[1] == 'm')
18405 pixels = 25.4;
18406 else if (unit[0] == 'c' && unit[1] == 'm')
18407 pixels = 2.54;
18408 else
18409 pixels = 0;
18410 if (pixels > 0)
18412 double ppi;
18413 #ifdef HAVE_WINDOW_SYSTEM
18414 if (FRAME_WINDOW_P (it->f)
18415 && (ppi = (width_p
18416 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18417 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18418 ppi > 0))
18419 return OK_PIXELS (ppi / pixels);
18420 #endif
18422 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18423 || (CONSP (Vdisplay_pixels_per_inch)
18424 && (ppi = (width_p
18425 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18426 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18427 ppi > 0)))
18428 return OK_PIXELS (ppi / pixels);
18430 return 0;
18434 #ifdef HAVE_WINDOW_SYSTEM
18435 if (EQ (prop, Qheight))
18436 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18437 if (EQ (prop, Qwidth))
18438 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18439 #else
18440 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18441 return OK_PIXELS (1);
18442 #endif
18444 if (EQ (prop, Qtext))
18445 return OK_PIXELS (width_p
18446 ? window_box_width (it->w, TEXT_AREA)
18447 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18449 if (align_to && *align_to < 0)
18451 *res = 0;
18452 if (EQ (prop, Qleft))
18453 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18454 if (EQ (prop, Qright))
18455 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18456 if (EQ (prop, Qcenter))
18457 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18458 + window_box_width (it->w, TEXT_AREA) / 2);
18459 if (EQ (prop, Qleft_fringe))
18460 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18461 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18462 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18463 if (EQ (prop, Qright_fringe))
18464 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18465 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18466 : window_box_right_offset (it->w, TEXT_AREA));
18467 if (EQ (prop, Qleft_margin))
18468 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18469 if (EQ (prop, Qright_margin))
18470 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18471 if (EQ (prop, Qscroll_bar))
18472 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18474 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18475 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18476 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18477 : 0)));
18479 else
18481 if (EQ (prop, Qleft_fringe))
18482 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18483 if (EQ (prop, Qright_fringe))
18484 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18485 if (EQ (prop, Qleft_margin))
18486 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18487 if (EQ (prop, Qright_margin))
18488 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18489 if (EQ (prop, Qscroll_bar))
18490 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18493 prop = Fbuffer_local_value (prop, it->w->buffer);
18496 if (INTEGERP (prop) || FLOATP (prop))
18498 int base_unit = (width_p
18499 ? FRAME_COLUMN_WIDTH (it->f)
18500 : FRAME_LINE_HEIGHT (it->f));
18501 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18504 if (CONSP (prop))
18506 Lisp_Object car = XCAR (prop);
18507 Lisp_Object cdr = XCDR (prop);
18509 if (SYMBOLP (car))
18511 #ifdef HAVE_WINDOW_SYSTEM
18512 if (valid_image_p (prop))
18514 int id = lookup_image (it->f, prop);
18515 struct image *img = IMAGE_FROM_ID (it->f, id);
18517 return OK_PIXELS (width_p ? img->width : img->height);
18519 #endif
18520 if (EQ (car, Qplus) || EQ (car, Qminus))
18522 int first = 1;
18523 double px;
18525 pixels = 0;
18526 while (CONSP (cdr))
18528 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18529 font, width_p, align_to))
18530 return 0;
18531 if (first)
18532 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18533 else
18534 pixels += px;
18535 cdr = XCDR (cdr);
18537 if (EQ (car, Qminus))
18538 pixels = -pixels;
18539 return OK_PIXELS (pixels);
18542 car = Fbuffer_local_value (car, it->w->buffer);
18545 if (INTEGERP (car) || FLOATP (car))
18547 double fact;
18548 pixels = XFLOATINT (car);
18549 if (NILP (cdr))
18550 return OK_PIXELS (pixels);
18551 if (calc_pixel_width_or_height (&fact, it, cdr,
18552 font, width_p, align_to))
18553 return OK_PIXELS (pixels * fact);
18554 return 0;
18557 return 0;
18560 return 0;
18564 /***********************************************************************
18565 Glyph Display
18566 ***********************************************************************/
18568 #ifdef HAVE_WINDOW_SYSTEM
18570 #if GLYPH_DEBUG
18572 void
18573 dump_glyph_string (s)
18574 struct glyph_string *s;
18576 fprintf (stderr, "glyph string\n");
18577 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18578 s->x, s->y, s->width, s->height);
18579 fprintf (stderr, " ybase = %d\n", s->ybase);
18580 fprintf (stderr, " hl = %d\n", s->hl);
18581 fprintf (stderr, " left overhang = %d, right = %d\n",
18582 s->left_overhang, s->right_overhang);
18583 fprintf (stderr, " nchars = %d\n", s->nchars);
18584 fprintf (stderr, " extends to end of line = %d\n",
18585 s->extends_to_end_of_line_p);
18586 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18587 fprintf (stderr, " bg width = %d\n", s->background_width);
18590 #endif /* GLYPH_DEBUG */
18592 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18593 of XChar2b structures for S; it can't be allocated in
18594 init_glyph_string because it must be allocated via `alloca'. W
18595 is the window on which S is drawn. ROW and AREA are the glyph row
18596 and area within the row from which S is constructed. START is the
18597 index of the first glyph structure covered by S. HL is a
18598 face-override for drawing S. */
18600 #ifdef HAVE_NTGUI
18601 #define OPTIONAL_HDC(hdc) hdc,
18602 #define DECLARE_HDC(hdc) HDC hdc;
18603 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18604 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18605 #endif
18607 #ifndef OPTIONAL_HDC
18608 #define OPTIONAL_HDC(hdc)
18609 #define DECLARE_HDC(hdc)
18610 #define ALLOCATE_HDC(hdc, f)
18611 #define RELEASE_HDC(hdc, f)
18612 #endif
18614 static void
18615 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18616 struct glyph_string *s;
18617 DECLARE_HDC (hdc)
18618 XChar2b *char2b;
18619 struct window *w;
18620 struct glyph_row *row;
18621 enum glyph_row_area area;
18622 int start;
18623 enum draw_glyphs_face hl;
18625 bzero (s, sizeof *s);
18626 s->w = w;
18627 s->f = XFRAME (w->frame);
18628 #ifdef HAVE_NTGUI
18629 s->hdc = hdc;
18630 #endif
18631 s->display = FRAME_X_DISPLAY (s->f);
18632 s->window = FRAME_X_WINDOW (s->f);
18633 s->char2b = char2b;
18634 s->hl = hl;
18635 s->row = row;
18636 s->area = area;
18637 s->first_glyph = row->glyphs[area] + start;
18638 s->height = row->height;
18639 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18641 /* Display the internal border below the tool-bar window. */
18642 if (WINDOWP (s->f->tool_bar_window)
18643 && s->w == XWINDOW (s->f->tool_bar_window))
18644 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18646 s->ybase = s->y + row->ascent;
18650 /* Append the list of glyph strings with head H and tail T to the list
18651 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18653 static INLINE void
18654 append_glyph_string_lists (head, tail, h, t)
18655 struct glyph_string **head, **tail;
18656 struct glyph_string *h, *t;
18658 if (h)
18660 if (*head)
18661 (*tail)->next = h;
18662 else
18663 *head = h;
18664 h->prev = *tail;
18665 *tail = t;
18670 /* Prepend the list of glyph strings with head H and tail T to the
18671 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18672 result. */
18674 static INLINE void
18675 prepend_glyph_string_lists (head, tail, h, t)
18676 struct glyph_string **head, **tail;
18677 struct glyph_string *h, *t;
18679 if (h)
18681 if (*head)
18682 (*head)->prev = t;
18683 else
18684 *tail = t;
18685 t->next = *head;
18686 *head = h;
18691 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18692 Set *HEAD and *TAIL to the resulting list. */
18694 static INLINE void
18695 append_glyph_string (head, tail, s)
18696 struct glyph_string **head, **tail;
18697 struct glyph_string *s;
18699 s->next = s->prev = NULL;
18700 append_glyph_string_lists (head, tail, s, s);
18704 /* Get face and two-byte form of character glyph GLYPH on frame F.
18705 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18706 a pointer to a realized face that is ready for display. */
18708 static INLINE struct face *
18709 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18710 struct frame *f;
18711 struct glyph *glyph;
18712 XChar2b *char2b;
18713 int *two_byte_p;
18715 struct face *face;
18717 xassert (glyph->type == CHAR_GLYPH);
18718 face = FACE_FROM_ID (f, glyph->face_id);
18720 if (two_byte_p)
18721 *two_byte_p = 0;
18723 if (!glyph->multibyte_p)
18725 /* Unibyte case. We don't have to encode, but we have to make
18726 sure to use a face suitable for unibyte. */
18727 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18729 else if (glyph->u.ch < 128)
18731 /* Case of ASCII in a face known to fit ASCII. */
18732 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18734 else
18736 int c1, c2, charset;
18738 /* Split characters into bytes. If c2 is -1 afterwards, C is
18739 really a one-byte character so that byte1 is zero. */
18740 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18741 if (c2 > 0)
18742 STORE_XCHAR2B (char2b, c1, c2);
18743 else
18744 STORE_XCHAR2B (char2b, 0, c1);
18746 /* Maybe encode the character in *CHAR2B. */
18747 if (charset != CHARSET_ASCII)
18749 struct font_info *font_info
18750 = FONT_INFO_FROM_ID (f, face->font_info_id);
18751 if (font_info)
18752 glyph->font_type
18753 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18757 /* Make sure X resources of the face are allocated. */
18758 xassert (face != NULL);
18759 PREPARE_FACE_FOR_DISPLAY (f, face);
18760 return face;
18764 /* Fill glyph string S with composition components specified by S->cmp.
18766 FACES is an array of faces for all components of this composition.
18767 S->gidx is the index of the first component for S.
18769 OVERLAPS non-zero means S should draw the foreground only, and use
18770 its physical height for clipping. See also draw_glyphs.
18772 Value is the index of a component not in S. */
18774 static int
18775 fill_composite_glyph_string (s, faces, overlaps)
18776 struct glyph_string *s;
18777 struct face **faces;
18778 int overlaps;
18780 int i;
18782 xassert (s);
18784 s->for_overlaps = overlaps;
18786 s->face = faces[s->gidx];
18787 s->font = s->face->font;
18788 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18790 /* For all glyphs of this composition, starting at the offset
18791 S->gidx, until we reach the end of the definition or encounter a
18792 glyph that requires the different face, add it to S. */
18793 ++s->nchars;
18794 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18795 ++s->nchars;
18797 /* All glyph strings for the same composition has the same width,
18798 i.e. the width set for the first component of the composition. */
18800 s->width = s->first_glyph->pixel_width;
18802 /* If the specified font could not be loaded, use the frame's
18803 default font, but record the fact that we couldn't load it in
18804 the glyph string so that we can draw rectangles for the
18805 characters of the glyph string. */
18806 if (s->font == NULL)
18808 s->font_not_found_p = 1;
18809 s->font = FRAME_FONT (s->f);
18812 /* Adjust base line for subscript/superscript text. */
18813 s->ybase += s->first_glyph->voffset;
18815 xassert (s->face && s->face->gc);
18817 /* This glyph string must always be drawn with 16-bit functions. */
18818 s->two_byte_p = 1;
18820 return s->gidx + s->nchars;
18824 /* Fill glyph string S from a sequence of character glyphs.
18826 FACE_ID is the face id of the string. START is the index of the
18827 first glyph to consider, END is the index of the last + 1.
18828 OVERLAPS non-zero means S should draw the foreground only, and use
18829 its physical height for clipping. See also draw_glyphs.
18831 Value is the index of the first glyph not in S. */
18833 static int
18834 fill_glyph_string (s, face_id, start, end, overlaps)
18835 struct glyph_string *s;
18836 int face_id;
18837 int start, end, overlaps;
18839 struct glyph *glyph, *last;
18840 int voffset;
18841 int glyph_not_available_p;
18843 xassert (s->f == XFRAME (s->w->frame));
18844 xassert (s->nchars == 0);
18845 xassert (start >= 0 && end > start);
18847 s->for_overlaps = overlaps,
18848 glyph = s->row->glyphs[s->area] + start;
18849 last = s->row->glyphs[s->area] + end;
18850 voffset = glyph->voffset;
18852 glyph_not_available_p = glyph->glyph_not_available_p;
18854 while (glyph < last
18855 && glyph->type == CHAR_GLYPH
18856 && glyph->voffset == voffset
18857 /* Same face id implies same font, nowadays. */
18858 && glyph->face_id == face_id
18859 && glyph->glyph_not_available_p == glyph_not_available_p)
18861 int two_byte_p;
18863 s->face = get_glyph_face_and_encoding (s->f, glyph,
18864 s->char2b + s->nchars,
18865 &two_byte_p);
18866 s->two_byte_p = two_byte_p;
18867 ++s->nchars;
18868 xassert (s->nchars <= end - start);
18869 s->width += glyph->pixel_width;
18870 ++glyph;
18873 s->font = s->face->font;
18874 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18876 /* If the specified font could not be loaded, use the frame's font,
18877 but record the fact that we couldn't load it in
18878 S->font_not_found_p so that we can draw rectangles for the
18879 characters of the glyph string. */
18880 if (s->font == NULL || glyph_not_available_p)
18882 s->font_not_found_p = 1;
18883 s->font = FRAME_FONT (s->f);
18886 /* Adjust base line for subscript/superscript text. */
18887 s->ybase += voffset;
18889 xassert (s->face && s->face->gc);
18890 return glyph - s->row->glyphs[s->area];
18894 /* Fill glyph string S from image glyph S->first_glyph. */
18896 static void
18897 fill_image_glyph_string (s)
18898 struct glyph_string *s;
18900 xassert (s->first_glyph->type == IMAGE_GLYPH);
18901 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18902 xassert (s->img);
18903 s->slice = s->first_glyph->slice;
18904 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18905 s->font = s->face->font;
18906 s->width = s->first_glyph->pixel_width;
18908 /* Adjust base line for subscript/superscript text. */
18909 s->ybase += s->first_glyph->voffset;
18913 /* Fill glyph string S from a sequence of stretch glyphs.
18915 ROW is the glyph row in which the glyphs are found, AREA is the
18916 area within the row. START is the index of the first glyph to
18917 consider, END is the index of the last + 1.
18919 Value is the index of the first glyph not in S. */
18921 static int
18922 fill_stretch_glyph_string (s, row, area, start, end)
18923 struct glyph_string *s;
18924 struct glyph_row *row;
18925 enum glyph_row_area area;
18926 int start, end;
18928 struct glyph *glyph, *last;
18929 int voffset, face_id;
18931 xassert (s->first_glyph->type == STRETCH_GLYPH);
18933 glyph = s->row->glyphs[s->area] + start;
18934 last = s->row->glyphs[s->area] + end;
18935 face_id = glyph->face_id;
18936 s->face = FACE_FROM_ID (s->f, face_id);
18937 s->font = s->face->font;
18938 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18939 s->width = glyph->pixel_width;
18940 s->nchars = 1;
18941 voffset = glyph->voffset;
18943 for (++glyph;
18944 (glyph < last
18945 && glyph->type == STRETCH_GLYPH
18946 && glyph->voffset == voffset
18947 && glyph->face_id == face_id);
18948 ++glyph)
18949 s->width += glyph->pixel_width;
18951 /* Adjust base line for subscript/superscript text. */
18952 s->ybase += voffset;
18954 /* The case that face->gc == 0 is handled when drawing the glyph
18955 string by calling PREPARE_FACE_FOR_DISPLAY. */
18956 xassert (s->face);
18957 return glyph - s->row->glyphs[s->area];
18961 /* EXPORT for RIF:
18962 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18963 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18964 assumed to be zero. */
18966 void
18967 x_get_glyph_overhangs (glyph, f, left, right)
18968 struct glyph *glyph;
18969 struct frame *f;
18970 int *left, *right;
18972 *left = *right = 0;
18974 if (glyph->type == CHAR_GLYPH)
18976 XFontStruct *font;
18977 struct face *face;
18978 struct font_info *font_info;
18979 XChar2b char2b;
18980 XCharStruct *pcm;
18982 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18983 font = face->font;
18984 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18985 if (font /* ++KFS: Should this be font_info ? */
18986 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18988 if (pcm->rbearing > pcm->width)
18989 *right = pcm->rbearing - pcm->width;
18990 if (pcm->lbearing < 0)
18991 *left = -pcm->lbearing;
18997 /* Return the index of the first glyph preceding glyph string S that
18998 is overwritten by S because of S's left overhang. Value is -1
18999 if no glyphs are overwritten. */
19001 static int
19002 left_overwritten (s)
19003 struct glyph_string *s;
19005 int k;
19007 if (s->left_overhang)
19009 int x = 0, i;
19010 struct glyph *glyphs = s->row->glyphs[s->area];
19011 int first = s->first_glyph - glyphs;
19013 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19014 x -= glyphs[i].pixel_width;
19016 k = i + 1;
19018 else
19019 k = -1;
19021 return k;
19025 /* Return the index of the first glyph preceding glyph string S that
19026 is overwriting S because of its right overhang. Value is -1 if no
19027 glyph in front of S overwrites S. */
19029 static int
19030 left_overwriting (s)
19031 struct glyph_string *s;
19033 int i, k, x;
19034 struct glyph *glyphs = s->row->glyphs[s->area];
19035 int first = s->first_glyph - glyphs;
19037 k = -1;
19038 x = 0;
19039 for (i = first - 1; i >= 0; --i)
19041 int left, right;
19042 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19043 if (x + right > 0)
19044 k = i;
19045 x -= glyphs[i].pixel_width;
19048 return k;
19052 /* Return the index of the last glyph following glyph string S that is
19053 not overwritten by S because of S's right overhang. Value is -1 if
19054 no such glyph is found. */
19056 static int
19057 right_overwritten (s)
19058 struct glyph_string *s;
19060 int k = -1;
19062 if (s->right_overhang)
19064 int x = 0, i;
19065 struct glyph *glyphs = s->row->glyphs[s->area];
19066 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19067 int end = s->row->used[s->area];
19069 for (i = first; i < end && s->right_overhang > x; ++i)
19070 x += glyphs[i].pixel_width;
19072 k = i;
19075 return k;
19079 /* Return the index of the last glyph following glyph string S that
19080 overwrites S because of its left overhang. Value is negative
19081 if no such glyph is found. */
19083 static int
19084 right_overwriting (s)
19085 struct glyph_string *s;
19087 int i, k, x;
19088 int end = s->row->used[s->area];
19089 struct glyph *glyphs = s->row->glyphs[s->area];
19090 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19092 k = -1;
19093 x = 0;
19094 for (i = first; i < end; ++i)
19096 int left, right;
19097 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19098 if (x - left < 0)
19099 k = i;
19100 x += glyphs[i].pixel_width;
19103 return k;
19107 /* Get face and two-byte form of character C in face FACE_ID on frame
19108 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19109 means we want to display multibyte text. DISPLAY_P non-zero means
19110 make sure that X resources for the face returned are allocated.
19111 Value is a pointer to a realized face that is ready for display if
19112 DISPLAY_P is non-zero. */
19114 static INLINE struct face *
19115 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19116 struct frame *f;
19117 int c, face_id;
19118 XChar2b *char2b;
19119 int multibyte_p, display_p;
19121 struct face *face = FACE_FROM_ID (f, face_id);
19123 if (!multibyte_p)
19125 /* Unibyte case. We don't have to encode, but we have to make
19126 sure to use a face suitable for unibyte. */
19127 STORE_XCHAR2B (char2b, 0, c);
19128 face_id = FACE_FOR_CHAR (f, face, c);
19129 face = FACE_FROM_ID (f, face_id);
19131 else if (c < 128)
19133 /* Case of ASCII in a face known to fit ASCII. */
19134 STORE_XCHAR2B (char2b, 0, c);
19136 else
19138 int c1, c2, charset;
19140 /* Split characters into bytes. If c2 is -1 afterwards, C is
19141 really a one-byte character so that byte1 is zero. */
19142 SPLIT_CHAR (c, charset, c1, c2);
19143 if (c2 > 0)
19144 STORE_XCHAR2B (char2b, c1, c2);
19145 else
19146 STORE_XCHAR2B (char2b, 0, c1);
19148 /* Maybe encode the character in *CHAR2B. */
19149 if (face->font != NULL)
19151 struct font_info *font_info
19152 = FONT_INFO_FROM_ID (f, face->font_info_id);
19153 if (font_info)
19154 rif->encode_char (c, char2b, font_info, 0);
19158 /* Make sure X resources of the face are allocated. */
19159 #ifdef HAVE_X_WINDOWS
19160 if (display_p)
19161 #endif
19163 xassert (face != NULL);
19164 PREPARE_FACE_FOR_DISPLAY (f, face);
19167 return face;
19171 /* Set background width of glyph string S. START is the index of the
19172 first glyph following S. LAST_X is the right-most x-position + 1
19173 in the drawing area. */
19175 static INLINE void
19176 set_glyph_string_background_width (s, start, last_x)
19177 struct glyph_string *s;
19178 int start;
19179 int last_x;
19181 /* If the face of this glyph string has to be drawn to the end of
19182 the drawing area, set S->extends_to_end_of_line_p. */
19184 if (start == s->row->used[s->area]
19185 && s->area == TEXT_AREA
19186 && ((s->row->fill_line_p
19187 && (s->hl == DRAW_NORMAL_TEXT
19188 || s->hl == DRAW_IMAGE_RAISED
19189 || s->hl == DRAW_IMAGE_SUNKEN))
19190 || s->hl == DRAW_MOUSE_FACE))
19191 s->extends_to_end_of_line_p = 1;
19193 /* If S extends its face to the end of the line, set its
19194 background_width to the distance to the right edge of the drawing
19195 area. */
19196 if (s->extends_to_end_of_line_p)
19197 s->background_width = last_x - s->x + 1;
19198 else
19199 s->background_width = s->width;
19203 /* Compute overhangs and x-positions for glyph string S and its
19204 predecessors, or successors. X is the starting x-position for S.
19205 BACKWARD_P non-zero means process predecessors. */
19207 static void
19208 compute_overhangs_and_x (s, x, backward_p)
19209 struct glyph_string *s;
19210 int x;
19211 int backward_p;
19213 if (backward_p)
19215 while (s)
19217 if (rif->compute_glyph_string_overhangs)
19218 rif->compute_glyph_string_overhangs (s);
19219 x -= s->width;
19220 s->x = x;
19221 s = s->prev;
19224 else
19226 while (s)
19228 if (rif->compute_glyph_string_overhangs)
19229 rif->compute_glyph_string_overhangs (s);
19230 s->x = x;
19231 x += s->width;
19232 s = s->next;
19239 /* The following macros are only called from draw_glyphs below.
19240 They reference the following parameters of that function directly:
19241 `w', `row', `area', and `overlap_p'
19242 as well as the following local variables:
19243 `s', `f', and `hdc' (in W32) */
19245 #ifdef HAVE_NTGUI
19246 /* On W32, silently add local `hdc' variable to argument list of
19247 init_glyph_string. */
19248 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19249 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19250 #else
19251 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19252 init_glyph_string (s, char2b, w, row, area, start, hl)
19253 #endif
19255 /* Add a glyph string for a stretch glyph to the list of strings
19256 between HEAD and TAIL. START is the index of the stretch glyph in
19257 row area AREA of glyph row ROW. END is the index of the last glyph
19258 in that glyph row area. X is the current output position assigned
19259 to the new glyph string constructed. HL overrides that face of the
19260 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19261 is the right-most x-position of the drawing area. */
19263 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19264 and below -- keep them on one line. */
19265 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19266 do \
19268 s = (struct glyph_string *) alloca (sizeof *s); \
19269 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19270 START = fill_stretch_glyph_string (s, row, area, START, END); \
19271 append_glyph_string (&HEAD, &TAIL, s); \
19272 s->x = (X); \
19274 while (0)
19277 /* Add a glyph string for an image glyph to the list of strings
19278 between HEAD and TAIL. START is the index of the image glyph in
19279 row area AREA of glyph row ROW. END is the index of the last glyph
19280 in that glyph row area. X is the current output position assigned
19281 to the new glyph string constructed. HL overrides that face of the
19282 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19283 is the right-most x-position of the drawing area. */
19285 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19286 do \
19288 s = (struct glyph_string *) alloca (sizeof *s); \
19289 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19290 fill_image_glyph_string (s); \
19291 append_glyph_string (&HEAD, &TAIL, s); \
19292 ++START; \
19293 s->x = (X); \
19295 while (0)
19298 /* Add a glyph string for a sequence of character glyphs to the list
19299 of strings between HEAD and TAIL. START is the index of the first
19300 glyph in row area AREA of glyph row ROW that is part of the new
19301 glyph string. END is the index of the last glyph in that glyph row
19302 area. X is the current output position assigned to the new glyph
19303 string constructed. HL overrides that face of the glyph; e.g. it
19304 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19305 right-most x-position of the drawing area. */
19307 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19308 do \
19310 int c, face_id; \
19311 XChar2b *char2b; \
19313 c = (row)->glyphs[area][START].u.ch; \
19314 face_id = (row)->glyphs[area][START].face_id; \
19316 s = (struct glyph_string *) alloca (sizeof *s); \
19317 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19318 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19319 append_glyph_string (&HEAD, &TAIL, s); \
19320 s->x = (X); \
19321 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19323 while (0)
19326 /* Add a glyph string for a composite sequence to the list of strings
19327 between HEAD and TAIL. START is the index of the first glyph in
19328 row area AREA of glyph row ROW that is part of the new glyph
19329 string. END is the index of the last glyph in that glyph row area.
19330 X is the current output position assigned to the new glyph string
19331 constructed. HL overrides that face of the glyph; e.g. it is
19332 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19333 x-position of the drawing area. */
19335 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19336 do { \
19337 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19338 int face_id = (row)->glyphs[area][START].face_id; \
19339 struct face *base_face = FACE_FROM_ID (f, face_id); \
19340 struct composition *cmp = composition_table[cmp_id]; \
19341 int glyph_len = cmp->glyph_len; \
19342 XChar2b *char2b; \
19343 struct face **faces; \
19344 struct glyph_string *first_s = NULL; \
19345 int n; \
19347 base_face = base_face->ascii_face; \
19348 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19349 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19350 /* At first, fill in `char2b' and `faces'. */ \
19351 for (n = 0; n < glyph_len; n++) \
19353 int c = COMPOSITION_GLYPH (cmp, n); \
19354 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19355 faces[n] = FACE_FROM_ID (f, this_face_id); \
19356 get_char_face_and_encoding (f, c, this_face_id, \
19357 char2b + n, 1, 1); \
19360 /* Make glyph_strings for each glyph sequence that is drawable by \
19361 the same face, and append them to HEAD/TAIL. */ \
19362 for (n = 0; n < cmp->glyph_len;) \
19364 s = (struct glyph_string *) alloca (sizeof *s); \
19365 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19366 append_glyph_string (&(HEAD), &(TAIL), s); \
19367 s->cmp = cmp; \
19368 s->gidx = n; \
19369 s->x = (X); \
19371 if (n == 0) \
19372 first_s = s; \
19374 n = fill_composite_glyph_string (s, faces, overlaps); \
19377 ++START; \
19378 s = first_s; \
19379 } while (0)
19382 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19383 of AREA of glyph row ROW on window W between indices START and END.
19384 HL overrides the face for drawing glyph strings, e.g. it is
19385 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19386 x-positions of the drawing area.
19388 This is an ugly monster macro construct because we must use alloca
19389 to allocate glyph strings (because draw_glyphs can be called
19390 asynchronously). */
19392 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19393 do \
19395 HEAD = TAIL = NULL; \
19396 while (START < END) \
19398 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19399 switch (first_glyph->type) \
19401 case CHAR_GLYPH: \
19402 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19403 HL, X, LAST_X); \
19404 break; \
19406 case COMPOSITE_GLYPH: \
19407 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19408 HL, X, LAST_X); \
19409 break; \
19411 case STRETCH_GLYPH: \
19412 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19413 HL, X, LAST_X); \
19414 break; \
19416 case IMAGE_GLYPH: \
19417 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19418 HL, X, LAST_X); \
19419 break; \
19421 default: \
19422 abort (); \
19425 set_glyph_string_background_width (s, START, LAST_X); \
19426 (X) += s->width; \
19429 while (0)
19432 /* Draw glyphs between START and END in AREA of ROW on window W,
19433 starting at x-position X. X is relative to AREA in W. HL is a
19434 face-override with the following meaning:
19436 DRAW_NORMAL_TEXT draw normally
19437 DRAW_CURSOR draw in cursor face
19438 DRAW_MOUSE_FACE draw in mouse face.
19439 DRAW_INVERSE_VIDEO draw in mode line face
19440 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19441 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19443 If OVERLAPS is non-zero, draw only the foreground of characters and
19444 clip to the physical height of ROW. Non-zero value also defines
19445 the overlapping part to be drawn:
19447 OVERLAPS_PRED overlap with preceding rows
19448 OVERLAPS_SUCC overlap with succeeding rows
19449 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19450 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19452 Value is the x-position reached, relative to AREA of W. */
19454 static int
19455 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19456 struct window *w;
19457 int x;
19458 struct glyph_row *row;
19459 enum glyph_row_area area;
19460 int start, end;
19461 enum draw_glyphs_face hl;
19462 int overlaps;
19464 struct glyph_string *head, *tail;
19465 struct glyph_string *s;
19466 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19467 int last_x, area_width;
19468 int x_reached;
19469 int i, j;
19470 struct frame *f = XFRAME (WINDOW_FRAME (w));
19471 DECLARE_HDC (hdc);
19473 ALLOCATE_HDC (hdc, f);
19475 /* Let's rather be paranoid than getting a SEGV. */
19476 end = min (end, row->used[area]);
19477 start = max (0, start);
19478 start = min (end, start);
19480 /* Translate X to frame coordinates. Set last_x to the right
19481 end of the drawing area. */
19482 if (row->full_width_p)
19484 /* X is relative to the left edge of W, without scroll bars
19485 or fringes. */
19486 x += WINDOW_LEFT_EDGE_X (w);
19487 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19489 else
19491 int area_left = window_box_left (w, area);
19492 x += area_left;
19493 area_width = window_box_width (w, area);
19494 last_x = area_left + area_width;
19497 /* Build a doubly-linked list of glyph_string structures between
19498 head and tail from what we have to draw. Note that the macro
19499 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19500 the reason we use a separate variable `i'. */
19501 i = start;
19502 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19503 if (tail)
19504 x_reached = tail->x + tail->background_width;
19505 else
19506 x_reached = x;
19508 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19509 the row, redraw some glyphs in front or following the glyph
19510 strings built above. */
19511 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19513 int dummy_x = 0;
19514 struct glyph_string *h, *t;
19516 /* Compute overhangs for all glyph strings. */
19517 if (rif->compute_glyph_string_overhangs)
19518 for (s = head; s; s = s->next)
19519 rif->compute_glyph_string_overhangs (s);
19521 /* Prepend glyph strings for glyphs in front of the first glyph
19522 string that are overwritten because of the first glyph
19523 string's left overhang. The background of all strings
19524 prepended must be drawn because the first glyph string
19525 draws over it. */
19526 i = left_overwritten (head);
19527 if (i >= 0)
19529 j = i;
19530 BUILD_GLYPH_STRINGS (j, start, h, t,
19531 DRAW_NORMAL_TEXT, dummy_x, last_x);
19532 start = i;
19533 compute_overhangs_and_x (t, head->x, 1);
19534 prepend_glyph_string_lists (&head, &tail, h, t);
19535 clip_head = head;
19538 /* Prepend glyph strings for glyphs in front of the first glyph
19539 string that overwrite that glyph string because of their
19540 right overhang. For these strings, only the foreground must
19541 be drawn, because it draws over the glyph string at `head'.
19542 The background must not be drawn because this would overwrite
19543 right overhangs of preceding glyphs for which no glyph
19544 strings exist. */
19545 i = left_overwriting (head);
19546 if (i >= 0)
19548 clip_head = head;
19549 BUILD_GLYPH_STRINGS (i, start, h, t,
19550 DRAW_NORMAL_TEXT, dummy_x, last_x);
19551 for (s = h; s; s = s->next)
19552 s->background_filled_p = 1;
19553 compute_overhangs_and_x (t, head->x, 1);
19554 prepend_glyph_string_lists (&head, &tail, h, t);
19557 /* Append glyphs strings for glyphs following the last glyph
19558 string tail that are overwritten by tail. The background of
19559 these strings has to be drawn because tail's foreground draws
19560 over it. */
19561 i = right_overwritten (tail);
19562 if (i >= 0)
19564 BUILD_GLYPH_STRINGS (end, i, h, t,
19565 DRAW_NORMAL_TEXT, x, last_x);
19566 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19567 append_glyph_string_lists (&head, &tail, h, t);
19568 clip_tail = tail;
19571 /* Append glyph strings for glyphs following the last glyph
19572 string tail that overwrite tail. The foreground of such
19573 glyphs has to be drawn because it writes into the background
19574 of tail. The background must not be drawn because it could
19575 paint over the foreground of following glyphs. */
19576 i = right_overwriting (tail);
19577 if (i >= 0)
19579 clip_tail = tail;
19580 BUILD_GLYPH_STRINGS (end, i, h, t,
19581 DRAW_NORMAL_TEXT, x, last_x);
19582 for (s = h; s; s = s->next)
19583 s->background_filled_p = 1;
19584 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19585 append_glyph_string_lists (&head, &tail, h, t);
19587 if (clip_head || clip_tail)
19588 for (s = head; s; s = s->next)
19590 s->clip_head = clip_head;
19591 s->clip_tail = clip_tail;
19595 /* Draw all strings. */
19596 for (s = head; s; s = s->next)
19597 rif->draw_glyph_string (s);
19599 if (area == TEXT_AREA
19600 && !row->full_width_p
19601 /* When drawing overlapping rows, only the glyph strings'
19602 foreground is drawn, which doesn't erase a cursor
19603 completely. */
19604 && !overlaps)
19606 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19607 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19608 : (tail ? tail->x + tail->background_width : x));
19610 int text_left = window_box_left (w, TEXT_AREA);
19611 x0 -= text_left;
19612 x1 -= text_left;
19614 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19615 row->y, MATRIX_ROW_BOTTOM_Y (row));
19618 /* Value is the x-position up to which drawn, relative to AREA of W.
19619 This doesn't include parts drawn because of overhangs. */
19620 if (row->full_width_p)
19621 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19622 else
19623 x_reached -= window_box_left (w, area);
19625 RELEASE_HDC (hdc, f);
19627 return x_reached;
19630 /* Expand row matrix if too narrow. Don't expand if area
19631 is not present. */
19633 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19635 if (!fonts_changed_p \
19636 && (it->glyph_row->glyphs[area] \
19637 < it->glyph_row->glyphs[area + 1])) \
19639 it->w->ncols_scale_factor++; \
19640 fonts_changed_p = 1; \
19644 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19645 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19647 static INLINE void
19648 append_glyph (it)
19649 struct it *it;
19651 struct glyph *glyph;
19652 enum glyph_row_area area = it->area;
19654 xassert (it->glyph_row);
19655 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19657 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19658 if (glyph < it->glyph_row->glyphs[area + 1])
19660 glyph->charpos = CHARPOS (it->position);
19661 glyph->object = it->object;
19662 glyph->pixel_width = it->pixel_width;
19663 glyph->ascent = it->ascent;
19664 glyph->descent = it->descent;
19665 glyph->voffset = it->voffset;
19666 glyph->type = CHAR_GLYPH;
19667 glyph->multibyte_p = it->multibyte_p;
19668 glyph->left_box_line_p = it->start_of_box_run_p;
19669 glyph->right_box_line_p = it->end_of_box_run_p;
19670 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19671 || it->phys_descent > it->descent);
19672 glyph->padding_p = 0;
19673 glyph->glyph_not_available_p = it->glyph_not_available_p;
19674 glyph->face_id = it->face_id;
19675 glyph->u.ch = it->char_to_display;
19676 glyph->slice = null_glyph_slice;
19677 glyph->font_type = FONT_TYPE_UNKNOWN;
19678 ++it->glyph_row->used[area];
19680 else
19681 IT_EXPAND_MATRIX_WIDTH (it, area);
19684 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19685 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19687 static INLINE void
19688 append_composite_glyph (it)
19689 struct it *it;
19691 struct glyph *glyph;
19692 enum glyph_row_area area = it->area;
19694 xassert (it->glyph_row);
19696 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19697 if (glyph < it->glyph_row->glyphs[area + 1])
19699 glyph->charpos = CHARPOS (it->position);
19700 glyph->object = it->object;
19701 glyph->pixel_width = it->pixel_width;
19702 glyph->ascent = it->ascent;
19703 glyph->descent = it->descent;
19704 glyph->voffset = it->voffset;
19705 glyph->type = COMPOSITE_GLYPH;
19706 glyph->multibyte_p = it->multibyte_p;
19707 glyph->left_box_line_p = it->start_of_box_run_p;
19708 glyph->right_box_line_p = it->end_of_box_run_p;
19709 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19710 || it->phys_descent > it->descent);
19711 glyph->padding_p = 0;
19712 glyph->glyph_not_available_p = 0;
19713 glyph->face_id = it->face_id;
19714 glyph->u.cmp_id = it->cmp_id;
19715 glyph->slice = null_glyph_slice;
19716 glyph->font_type = FONT_TYPE_UNKNOWN;
19717 ++it->glyph_row->used[area];
19719 else
19720 IT_EXPAND_MATRIX_WIDTH (it, area);
19724 /* Change IT->ascent and IT->height according to the setting of
19725 IT->voffset. */
19727 static INLINE void
19728 take_vertical_position_into_account (it)
19729 struct it *it;
19731 if (it->voffset)
19733 if (it->voffset < 0)
19734 /* Increase the ascent so that we can display the text higher
19735 in the line. */
19736 it->ascent -= it->voffset;
19737 else
19738 /* Increase the descent so that we can display the text lower
19739 in the line. */
19740 it->descent += it->voffset;
19745 /* Produce glyphs/get display metrics for the image IT is loaded with.
19746 See the description of struct display_iterator in dispextern.h for
19747 an overview of struct display_iterator. */
19749 static void
19750 produce_image_glyph (it)
19751 struct it *it;
19753 struct image *img;
19754 struct face *face;
19755 int glyph_ascent;
19756 struct glyph_slice slice;
19758 xassert (it->what == IT_IMAGE);
19760 face = FACE_FROM_ID (it->f, it->face_id);
19761 xassert (face);
19762 /* Make sure X resources of the face is loaded. */
19763 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19765 if (it->image_id < 0)
19767 /* Fringe bitmap. */
19768 it->ascent = it->phys_ascent = 0;
19769 it->descent = it->phys_descent = 0;
19770 it->pixel_width = 0;
19771 it->nglyphs = 0;
19772 return;
19775 img = IMAGE_FROM_ID (it->f, it->image_id);
19776 xassert (img);
19777 /* Make sure X resources of the image is loaded. */
19778 prepare_image_for_display (it->f, img);
19780 slice.x = slice.y = 0;
19781 slice.width = img->width;
19782 slice.height = img->height;
19784 if (INTEGERP (it->slice.x))
19785 slice.x = XINT (it->slice.x);
19786 else if (FLOATP (it->slice.x))
19787 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19789 if (INTEGERP (it->slice.y))
19790 slice.y = XINT (it->slice.y);
19791 else if (FLOATP (it->slice.y))
19792 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19794 if (INTEGERP (it->slice.width))
19795 slice.width = XINT (it->slice.width);
19796 else if (FLOATP (it->slice.width))
19797 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19799 if (INTEGERP (it->slice.height))
19800 slice.height = XINT (it->slice.height);
19801 else if (FLOATP (it->slice.height))
19802 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19804 if (slice.x >= img->width)
19805 slice.x = img->width;
19806 if (slice.y >= img->height)
19807 slice.y = img->height;
19808 if (slice.x + slice.width >= img->width)
19809 slice.width = img->width - slice.x;
19810 if (slice.y + slice.height > img->height)
19811 slice.height = img->height - slice.y;
19813 if (slice.width == 0 || slice.height == 0)
19814 return;
19816 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19818 it->descent = slice.height - glyph_ascent;
19819 if (slice.y == 0)
19820 it->descent += img->vmargin;
19821 if (slice.y + slice.height == img->height)
19822 it->descent += img->vmargin;
19823 it->phys_descent = it->descent;
19825 it->pixel_width = slice.width;
19826 if (slice.x == 0)
19827 it->pixel_width += img->hmargin;
19828 if (slice.x + slice.width == img->width)
19829 it->pixel_width += img->hmargin;
19831 /* It's quite possible for images to have an ascent greater than
19832 their height, so don't get confused in that case. */
19833 if (it->descent < 0)
19834 it->descent = 0;
19836 #if 0 /* this breaks image tiling */
19837 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19838 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19839 if (face_ascent > it->ascent)
19840 it->ascent = it->phys_ascent = face_ascent;
19841 #endif
19843 it->nglyphs = 1;
19845 if (face->box != FACE_NO_BOX)
19847 if (face->box_line_width > 0)
19849 if (slice.y == 0)
19850 it->ascent += face->box_line_width;
19851 if (slice.y + slice.height == img->height)
19852 it->descent += face->box_line_width;
19855 if (it->start_of_box_run_p && slice.x == 0)
19856 it->pixel_width += abs (face->box_line_width);
19857 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19858 it->pixel_width += abs (face->box_line_width);
19861 take_vertical_position_into_account (it);
19863 if (it->glyph_row)
19865 struct glyph *glyph;
19866 enum glyph_row_area area = it->area;
19868 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19869 if (glyph < it->glyph_row->glyphs[area + 1])
19871 glyph->charpos = CHARPOS (it->position);
19872 glyph->object = it->object;
19873 glyph->pixel_width = it->pixel_width;
19874 glyph->ascent = glyph_ascent;
19875 glyph->descent = it->descent;
19876 glyph->voffset = it->voffset;
19877 glyph->type = IMAGE_GLYPH;
19878 glyph->multibyte_p = it->multibyte_p;
19879 glyph->left_box_line_p = it->start_of_box_run_p;
19880 glyph->right_box_line_p = it->end_of_box_run_p;
19881 glyph->overlaps_vertically_p = 0;
19882 glyph->padding_p = 0;
19883 glyph->glyph_not_available_p = 0;
19884 glyph->face_id = it->face_id;
19885 glyph->u.img_id = img->id;
19886 glyph->slice = slice;
19887 glyph->font_type = FONT_TYPE_UNKNOWN;
19888 ++it->glyph_row->used[area];
19890 else
19891 IT_EXPAND_MATRIX_WIDTH (it, area);
19896 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19897 of the glyph, WIDTH and HEIGHT are the width and height of the
19898 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19900 static void
19901 append_stretch_glyph (it, object, width, height, ascent)
19902 struct it *it;
19903 Lisp_Object object;
19904 int width, height;
19905 int ascent;
19907 struct glyph *glyph;
19908 enum glyph_row_area area = it->area;
19910 xassert (ascent >= 0 && ascent <= height);
19912 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19913 if (glyph < it->glyph_row->glyphs[area + 1])
19915 glyph->charpos = CHARPOS (it->position);
19916 glyph->object = object;
19917 glyph->pixel_width = width;
19918 glyph->ascent = ascent;
19919 glyph->descent = height - ascent;
19920 glyph->voffset = it->voffset;
19921 glyph->type = STRETCH_GLYPH;
19922 glyph->multibyte_p = it->multibyte_p;
19923 glyph->left_box_line_p = it->start_of_box_run_p;
19924 glyph->right_box_line_p = it->end_of_box_run_p;
19925 glyph->overlaps_vertically_p = 0;
19926 glyph->padding_p = 0;
19927 glyph->glyph_not_available_p = 0;
19928 glyph->face_id = it->face_id;
19929 glyph->u.stretch.ascent = ascent;
19930 glyph->u.stretch.height = height;
19931 glyph->slice = null_glyph_slice;
19932 glyph->font_type = FONT_TYPE_UNKNOWN;
19933 ++it->glyph_row->used[area];
19935 else
19936 IT_EXPAND_MATRIX_WIDTH (it, area);
19940 /* Produce a stretch glyph for iterator IT. IT->object is the value
19941 of the glyph property displayed. The value must be a list
19942 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19943 being recognized:
19945 1. `:width WIDTH' specifies that the space should be WIDTH *
19946 canonical char width wide. WIDTH may be an integer or floating
19947 point number.
19949 2. `:relative-width FACTOR' specifies that the width of the stretch
19950 should be computed from the width of the first character having the
19951 `glyph' property, and should be FACTOR times that width.
19953 3. `:align-to HPOS' specifies that the space should be wide enough
19954 to reach HPOS, a value in canonical character units.
19956 Exactly one of the above pairs must be present.
19958 4. `:height HEIGHT' specifies that the height of the stretch produced
19959 should be HEIGHT, measured in canonical character units.
19961 5. `:relative-height FACTOR' specifies that the height of the
19962 stretch should be FACTOR times the height of the characters having
19963 the glyph property.
19965 Either none or exactly one of 4 or 5 must be present.
19967 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19968 of the stretch should be used for the ascent of the stretch.
19969 ASCENT must be in the range 0 <= ASCENT <= 100. */
19971 static void
19972 produce_stretch_glyph (it)
19973 struct it *it;
19975 /* (space :width WIDTH :height HEIGHT ...) */
19976 Lisp_Object prop, plist;
19977 int width = 0, height = 0, align_to = -1;
19978 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19979 int ascent = 0;
19980 double tem;
19981 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19982 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19984 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19986 /* List should start with `space'. */
19987 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19988 plist = XCDR (it->object);
19990 /* Compute the width of the stretch. */
19991 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19992 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19994 /* Absolute width `:width WIDTH' specified and valid. */
19995 zero_width_ok_p = 1;
19996 width = (int)tem;
19998 else if (prop = Fplist_get (plist, QCrelative_width),
19999 NUMVAL (prop) > 0)
20001 /* Relative width `:relative-width FACTOR' specified and valid.
20002 Compute the width of the characters having the `glyph'
20003 property. */
20004 struct it it2;
20005 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20007 it2 = *it;
20008 if (it->multibyte_p)
20010 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20011 - IT_BYTEPOS (*it));
20012 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20014 else
20015 it2.c = *p, it2.len = 1;
20017 it2.glyph_row = NULL;
20018 it2.what = IT_CHARACTER;
20019 x_produce_glyphs (&it2);
20020 width = NUMVAL (prop) * it2.pixel_width;
20022 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20023 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20025 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20026 align_to = (align_to < 0
20028 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20029 else if (align_to < 0)
20030 align_to = window_box_left_offset (it->w, TEXT_AREA);
20031 width = max (0, (int)tem + align_to - it->current_x);
20032 zero_width_ok_p = 1;
20034 else
20035 /* Nothing specified -> width defaults to canonical char width. */
20036 width = FRAME_COLUMN_WIDTH (it->f);
20038 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20039 width = 1;
20041 /* Compute height. */
20042 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20043 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20045 height = (int)tem;
20046 zero_height_ok_p = 1;
20048 else if (prop = Fplist_get (plist, QCrelative_height),
20049 NUMVAL (prop) > 0)
20050 height = FONT_HEIGHT (font) * NUMVAL (prop);
20051 else
20052 height = FONT_HEIGHT (font);
20054 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20055 height = 1;
20057 /* Compute percentage of height used for ascent. If
20058 `:ascent ASCENT' is present and valid, use that. Otherwise,
20059 derive the ascent from the font in use. */
20060 if (prop = Fplist_get (plist, QCascent),
20061 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20062 ascent = height * NUMVAL (prop) / 100.0;
20063 else if (!NILP (prop)
20064 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20065 ascent = min (max (0, (int)tem), height);
20066 else
20067 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20069 if (width > 0 && !it->truncate_lines_p
20070 && it->current_x + width > it->last_visible_x)
20071 width = it->last_visible_x - it->current_x - 1;
20073 if (width > 0 && height > 0 && it->glyph_row)
20075 Lisp_Object object = it->stack[it->sp - 1].string;
20076 if (!STRINGP (object))
20077 object = it->w->buffer;
20078 append_stretch_glyph (it, object, width, height, ascent);
20081 it->pixel_width = width;
20082 it->ascent = it->phys_ascent = ascent;
20083 it->descent = it->phys_descent = height - it->ascent;
20084 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20086 take_vertical_position_into_account (it);
20089 /* Get line-height and line-spacing property at point.
20090 If line-height has format (HEIGHT TOTAL), return TOTAL
20091 in TOTAL_HEIGHT. */
20093 static Lisp_Object
20094 get_line_height_property (it, prop)
20095 struct it *it;
20096 Lisp_Object prop;
20098 Lisp_Object position;
20100 if (STRINGP (it->object))
20101 position = make_number (IT_STRING_CHARPOS (*it));
20102 else if (BUFFERP (it->object))
20103 position = make_number (IT_CHARPOS (*it));
20104 else
20105 return Qnil;
20107 return Fget_char_property (position, prop, it->object);
20110 /* Calculate line-height and line-spacing properties.
20111 An integer value specifies explicit pixel value.
20112 A float value specifies relative value to current face height.
20113 A cons (float . face-name) specifies relative value to
20114 height of specified face font.
20116 Returns height in pixels, or nil. */
20119 static Lisp_Object
20120 calc_line_height_property (it, val, font, boff, override)
20121 struct it *it;
20122 Lisp_Object val;
20123 XFontStruct *font;
20124 int boff, override;
20126 Lisp_Object face_name = Qnil;
20127 int ascent, descent, height;
20129 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20130 return val;
20132 if (CONSP (val))
20134 face_name = XCAR (val);
20135 val = XCDR (val);
20136 if (!NUMBERP (val))
20137 val = make_number (1);
20138 if (NILP (face_name))
20140 height = it->ascent + it->descent;
20141 goto scale;
20145 if (NILP (face_name))
20147 font = FRAME_FONT (it->f);
20148 boff = FRAME_BASELINE_OFFSET (it->f);
20150 else if (EQ (face_name, Qt))
20152 override = 0;
20154 else
20156 int face_id;
20157 struct face *face;
20158 struct font_info *font_info;
20160 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20161 if (face_id < 0)
20162 return make_number (-1);
20164 face = FACE_FROM_ID (it->f, face_id);
20165 font = face->font;
20166 if (font == NULL)
20167 return make_number (-1);
20169 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20170 boff = font_info->baseline_offset;
20171 if (font_info->vertical_centering)
20172 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20175 ascent = FONT_BASE (font) + boff;
20176 descent = FONT_DESCENT (font) - boff;
20178 if (override)
20180 it->override_ascent = ascent;
20181 it->override_descent = descent;
20182 it->override_boff = boff;
20185 height = ascent + descent;
20187 scale:
20188 if (FLOATP (val))
20189 height = (int)(XFLOAT_DATA (val) * height);
20190 else if (INTEGERP (val))
20191 height *= XINT (val);
20193 return make_number (height);
20197 /* RIF:
20198 Produce glyphs/get display metrics for the display element IT is
20199 loaded with. See the description of struct it in dispextern.h
20200 for an overview of struct it. */
20202 void
20203 x_produce_glyphs (it)
20204 struct it *it;
20206 int extra_line_spacing = it->extra_line_spacing;
20208 it->glyph_not_available_p = 0;
20210 if (it->what == IT_CHARACTER)
20212 XChar2b char2b;
20213 XFontStruct *font;
20214 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20215 XCharStruct *pcm;
20216 int font_not_found_p;
20217 struct font_info *font_info;
20218 int boff; /* baseline offset */
20219 /* We may change it->multibyte_p upon unibyte<->multibyte
20220 conversion. So, save the current value now and restore it
20221 later.
20223 Note: It seems that we don't have to record multibyte_p in
20224 struct glyph because the character code itself tells if or
20225 not the character is multibyte. Thus, in the future, we must
20226 consider eliminating the field `multibyte_p' in the struct
20227 glyph. */
20228 int saved_multibyte_p = it->multibyte_p;
20230 /* Maybe translate single-byte characters to multibyte, or the
20231 other way. */
20232 it->char_to_display = it->c;
20233 if (!ASCII_BYTE_P (it->c))
20235 if (unibyte_display_via_language_environment
20236 && SINGLE_BYTE_CHAR_P (it->c)
20237 && (it->c >= 0240
20238 || !NILP (Vnonascii_translation_table)))
20240 it->char_to_display = unibyte_char_to_multibyte (it->c);
20241 it->multibyte_p = 1;
20242 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20243 face = FACE_FROM_ID (it->f, it->face_id);
20245 else if (!SINGLE_BYTE_CHAR_P (it->c)
20246 && !it->multibyte_p)
20248 it->multibyte_p = 1;
20249 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20250 face = FACE_FROM_ID (it->f, it->face_id);
20254 /* Get font to use. Encode IT->char_to_display. */
20255 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20256 &char2b, it->multibyte_p, 0);
20257 font = face->font;
20259 /* When no suitable font found, use the default font. */
20260 font_not_found_p = font == NULL;
20261 if (font_not_found_p)
20263 font = FRAME_FONT (it->f);
20264 boff = FRAME_BASELINE_OFFSET (it->f);
20265 font_info = NULL;
20267 else
20269 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20270 boff = font_info->baseline_offset;
20271 if (font_info->vertical_centering)
20272 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20275 if (it->char_to_display >= ' '
20276 && (!it->multibyte_p || it->char_to_display < 128))
20278 /* Either unibyte or ASCII. */
20279 int stretched_p;
20281 it->nglyphs = 1;
20283 pcm = rif->per_char_metric (font, &char2b,
20284 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20286 if (it->override_ascent >= 0)
20288 it->ascent = it->override_ascent;
20289 it->descent = it->override_descent;
20290 boff = it->override_boff;
20292 else
20294 it->ascent = FONT_BASE (font) + boff;
20295 it->descent = FONT_DESCENT (font) - boff;
20298 if (pcm)
20300 it->phys_ascent = pcm->ascent + boff;
20301 it->phys_descent = pcm->descent - boff;
20302 it->pixel_width = pcm->width;
20304 else
20306 it->glyph_not_available_p = 1;
20307 it->phys_ascent = it->ascent;
20308 it->phys_descent = it->descent;
20309 it->pixel_width = FONT_WIDTH (font);
20312 if (it->constrain_row_ascent_descent_p)
20314 if (it->descent > it->max_descent)
20316 it->ascent += it->descent - it->max_descent;
20317 it->descent = it->max_descent;
20319 if (it->ascent > it->max_ascent)
20321 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20322 it->ascent = it->max_ascent;
20324 it->phys_ascent = min (it->phys_ascent, it->ascent);
20325 it->phys_descent = min (it->phys_descent, it->descent);
20326 extra_line_spacing = 0;
20329 /* If this is a space inside a region of text with
20330 `space-width' property, change its width. */
20331 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20332 if (stretched_p)
20333 it->pixel_width *= XFLOATINT (it->space_width);
20335 /* If face has a box, add the box thickness to the character
20336 height. If character has a box line to the left and/or
20337 right, add the box line width to the character's width. */
20338 if (face->box != FACE_NO_BOX)
20340 int thick = face->box_line_width;
20342 if (thick > 0)
20344 it->ascent += thick;
20345 it->descent += thick;
20347 else
20348 thick = -thick;
20350 if (it->start_of_box_run_p)
20351 it->pixel_width += thick;
20352 if (it->end_of_box_run_p)
20353 it->pixel_width += thick;
20356 /* If face has an overline, add the height of the overline
20357 (1 pixel) and a 1 pixel margin to the character height. */
20358 if (face->overline_p)
20359 it->ascent += 2;
20361 if (it->constrain_row_ascent_descent_p)
20363 if (it->ascent > it->max_ascent)
20364 it->ascent = it->max_ascent;
20365 if (it->descent > it->max_descent)
20366 it->descent = it->max_descent;
20369 take_vertical_position_into_account (it);
20371 /* If we have to actually produce glyphs, do it. */
20372 if (it->glyph_row)
20374 if (stretched_p)
20376 /* Translate a space with a `space-width' property
20377 into a stretch glyph. */
20378 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20379 / FONT_HEIGHT (font));
20380 append_stretch_glyph (it, it->object, it->pixel_width,
20381 it->ascent + it->descent, ascent);
20383 else
20384 append_glyph (it);
20386 /* If characters with lbearing or rbearing are displayed
20387 in this line, record that fact in a flag of the
20388 glyph row. This is used to optimize X output code. */
20389 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20390 it->glyph_row->contains_overlapping_glyphs_p = 1;
20393 else if (it->char_to_display == '\n')
20395 /* A newline has no width but we need the height of the line.
20396 But if previous part of the line set a height, don't
20397 increase that height */
20399 Lisp_Object height;
20400 Lisp_Object total_height = Qnil;
20402 it->override_ascent = -1;
20403 it->pixel_width = 0;
20404 it->nglyphs = 0;
20406 height = get_line_height_property(it, Qline_height);
20407 /* Split (line-height total-height) list */
20408 if (CONSP (height)
20409 && CONSP (XCDR (height))
20410 && NILP (XCDR (XCDR (height))))
20412 total_height = XCAR (XCDR (height));
20413 height = XCAR (height);
20415 height = calc_line_height_property(it, height, font, boff, 1);
20417 if (it->override_ascent >= 0)
20419 it->ascent = it->override_ascent;
20420 it->descent = it->override_descent;
20421 boff = it->override_boff;
20423 else
20425 it->ascent = FONT_BASE (font) + boff;
20426 it->descent = FONT_DESCENT (font) - boff;
20429 if (EQ (height, Qt))
20431 if (it->descent > it->max_descent)
20433 it->ascent += it->descent - it->max_descent;
20434 it->descent = it->max_descent;
20436 if (it->ascent > it->max_ascent)
20438 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20439 it->ascent = it->max_ascent;
20441 it->phys_ascent = min (it->phys_ascent, it->ascent);
20442 it->phys_descent = min (it->phys_descent, it->descent);
20443 it->constrain_row_ascent_descent_p = 1;
20444 extra_line_spacing = 0;
20446 else
20448 Lisp_Object spacing;
20450 it->phys_ascent = it->ascent;
20451 it->phys_descent = it->descent;
20453 if ((it->max_ascent > 0 || it->max_descent > 0)
20454 && face->box != FACE_NO_BOX
20455 && face->box_line_width > 0)
20457 it->ascent += face->box_line_width;
20458 it->descent += face->box_line_width;
20460 if (!NILP (height)
20461 && XINT (height) > it->ascent + it->descent)
20462 it->ascent = XINT (height) - it->descent;
20464 if (!NILP (total_height))
20465 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20466 else
20468 spacing = get_line_height_property(it, Qline_spacing);
20469 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20471 if (INTEGERP (spacing))
20473 extra_line_spacing = XINT (spacing);
20474 if (!NILP (total_height))
20475 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20479 else if (it->char_to_display == '\t')
20481 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20482 int x = it->current_x + it->continuation_lines_width;
20483 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20485 /* If the distance from the current position to the next tab
20486 stop is less than a space character width, use the
20487 tab stop after that. */
20488 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20489 next_tab_x += tab_width;
20491 it->pixel_width = next_tab_x - x;
20492 it->nglyphs = 1;
20493 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20494 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20496 if (it->glyph_row)
20498 append_stretch_glyph (it, it->object, it->pixel_width,
20499 it->ascent + it->descent, it->ascent);
20502 else
20504 /* A multi-byte character. Assume that the display width of the
20505 character is the width of the character multiplied by the
20506 width of the font. */
20508 /* If we found a font, this font should give us the right
20509 metrics. If we didn't find a font, use the frame's
20510 default font and calculate the width of the character
20511 from the charset width; this is what old redisplay code
20512 did. */
20514 pcm = rif->per_char_metric (font, &char2b,
20515 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20517 if (font_not_found_p || !pcm)
20519 int charset = CHAR_CHARSET (it->char_to_display);
20521 it->glyph_not_available_p = 1;
20522 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20523 * CHARSET_WIDTH (charset));
20524 it->phys_ascent = FONT_BASE (font) + boff;
20525 it->phys_descent = FONT_DESCENT (font) - boff;
20527 else
20529 it->pixel_width = pcm->width;
20530 it->phys_ascent = pcm->ascent + boff;
20531 it->phys_descent = pcm->descent - boff;
20532 if (it->glyph_row
20533 && (pcm->lbearing < 0
20534 || pcm->rbearing > pcm->width))
20535 it->glyph_row->contains_overlapping_glyphs_p = 1;
20537 it->nglyphs = 1;
20538 it->ascent = FONT_BASE (font) + boff;
20539 it->descent = FONT_DESCENT (font) - boff;
20540 if (face->box != FACE_NO_BOX)
20542 int thick = face->box_line_width;
20544 if (thick > 0)
20546 it->ascent += thick;
20547 it->descent += thick;
20549 else
20550 thick = - thick;
20552 if (it->start_of_box_run_p)
20553 it->pixel_width += thick;
20554 if (it->end_of_box_run_p)
20555 it->pixel_width += thick;
20558 /* If face has an overline, add the height of the overline
20559 (1 pixel) and a 1 pixel margin to the character height. */
20560 if (face->overline_p)
20561 it->ascent += 2;
20563 take_vertical_position_into_account (it);
20565 if (it->glyph_row)
20566 append_glyph (it);
20568 it->multibyte_p = saved_multibyte_p;
20570 else if (it->what == IT_COMPOSITION)
20572 /* Note: A composition is represented as one glyph in the
20573 glyph matrix. There are no padding glyphs. */
20574 XChar2b char2b;
20575 XFontStruct *font;
20576 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20577 XCharStruct *pcm;
20578 int font_not_found_p;
20579 struct font_info *font_info;
20580 int boff; /* baseline offset */
20581 struct composition *cmp = composition_table[it->cmp_id];
20583 /* Maybe translate single-byte characters to multibyte. */
20584 it->char_to_display = it->c;
20585 if (unibyte_display_via_language_environment
20586 && SINGLE_BYTE_CHAR_P (it->c)
20587 && (it->c >= 0240
20588 || (it->c >= 0200
20589 && !NILP (Vnonascii_translation_table))))
20591 it->char_to_display = unibyte_char_to_multibyte (it->c);
20594 /* Get face and font to use. Encode IT->char_to_display. */
20595 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20596 face = FACE_FROM_ID (it->f, it->face_id);
20597 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20598 &char2b, it->multibyte_p, 0);
20599 font = face->font;
20601 /* When no suitable font found, use the default font. */
20602 font_not_found_p = font == NULL;
20603 if (font_not_found_p)
20605 font = FRAME_FONT (it->f);
20606 boff = FRAME_BASELINE_OFFSET (it->f);
20607 font_info = NULL;
20609 else
20611 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20612 boff = font_info->baseline_offset;
20613 if (font_info->vertical_centering)
20614 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20617 /* There are no padding glyphs, so there is only one glyph to
20618 produce for the composition. Important is that pixel_width,
20619 ascent and descent are the values of what is drawn by
20620 draw_glyphs (i.e. the values of the overall glyphs composed). */
20621 it->nglyphs = 1;
20623 /* If we have not yet calculated pixel size data of glyphs of
20624 the composition for the current face font, calculate them
20625 now. Theoretically, we have to check all fonts for the
20626 glyphs, but that requires much time and memory space. So,
20627 here we check only the font of the first glyph. This leads
20628 to incorrect display very rarely, and C-l (recenter) can
20629 correct the display anyway. */
20630 if (cmp->font != (void *) font)
20632 /* Ascent and descent of the font of the first character of
20633 this composition (adjusted by baseline offset). Ascent
20634 and descent of overall glyphs should not be less than
20635 them respectively. */
20636 int font_ascent = FONT_BASE (font) + boff;
20637 int font_descent = FONT_DESCENT (font) - boff;
20638 /* Bounding box of the overall glyphs. */
20639 int leftmost, rightmost, lowest, highest;
20640 int i, width, ascent, descent;
20642 cmp->font = (void *) font;
20644 /* Initialize the bounding box. */
20645 if (font_info
20646 && (pcm = rif->per_char_metric (font, &char2b,
20647 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20649 width = pcm->width;
20650 ascent = pcm->ascent;
20651 descent = pcm->descent;
20653 else
20655 width = FONT_WIDTH (font);
20656 ascent = FONT_BASE (font);
20657 descent = FONT_DESCENT (font);
20660 rightmost = width;
20661 lowest = - descent + boff;
20662 highest = ascent + boff;
20663 leftmost = 0;
20665 if (font_info
20666 && font_info->default_ascent
20667 && CHAR_TABLE_P (Vuse_default_ascent)
20668 && !NILP (Faref (Vuse_default_ascent,
20669 make_number (it->char_to_display))))
20670 highest = font_info->default_ascent + boff;
20672 /* Draw the first glyph at the normal position. It may be
20673 shifted to right later if some other glyphs are drawn at
20674 the left. */
20675 cmp->offsets[0] = 0;
20676 cmp->offsets[1] = boff;
20678 /* Set cmp->offsets for the remaining glyphs. */
20679 for (i = 1; i < cmp->glyph_len; i++)
20681 int left, right, btm, top;
20682 int ch = COMPOSITION_GLYPH (cmp, i);
20683 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20685 face = FACE_FROM_ID (it->f, face_id);
20686 get_char_face_and_encoding (it->f, ch, face->id,
20687 &char2b, it->multibyte_p, 0);
20688 font = face->font;
20689 if (font == NULL)
20691 font = FRAME_FONT (it->f);
20692 boff = FRAME_BASELINE_OFFSET (it->f);
20693 font_info = NULL;
20695 else
20697 font_info
20698 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20699 boff = font_info->baseline_offset;
20700 if (font_info->vertical_centering)
20701 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20704 if (font_info
20705 && (pcm = rif->per_char_metric (font, &char2b,
20706 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20708 width = pcm->width;
20709 ascent = pcm->ascent;
20710 descent = pcm->descent;
20712 else
20714 width = FONT_WIDTH (font);
20715 ascent = 1;
20716 descent = 0;
20719 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20721 /* Relative composition with or without
20722 alternate chars. */
20723 left = (leftmost + rightmost - width) / 2;
20724 btm = - descent + boff;
20725 if (font_info && font_info->relative_compose
20726 && (! CHAR_TABLE_P (Vignore_relative_composition)
20727 || NILP (Faref (Vignore_relative_composition,
20728 make_number (ch)))))
20731 if (- descent >= font_info->relative_compose)
20732 /* One extra pixel between two glyphs. */
20733 btm = highest + 1;
20734 else if (ascent <= 0)
20735 /* One extra pixel between two glyphs. */
20736 btm = lowest - 1 - ascent - descent;
20739 else
20741 /* A composition rule is specified by an integer
20742 value that encodes global and new reference
20743 points (GREF and NREF). GREF and NREF are
20744 specified by numbers as below:
20746 0---1---2 -- ascent
20750 9--10--11 -- center
20752 ---3---4---5--- baseline
20754 6---7---8 -- descent
20756 int rule = COMPOSITION_RULE (cmp, i);
20757 int gref, nref, grefx, grefy, nrefx, nrefy;
20759 COMPOSITION_DECODE_RULE (rule, gref, nref);
20760 grefx = gref % 3, nrefx = nref % 3;
20761 grefy = gref / 3, nrefy = nref / 3;
20763 left = (leftmost
20764 + grefx * (rightmost - leftmost) / 2
20765 - nrefx * width / 2);
20766 btm = ((grefy == 0 ? highest
20767 : grefy == 1 ? 0
20768 : grefy == 2 ? lowest
20769 : (highest + lowest) / 2)
20770 - (nrefy == 0 ? ascent + descent
20771 : nrefy == 1 ? descent - boff
20772 : nrefy == 2 ? 0
20773 : (ascent + descent) / 2));
20776 cmp->offsets[i * 2] = left;
20777 cmp->offsets[i * 2 + 1] = btm + descent;
20779 /* Update the bounding box of the overall glyphs. */
20780 right = left + width;
20781 top = btm + descent + ascent;
20782 if (left < leftmost)
20783 leftmost = left;
20784 if (right > rightmost)
20785 rightmost = right;
20786 if (top > highest)
20787 highest = top;
20788 if (btm < lowest)
20789 lowest = btm;
20792 /* If there are glyphs whose x-offsets are negative,
20793 shift all glyphs to the right and make all x-offsets
20794 non-negative. */
20795 if (leftmost < 0)
20797 for (i = 0; i < cmp->glyph_len; i++)
20798 cmp->offsets[i * 2] -= leftmost;
20799 rightmost -= leftmost;
20802 cmp->pixel_width = rightmost;
20803 cmp->ascent = highest;
20804 cmp->descent = - lowest;
20805 if (cmp->ascent < font_ascent)
20806 cmp->ascent = font_ascent;
20807 if (cmp->descent < font_descent)
20808 cmp->descent = font_descent;
20811 it->pixel_width = cmp->pixel_width;
20812 it->ascent = it->phys_ascent = cmp->ascent;
20813 it->descent = it->phys_descent = cmp->descent;
20815 if (face->box != FACE_NO_BOX)
20817 int thick = face->box_line_width;
20819 if (thick > 0)
20821 it->ascent += thick;
20822 it->descent += thick;
20824 else
20825 thick = - thick;
20827 if (it->start_of_box_run_p)
20828 it->pixel_width += thick;
20829 if (it->end_of_box_run_p)
20830 it->pixel_width += thick;
20833 /* If face has an overline, add the height of the overline
20834 (1 pixel) and a 1 pixel margin to the character height. */
20835 if (face->overline_p)
20836 it->ascent += 2;
20838 take_vertical_position_into_account (it);
20840 if (it->glyph_row)
20841 append_composite_glyph (it);
20843 else if (it->what == IT_IMAGE)
20844 produce_image_glyph (it);
20845 else if (it->what == IT_STRETCH)
20846 produce_stretch_glyph (it);
20848 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20849 because this isn't true for images with `:ascent 100'. */
20850 xassert (it->ascent >= 0 && it->descent >= 0);
20851 if (it->area == TEXT_AREA)
20852 it->current_x += it->pixel_width;
20854 if (extra_line_spacing > 0)
20856 it->descent += extra_line_spacing;
20857 if (extra_line_spacing > it->max_extra_line_spacing)
20858 it->max_extra_line_spacing = extra_line_spacing;
20861 it->max_ascent = max (it->max_ascent, it->ascent);
20862 it->max_descent = max (it->max_descent, it->descent);
20863 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20864 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20867 /* EXPORT for RIF:
20868 Output LEN glyphs starting at START at the nominal cursor position.
20869 Advance the nominal cursor over the text. The global variable
20870 updated_window contains the window being updated, updated_row is
20871 the glyph row being updated, and updated_area is the area of that
20872 row being updated. */
20874 void
20875 x_write_glyphs (start, len)
20876 struct glyph *start;
20877 int len;
20879 int x, hpos;
20881 xassert (updated_window && updated_row);
20882 BLOCK_INPUT;
20884 /* Write glyphs. */
20886 hpos = start - updated_row->glyphs[updated_area];
20887 x = draw_glyphs (updated_window, output_cursor.x,
20888 updated_row, updated_area,
20889 hpos, hpos + len,
20890 DRAW_NORMAL_TEXT, 0);
20892 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20893 if (updated_area == TEXT_AREA
20894 && updated_window->phys_cursor_on_p
20895 && updated_window->phys_cursor.vpos == output_cursor.vpos
20896 && updated_window->phys_cursor.hpos >= hpos
20897 && updated_window->phys_cursor.hpos < hpos + len)
20898 updated_window->phys_cursor_on_p = 0;
20900 UNBLOCK_INPUT;
20902 /* Advance the output cursor. */
20903 output_cursor.hpos += len;
20904 output_cursor.x = x;
20908 /* EXPORT for RIF:
20909 Insert LEN glyphs from START at the nominal cursor position. */
20911 void
20912 x_insert_glyphs (start, len)
20913 struct glyph *start;
20914 int len;
20916 struct frame *f;
20917 struct window *w;
20918 int line_height, shift_by_width, shifted_region_width;
20919 struct glyph_row *row;
20920 struct glyph *glyph;
20921 int frame_x, frame_y, hpos;
20923 xassert (updated_window && updated_row);
20924 BLOCK_INPUT;
20925 w = updated_window;
20926 f = XFRAME (WINDOW_FRAME (w));
20928 /* Get the height of the line we are in. */
20929 row = updated_row;
20930 line_height = row->height;
20932 /* Get the width of the glyphs to insert. */
20933 shift_by_width = 0;
20934 for (glyph = start; glyph < start + len; ++glyph)
20935 shift_by_width += glyph->pixel_width;
20937 /* Get the width of the region to shift right. */
20938 shifted_region_width = (window_box_width (w, updated_area)
20939 - output_cursor.x
20940 - shift_by_width);
20942 /* Shift right. */
20943 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20944 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20946 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20947 line_height, shift_by_width);
20949 /* Write the glyphs. */
20950 hpos = start - row->glyphs[updated_area];
20951 draw_glyphs (w, output_cursor.x, row, updated_area,
20952 hpos, hpos + len,
20953 DRAW_NORMAL_TEXT, 0);
20955 /* Advance the output cursor. */
20956 output_cursor.hpos += len;
20957 output_cursor.x += shift_by_width;
20958 UNBLOCK_INPUT;
20962 /* EXPORT for RIF:
20963 Erase the current text line from the nominal cursor position
20964 (inclusive) to pixel column TO_X (exclusive). The idea is that
20965 everything from TO_X onward is already erased.
20967 TO_X is a pixel position relative to updated_area of
20968 updated_window. TO_X == -1 means clear to the end of this area. */
20970 void
20971 x_clear_end_of_line (to_x)
20972 int to_x;
20974 struct frame *f;
20975 struct window *w = updated_window;
20976 int max_x, min_y, max_y;
20977 int from_x, from_y, to_y;
20979 xassert (updated_window && updated_row);
20980 f = XFRAME (w->frame);
20982 if (updated_row->full_width_p)
20983 max_x = WINDOW_TOTAL_WIDTH (w);
20984 else
20985 max_x = window_box_width (w, updated_area);
20986 max_y = window_text_bottom_y (w);
20988 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20989 of window. For TO_X > 0, truncate to end of drawing area. */
20990 if (to_x == 0)
20991 return;
20992 else if (to_x < 0)
20993 to_x = max_x;
20994 else
20995 to_x = min (to_x, max_x);
20997 to_y = min (max_y, output_cursor.y + updated_row->height);
20999 /* Notice if the cursor will be cleared by this operation. */
21000 if (!updated_row->full_width_p)
21001 notice_overwritten_cursor (w, updated_area,
21002 output_cursor.x, -1,
21003 updated_row->y,
21004 MATRIX_ROW_BOTTOM_Y (updated_row));
21006 from_x = output_cursor.x;
21008 /* Translate to frame coordinates. */
21009 if (updated_row->full_width_p)
21011 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21012 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21014 else
21016 int area_left = window_box_left (w, updated_area);
21017 from_x += area_left;
21018 to_x += area_left;
21021 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21022 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21023 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21025 /* Prevent inadvertently clearing to end of the X window. */
21026 if (to_x > from_x && to_y > from_y)
21028 BLOCK_INPUT;
21029 rif->clear_frame_area (f, from_x, from_y,
21030 to_x - from_x, to_y - from_y);
21031 UNBLOCK_INPUT;
21035 #endif /* HAVE_WINDOW_SYSTEM */
21039 /***********************************************************************
21040 Cursor types
21041 ***********************************************************************/
21043 /* Value is the internal representation of the specified cursor type
21044 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21045 of the bar cursor. */
21047 static enum text_cursor_kinds
21048 get_specified_cursor_type (arg, width)
21049 Lisp_Object arg;
21050 int *width;
21052 enum text_cursor_kinds type;
21054 if (NILP (arg))
21055 return NO_CURSOR;
21057 if (EQ (arg, Qbox))
21058 return FILLED_BOX_CURSOR;
21060 if (EQ (arg, Qhollow))
21061 return HOLLOW_BOX_CURSOR;
21063 if (EQ (arg, Qbar))
21065 *width = 2;
21066 return BAR_CURSOR;
21069 if (CONSP (arg)
21070 && EQ (XCAR (arg), Qbar)
21071 && INTEGERP (XCDR (arg))
21072 && XINT (XCDR (arg)) >= 0)
21074 *width = XINT (XCDR (arg));
21075 return BAR_CURSOR;
21078 if (EQ (arg, Qhbar))
21080 *width = 2;
21081 return HBAR_CURSOR;
21084 if (CONSP (arg)
21085 && EQ (XCAR (arg), Qhbar)
21086 && INTEGERP (XCDR (arg))
21087 && XINT (XCDR (arg)) >= 0)
21089 *width = XINT (XCDR (arg));
21090 return HBAR_CURSOR;
21093 /* Treat anything unknown as "hollow box cursor".
21094 It was bad to signal an error; people have trouble fixing
21095 .Xdefaults with Emacs, when it has something bad in it. */
21096 type = HOLLOW_BOX_CURSOR;
21098 return type;
21101 /* Set the default cursor types for specified frame. */
21102 void
21103 set_frame_cursor_types (f, arg)
21104 struct frame *f;
21105 Lisp_Object arg;
21107 int width;
21108 Lisp_Object tem;
21110 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21111 FRAME_CURSOR_WIDTH (f) = width;
21113 /* By default, set up the blink-off state depending on the on-state. */
21115 tem = Fassoc (arg, Vblink_cursor_alist);
21116 if (!NILP (tem))
21118 FRAME_BLINK_OFF_CURSOR (f)
21119 = get_specified_cursor_type (XCDR (tem), &width);
21120 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21122 else
21123 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21127 /* Return the cursor we want to be displayed in window W. Return
21128 width of bar/hbar cursor through WIDTH arg. Return with
21129 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21130 (i.e. if the `system caret' should track this cursor).
21132 In a mini-buffer window, we want the cursor only to appear if we
21133 are reading input from this window. For the selected window, we
21134 want the cursor type given by the frame parameter or buffer local
21135 setting of cursor-type. If explicitly marked off, draw no cursor.
21136 In all other cases, we want a hollow box cursor. */
21138 static enum text_cursor_kinds
21139 get_window_cursor_type (w, glyph, width, active_cursor)
21140 struct window *w;
21141 struct glyph *glyph;
21142 int *width;
21143 int *active_cursor;
21145 struct frame *f = XFRAME (w->frame);
21146 struct buffer *b = XBUFFER (w->buffer);
21147 int cursor_type = DEFAULT_CURSOR;
21148 Lisp_Object alt_cursor;
21149 int non_selected = 0;
21151 *active_cursor = 1;
21153 /* Echo area */
21154 if (cursor_in_echo_area
21155 && FRAME_HAS_MINIBUF_P (f)
21156 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21158 if (w == XWINDOW (echo_area_window))
21160 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21162 *width = FRAME_CURSOR_WIDTH (f);
21163 return FRAME_DESIRED_CURSOR (f);
21165 else
21166 return get_specified_cursor_type (b->cursor_type, width);
21169 *active_cursor = 0;
21170 non_selected = 1;
21173 /* Nonselected window or nonselected frame. */
21174 else if (w != XWINDOW (f->selected_window)
21175 #ifdef HAVE_WINDOW_SYSTEM
21176 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21177 #endif
21180 *active_cursor = 0;
21182 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21183 return NO_CURSOR;
21185 non_selected = 1;
21188 /* Never display a cursor in a window in which cursor-type is nil. */
21189 if (NILP (b->cursor_type))
21190 return NO_CURSOR;
21192 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21193 if (non_selected)
21195 alt_cursor = b->cursor_in_non_selected_windows;
21196 return get_specified_cursor_type (alt_cursor, width);
21199 /* Get the normal cursor type for this window. */
21200 if (EQ (b->cursor_type, Qt))
21202 cursor_type = FRAME_DESIRED_CURSOR (f);
21203 *width = FRAME_CURSOR_WIDTH (f);
21205 else
21206 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21208 /* Use normal cursor if not blinked off. */
21209 if (!w->cursor_off_p)
21211 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21212 if (cursor_type == FILLED_BOX_CURSOR)
21213 cursor_type = HOLLOW_BOX_CURSOR;
21215 return cursor_type;
21218 /* Cursor is blinked off, so determine how to "toggle" it. */
21220 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21221 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21222 return get_specified_cursor_type (XCDR (alt_cursor), width);
21224 /* Then see if frame has specified a specific blink off cursor type. */
21225 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21227 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21228 return FRAME_BLINK_OFF_CURSOR (f);
21231 #if 0
21232 /* Some people liked having a permanently visible blinking cursor,
21233 while others had very strong opinions against it. So it was
21234 decided to remove it. KFS 2003-09-03 */
21236 /* Finally perform built-in cursor blinking:
21237 filled box <-> hollow box
21238 wide [h]bar <-> narrow [h]bar
21239 narrow [h]bar <-> no cursor
21240 other type <-> no cursor */
21242 if (cursor_type == FILLED_BOX_CURSOR)
21243 return HOLLOW_BOX_CURSOR;
21245 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21247 *width = 1;
21248 return cursor_type;
21250 #endif
21252 return NO_CURSOR;
21256 #ifdef HAVE_WINDOW_SYSTEM
21258 /* Notice when the text cursor of window W has been completely
21259 overwritten by a drawing operation that outputs glyphs in AREA
21260 starting at X0 and ending at X1 in the line starting at Y0 and
21261 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21262 the rest of the line after X0 has been written. Y coordinates
21263 are window-relative. */
21265 static void
21266 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21267 struct window *w;
21268 enum glyph_row_area area;
21269 int x0, y0, x1, y1;
21271 int cx0, cx1, cy0, cy1;
21272 struct glyph_row *row;
21274 if (!w->phys_cursor_on_p)
21275 return;
21276 if (area != TEXT_AREA)
21277 return;
21279 if (w->phys_cursor.vpos < 0
21280 || w->phys_cursor.vpos >= w->current_matrix->nrows
21281 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21282 !(row->enabled_p && row->displays_text_p)))
21283 return;
21285 if (row->cursor_in_fringe_p)
21287 row->cursor_in_fringe_p = 0;
21288 draw_fringe_bitmap (w, row, 0);
21289 w->phys_cursor_on_p = 0;
21290 return;
21293 cx0 = w->phys_cursor.x;
21294 cx1 = cx0 + w->phys_cursor_width;
21295 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21296 return;
21298 /* The cursor image will be completely removed from the
21299 screen if the output area intersects the cursor area in
21300 y-direction. When we draw in [y0 y1[, and some part of
21301 the cursor is at y < y0, that part must have been drawn
21302 before. When scrolling, the cursor is erased before
21303 actually scrolling, so we don't come here. When not
21304 scrolling, the rows above the old cursor row must have
21305 changed, and in this case these rows must have written
21306 over the cursor image.
21308 Likewise if part of the cursor is below y1, with the
21309 exception of the cursor being in the first blank row at
21310 the buffer and window end because update_text_area
21311 doesn't draw that row. (Except when it does, but
21312 that's handled in update_text_area.) */
21314 cy0 = w->phys_cursor.y;
21315 cy1 = cy0 + w->phys_cursor_height;
21316 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21317 return;
21319 w->phys_cursor_on_p = 0;
21322 #endif /* HAVE_WINDOW_SYSTEM */
21325 /************************************************************************
21326 Mouse Face
21327 ************************************************************************/
21329 #ifdef HAVE_WINDOW_SYSTEM
21331 /* EXPORT for RIF:
21332 Fix the display of area AREA of overlapping row ROW in window W
21333 with respect to the overlapping part OVERLAPS. */
21335 void
21336 x_fix_overlapping_area (w, row, area, overlaps)
21337 struct window *w;
21338 struct glyph_row *row;
21339 enum glyph_row_area area;
21340 int overlaps;
21342 int i, x;
21344 BLOCK_INPUT;
21346 x = 0;
21347 for (i = 0; i < row->used[area];)
21349 if (row->glyphs[area][i].overlaps_vertically_p)
21351 int start = i, start_x = x;
21355 x += row->glyphs[area][i].pixel_width;
21356 ++i;
21358 while (i < row->used[area]
21359 && row->glyphs[area][i].overlaps_vertically_p);
21361 draw_glyphs (w, start_x, row, area,
21362 start, i,
21363 DRAW_NORMAL_TEXT, overlaps);
21365 else
21367 x += row->glyphs[area][i].pixel_width;
21368 ++i;
21372 UNBLOCK_INPUT;
21376 /* EXPORT:
21377 Draw the cursor glyph of window W in glyph row ROW. See the
21378 comment of draw_glyphs for the meaning of HL. */
21380 void
21381 draw_phys_cursor_glyph (w, row, hl)
21382 struct window *w;
21383 struct glyph_row *row;
21384 enum draw_glyphs_face hl;
21386 /* If cursor hpos is out of bounds, don't draw garbage. This can
21387 happen in mini-buffer windows when switching between echo area
21388 glyphs and mini-buffer. */
21389 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21391 int on_p = w->phys_cursor_on_p;
21392 int x1;
21393 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21394 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21395 hl, 0);
21396 w->phys_cursor_on_p = on_p;
21398 if (hl == DRAW_CURSOR)
21399 w->phys_cursor_width = x1 - w->phys_cursor.x;
21400 /* When we erase the cursor, and ROW is overlapped by other
21401 rows, make sure that these overlapping parts of other rows
21402 are redrawn. */
21403 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21405 w->phys_cursor_width = x1 - w->phys_cursor.x;
21407 if (row > w->current_matrix->rows
21408 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21409 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21410 OVERLAPS_ERASED_CURSOR);
21412 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21413 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21414 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21415 OVERLAPS_ERASED_CURSOR);
21421 /* EXPORT:
21422 Erase the image of a cursor of window W from the screen. */
21424 void
21425 erase_phys_cursor (w)
21426 struct window *w;
21428 struct frame *f = XFRAME (w->frame);
21429 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21430 int hpos = w->phys_cursor.hpos;
21431 int vpos = w->phys_cursor.vpos;
21432 int mouse_face_here_p = 0;
21433 struct glyph_matrix *active_glyphs = w->current_matrix;
21434 struct glyph_row *cursor_row;
21435 struct glyph *cursor_glyph;
21436 enum draw_glyphs_face hl;
21438 /* No cursor displayed or row invalidated => nothing to do on the
21439 screen. */
21440 if (w->phys_cursor_type == NO_CURSOR)
21441 goto mark_cursor_off;
21443 /* VPOS >= active_glyphs->nrows means that window has been resized.
21444 Don't bother to erase the cursor. */
21445 if (vpos >= active_glyphs->nrows)
21446 goto mark_cursor_off;
21448 /* If row containing cursor is marked invalid, there is nothing we
21449 can do. */
21450 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21451 if (!cursor_row->enabled_p)
21452 goto mark_cursor_off;
21454 /* If line spacing is > 0, old cursor may only be partially visible in
21455 window after split-window. So adjust visible height. */
21456 cursor_row->visible_height = min (cursor_row->visible_height,
21457 window_text_bottom_y (w) - cursor_row->y);
21459 /* If row is completely invisible, don't attempt to delete a cursor which
21460 isn't there. This can happen if cursor is at top of a window, and
21461 we switch to a buffer with a header line in that window. */
21462 if (cursor_row->visible_height <= 0)
21463 goto mark_cursor_off;
21465 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21466 if (cursor_row->cursor_in_fringe_p)
21468 cursor_row->cursor_in_fringe_p = 0;
21469 draw_fringe_bitmap (w, cursor_row, 0);
21470 goto mark_cursor_off;
21473 /* This can happen when the new row is shorter than the old one.
21474 In this case, either draw_glyphs or clear_end_of_line
21475 should have cleared the cursor. Note that we wouldn't be
21476 able to erase the cursor in this case because we don't have a
21477 cursor glyph at hand. */
21478 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21479 goto mark_cursor_off;
21481 /* If the cursor is in the mouse face area, redisplay that when
21482 we clear the cursor. */
21483 if (! NILP (dpyinfo->mouse_face_window)
21484 && w == XWINDOW (dpyinfo->mouse_face_window)
21485 && (vpos > dpyinfo->mouse_face_beg_row
21486 || (vpos == dpyinfo->mouse_face_beg_row
21487 && hpos >= dpyinfo->mouse_face_beg_col))
21488 && (vpos < dpyinfo->mouse_face_end_row
21489 || (vpos == dpyinfo->mouse_face_end_row
21490 && hpos < dpyinfo->mouse_face_end_col))
21491 /* Don't redraw the cursor's spot in mouse face if it is at the
21492 end of a line (on a newline). The cursor appears there, but
21493 mouse highlighting does not. */
21494 && cursor_row->used[TEXT_AREA] > hpos)
21495 mouse_face_here_p = 1;
21497 /* Maybe clear the display under the cursor. */
21498 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21500 int x, y, left_x;
21501 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21502 int width;
21504 cursor_glyph = get_phys_cursor_glyph (w);
21505 if (cursor_glyph == NULL)
21506 goto mark_cursor_off;
21508 width = cursor_glyph->pixel_width;
21509 left_x = window_box_left_offset (w, TEXT_AREA);
21510 x = w->phys_cursor.x;
21511 if (x < left_x)
21512 width -= left_x - x;
21513 width = min (width, window_box_width (w, TEXT_AREA) - x);
21514 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21515 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21517 if (width > 0)
21518 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21521 /* Erase the cursor by redrawing the character underneath it. */
21522 if (mouse_face_here_p)
21523 hl = DRAW_MOUSE_FACE;
21524 else
21525 hl = DRAW_NORMAL_TEXT;
21526 draw_phys_cursor_glyph (w, cursor_row, hl);
21528 mark_cursor_off:
21529 w->phys_cursor_on_p = 0;
21530 w->phys_cursor_type = NO_CURSOR;
21534 /* EXPORT:
21535 Display or clear cursor of window W. If ON is zero, clear the
21536 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21537 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21539 void
21540 display_and_set_cursor (w, on, hpos, vpos, x, y)
21541 struct window *w;
21542 int on, hpos, vpos, x, y;
21544 struct frame *f = XFRAME (w->frame);
21545 int new_cursor_type;
21546 int new_cursor_width;
21547 int active_cursor;
21548 struct glyph_row *glyph_row;
21549 struct glyph *glyph;
21551 /* This is pointless on invisible frames, and dangerous on garbaged
21552 windows and frames; in the latter case, the frame or window may
21553 be in the midst of changing its size, and x and y may be off the
21554 window. */
21555 if (! FRAME_VISIBLE_P (f)
21556 || FRAME_GARBAGED_P (f)
21557 || vpos >= w->current_matrix->nrows
21558 || hpos >= w->current_matrix->matrix_w)
21559 return;
21561 /* If cursor is off and we want it off, return quickly. */
21562 if (!on && !w->phys_cursor_on_p)
21563 return;
21565 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21566 /* If cursor row is not enabled, we don't really know where to
21567 display the cursor. */
21568 if (!glyph_row->enabled_p)
21570 w->phys_cursor_on_p = 0;
21571 return;
21574 glyph = NULL;
21575 if (!glyph_row->exact_window_width_line_p
21576 || hpos < glyph_row->used[TEXT_AREA])
21577 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21579 xassert (interrupt_input_blocked);
21581 /* Set new_cursor_type to the cursor we want to be displayed. */
21582 new_cursor_type = get_window_cursor_type (w, glyph,
21583 &new_cursor_width, &active_cursor);
21585 /* If cursor is currently being shown and we don't want it to be or
21586 it is in the wrong place, or the cursor type is not what we want,
21587 erase it. */
21588 if (w->phys_cursor_on_p
21589 && (!on
21590 || w->phys_cursor.x != x
21591 || w->phys_cursor.y != y
21592 || new_cursor_type != w->phys_cursor_type
21593 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21594 && new_cursor_width != w->phys_cursor_width)))
21595 erase_phys_cursor (w);
21597 /* Don't check phys_cursor_on_p here because that flag is only set
21598 to zero in some cases where we know that the cursor has been
21599 completely erased, to avoid the extra work of erasing the cursor
21600 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21601 still not be visible, or it has only been partly erased. */
21602 if (on)
21604 w->phys_cursor_ascent = glyph_row->ascent;
21605 w->phys_cursor_height = glyph_row->height;
21607 /* Set phys_cursor_.* before x_draw_.* is called because some
21608 of them may need the information. */
21609 w->phys_cursor.x = x;
21610 w->phys_cursor.y = glyph_row->y;
21611 w->phys_cursor.hpos = hpos;
21612 w->phys_cursor.vpos = vpos;
21615 rif->draw_window_cursor (w, glyph_row, x, y,
21616 new_cursor_type, new_cursor_width,
21617 on, active_cursor);
21621 /* Switch the display of W's cursor on or off, according to the value
21622 of ON. */
21624 static void
21625 update_window_cursor (w, on)
21626 struct window *w;
21627 int on;
21629 /* Don't update cursor in windows whose frame is in the process
21630 of being deleted. */
21631 if (w->current_matrix)
21633 BLOCK_INPUT;
21634 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21635 w->phys_cursor.x, w->phys_cursor.y);
21636 UNBLOCK_INPUT;
21641 /* Call update_window_cursor with parameter ON_P on all leaf windows
21642 in the window tree rooted at W. */
21644 static void
21645 update_cursor_in_window_tree (w, on_p)
21646 struct window *w;
21647 int on_p;
21649 while (w)
21651 if (!NILP (w->hchild))
21652 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21653 else if (!NILP (w->vchild))
21654 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21655 else
21656 update_window_cursor (w, on_p);
21658 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21663 /* EXPORT:
21664 Display the cursor on window W, or clear it, according to ON_P.
21665 Don't change the cursor's position. */
21667 void
21668 x_update_cursor (f, on_p)
21669 struct frame *f;
21670 int on_p;
21672 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21676 /* EXPORT:
21677 Clear the cursor of window W to background color, and mark the
21678 cursor as not shown. This is used when the text where the cursor
21679 is is about to be rewritten. */
21681 void
21682 x_clear_cursor (w)
21683 struct window *w;
21685 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21686 update_window_cursor (w, 0);
21690 /* EXPORT:
21691 Display the active region described by mouse_face_* according to DRAW. */
21693 void
21694 show_mouse_face (dpyinfo, draw)
21695 Display_Info *dpyinfo;
21696 enum draw_glyphs_face draw;
21698 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21699 struct frame *f = XFRAME (WINDOW_FRAME (w));
21701 if (/* If window is in the process of being destroyed, don't bother
21702 to do anything. */
21703 w->current_matrix != NULL
21704 /* Don't update mouse highlight if hidden */
21705 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21706 /* Recognize when we are called to operate on rows that don't exist
21707 anymore. This can happen when a window is split. */
21708 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21710 int phys_cursor_on_p = w->phys_cursor_on_p;
21711 struct glyph_row *row, *first, *last;
21713 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21714 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21716 for (row = first; row <= last && row->enabled_p; ++row)
21718 int start_hpos, end_hpos, start_x;
21720 /* For all but the first row, the highlight starts at column 0. */
21721 if (row == first)
21723 start_hpos = dpyinfo->mouse_face_beg_col;
21724 start_x = dpyinfo->mouse_face_beg_x;
21726 else
21728 start_hpos = 0;
21729 start_x = 0;
21732 if (row == last)
21733 end_hpos = dpyinfo->mouse_face_end_col;
21734 else
21736 end_hpos = row->used[TEXT_AREA];
21737 if (draw == DRAW_NORMAL_TEXT)
21738 row->fill_line_p = 1; /* Clear to end of line */
21741 if (end_hpos > start_hpos)
21743 draw_glyphs (w, start_x, row, TEXT_AREA,
21744 start_hpos, end_hpos,
21745 draw, 0);
21747 row->mouse_face_p
21748 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21752 /* When we've written over the cursor, arrange for it to
21753 be displayed again. */
21754 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21756 BLOCK_INPUT;
21757 display_and_set_cursor (w, 1,
21758 w->phys_cursor.hpos, w->phys_cursor.vpos,
21759 w->phys_cursor.x, w->phys_cursor.y);
21760 UNBLOCK_INPUT;
21764 /* Change the mouse cursor. */
21765 if (draw == DRAW_NORMAL_TEXT)
21766 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21767 else if (draw == DRAW_MOUSE_FACE)
21768 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21769 else
21770 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21773 /* EXPORT:
21774 Clear out the mouse-highlighted active region.
21775 Redraw it un-highlighted first. Value is non-zero if mouse
21776 face was actually drawn unhighlighted. */
21779 clear_mouse_face (dpyinfo)
21780 Display_Info *dpyinfo;
21782 int cleared = 0;
21784 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21786 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21787 cleared = 1;
21790 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21791 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21792 dpyinfo->mouse_face_window = Qnil;
21793 dpyinfo->mouse_face_overlay = Qnil;
21794 return cleared;
21798 /* EXPORT:
21799 Non-zero if physical cursor of window W is within mouse face. */
21802 cursor_in_mouse_face_p (w)
21803 struct window *w;
21805 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21806 int in_mouse_face = 0;
21808 if (WINDOWP (dpyinfo->mouse_face_window)
21809 && XWINDOW (dpyinfo->mouse_face_window) == w)
21811 int hpos = w->phys_cursor.hpos;
21812 int vpos = w->phys_cursor.vpos;
21814 if (vpos >= dpyinfo->mouse_face_beg_row
21815 && vpos <= dpyinfo->mouse_face_end_row
21816 && (vpos > dpyinfo->mouse_face_beg_row
21817 || hpos >= dpyinfo->mouse_face_beg_col)
21818 && (vpos < dpyinfo->mouse_face_end_row
21819 || hpos < dpyinfo->mouse_face_end_col
21820 || dpyinfo->mouse_face_past_end))
21821 in_mouse_face = 1;
21824 return in_mouse_face;
21830 /* Find the glyph matrix position of buffer position CHARPOS in window
21831 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21832 current glyphs must be up to date. If CHARPOS is above window
21833 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21834 of last line in W. In the row containing CHARPOS, stop before glyphs
21835 having STOP as object. */
21837 #if 1 /* This is a version of fast_find_position that's more correct
21838 in the presence of hscrolling, for example. I didn't install
21839 it right away because the problem fixed is minor, it failed
21840 in 20.x as well, and I think it's too risky to install
21841 so near the release of 21.1. 2001-09-25 gerd. */
21843 static int
21844 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21845 struct window *w;
21846 int charpos;
21847 int *hpos, *vpos, *x, *y;
21848 Lisp_Object stop;
21850 struct glyph_row *row, *first;
21851 struct glyph *glyph, *end;
21852 int past_end = 0;
21854 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21855 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21857 *x = first->x;
21858 *y = first->y;
21859 *hpos = 0;
21860 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21861 return 1;
21864 row = row_containing_pos (w, charpos, first, NULL, 0);
21865 if (row == NULL)
21867 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21868 past_end = 1;
21871 /* If whole rows or last part of a row came from a display overlay,
21872 row_containing_pos will skip over such rows because their end pos
21873 equals the start pos of the overlay or interval.
21875 Move back if we have a STOP object and previous row's
21876 end glyph came from STOP. */
21877 if (!NILP (stop))
21879 struct glyph_row *prev;
21880 while ((prev = row - 1, prev >= first)
21881 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21882 && prev->used[TEXT_AREA] > 0)
21884 struct glyph *beg = prev->glyphs[TEXT_AREA];
21885 glyph = beg + prev->used[TEXT_AREA];
21886 while (--glyph >= beg
21887 && INTEGERP (glyph->object));
21888 if (glyph < beg
21889 || !EQ (stop, glyph->object))
21890 break;
21891 row = prev;
21895 *x = row->x;
21896 *y = row->y;
21897 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21899 glyph = row->glyphs[TEXT_AREA];
21900 end = glyph + row->used[TEXT_AREA];
21902 /* Skip over glyphs not having an object at the start of the row.
21903 These are special glyphs like truncation marks on terminal
21904 frames. */
21905 if (row->displays_text_p)
21906 while (glyph < end
21907 && INTEGERP (glyph->object)
21908 && !EQ (stop, glyph->object)
21909 && glyph->charpos < 0)
21911 *x += glyph->pixel_width;
21912 ++glyph;
21915 while (glyph < end
21916 && !INTEGERP (glyph->object)
21917 && !EQ (stop, glyph->object)
21918 && (!BUFFERP (glyph->object)
21919 || glyph->charpos < charpos))
21921 *x += glyph->pixel_width;
21922 ++glyph;
21925 *hpos = glyph - row->glyphs[TEXT_AREA];
21926 return !past_end;
21929 #else /* not 1 */
21931 static int
21932 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21933 struct window *w;
21934 int pos;
21935 int *hpos, *vpos, *x, *y;
21936 Lisp_Object stop;
21938 int i;
21939 int lastcol;
21940 int maybe_next_line_p = 0;
21941 int line_start_position;
21942 int yb = window_text_bottom_y (w);
21943 struct glyph_row *row, *best_row;
21944 int row_vpos, best_row_vpos;
21945 int current_x;
21947 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21948 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21950 while (row->y < yb)
21952 if (row->used[TEXT_AREA])
21953 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21954 else
21955 line_start_position = 0;
21957 if (line_start_position > pos)
21958 break;
21959 /* If the position sought is the end of the buffer,
21960 don't include the blank lines at the bottom of the window. */
21961 else if (line_start_position == pos
21962 && pos == BUF_ZV (XBUFFER (w->buffer)))
21964 maybe_next_line_p = 1;
21965 break;
21967 else if (line_start_position > 0)
21969 best_row = row;
21970 best_row_vpos = row_vpos;
21973 if (row->y + row->height >= yb)
21974 break;
21976 ++row;
21977 ++row_vpos;
21980 /* Find the right column within BEST_ROW. */
21981 lastcol = 0;
21982 current_x = best_row->x;
21983 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21985 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21986 int charpos = glyph->charpos;
21988 if (BUFFERP (glyph->object))
21990 if (charpos == pos)
21992 *hpos = i;
21993 *vpos = best_row_vpos;
21994 *x = current_x;
21995 *y = best_row->y;
21996 return 1;
21998 else if (charpos > pos)
21999 break;
22001 else if (EQ (glyph->object, stop))
22002 break;
22004 if (charpos > 0)
22005 lastcol = i;
22006 current_x += glyph->pixel_width;
22009 /* If we're looking for the end of the buffer,
22010 and we didn't find it in the line we scanned,
22011 use the start of the following line. */
22012 if (maybe_next_line_p)
22014 ++best_row;
22015 ++best_row_vpos;
22016 lastcol = 0;
22017 current_x = best_row->x;
22020 *vpos = best_row_vpos;
22021 *hpos = lastcol + 1;
22022 *x = current_x;
22023 *y = best_row->y;
22024 return 0;
22027 #endif /* not 1 */
22030 /* Find the position of the glyph for position POS in OBJECT in
22031 window W's current matrix, and return in *X, *Y the pixel
22032 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22034 RIGHT_P non-zero means return the position of the right edge of the
22035 glyph, RIGHT_P zero means return the left edge position.
22037 If no glyph for POS exists in the matrix, return the position of
22038 the glyph with the next smaller position that is in the matrix, if
22039 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22040 exists in the matrix, return the position of the glyph with the
22041 next larger position in OBJECT.
22043 Value is non-zero if a glyph was found. */
22045 static int
22046 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22047 struct window *w;
22048 int pos;
22049 Lisp_Object object;
22050 int *hpos, *vpos, *x, *y;
22051 int right_p;
22053 int yb = window_text_bottom_y (w);
22054 struct glyph_row *r;
22055 struct glyph *best_glyph = NULL;
22056 struct glyph_row *best_row = NULL;
22057 int best_x = 0;
22059 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22060 r->enabled_p && r->y < yb;
22061 ++r)
22063 struct glyph *g = r->glyphs[TEXT_AREA];
22064 struct glyph *e = g + r->used[TEXT_AREA];
22065 int gx;
22067 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22068 if (EQ (g->object, object))
22070 if (g->charpos == pos)
22072 best_glyph = g;
22073 best_x = gx;
22074 best_row = r;
22075 goto found;
22077 else if (best_glyph == NULL
22078 || ((abs (g->charpos - pos)
22079 < abs (best_glyph->charpos - pos))
22080 && (right_p
22081 ? g->charpos < pos
22082 : g->charpos > pos)))
22084 best_glyph = g;
22085 best_x = gx;
22086 best_row = r;
22091 found:
22093 if (best_glyph)
22095 *x = best_x;
22096 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22098 if (right_p)
22100 *x += best_glyph->pixel_width;
22101 ++*hpos;
22104 *y = best_row->y;
22105 *vpos = best_row - w->current_matrix->rows;
22108 return best_glyph != NULL;
22112 /* See if position X, Y is within a hot-spot of an image. */
22114 static int
22115 on_hot_spot_p (hot_spot, x, y)
22116 Lisp_Object hot_spot;
22117 int x, y;
22119 if (!CONSP (hot_spot))
22120 return 0;
22122 if (EQ (XCAR (hot_spot), Qrect))
22124 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22125 Lisp_Object rect = XCDR (hot_spot);
22126 Lisp_Object tem;
22127 if (!CONSP (rect))
22128 return 0;
22129 if (!CONSP (XCAR (rect)))
22130 return 0;
22131 if (!CONSP (XCDR (rect)))
22132 return 0;
22133 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22134 return 0;
22135 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22136 return 0;
22137 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22138 return 0;
22139 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22140 return 0;
22141 return 1;
22143 else if (EQ (XCAR (hot_spot), Qcircle))
22145 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22146 Lisp_Object circ = XCDR (hot_spot);
22147 Lisp_Object lr, lx0, ly0;
22148 if (CONSP (circ)
22149 && CONSP (XCAR (circ))
22150 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22151 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22152 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22154 double r = XFLOATINT (lr);
22155 double dx = XINT (lx0) - x;
22156 double dy = XINT (ly0) - y;
22157 return (dx * dx + dy * dy <= r * r);
22160 else if (EQ (XCAR (hot_spot), Qpoly))
22162 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22163 if (VECTORP (XCDR (hot_spot)))
22165 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22166 Lisp_Object *poly = v->contents;
22167 int n = v->size;
22168 int i;
22169 int inside = 0;
22170 Lisp_Object lx, ly;
22171 int x0, y0;
22173 /* Need an even number of coordinates, and at least 3 edges. */
22174 if (n < 6 || n & 1)
22175 return 0;
22177 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22178 If count is odd, we are inside polygon. Pixels on edges
22179 may or may not be included depending on actual geometry of the
22180 polygon. */
22181 if ((lx = poly[n-2], !INTEGERP (lx))
22182 || (ly = poly[n-1], !INTEGERP (lx)))
22183 return 0;
22184 x0 = XINT (lx), y0 = XINT (ly);
22185 for (i = 0; i < n; i += 2)
22187 int x1 = x0, y1 = y0;
22188 if ((lx = poly[i], !INTEGERP (lx))
22189 || (ly = poly[i+1], !INTEGERP (ly)))
22190 return 0;
22191 x0 = XINT (lx), y0 = XINT (ly);
22193 /* Does this segment cross the X line? */
22194 if (x0 >= x)
22196 if (x1 >= x)
22197 continue;
22199 else if (x1 < x)
22200 continue;
22201 if (y > y0 && y > y1)
22202 continue;
22203 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22204 inside = !inside;
22206 return inside;
22209 /* If we don't understand the format, pretend we're not in the hot-spot. */
22210 return 0;
22213 Lisp_Object
22214 find_hot_spot (map, x, y)
22215 Lisp_Object map;
22216 int x, y;
22218 while (CONSP (map))
22220 if (CONSP (XCAR (map))
22221 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22222 return XCAR (map);
22223 map = XCDR (map);
22226 return Qnil;
22229 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22230 3, 3, 0,
22231 doc: /* Lookup in image map MAP coordinates X and Y.
22232 An image map is an alist where each element has the format (AREA ID PLIST).
22233 An AREA is specified as either a rectangle, a circle, or a polygon:
22234 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22235 pixel coordinates of the upper left and bottom right corners.
22236 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22237 and the radius of the circle; r may be a float or integer.
22238 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22239 vector describes one corner in the polygon.
22240 Returns the alist element for the first matching AREA in MAP. */)
22241 (map, x, y)
22242 Lisp_Object map;
22243 Lisp_Object x, y;
22245 if (NILP (map))
22246 return Qnil;
22248 CHECK_NUMBER (x);
22249 CHECK_NUMBER (y);
22251 return find_hot_spot (map, XINT (x), XINT (y));
22255 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22256 static void
22257 define_frame_cursor1 (f, cursor, pointer)
22258 struct frame *f;
22259 Cursor cursor;
22260 Lisp_Object pointer;
22262 /* Do not change cursor shape while dragging mouse. */
22263 if (!NILP (do_mouse_tracking))
22264 return;
22266 if (!NILP (pointer))
22268 if (EQ (pointer, Qarrow))
22269 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22270 else if (EQ (pointer, Qhand))
22271 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22272 else if (EQ (pointer, Qtext))
22273 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22274 else if (EQ (pointer, intern ("hdrag")))
22275 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22276 #ifdef HAVE_X_WINDOWS
22277 else if (EQ (pointer, intern ("vdrag")))
22278 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22279 #endif
22280 else if (EQ (pointer, intern ("hourglass")))
22281 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22282 else if (EQ (pointer, Qmodeline))
22283 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22284 else
22285 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22288 if (cursor != No_Cursor)
22289 rif->define_frame_cursor (f, cursor);
22292 /* Take proper action when mouse has moved to the mode or header line
22293 or marginal area AREA of window W, x-position X and y-position Y.
22294 X is relative to the start of the text display area of W, so the
22295 width of bitmap areas and scroll bars must be subtracted to get a
22296 position relative to the start of the mode line. */
22298 static void
22299 note_mode_line_or_margin_highlight (window, x, y, area)
22300 Lisp_Object window;
22301 int x, y;
22302 enum window_part area;
22304 struct window *w = XWINDOW (window);
22305 struct frame *f = XFRAME (w->frame);
22306 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22307 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22308 Lisp_Object pointer = Qnil;
22309 int charpos, dx, dy, width, height;
22310 Lisp_Object string, object = Qnil;
22311 Lisp_Object pos, help;
22313 Lisp_Object mouse_face;
22314 int original_x_pixel = x;
22315 struct glyph * glyph = NULL;
22316 struct glyph_row *row;
22318 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22320 int x0;
22321 struct glyph *end;
22323 string = mode_line_string (w, area, &x, &y, &charpos,
22324 &object, &dx, &dy, &width, &height);
22326 row = (area == ON_MODE_LINE
22327 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22328 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22330 /* Find glyph */
22331 if (row->mode_line_p && row->enabled_p)
22333 glyph = row->glyphs[TEXT_AREA];
22334 end = glyph + row->used[TEXT_AREA];
22336 for (x0 = original_x_pixel;
22337 glyph < end && x0 >= glyph->pixel_width;
22338 ++glyph)
22339 x0 -= glyph->pixel_width;
22341 if (glyph >= end)
22342 glyph = NULL;
22345 else
22347 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22348 string = marginal_area_string (w, area, &x, &y, &charpos,
22349 &object, &dx, &dy, &width, &height);
22352 help = Qnil;
22354 if (IMAGEP (object))
22356 Lisp_Object image_map, hotspot;
22357 if ((image_map = Fplist_get (XCDR (object), QCmap),
22358 !NILP (image_map))
22359 && (hotspot = find_hot_spot (image_map, dx, dy),
22360 CONSP (hotspot))
22361 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22363 Lisp_Object area_id, plist;
22365 area_id = XCAR (hotspot);
22366 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22367 If so, we could look for mouse-enter, mouse-leave
22368 properties in PLIST (and do something...). */
22369 hotspot = XCDR (hotspot);
22370 if (CONSP (hotspot)
22371 && (plist = XCAR (hotspot), CONSP (plist)))
22373 pointer = Fplist_get (plist, Qpointer);
22374 if (NILP (pointer))
22375 pointer = Qhand;
22376 help = Fplist_get (plist, Qhelp_echo);
22377 if (!NILP (help))
22379 help_echo_string = help;
22380 /* Is this correct? ++kfs */
22381 XSETWINDOW (help_echo_window, w);
22382 help_echo_object = w->buffer;
22383 help_echo_pos = charpos;
22387 if (NILP (pointer))
22388 pointer = Fplist_get (XCDR (object), QCpointer);
22391 if (STRINGP (string))
22393 pos = make_number (charpos);
22394 /* If we're on a string with `help-echo' text property, arrange
22395 for the help to be displayed. This is done by setting the
22396 global variable help_echo_string to the help string. */
22397 if (NILP (help))
22399 help = Fget_text_property (pos, Qhelp_echo, string);
22400 if (!NILP (help))
22402 help_echo_string = help;
22403 XSETWINDOW (help_echo_window, w);
22404 help_echo_object = string;
22405 help_echo_pos = charpos;
22409 if (NILP (pointer))
22410 pointer = Fget_text_property (pos, Qpointer, string);
22412 /* Change the mouse pointer according to what is under X/Y. */
22413 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22415 Lisp_Object map;
22416 map = Fget_text_property (pos, Qlocal_map, string);
22417 if (!KEYMAPP (map))
22418 map = Fget_text_property (pos, Qkeymap, string);
22419 if (!KEYMAPP (map))
22420 cursor = dpyinfo->vertical_scroll_bar_cursor;
22423 /* Change the mouse face according to what is under X/Y. */
22424 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22425 if (!NILP (mouse_face)
22426 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22427 && glyph)
22429 Lisp_Object b, e;
22431 struct glyph * tmp_glyph;
22433 int gpos;
22434 int gseq_length;
22435 int total_pixel_width;
22436 int ignore;
22438 int vpos, hpos;
22440 b = Fprevious_single_property_change (make_number (charpos + 1),
22441 Qmouse_face, string, Qnil);
22442 if (NILP (b))
22443 b = make_number (0);
22445 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22446 if (NILP (e))
22447 e = make_number (SCHARS (string));
22449 /* Calculate the position(glyph position: GPOS) of GLYPH in
22450 displayed string. GPOS is different from CHARPOS.
22452 CHARPOS is the position of glyph in internal string
22453 object. A mode line string format has structures which
22454 is converted to a flatten by emacs lisp interpreter.
22455 The internal string is an element of the structures.
22456 The displayed string is the flatten string. */
22457 for (tmp_glyph = glyph - 1, gpos = 0;
22458 tmp_glyph->charpos >= XINT (b);
22459 tmp_glyph--, gpos++)
22461 if (!EQ (tmp_glyph->object, glyph->object))
22462 break;
22465 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22466 displayed string holding GLYPH.
22468 GSEQ_LENGTH is different from SCHARS (STRING).
22469 SCHARS (STRING) returns the length of the internal string. */
22470 for (tmp_glyph = glyph, gseq_length = gpos;
22471 tmp_glyph->charpos < XINT (e);
22472 tmp_glyph++, gseq_length++)
22474 if (!EQ (tmp_glyph->object, glyph->object))
22475 break;
22478 total_pixel_width = 0;
22479 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22480 total_pixel_width += tmp_glyph->pixel_width;
22482 /* Pre calculation of re-rendering position */
22483 vpos = (x - gpos);
22484 hpos = (area == ON_MODE_LINE
22485 ? (w->current_matrix)->nrows - 1
22486 : 0);
22488 /* If the re-rendering position is included in the last
22489 re-rendering area, we should do nothing. */
22490 if ( EQ (window, dpyinfo->mouse_face_window)
22491 && dpyinfo->mouse_face_beg_col <= vpos
22492 && vpos < dpyinfo->mouse_face_end_col
22493 && dpyinfo->mouse_face_beg_row == hpos )
22494 return;
22496 if (clear_mouse_face (dpyinfo))
22497 cursor = No_Cursor;
22499 dpyinfo->mouse_face_beg_col = vpos;
22500 dpyinfo->mouse_face_beg_row = hpos;
22502 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22503 dpyinfo->mouse_face_beg_y = 0;
22505 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22506 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22508 dpyinfo->mouse_face_end_x = 0;
22509 dpyinfo->mouse_face_end_y = 0;
22511 dpyinfo->mouse_face_past_end = 0;
22512 dpyinfo->mouse_face_window = window;
22514 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22515 charpos,
22516 0, 0, 0, &ignore,
22517 glyph->face_id, 1);
22518 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22520 if (NILP (pointer))
22521 pointer = Qhand;
22523 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22524 clear_mouse_face (dpyinfo);
22526 define_frame_cursor1 (f, cursor, pointer);
22530 /* EXPORT:
22531 Take proper action when the mouse has moved to position X, Y on
22532 frame F as regards highlighting characters that have mouse-face
22533 properties. Also de-highlighting chars where the mouse was before.
22534 X and Y can be negative or out of range. */
22536 void
22537 note_mouse_highlight (f, x, y)
22538 struct frame *f;
22539 int x, y;
22541 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22542 enum window_part part;
22543 Lisp_Object window;
22544 struct window *w;
22545 Cursor cursor = No_Cursor;
22546 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22547 struct buffer *b;
22549 /* When a menu is active, don't highlight because this looks odd. */
22550 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22551 if (popup_activated ())
22552 return;
22553 #endif
22555 if (NILP (Vmouse_highlight)
22556 || !f->glyphs_initialized_p)
22557 return;
22559 dpyinfo->mouse_face_mouse_x = x;
22560 dpyinfo->mouse_face_mouse_y = y;
22561 dpyinfo->mouse_face_mouse_frame = f;
22563 if (dpyinfo->mouse_face_defer)
22564 return;
22566 if (gc_in_progress)
22568 dpyinfo->mouse_face_deferred_gc = 1;
22569 return;
22572 /* Which window is that in? */
22573 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22575 /* If we were displaying active text in another window, clear that.
22576 Also clear if we move out of text area in same window. */
22577 if (! EQ (window, dpyinfo->mouse_face_window)
22578 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22579 && !NILP (dpyinfo->mouse_face_window)))
22580 clear_mouse_face (dpyinfo);
22582 /* Not on a window -> return. */
22583 if (!WINDOWP (window))
22584 return;
22586 /* Reset help_echo_string. It will get recomputed below. */
22587 help_echo_string = Qnil;
22589 /* Convert to window-relative pixel coordinates. */
22590 w = XWINDOW (window);
22591 frame_to_window_pixel_xy (w, &x, &y);
22593 /* Handle tool-bar window differently since it doesn't display a
22594 buffer. */
22595 if (EQ (window, f->tool_bar_window))
22597 note_tool_bar_highlight (f, x, y);
22598 return;
22601 /* Mouse is on the mode, header line or margin? */
22602 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22603 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22605 note_mode_line_or_margin_highlight (window, x, y, part);
22606 return;
22609 if (part == ON_VERTICAL_BORDER)
22611 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22612 help_echo_string = build_string ("drag-mouse-1: resize");
22614 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22615 || part == ON_SCROLL_BAR)
22616 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22617 else
22618 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22620 /* Are we in a window whose display is up to date?
22621 And verify the buffer's text has not changed. */
22622 b = XBUFFER (w->buffer);
22623 if (part == ON_TEXT
22624 && EQ (w->window_end_valid, w->buffer)
22625 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22626 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22628 int hpos, vpos, pos, i, dx, dy, area;
22629 struct glyph *glyph;
22630 Lisp_Object object;
22631 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22632 Lisp_Object *overlay_vec = NULL;
22633 int noverlays;
22634 struct buffer *obuf;
22635 int obegv, ozv, same_region;
22637 /* Find the glyph under X/Y. */
22638 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22640 /* Look for :pointer property on image. */
22641 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22643 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22644 if (img != NULL && IMAGEP (img->spec))
22646 Lisp_Object image_map, hotspot;
22647 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22648 !NILP (image_map))
22649 && (hotspot = find_hot_spot (image_map,
22650 glyph->slice.x + dx,
22651 glyph->slice.y + dy),
22652 CONSP (hotspot))
22653 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22655 Lisp_Object area_id, plist;
22657 area_id = XCAR (hotspot);
22658 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22659 If so, we could look for mouse-enter, mouse-leave
22660 properties in PLIST (and do something...). */
22661 hotspot = XCDR (hotspot);
22662 if (CONSP (hotspot)
22663 && (plist = XCAR (hotspot), CONSP (plist)))
22665 pointer = Fplist_get (plist, Qpointer);
22666 if (NILP (pointer))
22667 pointer = Qhand;
22668 help_echo_string = Fplist_get (plist, Qhelp_echo);
22669 if (!NILP (help_echo_string))
22671 help_echo_window = window;
22672 help_echo_object = glyph->object;
22673 help_echo_pos = glyph->charpos;
22677 if (NILP (pointer))
22678 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22682 /* Clear mouse face if X/Y not over text. */
22683 if (glyph == NULL
22684 || area != TEXT_AREA
22685 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22687 if (clear_mouse_face (dpyinfo))
22688 cursor = No_Cursor;
22689 if (NILP (pointer))
22691 if (area != TEXT_AREA)
22692 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22693 else
22694 pointer = Vvoid_text_area_pointer;
22696 goto set_cursor;
22699 pos = glyph->charpos;
22700 object = glyph->object;
22701 if (!STRINGP (object) && !BUFFERP (object))
22702 goto set_cursor;
22704 /* If we get an out-of-range value, return now; avoid an error. */
22705 if (BUFFERP (object) && pos > BUF_Z (b))
22706 goto set_cursor;
22708 /* Make the window's buffer temporarily current for
22709 overlays_at and compute_char_face. */
22710 obuf = current_buffer;
22711 current_buffer = b;
22712 obegv = BEGV;
22713 ozv = ZV;
22714 BEGV = BEG;
22715 ZV = Z;
22717 /* Is this char mouse-active or does it have help-echo? */
22718 position = make_number (pos);
22720 if (BUFFERP (object))
22722 /* Put all the overlays we want in a vector in overlay_vec. */
22723 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22724 /* Sort overlays into increasing priority order. */
22725 noverlays = sort_overlays (overlay_vec, noverlays, w);
22727 else
22728 noverlays = 0;
22730 same_region = (EQ (window, dpyinfo->mouse_face_window)
22731 && vpos >= dpyinfo->mouse_face_beg_row
22732 && vpos <= dpyinfo->mouse_face_end_row
22733 && (vpos > dpyinfo->mouse_face_beg_row
22734 || hpos >= dpyinfo->mouse_face_beg_col)
22735 && (vpos < dpyinfo->mouse_face_end_row
22736 || hpos < dpyinfo->mouse_face_end_col
22737 || dpyinfo->mouse_face_past_end));
22739 if (same_region)
22740 cursor = No_Cursor;
22742 /* Check mouse-face highlighting. */
22743 if (! same_region
22744 /* If there exists an overlay with mouse-face overlapping
22745 the one we are currently highlighting, we have to
22746 check if we enter the overlapping overlay, and then
22747 highlight only that. */
22748 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22749 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22751 /* Find the highest priority overlay that has a mouse-face
22752 property. */
22753 overlay = Qnil;
22754 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22756 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22757 if (!NILP (mouse_face))
22758 overlay = overlay_vec[i];
22761 /* If we're actually highlighting the same overlay as
22762 before, there's no need to do that again. */
22763 if (!NILP (overlay)
22764 && EQ (overlay, dpyinfo->mouse_face_overlay))
22765 goto check_help_echo;
22767 dpyinfo->mouse_face_overlay = overlay;
22769 /* Clear the display of the old active region, if any. */
22770 if (clear_mouse_face (dpyinfo))
22771 cursor = No_Cursor;
22773 /* If no overlay applies, get a text property. */
22774 if (NILP (overlay))
22775 mouse_face = Fget_text_property (position, Qmouse_face, object);
22777 /* Handle the overlay case. */
22778 if (!NILP (overlay))
22780 /* Find the range of text around this char that
22781 should be active. */
22782 Lisp_Object before, after;
22783 int ignore;
22785 before = Foverlay_start (overlay);
22786 after = Foverlay_end (overlay);
22787 /* Record this as the current active region. */
22788 fast_find_position (w, XFASTINT (before),
22789 &dpyinfo->mouse_face_beg_col,
22790 &dpyinfo->mouse_face_beg_row,
22791 &dpyinfo->mouse_face_beg_x,
22792 &dpyinfo->mouse_face_beg_y, Qnil);
22794 dpyinfo->mouse_face_past_end
22795 = !fast_find_position (w, XFASTINT (after),
22796 &dpyinfo->mouse_face_end_col,
22797 &dpyinfo->mouse_face_end_row,
22798 &dpyinfo->mouse_face_end_x,
22799 &dpyinfo->mouse_face_end_y, Qnil);
22800 dpyinfo->mouse_face_window = window;
22802 dpyinfo->mouse_face_face_id
22803 = face_at_buffer_position (w, pos, 0, 0,
22804 &ignore, pos + 1,
22805 !dpyinfo->mouse_face_hidden);
22807 /* Display it as active. */
22808 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22809 cursor = No_Cursor;
22811 /* Handle the text property case. */
22812 else if (!NILP (mouse_face) && BUFFERP (object))
22814 /* Find the range of text around this char that
22815 should be active. */
22816 Lisp_Object before, after, beginning, end;
22817 int ignore;
22819 beginning = Fmarker_position (w->start);
22820 end = make_number (BUF_Z (XBUFFER (object))
22821 - XFASTINT (w->window_end_pos));
22822 before
22823 = Fprevious_single_property_change (make_number (pos + 1),
22824 Qmouse_face,
22825 object, beginning);
22826 after
22827 = Fnext_single_property_change (position, Qmouse_face,
22828 object, end);
22830 /* Record this as the current active region. */
22831 fast_find_position (w, XFASTINT (before),
22832 &dpyinfo->mouse_face_beg_col,
22833 &dpyinfo->mouse_face_beg_row,
22834 &dpyinfo->mouse_face_beg_x,
22835 &dpyinfo->mouse_face_beg_y, Qnil);
22836 dpyinfo->mouse_face_past_end
22837 = !fast_find_position (w, XFASTINT (after),
22838 &dpyinfo->mouse_face_end_col,
22839 &dpyinfo->mouse_face_end_row,
22840 &dpyinfo->mouse_face_end_x,
22841 &dpyinfo->mouse_face_end_y, Qnil);
22842 dpyinfo->mouse_face_window = window;
22844 if (BUFFERP (object))
22845 dpyinfo->mouse_face_face_id
22846 = face_at_buffer_position (w, pos, 0, 0,
22847 &ignore, pos + 1,
22848 !dpyinfo->mouse_face_hidden);
22850 /* Display it as active. */
22851 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22852 cursor = No_Cursor;
22854 else if (!NILP (mouse_face) && STRINGP (object))
22856 Lisp_Object b, e;
22857 int ignore;
22859 b = Fprevious_single_property_change (make_number (pos + 1),
22860 Qmouse_face,
22861 object, Qnil);
22862 e = Fnext_single_property_change (position, Qmouse_face,
22863 object, Qnil);
22864 if (NILP (b))
22865 b = make_number (0);
22866 if (NILP (e))
22867 e = make_number (SCHARS (object) - 1);
22869 fast_find_string_pos (w, XINT (b), object,
22870 &dpyinfo->mouse_face_beg_col,
22871 &dpyinfo->mouse_face_beg_row,
22872 &dpyinfo->mouse_face_beg_x,
22873 &dpyinfo->mouse_face_beg_y, 0);
22874 fast_find_string_pos (w, XINT (e), object,
22875 &dpyinfo->mouse_face_end_col,
22876 &dpyinfo->mouse_face_end_row,
22877 &dpyinfo->mouse_face_end_x,
22878 &dpyinfo->mouse_face_end_y, 1);
22879 dpyinfo->mouse_face_past_end = 0;
22880 dpyinfo->mouse_face_window = window;
22881 dpyinfo->mouse_face_face_id
22882 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22883 glyph->face_id, 1);
22884 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22885 cursor = No_Cursor;
22887 else if (STRINGP (object) && NILP (mouse_face))
22889 /* A string which doesn't have mouse-face, but
22890 the text ``under'' it might have. */
22891 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22892 int start = MATRIX_ROW_START_CHARPOS (r);
22894 pos = string_buffer_position (w, object, start);
22895 if (pos > 0)
22896 mouse_face = get_char_property_and_overlay (make_number (pos),
22897 Qmouse_face,
22898 w->buffer,
22899 &overlay);
22900 if (!NILP (mouse_face) && !NILP (overlay))
22902 Lisp_Object before = Foverlay_start (overlay);
22903 Lisp_Object after = Foverlay_end (overlay);
22904 int ignore;
22906 /* Note that we might not be able to find position
22907 BEFORE in the glyph matrix if the overlay is
22908 entirely covered by a `display' property. In
22909 this case, we overshoot. So let's stop in
22910 the glyph matrix before glyphs for OBJECT. */
22911 fast_find_position (w, XFASTINT (before),
22912 &dpyinfo->mouse_face_beg_col,
22913 &dpyinfo->mouse_face_beg_row,
22914 &dpyinfo->mouse_face_beg_x,
22915 &dpyinfo->mouse_face_beg_y,
22916 object);
22918 dpyinfo->mouse_face_past_end
22919 = !fast_find_position (w, XFASTINT (after),
22920 &dpyinfo->mouse_face_end_col,
22921 &dpyinfo->mouse_face_end_row,
22922 &dpyinfo->mouse_face_end_x,
22923 &dpyinfo->mouse_face_end_y,
22924 Qnil);
22925 dpyinfo->mouse_face_window = window;
22926 dpyinfo->mouse_face_face_id
22927 = face_at_buffer_position (w, pos, 0, 0,
22928 &ignore, pos + 1,
22929 !dpyinfo->mouse_face_hidden);
22931 /* Display it as active. */
22932 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22933 cursor = No_Cursor;
22938 check_help_echo:
22940 /* Look for a `help-echo' property. */
22941 if (NILP (help_echo_string)) {
22942 Lisp_Object help, overlay;
22944 /* Check overlays first. */
22945 help = overlay = Qnil;
22946 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22948 overlay = overlay_vec[i];
22949 help = Foverlay_get (overlay, Qhelp_echo);
22952 if (!NILP (help))
22954 help_echo_string = help;
22955 help_echo_window = window;
22956 help_echo_object = overlay;
22957 help_echo_pos = pos;
22959 else
22961 Lisp_Object object = glyph->object;
22962 int charpos = glyph->charpos;
22964 /* Try text properties. */
22965 if (STRINGP (object)
22966 && charpos >= 0
22967 && charpos < SCHARS (object))
22969 help = Fget_text_property (make_number (charpos),
22970 Qhelp_echo, object);
22971 if (NILP (help))
22973 /* If the string itself doesn't specify a help-echo,
22974 see if the buffer text ``under'' it does. */
22975 struct glyph_row *r
22976 = MATRIX_ROW (w->current_matrix, vpos);
22977 int start = MATRIX_ROW_START_CHARPOS (r);
22978 int pos = string_buffer_position (w, object, start);
22979 if (pos > 0)
22981 help = Fget_char_property (make_number (pos),
22982 Qhelp_echo, w->buffer);
22983 if (!NILP (help))
22985 charpos = pos;
22986 object = w->buffer;
22991 else if (BUFFERP (object)
22992 && charpos >= BEGV
22993 && charpos < ZV)
22994 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22995 object);
22997 if (!NILP (help))
22999 help_echo_string = help;
23000 help_echo_window = window;
23001 help_echo_object = object;
23002 help_echo_pos = charpos;
23007 /* Look for a `pointer' property. */
23008 if (NILP (pointer))
23010 /* Check overlays first. */
23011 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23012 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23014 if (NILP (pointer))
23016 Lisp_Object object = glyph->object;
23017 int charpos = glyph->charpos;
23019 /* Try text properties. */
23020 if (STRINGP (object)
23021 && charpos >= 0
23022 && charpos < SCHARS (object))
23024 pointer = Fget_text_property (make_number (charpos),
23025 Qpointer, object);
23026 if (NILP (pointer))
23028 /* If the string itself doesn't specify a pointer,
23029 see if the buffer text ``under'' it does. */
23030 struct glyph_row *r
23031 = MATRIX_ROW (w->current_matrix, vpos);
23032 int start = MATRIX_ROW_START_CHARPOS (r);
23033 int pos = string_buffer_position (w, object, start);
23034 if (pos > 0)
23035 pointer = Fget_char_property (make_number (pos),
23036 Qpointer, w->buffer);
23039 else if (BUFFERP (object)
23040 && charpos >= BEGV
23041 && charpos < ZV)
23042 pointer = Fget_text_property (make_number (charpos),
23043 Qpointer, object);
23047 BEGV = obegv;
23048 ZV = ozv;
23049 current_buffer = obuf;
23052 set_cursor:
23054 define_frame_cursor1 (f, cursor, pointer);
23058 /* EXPORT for RIF:
23059 Clear any mouse-face on window W. This function is part of the
23060 redisplay interface, and is called from try_window_id and similar
23061 functions to ensure the mouse-highlight is off. */
23063 void
23064 x_clear_window_mouse_face (w)
23065 struct window *w;
23067 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23068 Lisp_Object window;
23070 BLOCK_INPUT;
23071 XSETWINDOW (window, w);
23072 if (EQ (window, dpyinfo->mouse_face_window))
23073 clear_mouse_face (dpyinfo);
23074 UNBLOCK_INPUT;
23078 /* EXPORT:
23079 Just discard the mouse face information for frame F, if any.
23080 This is used when the size of F is changed. */
23082 void
23083 cancel_mouse_face (f)
23084 struct frame *f;
23086 Lisp_Object window;
23087 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23089 window = dpyinfo->mouse_face_window;
23090 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23092 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23093 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23094 dpyinfo->mouse_face_window = Qnil;
23099 #endif /* HAVE_WINDOW_SYSTEM */
23102 /***********************************************************************
23103 Exposure Events
23104 ***********************************************************************/
23106 #ifdef HAVE_WINDOW_SYSTEM
23108 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23109 which intersects rectangle R. R is in window-relative coordinates. */
23111 static void
23112 expose_area (w, row, r, area)
23113 struct window *w;
23114 struct glyph_row *row;
23115 XRectangle *r;
23116 enum glyph_row_area area;
23118 struct glyph *first = row->glyphs[area];
23119 struct glyph *end = row->glyphs[area] + row->used[area];
23120 struct glyph *last;
23121 int first_x, start_x, x;
23123 if (area == TEXT_AREA && row->fill_line_p)
23124 /* If row extends face to end of line write the whole line. */
23125 draw_glyphs (w, 0, row, area,
23126 0, row->used[area],
23127 DRAW_NORMAL_TEXT, 0);
23128 else
23130 /* Set START_X to the window-relative start position for drawing glyphs of
23131 AREA. The first glyph of the text area can be partially visible.
23132 The first glyphs of other areas cannot. */
23133 start_x = window_box_left_offset (w, area);
23134 x = start_x;
23135 if (area == TEXT_AREA)
23136 x += row->x;
23138 /* Find the first glyph that must be redrawn. */
23139 while (first < end
23140 && x + first->pixel_width < r->x)
23142 x += first->pixel_width;
23143 ++first;
23146 /* Find the last one. */
23147 last = first;
23148 first_x = x;
23149 while (last < end
23150 && x < r->x + r->width)
23152 x += last->pixel_width;
23153 ++last;
23156 /* Repaint. */
23157 if (last > first)
23158 draw_glyphs (w, first_x - start_x, row, area,
23159 first - row->glyphs[area], last - row->glyphs[area],
23160 DRAW_NORMAL_TEXT, 0);
23165 /* Redraw the parts of the glyph row ROW on window W intersecting
23166 rectangle R. R is in window-relative coordinates. Value is
23167 non-zero if mouse-face was overwritten. */
23169 static int
23170 expose_line (w, row, r)
23171 struct window *w;
23172 struct glyph_row *row;
23173 XRectangle *r;
23175 xassert (row->enabled_p);
23177 if (row->mode_line_p || w->pseudo_window_p)
23178 draw_glyphs (w, 0, row, TEXT_AREA,
23179 0, row->used[TEXT_AREA],
23180 DRAW_NORMAL_TEXT, 0);
23181 else
23183 if (row->used[LEFT_MARGIN_AREA])
23184 expose_area (w, row, r, LEFT_MARGIN_AREA);
23185 if (row->used[TEXT_AREA])
23186 expose_area (w, row, r, TEXT_AREA);
23187 if (row->used[RIGHT_MARGIN_AREA])
23188 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23189 draw_row_fringe_bitmaps (w, row);
23192 return row->mouse_face_p;
23196 /* Redraw those parts of glyphs rows during expose event handling that
23197 overlap other rows. Redrawing of an exposed line writes over parts
23198 of lines overlapping that exposed line; this function fixes that.
23200 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23201 row in W's current matrix that is exposed and overlaps other rows.
23202 LAST_OVERLAPPING_ROW is the last such row. */
23204 static void
23205 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23206 struct window *w;
23207 struct glyph_row *first_overlapping_row;
23208 struct glyph_row *last_overlapping_row;
23210 struct glyph_row *row;
23212 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23213 if (row->overlapping_p)
23215 xassert (row->enabled_p && !row->mode_line_p);
23217 if (row->used[LEFT_MARGIN_AREA])
23218 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23220 if (row->used[TEXT_AREA])
23221 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23223 if (row->used[RIGHT_MARGIN_AREA])
23224 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23229 /* Return non-zero if W's cursor intersects rectangle R. */
23231 static int
23232 phys_cursor_in_rect_p (w, r)
23233 struct window *w;
23234 XRectangle *r;
23236 XRectangle cr, result;
23237 struct glyph *cursor_glyph;
23239 cursor_glyph = get_phys_cursor_glyph (w);
23240 if (cursor_glyph)
23242 /* r is relative to W's box, but w->phys_cursor.x is relative
23243 to left edge of W's TEXT area. Adjust it. */
23244 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23245 cr.y = w->phys_cursor.y;
23246 cr.width = cursor_glyph->pixel_width;
23247 cr.height = w->phys_cursor_height;
23248 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23249 I assume the effect is the same -- and this is portable. */
23250 return x_intersect_rectangles (&cr, r, &result);
23252 else
23253 return 0;
23257 /* EXPORT:
23258 Draw a vertical window border to the right of window W if W doesn't
23259 have vertical scroll bars. */
23261 void
23262 x_draw_vertical_border (w)
23263 struct window *w;
23265 /* We could do better, if we knew what type of scroll-bar the adjacent
23266 windows (on either side) have... But we don't :-(
23267 However, I think this works ok. ++KFS 2003-04-25 */
23269 /* Redraw borders between horizontally adjacent windows. Don't
23270 do it for frames with vertical scroll bars because either the
23271 right scroll bar of a window, or the left scroll bar of its
23272 neighbor will suffice as a border. */
23273 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23274 return;
23276 if (!WINDOW_RIGHTMOST_P (w)
23277 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23279 int x0, x1, y0, y1;
23281 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23282 y1 -= 1;
23284 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23285 x1 -= 1;
23287 rif->draw_vertical_window_border (w, x1, y0, y1);
23289 else if (!WINDOW_LEFTMOST_P (w)
23290 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23292 int x0, x1, y0, y1;
23294 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23295 y1 -= 1;
23297 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23298 x0 -= 1;
23300 rif->draw_vertical_window_border (w, x0, y0, y1);
23305 /* Redraw the part of window W intersection rectangle FR. Pixel
23306 coordinates in FR are frame-relative. Call this function with
23307 input blocked. Value is non-zero if the exposure overwrites
23308 mouse-face. */
23310 static int
23311 expose_window (w, fr)
23312 struct window *w;
23313 XRectangle *fr;
23315 struct frame *f = XFRAME (w->frame);
23316 XRectangle wr, r;
23317 int mouse_face_overwritten_p = 0;
23319 /* If window is not yet fully initialized, do nothing. This can
23320 happen when toolkit scroll bars are used and a window is split.
23321 Reconfiguring the scroll bar will generate an expose for a newly
23322 created window. */
23323 if (w->current_matrix == NULL)
23324 return 0;
23326 /* When we're currently updating the window, display and current
23327 matrix usually don't agree. Arrange for a thorough display
23328 later. */
23329 if (w == updated_window)
23331 SET_FRAME_GARBAGED (f);
23332 return 0;
23335 /* Frame-relative pixel rectangle of W. */
23336 wr.x = WINDOW_LEFT_EDGE_X (w);
23337 wr.y = WINDOW_TOP_EDGE_Y (w);
23338 wr.width = WINDOW_TOTAL_WIDTH (w);
23339 wr.height = WINDOW_TOTAL_HEIGHT (w);
23341 if (x_intersect_rectangles (fr, &wr, &r))
23343 int yb = window_text_bottom_y (w);
23344 struct glyph_row *row;
23345 int cursor_cleared_p;
23346 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23348 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23349 r.x, r.y, r.width, r.height));
23351 /* Convert to window coordinates. */
23352 r.x -= WINDOW_LEFT_EDGE_X (w);
23353 r.y -= WINDOW_TOP_EDGE_Y (w);
23355 /* Turn off the cursor. */
23356 if (!w->pseudo_window_p
23357 && phys_cursor_in_rect_p (w, &r))
23359 x_clear_cursor (w);
23360 cursor_cleared_p = 1;
23362 else
23363 cursor_cleared_p = 0;
23365 /* Update lines intersecting rectangle R. */
23366 first_overlapping_row = last_overlapping_row = NULL;
23367 for (row = w->current_matrix->rows;
23368 row->enabled_p;
23369 ++row)
23371 int y0 = row->y;
23372 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23374 if ((y0 >= r.y && y0 < r.y + r.height)
23375 || (y1 > r.y && y1 < r.y + r.height)
23376 || (r.y >= y0 && r.y < y1)
23377 || (r.y + r.height > y0 && r.y + r.height < y1))
23379 /* A header line may be overlapping, but there is no need
23380 to fix overlapping areas for them. KFS 2005-02-12 */
23381 if (row->overlapping_p && !row->mode_line_p)
23383 if (first_overlapping_row == NULL)
23384 first_overlapping_row = row;
23385 last_overlapping_row = row;
23388 if (expose_line (w, row, &r))
23389 mouse_face_overwritten_p = 1;
23392 if (y1 >= yb)
23393 break;
23396 /* Display the mode line if there is one. */
23397 if (WINDOW_WANTS_MODELINE_P (w)
23398 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23399 row->enabled_p)
23400 && row->y < r.y + r.height)
23402 if (expose_line (w, row, &r))
23403 mouse_face_overwritten_p = 1;
23406 if (!w->pseudo_window_p)
23408 /* Fix the display of overlapping rows. */
23409 if (first_overlapping_row)
23410 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23412 /* Draw border between windows. */
23413 x_draw_vertical_border (w);
23415 /* Turn the cursor on again. */
23416 if (cursor_cleared_p)
23417 update_window_cursor (w, 1);
23421 return mouse_face_overwritten_p;
23426 /* Redraw (parts) of all windows in the window tree rooted at W that
23427 intersect R. R contains frame pixel coordinates. Value is
23428 non-zero if the exposure overwrites mouse-face. */
23430 static int
23431 expose_window_tree (w, r)
23432 struct window *w;
23433 XRectangle *r;
23435 struct frame *f = XFRAME (w->frame);
23436 int mouse_face_overwritten_p = 0;
23438 while (w && !FRAME_GARBAGED_P (f))
23440 if (!NILP (w->hchild))
23441 mouse_face_overwritten_p
23442 |= expose_window_tree (XWINDOW (w->hchild), r);
23443 else if (!NILP (w->vchild))
23444 mouse_face_overwritten_p
23445 |= expose_window_tree (XWINDOW (w->vchild), r);
23446 else
23447 mouse_face_overwritten_p |= expose_window (w, r);
23449 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23452 return mouse_face_overwritten_p;
23456 /* EXPORT:
23457 Redisplay an exposed area of frame F. X and Y are the upper-left
23458 corner of the exposed rectangle. W and H are width and height of
23459 the exposed area. All are pixel values. W or H zero means redraw
23460 the entire frame. */
23462 void
23463 expose_frame (f, x, y, w, h)
23464 struct frame *f;
23465 int x, y, w, h;
23467 XRectangle r;
23468 int mouse_face_overwritten_p = 0;
23470 TRACE ((stderr, "expose_frame "));
23472 /* No need to redraw if frame will be redrawn soon. */
23473 if (FRAME_GARBAGED_P (f))
23475 TRACE ((stderr, " garbaged\n"));
23476 return;
23479 /* If basic faces haven't been realized yet, there is no point in
23480 trying to redraw anything. This can happen when we get an expose
23481 event while Emacs is starting, e.g. by moving another window. */
23482 if (FRAME_FACE_CACHE (f) == NULL
23483 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23485 TRACE ((stderr, " no faces\n"));
23486 return;
23489 if (w == 0 || h == 0)
23491 r.x = r.y = 0;
23492 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23493 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23495 else
23497 r.x = x;
23498 r.y = y;
23499 r.width = w;
23500 r.height = h;
23503 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23504 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23506 if (WINDOWP (f->tool_bar_window))
23507 mouse_face_overwritten_p
23508 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23510 #ifdef HAVE_X_WINDOWS
23511 #ifndef MSDOS
23512 #ifndef USE_X_TOOLKIT
23513 if (WINDOWP (f->menu_bar_window))
23514 mouse_face_overwritten_p
23515 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23516 #endif /* not USE_X_TOOLKIT */
23517 #endif
23518 #endif
23520 /* Some window managers support a focus-follows-mouse style with
23521 delayed raising of frames. Imagine a partially obscured frame,
23522 and moving the mouse into partially obscured mouse-face on that
23523 frame. The visible part of the mouse-face will be highlighted,
23524 then the WM raises the obscured frame. With at least one WM, KDE
23525 2.1, Emacs is not getting any event for the raising of the frame
23526 (even tried with SubstructureRedirectMask), only Expose events.
23527 These expose events will draw text normally, i.e. not
23528 highlighted. Which means we must redo the highlight here.
23529 Subsume it under ``we love X''. --gerd 2001-08-15 */
23530 /* Included in Windows version because Windows most likely does not
23531 do the right thing if any third party tool offers
23532 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23533 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23535 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23536 if (f == dpyinfo->mouse_face_mouse_frame)
23538 int x = dpyinfo->mouse_face_mouse_x;
23539 int y = dpyinfo->mouse_face_mouse_y;
23540 clear_mouse_face (dpyinfo);
23541 note_mouse_highlight (f, x, y);
23547 /* EXPORT:
23548 Determine the intersection of two rectangles R1 and R2. Return
23549 the intersection in *RESULT. Value is non-zero if RESULT is not
23550 empty. */
23553 x_intersect_rectangles (r1, r2, result)
23554 XRectangle *r1, *r2, *result;
23556 XRectangle *left, *right;
23557 XRectangle *upper, *lower;
23558 int intersection_p = 0;
23560 /* Rearrange so that R1 is the left-most rectangle. */
23561 if (r1->x < r2->x)
23562 left = r1, right = r2;
23563 else
23564 left = r2, right = r1;
23566 /* X0 of the intersection is right.x0, if this is inside R1,
23567 otherwise there is no intersection. */
23568 if (right->x <= left->x + left->width)
23570 result->x = right->x;
23572 /* The right end of the intersection is the minimum of the
23573 the right ends of left and right. */
23574 result->width = (min (left->x + left->width, right->x + right->width)
23575 - result->x);
23577 /* Same game for Y. */
23578 if (r1->y < r2->y)
23579 upper = r1, lower = r2;
23580 else
23581 upper = r2, lower = r1;
23583 /* The upper end of the intersection is lower.y0, if this is inside
23584 of upper. Otherwise, there is no intersection. */
23585 if (lower->y <= upper->y + upper->height)
23587 result->y = lower->y;
23589 /* The lower end of the intersection is the minimum of the lower
23590 ends of upper and lower. */
23591 result->height = (min (lower->y + lower->height,
23592 upper->y + upper->height)
23593 - result->y);
23594 intersection_p = 1;
23598 return intersection_p;
23601 #endif /* HAVE_WINDOW_SYSTEM */
23604 /***********************************************************************
23605 Initialization
23606 ***********************************************************************/
23608 void
23609 syms_of_xdisp ()
23611 Vwith_echo_area_save_vector = Qnil;
23612 staticpro (&Vwith_echo_area_save_vector);
23614 Vmessage_stack = Qnil;
23615 staticpro (&Vmessage_stack);
23617 Qinhibit_redisplay = intern ("inhibit-redisplay");
23618 staticpro (&Qinhibit_redisplay);
23620 message_dolog_marker1 = Fmake_marker ();
23621 staticpro (&message_dolog_marker1);
23622 message_dolog_marker2 = Fmake_marker ();
23623 staticpro (&message_dolog_marker2);
23624 message_dolog_marker3 = Fmake_marker ();
23625 staticpro (&message_dolog_marker3);
23627 #if GLYPH_DEBUG
23628 defsubr (&Sdump_frame_glyph_matrix);
23629 defsubr (&Sdump_glyph_matrix);
23630 defsubr (&Sdump_glyph_row);
23631 defsubr (&Sdump_tool_bar_row);
23632 defsubr (&Strace_redisplay);
23633 defsubr (&Strace_to_stderr);
23634 #endif
23635 #ifdef HAVE_WINDOW_SYSTEM
23636 defsubr (&Stool_bar_lines_needed);
23637 defsubr (&Slookup_image_map);
23638 #endif
23639 defsubr (&Sformat_mode_line);
23641 staticpro (&Qmenu_bar_update_hook);
23642 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23644 staticpro (&Qoverriding_terminal_local_map);
23645 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23647 staticpro (&Qoverriding_local_map);
23648 Qoverriding_local_map = intern ("overriding-local-map");
23650 staticpro (&Qwindow_scroll_functions);
23651 Qwindow_scroll_functions = intern ("window-scroll-functions");
23653 staticpro (&Qredisplay_end_trigger_functions);
23654 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23656 staticpro (&Qinhibit_point_motion_hooks);
23657 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23659 QCdata = intern (":data");
23660 staticpro (&QCdata);
23661 Qdisplay = intern ("display");
23662 staticpro (&Qdisplay);
23663 Qspace_width = intern ("space-width");
23664 staticpro (&Qspace_width);
23665 Qraise = intern ("raise");
23666 staticpro (&Qraise);
23667 Qslice = intern ("slice");
23668 staticpro (&Qslice);
23669 Qspace = intern ("space");
23670 staticpro (&Qspace);
23671 Qmargin = intern ("margin");
23672 staticpro (&Qmargin);
23673 Qpointer = intern ("pointer");
23674 staticpro (&Qpointer);
23675 Qleft_margin = intern ("left-margin");
23676 staticpro (&Qleft_margin);
23677 Qright_margin = intern ("right-margin");
23678 staticpro (&Qright_margin);
23679 Qcenter = intern ("center");
23680 staticpro (&Qcenter);
23681 Qline_height = intern ("line-height");
23682 staticpro (&Qline_height);
23683 QCalign_to = intern (":align-to");
23684 staticpro (&QCalign_to);
23685 QCrelative_width = intern (":relative-width");
23686 staticpro (&QCrelative_width);
23687 QCrelative_height = intern (":relative-height");
23688 staticpro (&QCrelative_height);
23689 QCeval = intern (":eval");
23690 staticpro (&QCeval);
23691 QCpropertize = intern (":propertize");
23692 staticpro (&QCpropertize);
23693 QCfile = intern (":file");
23694 staticpro (&QCfile);
23695 Qfontified = intern ("fontified");
23696 staticpro (&Qfontified);
23697 Qfontification_functions = intern ("fontification-functions");
23698 staticpro (&Qfontification_functions);
23699 Qtrailing_whitespace = intern ("trailing-whitespace");
23700 staticpro (&Qtrailing_whitespace);
23701 Qescape_glyph = intern ("escape-glyph");
23702 staticpro (&Qescape_glyph);
23703 Qnobreak_space = intern ("nobreak-space");
23704 staticpro (&Qnobreak_space);
23705 Qimage = intern ("image");
23706 staticpro (&Qimage);
23707 QCmap = intern (":map");
23708 staticpro (&QCmap);
23709 QCpointer = intern (":pointer");
23710 staticpro (&QCpointer);
23711 Qrect = intern ("rect");
23712 staticpro (&Qrect);
23713 Qcircle = intern ("circle");
23714 staticpro (&Qcircle);
23715 Qpoly = intern ("poly");
23716 staticpro (&Qpoly);
23717 Qmessage_truncate_lines = intern ("message-truncate-lines");
23718 staticpro (&Qmessage_truncate_lines);
23719 Qgrow_only = intern ("grow-only");
23720 staticpro (&Qgrow_only);
23721 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23722 staticpro (&Qinhibit_menubar_update);
23723 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23724 staticpro (&Qinhibit_eval_during_redisplay);
23725 Qposition = intern ("position");
23726 staticpro (&Qposition);
23727 Qbuffer_position = intern ("buffer-position");
23728 staticpro (&Qbuffer_position);
23729 Qobject = intern ("object");
23730 staticpro (&Qobject);
23731 Qbar = intern ("bar");
23732 staticpro (&Qbar);
23733 Qhbar = intern ("hbar");
23734 staticpro (&Qhbar);
23735 Qbox = intern ("box");
23736 staticpro (&Qbox);
23737 Qhollow = intern ("hollow");
23738 staticpro (&Qhollow);
23739 Qhand = intern ("hand");
23740 staticpro (&Qhand);
23741 Qarrow = intern ("arrow");
23742 staticpro (&Qarrow);
23743 Qtext = intern ("text");
23744 staticpro (&Qtext);
23745 Qrisky_local_variable = intern ("risky-local-variable");
23746 staticpro (&Qrisky_local_variable);
23747 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23748 staticpro (&Qinhibit_free_realized_faces);
23750 list_of_error = Fcons (Fcons (intern ("error"),
23751 Fcons (intern ("void-variable"), Qnil)),
23752 Qnil);
23753 staticpro (&list_of_error);
23755 Qlast_arrow_position = intern ("last-arrow-position");
23756 staticpro (&Qlast_arrow_position);
23757 Qlast_arrow_string = intern ("last-arrow-string");
23758 staticpro (&Qlast_arrow_string);
23760 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23761 staticpro (&Qoverlay_arrow_string);
23762 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23763 staticpro (&Qoverlay_arrow_bitmap);
23765 echo_buffer[0] = echo_buffer[1] = Qnil;
23766 staticpro (&echo_buffer[0]);
23767 staticpro (&echo_buffer[1]);
23769 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23770 staticpro (&echo_area_buffer[0]);
23771 staticpro (&echo_area_buffer[1]);
23773 Vmessages_buffer_name = build_string ("*Messages*");
23774 staticpro (&Vmessages_buffer_name);
23776 mode_line_proptrans_alist = Qnil;
23777 staticpro (&mode_line_proptrans_alist);
23778 mode_line_string_list = Qnil;
23779 staticpro (&mode_line_string_list);
23780 mode_line_string_face = Qnil;
23781 staticpro (&mode_line_string_face);
23782 mode_line_string_face_prop = Qnil;
23783 staticpro (&mode_line_string_face_prop);
23784 Vmode_line_unwind_vector = Qnil;
23785 staticpro (&Vmode_line_unwind_vector);
23787 help_echo_string = Qnil;
23788 staticpro (&help_echo_string);
23789 help_echo_object = Qnil;
23790 staticpro (&help_echo_object);
23791 help_echo_window = Qnil;
23792 staticpro (&help_echo_window);
23793 previous_help_echo_string = Qnil;
23794 staticpro (&previous_help_echo_string);
23795 help_echo_pos = -1;
23797 #ifdef HAVE_WINDOW_SYSTEM
23798 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23799 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23800 For example, if a block cursor is over a tab, it will be drawn as
23801 wide as that tab on the display. */);
23802 x_stretch_cursor_p = 0;
23803 #endif
23805 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23806 doc: /* *Non-nil means highlight trailing whitespace.
23807 The face used for trailing whitespace is `trailing-whitespace'. */);
23808 Vshow_trailing_whitespace = Qnil;
23810 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23811 doc: /* *Control highlighting of nobreak space and soft hyphen.
23812 A value of t means highlight the character itself (for nobreak space,
23813 use face `nobreak-space').
23814 A value of nil means no highlighting.
23815 Other values mean display the escape glyph followed by an ordinary
23816 space or ordinary hyphen. */);
23817 Vnobreak_char_display = Qt;
23819 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23820 doc: /* *The pointer shape to show in void text areas.
23821 A value of nil means to show the text pointer. Other options are `arrow',
23822 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23823 Vvoid_text_area_pointer = Qarrow;
23825 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23826 doc: /* Non-nil means don't actually do any redisplay.
23827 This is used for internal purposes. */);
23828 Vinhibit_redisplay = Qnil;
23830 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23831 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23832 Vglobal_mode_string = Qnil;
23834 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23835 doc: /* Marker for where to display an arrow on top of the buffer text.
23836 This must be the beginning of a line in order to work.
23837 See also `overlay-arrow-string'. */);
23838 Voverlay_arrow_position = Qnil;
23840 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23841 doc: /* String to display as an arrow in non-window frames.
23842 See also `overlay-arrow-position'. */);
23843 Voverlay_arrow_string = build_string ("=>");
23845 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23846 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23847 The symbols on this list are examined during redisplay to determine
23848 where to display overlay arrows. */);
23849 Voverlay_arrow_variable_list
23850 = Fcons (intern ("overlay-arrow-position"), Qnil);
23852 DEFVAR_INT ("scroll-step", &scroll_step,
23853 doc: /* *The number of lines to try scrolling a window by when point moves out.
23854 If that fails to bring point back on frame, point is centered instead.
23855 If this is zero, point is always centered after it moves off frame.
23856 If you want scrolling to always be a line at a time, you should set
23857 `scroll-conservatively' to a large value rather than set this to 1. */);
23859 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23860 doc: /* *Scroll up to this many lines, to bring point back on screen.
23861 A value of zero means to scroll the text to center point vertically
23862 in the window. */);
23863 scroll_conservatively = 0;
23865 DEFVAR_INT ("scroll-margin", &scroll_margin,
23866 doc: /* *Number of lines of margin at the top and bottom of a window.
23867 Recenter the window whenever point gets within this many lines
23868 of the top or bottom of the window. */);
23869 scroll_margin = 0;
23871 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23872 doc: /* Pixels per inch value for non-window system displays.
23873 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23874 Vdisplay_pixels_per_inch = make_float (72.0);
23876 #if GLYPH_DEBUG
23877 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23878 #endif
23880 DEFVAR_BOOL ("truncate-partial-width-windows",
23881 &truncate_partial_width_windows,
23882 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23883 truncate_partial_width_windows = 1;
23885 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23886 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23887 Any other value means to use the appropriate face, `mode-line',
23888 `header-line', or `menu' respectively. */);
23889 mode_line_inverse_video = 1;
23891 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23892 doc: /* *Maximum buffer size for which line number should be displayed.
23893 If the buffer is bigger than this, the line number does not appear
23894 in the mode line. A value of nil means no limit. */);
23895 Vline_number_display_limit = Qnil;
23897 DEFVAR_INT ("line-number-display-limit-width",
23898 &line_number_display_limit_width,
23899 doc: /* *Maximum line width (in characters) for line number display.
23900 If the average length of the lines near point is bigger than this, then the
23901 line number may be omitted from the mode line. */);
23902 line_number_display_limit_width = 200;
23904 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23905 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23906 highlight_nonselected_windows = 0;
23908 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23909 doc: /* Non-nil if more than one frame is visible on this display.
23910 Minibuffer-only frames don't count, but iconified frames do.
23911 This variable is not guaranteed to be accurate except while processing
23912 `frame-title-format' and `icon-title-format'. */);
23914 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23915 doc: /* Template for displaying the title bar of visible frames.
23916 \(Assuming the window manager supports this feature.)
23917 This variable has the same structure as `mode-line-format' (which see),
23918 and is used only on frames for which no explicit name has been set
23919 \(see `modify-frame-parameters'). */);
23921 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23922 doc: /* Template for displaying the title bar of an iconified frame.
23923 \(Assuming the window manager supports this feature.)
23924 This variable has the same structure as `mode-line-format' (which see),
23925 and is used only on frames for which no explicit name has been set
23926 \(see `modify-frame-parameters'). */);
23927 Vicon_title_format
23928 = Vframe_title_format
23929 = Fcons (intern ("multiple-frames"),
23930 Fcons (build_string ("%b"),
23931 Fcons (Fcons (empty_string,
23932 Fcons (intern ("invocation-name"),
23933 Fcons (build_string ("@"),
23934 Fcons (intern ("system-name"),
23935 Qnil)))),
23936 Qnil)));
23938 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23939 doc: /* Maximum number of lines to keep in the message log buffer.
23940 If nil, disable message logging. If t, log messages but don't truncate
23941 the buffer when it becomes large. */);
23942 Vmessage_log_max = make_number (50);
23944 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23945 doc: /* Functions called before redisplay, if window sizes have changed.
23946 The value should be a list of functions that take one argument.
23947 Just before redisplay, for each frame, if any of its windows have changed
23948 size since the last redisplay, or have been split or deleted,
23949 all the functions in the list are called, with the frame as argument. */);
23950 Vwindow_size_change_functions = Qnil;
23952 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23953 doc: /* List of functions to call before redisplaying a window with scrolling.
23954 Each function is called with two arguments, the window
23955 and its new display-start position. Note that the value of `window-end'
23956 is not valid when these functions are called. */);
23957 Vwindow_scroll_functions = Qnil;
23959 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23960 doc: /* Functions called when redisplay of a window reaches the end trigger.
23961 Each function is called with two arguments, the window and the end trigger value.
23962 See `set-window-redisplay-end-trigger'. */);
23963 Vredisplay_end_trigger_functions = Qnil;
23965 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23966 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23967 mouse_autoselect_window = 0;
23969 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23970 doc: /* *Non-nil means automatically resize tool-bars.
23971 This increases a tool-bar's height if not all tool-bar items are visible.
23972 It decreases a tool-bar's height when it would display blank lines
23973 otherwise. */);
23974 auto_resize_tool_bars_p = 1;
23976 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23977 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23978 auto_raise_tool_bar_buttons_p = 1;
23980 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23981 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23982 make_cursor_line_fully_visible_p = 1;
23984 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23985 doc: /* *Border below tool-bar in pixels.
23986 If an integer, use it as the height of the border.
23987 If it is one of `internal-border-width' or `border-width', use the
23988 value of the corresponding frame parameter.
23989 Otherwise, no border is added below the tool-bar. */);
23990 Vtool_bar_border = Qinternal_border_width;
23992 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23993 doc: /* *Margin around tool-bar buttons in pixels.
23994 If an integer, use that for both horizontal and vertical margins.
23995 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23996 HORZ specifying the horizontal margin, and VERT specifying the
23997 vertical margin. */);
23998 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24000 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24001 doc: /* *Relief thickness of tool-bar buttons. */);
24002 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24004 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24005 doc: /* List of functions to call to fontify regions of text.
24006 Each function is called with one argument POS. Functions must
24007 fontify a region starting at POS in the current buffer, and give
24008 fontified regions the property `fontified'. */);
24009 Vfontification_functions = Qnil;
24010 Fmake_variable_buffer_local (Qfontification_functions);
24012 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24013 &unibyte_display_via_language_environment,
24014 doc: /* *Non-nil means display unibyte text according to language environment.
24015 Specifically this means that unibyte non-ASCII characters
24016 are displayed by converting them to the equivalent multibyte characters
24017 according to the current language environment. As a result, they are
24018 displayed according to the current fontset. */);
24019 unibyte_display_via_language_environment = 0;
24021 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24022 doc: /* *Maximum height for resizing mini-windows.
24023 If a float, it specifies a fraction of the mini-window frame's height.
24024 If an integer, it specifies a number of lines. */);
24025 Vmax_mini_window_height = make_float (0.25);
24027 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24028 doc: /* *How to resize mini-windows.
24029 A value of nil means don't automatically resize mini-windows.
24030 A value of t means resize them to fit the text displayed in them.
24031 A value of `grow-only', the default, means let mini-windows grow
24032 only, until their display becomes empty, at which point the windows
24033 go back to their normal size. */);
24034 Vresize_mini_windows = Qgrow_only;
24036 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24037 doc: /* Alist specifying how to blink the cursor off.
24038 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24039 `cursor-type' frame-parameter or variable equals ON-STATE,
24040 comparing using `equal', Emacs uses OFF-STATE to specify
24041 how to blink it off. ON-STATE and OFF-STATE are values for
24042 the `cursor-type' frame parameter.
24044 If a frame's ON-STATE has no entry in this list,
24045 the frame's other specifications determine how to blink the cursor off. */);
24046 Vblink_cursor_alist = Qnil;
24048 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24049 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24050 automatic_hscrolling_p = 1;
24052 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24053 doc: /* *How many columns away from the window edge point is allowed to get
24054 before automatic hscrolling will horizontally scroll the window. */);
24055 hscroll_margin = 5;
24057 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24058 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24059 When point is less than `hscroll-margin' columns from the window
24060 edge, automatic hscrolling will scroll the window by the amount of columns
24061 determined by this variable. If its value is a positive integer, scroll that
24062 many columns. If it's a positive floating-point number, it specifies the
24063 fraction of the window's width to scroll. If it's nil or zero, point will be
24064 centered horizontally after the scroll. Any other value, including negative
24065 numbers, are treated as if the value were zero.
24067 Automatic hscrolling always moves point outside the scroll margin, so if
24068 point was more than scroll step columns inside the margin, the window will
24069 scroll more than the value given by the scroll step.
24071 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24072 and `scroll-right' overrides this variable's effect. */);
24073 Vhscroll_step = make_number (0);
24075 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24076 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24077 Bind this around calls to `message' to let it take effect. */);
24078 message_truncate_lines = 0;
24080 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24081 doc: /* Normal hook run to update the menu bar definitions.
24082 Redisplay runs this hook before it redisplays the menu bar.
24083 This is used to update submenus such as Buffers,
24084 whose contents depend on various data. */);
24085 Vmenu_bar_update_hook = Qnil;
24087 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24088 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24089 inhibit_menubar_update = 0;
24091 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24092 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24093 inhibit_eval_during_redisplay = 0;
24095 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24096 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24097 inhibit_free_realized_faces = 0;
24099 #if GLYPH_DEBUG
24100 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24101 doc: /* Inhibit try_window_id display optimization. */);
24102 inhibit_try_window_id = 0;
24104 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24105 doc: /* Inhibit try_window_reusing display optimization. */);
24106 inhibit_try_window_reusing = 0;
24108 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24109 doc: /* Inhibit try_cursor_movement display optimization. */);
24110 inhibit_try_cursor_movement = 0;
24111 #endif /* GLYPH_DEBUG */
24115 /* Initialize this module when Emacs starts. */
24117 void
24118 init_xdisp ()
24120 Lisp_Object root_window;
24121 struct window *mini_w;
24123 current_header_line_height = current_mode_line_height = -1;
24125 CHARPOS (this_line_start_pos) = 0;
24127 mini_w = XWINDOW (minibuf_window);
24128 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24130 if (!noninteractive)
24132 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24133 int i;
24135 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24136 set_window_height (root_window,
24137 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24139 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24140 set_window_height (minibuf_window, 1, 0);
24142 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24143 mini_w->total_cols = make_number (FRAME_COLS (f));
24145 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24146 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24147 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24149 /* The default ellipsis glyphs `...'. */
24150 for (i = 0; i < 3; ++i)
24151 default_invis_vector[i] = make_number ('.');
24155 /* Allocate the buffer for frame titles.
24156 Also used for `format-mode-line'. */
24157 int size = 100;
24158 mode_line_noprop_buf = (char *) xmalloc (size);
24159 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24160 mode_line_noprop_ptr = mode_line_noprop_buf;
24161 mode_line_target = MODE_LINE_DISPLAY;
24164 help_echo_showing_p = 0;
24168 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24169 (do not change this comment) */