(normal-splash-screen): Fix last change so we don't wait
[emacs.git] / src / xdisp.c
blob1125020341b36d620ffe815f6e26dea0e1282d3f
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
289 int auto_resize_tool_bars_p;
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
295 int x_stretch_cursor_p;
297 /* Non-nil means don't actually do any redisplay. */
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
303 int inhibit_eval_during_redisplay;
305 /* Names of text properties relevant for redisplay. */
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
310 /* Symbols used in text property values. */
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
324 /* Non-nil means highlight trailing whitespace. */
326 Lisp_Object Vshow_trailing_whitespace;
328 /* Non-nil means escape non-break space and hyphens. */
330 Lisp_Object Vnobreak_char_display;
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
344 #endif /* HAVE_WINDOW_SYSTEM */
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
350 Lisp_Object Vvoid_text_area_pointer;
352 /* Name of the face used to highlight trailing whitespace. */
354 Lisp_Object Qtrailing_whitespace;
356 /* Name and number of the face used to highlight escape glyphs. */
358 Lisp_Object Qescape_glyph;
360 /* Name and number of the face used to highlight non-breaking spaces. */
362 Lisp_Object Qnobreak_space;
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
367 Lisp_Object Qimage;
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
376 int noninteractive_need_newline;
378 /* Non-zero means print newline to message log before next message. */
380 static int message_log_need_newline;
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
394 static struct text_pos this_line_start_pos;
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
399 static struct text_pos this_line_end_pos;
401 /* The vertical positions and the height of this line. */
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
410 static int this_line_start_x;
412 /* Buffer that this_line_.* variables are referring to. */
414 static struct buffer *this_line_buffer;
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
419 int truncate_partial_width_windows;
421 /* A flag to control how to display unibyte 8-bit character. */
423 int unibyte_display_via_language_environment;
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
429 int multiple_frames;
431 Lisp_Object Vglobal_mode_string;
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
438 Lisp_Object Voverlay_arrow_variable_list;
440 /* Marker for where to display an arrow on top of the buffer text. */
442 Lisp_Object Voverlay_arrow_position;
444 /* String to display for the arrow. Only used on terminal frames. */
446 Lisp_Object Voverlay_arrow_string;
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
460 /* Like mode-line-format, but for the title bar on a visible frame. */
462 Lisp_Object Vframe_title_format;
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
466 Lisp_Object Vicon_title_format;
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
472 static Lisp_Object Vwindow_size_change_functions;
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
476 /* Nonzero if an overlay arrow has been displayed in this window. */
478 static int overlay_arrow_seen;
480 /* Nonzero means highlight the region even in nonselected windows. */
482 int highlight_nonselected_windows;
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
487 static EMACS_INT scroll_step;
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
492 static EMACS_INT scroll_conservatively;
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
499 EMACS_INT scroll_margin;
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
505 int buffer_shared;
507 /* Vector containing glyphs for an ellipsis `...'. */
509 static Lisp_Object default_invis_vector[3];
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
515 This variable is deprecated. */
517 int mode_line_inverse_video;
519 /* Prompt to display in front of the mini-buffer contents. */
521 Lisp_Object minibuf_prompt;
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
526 int minibuf_prompt_width;
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
532 Lisp_Object echo_area_window;
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
539 Lisp_Object Vmessage_stack;
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
544 int message_enable_multibyte;
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548 int update_mode_lines;
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
553 int windows_or_buffers_changed;
555 /* Nonzero means a frame's cursor type has been changed. */
557 int cursor_type_changed;
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
562 int line_number_displayed;
564 /* Maximum buffer size for which to display line numbers. */
566 Lisp_Object Vline_number_display_limit;
568 /* Line width to consider when repositioning for line number display. */
570 static EMACS_INT line_number_display_limit_width;
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
575 Lisp_Object Vmessage_log_max;
577 /* The name of the *Messages* buffer, a string. */
579 static Lisp_Object Vmessages_buffer_name;
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
590 Lisp_Object echo_area_buffer[2];
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
597 static Lisp_Object echo_buffer[2];
599 /* A vector saved used in with_area_buffer to reduce consing. */
601 static Lisp_Object Vwith_echo_area_save_vector;
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
606 static int display_last_displayed_message_p;
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
611 int message_buf_print;
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
622 Lisp_Object Vmax_mini_window_height;
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
633 static int message_cleared_p;
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
645 /* Ascent and height of the last line processed by move_it_to. */
647 static int last_max_ascent, last_height;
649 /* Non-zero if there's a help-echo in the echo area. */
651 int help_echo_showing_p;
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
657 int current_mode_line_height, current_header_line_height;
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
665 #define TEXT_PROP_DISTANCE_LIMIT 100
667 #if GLYPH_DEBUG
669 /* Variables to turn off display optimizations from Lisp. */
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
677 int trace_redisplay_p;
679 #endif /* GLYPH_DEBUG */
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
693 int automatic_hscrolling_p;
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
707 Lisp_Object Vresize_mini_windows;
709 /* Buffer being redisplayed -- for redisplay_window_error. */
711 struct buffer *displayed_buffer;
713 /* Value returned from text property handlers (see below). */
715 enum prop_handled
717 HANDLED_NORMALLY,
718 HANDLED_RECOMPUTE_PROPS,
719 HANDLED_OVERLAY_STRING_CONSUMED,
720 HANDLED_RETURN
723 /* A description of text properties that redisplay is interested
724 in. */
726 struct props
728 /* The name of the property. */
729 Lisp_Object *name;
731 /* A unique index for the property. */
732 enum prop_idx idx;
734 /* A handler function called to set up iterator IT from the property
735 at IT's current position. Value is used to steer handle_stop. */
736 enum prop_handled (*handler) P_ ((struct it *it));
739 static enum prop_handled handle_face_prop P_ ((struct it *));
740 static enum prop_handled handle_invisible_prop P_ ((struct it *));
741 static enum prop_handled handle_display_prop P_ ((struct it *));
742 static enum prop_handled handle_composition_prop P_ ((struct it *));
743 static enum prop_handled handle_overlay_change P_ ((struct it *));
744 static enum prop_handled handle_fontified_prop P_ ((struct it *));
746 /* Properties handled by iterators. */
748 static struct props it_props[] =
750 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
751 /* Handle `face' before `display' because some sub-properties of
752 `display' need to know the face. */
753 {&Qface, FACE_PROP_IDX, handle_face_prop},
754 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
755 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
756 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
757 {NULL, 0, NULL}
760 /* Value is the position described by X. If X is a marker, value is
761 the marker_position of X. Otherwise, value is X. */
763 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
765 /* Enumeration returned by some move_it_.* functions internally. */
767 enum move_it_result
769 /* Not used. Undefined value. */
770 MOVE_UNDEFINED,
772 /* Move ended at the requested buffer position or ZV. */
773 MOVE_POS_MATCH_OR_ZV,
775 /* Move ended at the requested X pixel position. */
776 MOVE_X_REACHED,
778 /* Move within a line ended at the end of a line that must be
779 continued. */
780 MOVE_LINE_CONTINUED,
782 /* Move within a line ended at the end of a line that would
783 be displayed truncated. */
784 MOVE_LINE_TRUNCATED,
786 /* Move within a line ended at a line end. */
787 MOVE_NEWLINE_OR_CR
790 /* This counter is used to clear the face cache every once in a while
791 in redisplay_internal. It is incremented for each redisplay.
792 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
793 cleared. */
795 #define CLEAR_FACE_CACHE_COUNT 500
796 static int clear_face_cache_count;
798 /* Similarly for the image cache. */
800 #ifdef HAVE_WINDOW_SYSTEM
801 #define CLEAR_IMAGE_CACHE_COUNT 101
802 static int clear_image_cache_count;
803 #endif
805 /* Record the previous terminal frame we displayed. */
807 static struct frame *previous_terminal_frame;
809 /* Non-zero while redisplay_internal is in progress. */
811 int redisplaying_p;
813 /* Non-zero means don't free realized faces. Bound while freeing
814 realized faces is dangerous because glyph matrices might still
815 reference them. */
817 int inhibit_free_realized_faces;
818 Lisp_Object Qinhibit_free_realized_faces;
820 /* If a string, XTread_socket generates an event to display that string.
821 (The display is done in read_char.) */
823 Lisp_Object help_echo_string;
824 Lisp_Object help_echo_window;
825 Lisp_Object help_echo_object;
826 int help_echo_pos;
828 /* Temporary variable for XTread_socket. */
830 Lisp_Object previous_help_echo_string;
832 /* Null glyph slice */
834 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
837 /* Function prototypes. */
839 static void setup_for_ellipsis P_ ((struct it *, int));
840 static void mark_window_display_accurate_1 P_ ((struct window *, int));
841 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
843 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
844 static int redisplay_mode_lines P_ ((Lisp_Object, int));
845 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
847 #if 0
848 static int invisible_text_between_p P_ ((struct it *, int, int));
849 #endif
851 static void pint2str P_ ((char *, int, int));
852 static void pint2hrstr P_ ((char *, int, int));
853 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
854 struct text_pos));
855 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
856 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
857 static void store_mode_line_noprop_char P_ ((char));
858 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
859 static void x_consider_frame_title P_ ((Lisp_Object));
860 static void handle_stop P_ ((struct it *));
861 static int tool_bar_lines_needed P_ ((struct frame *, int *));
862 static int single_display_spec_intangible_p P_ ((Lisp_Object));
863 static void ensure_echo_area_buffers P_ ((void));
864 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
865 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
866 static int with_echo_area_buffer P_ ((struct window *, int,
867 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
868 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static void clear_garbaged_frames P_ ((void));
870 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int display_echo_area P_ ((struct window *));
874 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
877 static int string_char_and_length P_ ((const unsigned char *, int, int *));
878 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
879 struct text_pos));
880 static int compute_window_start_on_continuation_line P_ ((struct window *));
881 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
882 static void insert_left_trunc_glyphs P_ ((struct it *));
883 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
884 Lisp_Object));
885 static void extend_face_to_end_of_line P_ ((struct it *));
886 static int append_space_for_newline P_ ((struct it *, int));
887 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
888 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
889 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
890 static int trailing_whitespace_p P_ ((int));
891 static int message_log_check_duplicate P_ ((int, int, int, int));
892 static void push_it P_ ((struct it *));
893 static void pop_it P_ ((struct it *));
894 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
895 static void select_frame_for_redisplay P_ ((Lisp_Object));
896 static void redisplay_internal P_ ((int));
897 static int echo_area_display P_ ((int));
898 static void redisplay_windows P_ ((Lisp_Object));
899 static void redisplay_window P_ ((Lisp_Object, int));
900 static Lisp_Object redisplay_window_error ();
901 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
902 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
903 static void update_menu_bar P_ ((struct frame *, int));
904 static int try_window_reusing_current_matrix P_ ((struct window *));
905 static int try_window_id P_ ((struct window *));
906 static int display_line P_ ((struct it *));
907 static int display_mode_lines P_ ((struct window *));
908 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
909 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
910 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
911 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
912 static void display_menu_bar P_ ((struct window *));
913 static int display_count_lines P_ ((int, int, int, int, int *));
914 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
915 int, int, struct it *, int, int, int, int));
916 static void compute_line_metrics P_ ((struct it *));
917 static void run_redisplay_end_trigger_hook P_ ((struct it *));
918 static int get_overlay_strings P_ ((struct it *, int));
919 static void next_overlay_string P_ ((struct it *));
920 static void reseat P_ ((struct it *, struct text_pos, int));
921 static void reseat_1 P_ ((struct it *, struct text_pos, int));
922 static void back_to_previous_visible_line_start P_ ((struct it *));
923 void reseat_at_previous_visible_line_start P_ ((struct it *));
924 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
925 static int next_element_from_ellipsis P_ ((struct it *));
926 static int next_element_from_display_vector P_ ((struct it *));
927 static int next_element_from_string P_ ((struct it *));
928 static int next_element_from_c_string P_ ((struct it *));
929 static int next_element_from_buffer P_ ((struct it *));
930 static int next_element_from_composition P_ ((struct it *));
931 static int next_element_from_image P_ ((struct it *));
932 static int next_element_from_stretch P_ ((struct it *));
933 static void load_overlay_strings P_ ((struct it *, int));
934 static int init_from_display_pos P_ ((struct it *, struct window *,
935 struct display_pos *));
936 static void reseat_to_string P_ ((struct it *, unsigned char *,
937 Lisp_Object, int, int, int, int));
938 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
939 int, int, int));
940 void move_it_vertically_backward P_ ((struct it *, int));
941 static void init_to_row_start P_ ((struct it *, struct window *,
942 struct glyph_row *));
943 static int init_to_row_end P_ ((struct it *, struct window *,
944 struct glyph_row *));
945 static void back_to_previous_line_start P_ ((struct it *));
946 static int forward_to_next_line_start P_ ((struct it *, int *));
947 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
948 Lisp_Object, int));
949 static struct text_pos string_pos P_ ((int, Lisp_Object));
950 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
951 static int number_of_chars P_ ((unsigned char *, int));
952 static void compute_stop_pos P_ ((struct it *));
953 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
954 Lisp_Object));
955 static int face_before_or_after_it_pos P_ ((struct it *, int));
956 static int next_overlay_change P_ ((int));
957 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
958 Lisp_Object, struct text_pos *,
959 int));
960 static int underlying_face_id P_ ((struct it *));
961 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
962 struct window *));
964 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
965 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967 #ifdef HAVE_WINDOW_SYSTEM
969 static void update_tool_bar P_ ((struct frame *, int));
970 static void build_desired_tool_bar_string P_ ((struct frame *f));
971 static int redisplay_tool_bar P_ ((struct frame *));
972 static void display_tool_bar_line P_ ((struct it *, int));
973 static void notice_overwritten_cursor P_ ((struct window *,
974 enum glyph_row_area,
975 int, int, int, int));
979 #endif /* HAVE_WINDOW_SYSTEM */
982 /***********************************************************************
983 Window display dimensions
984 ***********************************************************************/
986 /* Return the bottom boundary y-position for text lines in window W.
987 This is the first y position at which a line cannot start.
988 It is relative to the top of the window.
990 This is the height of W minus the height of a mode line, if any. */
992 INLINE int
993 window_text_bottom_y (w)
994 struct window *w;
996 int height = WINDOW_TOTAL_HEIGHT (w);
998 if (WINDOW_WANTS_MODELINE_P (w))
999 height -= CURRENT_MODE_LINE_HEIGHT (w);
1000 return height;
1003 /* Return the pixel width of display area AREA of window W. AREA < 0
1004 means return the total width of W, not including fringes to
1005 the left and right of the window. */
1007 INLINE int
1008 window_box_width (w, area)
1009 struct window *w;
1010 int area;
1012 int cols = XFASTINT (w->total_cols);
1013 int pixels = 0;
1015 if (!w->pseudo_window_p)
1017 cols -= WINDOW_SCROLL_BAR_COLS (w);
1019 if (area == TEXT_AREA)
1021 if (INTEGERP (w->left_margin_cols))
1022 cols -= XFASTINT (w->left_margin_cols);
1023 if (INTEGERP (w->right_margin_cols))
1024 cols -= XFASTINT (w->right_margin_cols);
1025 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1027 else if (area == LEFT_MARGIN_AREA)
1029 cols = (INTEGERP (w->left_margin_cols)
1030 ? XFASTINT (w->left_margin_cols) : 0);
1031 pixels = 0;
1033 else if (area == RIGHT_MARGIN_AREA)
1035 cols = (INTEGERP (w->right_margin_cols)
1036 ? XFASTINT (w->right_margin_cols) : 0);
1037 pixels = 0;
1041 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1045 /* Return the pixel height of the display area of window W, not
1046 including mode lines of W, if any. */
1048 INLINE int
1049 window_box_height (w)
1050 struct window *w;
1052 struct frame *f = XFRAME (w->frame);
1053 int height = WINDOW_TOTAL_HEIGHT (w);
1055 xassert (height >= 0);
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1063 if (WINDOW_WANTS_MODELINE_P (w))
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. AREA < 0 means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1096 INLINE int
1097 window_box_left_offset (w, area)
1098 struct window *w;
1099 int area;
1101 int x;
1103 if (w->pseudo_window_p)
1104 return 0;
1106 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1108 if (area == TEXT_AREA)
1109 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1110 + window_box_width (w, LEFT_MARGIN_AREA));
1111 else if (area == RIGHT_MARGIN_AREA)
1112 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1113 + window_box_width (w, LEFT_MARGIN_AREA)
1114 + window_box_width (w, TEXT_AREA)
1115 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1117 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1118 else if (area == LEFT_MARGIN_AREA
1119 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1120 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1122 return x;
1126 /* Return the window-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the left edge of the
1128 whole window, to the left of the right fringe of W. */
1130 INLINE int
1131 window_box_right_offset (w, area)
1132 struct window *w;
1133 int area;
1135 return window_box_left_offset (w, area) + window_box_width (w, area);
1138 /* Return the frame-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1142 INLINE int
1143 window_box_left (w, area)
1144 struct window *w;
1145 int area;
1147 struct frame *f = XFRAME (w->frame);
1148 int x;
1150 if (w->pseudo_window_p)
1151 return FRAME_INTERNAL_BORDER_WIDTH (f);
1153 x = (WINDOW_LEFT_EDGE_X (w)
1154 + window_box_left_offset (w, area));
1156 return x;
1160 /* Return the frame-relative coordinate of the right edge of display
1161 area AREA of window W. AREA < 0 means return the left edge of the
1162 whole window, to the left of the right fringe of W. */
1164 INLINE int
1165 window_box_right (w, area)
1166 struct window *w;
1167 int area;
1169 return window_box_left (w, area) + window_box_width (w, area);
1172 /* Get the bounding box of the display area AREA of window W, without
1173 mode lines, in frame-relative coordinates. AREA < 0 means the
1174 whole window, not including the left and right fringes of
1175 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1176 coordinates of the upper-left corner of the box. Return in
1177 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1179 INLINE void
1180 window_box (w, area, box_x, box_y, box_width, box_height)
1181 struct window *w;
1182 int area;
1183 int *box_x, *box_y, *box_width, *box_height;
1185 if (box_width)
1186 *box_width = window_box_width (w, area);
1187 if (box_height)
1188 *box_height = window_box_height (w);
1189 if (box_x)
1190 *box_x = window_box_left (w, area);
1191 if (box_y)
1193 *box_y = WINDOW_TOP_EDGE_Y (w);
1194 if (WINDOW_WANTS_HEADER_LINE_P (w))
1195 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1200 /* Get the bounding box of the display area AREA of window W, without
1201 mode lines. AREA < 0 means the whole window, not including the
1202 left and right fringe of the window. Return in *TOP_LEFT_X
1203 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1204 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1205 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1206 box. */
1208 INLINE void
1209 window_box_edges (w, area, top_left_x, top_left_y,
1210 bottom_right_x, bottom_right_y)
1211 struct window *w;
1212 int area;
1213 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1215 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1216 bottom_right_y);
1217 *bottom_right_x += *top_left_x;
1218 *bottom_right_y += *top_left_y;
1223 /***********************************************************************
1224 Utilities
1225 ***********************************************************************/
1227 /* Return the bottom y-position of the line the iterator IT is in.
1228 This can modify IT's settings. */
1231 line_bottom_y (it)
1232 struct it *it;
1234 int line_height = it->max_ascent + it->max_descent;
1235 int line_top_y = it->current_y;
1237 if (line_height == 0)
1239 if (last_height)
1240 line_height = last_height;
1241 else if (IT_CHARPOS (*it) < ZV)
1243 move_it_by_lines (it, 1, 1);
1244 line_height = (it->max_ascent || it->max_descent
1245 ? it->max_ascent + it->max_descent
1246 : last_height);
1248 else
1250 struct glyph_row *row = it->glyph_row;
1252 /* Use the default character height. */
1253 it->glyph_row = NULL;
1254 it->what = IT_CHARACTER;
1255 it->c = ' ';
1256 it->len = 1;
1257 PRODUCE_GLYPHS (it);
1258 line_height = it->ascent + it->descent;
1259 it->glyph_row = row;
1263 return line_top_y + line_height;
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1271 and header-lines heights. */
1274 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1275 struct window *w;
1276 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1278 struct it it;
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1283 if (noninteractive)
1284 return visible_p;
1286 if (XBUFFER (w->buffer) != current_buffer)
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1294 /* Compute exact mode line heights, if requested. */
1295 if (exact_mode_line_heights_p)
1297 if (WINDOW_WANTS_MODELINE_P (w))
1298 current_mode_line_height
1299 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1300 current_buffer->mode_line_format);
1302 if (WINDOW_WANTS_HEADER_LINE_P (w))
1303 current_header_line_height
1304 = display_mode_line (w, HEADER_LINE_FACE_ID,
1305 current_buffer->header_line_format);
1308 start_display (&it, w, top);
1309 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1310 MOVE_TO_POS | MOVE_TO_Y);
1312 /* Note that we may overshoot because of invisible text. */
1313 if (IT_CHARPOS (it) >= charpos)
1315 int top_x = it.current_x;
1316 int top_y = it.current_y;
1317 int bottom_y = (last_height = 0, line_bottom_y (&it));
1318 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1320 if (top_y < window_top_y)
1321 visible_p = bottom_y > window_top_y;
1322 else if (top_y < it.last_visible_y)
1323 visible_p = 1;
1324 if (visible_p)
1326 *x = top_x;
1327 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1328 *rtop = max (0, window_top_y - top_y);
1329 *rbot = max (0, bottom_y - it.last_visible_y);
1332 else
1334 struct it it2;
1336 it2 = it;
1337 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1338 move_it_by_lines (&it, 1, 0);
1339 if (charpos < IT_CHARPOS (it))
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1351 if (old_buffer)
1352 set_buffer_internal_1 (old_buffer);
1354 current_header_line_height = current_mode_line_height = -1;
1356 if (visible_p && XFASTINT (w->hscroll) > 0)
1357 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1359 return visible_p;
1363 /* Return the next character from STR which is MAXLEN bytes long.
1364 Return in *LEN the length of the character. This is like
1365 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1366 we find one, we return a `?', but with the length of the invalid
1367 character. */
1369 static INLINE int
1370 string_char_and_length (str, maxlen, len)
1371 const unsigned char *str;
1372 int maxlen, *len;
1374 int c;
1376 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1377 if (!CHAR_VALID_P (c, 1))
1378 /* We may not change the length here because other places in Emacs
1379 don't use this function, i.e. they silently accept invalid
1380 characters. */
1381 c = '?';
1383 return c;
1388 /* Given a position POS containing a valid character and byte position
1389 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1391 static struct text_pos
1392 string_pos_nchars_ahead (pos, string, nchars)
1393 struct text_pos pos;
1394 Lisp_Object string;
1395 int nchars;
1397 xassert (STRINGP (string) && nchars >= 0);
1399 if (STRING_MULTIBYTE (string))
1401 int rest = SBYTES (string) - BYTEPOS (pos);
1402 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1403 int len;
1405 while (nchars--)
1407 string_char_and_length (p, rest, &len);
1408 p += len, rest -= len;
1409 xassert (rest >= 0);
1410 CHARPOS (pos) += 1;
1411 BYTEPOS (pos) += len;
1414 else
1415 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1417 return pos;
1421 /* Value is the text position, i.e. character and byte position,
1422 for character position CHARPOS in STRING. */
1424 static INLINE struct text_pos
1425 string_pos (charpos, string)
1426 int charpos;
1427 Lisp_Object string;
1429 struct text_pos pos;
1430 xassert (STRINGP (string));
1431 xassert (charpos >= 0);
1432 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1433 return pos;
1437 /* Value is a text position, i.e. character and byte position, for
1438 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1439 means recognize multibyte characters. */
1441 static struct text_pos
1442 c_string_pos (charpos, s, multibyte_p)
1443 int charpos;
1444 unsigned char *s;
1445 int multibyte_p;
1447 struct text_pos pos;
1449 xassert (s != NULL);
1450 xassert (charpos >= 0);
1452 if (multibyte_p)
1454 int rest = strlen (s), len;
1456 SET_TEXT_POS (pos, 0, 0);
1457 while (charpos--)
1459 string_char_and_length (s, rest, &len);
1460 s += len, rest -= len;
1461 xassert (rest >= 0);
1462 CHARPOS (pos) += 1;
1463 BYTEPOS (pos) += len;
1466 else
1467 SET_TEXT_POS (pos, charpos, charpos);
1469 return pos;
1473 /* Value is the number of characters in C string S. MULTIBYTE_P
1474 non-zero means recognize multibyte characters. */
1476 static int
1477 number_of_chars (s, multibyte_p)
1478 unsigned char *s;
1479 int multibyte_p;
1481 int nchars;
1483 if (multibyte_p)
1485 int rest = strlen (s), len;
1486 unsigned char *p = (unsigned char *) s;
1488 for (nchars = 0; rest > 0; ++nchars)
1490 string_char_and_length (p, rest, &len);
1491 rest -= len, p += len;
1494 else
1495 nchars = strlen (s);
1497 return nchars;
1501 /* Compute byte position NEWPOS->bytepos corresponding to
1502 NEWPOS->charpos. POS is a known position in string STRING.
1503 NEWPOS->charpos must be >= POS.charpos. */
1505 static void
1506 compute_string_pos (newpos, pos, string)
1507 struct text_pos *newpos, pos;
1508 Lisp_Object string;
1510 xassert (STRINGP (string));
1511 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1513 if (STRING_MULTIBYTE (string))
1514 *newpos = string_pos_nchars_ahead (pos, string,
1515 CHARPOS (*newpos) - CHARPOS (pos));
1516 else
1517 BYTEPOS (*newpos) = CHARPOS (*newpos);
1520 /* EXPORT:
1521 Return an estimation of the pixel height of mode or top lines on
1522 frame F. FACE_ID specifies what line's height to estimate. */
1525 estimate_mode_line_height (f, face_id)
1526 struct frame *f;
1527 enum face_id face_id;
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1532 int height = FONT_HEIGHT (FRAME_FONT (f));
1534 /* This function is called so early when Emacs starts that the face
1535 cache and mode line face are not yet initialized. */
1536 if (FRAME_FACE_CACHE (f))
1538 struct face *face = FACE_FROM_ID (f, face_id);
1539 if (face)
1541 if (face->font)
1542 height = FONT_HEIGHT (face->font);
1543 if (face->box_line_width > 0)
1544 height += 2 * face->box_line_width;
1548 return height;
1550 #endif
1552 return 1;
1555 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1556 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1557 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1558 not force the value into range. */
1560 void
1561 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1562 FRAME_PTR f;
1563 register int pix_x, pix_y;
1564 int *x, *y;
1565 NativeRectangle *bounds;
1566 int noclip;
1569 #ifdef HAVE_WINDOW_SYSTEM
1570 if (FRAME_WINDOW_P (f))
1572 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1573 even for negative values. */
1574 if (pix_x < 0)
1575 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1576 if (pix_y < 0)
1577 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1579 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1580 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1582 if (bounds)
1583 STORE_NATIVE_RECT (*bounds,
1584 FRAME_COL_TO_PIXEL_X (f, pix_x),
1585 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1586 FRAME_COLUMN_WIDTH (f) - 1,
1587 FRAME_LINE_HEIGHT (f) - 1);
1589 if (!noclip)
1591 if (pix_x < 0)
1592 pix_x = 0;
1593 else if (pix_x > FRAME_TOTAL_COLS (f))
1594 pix_x = FRAME_TOTAL_COLS (f);
1596 if (pix_y < 0)
1597 pix_y = 0;
1598 else if (pix_y > FRAME_LINES (f))
1599 pix_y = FRAME_LINES (f);
1602 #endif
1604 *x = pix_x;
1605 *y = pix_y;
1609 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1610 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1611 can't tell the positions because W's display is not up to date,
1612 return 0. */
1615 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1616 struct window *w;
1617 int hpos, vpos;
1618 int *frame_x, *frame_y;
1620 #ifdef HAVE_WINDOW_SYSTEM
1621 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1623 int success_p;
1625 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1626 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1628 if (display_completed)
1630 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1631 struct glyph *glyph = row->glyphs[TEXT_AREA];
1632 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1634 hpos = row->x;
1635 vpos = row->y;
1636 while (glyph < end)
1638 hpos += glyph->pixel_width;
1639 ++glyph;
1642 /* If first glyph is partially visible, its first visible position is still 0. */
1643 if (hpos < 0)
1644 hpos = 0;
1646 success_p = 1;
1648 else
1650 hpos = vpos = 0;
1651 success_p = 0;
1654 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1655 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1656 return success_p;
1658 #endif
1660 *frame_x = hpos;
1661 *frame_y = vpos;
1662 return 1;
1666 #ifdef HAVE_WINDOW_SYSTEM
1668 /* Find the glyph under window-relative coordinates X/Y in window W.
1669 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1670 strings. Return in *HPOS and *VPOS the row and column number of
1671 the glyph found. Return in *AREA the glyph area containing X.
1672 Value is a pointer to the glyph found or null if X/Y is not on
1673 text, or we can't tell because W's current matrix is not up to
1674 date. */
1676 static struct glyph *
1677 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1678 struct window *w;
1679 int x, y;
1680 int *hpos, *vpos, *dx, *dy, *area;
1682 struct glyph *glyph, *end;
1683 struct glyph_row *row = NULL;
1684 int x0, i;
1686 /* Find row containing Y. Give up if some row is not enabled. */
1687 for (i = 0; i < w->current_matrix->nrows; ++i)
1689 row = MATRIX_ROW (w->current_matrix, i);
1690 if (!row->enabled_p)
1691 return NULL;
1692 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1693 break;
1696 *vpos = i;
1697 *hpos = 0;
1699 /* Give up if Y is not in the window. */
1700 if (i == w->current_matrix->nrows)
1701 return NULL;
1703 /* Get the glyph area containing X. */
1704 if (w->pseudo_window_p)
1706 *area = TEXT_AREA;
1707 x0 = 0;
1709 else
1711 if (x < window_box_left_offset (w, TEXT_AREA))
1713 *area = LEFT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1716 else if (x < window_box_right_offset (w, TEXT_AREA))
1718 *area = TEXT_AREA;
1719 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1721 else
1723 *area = RIGHT_MARGIN_AREA;
1724 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1728 /* Find glyph containing X. */
1729 glyph = row->glyphs[*area];
1730 end = glyph + row->used[*area];
1731 x -= x0;
1732 while (glyph < end && x >= glyph->pixel_width)
1734 x -= glyph->pixel_width;
1735 ++glyph;
1738 if (glyph == end)
1739 return NULL;
1741 if (dx)
1743 *dx = x;
1744 *dy = y - (row->y + row->ascent - glyph->ascent);
1747 *hpos = glyph - row->glyphs[*area];
1748 return glyph;
1752 /* EXPORT:
1753 Convert frame-relative x/y to coordinates relative to window W.
1754 Takes pseudo-windows into account. */
1756 void
1757 frame_to_window_pixel_xy (w, x, y)
1758 struct window *w;
1759 int *x, *y;
1761 if (w->pseudo_window_p)
1763 /* A pseudo-window is always full-width, and starts at the
1764 left edge of the frame, plus a frame border. */
1765 struct frame *f = XFRAME (w->frame);
1766 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1767 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1769 else
1771 *x -= WINDOW_LEFT_EDGE_X (w);
1772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1776 /* EXPORT:
1777 Return in RECTS[] at most N clipping rectangles for glyph string S.
1778 Return the number of stored rectangles. */
1781 get_glyph_string_clip_rects (s, rects, n)
1782 struct glyph_string *s;
1783 NativeRectangle *rects;
1784 int n;
1786 XRectangle r;
1788 if (n <= 0)
1789 return 0;
1791 if (s->row->full_width_p)
1793 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1794 r.x = WINDOW_LEFT_EDGE_X (s->w);
1795 r.width = WINDOW_TOTAL_WIDTH (s->w);
1797 /* Unless displaying a mode or menu bar line, which are always
1798 fully visible, clip to the visible part of the row. */
1799 if (s->w->pseudo_window_p)
1800 r.height = s->row->visible_height;
1801 else
1802 r.height = s->height;
1804 else
1806 /* This is a text line that may be partially visible. */
1807 r.x = window_box_left (s->w, s->area);
1808 r.width = window_box_width (s->w, s->area);
1809 r.height = s->row->visible_height;
1812 if (s->clip_head)
1813 if (r.x < s->clip_head->x)
1815 if (r.width >= s->clip_head->x - r.x)
1816 r.width -= s->clip_head->x - r.x;
1817 else
1818 r.width = 0;
1819 r.x = s->clip_head->x;
1821 if (s->clip_tail)
1822 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1824 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1825 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1826 else
1827 r.width = 0;
1830 /* If S draws overlapping rows, it's sufficient to use the top and
1831 bottom of the window for clipping because this glyph string
1832 intentionally draws over other lines. */
1833 if (s->for_overlaps)
1835 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1836 r.height = window_text_bottom_y (s->w) - r.y;
1838 /* Alas, the above simple strategy does not work for the
1839 environments with anti-aliased text: if the same text is
1840 drawn onto the same place multiple times, it gets thicker.
1841 If the overlap we are processing is for the erased cursor, we
1842 take the intersection with the rectagle of the cursor. */
1843 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1845 XRectangle rc, r_save = r;
1847 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1848 rc.y = s->w->phys_cursor.y;
1849 rc.width = s->w->phys_cursor_width;
1850 rc.height = s->w->phys_cursor_height;
1852 x_intersect_rectangles (&r_save, &rc, &r);
1855 else
1857 /* Don't use S->y for clipping because it doesn't take partially
1858 visible lines into account. For example, it can be negative for
1859 partially visible lines at the top of a window. */
1860 if (!s->row->full_width_p
1861 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 else
1864 r.y = max (0, s->row->y);
1866 /* If drawing a tool-bar window, draw it over the internal border
1867 at the top of the window. */
1868 if (WINDOWP (s->f->tool_bar_window)
1869 && s->w == XWINDOW (s->f->tool_bar_window))
1870 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1873 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1875 /* If drawing the cursor, don't let glyph draw outside its
1876 advertised boundaries. Cleartype does this under some circumstances. */
1877 if (s->hl == DRAW_CURSOR)
1879 struct glyph *glyph = s->first_glyph;
1880 int height, max_y;
1882 if (s->x > r.x)
1884 r.width -= s->x - r.x;
1885 r.x = s->x;
1887 r.width = min (r.width, glyph->pixel_width);
1889 /* If r.y is below window bottom, ensure that we still see a cursor. */
1890 height = min (glyph->ascent + glyph->descent,
1891 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1892 max_y = window_text_bottom_y (s->w) - height;
1893 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1894 if (s->ybase - glyph->ascent > max_y)
1896 r.y = max_y;
1897 r.height = height;
1899 else
1901 /* Don't draw cursor glyph taller than our actual glyph. */
1902 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1903 if (height < r.height)
1905 max_y = r.y + r.height;
1906 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1907 r.height = min (max_y - r.y, height);
1912 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1913 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1915 #ifdef CONVERT_FROM_XRECT
1916 CONVERT_FROM_XRECT (r, *rects);
1917 #else
1918 *rects = r;
1919 #endif
1920 return 1;
1922 else
1924 /* If we are processing overlapping and allowed to return
1925 multiple clipping rectangles, we exclude the row of the glyph
1926 string from the clipping rectangle. This is to avoid drawing
1927 the same text on the environment with anti-aliasing. */
1928 #ifdef CONVERT_FROM_XRECT
1929 XRectangle rs[2];
1930 #else
1931 XRectangle *rs = rects;
1932 #endif
1933 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1935 if (s->for_overlaps & OVERLAPS_PRED)
1937 rs[i] = r;
1938 if (r.y + r.height > row_y)
1939 if (r.y < row_y)
1940 rs[i].height = row_y - r.y;
1941 else
1942 rs[i].height = 0;
1943 i++;
1945 if (s->for_overlaps & OVERLAPS_SUCC)
1947 rs[i] = r;
1948 if (r.y < row_y + s->row->visible_height)
1949 if (r.y + r.height > row_y + s->row->visible_height)
1951 rs[i].y = row_y + s->row->visible_height;
1952 rs[i].height = r.y + r.height - rs[i].y;
1954 else
1955 rs[i].height = 0;
1956 i++;
1959 n = i;
1960 #ifdef CONVERT_FROM_XRECT
1961 for (i = 0; i < n; i++)
1962 CONVERT_FROM_XRECT (rs[i], rects[i]);
1963 #endif
1964 return n;
1968 /* EXPORT:
1969 Return in *NR the clipping rectangle for glyph string S. */
1971 void
1972 get_glyph_string_clip_rect (s, nr)
1973 struct glyph_string *s;
1974 NativeRectangle *nr;
1976 get_glyph_string_clip_rects (s, nr, 1);
1980 /* EXPORT:
1981 Return the position and height of the phys cursor in window W.
1982 Set w->phys_cursor_width to width of phys cursor.
1986 get_phys_cursor_geometry (w, row, glyph, heightp)
1987 struct window *w;
1988 struct glyph_row *row;
1989 struct glyph *glyph;
1990 int *heightp;
1992 struct frame *f = XFRAME (WINDOW_FRAME (w));
1993 int y, wd, h, h0, y0;
1995 /* Compute the width of the rectangle to draw. If on a stretch
1996 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1997 rectangle as wide as the glyph, but use a canonical character
1998 width instead. */
1999 wd = glyph->pixel_width - 1;
2000 #ifdef HAVE_NTGUI
2001 wd++; /* Why? */
2002 #endif
2003 if (glyph->type == STRETCH_GLYPH
2004 && !x_stretch_cursor_p)
2005 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2006 w->phys_cursor_width = wd;
2008 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2010 /* If y is below window bottom, ensure that we still see a cursor. */
2011 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2013 h = max (h0, glyph->ascent + glyph->descent);
2014 h0 = min (h0, glyph->ascent + glyph->descent);
2016 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2017 if (y < y0)
2019 h = max (h - (y0 - y) + 1, h0);
2020 y = y0 - 1;
2022 else
2024 y0 = window_text_bottom_y (w) - h0;
2025 if (y > y0)
2027 h += y - y0;
2028 y = y0;
2032 *heightp = h;
2033 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2037 * Remember which glyph the mouse is over.
2040 void
2041 remember_mouse_glyph (f, gx, gy, rect)
2042 struct frame *f;
2043 int gx, gy;
2044 NativeRectangle *rect;
2046 Lisp_Object window;
2047 struct window *w;
2048 struct glyph_row *r, *gr, *end_row;
2049 enum window_part part;
2050 enum glyph_row_area area;
2051 int x, y, width, height;
2053 /* Try to determine frame pixel position and size of the glyph under
2054 frame pixel coordinates X/Y on frame F. */
2056 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2057 if (NILP (window))
2059 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2060 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2061 goto virtual_glyph;
2064 w = XWINDOW (window);
2065 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2066 height = WINDOW_FRAME_LINE_HEIGHT (w);
2068 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2069 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2071 if (w->pseudo_window_p)
2073 area = TEXT_AREA;
2074 part = ON_MODE_LINE; /* Don't adjust margin. */
2075 goto text_glyph;
2078 switch (part)
2080 case ON_LEFT_MARGIN:
2081 area = LEFT_MARGIN_AREA;
2082 goto text_glyph;
2084 case ON_RIGHT_MARGIN:
2085 area = RIGHT_MARGIN_AREA;
2086 goto text_glyph;
2088 case ON_HEADER_LINE:
2089 case ON_MODE_LINE:
2090 gr = (part == ON_HEADER_LINE
2091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2092 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2093 gy = gr->y;
2094 area = TEXT_AREA;
2095 goto text_glyph_row_found;
2097 case ON_TEXT:
2098 area = TEXT_AREA;
2100 text_glyph:
2101 gr = 0; gy = 0;
2102 for (; r <= end_row && r->enabled_p; ++r)
2103 if (r->y + r->height > y)
2105 gr = r; gy = r->y;
2106 break;
2109 text_glyph_row_found:
2110 if (gr && gy <= y)
2112 struct glyph *g = gr->glyphs[area];
2113 struct glyph *end = g + gr->used[area];
2115 height = gr->height;
2116 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2117 if (gx + g->pixel_width > x)
2118 break;
2120 if (g < end)
2122 if (g->type == IMAGE_GLYPH)
2124 /* Don't remember when mouse is over image, as
2125 image may have hot-spots. */
2126 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2127 return;
2129 width = g->pixel_width;
2131 else
2133 /* Use nominal char spacing at end of line. */
2134 x -= gx;
2135 gx += (x / width) * width;
2138 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2139 gx += window_box_left_offset (w, area);
2141 else
2143 /* Use nominal line height at end of window. */
2144 gx = (x / width) * width;
2145 y -= gy;
2146 gy += (y / height) * height;
2148 break;
2150 case ON_LEFT_FRINGE:
2151 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2152 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2153 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2154 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2155 goto row_glyph;
2157 case ON_RIGHT_FRINGE:
2158 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2159 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2160 : window_box_right_offset (w, TEXT_AREA));
2161 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2162 goto row_glyph;
2164 case ON_SCROLL_BAR:
2165 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2167 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2168 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2169 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2170 : 0)));
2171 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2173 row_glyph:
2174 gr = 0, gy = 0;
2175 for (; r <= end_row && r->enabled_p; ++r)
2176 if (r->y + r->height > y)
2178 gr = r; gy = r->y;
2179 break;
2182 if (gr && gy <= y)
2183 height = gr->height;
2184 else
2186 /* Use nominal line height at end of window. */
2187 y -= gy;
2188 gy += (y / height) * height;
2190 break;
2192 default:
2194 virtual_glyph:
2195 /* If there is no glyph under the mouse, then we divide the screen
2196 into a grid of the smallest glyph in the frame, and use that
2197 as our "glyph". */
2199 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2200 round down even for negative values. */
2201 if (gx < 0)
2202 gx -= width - 1;
2203 if (gy < 0)
2204 gy -= height - 1;
2206 gx = (gx / width) * width;
2207 gy = (gy / height) * height;
2209 goto store_rect;
2212 gx += WINDOW_LEFT_EDGE_X (w);
2213 gy += WINDOW_TOP_EDGE_Y (w);
2215 store_rect:
2216 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2218 /* Visible feedback for debugging. */
2219 #if 0
2220 #if HAVE_X_WINDOWS
2221 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2222 f->output_data.x->normal_gc,
2223 gx, gy, width, height);
2224 #endif
2225 #endif
2229 #endif /* HAVE_WINDOW_SYSTEM */
2232 /***********************************************************************
2233 Lisp form evaluation
2234 ***********************************************************************/
2236 /* Error handler for safe_eval and safe_call. */
2238 static Lisp_Object
2239 safe_eval_handler (arg)
2240 Lisp_Object arg;
2242 add_to_log ("Error during redisplay: %s", arg, Qnil);
2243 return Qnil;
2247 /* Evaluate SEXPR and return the result, or nil if something went
2248 wrong. Prevent redisplay during the evaluation. */
2250 Lisp_Object
2251 safe_eval (sexpr)
2252 Lisp_Object sexpr;
2254 Lisp_Object val;
2256 if (inhibit_eval_during_redisplay)
2257 val = Qnil;
2258 else
2260 int count = SPECPDL_INDEX ();
2261 struct gcpro gcpro1;
2263 GCPRO1 (sexpr);
2264 specbind (Qinhibit_redisplay, Qt);
2265 /* Use Qt to ensure debugger does not run,
2266 so there is no possibility of wanting to redisplay. */
2267 val = internal_condition_case_1 (Feval, sexpr, Qt,
2268 safe_eval_handler);
2269 UNGCPRO;
2270 val = unbind_to (count, val);
2273 return val;
2277 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2278 Return the result, or nil if something went wrong. Prevent
2279 redisplay during the evaluation. */
2281 Lisp_Object
2282 safe_call (nargs, args)
2283 int nargs;
2284 Lisp_Object *args;
2286 Lisp_Object val;
2288 if (inhibit_eval_during_redisplay)
2289 val = Qnil;
2290 else
2292 int count = SPECPDL_INDEX ();
2293 struct gcpro gcpro1;
2295 GCPRO1 (args[0]);
2296 gcpro1.nvars = nargs;
2297 specbind (Qinhibit_redisplay, Qt);
2298 /* Use Qt to ensure debugger does not run,
2299 so there is no possibility of wanting to redisplay. */
2300 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2301 safe_eval_handler);
2302 UNGCPRO;
2303 val = unbind_to (count, val);
2306 return val;
2310 /* Call function FN with one argument ARG.
2311 Return the result, or nil if something went wrong. */
2313 Lisp_Object
2314 safe_call1 (fn, arg)
2315 Lisp_Object fn, arg;
2317 Lisp_Object args[2];
2318 args[0] = fn;
2319 args[1] = arg;
2320 return safe_call (2, args);
2325 /***********************************************************************
2326 Debugging
2327 ***********************************************************************/
2329 #if 0
2331 /* Define CHECK_IT to perform sanity checks on iterators.
2332 This is for debugging. It is too slow to do unconditionally. */
2334 static void
2335 check_it (it)
2336 struct it *it;
2338 if (it->method == GET_FROM_STRING)
2340 xassert (STRINGP (it->string));
2341 xassert (IT_STRING_CHARPOS (*it) >= 0);
2343 else
2345 xassert (IT_STRING_CHARPOS (*it) < 0);
2346 if (it->method == GET_FROM_BUFFER)
2348 /* Check that character and byte positions agree. */
2349 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2353 if (it->dpvec)
2354 xassert (it->current.dpvec_index >= 0);
2355 else
2356 xassert (it->current.dpvec_index < 0);
2359 #define CHECK_IT(IT) check_it ((IT))
2361 #else /* not 0 */
2363 #define CHECK_IT(IT) (void) 0
2365 #endif /* not 0 */
2368 #if GLYPH_DEBUG
2370 /* Check that the window end of window W is what we expect it
2371 to be---the last row in the current matrix displaying text. */
2373 static void
2374 check_window_end (w)
2375 struct window *w;
2377 if (!MINI_WINDOW_P (w)
2378 && !NILP (w->window_end_valid))
2380 struct glyph_row *row;
2381 xassert ((row = MATRIX_ROW (w->current_matrix,
2382 XFASTINT (w->window_end_vpos)),
2383 !row->enabled_p
2384 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2385 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2389 #define CHECK_WINDOW_END(W) check_window_end ((W))
2391 #else /* not GLYPH_DEBUG */
2393 #define CHECK_WINDOW_END(W) (void) 0
2395 #endif /* not GLYPH_DEBUG */
2399 /***********************************************************************
2400 Iterator initialization
2401 ***********************************************************************/
2403 /* Initialize IT for displaying current_buffer in window W, starting
2404 at character position CHARPOS. CHARPOS < 0 means that no buffer
2405 position is specified which is useful when the iterator is assigned
2406 a position later. BYTEPOS is the byte position corresponding to
2407 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2409 If ROW is not null, calls to produce_glyphs with IT as parameter
2410 will produce glyphs in that row.
2412 BASE_FACE_ID is the id of a base face to use. It must be one of
2413 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2414 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2415 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2417 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2418 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2419 will be initialized to use the corresponding mode line glyph row of
2420 the desired matrix of W. */
2422 void
2423 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2424 struct it *it;
2425 struct window *w;
2426 int charpos, bytepos;
2427 struct glyph_row *row;
2428 enum face_id base_face_id;
2430 int highlight_region_p;
2432 /* Some precondition checks. */
2433 xassert (w != NULL && it != NULL);
2434 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2435 && charpos <= ZV));
2437 /* If face attributes have been changed since the last redisplay,
2438 free realized faces now because they depend on face definitions
2439 that might have changed. Don't free faces while there might be
2440 desired matrices pending which reference these faces. */
2441 if (face_change_count && !inhibit_free_realized_faces)
2443 face_change_count = 0;
2444 free_all_realized_faces (Qnil);
2447 /* Use one of the mode line rows of W's desired matrix if
2448 appropriate. */
2449 if (row == NULL)
2451 if (base_face_id == MODE_LINE_FACE_ID
2452 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2453 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2454 else if (base_face_id == HEADER_LINE_FACE_ID)
2455 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2458 /* Clear IT. */
2459 bzero (it, sizeof *it);
2460 it->current.overlay_string_index = -1;
2461 it->current.dpvec_index = -1;
2462 it->base_face_id = base_face_id;
2463 it->string = Qnil;
2464 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2466 /* The window in which we iterate over current_buffer: */
2467 XSETWINDOW (it->window, w);
2468 it->w = w;
2469 it->f = XFRAME (w->frame);
2471 /* Extra space between lines (on window systems only). */
2472 if (base_face_id == DEFAULT_FACE_ID
2473 && FRAME_WINDOW_P (it->f))
2475 if (NATNUMP (current_buffer->extra_line_spacing))
2476 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2477 else if (FLOATP (current_buffer->extra_line_spacing))
2478 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2479 * FRAME_LINE_HEIGHT (it->f));
2480 else if (it->f->extra_line_spacing > 0)
2481 it->extra_line_spacing = it->f->extra_line_spacing;
2482 it->max_extra_line_spacing = 0;
2485 /* If realized faces have been removed, e.g. because of face
2486 attribute changes of named faces, recompute them. When running
2487 in batch mode, the face cache of Vterminal_frame is null. If
2488 we happen to get called, make a dummy face cache. */
2489 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2490 init_frame_faces (it->f);
2491 if (FRAME_FACE_CACHE (it->f)->used == 0)
2492 recompute_basic_faces (it->f);
2494 /* Current value of the `slice', `space-width', and 'height' properties. */
2495 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2496 it->space_width = Qnil;
2497 it->font_height = Qnil;
2498 it->override_ascent = -1;
2500 /* Are control characters displayed as `^C'? */
2501 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2503 /* -1 means everything between a CR and the following line end
2504 is invisible. >0 means lines indented more than this value are
2505 invisible. */
2506 it->selective = (INTEGERP (current_buffer->selective_display)
2507 ? XFASTINT (current_buffer->selective_display)
2508 : (!NILP (current_buffer->selective_display)
2509 ? -1 : 0));
2510 it->selective_display_ellipsis_p
2511 = !NILP (current_buffer->selective_display_ellipses);
2513 /* Display table to use. */
2514 it->dp = window_display_table (w);
2516 /* Are multibyte characters enabled in current_buffer? */
2517 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2519 /* Non-zero if we should highlight the region. */
2520 highlight_region_p
2521 = (!NILP (Vtransient_mark_mode)
2522 && !NILP (current_buffer->mark_active)
2523 && XMARKER (current_buffer->mark)->buffer != 0);
2525 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2526 start and end of a visible region in window IT->w. Set both to
2527 -1 to indicate no region. */
2528 if (highlight_region_p
2529 /* Maybe highlight only in selected window. */
2530 && (/* Either show region everywhere. */
2531 highlight_nonselected_windows
2532 /* Or show region in the selected window. */
2533 || w == XWINDOW (selected_window)
2534 /* Or show the region if we are in the mini-buffer and W is
2535 the window the mini-buffer refers to. */
2536 || (MINI_WINDOW_P (XWINDOW (selected_window))
2537 && WINDOWP (minibuf_selected_window)
2538 && w == XWINDOW (minibuf_selected_window))))
2540 int charpos = marker_position (current_buffer->mark);
2541 it->region_beg_charpos = min (PT, charpos);
2542 it->region_end_charpos = max (PT, charpos);
2544 else
2545 it->region_beg_charpos = it->region_end_charpos = -1;
2547 /* Get the position at which the redisplay_end_trigger hook should
2548 be run, if it is to be run at all. */
2549 if (MARKERP (w->redisplay_end_trigger)
2550 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2551 it->redisplay_end_trigger_charpos
2552 = marker_position (w->redisplay_end_trigger);
2553 else if (INTEGERP (w->redisplay_end_trigger))
2554 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2556 /* Correct bogus values of tab_width. */
2557 it->tab_width = XINT (current_buffer->tab_width);
2558 if (it->tab_width <= 0 || it->tab_width > 1000)
2559 it->tab_width = 8;
2561 /* Are lines in the display truncated? */
2562 it->truncate_lines_p
2563 = (base_face_id != DEFAULT_FACE_ID
2564 || XINT (it->w->hscroll)
2565 || (truncate_partial_width_windows
2566 && !WINDOW_FULL_WIDTH_P (it->w))
2567 || !NILP (current_buffer->truncate_lines));
2569 /* Get dimensions of truncation and continuation glyphs. These are
2570 displayed as fringe bitmaps under X, so we don't need them for such
2571 frames. */
2572 if (!FRAME_WINDOW_P (it->f))
2574 if (it->truncate_lines_p)
2576 /* We will need the truncation glyph. */
2577 xassert (it->glyph_row == NULL);
2578 produce_special_glyphs (it, IT_TRUNCATION);
2579 it->truncation_pixel_width = it->pixel_width;
2581 else
2583 /* We will need the continuation glyph. */
2584 xassert (it->glyph_row == NULL);
2585 produce_special_glyphs (it, IT_CONTINUATION);
2586 it->continuation_pixel_width = it->pixel_width;
2589 /* Reset these values to zero because the produce_special_glyphs
2590 above has changed them. */
2591 it->pixel_width = it->ascent = it->descent = 0;
2592 it->phys_ascent = it->phys_descent = 0;
2595 /* Set this after getting the dimensions of truncation and
2596 continuation glyphs, so that we don't produce glyphs when calling
2597 produce_special_glyphs, above. */
2598 it->glyph_row = row;
2599 it->area = TEXT_AREA;
2601 /* Get the dimensions of the display area. The display area
2602 consists of the visible window area plus a horizontally scrolled
2603 part to the left of the window. All x-values are relative to the
2604 start of this total display area. */
2605 if (base_face_id != DEFAULT_FACE_ID)
2607 /* Mode lines, menu bar in terminal frames. */
2608 it->first_visible_x = 0;
2609 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2611 else
2613 it->first_visible_x
2614 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2615 it->last_visible_x = (it->first_visible_x
2616 + window_box_width (w, TEXT_AREA));
2618 /* If we truncate lines, leave room for the truncator glyph(s) at
2619 the right margin. Otherwise, leave room for the continuation
2620 glyph(s). Truncation and continuation glyphs are not inserted
2621 for window-based redisplay. */
2622 if (!FRAME_WINDOW_P (it->f))
2624 if (it->truncate_lines_p)
2625 it->last_visible_x -= it->truncation_pixel_width;
2626 else
2627 it->last_visible_x -= it->continuation_pixel_width;
2630 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2631 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2634 /* Leave room for a border glyph. */
2635 if (!FRAME_WINDOW_P (it->f)
2636 && !WINDOW_RIGHTMOST_P (it->w))
2637 it->last_visible_x -= 1;
2639 it->last_visible_y = window_text_bottom_y (w);
2641 /* For mode lines and alike, arrange for the first glyph having a
2642 left box line if the face specifies a box. */
2643 if (base_face_id != DEFAULT_FACE_ID)
2645 struct face *face;
2647 it->face_id = base_face_id;
2649 /* If we have a boxed mode line, make the first character appear
2650 with a left box line. */
2651 face = FACE_FROM_ID (it->f, base_face_id);
2652 if (face->box != FACE_NO_BOX)
2653 it->start_of_box_run_p = 1;
2656 /* If a buffer position was specified, set the iterator there,
2657 getting overlays and face properties from that position. */
2658 if (charpos >= BUF_BEG (current_buffer))
2660 it->end_charpos = ZV;
2661 it->face_id = -1;
2662 IT_CHARPOS (*it) = charpos;
2664 /* Compute byte position if not specified. */
2665 if (bytepos < charpos)
2666 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2667 else
2668 IT_BYTEPOS (*it) = bytepos;
2670 it->start = it->current;
2672 /* Compute faces etc. */
2673 reseat (it, it->current.pos, 1);
2676 CHECK_IT (it);
2680 /* Initialize IT for the display of window W with window start POS. */
2682 void
2683 start_display (it, w, pos)
2684 struct it *it;
2685 struct window *w;
2686 struct text_pos pos;
2688 struct glyph_row *row;
2689 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2691 row = w->desired_matrix->rows + first_vpos;
2692 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2693 it->first_vpos = first_vpos;
2695 /* Don't reseat to previous visible line start if current start
2696 position is in a string or image. */
2697 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2699 int start_at_line_beg_p;
2700 int first_y = it->current_y;
2702 /* If window start is not at a line start, skip forward to POS to
2703 get the correct continuation lines width. */
2704 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2705 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2706 if (!start_at_line_beg_p)
2708 int new_x;
2710 reseat_at_previous_visible_line_start (it);
2711 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2713 new_x = it->current_x + it->pixel_width;
2715 /* If lines are continued, this line may end in the middle
2716 of a multi-glyph character (e.g. a control character
2717 displayed as \003, or in the middle of an overlay
2718 string). In this case move_it_to above will not have
2719 taken us to the start of the continuation line but to the
2720 end of the continued line. */
2721 if (it->current_x > 0
2722 && !it->truncate_lines_p /* Lines are continued. */
2723 && (/* And glyph doesn't fit on the line. */
2724 new_x > it->last_visible_x
2725 /* Or it fits exactly and we're on a window
2726 system frame. */
2727 || (new_x == it->last_visible_x
2728 && FRAME_WINDOW_P (it->f))))
2730 if (it->current.dpvec_index >= 0
2731 || it->current.overlay_string_index >= 0)
2733 set_iterator_to_next (it, 1);
2734 move_it_in_display_line_to (it, -1, -1, 0);
2737 it->continuation_lines_width += it->current_x;
2740 /* We're starting a new display line, not affected by the
2741 height of the continued line, so clear the appropriate
2742 fields in the iterator structure. */
2743 it->max_ascent = it->max_descent = 0;
2744 it->max_phys_ascent = it->max_phys_descent = 0;
2746 it->current_y = first_y;
2747 it->vpos = 0;
2748 it->current_x = it->hpos = 0;
2752 #if 0 /* Don't assert the following because start_display is sometimes
2753 called intentionally with a window start that is not at a
2754 line start. Please leave this code in as a comment. */
2756 /* Window start should be on a line start, now. */
2757 xassert (it->continuation_lines_width
2758 || IT_CHARPOS (it) == BEGV
2759 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2760 #endif /* 0 */
2764 /* Return 1 if POS is a position in ellipses displayed for invisible
2765 text. W is the window we display, for text property lookup. */
2767 static int
2768 in_ellipses_for_invisible_text_p (pos, w)
2769 struct display_pos *pos;
2770 struct window *w;
2772 Lisp_Object prop, window;
2773 int ellipses_p = 0;
2774 int charpos = CHARPOS (pos->pos);
2776 /* If POS specifies a position in a display vector, this might
2777 be for an ellipsis displayed for invisible text. We won't
2778 get the iterator set up for delivering that ellipsis unless
2779 we make sure that it gets aware of the invisible text. */
2780 if (pos->dpvec_index >= 0
2781 && pos->overlay_string_index < 0
2782 && CHARPOS (pos->string_pos) < 0
2783 && charpos > BEGV
2784 && (XSETWINDOW (window, w),
2785 prop = Fget_char_property (make_number (charpos),
2786 Qinvisible, window),
2787 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2789 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2790 window);
2791 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2794 return ellipses_p;
2798 /* Initialize IT for stepping through current_buffer in window W,
2799 starting at position POS that includes overlay string and display
2800 vector/ control character translation position information. Value
2801 is zero if there are overlay strings with newlines at POS. */
2803 static int
2804 init_from_display_pos (it, w, pos)
2805 struct it *it;
2806 struct window *w;
2807 struct display_pos *pos;
2809 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2810 int i, overlay_strings_with_newlines = 0;
2812 /* If POS specifies a position in a display vector, this might
2813 be for an ellipsis displayed for invisible text. We won't
2814 get the iterator set up for delivering that ellipsis unless
2815 we make sure that it gets aware of the invisible text. */
2816 if (in_ellipses_for_invisible_text_p (pos, w))
2818 --charpos;
2819 bytepos = 0;
2822 /* Keep in mind: the call to reseat in init_iterator skips invisible
2823 text, so we might end up at a position different from POS. This
2824 is only a problem when POS is a row start after a newline and an
2825 overlay starts there with an after-string, and the overlay has an
2826 invisible property. Since we don't skip invisible text in
2827 display_line and elsewhere immediately after consuming the
2828 newline before the row start, such a POS will not be in a string,
2829 but the call to init_iterator below will move us to the
2830 after-string. */
2831 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2833 /* This only scans the current chunk -- it should scan all chunks.
2834 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2835 to 16 in 22.1 to make this a lesser problem. */
2836 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2838 const char *s = SDATA (it->overlay_strings[i]);
2839 const char *e = s + SBYTES (it->overlay_strings[i]);
2841 while (s < e && *s != '\n')
2842 ++s;
2844 if (s < e)
2846 overlay_strings_with_newlines = 1;
2847 break;
2851 /* If position is within an overlay string, set up IT to the right
2852 overlay string. */
2853 if (pos->overlay_string_index >= 0)
2855 int relative_index;
2857 /* If the first overlay string happens to have a `display'
2858 property for an image, the iterator will be set up for that
2859 image, and we have to undo that setup first before we can
2860 correct the overlay string index. */
2861 if (it->method == GET_FROM_IMAGE)
2862 pop_it (it);
2864 /* We already have the first chunk of overlay strings in
2865 IT->overlay_strings. Load more until the one for
2866 pos->overlay_string_index is in IT->overlay_strings. */
2867 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2869 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2870 it->current.overlay_string_index = 0;
2871 while (n--)
2873 load_overlay_strings (it, 0);
2874 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2878 it->current.overlay_string_index = pos->overlay_string_index;
2879 relative_index = (it->current.overlay_string_index
2880 % OVERLAY_STRING_CHUNK_SIZE);
2881 it->string = it->overlay_strings[relative_index];
2882 xassert (STRINGP (it->string));
2883 it->current.string_pos = pos->string_pos;
2884 it->method = GET_FROM_STRING;
2887 #if 0 /* This is bogus because POS not having an overlay string
2888 position does not mean it's after the string. Example: A
2889 line starting with a before-string and initialization of IT
2890 to the previous row's end position. */
2891 else if (it->current.overlay_string_index >= 0)
2893 /* If POS says we're already after an overlay string ending at
2894 POS, make sure to pop the iterator because it will be in
2895 front of that overlay string. When POS is ZV, we've thereby
2896 also ``processed'' overlay strings at ZV. */
2897 while (it->sp)
2898 pop_it (it);
2899 it->current.overlay_string_index = -1;
2900 it->method = GET_FROM_BUFFER;
2901 if (CHARPOS (pos->pos) == ZV)
2902 it->overlay_strings_at_end_processed_p = 1;
2904 #endif /* 0 */
2906 if (CHARPOS (pos->string_pos) >= 0)
2908 /* Recorded position is not in an overlay string, but in another
2909 string. This can only be a string from a `display' property.
2910 IT should already be filled with that string. */
2911 it->current.string_pos = pos->string_pos;
2912 xassert (STRINGP (it->string));
2915 /* Restore position in display vector translations, control
2916 character translations or ellipses. */
2917 if (pos->dpvec_index >= 0)
2919 if (it->dpvec == NULL)
2920 get_next_display_element (it);
2921 xassert (it->dpvec && it->current.dpvec_index == 0);
2922 it->current.dpvec_index = pos->dpvec_index;
2925 CHECK_IT (it);
2926 return !overlay_strings_with_newlines;
2930 /* Initialize IT for stepping through current_buffer in window W
2931 starting at ROW->start. */
2933 static void
2934 init_to_row_start (it, w, row)
2935 struct it *it;
2936 struct window *w;
2937 struct glyph_row *row;
2939 init_from_display_pos (it, w, &row->start);
2940 it->start = row->start;
2941 it->continuation_lines_width = row->continuation_lines_width;
2942 CHECK_IT (it);
2946 /* Initialize IT for stepping through current_buffer in window W
2947 starting in the line following ROW, i.e. starting at ROW->end.
2948 Value is zero if there are overlay strings with newlines at ROW's
2949 end position. */
2951 static int
2952 init_to_row_end (it, w, row)
2953 struct it *it;
2954 struct window *w;
2955 struct glyph_row *row;
2957 int success = 0;
2959 if (init_from_display_pos (it, w, &row->end))
2961 if (row->continued_p)
2962 it->continuation_lines_width
2963 = row->continuation_lines_width + row->pixel_width;
2964 CHECK_IT (it);
2965 success = 1;
2968 return success;
2974 /***********************************************************************
2975 Text properties
2976 ***********************************************************************/
2978 /* Called when IT reaches IT->stop_charpos. Handle text property and
2979 overlay changes. Set IT->stop_charpos to the next position where
2980 to stop. */
2982 static void
2983 handle_stop (it)
2984 struct it *it;
2986 enum prop_handled handled;
2987 int handle_overlay_change_p;
2988 struct props *p;
2990 it->dpvec = NULL;
2991 it->current.dpvec_index = -1;
2992 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2993 it->ignore_overlay_strings_at_pos_p = 0;
2995 /* Use face of preceding text for ellipsis (if invisible) */
2996 if (it->selective_display_ellipsis_p)
2997 it->saved_face_id = it->face_id;
3001 handled = HANDLED_NORMALLY;
3003 /* Call text property handlers. */
3004 for (p = it_props; p->handler; ++p)
3006 handled = p->handler (it);
3008 if (handled == HANDLED_RECOMPUTE_PROPS)
3009 break;
3010 else if (handled == HANDLED_RETURN)
3011 return;
3012 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3013 handle_overlay_change_p = 0;
3016 if (handled != HANDLED_RECOMPUTE_PROPS)
3018 /* Don't check for overlay strings below when set to deliver
3019 characters from a display vector. */
3020 if (it->method == GET_FROM_DISPLAY_VECTOR)
3021 handle_overlay_change_p = 0;
3023 /* Handle overlay changes. */
3024 if (handle_overlay_change_p)
3025 handled = handle_overlay_change (it);
3027 /* Determine where to stop next. */
3028 if (handled == HANDLED_NORMALLY)
3029 compute_stop_pos (it);
3032 while (handled == HANDLED_RECOMPUTE_PROPS);
3036 /* Compute IT->stop_charpos from text property and overlay change
3037 information for IT's current position. */
3039 static void
3040 compute_stop_pos (it)
3041 struct it *it;
3043 register INTERVAL iv, next_iv;
3044 Lisp_Object object, limit, position;
3046 /* If nowhere else, stop at the end. */
3047 it->stop_charpos = it->end_charpos;
3049 if (STRINGP (it->string))
3051 /* Strings are usually short, so don't limit the search for
3052 properties. */
3053 object = it->string;
3054 limit = Qnil;
3055 position = make_number (IT_STRING_CHARPOS (*it));
3057 else
3059 int charpos;
3061 /* If next overlay change is in front of the current stop pos
3062 (which is IT->end_charpos), stop there. Note: value of
3063 next_overlay_change is point-max if no overlay change
3064 follows. */
3065 charpos = next_overlay_change (IT_CHARPOS (*it));
3066 if (charpos < it->stop_charpos)
3067 it->stop_charpos = charpos;
3069 /* If showing the region, we have to stop at the region
3070 start or end because the face might change there. */
3071 if (it->region_beg_charpos > 0)
3073 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3074 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3075 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3076 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3079 /* Set up variables for computing the stop position from text
3080 property changes. */
3081 XSETBUFFER (object, current_buffer);
3082 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3083 position = make_number (IT_CHARPOS (*it));
3087 /* Get the interval containing IT's position. Value is a null
3088 interval if there isn't such an interval. */
3089 iv = validate_interval_range (object, &position, &position, 0);
3090 if (!NULL_INTERVAL_P (iv))
3092 Lisp_Object values_here[LAST_PROP_IDX];
3093 struct props *p;
3095 /* Get properties here. */
3096 for (p = it_props; p->handler; ++p)
3097 values_here[p->idx] = textget (iv->plist, *p->name);
3099 /* Look for an interval following iv that has different
3100 properties. */
3101 for (next_iv = next_interval (iv);
3102 (!NULL_INTERVAL_P (next_iv)
3103 && (NILP (limit)
3104 || XFASTINT (limit) > next_iv->position));
3105 next_iv = next_interval (next_iv))
3107 for (p = it_props; p->handler; ++p)
3109 Lisp_Object new_value;
3111 new_value = textget (next_iv->plist, *p->name);
3112 if (!EQ (values_here[p->idx], new_value))
3113 break;
3116 if (p->handler)
3117 break;
3120 if (!NULL_INTERVAL_P (next_iv))
3122 if (INTEGERP (limit)
3123 && next_iv->position >= XFASTINT (limit))
3124 /* No text property change up to limit. */
3125 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3126 else
3127 /* Text properties change in next_iv. */
3128 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3132 xassert (STRINGP (it->string)
3133 || (it->stop_charpos >= BEGV
3134 && it->stop_charpos >= IT_CHARPOS (*it)));
3138 /* Return the position of the next overlay change after POS in
3139 current_buffer. Value is point-max if no overlay change
3140 follows. This is like `next-overlay-change' but doesn't use
3141 xmalloc. */
3143 static int
3144 next_overlay_change (pos)
3145 int pos;
3147 int noverlays;
3148 int endpos;
3149 Lisp_Object *overlays;
3150 int i;
3152 /* Get all overlays at the given position. */
3153 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3155 /* If any of these overlays ends before endpos,
3156 use its ending point instead. */
3157 for (i = 0; i < noverlays; ++i)
3159 Lisp_Object oend;
3160 int oendpos;
3162 oend = OVERLAY_END (overlays[i]);
3163 oendpos = OVERLAY_POSITION (oend);
3164 endpos = min (endpos, oendpos);
3167 return endpos;
3172 /***********************************************************************
3173 Fontification
3174 ***********************************************************************/
3176 /* Handle changes in the `fontified' property of the current buffer by
3177 calling hook functions from Qfontification_functions to fontify
3178 regions of text. */
3180 static enum prop_handled
3181 handle_fontified_prop (it)
3182 struct it *it;
3184 Lisp_Object prop, pos;
3185 enum prop_handled handled = HANDLED_NORMALLY;
3187 if (!NILP (Vmemory_full))
3188 return handled;
3190 /* Get the value of the `fontified' property at IT's current buffer
3191 position. (The `fontified' property doesn't have a special
3192 meaning in strings.) If the value is nil, call functions from
3193 Qfontification_functions. */
3194 if (!STRINGP (it->string)
3195 && it->s == NULL
3196 && !NILP (Vfontification_functions)
3197 && !NILP (Vrun_hooks)
3198 && (pos = make_number (IT_CHARPOS (*it)),
3199 prop = Fget_char_property (pos, Qfontified, Qnil),
3200 NILP (prop)))
3202 int count = SPECPDL_INDEX ();
3203 Lisp_Object val;
3205 val = Vfontification_functions;
3206 specbind (Qfontification_functions, Qnil);
3208 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3209 safe_call1 (val, pos);
3210 else
3212 Lisp_Object globals, fn;
3213 struct gcpro gcpro1, gcpro2;
3215 globals = Qnil;
3216 GCPRO2 (val, globals);
3218 for (; CONSP (val); val = XCDR (val))
3220 fn = XCAR (val);
3222 if (EQ (fn, Qt))
3224 /* A value of t indicates this hook has a local
3225 binding; it means to run the global binding too.
3226 In a global value, t should not occur. If it
3227 does, we must ignore it to avoid an endless
3228 loop. */
3229 for (globals = Fdefault_value (Qfontification_functions);
3230 CONSP (globals);
3231 globals = XCDR (globals))
3233 fn = XCAR (globals);
3234 if (!EQ (fn, Qt))
3235 safe_call1 (fn, pos);
3238 else
3239 safe_call1 (fn, pos);
3242 UNGCPRO;
3245 unbind_to (count, Qnil);
3247 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3248 something. This avoids an endless loop if they failed to
3249 fontify the text for which reason ever. */
3250 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3251 handled = HANDLED_RECOMPUTE_PROPS;
3254 return handled;
3259 /***********************************************************************
3260 Faces
3261 ***********************************************************************/
3263 /* Set up iterator IT from face properties at its current position.
3264 Called from handle_stop. */
3266 static enum prop_handled
3267 handle_face_prop (it)
3268 struct it *it;
3270 int new_face_id, next_stop;
3272 if (!STRINGP (it->string))
3274 new_face_id
3275 = face_at_buffer_position (it->w,
3276 IT_CHARPOS (*it),
3277 it->region_beg_charpos,
3278 it->region_end_charpos,
3279 &next_stop,
3280 (IT_CHARPOS (*it)
3281 + TEXT_PROP_DISTANCE_LIMIT),
3284 /* Is this a start of a run of characters with box face?
3285 Caveat: this can be called for a freshly initialized
3286 iterator; face_id is -1 in this case. We know that the new
3287 face will not change until limit, i.e. if the new face has a
3288 box, all characters up to limit will have one. But, as
3289 usual, we don't know whether limit is really the end. */
3290 if (new_face_id != it->face_id)
3292 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3294 /* If new face has a box but old face has not, this is
3295 the start of a run of characters with box, i.e. it has
3296 a shadow on the left side. The value of face_id of the
3297 iterator will be -1 if this is the initial call that gets
3298 the face. In this case, we have to look in front of IT's
3299 position and see whether there is a face != new_face_id. */
3300 it->start_of_box_run_p
3301 = (new_face->box != FACE_NO_BOX
3302 && (it->face_id >= 0
3303 || IT_CHARPOS (*it) == BEG
3304 || new_face_id != face_before_it_pos (it)));
3305 it->face_box_p = new_face->box != FACE_NO_BOX;
3308 else
3310 int base_face_id, bufpos;
3312 if (it->current.overlay_string_index >= 0)
3313 bufpos = IT_CHARPOS (*it);
3314 else
3315 bufpos = 0;
3317 /* For strings from a buffer, i.e. overlay strings or strings
3318 from a `display' property, use the face at IT's current
3319 buffer position as the base face to merge with, so that
3320 overlay strings appear in the same face as surrounding
3321 text, unless they specify their own faces. */
3322 base_face_id = underlying_face_id (it);
3324 new_face_id = face_at_string_position (it->w,
3325 it->string,
3326 IT_STRING_CHARPOS (*it),
3327 bufpos,
3328 it->region_beg_charpos,
3329 it->region_end_charpos,
3330 &next_stop,
3331 base_face_id, 0);
3333 #if 0 /* This shouldn't be neccessary. Let's check it. */
3334 /* If IT is used to display a mode line we would really like to
3335 use the mode line face instead of the frame's default face. */
3336 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3337 && new_face_id == DEFAULT_FACE_ID)
3338 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3339 #endif
3341 /* Is this a start of a run of characters with box? Caveat:
3342 this can be called for a freshly allocated iterator; face_id
3343 is -1 is this case. We know that the new face will not
3344 change until the next check pos, i.e. if the new face has a
3345 box, all characters up to that position will have a
3346 box. But, as usual, we don't know whether that position
3347 is really the end. */
3348 if (new_face_id != it->face_id)
3350 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3351 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3353 /* If new face has a box but old face hasn't, this is the
3354 start of a run of characters with box, i.e. it has a
3355 shadow on the left side. */
3356 it->start_of_box_run_p
3357 = new_face->box && (old_face == NULL || !old_face->box);
3358 it->face_box_p = new_face->box != FACE_NO_BOX;
3362 it->face_id = new_face_id;
3363 return HANDLED_NORMALLY;
3367 /* Return the ID of the face ``underlying'' IT's current position,
3368 which is in a string. If the iterator is associated with a
3369 buffer, return the face at IT's current buffer position.
3370 Otherwise, use the iterator's base_face_id. */
3372 static int
3373 underlying_face_id (it)
3374 struct it *it;
3376 int face_id = it->base_face_id, i;
3378 xassert (STRINGP (it->string));
3380 for (i = it->sp - 1; i >= 0; --i)
3381 if (NILP (it->stack[i].string))
3382 face_id = it->stack[i].face_id;
3384 return face_id;
3388 /* Compute the face one character before or after the current position
3389 of IT. BEFORE_P non-zero means get the face in front of IT's
3390 position. Value is the id of the face. */
3392 static int
3393 face_before_or_after_it_pos (it, before_p)
3394 struct it *it;
3395 int before_p;
3397 int face_id, limit;
3398 int next_check_charpos;
3399 struct text_pos pos;
3401 xassert (it->s == NULL);
3403 if (STRINGP (it->string))
3405 int bufpos, base_face_id;
3407 /* No face change past the end of the string (for the case
3408 we are padding with spaces). No face change before the
3409 string start. */
3410 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3411 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3412 return it->face_id;
3414 /* Set pos to the position before or after IT's current position. */
3415 if (before_p)
3416 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3417 else
3418 /* For composition, we must check the character after the
3419 composition. */
3420 pos = (it->what == IT_COMPOSITION
3421 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3422 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3424 if (it->current.overlay_string_index >= 0)
3425 bufpos = IT_CHARPOS (*it);
3426 else
3427 bufpos = 0;
3429 base_face_id = underlying_face_id (it);
3431 /* Get the face for ASCII, or unibyte. */
3432 face_id = face_at_string_position (it->w,
3433 it->string,
3434 CHARPOS (pos),
3435 bufpos,
3436 it->region_beg_charpos,
3437 it->region_end_charpos,
3438 &next_check_charpos,
3439 base_face_id, 0);
3441 /* Correct the face for charsets different from ASCII. Do it
3442 for the multibyte case only. The face returned above is
3443 suitable for unibyte text if IT->string is unibyte. */
3444 if (STRING_MULTIBYTE (it->string))
3446 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3447 int rest = SBYTES (it->string) - BYTEPOS (pos);
3448 int c, len;
3449 struct face *face = FACE_FROM_ID (it->f, face_id);
3451 c = string_char_and_length (p, rest, &len);
3452 face_id = FACE_FOR_CHAR (it->f, face, c);
3455 else
3457 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3458 || (IT_CHARPOS (*it) <= BEGV && before_p))
3459 return it->face_id;
3461 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3462 pos = it->current.pos;
3464 if (before_p)
3465 DEC_TEXT_POS (pos, it->multibyte_p);
3466 else
3468 if (it->what == IT_COMPOSITION)
3469 /* For composition, we must check the position after the
3470 composition. */
3471 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3472 else
3473 INC_TEXT_POS (pos, it->multibyte_p);
3476 /* Determine face for CHARSET_ASCII, or unibyte. */
3477 face_id = face_at_buffer_position (it->w,
3478 CHARPOS (pos),
3479 it->region_beg_charpos,
3480 it->region_end_charpos,
3481 &next_check_charpos,
3482 limit, 0);
3484 /* Correct the face for charsets different from ASCII. Do it
3485 for the multibyte case only. The face returned above is
3486 suitable for unibyte text if current_buffer is unibyte. */
3487 if (it->multibyte_p)
3489 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3490 struct face *face = FACE_FROM_ID (it->f, face_id);
3491 face_id = FACE_FOR_CHAR (it->f, face, c);
3495 return face_id;
3500 /***********************************************************************
3501 Invisible text
3502 ***********************************************************************/
3504 /* Set up iterator IT from invisible properties at its current
3505 position. Called from handle_stop. */
3507 static enum prop_handled
3508 handle_invisible_prop (it)
3509 struct it *it;
3511 enum prop_handled handled = HANDLED_NORMALLY;
3513 if (STRINGP (it->string))
3515 extern Lisp_Object Qinvisible;
3516 Lisp_Object prop, end_charpos, limit, charpos;
3518 /* Get the value of the invisible text property at the
3519 current position. Value will be nil if there is no such
3520 property. */
3521 charpos = make_number (IT_STRING_CHARPOS (*it));
3522 prop = Fget_text_property (charpos, Qinvisible, it->string);
3524 if (!NILP (prop)
3525 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3527 handled = HANDLED_RECOMPUTE_PROPS;
3529 /* Get the position at which the next change of the
3530 invisible text property can be found in IT->string.
3531 Value will be nil if the property value is the same for
3532 all the rest of IT->string. */
3533 XSETINT (limit, SCHARS (it->string));
3534 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3535 it->string, limit);
3537 /* Text at current position is invisible. The next
3538 change in the property is at position end_charpos.
3539 Move IT's current position to that position. */
3540 if (INTEGERP (end_charpos)
3541 && XFASTINT (end_charpos) < XFASTINT (limit))
3543 struct text_pos old;
3544 old = it->current.string_pos;
3545 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3546 compute_string_pos (&it->current.string_pos, old, it->string);
3548 else
3550 /* The rest of the string is invisible. If this is an
3551 overlay string, proceed with the next overlay string
3552 or whatever comes and return a character from there. */
3553 if (it->current.overlay_string_index >= 0)
3555 next_overlay_string (it);
3556 /* Don't check for overlay strings when we just
3557 finished processing them. */
3558 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3560 else
3562 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3563 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3568 else
3570 int invis_p, newpos, next_stop, start_charpos;
3571 Lisp_Object pos, prop, overlay;
3573 /* First of all, is there invisible text at this position? */
3574 start_charpos = IT_CHARPOS (*it);
3575 pos = make_number (IT_CHARPOS (*it));
3576 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3577 &overlay);
3578 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3580 /* If we are on invisible text, skip over it. */
3581 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3583 /* Record whether we have to display an ellipsis for the
3584 invisible text. */
3585 int display_ellipsis_p = invis_p == 2;
3587 handled = HANDLED_RECOMPUTE_PROPS;
3589 /* Loop skipping over invisible text. The loop is left at
3590 ZV or with IT on the first char being visible again. */
3593 /* Try to skip some invisible text. Return value is the
3594 position reached which can be equal to IT's position
3595 if there is nothing invisible here. This skips both
3596 over invisible text properties and overlays with
3597 invisible property. */
3598 newpos = skip_invisible (IT_CHARPOS (*it),
3599 &next_stop, ZV, it->window);
3601 /* If we skipped nothing at all we weren't at invisible
3602 text in the first place. If everything to the end of
3603 the buffer was skipped, end the loop. */
3604 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3605 invis_p = 0;
3606 else
3608 /* We skipped some characters but not necessarily
3609 all there are. Check if we ended up on visible
3610 text. Fget_char_property returns the property of
3611 the char before the given position, i.e. if we
3612 get invis_p = 0, this means that the char at
3613 newpos is visible. */
3614 pos = make_number (newpos);
3615 prop = Fget_char_property (pos, Qinvisible, it->window);
3616 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3619 /* If we ended up on invisible text, proceed to
3620 skip starting with next_stop. */
3621 if (invis_p)
3622 IT_CHARPOS (*it) = next_stop;
3624 /* If there are adjacent invisible texts, don't lose the
3625 second one's ellipsis. */
3626 if (invis_p == 2)
3627 display_ellipsis_p = 1;
3629 while (invis_p);
3631 /* The position newpos is now either ZV or on visible text. */
3632 IT_CHARPOS (*it) = newpos;
3633 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3635 /* If there are before-strings at the start of invisible
3636 text, and the text is invisible because of a text
3637 property, arrange to show before-strings because 20.x did
3638 it that way. (If the text is invisible because of an
3639 overlay property instead of a text property, this is
3640 already handled in the overlay code.) */
3641 if (NILP (overlay)
3642 && get_overlay_strings (it, start_charpos))
3644 handled = HANDLED_RECOMPUTE_PROPS;
3645 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3647 else if (display_ellipsis_p)
3649 /* Make sure that the glyphs of the ellipsis will get
3650 correct `charpos' values. If we would not update
3651 it->position here, the glyphs would belong to the
3652 last visible character _before_ the invisible
3653 text, which confuses `set_cursor_from_row'.
3655 We use the last invisible position instead of the
3656 first because this way the cursor is always drawn on
3657 the first "." of the ellipsis, whenever PT is inside
3658 the invisible text. Otherwise the cursor would be
3659 placed _after_ the ellipsis when the point is after the
3660 first invisible character. */
3661 if (!STRINGP (it->object))
3663 it->position.charpos = IT_CHARPOS (*it) - 1;
3664 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3666 setup_for_ellipsis (it, 0);
3671 return handled;
3675 /* Make iterator IT return `...' next.
3676 Replaces LEN characters from buffer. */
3678 static void
3679 setup_for_ellipsis (it, len)
3680 struct it *it;
3681 int len;
3683 /* Use the display table definition for `...'. Invalid glyphs
3684 will be handled by the method returning elements from dpvec. */
3685 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3687 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3688 it->dpvec = v->contents;
3689 it->dpend = v->contents + v->size;
3691 else
3693 /* Default `...'. */
3694 it->dpvec = default_invis_vector;
3695 it->dpend = default_invis_vector + 3;
3698 it->dpvec_char_len = len;
3699 it->current.dpvec_index = 0;
3700 it->dpvec_face_id = -1;
3702 /* Remember the current face id in case glyphs specify faces.
3703 IT's face is restored in set_iterator_to_next.
3704 saved_face_id was set to preceding char's face in handle_stop. */
3705 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3706 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3708 it->method = GET_FROM_DISPLAY_VECTOR;
3709 it->ellipsis_p = 1;
3714 /***********************************************************************
3715 'display' property
3716 ***********************************************************************/
3718 /* Set up iterator IT from `display' property at its current position.
3719 Called from handle_stop.
3720 We return HANDLED_RETURN if some part of the display property
3721 overrides the display of the buffer text itself.
3722 Otherwise we return HANDLED_NORMALLY. */
3724 static enum prop_handled
3725 handle_display_prop (it)
3726 struct it *it;
3728 Lisp_Object prop, object;
3729 struct text_pos *position;
3730 /* Nonzero if some property replaces the display of the text itself. */
3731 int display_replaced_p = 0;
3733 if (STRINGP (it->string))
3735 object = it->string;
3736 position = &it->current.string_pos;
3738 else
3740 XSETWINDOW (object, it->w);
3741 position = &it->current.pos;
3744 /* Reset those iterator values set from display property values. */
3745 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3746 it->space_width = Qnil;
3747 it->font_height = Qnil;
3748 it->voffset = 0;
3750 /* We don't support recursive `display' properties, i.e. string
3751 values that have a string `display' property, that have a string
3752 `display' property etc. */
3753 if (!it->string_from_display_prop_p)
3754 it->area = TEXT_AREA;
3756 prop = Fget_char_property (make_number (position->charpos),
3757 Qdisplay, object);
3758 if (NILP (prop))
3759 return HANDLED_NORMALLY;
3761 if (!STRINGP (it->string))
3762 object = it->w->buffer;
3764 if (CONSP (prop)
3765 /* Simple properties. */
3766 && !EQ (XCAR (prop), Qimage)
3767 && !EQ (XCAR (prop), Qspace)
3768 && !EQ (XCAR (prop), Qwhen)
3769 && !EQ (XCAR (prop), Qslice)
3770 && !EQ (XCAR (prop), Qspace_width)
3771 && !EQ (XCAR (prop), Qheight)
3772 && !EQ (XCAR (prop), Qraise)
3773 /* Marginal area specifications. */
3774 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3775 && !EQ (XCAR (prop), Qleft_fringe)
3776 && !EQ (XCAR (prop), Qright_fringe)
3777 && !NILP (XCAR (prop)))
3779 for (; CONSP (prop); prop = XCDR (prop))
3781 if (handle_single_display_spec (it, XCAR (prop), object,
3782 position, display_replaced_p))
3783 display_replaced_p = 1;
3786 else if (VECTORP (prop))
3788 int i;
3789 for (i = 0; i < ASIZE (prop); ++i)
3790 if (handle_single_display_spec (it, AREF (prop, i), object,
3791 position, display_replaced_p))
3792 display_replaced_p = 1;
3794 else
3796 int ret = handle_single_display_spec (it, prop, object, position, 0);
3797 if (ret < 0) /* Replaced by "", i.e. nothing. */
3798 return HANDLED_RECOMPUTE_PROPS;
3799 if (ret)
3800 display_replaced_p = 1;
3803 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3807 /* Value is the position of the end of the `display' property starting
3808 at START_POS in OBJECT. */
3810 static struct text_pos
3811 display_prop_end (it, object, start_pos)
3812 struct it *it;
3813 Lisp_Object object;
3814 struct text_pos start_pos;
3816 Lisp_Object end;
3817 struct text_pos end_pos;
3819 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3820 Qdisplay, object, Qnil);
3821 CHARPOS (end_pos) = XFASTINT (end);
3822 if (STRINGP (object))
3823 compute_string_pos (&end_pos, start_pos, it->string);
3824 else
3825 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3827 return end_pos;
3831 /* Set up IT from a single `display' specification PROP. OBJECT
3832 is the object in which the `display' property was found. *POSITION
3833 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3834 means that we previously saw a display specification which already
3835 replaced text display with something else, for example an image;
3836 we ignore such properties after the first one has been processed.
3838 If PROP is a `space' or `image' specification, and in some other
3839 cases too, set *POSITION to the position where the `display'
3840 property ends.
3842 Value is non-zero if something was found which replaces the display
3843 of buffer or string text. Specifically, the value is -1 if that
3844 "something" is "nothing". */
3846 static int
3847 handle_single_display_spec (it, spec, object, position,
3848 display_replaced_before_p)
3849 struct it *it;
3850 Lisp_Object spec;
3851 Lisp_Object object;
3852 struct text_pos *position;
3853 int display_replaced_before_p;
3855 Lisp_Object form;
3856 Lisp_Object location, value;
3857 struct text_pos start_pos;
3858 int valid_p;
3860 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3861 If the result is non-nil, use VALUE instead of SPEC. */
3862 form = Qt;
3863 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3865 spec = XCDR (spec);
3866 if (!CONSP (spec))
3867 return 0;
3868 form = XCAR (spec);
3869 spec = XCDR (spec);
3872 if (!NILP (form) && !EQ (form, Qt))
3874 int count = SPECPDL_INDEX ();
3875 struct gcpro gcpro1;
3877 /* Bind `object' to the object having the `display' property, a
3878 buffer or string. Bind `position' to the position in the
3879 object where the property was found, and `buffer-position'
3880 to the current position in the buffer. */
3881 specbind (Qobject, object);
3882 specbind (Qposition, make_number (CHARPOS (*position)));
3883 specbind (Qbuffer_position,
3884 make_number (STRINGP (object)
3885 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3886 GCPRO1 (form);
3887 form = safe_eval (form);
3888 UNGCPRO;
3889 unbind_to (count, Qnil);
3892 if (NILP (form))
3893 return 0;
3895 /* Handle `(height HEIGHT)' specifications. */
3896 if (CONSP (spec)
3897 && EQ (XCAR (spec), Qheight)
3898 && CONSP (XCDR (spec)))
3900 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3901 return 0;
3903 it->font_height = XCAR (XCDR (spec));
3904 if (!NILP (it->font_height))
3906 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3907 int new_height = -1;
3909 if (CONSP (it->font_height)
3910 && (EQ (XCAR (it->font_height), Qplus)
3911 || EQ (XCAR (it->font_height), Qminus))
3912 && CONSP (XCDR (it->font_height))
3913 && INTEGERP (XCAR (XCDR (it->font_height))))
3915 /* `(+ N)' or `(- N)' where N is an integer. */
3916 int steps = XINT (XCAR (XCDR (it->font_height)));
3917 if (EQ (XCAR (it->font_height), Qplus))
3918 steps = - steps;
3919 it->face_id = smaller_face (it->f, it->face_id, steps);
3921 else if (FUNCTIONP (it->font_height))
3923 /* Call function with current height as argument.
3924 Value is the new height. */
3925 Lisp_Object height;
3926 height = safe_call1 (it->font_height,
3927 face->lface[LFACE_HEIGHT_INDEX]);
3928 if (NUMBERP (height))
3929 new_height = XFLOATINT (height);
3931 else if (NUMBERP (it->font_height))
3933 /* Value is a multiple of the canonical char height. */
3934 struct face *face;
3936 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3937 new_height = (XFLOATINT (it->font_height)
3938 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3940 else
3942 /* Evaluate IT->font_height with `height' bound to the
3943 current specified height to get the new height. */
3944 int count = SPECPDL_INDEX ();
3946 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3947 value = safe_eval (it->font_height);
3948 unbind_to (count, Qnil);
3950 if (NUMBERP (value))
3951 new_height = XFLOATINT (value);
3954 if (new_height > 0)
3955 it->face_id = face_with_height (it->f, it->face_id, new_height);
3958 return 0;
3961 /* Handle `(space_width WIDTH)'. */
3962 if (CONSP (spec)
3963 && EQ (XCAR (spec), Qspace_width)
3964 && CONSP (XCDR (spec)))
3966 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3967 return 0;
3969 value = XCAR (XCDR (spec));
3970 if (NUMBERP (value) && XFLOATINT (value) > 0)
3971 it->space_width = value;
3973 return 0;
3976 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3977 if (CONSP (spec)
3978 && EQ (XCAR (spec), Qslice))
3980 Lisp_Object tem;
3982 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3983 return 0;
3985 if (tem = XCDR (spec), CONSP (tem))
3987 it->slice.x = XCAR (tem);
3988 if (tem = XCDR (tem), CONSP (tem))
3990 it->slice.y = XCAR (tem);
3991 if (tem = XCDR (tem), CONSP (tem))
3993 it->slice.width = XCAR (tem);
3994 if (tem = XCDR (tem), CONSP (tem))
3995 it->slice.height = XCAR (tem);
4000 return 0;
4003 /* Handle `(raise FACTOR)'. */
4004 if (CONSP (spec)
4005 && EQ (XCAR (spec), Qraise)
4006 && CONSP (XCDR (spec)))
4008 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4009 return 0;
4011 #ifdef HAVE_WINDOW_SYSTEM
4012 value = XCAR (XCDR (spec));
4013 if (NUMBERP (value))
4015 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4016 it->voffset = - (XFLOATINT (value)
4017 * (FONT_HEIGHT (face->font)));
4019 #endif /* HAVE_WINDOW_SYSTEM */
4021 return 0;
4024 /* Don't handle the other kinds of display specifications
4025 inside a string that we got from a `display' property. */
4026 if (it->string_from_display_prop_p)
4027 return 0;
4029 /* Characters having this form of property are not displayed, so
4030 we have to find the end of the property. */
4031 start_pos = *position;
4032 *position = display_prop_end (it, object, start_pos);
4033 value = Qnil;
4035 /* Stop the scan at that end position--we assume that all
4036 text properties change there. */
4037 it->stop_charpos = position->charpos;
4039 /* Handle `(left-fringe BITMAP [FACE])'
4040 and `(right-fringe BITMAP [FACE])'. */
4041 if (CONSP (spec)
4042 && (EQ (XCAR (spec), Qleft_fringe)
4043 || EQ (XCAR (spec), Qright_fringe))
4044 && CONSP (XCDR (spec)))
4046 int face_id = DEFAULT_FACE_ID;
4047 int fringe_bitmap;
4049 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4050 /* If we return here, POSITION has been advanced
4051 across the text with this property. */
4052 return 0;
4054 #ifdef HAVE_WINDOW_SYSTEM
4055 value = XCAR (XCDR (spec));
4056 if (!SYMBOLP (value)
4057 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4058 /* If we return here, POSITION has been advanced
4059 across the text with this property. */
4060 return 0;
4062 if (CONSP (XCDR (XCDR (spec))))
4064 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4065 int face_id2 = lookup_derived_face (it->f, face_name,
4066 'A', FRINGE_FACE_ID, 0);
4067 if (face_id2 >= 0)
4068 face_id = face_id2;
4071 /* Save current settings of IT so that we can restore them
4072 when we are finished with the glyph property value. */
4074 push_it (it);
4076 it->area = TEXT_AREA;
4077 it->what = IT_IMAGE;
4078 it->image_id = -1; /* no image */
4079 it->position = start_pos;
4080 it->object = NILP (object) ? it->w->buffer : object;
4081 it->method = GET_FROM_IMAGE;
4082 it->face_id = face_id;
4084 /* Say that we haven't consumed the characters with
4085 `display' property yet. The call to pop_it in
4086 set_iterator_to_next will clean this up. */
4087 *position = start_pos;
4089 if (EQ (XCAR (spec), Qleft_fringe))
4091 it->left_user_fringe_bitmap = fringe_bitmap;
4092 it->left_user_fringe_face_id = face_id;
4094 else
4096 it->right_user_fringe_bitmap = fringe_bitmap;
4097 it->right_user_fringe_face_id = face_id;
4099 #endif /* HAVE_WINDOW_SYSTEM */
4100 return 1;
4103 /* Prepare to handle `((margin left-margin) ...)',
4104 `((margin right-margin) ...)' and `((margin nil) ...)'
4105 prefixes for display specifications. */
4106 location = Qunbound;
4107 if (CONSP (spec) && CONSP (XCAR (spec)))
4109 Lisp_Object tem;
4111 value = XCDR (spec);
4112 if (CONSP (value))
4113 value = XCAR (value);
4115 tem = XCAR (spec);
4116 if (EQ (XCAR (tem), Qmargin)
4117 && (tem = XCDR (tem),
4118 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4119 (NILP (tem)
4120 || EQ (tem, Qleft_margin)
4121 || EQ (tem, Qright_margin))))
4122 location = tem;
4125 if (EQ (location, Qunbound))
4127 location = Qnil;
4128 value = spec;
4131 /* After this point, VALUE is the property after any
4132 margin prefix has been stripped. It must be a string,
4133 an image specification, or `(space ...)'.
4135 LOCATION specifies where to display: `left-margin',
4136 `right-margin' or nil. */
4138 valid_p = (STRINGP (value)
4139 #ifdef HAVE_WINDOW_SYSTEM
4140 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4141 #endif /* not HAVE_WINDOW_SYSTEM */
4142 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4144 if (valid_p && !display_replaced_before_p)
4146 /* Save current settings of IT so that we can restore them
4147 when we are finished with the glyph property value. */
4148 push_it (it);
4150 if (NILP (location))
4151 it->area = TEXT_AREA;
4152 else if (EQ (location, Qleft_margin))
4153 it->area = LEFT_MARGIN_AREA;
4154 else
4155 it->area = RIGHT_MARGIN_AREA;
4157 if (STRINGP (value))
4159 if (SCHARS (value) == 0)
4161 pop_it (it);
4162 return -1; /* Replaced by "", i.e. nothing. */
4164 it->string = value;
4165 it->multibyte_p = STRING_MULTIBYTE (it->string);
4166 it->current.overlay_string_index = -1;
4167 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4168 it->end_charpos = it->string_nchars = SCHARS (it->string);
4169 it->method = GET_FROM_STRING;
4170 it->stop_charpos = 0;
4171 it->string_from_display_prop_p = 1;
4172 /* Say that we haven't consumed the characters with
4173 `display' property yet. The call to pop_it in
4174 set_iterator_to_next will clean this up. */
4175 *position = start_pos;
4177 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4179 it->method = GET_FROM_STRETCH;
4180 it->object = value;
4181 *position = it->position = start_pos;
4183 #ifdef HAVE_WINDOW_SYSTEM
4184 else
4186 it->what = IT_IMAGE;
4187 it->image_id = lookup_image (it->f, value);
4188 it->position = start_pos;
4189 it->object = NILP (object) ? it->w->buffer : object;
4190 it->method = GET_FROM_IMAGE;
4192 /* Say that we haven't consumed the characters with
4193 `display' property yet. The call to pop_it in
4194 set_iterator_to_next will clean this up. */
4195 *position = start_pos;
4197 #endif /* HAVE_WINDOW_SYSTEM */
4199 return 1;
4202 /* Invalid property or property not supported. Restore
4203 POSITION to what it was before. */
4204 *position = start_pos;
4205 return 0;
4209 /* Check if SPEC is a display specification value whose text should be
4210 treated as intangible. */
4212 static int
4213 single_display_spec_intangible_p (prop)
4214 Lisp_Object prop;
4216 /* Skip over `when FORM'. */
4217 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4219 prop = XCDR (prop);
4220 if (!CONSP (prop))
4221 return 0;
4222 prop = XCDR (prop);
4225 if (STRINGP (prop))
4226 return 1;
4228 if (!CONSP (prop))
4229 return 0;
4231 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4232 we don't need to treat text as intangible. */
4233 if (EQ (XCAR (prop), Qmargin))
4235 prop = XCDR (prop);
4236 if (!CONSP (prop))
4237 return 0;
4239 prop = XCDR (prop);
4240 if (!CONSP (prop)
4241 || EQ (XCAR (prop), Qleft_margin)
4242 || EQ (XCAR (prop), Qright_margin))
4243 return 0;
4246 return (CONSP (prop)
4247 && (EQ (XCAR (prop), Qimage)
4248 || EQ (XCAR (prop), Qspace)));
4252 /* Check if PROP is a display property value whose text should be
4253 treated as intangible. */
4256 display_prop_intangible_p (prop)
4257 Lisp_Object prop;
4259 if (CONSP (prop)
4260 && CONSP (XCAR (prop))
4261 && !EQ (Qmargin, XCAR (XCAR (prop))))
4263 /* A list of sub-properties. */
4264 while (CONSP (prop))
4266 if (single_display_spec_intangible_p (XCAR (prop)))
4267 return 1;
4268 prop = XCDR (prop);
4271 else if (VECTORP (prop))
4273 /* A vector of sub-properties. */
4274 int i;
4275 for (i = 0; i < ASIZE (prop); ++i)
4276 if (single_display_spec_intangible_p (AREF (prop, i)))
4277 return 1;
4279 else
4280 return single_display_spec_intangible_p (prop);
4282 return 0;
4286 /* Return 1 if PROP is a display sub-property value containing STRING. */
4288 static int
4289 single_display_spec_string_p (prop, string)
4290 Lisp_Object prop, string;
4292 if (EQ (string, prop))
4293 return 1;
4295 /* Skip over `when FORM'. */
4296 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4298 prop = XCDR (prop);
4299 if (!CONSP (prop))
4300 return 0;
4301 prop = XCDR (prop);
4304 if (CONSP (prop))
4305 /* Skip over `margin LOCATION'. */
4306 if (EQ (XCAR (prop), Qmargin))
4308 prop = XCDR (prop);
4309 if (!CONSP (prop))
4310 return 0;
4312 prop = XCDR (prop);
4313 if (!CONSP (prop))
4314 return 0;
4317 return CONSP (prop) && EQ (XCAR (prop), string);
4321 /* Return 1 if STRING appears in the `display' property PROP. */
4323 static int
4324 display_prop_string_p (prop, string)
4325 Lisp_Object prop, string;
4327 if (CONSP (prop)
4328 && CONSP (XCAR (prop))
4329 && !EQ (Qmargin, XCAR (XCAR (prop))))
4331 /* A list of sub-properties. */
4332 while (CONSP (prop))
4334 if (single_display_spec_string_p (XCAR (prop), string))
4335 return 1;
4336 prop = XCDR (prop);
4339 else if (VECTORP (prop))
4341 /* A vector of sub-properties. */
4342 int i;
4343 for (i = 0; i < ASIZE (prop); ++i)
4344 if (single_display_spec_string_p (AREF (prop, i), string))
4345 return 1;
4347 else
4348 return single_display_spec_string_p (prop, string);
4350 return 0;
4354 /* Determine from which buffer position in W's buffer STRING comes
4355 from. AROUND_CHARPOS is an approximate position where it could
4356 be from. Value is the buffer position or 0 if it couldn't be
4357 determined.
4359 W's buffer must be current.
4361 This function is necessary because we don't record buffer positions
4362 in glyphs generated from strings (to keep struct glyph small).
4363 This function may only use code that doesn't eval because it is
4364 called asynchronously from note_mouse_highlight. */
4367 string_buffer_position (w, string, around_charpos)
4368 struct window *w;
4369 Lisp_Object string;
4370 int around_charpos;
4372 Lisp_Object limit, prop, pos;
4373 const int MAX_DISTANCE = 1000;
4374 int found = 0;
4376 pos = make_number (around_charpos);
4377 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4378 while (!found && !EQ (pos, limit))
4380 prop = Fget_char_property (pos, Qdisplay, Qnil);
4381 if (!NILP (prop) && display_prop_string_p (prop, string))
4382 found = 1;
4383 else
4384 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4387 if (!found)
4389 pos = make_number (around_charpos);
4390 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4391 while (!found && !EQ (pos, limit))
4393 prop = Fget_char_property (pos, Qdisplay, Qnil);
4394 if (!NILP (prop) && display_prop_string_p (prop, string))
4395 found = 1;
4396 else
4397 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4398 limit);
4402 return found ? XINT (pos) : 0;
4407 /***********************************************************************
4408 `composition' property
4409 ***********************************************************************/
4411 /* Set up iterator IT from `composition' property at its current
4412 position. Called from handle_stop. */
4414 static enum prop_handled
4415 handle_composition_prop (it)
4416 struct it *it;
4418 Lisp_Object prop, string;
4419 int pos, pos_byte, end;
4420 enum prop_handled handled = HANDLED_NORMALLY;
4422 if (STRINGP (it->string))
4424 pos = IT_STRING_CHARPOS (*it);
4425 pos_byte = IT_STRING_BYTEPOS (*it);
4426 string = it->string;
4428 else
4430 pos = IT_CHARPOS (*it);
4431 pos_byte = IT_BYTEPOS (*it);
4432 string = Qnil;
4435 /* If there's a valid composition and point is not inside of the
4436 composition (in the case that the composition is from the current
4437 buffer), draw a glyph composed from the composition components. */
4438 if (find_composition (pos, -1, &pos, &end, &prop, string)
4439 && COMPOSITION_VALID_P (pos, end, prop)
4440 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4442 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4444 if (id >= 0)
4446 it->method = GET_FROM_COMPOSITION;
4447 it->cmp_id = id;
4448 it->cmp_len = COMPOSITION_LENGTH (prop);
4449 /* For a terminal, draw only the first character of the
4450 components. */
4451 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4452 it->len = (STRINGP (it->string)
4453 ? string_char_to_byte (it->string, end)
4454 : CHAR_TO_BYTE (end)) - pos_byte;
4455 it->stop_charpos = end;
4456 handled = HANDLED_RETURN;
4460 return handled;
4465 /***********************************************************************
4466 Overlay strings
4467 ***********************************************************************/
4469 /* The following structure is used to record overlay strings for
4470 later sorting in load_overlay_strings. */
4472 struct overlay_entry
4474 Lisp_Object overlay;
4475 Lisp_Object string;
4476 int priority;
4477 int after_string_p;
4481 /* Set up iterator IT from overlay strings at its current position.
4482 Called from handle_stop. */
4484 static enum prop_handled
4485 handle_overlay_change (it)
4486 struct it *it;
4488 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4489 return HANDLED_RECOMPUTE_PROPS;
4490 else
4491 return HANDLED_NORMALLY;
4495 /* Set up the next overlay string for delivery by IT, if there is an
4496 overlay string to deliver. Called by set_iterator_to_next when the
4497 end of the current overlay string is reached. If there are more
4498 overlay strings to display, IT->string and
4499 IT->current.overlay_string_index are set appropriately here.
4500 Otherwise IT->string is set to nil. */
4502 static void
4503 next_overlay_string (it)
4504 struct it *it;
4506 ++it->current.overlay_string_index;
4507 if (it->current.overlay_string_index == it->n_overlay_strings)
4509 /* No more overlay strings. Restore IT's settings to what
4510 they were before overlay strings were processed, and
4511 continue to deliver from current_buffer. */
4512 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4514 pop_it (it);
4515 xassert (it->stop_charpos >= BEGV
4516 && it->stop_charpos <= it->end_charpos);
4517 it->string = Qnil;
4518 it->current.overlay_string_index = -1;
4519 SET_TEXT_POS (it->current.string_pos, -1, -1);
4520 it->n_overlay_strings = 0;
4521 it->method = GET_FROM_BUFFER;
4523 /* If we're at the end of the buffer, record that we have
4524 processed the overlay strings there already, so that
4525 next_element_from_buffer doesn't try it again. */
4526 if (IT_CHARPOS (*it) >= it->end_charpos)
4527 it->overlay_strings_at_end_processed_p = 1;
4529 /* If we have to display `...' for invisible text, set
4530 the iterator up for that. */
4531 if (display_ellipsis_p)
4532 setup_for_ellipsis (it, 0);
4534 else
4536 /* There are more overlay strings to process. If
4537 IT->current.overlay_string_index has advanced to a position
4538 where we must load IT->overlay_strings with more strings, do
4539 it. */
4540 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4542 if (it->current.overlay_string_index && i == 0)
4543 load_overlay_strings (it, 0);
4545 /* Initialize IT to deliver display elements from the overlay
4546 string. */
4547 it->string = it->overlay_strings[i];
4548 it->multibyte_p = STRING_MULTIBYTE (it->string);
4549 SET_TEXT_POS (it->current.string_pos, 0, 0);
4550 it->method = GET_FROM_STRING;
4551 it->stop_charpos = 0;
4554 CHECK_IT (it);
4558 /* Compare two overlay_entry structures E1 and E2. Used as a
4559 comparison function for qsort in load_overlay_strings. Overlay
4560 strings for the same position are sorted so that
4562 1. All after-strings come in front of before-strings, except
4563 when they come from the same overlay.
4565 2. Within after-strings, strings are sorted so that overlay strings
4566 from overlays with higher priorities come first.
4568 2. Within before-strings, strings are sorted so that overlay
4569 strings from overlays with higher priorities come last.
4571 Value is analogous to strcmp. */
4574 static int
4575 compare_overlay_entries (e1, e2)
4576 void *e1, *e2;
4578 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4579 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4580 int result;
4582 if (entry1->after_string_p != entry2->after_string_p)
4584 /* Let after-strings appear in front of before-strings if
4585 they come from different overlays. */
4586 if (EQ (entry1->overlay, entry2->overlay))
4587 result = entry1->after_string_p ? 1 : -1;
4588 else
4589 result = entry1->after_string_p ? -1 : 1;
4591 else if (entry1->after_string_p)
4592 /* After-strings sorted in order of decreasing priority. */
4593 result = entry2->priority - entry1->priority;
4594 else
4595 /* Before-strings sorted in order of increasing priority. */
4596 result = entry1->priority - entry2->priority;
4598 return result;
4602 /* Load the vector IT->overlay_strings with overlay strings from IT's
4603 current buffer position, or from CHARPOS if that is > 0. Set
4604 IT->n_overlays to the total number of overlay strings found.
4606 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4607 a time. On entry into load_overlay_strings,
4608 IT->current.overlay_string_index gives the number of overlay
4609 strings that have already been loaded by previous calls to this
4610 function.
4612 IT->add_overlay_start contains an additional overlay start
4613 position to consider for taking overlay strings from, if non-zero.
4614 This position comes into play when the overlay has an `invisible'
4615 property, and both before and after-strings. When we've skipped to
4616 the end of the overlay, because of its `invisible' property, we
4617 nevertheless want its before-string to appear.
4618 IT->add_overlay_start will contain the overlay start position
4619 in this case.
4621 Overlay strings are sorted so that after-string strings come in
4622 front of before-string strings. Within before and after-strings,
4623 strings are sorted by overlay priority. See also function
4624 compare_overlay_entries. */
4626 static void
4627 load_overlay_strings (it, charpos)
4628 struct it *it;
4629 int charpos;
4631 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4632 Lisp_Object overlay, window, str, invisible;
4633 struct Lisp_Overlay *ov;
4634 int start, end;
4635 int size = 20;
4636 int n = 0, i, j, invis_p;
4637 struct overlay_entry *entries
4638 = (struct overlay_entry *) alloca (size * sizeof *entries);
4640 if (charpos <= 0)
4641 charpos = IT_CHARPOS (*it);
4643 /* Append the overlay string STRING of overlay OVERLAY to vector
4644 `entries' which has size `size' and currently contains `n'
4645 elements. AFTER_P non-zero means STRING is an after-string of
4646 OVERLAY. */
4647 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4648 do \
4650 Lisp_Object priority; \
4652 if (n == size) \
4654 int new_size = 2 * size; \
4655 struct overlay_entry *old = entries; \
4656 entries = \
4657 (struct overlay_entry *) alloca (new_size \
4658 * sizeof *entries); \
4659 bcopy (old, entries, size * sizeof *entries); \
4660 size = new_size; \
4663 entries[n].string = (STRING); \
4664 entries[n].overlay = (OVERLAY); \
4665 priority = Foverlay_get ((OVERLAY), Qpriority); \
4666 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4667 entries[n].after_string_p = (AFTER_P); \
4668 ++n; \
4670 while (0)
4672 /* Process overlay before the overlay center. */
4673 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4675 XSETMISC (overlay, ov);
4676 xassert (OVERLAYP (overlay));
4677 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4678 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4680 if (end < charpos)
4681 break;
4683 /* Skip this overlay if it doesn't start or end at IT's current
4684 position. */
4685 if (end != charpos && start != charpos)
4686 continue;
4688 /* Skip this overlay if it doesn't apply to IT->w. */
4689 window = Foverlay_get (overlay, Qwindow);
4690 if (WINDOWP (window) && XWINDOW (window) != it->w)
4691 continue;
4693 /* If the text ``under'' the overlay is invisible, both before-
4694 and after-strings from this overlay are visible; start and
4695 end position are indistinguishable. */
4696 invisible = Foverlay_get (overlay, Qinvisible);
4697 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4699 /* If overlay has a non-empty before-string, record it. */
4700 if ((start == charpos || (end == charpos && invis_p))
4701 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4702 && SCHARS (str))
4703 RECORD_OVERLAY_STRING (overlay, str, 0);
4705 /* If overlay has a non-empty after-string, record it. */
4706 if ((end == charpos || (start == charpos && invis_p))
4707 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4708 && SCHARS (str))
4709 RECORD_OVERLAY_STRING (overlay, str, 1);
4712 /* Process overlays after the overlay center. */
4713 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4715 XSETMISC (overlay, ov);
4716 xassert (OVERLAYP (overlay));
4717 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4718 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4720 if (start > charpos)
4721 break;
4723 /* Skip this overlay if it doesn't start or end at IT's current
4724 position. */
4725 if (end != charpos && start != charpos)
4726 continue;
4728 /* Skip this overlay if it doesn't apply to IT->w. */
4729 window = Foverlay_get (overlay, Qwindow);
4730 if (WINDOWP (window) && XWINDOW (window) != it->w)
4731 continue;
4733 /* If the text ``under'' the overlay is invisible, it has a zero
4734 dimension, and both before- and after-strings apply. */
4735 invisible = Foverlay_get (overlay, Qinvisible);
4736 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4738 /* If overlay has a non-empty before-string, record it. */
4739 if ((start == charpos || (end == charpos && invis_p))
4740 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4741 && SCHARS (str))
4742 RECORD_OVERLAY_STRING (overlay, str, 0);
4744 /* If overlay has a non-empty after-string, record it. */
4745 if ((end == charpos || (start == charpos && invis_p))
4746 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4747 && SCHARS (str))
4748 RECORD_OVERLAY_STRING (overlay, str, 1);
4751 #undef RECORD_OVERLAY_STRING
4753 /* Sort entries. */
4754 if (n > 1)
4755 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4757 /* Record the total number of strings to process. */
4758 it->n_overlay_strings = n;
4760 /* IT->current.overlay_string_index is the number of overlay strings
4761 that have already been consumed by IT. Copy some of the
4762 remaining overlay strings to IT->overlay_strings. */
4763 i = 0;
4764 j = it->current.overlay_string_index;
4765 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4766 it->overlay_strings[i++] = entries[j++].string;
4768 CHECK_IT (it);
4772 /* Get the first chunk of overlay strings at IT's current buffer
4773 position, or at CHARPOS if that is > 0. Value is non-zero if at
4774 least one overlay string was found. */
4776 static int
4777 get_overlay_strings (it, charpos)
4778 struct it *it;
4779 int charpos;
4781 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4782 process. This fills IT->overlay_strings with strings, and sets
4783 IT->n_overlay_strings to the total number of strings to process.
4784 IT->pos.overlay_string_index has to be set temporarily to zero
4785 because load_overlay_strings needs this; it must be set to -1
4786 when no overlay strings are found because a zero value would
4787 indicate a position in the first overlay string. */
4788 it->current.overlay_string_index = 0;
4789 load_overlay_strings (it, charpos);
4791 /* If we found overlay strings, set up IT to deliver display
4792 elements from the first one. Otherwise set up IT to deliver
4793 from current_buffer. */
4794 if (it->n_overlay_strings)
4796 /* Make sure we know settings in current_buffer, so that we can
4797 restore meaningful values when we're done with the overlay
4798 strings. */
4799 compute_stop_pos (it);
4800 xassert (it->face_id >= 0);
4802 /* Save IT's settings. They are restored after all overlay
4803 strings have been processed. */
4804 xassert (it->sp == 0);
4805 push_it (it);
4807 /* Set up IT to deliver display elements from the first overlay
4808 string. */
4809 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4810 it->string = it->overlay_strings[0];
4811 it->stop_charpos = 0;
4812 xassert (STRINGP (it->string));
4813 it->end_charpos = SCHARS (it->string);
4814 it->multibyte_p = STRING_MULTIBYTE (it->string);
4815 it->method = GET_FROM_STRING;
4817 else
4819 it->string = Qnil;
4820 it->current.overlay_string_index = -1;
4821 it->method = GET_FROM_BUFFER;
4824 CHECK_IT (it);
4826 /* Value is non-zero if we found at least one overlay string. */
4827 return STRINGP (it->string);
4832 /***********************************************************************
4833 Saving and restoring state
4834 ***********************************************************************/
4836 /* Save current settings of IT on IT->stack. Called, for example,
4837 before setting up IT for an overlay string, to be able to restore
4838 IT's settings to what they were after the overlay string has been
4839 processed. */
4841 static void
4842 push_it (it)
4843 struct it *it;
4845 struct iterator_stack_entry *p;
4847 xassert (it->sp < 2);
4848 p = it->stack + it->sp;
4850 p->stop_charpos = it->stop_charpos;
4851 xassert (it->face_id >= 0);
4852 p->face_id = it->face_id;
4853 p->string = it->string;
4854 p->pos = it->current;
4855 p->end_charpos = it->end_charpos;
4856 p->string_nchars = it->string_nchars;
4857 p->area = it->area;
4858 p->multibyte_p = it->multibyte_p;
4859 p->slice = it->slice;
4860 p->space_width = it->space_width;
4861 p->font_height = it->font_height;
4862 p->voffset = it->voffset;
4863 p->string_from_display_prop_p = it->string_from_display_prop_p;
4864 p->display_ellipsis_p = 0;
4865 ++it->sp;
4869 /* Restore IT's settings from IT->stack. Called, for example, when no
4870 more overlay strings must be processed, and we return to delivering
4871 display elements from a buffer, or when the end of a string from a
4872 `display' property is reached and we return to delivering display
4873 elements from an overlay string, or from a buffer. */
4875 static void
4876 pop_it (it)
4877 struct it *it;
4879 struct iterator_stack_entry *p;
4881 xassert (it->sp > 0);
4882 --it->sp;
4883 p = it->stack + it->sp;
4884 it->stop_charpos = p->stop_charpos;
4885 it->face_id = p->face_id;
4886 it->string = p->string;
4887 it->current = p->pos;
4888 it->end_charpos = p->end_charpos;
4889 it->string_nchars = p->string_nchars;
4890 it->area = p->area;
4891 it->multibyte_p = p->multibyte_p;
4892 it->slice = p->slice;
4893 it->space_width = p->space_width;
4894 it->font_height = p->font_height;
4895 it->voffset = p->voffset;
4896 it->string_from_display_prop_p = p->string_from_display_prop_p;
4901 /***********************************************************************
4902 Moving over lines
4903 ***********************************************************************/
4905 /* Set IT's current position to the previous line start. */
4907 static void
4908 back_to_previous_line_start (it)
4909 struct it *it;
4911 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4912 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4916 /* Move IT to the next line start.
4918 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4919 we skipped over part of the text (as opposed to moving the iterator
4920 continuously over the text). Otherwise, don't change the value
4921 of *SKIPPED_P.
4923 Newlines may come from buffer text, overlay strings, or strings
4924 displayed via the `display' property. That's the reason we can't
4925 simply use find_next_newline_no_quit.
4927 Note that this function may not skip over invisible text that is so
4928 because of text properties and immediately follows a newline. If
4929 it would, function reseat_at_next_visible_line_start, when called
4930 from set_iterator_to_next, would effectively make invisible
4931 characters following a newline part of the wrong glyph row, which
4932 leads to wrong cursor motion. */
4934 static int
4935 forward_to_next_line_start (it, skipped_p)
4936 struct it *it;
4937 int *skipped_p;
4939 int old_selective, newline_found_p, n;
4940 const int MAX_NEWLINE_DISTANCE = 500;
4942 /* If already on a newline, just consume it to avoid unintended
4943 skipping over invisible text below. */
4944 if (it->what == IT_CHARACTER
4945 && it->c == '\n'
4946 && CHARPOS (it->position) == IT_CHARPOS (*it))
4948 set_iterator_to_next (it, 0);
4949 it->c = 0;
4950 return 1;
4953 /* Don't handle selective display in the following. It's (a)
4954 unnecessary because it's done by the caller, and (b) leads to an
4955 infinite recursion because next_element_from_ellipsis indirectly
4956 calls this function. */
4957 old_selective = it->selective;
4958 it->selective = 0;
4960 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4961 from buffer text. */
4962 for (n = newline_found_p = 0;
4963 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4964 n += STRINGP (it->string) ? 0 : 1)
4966 if (!get_next_display_element (it))
4967 return 0;
4968 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4969 set_iterator_to_next (it, 0);
4972 /* If we didn't find a newline near enough, see if we can use a
4973 short-cut. */
4974 if (!newline_found_p)
4976 int start = IT_CHARPOS (*it);
4977 int limit = find_next_newline_no_quit (start, 1);
4978 Lisp_Object pos;
4980 xassert (!STRINGP (it->string));
4982 /* If there isn't any `display' property in sight, and no
4983 overlays, we can just use the position of the newline in
4984 buffer text. */
4985 if (it->stop_charpos >= limit
4986 || ((pos = Fnext_single_property_change (make_number (start),
4987 Qdisplay,
4988 Qnil, make_number (limit)),
4989 NILP (pos))
4990 && next_overlay_change (start) == ZV))
4992 IT_CHARPOS (*it) = limit;
4993 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4994 *skipped_p = newline_found_p = 1;
4996 else
4998 while (get_next_display_element (it)
4999 && !newline_found_p)
5001 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5002 set_iterator_to_next (it, 0);
5007 it->selective = old_selective;
5008 return newline_found_p;
5012 /* Set IT's current position to the previous visible line start. Skip
5013 invisible text that is so either due to text properties or due to
5014 selective display. Caution: this does not change IT->current_x and
5015 IT->hpos. */
5017 static void
5018 back_to_previous_visible_line_start (it)
5019 struct it *it;
5021 while (IT_CHARPOS (*it) > BEGV)
5023 back_to_previous_line_start (it);
5024 if (IT_CHARPOS (*it) <= BEGV)
5025 break;
5027 /* If selective > 0, then lines indented more than that values
5028 are invisible. */
5029 if (it->selective > 0
5030 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5031 (double) it->selective)) /* iftc */
5032 continue;
5034 /* Check the newline before point for invisibility. */
5036 Lisp_Object prop;
5037 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5038 Qinvisible, it->window);
5039 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5040 continue;
5043 /* If newline has a display property that replaces the newline with something
5044 else (image or text), find start of overlay or interval and continue search
5045 from that point. */
5046 if (IT_CHARPOS (*it) > BEGV)
5048 struct it it2 = *it;
5049 int pos;
5050 int beg, end;
5051 Lisp_Object val, overlay;
5053 pos = --IT_CHARPOS (it2);
5054 --IT_BYTEPOS (it2);
5055 it2.sp = 0;
5056 if (handle_display_prop (&it2) == HANDLED_RETURN
5057 && !NILP (val = get_char_property_and_overlay
5058 (make_number (pos), Qdisplay, Qnil, &overlay))
5059 && (OVERLAYP (overlay)
5060 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5061 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5063 if (beg < BEGV)
5064 beg = BEGV;
5065 IT_CHARPOS (*it) = beg;
5066 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5067 continue;
5071 break;
5074 xassert (IT_CHARPOS (*it) >= BEGV);
5075 xassert (IT_CHARPOS (*it) == BEGV
5076 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5077 CHECK_IT (it);
5081 /* Reseat iterator IT at the previous visible line start. Skip
5082 invisible text that is so either due to text properties or due to
5083 selective display. At the end, update IT's overlay information,
5084 face information etc. */
5086 void
5087 reseat_at_previous_visible_line_start (it)
5088 struct it *it;
5090 back_to_previous_visible_line_start (it);
5091 reseat (it, it->current.pos, 1);
5092 CHECK_IT (it);
5096 /* Reseat iterator IT on the next visible line start in the current
5097 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5098 preceding the line start. Skip over invisible text that is so
5099 because of selective display. Compute faces, overlays etc at the
5100 new position. Note that this function does not skip over text that
5101 is invisible because of text properties. */
5103 static void
5104 reseat_at_next_visible_line_start (it, on_newline_p)
5105 struct it *it;
5106 int on_newline_p;
5108 int newline_found_p, skipped_p = 0;
5110 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5112 /* Skip over lines that are invisible because they are indented
5113 more than the value of IT->selective. */
5114 if (it->selective > 0)
5115 while (IT_CHARPOS (*it) < ZV
5116 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5117 (double) it->selective)) /* iftc */
5119 xassert (IT_BYTEPOS (*it) == BEGV
5120 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5121 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5124 /* Position on the newline if that's what's requested. */
5125 if (on_newline_p && newline_found_p)
5127 if (STRINGP (it->string))
5129 if (IT_STRING_CHARPOS (*it) > 0)
5131 --IT_STRING_CHARPOS (*it);
5132 --IT_STRING_BYTEPOS (*it);
5135 else if (IT_CHARPOS (*it) > BEGV)
5137 --IT_CHARPOS (*it);
5138 --IT_BYTEPOS (*it);
5139 reseat (it, it->current.pos, 0);
5142 else if (skipped_p)
5143 reseat (it, it->current.pos, 0);
5145 CHECK_IT (it);
5150 /***********************************************************************
5151 Changing an iterator's position
5152 ***********************************************************************/
5154 /* Change IT's current position to POS in current_buffer. If FORCE_P
5155 is non-zero, always check for text properties at the new position.
5156 Otherwise, text properties are only looked up if POS >=
5157 IT->check_charpos of a property. */
5159 static void
5160 reseat (it, pos, force_p)
5161 struct it *it;
5162 struct text_pos pos;
5163 int force_p;
5165 int original_pos = IT_CHARPOS (*it);
5167 reseat_1 (it, pos, 0);
5169 /* Determine where to check text properties. Avoid doing it
5170 where possible because text property lookup is very expensive. */
5171 if (force_p
5172 || CHARPOS (pos) > it->stop_charpos
5173 || CHARPOS (pos) < original_pos)
5174 handle_stop (it);
5176 CHECK_IT (it);
5180 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5181 IT->stop_pos to POS, also. */
5183 static void
5184 reseat_1 (it, pos, set_stop_p)
5185 struct it *it;
5186 struct text_pos pos;
5187 int set_stop_p;
5189 /* Don't call this function when scanning a C string. */
5190 xassert (it->s == NULL);
5192 /* POS must be a reasonable value. */
5193 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5195 it->current.pos = it->position = pos;
5196 XSETBUFFER (it->object, current_buffer);
5197 it->end_charpos = ZV;
5198 it->dpvec = NULL;
5199 it->current.dpvec_index = -1;
5200 it->current.overlay_string_index = -1;
5201 IT_STRING_CHARPOS (*it) = -1;
5202 IT_STRING_BYTEPOS (*it) = -1;
5203 it->string = Qnil;
5204 it->method = GET_FROM_BUFFER;
5205 /* RMS: I added this to fix a bug in move_it_vertically_backward
5206 where it->area continued to relate to the starting point
5207 for the backward motion. Bug report from
5208 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5209 However, I am not sure whether reseat still does the right thing
5210 in general after this change. */
5211 it->area = TEXT_AREA;
5212 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5213 it->sp = 0;
5214 it->face_before_selective_p = 0;
5216 if (set_stop_p)
5217 it->stop_charpos = CHARPOS (pos);
5221 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5222 If S is non-null, it is a C string to iterate over. Otherwise,
5223 STRING gives a Lisp string to iterate over.
5225 If PRECISION > 0, don't return more then PRECISION number of
5226 characters from the string.
5228 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5229 characters have been returned. FIELD_WIDTH < 0 means an infinite
5230 field width.
5232 MULTIBYTE = 0 means disable processing of multibyte characters,
5233 MULTIBYTE > 0 means enable it,
5234 MULTIBYTE < 0 means use IT->multibyte_p.
5236 IT must be initialized via a prior call to init_iterator before
5237 calling this function. */
5239 static void
5240 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5241 struct it *it;
5242 unsigned char *s;
5243 Lisp_Object string;
5244 int charpos;
5245 int precision, field_width, multibyte;
5247 /* No region in strings. */
5248 it->region_beg_charpos = it->region_end_charpos = -1;
5250 /* No text property checks performed by default, but see below. */
5251 it->stop_charpos = -1;
5253 /* Set iterator position and end position. */
5254 bzero (&it->current, sizeof it->current);
5255 it->current.overlay_string_index = -1;
5256 it->current.dpvec_index = -1;
5257 xassert (charpos >= 0);
5259 /* If STRING is specified, use its multibyteness, otherwise use the
5260 setting of MULTIBYTE, if specified. */
5261 if (multibyte >= 0)
5262 it->multibyte_p = multibyte > 0;
5264 if (s == NULL)
5266 xassert (STRINGP (string));
5267 it->string = string;
5268 it->s = NULL;
5269 it->end_charpos = it->string_nchars = SCHARS (string);
5270 it->method = GET_FROM_STRING;
5271 it->current.string_pos = string_pos (charpos, string);
5273 else
5275 it->s = s;
5276 it->string = Qnil;
5278 /* Note that we use IT->current.pos, not it->current.string_pos,
5279 for displaying C strings. */
5280 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5281 if (it->multibyte_p)
5283 it->current.pos = c_string_pos (charpos, s, 1);
5284 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5286 else
5288 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5289 it->end_charpos = it->string_nchars = strlen (s);
5292 it->method = GET_FROM_C_STRING;
5295 /* PRECISION > 0 means don't return more than PRECISION characters
5296 from the string. */
5297 if (precision > 0 && it->end_charpos - charpos > precision)
5298 it->end_charpos = it->string_nchars = charpos + precision;
5300 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5301 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5302 FIELD_WIDTH < 0 means infinite field width. This is useful for
5303 padding with `-' at the end of a mode line. */
5304 if (field_width < 0)
5305 field_width = INFINITY;
5306 if (field_width > it->end_charpos - charpos)
5307 it->end_charpos = charpos + field_width;
5309 /* Use the standard display table for displaying strings. */
5310 if (DISP_TABLE_P (Vstandard_display_table))
5311 it->dp = XCHAR_TABLE (Vstandard_display_table);
5313 it->stop_charpos = charpos;
5314 CHECK_IT (it);
5319 /***********************************************************************
5320 Iteration
5321 ***********************************************************************/
5323 /* Map enum it_method value to corresponding next_element_from_* function. */
5325 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5327 next_element_from_buffer,
5328 next_element_from_display_vector,
5329 next_element_from_composition,
5330 next_element_from_string,
5331 next_element_from_c_string,
5332 next_element_from_image,
5333 next_element_from_stretch
5337 /* Load IT's display element fields with information about the next
5338 display element from the current position of IT. Value is zero if
5339 end of buffer (or C string) is reached. */
5341 static struct frame *last_escape_glyph_frame = NULL;
5342 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5343 static int last_escape_glyph_merged_face_id = 0;
5346 get_next_display_element (it)
5347 struct it *it;
5349 /* Non-zero means that we found a display element. Zero means that
5350 we hit the end of what we iterate over. Performance note: the
5351 function pointer `method' used here turns out to be faster than
5352 using a sequence of if-statements. */
5353 int success_p;
5355 get_next:
5356 success_p = (*get_next_element[it->method]) (it);
5358 if (it->what == IT_CHARACTER)
5360 /* Map via display table or translate control characters.
5361 IT->c, IT->len etc. have been set to the next character by
5362 the function call above. If we have a display table, and it
5363 contains an entry for IT->c, translate it. Don't do this if
5364 IT->c itself comes from a display table, otherwise we could
5365 end up in an infinite recursion. (An alternative could be to
5366 count the recursion depth of this function and signal an
5367 error when a certain maximum depth is reached.) Is it worth
5368 it? */
5369 if (success_p && it->dpvec == NULL)
5371 Lisp_Object dv;
5373 if (it->dp
5374 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5375 VECTORP (dv)))
5377 struct Lisp_Vector *v = XVECTOR (dv);
5379 /* Return the first character from the display table
5380 entry, if not empty. If empty, don't display the
5381 current character. */
5382 if (v->size)
5384 it->dpvec_char_len = it->len;
5385 it->dpvec = v->contents;
5386 it->dpend = v->contents + v->size;
5387 it->current.dpvec_index = 0;
5388 it->dpvec_face_id = -1;
5389 it->saved_face_id = it->face_id;
5390 it->method = GET_FROM_DISPLAY_VECTOR;
5391 it->ellipsis_p = 0;
5393 else
5395 set_iterator_to_next (it, 0);
5397 goto get_next;
5400 /* Translate control characters into `\003' or `^C' form.
5401 Control characters coming from a display table entry are
5402 currently not translated because we use IT->dpvec to hold
5403 the translation. This could easily be changed but I
5404 don't believe that it is worth doing.
5406 If it->multibyte_p is nonzero, eight-bit characters and
5407 non-printable multibyte characters are also translated to
5408 octal form.
5410 If it->multibyte_p is zero, eight-bit characters that
5411 don't have corresponding multibyte char code are also
5412 translated to octal form. */
5413 else if ((it->c < ' '
5414 && (it->area != TEXT_AREA
5415 /* In mode line, treat \n like other crl chars. */
5416 || (it->c != '\t'
5417 && it->glyph_row && it->glyph_row->mode_line_p)
5418 || (it->c != '\n' && it->c != '\t')))
5419 || (it->multibyte_p
5420 ? ((it->c >= 127
5421 && it->len == 1)
5422 || !CHAR_PRINTABLE_P (it->c)
5423 || (!NILP (Vnobreak_char_display)
5424 && (it->c == 0x8a0 || it->c == 0x8ad
5425 || it->c == 0x920 || it->c == 0x92d
5426 || it->c == 0xe20 || it->c == 0xe2d
5427 || it->c == 0xf20 || it->c == 0xf2d)))
5428 : (it->c >= 127
5429 && (!unibyte_display_via_language_environment
5430 || it->c == unibyte_char_to_multibyte (it->c)))))
5432 /* IT->c is a control character which must be displayed
5433 either as '\003' or as `^C' where the '\\' and '^'
5434 can be defined in the display table. Fill
5435 IT->ctl_chars with glyphs for what we have to
5436 display. Then, set IT->dpvec to these glyphs. */
5437 GLYPH g;
5438 int ctl_len;
5439 int face_id, lface_id = 0 ;
5440 GLYPH escape_glyph;
5442 /* Handle control characters with ^. */
5444 if (it->c < 128 && it->ctl_arrow_p)
5446 g = '^'; /* default glyph for Control */
5447 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5448 if (it->dp
5449 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5450 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5452 g = XINT (DISP_CTRL_GLYPH (it->dp));
5453 lface_id = FAST_GLYPH_FACE (g);
5455 if (lface_id)
5457 g = FAST_GLYPH_CHAR (g);
5458 face_id = merge_faces (it->f, Qt, lface_id,
5459 it->face_id);
5461 else if (it->f == last_escape_glyph_frame
5462 && it->face_id == last_escape_glyph_face_id)
5464 face_id = last_escape_glyph_merged_face_id;
5466 else
5468 /* Merge the escape-glyph face into the current face. */
5469 face_id = merge_faces (it->f, Qescape_glyph, 0,
5470 it->face_id);
5471 last_escape_glyph_frame = it->f;
5472 last_escape_glyph_face_id = it->face_id;
5473 last_escape_glyph_merged_face_id = face_id;
5476 XSETINT (it->ctl_chars[0], g);
5477 g = it->c ^ 0100;
5478 XSETINT (it->ctl_chars[1], g);
5479 ctl_len = 2;
5480 goto display_control;
5483 /* Handle non-break space in the mode where it only gets
5484 highlighting. */
5486 if (EQ (Vnobreak_char_display, Qt)
5487 && (it->c == 0x8a0 || it->c == 0x920
5488 || it->c == 0xe20 || it->c == 0xf20))
5490 /* Merge the no-break-space face into the current face. */
5491 face_id = merge_faces (it->f, Qnobreak_space, 0,
5492 it->face_id);
5494 g = it->c = ' ';
5495 XSETINT (it->ctl_chars[0], g);
5496 ctl_len = 1;
5497 goto display_control;
5500 /* Handle sequences that start with the "escape glyph". */
5502 /* the default escape glyph is \. */
5503 escape_glyph = '\\';
5505 if (it->dp
5506 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5507 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5509 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5510 lface_id = FAST_GLYPH_FACE (escape_glyph);
5512 if (lface_id)
5514 /* The display table specified a face.
5515 Merge it into face_id and also into escape_glyph. */
5516 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5517 face_id = merge_faces (it->f, Qt, lface_id,
5518 it->face_id);
5520 else if (it->f == last_escape_glyph_frame
5521 && it->face_id == last_escape_glyph_face_id)
5523 face_id = last_escape_glyph_merged_face_id;
5525 else
5527 /* Merge the escape-glyph face into the current face. */
5528 face_id = merge_faces (it->f, Qescape_glyph, 0,
5529 it->face_id);
5530 last_escape_glyph_frame = it->f;
5531 last_escape_glyph_face_id = it->face_id;
5532 last_escape_glyph_merged_face_id = face_id;
5535 /* Handle soft hyphens in the mode where they only get
5536 highlighting. */
5538 if (EQ (Vnobreak_char_display, Qt)
5539 && (it->c == 0x8ad || it->c == 0x92d
5540 || it->c == 0xe2d || it->c == 0xf2d))
5542 g = it->c = '-';
5543 XSETINT (it->ctl_chars[0], g);
5544 ctl_len = 1;
5545 goto display_control;
5548 /* Handle non-break space and soft hyphen
5549 with the escape glyph. */
5551 if (it->c == 0x8a0 || it->c == 0x8ad
5552 || it->c == 0x920 || it->c == 0x92d
5553 || it->c == 0xe20 || it->c == 0xe2d
5554 || it->c == 0xf20 || it->c == 0xf2d)
5556 XSETINT (it->ctl_chars[0], escape_glyph);
5557 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5558 XSETINT (it->ctl_chars[1], g);
5559 ctl_len = 2;
5560 goto display_control;
5564 unsigned char str[MAX_MULTIBYTE_LENGTH];
5565 int len;
5566 int i;
5568 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5569 if (SINGLE_BYTE_CHAR_P (it->c))
5570 str[0] = it->c, len = 1;
5571 else
5573 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5574 if (len < 0)
5576 /* It's an invalid character, which shouldn't
5577 happen actually, but due to bugs it may
5578 happen. Let's print the char as is, there's
5579 not much meaningful we can do with it. */
5580 str[0] = it->c;
5581 str[1] = it->c >> 8;
5582 str[2] = it->c >> 16;
5583 str[3] = it->c >> 24;
5584 len = 4;
5588 for (i = 0; i < len; i++)
5590 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5591 /* Insert three more glyphs into IT->ctl_chars for
5592 the octal display of the character. */
5593 g = ((str[i] >> 6) & 7) + '0';
5594 XSETINT (it->ctl_chars[i * 4 + 1], g);
5595 g = ((str[i] >> 3) & 7) + '0';
5596 XSETINT (it->ctl_chars[i * 4 + 2], g);
5597 g = (str[i] & 7) + '0';
5598 XSETINT (it->ctl_chars[i * 4 + 3], g);
5600 ctl_len = len * 4;
5603 display_control:
5604 /* Set up IT->dpvec and return first character from it. */
5605 it->dpvec_char_len = it->len;
5606 it->dpvec = it->ctl_chars;
5607 it->dpend = it->dpvec + ctl_len;
5608 it->current.dpvec_index = 0;
5609 it->dpvec_face_id = face_id;
5610 it->saved_face_id = it->face_id;
5611 it->method = GET_FROM_DISPLAY_VECTOR;
5612 it->ellipsis_p = 0;
5613 goto get_next;
5617 /* Adjust face id for a multibyte character. There are no
5618 multibyte character in unibyte text. */
5619 if (it->multibyte_p
5620 && success_p
5621 && FRAME_WINDOW_P (it->f))
5623 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5624 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5628 /* Is this character the last one of a run of characters with
5629 box? If yes, set IT->end_of_box_run_p to 1. */
5630 if (it->face_box_p
5631 && it->s == NULL)
5633 int face_id;
5634 struct face *face;
5636 it->end_of_box_run_p
5637 = ((face_id = face_after_it_pos (it),
5638 face_id != it->face_id)
5639 && (face = FACE_FROM_ID (it->f, face_id),
5640 face->box == FACE_NO_BOX));
5643 /* Value is 0 if end of buffer or string reached. */
5644 return success_p;
5648 /* Move IT to the next display element.
5650 RESEAT_P non-zero means if called on a newline in buffer text,
5651 skip to the next visible line start.
5653 Functions get_next_display_element and set_iterator_to_next are
5654 separate because I find this arrangement easier to handle than a
5655 get_next_display_element function that also increments IT's
5656 position. The way it is we can first look at an iterator's current
5657 display element, decide whether it fits on a line, and if it does,
5658 increment the iterator position. The other way around we probably
5659 would either need a flag indicating whether the iterator has to be
5660 incremented the next time, or we would have to implement a
5661 decrement position function which would not be easy to write. */
5663 void
5664 set_iterator_to_next (it, reseat_p)
5665 struct it *it;
5666 int reseat_p;
5668 /* Reset flags indicating start and end of a sequence of characters
5669 with box. Reset them at the start of this function because
5670 moving the iterator to a new position might set them. */
5671 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5673 switch (it->method)
5675 case GET_FROM_BUFFER:
5676 /* The current display element of IT is a character from
5677 current_buffer. Advance in the buffer, and maybe skip over
5678 invisible lines that are so because of selective display. */
5679 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5680 reseat_at_next_visible_line_start (it, 0);
5681 else
5683 xassert (it->len != 0);
5684 IT_BYTEPOS (*it) += it->len;
5685 IT_CHARPOS (*it) += 1;
5686 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5688 break;
5690 case GET_FROM_COMPOSITION:
5691 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5692 if (STRINGP (it->string))
5694 IT_STRING_BYTEPOS (*it) += it->len;
5695 IT_STRING_CHARPOS (*it) += it->cmp_len;
5696 it->method = GET_FROM_STRING;
5697 goto consider_string_end;
5699 else
5701 IT_BYTEPOS (*it) += it->len;
5702 IT_CHARPOS (*it) += it->cmp_len;
5703 it->method = GET_FROM_BUFFER;
5705 break;
5707 case GET_FROM_C_STRING:
5708 /* Current display element of IT is from a C string. */
5709 IT_BYTEPOS (*it) += it->len;
5710 IT_CHARPOS (*it) += 1;
5711 break;
5713 case GET_FROM_DISPLAY_VECTOR:
5714 /* Current display element of IT is from a display table entry.
5715 Advance in the display table definition. Reset it to null if
5716 end reached, and continue with characters from buffers/
5717 strings. */
5718 ++it->current.dpvec_index;
5720 /* Restore face of the iterator to what they were before the
5721 display vector entry (these entries may contain faces). */
5722 it->face_id = it->saved_face_id;
5724 if (it->dpvec + it->current.dpvec_index == it->dpend)
5726 int recheck_faces = it->ellipsis_p;
5728 if (it->s)
5729 it->method = GET_FROM_C_STRING;
5730 else if (STRINGP (it->string))
5731 it->method = GET_FROM_STRING;
5732 else
5733 it->method = GET_FROM_BUFFER;
5735 it->dpvec = NULL;
5736 it->current.dpvec_index = -1;
5738 /* Skip over characters which were displayed via IT->dpvec. */
5739 if (it->dpvec_char_len < 0)
5740 reseat_at_next_visible_line_start (it, 1);
5741 else if (it->dpvec_char_len > 0)
5743 if (it->method == GET_FROM_STRING
5744 && it->n_overlay_strings > 0)
5745 it->ignore_overlay_strings_at_pos_p = 1;
5746 it->len = it->dpvec_char_len;
5747 set_iterator_to_next (it, reseat_p);
5750 /* Maybe recheck faces after display vector */
5751 if (recheck_faces)
5752 it->stop_charpos = IT_CHARPOS (*it);
5754 break;
5756 case GET_FROM_STRING:
5757 /* Current display element is a character from a Lisp string. */
5758 xassert (it->s == NULL && STRINGP (it->string));
5759 IT_STRING_BYTEPOS (*it) += it->len;
5760 IT_STRING_CHARPOS (*it) += 1;
5762 consider_string_end:
5764 if (it->current.overlay_string_index >= 0)
5766 /* IT->string is an overlay string. Advance to the
5767 next, if there is one. */
5768 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5769 next_overlay_string (it);
5771 else
5773 /* IT->string is not an overlay string. If we reached
5774 its end, and there is something on IT->stack, proceed
5775 with what is on the stack. This can be either another
5776 string, this time an overlay string, or a buffer. */
5777 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5778 && it->sp > 0)
5780 pop_it (it);
5781 if (STRINGP (it->string))
5782 goto consider_string_end;
5783 it->method = GET_FROM_BUFFER;
5786 break;
5788 case GET_FROM_IMAGE:
5789 case GET_FROM_STRETCH:
5790 /* The position etc with which we have to proceed are on
5791 the stack. The position may be at the end of a string,
5792 if the `display' property takes up the whole string. */
5793 xassert (it->sp > 0);
5794 pop_it (it);
5795 it->image_id = 0;
5796 if (STRINGP (it->string))
5798 it->method = GET_FROM_STRING;
5799 goto consider_string_end;
5801 it->method = GET_FROM_BUFFER;
5802 break;
5804 default:
5805 /* There are no other methods defined, so this should be a bug. */
5806 abort ();
5809 xassert (it->method != GET_FROM_STRING
5810 || (STRINGP (it->string)
5811 && IT_STRING_CHARPOS (*it) >= 0));
5814 /* Load IT's display element fields with information about the next
5815 display element which comes from a display table entry or from the
5816 result of translating a control character to one of the forms `^C'
5817 or `\003'.
5819 IT->dpvec holds the glyphs to return as characters.
5820 IT->saved_face_id holds the face id before the display vector--
5821 it is restored into IT->face_idin set_iterator_to_next. */
5823 static int
5824 next_element_from_display_vector (it)
5825 struct it *it;
5827 /* Precondition. */
5828 xassert (it->dpvec && it->current.dpvec_index >= 0);
5830 it->face_id = it->saved_face_id;
5832 if (INTEGERP (*it->dpvec)
5833 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5835 GLYPH g;
5837 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5838 it->c = FAST_GLYPH_CHAR (g);
5839 it->len = CHAR_BYTES (it->c);
5841 /* The entry may contain a face id to use. Such a face id is
5842 the id of a Lisp face, not a realized face. A face id of
5843 zero means no face is specified. */
5844 if (it->dpvec_face_id >= 0)
5845 it->face_id = it->dpvec_face_id;
5846 else
5848 int lface_id = FAST_GLYPH_FACE (g);
5849 if (lface_id > 0)
5850 it->face_id = merge_faces (it->f, Qt, lface_id,
5851 it->saved_face_id);
5854 else
5855 /* Display table entry is invalid. Return a space. */
5856 it->c = ' ', it->len = 1;
5858 /* Don't change position and object of the iterator here. They are
5859 still the values of the character that had this display table
5860 entry or was translated, and that's what we want. */
5861 it->what = IT_CHARACTER;
5862 return 1;
5866 /* Load IT with the next display element from Lisp string IT->string.
5867 IT->current.string_pos is the current position within the string.
5868 If IT->current.overlay_string_index >= 0, the Lisp string is an
5869 overlay string. */
5871 static int
5872 next_element_from_string (it)
5873 struct it *it;
5875 struct text_pos position;
5877 xassert (STRINGP (it->string));
5878 xassert (IT_STRING_CHARPOS (*it) >= 0);
5879 position = it->current.string_pos;
5881 /* Time to check for invisible text? */
5882 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5883 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5885 handle_stop (it);
5887 /* Since a handler may have changed IT->method, we must
5888 recurse here. */
5889 return get_next_display_element (it);
5892 if (it->current.overlay_string_index >= 0)
5894 /* Get the next character from an overlay string. In overlay
5895 strings, There is no field width or padding with spaces to
5896 do. */
5897 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5899 it->what = IT_EOB;
5900 return 0;
5902 else if (STRING_MULTIBYTE (it->string))
5904 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5905 const unsigned char *s = (SDATA (it->string)
5906 + IT_STRING_BYTEPOS (*it));
5907 it->c = string_char_and_length (s, remaining, &it->len);
5909 else
5911 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5912 it->len = 1;
5915 else
5917 /* Get the next character from a Lisp string that is not an
5918 overlay string. Such strings come from the mode line, for
5919 example. We may have to pad with spaces, or truncate the
5920 string. See also next_element_from_c_string. */
5921 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5923 it->what = IT_EOB;
5924 return 0;
5926 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5928 /* Pad with spaces. */
5929 it->c = ' ', it->len = 1;
5930 CHARPOS (position) = BYTEPOS (position) = -1;
5932 else if (STRING_MULTIBYTE (it->string))
5934 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5935 const unsigned char *s = (SDATA (it->string)
5936 + IT_STRING_BYTEPOS (*it));
5937 it->c = string_char_and_length (s, maxlen, &it->len);
5939 else
5941 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5942 it->len = 1;
5946 /* Record what we have and where it came from. Note that we store a
5947 buffer position in IT->position although it could arguably be a
5948 string position. */
5949 it->what = IT_CHARACTER;
5950 it->object = it->string;
5951 it->position = position;
5952 return 1;
5956 /* Load IT with next display element from C string IT->s.
5957 IT->string_nchars is the maximum number of characters to return
5958 from the string. IT->end_charpos may be greater than
5959 IT->string_nchars when this function is called, in which case we
5960 may have to return padding spaces. Value is zero if end of string
5961 reached, including padding spaces. */
5963 static int
5964 next_element_from_c_string (it)
5965 struct it *it;
5967 int success_p = 1;
5969 xassert (it->s);
5970 it->what = IT_CHARACTER;
5971 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5972 it->object = Qnil;
5974 /* IT's position can be greater IT->string_nchars in case a field
5975 width or precision has been specified when the iterator was
5976 initialized. */
5977 if (IT_CHARPOS (*it) >= it->end_charpos)
5979 /* End of the game. */
5980 it->what = IT_EOB;
5981 success_p = 0;
5983 else if (IT_CHARPOS (*it) >= it->string_nchars)
5985 /* Pad with spaces. */
5986 it->c = ' ', it->len = 1;
5987 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5989 else if (it->multibyte_p)
5991 /* Implementation note: The calls to strlen apparently aren't a
5992 performance problem because there is no noticeable performance
5993 difference between Emacs running in unibyte or multibyte mode. */
5994 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5995 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5996 maxlen, &it->len);
5998 else
5999 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6001 return success_p;
6005 /* Set up IT to return characters from an ellipsis, if appropriate.
6006 The definition of the ellipsis glyphs may come from a display table
6007 entry. This function Fills IT with the first glyph from the
6008 ellipsis if an ellipsis is to be displayed. */
6010 static int
6011 next_element_from_ellipsis (it)
6012 struct it *it;
6014 if (it->selective_display_ellipsis_p)
6015 setup_for_ellipsis (it, it->len);
6016 else
6018 /* The face at the current position may be different from the
6019 face we find after the invisible text. Remember what it
6020 was in IT->saved_face_id, and signal that it's there by
6021 setting face_before_selective_p. */
6022 it->saved_face_id = it->face_id;
6023 it->method = GET_FROM_BUFFER;
6024 reseat_at_next_visible_line_start (it, 1);
6025 it->face_before_selective_p = 1;
6028 return get_next_display_element (it);
6032 /* Deliver an image display element. The iterator IT is already
6033 filled with image information (done in handle_display_prop). Value
6034 is always 1. */
6037 static int
6038 next_element_from_image (it)
6039 struct it *it;
6041 it->what = IT_IMAGE;
6042 return 1;
6046 /* Fill iterator IT with next display element from a stretch glyph
6047 property. IT->object is the value of the text property. Value is
6048 always 1. */
6050 static int
6051 next_element_from_stretch (it)
6052 struct it *it;
6054 it->what = IT_STRETCH;
6055 return 1;
6059 /* Load IT with the next display element from current_buffer. Value
6060 is zero if end of buffer reached. IT->stop_charpos is the next
6061 position at which to stop and check for text properties or buffer
6062 end. */
6064 static int
6065 next_element_from_buffer (it)
6066 struct it *it;
6068 int success_p = 1;
6070 /* Check this assumption, otherwise, we would never enter the
6071 if-statement, below. */
6072 xassert (IT_CHARPOS (*it) >= BEGV
6073 && IT_CHARPOS (*it) <= it->stop_charpos);
6075 if (IT_CHARPOS (*it) >= it->stop_charpos)
6077 if (IT_CHARPOS (*it) >= it->end_charpos)
6079 int overlay_strings_follow_p;
6081 /* End of the game, except when overlay strings follow that
6082 haven't been returned yet. */
6083 if (it->overlay_strings_at_end_processed_p)
6084 overlay_strings_follow_p = 0;
6085 else
6087 it->overlay_strings_at_end_processed_p = 1;
6088 overlay_strings_follow_p = get_overlay_strings (it, 0);
6091 if (overlay_strings_follow_p)
6092 success_p = get_next_display_element (it);
6093 else
6095 it->what = IT_EOB;
6096 it->position = it->current.pos;
6097 success_p = 0;
6100 else
6102 handle_stop (it);
6103 return get_next_display_element (it);
6106 else
6108 /* No face changes, overlays etc. in sight, so just return a
6109 character from current_buffer. */
6110 unsigned char *p;
6112 /* Maybe run the redisplay end trigger hook. Performance note:
6113 This doesn't seem to cost measurable time. */
6114 if (it->redisplay_end_trigger_charpos
6115 && it->glyph_row
6116 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6117 run_redisplay_end_trigger_hook (it);
6119 /* Get the next character, maybe multibyte. */
6120 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6121 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6123 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6124 - IT_BYTEPOS (*it));
6125 it->c = string_char_and_length (p, maxlen, &it->len);
6127 else
6128 it->c = *p, it->len = 1;
6130 /* Record what we have and where it came from. */
6131 it->what = IT_CHARACTER;;
6132 it->object = it->w->buffer;
6133 it->position = it->current.pos;
6135 /* Normally we return the character found above, except when we
6136 really want to return an ellipsis for selective display. */
6137 if (it->selective)
6139 if (it->c == '\n')
6141 /* A value of selective > 0 means hide lines indented more
6142 than that number of columns. */
6143 if (it->selective > 0
6144 && IT_CHARPOS (*it) + 1 < ZV
6145 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6146 IT_BYTEPOS (*it) + 1,
6147 (double) it->selective)) /* iftc */
6149 success_p = next_element_from_ellipsis (it);
6150 it->dpvec_char_len = -1;
6153 else if (it->c == '\r' && it->selective == -1)
6155 /* A value of selective == -1 means that everything from the
6156 CR to the end of the line is invisible, with maybe an
6157 ellipsis displayed for it. */
6158 success_p = next_element_from_ellipsis (it);
6159 it->dpvec_char_len = -1;
6164 /* Value is zero if end of buffer reached. */
6165 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6166 return success_p;
6170 /* Run the redisplay end trigger hook for IT. */
6172 static void
6173 run_redisplay_end_trigger_hook (it)
6174 struct it *it;
6176 Lisp_Object args[3];
6178 /* IT->glyph_row should be non-null, i.e. we should be actually
6179 displaying something, or otherwise we should not run the hook. */
6180 xassert (it->glyph_row);
6182 /* Set up hook arguments. */
6183 args[0] = Qredisplay_end_trigger_functions;
6184 args[1] = it->window;
6185 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6186 it->redisplay_end_trigger_charpos = 0;
6188 /* Since we are *trying* to run these functions, don't try to run
6189 them again, even if they get an error. */
6190 it->w->redisplay_end_trigger = Qnil;
6191 Frun_hook_with_args (3, args);
6193 /* Notice if it changed the face of the character we are on. */
6194 handle_face_prop (it);
6198 /* Deliver a composition display element. The iterator IT is already
6199 filled with composition information (done in
6200 handle_composition_prop). Value is always 1. */
6202 static int
6203 next_element_from_composition (it)
6204 struct it *it;
6206 it->what = IT_COMPOSITION;
6207 it->position = (STRINGP (it->string)
6208 ? it->current.string_pos
6209 : it->current.pos);
6210 return 1;
6215 /***********************************************************************
6216 Moving an iterator without producing glyphs
6217 ***********************************************************************/
6219 /* Check if iterator is at a position corresponding to a valid buffer
6220 position after some move_it_ call. */
6222 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6223 ((it)->method == GET_FROM_STRING \
6224 ? IT_STRING_CHARPOS (*it) == 0 \
6225 : 1)
6228 /* Move iterator IT to a specified buffer or X position within one
6229 line on the display without producing glyphs.
6231 OP should be a bit mask including some or all of these bits:
6232 MOVE_TO_X: Stop on reaching x-position TO_X.
6233 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6234 Regardless of OP's value, stop in reaching the end of the display line.
6236 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6237 This means, in particular, that TO_X includes window's horizontal
6238 scroll amount.
6240 The return value has several possible values that
6241 say what condition caused the scan to stop:
6243 MOVE_POS_MATCH_OR_ZV
6244 - when TO_POS or ZV was reached.
6246 MOVE_X_REACHED
6247 -when TO_X was reached before TO_POS or ZV were reached.
6249 MOVE_LINE_CONTINUED
6250 - when we reached the end of the display area and the line must
6251 be continued.
6253 MOVE_LINE_TRUNCATED
6254 - when we reached the end of the display area and the line is
6255 truncated.
6257 MOVE_NEWLINE_OR_CR
6258 - when we stopped at a line end, i.e. a newline or a CR and selective
6259 display is on. */
6261 static enum move_it_result
6262 move_it_in_display_line_to (it, to_charpos, to_x, op)
6263 struct it *it;
6264 int to_charpos, to_x, op;
6266 enum move_it_result result = MOVE_UNDEFINED;
6267 struct glyph_row *saved_glyph_row;
6269 /* Don't produce glyphs in produce_glyphs. */
6270 saved_glyph_row = it->glyph_row;
6271 it->glyph_row = NULL;
6273 #define BUFFER_POS_REACHED_P() \
6274 ((op & MOVE_TO_POS) != 0 \
6275 && BUFFERP (it->object) \
6276 && IT_CHARPOS (*it) >= to_charpos \
6277 && (it->method == GET_FROM_BUFFER \
6278 || (it->method == GET_FROM_DISPLAY_VECTOR \
6279 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6282 while (1)
6284 int x, i, ascent = 0, descent = 0;
6286 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6287 if ((op & MOVE_TO_POS) != 0
6288 && BUFFERP (it->object)
6289 && it->method == GET_FROM_BUFFER
6290 && IT_CHARPOS (*it) > to_charpos)
6292 result = MOVE_POS_MATCH_OR_ZV;
6293 break;
6296 /* Stop when ZV reached.
6297 We used to stop here when TO_CHARPOS reached as well, but that is
6298 too soon if this glyph does not fit on this line. So we handle it
6299 explicitly below. */
6300 if (!get_next_display_element (it)
6301 || (it->truncate_lines_p
6302 && BUFFER_POS_REACHED_P ()))
6304 result = MOVE_POS_MATCH_OR_ZV;
6305 break;
6308 /* The call to produce_glyphs will get the metrics of the
6309 display element IT is loaded with. We record in x the
6310 x-position before this display element in case it does not
6311 fit on the line. */
6312 x = it->current_x;
6314 /* Remember the line height so far in case the next element doesn't
6315 fit on the line. */
6316 if (!it->truncate_lines_p)
6318 ascent = it->max_ascent;
6319 descent = it->max_descent;
6322 PRODUCE_GLYPHS (it);
6324 if (it->area != TEXT_AREA)
6326 set_iterator_to_next (it, 1);
6327 continue;
6330 /* The number of glyphs we get back in IT->nglyphs will normally
6331 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6332 character on a terminal frame, or (iii) a line end. For the
6333 second case, IT->nglyphs - 1 padding glyphs will be present
6334 (on X frames, there is only one glyph produced for a
6335 composite character.
6337 The behavior implemented below means, for continuation lines,
6338 that as many spaces of a TAB as fit on the current line are
6339 displayed there. For terminal frames, as many glyphs of a
6340 multi-glyph character are displayed in the current line, too.
6341 This is what the old redisplay code did, and we keep it that
6342 way. Under X, the whole shape of a complex character must
6343 fit on the line or it will be completely displayed in the
6344 next line.
6346 Note that both for tabs and padding glyphs, all glyphs have
6347 the same width. */
6348 if (it->nglyphs)
6350 /* More than one glyph or glyph doesn't fit on line. All
6351 glyphs have the same width. */
6352 int single_glyph_width = it->pixel_width / it->nglyphs;
6353 int new_x;
6354 int x_before_this_char = x;
6355 int hpos_before_this_char = it->hpos;
6357 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6359 new_x = x + single_glyph_width;
6361 /* We want to leave anything reaching TO_X to the caller. */
6362 if ((op & MOVE_TO_X) && new_x > to_x)
6364 if (BUFFER_POS_REACHED_P ())
6365 goto buffer_pos_reached;
6366 it->current_x = x;
6367 result = MOVE_X_REACHED;
6368 break;
6370 else if (/* Lines are continued. */
6371 !it->truncate_lines_p
6372 && (/* And glyph doesn't fit on the line. */
6373 new_x > it->last_visible_x
6374 /* Or it fits exactly and we're on a window
6375 system frame. */
6376 || (new_x == it->last_visible_x
6377 && FRAME_WINDOW_P (it->f))))
6379 if (/* IT->hpos == 0 means the very first glyph
6380 doesn't fit on the line, e.g. a wide image. */
6381 it->hpos == 0
6382 || (new_x == it->last_visible_x
6383 && FRAME_WINDOW_P (it->f)))
6385 ++it->hpos;
6386 it->current_x = new_x;
6388 /* The character's last glyph just barely fits
6389 in this row. */
6390 if (i == it->nglyphs - 1)
6392 /* If this is the destination position,
6393 return a position *before* it in this row,
6394 now that we know it fits in this row. */
6395 if (BUFFER_POS_REACHED_P ())
6397 it->hpos = hpos_before_this_char;
6398 it->current_x = x_before_this_char;
6399 result = MOVE_POS_MATCH_OR_ZV;
6400 break;
6403 set_iterator_to_next (it, 1);
6404 #ifdef HAVE_WINDOW_SYSTEM
6405 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6407 if (!get_next_display_element (it))
6409 result = MOVE_POS_MATCH_OR_ZV;
6410 break;
6412 if (BUFFER_POS_REACHED_P ())
6414 if (ITERATOR_AT_END_OF_LINE_P (it))
6415 result = MOVE_POS_MATCH_OR_ZV;
6416 else
6417 result = MOVE_LINE_CONTINUED;
6418 break;
6420 if (ITERATOR_AT_END_OF_LINE_P (it))
6422 result = MOVE_NEWLINE_OR_CR;
6423 break;
6426 #endif /* HAVE_WINDOW_SYSTEM */
6429 else
6431 it->current_x = x;
6432 it->max_ascent = ascent;
6433 it->max_descent = descent;
6436 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6437 IT_CHARPOS (*it)));
6438 result = MOVE_LINE_CONTINUED;
6439 break;
6441 else if (BUFFER_POS_REACHED_P ())
6442 goto buffer_pos_reached;
6443 else if (new_x > it->first_visible_x)
6445 /* Glyph is visible. Increment number of glyphs that
6446 would be displayed. */
6447 ++it->hpos;
6449 else
6451 /* Glyph is completely off the left margin of the display
6452 area. Nothing to do. */
6456 if (result != MOVE_UNDEFINED)
6457 break;
6459 else if (BUFFER_POS_REACHED_P ())
6461 buffer_pos_reached:
6462 it->current_x = x;
6463 it->max_ascent = ascent;
6464 it->max_descent = descent;
6465 result = MOVE_POS_MATCH_OR_ZV;
6466 break;
6468 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6470 /* Stop when TO_X specified and reached. This check is
6471 necessary here because of lines consisting of a line end,
6472 only. The line end will not produce any glyphs and we
6473 would never get MOVE_X_REACHED. */
6474 xassert (it->nglyphs == 0);
6475 result = MOVE_X_REACHED;
6476 break;
6479 /* Is this a line end? If yes, we're done. */
6480 if (ITERATOR_AT_END_OF_LINE_P (it))
6482 result = MOVE_NEWLINE_OR_CR;
6483 break;
6486 /* The current display element has been consumed. Advance
6487 to the next. */
6488 set_iterator_to_next (it, 1);
6490 /* Stop if lines are truncated and IT's current x-position is
6491 past the right edge of the window now. */
6492 if (it->truncate_lines_p
6493 && it->current_x >= it->last_visible_x)
6495 #ifdef HAVE_WINDOW_SYSTEM
6496 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6498 if (!get_next_display_element (it)
6499 || BUFFER_POS_REACHED_P ())
6501 result = MOVE_POS_MATCH_OR_ZV;
6502 break;
6504 if (ITERATOR_AT_END_OF_LINE_P (it))
6506 result = MOVE_NEWLINE_OR_CR;
6507 break;
6510 #endif /* HAVE_WINDOW_SYSTEM */
6511 result = MOVE_LINE_TRUNCATED;
6512 break;
6516 #undef BUFFER_POS_REACHED_P
6518 /* Restore the iterator settings altered at the beginning of this
6519 function. */
6520 it->glyph_row = saved_glyph_row;
6521 return result;
6525 /* Move IT forward until it satisfies one or more of the criteria in
6526 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6528 OP is a bit-mask that specifies where to stop, and in particular,
6529 which of those four position arguments makes a difference. See the
6530 description of enum move_operation_enum.
6532 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6533 screen line, this function will set IT to the next position >
6534 TO_CHARPOS. */
6536 void
6537 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6538 struct it *it;
6539 int to_charpos, to_x, to_y, to_vpos;
6540 int op;
6542 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6543 int line_height;
6544 int reached = 0;
6546 for (;;)
6548 if (op & MOVE_TO_VPOS)
6550 /* If no TO_CHARPOS and no TO_X specified, stop at the
6551 start of the line TO_VPOS. */
6552 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6554 if (it->vpos == to_vpos)
6556 reached = 1;
6557 break;
6559 else
6560 skip = move_it_in_display_line_to (it, -1, -1, 0);
6562 else
6564 /* TO_VPOS >= 0 means stop at TO_X in the line at
6565 TO_VPOS, or at TO_POS, whichever comes first. */
6566 if (it->vpos == to_vpos)
6568 reached = 2;
6569 break;
6572 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6574 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6576 reached = 3;
6577 break;
6579 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6581 /* We have reached TO_X but not in the line we want. */
6582 skip = move_it_in_display_line_to (it, to_charpos,
6583 -1, MOVE_TO_POS);
6584 if (skip == MOVE_POS_MATCH_OR_ZV)
6586 reached = 4;
6587 break;
6592 else if (op & MOVE_TO_Y)
6594 struct it it_backup;
6596 /* TO_Y specified means stop at TO_X in the line containing
6597 TO_Y---or at TO_CHARPOS if this is reached first. The
6598 problem is that we can't really tell whether the line
6599 contains TO_Y before we have completely scanned it, and
6600 this may skip past TO_X. What we do is to first scan to
6601 TO_X.
6603 If TO_X is not specified, use a TO_X of zero. The reason
6604 is to make the outcome of this function more predictable.
6605 If we didn't use TO_X == 0, we would stop at the end of
6606 the line which is probably not what a caller would expect
6607 to happen. */
6608 skip = move_it_in_display_line_to (it, to_charpos,
6609 ((op & MOVE_TO_X)
6610 ? to_x : 0),
6611 (MOVE_TO_X
6612 | (op & MOVE_TO_POS)));
6614 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6615 if (skip == MOVE_POS_MATCH_OR_ZV)
6617 reached = 5;
6618 break;
6621 /* If TO_X was reached, we would like to know whether TO_Y
6622 is in the line. This can only be said if we know the
6623 total line height which requires us to scan the rest of
6624 the line. */
6625 if (skip == MOVE_X_REACHED)
6627 it_backup = *it;
6628 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6629 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6630 op & MOVE_TO_POS);
6631 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6634 /* Now, decide whether TO_Y is in this line. */
6635 line_height = it->max_ascent + it->max_descent;
6636 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6638 if (to_y >= it->current_y
6639 && to_y < it->current_y + line_height)
6641 if (skip == MOVE_X_REACHED)
6642 /* If TO_Y is in this line and TO_X was reached above,
6643 we scanned too far. We have to restore IT's settings
6644 to the ones before skipping. */
6645 *it = it_backup;
6646 reached = 6;
6648 else if (skip == MOVE_X_REACHED)
6650 skip = skip2;
6651 if (skip == MOVE_POS_MATCH_OR_ZV)
6652 reached = 7;
6655 if (reached)
6656 break;
6658 else
6659 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6661 switch (skip)
6663 case MOVE_POS_MATCH_OR_ZV:
6664 reached = 8;
6665 goto out;
6667 case MOVE_NEWLINE_OR_CR:
6668 set_iterator_to_next (it, 1);
6669 it->continuation_lines_width = 0;
6670 break;
6672 case MOVE_LINE_TRUNCATED:
6673 it->continuation_lines_width = 0;
6674 reseat_at_next_visible_line_start (it, 0);
6675 if ((op & MOVE_TO_POS) != 0
6676 && IT_CHARPOS (*it) > to_charpos)
6678 reached = 9;
6679 goto out;
6681 break;
6683 case MOVE_LINE_CONTINUED:
6684 it->continuation_lines_width += it->current_x;
6685 break;
6687 default:
6688 abort ();
6691 /* Reset/increment for the next run. */
6692 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6693 it->current_x = it->hpos = 0;
6694 it->current_y += it->max_ascent + it->max_descent;
6695 ++it->vpos;
6696 last_height = it->max_ascent + it->max_descent;
6697 last_max_ascent = it->max_ascent;
6698 it->max_ascent = it->max_descent = 0;
6701 out:
6703 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6707 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6709 If DY > 0, move IT backward at least that many pixels. DY = 0
6710 means move IT backward to the preceding line start or BEGV. This
6711 function may move over more than DY pixels if IT->current_y - DY
6712 ends up in the middle of a line; in this case IT->current_y will be
6713 set to the top of the line moved to. */
6715 void
6716 move_it_vertically_backward (it, dy)
6717 struct it *it;
6718 int dy;
6720 int nlines, h;
6721 struct it it2, it3;
6722 int start_pos;
6724 move_further_back:
6725 xassert (dy >= 0);
6727 start_pos = IT_CHARPOS (*it);
6729 /* Estimate how many newlines we must move back. */
6730 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6732 /* Set the iterator's position that many lines back. */
6733 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6734 back_to_previous_visible_line_start (it);
6736 /* Reseat the iterator here. When moving backward, we don't want
6737 reseat to skip forward over invisible text, set up the iterator
6738 to deliver from overlay strings at the new position etc. So,
6739 use reseat_1 here. */
6740 reseat_1 (it, it->current.pos, 1);
6742 /* We are now surely at a line start. */
6743 it->current_x = it->hpos = 0;
6744 it->continuation_lines_width = 0;
6746 /* Move forward and see what y-distance we moved. First move to the
6747 start of the next line so that we get its height. We need this
6748 height to be able to tell whether we reached the specified
6749 y-distance. */
6750 it2 = *it;
6751 it2.max_ascent = it2.max_descent = 0;
6754 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6755 MOVE_TO_POS | MOVE_TO_VPOS);
6757 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6758 xassert (IT_CHARPOS (*it) >= BEGV);
6759 it3 = it2;
6761 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6762 xassert (IT_CHARPOS (*it) >= BEGV);
6763 /* H is the actual vertical distance from the position in *IT
6764 and the starting position. */
6765 h = it2.current_y - it->current_y;
6766 /* NLINES is the distance in number of lines. */
6767 nlines = it2.vpos - it->vpos;
6769 /* Correct IT's y and vpos position
6770 so that they are relative to the starting point. */
6771 it->vpos -= nlines;
6772 it->current_y -= h;
6774 if (dy == 0)
6776 /* DY == 0 means move to the start of the screen line. The
6777 value of nlines is > 0 if continuation lines were involved. */
6778 if (nlines > 0)
6779 move_it_by_lines (it, nlines, 1);
6780 #if 0
6781 /* I think this assert is bogus if buffer contains
6782 invisible text or images. KFS. */
6783 xassert (IT_CHARPOS (*it) <= start_pos);
6784 #endif
6786 else
6788 /* The y-position we try to reach, relative to *IT.
6789 Note that H has been subtracted in front of the if-statement. */
6790 int target_y = it->current_y + h - dy;
6791 int y0 = it3.current_y;
6792 int y1 = line_bottom_y (&it3);
6793 int line_height = y1 - y0;
6795 /* If we did not reach target_y, try to move further backward if
6796 we can. If we moved too far backward, try to move forward. */
6797 if (target_y < it->current_y
6798 /* This is heuristic. In a window that's 3 lines high, with
6799 a line height of 13 pixels each, recentering with point
6800 on the bottom line will try to move -39/2 = 19 pixels
6801 backward. Try to avoid moving into the first line. */
6802 && (it->current_y - target_y
6803 > min (window_box_height (it->w), line_height * 2 / 3))
6804 && IT_CHARPOS (*it) > BEGV)
6806 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6807 target_y - it->current_y));
6808 dy = it->current_y - target_y;
6809 goto move_further_back;
6811 else if (target_y >= it->current_y + line_height
6812 && IT_CHARPOS (*it) < ZV)
6814 /* Should move forward by at least one line, maybe more.
6816 Note: Calling move_it_by_lines can be expensive on
6817 terminal frames, where compute_motion is used (via
6818 vmotion) to do the job, when there are very long lines
6819 and truncate-lines is nil. That's the reason for
6820 treating terminal frames specially here. */
6822 if (!FRAME_WINDOW_P (it->f))
6823 move_it_vertically (it, target_y - (it->current_y + line_height));
6824 else
6828 move_it_by_lines (it, 1, 1);
6830 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6833 #if 0
6834 /* I think this assert is bogus if buffer contains
6835 invisible text or images. KFS. */
6836 xassert (IT_CHARPOS (*it) >= BEGV);
6837 #endif
6843 /* Move IT by a specified amount of pixel lines DY. DY negative means
6844 move backwards. DY = 0 means move to start of screen line. At the
6845 end, IT will be on the start of a screen line. */
6847 void
6848 move_it_vertically (it, dy)
6849 struct it *it;
6850 int dy;
6852 if (dy <= 0)
6853 move_it_vertically_backward (it, -dy);
6854 else
6856 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6857 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6858 MOVE_TO_POS | MOVE_TO_Y);
6859 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6861 /* If buffer ends in ZV without a newline, move to the start of
6862 the line to satisfy the post-condition. */
6863 if (IT_CHARPOS (*it) == ZV
6864 && ZV > BEGV
6865 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6866 move_it_by_lines (it, 0, 0);
6871 /* Move iterator IT past the end of the text line it is in. */
6873 void
6874 move_it_past_eol (it)
6875 struct it *it;
6877 enum move_it_result rc;
6879 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6880 if (rc == MOVE_NEWLINE_OR_CR)
6881 set_iterator_to_next (it, 0);
6885 #if 0 /* Currently not used. */
6887 /* Return non-zero if some text between buffer positions START_CHARPOS
6888 and END_CHARPOS is invisible. IT->window is the window for text
6889 property lookup. */
6891 static int
6892 invisible_text_between_p (it, start_charpos, end_charpos)
6893 struct it *it;
6894 int start_charpos, end_charpos;
6896 Lisp_Object prop, limit;
6897 int invisible_found_p;
6899 xassert (it != NULL && start_charpos <= end_charpos);
6901 /* Is text at START invisible? */
6902 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6903 it->window);
6904 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6905 invisible_found_p = 1;
6906 else
6908 limit = Fnext_single_char_property_change (make_number (start_charpos),
6909 Qinvisible, Qnil,
6910 make_number (end_charpos));
6911 invisible_found_p = XFASTINT (limit) < end_charpos;
6914 return invisible_found_p;
6917 #endif /* 0 */
6920 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6921 negative means move up. DVPOS == 0 means move to the start of the
6922 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6923 NEED_Y_P is zero, IT->current_y will be left unchanged.
6925 Further optimization ideas: If we would know that IT->f doesn't use
6926 a face with proportional font, we could be faster for
6927 truncate-lines nil. */
6929 void
6930 move_it_by_lines (it, dvpos, need_y_p)
6931 struct it *it;
6932 int dvpos, need_y_p;
6934 struct position pos;
6936 if (!FRAME_WINDOW_P (it->f))
6938 struct text_pos textpos;
6940 /* We can use vmotion on frames without proportional fonts. */
6941 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6942 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6943 reseat (it, textpos, 1);
6944 it->vpos += pos.vpos;
6945 it->current_y += pos.vpos;
6947 else if (dvpos == 0)
6949 /* DVPOS == 0 means move to the start of the screen line. */
6950 move_it_vertically_backward (it, 0);
6951 xassert (it->current_x == 0 && it->hpos == 0);
6952 /* Let next call to line_bottom_y calculate real line height */
6953 last_height = 0;
6955 else if (dvpos > 0)
6957 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6958 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6959 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6961 else
6963 struct it it2;
6964 int start_charpos, i;
6966 /* Start at the beginning of the screen line containing IT's
6967 position. This may actually move vertically backwards,
6968 in case of overlays, so adjust dvpos accordingly. */
6969 dvpos += it->vpos;
6970 move_it_vertically_backward (it, 0);
6971 dvpos -= it->vpos;
6973 /* Go back -DVPOS visible lines and reseat the iterator there. */
6974 start_charpos = IT_CHARPOS (*it);
6975 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6976 back_to_previous_visible_line_start (it);
6977 reseat (it, it->current.pos, 1);
6979 /* Move further back if we end up in a string or an image. */
6980 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6982 /* First try to move to start of display line. */
6983 dvpos += it->vpos;
6984 move_it_vertically_backward (it, 0);
6985 dvpos -= it->vpos;
6986 if (IT_POS_VALID_AFTER_MOVE_P (it))
6987 break;
6988 /* If start of line is still in string or image,
6989 move further back. */
6990 back_to_previous_visible_line_start (it);
6991 reseat (it, it->current.pos, 1);
6992 dvpos--;
6995 it->current_x = it->hpos = 0;
6997 /* Above call may have moved too far if continuation lines
6998 are involved. Scan forward and see if it did. */
6999 it2 = *it;
7000 it2.vpos = it2.current_y = 0;
7001 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7002 it->vpos -= it2.vpos;
7003 it->current_y -= it2.current_y;
7004 it->current_x = it->hpos = 0;
7006 /* If we moved too far back, move IT some lines forward. */
7007 if (it2.vpos > -dvpos)
7009 int delta = it2.vpos + dvpos;
7010 it2 = *it;
7011 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7012 /* Move back again if we got too far ahead. */
7013 if (IT_CHARPOS (*it) >= start_charpos)
7014 *it = it2;
7019 /* Return 1 if IT points into the middle of a display vector. */
7022 in_display_vector_p (it)
7023 struct it *it;
7025 return (it->method == GET_FROM_DISPLAY_VECTOR
7026 && it->current.dpvec_index > 0
7027 && it->dpvec + it->current.dpvec_index != it->dpend);
7031 /***********************************************************************
7032 Messages
7033 ***********************************************************************/
7036 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7037 to *Messages*. */
7039 void
7040 add_to_log (format, arg1, arg2)
7041 char *format;
7042 Lisp_Object arg1, arg2;
7044 Lisp_Object args[3];
7045 Lisp_Object msg, fmt;
7046 char *buffer;
7047 int len;
7048 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7049 USE_SAFE_ALLOCA;
7051 /* Do nothing if called asynchronously. Inserting text into
7052 a buffer may call after-change-functions and alike and
7053 that would means running Lisp asynchronously. */
7054 if (handling_signal)
7055 return;
7057 fmt = msg = Qnil;
7058 GCPRO4 (fmt, msg, arg1, arg2);
7060 args[0] = fmt = build_string (format);
7061 args[1] = arg1;
7062 args[2] = arg2;
7063 msg = Fformat (3, args);
7065 len = SBYTES (msg) + 1;
7066 SAFE_ALLOCA (buffer, char *, len);
7067 bcopy (SDATA (msg), buffer, len);
7069 message_dolog (buffer, len - 1, 1, 0);
7070 SAFE_FREE ();
7072 UNGCPRO;
7076 /* Output a newline in the *Messages* buffer if "needs" one. */
7078 void
7079 message_log_maybe_newline ()
7081 if (message_log_need_newline)
7082 message_dolog ("", 0, 1, 0);
7086 /* Add a string M of length NBYTES to the message log, optionally
7087 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7088 nonzero, means interpret the contents of M as multibyte. This
7089 function calls low-level routines in order to bypass text property
7090 hooks, etc. which might not be safe to run.
7092 This may GC (insert may run before/after change hooks),
7093 so the buffer M must NOT point to a Lisp string. */
7095 void
7096 message_dolog (m, nbytes, nlflag, multibyte)
7097 const char *m;
7098 int nbytes, nlflag, multibyte;
7100 if (!NILP (Vmemory_full))
7101 return;
7103 if (!NILP (Vmessage_log_max))
7105 struct buffer *oldbuf;
7106 Lisp_Object oldpoint, oldbegv, oldzv;
7107 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7108 int point_at_end = 0;
7109 int zv_at_end = 0;
7110 Lisp_Object old_deactivate_mark, tem;
7111 struct gcpro gcpro1;
7113 old_deactivate_mark = Vdeactivate_mark;
7114 oldbuf = current_buffer;
7115 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7116 current_buffer->undo_list = Qt;
7118 oldpoint = message_dolog_marker1;
7119 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7120 oldbegv = message_dolog_marker2;
7121 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7122 oldzv = message_dolog_marker3;
7123 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7124 GCPRO1 (old_deactivate_mark);
7126 if (PT == Z)
7127 point_at_end = 1;
7128 if (ZV == Z)
7129 zv_at_end = 1;
7131 BEGV = BEG;
7132 BEGV_BYTE = BEG_BYTE;
7133 ZV = Z;
7134 ZV_BYTE = Z_BYTE;
7135 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7137 /* Insert the string--maybe converting multibyte to single byte
7138 or vice versa, so that all the text fits the buffer. */
7139 if (multibyte
7140 && NILP (current_buffer->enable_multibyte_characters))
7142 int i, c, char_bytes;
7143 unsigned char work[1];
7145 /* Convert a multibyte string to single-byte
7146 for the *Message* buffer. */
7147 for (i = 0; i < nbytes; i += char_bytes)
7149 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7150 work[0] = (SINGLE_BYTE_CHAR_P (c)
7152 : multibyte_char_to_unibyte (c, Qnil));
7153 insert_1_both (work, 1, 1, 1, 0, 0);
7156 else if (! multibyte
7157 && ! NILP (current_buffer->enable_multibyte_characters))
7159 int i, c, char_bytes;
7160 unsigned char *msg = (unsigned char *) m;
7161 unsigned char str[MAX_MULTIBYTE_LENGTH];
7162 /* Convert a single-byte string to multibyte
7163 for the *Message* buffer. */
7164 for (i = 0; i < nbytes; i++)
7166 c = unibyte_char_to_multibyte (msg[i]);
7167 char_bytes = CHAR_STRING (c, str);
7168 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7171 else if (nbytes)
7172 insert_1 (m, nbytes, 1, 0, 0);
7174 if (nlflag)
7176 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7177 insert_1 ("\n", 1, 1, 0, 0);
7179 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7180 this_bol = PT;
7181 this_bol_byte = PT_BYTE;
7183 /* See if this line duplicates the previous one.
7184 If so, combine duplicates. */
7185 if (this_bol > BEG)
7187 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7188 prev_bol = PT;
7189 prev_bol_byte = PT_BYTE;
7191 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7192 this_bol, this_bol_byte);
7193 if (dup)
7195 del_range_both (prev_bol, prev_bol_byte,
7196 this_bol, this_bol_byte, 0);
7197 if (dup > 1)
7199 char dupstr[40];
7200 int duplen;
7202 /* If you change this format, don't forget to also
7203 change message_log_check_duplicate. */
7204 sprintf (dupstr, " [%d times]", dup);
7205 duplen = strlen (dupstr);
7206 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7207 insert_1 (dupstr, duplen, 1, 0, 1);
7212 /* If we have more than the desired maximum number of lines
7213 in the *Messages* buffer now, delete the oldest ones.
7214 This is safe because we don't have undo in this buffer. */
7216 if (NATNUMP (Vmessage_log_max))
7218 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7219 -XFASTINT (Vmessage_log_max) - 1, 0);
7220 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7223 BEGV = XMARKER (oldbegv)->charpos;
7224 BEGV_BYTE = marker_byte_position (oldbegv);
7226 if (zv_at_end)
7228 ZV = Z;
7229 ZV_BYTE = Z_BYTE;
7231 else
7233 ZV = XMARKER (oldzv)->charpos;
7234 ZV_BYTE = marker_byte_position (oldzv);
7237 if (point_at_end)
7238 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7239 else
7240 /* We can't do Fgoto_char (oldpoint) because it will run some
7241 Lisp code. */
7242 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7243 XMARKER (oldpoint)->bytepos);
7245 UNGCPRO;
7246 unchain_marker (XMARKER (oldpoint));
7247 unchain_marker (XMARKER (oldbegv));
7248 unchain_marker (XMARKER (oldzv));
7250 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7251 set_buffer_internal (oldbuf);
7252 if (NILP (tem))
7253 windows_or_buffers_changed = old_windows_or_buffers_changed;
7254 message_log_need_newline = !nlflag;
7255 Vdeactivate_mark = old_deactivate_mark;
7260 /* We are at the end of the buffer after just having inserted a newline.
7261 (Note: We depend on the fact we won't be crossing the gap.)
7262 Check to see if the most recent message looks a lot like the previous one.
7263 Return 0 if different, 1 if the new one should just replace it, or a
7264 value N > 1 if we should also append " [N times]". */
7266 static int
7267 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7268 int prev_bol, this_bol;
7269 int prev_bol_byte, this_bol_byte;
7271 int i;
7272 int len = Z_BYTE - 1 - this_bol_byte;
7273 int seen_dots = 0;
7274 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7275 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7277 for (i = 0; i < len; i++)
7279 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7280 seen_dots = 1;
7281 if (p1[i] != p2[i])
7282 return seen_dots;
7284 p1 += len;
7285 if (*p1 == '\n')
7286 return 2;
7287 if (*p1++ == ' ' && *p1++ == '[')
7289 int n = 0;
7290 while (*p1 >= '0' && *p1 <= '9')
7291 n = n * 10 + *p1++ - '0';
7292 if (strncmp (p1, " times]\n", 8) == 0)
7293 return n+1;
7295 return 0;
7299 /* Display an echo area message M with a specified length of NBYTES
7300 bytes. The string may include null characters. If M is 0, clear
7301 out any existing message, and let the mini-buffer text show
7302 through.
7304 This may GC, so the buffer M must NOT point to a Lisp string. */
7306 void
7307 message2 (m, nbytes, multibyte)
7308 const char *m;
7309 int nbytes;
7310 int multibyte;
7312 /* First flush out any partial line written with print. */
7313 message_log_maybe_newline ();
7314 if (m)
7315 message_dolog (m, nbytes, 1, multibyte);
7316 message2_nolog (m, nbytes, multibyte);
7320 /* The non-logging counterpart of message2. */
7322 void
7323 message2_nolog (m, nbytes, multibyte)
7324 const char *m;
7325 int nbytes, multibyte;
7327 struct frame *sf = SELECTED_FRAME ();
7328 message_enable_multibyte = multibyte;
7330 if (noninteractive)
7332 if (noninteractive_need_newline)
7333 putc ('\n', stderr);
7334 noninteractive_need_newline = 0;
7335 if (m)
7336 fwrite (m, nbytes, 1, stderr);
7337 if (cursor_in_echo_area == 0)
7338 fprintf (stderr, "\n");
7339 fflush (stderr);
7341 /* A null message buffer means that the frame hasn't really been
7342 initialized yet. Error messages get reported properly by
7343 cmd_error, so this must be just an informative message; toss it. */
7344 else if (INTERACTIVE
7345 && sf->glyphs_initialized_p
7346 && FRAME_MESSAGE_BUF (sf))
7348 Lisp_Object mini_window;
7349 struct frame *f;
7351 /* Get the frame containing the mini-buffer
7352 that the selected frame is using. */
7353 mini_window = FRAME_MINIBUF_WINDOW (sf);
7354 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7356 FRAME_SAMPLE_VISIBILITY (f);
7357 if (FRAME_VISIBLE_P (sf)
7358 && ! FRAME_VISIBLE_P (f))
7359 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7361 if (m)
7363 set_message (m, Qnil, nbytes, multibyte);
7364 if (minibuffer_auto_raise)
7365 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7367 else
7368 clear_message (1, 1);
7370 do_pending_window_change (0);
7371 echo_area_display (1);
7372 do_pending_window_change (0);
7373 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7374 (*frame_up_to_date_hook) (f);
7379 /* Display an echo area message M with a specified length of NBYTES
7380 bytes. The string may include null characters. If M is not a
7381 string, clear out any existing message, and let the mini-buffer
7382 text show through.
7384 This function cancels echoing. */
7386 void
7387 message3 (m, nbytes, multibyte)
7388 Lisp_Object m;
7389 int nbytes;
7390 int multibyte;
7392 struct gcpro gcpro1;
7394 GCPRO1 (m);
7395 clear_message (1,1);
7396 cancel_echoing ();
7398 /* First flush out any partial line written with print. */
7399 message_log_maybe_newline ();
7400 if (STRINGP (m))
7402 char *buffer;
7403 USE_SAFE_ALLOCA;
7405 SAFE_ALLOCA (buffer, char *, nbytes);
7406 bcopy (SDATA (m), buffer, nbytes);
7407 message_dolog (buffer, nbytes, 1, multibyte);
7408 SAFE_FREE ();
7410 message3_nolog (m, nbytes, multibyte);
7412 UNGCPRO;
7416 /* The non-logging version of message3.
7417 This does not cancel echoing, because it is used for echoing.
7418 Perhaps we need to make a separate function for echoing
7419 and make this cancel echoing. */
7421 void
7422 message3_nolog (m, nbytes, multibyte)
7423 Lisp_Object m;
7424 int nbytes, multibyte;
7426 struct frame *sf = SELECTED_FRAME ();
7427 message_enable_multibyte = multibyte;
7429 if (noninteractive)
7431 if (noninteractive_need_newline)
7432 putc ('\n', stderr);
7433 noninteractive_need_newline = 0;
7434 if (STRINGP (m))
7435 fwrite (SDATA (m), nbytes, 1, stderr);
7436 if (cursor_in_echo_area == 0)
7437 fprintf (stderr, "\n");
7438 fflush (stderr);
7440 /* A null message buffer means that the frame hasn't really been
7441 initialized yet. Error messages get reported properly by
7442 cmd_error, so this must be just an informative message; toss it. */
7443 else if (INTERACTIVE
7444 && sf->glyphs_initialized_p
7445 && FRAME_MESSAGE_BUF (sf))
7447 Lisp_Object mini_window;
7448 Lisp_Object frame;
7449 struct frame *f;
7451 /* Get the frame containing the mini-buffer
7452 that the selected frame is using. */
7453 mini_window = FRAME_MINIBUF_WINDOW (sf);
7454 frame = XWINDOW (mini_window)->frame;
7455 f = XFRAME (frame);
7457 FRAME_SAMPLE_VISIBILITY (f);
7458 if (FRAME_VISIBLE_P (sf)
7459 && !FRAME_VISIBLE_P (f))
7460 Fmake_frame_visible (frame);
7462 if (STRINGP (m) && SCHARS (m) > 0)
7464 set_message (NULL, m, nbytes, multibyte);
7465 if (minibuffer_auto_raise)
7466 Fraise_frame (frame);
7467 /* Assume we are not echoing.
7468 (If we are, echo_now will override this.) */
7469 echo_message_buffer = Qnil;
7471 else
7472 clear_message (1, 1);
7474 do_pending_window_change (0);
7475 echo_area_display (1);
7476 do_pending_window_change (0);
7477 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7478 (*frame_up_to_date_hook) (f);
7483 /* Display a null-terminated echo area message M. If M is 0, clear
7484 out any existing message, and let the mini-buffer text show through.
7486 The buffer M must continue to exist until after the echo area gets
7487 cleared or some other message gets displayed there. Do not pass
7488 text that is stored in a Lisp string. Do not pass text in a buffer
7489 that was alloca'd. */
7491 void
7492 message1 (m)
7493 char *m;
7495 message2 (m, (m ? strlen (m) : 0), 0);
7499 /* The non-logging counterpart of message1. */
7501 void
7502 message1_nolog (m)
7503 char *m;
7505 message2_nolog (m, (m ? strlen (m) : 0), 0);
7508 /* Display a message M which contains a single %s
7509 which gets replaced with STRING. */
7511 void
7512 message_with_string (m, string, log)
7513 char *m;
7514 Lisp_Object string;
7515 int log;
7517 CHECK_STRING (string);
7519 if (noninteractive)
7521 if (m)
7523 if (noninteractive_need_newline)
7524 putc ('\n', stderr);
7525 noninteractive_need_newline = 0;
7526 fprintf (stderr, m, SDATA (string));
7527 if (cursor_in_echo_area == 0)
7528 fprintf (stderr, "\n");
7529 fflush (stderr);
7532 else if (INTERACTIVE)
7534 /* The frame whose minibuffer we're going to display the message on.
7535 It may be larger than the selected frame, so we need
7536 to use its buffer, not the selected frame's buffer. */
7537 Lisp_Object mini_window;
7538 struct frame *f, *sf = SELECTED_FRAME ();
7540 /* Get the frame containing the minibuffer
7541 that the selected frame is using. */
7542 mini_window = FRAME_MINIBUF_WINDOW (sf);
7543 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7545 /* A null message buffer means that the frame hasn't really been
7546 initialized yet. Error messages get reported properly by
7547 cmd_error, so this must be just an informative message; toss it. */
7548 if (FRAME_MESSAGE_BUF (f))
7550 Lisp_Object args[2], message;
7551 struct gcpro gcpro1, gcpro2;
7553 args[0] = build_string (m);
7554 args[1] = message = string;
7555 GCPRO2 (args[0], message);
7556 gcpro1.nvars = 2;
7558 message = Fformat (2, args);
7560 if (log)
7561 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7562 else
7563 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7565 UNGCPRO;
7567 /* Print should start at the beginning of the message
7568 buffer next time. */
7569 message_buf_print = 0;
7575 /* Dump an informative message to the minibuf. If M is 0, clear out
7576 any existing message, and let the mini-buffer text show through. */
7578 /* VARARGS 1 */
7579 void
7580 message (m, a1, a2, a3)
7581 char *m;
7582 EMACS_INT a1, a2, a3;
7584 if (noninteractive)
7586 if (m)
7588 if (noninteractive_need_newline)
7589 putc ('\n', stderr);
7590 noninteractive_need_newline = 0;
7591 fprintf (stderr, m, a1, a2, a3);
7592 if (cursor_in_echo_area == 0)
7593 fprintf (stderr, "\n");
7594 fflush (stderr);
7597 else if (INTERACTIVE)
7599 /* The frame whose mini-buffer we're going to display the message
7600 on. It may be larger than the selected frame, so we need to
7601 use its buffer, not the selected frame's buffer. */
7602 Lisp_Object mini_window;
7603 struct frame *f, *sf = SELECTED_FRAME ();
7605 /* Get the frame containing the mini-buffer
7606 that the selected frame is using. */
7607 mini_window = FRAME_MINIBUF_WINDOW (sf);
7608 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7610 /* A null message buffer means that the frame hasn't really been
7611 initialized yet. Error messages get reported properly by
7612 cmd_error, so this must be just an informative message; toss
7613 it. */
7614 if (FRAME_MESSAGE_BUF (f))
7616 if (m)
7618 int len;
7619 #ifdef NO_ARG_ARRAY
7620 char *a[3];
7621 a[0] = (char *) a1;
7622 a[1] = (char *) a2;
7623 a[2] = (char *) a3;
7625 len = doprnt (FRAME_MESSAGE_BUF (f),
7626 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7627 #else
7628 len = doprnt (FRAME_MESSAGE_BUF (f),
7629 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7630 (char **) &a1);
7631 #endif /* NO_ARG_ARRAY */
7633 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7635 else
7636 message1 (0);
7638 /* Print should start at the beginning of the message
7639 buffer next time. */
7640 message_buf_print = 0;
7646 /* The non-logging version of message. */
7648 void
7649 message_nolog (m, a1, a2, a3)
7650 char *m;
7651 EMACS_INT a1, a2, a3;
7653 Lisp_Object old_log_max;
7654 old_log_max = Vmessage_log_max;
7655 Vmessage_log_max = Qnil;
7656 message (m, a1, a2, a3);
7657 Vmessage_log_max = old_log_max;
7661 /* Display the current message in the current mini-buffer. This is
7662 only called from error handlers in process.c, and is not time
7663 critical. */
7665 void
7666 update_echo_area ()
7668 if (!NILP (echo_area_buffer[0]))
7670 Lisp_Object string;
7671 string = Fcurrent_message ();
7672 message3 (string, SBYTES (string),
7673 !NILP (current_buffer->enable_multibyte_characters));
7678 /* Make sure echo area buffers in `echo_buffers' are live.
7679 If they aren't, make new ones. */
7681 static void
7682 ensure_echo_area_buffers ()
7684 int i;
7686 for (i = 0; i < 2; ++i)
7687 if (!BUFFERP (echo_buffer[i])
7688 || NILP (XBUFFER (echo_buffer[i])->name))
7690 char name[30];
7691 Lisp_Object old_buffer;
7692 int j;
7694 old_buffer = echo_buffer[i];
7695 sprintf (name, " *Echo Area %d*", i);
7696 echo_buffer[i] = Fget_buffer_create (build_string (name));
7697 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7699 for (j = 0; j < 2; ++j)
7700 if (EQ (old_buffer, echo_area_buffer[j]))
7701 echo_area_buffer[j] = echo_buffer[i];
7706 /* Call FN with args A1..A4 with either the current or last displayed
7707 echo_area_buffer as current buffer.
7709 WHICH zero means use the current message buffer
7710 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7711 from echo_buffer[] and clear it.
7713 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7714 suitable buffer from echo_buffer[] and clear it.
7716 Value is what FN returns. */
7718 static int
7719 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7720 struct window *w;
7721 int which;
7722 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7723 EMACS_INT a1;
7724 Lisp_Object a2;
7725 EMACS_INT a3, a4;
7727 Lisp_Object buffer;
7728 int this_one, the_other, clear_buffer_p, rc;
7729 int count = SPECPDL_INDEX ();
7731 /* If buffers aren't live, make new ones. */
7732 ensure_echo_area_buffers ();
7734 clear_buffer_p = 0;
7736 if (which == 0)
7737 this_one = 0, the_other = 1;
7738 else if (which > 0)
7739 this_one = 1, the_other = 0;
7741 /* Choose a suitable buffer from echo_buffer[] is we don't
7742 have one. */
7743 if (NILP (echo_area_buffer[this_one]))
7745 echo_area_buffer[this_one]
7746 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7747 ? echo_buffer[the_other]
7748 : echo_buffer[this_one]);
7749 clear_buffer_p = 1;
7752 buffer = echo_area_buffer[this_one];
7754 /* Don't get confused by reusing the buffer used for echoing
7755 for a different purpose. */
7756 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7757 cancel_echoing ();
7759 record_unwind_protect (unwind_with_echo_area_buffer,
7760 with_echo_area_buffer_unwind_data (w));
7762 /* Make the echo area buffer current. Note that for display
7763 purposes, it is not necessary that the displayed window's buffer
7764 == current_buffer, except for text property lookup. So, let's
7765 only set that buffer temporarily here without doing a full
7766 Fset_window_buffer. We must also change w->pointm, though,
7767 because otherwise an assertions in unshow_buffer fails, and Emacs
7768 aborts. */
7769 set_buffer_internal_1 (XBUFFER (buffer));
7770 if (w)
7772 w->buffer = buffer;
7773 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7776 current_buffer->undo_list = Qt;
7777 current_buffer->read_only = Qnil;
7778 specbind (Qinhibit_read_only, Qt);
7779 specbind (Qinhibit_modification_hooks, Qt);
7781 if (clear_buffer_p && Z > BEG)
7782 del_range (BEG, Z);
7784 xassert (BEGV >= BEG);
7785 xassert (ZV <= Z && ZV >= BEGV);
7787 rc = fn (a1, a2, a3, a4);
7789 xassert (BEGV >= BEG);
7790 xassert (ZV <= Z && ZV >= BEGV);
7792 unbind_to (count, Qnil);
7793 return rc;
7797 /* Save state that should be preserved around the call to the function
7798 FN called in with_echo_area_buffer. */
7800 static Lisp_Object
7801 with_echo_area_buffer_unwind_data (w)
7802 struct window *w;
7804 int i = 0;
7805 Lisp_Object vector;
7807 /* Reduce consing by keeping one vector in
7808 Vwith_echo_area_save_vector. */
7809 vector = Vwith_echo_area_save_vector;
7810 Vwith_echo_area_save_vector = Qnil;
7812 if (NILP (vector))
7813 vector = Fmake_vector (make_number (7), Qnil);
7815 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7816 AREF (vector, i) = Vdeactivate_mark, ++i;
7817 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7819 if (w)
7821 XSETWINDOW (AREF (vector, i), w); ++i;
7822 AREF (vector, i) = w->buffer; ++i;
7823 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7824 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7826 else
7828 int end = i + 4;
7829 for (; i < end; ++i)
7830 AREF (vector, i) = Qnil;
7833 xassert (i == ASIZE (vector));
7834 return vector;
7838 /* Restore global state from VECTOR which was created by
7839 with_echo_area_buffer_unwind_data. */
7841 static Lisp_Object
7842 unwind_with_echo_area_buffer (vector)
7843 Lisp_Object vector;
7845 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7846 Vdeactivate_mark = AREF (vector, 1);
7847 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7849 if (WINDOWP (AREF (vector, 3)))
7851 struct window *w;
7852 Lisp_Object buffer, charpos, bytepos;
7854 w = XWINDOW (AREF (vector, 3));
7855 buffer = AREF (vector, 4);
7856 charpos = AREF (vector, 5);
7857 bytepos = AREF (vector, 6);
7859 w->buffer = buffer;
7860 set_marker_both (w->pointm, buffer,
7861 XFASTINT (charpos), XFASTINT (bytepos));
7864 Vwith_echo_area_save_vector = vector;
7865 return Qnil;
7869 /* Set up the echo area for use by print functions. MULTIBYTE_P
7870 non-zero means we will print multibyte. */
7872 void
7873 setup_echo_area_for_printing (multibyte_p)
7874 int multibyte_p;
7876 /* If we can't find an echo area any more, exit. */
7877 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7878 Fkill_emacs (Qnil);
7880 ensure_echo_area_buffers ();
7882 if (!message_buf_print)
7884 /* A message has been output since the last time we printed.
7885 Choose a fresh echo area buffer. */
7886 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7887 echo_area_buffer[0] = echo_buffer[1];
7888 else
7889 echo_area_buffer[0] = echo_buffer[0];
7891 /* Switch to that buffer and clear it. */
7892 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7893 current_buffer->truncate_lines = Qnil;
7895 if (Z > BEG)
7897 int count = SPECPDL_INDEX ();
7898 specbind (Qinhibit_read_only, Qt);
7899 /* Note that undo recording is always disabled. */
7900 del_range (BEG, Z);
7901 unbind_to (count, Qnil);
7903 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7905 /* Set up the buffer for the multibyteness we need. */
7906 if (multibyte_p
7907 != !NILP (current_buffer->enable_multibyte_characters))
7908 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7910 /* Raise the frame containing the echo area. */
7911 if (minibuffer_auto_raise)
7913 struct frame *sf = SELECTED_FRAME ();
7914 Lisp_Object mini_window;
7915 mini_window = FRAME_MINIBUF_WINDOW (sf);
7916 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7919 message_log_maybe_newline ();
7920 message_buf_print = 1;
7922 else
7924 if (NILP (echo_area_buffer[0]))
7926 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7927 echo_area_buffer[0] = echo_buffer[1];
7928 else
7929 echo_area_buffer[0] = echo_buffer[0];
7932 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7934 /* Someone switched buffers between print requests. */
7935 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7936 current_buffer->truncate_lines = Qnil;
7942 /* Display an echo area message in window W. Value is non-zero if W's
7943 height is changed. If display_last_displayed_message_p is
7944 non-zero, display the message that was last displayed, otherwise
7945 display the current message. */
7947 static int
7948 display_echo_area (w)
7949 struct window *w;
7951 int i, no_message_p, window_height_changed_p, count;
7953 /* Temporarily disable garbage collections while displaying the echo
7954 area. This is done because a GC can print a message itself.
7955 That message would modify the echo area buffer's contents while a
7956 redisplay of the buffer is going on, and seriously confuse
7957 redisplay. */
7958 count = inhibit_garbage_collection ();
7960 /* If there is no message, we must call display_echo_area_1
7961 nevertheless because it resizes the window. But we will have to
7962 reset the echo_area_buffer in question to nil at the end because
7963 with_echo_area_buffer will sets it to an empty buffer. */
7964 i = display_last_displayed_message_p ? 1 : 0;
7965 no_message_p = NILP (echo_area_buffer[i]);
7967 window_height_changed_p
7968 = with_echo_area_buffer (w, display_last_displayed_message_p,
7969 display_echo_area_1,
7970 (EMACS_INT) w, Qnil, 0, 0);
7972 if (no_message_p)
7973 echo_area_buffer[i] = Qnil;
7975 unbind_to (count, Qnil);
7976 return window_height_changed_p;
7980 /* Helper for display_echo_area. Display the current buffer which
7981 contains the current echo area message in window W, a mini-window,
7982 a pointer to which is passed in A1. A2..A4 are currently not used.
7983 Change the height of W so that all of the message is displayed.
7984 Value is non-zero if height of W was changed. */
7986 static int
7987 display_echo_area_1 (a1, a2, a3, a4)
7988 EMACS_INT a1;
7989 Lisp_Object a2;
7990 EMACS_INT a3, a4;
7992 struct window *w = (struct window *) a1;
7993 Lisp_Object window;
7994 struct text_pos start;
7995 int window_height_changed_p = 0;
7997 /* Do this before displaying, so that we have a large enough glyph
7998 matrix for the display. If we can't get enough space for the
7999 whole text, display the last N lines. That works by setting w->start. */
8000 window_height_changed_p = resize_mini_window (w, 0);
8002 /* Use the starting position chosen by resize_mini_window. */
8003 SET_TEXT_POS_FROM_MARKER (start, w->start);
8005 /* Display. */
8006 clear_glyph_matrix (w->desired_matrix);
8007 XSETWINDOW (window, w);
8008 try_window (window, start, 0);
8010 return window_height_changed_p;
8014 /* Resize the echo area window to exactly the size needed for the
8015 currently displayed message, if there is one. If a mini-buffer
8016 is active, don't shrink it. */
8018 void
8019 resize_echo_area_exactly ()
8021 if (BUFFERP (echo_area_buffer[0])
8022 && WINDOWP (echo_area_window))
8024 struct window *w = XWINDOW (echo_area_window);
8025 int resized_p;
8026 Lisp_Object resize_exactly;
8028 if (minibuf_level == 0)
8029 resize_exactly = Qt;
8030 else
8031 resize_exactly = Qnil;
8033 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8034 (EMACS_INT) w, resize_exactly, 0, 0);
8035 if (resized_p)
8037 ++windows_or_buffers_changed;
8038 ++update_mode_lines;
8039 redisplay_internal (0);
8045 /* Callback function for with_echo_area_buffer, when used from
8046 resize_echo_area_exactly. A1 contains a pointer to the window to
8047 resize, EXACTLY non-nil means resize the mini-window exactly to the
8048 size of the text displayed. A3 and A4 are not used. Value is what
8049 resize_mini_window returns. */
8051 static int
8052 resize_mini_window_1 (a1, exactly, a3, a4)
8053 EMACS_INT a1;
8054 Lisp_Object exactly;
8055 EMACS_INT a3, a4;
8057 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8061 /* Resize mini-window W to fit the size of its contents. EXACT:P
8062 means size the window exactly to the size needed. Otherwise, it's
8063 only enlarged until W's buffer is empty.
8065 Set W->start to the right place to begin display. If the whole
8066 contents fit, start at the beginning. Otherwise, start so as
8067 to make the end of the contents appear. This is particularly
8068 important for y-or-n-p, but seems desirable generally.
8070 Value is non-zero if the window height has been changed. */
8073 resize_mini_window (w, exact_p)
8074 struct window *w;
8075 int exact_p;
8077 struct frame *f = XFRAME (w->frame);
8078 int window_height_changed_p = 0;
8080 xassert (MINI_WINDOW_P (w));
8082 /* By default, start display at the beginning. */
8083 set_marker_both (w->start, w->buffer,
8084 BUF_BEGV (XBUFFER (w->buffer)),
8085 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8087 /* Don't resize windows while redisplaying a window; it would
8088 confuse redisplay functions when the size of the window they are
8089 displaying changes from under them. Such a resizing can happen,
8090 for instance, when which-func prints a long message while
8091 we are running fontification-functions. We're running these
8092 functions with safe_call which binds inhibit-redisplay to t. */
8093 if (!NILP (Vinhibit_redisplay))
8094 return 0;
8096 /* Nil means don't try to resize. */
8097 if (NILP (Vresize_mini_windows)
8098 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8099 return 0;
8101 if (!FRAME_MINIBUF_ONLY_P (f))
8103 struct it it;
8104 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8105 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8106 int height, max_height;
8107 int unit = FRAME_LINE_HEIGHT (f);
8108 struct text_pos start;
8109 struct buffer *old_current_buffer = NULL;
8111 if (current_buffer != XBUFFER (w->buffer))
8113 old_current_buffer = current_buffer;
8114 set_buffer_internal (XBUFFER (w->buffer));
8117 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8119 /* Compute the max. number of lines specified by the user. */
8120 if (FLOATP (Vmax_mini_window_height))
8121 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8122 else if (INTEGERP (Vmax_mini_window_height))
8123 max_height = XINT (Vmax_mini_window_height);
8124 else
8125 max_height = total_height / 4;
8127 /* Correct that max. height if it's bogus. */
8128 max_height = max (1, max_height);
8129 max_height = min (total_height, max_height);
8131 /* Find out the height of the text in the window. */
8132 if (it.truncate_lines_p)
8133 height = 1;
8134 else
8136 last_height = 0;
8137 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8138 if (it.max_ascent == 0 && it.max_descent == 0)
8139 height = it.current_y + last_height;
8140 else
8141 height = it.current_y + it.max_ascent + it.max_descent;
8142 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8143 height = (height + unit - 1) / unit;
8146 /* Compute a suitable window start. */
8147 if (height > max_height)
8149 height = max_height;
8150 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8151 move_it_vertically_backward (&it, (height - 1) * unit);
8152 start = it.current.pos;
8154 else
8155 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8156 SET_MARKER_FROM_TEXT_POS (w->start, start);
8158 if (EQ (Vresize_mini_windows, Qgrow_only))
8160 /* Let it grow only, until we display an empty message, in which
8161 case the window shrinks again. */
8162 if (height > WINDOW_TOTAL_LINES (w))
8164 int old_height = WINDOW_TOTAL_LINES (w);
8165 freeze_window_starts (f, 1);
8166 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8167 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8169 else if (height < WINDOW_TOTAL_LINES (w)
8170 && (exact_p || BEGV == ZV))
8172 int old_height = WINDOW_TOTAL_LINES (w);
8173 freeze_window_starts (f, 0);
8174 shrink_mini_window (w);
8175 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8178 else
8180 /* Always resize to exact size needed. */
8181 if (height > WINDOW_TOTAL_LINES (w))
8183 int old_height = WINDOW_TOTAL_LINES (w);
8184 freeze_window_starts (f, 1);
8185 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8186 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8188 else if (height < WINDOW_TOTAL_LINES (w))
8190 int old_height = WINDOW_TOTAL_LINES (w);
8191 freeze_window_starts (f, 0);
8192 shrink_mini_window (w);
8194 if (height)
8196 freeze_window_starts (f, 1);
8197 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8200 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8204 if (old_current_buffer)
8205 set_buffer_internal (old_current_buffer);
8208 return window_height_changed_p;
8212 /* Value is the current message, a string, or nil if there is no
8213 current message. */
8215 Lisp_Object
8216 current_message ()
8218 Lisp_Object msg;
8220 if (NILP (echo_area_buffer[0]))
8221 msg = Qnil;
8222 else
8224 with_echo_area_buffer (0, 0, current_message_1,
8225 (EMACS_INT) &msg, Qnil, 0, 0);
8226 if (NILP (msg))
8227 echo_area_buffer[0] = Qnil;
8230 return msg;
8234 static int
8235 current_message_1 (a1, a2, a3, a4)
8236 EMACS_INT a1;
8237 Lisp_Object a2;
8238 EMACS_INT a3, a4;
8240 Lisp_Object *msg = (Lisp_Object *) a1;
8242 if (Z > BEG)
8243 *msg = make_buffer_string (BEG, Z, 1);
8244 else
8245 *msg = Qnil;
8246 return 0;
8250 /* Push the current message on Vmessage_stack for later restauration
8251 by restore_message. Value is non-zero if the current message isn't
8252 empty. This is a relatively infrequent operation, so it's not
8253 worth optimizing. */
8256 push_message ()
8258 Lisp_Object msg;
8259 msg = current_message ();
8260 Vmessage_stack = Fcons (msg, Vmessage_stack);
8261 return STRINGP (msg);
8265 /* Restore message display from the top of Vmessage_stack. */
8267 void
8268 restore_message ()
8270 Lisp_Object msg;
8272 xassert (CONSP (Vmessage_stack));
8273 msg = XCAR (Vmessage_stack);
8274 if (STRINGP (msg))
8275 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8276 else
8277 message3_nolog (msg, 0, 0);
8281 /* Handler for record_unwind_protect calling pop_message. */
8283 Lisp_Object
8284 pop_message_unwind (dummy)
8285 Lisp_Object dummy;
8287 pop_message ();
8288 return Qnil;
8291 /* Pop the top-most entry off Vmessage_stack. */
8293 void
8294 pop_message ()
8296 xassert (CONSP (Vmessage_stack));
8297 Vmessage_stack = XCDR (Vmessage_stack);
8301 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8302 exits. If the stack is not empty, we have a missing pop_message
8303 somewhere. */
8305 void
8306 check_message_stack ()
8308 if (!NILP (Vmessage_stack))
8309 abort ();
8313 /* Truncate to NCHARS what will be displayed in the echo area the next
8314 time we display it---but don't redisplay it now. */
8316 void
8317 truncate_echo_area (nchars)
8318 int nchars;
8320 if (nchars == 0)
8321 echo_area_buffer[0] = Qnil;
8322 /* A null message buffer means that the frame hasn't really been
8323 initialized yet. Error messages get reported properly by
8324 cmd_error, so this must be just an informative message; toss it. */
8325 else if (!noninteractive
8326 && INTERACTIVE
8327 && !NILP (echo_area_buffer[0]))
8329 struct frame *sf = SELECTED_FRAME ();
8330 if (FRAME_MESSAGE_BUF (sf))
8331 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8336 /* Helper function for truncate_echo_area. Truncate the current
8337 message to at most NCHARS characters. */
8339 static int
8340 truncate_message_1 (nchars, a2, a3, a4)
8341 EMACS_INT nchars;
8342 Lisp_Object a2;
8343 EMACS_INT a3, a4;
8345 if (BEG + nchars < Z)
8346 del_range (BEG + nchars, Z);
8347 if (Z == BEG)
8348 echo_area_buffer[0] = Qnil;
8349 return 0;
8353 /* Set the current message to a substring of S or STRING.
8355 If STRING is a Lisp string, set the message to the first NBYTES
8356 bytes from STRING. NBYTES zero means use the whole string. If
8357 STRING is multibyte, the message will be displayed multibyte.
8359 If S is not null, set the message to the first LEN bytes of S. LEN
8360 zero means use the whole string. MULTIBYTE_P non-zero means S is
8361 multibyte. Display the message multibyte in that case.
8363 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8364 to t before calling set_message_1 (which calls insert).
8367 void
8368 set_message (s, string, nbytes, multibyte_p)
8369 const char *s;
8370 Lisp_Object string;
8371 int nbytes, multibyte_p;
8373 message_enable_multibyte
8374 = ((s && multibyte_p)
8375 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8377 with_echo_area_buffer (0, 0, set_message_1,
8378 (EMACS_INT) s, string, nbytes, multibyte_p);
8379 message_buf_print = 0;
8380 help_echo_showing_p = 0;
8384 /* Helper function for set_message. Arguments have the same meaning
8385 as there, with A1 corresponding to S and A2 corresponding to STRING
8386 This function is called with the echo area buffer being
8387 current. */
8389 static int
8390 set_message_1 (a1, a2, nbytes, multibyte_p)
8391 EMACS_INT a1;
8392 Lisp_Object a2;
8393 EMACS_INT nbytes, multibyte_p;
8395 const char *s = (const char *) a1;
8396 Lisp_Object string = a2;
8398 /* Change multibyteness of the echo buffer appropriately. */
8399 if (message_enable_multibyte
8400 != !NILP (current_buffer->enable_multibyte_characters))
8401 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8403 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8405 /* Insert new message at BEG. */
8406 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8407 Ferase_buffer ();
8409 if (STRINGP (string))
8411 int nchars;
8413 if (nbytes == 0)
8414 nbytes = SBYTES (string);
8415 nchars = string_byte_to_char (string, nbytes);
8417 /* This function takes care of single/multibyte conversion. We
8418 just have to ensure that the echo area buffer has the right
8419 setting of enable_multibyte_characters. */
8420 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8422 else if (s)
8424 if (nbytes == 0)
8425 nbytes = strlen (s);
8427 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8429 /* Convert from multi-byte to single-byte. */
8430 int i, c, n;
8431 unsigned char work[1];
8433 /* Convert a multibyte string to single-byte. */
8434 for (i = 0; i < nbytes; i += n)
8436 c = string_char_and_length (s + i, nbytes - i, &n);
8437 work[0] = (SINGLE_BYTE_CHAR_P (c)
8439 : multibyte_char_to_unibyte (c, Qnil));
8440 insert_1_both (work, 1, 1, 1, 0, 0);
8443 else if (!multibyte_p
8444 && !NILP (current_buffer->enable_multibyte_characters))
8446 /* Convert from single-byte to multi-byte. */
8447 int i, c, n;
8448 const unsigned char *msg = (const unsigned char *) s;
8449 unsigned char str[MAX_MULTIBYTE_LENGTH];
8451 /* Convert a single-byte string to multibyte. */
8452 for (i = 0; i < nbytes; i++)
8454 c = unibyte_char_to_multibyte (msg[i]);
8455 n = CHAR_STRING (c, str);
8456 insert_1_both (str, 1, n, 1, 0, 0);
8459 else
8460 insert_1 (s, nbytes, 1, 0, 0);
8463 return 0;
8467 /* Clear messages. CURRENT_P non-zero means clear the current
8468 message. LAST_DISPLAYED_P non-zero means clear the message
8469 last displayed. */
8471 void
8472 clear_message (current_p, last_displayed_p)
8473 int current_p, last_displayed_p;
8475 if (current_p)
8477 echo_area_buffer[0] = Qnil;
8478 message_cleared_p = 1;
8481 if (last_displayed_p)
8482 echo_area_buffer[1] = Qnil;
8484 message_buf_print = 0;
8487 /* Clear garbaged frames.
8489 This function is used where the old redisplay called
8490 redraw_garbaged_frames which in turn called redraw_frame which in
8491 turn called clear_frame. The call to clear_frame was a source of
8492 flickering. I believe a clear_frame is not necessary. It should
8493 suffice in the new redisplay to invalidate all current matrices,
8494 and ensure a complete redisplay of all windows. */
8496 static void
8497 clear_garbaged_frames ()
8499 if (frame_garbaged)
8501 Lisp_Object tail, frame;
8502 int changed_count = 0;
8504 FOR_EACH_FRAME (tail, frame)
8506 struct frame *f = XFRAME (frame);
8508 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8510 if (f->resized_p)
8512 Fredraw_frame (frame);
8513 f->force_flush_display_p = 1;
8515 clear_current_matrices (f);
8516 changed_count++;
8517 f->garbaged = 0;
8518 f->resized_p = 0;
8522 frame_garbaged = 0;
8523 if (changed_count)
8524 ++windows_or_buffers_changed;
8529 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8530 is non-zero update selected_frame. Value is non-zero if the
8531 mini-windows height has been changed. */
8533 static int
8534 echo_area_display (update_frame_p)
8535 int update_frame_p;
8537 Lisp_Object mini_window;
8538 struct window *w;
8539 struct frame *f;
8540 int window_height_changed_p = 0;
8541 struct frame *sf = SELECTED_FRAME ();
8543 mini_window = FRAME_MINIBUF_WINDOW (sf);
8544 w = XWINDOW (mini_window);
8545 f = XFRAME (WINDOW_FRAME (w));
8547 /* Don't display if frame is invisible or not yet initialized. */
8548 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8549 return 0;
8551 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8552 #ifndef MAC_OS8
8553 #ifdef HAVE_WINDOW_SYSTEM
8554 /* When Emacs starts, selected_frame may be a visible terminal
8555 frame, even if we run under a window system. If we let this
8556 through, a message would be displayed on the terminal. */
8557 if (EQ (selected_frame, Vterminal_frame)
8558 && !NILP (Vwindow_system))
8559 return 0;
8560 #endif /* HAVE_WINDOW_SYSTEM */
8561 #endif
8563 /* Redraw garbaged frames. */
8564 if (frame_garbaged)
8565 clear_garbaged_frames ();
8567 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8569 echo_area_window = mini_window;
8570 window_height_changed_p = display_echo_area (w);
8571 w->must_be_updated_p = 1;
8573 /* Update the display, unless called from redisplay_internal.
8574 Also don't update the screen during redisplay itself. The
8575 update will happen at the end of redisplay, and an update
8576 here could cause confusion. */
8577 if (update_frame_p && !redisplaying_p)
8579 int n = 0;
8581 /* If the display update has been interrupted by pending
8582 input, update mode lines in the frame. Due to the
8583 pending input, it might have been that redisplay hasn't
8584 been called, so that mode lines above the echo area are
8585 garbaged. This looks odd, so we prevent it here. */
8586 if (!display_completed)
8587 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8589 if (window_height_changed_p
8590 /* Don't do this if Emacs is shutting down. Redisplay
8591 needs to run hooks. */
8592 && !NILP (Vrun_hooks))
8594 /* Must update other windows. Likewise as in other
8595 cases, don't let this update be interrupted by
8596 pending input. */
8597 int count = SPECPDL_INDEX ();
8598 specbind (Qredisplay_dont_pause, Qt);
8599 windows_or_buffers_changed = 1;
8600 redisplay_internal (0);
8601 unbind_to (count, Qnil);
8603 else if (FRAME_WINDOW_P (f) && n == 0)
8605 /* Window configuration is the same as before.
8606 Can do with a display update of the echo area,
8607 unless we displayed some mode lines. */
8608 update_single_window (w, 1);
8609 rif->flush_display (f);
8611 else
8612 update_frame (f, 1, 1);
8614 /* If cursor is in the echo area, make sure that the next
8615 redisplay displays the minibuffer, so that the cursor will
8616 be replaced with what the minibuffer wants. */
8617 if (cursor_in_echo_area)
8618 ++windows_or_buffers_changed;
8621 else if (!EQ (mini_window, selected_window))
8622 windows_or_buffers_changed++;
8624 /* The current message is now also the last one displayed. */
8625 echo_area_buffer[1] = echo_area_buffer[0];
8627 /* Prevent redisplay optimization in redisplay_internal by resetting
8628 this_line_start_pos. This is done because the mini-buffer now
8629 displays the message instead of its buffer text. */
8630 if (EQ (mini_window, selected_window))
8631 CHARPOS (this_line_start_pos) = 0;
8633 return window_height_changed_p;
8638 /***********************************************************************
8639 Mode Lines and Frame Titles
8640 ***********************************************************************/
8642 /* A buffer for constructing non-propertized mode-line strings and
8643 frame titles in it; allocated from the heap in init_xdisp and
8644 resized as needed in store_mode_line_noprop_char. */
8646 static char *mode_line_noprop_buf;
8648 /* The buffer's end, and a current output position in it. */
8650 static char *mode_line_noprop_buf_end;
8651 static char *mode_line_noprop_ptr;
8653 #define MODE_LINE_NOPROP_LEN(start) \
8654 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8656 static enum {
8657 MODE_LINE_DISPLAY = 0,
8658 MODE_LINE_TITLE,
8659 MODE_LINE_NOPROP,
8660 MODE_LINE_STRING
8661 } mode_line_target;
8663 /* Alist that caches the results of :propertize.
8664 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8665 static Lisp_Object mode_line_proptrans_alist;
8667 /* List of strings making up the mode-line. */
8668 static Lisp_Object mode_line_string_list;
8670 /* Base face property when building propertized mode line string. */
8671 static Lisp_Object mode_line_string_face;
8672 static Lisp_Object mode_line_string_face_prop;
8675 /* Unwind data for mode line strings */
8677 static Lisp_Object Vmode_line_unwind_vector;
8679 static Lisp_Object
8680 format_mode_line_unwind_data (obuf, save_proptrans)
8681 struct buffer *obuf;
8683 Lisp_Object vector;
8685 /* Reduce consing by keeping one vector in
8686 Vwith_echo_area_save_vector. */
8687 vector = Vmode_line_unwind_vector;
8688 Vmode_line_unwind_vector = Qnil;
8690 if (NILP (vector))
8691 vector = Fmake_vector (make_number (7), Qnil);
8693 AREF (vector, 0) = make_number (mode_line_target);
8694 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8695 AREF (vector, 2) = mode_line_string_list;
8696 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8697 AREF (vector, 4) = mode_line_string_face;
8698 AREF (vector, 5) = mode_line_string_face_prop;
8700 if (obuf)
8701 XSETBUFFER (AREF (vector, 6), obuf);
8702 else
8703 AREF (vector, 6) = Qnil;
8705 return vector;
8708 static Lisp_Object
8709 unwind_format_mode_line (vector)
8710 Lisp_Object vector;
8712 mode_line_target = XINT (AREF (vector, 0));
8713 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8714 mode_line_string_list = AREF (vector, 2);
8715 if (! EQ (AREF (vector, 3), Qt))
8716 mode_line_proptrans_alist = AREF (vector, 3);
8717 mode_line_string_face = AREF (vector, 4);
8718 mode_line_string_face_prop = AREF (vector, 5);
8720 if (!NILP (AREF (vector, 6)))
8722 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8723 AREF (vector, 6) = Qnil;
8726 Vmode_line_unwind_vector = vector;
8727 return Qnil;
8731 /* Store a single character C for the frame title in mode_line_noprop_buf.
8732 Re-allocate mode_line_noprop_buf if necessary. */
8734 static void
8735 #ifdef PROTOTYPES
8736 store_mode_line_noprop_char (char c)
8737 #else
8738 store_mode_line_noprop_char (c)
8739 char c;
8740 #endif
8742 /* If output position has reached the end of the allocated buffer,
8743 double the buffer's size. */
8744 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8746 int len = MODE_LINE_NOPROP_LEN (0);
8747 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8748 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8749 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8750 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8753 *mode_line_noprop_ptr++ = c;
8757 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8758 mode_line_noprop_ptr. STR is the string to store. Do not copy
8759 characters that yield more columns than PRECISION; PRECISION <= 0
8760 means copy the whole string. Pad with spaces until FIELD_WIDTH
8761 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8762 pad. Called from display_mode_element when it is used to build a
8763 frame title. */
8765 static int
8766 store_mode_line_noprop (str, field_width, precision)
8767 const unsigned char *str;
8768 int field_width, precision;
8770 int n = 0;
8771 int dummy, nbytes;
8773 /* Copy at most PRECISION chars from STR. */
8774 nbytes = strlen (str);
8775 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8776 while (nbytes--)
8777 store_mode_line_noprop_char (*str++);
8779 /* Fill up with spaces until FIELD_WIDTH reached. */
8780 while (field_width > 0
8781 && n < field_width)
8783 store_mode_line_noprop_char (' ');
8784 ++n;
8787 return n;
8790 /***********************************************************************
8791 Frame Titles
8792 ***********************************************************************/
8794 #ifdef HAVE_WINDOW_SYSTEM
8796 /* Set the title of FRAME, if it has changed. The title format is
8797 Vicon_title_format if FRAME is iconified, otherwise it is
8798 frame_title_format. */
8800 static void
8801 x_consider_frame_title (frame)
8802 Lisp_Object frame;
8804 struct frame *f = XFRAME (frame);
8806 if (FRAME_WINDOW_P (f)
8807 || FRAME_MINIBUF_ONLY_P (f)
8808 || f->explicit_name)
8810 /* Do we have more than one visible frame on this X display? */
8811 Lisp_Object tail;
8812 Lisp_Object fmt;
8813 int title_start;
8814 char *title;
8815 int len;
8816 struct it it;
8817 int count = SPECPDL_INDEX ();
8819 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8821 Lisp_Object other_frame = XCAR (tail);
8822 struct frame *tf = XFRAME (other_frame);
8824 if (tf != f
8825 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8826 && !FRAME_MINIBUF_ONLY_P (tf)
8827 && !EQ (other_frame, tip_frame)
8828 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8829 break;
8832 /* Set global variable indicating that multiple frames exist. */
8833 multiple_frames = CONSP (tail);
8835 /* Switch to the buffer of selected window of the frame. Set up
8836 mode_line_target so that display_mode_element will output into
8837 mode_line_noprop_buf; then display the title. */
8838 record_unwind_protect (unwind_format_mode_line,
8839 format_mode_line_unwind_data (current_buffer, 0));
8841 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8842 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8844 mode_line_target = MODE_LINE_TITLE;
8845 title_start = MODE_LINE_NOPROP_LEN (0);
8846 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8847 NULL, DEFAULT_FACE_ID);
8848 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8849 len = MODE_LINE_NOPROP_LEN (title_start);
8850 title = mode_line_noprop_buf + title_start;
8851 unbind_to (count, Qnil);
8853 /* Set the title only if it's changed. This avoids consing in
8854 the common case where it hasn't. (If it turns out that we've
8855 already wasted too much time by walking through the list with
8856 display_mode_element, then we might need to optimize at a
8857 higher level than this.) */
8858 if (! STRINGP (f->name)
8859 || SBYTES (f->name) != len
8860 || bcmp (title, SDATA (f->name), len) != 0)
8861 x_implicitly_set_name (f, make_string (title, len), Qnil);
8865 #endif /* not HAVE_WINDOW_SYSTEM */
8870 /***********************************************************************
8871 Menu Bars
8872 ***********************************************************************/
8875 /* Prepare for redisplay by updating menu-bar item lists when
8876 appropriate. This can call eval. */
8878 void
8879 prepare_menu_bars ()
8881 int all_windows;
8882 struct gcpro gcpro1, gcpro2;
8883 struct frame *f;
8884 Lisp_Object tooltip_frame;
8886 #ifdef HAVE_WINDOW_SYSTEM
8887 tooltip_frame = tip_frame;
8888 #else
8889 tooltip_frame = Qnil;
8890 #endif
8892 /* Update all frame titles based on their buffer names, etc. We do
8893 this before the menu bars so that the buffer-menu will show the
8894 up-to-date frame titles. */
8895 #ifdef HAVE_WINDOW_SYSTEM
8896 if (windows_or_buffers_changed || update_mode_lines)
8898 Lisp_Object tail, frame;
8900 FOR_EACH_FRAME (tail, frame)
8902 f = XFRAME (frame);
8903 if (!EQ (frame, tooltip_frame)
8904 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8905 x_consider_frame_title (frame);
8908 #endif /* HAVE_WINDOW_SYSTEM */
8910 /* Update the menu bar item lists, if appropriate. This has to be
8911 done before any actual redisplay or generation of display lines. */
8912 all_windows = (update_mode_lines
8913 || buffer_shared > 1
8914 || windows_or_buffers_changed);
8915 if (all_windows)
8917 Lisp_Object tail, frame;
8918 int count = SPECPDL_INDEX ();
8920 record_unwind_save_match_data ();
8922 FOR_EACH_FRAME (tail, frame)
8924 f = XFRAME (frame);
8926 /* Ignore tooltip frame. */
8927 if (EQ (frame, tooltip_frame))
8928 continue;
8930 /* If a window on this frame changed size, report that to
8931 the user and clear the size-change flag. */
8932 if (FRAME_WINDOW_SIZES_CHANGED (f))
8934 Lisp_Object functions;
8936 /* Clear flag first in case we get an error below. */
8937 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8938 functions = Vwindow_size_change_functions;
8939 GCPRO2 (tail, functions);
8941 while (CONSP (functions))
8943 call1 (XCAR (functions), frame);
8944 functions = XCDR (functions);
8946 UNGCPRO;
8949 GCPRO1 (tail);
8950 update_menu_bar (f, 0);
8951 #ifdef HAVE_WINDOW_SYSTEM
8952 update_tool_bar (f, 0);
8953 #endif
8954 UNGCPRO;
8957 unbind_to (count, Qnil);
8959 else
8961 struct frame *sf = SELECTED_FRAME ();
8962 update_menu_bar (sf, 1);
8963 #ifdef HAVE_WINDOW_SYSTEM
8964 update_tool_bar (sf, 1);
8965 #endif
8968 /* Motif needs this. See comment in xmenu.c. Turn it off when
8969 pending_menu_activation is not defined. */
8970 #ifdef USE_X_TOOLKIT
8971 pending_menu_activation = 0;
8972 #endif
8976 /* Update the menu bar item list for frame F. This has to be done
8977 before we start to fill in any display lines, because it can call
8978 eval.
8980 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8982 static void
8983 update_menu_bar (f, save_match_data)
8984 struct frame *f;
8985 int save_match_data;
8987 Lisp_Object window;
8988 register struct window *w;
8990 /* If called recursively during a menu update, do nothing. This can
8991 happen when, for instance, an activate-menubar-hook causes a
8992 redisplay. */
8993 if (inhibit_menubar_update)
8994 return;
8996 window = FRAME_SELECTED_WINDOW (f);
8997 w = XWINDOW (window);
8999 #if 0 /* The if statement below this if statement used to include the
9000 condition !NILP (w->update_mode_line), rather than using
9001 update_mode_lines directly, and this if statement may have
9002 been added to make that condition work. Now the if
9003 statement below matches its comment, this isn't needed. */
9004 if (update_mode_lines)
9005 w->update_mode_line = Qt;
9006 #endif
9008 if (FRAME_WINDOW_P (f)
9010 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9011 || defined (USE_GTK)
9012 FRAME_EXTERNAL_MENU_BAR (f)
9013 #else
9014 FRAME_MENU_BAR_LINES (f) > 0
9015 #endif
9016 : FRAME_MENU_BAR_LINES (f) > 0)
9018 /* If the user has switched buffers or windows, we need to
9019 recompute to reflect the new bindings. But we'll
9020 recompute when update_mode_lines is set too; that means
9021 that people can use force-mode-line-update to request
9022 that the menu bar be recomputed. The adverse effect on
9023 the rest of the redisplay algorithm is about the same as
9024 windows_or_buffers_changed anyway. */
9025 if (windows_or_buffers_changed
9026 /* This used to test w->update_mode_line, but we believe
9027 there is no need to recompute the menu in that case. */
9028 || update_mode_lines
9029 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9030 < BUF_MODIFF (XBUFFER (w->buffer)))
9031 != !NILP (w->last_had_star))
9032 || ((!NILP (Vtransient_mark_mode)
9033 && !NILP (XBUFFER (w->buffer)->mark_active))
9034 != !NILP (w->region_showing)))
9036 struct buffer *prev = current_buffer;
9037 int count = SPECPDL_INDEX ();
9039 specbind (Qinhibit_menubar_update, Qt);
9041 set_buffer_internal_1 (XBUFFER (w->buffer));
9042 if (save_match_data)
9043 record_unwind_save_match_data ();
9044 if (NILP (Voverriding_local_map_menu_flag))
9046 specbind (Qoverriding_terminal_local_map, Qnil);
9047 specbind (Qoverriding_local_map, Qnil);
9050 /* Run the Lucid hook. */
9051 safe_run_hooks (Qactivate_menubar_hook);
9053 /* If it has changed current-menubar from previous value,
9054 really recompute the menu-bar from the value. */
9055 if (! NILP (Vlucid_menu_bar_dirty_flag))
9056 call0 (Qrecompute_lucid_menubar);
9058 safe_run_hooks (Qmenu_bar_update_hook);
9059 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9061 /* Redisplay the menu bar in case we changed it. */
9062 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9063 || defined (USE_GTK)
9064 if (FRAME_WINDOW_P (f))
9066 #ifdef MAC_OS
9067 /* All frames on Mac OS share the same menubar. So only
9068 the selected frame should be allowed to set it. */
9069 if (f == SELECTED_FRAME ())
9070 #endif
9071 set_frame_menubar (f, 0, 0);
9073 else
9074 /* On a terminal screen, the menu bar is an ordinary screen
9075 line, and this makes it get updated. */
9076 w->update_mode_line = Qt;
9077 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9078 /* In the non-toolkit version, the menu bar is an ordinary screen
9079 line, and this makes it get updated. */
9080 w->update_mode_line = Qt;
9081 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9083 unbind_to (count, Qnil);
9084 set_buffer_internal_1 (prev);
9091 /***********************************************************************
9092 Output Cursor
9093 ***********************************************************************/
9095 #ifdef HAVE_WINDOW_SYSTEM
9097 /* EXPORT:
9098 Nominal cursor position -- where to draw output.
9099 HPOS and VPOS are window relative glyph matrix coordinates.
9100 X and Y are window relative pixel coordinates. */
9102 struct cursor_pos output_cursor;
9105 /* EXPORT:
9106 Set the global variable output_cursor to CURSOR. All cursor
9107 positions are relative to updated_window. */
9109 void
9110 set_output_cursor (cursor)
9111 struct cursor_pos *cursor;
9113 output_cursor.hpos = cursor->hpos;
9114 output_cursor.vpos = cursor->vpos;
9115 output_cursor.x = cursor->x;
9116 output_cursor.y = cursor->y;
9120 /* EXPORT for RIF:
9121 Set a nominal cursor position.
9123 HPOS and VPOS are column/row positions in a window glyph matrix. X
9124 and Y are window text area relative pixel positions.
9126 If this is done during an update, updated_window will contain the
9127 window that is being updated and the position is the future output
9128 cursor position for that window. If updated_window is null, use
9129 selected_window and display the cursor at the given position. */
9131 void
9132 x_cursor_to (vpos, hpos, y, x)
9133 int vpos, hpos, y, x;
9135 struct window *w;
9137 /* If updated_window is not set, work on selected_window. */
9138 if (updated_window)
9139 w = updated_window;
9140 else
9141 w = XWINDOW (selected_window);
9143 /* Set the output cursor. */
9144 output_cursor.hpos = hpos;
9145 output_cursor.vpos = vpos;
9146 output_cursor.x = x;
9147 output_cursor.y = y;
9149 /* If not called as part of an update, really display the cursor.
9150 This will also set the cursor position of W. */
9151 if (updated_window == NULL)
9153 BLOCK_INPUT;
9154 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9155 if (rif->flush_display_optional)
9156 rif->flush_display_optional (SELECTED_FRAME ());
9157 UNBLOCK_INPUT;
9161 #endif /* HAVE_WINDOW_SYSTEM */
9164 /***********************************************************************
9165 Tool-bars
9166 ***********************************************************************/
9168 #ifdef HAVE_WINDOW_SYSTEM
9170 /* Where the mouse was last time we reported a mouse event. */
9172 FRAME_PTR last_mouse_frame;
9174 /* Tool-bar item index of the item on which a mouse button was pressed
9175 or -1. */
9177 int last_tool_bar_item;
9180 /* Update the tool-bar item list for frame F. This has to be done
9181 before we start to fill in any display lines. Called from
9182 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9183 and restore it here. */
9185 static void
9186 update_tool_bar (f, save_match_data)
9187 struct frame *f;
9188 int save_match_data;
9190 #ifdef USE_GTK
9191 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9192 #else
9193 int do_update = WINDOWP (f->tool_bar_window)
9194 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9195 #endif
9197 if (do_update)
9199 Lisp_Object window;
9200 struct window *w;
9202 window = FRAME_SELECTED_WINDOW (f);
9203 w = XWINDOW (window);
9205 /* If the user has switched buffers or windows, we need to
9206 recompute to reflect the new bindings. But we'll
9207 recompute when update_mode_lines is set too; that means
9208 that people can use force-mode-line-update to request
9209 that the menu bar be recomputed. The adverse effect on
9210 the rest of the redisplay algorithm is about the same as
9211 windows_or_buffers_changed anyway. */
9212 if (windows_or_buffers_changed
9213 || !NILP (w->update_mode_line)
9214 || update_mode_lines
9215 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9216 < BUF_MODIFF (XBUFFER (w->buffer)))
9217 != !NILP (w->last_had_star))
9218 || ((!NILP (Vtransient_mark_mode)
9219 && !NILP (XBUFFER (w->buffer)->mark_active))
9220 != !NILP (w->region_showing)))
9222 struct buffer *prev = current_buffer;
9223 int count = SPECPDL_INDEX ();
9224 Lisp_Object new_tool_bar;
9225 int new_n_tool_bar;
9226 struct gcpro gcpro1;
9228 /* Set current_buffer to the buffer of the selected
9229 window of the frame, so that we get the right local
9230 keymaps. */
9231 set_buffer_internal_1 (XBUFFER (w->buffer));
9233 /* Save match data, if we must. */
9234 if (save_match_data)
9235 record_unwind_save_match_data ();
9237 /* Make sure that we don't accidentally use bogus keymaps. */
9238 if (NILP (Voverriding_local_map_menu_flag))
9240 specbind (Qoverriding_terminal_local_map, Qnil);
9241 specbind (Qoverriding_local_map, Qnil);
9244 GCPRO1 (new_tool_bar);
9246 /* Build desired tool-bar items from keymaps. */
9247 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9248 &new_n_tool_bar);
9250 /* Redisplay the tool-bar if we changed it. */
9251 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9253 /* Redisplay that happens asynchronously due to an expose event
9254 may access f->tool_bar_items. Make sure we update both
9255 variables within BLOCK_INPUT so no such event interrupts. */
9256 BLOCK_INPUT;
9257 f->tool_bar_items = new_tool_bar;
9258 f->n_tool_bar_items = new_n_tool_bar;
9259 w->update_mode_line = Qt;
9260 UNBLOCK_INPUT;
9263 UNGCPRO;
9265 unbind_to (count, Qnil);
9266 set_buffer_internal_1 (prev);
9272 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9273 F's desired tool-bar contents. F->tool_bar_items must have
9274 been set up previously by calling prepare_menu_bars. */
9276 static void
9277 build_desired_tool_bar_string (f)
9278 struct frame *f;
9280 int i, size, size_needed;
9281 struct gcpro gcpro1, gcpro2, gcpro3;
9282 Lisp_Object image, plist, props;
9284 image = plist = props = Qnil;
9285 GCPRO3 (image, plist, props);
9287 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9288 Otherwise, make a new string. */
9290 /* The size of the string we might be able to reuse. */
9291 size = (STRINGP (f->desired_tool_bar_string)
9292 ? SCHARS (f->desired_tool_bar_string)
9293 : 0);
9295 /* We need one space in the string for each image. */
9296 size_needed = f->n_tool_bar_items;
9298 /* Reuse f->desired_tool_bar_string, if possible. */
9299 if (size < size_needed || NILP (f->desired_tool_bar_string))
9300 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9301 make_number (' '));
9302 else
9304 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9305 Fremove_text_properties (make_number (0), make_number (size),
9306 props, f->desired_tool_bar_string);
9309 /* Put a `display' property on the string for the images to display,
9310 put a `menu_item' property on tool-bar items with a value that
9311 is the index of the item in F's tool-bar item vector. */
9312 for (i = 0; i < f->n_tool_bar_items; ++i)
9314 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9316 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9317 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9318 int hmargin, vmargin, relief, idx, end;
9319 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9321 /* If image is a vector, choose the image according to the
9322 button state. */
9323 image = PROP (TOOL_BAR_ITEM_IMAGES);
9324 if (VECTORP (image))
9326 if (enabled_p)
9327 idx = (selected_p
9328 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9329 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9330 else
9331 idx = (selected_p
9332 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9333 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9335 xassert (ASIZE (image) >= idx);
9336 image = AREF (image, idx);
9338 else
9339 idx = -1;
9341 /* Ignore invalid image specifications. */
9342 if (!valid_image_p (image))
9343 continue;
9345 /* Display the tool-bar button pressed, or depressed. */
9346 plist = Fcopy_sequence (XCDR (image));
9348 /* Compute margin and relief to draw. */
9349 relief = (tool_bar_button_relief >= 0
9350 ? tool_bar_button_relief
9351 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9352 hmargin = vmargin = relief;
9354 if (INTEGERP (Vtool_bar_button_margin)
9355 && XINT (Vtool_bar_button_margin) > 0)
9357 hmargin += XFASTINT (Vtool_bar_button_margin);
9358 vmargin += XFASTINT (Vtool_bar_button_margin);
9360 else if (CONSP (Vtool_bar_button_margin))
9362 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9363 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9364 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9366 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9367 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9368 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9371 if (auto_raise_tool_bar_buttons_p)
9373 /* Add a `:relief' property to the image spec if the item is
9374 selected. */
9375 if (selected_p)
9377 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9378 hmargin -= relief;
9379 vmargin -= relief;
9382 else
9384 /* If image is selected, display it pressed, i.e. with a
9385 negative relief. If it's not selected, display it with a
9386 raised relief. */
9387 plist = Fplist_put (plist, QCrelief,
9388 (selected_p
9389 ? make_number (-relief)
9390 : make_number (relief)));
9391 hmargin -= relief;
9392 vmargin -= relief;
9395 /* Put a margin around the image. */
9396 if (hmargin || vmargin)
9398 if (hmargin == vmargin)
9399 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9400 else
9401 plist = Fplist_put (plist, QCmargin,
9402 Fcons (make_number (hmargin),
9403 make_number (vmargin)));
9406 /* If button is not enabled, and we don't have special images
9407 for the disabled state, make the image appear disabled by
9408 applying an appropriate algorithm to it. */
9409 if (!enabled_p && idx < 0)
9410 plist = Fplist_put (plist, QCconversion, Qdisabled);
9412 /* Put a `display' text property on the string for the image to
9413 display. Put a `menu-item' property on the string that gives
9414 the start of this item's properties in the tool-bar items
9415 vector. */
9416 image = Fcons (Qimage, plist);
9417 props = list4 (Qdisplay, image,
9418 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9420 /* Let the last image hide all remaining spaces in the tool bar
9421 string. The string can be longer than needed when we reuse a
9422 previous string. */
9423 if (i + 1 == f->n_tool_bar_items)
9424 end = SCHARS (f->desired_tool_bar_string);
9425 else
9426 end = i + 1;
9427 Fadd_text_properties (make_number (i), make_number (end),
9428 props, f->desired_tool_bar_string);
9429 #undef PROP
9432 UNGCPRO;
9436 /* Display one line of the tool-bar of frame IT->f.
9438 HEIGHT specifies the desired height of the tool-bar line.
9439 If the actual height of the glyph row is less than HEIGHT, the
9440 row's height is increased to HEIGHT, and the icons are centered
9441 vertically in the new height.
9443 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9444 count a final empty row in case the tool-bar width exactly matches
9445 the window width.
9448 static void
9449 display_tool_bar_line (it, height)
9450 struct it *it;
9451 int height;
9453 struct glyph_row *row = it->glyph_row;
9454 int max_x = it->last_visible_x;
9455 struct glyph *last;
9457 prepare_desired_row (row);
9458 row->y = it->current_y;
9460 /* Note that this isn't made use of if the face hasn't a box,
9461 so there's no need to check the face here. */
9462 it->start_of_box_run_p = 1;
9464 while (it->current_x < max_x)
9466 int x_before, x, n_glyphs_before, i, nglyphs;
9468 /* Get the next display element. */
9469 if (!get_next_display_element (it))
9471 /* Don't count empty row if we are counting needed tool-bar lines. */
9472 if (height < 0 && !it->hpos)
9473 return;
9474 break;
9477 /* Produce glyphs. */
9478 x_before = it->current_x;
9479 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9480 PRODUCE_GLYPHS (it);
9482 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9483 i = 0;
9484 x = x_before;
9485 while (i < nglyphs)
9487 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9489 if (x + glyph->pixel_width > max_x)
9491 /* Glyph doesn't fit on line. */
9492 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9493 it->current_x = x;
9494 goto out;
9497 ++it->hpos;
9498 x += glyph->pixel_width;
9499 ++i;
9502 /* Stop at line ends. */
9503 if (ITERATOR_AT_END_OF_LINE_P (it))
9504 break;
9506 set_iterator_to_next (it, 1);
9509 out:;
9511 row->displays_text_p = row->used[TEXT_AREA] != 0;
9512 /* Use default face for the border below the tool bar. */
9513 if (!row->displays_text_p)
9514 it->face_id = DEFAULT_FACE_ID;
9515 extend_face_to_end_of_line (it);
9516 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9517 last->right_box_line_p = 1;
9518 if (last == row->glyphs[TEXT_AREA])
9519 last->left_box_line_p = 1;
9521 /* Make line the desired height and center it vertically. */
9522 if ((height -= it->max_ascent + it->max_descent) > 0)
9524 it->max_ascent += height / 2;
9525 it->max_descent += (height + 1) / 2;
9528 compute_line_metrics (it);
9530 /* If line is empty, make it occupy the rest of the tool-bar. */
9531 if (!row->displays_text_p)
9533 row->height = row->phys_height = it->last_visible_y - row->y;
9534 row->ascent = row->phys_ascent = 0;
9535 row->extra_line_spacing = 0;
9538 row->full_width_p = 1;
9539 row->continued_p = 0;
9540 row->truncated_on_left_p = 0;
9541 row->truncated_on_right_p = 0;
9543 it->current_x = it->hpos = 0;
9544 it->current_y += row->height;
9545 ++it->vpos;
9546 ++it->glyph_row;
9550 /* Value is the number of screen lines needed to make all tool-bar
9551 items of frame F visible. The number of actual rows needed is
9552 returned in *N_ROWS if non-NULL. */
9554 static int
9555 tool_bar_lines_needed (f, n_rows)
9556 struct frame *f;
9557 int *n_rows;
9559 struct window *w = XWINDOW (f->tool_bar_window);
9560 struct it it;
9562 /* Initialize an iterator for iteration over
9563 F->desired_tool_bar_string in the tool-bar window of frame F. */
9564 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9565 it.first_visible_x = 0;
9566 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9567 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9569 while (!ITERATOR_AT_END_P (&it))
9571 it.glyph_row = w->desired_matrix->rows;
9572 clear_glyph_row (it.glyph_row);
9573 display_tool_bar_line (&it, -1);
9576 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9577 if (n_rows)
9578 *n_rows = it.vpos > 0 ? it.vpos : -1;
9580 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9584 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9585 0, 1, 0,
9586 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9587 (frame)
9588 Lisp_Object frame;
9590 struct frame *f;
9591 struct window *w;
9592 int nlines = 0;
9594 if (NILP (frame))
9595 frame = selected_frame;
9596 else
9597 CHECK_FRAME (frame);
9598 f = XFRAME (frame);
9600 if (WINDOWP (f->tool_bar_window)
9601 || (w = XWINDOW (f->tool_bar_window),
9602 WINDOW_TOTAL_LINES (w) > 0))
9604 update_tool_bar (f, 1);
9605 if (f->n_tool_bar_items)
9607 build_desired_tool_bar_string (f);
9608 nlines = tool_bar_lines_needed (f, NULL);
9612 return make_number (nlines);
9616 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9617 height should be changed. */
9619 static int
9620 redisplay_tool_bar (f)
9621 struct frame *f;
9623 struct window *w;
9624 struct it it;
9625 struct glyph_row *row;
9626 int change_height_p = 0;
9628 #ifdef USE_GTK
9629 if (FRAME_EXTERNAL_TOOL_BAR (f))
9630 update_frame_tool_bar (f);
9631 return 0;
9632 #endif
9634 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9635 do anything. This means you must start with tool-bar-lines
9636 non-zero to get the auto-sizing effect. Or in other words, you
9637 can turn off tool-bars by specifying tool-bar-lines zero. */
9638 if (!WINDOWP (f->tool_bar_window)
9639 || (w = XWINDOW (f->tool_bar_window),
9640 WINDOW_TOTAL_LINES (w) == 0))
9641 return 0;
9643 /* Set up an iterator for the tool-bar window. */
9644 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9645 it.first_visible_x = 0;
9646 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9647 row = it.glyph_row;
9649 /* Build a string that represents the contents of the tool-bar. */
9650 build_desired_tool_bar_string (f);
9651 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9653 if (f->n_tool_bar_rows == 0)
9654 (void)tool_bar_lines_needed (f, &f->n_tool_bar_rows);
9656 /* Display as many lines as needed to display all tool-bar items. */
9658 if (f->n_tool_bar_rows > 0)
9660 int border, rows, height, extra;
9662 if (INTEGERP (Vtool_bar_border))
9663 border = XINT (Vtool_bar_border);
9664 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9665 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9666 else if (EQ (Vtool_bar_border, Qborder_width))
9667 border = f->border_width;
9668 else
9669 border = 0;
9670 if (border < 0)
9671 border = 0;
9673 rows = f->n_tool_bar_rows;
9674 height = (it.last_visible_y - border) / rows;
9675 extra = it.last_visible_y - border - height * rows;
9677 while (it.current_y < it.last_visible_y)
9679 int h = 0;
9680 if (extra > 0 && rows-- > 0)
9682 h = (extra + rows - 1) / rows;
9683 extra -= h;
9685 display_tool_bar_line (&it, height + h);
9688 else
9690 while (it.current_y < it.last_visible_y)
9691 display_tool_bar_line (&it, 0);
9694 /* It doesn't make much sense to try scrolling in the tool-bar
9695 window, so don't do it. */
9696 w->desired_matrix->no_scrolling_p = 1;
9697 w->must_be_updated_p = 1;
9699 if (auto_resize_tool_bars_p)
9701 int nlines;
9703 /* If we couldn't display everything, change the tool-bar's
9704 height. */
9705 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9706 change_height_p = 1;
9708 /* If there are blank lines at the end, except for a partially
9709 visible blank line at the end that is smaller than
9710 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9711 row = it.glyph_row - 1;
9712 if (!row->displays_text_p
9713 && row->height >= FRAME_LINE_HEIGHT (f))
9714 change_height_p = 1;
9716 /* If row displays tool-bar items, but is partially visible,
9717 change the tool-bar's height. */
9718 if (row->displays_text_p
9719 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9720 change_height_p = 1;
9722 /* Resize windows as needed by changing the `tool-bar-lines'
9723 frame parameter. */
9724 if (change_height_p
9725 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9726 nlines != WINDOW_TOTAL_LINES (w)))
9728 extern Lisp_Object Qtool_bar_lines;
9729 Lisp_Object frame;
9730 int old_height = WINDOW_TOTAL_LINES (w);
9732 XSETFRAME (frame, f);
9733 clear_glyph_matrix (w->desired_matrix);
9734 Fmodify_frame_parameters (frame,
9735 Fcons (Fcons (Qtool_bar_lines,
9736 make_number (nlines)),
9737 Qnil));
9738 if (WINDOW_TOTAL_LINES (w) != old_height)
9739 fonts_changed_p = 1;
9743 return change_height_p;
9747 /* Get information about the tool-bar item which is displayed in GLYPH
9748 on frame F. Return in *PROP_IDX the index where tool-bar item
9749 properties start in F->tool_bar_items. Value is zero if
9750 GLYPH doesn't display a tool-bar item. */
9752 static int
9753 tool_bar_item_info (f, glyph, prop_idx)
9754 struct frame *f;
9755 struct glyph *glyph;
9756 int *prop_idx;
9758 Lisp_Object prop;
9759 int success_p;
9760 int charpos;
9762 /* This function can be called asynchronously, which means we must
9763 exclude any possibility that Fget_text_property signals an
9764 error. */
9765 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9766 charpos = max (0, charpos);
9768 /* Get the text property `menu-item' at pos. The value of that
9769 property is the start index of this item's properties in
9770 F->tool_bar_items. */
9771 prop = Fget_text_property (make_number (charpos),
9772 Qmenu_item, f->current_tool_bar_string);
9773 if (INTEGERP (prop))
9775 *prop_idx = XINT (prop);
9776 success_p = 1;
9778 else
9779 success_p = 0;
9781 return success_p;
9785 /* Get information about the tool-bar item at position X/Y on frame F.
9786 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9787 the current matrix of the tool-bar window of F, or NULL if not
9788 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9789 item in F->tool_bar_items. Value is
9791 -1 if X/Y is not on a tool-bar item
9792 0 if X/Y is on the same item that was highlighted before.
9793 1 otherwise. */
9795 static int
9796 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9797 struct frame *f;
9798 int x, y;
9799 struct glyph **glyph;
9800 int *hpos, *vpos, *prop_idx;
9802 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9803 struct window *w = XWINDOW (f->tool_bar_window);
9804 int area;
9806 /* Find the glyph under X/Y. */
9807 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9808 if (*glyph == NULL)
9809 return -1;
9811 /* Get the start of this tool-bar item's properties in
9812 f->tool_bar_items. */
9813 if (!tool_bar_item_info (f, *glyph, prop_idx))
9814 return -1;
9816 /* Is mouse on the highlighted item? */
9817 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9818 && *vpos >= dpyinfo->mouse_face_beg_row
9819 && *vpos <= dpyinfo->mouse_face_end_row
9820 && (*vpos > dpyinfo->mouse_face_beg_row
9821 || *hpos >= dpyinfo->mouse_face_beg_col)
9822 && (*vpos < dpyinfo->mouse_face_end_row
9823 || *hpos < dpyinfo->mouse_face_end_col
9824 || dpyinfo->mouse_face_past_end))
9825 return 0;
9827 return 1;
9831 /* EXPORT:
9832 Handle mouse button event on the tool-bar of frame F, at
9833 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9834 0 for button release. MODIFIERS is event modifiers for button
9835 release. */
9837 void
9838 handle_tool_bar_click (f, x, y, down_p, modifiers)
9839 struct frame *f;
9840 int x, y, down_p;
9841 unsigned int modifiers;
9843 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9844 struct window *w = XWINDOW (f->tool_bar_window);
9845 int hpos, vpos, prop_idx;
9846 struct glyph *glyph;
9847 Lisp_Object enabled_p;
9849 /* If not on the highlighted tool-bar item, return. */
9850 frame_to_window_pixel_xy (w, &x, &y);
9851 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9852 return;
9854 /* If item is disabled, do nothing. */
9855 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9856 if (NILP (enabled_p))
9857 return;
9859 if (down_p)
9861 /* Show item in pressed state. */
9862 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9863 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9864 last_tool_bar_item = prop_idx;
9866 else
9868 Lisp_Object key, frame;
9869 struct input_event event;
9870 EVENT_INIT (event);
9872 /* Show item in released state. */
9873 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9874 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9876 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9878 XSETFRAME (frame, f);
9879 event.kind = TOOL_BAR_EVENT;
9880 event.frame_or_window = frame;
9881 event.arg = frame;
9882 kbd_buffer_store_event (&event);
9884 event.kind = TOOL_BAR_EVENT;
9885 event.frame_or_window = frame;
9886 event.arg = key;
9887 event.modifiers = modifiers;
9888 kbd_buffer_store_event (&event);
9889 last_tool_bar_item = -1;
9894 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9895 tool-bar window-relative coordinates X/Y. Called from
9896 note_mouse_highlight. */
9898 static void
9899 note_tool_bar_highlight (f, x, y)
9900 struct frame *f;
9901 int x, y;
9903 Lisp_Object window = f->tool_bar_window;
9904 struct window *w = XWINDOW (window);
9905 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9906 int hpos, vpos;
9907 struct glyph *glyph;
9908 struct glyph_row *row;
9909 int i;
9910 Lisp_Object enabled_p;
9911 int prop_idx;
9912 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9913 int mouse_down_p, rc;
9915 /* Function note_mouse_highlight is called with negative x(y
9916 values when mouse moves outside of the frame. */
9917 if (x <= 0 || y <= 0)
9919 clear_mouse_face (dpyinfo);
9920 return;
9923 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9924 if (rc < 0)
9926 /* Not on tool-bar item. */
9927 clear_mouse_face (dpyinfo);
9928 return;
9930 else if (rc == 0)
9931 /* On same tool-bar item as before. */
9932 goto set_help_echo;
9934 clear_mouse_face (dpyinfo);
9936 /* Mouse is down, but on different tool-bar item? */
9937 mouse_down_p = (dpyinfo->grabbed
9938 && f == last_mouse_frame
9939 && FRAME_LIVE_P (f));
9940 if (mouse_down_p
9941 && last_tool_bar_item != prop_idx)
9942 return;
9944 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9945 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9947 /* If tool-bar item is not enabled, don't highlight it. */
9948 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9949 if (!NILP (enabled_p))
9951 /* Compute the x-position of the glyph. In front and past the
9952 image is a space. We include this in the highlighted area. */
9953 row = MATRIX_ROW (w->current_matrix, vpos);
9954 for (i = x = 0; i < hpos; ++i)
9955 x += row->glyphs[TEXT_AREA][i].pixel_width;
9957 /* Record this as the current active region. */
9958 dpyinfo->mouse_face_beg_col = hpos;
9959 dpyinfo->mouse_face_beg_row = vpos;
9960 dpyinfo->mouse_face_beg_x = x;
9961 dpyinfo->mouse_face_beg_y = row->y;
9962 dpyinfo->mouse_face_past_end = 0;
9964 dpyinfo->mouse_face_end_col = hpos + 1;
9965 dpyinfo->mouse_face_end_row = vpos;
9966 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9967 dpyinfo->mouse_face_end_y = row->y;
9968 dpyinfo->mouse_face_window = window;
9969 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9971 /* Display it as active. */
9972 show_mouse_face (dpyinfo, draw);
9973 dpyinfo->mouse_face_image_state = draw;
9976 set_help_echo:
9978 /* Set help_echo_string to a help string to display for this tool-bar item.
9979 XTread_socket does the rest. */
9980 help_echo_object = help_echo_window = Qnil;
9981 help_echo_pos = -1;
9982 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9983 if (NILP (help_echo_string))
9984 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9987 #endif /* HAVE_WINDOW_SYSTEM */
9991 /************************************************************************
9992 Horizontal scrolling
9993 ************************************************************************/
9995 static int hscroll_window_tree P_ ((Lisp_Object));
9996 static int hscroll_windows P_ ((Lisp_Object));
9998 /* For all leaf windows in the window tree rooted at WINDOW, set their
9999 hscroll value so that PT is (i) visible in the window, and (ii) so
10000 that it is not within a certain margin at the window's left and
10001 right border. Value is non-zero if any window's hscroll has been
10002 changed. */
10004 static int
10005 hscroll_window_tree (window)
10006 Lisp_Object window;
10008 int hscrolled_p = 0;
10009 int hscroll_relative_p = FLOATP (Vhscroll_step);
10010 int hscroll_step_abs = 0;
10011 double hscroll_step_rel = 0;
10013 if (hscroll_relative_p)
10015 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10016 if (hscroll_step_rel < 0)
10018 hscroll_relative_p = 0;
10019 hscroll_step_abs = 0;
10022 else if (INTEGERP (Vhscroll_step))
10024 hscroll_step_abs = XINT (Vhscroll_step);
10025 if (hscroll_step_abs < 0)
10026 hscroll_step_abs = 0;
10028 else
10029 hscroll_step_abs = 0;
10031 while (WINDOWP (window))
10033 struct window *w = XWINDOW (window);
10035 if (WINDOWP (w->hchild))
10036 hscrolled_p |= hscroll_window_tree (w->hchild);
10037 else if (WINDOWP (w->vchild))
10038 hscrolled_p |= hscroll_window_tree (w->vchild);
10039 else if (w->cursor.vpos >= 0)
10041 int h_margin;
10042 int text_area_width;
10043 struct glyph_row *current_cursor_row
10044 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10045 struct glyph_row *desired_cursor_row
10046 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10047 struct glyph_row *cursor_row
10048 = (desired_cursor_row->enabled_p
10049 ? desired_cursor_row
10050 : current_cursor_row);
10052 text_area_width = window_box_width (w, TEXT_AREA);
10054 /* Scroll when cursor is inside this scroll margin. */
10055 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10057 if ((XFASTINT (w->hscroll)
10058 && w->cursor.x <= h_margin)
10059 || (cursor_row->enabled_p
10060 && cursor_row->truncated_on_right_p
10061 && (w->cursor.x >= text_area_width - h_margin)))
10063 struct it it;
10064 int hscroll;
10065 struct buffer *saved_current_buffer;
10066 int pt;
10067 int wanted_x;
10069 /* Find point in a display of infinite width. */
10070 saved_current_buffer = current_buffer;
10071 current_buffer = XBUFFER (w->buffer);
10073 if (w == XWINDOW (selected_window))
10074 pt = BUF_PT (current_buffer);
10075 else
10077 pt = marker_position (w->pointm);
10078 pt = max (BEGV, pt);
10079 pt = min (ZV, pt);
10082 /* Move iterator to pt starting at cursor_row->start in
10083 a line with infinite width. */
10084 init_to_row_start (&it, w, cursor_row);
10085 it.last_visible_x = INFINITY;
10086 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10087 current_buffer = saved_current_buffer;
10089 /* Position cursor in window. */
10090 if (!hscroll_relative_p && hscroll_step_abs == 0)
10091 hscroll = max (0, (it.current_x
10092 - (ITERATOR_AT_END_OF_LINE_P (&it)
10093 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10094 : (text_area_width / 2))))
10095 / FRAME_COLUMN_WIDTH (it.f);
10096 else if (w->cursor.x >= text_area_width - h_margin)
10098 if (hscroll_relative_p)
10099 wanted_x = text_area_width * (1 - hscroll_step_rel)
10100 - h_margin;
10101 else
10102 wanted_x = text_area_width
10103 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10104 - h_margin;
10105 hscroll
10106 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10108 else
10110 if (hscroll_relative_p)
10111 wanted_x = text_area_width * hscroll_step_rel
10112 + h_margin;
10113 else
10114 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10115 + h_margin;
10116 hscroll
10117 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10119 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10121 /* Don't call Fset_window_hscroll if value hasn't
10122 changed because it will prevent redisplay
10123 optimizations. */
10124 if (XFASTINT (w->hscroll) != hscroll)
10126 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10127 w->hscroll = make_number (hscroll);
10128 hscrolled_p = 1;
10133 window = w->next;
10136 /* Value is non-zero if hscroll of any leaf window has been changed. */
10137 return hscrolled_p;
10141 /* Set hscroll so that cursor is visible and not inside horizontal
10142 scroll margins for all windows in the tree rooted at WINDOW. See
10143 also hscroll_window_tree above. Value is non-zero if any window's
10144 hscroll has been changed. If it has, desired matrices on the frame
10145 of WINDOW are cleared. */
10147 static int
10148 hscroll_windows (window)
10149 Lisp_Object window;
10151 int hscrolled_p;
10153 if (automatic_hscrolling_p)
10155 hscrolled_p = hscroll_window_tree (window);
10156 if (hscrolled_p)
10157 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10159 else
10160 hscrolled_p = 0;
10161 return hscrolled_p;
10166 /************************************************************************
10167 Redisplay
10168 ************************************************************************/
10170 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10171 to a non-zero value. This is sometimes handy to have in a debugger
10172 session. */
10174 #if GLYPH_DEBUG
10176 /* First and last unchanged row for try_window_id. */
10178 int debug_first_unchanged_at_end_vpos;
10179 int debug_last_unchanged_at_beg_vpos;
10181 /* Delta vpos and y. */
10183 int debug_dvpos, debug_dy;
10185 /* Delta in characters and bytes for try_window_id. */
10187 int debug_delta, debug_delta_bytes;
10189 /* Values of window_end_pos and window_end_vpos at the end of
10190 try_window_id. */
10192 EMACS_INT debug_end_pos, debug_end_vpos;
10194 /* Append a string to W->desired_matrix->method. FMT is a printf
10195 format string. A1...A9 are a supplement for a variable-length
10196 argument list. If trace_redisplay_p is non-zero also printf the
10197 resulting string to stderr. */
10199 static void
10200 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10201 struct window *w;
10202 char *fmt;
10203 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10205 char buffer[512];
10206 char *method = w->desired_matrix->method;
10207 int len = strlen (method);
10208 int size = sizeof w->desired_matrix->method;
10209 int remaining = size - len - 1;
10211 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10212 if (len && remaining)
10214 method[len] = '|';
10215 --remaining, ++len;
10218 strncpy (method + len, buffer, remaining);
10220 if (trace_redisplay_p)
10221 fprintf (stderr, "%p (%s): %s\n",
10223 ((BUFFERP (w->buffer)
10224 && STRINGP (XBUFFER (w->buffer)->name))
10225 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10226 : "no buffer"),
10227 buffer);
10230 #endif /* GLYPH_DEBUG */
10233 /* Value is non-zero if all changes in window W, which displays
10234 current_buffer, are in the text between START and END. START is a
10235 buffer position, END is given as a distance from Z. Used in
10236 redisplay_internal for display optimization. */
10238 static INLINE int
10239 text_outside_line_unchanged_p (w, start, end)
10240 struct window *w;
10241 int start, end;
10243 int unchanged_p = 1;
10245 /* If text or overlays have changed, see where. */
10246 if (XFASTINT (w->last_modified) < MODIFF
10247 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10249 /* Gap in the line? */
10250 if (GPT < start || Z - GPT < end)
10251 unchanged_p = 0;
10253 /* Changes start in front of the line, or end after it? */
10254 if (unchanged_p
10255 && (BEG_UNCHANGED < start - 1
10256 || END_UNCHANGED < end))
10257 unchanged_p = 0;
10259 /* If selective display, can't optimize if changes start at the
10260 beginning of the line. */
10261 if (unchanged_p
10262 && INTEGERP (current_buffer->selective_display)
10263 && XINT (current_buffer->selective_display) > 0
10264 && (BEG_UNCHANGED < start || GPT <= start))
10265 unchanged_p = 0;
10267 /* If there are overlays at the start or end of the line, these
10268 may have overlay strings with newlines in them. A change at
10269 START, for instance, may actually concern the display of such
10270 overlay strings as well, and they are displayed on different
10271 lines. So, quickly rule out this case. (For the future, it
10272 might be desirable to implement something more telling than
10273 just BEG/END_UNCHANGED.) */
10274 if (unchanged_p)
10276 if (BEG + BEG_UNCHANGED == start
10277 && overlay_touches_p (start))
10278 unchanged_p = 0;
10279 if (END_UNCHANGED == end
10280 && overlay_touches_p (Z - end))
10281 unchanged_p = 0;
10285 return unchanged_p;
10289 /* Do a frame update, taking possible shortcuts into account. This is
10290 the main external entry point for redisplay.
10292 If the last redisplay displayed an echo area message and that message
10293 is no longer requested, we clear the echo area or bring back the
10294 mini-buffer if that is in use. */
10296 void
10297 redisplay ()
10299 redisplay_internal (0);
10303 static Lisp_Object
10304 overlay_arrow_string_or_property (var)
10305 Lisp_Object var;
10307 Lisp_Object val;
10309 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10310 return val;
10312 return Voverlay_arrow_string;
10315 /* Return 1 if there are any overlay-arrows in current_buffer. */
10316 static int
10317 overlay_arrow_in_current_buffer_p ()
10319 Lisp_Object vlist;
10321 for (vlist = Voverlay_arrow_variable_list;
10322 CONSP (vlist);
10323 vlist = XCDR (vlist))
10325 Lisp_Object var = XCAR (vlist);
10326 Lisp_Object val;
10328 if (!SYMBOLP (var))
10329 continue;
10330 val = find_symbol_value (var);
10331 if (MARKERP (val)
10332 && current_buffer == XMARKER (val)->buffer)
10333 return 1;
10335 return 0;
10339 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10340 has changed. */
10342 static int
10343 overlay_arrows_changed_p ()
10345 Lisp_Object vlist;
10347 for (vlist = Voverlay_arrow_variable_list;
10348 CONSP (vlist);
10349 vlist = XCDR (vlist))
10351 Lisp_Object var = XCAR (vlist);
10352 Lisp_Object val, pstr;
10354 if (!SYMBOLP (var))
10355 continue;
10356 val = find_symbol_value (var);
10357 if (!MARKERP (val))
10358 continue;
10359 if (! EQ (COERCE_MARKER (val),
10360 Fget (var, Qlast_arrow_position))
10361 || ! (pstr = overlay_arrow_string_or_property (var),
10362 EQ (pstr, Fget (var, Qlast_arrow_string))))
10363 return 1;
10365 return 0;
10368 /* Mark overlay arrows to be updated on next redisplay. */
10370 static void
10371 update_overlay_arrows (up_to_date)
10372 int up_to_date;
10374 Lisp_Object vlist;
10376 for (vlist = Voverlay_arrow_variable_list;
10377 CONSP (vlist);
10378 vlist = XCDR (vlist))
10380 Lisp_Object var = XCAR (vlist);
10382 if (!SYMBOLP (var))
10383 continue;
10385 if (up_to_date > 0)
10387 Lisp_Object val = find_symbol_value (var);
10388 Fput (var, Qlast_arrow_position,
10389 COERCE_MARKER (val));
10390 Fput (var, Qlast_arrow_string,
10391 overlay_arrow_string_or_property (var));
10393 else if (up_to_date < 0
10394 || !NILP (Fget (var, Qlast_arrow_position)))
10396 Fput (var, Qlast_arrow_position, Qt);
10397 Fput (var, Qlast_arrow_string, Qt);
10403 /* Return overlay arrow string to display at row.
10404 Return integer (bitmap number) for arrow bitmap in left fringe.
10405 Return nil if no overlay arrow. */
10407 static Lisp_Object
10408 overlay_arrow_at_row (it, row)
10409 struct it *it;
10410 struct glyph_row *row;
10412 Lisp_Object vlist;
10414 for (vlist = Voverlay_arrow_variable_list;
10415 CONSP (vlist);
10416 vlist = XCDR (vlist))
10418 Lisp_Object var = XCAR (vlist);
10419 Lisp_Object val;
10421 if (!SYMBOLP (var))
10422 continue;
10424 val = find_symbol_value (var);
10426 if (MARKERP (val)
10427 && current_buffer == XMARKER (val)->buffer
10428 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10430 if (FRAME_WINDOW_P (it->f)
10431 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10433 #ifdef HAVE_WINDOW_SYSTEM
10434 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10436 int fringe_bitmap;
10437 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10438 return make_number (fringe_bitmap);
10440 #endif
10441 return make_number (-1); /* Use default arrow bitmap */
10443 return overlay_arrow_string_or_property (var);
10447 return Qnil;
10450 /* Return 1 if point moved out of or into a composition. Otherwise
10451 return 0. PREV_BUF and PREV_PT are the last point buffer and
10452 position. BUF and PT are the current point buffer and position. */
10455 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10456 struct buffer *prev_buf, *buf;
10457 int prev_pt, pt;
10459 int start, end;
10460 Lisp_Object prop;
10461 Lisp_Object buffer;
10463 XSETBUFFER (buffer, buf);
10464 /* Check a composition at the last point if point moved within the
10465 same buffer. */
10466 if (prev_buf == buf)
10468 if (prev_pt == pt)
10469 /* Point didn't move. */
10470 return 0;
10472 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10473 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10474 && COMPOSITION_VALID_P (start, end, prop)
10475 && start < prev_pt && end > prev_pt)
10476 /* The last point was within the composition. Return 1 iff
10477 point moved out of the composition. */
10478 return (pt <= start || pt >= end);
10481 /* Check a composition at the current point. */
10482 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10483 && find_composition (pt, -1, &start, &end, &prop, buffer)
10484 && COMPOSITION_VALID_P (start, end, prop)
10485 && start < pt && end > pt);
10489 /* Reconsider the setting of B->clip_changed which is displayed
10490 in window W. */
10492 static INLINE void
10493 reconsider_clip_changes (w, b)
10494 struct window *w;
10495 struct buffer *b;
10497 if (b->clip_changed
10498 && !NILP (w->window_end_valid)
10499 && w->current_matrix->buffer == b
10500 && w->current_matrix->zv == BUF_ZV (b)
10501 && w->current_matrix->begv == BUF_BEGV (b))
10502 b->clip_changed = 0;
10504 /* If display wasn't paused, and W is not a tool bar window, see if
10505 point has been moved into or out of a composition. In that case,
10506 we set b->clip_changed to 1 to force updating the screen. If
10507 b->clip_changed has already been set to 1, we can skip this
10508 check. */
10509 if (!b->clip_changed
10510 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10512 int pt;
10514 if (w == XWINDOW (selected_window))
10515 pt = BUF_PT (current_buffer);
10516 else
10517 pt = marker_position (w->pointm);
10519 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10520 || pt != XINT (w->last_point))
10521 && check_point_in_composition (w->current_matrix->buffer,
10522 XINT (w->last_point),
10523 XBUFFER (w->buffer), pt))
10524 b->clip_changed = 1;
10529 /* Select FRAME to forward the values of frame-local variables into C
10530 variables so that the redisplay routines can access those values
10531 directly. */
10533 static void
10534 select_frame_for_redisplay (frame)
10535 Lisp_Object frame;
10537 Lisp_Object tail, sym, val;
10538 Lisp_Object old = selected_frame;
10540 selected_frame = frame;
10542 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10543 if (CONSP (XCAR (tail))
10544 && (sym = XCAR (XCAR (tail)),
10545 SYMBOLP (sym))
10546 && (sym = indirect_variable (sym),
10547 val = SYMBOL_VALUE (sym),
10548 (BUFFER_LOCAL_VALUEP (val)
10549 || SOME_BUFFER_LOCAL_VALUEP (val)))
10550 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10551 /* Use find_symbol_value rather than Fsymbol_value
10552 to avoid an error if it is void. */
10553 find_symbol_value (sym);
10555 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10556 if (CONSP (XCAR (tail))
10557 && (sym = XCAR (XCAR (tail)),
10558 SYMBOLP (sym))
10559 && (sym = indirect_variable (sym),
10560 val = SYMBOL_VALUE (sym),
10561 (BUFFER_LOCAL_VALUEP (val)
10562 || SOME_BUFFER_LOCAL_VALUEP (val)))
10563 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10564 find_symbol_value (sym);
10568 #define STOP_POLLING \
10569 do { if (! polling_stopped_here) stop_polling (); \
10570 polling_stopped_here = 1; } while (0)
10572 #define RESUME_POLLING \
10573 do { if (polling_stopped_here) start_polling (); \
10574 polling_stopped_here = 0; } while (0)
10577 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10578 response to any user action; therefore, we should preserve the echo
10579 area. (Actually, our caller does that job.) Perhaps in the future
10580 avoid recentering windows if it is not necessary; currently that
10581 causes some problems. */
10583 static void
10584 redisplay_internal (preserve_echo_area)
10585 int preserve_echo_area;
10587 struct window *w = XWINDOW (selected_window);
10588 struct frame *f = XFRAME (w->frame);
10589 int pause;
10590 int must_finish = 0;
10591 struct text_pos tlbufpos, tlendpos;
10592 int number_of_visible_frames;
10593 int count;
10594 struct frame *sf = SELECTED_FRAME ();
10595 int polling_stopped_here = 0;
10597 /* Non-zero means redisplay has to consider all windows on all
10598 frames. Zero means, only selected_window is considered. */
10599 int consider_all_windows_p;
10601 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10603 /* No redisplay if running in batch mode or frame is not yet fully
10604 initialized, or redisplay is explicitly turned off by setting
10605 Vinhibit_redisplay. */
10606 if (noninteractive
10607 || !NILP (Vinhibit_redisplay)
10608 || !f->glyphs_initialized_p)
10609 return;
10611 /* The flag redisplay_performed_directly_p is set by
10612 direct_output_for_insert when it already did the whole screen
10613 update necessary. */
10614 if (redisplay_performed_directly_p)
10616 redisplay_performed_directly_p = 0;
10617 if (!hscroll_windows (selected_window))
10618 return;
10621 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10622 if (popup_activated ())
10623 return;
10624 #endif
10626 /* I don't think this happens but let's be paranoid. */
10627 if (redisplaying_p)
10628 return;
10630 /* Record a function that resets redisplaying_p to its old value
10631 when we leave this function. */
10632 count = SPECPDL_INDEX ();
10633 record_unwind_protect (unwind_redisplay,
10634 Fcons (make_number (redisplaying_p), selected_frame));
10635 ++redisplaying_p;
10636 specbind (Qinhibit_free_realized_faces, Qnil);
10639 Lisp_Object tail, frame;
10641 FOR_EACH_FRAME (tail, frame)
10643 struct frame *f = XFRAME (frame);
10644 f->already_hscrolled_p = 0;
10648 retry:
10649 pause = 0;
10650 reconsider_clip_changes (w, current_buffer);
10651 last_escape_glyph_frame = NULL;
10652 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10654 /* If new fonts have been loaded that make a glyph matrix adjustment
10655 necessary, do it. */
10656 if (fonts_changed_p)
10658 adjust_glyphs (NULL);
10659 ++windows_or_buffers_changed;
10660 fonts_changed_p = 0;
10663 /* If face_change_count is non-zero, init_iterator will free all
10664 realized faces, which includes the faces referenced from current
10665 matrices. So, we can't reuse current matrices in this case. */
10666 if (face_change_count)
10667 ++windows_or_buffers_changed;
10669 if (! FRAME_WINDOW_P (sf)
10670 && previous_terminal_frame != sf)
10672 /* Since frames on an ASCII terminal share the same display
10673 area, displaying a different frame means redisplay the whole
10674 thing. */
10675 windows_or_buffers_changed++;
10676 SET_FRAME_GARBAGED (sf);
10677 XSETFRAME (Vterminal_frame, sf);
10679 previous_terminal_frame = sf;
10681 /* Set the visible flags for all frames. Do this before checking
10682 for resized or garbaged frames; they want to know if their frames
10683 are visible. See the comment in frame.h for
10684 FRAME_SAMPLE_VISIBILITY. */
10686 Lisp_Object tail, frame;
10688 number_of_visible_frames = 0;
10690 FOR_EACH_FRAME (tail, frame)
10692 struct frame *f = XFRAME (frame);
10694 FRAME_SAMPLE_VISIBILITY (f);
10695 if (FRAME_VISIBLE_P (f))
10696 ++number_of_visible_frames;
10697 clear_desired_matrices (f);
10701 /* Notice any pending interrupt request to change frame size. */
10702 do_pending_window_change (1);
10704 /* Clear frames marked as garbaged. */
10705 if (frame_garbaged)
10706 clear_garbaged_frames ();
10708 /* Build menubar and tool-bar items. */
10709 if (NILP (Vmemory_full))
10710 prepare_menu_bars ();
10712 if (windows_or_buffers_changed)
10713 update_mode_lines++;
10715 /* Detect case that we need to write or remove a star in the mode line. */
10716 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10718 w->update_mode_line = Qt;
10719 if (buffer_shared > 1)
10720 update_mode_lines++;
10723 /* If %c is in the mode line, update it if needed. */
10724 if (!NILP (w->column_number_displayed)
10725 /* This alternative quickly identifies a common case
10726 where no change is needed. */
10727 && !(PT == XFASTINT (w->last_point)
10728 && XFASTINT (w->last_modified) >= MODIFF
10729 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10730 && (XFASTINT (w->column_number_displayed)
10731 != (int) current_column ())) /* iftc */
10732 w->update_mode_line = Qt;
10734 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10736 /* The variable buffer_shared is set in redisplay_window and
10737 indicates that we redisplay a buffer in different windows. See
10738 there. */
10739 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10740 || cursor_type_changed);
10742 /* If specs for an arrow have changed, do thorough redisplay
10743 to ensure we remove any arrow that should no longer exist. */
10744 if (overlay_arrows_changed_p ())
10745 consider_all_windows_p = windows_or_buffers_changed = 1;
10747 /* Normally the message* functions will have already displayed and
10748 updated the echo area, but the frame may have been trashed, or
10749 the update may have been preempted, so display the echo area
10750 again here. Checking message_cleared_p captures the case that
10751 the echo area should be cleared. */
10752 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10753 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10754 || (message_cleared_p
10755 && minibuf_level == 0
10756 /* If the mini-window is currently selected, this means the
10757 echo-area doesn't show through. */
10758 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10760 int window_height_changed_p = echo_area_display (0);
10761 must_finish = 1;
10763 /* If we don't display the current message, don't clear the
10764 message_cleared_p flag, because, if we did, we wouldn't clear
10765 the echo area in the next redisplay which doesn't preserve
10766 the echo area. */
10767 if (!display_last_displayed_message_p)
10768 message_cleared_p = 0;
10770 if (fonts_changed_p)
10771 goto retry;
10772 else if (window_height_changed_p)
10774 consider_all_windows_p = 1;
10775 ++update_mode_lines;
10776 ++windows_or_buffers_changed;
10778 /* If window configuration was changed, frames may have been
10779 marked garbaged. Clear them or we will experience
10780 surprises wrt scrolling. */
10781 if (frame_garbaged)
10782 clear_garbaged_frames ();
10785 else if (EQ (selected_window, minibuf_window)
10786 && (current_buffer->clip_changed
10787 || XFASTINT (w->last_modified) < MODIFF
10788 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10789 && resize_mini_window (w, 0))
10791 /* Resized active mini-window to fit the size of what it is
10792 showing if its contents might have changed. */
10793 must_finish = 1;
10794 consider_all_windows_p = 1;
10795 ++windows_or_buffers_changed;
10796 ++update_mode_lines;
10798 /* If window configuration was changed, frames may have been
10799 marked garbaged. Clear them or we will experience
10800 surprises wrt scrolling. */
10801 if (frame_garbaged)
10802 clear_garbaged_frames ();
10806 /* If showing the region, and mark has changed, we must redisplay
10807 the whole window. The assignment to this_line_start_pos prevents
10808 the optimization directly below this if-statement. */
10809 if (((!NILP (Vtransient_mark_mode)
10810 && !NILP (XBUFFER (w->buffer)->mark_active))
10811 != !NILP (w->region_showing))
10812 || (!NILP (w->region_showing)
10813 && !EQ (w->region_showing,
10814 Fmarker_position (XBUFFER (w->buffer)->mark))))
10815 CHARPOS (this_line_start_pos) = 0;
10817 /* Optimize the case that only the line containing the cursor in the
10818 selected window has changed. Variables starting with this_ are
10819 set in display_line and record information about the line
10820 containing the cursor. */
10821 tlbufpos = this_line_start_pos;
10822 tlendpos = this_line_end_pos;
10823 if (!consider_all_windows_p
10824 && CHARPOS (tlbufpos) > 0
10825 && NILP (w->update_mode_line)
10826 && !current_buffer->clip_changed
10827 && !current_buffer->prevent_redisplay_optimizations_p
10828 && FRAME_VISIBLE_P (XFRAME (w->frame))
10829 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10830 /* Make sure recorded data applies to current buffer, etc. */
10831 && this_line_buffer == current_buffer
10832 && current_buffer == XBUFFER (w->buffer)
10833 && NILP (w->force_start)
10834 && NILP (w->optional_new_start)
10835 /* Point must be on the line that we have info recorded about. */
10836 && PT >= CHARPOS (tlbufpos)
10837 && PT <= Z - CHARPOS (tlendpos)
10838 /* All text outside that line, including its final newline,
10839 must be unchanged */
10840 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10841 CHARPOS (tlendpos)))
10843 if (CHARPOS (tlbufpos) > BEGV
10844 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10845 && (CHARPOS (tlbufpos) == ZV
10846 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10847 /* Former continuation line has disappeared by becoming empty */
10848 goto cancel;
10849 else if (XFASTINT (w->last_modified) < MODIFF
10850 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10851 || MINI_WINDOW_P (w))
10853 /* We have to handle the case of continuation around a
10854 wide-column character (See the comment in indent.c around
10855 line 885).
10857 For instance, in the following case:
10859 -------- Insert --------
10860 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10861 J_I_ ==> J_I_ `^^' are cursors.
10862 ^^ ^^
10863 -------- --------
10865 As we have to redraw the line above, we should goto cancel. */
10867 struct it it;
10868 int line_height_before = this_line_pixel_height;
10870 /* Note that start_display will handle the case that the
10871 line starting at tlbufpos is a continuation lines. */
10872 start_display (&it, w, tlbufpos);
10874 /* Implementation note: It this still necessary? */
10875 if (it.current_x != this_line_start_x)
10876 goto cancel;
10878 TRACE ((stderr, "trying display optimization 1\n"));
10879 w->cursor.vpos = -1;
10880 overlay_arrow_seen = 0;
10881 it.vpos = this_line_vpos;
10882 it.current_y = this_line_y;
10883 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10884 display_line (&it);
10886 /* If line contains point, is not continued,
10887 and ends at same distance from eob as before, we win */
10888 if (w->cursor.vpos >= 0
10889 /* Line is not continued, otherwise this_line_start_pos
10890 would have been set to 0 in display_line. */
10891 && CHARPOS (this_line_start_pos)
10892 /* Line ends as before. */
10893 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10894 /* Line has same height as before. Otherwise other lines
10895 would have to be shifted up or down. */
10896 && this_line_pixel_height == line_height_before)
10898 /* If this is not the window's last line, we must adjust
10899 the charstarts of the lines below. */
10900 if (it.current_y < it.last_visible_y)
10902 struct glyph_row *row
10903 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10904 int delta, delta_bytes;
10906 if (Z - CHARPOS (tlendpos) == ZV)
10908 /* This line ends at end of (accessible part of)
10909 buffer. There is no newline to count. */
10910 delta = (Z
10911 - CHARPOS (tlendpos)
10912 - MATRIX_ROW_START_CHARPOS (row));
10913 delta_bytes = (Z_BYTE
10914 - BYTEPOS (tlendpos)
10915 - MATRIX_ROW_START_BYTEPOS (row));
10917 else
10919 /* This line ends in a newline. Must take
10920 account of the newline and the rest of the
10921 text that follows. */
10922 delta = (Z
10923 - CHARPOS (tlendpos)
10924 - MATRIX_ROW_START_CHARPOS (row));
10925 delta_bytes = (Z_BYTE
10926 - BYTEPOS (tlendpos)
10927 - MATRIX_ROW_START_BYTEPOS (row));
10930 increment_matrix_positions (w->current_matrix,
10931 this_line_vpos + 1,
10932 w->current_matrix->nrows,
10933 delta, delta_bytes);
10936 /* If this row displays text now but previously didn't,
10937 or vice versa, w->window_end_vpos may have to be
10938 adjusted. */
10939 if ((it.glyph_row - 1)->displays_text_p)
10941 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10942 XSETINT (w->window_end_vpos, this_line_vpos);
10944 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10945 && this_line_vpos > 0)
10946 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10947 w->window_end_valid = Qnil;
10949 /* Update hint: No need to try to scroll in update_window. */
10950 w->desired_matrix->no_scrolling_p = 1;
10952 #if GLYPH_DEBUG
10953 *w->desired_matrix->method = 0;
10954 debug_method_add (w, "optimization 1");
10955 #endif
10956 #ifdef HAVE_WINDOW_SYSTEM
10957 update_window_fringes (w, 0);
10958 #endif
10959 goto update;
10961 else
10962 goto cancel;
10964 else if (/* Cursor position hasn't changed. */
10965 PT == XFASTINT (w->last_point)
10966 /* Make sure the cursor was last displayed
10967 in this window. Otherwise we have to reposition it. */
10968 && 0 <= w->cursor.vpos
10969 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10971 if (!must_finish)
10973 do_pending_window_change (1);
10975 /* We used to always goto end_of_redisplay here, but this
10976 isn't enough if we have a blinking cursor. */
10977 if (w->cursor_off_p == w->last_cursor_off_p)
10978 goto end_of_redisplay;
10980 goto update;
10982 /* If highlighting the region, or if the cursor is in the echo area,
10983 then we can't just move the cursor. */
10984 else if (! (!NILP (Vtransient_mark_mode)
10985 && !NILP (current_buffer->mark_active))
10986 && (EQ (selected_window, current_buffer->last_selected_window)
10987 || highlight_nonselected_windows)
10988 && NILP (w->region_showing)
10989 && NILP (Vshow_trailing_whitespace)
10990 && !cursor_in_echo_area)
10992 struct it it;
10993 struct glyph_row *row;
10995 /* Skip from tlbufpos to PT and see where it is. Note that
10996 PT may be in invisible text. If so, we will end at the
10997 next visible position. */
10998 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10999 NULL, DEFAULT_FACE_ID);
11000 it.current_x = this_line_start_x;
11001 it.current_y = this_line_y;
11002 it.vpos = this_line_vpos;
11004 /* The call to move_it_to stops in front of PT, but
11005 moves over before-strings. */
11006 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11008 if (it.vpos == this_line_vpos
11009 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11010 row->enabled_p))
11012 xassert (this_line_vpos == it.vpos);
11013 xassert (this_line_y == it.current_y);
11014 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11015 #if GLYPH_DEBUG
11016 *w->desired_matrix->method = 0;
11017 debug_method_add (w, "optimization 3");
11018 #endif
11019 goto update;
11021 else
11022 goto cancel;
11025 cancel:
11026 /* Text changed drastically or point moved off of line. */
11027 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11030 CHARPOS (this_line_start_pos) = 0;
11031 consider_all_windows_p |= buffer_shared > 1;
11032 ++clear_face_cache_count;
11033 #ifdef HAVE_WINDOW_SYSTEM
11034 ++clear_image_cache_count;
11035 #endif
11037 /* Build desired matrices, and update the display. If
11038 consider_all_windows_p is non-zero, do it for all windows on all
11039 frames. Otherwise do it for selected_window, only. */
11041 if (consider_all_windows_p)
11043 Lisp_Object tail, frame;
11045 FOR_EACH_FRAME (tail, frame)
11046 XFRAME (frame)->updated_p = 0;
11048 /* Recompute # windows showing selected buffer. This will be
11049 incremented each time such a window is displayed. */
11050 buffer_shared = 0;
11052 FOR_EACH_FRAME (tail, frame)
11054 struct frame *f = XFRAME (frame);
11056 if (FRAME_WINDOW_P (f) || f == sf)
11058 if (! EQ (frame, selected_frame))
11059 /* Select the frame, for the sake of frame-local
11060 variables. */
11061 select_frame_for_redisplay (frame);
11063 /* Mark all the scroll bars to be removed; we'll redeem
11064 the ones we want when we redisplay their windows. */
11065 if (condemn_scroll_bars_hook)
11066 condemn_scroll_bars_hook (f);
11068 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11069 redisplay_windows (FRAME_ROOT_WINDOW (f));
11071 /* Any scroll bars which redisplay_windows should have
11072 nuked should now go away. */
11073 if (judge_scroll_bars_hook)
11074 judge_scroll_bars_hook (f);
11076 /* If fonts changed, display again. */
11077 /* ??? rms: I suspect it is a mistake to jump all the way
11078 back to retry here. It should just retry this frame. */
11079 if (fonts_changed_p)
11080 goto retry;
11082 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11084 /* See if we have to hscroll. */
11085 if (!f->already_hscrolled_p)
11087 f->already_hscrolled_p = 1;
11088 if (hscroll_windows (f->root_window))
11089 goto retry;
11092 /* Prevent various kinds of signals during display
11093 update. stdio is not robust about handling
11094 signals, which can cause an apparent I/O
11095 error. */
11096 if (interrupt_input)
11097 unrequest_sigio ();
11098 STOP_POLLING;
11100 /* Update the display. */
11101 set_window_update_flags (XWINDOW (f->root_window), 1);
11102 pause |= update_frame (f, 0, 0);
11103 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11104 if (pause)
11105 break;
11106 #endif
11108 f->updated_p = 1;
11113 if (!pause)
11115 /* Do the mark_window_display_accurate after all windows have
11116 been redisplayed because this call resets flags in buffers
11117 which are needed for proper redisplay. */
11118 FOR_EACH_FRAME (tail, frame)
11120 struct frame *f = XFRAME (frame);
11121 if (f->updated_p)
11123 mark_window_display_accurate (f->root_window, 1);
11124 if (frame_up_to_date_hook)
11125 frame_up_to_date_hook (f);
11130 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11132 Lisp_Object mini_window;
11133 struct frame *mini_frame;
11135 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11136 /* Use list_of_error, not Qerror, so that
11137 we catch only errors and don't run the debugger. */
11138 internal_condition_case_1 (redisplay_window_1, selected_window,
11139 list_of_error,
11140 redisplay_window_error);
11142 /* Compare desired and current matrices, perform output. */
11144 update:
11145 /* If fonts changed, display again. */
11146 if (fonts_changed_p)
11147 goto retry;
11149 /* Prevent various kinds of signals during display update.
11150 stdio is not robust about handling signals,
11151 which can cause an apparent I/O error. */
11152 if (interrupt_input)
11153 unrequest_sigio ();
11154 STOP_POLLING;
11156 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11158 if (hscroll_windows (selected_window))
11159 goto retry;
11161 XWINDOW (selected_window)->must_be_updated_p = 1;
11162 pause = update_frame (sf, 0, 0);
11165 /* We may have called echo_area_display at the top of this
11166 function. If the echo area is on another frame, that may
11167 have put text on a frame other than the selected one, so the
11168 above call to update_frame would not have caught it. Catch
11169 it here. */
11170 mini_window = FRAME_MINIBUF_WINDOW (sf);
11171 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11173 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11175 XWINDOW (mini_window)->must_be_updated_p = 1;
11176 pause |= update_frame (mini_frame, 0, 0);
11177 if (!pause && hscroll_windows (mini_window))
11178 goto retry;
11182 /* If display was paused because of pending input, make sure we do a
11183 thorough update the next time. */
11184 if (pause)
11186 /* Prevent the optimization at the beginning of
11187 redisplay_internal that tries a single-line update of the
11188 line containing the cursor in the selected window. */
11189 CHARPOS (this_line_start_pos) = 0;
11191 /* Let the overlay arrow be updated the next time. */
11192 update_overlay_arrows (0);
11194 /* If we pause after scrolling, some rows in the current
11195 matrices of some windows are not valid. */
11196 if (!WINDOW_FULL_WIDTH_P (w)
11197 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11198 update_mode_lines = 1;
11200 else
11202 if (!consider_all_windows_p)
11204 /* This has already been done above if
11205 consider_all_windows_p is set. */
11206 mark_window_display_accurate_1 (w, 1);
11208 /* Say overlay arrows are up to date. */
11209 update_overlay_arrows (1);
11211 if (frame_up_to_date_hook != 0)
11212 frame_up_to_date_hook (sf);
11215 update_mode_lines = 0;
11216 windows_or_buffers_changed = 0;
11217 cursor_type_changed = 0;
11220 /* Start SIGIO interrupts coming again. Having them off during the
11221 code above makes it less likely one will discard output, but not
11222 impossible, since there might be stuff in the system buffer here.
11223 But it is much hairier to try to do anything about that. */
11224 if (interrupt_input)
11225 request_sigio ();
11226 RESUME_POLLING;
11228 /* If a frame has become visible which was not before, redisplay
11229 again, so that we display it. Expose events for such a frame
11230 (which it gets when becoming visible) don't call the parts of
11231 redisplay constructing glyphs, so simply exposing a frame won't
11232 display anything in this case. So, we have to display these
11233 frames here explicitly. */
11234 if (!pause)
11236 Lisp_Object tail, frame;
11237 int new_count = 0;
11239 FOR_EACH_FRAME (tail, frame)
11241 int this_is_visible = 0;
11243 if (XFRAME (frame)->visible)
11244 this_is_visible = 1;
11245 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11246 if (XFRAME (frame)->visible)
11247 this_is_visible = 1;
11249 if (this_is_visible)
11250 new_count++;
11253 if (new_count != number_of_visible_frames)
11254 windows_or_buffers_changed++;
11257 /* Change frame size now if a change is pending. */
11258 do_pending_window_change (1);
11260 /* If we just did a pending size change, or have additional
11261 visible frames, redisplay again. */
11262 if (windows_or_buffers_changed && !pause)
11263 goto retry;
11265 /* Clear the face cache eventually. */
11266 if (consider_all_windows_p)
11268 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11270 clear_face_cache (0);
11271 clear_face_cache_count = 0;
11273 #ifdef HAVE_WINDOW_SYSTEM
11274 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11276 Lisp_Object tail, frame;
11277 FOR_EACH_FRAME (tail, frame)
11279 struct frame *f = XFRAME (frame);
11280 if (FRAME_WINDOW_P (f))
11281 clear_image_cache (f, 0);
11283 clear_image_cache_count = 0;
11285 #endif /* HAVE_WINDOW_SYSTEM */
11288 end_of_redisplay:
11289 unbind_to (count, Qnil);
11290 RESUME_POLLING;
11294 /* Redisplay, but leave alone any recent echo area message unless
11295 another message has been requested in its place.
11297 This is useful in situations where you need to redisplay but no
11298 user action has occurred, making it inappropriate for the message
11299 area to be cleared. See tracking_off and
11300 wait_reading_process_output for examples of these situations.
11302 FROM_WHERE is an integer saying from where this function was
11303 called. This is useful for debugging. */
11305 void
11306 redisplay_preserve_echo_area (from_where)
11307 int from_where;
11309 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11311 if (!NILP (echo_area_buffer[1]))
11313 /* We have a previously displayed message, but no current
11314 message. Redisplay the previous message. */
11315 display_last_displayed_message_p = 1;
11316 redisplay_internal (1);
11317 display_last_displayed_message_p = 0;
11319 else
11320 redisplay_internal (1);
11322 if (rif != NULL && rif->flush_display_optional)
11323 rif->flush_display_optional (NULL);
11327 /* Function registered with record_unwind_protect in
11328 redisplay_internal. Reset redisplaying_p to the value it had
11329 before redisplay_internal was called, and clear
11330 prevent_freeing_realized_faces_p. It also selects the previously
11331 selected frame. */
11333 static Lisp_Object
11334 unwind_redisplay (val)
11335 Lisp_Object val;
11337 Lisp_Object old_redisplaying_p, old_frame;
11339 old_redisplaying_p = XCAR (val);
11340 redisplaying_p = XFASTINT (old_redisplaying_p);
11341 old_frame = XCDR (val);
11342 if (! EQ (old_frame, selected_frame))
11343 select_frame_for_redisplay (old_frame);
11344 return Qnil;
11348 /* Mark the display of window W as accurate or inaccurate. If
11349 ACCURATE_P is non-zero mark display of W as accurate. If
11350 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11351 redisplay_internal is called. */
11353 static void
11354 mark_window_display_accurate_1 (w, accurate_p)
11355 struct window *w;
11356 int accurate_p;
11358 if (BUFFERP (w->buffer))
11360 struct buffer *b = XBUFFER (w->buffer);
11362 w->last_modified
11363 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11364 w->last_overlay_modified
11365 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11366 w->last_had_star
11367 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11369 if (accurate_p)
11371 b->clip_changed = 0;
11372 b->prevent_redisplay_optimizations_p = 0;
11374 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11375 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11376 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11377 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11379 w->current_matrix->buffer = b;
11380 w->current_matrix->begv = BUF_BEGV (b);
11381 w->current_matrix->zv = BUF_ZV (b);
11383 w->last_cursor = w->cursor;
11384 w->last_cursor_off_p = w->cursor_off_p;
11386 if (w == XWINDOW (selected_window))
11387 w->last_point = make_number (BUF_PT (b));
11388 else
11389 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11393 if (accurate_p)
11395 w->window_end_valid = w->buffer;
11396 #if 0 /* This is incorrect with variable-height lines. */
11397 xassert (XINT (w->window_end_vpos)
11398 < (WINDOW_TOTAL_LINES (w)
11399 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11400 #endif
11401 w->update_mode_line = Qnil;
11406 /* Mark the display of windows in the window tree rooted at WINDOW as
11407 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11408 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11409 be redisplayed the next time redisplay_internal is called. */
11411 void
11412 mark_window_display_accurate (window, accurate_p)
11413 Lisp_Object window;
11414 int accurate_p;
11416 struct window *w;
11418 for (; !NILP (window); window = w->next)
11420 w = XWINDOW (window);
11421 mark_window_display_accurate_1 (w, accurate_p);
11423 if (!NILP (w->vchild))
11424 mark_window_display_accurate (w->vchild, accurate_p);
11425 if (!NILP (w->hchild))
11426 mark_window_display_accurate (w->hchild, accurate_p);
11429 if (accurate_p)
11431 update_overlay_arrows (1);
11433 else
11435 /* Force a thorough redisplay the next time by setting
11436 last_arrow_position and last_arrow_string to t, which is
11437 unequal to any useful value of Voverlay_arrow_... */
11438 update_overlay_arrows (-1);
11443 /* Return value in display table DP (Lisp_Char_Table *) for character
11444 C. Since a display table doesn't have any parent, we don't have to
11445 follow parent. Do not call this function directly but use the
11446 macro DISP_CHAR_VECTOR. */
11448 Lisp_Object
11449 disp_char_vector (dp, c)
11450 struct Lisp_Char_Table *dp;
11451 int c;
11453 int code[4], i;
11454 Lisp_Object val;
11456 if (SINGLE_BYTE_CHAR_P (c))
11457 return (dp->contents[c]);
11459 SPLIT_CHAR (c, code[0], code[1], code[2]);
11460 if (code[1] < 32)
11461 code[1] = -1;
11462 else if (code[2] < 32)
11463 code[2] = -1;
11465 /* Here, the possible range of code[0] (== charset ID) is
11466 128..max_charset. Since the top level char table contains data
11467 for multibyte characters after 256th element, we must increment
11468 code[0] by 128 to get a correct index. */
11469 code[0] += 128;
11470 code[3] = -1; /* anchor */
11472 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11474 val = dp->contents[code[i]];
11475 if (!SUB_CHAR_TABLE_P (val))
11476 return (NILP (val) ? dp->defalt : val);
11479 /* Here, val is a sub char table. We return the default value of
11480 it. */
11481 return (dp->defalt);
11486 /***********************************************************************
11487 Window Redisplay
11488 ***********************************************************************/
11490 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11492 static void
11493 redisplay_windows (window)
11494 Lisp_Object window;
11496 while (!NILP (window))
11498 struct window *w = XWINDOW (window);
11500 if (!NILP (w->hchild))
11501 redisplay_windows (w->hchild);
11502 else if (!NILP (w->vchild))
11503 redisplay_windows (w->vchild);
11504 else
11506 displayed_buffer = XBUFFER (w->buffer);
11507 /* Use list_of_error, not Qerror, so that
11508 we catch only errors and don't run the debugger. */
11509 internal_condition_case_1 (redisplay_window_0, window,
11510 list_of_error,
11511 redisplay_window_error);
11514 window = w->next;
11518 static Lisp_Object
11519 redisplay_window_error ()
11521 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11522 return Qnil;
11525 static Lisp_Object
11526 redisplay_window_0 (window)
11527 Lisp_Object window;
11529 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11530 redisplay_window (window, 0);
11531 return Qnil;
11534 static Lisp_Object
11535 redisplay_window_1 (window)
11536 Lisp_Object window;
11538 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11539 redisplay_window (window, 1);
11540 return Qnil;
11544 /* Increment GLYPH until it reaches END or CONDITION fails while
11545 adding (GLYPH)->pixel_width to X. */
11547 #define SKIP_GLYPHS(glyph, end, x, condition) \
11548 do \
11550 (x) += (glyph)->pixel_width; \
11551 ++(glyph); \
11553 while ((glyph) < (end) && (condition))
11556 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11557 DELTA is the number of bytes by which positions recorded in ROW
11558 differ from current buffer positions. */
11560 void
11561 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11562 struct window *w;
11563 struct glyph_row *row;
11564 struct glyph_matrix *matrix;
11565 int delta, delta_bytes, dy, dvpos;
11567 struct glyph *glyph = row->glyphs[TEXT_AREA];
11568 struct glyph *end = glyph + row->used[TEXT_AREA];
11569 struct glyph *cursor = NULL;
11570 /* The first glyph that starts a sequence of glyphs from string. */
11571 struct glyph *string_start;
11572 /* The X coordinate of string_start. */
11573 int string_start_x;
11574 /* The last known character position. */
11575 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11576 /* The last known character position before string_start. */
11577 int string_before_pos;
11578 int x = row->x;
11579 int cursor_x = x;
11580 int cursor_from_overlay_pos = 0;
11581 int pt_old = PT - delta;
11583 /* Skip over glyphs not having an object at the start of the row.
11584 These are special glyphs like truncation marks on terminal
11585 frames. */
11586 if (row->displays_text_p)
11587 while (glyph < end
11588 && INTEGERP (glyph->object)
11589 && glyph->charpos < 0)
11591 x += glyph->pixel_width;
11592 ++glyph;
11595 string_start = NULL;
11596 while (glyph < end
11597 && !INTEGERP (glyph->object)
11598 && (!BUFFERP (glyph->object)
11599 || (last_pos = glyph->charpos) < pt_old))
11601 if (! STRINGP (glyph->object))
11603 string_start = NULL;
11604 x += glyph->pixel_width;
11605 ++glyph;
11606 if (cursor_from_overlay_pos
11607 && last_pos >= cursor_from_overlay_pos)
11609 cursor_from_overlay_pos = 0;
11610 cursor = 0;
11613 else
11615 string_before_pos = last_pos;
11616 string_start = glyph;
11617 string_start_x = x;
11618 /* Skip all glyphs from string. */
11621 Lisp_Object cprop;
11622 int pos;
11623 if ((cursor == NULL || glyph > cursor)
11624 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11625 Qcursor, (glyph)->object),
11626 !NILP (cprop))
11627 && (pos = string_buffer_position (w, glyph->object,
11628 string_before_pos),
11629 (pos == 0 /* From overlay */
11630 || pos == pt_old)))
11632 /* Estimate overlay buffer position from the buffer
11633 positions of the glyphs before and after the overlay.
11634 Add 1 to last_pos so that if point corresponds to the
11635 glyph right after the overlay, we still use a 'cursor'
11636 property found in that overlay. */
11637 cursor_from_overlay_pos = (pos ? 0 : last_pos
11638 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11639 cursor = glyph;
11640 cursor_x = x;
11642 x += glyph->pixel_width;
11643 ++glyph;
11645 while (glyph < end && EQ (glyph->object, string_start->object));
11649 if (cursor != NULL)
11651 glyph = cursor;
11652 x = cursor_x;
11654 else if (row->ends_in_ellipsis_p && glyph == end)
11656 /* Scan back over the ellipsis glyphs, decrementing positions. */
11657 while (glyph > row->glyphs[TEXT_AREA]
11658 && (glyph - 1)->charpos == last_pos)
11659 glyph--, x -= glyph->pixel_width;
11660 /* That loop always goes one position too far,
11661 including the glyph before the ellipsis.
11662 So scan forward over that one. */
11663 x += glyph->pixel_width;
11664 glyph++;
11666 else if (string_start
11667 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11669 /* We may have skipped over point because the previous glyphs
11670 are from string. As there's no easy way to know the
11671 character position of the current glyph, find the correct
11672 glyph on point by scanning from string_start again. */
11673 Lisp_Object limit;
11674 Lisp_Object string;
11675 int pos;
11677 limit = make_number (pt_old + 1);
11678 end = glyph;
11679 glyph = string_start;
11680 x = string_start_x;
11681 string = glyph->object;
11682 pos = string_buffer_position (w, string, string_before_pos);
11683 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11684 because we always put cursor after overlay strings. */
11685 while (pos == 0 && glyph < end)
11687 string = glyph->object;
11688 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11689 if (glyph < end)
11690 pos = string_buffer_position (w, glyph->object, string_before_pos);
11693 while (glyph < end)
11695 pos = XINT (Fnext_single_char_property_change
11696 (make_number (pos), Qdisplay, Qnil, limit));
11697 if (pos > pt_old)
11698 break;
11699 /* Skip glyphs from the same string. */
11700 string = glyph->object;
11701 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11702 /* Skip glyphs from an overlay. */
11703 while (glyph < end
11704 && ! string_buffer_position (w, glyph->object, pos))
11706 string = glyph->object;
11707 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11712 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11713 w->cursor.x = x;
11714 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11715 w->cursor.y = row->y + dy;
11717 if (w == XWINDOW (selected_window))
11719 if (!row->continued_p
11720 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11721 && row->x == 0)
11723 this_line_buffer = XBUFFER (w->buffer);
11725 CHARPOS (this_line_start_pos)
11726 = MATRIX_ROW_START_CHARPOS (row) + delta;
11727 BYTEPOS (this_line_start_pos)
11728 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11730 CHARPOS (this_line_end_pos)
11731 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11732 BYTEPOS (this_line_end_pos)
11733 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11735 this_line_y = w->cursor.y;
11736 this_line_pixel_height = row->height;
11737 this_line_vpos = w->cursor.vpos;
11738 this_line_start_x = row->x;
11740 else
11741 CHARPOS (this_line_start_pos) = 0;
11746 /* Run window scroll functions, if any, for WINDOW with new window
11747 start STARTP. Sets the window start of WINDOW to that position.
11749 We assume that the window's buffer is really current. */
11751 static INLINE struct text_pos
11752 run_window_scroll_functions (window, startp)
11753 Lisp_Object window;
11754 struct text_pos startp;
11756 struct window *w = XWINDOW (window);
11757 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11759 if (current_buffer != XBUFFER (w->buffer))
11760 abort ();
11762 if (!NILP (Vwindow_scroll_functions))
11764 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11765 make_number (CHARPOS (startp)));
11766 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11767 /* In case the hook functions switch buffers. */
11768 if (current_buffer != XBUFFER (w->buffer))
11769 set_buffer_internal_1 (XBUFFER (w->buffer));
11772 return startp;
11776 /* Make sure the line containing the cursor is fully visible.
11777 A value of 1 means there is nothing to be done.
11778 (Either the line is fully visible, or it cannot be made so,
11779 or we cannot tell.)
11781 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11782 is higher than window.
11784 A value of 0 means the caller should do scrolling
11785 as if point had gone off the screen. */
11787 static int
11788 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11789 struct window *w;
11790 int force_p;
11791 int current_matrix_p;
11793 struct glyph_matrix *matrix;
11794 struct glyph_row *row;
11795 int window_height;
11797 if (!make_cursor_line_fully_visible_p)
11798 return 1;
11800 /* It's not always possible to find the cursor, e.g, when a window
11801 is full of overlay strings. Don't do anything in that case. */
11802 if (w->cursor.vpos < 0)
11803 return 1;
11805 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11806 row = MATRIX_ROW (matrix, w->cursor.vpos);
11808 /* If the cursor row is not partially visible, there's nothing to do. */
11809 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11810 return 1;
11812 /* If the row the cursor is in is taller than the window's height,
11813 it's not clear what to do, so do nothing. */
11814 window_height = window_box_height (w);
11815 if (row->height >= window_height)
11817 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11818 return 1;
11820 return 0;
11822 #if 0
11823 /* This code used to try to scroll the window just enough to make
11824 the line visible. It returned 0 to say that the caller should
11825 allocate larger glyph matrices. */
11827 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11829 int dy = row->height - row->visible_height;
11830 w->vscroll = 0;
11831 w->cursor.y += dy;
11832 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11834 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11836 int dy = - (row->height - row->visible_height);
11837 w->vscroll = dy;
11838 w->cursor.y += dy;
11839 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11842 /* When we change the cursor y-position of the selected window,
11843 change this_line_y as well so that the display optimization for
11844 the cursor line of the selected window in redisplay_internal uses
11845 the correct y-position. */
11846 if (w == XWINDOW (selected_window))
11847 this_line_y = w->cursor.y;
11849 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11850 redisplay with larger matrices. */
11851 if (matrix->nrows < required_matrix_height (w))
11853 fonts_changed_p = 1;
11854 return 0;
11857 return 1;
11858 #endif /* 0 */
11862 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11863 non-zero means only WINDOW is redisplayed in redisplay_internal.
11864 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11865 in redisplay_window to bring a partially visible line into view in
11866 the case that only the cursor has moved.
11868 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11869 last screen line's vertical height extends past the end of the screen.
11871 Value is
11873 1 if scrolling succeeded
11875 0 if scrolling didn't find point.
11877 -1 if new fonts have been loaded so that we must interrupt
11878 redisplay, adjust glyph matrices, and try again. */
11880 enum
11882 SCROLLING_SUCCESS,
11883 SCROLLING_FAILED,
11884 SCROLLING_NEED_LARGER_MATRICES
11887 static int
11888 try_scrolling (window, just_this_one_p, scroll_conservatively,
11889 scroll_step, temp_scroll_step, last_line_misfit)
11890 Lisp_Object window;
11891 int just_this_one_p;
11892 EMACS_INT scroll_conservatively, scroll_step;
11893 int temp_scroll_step;
11894 int last_line_misfit;
11896 struct window *w = XWINDOW (window);
11897 struct frame *f = XFRAME (w->frame);
11898 struct text_pos scroll_margin_pos;
11899 struct text_pos pos;
11900 struct text_pos startp;
11901 struct it it;
11902 Lisp_Object window_end;
11903 int this_scroll_margin;
11904 int dy = 0;
11905 int scroll_max;
11906 int rc;
11907 int amount_to_scroll = 0;
11908 Lisp_Object aggressive;
11909 int height;
11910 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11912 #if GLYPH_DEBUG
11913 debug_method_add (w, "try_scrolling");
11914 #endif
11916 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11918 /* Compute scroll margin height in pixels. We scroll when point is
11919 within this distance from the top or bottom of the window. */
11920 if (scroll_margin > 0)
11922 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11923 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11925 else
11926 this_scroll_margin = 0;
11928 /* Force scroll_conservatively to have a reasonable value so it doesn't
11929 cause an overflow while computing how much to scroll. */
11930 if (scroll_conservatively)
11931 scroll_conservatively = min (scroll_conservatively,
11932 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11934 /* Compute how much we should try to scroll maximally to bring point
11935 into view. */
11936 if (scroll_step || scroll_conservatively || temp_scroll_step)
11937 scroll_max = max (scroll_step,
11938 max (scroll_conservatively, temp_scroll_step));
11939 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11940 || NUMBERP (current_buffer->scroll_up_aggressively))
11941 /* We're trying to scroll because of aggressive scrolling
11942 but no scroll_step is set. Choose an arbitrary one. Maybe
11943 there should be a variable for this. */
11944 scroll_max = 10;
11945 else
11946 scroll_max = 0;
11947 scroll_max *= FRAME_LINE_HEIGHT (f);
11949 /* Decide whether we have to scroll down. Start at the window end
11950 and move this_scroll_margin up to find the position of the scroll
11951 margin. */
11952 window_end = Fwindow_end (window, Qt);
11954 too_near_end:
11956 CHARPOS (scroll_margin_pos) = XINT (window_end);
11957 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11959 if (this_scroll_margin || extra_scroll_margin_lines)
11961 start_display (&it, w, scroll_margin_pos);
11962 if (this_scroll_margin)
11963 move_it_vertically_backward (&it, this_scroll_margin);
11964 if (extra_scroll_margin_lines)
11965 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11966 scroll_margin_pos = it.current.pos;
11969 if (PT >= CHARPOS (scroll_margin_pos))
11971 int y0;
11973 /* Point is in the scroll margin at the bottom of the window, or
11974 below. Compute a new window start that makes point visible. */
11976 /* Compute the distance from the scroll margin to PT.
11977 Give up if the distance is greater than scroll_max. */
11978 start_display (&it, w, scroll_margin_pos);
11979 y0 = it.current_y;
11980 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11981 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11983 /* To make point visible, we have to move the window start
11984 down so that the line the cursor is in is visible, which
11985 means we have to add in the height of the cursor line. */
11986 dy = line_bottom_y (&it) - y0;
11988 if (dy > scroll_max)
11989 return SCROLLING_FAILED;
11991 /* Move the window start down. If scrolling conservatively,
11992 move it just enough down to make point visible. If
11993 scroll_step is set, move it down by scroll_step. */
11994 start_display (&it, w, startp);
11996 if (scroll_conservatively)
11997 /* Set AMOUNT_TO_SCROLL to at least one line,
11998 and at most scroll_conservatively lines. */
11999 amount_to_scroll
12000 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12001 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12002 else if (scroll_step || temp_scroll_step)
12003 amount_to_scroll = scroll_max;
12004 else
12006 aggressive = current_buffer->scroll_up_aggressively;
12007 height = WINDOW_BOX_TEXT_HEIGHT (w);
12008 if (NUMBERP (aggressive))
12010 double float_amount = XFLOATINT (aggressive) * height;
12011 amount_to_scroll = float_amount;
12012 if (amount_to_scroll == 0 && float_amount > 0)
12013 amount_to_scroll = 1;
12017 if (amount_to_scroll <= 0)
12018 return SCROLLING_FAILED;
12020 /* If moving by amount_to_scroll leaves STARTP unchanged,
12021 move it down one screen line. */
12023 move_it_vertically (&it, amount_to_scroll);
12024 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12025 move_it_by_lines (&it, 1, 1);
12026 startp = it.current.pos;
12028 else
12030 /* See if point is inside the scroll margin at the top of the
12031 window. */
12032 scroll_margin_pos = startp;
12033 if (this_scroll_margin)
12035 start_display (&it, w, startp);
12036 move_it_vertically (&it, this_scroll_margin);
12037 scroll_margin_pos = it.current.pos;
12040 if (PT < CHARPOS (scroll_margin_pos))
12042 /* Point is in the scroll margin at the top of the window or
12043 above what is displayed in the window. */
12044 int y0;
12046 /* Compute the vertical distance from PT to the scroll
12047 margin position. Give up if distance is greater than
12048 scroll_max. */
12049 SET_TEXT_POS (pos, PT, PT_BYTE);
12050 start_display (&it, w, pos);
12051 y0 = it.current_y;
12052 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12053 it.last_visible_y, -1,
12054 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12055 dy = it.current_y - y0;
12056 if (dy > scroll_max)
12057 return SCROLLING_FAILED;
12059 /* Compute new window start. */
12060 start_display (&it, w, startp);
12062 if (scroll_conservatively)
12063 amount_to_scroll
12064 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12065 else if (scroll_step || temp_scroll_step)
12066 amount_to_scroll = scroll_max;
12067 else
12069 aggressive = current_buffer->scroll_down_aggressively;
12070 height = WINDOW_BOX_TEXT_HEIGHT (w);
12071 if (NUMBERP (aggressive))
12073 double float_amount = XFLOATINT (aggressive) * height;
12074 amount_to_scroll = float_amount;
12075 if (amount_to_scroll == 0 && float_amount > 0)
12076 amount_to_scroll = 1;
12080 if (amount_to_scroll <= 0)
12081 return SCROLLING_FAILED;
12083 move_it_vertically_backward (&it, amount_to_scroll);
12084 startp = it.current.pos;
12088 /* Run window scroll functions. */
12089 startp = run_window_scroll_functions (window, startp);
12091 /* Display the window. Give up if new fonts are loaded, or if point
12092 doesn't appear. */
12093 if (!try_window (window, startp, 0))
12094 rc = SCROLLING_NEED_LARGER_MATRICES;
12095 else if (w->cursor.vpos < 0)
12097 clear_glyph_matrix (w->desired_matrix);
12098 rc = SCROLLING_FAILED;
12100 else
12102 /* Maybe forget recorded base line for line number display. */
12103 if (!just_this_one_p
12104 || current_buffer->clip_changed
12105 || BEG_UNCHANGED < CHARPOS (startp))
12106 w->base_line_number = Qnil;
12108 /* If cursor ends up on a partially visible line,
12109 treat that as being off the bottom of the screen. */
12110 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12112 clear_glyph_matrix (w->desired_matrix);
12113 ++extra_scroll_margin_lines;
12114 goto too_near_end;
12116 rc = SCROLLING_SUCCESS;
12119 return rc;
12123 /* Compute a suitable window start for window W if display of W starts
12124 on a continuation line. Value is non-zero if a new window start
12125 was computed.
12127 The new window start will be computed, based on W's width, starting
12128 from the start of the continued line. It is the start of the
12129 screen line with the minimum distance from the old start W->start. */
12131 static int
12132 compute_window_start_on_continuation_line (w)
12133 struct window *w;
12135 struct text_pos pos, start_pos;
12136 int window_start_changed_p = 0;
12138 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12140 /* If window start is on a continuation line... Window start may be
12141 < BEGV in case there's invisible text at the start of the
12142 buffer (M-x rmail, for example). */
12143 if (CHARPOS (start_pos) > BEGV
12144 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12146 struct it it;
12147 struct glyph_row *row;
12149 /* Handle the case that the window start is out of range. */
12150 if (CHARPOS (start_pos) < BEGV)
12151 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12152 else if (CHARPOS (start_pos) > ZV)
12153 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12155 /* Find the start of the continued line. This should be fast
12156 because scan_buffer is fast (newline cache). */
12157 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12158 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12159 row, DEFAULT_FACE_ID);
12160 reseat_at_previous_visible_line_start (&it);
12162 /* If the line start is "too far" away from the window start,
12163 say it takes too much time to compute a new window start. */
12164 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12165 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12167 int min_distance, distance;
12169 /* Move forward by display lines to find the new window
12170 start. If window width was enlarged, the new start can
12171 be expected to be > the old start. If window width was
12172 decreased, the new window start will be < the old start.
12173 So, we're looking for the display line start with the
12174 minimum distance from the old window start. */
12175 pos = it.current.pos;
12176 min_distance = INFINITY;
12177 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12178 distance < min_distance)
12180 min_distance = distance;
12181 pos = it.current.pos;
12182 move_it_by_lines (&it, 1, 0);
12185 /* Set the window start there. */
12186 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12187 window_start_changed_p = 1;
12191 return window_start_changed_p;
12195 /* Try cursor movement in case text has not changed in window WINDOW,
12196 with window start STARTP. Value is
12198 CURSOR_MOVEMENT_SUCCESS if successful
12200 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12202 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12203 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12204 we want to scroll as if scroll-step were set to 1. See the code.
12206 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12207 which case we have to abort this redisplay, and adjust matrices
12208 first. */
12210 enum
12212 CURSOR_MOVEMENT_SUCCESS,
12213 CURSOR_MOVEMENT_CANNOT_BE_USED,
12214 CURSOR_MOVEMENT_MUST_SCROLL,
12215 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12218 static int
12219 try_cursor_movement (window, startp, scroll_step)
12220 Lisp_Object window;
12221 struct text_pos startp;
12222 int *scroll_step;
12224 struct window *w = XWINDOW (window);
12225 struct frame *f = XFRAME (w->frame);
12226 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12228 #if GLYPH_DEBUG
12229 if (inhibit_try_cursor_movement)
12230 return rc;
12231 #endif
12233 /* Handle case where text has not changed, only point, and it has
12234 not moved off the frame. */
12235 if (/* Point may be in this window. */
12236 PT >= CHARPOS (startp)
12237 /* Selective display hasn't changed. */
12238 && !current_buffer->clip_changed
12239 /* Function force-mode-line-update is used to force a thorough
12240 redisplay. It sets either windows_or_buffers_changed or
12241 update_mode_lines. So don't take a shortcut here for these
12242 cases. */
12243 && !update_mode_lines
12244 && !windows_or_buffers_changed
12245 && !cursor_type_changed
12246 /* Can't use this case if highlighting a region. When a
12247 region exists, cursor movement has to do more than just
12248 set the cursor. */
12249 && !(!NILP (Vtransient_mark_mode)
12250 && !NILP (current_buffer->mark_active))
12251 && NILP (w->region_showing)
12252 && NILP (Vshow_trailing_whitespace)
12253 /* Right after splitting windows, last_point may be nil. */
12254 && INTEGERP (w->last_point)
12255 /* This code is not used for mini-buffer for the sake of the case
12256 of redisplaying to replace an echo area message; since in
12257 that case the mini-buffer contents per se are usually
12258 unchanged. This code is of no real use in the mini-buffer
12259 since the handling of this_line_start_pos, etc., in redisplay
12260 handles the same cases. */
12261 && !EQ (window, minibuf_window)
12262 /* When splitting windows or for new windows, it happens that
12263 redisplay is called with a nil window_end_vpos or one being
12264 larger than the window. This should really be fixed in
12265 window.c. I don't have this on my list, now, so we do
12266 approximately the same as the old redisplay code. --gerd. */
12267 && INTEGERP (w->window_end_vpos)
12268 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12269 && (FRAME_WINDOW_P (f)
12270 || !overlay_arrow_in_current_buffer_p ()))
12272 int this_scroll_margin, top_scroll_margin;
12273 struct glyph_row *row = NULL;
12275 #if GLYPH_DEBUG
12276 debug_method_add (w, "cursor movement");
12277 #endif
12279 /* Scroll if point within this distance from the top or bottom
12280 of the window. This is a pixel value. */
12281 this_scroll_margin = max (0, scroll_margin);
12282 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12283 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12285 top_scroll_margin = this_scroll_margin;
12286 if (WINDOW_WANTS_HEADER_LINE_P (w))
12287 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12289 /* Start with the row the cursor was displayed during the last
12290 not paused redisplay. Give up if that row is not valid. */
12291 if (w->last_cursor.vpos < 0
12292 || w->last_cursor.vpos >= w->current_matrix->nrows)
12293 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12294 else
12296 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12297 if (row->mode_line_p)
12298 ++row;
12299 if (!row->enabled_p)
12300 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12303 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12305 int scroll_p = 0;
12306 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12308 if (PT > XFASTINT (w->last_point))
12310 /* Point has moved forward. */
12311 while (MATRIX_ROW_END_CHARPOS (row) < PT
12312 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12314 xassert (row->enabled_p);
12315 ++row;
12318 /* The end position of a row equals the start position
12319 of the next row. If PT is there, we would rather
12320 display it in the next line. */
12321 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12322 && MATRIX_ROW_END_CHARPOS (row) == PT
12323 && !cursor_row_p (w, row))
12324 ++row;
12326 /* If within the scroll margin, scroll. Note that
12327 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12328 the next line would be drawn, and that
12329 this_scroll_margin can be zero. */
12330 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12331 || PT > MATRIX_ROW_END_CHARPOS (row)
12332 /* Line is completely visible last line in window
12333 and PT is to be set in the next line. */
12334 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12335 && PT == MATRIX_ROW_END_CHARPOS (row)
12336 && !row->ends_at_zv_p
12337 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12338 scroll_p = 1;
12340 else if (PT < XFASTINT (w->last_point))
12342 /* Cursor has to be moved backward. Note that PT >=
12343 CHARPOS (startp) because of the outer if-statement. */
12344 while (!row->mode_line_p
12345 && (MATRIX_ROW_START_CHARPOS (row) > PT
12346 || (MATRIX_ROW_START_CHARPOS (row) == PT
12347 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12348 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12349 row > w->current_matrix->rows
12350 && (row-1)->ends_in_newline_from_string_p))))
12351 && (row->y > top_scroll_margin
12352 || CHARPOS (startp) == BEGV))
12354 xassert (row->enabled_p);
12355 --row;
12358 /* Consider the following case: Window starts at BEGV,
12359 there is invisible, intangible text at BEGV, so that
12360 display starts at some point START > BEGV. It can
12361 happen that we are called with PT somewhere between
12362 BEGV and START. Try to handle that case. */
12363 if (row < w->current_matrix->rows
12364 || row->mode_line_p)
12366 row = w->current_matrix->rows;
12367 if (row->mode_line_p)
12368 ++row;
12371 /* Due to newlines in overlay strings, we may have to
12372 skip forward over overlay strings. */
12373 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12374 && MATRIX_ROW_END_CHARPOS (row) == PT
12375 && !cursor_row_p (w, row))
12376 ++row;
12378 /* If within the scroll margin, scroll. */
12379 if (row->y < top_scroll_margin
12380 && CHARPOS (startp) != BEGV)
12381 scroll_p = 1;
12383 else
12385 /* Cursor did not move. So don't scroll even if cursor line
12386 is partially visible, as it was so before. */
12387 rc = CURSOR_MOVEMENT_SUCCESS;
12390 if (PT < MATRIX_ROW_START_CHARPOS (row)
12391 || PT > MATRIX_ROW_END_CHARPOS (row))
12393 /* if PT is not in the glyph row, give up. */
12394 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12396 else if (rc != CURSOR_MOVEMENT_SUCCESS
12397 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12398 && make_cursor_line_fully_visible_p)
12400 if (PT == MATRIX_ROW_END_CHARPOS (row)
12401 && !row->ends_at_zv_p
12402 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12403 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12404 else if (row->height > window_box_height (w))
12406 /* If we end up in a partially visible line, let's
12407 make it fully visible, except when it's taller
12408 than the window, in which case we can't do much
12409 about it. */
12410 *scroll_step = 1;
12411 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12413 else
12415 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12416 if (!cursor_row_fully_visible_p (w, 0, 1))
12417 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12418 else
12419 rc = CURSOR_MOVEMENT_SUCCESS;
12422 else if (scroll_p)
12423 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12424 else
12426 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12427 rc = CURSOR_MOVEMENT_SUCCESS;
12432 return rc;
12435 void
12436 set_vertical_scroll_bar (w)
12437 struct window *w;
12439 int start, end, whole;
12441 /* Calculate the start and end positions for the current window.
12442 At some point, it would be nice to choose between scrollbars
12443 which reflect the whole buffer size, with special markers
12444 indicating narrowing, and scrollbars which reflect only the
12445 visible region.
12447 Note that mini-buffers sometimes aren't displaying any text. */
12448 if (!MINI_WINDOW_P (w)
12449 || (w == XWINDOW (minibuf_window)
12450 && NILP (echo_area_buffer[0])))
12452 struct buffer *buf = XBUFFER (w->buffer);
12453 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12454 start = marker_position (w->start) - BUF_BEGV (buf);
12455 /* I don't think this is guaranteed to be right. For the
12456 moment, we'll pretend it is. */
12457 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12459 if (end < start)
12460 end = start;
12461 if (whole < (end - start))
12462 whole = end - start;
12464 else
12465 start = end = whole = 0;
12467 /* Indicate what this scroll bar ought to be displaying now. */
12468 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12472 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12473 selected_window is redisplayed.
12475 We can return without actually redisplaying the window if
12476 fonts_changed_p is nonzero. In that case, redisplay_internal will
12477 retry. */
12479 static void
12480 redisplay_window (window, just_this_one_p)
12481 Lisp_Object window;
12482 int just_this_one_p;
12484 struct window *w = XWINDOW (window);
12485 struct frame *f = XFRAME (w->frame);
12486 struct buffer *buffer = XBUFFER (w->buffer);
12487 struct buffer *old = current_buffer;
12488 struct text_pos lpoint, opoint, startp;
12489 int update_mode_line;
12490 int tem;
12491 struct it it;
12492 /* Record it now because it's overwritten. */
12493 int current_matrix_up_to_date_p = 0;
12494 int used_current_matrix_p = 0;
12495 /* This is less strict than current_matrix_up_to_date_p.
12496 It indictes that the buffer contents and narrowing are unchanged. */
12497 int buffer_unchanged_p = 0;
12498 int temp_scroll_step = 0;
12499 int count = SPECPDL_INDEX ();
12500 int rc;
12501 int centering_position = -1;
12502 int last_line_misfit = 0;
12504 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12505 opoint = lpoint;
12507 /* W must be a leaf window here. */
12508 xassert (!NILP (w->buffer));
12509 #if GLYPH_DEBUG
12510 *w->desired_matrix->method = 0;
12511 #endif
12513 specbind (Qinhibit_point_motion_hooks, Qt);
12515 reconsider_clip_changes (w, buffer);
12517 /* Has the mode line to be updated? */
12518 update_mode_line = (!NILP (w->update_mode_line)
12519 || update_mode_lines
12520 || buffer->clip_changed
12521 || buffer->prevent_redisplay_optimizations_p);
12523 if (MINI_WINDOW_P (w))
12525 if (w == XWINDOW (echo_area_window)
12526 && !NILP (echo_area_buffer[0]))
12528 if (update_mode_line)
12529 /* We may have to update a tty frame's menu bar or a
12530 tool-bar. Example `M-x C-h C-h C-g'. */
12531 goto finish_menu_bars;
12532 else
12533 /* We've already displayed the echo area glyphs in this window. */
12534 goto finish_scroll_bars;
12536 else if ((w != XWINDOW (minibuf_window)
12537 || minibuf_level == 0)
12538 /* When buffer is nonempty, redisplay window normally. */
12539 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12540 /* Quail displays non-mini buffers in minibuffer window.
12541 In that case, redisplay the window normally. */
12542 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12544 /* W is a mini-buffer window, but it's not active, so clear
12545 it. */
12546 int yb = window_text_bottom_y (w);
12547 struct glyph_row *row;
12548 int y;
12550 for (y = 0, row = w->desired_matrix->rows;
12551 y < yb;
12552 y += row->height, ++row)
12553 blank_row (w, row, y);
12554 goto finish_scroll_bars;
12557 clear_glyph_matrix (w->desired_matrix);
12560 /* Otherwise set up data on this window; select its buffer and point
12561 value. */
12562 /* Really select the buffer, for the sake of buffer-local
12563 variables. */
12564 set_buffer_internal_1 (XBUFFER (w->buffer));
12565 SET_TEXT_POS (opoint, PT, PT_BYTE);
12567 current_matrix_up_to_date_p
12568 = (!NILP (w->window_end_valid)
12569 && !current_buffer->clip_changed
12570 && !current_buffer->prevent_redisplay_optimizations_p
12571 && XFASTINT (w->last_modified) >= MODIFF
12572 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12574 buffer_unchanged_p
12575 = (!NILP (w->window_end_valid)
12576 && !current_buffer->clip_changed
12577 && XFASTINT (w->last_modified) >= MODIFF
12578 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12580 /* When windows_or_buffers_changed is non-zero, we can't rely on
12581 the window end being valid, so set it to nil there. */
12582 if (windows_or_buffers_changed)
12584 /* If window starts on a continuation line, maybe adjust the
12585 window start in case the window's width changed. */
12586 if (XMARKER (w->start)->buffer == current_buffer)
12587 compute_window_start_on_continuation_line (w);
12589 w->window_end_valid = Qnil;
12592 /* Some sanity checks. */
12593 CHECK_WINDOW_END (w);
12594 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12595 abort ();
12596 if (BYTEPOS (opoint) < CHARPOS (opoint))
12597 abort ();
12599 /* If %c is in mode line, update it if needed. */
12600 if (!NILP (w->column_number_displayed)
12601 /* This alternative quickly identifies a common case
12602 where no change is needed. */
12603 && !(PT == XFASTINT (w->last_point)
12604 && XFASTINT (w->last_modified) >= MODIFF
12605 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12606 && (XFASTINT (w->column_number_displayed)
12607 != (int) current_column ())) /* iftc */
12608 update_mode_line = 1;
12610 /* Count number of windows showing the selected buffer. An indirect
12611 buffer counts as its base buffer. */
12612 if (!just_this_one_p)
12614 struct buffer *current_base, *window_base;
12615 current_base = current_buffer;
12616 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12617 if (current_base->base_buffer)
12618 current_base = current_base->base_buffer;
12619 if (window_base->base_buffer)
12620 window_base = window_base->base_buffer;
12621 if (current_base == window_base)
12622 buffer_shared++;
12625 /* Point refers normally to the selected window. For any other
12626 window, set up appropriate value. */
12627 if (!EQ (window, selected_window))
12629 int new_pt = XMARKER (w->pointm)->charpos;
12630 int new_pt_byte = marker_byte_position (w->pointm);
12631 if (new_pt < BEGV)
12633 new_pt = BEGV;
12634 new_pt_byte = BEGV_BYTE;
12635 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12637 else if (new_pt > (ZV - 1))
12639 new_pt = ZV;
12640 new_pt_byte = ZV_BYTE;
12641 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12644 /* We don't use SET_PT so that the point-motion hooks don't run. */
12645 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12648 /* If any of the character widths specified in the display table
12649 have changed, invalidate the width run cache. It's true that
12650 this may be a bit late to catch such changes, but the rest of
12651 redisplay goes (non-fatally) haywire when the display table is
12652 changed, so why should we worry about doing any better? */
12653 if (current_buffer->width_run_cache)
12655 struct Lisp_Char_Table *disptab = buffer_display_table ();
12657 if (! disptab_matches_widthtab (disptab,
12658 XVECTOR (current_buffer->width_table)))
12660 invalidate_region_cache (current_buffer,
12661 current_buffer->width_run_cache,
12662 BEG, Z);
12663 recompute_width_table (current_buffer, disptab);
12667 /* If window-start is screwed up, choose a new one. */
12668 if (XMARKER (w->start)->buffer != current_buffer)
12669 goto recenter;
12671 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12673 /* If someone specified a new starting point but did not insist,
12674 check whether it can be used. */
12675 if (!NILP (w->optional_new_start)
12676 && CHARPOS (startp) >= BEGV
12677 && CHARPOS (startp) <= ZV)
12679 w->optional_new_start = Qnil;
12680 start_display (&it, w, startp);
12681 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12682 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12683 if (IT_CHARPOS (it) == PT)
12684 w->force_start = Qt;
12685 /* IT may overshoot PT if text at PT is invisible. */
12686 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12687 w->force_start = Qt;
12692 /* Handle case where place to start displaying has been specified,
12693 unless the specified location is outside the accessible range. */
12694 if (!NILP (w->force_start)
12695 || w->frozen_window_start_p)
12697 /* We set this later on if we have to adjust point. */
12698 int new_vpos = -1;
12699 int val;
12701 w->force_start = Qnil;
12702 w->vscroll = 0;
12703 w->window_end_valid = Qnil;
12705 /* Forget any recorded base line for line number display. */
12706 if (!buffer_unchanged_p)
12707 w->base_line_number = Qnil;
12709 /* Redisplay the mode line. Select the buffer properly for that.
12710 Also, run the hook window-scroll-functions
12711 because we have scrolled. */
12712 /* Note, we do this after clearing force_start because
12713 if there's an error, it is better to forget about force_start
12714 than to get into an infinite loop calling the hook functions
12715 and having them get more errors. */
12716 if (!update_mode_line
12717 || ! NILP (Vwindow_scroll_functions))
12719 update_mode_line = 1;
12720 w->update_mode_line = Qt;
12721 startp = run_window_scroll_functions (window, startp);
12724 w->last_modified = make_number (0);
12725 w->last_overlay_modified = make_number (0);
12726 if (CHARPOS (startp) < BEGV)
12727 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12728 else if (CHARPOS (startp) > ZV)
12729 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12731 /* Redisplay, then check if cursor has been set during the
12732 redisplay. Give up if new fonts were loaded. */
12733 val = try_window (window, startp, 1);
12734 if (!val)
12736 w->force_start = Qt;
12737 clear_glyph_matrix (w->desired_matrix);
12738 goto need_larger_matrices;
12740 /* Point was outside the scroll margins. */
12741 if (val < 0)
12742 new_vpos = window_box_height (w) / 2;
12744 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12746 /* If point does not appear, try to move point so it does
12747 appear. The desired matrix has been built above, so we
12748 can use it here. */
12749 new_vpos = window_box_height (w) / 2;
12752 if (!cursor_row_fully_visible_p (w, 0, 0))
12754 /* Point does appear, but on a line partly visible at end of window.
12755 Move it back to a fully-visible line. */
12756 new_vpos = window_box_height (w);
12759 /* If we need to move point for either of the above reasons,
12760 now actually do it. */
12761 if (new_vpos >= 0)
12763 struct glyph_row *row;
12765 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12766 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12767 ++row;
12769 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12770 MATRIX_ROW_START_BYTEPOS (row));
12772 if (w != XWINDOW (selected_window))
12773 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12774 else if (current_buffer == old)
12775 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12777 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12779 /* If we are highlighting the region, then we just changed
12780 the region, so redisplay to show it. */
12781 if (!NILP (Vtransient_mark_mode)
12782 && !NILP (current_buffer->mark_active))
12784 clear_glyph_matrix (w->desired_matrix);
12785 if (!try_window (window, startp, 0))
12786 goto need_larger_matrices;
12790 #if GLYPH_DEBUG
12791 debug_method_add (w, "forced window start");
12792 #endif
12793 goto done;
12796 /* Handle case where text has not changed, only point, and it has
12797 not moved off the frame, and we are not retrying after hscroll.
12798 (current_matrix_up_to_date_p is nonzero when retrying.) */
12799 if (current_matrix_up_to_date_p
12800 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12801 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12803 switch (rc)
12805 case CURSOR_MOVEMENT_SUCCESS:
12806 used_current_matrix_p = 1;
12807 goto done;
12809 #if 0 /* try_cursor_movement never returns this value. */
12810 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12811 goto need_larger_matrices;
12812 #endif
12814 case CURSOR_MOVEMENT_MUST_SCROLL:
12815 goto try_to_scroll;
12817 default:
12818 abort ();
12821 /* If current starting point was originally the beginning of a line
12822 but no longer is, find a new starting point. */
12823 else if (!NILP (w->start_at_line_beg)
12824 && !(CHARPOS (startp) <= BEGV
12825 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12827 #if GLYPH_DEBUG
12828 debug_method_add (w, "recenter 1");
12829 #endif
12830 goto recenter;
12833 /* Try scrolling with try_window_id. Value is > 0 if update has
12834 been done, it is -1 if we know that the same window start will
12835 not work. It is 0 if unsuccessful for some other reason. */
12836 else if ((tem = try_window_id (w)) != 0)
12838 #if GLYPH_DEBUG
12839 debug_method_add (w, "try_window_id %d", tem);
12840 #endif
12842 if (fonts_changed_p)
12843 goto need_larger_matrices;
12844 if (tem > 0)
12845 goto done;
12847 /* Otherwise try_window_id has returned -1 which means that we
12848 don't want the alternative below this comment to execute. */
12850 else if (CHARPOS (startp) >= BEGV
12851 && CHARPOS (startp) <= ZV
12852 && PT >= CHARPOS (startp)
12853 && (CHARPOS (startp) < ZV
12854 /* Avoid starting at end of buffer. */
12855 || CHARPOS (startp) == BEGV
12856 || (XFASTINT (w->last_modified) >= MODIFF
12857 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12859 #if GLYPH_DEBUG
12860 debug_method_add (w, "same window start");
12861 #endif
12863 /* Try to redisplay starting at same place as before.
12864 If point has not moved off frame, accept the results. */
12865 if (!current_matrix_up_to_date_p
12866 /* Don't use try_window_reusing_current_matrix in this case
12867 because a window scroll function can have changed the
12868 buffer. */
12869 || !NILP (Vwindow_scroll_functions)
12870 || MINI_WINDOW_P (w)
12871 || !(used_current_matrix_p
12872 = try_window_reusing_current_matrix (w)))
12874 IF_DEBUG (debug_method_add (w, "1"));
12875 if (try_window (window, startp, 1) < 0)
12876 /* -1 means we need to scroll.
12877 0 means we need new matrices, but fonts_changed_p
12878 is set in that case, so we will detect it below. */
12879 goto try_to_scroll;
12882 if (fonts_changed_p)
12883 goto need_larger_matrices;
12885 if (w->cursor.vpos >= 0)
12887 if (!just_this_one_p
12888 || current_buffer->clip_changed
12889 || BEG_UNCHANGED < CHARPOS (startp))
12890 /* Forget any recorded base line for line number display. */
12891 w->base_line_number = Qnil;
12893 if (!cursor_row_fully_visible_p (w, 1, 0))
12895 clear_glyph_matrix (w->desired_matrix);
12896 last_line_misfit = 1;
12898 /* Drop through and scroll. */
12899 else
12900 goto done;
12902 else
12903 clear_glyph_matrix (w->desired_matrix);
12906 try_to_scroll:
12908 w->last_modified = make_number (0);
12909 w->last_overlay_modified = make_number (0);
12911 /* Redisplay the mode line. Select the buffer properly for that. */
12912 if (!update_mode_line)
12914 update_mode_line = 1;
12915 w->update_mode_line = Qt;
12918 /* Try to scroll by specified few lines. */
12919 if ((scroll_conservatively
12920 || scroll_step
12921 || temp_scroll_step
12922 || NUMBERP (current_buffer->scroll_up_aggressively)
12923 || NUMBERP (current_buffer->scroll_down_aggressively))
12924 && !current_buffer->clip_changed
12925 && CHARPOS (startp) >= BEGV
12926 && CHARPOS (startp) <= ZV)
12928 /* The function returns -1 if new fonts were loaded, 1 if
12929 successful, 0 if not successful. */
12930 int rc = try_scrolling (window, just_this_one_p,
12931 scroll_conservatively,
12932 scroll_step,
12933 temp_scroll_step, last_line_misfit);
12934 switch (rc)
12936 case SCROLLING_SUCCESS:
12937 goto done;
12939 case SCROLLING_NEED_LARGER_MATRICES:
12940 goto need_larger_matrices;
12942 case SCROLLING_FAILED:
12943 break;
12945 default:
12946 abort ();
12950 /* Finally, just choose place to start which centers point */
12952 recenter:
12953 if (centering_position < 0)
12954 centering_position = window_box_height (w) / 2;
12956 #if GLYPH_DEBUG
12957 debug_method_add (w, "recenter");
12958 #endif
12960 /* w->vscroll = 0; */
12962 /* Forget any previously recorded base line for line number display. */
12963 if (!buffer_unchanged_p)
12964 w->base_line_number = Qnil;
12966 /* Move backward half the height of the window. */
12967 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12968 it.current_y = it.last_visible_y;
12969 move_it_vertically_backward (&it, centering_position);
12970 xassert (IT_CHARPOS (it) >= BEGV);
12972 /* The function move_it_vertically_backward may move over more
12973 than the specified y-distance. If it->w is small, e.g. a
12974 mini-buffer window, we may end up in front of the window's
12975 display area. Start displaying at the start of the line
12976 containing PT in this case. */
12977 if (it.current_y <= 0)
12979 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12980 move_it_vertically_backward (&it, 0);
12981 #if 0
12982 /* I think this assert is bogus if buffer contains
12983 invisible text or images. KFS. */
12984 xassert (IT_CHARPOS (it) <= PT);
12985 #endif
12986 it.current_y = 0;
12989 it.current_x = it.hpos = 0;
12991 /* Set startp here explicitly in case that helps avoid an infinite loop
12992 in case the window-scroll-functions functions get errors. */
12993 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12995 /* Run scroll hooks. */
12996 startp = run_window_scroll_functions (window, it.current.pos);
12998 /* Redisplay the window. */
12999 if (!current_matrix_up_to_date_p
13000 || windows_or_buffers_changed
13001 || cursor_type_changed
13002 /* Don't use try_window_reusing_current_matrix in this case
13003 because it can have changed the buffer. */
13004 || !NILP (Vwindow_scroll_functions)
13005 || !just_this_one_p
13006 || MINI_WINDOW_P (w)
13007 || !(used_current_matrix_p
13008 = try_window_reusing_current_matrix (w)))
13009 try_window (window, startp, 0);
13011 /* If new fonts have been loaded (due to fontsets), give up. We
13012 have to start a new redisplay since we need to re-adjust glyph
13013 matrices. */
13014 if (fonts_changed_p)
13015 goto need_larger_matrices;
13017 /* If cursor did not appear assume that the middle of the window is
13018 in the first line of the window. Do it again with the next line.
13019 (Imagine a window of height 100, displaying two lines of height
13020 60. Moving back 50 from it->last_visible_y will end in the first
13021 line.) */
13022 if (w->cursor.vpos < 0)
13024 if (!NILP (w->window_end_valid)
13025 && PT >= Z - XFASTINT (w->window_end_pos))
13027 clear_glyph_matrix (w->desired_matrix);
13028 move_it_by_lines (&it, 1, 0);
13029 try_window (window, it.current.pos, 0);
13031 else if (PT < IT_CHARPOS (it))
13033 clear_glyph_matrix (w->desired_matrix);
13034 move_it_by_lines (&it, -1, 0);
13035 try_window (window, it.current.pos, 0);
13037 else
13039 /* Not much we can do about it. */
13043 /* Consider the following case: Window starts at BEGV, there is
13044 invisible, intangible text at BEGV, so that display starts at
13045 some point START > BEGV. It can happen that we are called with
13046 PT somewhere between BEGV and START. Try to handle that case. */
13047 if (w->cursor.vpos < 0)
13049 struct glyph_row *row = w->current_matrix->rows;
13050 if (row->mode_line_p)
13051 ++row;
13052 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13055 if (!cursor_row_fully_visible_p (w, 0, 0))
13057 /* If vscroll is enabled, disable it and try again. */
13058 if (w->vscroll)
13060 w->vscroll = 0;
13061 clear_glyph_matrix (w->desired_matrix);
13062 goto recenter;
13065 /* If centering point failed to make the whole line visible,
13066 put point at the top instead. That has to make the whole line
13067 visible, if it can be done. */
13068 if (centering_position == 0)
13069 goto done;
13071 clear_glyph_matrix (w->desired_matrix);
13072 centering_position = 0;
13073 goto recenter;
13076 done:
13078 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13079 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13080 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13081 ? Qt : Qnil);
13083 /* Display the mode line, if we must. */
13084 if ((update_mode_line
13085 /* If window not full width, must redo its mode line
13086 if (a) the window to its side is being redone and
13087 (b) we do a frame-based redisplay. This is a consequence
13088 of how inverted lines are drawn in frame-based redisplay. */
13089 || (!just_this_one_p
13090 && !FRAME_WINDOW_P (f)
13091 && !WINDOW_FULL_WIDTH_P (w))
13092 /* Line number to display. */
13093 || INTEGERP (w->base_line_pos)
13094 /* Column number is displayed and different from the one displayed. */
13095 || (!NILP (w->column_number_displayed)
13096 && (XFASTINT (w->column_number_displayed)
13097 != (int) current_column ()))) /* iftc */
13098 /* This means that the window has a mode line. */
13099 && (WINDOW_WANTS_MODELINE_P (w)
13100 || WINDOW_WANTS_HEADER_LINE_P (w)))
13102 display_mode_lines (w);
13104 /* If mode line height has changed, arrange for a thorough
13105 immediate redisplay using the correct mode line height. */
13106 if (WINDOW_WANTS_MODELINE_P (w)
13107 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13109 fonts_changed_p = 1;
13110 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13111 = DESIRED_MODE_LINE_HEIGHT (w);
13114 /* If top line height has changed, arrange for a thorough
13115 immediate redisplay using the correct mode line height. */
13116 if (WINDOW_WANTS_HEADER_LINE_P (w)
13117 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13119 fonts_changed_p = 1;
13120 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13121 = DESIRED_HEADER_LINE_HEIGHT (w);
13124 if (fonts_changed_p)
13125 goto need_larger_matrices;
13128 if (!line_number_displayed
13129 && !BUFFERP (w->base_line_pos))
13131 w->base_line_pos = Qnil;
13132 w->base_line_number = Qnil;
13135 finish_menu_bars:
13137 /* When we reach a frame's selected window, redo the frame's menu bar. */
13138 if (update_mode_line
13139 && EQ (FRAME_SELECTED_WINDOW (f), window))
13141 int redisplay_menu_p = 0;
13142 int redisplay_tool_bar_p = 0;
13144 if (FRAME_WINDOW_P (f))
13146 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13147 || defined (USE_GTK)
13148 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13149 #else
13150 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13151 #endif
13153 else
13154 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13156 if (redisplay_menu_p)
13157 display_menu_bar (w);
13159 #ifdef HAVE_WINDOW_SYSTEM
13160 #ifdef USE_GTK
13161 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13162 #else
13163 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13164 && (FRAME_TOOL_BAR_LINES (f) > 0
13165 || auto_resize_tool_bars_p);
13167 #endif
13169 if (redisplay_tool_bar_p)
13170 redisplay_tool_bar (f);
13171 #endif
13174 #ifdef HAVE_WINDOW_SYSTEM
13175 if (FRAME_WINDOW_P (f)
13176 && update_window_fringes (w, (just_this_one_p
13177 || (!used_current_matrix_p && !overlay_arrow_seen)
13178 || w->pseudo_window_p)))
13180 update_begin (f);
13181 BLOCK_INPUT;
13182 if (draw_window_fringes (w, 1))
13183 x_draw_vertical_border (w);
13184 UNBLOCK_INPUT;
13185 update_end (f);
13187 #endif /* HAVE_WINDOW_SYSTEM */
13189 /* We go to this label, with fonts_changed_p nonzero,
13190 if it is necessary to try again using larger glyph matrices.
13191 We have to redeem the scroll bar even in this case,
13192 because the loop in redisplay_internal expects that. */
13193 need_larger_matrices:
13195 finish_scroll_bars:
13197 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13199 /* Set the thumb's position and size. */
13200 set_vertical_scroll_bar (w);
13202 /* Note that we actually used the scroll bar attached to this
13203 window, so it shouldn't be deleted at the end of redisplay. */
13204 redeem_scroll_bar_hook (w);
13207 /* Restore current_buffer and value of point in it. */
13208 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13209 set_buffer_internal_1 (old);
13210 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13212 unbind_to (count, Qnil);
13216 /* Build the complete desired matrix of WINDOW with a window start
13217 buffer position POS.
13219 Value is 1 if successful. It is zero if fonts were loaded during
13220 redisplay which makes re-adjusting glyph matrices necessary, and -1
13221 if point would appear in the scroll margins.
13222 (We check that only if CHECK_MARGINS is nonzero. */
13225 try_window (window, pos, check_margins)
13226 Lisp_Object window;
13227 struct text_pos pos;
13228 int check_margins;
13230 struct window *w = XWINDOW (window);
13231 struct it it;
13232 struct glyph_row *last_text_row = NULL;
13234 /* Make POS the new window start. */
13235 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13237 /* Mark cursor position as unknown. No overlay arrow seen. */
13238 w->cursor.vpos = -1;
13239 overlay_arrow_seen = 0;
13241 /* Initialize iterator and info to start at POS. */
13242 start_display (&it, w, pos);
13244 /* Display all lines of W. */
13245 while (it.current_y < it.last_visible_y)
13247 if (display_line (&it))
13248 last_text_row = it.glyph_row - 1;
13249 if (fonts_changed_p)
13250 return 0;
13253 /* Don't let the cursor end in the scroll margins. */
13254 if (check_margins
13255 && !MINI_WINDOW_P (w))
13257 int this_scroll_margin;
13259 this_scroll_margin = max (0, scroll_margin);
13260 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13261 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13263 if ((w->cursor.y < this_scroll_margin
13264 && CHARPOS (pos) > BEGV
13265 && IT_CHARPOS (it) < ZV)
13266 /* rms: considering make_cursor_line_fully_visible_p here
13267 seems to give wrong results. We don't want to recenter
13268 when the last line is partly visible, we want to allow
13269 that case to be handled in the usual way. */
13270 || (w->cursor.y + 1) > it.last_visible_y)
13272 w->cursor.vpos = -1;
13273 clear_glyph_matrix (w->desired_matrix);
13274 return -1;
13278 /* If bottom moved off end of frame, change mode line percentage. */
13279 if (XFASTINT (w->window_end_pos) <= 0
13280 && Z != IT_CHARPOS (it))
13281 w->update_mode_line = Qt;
13283 /* Set window_end_pos to the offset of the last character displayed
13284 on the window from the end of current_buffer. Set
13285 window_end_vpos to its row number. */
13286 if (last_text_row)
13288 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13289 w->window_end_bytepos
13290 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13291 w->window_end_pos
13292 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13293 w->window_end_vpos
13294 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13295 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13296 ->displays_text_p);
13298 else
13300 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13301 w->window_end_pos = make_number (Z - ZV);
13302 w->window_end_vpos = make_number (0);
13305 /* But that is not valid info until redisplay finishes. */
13306 w->window_end_valid = Qnil;
13307 return 1;
13312 /************************************************************************
13313 Window redisplay reusing current matrix when buffer has not changed
13314 ************************************************************************/
13316 /* Try redisplay of window W showing an unchanged buffer with a
13317 different window start than the last time it was displayed by
13318 reusing its current matrix. Value is non-zero if successful.
13319 W->start is the new window start. */
13321 static int
13322 try_window_reusing_current_matrix (w)
13323 struct window *w;
13325 struct frame *f = XFRAME (w->frame);
13326 struct glyph_row *row, *bottom_row;
13327 struct it it;
13328 struct run run;
13329 struct text_pos start, new_start;
13330 int nrows_scrolled, i;
13331 struct glyph_row *last_text_row;
13332 struct glyph_row *last_reused_text_row;
13333 struct glyph_row *start_row;
13334 int start_vpos, min_y, max_y;
13336 #if GLYPH_DEBUG
13337 if (inhibit_try_window_reusing)
13338 return 0;
13339 #endif
13341 if (/* This function doesn't handle terminal frames. */
13342 !FRAME_WINDOW_P (f)
13343 /* Don't try to reuse the display if windows have been split
13344 or such. */
13345 || windows_or_buffers_changed
13346 || cursor_type_changed)
13347 return 0;
13349 /* Can't do this if region may have changed. */
13350 if ((!NILP (Vtransient_mark_mode)
13351 && !NILP (current_buffer->mark_active))
13352 || !NILP (w->region_showing)
13353 || !NILP (Vshow_trailing_whitespace))
13354 return 0;
13356 /* If top-line visibility has changed, give up. */
13357 if (WINDOW_WANTS_HEADER_LINE_P (w)
13358 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13359 return 0;
13361 /* Give up if old or new display is scrolled vertically. We could
13362 make this function handle this, but right now it doesn't. */
13363 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13364 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13365 return 0;
13367 /* The variable new_start now holds the new window start. The old
13368 start `start' can be determined from the current matrix. */
13369 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13370 start = start_row->start.pos;
13371 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13373 /* Clear the desired matrix for the display below. */
13374 clear_glyph_matrix (w->desired_matrix);
13376 if (CHARPOS (new_start) <= CHARPOS (start))
13378 int first_row_y;
13380 /* Don't use this method if the display starts with an ellipsis
13381 displayed for invisible text. It's not easy to handle that case
13382 below, and it's certainly not worth the effort since this is
13383 not a frequent case. */
13384 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13385 return 0;
13387 IF_DEBUG (debug_method_add (w, "twu1"));
13389 /* Display up to a row that can be reused. The variable
13390 last_text_row is set to the last row displayed that displays
13391 text. Note that it.vpos == 0 if or if not there is a
13392 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13393 start_display (&it, w, new_start);
13394 first_row_y = it.current_y;
13395 w->cursor.vpos = -1;
13396 last_text_row = last_reused_text_row = NULL;
13398 while (it.current_y < it.last_visible_y
13399 && !fonts_changed_p)
13401 /* If we have reached into the characters in the START row,
13402 that means the line boundaries have changed. So we
13403 can't start copying with the row START. Maybe it will
13404 work to start copying with the following row. */
13405 while (IT_CHARPOS (it) > CHARPOS (start))
13407 /* Advance to the next row as the "start". */
13408 start_row++;
13409 start = start_row->start.pos;
13410 /* If there are no more rows to try, or just one, give up. */
13411 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13412 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13413 || CHARPOS (start) == ZV)
13415 clear_glyph_matrix (w->desired_matrix);
13416 return 0;
13419 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13421 /* If we have reached alignment,
13422 we can copy the rest of the rows. */
13423 if (IT_CHARPOS (it) == CHARPOS (start))
13424 break;
13426 if (display_line (&it))
13427 last_text_row = it.glyph_row - 1;
13430 /* A value of current_y < last_visible_y means that we stopped
13431 at the previous window start, which in turn means that we
13432 have at least one reusable row. */
13433 if (it.current_y < it.last_visible_y)
13435 /* IT.vpos always starts from 0; it counts text lines. */
13436 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13438 /* Find PT if not already found in the lines displayed. */
13439 if (w->cursor.vpos < 0)
13441 int dy = it.current_y - start_row->y;
13443 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13444 row = row_containing_pos (w, PT, row, NULL, dy);
13445 if (row)
13446 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13447 dy, nrows_scrolled);
13448 else
13450 clear_glyph_matrix (w->desired_matrix);
13451 return 0;
13455 /* Scroll the display. Do it before the current matrix is
13456 changed. The problem here is that update has not yet
13457 run, i.e. part of the current matrix is not up to date.
13458 scroll_run_hook will clear the cursor, and use the
13459 current matrix to get the height of the row the cursor is
13460 in. */
13461 run.current_y = start_row->y;
13462 run.desired_y = it.current_y;
13463 run.height = it.last_visible_y - it.current_y;
13465 if (run.height > 0 && run.current_y != run.desired_y)
13467 update_begin (f);
13468 rif->update_window_begin_hook (w);
13469 rif->clear_window_mouse_face (w);
13470 rif->scroll_run_hook (w, &run);
13471 rif->update_window_end_hook (w, 0, 0);
13472 update_end (f);
13475 /* Shift current matrix down by nrows_scrolled lines. */
13476 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13477 rotate_matrix (w->current_matrix,
13478 start_vpos,
13479 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13480 nrows_scrolled);
13482 /* Disable lines that must be updated. */
13483 for (i = 0; i < it.vpos; ++i)
13484 (start_row + i)->enabled_p = 0;
13486 /* Re-compute Y positions. */
13487 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13488 max_y = it.last_visible_y;
13489 for (row = start_row + nrows_scrolled;
13490 row < bottom_row;
13491 ++row)
13493 row->y = it.current_y;
13494 row->visible_height = row->height;
13496 if (row->y < min_y)
13497 row->visible_height -= min_y - row->y;
13498 if (row->y + row->height > max_y)
13499 row->visible_height -= row->y + row->height - max_y;
13500 row->redraw_fringe_bitmaps_p = 1;
13502 it.current_y += row->height;
13504 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13505 last_reused_text_row = row;
13506 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13507 break;
13510 /* Disable lines in the current matrix which are now
13511 below the window. */
13512 for (++row; row < bottom_row; ++row)
13513 row->enabled_p = row->mode_line_p = 0;
13516 /* Update window_end_pos etc.; last_reused_text_row is the last
13517 reused row from the current matrix containing text, if any.
13518 The value of last_text_row is the last displayed line
13519 containing text. */
13520 if (last_reused_text_row)
13522 w->window_end_bytepos
13523 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13524 w->window_end_pos
13525 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13526 w->window_end_vpos
13527 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13528 w->current_matrix));
13530 else if (last_text_row)
13532 w->window_end_bytepos
13533 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13534 w->window_end_pos
13535 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13536 w->window_end_vpos
13537 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13539 else
13541 /* This window must be completely empty. */
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);
13546 w->window_end_valid = Qnil;
13548 /* Update hint: don't try scrolling again in update_window. */
13549 w->desired_matrix->no_scrolling_p = 1;
13551 #if GLYPH_DEBUG
13552 debug_method_add (w, "try_window_reusing_current_matrix 1");
13553 #endif
13554 return 1;
13556 else if (CHARPOS (new_start) > CHARPOS (start))
13558 struct glyph_row *pt_row, *row;
13559 struct glyph_row *first_reusable_row;
13560 struct glyph_row *first_row_to_display;
13561 int dy;
13562 int yb = window_text_bottom_y (w);
13564 /* Find the row starting at new_start, if there is one. Don't
13565 reuse a partially visible line at the end. */
13566 first_reusable_row = start_row;
13567 while (first_reusable_row->enabled_p
13568 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13569 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13570 < CHARPOS (new_start)))
13571 ++first_reusable_row;
13573 /* Give up if there is no row to reuse. */
13574 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13575 || !first_reusable_row->enabled_p
13576 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13577 != CHARPOS (new_start)))
13578 return 0;
13580 /* We can reuse fully visible rows beginning with
13581 first_reusable_row to the end of the window. Set
13582 first_row_to_display to the first row that cannot be reused.
13583 Set pt_row to the row containing point, if there is any. */
13584 pt_row = NULL;
13585 for (first_row_to_display = first_reusable_row;
13586 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13587 ++first_row_to_display)
13589 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13590 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13591 pt_row = first_row_to_display;
13594 /* Start displaying at the start of first_row_to_display. */
13595 xassert (first_row_to_display->y < yb);
13596 init_to_row_start (&it, w, first_row_to_display);
13598 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13599 - start_vpos);
13600 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13601 - nrows_scrolled);
13602 it.current_y = (first_row_to_display->y - first_reusable_row->y
13603 + WINDOW_HEADER_LINE_HEIGHT (w));
13605 /* Display lines beginning with first_row_to_display in the
13606 desired matrix. Set last_text_row to the last row displayed
13607 that displays text. */
13608 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13609 if (pt_row == NULL)
13610 w->cursor.vpos = -1;
13611 last_text_row = NULL;
13612 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13613 if (display_line (&it))
13614 last_text_row = it.glyph_row - 1;
13616 /* Give up If point isn't in a row displayed or reused. */
13617 if (w->cursor.vpos < 0)
13619 clear_glyph_matrix (w->desired_matrix);
13620 return 0;
13623 /* If point is in a reused row, adjust y and vpos of the cursor
13624 position. */
13625 if (pt_row)
13627 w->cursor.vpos -= nrows_scrolled;
13628 w->cursor.y -= first_reusable_row->y - start_row->y;
13631 /* Scroll the display. */
13632 run.current_y = first_reusable_row->y;
13633 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13634 run.height = it.last_visible_y - run.current_y;
13635 dy = run.current_y - run.desired_y;
13637 if (run.height)
13639 update_begin (f);
13640 rif->update_window_begin_hook (w);
13641 rif->clear_window_mouse_face (w);
13642 rif->scroll_run_hook (w, &run);
13643 rif->update_window_end_hook (w, 0, 0);
13644 update_end (f);
13647 /* Adjust Y positions of reused rows. */
13648 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13649 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13650 max_y = it.last_visible_y;
13651 for (row = first_reusable_row; row < first_row_to_display; ++row)
13653 row->y -= dy;
13654 row->visible_height = row->height;
13655 if (row->y < min_y)
13656 row->visible_height -= min_y - row->y;
13657 if (row->y + row->height > max_y)
13658 row->visible_height -= row->y + row->height - max_y;
13659 row->redraw_fringe_bitmaps_p = 1;
13662 /* Scroll the current matrix. */
13663 xassert (nrows_scrolled > 0);
13664 rotate_matrix (w->current_matrix,
13665 start_vpos,
13666 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13667 -nrows_scrolled);
13669 /* Disable rows not reused. */
13670 for (row -= nrows_scrolled; row < bottom_row; ++row)
13671 row->enabled_p = 0;
13673 /* Point may have moved to a different line, so we cannot assume that
13674 the previous cursor position is valid; locate the correct row. */
13675 if (pt_row)
13677 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13678 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13679 row++)
13681 w->cursor.vpos++;
13682 w->cursor.y = row->y;
13684 if (row < bottom_row)
13686 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13687 while (glyph->charpos < PT)
13689 w->cursor.hpos++;
13690 w->cursor.x += glyph->pixel_width;
13691 glyph++;
13696 /* Adjust window end. A null value of last_text_row means that
13697 the window end is in reused rows which in turn means that
13698 only its vpos can have changed. */
13699 if (last_text_row)
13701 w->window_end_bytepos
13702 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13703 w->window_end_pos
13704 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13705 w->window_end_vpos
13706 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13708 else
13710 w->window_end_vpos
13711 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13714 w->window_end_valid = Qnil;
13715 w->desired_matrix->no_scrolling_p = 1;
13717 #if GLYPH_DEBUG
13718 debug_method_add (w, "try_window_reusing_current_matrix 2");
13719 #endif
13720 return 1;
13723 return 0;
13728 /************************************************************************
13729 Window redisplay reusing current matrix when buffer has changed
13730 ************************************************************************/
13732 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13733 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13734 int *, int *));
13735 static struct glyph_row *
13736 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13737 struct glyph_row *));
13740 /* Return the last row in MATRIX displaying text. If row START is
13741 non-null, start searching with that row. IT gives the dimensions
13742 of the display. Value is null if matrix is empty; otherwise it is
13743 a pointer to the row found. */
13745 static struct glyph_row *
13746 find_last_row_displaying_text (matrix, it, start)
13747 struct glyph_matrix *matrix;
13748 struct it *it;
13749 struct glyph_row *start;
13751 struct glyph_row *row, *row_found;
13753 /* Set row_found to the last row in IT->w's current matrix
13754 displaying text. The loop looks funny but think of partially
13755 visible lines. */
13756 row_found = NULL;
13757 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13758 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13760 xassert (row->enabled_p);
13761 row_found = row;
13762 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13763 break;
13764 ++row;
13767 return row_found;
13771 /* Return the last row in the current matrix of W that is not affected
13772 by changes at the start of current_buffer that occurred since W's
13773 current matrix was built. Value is null if no such row exists.
13775 BEG_UNCHANGED us the number of characters unchanged at the start of
13776 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13777 first changed character in current_buffer. Characters at positions <
13778 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13779 when the current matrix was built. */
13781 static struct glyph_row *
13782 find_last_unchanged_at_beg_row (w)
13783 struct window *w;
13785 int first_changed_pos = BEG + BEG_UNCHANGED;
13786 struct glyph_row *row;
13787 struct glyph_row *row_found = NULL;
13788 int yb = window_text_bottom_y (w);
13790 /* Find the last row displaying unchanged text. */
13791 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13792 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13793 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13795 if (/* If row ends before first_changed_pos, it is unchanged,
13796 except in some case. */
13797 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13798 /* When row ends in ZV and we write at ZV it is not
13799 unchanged. */
13800 && !row->ends_at_zv_p
13801 /* When first_changed_pos is the end of a continued line,
13802 row is not unchanged because it may be no longer
13803 continued. */
13804 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13805 && (row->continued_p
13806 || row->exact_window_width_line_p)))
13807 row_found = row;
13809 /* Stop if last visible row. */
13810 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13811 break;
13813 ++row;
13816 return row_found;
13820 /* Find the first glyph row in the current matrix of W that is not
13821 affected by changes at the end of current_buffer since the
13822 time W's current matrix was built.
13824 Return in *DELTA the number of chars by which buffer positions in
13825 unchanged text at the end of current_buffer must be adjusted.
13827 Return in *DELTA_BYTES the corresponding number of bytes.
13829 Value is null if no such row exists, i.e. all rows are affected by
13830 changes. */
13832 static struct glyph_row *
13833 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13834 struct window *w;
13835 int *delta, *delta_bytes;
13837 struct glyph_row *row;
13838 struct glyph_row *row_found = NULL;
13840 *delta = *delta_bytes = 0;
13842 /* Display must not have been paused, otherwise the current matrix
13843 is not up to date. */
13844 if (NILP (w->window_end_valid))
13845 abort ();
13847 /* A value of window_end_pos >= END_UNCHANGED means that the window
13848 end is in the range of changed text. If so, there is no
13849 unchanged row at the end of W's current matrix. */
13850 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13851 return NULL;
13853 /* Set row to the last row in W's current matrix displaying text. */
13854 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13856 /* If matrix is entirely empty, no unchanged row exists. */
13857 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13859 /* The value of row is the last glyph row in the matrix having a
13860 meaningful buffer position in it. The end position of row
13861 corresponds to window_end_pos. This allows us to translate
13862 buffer positions in the current matrix to current buffer
13863 positions for characters not in changed text. */
13864 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13865 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13866 int last_unchanged_pos, last_unchanged_pos_old;
13867 struct glyph_row *first_text_row
13868 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13870 *delta = Z - Z_old;
13871 *delta_bytes = Z_BYTE - Z_BYTE_old;
13873 /* Set last_unchanged_pos to the buffer position of the last
13874 character in the buffer that has not been changed. Z is the
13875 index + 1 of the last character in current_buffer, i.e. by
13876 subtracting END_UNCHANGED we get the index of the last
13877 unchanged character, and we have to add BEG to get its buffer
13878 position. */
13879 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13880 last_unchanged_pos_old = last_unchanged_pos - *delta;
13882 /* Search backward from ROW for a row displaying a line that
13883 starts at a minimum position >= last_unchanged_pos_old. */
13884 for (; row > first_text_row; --row)
13886 /* This used to abort, but it can happen.
13887 It is ok to just stop the search instead here. KFS. */
13888 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13889 break;
13891 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13892 row_found = row;
13896 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13897 abort ();
13899 return row_found;
13903 /* Make sure that glyph rows in the current matrix of window W
13904 reference the same glyph memory as corresponding rows in the
13905 frame's frame matrix. This function is called after scrolling W's
13906 current matrix on a terminal frame in try_window_id and
13907 try_window_reusing_current_matrix. */
13909 static void
13910 sync_frame_with_window_matrix_rows (w)
13911 struct window *w;
13913 struct frame *f = XFRAME (w->frame);
13914 struct glyph_row *window_row, *window_row_end, *frame_row;
13916 /* Preconditions: W must be a leaf window and full-width. Its frame
13917 must have a frame matrix. */
13918 xassert (NILP (w->hchild) && NILP (w->vchild));
13919 xassert (WINDOW_FULL_WIDTH_P (w));
13920 xassert (!FRAME_WINDOW_P (f));
13922 /* If W is a full-width window, glyph pointers in W's current matrix
13923 have, by definition, to be the same as glyph pointers in the
13924 corresponding frame matrix. Note that frame matrices have no
13925 marginal areas (see build_frame_matrix). */
13926 window_row = w->current_matrix->rows;
13927 window_row_end = window_row + w->current_matrix->nrows;
13928 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13929 while (window_row < window_row_end)
13931 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13932 struct glyph *end = window_row->glyphs[LAST_AREA];
13934 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13935 frame_row->glyphs[TEXT_AREA] = start;
13936 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13937 frame_row->glyphs[LAST_AREA] = end;
13939 /* Disable frame rows whose corresponding window rows have
13940 been disabled in try_window_id. */
13941 if (!window_row->enabled_p)
13942 frame_row->enabled_p = 0;
13944 ++window_row, ++frame_row;
13949 /* Find the glyph row in window W containing CHARPOS. Consider all
13950 rows between START and END (not inclusive). END null means search
13951 all rows to the end of the display area of W. Value is the row
13952 containing CHARPOS or null. */
13954 struct glyph_row *
13955 row_containing_pos (w, charpos, start, end, dy)
13956 struct window *w;
13957 int charpos;
13958 struct glyph_row *start, *end;
13959 int dy;
13961 struct glyph_row *row = start;
13962 int last_y;
13964 /* If we happen to start on a header-line, skip that. */
13965 if (row->mode_line_p)
13966 ++row;
13968 if ((end && row >= end) || !row->enabled_p)
13969 return NULL;
13971 last_y = window_text_bottom_y (w) - dy;
13973 while (1)
13975 /* Give up if we have gone too far. */
13976 if (end && row >= end)
13977 return NULL;
13978 /* This formerly returned if they were equal.
13979 I think that both quantities are of a "last plus one" type;
13980 if so, when they are equal, the row is within the screen. -- rms. */
13981 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13982 return NULL;
13984 /* If it is in this row, return this row. */
13985 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13986 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13987 /* The end position of a row equals the start
13988 position of the next row. If CHARPOS is there, we
13989 would rather display it in the next line, except
13990 when this line ends in ZV. */
13991 && !row->ends_at_zv_p
13992 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13993 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13994 return row;
13995 ++row;
14000 /* Try to redisplay window W by reusing its existing display. W's
14001 current matrix must be up to date when this function is called,
14002 i.e. window_end_valid must not be nil.
14004 Value is
14006 1 if display has been updated
14007 0 if otherwise unsuccessful
14008 -1 if redisplay with same window start is known not to succeed
14010 The following steps are performed:
14012 1. Find the last row in the current matrix of W that is not
14013 affected by changes at the start of current_buffer. If no such row
14014 is found, give up.
14016 2. Find the first row in W's current matrix that is not affected by
14017 changes at the end of current_buffer. Maybe there is no such row.
14019 3. Display lines beginning with the row + 1 found in step 1 to the
14020 row found in step 2 or, if step 2 didn't find a row, to the end of
14021 the window.
14023 4. If cursor is not known to appear on the window, give up.
14025 5. If display stopped at the row found in step 2, scroll the
14026 display and current matrix as needed.
14028 6. Maybe display some lines at the end of W, if we must. This can
14029 happen under various circumstances, like a partially visible line
14030 becoming fully visible, or because newly displayed lines are displayed
14031 in smaller font sizes.
14033 7. Update W's window end information. */
14035 static int
14036 try_window_id (w)
14037 struct window *w;
14039 struct frame *f = XFRAME (w->frame);
14040 struct glyph_matrix *current_matrix = w->current_matrix;
14041 struct glyph_matrix *desired_matrix = w->desired_matrix;
14042 struct glyph_row *last_unchanged_at_beg_row;
14043 struct glyph_row *first_unchanged_at_end_row;
14044 struct glyph_row *row;
14045 struct glyph_row *bottom_row;
14046 int bottom_vpos;
14047 struct it it;
14048 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14049 struct text_pos start_pos;
14050 struct run run;
14051 int first_unchanged_at_end_vpos = 0;
14052 struct glyph_row *last_text_row, *last_text_row_at_end;
14053 struct text_pos start;
14054 int first_changed_charpos, last_changed_charpos;
14056 #if GLYPH_DEBUG
14057 if (inhibit_try_window_id)
14058 return 0;
14059 #endif
14061 /* This is handy for debugging. */
14062 #if 0
14063 #define GIVE_UP(X) \
14064 do { \
14065 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14066 return 0; \
14067 } while (0)
14068 #else
14069 #define GIVE_UP(X) return 0
14070 #endif
14072 SET_TEXT_POS_FROM_MARKER (start, w->start);
14074 /* Don't use this for mini-windows because these can show
14075 messages and mini-buffers, and we don't handle that here. */
14076 if (MINI_WINDOW_P (w))
14077 GIVE_UP (1);
14079 /* This flag is used to prevent redisplay optimizations. */
14080 if (windows_or_buffers_changed || cursor_type_changed)
14081 GIVE_UP (2);
14083 /* Verify that narrowing has not changed.
14084 Also verify that we were not told to prevent redisplay optimizations.
14085 It would be nice to further
14086 reduce the number of cases where this prevents try_window_id. */
14087 if (current_buffer->clip_changed
14088 || current_buffer->prevent_redisplay_optimizations_p)
14089 GIVE_UP (3);
14091 /* Window must either use window-based redisplay or be full width. */
14092 if (!FRAME_WINDOW_P (f)
14093 && (!line_ins_del_ok
14094 || !WINDOW_FULL_WIDTH_P (w)))
14095 GIVE_UP (4);
14097 /* Give up if point is not known NOT to appear in W. */
14098 if (PT < CHARPOS (start))
14099 GIVE_UP (5);
14101 /* Another way to prevent redisplay optimizations. */
14102 if (XFASTINT (w->last_modified) == 0)
14103 GIVE_UP (6);
14105 /* Verify that window is not hscrolled. */
14106 if (XFASTINT (w->hscroll) != 0)
14107 GIVE_UP (7);
14109 /* Verify that display wasn't paused. */
14110 if (NILP (w->window_end_valid))
14111 GIVE_UP (8);
14113 /* Can't use this if highlighting a region because a cursor movement
14114 will do more than just set the cursor. */
14115 if (!NILP (Vtransient_mark_mode)
14116 && !NILP (current_buffer->mark_active))
14117 GIVE_UP (9);
14119 /* Likewise if highlighting trailing whitespace. */
14120 if (!NILP (Vshow_trailing_whitespace))
14121 GIVE_UP (11);
14123 /* Likewise if showing a region. */
14124 if (!NILP (w->region_showing))
14125 GIVE_UP (10);
14127 /* Can use this if overlay arrow position and or string have changed. */
14128 if (overlay_arrows_changed_p ())
14129 GIVE_UP (12);
14132 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14133 only if buffer has really changed. The reason is that the gap is
14134 initially at Z for freshly visited files. The code below would
14135 set end_unchanged to 0 in that case. */
14136 if (MODIFF > SAVE_MODIFF
14137 /* This seems to happen sometimes after saving a buffer. */
14138 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14140 if (GPT - BEG < BEG_UNCHANGED)
14141 BEG_UNCHANGED = GPT - BEG;
14142 if (Z - GPT < END_UNCHANGED)
14143 END_UNCHANGED = Z - GPT;
14146 /* The position of the first and last character that has been changed. */
14147 first_changed_charpos = BEG + BEG_UNCHANGED;
14148 last_changed_charpos = Z - END_UNCHANGED;
14150 /* If window starts after a line end, and the last change is in
14151 front of that newline, then changes don't affect the display.
14152 This case happens with stealth-fontification. Note that although
14153 the display is unchanged, glyph positions in the matrix have to
14154 be adjusted, of course. */
14155 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14156 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14157 && ((last_changed_charpos < CHARPOS (start)
14158 && CHARPOS (start) == BEGV)
14159 || (last_changed_charpos < CHARPOS (start) - 1
14160 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14162 int Z_old, delta, Z_BYTE_old, delta_bytes;
14163 struct glyph_row *r0;
14165 /* Compute how many chars/bytes have been added to or removed
14166 from the buffer. */
14167 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14168 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14169 delta = Z - Z_old;
14170 delta_bytes = Z_BYTE - Z_BYTE_old;
14172 /* Give up if PT is not in the window. Note that it already has
14173 been checked at the start of try_window_id that PT is not in
14174 front of the window start. */
14175 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14176 GIVE_UP (13);
14178 /* If window start is unchanged, we can reuse the whole matrix
14179 as is, after adjusting glyph positions. No need to compute
14180 the window end again, since its offset from Z hasn't changed. */
14181 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14182 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14183 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14184 /* PT must not be in a partially visible line. */
14185 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14186 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14188 /* Adjust positions in the glyph matrix. */
14189 if (delta || delta_bytes)
14191 struct glyph_row *r1
14192 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14193 increment_matrix_positions (w->current_matrix,
14194 MATRIX_ROW_VPOS (r0, current_matrix),
14195 MATRIX_ROW_VPOS (r1, current_matrix),
14196 delta, delta_bytes);
14199 /* Set the cursor. */
14200 row = row_containing_pos (w, PT, r0, NULL, 0);
14201 if (row)
14202 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14203 else
14204 abort ();
14205 return 1;
14209 /* Handle the case that changes are all below what is displayed in
14210 the window, and that PT is in the window. This shortcut cannot
14211 be taken if ZV is visible in the window, and text has been added
14212 there that is visible in the window. */
14213 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14214 /* ZV is not visible in the window, or there are no
14215 changes at ZV, actually. */
14216 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14217 || first_changed_charpos == last_changed_charpos))
14219 struct glyph_row *r0;
14221 /* Give up if PT is not in the window. Note that it already has
14222 been checked at the start of try_window_id that PT is not in
14223 front of the window start. */
14224 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14225 GIVE_UP (14);
14227 /* If window start is unchanged, we can reuse the whole matrix
14228 as is, without changing glyph positions since no text has
14229 been added/removed in front of the window end. */
14230 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14231 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14232 /* PT must not be in a partially visible line. */
14233 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14234 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14236 /* We have to compute the window end anew since text
14237 can have been added/removed after it. */
14238 w->window_end_pos
14239 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14240 w->window_end_bytepos
14241 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14243 /* Set the cursor. */
14244 row = row_containing_pos (w, PT, r0, NULL, 0);
14245 if (row)
14246 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14247 else
14248 abort ();
14249 return 2;
14253 /* Give up if window start is in the changed area.
14255 The condition used to read
14257 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14259 but why that was tested escapes me at the moment. */
14260 if (CHARPOS (start) >= first_changed_charpos
14261 && CHARPOS (start) <= last_changed_charpos)
14262 GIVE_UP (15);
14264 /* Check that window start agrees with the start of the first glyph
14265 row in its current matrix. Check this after we know the window
14266 start is not in changed text, otherwise positions would not be
14267 comparable. */
14268 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14269 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14270 GIVE_UP (16);
14272 /* Give up if the window ends in strings. Overlay strings
14273 at the end are difficult to handle, so don't try. */
14274 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14275 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14276 GIVE_UP (20);
14278 /* Compute the position at which we have to start displaying new
14279 lines. Some of the lines at the top of the window might be
14280 reusable because they are not displaying changed text. Find the
14281 last row in W's current matrix not affected by changes at the
14282 start of current_buffer. Value is null if changes start in the
14283 first line of window. */
14284 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14285 if (last_unchanged_at_beg_row)
14287 /* Avoid starting to display in the moddle of a character, a TAB
14288 for instance. This is easier than to set up the iterator
14289 exactly, and it's not a frequent case, so the additional
14290 effort wouldn't really pay off. */
14291 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14292 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14293 && last_unchanged_at_beg_row > w->current_matrix->rows)
14294 --last_unchanged_at_beg_row;
14296 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14297 GIVE_UP (17);
14299 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14300 GIVE_UP (18);
14301 start_pos = it.current.pos;
14303 /* Start displaying new lines in the desired matrix at the same
14304 vpos we would use in the current matrix, i.e. below
14305 last_unchanged_at_beg_row. */
14306 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14307 current_matrix);
14308 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14309 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14311 xassert (it.hpos == 0 && it.current_x == 0);
14313 else
14315 /* There are no reusable lines at the start of the window.
14316 Start displaying in the first text line. */
14317 start_display (&it, w, start);
14318 it.vpos = it.first_vpos;
14319 start_pos = it.current.pos;
14322 /* Find the first row that is not affected by changes at the end of
14323 the buffer. Value will be null if there is no unchanged row, in
14324 which case we must redisplay to the end of the window. delta
14325 will be set to the value by which buffer positions beginning with
14326 first_unchanged_at_end_row have to be adjusted due to text
14327 changes. */
14328 first_unchanged_at_end_row
14329 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14330 IF_DEBUG (debug_delta = delta);
14331 IF_DEBUG (debug_delta_bytes = delta_bytes);
14333 /* Set stop_pos to the buffer position up to which we will have to
14334 display new lines. If first_unchanged_at_end_row != NULL, this
14335 is the buffer position of the start of the line displayed in that
14336 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14337 that we don't stop at a buffer position. */
14338 stop_pos = 0;
14339 if (first_unchanged_at_end_row)
14341 xassert (last_unchanged_at_beg_row == NULL
14342 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14344 /* If this is a continuation line, move forward to the next one
14345 that isn't. Changes in lines above affect this line.
14346 Caution: this may move first_unchanged_at_end_row to a row
14347 not displaying text. */
14348 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14349 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14350 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14351 < it.last_visible_y))
14352 ++first_unchanged_at_end_row;
14354 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14355 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14356 >= it.last_visible_y))
14357 first_unchanged_at_end_row = NULL;
14358 else
14360 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14361 + delta);
14362 first_unchanged_at_end_vpos
14363 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14364 xassert (stop_pos >= Z - END_UNCHANGED);
14367 else if (last_unchanged_at_beg_row == NULL)
14368 GIVE_UP (19);
14371 #if GLYPH_DEBUG
14373 /* Either there is no unchanged row at the end, or the one we have
14374 now displays text. This is a necessary condition for the window
14375 end pos calculation at the end of this function. */
14376 xassert (first_unchanged_at_end_row == NULL
14377 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14379 debug_last_unchanged_at_beg_vpos
14380 = (last_unchanged_at_beg_row
14381 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14382 : -1);
14383 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14385 #endif /* GLYPH_DEBUG != 0 */
14388 /* Display new lines. Set last_text_row to the last new line
14389 displayed which has text on it, i.e. might end up as being the
14390 line where the window_end_vpos is. */
14391 w->cursor.vpos = -1;
14392 last_text_row = NULL;
14393 overlay_arrow_seen = 0;
14394 while (it.current_y < it.last_visible_y
14395 && !fonts_changed_p
14396 && (first_unchanged_at_end_row == NULL
14397 || IT_CHARPOS (it) < stop_pos))
14399 if (display_line (&it))
14400 last_text_row = it.glyph_row - 1;
14403 if (fonts_changed_p)
14404 return -1;
14407 /* Compute differences in buffer positions, y-positions etc. for
14408 lines reused at the bottom of the window. Compute what we can
14409 scroll. */
14410 if (first_unchanged_at_end_row
14411 /* No lines reused because we displayed everything up to the
14412 bottom of the window. */
14413 && it.current_y < it.last_visible_y)
14415 dvpos = (it.vpos
14416 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14417 current_matrix));
14418 dy = it.current_y - first_unchanged_at_end_row->y;
14419 run.current_y = first_unchanged_at_end_row->y;
14420 run.desired_y = run.current_y + dy;
14421 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14423 else
14425 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14426 first_unchanged_at_end_row = NULL;
14428 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14431 /* Find the cursor if not already found. We have to decide whether
14432 PT will appear on this window (it sometimes doesn't, but this is
14433 not a very frequent case.) This decision has to be made before
14434 the current matrix is altered. A value of cursor.vpos < 0 means
14435 that PT is either in one of the lines beginning at
14436 first_unchanged_at_end_row or below the window. Don't care for
14437 lines that might be displayed later at the window end; as
14438 mentioned, this is not a frequent case. */
14439 if (w->cursor.vpos < 0)
14441 /* Cursor in unchanged rows at the top? */
14442 if (PT < CHARPOS (start_pos)
14443 && last_unchanged_at_beg_row)
14445 row = row_containing_pos (w, PT,
14446 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14447 last_unchanged_at_beg_row + 1, 0);
14448 if (row)
14449 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14452 /* Start from first_unchanged_at_end_row looking for PT. */
14453 else if (first_unchanged_at_end_row)
14455 row = row_containing_pos (w, PT - delta,
14456 first_unchanged_at_end_row, NULL, 0);
14457 if (row)
14458 set_cursor_from_row (w, row, w->current_matrix, delta,
14459 delta_bytes, dy, dvpos);
14462 /* Give up if cursor was not found. */
14463 if (w->cursor.vpos < 0)
14465 clear_glyph_matrix (w->desired_matrix);
14466 return -1;
14470 /* Don't let the cursor end in the scroll margins. */
14472 int this_scroll_margin, cursor_height;
14474 this_scroll_margin = max (0, scroll_margin);
14475 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14476 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14477 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14479 if ((w->cursor.y < this_scroll_margin
14480 && CHARPOS (start) > BEGV)
14481 /* Old redisplay didn't take scroll margin into account at the bottom,
14482 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14483 || (w->cursor.y + (make_cursor_line_fully_visible_p
14484 ? cursor_height + this_scroll_margin
14485 : 1)) > it.last_visible_y)
14487 w->cursor.vpos = -1;
14488 clear_glyph_matrix (w->desired_matrix);
14489 return -1;
14493 /* Scroll the display. Do it before changing the current matrix so
14494 that xterm.c doesn't get confused about where the cursor glyph is
14495 found. */
14496 if (dy && run.height)
14498 update_begin (f);
14500 if (FRAME_WINDOW_P (f))
14502 rif->update_window_begin_hook (w);
14503 rif->clear_window_mouse_face (w);
14504 rif->scroll_run_hook (w, &run);
14505 rif->update_window_end_hook (w, 0, 0);
14507 else
14509 /* Terminal frame. In this case, dvpos gives the number of
14510 lines to scroll by; dvpos < 0 means scroll up. */
14511 int first_unchanged_at_end_vpos
14512 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14513 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14514 int end = (WINDOW_TOP_EDGE_LINE (w)
14515 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14516 + window_internal_height (w));
14518 /* Perform the operation on the screen. */
14519 if (dvpos > 0)
14521 /* Scroll last_unchanged_at_beg_row to the end of the
14522 window down dvpos lines. */
14523 set_terminal_window (end);
14525 /* On dumb terminals delete dvpos lines at the end
14526 before inserting dvpos empty lines. */
14527 if (!scroll_region_ok)
14528 ins_del_lines (end - dvpos, -dvpos);
14530 /* Insert dvpos empty lines in front of
14531 last_unchanged_at_beg_row. */
14532 ins_del_lines (from, dvpos);
14534 else if (dvpos < 0)
14536 /* Scroll up last_unchanged_at_beg_vpos to the end of
14537 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14538 set_terminal_window (end);
14540 /* Delete dvpos lines in front of
14541 last_unchanged_at_beg_vpos. ins_del_lines will set
14542 the cursor to the given vpos and emit |dvpos| delete
14543 line sequences. */
14544 ins_del_lines (from + dvpos, dvpos);
14546 /* On a dumb terminal insert dvpos empty lines at the
14547 end. */
14548 if (!scroll_region_ok)
14549 ins_del_lines (end + dvpos, -dvpos);
14552 set_terminal_window (0);
14555 update_end (f);
14558 /* Shift reused rows of the current matrix to the right position.
14559 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14560 text. */
14561 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14562 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14563 if (dvpos < 0)
14565 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14566 bottom_vpos, dvpos);
14567 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14568 bottom_vpos, 0);
14570 else if (dvpos > 0)
14572 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14573 bottom_vpos, dvpos);
14574 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14575 first_unchanged_at_end_vpos + dvpos, 0);
14578 /* For frame-based redisplay, make sure that current frame and window
14579 matrix are in sync with respect to glyph memory. */
14580 if (!FRAME_WINDOW_P (f))
14581 sync_frame_with_window_matrix_rows (w);
14583 /* Adjust buffer positions in reused rows. */
14584 if (delta)
14585 increment_matrix_positions (current_matrix,
14586 first_unchanged_at_end_vpos + dvpos,
14587 bottom_vpos, delta, delta_bytes);
14589 /* Adjust Y positions. */
14590 if (dy)
14591 shift_glyph_matrix (w, current_matrix,
14592 first_unchanged_at_end_vpos + dvpos,
14593 bottom_vpos, dy);
14595 if (first_unchanged_at_end_row)
14597 first_unchanged_at_end_row += dvpos;
14598 if (first_unchanged_at_end_row->y >= it.last_visible_y
14599 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14600 first_unchanged_at_end_row = NULL;
14603 /* If scrolling up, there may be some lines to display at the end of
14604 the window. */
14605 last_text_row_at_end = NULL;
14606 if (dy < 0)
14608 /* Scrolling up can leave for example a partially visible line
14609 at the end of the window to be redisplayed. */
14610 /* Set last_row to the glyph row in the current matrix where the
14611 window end line is found. It has been moved up or down in
14612 the matrix by dvpos. */
14613 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14614 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14616 /* If last_row is the window end line, it should display text. */
14617 xassert (last_row->displays_text_p);
14619 /* If window end line was partially visible before, begin
14620 displaying at that line. Otherwise begin displaying with the
14621 line following it. */
14622 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14624 init_to_row_start (&it, w, last_row);
14625 it.vpos = last_vpos;
14626 it.current_y = last_row->y;
14628 else
14630 init_to_row_end (&it, w, last_row);
14631 it.vpos = 1 + last_vpos;
14632 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14633 ++last_row;
14636 /* We may start in a continuation line. If so, we have to
14637 get the right continuation_lines_width and current_x. */
14638 it.continuation_lines_width = last_row->continuation_lines_width;
14639 it.hpos = it.current_x = 0;
14641 /* Display the rest of the lines at the window end. */
14642 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14643 while (it.current_y < it.last_visible_y
14644 && !fonts_changed_p)
14646 /* Is it always sure that the display agrees with lines in
14647 the current matrix? I don't think so, so we mark rows
14648 displayed invalid in the current matrix by setting their
14649 enabled_p flag to zero. */
14650 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14651 if (display_line (&it))
14652 last_text_row_at_end = it.glyph_row - 1;
14656 /* Update window_end_pos and window_end_vpos. */
14657 if (first_unchanged_at_end_row
14658 && !last_text_row_at_end)
14660 /* Window end line if one of the preserved rows from the current
14661 matrix. Set row to the last row displaying text in current
14662 matrix starting at first_unchanged_at_end_row, after
14663 scrolling. */
14664 xassert (first_unchanged_at_end_row->displays_text_p);
14665 row = find_last_row_displaying_text (w->current_matrix, &it,
14666 first_unchanged_at_end_row);
14667 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14669 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14670 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14671 w->window_end_vpos
14672 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14673 xassert (w->window_end_bytepos >= 0);
14674 IF_DEBUG (debug_method_add (w, "A"));
14676 else if (last_text_row_at_end)
14678 w->window_end_pos
14679 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14680 w->window_end_bytepos
14681 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14682 w->window_end_vpos
14683 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14684 xassert (w->window_end_bytepos >= 0);
14685 IF_DEBUG (debug_method_add (w, "B"));
14687 else if (last_text_row)
14689 /* We have displayed either to the end of the window or at the
14690 end of the window, i.e. the last row with text is to be found
14691 in the desired matrix. */
14692 w->window_end_pos
14693 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14694 w->window_end_bytepos
14695 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14696 w->window_end_vpos
14697 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14698 xassert (w->window_end_bytepos >= 0);
14700 else if (first_unchanged_at_end_row == NULL
14701 && last_text_row == NULL
14702 && last_text_row_at_end == NULL)
14704 /* Displayed to end of window, but no line containing text was
14705 displayed. Lines were deleted at the end of the window. */
14706 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14707 int vpos = XFASTINT (w->window_end_vpos);
14708 struct glyph_row *current_row = current_matrix->rows + vpos;
14709 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14711 for (row = NULL;
14712 row == NULL && vpos >= first_vpos;
14713 --vpos, --current_row, --desired_row)
14715 if (desired_row->enabled_p)
14717 if (desired_row->displays_text_p)
14718 row = desired_row;
14720 else if (current_row->displays_text_p)
14721 row = current_row;
14724 xassert (row != NULL);
14725 w->window_end_vpos = make_number (vpos + 1);
14726 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14727 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14728 xassert (w->window_end_bytepos >= 0);
14729 IF_DEBUG (debug_method_add (w, "C"));
14731 else
14732 abort ();
14734 #if 0 /* This leads to problems, for instance when the cursor is
14735 at ZV, and the cursor line displays no text. */
14736 /* Disable rows below what's displayed in the window. This makes
14737 debugging easier. */
14738 enable_glyph_matrix_rows (current_matrix,
14739 XFASTINT (w->window_end_vpos) + 1,
14740 bottom_vpos, 0);
14741 #endif
14743 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14744 debug_end_vpos = XFASTINT (w->window_end_vpos));
14746 /* Record that display has not been completed. */
14747 w->window_end_valid = Qnil;
14748 w->desired_matrix->no_scrolling_p = 1;
14749 return 3;
14751 #undef GIVE_UP
14756 /***********************************************************************
14757 More debugging support
14758 ***********************************************************************/
14760 #if GLYPH_DEBUG
14762 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14763 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14764 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14767 /* Dump the contents of glyph matrix MATRIX on stderr.
14769 GLYPHS 0 means don't show glyph contents.
14770 GLYPHS 1 means show glyphs in short form
14771 GLYPHS > 1 means show glyphs in long form. */
14773 void
14774 dump_glyph_matrix (matrix, glyphs)
14775 struct glyph_matrix *matrix;
14776 int glyphs;
14778 int i;
14779 for (i = 0; i < matrix->nrows; ++i)
14780 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14784 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14785 the glyph row and area where the glyph comes from. */
14787 void
14788 dump_glyph (row, glyph, area)
14789 struct glyph_row *row;
14790 struct glyph *glyph;
14791 int area;
14793 if (glyph->type == CHAR_GLYPH)
14795 fprintf (stderr,
14796 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14797 glyph - row->glyphs[TEXT_AREA],
14798 'C',
14799 glyph->charpos,
14800 (BUFFERP (glyph->object)
14801 ? 'B'
14802 : (STRINGP (glyph->object)
14803 ? 'S'
14804 : '-')),
14805 glyph->pixel_width,
14806 glyph->u.ch,
14807 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14808 ? glyph->u.ch
14809 : '.'),
14810 glyph->face_id,
14811 glyph->left_box_line_p,
14812 glyph->right_box_line_p);
14814 else if (glyph->type == STRETCH_GLYPH)
14816 fprintf (stderr,
14817 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14818 glyph - row->glyphs[TEXT_AREA],
14819 'S',
14820 glyph->charpos,
14821 (BUFFERP (glyph->object)
14822 ? 'B'
14823 : (STRINGP (glyph->object)
14824 ? 'S'
14825 : '-')),
14826 glyph->pixel_width,
14828 '.',
14829 glyph->face_id,
14830 glyph->left_box_line_p,
14831 glyph->right_box_line_p);
14833 else if (glyph->type == IMAGE_GLYPH)
14835 fprintf (stderr,
14836 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14837 glyph - row->glyphs[TEXT_AREA],
14838 'I',
14839 glyph->charpos,
14840 (BUFFERP (glyph->object)
14841 ? 'B'
14842 : (STRINGP (glyph->object)
14843 ? 'S'
14844 : '-')),
14845 glyph->pixel_width,
14846 glyph->u.img_id,
14847 '.',
14848 glyph->face_id,
14849 glyph->left_box_line_p,
14850 glyph->right_box_line_p);
14855 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14856 GLYPHS 0 means don't show glyph contents.
14857 GLYPHS 1 means show glyphs in short form
14858 GLYPHS > 1 means show glyphs in long form. */
14860 void
14861 dump_glyph_row (row, vpos, glyphs)
14862 struct glyph_row *row;
14863 int vpos, glyphs;
14865 if (glyphs != 1)
14867 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14868 fprintf (stderr, "======================================================================\n");
14870 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14871 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14872 vpos,
14873 MATRIX_ROW_START_CHARPOS (row),
14874 MATRIX_ROW_END_CHARPOS (row),
14875 row->used[TEXT_AREA],
14876 row->contains_overlapping_glyphs_p,
14877 row->enabled_p,
14878 row->truncated_on_left_p,
14879 row->truncated_on_right_p,
14880 row->continued_p,
14881 MATRIX_ROW_CONTINUATION_LINE_P (row),
14882 row->displays_text_p,
14883 row->ends_at_zv_p,
14884 row->fill_line_p,
14885 row->ends_in_middle_of_char_p,
14886 row->starts_in_middle_of_char_p,
14887 row->mouse_face_p,
14888 row->x,
14889 row->y,
14890 row->pixel_width,
14891 row->height,
14892 row->visible_height,
14893 row->ascent,
14894 row->phys_ascent);
14895 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14896 row->end.overlay_string_index,
14897 row->continuation_lines_width);
14898 fprintf (stderr, "%9d %5d\n",
14899 CHARPOS (row->start.string_pos),
14900 CHARPOS (row->end.string_pos));
14901 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14902 row->end.dpvec_index);
14905 if (glyphs > 1)
14907 int area;
14909 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14911 struct glyph *glyph = row->glyphs[area];
14912 struct glyph *glyph_end = glyph + row->used[area];
14914 /* Glyph for a line end in text. */
14915 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14916 ++glyph_end;
14918 if (glyph < glyph_end)
14919 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14921 for (; glyph < glyph_end; ++glyph)
14922 dump_glyph (row, glyph, area);
14925 else if (glyphs == 1)
14927 int area;
14929 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14931 char *s = (char *) alloca (row->used[area] + 1);
14932 int i;
14934 for (i = 0; i < row->used[area]; ++i)
14936 struct glyph *glyph = row->glyphs[area] + i;
14937 if (glyph->type == CHAR_GLYPH
14938 && glyph->u.ch < 0x80
14939 && glyph->u.ch >= ' ')
14940 s[i] = glyph->u.ch;
14941 else
14942 s[i] = '.';
14945 s[i] = '\0';
14946 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14952 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14953 Sdump_glyph_matrix, 0, 1, "p",
14954 doc: /* Dump the current matrix of the selected window to stderr.
14955 Shows contents of glyph row structures. With non-nil
14956 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14957 glyphs in short form, otherwise show glyphs in long form. */)
14958 (glyphs)
14959 Lisp_Object glyphs;
14961 struct window *w = XWINDOW (selected_window);
14962 struct buffer *buffer = XBUFFER (w->buffer);
14964 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14965 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14966 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14967 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14968 fprintf (stderr, "=============================================\n");
14969 dump_glyph_matrix (w->current_matrix,
14970 NILP (glyphs) ? 0 : XINT (glyphs));
14971 return Qnil;
14975 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14976 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14979 struct frame *f = XFRAME (selected_frame);
14980 dump_glyph_matrix (f->current_matrix, 1);
14981 return Qnil;
14985 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14986 doc: /* Dump glyph row ROW to stderr.
14987 GLYPH 0 means don't dump glyphs.
14988 GLYPH 1 means dump glyphs in short form.
14989 GLYPH > 1 or omitted means dump glyphs in long form. */)
14990 (row, glyphs)
14991 Lisp_Object row, glyphs;
14993 struct glyph_matrix *matrix;
14994 int vpos;
14996 CHECK_NUMBER (row);
14997 matrix = XWINDOW (selected_window)->current_matrix;
14998 vpos = XINT (row);
14999 if (vpos >= 0 && vpos < matrix->nrows)
15000 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15001 vpos,
15002 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15003 return Qnil;
15007 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15008 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15009 GLYPH 0 means don't dump glyphs.
15010 GLYPH 1 means dump glyphs in short form.
15011 GLYPH > 1 or omitted means dump glyphs in long form. */)
15012 (row, glyphs)
15013 Lisp_Object row, glyphs;
15015 struct frame *sf = SELECTED_FRAME ();
15016 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15017 int vpos;
15019 CHECK_NUMBER (row);
15020 vpos = XINT (row);
15021 if (vpos >= 0 && vpos < m->nrows)
15022 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15023 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15024 return Qnil;
15028 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15029 doc: /* Toggle tracing of redisplay.
15030 With ARG, turn tracing on if and only if ARG is positive. */)
15031 (arg)
15032 Lisp_Object arg;
15034 if (NILP (arg))
15035 trace_redisplay_p = !trace_redisplay_p;
15036 else
15038 arg = Fprefix_numeric_value (arg);
15039 trace_redisplay_p = XINT (arg) > 0;
15042 return Qnil;
15046 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15047 doc: /* Like `format', but print result to stderr.
15048 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15049 (nargs, args)
15050 int nargs;
15051 Lisp_Object *args;
15053 Lisp_Object s = Fformat (nargs, args);
15054 fprintf (stderr, "%s", SDATA (s));
15055 return Qnil;
15058 #endif /* GLYPH_DEBUG */
15062 /***********************************************************************
15063 Building Desired Matrix Rows
15064 ***********************************************************************/
15066 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15067 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15069 static struct glyph_row *
15070 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15071 struct window *w;
15072 Lisp_Object overlay_arrow_string;
15074 struct frame *f = XFRAME (WINDOW_FRAME (w));
15075 struct buffer *buffer = XBUFFER (w->buffer);
15076 struct buffer *old = current_buffer;
15077 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15078 int arrow_len = SCHARS (overlay_arrow_string);
15079 const unsigned char *arrow_end = arrow_string + arrow_len;
15080 const unsigned char *p;
15081 struct it it;
15082 int multibyte_p;
15083 int n_glyphs_before;
15085 set_buffer_temp (buffer);
15086 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15087 it.glyph_row->used[TEXT_AREA] = 0;
15088 SET_TEXT_POS (it.position, 0, 0);
15090 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15091 p = arrow_string;
15092 while (p < arrow_end)
15094 Lisp_Object face, ilisp;
15096 /* Get the next character. */
15097 if (multibyte_p)
15098 it.c = string_char_and_length (p, arrow_len, &it.len);
15099 else
15100 it.c = *p, it.len = 1;
15101 p += it.len;
15103 /* Get its face. */
15104 ilisp = make_number (p - arrow_string);
15105 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15106 it.face_id = compute_char_face (f, it.c, face);
15108 /* Compute its width, get its glyphs. */
15109 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15110 SET_TEXT_POS (it.position, -1, -1);
15111 PRODUCE_GLYPHS (&it);
15113 /* If this character doesn't fit any more in the line, we have
15114 to remove some glyphs. */
15115 if (it.current_x > it.last_visible_x)
15117 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15118 break;
15122 set_buffer_temp (old);
15123 return it.glyph_row;
15127 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15128 glyphs are only inserted for terminal frames since we can't really
15129 win with truncation glyphs when partially visible glyphs are
15130 involved. Which glyphs to insert is determined by
15131 produce_special_glyphs. */
15133 static void
15134 insert_left_trunc_glyphs (it)
15135 struct it *it;
15137 struct it truncate_it;
15138 struct glyph *from, *end, *to, *toend;
15140 xassert (!FRAME_WINDOW_P (it->f));
15142 /* Get the truncation glyphs. */
15143 truncate_it = *it;
15144 truncate_it.current_x = 0;
15145 truncate_it.face_id = DEFAULT_FACE_ID;
15146 truncate_it.glyph_row = &scratch_glyph_row;
15147 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15148 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15149 truncate_it.object = make_number (0);
15150 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15152 /* Overwrite glyphs from IT with truncation glyphs. */
15153 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15154 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15155 to = it->glyph_row->glyphs[TEXT_AREA];
15156 toend = to + it->glyph_row->used[TEXT_AREA];
15158 while (from < end)
15159 *to++ = *from++;
15161 /* There may be padding glyphs left over. Overwrite them too. */
15162 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15164 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15165 while (from < end)
15166 *to++ = *from++;
15169 if (to > toend)
15170 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15174 /* Compute the pixel height and width of IT->glyph_row.
15176 Most of the time, ascent and height of a display line will be equal
15177 to the max_ascent and max_height values of the display iterator
15178 structure. This is not the case if
15180 1. We hit ZV without displaying anything. In this case, max_ascent
15181 and max_height will be zero.
15183 2. We have some glyphs that don't contribute to the line height.
15184 (The glyph row flag contributes_to_line_height_p is for future
15185 pixmap extensions).
15187 The first case is easily covered by using default values because in
15188 these cases, the line height does not really matter, except that it
15189 must not be zero. */
15191 static void
15192 compute_line_metrics (it)
15193 struct it *it;
15195 struct glyph_row *row = it->glyph_row;
15196 int area, i;
15198 if (FRAME_WINDOW_P (it->f))
15200 int i, min_y, max_y;
15202 /* The line may consist of one space only, that was added to
15203 place the cursor on it. If so, the row's height hasn't been
15204 computed yet. */
15205 if (row->height == 0)
15207 if (it->max_ascent + it->max_descent == 0)
15208 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15209 row->ascent = it->max_ascent;
15210 row->height = it->max_ascent + it->max_descent;
15211 row->phys_ascent = it->max_phys_ascent;
15212 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15213 row->extra_line_spacing = it->max_extra_line_spacing;
15216 /* Compute the width of this line. */
15217 row->pixel_width = row->x;
15218 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15219 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15221 xassert (row->pixel_width >= 0);
15222 xassert (row->ascent >= 0 && row->height > 0);
15224 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15225 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15227 /* If first line's physical ascent is larger than its logical
15228 ascent, use the physical ascent, and make the row taller.
15229 This makes accented characters fully visible. */
15230 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15231 && row->phys_ascent > row->ascent)
15233 row->height += row->phys_ascent - row->ascent;
15234 row->ascent = row->phys_ascent;
15237 /* Compute how much of the line is visible. */
15238 row->visible_height = row->height;
15240 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15241 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15243 if (row->y < min_y)
15244 row->visible_height -= min_y - row->y;
15245 if (row->y + row->height > max_y)
15246 row->visible_height -= row->y + row->height - max_y;
15248 else
15250 row->pixel_width = row->used[TEXT_AREA];
15251 if (row->continued_p)
15252 row->pixel_width -= it->continuation_pixel_width;
15253 else if (row->truncated_on_right_p)
15254 row->pixel_width -= it->truncation_pixel_width;
15255 row->ascent = row->phys_ascent = 0;
15256 row->height = row->phys_height = row->visible_height = 1;
15257 row->extra_line_spacing = 0;
15260 /* Compute a hash code for this row. */
15261 row->hash = 0;
15262 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15263 for (i = 0; i < row->used[area]; ++i)
15264 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15265 + row->glyphs[area][i].u.val
15266 + row->glyphs[area][i].face_id
15267 + row->glyphs[area][i].padding_p
15268 + (row->glyphs[area][i].type << 2));
15270 it->max_ascent = it->max_descent = 0;
15271 it->max_phys_ascent = it->max_phys_descent = 0;
15275 /* Append one space to the glyph row of iterator IT if doing a
15276 window-based redisplay. The space has the same face as
15277 IT->face_id. Value is non-zero if a space was added.
15279 This function is called to make sure that there is always one glyph
15280 at the end of a glyph row that the cursor can be set on under
15281 window-systems. (If there weren't such a glyph we would not know
15282 how wide and tall a box cursor should be displayed).
15284 At the same time this space let's a nicely handle clearing to the
15285 end of the line if the row ends in italic text. */
15287 static int
15288 append_space_for_newline (it, default_face_p)
15289 struct it *it;
15290 int default_face_p;
15292 if (FRAME_WINDOW_P (it->f))
15294 int n = it->glyph_row->used[TEXT_AREA];
15296 if (it->glyph_row->glyphs[TEXT_AREA] + n
15297 < it->glyph_row->glyphs[1 + TEXT_AREA])
15299 /* Save some values that must not be changed.
15300 Must save IT->c and IT->len because otherwise
15301 ITERATOR_AT_END_P wouldn't work anymore after
15302 append_space_for_newline has been called. */
15303 enum display_element_type saved_what = it->what;
15304 int saved_c = it->c, saved_len = it->len;
15305 int saved_x = it->current_x;
15306 int saved_face_id = it->face_id;
15307 struct text_pos saved_pos;
15308 Lisp_Object saved_object;
15309 struct face *face;
15311 saved_object = it->object;
15312 saved_pos = it->position;
15314 it->what = IT_CHARACTER;
15315 bzero (&it->position, sizeof it->position);
15316 it->object = make_number (0);
15317 it->c = ' ';
15318 it->len = 1;
15320 if (default_face_p)
15321 it->face_id = DEFAULT_FACE_ID;
15322 else if (it->face_before_selective_p)
15323 it->face_id = it->saved_face_id;
15324 face = FACE_FROM_ID (it->f, it->face_id);
15325 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15327 PRODUCE_GLYPHS (it);
15329 it->override_ascent = -1;
15330 it->constrain_row_ascent_descent_p = 0;
15331 it->current_x = saved_x;
15332 it->object = saved_object;
15333 it->position = saved_pos;
15334 it->what = saved_what;
15335 it->face_id = saved_face_id;
15336 it->len = saved_len;
15337 it->c = saved_c;
15338 return 1;
15342 return 0;
15346 /* Extend the face of the last glyph in the text area of IT->glyph_row
15347 to the end of the display line. Called from display_line.
15348 If the glyph row is empty, add a space glyph to it so that we
15349 know the face to draw. Set the glyph row flag fill_line_p. */
15351 static void
15352 extend_face_to_end_of_line (it)
15353 struct it *it;
15355 struct face *face;
15356 struct frame *f = it->f;
15358 /* If line is already filled, do nothing. */
15359 if (it->current_x >= it->last_visible_x)
15360 return;
15362 /* Face extension extends the background and box of IT->face_id
15363 to the end of the line. If the background equals the background
15364 of the frame, we don't have to do anything. */
15365 if (it->face_before_selective_p)
15366 face = FACE_FROM_ID (it->f, it->saved_face_id);
15367 else
15368 face = FACE_FROM_ID (f, it->face_id);
15370 if (FRAME_WINDOW_P (f)
15371 && it->glyph_row->displays_text_p
15372 && face->box == FACE_NO_BOX
15373 && face->background == FRAME_BACKGROUND_PIXEL (f)
15374 && !face->stipple)
15375 return;
15377 /* Set the glyph row flag indicating that the face of the last glyph
15378 in the text area has to be drawn to the end of the text area. */
15379 it->glyph_row->fill_line_p = 1;
15381 /* If current character of IT is not ASCII, make sure we have the
15382 ASCII face. This will be automatically undone the next time
15383 get_next_display_element returns a multibyte character. Note
15384 that the character will always be single byte in unibyte text. */
15385 if (!SINGLE_BYTE_CHAR_P (it->c))
15387 it->face_id = FACE_FOR_CHAR (f, face, 0);
15390 if (FRAME_WINDOW_P (f))
15392 /* If the row is empty, add a space with the current face of IT,
15393 so that we know which face to draw. */
15394 if (it->glyph_row->used[TEXT_AREA] == 0)
15396 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15397 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15398 it->glyph_row->used[TEXT_AREA] = 1;
15401 else
15403 /* Save some values that must not be changed. */
15404 int saved_x = it->current_x;
15405 struct text_pos saved_pos;
15406 Lisp_Object saved_object;
15407 enum display_element_type saved_what = it->what;
15408 int saved_face_id = it->face_id;
15410 saved_object = it->object;
15411 saved_pos = it->position;
15413 it->what = IT_CHARACTER;
15414 bzero (&it->position, sizeof it->position);
15415 it->object = make_number (0);
15416 it->c = ' ';
15417 it->len = 1;
15418 it->face_id = face->id;
15420 PRODUCE_GLYPHS (it);
15422 while (it->current_x <= it->last_visible_x)
15423 PRODUCE_GLYPHS (it);
15425 /* Don't count these blanks really. It would let us insert a left
15426 truncation glyph below and make us set the cursor on them, maybe. */
15427 it->current_x = saved_x;
15428 it->object = saved_object;
15429 it->position = saved_pos;
15430 it->what = saved_what;
15431 it->face_id = saved_face_id;
15436 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15437 trailing whitespace. */
15439 static int
15440 trailing_whitespace_p (charpos)
15441 int charpos;
15443 int bytepos = CHAR_TO_BYTE (charpos);
15444 int c = 0;
15446 while (bytepos < ZV_BYTE
15447 && (c = FETCH_CHAR (bytepos),
15448 c == ' ' || c == '\t'))
15449 ++bytepos;
15451 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15453 if (bytepos != PT_BYTE)
15454 return 1;
15456 return 0;
15460 /* Highlight trailing whitespace, if any, in ROW. */
15462 void
15463 highlight_trailing_whitespace (f, row)
15464 struct frame *f;
15465 struct glyph_row *row;
15467 int used = row->used[TEXT_AREA];
15469 if (used)
15471 struct glyph *start = row->glyphs[TEXT_AREA];
15472 struct glyph *glyph = start + used - 1;
15474 /* Skip over glyphs inserted to display the cursor at the
15475 end of a line, for extending the face of the last glyph
15476 to the end of the line on terminals, and for truncation
15477 and continuation glyphs. */
15478 while (glyph >= start
15479 && glyph->type == CHAR_GLYPH
15480 && INTEGERP (glyph->object))
15481 --glyph;
15483 /* If last glyph is a space or stretch, and it's trailing
15484 whitespace, set the face of all trailing whitespace glyphs in
15485 IT->glyph_row to `trailing-whitespace'. */
15486 if (glyph >= start
15487 && BUFFERP (glyph->object)
15488 && (glyph->type == STRETCH_GLYPH
15489 || (glyph->type == CHAR_GLYPH
15490 && glyph->u.ch == ' '))
15491 && trailing_whitespace_p (glyph->charpos))
15493 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15494 if (face_id < 0)
15495 return;
15497 while (glyph >= start
15498 && BUFFERP (glyph->object)
15499 && (glyph->type == STRETCH_GLYPH
15500 || (glyph->type == CHAR_GLYPH
15501 && glyph->u.ch == ' ')))
15502 (glyph--)->face_id = face_id;
15508 /* Value is non-zero if glyph row ROW in window W should be
15509 used to hold the cursor. */
15511 static int
15512 cursor_row_p (w, row)
15513 struct window *w;
15514 struct glyph_row *row;
15516 int cursor_row_p = 1;
15518 if (PT == MATRIX_ROW_END_CHARPOS (row))
15520 /* If the row ends with a newline from a string, we don't want
15521 the cursor there, but we still want it at the start of the
15522 string if the string starts in this row.
15523 If the row is continued it doesn't end in a newline. */
15524 if (CHARPOS (row->end.string_pos) >= 0)
15525 cursor_row_p = (row->continued_p
15526 || PT >= MATRIX_ROW_START_CHARPOS (row));
15527 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15529 /* If the row ends in middle of a real character,
15530 and the line is continued, we want the cursor here.
15531 That's because MATRIX_ROW_END_CHARPOS would equal
15532 PT if PT is before the character. */
15533 if (!row->ends_in_ellipsis_p)
15534 cursor_row_p = row->continued_p;
15535 else
15536 /* If the row ends in an ellipsis, then
15537 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15538 We want that position to be displayed after the ellipsis. */
15539 cursor_row_p = 0;
15541 /* If the row ends at ZV, display the cursor at the end of that
15542 row instead of at the start of the row below. */
15543 else if (row->ends_at_zv_p)
15544 cursor_row_p = 1;
15545 else
15546 cursor_row_p = 0;
15549 return cursor_row_p;
15553 /* Construct the glyph row IT->glyph_row in the desired matrix of
15554 IT->w from text at the current position of IT. See dispextern.h
15555 for an overview of struct it. Value is non-zero if
15556 IT->glyph_row displays text, as opposed to a line displaying ZV
15557 only. */
15559 static int
15560 display_line (it)
15561 struct it *it;
15563 struct glyph_row *row = it->glyph_row;
15564 Lisp_Object overlay_arrow_string;
15566 /* We always start displaying at hpos zero even if hscrolled. */
15567 xassert (it->hpos == 0 && it->current_x == 0);
15569 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15570 >= it->w->desired_matrix->nrows)
15572 it->w->nrows_scale_factor++;
15573 fonts_changed_p = 1;
15574 return 0;
15577 /* Is IT->w showing the region? */
15578 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15580 /* Clear the result glyph row and enable it. */
15581 prepare_desired_row (row);
15583 row->y = it->current_y;
15584 row->start = it->start;
15585 row->continuation_lines_width = it->continuation_lines_width;
15586 row->displays_text_p = 1;
15587 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15588 it->starts_in_middle_of_char_p = 0;
15590 /* Arrange the overlays nicely for our purposes. Usually, we call
15591 display_line on only one line at a time, in which case this
15592 can't really hurt too much, or we call it on lines which appear
15593 one after another in the buffer, in which case all calls to
15594 recenter_overlay_lists but the first will be pretty cheap. */
15595 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15597 /* Move over display elements that are not visible because we are
15598 hscrolled. This may stop at an x-position < IT->first_visible_x
15599 if the first glyph is partially visible or if we hit a line end. */
15600 if (it->current_x < it->first_visible_x)
15602 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15603 MOVE_TO_POS | MOVE_TO_X);
15606 /* Get the initial row height. This is either the height of the
15607 text hscrolled, if there is any, or zero. */
15608 row->ascent = it->max_ascent;
15609 row->height = it->max_ascent + it->max_descent;
15610 row->phys_ascent = it->max_phys_ascent;
15611 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15612 row->extra_line_spacing = it->max_extra_line_spacing;
15614 /* Loop generating characters. The loop is left with IT on the next
15615 character to display. */
15616 while (1)
15618 int n_glyphs_before, hpos_before, x_before;
15619 int x, i, nglyphs;
15620 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15622 /* Retrieve the next thing to display. Value is zero if end of
15623 buffer reached. */
15624 if (!get_next_display_element (it))
15626 /* Maybe add a space at the end of this line that is used to
15627 display the cursor there under X. Set the charpos of the
15628 first glyph of blank lines not corresponding to any text
15629 to -1. */
15630 #ifdef HAVE_WINDOW_SYSTEM
15631 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15632 row->exact_window_width_line_p = 1;
15633 else
15634 #endif /* HAVE_WINDOW_SYSTEM */
15635 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15636 || row->used[TEXT_AREA] == 0)
15638 row->glyphs[TEXT_AREA]->charpos = -1;
15639 row->displays_text_p = 0;
15641 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15642 && (!MINI_WINDOW_P (it->w)
15643 || (minibuf_level && EQ (it->window, minibuf_window))))
15644 row->indicate_empty_line_p = 1;
15647 it->continuation_lines_width = 0;
15648 row->ends_at_zv_p = 1;
15649 break;
15652 /* Now, get the metrics of what we want to display. This also
15653 generates glyphs in `row' (which is IT->glyph_row). */
15654 n_glyphs_before = row->used[TEXT_AREA];
15655 x = it->current_x;
15657 /* Remember the line height so far in case the next element doesn't
15658 fit on the line. */
15659 if (!it->truncate_lines_p)
15661 ascent = it->max_ascent;
15662 descent = it->max_descent;
15663 phys_ascent = it->max_phys_ascent;
15664 phys_descent = it->max_phys_descent;
15667 PRODUCE_GLYPHS (it);
15669 /* If this display element was in marginal areas, continue with
15670 the next one. */
15671 if (it->area != TEXT_AREA)
15673 row->ascent = max (row->ascent, it->max_ascent);
15674 row->height = max (row->height, it->max_ascent + it->max_descent);
15675 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15676 row->phys_height = max (row->phys_height,
15677 it->max_phys_ascent + it->max_phys_descent);
15678 row->extra_line_spacing = max (row->extra_line_spacing,
15679 it->max_extra_line_spacing);
15680 set_iterator_to_next (it, 1);
15681 continue;
15684 /* Does the display element fit on the line? If we truncate
15685 lines, we should draw past the right edge of the window. If
15686 we don't truncate, we want to stop so that we can display the
15687 continuation glyph before the right margin. If lines are
15688 continued, there are two possible strategies for characters
15689 resulting in more than 1 glyph (e.g. tabs): Display as many
15690 glyphs as possible in this line and leave the rest for the
15691 continuation line, or display the whole element in the next
15692 line. Original redisplay did the former, so we do it also. */
15693 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15694 hpos_before = it->hpos;
15695 x_before = x;
15697 if (/* Not a newline. */
15698 nglyphs > 0
15699 /* Glyphs produced fit entirely in the line. */
15700 && it->current_x < it->last_visible_x)
15702 it->hpos += nglyphs;
15703 row->ascent = max (row->ascent, it->max_ascent);
15704 row->height = max (row->height, it->max_ascent + it->max_descent);
15705 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15706 row->phys_height = max (row->phys_height,
15707 it->max_phys_ascent + it->max_phys_descent);
15708 row->extra_line_spacing = max (row->extra_line_spacing,
15709 it->max_extra_line_spacing);
15710 if (it->current_x - it->pixel_width < it->first_visible_x)
15711 row->x = x - it->first_visible_x;
15713 else
15715 int new_x;
15716 struct glyph *glyph;
15718 for (i = 0; i < nglyphs; ++i, x = new_x)
15720 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15721 new_x = x + glyph->pixel_width;
15723 if (/* Lines are continued. */
15724 !it->truncate_lines_p
15725 && (/* Glyph doesn't fit on the line. */
15726 new_x > it->last_visible_x
15727 /* Or it fits exactly on a window system frame. */
15728 || (new_x == it->last_visible_x
15729 && FRAME_WINDOW_P (it->f))))
15731 /* End of a continued line. */
15733 if (it->hpos == 0
15734 || (new_x == it->last_visible_x
15735 && FRAME_WINDOW_P (it->f)))
15737 /* Current glyph is the only one on the line or
15738 fits exactly on the line. We must continue
15739 the line because we can't draw the cursor
15740 after the glyph. */
15741 row->continued_p = 1;
15742 it->current_x = new_x;
15743 it->continuation_lines_width += new_x;
15744 ++it->hpos;
15745 if (i == nglyphs - 1)
15747 set_iterator_to_next (it, 1);
15748 #ifdef HAVE_WINDOW_SYSTEM
15749 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15751 if (!get_next_display_element (it))
15753 row->exact_window_width_line_p = 1;
15754 it->continuation_lines_width = 0;
15755 row->continued_p = 0;
15756 row->ends_at_zv_p = 1;
15758 else if (ITERATOR_AT_END_OF_LINE_P (it))
15760 row->continued_p = 0;
15761 row->exact_window_width_line_p = 1;
15764 #endif /* HAVE_WINDOW_SYSTEM */
15767 else if (CHAR_GLYPH_PADDING_P (*glyph)
15768 && !FRAME_WINDOW_P (it->f))
15770 /* A padding glyph that doesn't fit on this line.
15771 This means the whole character doesn't fit
15772 on the line. */
15773 row->used[TEXT_AREA] = n_glyphs_before;
15775 /* Fill the rest of the row with continuation
15776 glyphs like in 20.x. */
15777 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15778 < row->glyphs[1 + TEXT_AREA])
15779 produce_special_glyphs (it, IT_CONTINUATION);
15781 row->continued_p = 1;
15782 it->current_x = x_before;
15783 it->continuation_lines_width += x_before;
15785 /* Restore the height to what it was before the
15786 element not fitting on the line. */
15787 it->max_ascent = ascent;
15788 it->max_descent = descent;
15789 it->max_phys_ascent = phys_ascent;
15790 it->max_phys_descent = phys_descent;
15792 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15794 /* A TAB that extends past the right edge of the
15795 window. This produces a single glyph on
15796 window system frames. We leave the glyph in
15797 this row and let it fill the row, but don't
15798 consume the TAB. */
15799 it->continuation_lines_width += it->last_visible_x;
15800 row->ends_in_middle_of_char_p = 1;
15801 row->continued_p = 1;
15802 glyph->pixel_width = it->last_visible_x - x;
15803 it->starts_in_middle_of_char_p = 1;
15805 else
15807 /* Something other than a TAB that draws past
15808 the right edge of the window. Restore
15809 positions to values before the element. */
15810 row->used[TEXT_AREA] = n_glyphs_before + i;
15812 /* Display continuation glyphs. */
15813 if (!FRAME_WINDOW_P (it->f))
15814 produce_special_glyphs (it, IT_CONTINUATION);
15815 row->continued_p = 1;
15817 it->current_x = x_before;
15818 it->continuation_lines_width += x;
15819 extend_face_to_end_of_line (it);
15821 if (nglyphs > 1 && i > 0)
15823 row->ends_in_middle_of_char_p = 1;
15824 it->starts_in_middle_of_char_p = 1;
15827 /* Restore the height to what it was before the
15828 element not fitting on the line. */
15829 it->max_ascent = ascent;
15830 it->max_descent = descent;
15831 it->max_phys_ascent = phys_ascent;
15832 it->max_phys_descent = phys_descent;
15835 break;
15837 else if (new_x > it->first_visible_x)
15839 /* Increment number of glyphs actually displayed. */
15840 ++it->hpos;
15842 if (x < it->first_visible_x)
15843 /* Glyph is partially visible, i.e. row starts at
15844 negative X position. */
15845 row->x = x - it->first_visible_x;
15847 else
15849 /* Glyph is completely off the left margin of the
15850 window. This should not happen because of the
15851 move_it_in_display_line at the start of this
15852 function, unless the text display area of the
15853 window is empty. */
15854 xassert (it->first_visible_x <= it->last_visible_x);
15858 row->ascent = max (row->ascent, it->max_ascent);
15859 row->height = max (row->height, it->max_ascent + it->max_descent);
15860 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15861 row->phys_height = max (row->phys_height,
15862 it->max_phys_ascent + it->max_phys_descent);
15863 row->extra_line_spacing = max (row->extra_line_spacing,
15864 it->max_extra_line_spacing);
15866 /* End of this display line if row is continued. */
15867 if (row->continued_p || row->ends_at_zv_p)
15868 break;
15871 at_end_of_line:
15872 /* Is this a line end? If yes, we're also done, after making
15873 sure that a non-default face is extended up to the right
15874 margin of the window. */
15875 if (ITERATOR_AT_END_OF_LINE_P (it))
15877 int used_before = row->used[TEXT_AREA];
15879 row->ends_in_newline_from_string_p = STRINGP (it->object);
15881 #ifdef HAVE_WINDOW_SYSTEM
15882 /* Add a space at the end of the line that is used to
15883 display the cursor there. */
15884 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15885 append_space_for_newline (it, 0);
15886 #endif /* HAVE_WINDOW_SYSTEM */
15888 /* Extend the face to the end of the line. */
15889 extend_face_to_end_of_line (it);
15891 /* Make sure we have the position. */
15892 if (used_before == 0)
15893 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15895 /* Consume the line end. This skips over invisible lines. */
15896 set_iterator_to_next (it, 1);
15897 it->continuation_lines_width = 0;
15898 break;
15901 /* Proceed with next display element. Note that this skips
15902 over lines invisible because of selective display. */
15903 set_iterator_to_next (it, 1);
15905 /* If we truncate lines, we are done when the last displayed
15906 glyphs reach past the right margin of the window. */
15907 if (it->truncate_lines_p
15908 && (FRAME_WINDOW_P (it->f)
15909 ? (it->current_x >= it->last_visible_x)
15910 : (it->current_x > it->last_visible_x)))
15912 /* Maybe add truncation glyphs. */
15913 if (!FRAME_WINDOW_P (it->f))
15915 int i, n;
15917 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15918 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15919 break;
15921 for (n = row->used[TEXT_AREA]; i < n; ++i)
15923 row->used[TEXT_AREA] = i;
15924 produce_special_glyphs (it, IT_TRUNCATION);
15927 #ifdef HAVE_WINDOW_SYSTEM
15928 else
15930 /* Don't truncate if we can overflow newline into fringe. */
15931 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15933 if (!get_next_display_element (it))
15935 it->continuation_lines_width = 0;
15936 row->ends_at_zv_p = 1;
15937 row->exact_window_width_line_p = 1;
15938 break;
15940 if (ITERATOR_AT_END_OF_LINE_P (it))
15942 row->exact_window_width_line_p = 1;
15943 goto at_end_of_line;
15947 #endif /* HAVE_WINDOW_SYSTEM */
15949 row->truncated_on_right_p = 1;
15950 it->continuation_lines_width = 0;
15951 reseat_at_next_visible_line_start (it, 0);
15952 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15953 it->hpos = hpos_before;
15954 it->current_x = x_before;
15955 break;
15959 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15960 at the left window margin. */
15961 if (it->first_visible_x
15962 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15964 if (!FRAME_WINDOW_P (it->f))
15965 insert_left_trunc_glyphs (it);
15966 row->truncated_on_left_p = 1;
15969 /* If the start of this line is the overlay arrow-position, then
15970 mark this glyph row as the one containing the overlay arrow.
15971 This is clearly a mess with variable size fonts. It would be
15972 better to let it be displayed like cursors under X. */
15973 if ((row->displays_text_p || !overlay_arrow_seen)
15974 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15975 !NILP (overlay_arrow_string)))
15977 /* Overlay arrow in window redisplay is a fringe bitmap. */
15978 if (STRINGP (overlay_arrow_string))
15980 struct glyph_row *arrow_row
15981 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15982 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15983 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15984 struct glyph *p = row->glyphs[TEXT_AREA];
15985 struct glyph *p2, *end;
15987 /* Copy the arrow glyphs. */
15988 while (glyph < arrow_end)
15989 *p++ = *glyph++;
15991 /* Throw away padding glyphs. */
15992 p2 = p;
15993 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15994 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15995 ++p2;
15996 if (p2 > p)
15998 while (p2 < end)
15999 *p++ = *p2++;
16000 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16003 else
16005 xassert (INTEGERP (overlay_arrow_string));
16006 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16008 overlay_arrow_seen = 1;
16011 /* Compute pixel dimensions of this line. */
16012 compute_line_metrics (it);
16014 /* Remember the position at which this line ends. */
16015 row->end = it->current;
16017 /* Record whether this row ends inside an ellipsis. */
16018 row->ends_in_ellipsis_p
16019 = (it->method == GET_FROM_DISPLAY_VECTOR
16020 && it->ellipsis_p);
16022 /* Save fringe bitmaps in this row. */
16023 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16024 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16025 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16026 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16028 it->left_user_fringe_bitmap = 0;
16029 it->left_user_fringe_face_id = 0;
16030 it->right_user_fringe_bitmap = 0;
16031 it->right_user_fringe_face_id = 0;
16033 /* Maybe set the cursor. */
16034 if (it->w->cursor.vpos < 0
16035 && PT >= MATRIX_ROW_START_CHARPOS (row)
16036 && PT <= MATRIX_ROW_END_CHARPOS (row)
16037 && cursor_row_p (it->w, row))
16038 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16040 /* Highlight trailing whitespace. */
16041 if (!NILP (Vshow_trailing_whitespace))
16042 highlight_trailing_whitespace (it->f, it->glyph_row);
16044 /* Prepare for the next line. This line starts horizontally at (X
16045 HPOS) = (0 0). Vertical positions are incremented. As a
16046 convenience for the caller, IT->glyph_row is set to the next
16047 row to be used. */
16048 it->current_x = it->hpos = 0;
16049 it->current_y += row->height;
16050 ++it->vpos;
16051 ++it->glyph_row;
16052 it->start = it->current;
16053 return row->displays_text_p;
16058 /***********************************************************************
16059 Menu Bar
16060 ***********************************************************************/
16062 /* Redisplay the menu bar in the frame for window W.
16064 The menu bar of X frames that don't have X toolkit support is
16065 displayed in a special window W->frame->menu_bar_window.
16067 The menu bar of terminal frames is treated specially as far as
16068 glyph matrices are concerned. Menu bar lines are not part of
16069 windows, so the update is done directly on the frame matrix rows
16070 for the menu bar. */
16072 static void
16073 display_menu_bar (w)
16074 struct window *w;
16076 struct frame *f = XFRAME (WINDOW_FRAME (w));
16077 struct it it;
16078 Lisp_Object items;
16079 int i;
16081 /* Don't do all this for graphical frames. */
16082 #ifdef HAVE_NTGUI
16083 if (!NILP (Vwindow_system))
16084 return;
16085 #endif
16086 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16087 if (FRAME_X_P (f))
16088 return;
16089 #endif
16090 #ifdef MAC_OS
16091 if (FRAME_MAC_P (f))
16092 return;
16093 #endif
16095 #ifdef USE_X_TOOLKIT
16096 xassert (!FRAME_WINDOW_P (f));
16097 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16098 it.first_visible_x = 0;
16099 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16100 #else /* not USE_X_TOOLKIT */
16101 if (FRAME_WINDOW_P (f))
16103 /* Menu bar lines are displayed in the desired matrix of the
16104 dummy window menu_bar_window. */
16105 struct window *menu_w;
16106 xassert (WINDOWP (f->menu_bar_window));
16107 menu_w = XWINDOW (f->menu_bar_window);
16108 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16109 MENU_FACE_ID);
16110 it.first_visible_x = 0;
16111 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16113 else
16115 /* This is a TTY frame, i.e. character hpos/vpos are used as
16116 pixel x/y. */
16117 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16118 MENU_FACE_ID);
16119 it.first_visible_x = 0;
16120 it.last_visible_x = FRAME_COLS (f);
16122 #endif /* not USE_X_TOOLKIT */
16124 if (! mode_line_inverse_video)
16125 /* Force the menu-bar to be displayed in the default face. */
16126 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16128 /* Clear all rows of the menu bar. */
16129 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16131 struct glyph_row *row = it.glyph_row + i;
16132 clear_glyph_row (row);
16133 row->enabled_p = 1;
16134 row->full_width_p = 1;
16137 /* Display all items of the menu bar. */
16138 items = FRAME_MENU_BAR_ITEMS (it.f);
16139 for (i = 0; i < XVECTOR (items)->size; i += 4)
16141 Lisp_Object string;
16143 /* Stop at nil string. */
16144 string = AREF (items, i + 1);
16145 if (NILP (string))
16146 break;
16148 /* Remember where item was displayed. */
16149 AREF (items, i + 3) = make_number (it.hpos);
16151 /* Display the item, pad with one space. */
16152 if (it.current_x < it.last_visible_x)
16153 display_string (NULL, string, Qnil, 0, 0, &it,
16154 SCHARS (string) + 1, 0, 0, -1);
16157 /* Fill out the line with spaces. */
16158 if (it.current_x < it.last_visible_x)
16159 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16161 /* Compute the total height of the lines. */
16162 compute_line_metrics (&it);
16167 /***********************************************************************
16168 Mode Line
16169 ***********************************************************************/
16171 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16172 FORCE is non-zero, redisplay mode lines unconditionally.
16173 Otherwise, redisplay only mode lines that are garbaged. Value is
16174 the number of windows whose mode lines were redisplayed. */
16176 static int
16177 redisplay_mode_lines (window, force)
16178 Lisp_Object window;
16179 int force;
16181 int nwindows = 0;
16183 while (!NILP (window))
16185 struct window *w = XWINDOW (window);
16187 if (WINDOWP (w->hchild))
16188 nwindows += redisplay_mode_lines (w->hchild, force);
16189 else if (WINDOWP (w->vchild))
16190 nwindows += redisplay_mode_lines (w->vchild, force);
16191 else if (force
16192 || FRAME_GARBAGED_P (XFRAME (w->frame))
16193 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16195 struct text_pos lpoint;
16196 struct buffer *old = current_buffer;
16198 /* Set the window's buffer for the mode line display. */
16199 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16200 set_buffer_internal_1 (XBUFFER (w->buffer));
16202 /* Point refers normally to the selected window. For any
16203 other window, set up appropriate value. */
16204 if (!EQ (window, selected_window))
16206 struct text_pos pt;
16208 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16209 if (CHARPOS (pt) < BEGV)
16210 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16211 else if (CHARPOS (pt) > (ZV - 1))
16212 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16213 else
16214 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16217 /* Display mode lines. */
16218 clear_glyph_matrix (w->desired_matrix);
16219 if (display_mode_lines (w))
16221 ++nwindows;
16222 w->must_be_updated_p = 1;
16225 /* Restore old settings. */
16226 set_buffer_internal_1 (old);
16227 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16230 window = w->next;
16233 return nwindows;
16237 /* Display the mode and/or top line of window W. Value is the number
16238 of mode lines displayed. */
16240 static int
16241 display_mode_lines (w)
16242 struct window *w;
16244 Lisp_Object old_selected_window, old_selected_frame;
16245 int n = 0;
16247 old_selected_frame = selected_frame;
16248 selected_frame = w->frame;
16249 old_selected_window = selected_window;
16250 XSETWINDOW (selected_window, w);
16252 /* These will be set while the mode line specs are processed. */
16253 line_number_displayed = 0;
16254 w->column_number_displayed = Qnil;
16256 if (WINDOW_WANTS_MODELINE_P (w))
16258 struct window *sel_w = XWINDOW (old_selected_window);
16260 /* Select mode line face based on the real selected window. */
16261 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16262 current_buffer->mode_line_format);
16263 ++n;
16266 if (WINDOW_WANTS_HEADER_LINE_P (w))
16268 display_mode_line (w, HEADER_LINE_FACE_ID,
16269 current_buffer->header_line_format);
16270 ++n;
16273 selected_frame = old_selected_frame;
16274 selected_window = old_selected_window;
16275 return n;
16279 /* Display mode or top line of window W. FACE_ID specifies which line
16280 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16281 FORMAT is the mode line format to display. Value is the pixel
16282 height of the mode line displayed. */
16284 static int
16285 display_mode_line (w, face_id, format)
16286 struct window *w;
16287 enum face_id face_id;
16288 Lisp_Object format;
16290 struct it it;
16291 struct face *face;
16292 int count = SPECPDL_INDEX ();
16294 init_iterator (&it, w, -1, -1, NULL, face_id);
16295 prepare_desired_row (it.glyph_row);
16297 it.glyph_row->mode_line_p = 1;
16299 if (! mode_line_inverse_video)
16300 /* Force the mode-line to be displayed in the default face. */
16301 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16303 record_unwind_protect (unwind_format_mode_line,
16304 format_mode_line_unwind_data (NULL, 0));
16306 mode_line_target = MODE_LINE_DISPLAY;
16308 /* Temporarily make frame's keyboard the current kboard so that
16309 kboard-local variables in the mode_line_format will get the right
16310 values. */
16311 push_frame_kboard (it.f);
16312 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16313 pop_frame_kboard ();
16315 unbind_to (count, Qnil);
16317 /* Fill up with spaces. */
16318 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16320 compute_line_metrics (&it);
16321 it.glyph_row->full_width_p = 1;
16322 it.glyph_row->continued_p = 0;
16323 it.glyph_row->truncated_on_left_p = 0;
16324 it.glyph_row->truncated_on_right_p = 0;
16326 /* Make a 3D mode-line have a shadow at its right end. */
16327 face = FACE_FROM_ID (it.f, face_id);
16328 extend_face_to_end_of_line (&it);
16329 if (face->box != FACE_NO_BOX)
16331 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16332 + it.glyph_row->used[TEXT_AREA] - 1);
16333 last->right_box_line_p = 1;
16336 return it.glyph_row->height;
16339 /* Move element ELT in LIST to the front of LIST.
16340 Return the updated list. */
16342 static Lisp_Object
16343 move_elt_to_front (elt, list)
16344 Lisp_Object elt, list;
16346 register Lisp_Object tail, prev;
16347 register Lisp_Object tem;
16349 tail = list;
16350 prev = Qnil;
16351 while (CONSP (tail))
16353 tem = XCAR (tail);
16355 if (EQ (elt, tem))
16357 /* Splice out the link TAIL. */
16358 if (NILP (prev))
16359 list = XCDR (tail);
16360 else
16361 Fsetcdr (prev, XCDR (tail));
16363 /* Now make it the first. */
16364 Fsetcdr (tail, list);
16365 return tail;
16367 else
16368 prev = tail;
16369 tail = XCDR (tail);
16370 QUIT;
16373 /* Not found--return unchanged LIST. */
16374 return list;
16377 /* Contribute ELT to the mode line for window IT->w. How it
16378 translates into text depends on its data type.
16380 IT describes the display environment in which we display, as usual.
16382 DEPTH is the depth in recursion. It is used to prevent
16383 infinite recursion here.
16385 FIELD_WIDTH is the number of characters the display of ELT should
16386 occupy in the mode line, and PRECISION is the maximum number of
16387 characters to display from ELT's representation. See
16388 display_string for details.
16390 Returns the hpos of the end of the text generated by ELT.
16392 PROPS is a property list to add to any string we encounter.
16394 If RISKY is nonzero, remove (disregard) any properties in any string
16395 we encounter, and ignore :eval and :propertize.
16397 The global variable `mode_line_target' determines whether the
16398 output is passed to `store_mode_line_noprop',
16399 `store_mode_line_string', or `display_string'. */
16401 static int
16402 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16403 struct it *it;
16404 int depth;
16405 int field_width, precision;
16406 Lisp_Object elt, props;
16407 int risky;
16409 int n = 0, field, prec;
16410 int literal = 0;
16412 tail_recurse:
16413 if (depth > 100)
16414 elt = build_string ("*too-deep*");
16416 depth++;
16418 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16420 case Lisp_String:
16422 /* A string: output it and check for %-constructs within it. */
16423 unsigned char c;
16424 int offset = 0;
16426 if (SCHARS (elt) > 0
16427 && (!NILP (props) || risky))
16429 Lisp_Object oprops, aelt;
16430 oprops = Ftext_properties_at (make_number (0), elt);
16432 /* If the starting string's properties are not what
16433 we want, translate the string. Also, if the string
16434 is risky, do that anyway. */
16436 if (NILP (Fequal (props, oprops)) || risky)
16438 /* If the starting string has properties,
16439 merge the specified ones onto the existing ones. */
16440 if (! NILP (oprops) && !risky)
16442 Lisp_Object tem;
16444 oprops = Fcopy_sequence (oprops);
16445 tem = props;
16446 while (CONSP (tem))
16448 oprops = Fplist_put (oprops, XCAR (tem),
16449 XCAR (XCDR (tem)));
16450 tem = XCDR (XCDR (tem));
16452 props = oprops;
16455 aelt = Fassoc (elt, mode_line_proptrans_alist);
16456 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16458 /* AELT is what we want. Move it to the front
16459 without consing. */
16460 elt = XCAR (aelt);
16461 mode_line_proptrans_alist
16462 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16464 else
16466 Lisp_Object tem;
16468 /* If AELT has the wrong props, it is useless.
16469 so get rid of it. */
16470 if (! NILP (aelt))
16471 mode_line_proptrans_alist
16472 = Fdelq (aelt, mode_line_proptrans_alist);
16474 elt = Fcopy_sequence (elt);
16475 Fset_text_properties (make_number (0), Flength (elt),
16476 props, elt);
16477 /* Add this item to mode_line_proptrans_alist. */
16478 mode_line_proptrans_alist
16479 = Fcons (Fcons (elt, props),
16480 mode_line_proptrans_alist);
16481 /* Truncate mode_line_proptrans_alist
16482 to at most 50 elements. */
16483 tem = Fnthcdr (make_number (50),
16484 mode_line_proptrans_alist);
16485 if (! NILP (tem))
16486 XSETCDR (tem, Qnil);
16491 offset = 0;
16493 if (literal)
16495 prec = precision - n;
16496 switch (mode_line_target)
16498 case MODE_LINE_NOPROP:
16499 case MODE_LINE_TITLE:
16500 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16501 break;
16502 case MODE_LINE_STRING:
16503 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16504 break;
16505 case MODE_LINE_DISPLAY:
16506 n += display_string (NULL, elt, Qnil, 0, 0, it,
16507 0, prec, 0, STRING_MULTIBYTE (elt));
16508 break;
16511 break;
16514 /* Handle the non-literal case. */
16516 while ((precision <= 0 || n < precision)
16517 && SREF (elt, offset) != 0
16518 && (mode_line_target != MODE_LINE_DISPLAY
16519 || it->current_x < it->last_visible_x))
16521 int last_offset = offset;
16523 /* Advance to end of string or next format specifier. */
16524 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16527 if (offset - 1 != last_offset)
16529 int nchars, nbytes;
16531 /* Output to end of string or up to '%'. Field width
16532 is length of string. Don't output more than
16533 PRECISION allows us. */
16534 offset--;
16536 prec = c_string_width (SDATA (elt) + last_offset,
16537 offset - last_offset, precision - n,
16538 &nchars, &nbytes);
16540 switch (mode_line_target)
16542 case MODE_LINE_NOPROP:
16543 case MODE_LINE_TITLE:
16544 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16545 break;
16546 case MODE_LINE_STRING:
16548 int bytepos = last_offset;
16549 int charpos = string_byte_to_char (elt, bytepos);
16550 int endpos = (precision <= 0
16551 ? string_byte_to_char (elt, offset)
16552 : charpos + nchars);
16554 n += store_mode_line_string (NULL,
16555 Fsubstring (elt, make_number (charpos),
16556 make_number (endpos)),
16557 0, 0, 0, Qnil);
16559 break;
16560 case MODE_LINE_DISPLAY:
16562 int bytepos = last_offset;
16563 int charpos = string_byte_to_char (elt, bytepos);
16565 if (precision <= 0)
16566 nchars = string_byte_to_char (elt, offset) - charpos;
16567 n += display_string (NULL, elt, Qnil, 0, charpos,
16568 it, 0, nchars, 0,
16569 STRING_MULTIBYTE (elt));
16571 break;
16574 else /* c == '%' */
16576 int percent_position = offset;
16578 /* Get the specified minimum width. Zero means
16579 don't pad. */
16580 field = 0;
16581 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16582 field = field * 10 + c - '0';
16584 /* Don't pad beyond the total padding allowed. */
16585 if (field_width - n > 0 && field > field_width - n)
16586 field = field_width - n;
16588 /* Note that either PRECISION <= 0 or N < PRECISION. */
16589 prec = precision - n;
16591 if (c == 'M')
16592 n += display_mode_element (it, depth, field, prec,
16593 Vglobal_mode_string, props,
16594 risky);
16595 else if (c != 0)
16597 int multibyte;
16598 int bytepos, charpos;
16599 unsigned char *spec;
16601 bytepos = percent_position;
16602 charpos = (STRING_MULTIBYTE (elt)
16603 ? string_byte_to_char (elt, bytepos)
16604 : bytepos);
16606 spec
16607 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16609 switch (mode_line_target)
16611 case MODE_LINE_NOPROP:
16612 case MODE_LINE_TITLE:
16613 n += store_mode_line_noprop (spec, field, prec);
16614 break;
16615 case MODE_LINE_STRING:
16617 int len = strlen (spec);
16618 Lisp_Object tem = make_string (spec, len);
16619 props = Ftext_properties_at (make_number (charpos), elt);
16620 /* Should only keep face property in props */
16621 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16623 break;
16624 case MODE_LINE_DISPLAY:
16626 int nglyphs_before, nwritten;
16628 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16629 nwritten = display_string (spec, Qnil, elt,
16630 charpos, 0, it,
16631 field, prec, 0,
16632 multibyte);
16634 /* Assign to the glyphs written above the
16635 string where the `%x' came from, position
16636 of the `%'. */
16637 if (nwritten > 0)
16639 struct glyph *glyph
16640 = (it->glyph_row->glyphs[TEXT_AREA]
16641 + nglyphs_before);
16642 int i;
16644 for (i = 0; i < nwritten; ++i)
16646 glyph[i].object = elt;
16647 glyph[i].charpos = charpos;
16650 n += nwritten;
16653 break;
16656 else /* c == 0 */
16657 break;
16661 break;
16663 case Lisp_Symbol:
16664 /* A symbol: process the value of the symbol recursively
16665 as if it appeared here directly. Avoid error if symbol void.
16666 Special case: if value of symbol is a string, output the string
16667 literally. */
16669 register Lisp_Object tem;
16671 /* If the variable is not marked as risky to set
16672 then its contents are risky to use. */
16673 if (NILP (Fget (elt, Qrisky_local_variable)))
16674 risky = 1;
16676 tem = Fboundp (elt);
16677 if (!NILP (tem))
16679 tem = Fsymbol_value (elt);
16680 /* If value is a string, output that string literally:
16681 don't check for % within it. */
16682 if (STRINGP (tem))
16683 literal = 1;
16685 if (!EQ (tem, elt))
16687 /* Give up right away for nil or t. */
16688 elt = tem;
16689 goto tail_recurse;
16693 break;
16695 case Lisp_Cons:
16697 register Lisp_Object car, tem;
16699 /* A cons cell: five distinct cases.
16700 If first element is :eval or :propertize, do something special.
16701 If first element is a string or a cons, process all the elements
16702 and effectively concatenate them.
16703 If first element is a negative number, truncate displaying cdr to
16704 at most that many characters. If positive, pad (with spaces)
16705 to at least that many characters.
16706 If first element is a symbol, process the cadr or caddr recursively
16707 according to whether the symbol's value is non-nil or nil. */
16708 car = XCAR (elt);
16709 if (EQ (car, QCeval))
16711 /* An element of the form (:eval FORM) means evaluate FORM
16712 and use the result as mode line elements. */
16714 if (risky)
16715 break;
16717 if (CONSP (XCDR (elt)))
16719 Lisp_Object spec;
16720 spec = safe_eval (XCAR (XCDR (elt)));
16721 n += display_mode_element (it, depth, field_width - n,
16722 precision - n, spec, props,
16723 risky);
16726 else if (EQ (car, QCpropertize))
16728 /* An element of the form (:propertize ELT PROPS...)
16729 means display ELT but applying properties PROPS. */
16731 if (risky)
16732 break;
16734 if (CONSP (XCDR (elt)))
16735 n += display_mode_element (it, depth, field_width - n,
16736 precision - n, XCAR (XCDR (elt)),
16737 XCDR (XCDR (elt)), risky);
16739 else if (SYMBOLP (car))
16741 tem = Fboundp (car);
16742 elt = XCDR (elt);
16743 if (!CONSP (elt))
16744 goto invalid;
16745 /* elt is now the cdr, and we know it is a cons cell.
16746 Use its car if CAR has a non-nil value. */
16747 if (!NILP (tem))
16749 tem = Fsymbol_value (car);
16750 if (!NILP (tem))
16752 elt = XCAR (elt);
16753 goto tail_recurse;
16756 /* Symbol's value is nil (or symbol is unbound)
16757 Get the cddr of the original list
16758 and if possible find the caddr and use that. */
16759 elt = XCDR (elt);
16760 if (NILP (elt))
16761 break;
16762 else if (!CONSP (elt))
16763 goto invalid;
16764 elt = XCAR (elt);
16765 goto tail_recurse;
16767 else if (INTEGERP (car))
16769 register int lim = XINT (car);
16770 elt = XCDR (elt);
16771 if (lim < 0)
16773 /* Negative int means reduce maximum width. */
16774 if (precision <= 0)
16775 precision = -lim;
16776 else
16777 precision = min (precision, -lim);
16779 else if (lim > 0)
16781 /* Padding specified. Don't let it be more than
16782 current maximum. */
16783 if (precision > 0)
16784 lim = min (precision, lim);
16786 /* If that's more padding than already wanted, queue it.
16787 But don't reduce padding already specified even if
16788 that is beyond the current truncation point. */
16789 field_width = max (lim, field_width);
16791 goto tail_recurse;
16793 else if (STRINGP (car) || CONSP (car))
16795 register int limit = 50;
16796 /* Limit is to protect against circular lists. */
16797 while (CONSP (elt)
16798 && --limit > 0
16799 && (precision <= 0 || n < precision))
16801 n += display_mode_element (it, depth,
16802 /* Do padding only after the last
16803 element in the list. */
16804 (! CONSP (XCDR (elt))
16805 ? field_width - n
16806 : 0),
16807 precision - n, XCAR (elt),
16808 props, risky);
16809 elt = XCDR (elt);
16813 break;
16815 default:
16816 invalid:
16817 elt = build_string ("*invalid*");
16818 goto tail_recurse;
16821 /* Pad to FIELD_WIDTH. */
16822 if (field_width > 0 && n < field_width)
16824 switch (mode_line_target)
16826 case MODE_LINE_NOPROP:
16827 case MODE_LINE_TITLE:
16828 n += store_mode_line_noprop ("", field_width - n, 0);
16829 break;
16830 case MODE_LINE_STRING:
16831 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16832 break;
16833 case MODE_LINE_DISPLAY:
16834 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16835 0, 0, 0);
16836 break;
16840 return n;
16843 /* Store a mode-line string element in mode_line_string_list.
16845 If STRING is non-null, display that C string. Otherwise, the Lisp
16846 string LISP_STRING is displayed.
16848 FIELD_WIDTH is the minimum number of output glyphs to produce.
16849 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16850 with spaces. FIELD_WIDTH <= 0 means don't pad.
16852 PRECISION is the maximum number of characters to output from
16853 STRING. PRECISION <= 0 means don't truncate the string.
16855 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16856 properties to the string.
16858 PROPS are the properties to add to the string.
16859 The mode_line_string_face face property is always added to the string.
16862 static int
16863 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16864 char *string;
16865 Lisp_Object lisp_string;
16866 int copy_string;
16867 int field_width;
16868 int precision;
16869 Lisp_Object props;
16871 int len;
16872 int n = 0;
16874 if (string != NULL)
16876 len = strlen (string);
16877 if (precision > 0 && len > precision)
16878 len = precision;
16879 lisp_string = make_string (string, len);
16880 if (NILP (props))
16881 props = mode_line_string_face_prop;
16882 else if (!NILP (mode_line_string_face))
16884 Lisp_Object face = Fplist_get (props, Qface);
16885 props = Fcopy_sequence (props);
16886 if (NILP (face))
16887 face = mode_line_string_face;
16888 else
16889 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16890 props = Fplist_put (props, Qface, face);
16892 Fadd_text_properties (make_number (0), make_number (len),
16893 props, lisp_string);
16895 else
16897 len = XFASTINT (Flength (lisp_string));
16898 if (precision > 0 && len > precision)
16900 len = precision;
16901 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16902 precision = -1;
16904 if (!NILP (mode_line_string_face))
16906 Lisp_Object face;
16907 if (NILP (props))
16908 props = Ftext_properties_at (make_number (0), lisp_string);
16909 face = Fplist_get (props, Qface);
16910 if (NILP (face))
16911 face = mode_line_string_face;
16912 else
16913 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16914 props = Fcons (Qface, Fcons (face, Qnil));
16915 if (copy_string)
16916 lisp_string = Fcopy_sequence (lisp_string);
16918 if (!NILP (props))
16919 Fadd_text_properties (make_number (0), make_number (len),
16920 props, lisp_string);
16923 if (len > 0)
16925 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16926 n += len;
16929 if (field_width > len)
16931 field_width -= len;
16932 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16933 if (!NILP (props))
16934 Fadd_text_properties (make_number (0), make_number (field_width),
16935 props, lisp_string);
16936 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16937 n += field_width;
16940 return n;
16944 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16945 1, 4, 0,
16946 doc: /* Format a string out of a mode line format specification.
16947 First arg FORMAT specifies the mode line format (see `mode-line-format'
16948 for details) to use.
16950 Optional second arg FACE specifies the face property to put
16951 on all characters for which no face is specified.
16952 t means whatever face the window's mode line currently uses
16953 \(either `mode-line' or `mode-line-inactive', depending).
16954 nil means the default is no face property.
16955 If FACE is an integer, the value string has no text properties.
16957 Optional third and fourth args WINDOW and BUFFER specify the window
16958 and buffer to use as the context for the formatting (defaults
16959 are the selected window and the window's buffer). */)
16960 (format, face, window, buffer)
16961 Lisp_Object format, face, window, buffer;
16963 struct it it;
16964 int len;
16965 struct window *w;
16966 struct buffer *old_buffer = NULL;
16967 int face_id = -1;
16968 int no_props = INTEGERP (face);
16969 int count = SPECPDL_INDEX ();
16970 Lisp_Object str;
16971 int string_start = 0;
16973 if (NILP (window))
16974 window = selected_window;
16975 CHECK_WINDOW (window);
16976 w = XWINDOW (window);
16978 if (NILP (buffer))
16979 buffer = w->buffer;
16980 CHECK_BUFFER (buffer);
16982 if (NILP (format))
16983 return build_string ("");
16985 if (no_props)
16986 face = Qnil;
16988 if (!NILP (face))
16990 if (EQ (face, Qt))
16991 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16992 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16995 if (face_id < 0)
16996 face_id = DEFAULT_FACE_ID;
16998 if (XBUFFER (buffer) != current_buffer)
16999 old_buffer = current_buffer;
17001 /* Save things including mode_line_proptrans_alist,
17002 and set that to nil so that we don't alter the outer value. */
17003 record_unwind_protect (unwind_format_mode_line,
17004 format_mode_line_unwind_data (old_buffer, 1));
17005 mode_line_proptrans_alist = Qnil;
17007 if (old_buffer)
17008 set_buffer_internal_1 (XBUFFER (buffer));
17010 init_iterator (&it, w, -1, -1, NULL, face_id);
17012 if (no_props)
17014 mode_line_target = MODE_LINE_NOPROP;
17015 mode_line_string_face_prop = Qnil;
17016 mode_line_string_list = Qnil;
17017 string_start = MODE_LINE_NOPROP_LEN (0);
17019 else
17021 mode_line_target = MODE_LINE_STRING;
17022 mode_line_string_list = Qnil;
17023 mode_line_string_face = face;
17024 mode_line_string_face_prop
17025 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17028 push_frame_kboard (it.f);
17029 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17030 pop_frame_kboard ();
17032 if (no_props)
17034 len = MODE_LINE_NOPROP_LEN (string_start);
17035 str = make_string (mode_line_noprop_buf + string_start, len);
17037 else
17039 mode_line_string_list = Fnreverse (mode_line_string_list);
17040 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17041 make_string ("", 0));
17044 unbind_to (count, Qnil);
17045 return str;
17048 /* Write a null-terminated, right justified decimal representation of
17049 the positive integer D to BUF using a minimal field width WIDTH. */
17051 static void
17052 pint2str (buf, width, d)
17053 register char *buf;
17054 register int width;
17055 register int d;
17057 register char *p = buf;
17059 if (d <= 0)
17060 *p++ = '0';
17061 else
17063 while (d > 0)
17065 *p++ = d % 10 + '0';
17066 d /= 10;
17070 for (width -= (int) (p - buf); width > 0; --width)
17071 *p++ = ' ';
17072 *p-- = '\0';
17073 while (p > buf)
17075 d = *buf;
17076 *buf++ = *p;
17077 *p-- = d;
17081 /* Write a null-terminated, right justified decimal and "human
17082 readable" representation of the nonnegative integer D to BUF using
17083 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17085 static const char power_letter[] =
17087 0, /* not used */
17088 'k', /* kilo */
17089 'M', /* mega */
17090 'G', /* giga */
17091 'T', /* tera */
17092 'P', /* peta */
17093 'E', /* exa */
17094 'Z', /* zetta */
17095 'Y' /* yotta */
17098 static void
17099 pint2hrstr (buf, width, d)
17100 char *buf;
17101 int width;
17102 int d;
17104 /* We aim to represent the nonnegative integer D as
17105 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17106 int quotient = d;
17107 int remainder = 0;
17108 /* -1 means: do not use TENTHS. */
17109 int tenths = -1;
17110 int exponent = 0;
17112 /* Length of QUOTIENT.TENTHS as a string. */
17113 int length;
17115 char * psuffix;
17116 char * p;
17118 if (1000 <= quotient)
17120 /* Scale to the appropriate EXPONENT. */
17123 remainder = quotient % 1000;
17124 quotient /= 1000;
17125 exponent++;
17127 while (1000 <= quotient);
17129 /* Round to nearest and decide whether to use TENTHS or not. */
17130 if (quotient <= 9)
17132 tenths = remainder / 100;
17133 if (50 <= remainder % 100)
17135 if (tenths < 9)
17136 tenths++;
17137 else
17139 quotient++;
17140 if (quotient == 10)
17141 tenths = -1;
17142 else
17143 tenths = 0;
17147 else
17148 if (500 <= remainder)
17150 if (quotient < 999)
17151 quotient++;
17152 else
17154 quotient = 1;
17155 exponent++;
17156 tenths = 0;
17161 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17162 if (tenths == -1 && quotient <= 99)
17163 if (quotient <= 9)
17164 length = 1;
17165 else
17166 length = 2;
17167 else
17168 length = 3;
17169 p = psuffix = buf + max (width, length);
17171 /* Print EXPONENT. */
17172 if (exponent)
17173 *psuffix++ = power_letter[exponent];
17174 *psuffix = '\0';
17176 /* Print TENTHS. */
17177 if (tenths >= 0)
17179 *--p = '0' + tenths;
17180 *--p = '.';
17183 /* Print QUOTIENT. */
17186 int digit = quotient % 10;
17187 *--p = '0' + digit;
17189 while ((quotient /= 10) != 0);
17191 /* Print leading spaces. */
17192 while (buf < p)
17193 *--p = ' ';
17196 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17197 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17198 type of CODING_SYSTEM. Return updated pointer into BUF. */
17200 static unsigned char invalid_eol_type[] = "(*invalid*)";
17202 static char *
17203 decode_mode_spec_coding (coding_system, buf, eol_flag)
17204 Lisp_Object coding_system;
17205 register char *buf;
17206 int eol_flag;
17208 Lisp_Object val;
17209 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17210 const unsigned char *eol_str;
17211 int eol_str_len;
17212 /* The EOL conversion we are using. */
17213 Lisp_Object eoltype;
17215 val = Fget (coding_system, Qcoding_system);
17216 eoltype = Qnil;
17218 if (!VECTORP (val)) /* Not yet decided. */
17220 if (multibyte)
17221 *buf++ = '-';
17222 if (eol_flag)
17223 eoltype = eol_mnemonic_undecided;
17224 /* Don't mention EOL conversion if it isn't decided. */
17226 else
17228 Lisp_Object eolvalue;
17230 eolvalue = Fget (coding_system, Qeol_type);
17232 if (multibyte)
17233 *buf++ = XFASTINT (AREF (val, 1));
17235 if (eol_flag)
17237 /* The EOL conversion that is normal on this system. */
17239 if (NILP (eolvalue)) /* Not yet decided. */
17240 eoltype = eol_mnemonic_undecided;
17241 else if (VECTORP (eolvalue)) /* Not yet decided. */
17242 eoltype = eol_mnemonic_undecided;
17243 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17244 eoltype = (XFASTINT (eolvalue) == 0
17245 ? eol_mnemonic_unix
17246 : (XFASTINT (eolvalue) == 1
17247 ? eol_mnemonic_dos : eol_mnemonic_mac));
17251 if (eol_flag)
17253 /* Mention the EOL conversion if it is not the usual one. */
17254 if (STRINGP (eoltype))
17256 eol_str = SDATA (eoltype);
17257 eol_str_len = SBYTES (eoltype);
17259 else if (INTEGERP (eoltype)
17260 && CHAR_VALID_P (XINT (eoltype), 0))
17262 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17263 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17264 eol_str = tmp;
17266 else
17268 eol_str = invalid_eol_type;
17269 eol_str_len = sizeof (invalid_eol_type) - 1;
17271 bcopy (eol_str, buf, eol_str_len);
17272 buf += eol_str_len;
17275 return buf;
17278 /* Return a string for the output of a mode line %-spec for window W,
17279 generated by character C. PRECISION >= 0 means don't return a
17280 string longer than that value. FIELD_WIDTH > 0 means pad the
17281 string returned with spaces to that value. Return 1 in *MULTIBYTE
17282 if the result is multibyte text.
17284 Note we operate on the current buffer for most purposes,
17285 the exception being w->base_line_pos. */
17287 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17289 static char *
17290 decode_mode_spec (w, c, field_width, precision, multibyte)
17291 struct window *w;
17292 register int c;
17293 int field_width, precision;
17294 int *multibyte;
17296 Lisp_Object obj;
17297 struct frame *f = XFRAME (WINDOW_FRAME (w));
17298 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17299 struct buffer *b = current_buffer;
17301 obj = Qnil;
17302 *multibyte = 0;
17304 switch (c)
17306 case '*':
17307 if (!NILP (b->read_only))
17308 return "%";
17309 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17310 return "*";
17311 return "-";
17313 case '+':
17314 /* This differs from %* only for a modified read-only buffer. */
17315 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17316 return "*";
17317 if (!NILP (b->read_only))
17318 return "%";
17319 return "-";
17321 case '&':
17322 /* This differs from %* in ignoring read-only-ness. */
17323 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17324 return "*";
17325 return "-";
17327 case '%':
17328 return "%";
17330 case '[':
17332 int i;
17333 char *p;
17335 if (command_loop_level > 5)
17336 return "[[[... ";
17337 p = decode_mode_spec_buf;
17338 for (i = 0; i < command_loop_level; i++)
17339 *p++ = '[';
17340 *p = 0;
17341 return decode_mode_spec_buf;
17344 case ']':
17346 int i;
17347 char *p;
17349 if (command_loop_level > 5)
17350 return " ...]]]";
17351 p = decode_mode_spec_buf;
17352 for (i = 0; i < command_loop_level; i++)
17353 *p++ = ']';
17354 *p = 0;
17355 return decode_mode_spec_buf;
17358 case '-':
17360 register int i;
17362 /* Let lots_of_dashes be a string of infinite length. */
17363 if (mode_line_target == MODE_LINE_NOPROP ||
17364 mode_line_target == MODE_LINE_STRING)
17365 return "--";
17366 if (field_width <= 0
17367 || field_width > sizeof (lots_of_dashes))
17369 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17370 decode_mode_spec_buf[i] = '-';
17371 decode_mode_spec_buf[i] = '\0';
17372 return decode_mode_spec_buf;
17374 else
17375 return lots_of_dashes;
17378 case 'b':
17379 obj = b->name;
17380 break;
17382 case 'c':
17384 int col = (int) current_column (); /* iftc */
17385 w->column_number_displayed = make_number (col);
17386 pint2str (decode_mode_spec_buf, field_width, col);
17387 return decode_mode_spec_buf;
17390 case 'e':
17391 #ifndef SYSTEM_MALLOC
17393 if (NILP (Vmemory_full))
17394 return "";
17395 else
17396 return "!MEM FULL! ";
17398 #else
17399 return "";
17400 #endif
17402 case 'F':
17403 /* %F displays the frame name. */
17404 if (!NILP (f->title))
17405 return (char *) SDATA (f->title);
17406 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17407 return (char *) SDATA (f->name);
17408 return "Emacs";
17410 case 'f':
17411 obj = b->filename;
17412 break;
17414 case 'i':
17416 int size = ZV - BEGV;
17417 pint2str (decode_mode_spec_buf, field_width, size);
17418 return decode_mode_spec_buf;
17421 case 'I':
17423 int size = ZV - BEGV;
17424 pint2hrstr (decode_mode_spec_buf, field_width, size);
17425 return decode_mode_spec_buf;
17428 case 'l':
17430 int startpos = XMARKER (w->start)->charpos;
17431 int startpos_byte = marker_byte_position (w->start);
17432 int line, linepos, linepos_byte, topline;
17433 int nlines, junk;
17434 int height = WINDOW_TOTAL_LINES (w);
17436 /* If we decided that this buffer isn't suitable for line numbers,
17437 don't forget that too fast. */
17438 if (EQ (w->base_line_pos, w->buffer))
17439 goto no_value;
17440 /* But do forget it, if the window shows a different buffer now. */
17441 else if (BUFFERP (w->base_line_pos))
17442 w->base_line_pos = Qnil;
17444 /* If the buffer is very big, don't waste time. */
17445 if (INTEGERP (Vline_number_display_limit)
17446 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17448 w->base_line_pos = Qnil;
17449 w->base_line_number = Qnil;
17450 goto no_value;
17453 if (!NILP (w->base_line_number)
17454 && !NILP (w->base_line_pos)
17455 && XFASTINT (w->base_line_pos) <= startpos)
17457 line = XFASTINT (w->base_line_number);
17458 linepos = XFASTINT (w->base_line_pos);
17459 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17461 else
17463 line = 1;
17464 linepos = BUF_BEGV (b);
17465 linepos_byte = BUF_BEGV_BYTE (b);
17468 /* Count lines from base line to window start position. */
17469 nlines = display_count_lines (linepos, linepos_byte,
17470 startpos_byte,
17471 startpos, &junk);
17473 topline = nlines + line;
17475 /* Determine a new base line, if the old one is too close
17476 or too far away, or if we did not have one.
17477 "Too close" means it's plausible a scroll-down would
17478 go back past it. */
17479 if (startpos == BUF_BEGV (b))
17481 w->base_line_number = make_number (topline);
17482 w->base_line_pos = make_number (BUF_BEGV (b));
17484 else if (nlines < height + 25 || nlines > height * 3 + 50
17485 || linepos == BUF_BEGV (b))
17487 int limit = BUF_BEGV (b);
17488 int limit_byte = BUF_BEGV_BYTE (b);
17489 int position;
17490 int distance = (height * 2 + 30) * line_number_display_limit_width;
17492 if (startpos - distance > limit)
17494 limit = startpos - distance;
17495 limit_byte = CHAR_TO_BYTE (limit);
17498 nlines = display_count_lines (startpos, startpos_byte,
17499 limit_byte,
17500 - (height * 2 + 30),
17501 &position);
17502 /* If we couldn't find the lines we wanted within
17503 line_number_display_limit_width chars per line,
17504 give up on line numbers for this window. */
17505 if (position == limit_byte && limit == startpos - distance)
17507 w->base_line_pos = w->buffer;
17508 w->base_line_number = Qnil;
17509 goto no_value;
17512 w->base_line_number = make_number (topline - nlines);
17513 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17516 /* Now count lines from the start pos to point. */
17517 nlines = display_count_lines (startpos, startpos_byte,
17518 PT_BYTE, PT, &junk);
17520 /* Record that we did display the line number. */
17521 line_number_displayed = 1;
17523 /* Make the string to show. */
17524 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17525 return decode_mode_spec_buf;
17526 no_value:
17528 char* p = decode_mode_spec_buf;
17529 int pad = field_width - 2;
17530 while (pad-- > 0)
17531 *p++ = ' ';
17532 *p++ = '?';
17533 *p++ = '?';
17534 *p = '\0';
17535 return decode_mode_spec_buf;
17538 break;
17540 case 'm':
17541 obj = b->mode_name;
17542 break;
17544 case 'n':
17545 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17546 return " Narrow";
17547 break;
17549 case 'p':
17551 int pos = marker_position (w->start);
17552 int total = BUF_ZV (b) - BUF_BEGV (b);
17554 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17556 if (pos <= BUF_BEGV (b))
17557 return "All";
17558 else
17559 return "Bottom";
17561 else if (pos <= BUF_BEGV (b))
17562 return "Top";
17563 else
17565 if (total > 1000000)
17566 /* Do it differently for a large value, to avoid overflow. */
17567 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17568 else
17569 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17570 /* We can't normally display a 3-digit number,
17571 so get us a 2-digit number that is close. */
17572 if (total == 100)
17573 total = 99;
17574 sprintf (decode_mode_spec_buf, "%2d%%", total);
17575 return decode_mode_spec_buf;
17579 /* Display percentage of size above the bottom of the screen. */
17580 case 'P':
17582 int toppos = marker_position (w->start);
17583 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17584 int total = BUF_ZV (b) - BUF_BEGV (b);
17586 if (botpos >= BUF_ZV (b))
17588 if (toppos <= BUF_BEGV (b))
17589 return "All";
17590 else
17591 return "Bottom";
17593 else
17595 if (total > 1000000)
17596 /* Do it differently for a large value, to avoid overflow. */
17597 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17598 else
17599 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17600 /* We can't normally display a 3-digit number,
17601 so get us a 2-digit number that is close. */
17602 if (total == 100)
17603 total = 99;
17604 if (toppos <= BUF_BEGV (b))
17605 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17606 else
17607 sprintf (decode_mode_spec_buf, "%2d%%", total);
17608 return decode_mode_spec_buf;
17612 case 's':
17613 /* status of process */
17614 obj = Fget_buffer_process (Fcurrent_buffer ());
17615 if (NILP (obj))
17616 return "no process";
17617 #ifdef subprocesses
17618 obj = Fsymbol_name (Fprocess_status (obj));
17619 #endif
17620 break;
17622 case 't': /* indicate TEXT or BINARY */
17623 #ifdef MODE_LINE_BINARY_TEXT
17624 return MODE_LINE_BINARY_TEXT (b);
17625 #else
17626 return "T";
17627 #endif
17629 case 'z':
17630 /* coding-system (not including end-of-line format) */
17631 case 'Z':
17632 /* coding-system (including end-of-line type) */
17634 int eol_flag = (c == 'Z');
17635 char *p = decode_mode_spec_buf;
17637 if (! FRAME_WINDOW_P (f))
17639 /* No need to mention EOL here--the terminal never needs
17640 to do EOL conversion. */
17641 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17642 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17644 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17645 p, eol_flag);
17647 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17648 #ifdef subprocesses
17649 obj = Fget_buffer_process (Fcurrent_buffer ());
17650 if (PROCESSP (obj))
17652 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17653 p, eol_flag);
17654 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17655 p, eol_flag);
17657 #endif /* subprocesses */
17658 #endif /* 0 */
17659 *p = 0;
17660 return decode_mode_spec_buf;
17664 if (STRINGP (obj))
17666 *multibyte = STRING_MULTIBYTE (obj);
17667 return (char *) SDATA (obj);
17669 else
17670 return "";
17674 /* Count up to COUNT lines starting from START / START_BYTE.
17675 But don't go beyond LIMIT_BYTE.
17676 Return the number of lines thus found (always nonnegative).
17678 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17680 static int
17681 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17682 int start, start_byte, limit_byte, count;
17683 int *byte_pos_ptr;
17685 register unsigned char *cursor;
17686 unsigned char *base;
17688 register int ceiling;
17689 register unsigned char *ceiling_addr;
17690 int orig_count = count;
17692 /* If we are not in selective display mode,
17693 check only for newlines. */
17694 int selective_display = (!NILP (current_buffer->selective_display)
17695 && !INTEGERP (current_buffer->selective_display));
17697 if (count > 0)
17699 while (start_byte < limit_byte)
17701 ceiling = BUFFER_CEILING_OF (start_byte);
17702 ceiling = min (limit_byte - 1, ceiling);
17703 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17704 base = (cursor = BYTE_POS_ADDR (start_byte));
17705 while (1)
17707 if (selective_display)
17708 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17710 else
17711 while (*cursor != '\n' && ++cursor != ceiling_addr)
17714 if (cursor != ceiling_addr)
17716 if (--count == 0)
17718 start_byte += cursor - base + 1;
17719 *byte_pos_ptr = start_byte;
17720 return orig_count;
17722 else
17723 if (++cursor == ceiling_addr)
17724 break;
17726 else
17727 break;
17729 start_byte += cursor - base;
17732 else
17734 while (start_byte > limit_byte)
17736 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17737 ceiling = max (limit_byte, ceiling);
17738 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17739 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17740 while (1)
17742 if (selective_display)
17743 while (--cursor != ceiling_addr
17744 && *cursor != '\n' && *cursor != 015)
17746 else
17747 while (--cursor != ceiling_addr && *cursor != '\n')
17750 if (cursor != ceiling_addr)
17752 if (++count == 0)
17754 start_byte += cursor - base + 1;
17755 *byte_pos_ptr = start_byte;
17756 /* When scanning backwards, we should
17757 not count the newline posterior to which we stop. */
17758 return - orig_count - 1;
17761 else
17762 break;
17764 /* Here we add 1 to compensate for the last decrement
17765 of CURSOR, which took it past the valid range. */
17766 start_byte += cursor - base + 1;
17770 *byte_pos_ptr = limit_byte;
17772 if (count < 0)
17773 return - orig_count + count;
17774 return orig_count - count;
17780 /***********************************************************************
17781 Displaying strings
17782 ***********************************************************************/
17784 /* Display a NUL-terminated string, starting with index START.
17786 If STRING is non-null, display that C string. Otherwise, the Lisp
17787 string LISP_STRING is displayed.
17789 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17790 FACE_STRING. Display STRING or LISP_STRING with the face at
17791 FACE_STRING_POS in FACE_STRING:
17793 Display the string in the environment given by IT, but use the
17794 standard display table, temporarily.
17796 FIELD_WIDTH is the minimum number of output glyphs to produce.
17797 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17798 with spaces. If STRING has more characters, more than FIELD_WIDTH
17799 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17801 PRECISION is the maximum number of characters to output from
17802 STRING. PRECISION < 0 means don't truncate the string.
17804 This is roughly equivalent to printf format specifiers:
17806 FIELD_WIDTH PRECISION PRINTF
17807 ----------------------------------------
17808 -1 -1 %s
17809 -1 10 %.10s
17810 10 -1 %10s
17811 20 10 %20.10s
17813 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17814 display them, and < 0 means obey the current buffer's value of
17815 enable_multibyte_characters.
17817 Value is the number of columns displayed. */
17819 static int
17820 display_string (string, lisp_string, face_string, face_string_pos,
17821 start, it, field_width, precision, max_x, multibyte)
17822 unsigned char *string;
17823 Lisp_Object lisp_string;
17824 Lisp_Object face_string;
17825 int face_string_pos;
17826 int start;
17827 struct it *it;
17828 int field_width, precision, max_x;
17829 int multibyte;
17831 int hpos_at_start = it->hpos;
17832 int saved_face_id = it->face_id;
17833 struct glyph_row *row = it->glyph_row;
17835 /* Initialize the iterator IT for iteration over STRING beginning
17836 with index START. */
17837 reseat_to_string (it, string, lisp_string, start,
17838 precision, field_width, multibyte);
17840 /* If displaying STRING, set up the face of the iterator
17841 from LISP_STRING, if that's given. */
17842 if (STRINGP (face_string))
17844 int endptr;
17845 struct face *face;
17847 it->face_id
17848 = face_at_string_position (it->w, face_string, face_string_pos,
17849 0, it->region_beg_charpos,
17850 it->region_end_charpos,
17851 &endptr, it->base_face_id, 0);
17852 face = FACE_FROM_ID (it->f, it->face_id);
17853 it->face_box_p = face->box != FACE_NO_BOX;
17856 /* Set max_x to the maximum allowed X position. Don't let it go
17857 beyond the right edge of the window. */
17858 if (max_x <= 0)
17859 max_x = it->last_visible_x;
17860 else
17861 max_x = min (max_x, it->last_visible_x);
17863 /* Skip over display elements that are not visible. because IT->w is
17864 hscrolled. */
17865 if (it->current_x < it->first_visible_x)
17866 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17867 MOVE_TO_POS | MOVE_TO_X);
17869 row->ascent = it->max_ascent;
17870 row->height = it->max_ascent + it->max_descent;
17871 row->phys_ascent = it->max_phys_ascent;
17872 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17873 row->extra_line_spacing = it->max_extra_line_spacing;
17875 /* This condition is for the case that we are called with current_x
17876 past last_visible_x. */
17877 while (it->current_x < max_x)
17879 int x_before, x, n_glyphs_before, i, nglyphs;
17881 /* Get the next display element. */
17882 if (!get_next_display_element (it))
17883 break;
17885 /* Produce glyphs. */
17886 x_before = it->current_x;
17887 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17888 PRODUCE_GLYPHS (it);
17890 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17891 i = 0;
17892 x = x_before;
17893 while (i < nglyphs)
17895 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17897 if (!it->truncate_lines_p
17898 && x + glyph->pixel_width > max_x)
17900 /* End of continued line or max_x reached. */
17901 if (CHAR_GLYPH_PADDING_P (*glyph))
17903 /* A wide character is unbreakable. */
17904 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17905 it->current_x = x_before;
17907 else
17909 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17910 it->current_x = x;
17912 break;
17914 else if (x + glyph->pixel_width > it->first_visible_x)
17916 /* Glyph is at least partially visible. */
17917 ++it->hpos;
17918 if (x < it->first_visible_x)
17919 it->glyph_row->x = x - it->first_visible_x;
17921 else
17923 /* Glyph is off the left margin of the display area.
17924 Should not happen. */
17925 abort ();
17928 row->ascent = max (row->ascent, it->max_ascent);
17929 row->height = max (row->height, it->max_ascent + it->max_descent);
17930 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17931 row->phys_height = max (row->phys_height,
17932 it->max_phys_ascent + it->max_phys_descent);
17933 row->extra_line_spacing = max (row->extra_line_spacing,
17934 it->max_extra_line_spacing);
17935 x += glyph->pixel_width;
17936 ++i;
17939 /* Stop if max_x reached. */
17940 if (i < nglyphs)
17941 break;
17943 /* Stop at line ends. */
17944 if (ITERATOR_AT_END_OF_LINE_P (it))
17946 it->continuation_lines_width = 0;
17947 break;
17950 set_iterator_to_next (it, 1);
17952 /* Stop if truncating at the right edge. */
17953 if (it->truncate_lines_p
17954 && it->current_x >= it->last_visible_x)
17956 /* Add truncation mark, but don't do it if the line is
17957 truncated at a padding space. */
17958 if (IT_CHARPOS (*it) < it->string_nchars)
17960 if (!FRAME_WINDOW_P (it->f))
17962 int i, n;
17964 if (it->current_x > it->last_visible_x)
17966 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17967 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17968 break;
17969 for (n = row->used[TEXT_AREA]; i < n; ++i)
17971 row->used[TEXT_AREA] = i;
17972 produce_special_glyphs (it, IT_TRUNCATION);
17975 produce_special_glyphs (it, IT_TRUNCATION);
17977 it->glyph_row->truncated_on_right_p = 1;
17979 break;
17983 /* Maybe insert a truncation at the left. */
17984 if (it->first_visible_x
17985 && IT_CHARPOS (*it) > 0)
17987 if (!FRAME_WINDOW_P (it->f))
17988 insert_left_trunc_glyphs (it);
17989 it->glyph_row->truncated_on_left_p = 1;
17992 it->face_id = saved_face_id;
17994 /* Value is number of columns displayed. */
17995 return it->hpos - hpos_at_start;
18000 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18001 appears as an element of LIST or as the car of an element of LIST.
18002 If PROPVAL is a list, compare each element against LIST in that
18003 way, and return 1/2 if any element of PROPVAL is found in LIST.
18004 Otherwise return 0. This function cannot quit.
18005 The return value is 2 if the text is invisible but with an ellipsis
18006 and 1 if it's invisible and without an ellipsis. */
18009 invisible_p (propval, list)
18010 register Lisp_Object propval;
18011 Lisp_Object list;
18013 register Lisp_Object tail, proptail;
18015 for (tail = list; CONSP (tail); tail = XCDR (tail))
18017 register Lisp_Object tem;
18018 tem = XCAR (tail);
18019 if (EQ (propval, tem))
18020 return 1;
18021 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18022 return NILP (XCDR (tem)) ? 1 : 2;
18025 if (CONSP (propval))
18027 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18029 Lisp_Object propelt;
18030 propelt = XCAR (proptail);
18031 for (tail = list; CONSP (tail); tail = XCDR (tail))
18033 register Lisp_Object tem;
18034 tem = XCAR (tail);
18035 if (EQ (propelt, tem))
18036 return 1;
18037 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18038 return NILP (XCDR (tem)) ? 1 : 2;
18043 return 0;
18046 /* Calculate a width or height in pixels from a specification using
18047 the following elements:
18049 SPEC ::=
18050 NUM - a (fractional) multiple of the default font width/height
18051 (NUM) - specifies exactly NUM pixels
18052 UNIT - a fixed number of pixels, see below.
18053 ELEMENT - size of a display element in pixels, see below.
18054 (NUM . SPEC) - equals NUM * SPEC
18055 (+ SPEC SPEC ...) - add pixel values
18056 (- SPEC SPEC ...) - subtract pixel values
18057 (- SPEC) - negate pixel value
18059 NUM ::=
18060 INT or FLOAT - a number constant
18061 SYMBOL - use symbol's (buffer local) variable binding.
18063 UNIT ::=
18064 in - pixels per inch *)
18065 mm - pixels per 1/1000 meter *)
18066 cm - pixels per 1/100 meter *)
18067 width - width of current font in pixels.
18068 height - height of current font in pixels.
18070 *) using the ratio(s) defined in display-pixels-per-inch.
18072 ELEMENT ::=
18074 left-fringe - left fringe width in pixels
18075 right-fringe - right fringe width in pixels
18077 left-margin - left margin width in pixels
18078 right-margin - right margin width in pixels
18080 scroll-bar - scroll-bar area width in pixels
18082 Examples:
18084 Pixels corresponding to 5 inches:
18085 (5 . in)
18087 Total width of non-text areas on left side of window (if scroll-bar is on left):
18088 '(space :width (+ left-fringe left-margin scroll-bar))
18090 Align to first text column (in header line):
18091 '(space :align-to 0)
18093 Align to middle of text area minus half the width of variable `my-image'
18094 containing a loaded image:
18095 '(space :align-to (0.5 . (- text my-image)))
18097 Width of left margin minus width of 1 character in the default font:
18098 '(space :width (- left-margin 1))
18100 Width of left margin minus width of 2 characters in the current font:
18101 '(space :width (- left-margin (2 . width)))
18103 Center 1 character over left-margin (in header line):
18104 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18106 Different ways to express width of left fringe plus left margin minus one pixel:
18107 '(space :width (- (+ left-fringe left-margin) (1)))
18108 '(space :width (+ left-fringe left-margin (- (1))))
18109 '(space :width (+ left-fringe left-margin (-1)))
18113 #define NUMVAL(X) \
18114 ((INTEGERP (X) || FLOATP (X)) \
18115 ? XFLOATINT (X) \
18116 : - 1)
18119 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18120 double *res;
18121 struct it *it;
18122 Lisp_Object prop;
18123 void *font;
18124 int width_p, *align_to;
18126 double pixels;
18128 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18129 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18131 if (NILP (prop))
18132 return OK_PIXELS (0);
18134 if (SYMBOLP (prop))
18136 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18138 char *unit = SDATA (SYMBOL_NAME (prop));
18140 if (unit[0] == 'i' && unit[1] == 'n')
18141 pixels = 1.0;
18142 else if (unit[0] == 'm' && unit[1] == 'm')
18143 pixels = 25.4;
18144 else if (unit[0] == 'c' && unit[1] == 'm')
18145 pixels = 2.54;
18146 else
18147 pixels = 0;
18148 if (pixels > 0)
18150 double ppi;
18151 #ifdef HAVE_WINDOW_SYSTEM
18152 if (FRAME_WINDOW_P (it->f)
18153 && (ppi = (width_p
18154 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18155 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18156 ppi > 0))
18157 return OK_PIXELS (ppi / pixels);
18158 #endif
18160 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18161 || (CONSP (Vdisplay_pixels_per_inch)
18162 && (ppi = (width_p
18163 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18164 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18165 ppi > 0)))
18166 return OK_PIXELS (ppi / pixels);
18168 return 0;
18172 #ifdef HAVE_WINDOW_SYSTEM
18173 if (EQ (prop, Qheight))
18174 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18175 if (EQ (prop, Qwidth))
18176 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18177 #else
18178 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18179 return OK_PIXELS (1);
18180 #endif
18182 if (EQ (prop, Qtext))
18183 return OK_PIXELS (width_p
18184 ? window_box_width (it->w, TEXT_AREA)
18185 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18187 if (align_to && *align_to < 0)
18189 *res = 0;
18190 if (EQ (prop, Qleft))
18191 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18192 if (EQ (prop, Qright))
18193 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18194 if (EQ (prop, Qcenter))
18195 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18196 + window_box_width (it->w, TEXT_AREA) / 2);
18197 if (EQ (prop, Qleft_fringe))
18198 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18199 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18200 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18201 if (EQ (prop, Qright_fringe))
18202 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18203 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18204 : window_box_right_offset (it->w, TEXT_AREA));
18205 if (EQ (prop, Qleft_margin))
18206 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18207 if (EQ (prop, Qright_margin))
18208 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18209 if (EQ (prop, Qscroll_bar))
18210 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18212 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18213 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18214 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18215 : 0)));
18217 else
18219 if (EQ (prop, Qleft_fringe))
18220 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18221 if (EQ (prop, Qright_fringe))
18222 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18223 if (EQ (prop, Qleft_margin))
18224 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18225 if (EQ (prop, Qright_margin))
18226 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18227 if (EQ (prop, Qscroll_bar))
18228 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18231 prop = Fbuffer_local_value (prop, it->w->buffer);
18234 if (INTEGERP (prop) || FLOATP (prop))
18236 int base_unit = (width_p
18237 ? FRAME_COLUMN_WIDTH (it->f)
18238 : FRAME_LINE_HEIGHT (it->f));
18239 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18242 if (CONSP (prop))
18244 Lisp_Object car = XCAR (prop);
18245 Lisp_Object cdr = XCDR (prop);
18247 if (SYMBOLP (car))
18249 #ifdef HAVE_WINDOW_SYSTEM
18250 if (valid_image_p (prop))
18252 int id = lookup_image (it->f, prop);
18253 struct image *img = IMAGE_FROM_ID (it->f, id);
18255 return OK_PIXELS (width_p ? img->width : img->height);
18257 #endif
18258 if (EQ (car, Qplus) || EQ (car, Qminus))
18260 int first = 1;
18261 double px;
18263 pixels = 0;
18264 while (CONSP (cdr))
18266 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18267 font, width_p, align_to))
18268 return 0;
18269 if (first)
18270 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18271 else
18272 pixels += px;
18273 cdr = XCDR (cdr);
18275 if (EQ (car, Qminus))
18276 pixels = -pixels;
18277 return OK_PIXELS (pixels);
18280 car = Fbuffer_local_value (car, it->w->buffer);
18283 if (INTEGERP (car) || FLOATP (car))
18285 double fact;
18286 pixels = XFLOATINT (car);
18287 if (NILP (cdr))
18288 return OK_PIXELS (pixels);
18289 if (calc_pixel_width_or_height (&fact, it, cdr,
18290 font, width_p, align_to))
18291 return OK_PIXELS (pixels * fact);
18292 return 0;
18295 return 0;
18298 return 0;
18302 /***********************************************************************
18303 Glyph Display
18304 ***********************************************************************/
18306 #ifdef HAVE_WINDOW_SYSTEM
18308 #if GLYPH_DEBUG
18310 void
18311 dump_glyph_string (s)
18312 struct glyph_string *s;
18314 fprintf (stderr, "glyph string\n");
18315 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18316 s->x, s->y, s->width, s->height);
18317 fprintf (stderr, " ybase = %d\n", s->ybase);
18318 fprintf (stderr, " hl = %d\n", s->hl);
18319 fprintf (stderr, " left overhang = %d, right = %d\n",
18320 s->left_overhang, s->right_overhang);
18321 fprintf (stderr, " nchars = %d\n", s->nchars);
18322 fprintf (stderr, " extends to end of line = %d\n",
18323 s->extends_to_end_of_line_p);
18324 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18325 fprintf (stderr, " bg width = %d\n", s->background_width);
18328 #endif /* GLYPH_DEBUG */
18330 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18331 of XChar2b structures for S; it can't be allocated in
18332 init_glyph_string because it must be allocated via `alloca'. W
18333 is the window on which S is drawn. ROW and AREA are the glyph row
18334 and area within the row from which S is constructed. START is the
18335 index of the first glyph structure covered by S. HL is a
18336 face-override for drawing S. */
18338 #ifdef HAVE_NTGUI
18339 #define OPTIONAL_HDC(hdc) hdc,
18340 #define DECLARE_HDC(hdc) HDC hdc;
18341 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18342 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18343 #endif
18345 #ifndef OPTIONAL_HDC
18346 #define OPTIONAL_HDC(hdc)
18347 #define DECLARE_HDC(hdc)
18348 #define ALLOCATE_HDC(hdc, f)
18349 #define RELEASE_HDC(hdc, f)
18350 #endif
18352 static void
18353 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18354 struct glyph_string *s;
18355 DECLARE_HDC (hdc)
18356 XChar2b *char2b;
18357 struct window *w;
18358 struct glyph_row *row;
18359 enum glyph_row_area area;
18360 int start;
18361 enum draw_glyphs_face hl;
18363 bzero (s, sizeof *s);
18364 s->w = w;
18365 s->f = XFRAME (w->frame);
18366 #ifdef HAVE_NTGUI
18367 s->hdc = hdc;
18368 #endif
18369 s->display = FRAME_X_DISPLAY (s->f);
18370 s->window = FRAME_X_WINDOW (s->f);
18371 s->char2b = char2b;
18372 s->hl = hl;
18373 s->row = row;
18374 s->area = area;
18375 s->first_glyph = row->glyphs[area] + start;
18376 s->height = row->height;
18377 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18379 /* Display the internal border below the tool-bar window. */
18380 if (WINDOWP (s->f->tool_bar_window)
18381 && s->w == XWINDOW (s->f->tool_bar_window))
18382 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18384 s->ybase = s->y + row->ascent;
18388 /* Append the list of glyph strings with head H and tail T to the list
18389 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18391 static INLINE void
18392 append_glyph_string_lists (head, tail, h, t)
18393 struct glyph_string **head, **tail;
18394 struct glyph_string *h, *t;
18396 if (h)
18398 if (*head)
18399 (*tail)->next = h;
18400 else
18401 *head = h;
18402 h->prev = *tail;
18403 *tail = t;
18408 /* Prepend the list of glyph strings with head H and tail T to the
18409 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18410 result. */
18412 static INLINE void
18413 prepend_glyph_string_lists (head, tail, h, t)
18414 struct glyph_string **head, **tail;
18415 struct glyph_string *h, *t;
18417 if (h)
18419 if (*head)
18420 (*head)->prev = t;
18421 else
18422 *tail = t;
18423 t->next = *head;
18424 *head = h;
18429 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18430 Set *HEAD and *TAIL to the resulting list. */
18432 static INLINE void
18433 append_glyph_string (head, tail, s)
18434 struct glyph_string **head, **tail;
18435 struct glyph_string *s;
18437 s->next = s->prev = NULL;
18438 append_glyph_string_lists (head, tail, s, s);
18442 /* Get face and two-byte form of character glyph GLYPH on frame F.
18443 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18444 a pointer to a realized face that is ready for display. */
18446 static INLINE struct face *
18447 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18448 struct frame *f;
18449 struct glyph *glyph;
18450 XChar2b *char2b;
18451 int *two_byte_p;
18453 struct face *face;
18455 xassert (glyph->type == CHAR_GLYPH);
18456 face = FACE_FROM_ID (f, glyph->face_id);
18458 if (two_byte_p)
18459 *two_byte_p = 0;
18461 if (!glyph->multibyte_p)
18463 /* Unibyte case. We don't have to encode, but we have to make
18464 sure to use a face suitable for unibyte. */
18465 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18467 else if (glyph->u.ch < 128
18468 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18470 /* Case of ASCII in a face known to fit ASCII. */
18471 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18473 else
18475 int c1, c2, charset;
18477 /* Split characters into bytes. If c2 is -1 afterwards, C is
18478 really a one-byte character so that byte1 is zero. */
18479 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18480 if (c2 > 0)
18481 STORE_XCHAR2B (char2b, c1, c2);
18482 else
18483 STORE_XCHAR2B (char2b, 0, c1);
18485 /* Maybe encode the character in *CHAR2B. */
18486 if (charset != CHARSET_ASCII)
18488 struct font_info *font_info
18489 = FONT_INFO_FROM_ID (f, face->font_info_id);
18490 if (font_info)
18491 glyph->font_type
18492 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18496 /* Make sure X resources of the face are allocated. */
18497 xassert (face != NULL);
18498 PREPARE_FACE_FOR_DISPLAY (f, face);
18499 return face;
18503 /* Fill glyph string S with composition components specified by S->cmp.
18505 FACES is an array of faces for all components of this composition.
18506 S->gidx is the index of the first component for S.
18508 OVERLAPS non-zero means S should draw the foreground only, and use
18509 its physical height for clipping. See also draw_glyphs.
18511 Value is the index of a component not in S. */
18513 static int
18514 fill_composite_glyph_string (s, faces, overlaps)
18515 struct glyph_string *s;
18516 struct face **faces;
18517 int overlaps;
18519 int i;
18521 xassert (s);
18523 s->for_overlaps = overlaps;
18525 s->face = faces[s->gidx];
18526 s->font = s->face->font;
18527 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18529 /* For all glyphs of this composition, starting at the offset
18530 S->gidx, until we reach the end of the definition or encounter a
18531 glyph that requires the different face, add it to S. */
18532 ++s->nchars;
18533 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18534 ++s->nchars;
18536 /* All glyph strings for the same composition has the same width,
18537 i.e. the width set for the first component of the composition. */
18539 s->width = s->first_glyph->pixel_width;
18541 /* If the specified font could not be loaded, use the frame's
18542 default font, but record the fact that we couldn't load it in
18543 the glyph string so that we can draw rectangles for the
18544 characters of the glyph string. */
18545 if (s->font == NULL)
18547 s->font_not_found_p = 1;
18548 s->font = FRAME_FONT (s->f);
18551 /* Adjust base line for subscript/superscript text. */
18552 s->ybase += s->first_glyph->voffset;
18554 xassert (s->face && s->face->gc);
18556 /* This glyph string must always be drawn with 16-bit functions. */
18557 s->two_byte_p = 1;
18559 return s->gidx + s->nchars;
18563 /* Fill glyph string S from a sequence of character glyphs.
18565 FACE_ID is the face id of the string. START is the index of the
18566 first glyph to consider, END is the index of the last + 1.
18567 OVERLAPS non-zero means S should draw the foreground only, and use
18568 its physical height for clipping. See also draw_glyphs.
18570 Value is the index of the first glyph not in S. */
18572 static int
18573 fill_glyph_string (s, face_id, start, end, overlaps)
18574 struct glyph_string *s;
18575 int face_id;
18576 int start, end, overlaps;
18578 struct glyph *glyph, *last;
18579 int voffset;
18580 int glyph_not_available_p;
18582 xassert (s->f == XFRAME (s->w->frame));
18583 xassert (s->nchars == 0);
18584 xassert (start >= 0 && end > start);
18586 s->for_overlaps = overlaps,
18587 glyph = s->row->glyphs[s->area] + start;
18588 last = s->row->glyphs[s->area] + end;
18589 voffset = glyph->voffset;
18591 glyph_not_available_p = glyph->glyph_not_available_p;
18593 while (glyph < last
18594 && glyph->type == CHAR_GLYPH
18595 && glyph->voffset == voffset
18596 /* Same face id implies same font, nowadays. */
18597 && glyph->face_id == face_id
18598 && glyph->glyph_not_available_p == glyph_not_available_p)
18600 int two_byte_p;
18602 s->face = get_glyph_face_and_encoding (s->f, glyph,
18603 s->char2b + s->nchars,
18604 &two_byte_p);
18605 s->two_byte_p = two_byte_p;
18606 ++s->nchars;
18607 xassert (s->nchars <= end - start);
18608 s->width += glyph->pixel_width;
18609 ++glyph;
18612 s->font = s->face->font;
18613 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18615 /* If the specified font could not be loaded, use the frame's font,
18616 but record the fact that we couldn't load it in
18617 S->font_not_found_p so that we can draw rectangles for the
18618 characters of the glyph string. */
18619 if (s->font == NULL || glyph_not_available_p)
18621 s->font_not_found_p = 1;
18622 s->font = FRAME_FONT (s->f);
18625 /* Adjust base line for subscript/superscript text. */
18626 s->ybase += voffset;
18628 xassert (s->face && s->face->gc);
18629 return glyph - s->row->glyphs[s->area];
18633 /* Fill glyph string S from image glyph S->first_glyph. */
18635 static void
18636 fill_image_glyph_string (s)
18637 struct glyph_string *s;
18639 xassert (s->first_glyph->type == IMAGE_GLYPH);
18640 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18641 xassert (s->img);
18642 s->slice = s->first_glyph->slice;
18643 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18644 s->font = s->face->font;
18645 s->width = s->first_glyph->pixel_width;
18647 /* Adjust base line for subscript/superscript text. */
18648 s->ybase += s->first_glyph->voffset;
18652 /* Fill glyph string S from a sequence of stretch glyphs.
18654 ROW is the glyph row in which the glyphs are found, AREA is the
18655 area within the row. START is the index of the first glyph to
18656 consider, END is the index of the last + 1.
18658 Value is the index of the first glyph not in S. */
18660 static int
18661 fill_stretch_glyph_string (s, row, area, start, end)
18662 struct glyph_string *s;
18663 struct glyph_row *row;
18664 enum glyph_row_area area;
18665 int start, end;
18667 struct glyph *glyph, *last;
18668 int voffset, face_id;
18670 xassert (s->first_glyph->type == STRETCH_GLYPH);
18672 glyph = s->row->glyphs[s->area] + start;
18673 last = s->row->glyphs[s->area] + end;
18674 face_id = glyph->face_id;
18675 s->face = FACE_FROM_ID (s->f, face_id);
18676 s->font = s->face->font;
18677 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18678 s->width = glyph->pixel_width;
18679 voffset = glyph->voffset;
18681 for (++glyph;
18682 (glyph < last
18683 && glyph->type == STRETCH_GLYPH
18684 && glyph->voffset == voffset
18685 && glyph->face_id == face_id);
18686 ++glyph)
18687 s->width += glyph->pixel_width;
18689 /* Adjust base line for subscript/superscript text. */
18690 s->ybase += voffset;
18692 /* The case that face->gc == 0 is handled when drawing the glyph
18693 string by calling PREPARE_FACE_FOR_DISPLAY. */
18694 xassert (s->face);
18695 return glyph - s->row->glyphs[s->area];
18699 /* EXPORT for RIF:
18700 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18701 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18702 assumed to be zero. */
18704 void
18705 x_get_glyph_overhangs (glyph, f, left, right)
18706 struct glyph *glyph;
18707 struct frame *f;
18708 int *left, *right;
18710 *left = *right = 0;
18712 if (glyph->type == CHAR_GLYPH)
18714 XFontStruct *font;
18715 struct face *face;
18716 struct font_info *font_info;
18717 XChar2b char2b;
18718 XCharStruct *pcm;
18720 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18721 font = face->font;
18722 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18723 if (font /* ++KFS: Should this be font_info ? */
18724 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18726 if (pcm->rbearing > pcm->width)
18727 *right = pcm->rbearing - pcm->width;
18728 if (pcm->lbearing < 0)
18729 *left = -pcm->lbearing;
18735 /* Return the index of the first glyph preceding glyph string S that
18736 is overwritten by S because of S's left overhang. Value is -1
18737 if no glyphs are overwritten. */
18739 static int
18740 left_overwritten (s)
18741 struct glyph_string *s;
18743 int k;
18745 if (s->left_overhang)
18747 int x = 0, i;
18748 struct glyph *glyphs = s->row->glyphs[s->area];
18749 int first = s->first_glyph - glyphs;
18751 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18752 x -= glyphs[i].pixel_width;
18754 k = i + 1;
18756 else
18757 k = -1;
18759 return k;
18763 /* Return the index of the first glyph preceding glyph string S that
18764 is overwriting S because of its right overhang. Value is -1 if no
18765 glyph in front of S overwrites S. */
18767 static int
18768 left_overwriting (s)
18769 struct glyph_string *s;
18771 int i, k, x;
18772 struct glyph *glyphs = s->row->glyphs[s->area];
18773 int first = s->first_glyph - glyphs;
18775 k = -1;
18776 x = 0;
18777 for (i = first - 1; i >= 0; --i)
18779 int left, right;
18780 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18781 if (x + right > 0)
18782 k = i;
18783 x -= glyphs[i].pixel_width;
18786 return k;
18790 /* Return the index of the last glyph following glyph string S that is
18791 not overwritten by S because of S's right overhang. Value is -1 if
18792 no such glyph is found. */
18794 static int
18795 right_overwritten (s)
18796 struct glyph_string *s;
18798 int k = -1;
18800 if (s->right_overhang)
18802 int x = 0, i;
18803 struct glyph *glyphs = s->row->glyphs[s->area];
18804 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18805 int end = s->row->used[s->area];
18807 for (i = first; i < end && s->right_overhang > x; ++i)
18808 x += glyphs[i].pixel_width;
18810 k = i;
18813 return k;
18817 /* Return the index of the last glyph following glyph string S that
18818 overwrites S because of its left overhang. Value is negative
18819 if no such glyph is found. */
18821 static int
18822 right_overwriting (s)
18823 struct glyph_string *s;
18825 int i, k, x;
18826 int end = s->row->used[s->area];
18827 struct glyph *glyphs = s->row->glyphs[s->area];
18828 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18830 k = -1;
18831 x = 0;
18832 for (i = first; i < end; ++i)
18834 int left, right;
18835 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18836 if (x - left < 0)
18837 k = i;
18838 x += glyphs[i].pixel_width;
18841 return k;
18845 /* Get face and two-byte form of character C in face FACE_ID on frame
18846 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18847 means we want to display multibyte text. DISPLAY_P non-zero means
18848 make sure that X resources for the face returned are allocated.
18849 Value is a pointer to a realized face that is ready for display if
18850 DISPLAY_P is non-zero. */
18852 static INLINE struct face *
18853 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18854 struct frame *f;
18855 int c, face_id;
18856 XChar2b *char2b;
18857 int multibyte_p, display_p;
18859 struct face *face = FACE_FROM_ID (f, face_id);
18861 if (!multibyte_p)
18863 /* Unibyte case. We don't have to encode, but we have to make
18864 sure to use a face suitable for unibyte. */
18865 STORE_XCHAR2B (char2b, 0, c);
18866 face_id = FACE_FOR_CHAR (f, face, c);
18867 face = FACE_FROM_ID (f, face_id);
18869 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18871 /* Case of ASCII in a face known to fit ASCII. */
18872 STORE_XCHAR2B (char2b, 0, c);
18874 else
18876 int c1, c2, charset;
18878 /* Split characters into bytes. If c2 is -1 afterwards, C is
18879 really a one-byte character so that byte1 is zero. */
18880 SPLIT_CHAR (c, charset, c1, c2);
18881 if (c2 > 0)
18882 STORE_XCHAR2B (char2b, c1, c2);
18883 else
18884 STORE_XCHAR2B (char2b, 0, c1);
18886 /* Maybe encode the character in *CHAR2B. */
18887 if (face->font != NULL)
18889 struct font_info *font_info
18890 = FONT_INFO_FROM_ID (f, face->font_info_id);
18891 if (font_info)
18892 rif->encode_char (c, char2b, font_info, 0);
18896 /* Make sure X resources of the face are allocated. */
18897 #ifdef HAVE_X_WINDOWS
18898 if (display_p)
18899 #endif
18901 xassert (face != NULL);
18902 PREPARE_FACE_FOR_DISPLAY (f, face);
18905 return face;
18909 /* Set background width of glyph string S. START is the index of the
18910 first glyph following S. LAST_X is the right-most x-position + 1
18911 in the drawing area. */
18913 static INLINE void
18914 set_glyph_string_background_width (s, start, last_x)
18915 struct glyph_string *s;
18916 int start;
18917 int last_x;
18919 /* If the face of this glyph string has to be drawn to the end of
18920 the drawing area, set S->extends_to_end_of_line_p. */
18922 if (start == s->row->used[s->area]
18923 && s->area == TEXT_AREA
18924 && ((s->row->fill_line_p
18925 && (s->hl == DRAW_NORMAL_TEXT
18926 || s->hl == DRAW_IMAGE_RAISED
18927 || s->hl == DRAW_IMAGE_SUNKEN))
18928 || s->hl == DRAW_MOUSE_FACE))
18929 s->extends_to_end_of_line_p = 1;
18931 /* If S extends its face to the end of the line, set its
18932 background_width to the distance to the right edge of the drawing
18933 area. */
18934 if (s->extends_to_end_of_line_p)
18935 s->background_width = last_x - s->x + 1;
18936 else
18937 s->background_width = s->width;
18941 /* Compute overhangs and x-positions for glyph string S and its
18942 predecessors, or successors. X is the starting x-position for S.
18943 BACKWARD_P non-zero means process predecessors. */
18945 static void
18946 compute_overhangs_and_x (s, x, backward_p)
18947 struct glyph_string *s;
18948 int x;
18949 int backward_p;
18951 if (backward_p)
18953 while (s)
18955 if (rif->compute_glyph_string_overhangs)
18956 rif->compute_glyph_string_overhangs (s);
18957 x -= s->width;
18958 s->x = x;
18959 s = s->prev;
18962 else
18964 while (s)
18966 if (rif->compute_glyph_string_overhangs)
18967 rif->compute_glyph_string_overhangs (s);
18968 s->x = x;
18969 x += s->width;
18970 s = s->next;
18977 /* The following macros are only called from draw_glyphs below.
18978 They reference the following parameters of that function directly:
18979 `w', `row', `area', and `overlap_p'
18980 as well as the following local variables:
18981 `s', `f', and `hdc' (in W32) */
18983 #ifdef HAVE_NTGUI
18984 /* On W32, silently add local `hdc' variable to argument list of
18985 init_glyph_string. */
18986 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18987 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18988 #else
18989 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18990 init_glyph_string (s, char2b, w, row, area, start, hl)
18991 #endif
18993 /* Add a glyph string for a stretch glyph to the list of strings
18994 between HEAD and TAIL. START is the index of the stretch glyph in
18995 row area AREA of glyph row ROW. END is the index of the last glyph
18996 in that glyph row area. X is the current output position assigned
18997 to the new glyph string constructed. HL overrides that face of the
18998 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18999 is the right-most x-position of the drawing area. */
19001 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19002 and below -- keep them on one line. */
19003 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19004 do \
19006 s = (struct glyph_string *) alloca (sizeof *s); \
19007 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19008 START = fill_stretch_glyph_string (s, row, area, START, END); \
19009 append_glyph_string (&HEAD, &TAIL, s); \
19010 s->x = (X); \
19012 while (0)
19015 /* Add a glyph string for an image glyph to the list of strings
19016 between HEAD and TAIL. START is the index of the image glyph in
19017 row area AREA of glyph row ROW. END is the index of the last glyph
19018 in that glyph row area. X is the current output position assigned
19019 to the new glyph string constructed. HL overrides that face of the
19020 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19021 is the right-most x-position of the drawing area. */
19023 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19024 do \
19026 s = (struct glyph_string *) alloca (sizeof *s); \
19027 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19028 fill_image_glyph_string (s); \
19029 append_glyph_string (&HEAD, &TAIL, s); \
19030 ++START; \
19031 s->x = (X); \
19033 while (0)
19036 /* Add a glyph string for a sequence of character glyphs to the list
19037 of strings between HEAD and TAIL. START is the index of the first
19038 glyph in row area AREA of glyph row ROW that is part of the new
19039 glyph string. END is the index of the last glyph in that glyph row
19040 area. X is the current output position assigned to the new glyph
19041 string constructed. HL overrides that face of the glyph; e.g. it
19042 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19043 right-most x-position of the drawing area. */
19045 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19046 do \
19048 int c, face_id; \
19049 XChar2b *char2b; \
19051 c = (row)->glyphs[area][START].u.ch; \
19052 face_id = (row)->glyphs[area][START].face_id; \
19054 s = (struct glyph_string *) alloca (sizeof *s); \
19055 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19056 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19057 append_glyph_string (&HEAD, &TAIL, s); \
19058 s->x = (X); \
19059 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19061 while (0)
19064 /* Add a glyph string for a composite sequence to the list of strings
19065 between HEAD and TAIL. START is the index of the first glyph in
19066 row area AREA of glyph row ROW that is part of the new glyph
19067 string. END is the index of the last glyph in that glyph row area.
19068 X is the current output position assigned to the new glyph string
19069 constructed. HL overrides that face of the glyph; e.g. it is
19070 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19071 x-position of the drawing area. */
19073 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19074 do { \
19075 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19076 int face_id = (row)->glyphs[area][START].face_id; \
19077 struct face *base_face = FACE_FROM_ID (f, face_id); \
19078 struct composition *cmp = composition_table[cmp_id]; \
19079 int glyph_len = cmp->glyph_len; \
19080 XChar2b *char2b; \
19081 struct face **faces; \
19082 struct glyph_string *first_s = NULL; \
19083 int n; \
19085 base_face = base_face->ascii_face; \
19086 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19087 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19088 /* At first, fill in `char2b' and `faces'. */ \
19089 for (n = 0; n < glyph_len; n++) \
19091 int c = COMPOSITION_GLYPH (cmp, n); \
19092 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19093 faces[n] = FACE_FROM_ID (f, this_face_id); \
19094 get_char_face_and_encoding (f, c, this_face_id, \
19095 char2b + n, 1, 1); \
19098 /* Make glyph_strings for each glyph sequence that is drawable by \
19099 the same face, and append them to HEAD/TAIL. */ \
19100 for (n = 0; n < cmp->glyph_len;) \
19102 s = (struct glyph_string *) alloca (sizeof *s); \
19103 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19104 append_glyph_string (&(HEAD), &(TAIL), s); \
19105 s->cmp = cmp; \
19106 s->gidx = n; \
19107 s->x = (X); \
19109 if (n == 0) \
19110 first_s = s; \
19112 n = fill_composite_glyph_string (s, faces, overlaps); \
19115 ++START; \
19116 s = first_s; \
19117 } while (0)
19120 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19121 of AREA of glyph row ROW on window W between indices START and END.
19122 HL overrides the face for drawing glyph strings, e.g. it is
19123 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19124 x-positions of the drawing area.
19126 This is an ugly monster macro construct because we must use alloca
19127 to allocate glyph strings (because draw_glyphs can be called
19128 asynchronously). */
19130 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19131 do \
19133 HEAD = TAIL = NULL; \
19134 while (START < END) \
19136 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19137 switch (first_glyph->type) \
19139 case CHAR_GLYPH: \
19140 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19141 HL, X, LAST_X); \
19142 break; \
19144 case COMPOSITE_GLYPH: \
19145 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19146 HL, X, LAST_X); \
19147 break; \
19149 case STRETCH_GLYPH: \
19150 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19151 HL, X, LAST_X); \
19152 break; \
19154 case IMAGE_GLYPH: \
19155 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19156 HL, X, LAST_X); \
19157 break; \
19159 default: \
19160 abort (); \
19163 set_glyph_string_background_width (s, START, LAST_X); \
19164 (X) += s->width; \
19167 while (0)
19170 /* Draw glyphs between START and END in AREA of ROW on window W,
19171 starting at x-position X. X is relative to AREA in W. HL is a
19172 face-override with the following meaning:
19174 DRAW_NORMAL_TEXT draw normally
19175 DRAW_CURSOR draw in cursor face
19176 DRAW_MOUSE_FACE draw in mouse face.
19177 DRAW_INVERSE_VIDEO draw in mode line face
19178 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19179 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19181 If OVERLAPS is non-zero, draw only the foreground of characters and
19182 clip to the physical height of ROW. Non-zero value also defines
19183 the overlapping part to be drawn:
19185 OVERLAPS_PRED overlap with preceding rows
19186 OVERLAPS_SUCC overlap with succeeding rows
19187 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19188 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19190 Value is the x-position reached, relative to AREA of W. */
19192 static int
19193 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19194 struct window *w;
19195 int x;
19196 struct glyph_row *row;
19197 enum glyph_row_area area;
19198 int start, end;
19199 enum draw_glyphs_face hl;
19200 int overlaps;
19202 struct glyph_string *head, *tail;
19203 struct glyph_string *s;
19204 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19205 int last_x, area_width;
19206 int x_reached;
19207 int i, j;
19208 struct frame *f = XFRAME (WINDOW_FRAME (w));
19209 DECLARE_HDC (hdc);
19211 ALLOCATE_HDC (hdc, f);
19213 /* Let's rather be paranoid than getting a SEGV. */
19214 end = min (end, row->used[area]);
19215 start = max (0, start);
19216 start = min (end, start);
19218 /* Translate X to frame coordinates. Set last_x to the right
19219 end of the drawing area. */
19220 if (row->full_width_p)
19222 /* X is relative to the left edge of W, without scroll bars
19223 or fringes. */
19224 x += WINDOW_LEFT_EDGE_X (w);
19225 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19227 else
19229 int area_left = window_box_left (w, area);
19230 x += area_left;
19231 area_width = window_box_width (w, area);
19232 last_x = area_left + area_width;
19235 /* Build a doubly-linked list of glyph_string structures between
19236 head and tail from what we have to draw. Note that the macro
19237 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19238 the reason we use a separate variable `i'. */
19239 i = start;
19240 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19241 if (tail)
19242 x_reached = tail->x + tail->background_width;
19243 else
19244 x_reached = x;
19246 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19247 the row, redraw some glyphs in front or following the glyph
19248 strings built above. */
19249 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19251 int dummy_x = 0;
19252 struct glyph_string *h, *t;
19254 /* Compute overhangs for all glyph strings. */
19255 if (rif->compute_glyph_string_overhangs)
19256 for (s = head; s; s = s->next)
19257 rif->compute_glyph_string_overhangs (s);
19259 /* Prepend glyph strings for glyphs in front of the first glyph
19260 string that are overwritten because of the first glyph
19261 string's left overhang. The background of all strings
19262 prepended must be drawn because the first glyph string
19263 draws over it. */
19264 i = left_overwritten (head);
19265 if (i >= 0)
19267 j = i;
19268 BUILD_GLYPH_STRINGS (j, start, h, t,
19269 DRAW_NORMAL_TEXT, dummy_x, last_x);
19270 start = i;
19271 compute_overhangs_and_x (t, head->x, 1);
19272 prepend_glyph_string_lists (&head, &tail, h, t);
19273 clip_head = head;
19276 /* Prepend glyph strings for glyphs in front of the first glyph
19277 string that overwrite that glyph string because of their
19278 right overhang. For these strings, only the foreground must
19279 be drawn, because it draws over the glyph string at `head'.
19280 The background must not be drawn because this would overwrite
19281 right overhangs of preceding glyphs for which no glyph
19282 strings exist. */
19283 i = left_overwriting (head);
19284 if (i >= 0)
19286 clip_head = head;
19287 BUILD_GLYPH_STRINGS (i, start, h, t,
19288 DRAW_NORMAL_TEXT, dummy_x, last_x);
19289 for (s = h; s; s = s->next)
19290 s->background_filled_p = 1;
19291 compute_overhangs_and_x (t, head->x, 1);
19292 prepend_glyph_string_lists (&head, &tail, h, t);
19295 /* Append glyphs strings for glyphs following the last glyph
19296 string tail that are overwritten by tail. The background of
19297 these strings has to be drawn because tail's foreground draws
19298 over it. */
19299 i = right_overwritten (tail);
19300 if (i >= 0)
19302 BUILD_GLYPH_STRINGS (end, i, h, t,
19303 DRAW_NORMAL_TEXT, x, last_x);
19304 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19305 append_glyph_string_lists (&head, &tail, h, t);
19306 clip_tail = tail;
19309 /* Append glyph strings for glyphs following the last glyph
19310 string tail that overwrite tail. The foreground of such
19311 glyphs has to be drawn because it writes into the background
19312 of tail. The background must not be drawn because it could
19313 paint over the foreground of following glyphs. */
19314 i = right_overwriting (tail);
19315 if (i >= 0)
19317 clip_tail = tail;
19318 BUILD_GLYPH_STRINGS (end, i, h, t,
19319 DRAW_NORMAL_TEXT, x, last_x);
19320 for (s = h; s; s = s->next)
19321 s->background_filled_p = 1;
19322 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19323 append_glyph_string_lists (&head, &tail, h, t);
19325 if (clip_head || clip_tail)
19326 for (s = head; s; s = s->next)
19328 s->clip_head = clip_head;
19329 s->clip_tail = clip_tail;
19333 /* Draw all strings. */
19334 for (s = head; s; s = s->next)
19335 rif->draw_glyph_string (s);
19337 if (area == TEXT_AREA
19338 && !row->full_width_p
19339 /* When drawing overlapping rows, only the glyph strings'
19340 foreground is drawn, which doesn't erase a cursor
19341 completely. */
19342 && !overlaps)
19344 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19345 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19346 : (tail ? tail->x + tail->background_width : x));
19348 int text_left = window_box_left (w, TEXT_AREA);
19349 x0 -= text_left;
19350 x1 -= text_left;
19352 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19353 row->y, MATRIX_ROW_BOTTOM_Y (row));
19356 /* Value is the x-position up to which drawn, relative to AREA of W.
19357 This doesn't include parts drawn because of overhangs. */
19358 if (row->full_width_p)
19359 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19360 else
19361 x_reached -= window_box_left (w, area);
19363 RELEASE_HDC (hdc, f);
19365 return x_reached;
19368 /* Expand row matrix if too narrow. Don't expand if area
19369 is not present. */
19371 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19373 if (!fonts_changed_p \
19374 && (it->glyph_row->glyphs[area] \
19375 < it->glyph_row->glyphs[area + 1])) \
19377 it->w->ncols_scale_factor++; \
19378 fonts_changed_p = 1; \
19382 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19383 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19385 static INLINE void
19386 append_glyph (it)
19387 struct it *it;
19389 struct glyph *glyph;
19390 enum glyph_row_area area = it->area;
19392 xassert (it->glyph_row);
19393 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19395 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19396 if (glyph < it->glyph_row->glyphs[area + 1])
19398 glyph->charpos = CHARPOS (it->position);
19399 glyph->object = it->object;
19400 glyph->pixel_width = it->pixel_width;
19401 glyph->ascent = it->ascent;
19402 glyph->descent = it->descent;
19403 glyph->voffset = it->voffset;
19404 glyph->type = CHAR_GLYPH;
19405 glyph->multibyte_p = it->multibyte_p;
19406 glyph->left_box_line_p = it->start_of_box_run_p;
19407 glyph->right_box_line_p = it->end_of_box_run_p;
19408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19409 || it->phys_descent > it->descent);
19410 glyph->padding_p = 0;
19411 glyph->glyph_not_available_p = it->glyph_not_available_p;
19412 glyph->face_id = it->face_id;
19413 glyph->u.ch = it->char_to_display;
19414 glyph->slice = null_glyph_slice;
19415 glyph->font_type = FONT_TYPE_UNKNOWN;
19416 ++it->glyph_row->used[area];
19418 else
19419 IT_EXPAND_MATRIX_WIDTH (it, area);
19422 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19423 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19425 static INLINE void
19426 append_composite_glyph (it)
19427 struct it *it;
19429 struct glyph *glyph;
19430 enum glyph_row_area area = it->area;
19432 xassert (it->glyph_row);
19434 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19435 if (glyph < it->glyph_row->glyphs[area + 1])
19437 glyph->charpos = CHARPOS (it->position);
19438 glyph->object = it->object;
19439 glyph->pixel_width = it->pixel_width;
19440 glyph->ascent = it->ascent;
19441 glyph->descent = it->descent;
19442 glyph->voffset = it->voffset;
19443 glyph->type = COMPOSITE_GLYPH;
19444 glyph->multibyte_p = it->multibyte_p;
19445 glyph->left_box_line_p = it->start_of_box_run_p;
19446 glyph->right_box_line_p = it->end_of_box_run_p;
19447 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19448 || it->phys_descent > it->descent);
19449 glyph->padding_p = 0;
19450 glyph->glyph_not_available_p = 0;
19451 glyph->face_id = it->face_id;
19452 glyph->u.cmp_id = it->cmp_id;
19453 glyph->slice = null_glyph_slice;
19454 glyph->font_type = FONT_TYPE_UNKNOWN;
19455 ++it->glyph_row->used[area];
19457 else
19458 IT_EXPAND_MATRIX_WIDTH (it, area);
19462 /* Change IT->ascent and IT->height according to the setting of
19463 IT->voffset. */
19465 static INLINE void
19466 take_vertical_position_into_account (it)
19467 struct it *it;
19469 if (it->voffset)
19471 if (it->voffset < 0)
19472 /* Increase the ascent so that we can display the text higher
19473 in the line. */
19474 it->ascent -= it->voffset;
19475 else
19476 /* Increase the descent so that we can display the text lower
19477 in the line. */
19478 it->descent += it->voffset;
19483 /* Produce glyphs/get display metrics for the image IT is loaded with.
19484 See the description of struct display_iterator in dispextern.h for
19485 an overview of struct display_iterator. */
19487 static void
19488 produce_image_glyph (it)
19489 struct it *it;
19491 struct image *img;
19492 struct face *face;
19493 int glyph_ascent;
19494 struct glyph_slice slice;
19496 xassert (it->what == IT_IMAGE);
19498 face = FACE_FROM_ID (it->f, it->face_id);
19499 xassert (face);
19500 /* Make sure X resources of the face is loaded. */
19501 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19503 if (it->image_id < 0)
19505 /* Fringe bitmap. */
19506 it->ascent = it->phys_ascent = 0;
19507 it->descent = it->phys_descent = 0;
19508 it->pixel_width = 0;
19509 it->nglyphs = 0;
19510 return;
19513 img = IMAGE_FROM_ID (it->f, it->image_id);
19514 xassert (img);
19515 /* Make sure X resources of the image is loaded. */
19516 prepare_image_for_display (it->f, img);
19518 slice.x = slice.y = 0;
19519 slice.width = img->width;
19520 slice.height = img->height;
19522 if (INTEGERP (it->slice.x))
19523 slice.x = XINT (it->slice.x);
19524 else if (FLOATP (it->slice.x))
19525 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19527 if (INTEGERP (it->slice.y))
19528 slice.y = XINT (it->slice.y);
19529 else if (FLOATP (it->slice.y))
19530 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19532 if (INTEGERP (it->slice.width))
19533 slice.width = XINT (it->slice.width);
19534 else if (FLOATP (it->slice.width))
19535 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19537 if (INTEGERP (it->slice.height))
19538 slice.height = XINT (it->slice.height);
19539 else if (FLOATP (it->slice.height))
19540 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19542 if (slice.x >= img->width)
19543 slice.x = img->width;
19544 if (slice.y >= img->height)
19545 slice.y = img->height;
19546 if (slice.x + slice.width >= img->width)
19547 slice.width = img->width - slice.x;
19548 if (slice.y + slice.height > img->height)
19549 slice.height = img->height - slice.y;
19551 if (slice.width == 0 || slice.height == 0)
19552 return;
19554 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19556 it->descent = slice.height - glyph_ascent;
19557 if (slice.y == 0)
19558 it->descent += img->vmargin;
19559 if (slice.y + slice.height == img->height)
19560 it->descent += img->vmargin;
19561 it->phys_descent = it->descent;
19563 it->pixel_width = slice.width;
19564 if (slice.x == 0)
19565 it->pixel_width += img->hmargin;
19566 if (slice.x + slice.width == img->width)
19567 it->pixel_width += img->hmargin;
19569 /* It's quite possible for images to have an ascent greater than
19570 their height, so don't get confused in that case. */
19571 if (it->descent < 0)
19572 it->descent = 0;
19574 #if 0 /* this breaks image tiling */
19575 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19576 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19577 if (face_ascent > it->ascent)
19578 it->ascent = it->phys_ascent = face_ascent;
19579 #endif
19581 it->nglyphs = 1;
19583 if (face->box != FACE_NO_BOX)
19585 if (face->box_line_width > 0)
19587 if (slice.y == 0)
19588 it->ascent += face->box_line_width;
19589 if (slice.y + slice.height == img->height)
19590 it->descent += face->box_line_width;
19593 if (it->start_of_box_run_p && slice.x == 0)
19594 it->pixel_width += abs (face->box_line_width);
19595 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19596 it->pixel_width += abs (face->box_line_width);
19599 take_vertical_position_into_account (it);
19601 if (it->glyph_row)
19603 struct glyph *glyph;
19604 enum glyph_row_area area = it->area;
19606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19607 if (glyph < it->glyph_row->glyphs[area + 1])
19609 glyph->charpos = CHARPOS (it->position);
19610 glyph->object = it->object;
19611 glyph->pixel_width = it->pixel_width;
19612 glyph->ascent = glyph_ascent;
19613 glyph->descent = it->descent;
19614 glyph->voffset = it->voffset;
19615 glyph->type = IMAGE_GLYPH;
19616 glyph->multibyte_p = it->multibyte_p;
19617 glyph->left_box_line_p = it->start_of_box_run_p;
19618 glyph->right_box_line_p = it->end_of_box_run_p;
19619 glyph->overlaps_vertically_p = 0;
19620 glyph->padding_p = 0;
19621 glyph->glyph_not_available_p = 0;
19622 glyph->face_id = it->face_id;
19623 glyph->u.img_id = img->id;
19624 glyph->slice = slice;
19625 glyph->font_type = FONT_TYPE_UNKNOWN;
19626 ++it->glyph_row->used[area];
19628 else
19629 IT_EXPAND_MATRIX_WIDTH (it, area);
19634 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19635 of the glyph, WIDTH and HEIGHT are the width and height of the
19636 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19638 static void
19639 append_stretch_glyph (it, object, width, height, ascent)
19640 struct it *it;
19641 Lisp_Object object;
19642 int width, height;
19643 int ascent;
19645 struct glyph *glyph;
19646 enum glyph_row_area area = it->area;
19648 xassert (ascent >= 0 && ascent <= height);
19650 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19651 if (glyph < it->glyph_row->glyphs[area + 1])
19653 glyph->charpos = CHARPOS (it->position);
19654 glyph->object = object;
19655 glyph->pixel_width = width;
19656 glyph->ascent = ascent;
19657 glyph->descent = height - ascent;
19658 glyph->voffset = it->voffset;
19659 glyph->type = STRETCH_GLYPH;
19660 glyph->multibyte_p = it->multibyte_p;
19661 glyph->left_box_line_p = it->start_of_box_run_p;
19662 glyph->right_box_line_p = it->end_of_box_run_p;
19663 glyph->overlaps_vertically_p = 0;
19664 glyph->padding_p = 0;
19665 glyph->glyph_not_available_p = 0;
19666 glyph->face_id = it->face_id;
19667 glyph->u.stretch.ascent = ascent;
19668 glyph->u.stretch.height = height;
19669 glyph->slice = null_glyph_slice;
19670 glyph->font_type = FONT_TYPE_UNKNOWN;
19671 ++it->glyph_row->used[area];
19673 else
19674 IT_EXPAND_MATRIX_WIDTH (it, area);
19678 /* Produce a stretch glyph for iterator IT. IT->object is the value
19679 of the glyph property displayed. The value must be a list
19680 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19681 being recognized:
19683 1. `:width WIDTH' specifies that the space should be WIDTH *
19684 canonical char width wide. WIDTH may be an integer or floating
19685 point number.
19687 2. `:relative-width FACTOR' specifies that the width of the stretch
19688 should be computed from the width of the first character having the
19689 `glyph' property, and should be FACTOR times that width.
19691 3. `:align-to HPOS' specifies that the space should be wide enough
19692 to reach HPOS, a value in canonical character units.
19694 Exactly one of the above pairs must be present.
19696 4. `:height HEIGHT' specifies that the height of the stretch produced
19697 should be HEIGHT, measured in canonical character units.
19699 5. `:relative-height FACTOR' specifies that the height of the
19700 stretch should be FACTOR times the height of the characters having
19701 the glyph property.
19703 Either none or exactly one of 4 or 5 must be present.
19705 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19706 of the stretch should be used for the ascent of the stretch.
19707 ASCENT must be in the range 0 <= ASCENT <= 100. */
19709 static void
19710 produce_stretch_glyph (it)
19711 struct it *it;
19713 /* (space :width WIDTH :height HEIGHT ...) */
19714 Lisp_Object prop, plist;
19715 int width = 0, height = 0, align_to = -1;
19716 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19717 int ascent = 0;
19718 double tem;
19719 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19720 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19722 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19724 /* List should start with `space'. */
19725 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19726 plist = XCDR (it->object);
19728 /* Compute the width of the stretch. */
19729 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19730 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19732 /* Absolute width `:width WIDTH' specified and valid. */
19733 zero_width_ok_p = 1;
19734 width = (int)tem;
19736 else if (prop = Fplist_get (plist, QCrelative_width),
19737 NUMVAL (prop) > 0)
19739 /* Relative width `:relative-width FACTOR' specified and valid.
19740 Compute the width of the characters having the `glyph'
19741 property. */
19742 struct it it2;
19743 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19745 it2 = *it;
19746 if (it->multibyte_p)
19748 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19749 - IT_BYTEPOS (*it));
19750 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19752 else
19753 it2.c = *p, it2.len = 1;
19755 it2.glyph_row = NULL;
19756 it2.what = IT_CHARACTER;
19757 x_produce_glyphs (&it2);
19758 width = NUMVAL (prop) * it2.pixel_width;
19760 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19761 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19763 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19764 align_to = (align_to < 0
19766 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19767 else if (align_to < 0)
19768 align_to = window_box_left_offset (it->w, TEXT_AREA);
19769 width = max (0, (int)tem + align_to - it->current_x);
19770 zero_width_ok_p = 1;
19772 else
19773 /* Nothing specified -> width defaults to canonical char width. */
19774 width = FRAME_COLUMN_WIDTH (it->f);
19776 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19777 width = 1;
19779 /* Compute height. */
19780 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19781 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19783 height = (int)tem;
19784 zero_height_ok_p = 1;
19786 else if (prop = Fplist_get (plist, QCrelative_height),
19787 NUMVAL (prop) > 0)
19788 height = FONT_HEIGHT (font) * NUMVAL (prop);
19789 else
19790 height = FONT_HEIGHT (font);
19792 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19793 height = 1;
19795 /* Compute percentage of height used for ascent. If
19796 `:ascent ASCENT' is present and valid, use that. Otherwise,
19797 derive the ascent from the font in use. */
19798 if (prop = Fplist_get (plist, QCascent),
19799 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19800 ascent = height * NUMVAL (prop) / 100.0;
19801 else if (!NILP (prop)
19802 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19803 ascent = min (max (0, (int)tem), height);
19804 else
19805 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19807 if (width > 0 && !it->truncate_lines_p
19808 && it->current_x + width > it->last_visible_x)
19809 width = it->last_visible_x - it->current_x - 1;
19811 if (width > 0 && height > 0 && it->glyph_row)
19813 Lisp_Object object = it->stack[it->sp - 1].string;
19814 if (!STRINGP (object))
19815 object = it->w->buffer;
19816 append_stretch_glyph (it, object, width, height, ascent);
19819 it->pixel_width = width;
19820 it->ascent = it->phys_ascent = ascent;
19821 it->descent = it->phys_descent = height - it->ascent;
19822 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19824 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19826 if (face->box_line_width > 0)
19828 it->ascent += face->box_line_width;
19829 it->descent += face->box_line_width;
19832 if (it->start_of_box_run_p)
19833 it->pixel_width += abs (face->box_line_width);
19834 if (it->end_of_box_run_p)
19835 it->pixel_width += abs (face->box_line_width);
19838 take_vertical_position_into_account (it);
19841 /* Get line-height and line-spacing property at point.
19842 If line-height has format (HEIGHT TOTAL), return TOTAL
19843 in TOTAL_HEIGHT. */
19845 static Lisp_Object
19846 get_line_height_property (it, prop)
19847 struct it *it;
19848 Lisp_Object prop;
19850 Lisp_Object position;
19852 if (STRINGP (it->object))
19853 position = make_number (IT_STRING_CHARPOS (*it));
19854 else if (BUFFERP (it->object))
19855 position = make_number (IT_CHARPOS (*it));
19856 else
19857 return Qnil;
19859 return Fget_char_property (position, prop, it->object);
19862 /* Calculate line-height and line-spacing properties.
19863 An integer value specifies explicit pixel value.
19864 A float value specifies relative value to current face height.
19865 A cons (float . face-name) specifies relative value to
19866 height of specified face font.
19868 Returns height in pixels, or nil. */
19871 static Lisp_Object
19872 calc_line_height_property (it, val, font, boff, override)
19873 struct it *it;
19874 Lisp_Object val;
19875 XFontStruct *font;
19876 int boff, override;
19878 Lisp_Object face_name = Qnil;
19879 int ascent, descent, height;
19881 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19882 return val;
19884 if (CONSP (val))
19886 face_name = XCAR (val);
19887 val = XCDR (val);
19888 if (!NUMBERP (val))
19889 val = make_number (1);
19890 if (NILP (face_name))
19892 height = it->ascent + it->descent;
19893 goto scale;
19897 if (NILP (face_name))
19899 font = FRAME_FONT (it->f);
19900 boff = FRAME_BASELINE_OFFSET (it->f);
19902 else if (EQ (face_name, Qt))
19904 override = 0;
19906 else
19908 int face_id;
19909 struct face *face;
19910 struct font_info *font_info;
19912 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19913 if (face_id < 0)
19914 return make_number (-1);
19916 face = FACE_FROM_ID (it->f, face_id);
19917 font = face->font;
19918 if (font == NULL)
19919 return make_number (-1);
19921 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19922 boff = font_info->baseline_offset;
19923 if (font_info->vertical_centering)
19924 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19927 ascent = FONT_BASE (font) + boff;
19928 descent = FONT_DESCENT (font) - boff;
19930 if (override)
19932 it->override_ascent = ascent;
19933 it->override_descent = descent;
19934 it->override_boff = boff;
19937 height = ascent + descent;
19939 scale:
19940 if (FLOATP (val))
19941 height = (int)(XFLOAT_DATA (val) * height);
19942 else if (INTEGERP (val))
19943 height *= XINT (val);
19945 return make_number (height);
19949 /* RIF:
19950 Produce glyphs/get display metrics for the display element IT is
19951 loaded with. See the description of struct it in dispextern.h
19952 for an overview of struct it. */
19954 void
19955 x_produce_glyphs (it)
19956 struct it *it;
19958 int extra_line_spacing = it->extra_line_spacing;
19960 it->glyph_not_available_p = 0;
19962 if (it->what == IT_CHARACTER)
19964 XChar2b char2b;
19965 XFontStruct *font;
19966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19967 XCharStruct *pcm;
19968 int font_not_found_p;
19969 struct font_info *font_info;
19970 int boff; /* baseline offset */
19971 /* We may change it->multibyte_p upon unibyte<->multibyte
19972 conversion. So, save the current value now and restore it
19973 later.
19975 Note: It seems that we don't have to record multibyte_p in
19976 struct glyph because the character code itself tells if or
19977 not the character is multibyte. Thus, in the future, we must
19978 consider eliminating the field `multibyte_p' in the struct
19979 glyph. */
19980 int saved_multibyte_p = it->multibyte_p;
19982 /* Maybe translate single-byte characters to multibyte, or the
19983 other way. */
19984 it->char_to_display = it->c;
19985 if (!ASCII_BYTE_P (it->c))
19987 if (unibyte_display_via_language_environment
19988 && SINGLE_BYTE_CHAR_P (it->c)
19989 && (it->c >= 0240
19990 || !NILP (Vnonascii_translation_table)))
19992 it->char_to_display = unibyte_char_to_multibyte (it->c);
19993 it->multibyte_p = 1;
19994 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19995 face = FACE_FROM_ID (it->f, it->face_id);
19997 else if (!SINGLE_BYTE_CHAR_P (it->c)
19998 && !it->multibyte_p)
20000 it->multibyte_p = 1;
20001 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20002 face = FACE_FROM_ID (it->f, it->face_id);
20006 /* Get font to use. Encode IT->char_to_display. */
20007 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20008 &char2b, it->multibyte_p, 0);
20009 font = face->font;
20011 /* When no suitable font found, use the default font. */
20012 font_not_found_p = font == NULL;
20013 if (font_not_found_p)
20015 font = FRAME_FONT (it->f);
20016 boff = FRAME_BASELINE_OFFSET (it->f);
20017 font_info = NULL;
20019 else
20021 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20022 boff = font_info->baseline_offset;
20023 if (font_info->vertical_centering)
20024 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20027 if (it->char_to_display >= ' '
20028 && (!it->multibyte_p || it->char_to_display < 128))
20030 /* Either unibyte or ASCII. */
20031 int stretched_p;
20033 it->nglyphs = 1;
20035 pcm = rif->per_char_metric (font, &char2b,
20036 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20038 if (it->override_ascent >= 0)
20040 it->ascent = it->override_ascent;
20041 it->descent = it->override_descent;
20042 boff = it->override_boff;
20044 else
20046 it->ascent = FONT_BASE (font) + boff;
20047 it->descent = FONT_DESCENT (font) - boff;
20050 if (pcm)
20052 it->phys_ascent = pcm->ascent + boff;
20053 it->phys_descent = pcm->descent - boff;
20054 it->pixel_width = pcm->width;
20056 else
20058 it->glyph_not_available_p = 1;
20059 it->phys_ascent = it->ascent;
20060 it->phys_descent = it->descent;
20061 it->pixel_width = FONT_WIDTH (font);
20064 if (it->constrain_row_ascent_descent_p)
20066 if (it->descent > it->max_descent)
20068 it->ascent += it->descent - it->max_descent;
20069 it->descent = it->max_descent;
20071 if (it->ascent > it->max_ascent)
20073 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20074 it->ascent = it->max_ascent;
20076 it->phys_ascent = min (it->phys_ascent, it->ascent);
20077 it->phys_descent = min (it->phys_descent, it->descent);
20078 extra_line_spacing = 0;
20081 /* If this is a space inside a region of text with
20082 `space-width' property, change its width. */
20083 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20084 if (stretched_p)
20085 it->pixel_width *= XFLOATINT (it->space_width);
20087 /* If face has a box, add the box thickness to the character
20088 height. If character has a box line to the left and/or
20089 right, add the box line width to the character's width. */
20090 if (face->box != FACE_NO_BOX)
20092 int thick = face->box_line_width;
20094 if (thick > 0)
20096 it->ascent += thick;
20097 it->descent += thick;
20099 else
20100 thick = -thick;
20102 if (it->start_of_box_run_p)
20103 it->pixel_width += thick;
20104 if (it->end_of_box_run_p)
20105 it->pixel_width += thick;
20108 /* If face has an overline, add the height of the overline
20109 (1 pixel) and a 1 pixel margin to the character height. */
20110 if (face->overline_p)
20111 it->ascent += 2;
20113 if (it->constrain_row_ascent_descent_p)
20115 if (it->ascent > it->max_ascent)
20116 it->ascent = it->max_ascent;
20117 if (it->descent > it->max_descent)
20118 it->descent = it->max_descent;
20121 take_vertical_position_into_account (it);
20123 /* If we have to actually produce glyphs, do it. */
20124 if (it->glyph_row)
20126 if (stretched_p)
20128 /* Translate a space with a `space-width' property
20129 into a stretch glyph. */
20130 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20131 / FONT_HEIGHT (font));
20132 append_stretch_glyph (it, it->object, it->pixel_width,
20133 it->ascent + it->descent, ascent);
20135 else
20136 append_glyph (it);
20138 /* If characters with lbearing or rbearing are displayed
20139 in this line, record that fact in a flag of the
20140 glyph row. This is used to optimize X output code. */
20141 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20142 it->glyph_row->contains_overlapping_glyphs_p = 1;
20145 else if (it->char_to_display == '\n')
20147 /* A newline has no width but we need the height of the line.
20148 But if previous part of the line set a height, don't
20149 increase that height */
20151 Lisp_Object height;
20152 Lisp_Object total_height = Qnil;
20154 it->override_ascent = -1;
20155 it->pixel_width = 0;
20156 it->nglyphs = 0;
20158 height = get_line_height_property(it, Qline_height);
20159 /* Split (line-height total-height) list */
20160 if (CONSP (height)
20161 && CONSP (XCDR (height))
20162 && NILP (XCDR (XCDR (height))))
20164 total_height = XCAR (XCDR (height));
20165 height = XCAR (height);
20167 height = calc_line_height_property(it, height, font, boff, 1);
20169 if (it->override_ascent >= 0)
20171 it->ascent = it->override_ascent;
20172 it->descent = it->override_descent;
20173 boff = it->override_boff;
20175 else
20177 it->ascent = FONT_BASE (font) + boff;
20178 it->descent = FONT_DESCENT (font) - boff;
20181 if (EQ (height, Qt))
20183 if (it->descent > it->max_descent)
20185 it->ascent += it->descent - it->max_descent;
20186 it->descent = it->max_descent;
20188 if (it->ascent > it->max_ascent)
20190 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20191 it->ascent = it->max_ascent;
20193 it->phys_ascent = min (it->phys_ascent, it->ascent);
20194 it->phys_descent = min (it->phys_descent, it->descent);
20195 it->constrain_row_ascent_descent_p = 1;
20196 extra_line_spacing = 0;
20198 else
20200 Lisp_Object spacing;
20202 it->phys_ascent = it->ascent;
20203 it->phys_descent = it->descent;
20205 if ((it->max_ascent > 0 || it->max_descent > 0)
20206 && face->box != FACE_NO_BOX
20207 && face->box_line_width > 0)
20209 it->ascent += face->box_line_width;
20210 it->descent += face->box_line_width;
20212 if (!NILP (height)
20213 && XINT (height) > it->ascent + it->descent)
20214 it->ascent = XINT (height) - it->descent;
20216 if (!NILP (total_height))
20217 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20218 else
20220 spacing = get_line_height_property(it, Qline_spacing);
20221 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20223 if (INTEGERP (spacing))
20225 extra_line_spacing = XINT (spacing);
20226 if (!NILP (total_height))
20227 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20231 else if (it->char_to_display == '\t')
20233 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20234 int x = it->current_x + it->continuation_lines_width;
20235 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20237 /* If the distance from the current position to the next tab
20238 stop is less than a space character width, use the
20239 tab stop after that. */
20240 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20241 next_tab_x += tab_width;
20243 it->pixel_width = next_tab_x - x;
20244 it->nglyphs = 1;
20245 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20246 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20248 if (it->glyph_row)
20250 append_stretch_glyph (it, it->object, it->pixel_width,
20251 it->ascent + it->descent, it->ascent);
20254 else
20256 /* A multi-byte character. Assume that the display width of the
20257 character is the width of the character multiplied by the
20258 width of the font. */
20260 /* If we found a font, this font should give us the right
20261 metrics. If we didn't find a font, use the frame's
20262 default font and calculate the width of the character
20263 from the charset width; this is what old redisplay code
20264 did. */
20266 pcm = rif->per_char_metric (font, &char2b,
20267 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20269 if (font_not_found_p || !pcm)
20271 int charset = CHAR_CHARSET (it->char_to_display);
20273 it->glyph_not_available_p = 1;
20274 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20275 * CHARSET_WIDTH (charset));
20276 it->phys_ascent = FONT_BASE (font) + boff;
20277 it->phys_descent = FONT_DESCENT (font) - boff;
20279 else
20281 it->pixel_width = pcm->width;
20282 it->phys_ascent = pcm->ascent + boff;
20283 it->phys_descent = pcm->descent - boff;
20284 if (it->glyph_row
20285 && (pcm->lbearing < 0
20286 || pcm->rbearing > pcm->width))
20287 it->glyph_row->contains_overlapping_glyphs_p = 1;
20289 it->nglyphs = 1;
20290 it->ascent = FONT_BASE (font) + boff;
20291 it->descent = FONT_DESCENT (font) - boff;
20292 if (face->box != FACE_NO_BOX)
20294 int thick = face->box_line_width;
20296 if (thick > 0)
20298 it->ascent += thick;
20299 it->descent += thick;
20301 else
20302 thick = - thick;
20304 if (it->start_of_box_run_p)
20305 it->pixel_width += thick;
20306 if (it->end_of_box_run_p)
20307 it->pixel_width += thick;
20310 /* If face has an overline, add the height of the overline
20311 (1 pixel) and a 1 pixel margin to the character height. */
20312 if (face->overline_p)
20313 it->ascent += 2;
20315 take_vertical_position_into_account (it);
20317 if (it->glyph_row)
20318 append_glyph (it);
20320 it->multibyte_p = saved_multibyte_p;
20322 else if (it->what == IT_COMPOSITION)
20324 /* Note: A composition is represented as one glyph in the
20325 glyph matrix. There are no padding glyphs. */
20326 XChar2b char2b;
20327 XFontStruct *font;
20328 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20329 XCharStruct *pcm;
20330 int font_not_found_p;
20331 struct font_info *font_info;
20332 int boff; /* baseline offset */
20333 struct composition *cmp = composition_table[it->cmp_id];
20335 /* Maybe translate single-byte characters to multibyte. */
20336 it->char_to_display = it->c;
20337 if (unibyte_display_via_language_environment
20338 && SINGLE_BYTE_CHAR_P (it->c)
20339 && (it->c >= 0240
20340 || (it->c >= 0200
20341 && !NILP (Vnonascii_translation_table))))
20343 it->char_to_display = unibyte_char_to_multibyte (it->c);
20346 /* Get face and font to use. Encode IT->char_to_display. */
20347 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20348 face = FACE_FROM_ID (it->f, it->face_id);
20349 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20350 &char2b, it->multibyte_p, 0);
20351 font = face->font;
20353 /* When no suitable font found, use the default font. */
20354 font_not_found_p = font == NULL;
20355 if (font_not_found_p)
20357 font = FRAME_FONT (it->f);
20358 boff = FRAME_BASELINE_OFFSET (it->f);
20359 font_info = NULL;
20361 else
20363 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20364 boff = font_info->baseline_offset;
20365 if (font_info->vertical_centering)
20366 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20369 /* There are no padding glyphs, so there is only one glyph to
20370 produce for the composition. Important is that pixel_width,
20371 ascent and descent are the values of what is drawn by
20372 draw_glyphs (i.e. the values of the overall glyphs composed). */
20373 it->nglyphs = 1;
20375 /* If we have not yet calculated pixel size data of glyphs of
20376 the composition for the current face font, calculate them
20377 now. Theoretically, we have to check all fonts for the
20378 glyphs, but that requires much time and memory space. So,
20379 here we check only the font of the first glyph. This leads
20380 to incorrect display very rarely, and C-l (recenter) can
20381 correct the display anyway. */
20382 if (cmp->font != (void *) font)
20384 /* Ascent and descent of the font of the first character of
20385 this composition (adjusted by baseline offset). Ascent
20386 and descent of overall glyphs should not be less than
20387 them respectively. */
20388 int font_ascent = FONT_BASE (font) + boff;
20389 int font_descent = FONT_DESCENT (font) - boff;
20390 /* Bounding box of the overall glyphs. */
20391 int leftmost, rightmost, lowest, highest;
20392 int i, width, ascent, descent;
20394 cmp->font = (void *) font;
20396 /* Initialize the bounding box. */
20397 if (font_info
20398 && (pcm = rif->per_char_metric (font, &char2b,
20399 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20401 width = pcm->width;
20402 ascent = pcm->ascent;
20403 descent = pcm->descent;
20405 else
20407 width = FONT_WIDTH (font);
20408 ascent = FONT_BASE (font);
20409 descent = FONT_DESCENT (font);
20412 rightmost = width;
20413 lowest = - descent + boff;
20414 highest = ascent + boff;
20415 leftmost = 0;
20417 if (font_info
20418 && font_info->default_ascent
20419 && CHAR_TABLE_P (Vuse_default_ascent)
20420 && !NILP (Faref (Vuse_default_ascent,
20421 make_number (it->char_to_display))))
20422 highest = font_info->default_ascent + boff;
20424 /* Draw the first glyph at the normal position. It may be
20425 shifted to right later if some other glyphs are drawn at
20426 the left. */
20427 cmp->offsets[0] = 0;
20428 cmp->offsets[1] = boff;
20430 /* Set cmp->offsets for the remaining glyphs. */
20431 for (i = 1; i < cmp->glyph_len; i++)
20433 int left, right, btm, top;
20434 int ch = COMPOSITION_GLYPH (cmp, i);
20435 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20437 face = FACE_FROM_ID (it->f, face_id);
20438 get_char_face_and_encoding (it->f, ch, face->id,
20439 &char2b, it->multibyte_p, 0);
20440 font = face->font;
20441 if (font == NULL)
20443 font = FRAME_FONT (it->f);
20444 boff = FRAME_BASELINE_OFFSET (it->f);
20445 font_info = NULL;
20447 else
20449 font_info
20450 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20451 boff = font_info->baseline_offset;
20452 if (font_info->vertical_centering)
20453 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20456 if (font_info
20457 && (pcm = rif->per_char_metric (font, &char2b,
20458 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20460 width = pcm->width;
20461 ascent = pcm->ascent;
20462 descent = pcm->descent;
20464 else
20466 width = FONT_WIDTH (font);
20467 ascent = 1;
20468 descent = 0;
20471 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20473 /* Relative composition with or without
20474 alternate chars. */
20475 left = (leftmost + rightmost - width) / 2;
20476 btm = - descent + boff;
20477 if (font_info && font_info->relative_compose
20478 && (! CHAR_TABLE_P (Vignore_relative_composition)
20479 || NILP (Faref (Vignore_relative_composition,
20480 make_number (ch)))))
20483 if (- descent >= font_info->relative_compose)
20484 /* One extra pixel between two glyphs. */
20485 btm = highest + 1;
20486 else if (ascent <= 0)
20487 /* One extra pixel between two glyphs. */
20488 btm = lowest - 1 - ascent - descent;
20491 else
20493 /* A composition rule is specified by an integer
20494 value that encodes global and new reference
20495 points (GREF and NREF). GREF and NREF are
20496 specified by numbers as below:
20498 0---1---2 -- ascent
20502 9--10--11 -- center
20504 ---3---4---5--- baseline
20506 6---7---8 -- descent
20508 int rule = COMPOSITION_RULE (cmp, i);
20509 int gref, nref, grefx, grefy, nrefx, nrefy;
20511 COMPOSITION_DECODE_RULE (rule, gref, nref);
20512 grefx = gref % 3, nrefx = nref % 3;
20513 grefy = gref / 3, nrefy = nref / 3;
20515 left = (leftmost
20516 + grefx * (rightmost - leftmost) / 2
20517 - nrefx * width / 2);
20518 btm = ((grefy == 0 ? highest
20519 : grefy == 1 ? 0
20520 : grefy == 2 ? lowest
20521 : (highest + lowest) / 2)
20522 - (nrefy == 0 ? ascent + descent
20523 : nrefy == 1 ? descent - boff
20524 : nrefy == 2 ? 0
20525 : (ascent + descent) / 2));
20528 cmp->offsets[i * 2] = left;
20529 cmp->offsets[i * 2 + 1] = btm + descent;
20531 /* Update the bounding box of the overall glyphs. */
20532 right = left + width;
20533 top = btm + descent + ascent;
20534 if (left < leftmost)
20535 leftmost = left;
20536 if (right > rightmost)
20537 rightmost = right;
20538 if (top > highest)
20539 highest = top;
20540 if (btm < lowest)
20541 lowest = btm;
20544 /* If there are glyphs whose x-offsets are negative,
20545 shift all glyphs to the right and make all x-offsets
20546 non-negative. */
20547 if (leftmost < 0)
20549 for (i = 0; i < cmp->glyph_len; i++)
20550 cmp->offsets[i * 2] -= leftmost;
20551 rightmost -= leftmost;
20554 cmp->pixel_width = rightmost;
20555 cmp->ascent = highest;
20556 cmp->descent = - lowest;
20557 if (cmp->ascent < font_ascent)
20558 cmp->ascent = font_ascent;
20559 if (cmp->descent < font_descent)
20560 cmp->descent = font_descent;
20563 it->pixel_width = cmp->pixel_width;
20564 it->ascent = it->phys_ascent = cmp->ascent;
20565 it->descent = it->phys_descent = cmp->descent;
20567 if (face->box != FACE_NO_BOX)
20569 int thick = face->box_line_width;
20571 if (thick > 0)
20573 it->ascent += thick;
20574 it->descent += thick;
20576 else
20577 thick = - thick;
20579 if (it->start_of_box_run_p)
20580 it->pixel_width += thick;
20581 if (it->end_of_box_run_p)
20582 it->pixel_width += thick;
20585 /* If face has an overline, add the height of the overline
20586 (1 pixel) and a 1 pixel margin to the character height. */
20587 if (face->overline_p)
20588 it->ascent += 2;
20590 take_vertical_position_into_account (it);
20592 if (it->glyph_row)
20593 append_composite_glyph (it);
20595 else if (it->what == IT_IMAGE)
20596 produce_image_glyph (it);
20597 else if (it->what == IT_STRETCH)
20598 produce_stretch_glyph (it);
20600 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20601 because this isn't true for images with `:ascent 100'. */
20602 xassert (it->ascent >= 0 && it->descent >= 0);
20603 if (it->area == TEXT_AREA)
20604 it->current_x += it->pixel_width;
20606 if (extra_line_spacing > 0)
20608 it->descent += extra_line_spacing;
20609 if (extra_line_spacing > it->max_extra_line_spacing)
20610 it->max_extra_line_spacing = extra_line_spacing;
20613 it->max_ascent = max (it->max_ascent, it->ascent);
20614 it->max_descent = max (it->max_descent, it->descent);
20615 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20616 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20619 /* EXPORT for RIF:
20620 Output LEN glyphs starting at START at the nominal cursor position.
20621 Advance the nominal cursor over the text. The global variable
20622 updated_window contains the window being updated, updated_row is
20623 the glyph row being updated, and updated_area is the area of that
20624 row being updated. */
20626 void
20627 x_write_glyphs (start, len)
20628 struct glyph *start;
20629 int len;
20631 int x, hpos;
20633 xassert (updated_window && updated_row);
20634 BLOCK_INPUT;
20636 /* Write glyphs. */
20638 hpos = start - updated_row->glyphs[updated_area];
20639 x = draw_glyphs (updated_window, output_cursor.x,
20640 updated_row, updated_area,
20641 hpos, hpos + len,
20642 DRAW_NORMAL_TEXT, 0);
20644 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20645 if (updated_area == TEXT_AREA
20646 && updated_window->phys_cursor_on_p
20647 && updated_window->phys_cursor.vpos == output_cursor.vpos
20648 && updated_window->phys_cursor.hpos >= hpos
20649 && updated_window->phys_cursor.hpos < hpos + len)
20650 updated_window->phys_cursor_on_p = 0;
20652 UNBLOCK_INPUT;
20654 /* Advance the output cursor. */
20655 output_cursor.hpos += len;
20656 output_cursor.x = x;
20660 /* EXPORT for RIF:
20661 Insert LEN glyphs from START at the nominal cursor position. */
20663 void
20664 x_insert_glyphs (start, len)
20665 struct glyph *start;
20666 int len;
20668 struct frame *f;
20669 struct window *w;
20670 int line_height, shift_by_width, shifted_region_width;
20671 struct glyph_row *row;
20672 struct glyph *glyph;
20673 int frame_x, frame_y, hpos;
20675 xassert (updated_window && updated_row);
20676 BLOCK_INPUT;
20677 w = updated_window;
20678 f = XFRAME (WINDOW_FRAME (w));
20680 /* Get the height of the line we are in. */
20681 row = updated_row;
20682 line_height = row->height;
20684 /* Get the width of the glyphs to insert. */
20685 shift_by_width = 0;
20686 for (glyph = start; glyph < start + len; ++glyph)
20687 shift_by_width += glyph->pixel_width;
20689 /* Get the width of the region to shift right. */
20690 shifted_region_width = (window_box_width (w, updated_area)
20691 - output_cursor.x
20692 - shift_by_width);
20694 /* Shift right. */
20695 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20696 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20698 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20699 line_height, shift_by_width);
20701 /* Write the glyphs. */
20702 hpos = start - row->glyphs[updated_area];
20703 draw_glyphs (w, output_cursor.x, row, updated_area,
20704 hpos, hpos + len,
20705 DRAW_NORMAL_TEXT, 0);
20707 /* Advance the output cursor. */
20708 output_cursor.hpos += len;
20709 output_cursor.x += shift_by_width;
20710 UNBLOCK_INPUT;
20714 /* EXPORT for RIF:
20715 Erase the current text line from the nominal cursor position
20716 (inclusive) to pixel column TO_X (exclusive). The idea is that
20717 everything from TO_X onward is already erased.
20719 TO_X is a pixel position relative to updated_area of
20720 updated_window. TO_X == -1 means clear to the end of this area. */
20722 void
20723 x_clear_end_of_line (to_x)
20724 int to_x;
20726 struct frame *f;
20727 struct window *w = updated_window;
20728 int max_x, min_y, max_y;
20729 int from_x, from_y, to_y;
20731 xassert (updated_window && updated_row);
20732 f = XFRAME (w->frame);
20734 if (updated_row->full_width_p)
20735 max_x = WINDOW_TOTAL_WIDTH (w);
20736 else
20737 max_x = window_box_width (w, updated_area);
20738 max_y = window_text_bottom_y (w);
20740 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20741 of window. For TO_X > 0, truncate to end of drawing area. */
20742 if (to_x == 0)
20743 return;
20744 else if (to_x < 0)
20745 to_x = max_x;
20746 else
20747 to_x = min (to_x, max_x);
20749 to_y = min (max_y, output_cursor.y + updated_row->height);
20751 /* Notice if the cursor will be cleared by this operation. */
20752 if (!updated_row->full_width_p)
20753 notice_overwritten_cursor (w, updated_area,
20754 output_cursor.x, -1,
20755 updated_row->y,
20756 MATRIX_ROW_BOTTOM_Y (updated_row));
20758 from_x = output_cursor.x;
20760 /* Translate to frame coordinates. */
20761 if (updated_row->full_width_p)
20763 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20764 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20766 else
20768 int area_left = window_box_left (w, updated_area);
20769 from_x += area_left;
20770 to_x += area_left;
20773 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20774 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20775 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20777 /* Prevent inadvertently clearing to end of the X window. */
20778 if (to_x > from_x && to_y > from_y)
20780 BLOCK_INPUT;
20781 rif->clear_frame_area (f, from_x, from_y,
20782 to_x - from_x, to_y - from_y);
20783 UNBLOCK_INPUT;
20787 #endif /* HAVE_WINDOW_SYSTEM */
20791 /***********************************************************************
20792 Cursor types
20793 ***********************************************************************/
20795 /* Value is the internal representation of the specified cursor type
20796 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20797 of the bar cursor. */
20799 static enum text_cursor_kinds
20800 get_specified_cursor_type (arg, width)
20801 Lisp_Object arg;
20802 int *width;
20804 enum text_cursor_kinds type;
20806 if (NILP (arg))
20807 return NO_CURSOR;
20809 if (EQ (arg, Qbox))
20810 return FILLED_BOX_CURSOR;
20812 if (EQ (arg, Qhollow))
20813 return HOLLOW_BOX_CURSOR;
20815 if (EQ (arg, Qbar))
20817 *width = 2;
20818 return BAR_CURSOR;
20821 if (CONSP (arg)
20822 && EQ (XCAR (arg), Qbar)
20823 && INTEGERP (XCDR (arg))
20824 && XINT (XCDR (arg)) >= 0)
20826 *width = XINT (XCDR (arg));
20827 return BAR_CURSOR;
20830 if (EQ (arg, Qhbar))
20832 *width = 2;
20833 return HBAR_CURSOR;
20836 if (CONSP (arg)
20837 && EQ (XCAR (arg), Qhbar)
20838 && INTEGERP (XCDR (arg))
20839 && XINT (XCDR (arg)) >= 0)
20841 *width = XINT (XCDR (arg));
20842 return HBAR_CURSOR;
20845 /* Treat anything unknown as "hollow box cursor".
20846 It was bad to signal an error; people have trouble fixing
20847 .Xdefaults with Emacs, when it has something bad in it. */
20848 type = HOLLOW_BOX_CURSOR;
20850 return type;
20853 /* Set the default cursor types for specified frame. */
20854 void
20855 set_frame_cursor_types (f, arg)
20856 struct frame *f;
20857 Lisp_Object arg;
20859 int width;
20860 Lisp_Object tem;
20862 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20863 FRAME_CURSOR_WIDTH (f) = width;
20865 /* By default, set up the blink-off state depending on the on-state. */
20867 tem = Fassoc (arg, Vblink_cursor_alist);
20868 if (!NILP (tem))
20870 FRAME_BLINK_OFF_CURSOR (f)
20871 = get_specified_cursor_type (XCDR (tem), &width);
20872 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20874 else
20875 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20879 /* Return the cursor we want to be displayed in window W. Return
20880 width of bar/hbar cursor through WIDTH arg. Return with
20881 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20882 (i.e. if the `system caret' should track this cursor).
20884 In a mini-buffer window, we want the cursor only to appear if we
20885 are reading input from this window. For the selected window, we
20886 want the cursor type given by the frame parameter or buffer local
20887 setting of cursor-type. If explicitly marked off, draw no cursor.
20888 In all other cases, we want a hollow box cursor. */
20890 static enum text_cursor_kinds
20891 get_window_cursor_type (w, glyph, width, active_cursor)
20892 struct window *w;
20893 struct glyph *glyph;
20894 int *width;
20895 int *active_cursor;
20897 struct frame *f = XFRAME (w->frame);
20898 struct buffer *b = XBUFFER (w->buffer);
20899 int cursor_type = DEFAULT_CURSOR;
20900 Lisp_Object alt_cursor;
20901 int non_selected = 0;
20903 *active_cursor = 1;
20905 /* Echo area */
20906 if (cursor_in_echo_area
20907 && FRAME_HAS_MINIBUF_P (f)
20908 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20910 if (w == XWINDOW (echo_area_window))
20912 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20914 *width = FRAME_CURSOR_WIDTH (f);
20915 return FRAME_DESIRED_CURSOR (f);
20917 else
20918 return get_specified_cursor_type (b->cursor_type, width);
20921 *active_cursor = 0;
20922 non_selected = 1;
20925 /* Nonselected window or nonselected frame. */
20926 else if (w != XWINDOW (f->selected_window)
20927 #ifdef HAVE_WINDOW_SYSTEM
20928 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20929 #endif
20932 *active_cursor = 0;
20934 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20935 return NO_CURSOR;
20937 non_selected = 1;
20940 /* Never display a cursor in a window in which cursor-type is nil. */
20941 if (NILP (b->cursor_type))
20942 return NO_CURSOR;
20944 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20945 if (non_selected)
20947 alt_cursor = b->cursor_in_non_selected_windows;
20948 return get_specified_cursor_type (alt_cursor, width);
20951 /* Get the normal cursor type for this window. */
20952 if (EQ (b->cursor_type, Qt))
20954 cursor_type = FRAME_DESIRED_CURSOR (f);
20955 *width = FRAME_CURSOR_WIDTH (f);
20957 else
20958 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20960 /* Use normal cursor if not blinked off. */
20961 if (!w->cursor_off_p)
20963 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20964 if (cursor_type == FILLED_BOX_CURSOR)
20965 cursor_type = HOLLOW_BOX_CURSOR;
20967 return cursor_type;
20970 /* Cursor is blinked off, so determine how to "toggle" it. */
20972 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20973 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20974 return get_specified_cursor_type (XCDR (alt_cursor), width);
20976 /* Then see if frame has specified a specific blink off cursor type. */
20977 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20979 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20980 return FRAME_BLINK_OFF_CURSOR (f);
20983 #if 0
20984 /* Some people liked having a permanently visible blinking cursor,
20985 while others had very strong opinions against it. So it was
20986 decided to remove it. KFS 2003-09-03 */
20988 /* Finally perform built-in cursor blinking:
20989 filled box <-> hollow box
20990 wide [h]bar <-> narrow [h]bar
20991 narrow [h]bar <-> no cursor
20992 other type <-> no cursor */
20994 if (cursor_type == FILLED_BOX_CURSOR)
20995 return HOLLOW_BOX_CURSOR;
20997 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20999 *width = 1;
21000 return cursor_type;
21002 #endif
21004 return NO_CURSOR;
21008 #ifdef HAVE_WINDOW_SYSTEM
21010 /* Notice when the text cursor of window W has been completely
21011 overwritten by a drawing operation that outputs glyphs in AREA
21012 starting at X0 and ending at X1 in the line starting at Y0 and
21013 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21014 the rest of the line after X0 has been written. Y coordinates
21015 are window-relative. */
21017 static void
21018 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21019 struct window *w;
21020 enum glyph_row_area area;
21021 int x0, y0, x1, y1;
21023 int cx0, cx1, cy0, cy1;
21024 struct glyph_row *row;
21026 if (!w->phys_cursor_on_p)
21027 return;
21028 if (area != TEXT_AREA)
21029 return;
21031 if (w->phys_cursor.vpos < 0
21032 || w->phys_cursor.vpos >= w->current_matrix->nrows
21033 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21034 !(row->enabled_p && row->displays_text_p)))
21035 return;
21037 if (row->cursor_in_fringe_p)
21039 row->cursor_in_fringe_p = 0;
21040 draw_fringe_bitmap (w, row, 0);
21041 w->phys_cursor_on_p = 0;
21042 return;
21045 cx0 = w->phys_cursor.x;
21046 cx1 = cx0 + w->phys_cursor_width;
21047 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21048 return;
21050 /* The cursor image will be completely removed from the
21051 screen if the output area intersects the cursor area in
21052 y-direction. When we draw in [y0 y1[, and some part of
21053 the cursor is at y < y0, that part must have been drawn
21054 before. When scrolling, the cursor is erased before
21055 actually scrolling, so we don't come here. When not
21056 scrolling, the rows above the old cursor row must have
21057 changed, and in this case these rows must have written
21058 over the cursor image.
21060 Likewise if part of the cursor is below y1, with the
21061 exception of the cursor being in the first blank row at
21062 the buffer and window end because update_text_area
21063 doesn't draw that row. (Except when it does, but
21064 that's handled in update_text_area.) */
21066 cy0 = w->phys_cursor.y;
21067 cy1 = cy0 + w->phys_cursor_height;
21068 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21069 return;
21071 w->phys_cursor_on_p = 0;
21074 #endif /* HAVE_WINDOW_SYSTEM */
21077 /************************************************************************
21078 Mouse Face
21079 ************************************************************************/
21081 #ifdef HAVE_WINDOW_SYSTEM
21083 /* EXPORT for RIF:
21084 Fix the display of area AREA of overlapping row ROW in window W
21085 with respect to the overlapping part OVERLAPS. */
21087 void
21088 x_fix_overlapping_area (w, row, area, overlaps)
21089 struct window *w;
21090 struct glyph_row *row;
21091 enum glyph_row_area area;
21092 int overlaps;
21094 int i, x;
21096 BLOCK_INPUT;
21098 x = 0;
21099 for (i = 0; i < row->used[area];)
21101 if (row->glyphs[area][i].overlaps_vertically_p)
21103 int start = i, start_x = x;
21107 x += row->glyphs[area][i].pixel_width;
21108 ++i;
21110 while (i < row->used[area]
21111 && row->glyphs[area][i].overlaps_vertically_p);
21113 draw_glyphs (w, start_x, row, area,
21114 start, i,
21115 DRAW_NORMAL_TEXT, overlaps);
21117 else
21119 x += row->glyphs[area][i].pixel_width;
21120 ++i;
21124 UNBLOCK_INPUT;
21128 /* EXPORT:
21129 Draw the cursor glyph of window W in glyph row ROW. See the
21130 comment of draw_glyphs for the meaning of HL. */
21132 void
21133 draw_phys_cursor_glyph (w, row, hl)
21134 struct window *w;
21135 struct glyph_row *row;
21136 enum draw_glyphs_face hl;
21138 /* If cursor hpos is out of bounds, don't draw garbage. This can
21139 happen in mini-buffer windows when switching between echo area
21140 glyphs and mini-buffer. */
21141 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21143 int on_p = w->phys_cursor_on_p;
21144 int x1;
21145 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21146 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21147 hl, 0);
21148 w->phys_cursor_on_p = on_p;
21150 if (hl == DRAW_CURSOR)
21151 w->phys_cursor_width = x1 - w->phys_cursor.x;
21152 /* When we erase the cursor, and ROW is overlapped by other
21153 rows, make sure that these overlapping parts of other rows
21154 are redrawn. */
21155 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21157 w->phys_cursor_width = x1 - w->phys_cursor.x;
21159 if (row > w->current_matrix->rows
21160 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21161 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21162 OVERLAPS_ERASED_CURSOR);
21164 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21165 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21166 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21167 OVERLAPS_ERASED_CURSOR);
21173 /* EXPORT:
21174 Erase the image of a cursor of window W from the screen. */
21176 void
21177 erase_phys_cursor (w)
21178 struct window *w;
21180 struct frame *f = XFRAME (w->frame);
21181 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21182 int hpos = w->phys_cursor.hpos;
21183 int vpos = w->phys_cursor.vpos;
21184 int mouse_face_here_p = 0;
21185 struct glyph_matrix *active_glyphs = w->current_matrix;
21186 struct glyph_row *cursor_row;
21187 struct glyph *cursor_glyph;
21188 enum draw_glyphs_face hl;
21190 /* No cursor displayed or row invalidated => nothing to do on the
21191 screen. */
21192 if (w->phys_cursor_type == NO_CURSOR)
21193 goto mark_cursor_off;
21195 /* VPOS >= active_glyphs->nrows means that window has been resized.
21196 Don't bother to erase the cursor. */
21197 if (vpos >= active_glyphs->nrows)
21198 goto mark_cursor_off;
21200 /* If row containing cursor is marked invalid, there is nothing we
21201 can do. */
21202 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21203 if (!cursor_row->enabled_p)
21204 goto mark_cursor_off;
21206 /* If line spacing is > 0, old cursor may only be partially visible in
21207 window after split-window. So adjust visible height. */
21208 cursor_row->visible_height = min (cursor_row->visible_height,
21209 window_text_bottom_y (w) - cursor_row->y);
21211 /* If row is completely invisible, don't attempt to delete a cursor which
21212 isn't there. This can happen if cursor is at top of a window, and
21213 we switch to a buffer with a header line in that window. */
21214 if (cursor_row->visible_height <= 0)
21215 goto mark_cursor_off;
21217 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21218 if (cursor_row->cursor_in_fringe_p)
21220 cursor_row->cursor_in_fringe_p = 0;
21221 draw_fringe_bitmap (w, cursor_row, 0);
21222 goto mark_cursor_off;
21225 /* This can happen when the new row is shorter than the old one.
21226 In this case, either draw_glyphs or clear_end_of_line
21227 should have cleared the cursor. Note that we wouldn't be
21228 able to erase the cursor in this case because we don't have a
21229 cursor glyph at hand. */
21230 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21231 goto mark_cursor_off;
21233 /* If the cursor is in the mouse face area, redisplay that when
21234 we clear the cursor. */
21235 if (! NILP (dpyinfo->mouse_face_window)
21236 && w == XWINDOW (dpyinfo->mouse_face_window)
21237 && (vpos > dpyinfo->mouse_face_beg_row
21238 || (vpos == dpyinfo->mouse_face_beg_row
21239 && hpos >= dpyinfo->mouse_face_beg_col))
21240 && (vpos < dpyinfo->mouse_face_end_row
21241 || (vpos == dpyinfo->mouse_face_end_row
21242 && hpos < dpyinfo->mouse_face_end_col))
21243 /* Don't redraw the cursor's spot in mouse face if it is at the
21244 end of a line (on a newline). The cursor appears there, but
21245 mouse highlighting does not. */
21246 && cursor_row->used[TEXT_AREA] > hpos)
21247 mouse_face_here_p = 1;
21249 /* Maybe clear the display under the cursor. */
21250 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21252 int x, y;
21253 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21254 int width;
21256 cursor_glyph = get_phys_cursor_glyph (w);
21257 if (cursor_glyph == NULL)
21258 goto mark_cursor_off;
21260 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21261 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21262 width = min (cursor_glyph->pixel_width,
21263 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21265 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21268 /* Erase the cursor by redrawing the character underneath it. */
21269 if (mouse_face_here_p)
21270 hl = DRAW_MOUSE_FACE;
21271 else
21272 hl = DRAW_NORMAL_TEXT;
21273 draw_phys_cursor_glyph (w, cursor_row, hl);
21275 mark_cursor_off:
21276 w->phys_cursor_on_p = 0;
21277 w->phys_cursor_type = NO_CURSOR;
21281 /* EXPORT:
21282 Display or clear cursor of window W. If ON is zero, clear the
21283 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21284 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21286 void
21287 display_and_set_cursor (w, on, hpos, vpos, x, y)
21288 struct window *w;
21289 int on, hpos, vpos, x, y;
21291 struct frame *f = XFRAME (w->frame);
21292 int new_cursor_type;
21293 int new_cursor_width;
21294 int active_cursor;
21295 struct glyph_row *glyph_row;
21296 struct glyph *glyph;
21298 /* This is pointless on invisible frames, and dangerous on garbaged
21299 windows and frames; in the latter case, the frame or window may
21300 be in the midst of changing its size, and x and y may be off the
21301 window. */
21302 if (! FRAME_VISIBLE_P (f)
21303 || FRAME_GARBAGED_P (f)
21304 || vpos >= w->current_matrix->nrows
21305 || hpos >= w->current_matrix->matrix_w)
21306 return;
21308 /* If cursor is off and we want it off, return quickly. */
21309 if (!on && !w->phys_cursor_on_p)
21310 return;
21312 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21313 /* If cursor row is not enabled, we don't really know where to
21314 display the cursor. */
21315 if (!glyph_row->enabled_p)
21317 w->phys_cursor_on_p = 0;
21318 return;
21321 glyph = NULL;
21322 if (!glyph_row->exact_window_width_line_p
21323 || hpos < glyph_row->used[TEXT_AREA])
21324 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21326 xassert (interrupt_input_blocked);
21328 /* Set new_cursor_type to the cursor we want to be displayed. */
21329 new_cursor_type = get_window_cursor_type (w, glyph,
21330 &new_cursor_width, &active_cursor);
21332 /* If cursor is currently being shown and we don't want it to be or
21333 it is in the wrong place, or the cursor type is not what we want,
21334 erase it. */
21335 if (w->phys_cursor_on_p
21336 && (!on
21337 || w->phys_cursor.x != x
21338 || w->phys_cursor.y != y
21339 || new_cursor_type != w->phys_cursor_type
21340 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21341 && new_cursor_width != w->phys_cursor_width)))
21342 erase_phys_cursor (w);
21344 /* Don't check phys_cursor_on_p here because that flag is only set
21345 to zero in some cases where we know that the cursor has been
21346 completely erased, to avoid the extra work of erasing the cursor
21347 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21348 still not be visible, or it has only been partly erased. */
21349 if (on)
21351 w->phys_cursor_ascent = glyph_row->ascent;
21352 w->phys_cursor_height = glyph_row->height;
21354 /* Set phys_cursor_.* before x_draw_.* is called because some
21355 of them may need the information. */
21356 w->phys_cursor.x = x;
21357 w->phys_cursor.y = glyph_row->y;
21358 w->phys_cursor.hpos = hpos;
21359 w->phys_cursor.vpos = vpos;
21362 rif->draw_window_cursor (w, glyph_row, x, y,
21363 new_cursor_type, new_cursor_width,
21364 on, active_cursor);
21368 /* Switch the display of W's cursor on or off, according to the value
21369 of ON. */
21371 static void
21372 update_window_cursor (w, on)
21373 struct window *w;
21374 int on;
21376 /* Don't update cursor in windows whose frame is in the process
21377 of being deleted. */
21378 if (w->current_matrix)
21380 BLOCK_INPUT;
21381 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21382 w->phys_cursor.x, w->phys_cursor.y);
21383 UNBLOCK_INPUT;
21388 /* Call update_window_cursor with parameter ON_P on all leaf windows
21389 in the window tree rooted at W. */
21391 static void
21392 update_cursor_in_window_tree (w, on_p)
21393 struct window *w;
21394 int on_p;
21396 while (w)
21398 if (!NILP (w->hchild))
21399 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21400 else if (!NILP (w->vchild))
21401 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21402 else
21403 update_window_cursor (w, on_p);
21405 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21410 /* EXPORT:
21411 Display the cursor on window W, or clear it, according to ON_P.
21412 Don't change the cursor's position. */
21414 void
21415 x_update_cursor (f, on_p)
21416 struct frame *f;
21417 int on_p;
21419 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21423 /* EXPORT:
21424 Clear the cursor of window W to background color, and mark the
21425 cursor as not shown. This is used when the text where the cursor
21426 is is about to be rewritten. */
21428 void
21429 x_clear_cursor (w)
21430 struct window *w;
21432 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21433 update_window_cursor (w, 0);
21437 /* EXPORT:
21438 Display the active region described by mouse_face_* according to DRAW. */
21440 void
21441 show_mouse_face (dpyinfo, draw)
21442 Display_Info *dpyinfo;
21443 enum draw_glyphs_face draw;
21445 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21446 struct frame *f = XFRAME (WINDOW_FRAME (w));
21448 if (/* If window is in the process of being destroyed, don't bother
21449 to do anything. */
21450 w->current_matrix != NULL
21451 /* Don't update mouse highlight if hidden */
21452 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21453 /* Recognize when we are called to operate on rows that don't exist
21454 anymore. This can happen when a window is split. */
21455 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21457 int phys_cursor_on_p = w->phys_cursor_on_p;
21458 struct glyph_row *row, *first, *last;
21460 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21461 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21463 for (row = first; row <= last && row->enabled_p; ++row)
21465 int start_hpos, end_hpos, start_x;
21467 /* For all but the first row, the highlight starts at column 0. */
21468 if (row == first)
21470 start_hpos = dpyinfo->mouse_face_beg_col;
21471 start_x = dpyinfo->mouse_face_beg_x;
21473 else
21475 start_hpos = 0;
21476 start_x = 0;
21479 if (row == last)
21480 end_hpos = dpyinfo->mouse_face_end_col;
21481 else
21483 end_hpos = row->used[TEXT_AREA];
21484 if (draw == DRAW_NORMAL_TEXT)
21485 row->fill_line_p = 1; /* Clear to end of line */
21488 if (end_hpos > start_hpos)
21490 draw_glyphs (w, start_x, row, TEXT_AREA,
21491 start_hpos, end_hpos,
21492 draw, 0);
21494 row->mouse_face_p
21495 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21499 /* When we've written over the cursor, arrange for it to
21500 be displayed again. */
21501 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21503 BLOCK_INPUT;
21504 display_and_set_cursor (w, 1,
21505 w->phys_cursor.hpos, w->phys_cursor.vpos,
21506 w->phys_cursor.x, w->phys_cursor.y);
21507 UNBLOCK_INPUT;
21511 /* Change the mouse cursor. */
21512 if (draw == DRAW_NORMAL_TEXT)
21513 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21514 else if (draw == DRAW_MOUSE_FACE)
21515 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21516 else
21517 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21520 /* EXPORT:
21521 Clear out the mouse-highlighted active region.
21522 Redraw it un-highlighted first. Value is non-zero if mouse
21523 face was actually drawn unhighlighted. */
21526 clear_mouse_face (dpyinfo)
21527 Display_Info *dpyinfo;
21529 int cleared = 0;
21531 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21533 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21534 cleared = 1;
21537 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21538 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21539 dpyinfo->mouse_face_window = Qnil;
21540 dpyinfo->mouse_face_overlay = Qnil;
21541 return cleared;
21545 /* EXPORT:
21546 Non-zero if physical cursor of window W is within mouse face. */
21549 cursor_in_mouse_face_p (w)
21550 struct window *w;
21552 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21553 int in_mouse_face = 0;
21555 if (WINDOWP (dpyinfo->mouse_face_window)
21556 && XWINDOW (dpyinfo->mouse_face_window) == w)
21558 int hpos = w->phys_cursor.hpos;
21559 int vpos = w->phys_cursor.vpos;
21561 if (vpos >= dpyinfo->mouse_face_beg_row
21562 && vpos <= dpyinfo->mouse_face_end_row
21563 && (vpos > dpyinfo->mouse_face_beg_row
21564 || hpos >= dpyinfo->mouse_face_beg_col)
21565 && (vpos < dpyinfo->mouse_face_end_row
21566 || hpos < dpyinfo->mouse_face_end_col
21567 || dpyinfo->mouse_face_past_end))
21568 in_mouse_face = 1;
21571 return in_mouse_face;
21577 /* Find the glyph matrix position of buffer position CHARPOS in window
21578 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21579 current glyphs must be up to date. If CHARPOS is above window
21580 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21581 of last line in W. In the row containing CHARPOS, stop before glyphs
21582 having STOP as object. */
21584 #if 1 /* This is a version of fast_find_position that's more correct
21585 in the presence of hscrolling, for example. I didn't install
21586 it right away because the problem fixed is minor, it failed
21587 in 20.x as well, and I think it's too risky to install
21588 so near the release of 21.1. 2001-09-25 gerd. */
21590 static int
21591 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21592 struct window *w;
21593 int charpos;
21594 int *hpos, *vpos, *x, *y;
21595 Lisp_Object stop;
21597 struct glyph_row *row, *first;
21598 struct glyph *glyph, *end;
21599 int past_end = 0;
21601 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21602 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21604 *x = first->x;
21605 *y = first->y;
21606 *hpos = 0;
21607 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21608 return 1;
21611 row = row_containing_pos (w, charpos, first, NULL, 0);
21612 if (row == NULL)
21614 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21615 past_end = 1;
21618 /* If whole rows or last part of a row came from a display overlay,
21619 row_containing_pos will skip over such rows because their end pos
21620 equals the start pos of the overlay or interval.
21622 Move back if we have a STOP object and previous row's
21623 end glyph came from STOP. */
21624 if (!NILP (stop))
21626 struct glyph_row *prev;
21627 while ((prev = row - 1, prev >= first)
21628 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21629 && prev->used[TEXT_AREA] > 0)
21631 struct glyph *beg = prev->glyphs[TEXT_AREA];
21632 glyph = beg + prev->used[TEXT_AREA];
21633 while (--glyph >= beg
21634 && INTEGERP (glyph->object));
21635 if (glyph < beg
21636 || !EQ (stop, glyph->object))
21637 break;
21638 row = prev;
21642 *x = row->x;
21643 *y = row->y;
21644 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21646 glyph = row->glyphs[TEXT_AREA];
21647 end = glyph + row->used[TEXT_AREA];
21649 /* Skip over glyphs not having an object at the start of the row.
21650 These are special glyphs like truncation marks on terminal
21651 frames. */
21652 if (row->displays_text_p)
21653 while (glyph < end
21654 && INTEGERP (glyph->object)
21655 && !EQ (stop, glyph->object)
21656 && glyph->charpos < 0)
21658 *x += glyph->pixel_width;
21659 ++glyph;
21662 while (glyph < end
21663 && !INTEGERP (glyph->object)
21664 && !EQ (stop, glyph->object)
21665 && (!BUFFERP (glyph->object)
21666 || glyph->charpos < charpos))
21668 *x += glyph->pixel_width;
21669 ++glyph;
21672 *hpos = glyph - row->glyphs[TEXT_AREA];
21673 return !past_end;
21676 #else /* not 1 */
21678 static int
21679 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21680 struct window *w;
21681 int pos;
21682 int *hpos, *vpos, *x, *y;
21683 Lisp_Object stop;
21685 int i;
21686 int lastcol;
21687 int maybe_next_line_p = 0;
21688 int line_start_position;
21689 int yb = window_text_bottom_y (w);
21690 struct glyph_row *row, *best_row;
21691 int row_vpos, best_row_vpos;
21692 int current_x;
21694 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21695 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21697 while (row->y < yb)
21699 if (row->used[TEXT_AREA])
21700 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21701 else
21702 line_start_position = 0;
21704 if (line_start_position > pos)
21705 break;
21706 /* If the position sought is the end of the buffer,
21707 don't include the blank lines at the bottom of the window. */
21708 else if (line_start_position == pos
21709 && pos == BUF_ZV (XBUFFER (w->buffer)))
21711 maybe_next_line_p = 1;
21712 break;
21714 else if (line_start_position > 0)
21716 best_row = row;
21717 best_row_vpos = row_vpos;
21720 if (row->y + row->height >= yb)
21721 break;
21723 ++row;
21724 ++row_vpos;
21727 /* Find the right column within BEST_ROW. */
21728 lastcol = 0;
21729 current_x = best_row->x;
21730 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21732 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21733 int charpos = glyph->charpos;
21735 if (BUFFERP (glyph->object))
21737 if (charpos == pos)
21739 *hpos = i;
21740 *vpos = best_row_vpos;
21741 *x = current_x;
21742 *y = best_row->y;
21743 return 1;
21745 else if (charpos > pos)
21746 break;
21748 else if (EQ (glyph->object, stop))
21749 break;
21751 if (charpos > 0)
21752 lastcol = i;
21753 current_x += glyph->pixel_width;
21756 /* If we're looking for the end of the buffer,
21757 and we didn't find it in the line we scanned,
21758 use the start of the following line. */
21759 if (maybe_next_line_p)
21761 ++best_row;
21762 ++best_row_vpos;
21763 lastcol = 0;
21764 current_x = best_row->x;
21767 *vpos = best_row_vpos;
21768 *hpos = lastcol + 1;
21769 *x = current_x;
21770 *y = best_row->y;
21771 return 0;
21774 #endif /* not 1 */
21777 /* Find the position of the glyph for position POS in OBJECT in
21778 window W's current matrix, and return in *X, *Y the pixel
21779 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21781 RIGHT_P non-zero means return the position of the right edge of the
21782 glyph, RIGHT_P zero means return the left edge position.
21784 If no glyph for POS exists in the matrix, return the position of
21785 the glyph with the next smaller position that is in the matrix, if
21786 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21787 exists in the matrix, return the position of the glyph with the
21788 next larger position in OBJECT.
21790 Value is non-zero if a glyph was found. */
21792 static int
21793 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21794 struct window *w;
21795 int pos;
21796 Lisp_Object object;
21797 int *hpos, *vpos, *x, *y;
21798 int right_p;
21800 int yb = window_text_bottom_y (w);
21801 struct glyph_row *r;
21802 struct glyph *best_glyph = NULL;
21803 struct glyph_row *best_row = NULL;
21804 int best_x = 0;
21806 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21807 r->enabled_p && r->y < yb;
21808 ++r)
21810 struct glyph *g = r->glyphs[TEXT_AREA];
21811 struct glyph *e = g + r->used[TEXT_AREA];
21812 int gx;
21814 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21815 if (EQ (g->object, object))
21817 if (g->charpos == pos)
21819 best_glyph = g;
21820 best_x = gx;
21821 best_row = r;
21822 goto found;
21824 else if (best_glyph == NULL
21825 || ((abs (g->charpos - pos)
21826 < abs (best_glyph->charpos - pos))
21827 && (right_p
21828 ? g->charpos < pos
21829 : g->charpos > pos)))
21831 best_glyph = g;
21832 best_x = gx;
21833 best_row = r;
21838 found:
21840 if (best_glyph)
21842 *x = best_x;
21843 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21845 if (right_p)
21847 *x += best_glyph->pixel_width;
21848 ++*hpos;
21851 *y = best_row->y;
21852 *vpos = best_row - w->current_matrix->rows;
21855 return best_glyph != NULL;
21859 /* See if position X, Y is within a hot-spot of an image. */
21861 static int
21862 on_hot_spot_p (hot_spot, x, y)
21863 Lisp_Object hot_spot;
21864 int x, y;
21866 if (!CONSP (hot_spot))
21867 return 0;
21869 if (EQ (XCAR (hot_spot), Qrect))
21871 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21872 Lisp_Object rect = XCDR (hot_spot);
21873 Lisp_Object tem;
21874 if (!CONSP (rect))
21875 return 0;
21876 if (!CONSP (XCAR (rect)))
21877 return 0;
21878 if (!CONSP (XCDR (rect)))
21879 return 0;
21880 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21881 return 0;
21882 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21883 return 0;
21884 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21885 return 0;
21886 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21887 return 0;
21888 return 1;
21890 else if (EQ (XCAR (hot_spot), Qcircle))
21892 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21893 Lisp_Object circ = XCDR (hot_spot);
21894 Lisp_Object lr, lx0, ly0;
21895 if (CONSP (circ)
21896 && CONSP (XCAR (circ))
21897 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21898 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21899 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21901 double r = XFLOATINT (lr);
21902 double dx = XINT (lx0) - x;
21903 double dy = XINT (ly0) - y;
21904 return (dx * dx + dy * dy <= r * r);
21907 else if (EQ (XCAR (hot_spot), Qpoly))
21909 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21910 if (VECTORP (XCDR (hot_spot)))
21912 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21913 Lisp_Object *poly = v->contents;
21914 int n = v->size;
21915 int i;
21916 int inside = 0;
21917 Lisp_Object lx, ly;
21918 int x0, y0;
21920 /* Need an even number of coordinates, and at least 3 edges. */
21921 if (n < 6 || n & 1)
21922 return 0;
21924 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21925 If count is odd, we are inside polygon. Pixels on edges
21926 may or may not be included depending on actual geometry of the
21927 polygon. */
21928 if ((lx = poly[n-2], !INTEGERP (lx))
21929 || (ly = poly[n-1], !INTEGERP (lx)))
21930 return 0;
21931 x0 = XINT (lx), y0 = XINT (ly);
21932 for (i = 0; i < n; i += 2)
21934 int x1 = x0, y1 = y0;
21935 if ((lx = poly[i], !INTEGERP (lx))
21936 || (ly = poly[i+1], !INTEGERP (ly)))
21937 return 0;
21938 x0 = XINT (lx), y0 = XINT (ly);
21940 /* Does this segment cross the X line? */
21941 if (x0 >= x)
21943 if (x1 >= x)
21944 continue;
21946 else if (x1 < x)
21947 continue;
21948 if (y > y0 && y > y1)
21949 continue;
21950 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21951 inside = !inside;
21953 return inside;
21956 /* If we don't understand the format, pretend we're not in the hot-spot. */
21957 return 0;
21960 Lisp_Object
21961 find_hot_spot (map, x, y)
21962 Lisp_Object map;
21963 int x, y;
21965 while (CONSP (map))
21967 if (CONSP (XCAR (map))
21968 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21969 return XCAR (map);
21970 map = XCDR (map);
21973 return Qnil;
21976 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21977 3, 3, 0,
21978 doc: /* Lookup in image map MAP coordinates X and Y.
21979 An image map is an alist where each element has the format (AREA ID PLIST).
21980 An AREA is specified as either a rectangle, a circle, or a polygon:
21981 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21982 pixel coordinates of the upper left and bottom right corners.
21983 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21984 and the radius of the circle; r may be a float or integer.
21985 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21986 vector describes one corner in the polygon.
21987 Returns the alist element for the first matching AREA in MAP. */)
21988 (map, x, y)
21989 Lisp_Object map;
21990 Lisp_Object x, y;
21992 if (NILP (map))
21993 return Qnil;
21995 CHECK_NUMBER (x);
21996 CHECK_NUMBER (y);
21998 return find_hot_spot (map, XINT (x), XINT (y));
22002 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22003 static void
22004 define_frame_cursor1 (f, cursor, pointer)
22005 struct frame *f;
22006 Cursor cursor;
22007 Lisp_Object pointer;
22009 /* Do not change cursor shape while dragging mouse. */
22010 if (!NILP (do_mouse_tracking))
22011 return;
22013 if (!NILP (pointer))
22015 if (EQ (pointer, Qarrow))
22016 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22017 else if (EQ (pointer, Qhand))
22018 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22019 else if (EQ (pointer, Qtext))
22020 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22021 else if (EQ (pointer, intern ("hdrag")))
22022 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22023 #ifdef HAVE_X_WINDOWS
22024 else if (EQ (pointer, intern ("vdrag")))
22025 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22026 #endif
22027 else if (EQ (pointer, intern ("hourglass")))
22028 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22029 else if (EQ (pointer, Qmodeline))
22030 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22031 else
22032 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22035 if (cursor != No_Cursor)
22036 rif->define_frame_cursor (f, cursor);
22039 /* Take proper action when mouse has moved to the mode or header line
22040 or marginal area AREA of window W, x-position X and y-position Y.
22041 X is relative to the start of the text display area of W, so the
22042 width of bitmap areas and scroll bars must be subtracted to get a
22043 position relative to the start of the mode line. */
22045 static void
22046 note_mode_line_or_margin_highlight (window, x, y, area)
22047 Lisp_Object window;
22048 int x, y;
22049 enum window_part area;
22051 struct window *w = XWINDOW (window);
22052 struct frame *f = XFRAME (w->frame);
22053 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22054 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22055 Lisp_Object pointer = Qnil;
22056 int charpos, dx, dy, width, height;
22057 Lisp_Object string, object = Qnil;
22058 Lisp_Object pos, help;
22060 Lisp_Object mouse_face;
22061 int original_x_pixel = x;
22062 struct glyph * glyph = NULL;
22063 struct glyph_row *row;
22065 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22067 int x0;
22068 struct glyph *end;
22070 string = mode_line_string (w, area, &x, &y, &charpos,
22071 &object, &dx, &dy, &width, &height);
22073 row = (area == ON_MODE_LINE
22074 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22075 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22077 /* Find glyph */
22078 if (row->mode_line_p && row->enabled_p)
22080 glyph = row->glyphs[TEXT_AREA];
22081 end = glyph + row->used[TEXT_AREA];
22083 for (x0 = original_x_pixel;
22084 glyph < end && x0 >= glyph->pixel_width;
22085 ++glyph)
22086 x0 -= glyph->pixel_width;
22088 if (glyph >= end)
22089 glyph = NULL;
22092 else
22094 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22095 string = marginal_area_string (w, area, &x, &y, &charpos,
22096 &object, &dx, &dy, &width, &height);
22099 help = Qnil;
22101 if (IMAGEP (object))
22103 Lisp_Object image_map, hotspot;
22104 if ((image_map = Fplist_get (XCDR (object), QCmap),
22105 !NILP (image_map))
22106 && (hotspot = find_hot_spot (image_map, dx, dy),
22107 CONSP (hotspot))
22108 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22110 Lisp_Object area_id, plist;
22112 area_id = XCAR (hotspot);
22113 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22114 If so, we could look for mouse-enter, mouse-leave
22115 properties in PLIST (and do something...). */
22116 hotspot = XCDR (hotspot);
22117 if (CONSP (hotspot)
22118 && (plist = XCAR (hotspot), CONSP (plist)))
22120 pointer = Fplist_get (plist, Qpointer);
22121 if (NILP (pointer))
22122 pointer = Qhand;
22123 help = Fplist_get (plist, Qhelp_echo);
22124 if (!NILP (help))
22126 help_echo_string = help;
22127 /* Is this correct? ++kfs */
22128 XSETWINDOW (help_echo_window, w);
22129 help_echo_object = w->buffer;
22130 help_echo_pos = charpos;
22134 if (NILP (pointer))
22135 pointer = Fplist_get (XCDR (object), QCpointer);
22138 if (STRINGP (string))
22140 pos = make_number (charpos);
22141 /* If we're on a string with `help-echo' text property, arrange
22142 for the help to be displayed. This is done by setting the
22143 global variable help_echo_string to the help string. */
22144 if (NILP (help))
22146 help = Fget_text_property (pos, Qhelp_echo, string);
22147 if (!NILP (help))
22149 help_echo_string = help;
22150 XSETWINDOW (help_echo_window, w);
22151 help_echo_object = string;
22152 help_echo_pos = charpos;
22156 if (NILP (pointer))
22157 pointer = Fget_text_property (pos, Qpointer, string);
22159 /* Change the mouse pointer according to what is under X/Y. */
22160 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22162 Lisp_Object map;
22163 map = Fget_text_property (pos, Qlocal_map, string);
22164 if (!KEYMAPP (map))
22165 map = Fget_text_property (pos, Qkeymap, string);
22166 if (!KEYMAPP (map))
22167 cursor = dpyinfo->vertical_scroll_bar_cursor;
22170 /* Change the mouse face according to what is under X/Y. */
22171 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22172 if (!NILP (mouse_face)
22173 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22174 && glyph)
22176 Lisp_Object b, e;
22178 struct glyph * tmp_glyph;
22180 int gpos;
22181 int gseq_length;
22182 int total_pixel_width;
22183 int ignore;
22185 int vpos, hpos;
22187 b = Fprevious_single_property_change (make_number (charpos + 1),
22188 Qmouse_face, string, Qnil);
22189 if (NILP (b))
22190 b = make_number (0);
22192 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22193 if (NILP (e))
22194 e = make_number (SCHARS (string));
22196 /* Calculate the position(glyph position: GPOS) of GLYPH in
22197 displayed string. GPOS is different from CHARPOS.
22199 CHARPOS is the position of glyph in internal string
22200 object. A mode line string format has structures which
22201 is converted to a flatten by emacs lisp interpreter.
22202 The internal string is an element of the structures.
22203 The displayed string is the flatten string. */
22204 for (tmp_glyph = glyph - 1, gpos = 0;
22205 tmp_glyph->charpos >= XINT (b);
22206 tmp_glyph--, gpos++)
22208 if (!EQ (tmp_glyph->object, glyph->object))
22209 break;
22212 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22213 displayed string holding GLYPH.
22215 GSEQ_LENGTH is different from SCHARS (STRING).
22216 SCHARS (STRING) returns the length of the internal string. */
22217 for (tmp_glyph = glyph, gseq_length = gpos;
22218 tmp_glyph->charpos < XINT (e);
22219 tmp_glyph++, gseq_length++)
22221 if (!EQ (tmp_glyph->object, glyph->object))
22222 break;
22225 total_pixel_width = 0;
22226 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22227 total_pixel_width += tmp_glyph->pixel_width;
22229 /* Pre calculation of re-rendering position */
22230 vpos = (x - gpos);
22231 hpos = (area == ON_MODE_LINE
22232 ? (w->current_matrix)->nrows - 1
22233 : 0);
22235 /* If the re-rendering position is included in the last
22236 re-rendering area, we should do nothing. */
22237 if ( EQ (window, dpyinfo->mouse_face_window)
22238 && dpyinfo->mouse_face_beg_col <= vpos
22239 && vpos < dpyinfo->mouse_face_end_col
22240 && dpyinfo->mouse_face_beg_row == hpos )
22241 return;
22243 if (clear_mouse_face (dpyinfo))
22244 cursor = No_Cursor;
22246 dpyinfo->mouse_face_beg_col = vpos;
22247 dpyinfo->mouse_face_beg_row = hpos;
22249 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22250 dpyinfo->mouse_face_beg_y = 0;
22252 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22253 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22255 dpyinfo->mouse_face_end_x = 0;
22256 dpyinfo->mouse_face_end_y = 0;
22258 dpyinfo->mouse_face_past_end = 0;
22259 dpyinfo->mouse_face_window = window;
22261 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22262 charpos,
22263 0, 0, 0, &ignore,
22264 glyph->face_id, 1);
22265 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22267 if (NILP (pointer))
22268 pointer = Qhand;
22270 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22271 clear_mouse_face (dpyinfo);
22273 define_frame_cursor1 (f, cursor, pointer);
22277 /* EXPORT:
22278 Take proper action when the mouse has moved to position X, Y on
22279 frame F as regards highlighting characters that have mouse-face
22280 properties. Also de-highlighting chars where the mouse was before.
22281 X and Y can be negative or out of range. */
22283 void
22284 note_mouse_highlight (f, x, y)
22285 struct frame *f;
22286 int x, y;
22288 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22289 enum window_part part;
22290 Lisp_Object window;
22291 struct window *w;
22292 Cursor cursor = No_Cursor;
22293 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22294 struct buffer *b;
22296 /* When a menu is active, don't highlight because this looks odd. */
22297 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22298 if (popup_activated ())
22299 return;
22300 #endif
22302 if (NILP (Vmouse_highlight)
22303 || !f->glyphs_initialized_p)
22304 return;
22306 dpyinfo->mouse_face_mouse_x = x;
22307 dpyinfo->mouse_face_mouse_y = y;
22308 dpyinfo->mouse_face_mouse_frame = f;
22310 if (dpyinfo->mouse_face_defer)
22311 return;
22313 if (gc_in_progress)
22315 dpyinfo->mouse_face_deferred_gc = 1;
22316 return;
22319 /* Which window is that in? */
22320 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22322 /* If we were displaying active text in another window, clear that.
22323 Also clear if we move out of text area in same window. */
22324 if (! EQ (window, dpyinfo->mouse_face_window)
22325 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22326 && !NILP (dpyinfo->mouse_face_window)))
22327 clear_mouse_face (dpyinfo);
22329 /* Not on a window -> return. */
22330 if (!WINDOWP (window))
22331 return;
22333 /* Reset help_echo_string. It will get recomputed below. */
22334 help_echo_string = Qnil;
22336 /* Convert to window-relative pixel coordinates. */
22337 w = XWINDOW (window);
22338 frame_to_window_pixel_xy (w, &x, &y);
22340 /* Handle tool-bar window differently since it doesn't display a
22341 buffer. */
22342 if (EQ (window, f->tool_bar_window))
22344 note_tool_bar_highlight (f, x, y);
22345 return;
22348 /* Mouse is on the mode, header line or margin? */
22349 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22350 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22352 note_mode_line_or_margin_highlight (window, x, y, part);
22353 return;
22356 if (part == ON_VERTICAL_BORDER)
22357 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22358 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22359 || part == ON_SCROLL_BAR)
22360 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22361 else
22362 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22364 /* Are we in a window whose display is up to date?
22365 And verify the buffer's text has not changed. */
22366 b = XBUFFER (w->buffer);
22367 if (part == ON_TEXT
22368 && EQ (w->window_end_valid, w->buffer)
22369 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22370 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22372 int hpos, vpos, pos, i, dx, dy, area;
22373 struct glyph *glyph;
22374 Lisp_Object object;
22375 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22376 Lisp_Object *overlay_vec = NULL;
22377 int noverlays;
22378 struct buffer *obuf;
22379 int obegv, ozv, same_region;
22381 /* Find the glyph under X/Y. */
22382 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22384 /* Look for :pointer property on image. */
22385 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22387 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22388 if (img != NULL && IMAGEP (img->spec))
22390 Lisp_Object image_map, hotspot;
22391 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22392 !NILP (image_map))
22393 && (hotspot = find_hot_spot (image_map,
22394 glyph->slice.x + dx,
22395 glyph->slice.y + dy),
22396 CONSP (hotspot))
22397 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22399 Lisp_Object area_id, plist;
22401 area_id = XCAR (hotspot);
22402 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22403 If so, we could look for mouse-enter, mouse-leave
22404 properties in PLIST (and do something...). */
22405 hotspot = XCDR (hotspot);
22406 if (CONSP (hotspot)
22407 && (plist = XCAR (hotspot), CONSP (plist)))
22409 pointer = Fplist_get (plist, Qpointer);
22410 if (NILP (pointer))
22411 pointer = Qhand;
22412 help_echo_string = Fplist_get (plist, Qhelp_echo);
22413 if (!NILP (help_echo_string))
22415 help_echo_window = window;
22416 help_echo_object = glyph->object;
22417 help_echo_pos = glyph->charpos;
22421 if (NILP (pointer))
22422 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22426 /* Clear mouse face if X/Y not over text. */
22427 if (glyph == NULL
22428 || area != TEXT_AREA
22429 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22431 if (clear_mouse_face (dpyinfo))
22432 cursor = No_Cursor;
22433 if (NILP (pointer))
22435 if (area != TEXT_AREA)
22436 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22437 else
22438 pointer = Vvoid_text_area_pointer;
22440 goto set_cursor;
22443 pos = glyph->charpos;
22444 object = glyph->object;
22445 if (!STRINGP (object) && !BUFFERP (object))
22446 goto set_cursor;
22448 /* If we get an out-of-range value, return now; avoid an error. */
22449 if (BUFFERP (object) && pos > BUF_Z (b))
22450 goto set_cursor;
22452 /* Make the window's buffer temporarily current for
22453 overlays_at and compute_char_face. */
22454 obuf = current_buffer;
22455 current_buffer = b;
22456 obegv = BEGV;
22457 ozv = ZV;
22458 BEGV = BEG;
22459 ZV = Z;
22461 /* Is this char mouse-active or does it have help-echo? */
22462 position = make_number (pos);
22464 if (BUFFERP (object))
22466 /* Put all the overlays we want in a vector in overlay_vec. */
22467 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22468 /* Sort overlays into increasing priority order. */
22469 noverlays = sort_overlays (overlay_vec, noverlays, w);
22471 else
22472 noverlays = 0;
22474 same_region = (EQ (window, dpyinfo->mouse_face_window)
22475 && vpos >= dpyinfo->mouse_face_beg_row
22476 && vpos <= dpyinfo->mouse_face_end_row
22477 && (vpos > dpyinfo->mouse_face_beg_row
22478 || hpos >= dpyinfo->mouse_face_beg_col)
22479 && (vpos < dpyinfo->mouse_face_end_row
22480 || hpos < dpyinfo->mouse_face_end_col
22481 || dpyinfo->mouse_face_past_end));
22483 if (same_region)
22484 cursor = No_Cursor;
22486 /* Check mouse-face highlighting. */
22487 if (! same_region
22488 /* If there exists an overlay with mouse-face overlapping
22489 the one we are currently highlighting, we have to
22490 check if we enter the overlapping overlay, and then
22491 highlight only that. */
22492 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22493 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22495 /* Find the highest priority overlay that has a mouse-face
22496 property. */
22497 overlay = Qnil;
22498 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22500 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22501 if (!NILP (mouse_face))
22502 overlay = overlay_vec[i];
22505 /* If we're actually highlighting the same overlay as
22506 before, there's no need to do that again. */
22507 if (!NILP (overlay)
22508 && EQ (overlay, dpyinfo->mouse_face_overlay))
22509 goto check_help_echo;
22511 dpyinfo->mouse_face_overlay = overlay;
22513 /* Clear the display of the old active region, if any. */
22514 if (clear_mouse_face (dpyinfo))
22515 cursor = No_Cursor;
22517 /* If no overlay applies, get a text property. */
22518 if (NILP (overlay))
22519 mouse_face = Fget_text_property (position, Qmouse_face, object);
22521 /* Handle the overlay case. */
22522 if (!NILP (overlay))
22524 /* Find the range of text around this char that
22525 should be active. */
22526 Lisp_Object before, after;
22527 int ignore;
22529 before = Foverlay_start (overlay);
22530 after = Foverlay_end (overlay);
22531 /* Record this as the current active region. */
22532 fast_find_position (w, XFASTINT (before),
22533 &dpyinfo->mouse_face_beg_col,
22534 &dpyinfo->mouse_face_beg_row,
22535 &dpyinfo->mouse_face_beg_x,
22536 &dpyinfo->mouse_face_beg_y, Qnil);
22538 dpyinfo->mouse_face_past_end
22539 = !fast_find_position (w, XFASTINT (after),
22540 &dpyinfo->mouse_face_end_col,
22541 &dpyinfo->mouse_face_end_row,
22542 &dpyinfo->mouse_face_end_x,
22543 &dpyinfo->mouse_face_end_y, Qnil);
22544 dpyinfo->mouse_face_window = window;
22546 dpyinfo->mouse_face_face_id
22547 = face_at_buffer_position (w, pos, 0, 0,
22548 &ignore, pos + 1,
22549 !dpyinfo->mouse_face_hidden);
22551 /* Display it as active. */
22552 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22553 cursor = No_Cursor;
22555 /* Handle the text property case. */
22556 else if (!NILP (mouse_face) && BUFFERP (object))
22558 /* Find the range of text around this char that
22559 should be active. */
22560 Lisp_Object before, after, beginning, end;
22561 int ignore;
22563 beginning = Fmarker_position (w->start);
22564 end = make_number (BUF_Z (XBUFFER (object))
22565 - XFASTINT (w->window_end_pos));
22566 before
22567 = Fprevious_single_property_change (make_number (pos + 1),
22568 Qmouse_face,
22569 object, beginning);
22570 after
22571 = Fnext_single_property_change (position, Qmouse_face,
22572 object, end);
22574 /* Record this as the current active region. */
22575 fast_find_position (w, XFASTINT (before),
22576 &dpyinfo->mouse_face_beg_col,
22577 &dpyinfo->mouse_face_beg_row,
22578 &dpyinfo->mouse_face_beg_x,
22579 &dpyinfo->mouse_face_beg_y, Qnil);
22580 dpyinfo->mouse_face_past_end
22581 = !fast_find_position (w, XFASTINT (after),
22582 &dpyinfo->mouse_face_end_col,
22583 &dpyinfo->mouse_face_end_row,
22584 &dpyinfo->mouse_face_end_x,
22585 &dpyinfo->mouse_face_end_y, Qnil);
22586 dpyinfo->mouse_face_window = window;
22588 if (BUFFERP (object))
22589 dpyinfo->mouse_face_face_id
22590 = face_at_buffer_position (w, pos, 0, 0,
22591 &ignore, pos + 1,
22592 !dpyinfo->mouse_face_hidden);
22594 /* Display it as active. */
22595 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22596 cursor = No_Cursor;
22598 else if (!NILP (mouse_face) && STRINGP (object))
22600 Lisp_Object b, e;
22601 int ignore;
22603 b = Fprevious_single_property_change (make_number (pos + 1),
22604 Qmouse_face,
22605 object, Qnil);
22606 e = Fnext_single_property_change (position, Qmouse_face,
22607 object, Qnil);
22608 if (NILP (b))
22609 b = make_number (0);
22610 if (NILP (e))
22611 e = make_number (SCHARS (object) - 1);
22613 fast_find_string_pos (w, XINT (b), object,
22614 &dpyinfo->mouse_face_beg_col,
22615 &dpyinfo->mouse_face_beg_row,
22616 &dpyinfo->mouse_face_beg_x,
22617 &dpyinfo->mouse_face_beg_y, 0);
22618 fast_find_string_pos (w, XINT (e), object,
22619 &dpyinfo->mouse_face_end_col,
22620 &dpyinfo->mouse_face_end_row,
22621 &dpyinfo->mouse_face_end_x,
22622 &dpyinfo->mouse_face_end_y, 1);
22623 dpyinfo->mouse_face_past_end = 0;
22624 dpyinfo->mouse_face_window = window;
22625 dpyinfo->mouse_face_face_id
22626 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22627 glyph->face_id, 1);
22628 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22629 cursor = No_Cursor;
22631 else if (STRINGP (object) && NILP (mouse_face))
22633 /* A string which doesn't have mouse-face, but
22634 the text ``under'' it might have. */
22635 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22636 int start = MATRIX_ROW_START_CHARPOS (r);
22638 pos = string_buffer_position (w, object, start);
22639 if (pos > 0)
22640 mouse_face = get_char_property_and_overlay (make_number (pos),
22641 Qmouse_face,
22642 w->buffer,
22643 &overlay);
22644 if (!NILP (mouse_face) && !NILP (overlay))
22646 Lisp_Object before = Foverlay_start (overlay);
22647 Lisp_Object after = Foverlay_end (overlay);
22648 int ignore;
22650 /* Note that we might not be able to find position
22651 BEFORE in the glyph matrix if the overlay is
22652 entirely covered by a `display' property. In
22653 this case, we overshoot. So let's stop in
22654 the glyph matrix before glyphs for OBJECT. */
22655 fast_find_position (w, XFASTINT (before),
22656 &dpyinfo->mouse_face_beg_col,
22657 &dpyinfo->mouse_face_beg_row,
22658 &dpyinfo->mouse_face_beg_x,
22659 &dpyinfo->mouse_face_beg_y,
22660 object);
22662 dpyinfo->mouse_face_past_end
22663 = !fast_find_position (w, XFASTINT (after),
22664 &dpyinfo->mouse_face_end_col,
22665 &dpyinfo->mouse_face_end_row,
22666 &dpyinfo->mouse_face_end_x,
22667 &dpyinfo->mouse_face_end_y,
22668 Qnil);
22669 dpyinfo->mouse_face_window = window;
22670 dpyinfo->mouse_face_face_id
22671 = face_at_buffer_position (w, pos, 0, 0,
22672 &ignore, pos + 1,
22673 !dpyinfo->mouse_face_hidden);
22675 /* Display it as active. */
22676 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22677 cursor = No_Cursor;
22682 check_help_echo:
22684 /* Look for a `help-echo' property. */
22685 if (NILP (help_echo_string)) {
22686 Lisp_Object help, overlay;
22688 /* Check overlays first. */
22689 help = overlay = Qnil;
22690 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22692 overlay = overlay_vec[i];
22693 help = Foverlay_get (overlay, Qhelp_echo);
22696 if (!NILP (help))
22698 help_echo_string = help;
22699 help_echo_window = window;
22700 help_echo_object = overlay;
22701 help_echo_pos = pos;
22703 else
22705 Lisp_Object object = glyph->object;
22706 int charpos = glyph->charpos;
22708 /* Try text properties. */
22709 if (STRINGP (object)
22710 && charpos >= 0
22711 && charpos < SCHARS (object))
22713 help = Fget_text_property (make_number (charpos),
22714 Qhelp_echo, object);
22715 if (NILP (help))
22717 /* If the string itself doesn't specify a help-echo,
22718 see if the buffer text ``under'' it does. */
22719 struct glyph_row *r
22720 = MATRIX_ROW (w->current_matrix, vpos);
22721 int start = MATRIX_ROW_START_CHARPOS (r);
22722 int pos = string_buffer_position (w, object, start);
22723 if (pos > 0)
22725 help = Fget_char_property (make_number (pos),
22726 Qhelp_echo, w->buffer);
22727 if (!NILP (help))
22729 charpos = pos;
22730 object = w->buffer;
22735 else if (BUFFERP (object)
22736 && charpos >= BEGV
22737 && charpos < ZV)
22738 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22739 object);
22741 if (!NILP (help))
22743 help_echo_string = help;
22744 help_echo_window = window;
22745 help_echo_object = object;
22746 help_echo_pos = charpos;
22751 /* Look for a `pointer' property. */
22752 if (NILP (pointer))
22754 /* Check overlays first. */
22755 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22756 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22758 if (NILP (pointer))
22760 Lisp_Object object = glyph->object;
22761 int charpos = glyph->charpos;
22763 /* Try text properties. */
22764 if (STRINGP (object)
22765 && charpos >= 0
22766 && charpos < SCHARS (object))
22768 pointer = Fget_text_property (make_number (charpos),
22769 Qpointer, object);
22770 if (NILP (pointer))
22772 /* If the string itself doesn't specify a pointer,
22773 see if the buffer text ``under'' it does. */
22774 struct glyph_row *r
22775 = MATRIX_ROW (w->current_matrix, vpos);
22776 int start = MATRIX_ROW_START_CHARPOS (r);
22777 int pos = string_buffer_position (w, object, start);
22778 if (pos > 0)
22779 pointer = Fget_char_property (make_number (pos),
22780 Qpointer, w->buffer);
22783 else if (BUFFERP (object)
22784 && charpos >= BEGV
22785 && charpos < ZV)
22786 pointer = Fget_text_property (make_number (charpos),
22787 Qpointer, object);
22791 BEGV = obegv;
22792 ZV = ozv;
22793 current_buffer = obuf;
22796 set_cursor:
22798 define_frame_cursor1 (f, cursor, pointer);
22802 /* EXPORT for RIF:
22803 Clear any mouse-face on window W. This function is part of the
22804 redisplay interface, and is called from try_window_id and similar
22805 functions to ensure the mouse-highlight is off. */
22807 void
22808 x_clear_window_mouse_face (w)
22809 struct window *w;
22811 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22812 Lisp_Object window;
22814 BLOCK_INPUT;
22815 XSETWINDOW (window, w);
22816 if (EQ (window, dpyinfo->mouse_face_window))
22817 clear_mouse_face (dpyinfo);
22818 UNBLOCK_INPUT;
22822 /* EXPORT:
22823 Just discard the mouse face information for frame F, if any.
22824 This is used when the size of F is changed. */
22826 void
22827 cancel_mouse_face (f)
22828 struct frame *f;
22830 Lisp_Object window;
22831 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22833 window = dpyinfo->mouse_face_window;
22834 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22836 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22837 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22838 dpyinfo->mouse_face_window = Qnil;
22843 #endif /* HAVE_WINDOW_SYSTEM */
22846 /***********************************************************************
22847 Exposure Events
22848 ***********************************************************************/
22850 #ifdef HAVE_WINDOW_SYSTEM
22852 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22853 which intersects rectangle R. R is in window-relative coordinates. */
22855 static void
22856 expose_area (w, row, r, area)
22857 struct window *w;
22858 struct glyph_row *row;
22859 XRectangle *r;
22860 enum glyph_row_area area;
22862 struct glyph *first = row->glyphs[area];
22863 struct glyph *end = row->glyphs[area] + row->used[area];
22864 struct glyph *last;
22865 int first_x, start_x, x;
22867 if (area == TEXT_AREA && row->fill_line_p)
22868 /* If row extends face to end of line write the whole line. */
22869 draw_glyphs (w, 0, row, area,
22870 0, row->used[area],
22871 DRAW_NORMAL_TEXT, 0);
22872 else
22874 /* Set START_X to the window-relative start position for drawing glyphs of
22875 AREA. The first glyph of the text area can be partially visible.
22876 The first glyphs of other areas cannot. */
22877 start_x = window_box_left_offset (w, area);
22878 x = start_x;
22879 if (area == TEXT_AREA)
22880 x += row->x;
22882 /* Find the first glyph that must be redrawn. */
22883 while (first < end
22884 && x + first->pixel_width < r->x)
22886 x += first->pixel_width;
22887 ++first;
22890 /* Find the last one. */
22891 last = first;
22892 first_x = x;
22893 while (last < end
22894 && x < r->x + r->width)
22896 x += last->pixel_width;
22897 ++last;
22900 /* Repaint. */
22901 if (last > first)
22902 draw_glyphs (w, first_x - start_x, row, area,
22903 first - row->glyphs[area], last - row->glyphs[area],
22904 DRAW_NORMAL_TEXT, 0);
22909 /* Redraw the parts of the glyph row ROW on window W intersecting
22910 rectangle R. R is in window-relative coordinates. Value is
22911 non-zero if mouse-face was overwritten. */
22913 static int
22914 expose_line (w, row, r)
22915 struct window *w;
22916 struct glyph_row *row;
22917 XRectangle *r;
22919 xassert (row->enabled_p);
22921 if (row->mode_line_p || w->pseudo_window_p)
22922 draw_glyphs (w, 0, row, TEXT_AREA,
22923 0, row->used[TEXT_AREA],
22924 DRAW_NORMAL_TEXT, 0);
22925 else
22927 if (row->used[LEFT_MARGIN_AREA])
22928 expose_area (w, row, r, LEFT_MARGIN_AREA);
22929 if (row->used[TEXT_AREA])
22930 expose_area (w, row, r, TEXT_AREA);
22931 if (row->used[RIGHT_MARGIN_AREA])
22932 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22933 draw_row_fringe_bitmaps (w, row);
22936 return row->mouse_face_p;
22940 /* Redraw those parts of glyphs rows during expose event handling that
22941 overlap other rows. Redrawing of an exposed line writes over parts
22942 of lines overlapping that exposed line; this function fixes that.
22944 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22945 row in W's current matrix that is exposed and overlaps other rows.
22946 LAST_OVERLAPPING_ROW is the last such row. */
22948 static void
22949 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22950 struct window *w;
22951 struct glyph_row *first_overlapping_row;
22952 struct glyph_row *last_overlapping_row;
22954 struct glyph_row *row;
22956 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22957 if (row->overlapping_p)
22959 xassert (row->enabled_p && !row->mode_line_p);
22961 if (row->used[LEFT_MARGIN_AREA])
22962 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22964 if (row->used[TEXT_AREA])
22965 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22967 if (row->used[RIGHT_MARGIN_AREA])
22968 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22973 /* Return non-zero if W's cursor intersects rectangle R. */
22975 static int
22976 phys_cursor_in_rect_p (w, r)
22977 struct window *w;
22978 XRectangle *r;
22980 XRectangle cr, result;
22981 struct glyph *cursor_glyph;
22983 cursor_glyph = get_phys_cursor_glyph (w);
22984 if (cursor_glyph)
22986 /* r is relative to W's box, but w->phys_cursor.x is relative
22987 to left edge of W's TEXT area. Adjust it. */
22988 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22989 cr.y = w->phys_cursor.y;
22990 cr.width = cursor_glyph->pixel_width;
22991 cr.height = w->phys_cursor_height;
22992 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22993 I assume the effect is the same -- and this is portable. */
22994 return x_intersect_rectangles (&cr, r, &result);
22996 else
22997 return 0;
23001 /* EXPORT:
23002 Draw a vertical window border to the right of window W if W doesn't
23003 have vertical scroll bars. */
23005 void
23006 x_draw_vertical_border (w)
23007 struct window *w;
23009 /* We could do better, if we knew what type of scroll-bar the adjacent
23010 windows (on either side) have... But we don't :-(
23011 However, I think this works ok. ++KFS 2003-04-25 */
23013 /* Redraw borders between horizontally adjacent windows. Don't
23014 do it for frames with vertical scroll bars because either the
23015 right scroll bar of a window, or the left scroll bar of its
23016 neighbor will suffice as a border. */
23017 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23018 return;
23020 if (!WINDOW_RIGHTMOST_P (w)
23021 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23023 int x0, x1, y0, y1;
23025 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23026 y1 -= 1;
23028 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23029 x1 -= 1;
23031 rif->draw_vertical_window_border (w, x1, y0, y1);
23033 else if (!WINDOW_LEFTMOST_P (w)
23034 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23036 int x0, x1, y0, y1;
23038 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23039 y1 -= 1;
23041 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23042 x0 -= 1;
23044 rif->draw_vertical_window_border (w, x0, y0, y1);
23049 /* Redraw the part of window W intersection rectangle FR. Pixel
23050 coordinates in FR are frame-relative. Call this function with
23051 input blocked. Value is non-zero if the exposure overwrites
23052 mouse-face. */
23054 static int
23055 expose_window (w, fr)
23056 struct window *w;
23057 XRectangle *fr;
23059 struct frame *f = XFRAME (w->frame);
23060 XRectangle wr, r;
23061 int mouse_face_overwritten_p = 0;
23063 /* If window is not yet fully initialized, do nothing. This can
23064 happen when toolkit scroll bars are used and a window is split.
23065 Reconfiguring the scroll bar will generate an expose for a newly
23066 created window. */
23067 if (w->current_matrix == NULL)
23068 return 0;
23070 /* When we're currently updating the window, display and current
23071 matrix usually don't agree. Arrange for a thorough display
23072 later. */
23073 if (w == updated_window)
23075 SET_FRAME_GARBAGED (f);
23076 return 0;
23079 /* Frame-relative pixel rectangle of W. */
23080 wr.x = WINDOW_LEFT_EDGE_X (w);
23081 wr.y = WINDOW_TOP_EDGE_Y (w);
23082 wr.width = WINDOW_TOTAL_WIDTH (w);
23083 wr.height = WINDOW_TOTAL_HEIGHT (w);
23085 if (x_intersect_rectangles (fr, &wr, &r))
23087 int yb = window_text_bottom_y (w);
23088 struct glyph_row *row;
23089 int cursor_cleared_p;
23090 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23092 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23093 r.x, r.y, r.width, r.height));
23095 /* Convert to window coordinates. */
23096 r.x -= WINDOW_LEFT_EDGE_X (w);
23097 r.y -= WINDOW_TOP_EDGE_Y (w);
23099 /* Turn off the cursor. */
23100 if (!w->pseudo_window_p
23101 && phys_cursor_in_rect_p (w, &r))
23103 x_clear_cursor (w);
23104 cursor_cleared_p = 1;
23106 else
23107 cursor_cleared_p = 0;
23109 /* Update lines intersecting rectangle R. */
23110 first_overlapping_row = last_overlapping_row = NULL;
23111 for (row = w->current_matrix->rows;
23112 row->enabled_p;
23113 ++row)
23115 int y0 = row->y;
23116 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23118 if ((y0 >= r.y && y0 < r.y + r.height)
23119 || (y1 > r.y && y1 < r.y + r.height)
23120 || (r.y >= y0 && r.y < y1)
23121 || (r.y + r.height > y0 && r.y + r.height < y1))
23123 /* A header line may be overlapping, but there is no need
23124 to fix overlapping areas for them. KFS 2005-02-12 */
23125 if (row->overlapping_p && !row->mode_line_p)
23127 if (first_overlapping_row == NULL)
23128 first_overlapping_row = row;
23129 last_overlapping_row = row;
23132 if (expose_line (w, row, &r))
23133 mouse_face_overwritten_p = 1;
23136 if (y1 >= yb)
23137 break;
23140 /* Display the mode line if there is one. */
23141 if (WINDOW_WANTS_MODELINE_P (w)
23142 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23143 row->enabled_p)
23144 && row->y < r.y + r.height)
23146 if (expose_line (w, row, &r))
23147 mouse_face_overwritten_p = 1;
23150 if (!w->pseudo_window_p)
23152 /* Fix the display of overlapping rows. */
23153 if (first_overlapping_row)
23154 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23156 /* Draw border between windows. */
23157 x_draw_vertical_border (w);
23159 /* Turn the cursor on again. */
23160 if (cursor_cleared_p)
23161 update_window_cursor (w, 1);
23165 return mouse_face_overwritten_p;
23170 /* Redraw (parts) of all windows in the window tree rooted at W that
23171 intersect R. R contains frame pixel coordinates. Value is
23172 non-zero if the exposure overwrites mouse-face. */
23174 static int
23175 expose_window_tree (w, r)
23176 struct window *w;
23177 XRectangle *r;
23179 struct frame *f = XFRAME (w->frame);
23180 int mouse_face_overwritten_p = 0;
23182 while (w && !FRAME_GARBAGED_P (f))
23184 if (!NILP (w->hchild))
23185 mouse_face_overwritten_p
23186 |= expose_window_tree (XWINDOW (w->hchild), r);
23187 else if (!NILP (w->vchild))
23188 mouse_face_overwritten_p
23189 |= expose_window_tree (XWINDOW (w->vchild), r);
23190 else
23191 mouse_face_overwritten_p |= expose_window (w, r);
23193 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23196 return mouse_face_overwritten_p;
23200 /* EXPORT:
23201 Redisplay an exposed area of frame F. X and Y are the upper-left
23202 corner of the exposed rectangle. W and H are width and height of
23203 the exposed area. All are pixel values. W or H zero means redraw
23204 the entire frame. */
23206 void
23207 expose_frame (f, x, y, w, h)
23208 struct frame *f;
23209 int x, y, w, h;
23211 XRectangle r;
23212 int mouse_face_overwritten_p = 0;
23214 TRACE ((stderr, "expose_frame "));
23216 /* No need to redraw if frame will be redrawn soon. */
23217 if (FRAME_GARBAGED_P (f))
23219 TRACE ((stderr, " garbaged\n"));
23220 return;
23223 /* If basic faces haven't been realized yet, there is no point in
23224 trying to redraw anything. This can happen when we get an expose
23225 event while Emacs is starting, e.g. by moving another window. */
23226 if (FRAME_FACE_CACHE (f) == NULL
23227 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23229 TRACE ((stderr, " no faces\n"));
23230 return;
23233 if (w == 0 || h == 0)
23235 r.x = r.y = 0;
23236 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23237 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23239 else
23241 r.x = x;
23242 r.y = y;
23243 r.width = w;
23244 r.height = h;
23247 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23248 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23250 if (WINDOWP (f->tool_bar_window))
23251 mouse_face_overwritten_p
23252 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23254 #ifdef HAVE_X_WINDOWS
23255 #ifndef MSDOS
23256 #ifndef USE_X_TOOLKIT
23257 if (WINDOWP (f->menu_bar_window))
23258 mouse_face_overwritten_p
23259 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23260 #endif /* not USE_X_TOOLKIT */
23261 #endif
23262 #endif
23264 /* Some window managers support a focus-follows-mouse style with
23265 delayed raising of frames. Imagine a partially obscured frame,
23266 and moving the mouse into partially obscured mouse-face on that
23267 frame. The visible part of the mouse-face will be highlighted,
23268 then the WM raises the obscured frame. With at least one WM, KDE
23269 2.1, Emacs is not getting any event for the raising of the frame
23270 (even tried with SubstructureRedirectMask), only Expose events.
23271 These expose events will draw text normally, i.e. not
23272 highlighted. Which means we must redo the highlight here.
23273 Subsume it under ``we love X''. --gerd 2001-08-15 */
23274 /* Included in Windows version because Windows most likely does not
23275 do the right thing if any third party tool offers
23276 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23277 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23279 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23280 if (f == dpyinfo->mouse_face_mouse_frame)
23282 int x = dpyinfo->mouse_face_mouse_x;
23283 int y = dpyinfo->mouse_face_mouse_y;
23284 clear_mouse_face (dpyinfo);
23285 note_mouse_highlight (f, x, y);
23291 /* EXPORT:
23292 Determine the intersection of two rectangles R1 and R2. Return
23293 the intersection in *RESULT. Value is non-zero if RESULT is not
23294 empty. */
23297 x_intersect_rectangles (r1, r2, result)
23298 XRectangle *r1, *r2, *result;
23300 XRectangle *left, *right;
23301 XRectangle *upper, *lower;
23302 int intersection_p = 0;
23304 /* Rearrange so that R1 is the left-most rectangle. */
23305 if (r1->x < r2->x)
23306 left = r1, right = r2;
23307 else
23308 left = r2, right = r1;
23310 /* X0 of the intersection is right.x0, if this is inside R1,
23311 otherwise there is no intersection. */
23312 if (right->x <= left->x + left->width)
23314 result->x = right->x;
23316 /* The right end of the intersection is the minimum of the
23317 the right ends of left and right. */
23318 result->width = (min (left->x + left->width, right->x + right->width)
23319 - result->x);
23321 /* Same game for Y. */
23322 if (r1->y < r2->y)
23323 upper = r1, lower = r2;
23324 else
23325 upper = r2, lower = r1;
23327 /* The upper end of the intersection is lower.y0, if this is inside
23328 of upper. Otherwise, there is no intersection. */
23329 if (lower->y <= upper->y + upper->height)
23331 result->y = lower->y;
23333 /* The lower end of the intersection is the minimum of the lower
23334 ends of upper and lower. */
23335 result->height = (min (lower->y + lower->height,
23336 upper->y + upper->height)
23337 - result->y);
23338 intersection_p = 1;
23342 return intersection_p;
23345 #endif /* HAVE_WINDOW_SYSTEM */
23348 /***********************************************************************
23349 Initialization
23350 ***********************************************************************/
23352 void
23353 syms_of_xdisp ()
23355 Vwith_echo_area_save_vector = Qnil;
23356 staticpro (&Vwith_echo_area_save_vector);
23358 Vmessage_stack = Qnil;
23359 staticpro (&Vmessage_stack);
23361 Qinhibit_redisplay = intern ("inhibit-redisplay");
23362 staticpro (&Qinhibit_redisplay);
23364 message_dolog_marker1 = Fmake_marker ();
23365 staticpro (&message_dolog_marker1);
23366 message_dolog_marker2 = Fmake_marker ();
23367 staticpro (&message_dolog_marker2);
23368 message_dolog_marker3 = Fmake_marker ();
23369 staticpro (&message_dolog_marker3);
23371 #if GLYPH_DEBUG
23372 defsubr (&Sdump_frame_glyph_matrix);
23373 defsubr (&Sdump_glyph_matrix);
23374 defsubr (&Sdump_glyph_row);
23375 defsubr (&Sdump_tool_bar_row);
23376 defsubr (&Strace_redisplay);
23377 defsubr (&Strace_to_stderr);
23378 #endif
23379 #ifdef HAVE_WINDOW_SYSTEM
23380 defsubr (&Stool_bar_lines_needed);
23381 defsubr (&Slookup_image_map);
23382 #endif
23383 defsubr (&Sformat_mode_line);
23385 staticpro (&Qmenu_bar_update_hook);
23386 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23388 staticpro (&Qoverriding_terminal_local_map);
23389 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23391 staticpro (&Qoverriding_local_map);
23392 Qoverriding_local_map = intern ("overriding-local-map");
23394 staticpro (&Qwindow_scroll_functions);
23395 Qwindow_scroll_functions = intern ("window-scroll-functions");
23397 staticpro (&Qredisplay_end_trigger_functions);
23398 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23400 staticpro (&Qinhibit_point_motion_hooks);
23401 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23403 QCdata = intern (":data");
23404 staticpro (&QCdata);
23405 Qdisplay = intern ("display");
23406 staticpro (&Qdisplay);
23407 Qspace_width = intern ("space-width");
23408 staticpro (&Qspace_width);
23409 Qraise = intern ("raise");
23410 staticpro (&Qraise);
23411 Qslice = intern ("slice");
23412 staticpro (&Qslice);
23413 Qspace = intern ("space");
23414 staticpro (&Qspace);
23415 Qmargin = intern ("margin");
23416 staticpro (&Qmargin);
23417 Qpointer = intern ("pointer");
23418 staticpro (&Qpointer);
23419 Qleft_margin = intern ("left-margin");
23420 staticpro (&Qleft_margin);
23421 Qright_margin = intern ("right-margin");
23422 staticpro (&Qright_margin);
23423 Qcenter = intern ("center");
23424 staticpro (&Qcenter);
23425 Qline_height = intern ("line-height");
23426 staticpro (&Qline_height);
23427 QCalign_to = intern (":align-to");
23428 staticpro (&QCalign_to);
23429 QCrelative_width = intern (":relative-width");
23430 staticpro (&QCrelative_width);
23431 QCrelative_height = intern (":relative-height");
23432 staticpro (&QCrelative_height);
23433 QCeval = intern (":eval");
23434 staticpro (&QCeval);
23435 QCpropertize = intern (":propertize");
23436 staticpro (&QCpropertize);
23437 QCfile = intern (":file");
23438 staticpro (&QCfile);
23439 Qfontified = intern ("fontified");
23440 staticpro (&Qfontified);
23441 Qfontification_functions = intern ("fontification-functions");
23442 staticpro (&Qfontification_functions);
23443 Qtrailing_whitespace = intern ("trailing-whitespace");
23444 staticpro (&Qtrailing_whitespace);
23445 Qescape_glyph = intern ("escape-glyph");
23446 staticpro (&Qescape_glyph);
23447 Qnobreak_space = intern ("nobreak-space");
23448 staticpro (&Qnobreak_space);
23449 Qimage = intern ("image");
23450 staticpro (&Qimage);
23451 QCmap = intern (":map");
23452 staticpro (&QCmap);
23453 QCpointer = intern (":pointer");
23454 staticpro (&QCpointer);
23455 Qrect = intern ("rect");
23456 staticpro (&Qrect);
23457 Qcircle = intern ("circle");
23458 staticpro (&Qcircle);
23459 Qpoly = intern ("poly");
23460 staticpro (&Qpoly);
23461 Qmessage_truncate_lines = intern ("message-truncate-lines");
23462 staticpro (&Qmessage_truncate_lines);
23463 Qgrow_only = intern ("grow-only");
23464 staticpro (&Qgrow_only);
23465 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23466 staticpro (&Qinhibit_menubar_update);
23467 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23468 staticpro (&Qinhibit_eval_during_redisplay);
23469 Qposition = intern ("position");
23470 staticpro (&Qposition);
23471 Qbuffer_position = intern ("buffer-position");
23472 staticpro (&Qbuffer_position);
23473 Qobject = intern ("object");
23474 staticpro (&Qobject);
23475 Qbar = intern ("bar");
23476 staticpro (&Qbar);
23477 Qhbar = intern ("hbar");
23478 staticpro (&Qhbar);
23479 Qbox = intern ("box");
23480 staticpro (&Qbox);
23481 Qhollow = intern ("hollow");
23482 staticpro (&Qhollow);
23483 Qhand = intern ("hand");
23484 staticpro (&Qhand);
23485 Qarrow = intern ("arrow");
23486 staticpro (&Qarrow);
23487 Qtext = intern ("text");
23488 staticpro (&Qtext);
23489 Qrisky_local_variable = intern ("risky-local-variable");
23490 staticpro (&Qrisky_local_variable);
23491 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23492 staticpro (&Qinhibit_free_realized_faces);
23494 list_of_error = Fcons (Fcons (intern ("error"),
23495 Fcons (intern ("void-variable"), Qnil)),
23496 Qnil);
23497 staticpro (&list_of_error);
23499 Qlast_arrow_position = intern ("last-arrow-position");
23500 staticpro (&Qlast_arrow_position);
23501 Qlast_arrow_string = intern ("last-arrow-string");
23502 staticpro (&Qlast_arrow_string);
23504 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23505 staticpro (&Qoverlay_arrow_string);
23506 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23507 staticpro (&Qoverlay_arrow_bitmap);
23509 echo_buffer[0] = echo_buffer[1] = Qnil;
23510 staticpro (&echo_buffer[0]);
23511 staticpro (&echo_buffer[1]);
23513 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23514 staticpro (&echo_area_buffer[0]);
23515 staticpro (&echo_area_buffer[1]);
23517 Vmessages_buffer_name = build_string ("*Messages*");
23518 staticpro (&Vmessages_buffer_name);
23520 mode_line_proptrans_alist = Qnil;
23521 staticpro (&mode_line_proptrans_alist);
23522 mode_line_string_list = Qnil;
23523 staticpro (&mode_line_string_list);
23524 mode_line_string_face = Qnil;
23525 staticpro (&mode_line_string_face);
23526 mode_line_string_face_prop = Qnil;
23527 staticpro (&mode_line_string_face_prop);
23528 Vmode_line_unwind_vector = Qnil;
23529 staticpro (&Vmode_line_unwind_vector);
23531 help_echo_string = Qnil;
23532 staticpro (&help_echo_string);
23533 help_echo_object = Qnil;
23534 staticpro (&help_echo_object);
23535 help_echo_window = Qnil;
23536 staticpro (&help_echo_window);
23537 previous_help_echo_string = Qnil;
23538 staticpro (&previous_help_echo_string);
23539 help_echo_pos = -1;
23541 #ifdef HAVE_WINDOW_SYSTEM
23542 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23543 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23544 For example, if a block cursor is over a tab, it will be drawn as
23545 wide as that tab on the display. */);
23546 x_stretch_cursor_p = 0;
23547 #endif
23549 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23550 doc: /* *Non-nil means highlight trailing whitespace.
23551 The face used for trailing whitespace is `trailing-whitespace'. */);
23552 Vshow_trailing_whitespace = Qnil;
23554 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23555 doc: /* *Control highlighting of nobreak space and soft hyphen.
23556 A value of t means highlight the character itself (for nobreak space,
23557 use face `nobreak-space').
23558 A value of nil means no highlighting.
23559 Other values mean display the escape glyph followed by an ordinary
23560 space or ordinary hyphen. */);
23561 Vnobreak_char_display = Qt;
23563 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23564 doc: /* *The pointer shape to show in void text areas.
23565 A value of nil means to show the text pointer. Other options are `arrow',
23566 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23567 Vvoid_text_area_pointer = Qarrow;
23569 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23570 doc: /* Non-nil means don't actually do any redisplay.
23571 This is used for internal purposes. */);
23572 Vinhibit_redisplay = Qnil;
23574 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23575 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23576 Vglobal_mode_string = Qnil;
23578 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23579 doc: /* Marker for where to display an arrow on top of the buffer text.
23580 This must be the beginning of a line in order to work.
23581 See also `overlay-arrow-string'. */);
23582 Voverlay_arrow_position = Qnil;
23584 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23585 doc: /* String to display as an arrow in non-window frames.
23586 See also `overlay-arrow-position'. */);
23587 Voverlay_arrow_string = build_string ("=>");
23589 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23590 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23591 The symbols on this list are examined during redisplay to determine
23592 where to display overlay arrows. */);
23593 Voverlay_arrow_variable_list
23594 = Fcons (intern ("overlay-arrow-position"), Qnil);
23596 DEFVAR_INT ("scroll-step", &scroll_step,
23597 doc: /* *The number of lines to try scrolling a window by when point moves out.
23598 If that fails to bring point back on frame, point is centered instead.
23599 If this is zero, point is always centered after it moves off frame.
23600 If you want scrolling to always be a line at a time, you should set
23601 `scroll-conservatively' to a large value rather than set this to 1. */);
23603 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23604 doc: /* *Scroll up to this many lines, to bring point back on screen.
23605 A value of zero means to scroll the text to center point vertically
23606 in the window. */);
23607 scroll_conservatively = 0;
23609 DEFVAR_INT ("scroll-margin", &scroll_margin,
23610 doc: /* *Number of lines of margin at the top and bottom of a window.
23611 Recenter the window whenever point gets within this many lines
23612 of the top or bottom of the window. */);
23613 scroll_margin = 0;
23615 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23616 doc: /* Pixels per inch value for non-window system displays.
23617 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23618 Vdisplay_pixels_per_inch = make_float (72.0);
23620 #if GLYPH_DEBUG
23621 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23622 #endif
23624 DEFVAR_BOOL ("truncate-partial-width-windows",
23625 &truncate_partial_width_windows,
23626 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23627 truncate_partial_width_windows = 1;
23629 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23630 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23631 Any other value means to use the appropriate face, `mode-line',
23632 `header-line', or `menu' respectively. */);
23633 mode_line_inverse_video = 1;
23635 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23636 doc: /* *Maximum buffer size for which line number should be displayed.
23637 If the buffer is bigger than this, the line number does not appear
23638 in the mode line. A value of nil means no limit. */);
23639 Vline_number_display_limit = Qnil;
23641 DEFVAR_INT ("line-number-display-limit-width",
23642 &line_number_display_limit_width,
23643 doc: /* *Maximum line width (in characters) for line number display.
23644 If the average length of the lines near point is bigger than this, then the
23645 line number may be omitted from the mode line. */);
23646 line_number_display_limit_width = 200;
23648 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23649 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23650 highlight_nonselected_windows = 0;
23652 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23653 doc: /* Non-nil if more than one frame is visible on this display.
23654 Minibuffer-only frames don't count, but iconified frames do.
23655 This variable is not guaranteed to be accurate except while processing
23656 `frame-title-format' and `icon-title-format'. */);
23658 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23659 doc: /* Template for displaying the title bar of visible frames.
23660 \(Assuming the window manager supports this feature.)
23661 This variable has the same structure as `mode-line-format' (which see),
23662 and is used only on frames for which no explicit name has been set
23663 \(see `modify-frame-parameters'). */);
23665 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23666 doc: /* Template for displaying the title bar of an iconified frame.
23667 \(Assuming the window manager supports this feature.)
23668 This variable has the same structure as `mode-line-format' (which see),
23669 and is used only on frames for which no explicit name has been set
23670 \(see `modify-frame-parameters'). */);
23671 Vicon_title_format
23672 = Vframe_title_format
23673 = Fcons (intern ("multiple-frames"),
23674 Fcons (build_string ("%b"),
23675 Fcons (Fcons (empty_string,
23676 Fcons (intern ("invocation-name"),
23677 Fcons (build_string ("@"),
23678 Fcons (intern ("system-name"),
23679 Qnil)))),
23680 Qnil)));
23682 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23683 doc: /* Maximum number of lines to keep in the message log buffer.
23684 If nil, disable message logging. If t, log messages but don't truncate
23685 the buffer when it becomes large. */);
23686 Vmessage_log_max = make_number (50);
23688 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23689 doc: /* Functions called before redisplay, if window sizes have changed.
23690 The value should be a list of functions that take one argument.
23691 Just before redisplay, for each frame, if any of its windows have changed
23692 size since the last redisplay, or have been split or deleted,
23693 all the functions in the list are called, with the frame as argument. */);
23694 Vwindow_size_change_functions = Qnil;
23696 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23697 doc: /* List of functions to call before redisplaying a window with scrolling.
23698 Each function is called with two arguments, the window
23699 and its new display-start position. Note that the value of `window-end'
23700 is not valid when these functions are called. */);
23701 Vwindow_scroll_functions = Qnil;
23703 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23704 doc: /* Functions called when redisplay of a window reaches the end trigger.
23705 Each function is called with two arguments, the window and the end trigger value.
23706 See `set-window-redisplay-end-trigger'. */);
23707 Vredisplay_end_trigger_functions = Qnil;
23709 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23710 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23711 mouse_autoselect_window = 0;
23713 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23714 doc: /* *Non-nil means automatically resize tool-bars.
23715 This increases a tool-bar's height if not all tool-bar items are visible.
23716 It decreases a tool-bar's height when it would display blank lines
23717 otherwise. */);
23718 auto_resize_tool_bars_p = 1;
23720 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23721 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23722 auto_raise_tool_bar_buttons_p = 1;
23724 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23725 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23726 make_cursor_line_fully_visible_p = 1;
23728 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23729 doc: /* *Border below tool-bar in pixels.
23730 If an integer, use it as the height of the border.
23731 If it is one of `internal-border-width' or `border-width', use the
23732 value of the corresponding frame parameter.
23733 Otherwise, no border is added below the tool-bar. */);
23734 Vtool_bar_border = Qinternal_border_width;
23736 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23737 doc: /* *Margin around tool-bar buttons in pixels.
23738 If an integer, use that for both horizontal and vertical margins.
23739 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23740 HORZ specifying the horizontal margin, and VERT specifying the
23741 vertical margin. */);
23742 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23744 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23745 doc: /* *Relief thickness of tool-bar buttons. */);
23746 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23748 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23749 doc: /* List of functions to call to fontify regions of text.
23750 Each function is called with one argument POS. Functions must
23751 fontify a region starting at POS in the current buffer, and give
23752 fontified regions the property `fontified'. */);
23753 Vfontification_functions = Qnil;
23754 Fmake_variable_buffer_local (Qfontification_functions);
23756 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23757 &unibyte_display_via_language_environment,
23758 doc: /* *Non-nil means display unibyte text according to language environment.
23759 Specifically this means that unibyte non-ASCII characters
23760 are displayed by converting them to the equivalent multibyte characters
23761 according to the current language environment. As a result, they are
23762 displayed according to the current fontset. */);
23763 unibyte_display_via_language_environment = 0;
23765 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23766 doc: /* *Maximum height for resizing mini-windows.
23767 If a float, it specifies a fraction of the mini-window frame's height.
23768 If an integer, it specifies a number of lines. */);
23769 Vmax_mini_window_height = make_float (0.25);
23771 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23772 doc: /* *How to resize mini-windows.
23773 A value of nil means don't automatically resize mini-windows.
23774 A value of t means resize them to fit the text displayed in them.
23775 A value of `grow-only', the default, means let mini-windows grow
23776 only, until their display becomes empty, at which point the windows
23777 go back to their normal size. */);
23778 Vresize_mini_windows = Qgrow_only;
23780 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23781 doc: /* Alist specifying how to blink the cursor off.
23782 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23783 `cursor-type' frame-parameter or variable equals ON-STATE,
23784 comparing using `equal', Emacs uses OFF-STATE to specify
23785 how to blink it off. ON-STATE and OFF-STATE are values for
23786 the `cursor-type' frame parameter.
23788 If a frame's ON-STATE has no entry in this list,
23789 the frame's other specifications determine how to blink the cursor off. */);
23790 Vblink_cursor_alist = Qnil;
23792 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23793 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23794 automatic_hscrolling_p = 1;
23796 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23797 doc: /* *How many columns away from the window edge point is allowed to get
23798 before automatic hscrolling will horizontally scroll the window. */);
23799 hscroll_margin = 5;
23801 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23802 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23803 When point is less than `hscroll-margin' columns from the window
23804 edge, automatic hscrolling will scroll the window by the amount of columns
23805 determined by this variable. If its value is a positive integer, scroll that
23806 many columns. If it's a positive floating-point number, it specifies the
23807 fraction of the window's width to scroll. If it's nil or zero, point will be
23808 centered horizontally after the scroll. Any other value, including negative
23809 numbers, are treated as if the value were zero.
23811 Automatic hscrolling always moves point outside the scroll margin, so if
23812 point was more than scroll step columns inside the margin, the window will
23813 scroll more than the value given by the scroll step.
23815 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23816 and `scroll-right' overrides this variable's effect. */);
23817 Vhscroll_step = make_number (0);
23819 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23820 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23821 Bind this around calls to `message' to let it take effect. */);
23822 message_truncate_lines = 0;
23824 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23825 doc: /* Normal hook run to update the menu bar definitions.
23826 Redisplay runs this hook before it redisplays the menu bar.
23827 This is used to update submenus such as Buffers,
23828 whose contents depend on various data. */);
23829 Vmenu_bar_update_hook = Qnil;
23831 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23832 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23833 inhibit_menubar_update = 0;
23835 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23836 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23837 inhibit_eval_during_redisplay = 0;
23839 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23840 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23841 inhibit_free_realized_faces = 0;
23843 #if GLYPH_DEBUG
23844 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23845 doc: /* Inhibit try_window_id display optimization. */);
23846 inhibit_try_window_id = 0;
23848 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23849 doc: /* Inhibit try_window_reusing display optimization. */);
23850 inhibit_try_window_reusing = 0;
23852 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23853 doc: /* Inhibit try_cursor_movement display optimization. */);
23854 inhibit_try_cursor_movement = 0;
23855 #endif /* GLYPH_DEBUG */
23859 /* Initialize this module when Emacs starts. */
23861 void
23862 init_xdisp ()
23864 Lisp_Object root_window;
23865 struct window *mini_w;
23867 current_header_line_height = current_mode_line_height = -1;
23869 CHARPOS (this_line_start_pos) = 0;
23871 mini_w = XWINDOW (minibuf_window);
23872 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23874 if (!noninteractive)
23876 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23877 int i;
23879 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23880 set_window_height (root_window,
23881 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23883 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23884 set_window_height (minibuf_window, 1, 0);
23886 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23887 mini_w->total_cols = make_number (FRAME_COLS (f));
23889 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23890 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23891 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23893 /* The default ellipsis glyphs `...'. */
23894 for (i = 0; i < 3; ++i)
23895 default_invis_vector[i] = make_number ('.');
23899 /* Allocate the buffer for frame titles.
23900 Also used for `format-mode-line'. */
23901 int size = 100;
23902 mode_line_noprop_buf = (char *) xmalloc (size);
23903 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23904 mode_line_noprop_ptr = mode_line_noprop_buf;
23905 mode_line_target = MODE_LINE_DISPLAY;
23908 help_echo_showing_p = 0;
23912 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23913 (do not change this comment) */