Mention `redisplay-dont-pause' in doc string of
[emacs.git] / src / xdisp.c
blob80237e89fa1b60c4ebd3093542b1517f066cd3d0
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)
1940 if (r.y < row_y)
1941 rs[i].height = row_y - r.y;
1942 else
1943 rs[i].height = 0;
1945 i++;
1947 if (s->for_overlaps & OVERLAPS_SUCC)
1949 rs[i] = r;
1950 if (r.y < row_y + s->row->visible_height)
1952 if (r.y + r.height > row_y + s->row->visible_height)
1954 rs[i].y = row_y + s->row->visible_height;
1955 rs[i].height = r.y + r.height - rs[i].y;
1957 else
1958 rs[i].height = 0;
1960 i++;
1963 n = i;
1964 #ifdef CONVERT_FROM_XRECT
1965 for (i = 0; i < n; i++)
1966 CONVERT_FROM_XRECT (rs[i], rects[i]);
1967 #endif
1968 return n;
1972 /* EXPORT:
1973 Return in *NR the clipping rectangle for glyph string S. */
1975 void
1976 get_glyph_string_clip_rect (s, nr)
1977 struct glyph_string *s;
1978 NativeRectangle *nr;
1980 get_glyph_string_clip_rects (s, nr, 1);
1984 /* EXPORT:
1985 Return the position and height of the phys cursor in window W.
1986 Set w->phys_cursor_width to width of phys cursor.
1989 void
1990 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
1991 struct window *w;
1992 struct glyph_row *row;
1993 struct glyph *glyph;
1994 int *xp, *yp, *heightp;
1996 struct frame *f = XFRAME (WINDOW_FRAME (w));
1997 int x, y, wd, h, h0, y0;
1999 /* Compute the width of the rectangle to draw. If on a stretch
2000 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2001 rectangle as wide as the glyph, but use a canonical character
2002 width instead. */
2003 wd = glyph->pixel_width - 1;
2004 #ifdef HAVE_NTGUI
2005 wd++; /* Why? */
2006 #endif
2008 x = w->phys_cursor.x;
2009 if (x < 0)
2011 wd += x;
2012 x = 0;
2015 if (glyph->type == STRETCH_GLYPH
2016 && !x_stretch_cursor_p)
2017 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2018 w->phys_cursor_width = wd;
2020 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2022 /* If y is below window bottom, ensure that we still see a cursor. */
2023 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2025 h = max (h0, glyph->ascent + glyph->descent);
2026 h0 = min (h0, glyph->ascent + glyph->descent);
2028 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2029 if (y < y0)
2031 h = max (h - (y0 - y) + 1, h0);
2032 y = y0 - 1;
2034 else
2036 y0 = window_text_bottom_y (w) - h0;
2037 if (y > y0)
2039 h += y - y0;
2040 y = y0;
2044 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2045 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2046 *heightp = h;
2050 * Remember which glyph the mouse is over.
2053 void
2054 remember_mouse_glyph (f, gx, gy, rect)
2055 struct frame *f;
2056 int gx, gy;
2057 NativeRectangle *rect;
2059 Lisp_Object window;
2060 struct window *w;
2061 struct glyph_row *r, *gr, *end_row;
2062 enum window_part part;
2063 enum glyph_row_area area;
2064 int x, y, width, height;
2066 /* Try to determine frame pixel position and size of the glyph under
2067 frame pixel coordinates X/Y on frame F. */
2069 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2070 if (NILP (window))
2072 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2073 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2074 goto virtual_glyph;
2077 w = XWINDOW (window);
2078 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2079 height = WINDOW_FRAME_LINE_HEIGHT (w);
2081 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2082 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2084 if (w->pseudo_window_p)
2086 area = TEXT_AREA;
2087 part = ON_MODE_LINE; /* Don't adjust margin. */
2088 goto text_glyph;
2091 switch (part)
2093 case ON_LEFT_MARGIN:
2094 area = LEFT_MARGIN_AREA;
2095 goto text_glyph;
2097 case ON_RIGHT_MARGIN:
2098 area = RIGHT_MARGIN_AREA;
2099 goto text_glyph;
2101 case ON_HEADER_LINE:
2102 case ON_MODE_LINE:
2103 gr = (part == ON_HEADER_LINE
2104 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2105 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2106 gy = gr->y;
2107 area = TEXT_AREA;
2108 goto text_glyph_row_found;
2110 case ON_TEXT:
2111 area = TEXT_AREA;
2113 text_glyph:
2114 gr = 0; gy = 0;
2115 for (; r <= end_row && r->enabled_p; ++r)
2116 if (r->y + r->height > y)
2118 gr = r; gy = r->y;
2119 break;
2122 text_glyph_row_found:
2123 if (gr && gy <= y)
2125 struct glyph *g = gr->glyphs[area];
2126 struct glyph *end = g + gr->used[area];
2128 height = gr->height;
2129 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2130 if (gx + g->pixel_width > x)
2131 break;
2133 if (g < end)
2135 if (g->type == IMAGE_GLYPH)
2137 /* Don't remember when mouse is over image, as
2138 image may have hot-spots. */
2139 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2140 return;
2142 width = g->pixel_width;
2144 else
2146 /* Use nominal char spacing at end of line. */
2147 x -= gx;
2148 gx += (x / width) * width;
2151 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2152 gx += window_box_left_offset (w, area);
2154 else
2156 /* Use nominal line height at end of window. */
2157 gx = (x / width) * width;
2158 y -= gy;
2159 gy += (y / height) * height;
2161 break;
2163 case ON_LEFT_FRINGE:
2164 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2165 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2166 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2167 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2168 goto row_glyph;
2170 case ON_RIGHT_FRINGE:
2171 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2172 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2173 : window_box_right_offset (w, TEXT_AREA));
2174 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2175 goto row_glyph;
2177 case ON_SCROLL_BAR:
2178 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2180 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2181 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2182 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2183 : 0)));
2184 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2186 row_glyph:
2187 gr = 0, gy = 0;
2188 for (; r <= end_row && r->enabled_p; ++r)
2189 if (r->y + r->height > y)
2191 gr = r; gy = r->y;
2192 break;
2195 if (gr && gy <= y)
2196 height = gr->height;
2197 else
2199 /* Use nominal line height at end of window. */
2200 y -= gy;
2201 gy += (y / height) * height;
2203 break;
2205 default:
2207 virtual_glyph:
2208 /* If there is no glyph under the mouse, then we divide the screen
2209 into a grid of the smallest glyph in the frame, and use that
2210 as our "glyph". */
2212 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2213 round down even for negative values. */
2214 if (gx < 0)
2215 gx -= width - 1;
2216 if (gy < 0)
2217 gy -= height - 1;
2219 gx = (gx / width) * width;
2220 gy = (gy / height) * height;
2222 goto store_rect;
2225 gx += WINDOW_LEFT_EDGE_X (w);
2226 gy += WINDOW_TOP_EDGE_Y (w);
2228 store_rect:
2229 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2231 /* Visible feedback for debugging. */
2232 #if 0
2233 #if HAVE_X_WINDOWS
2234 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2235 f->output_data.x->normal_gc,
2236 gx, gy, width, height);
2237 #endif
2238 #endif
2242 #endif /* HAVE_WINDOW_SYSTEM */
2245 /***********************************************************************
2246 Lisp form evaluation
2247 ***********************************************************************/
2249 /* Error handler for safe_eval and safe_call. */
2251 static Lisp_Object
2252 safe_eval_handler (arg)
2253 Lisp_Object arg;
2255 add_to_log ("Error during redisplay: %s", arg, Qnil);
2256 return Qnil;
2260 /* Evaluate SEXPR and return the result, or nil if something went
2261 wrong. Prevent redisplay during the evaluation. */
2263 Lisp_Object
2264 safe_eval (sexpr)
2265 Lisp_Object sexpr;
2267 Lisp_Object val;
2269 if (inhibit_eval_during_redisplay)
2270 val = Qnil;
2271 else
2273 int count = SPECPDL_INDEX ();
2274 struct gcpro gcpro1;
2276 GCPRO1 (sexpr);
2277 specbind (Qinhibit_redisplay, Qt);
2278 /* Use Qt to ensure debugger does not run,
2279 so there is no possibility of wanting to redisplay. */
2280 val = internal_condition_case_1 (Feval, sexpr, Qt,
2281 safe_eval_handler);
2282 UNGCPRO;
2283 val = unbind_to (count, val);
2286 return val;
2290 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2291 Return the result, or nil if something went wrong. Prevent
2292 redisplay during the evaluation. */
2294 Lisp_Object
2295 safe_call (nargs, args)
2296 int nargs;
2297 Lisp_Object *args;
2299 Lisp_Object val;
2301 if (inhibit_eval_during_redisplay)
2302 val = Qnil;
2303 else
2305 int count = SPECPDL_INDEX ();
2306 struct gcpro gcpro1;
2308 GCPRO1 (args[0]);
2309 gcpro1.nvars = nargs;
2310 specbind (Qinhibit_redisplay, Qt);
2311 /* Use Qt to ensure debugger does not run,
2312 so there is no possibility of wanting to redisplay. */
2313 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2314 safe_eval_handler);
2315 UNGCPRO;
2316 val = unbind_to (count, val);
2319 return val;
2323 /* Call function FN with one argument ARG.
2324 Return the result, or nil if something went wrong. */
2326 Lisp_Object
2327 safe_call1 (fn, arg)
2328 Lisp_Object fn, arg;
2330 Lisp_Object args[2];
2331 args[0] = fn;
2332 args[1] = arg;
2333 return safe_call (2, args);
2338 /***********************************************************************
2339 Debugging
2340 ***********************************************************************/
2342 #if 0
2344 /* Define CHECK_IT to perform sanity checks on iterators.
2345 This is for debugging. It is too slow to do unconditionally. */
2347 static void
2348 check_it (it)
2349 struct it *it;
2351 if (it->method == GET_FROM_STRING)
2353 xassert (STRINGP (it->string));
2354 xassert (IT_STRING_CHARPOS (*it) >= 0);
2356 else
2358 xassert (IT_STRING_CHARPOS (*it) < 0);
2359 if (it->method == GET_FROM_BUFFER)
2361 /* Check that character and byte positions agree. */
2362 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2366 if (it->dpvec)
2367 xassert (it->current.dpvec_index >= 0);
2368 else
2369 xassert (it->current.dpvec_index < 0);
2372 #define CHECK_IT(IT) check_it ((IT))
2374 #else /* not 0 */
2376 #define CHECK_IT(IT) (void) 0
2378 #endif /* not 0 */
2381 #if GLYPH_DEBUG
2383 /* Check that the window end of window W is what we expect it
2384 to be---the last row in the current matrix displaying text. */
2386 static void
2387 check_window_end (w)
2388 struct window *w;
2390 if (!MINI_WINDOW_P (w)
2391 && !NILP (w->window_end_valid))
2393 struct glyph_row *row;
2394 xassert ((row = MATRIX_ROW (w->current_matrix,
2395 XFASTINT (w->window_end_vpos)),
2396 !row->enabled_p
2397 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2398 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2402 #define CHECK_WINDOW_END(W) check_window_end ((W))
2404 #else /* not GLYPH_DEBUG */
2406 #define CHECK_WINDOW_END(W) (void) 0
2408 #endif /* not GLYPH_DEBUG */
2412 /***********************************************************************
2413 Iterator initialization
2414 ***********************************************************************/
2416 /* Initialize IT for displaying current_buffer in window W, starting
2417 at character position CHARPOS. CHARPOS < 0 means that no buffer
2418 position is specified which is useful when the iterator is assigned
2419 a position later. BYTEPOS is the byte position corresponding to
2420 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2422 If ROW is not null, calls to produce_glyphs with IT as parameter
2423 will produce glyphs in that row.
2425 BASE_FACE_ID is the id of a base face to use. It must be one of
2426 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2427 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2428 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2430 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2431 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2432 will be initialized to use the corresponding mode line glyph row of
2433 the desired matrix of W. */
2435 void
2436 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2437 struct it *it;
2438 struct window *w;
2439 int charpos, bytepos;
2440 struct glyph_row *row;
2441 enum face_id base_face_id;
2443 int highlight_region_p;
2445 /* Some precondition checks. */
2446 xassert (w != NULL && it != NULL);
2447 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2448 && charpos <= ZV));
2450 /* If face attributes have been changed since the last redisplay,
2451 free realized faces now because they depend on face definitions
2452 that might have changed. Don't free faces while there might be
2453 desired matrices pending which reference these faces. */
2454 if (face_change_count && !inhibit_free_realized_faces)
2456 face_change_count = 0;
2457 free_all_realized_faces (Qnil);
2460 /* Use one of the mode line rows of W's desired matrix if
2461 appropriate. */
2462 if (row == NULL)
2464 if (base_face_id == MODE_LINE_FACE_ID
2465 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2466 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2467 else if (base_face_id == HEADER_LINE_FACE_ID)
2468 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2471 /* Clear IT. */
2472 bzero (it, sizeof *it);
2473 it->current.overlay_string_index = -1;
2474 it->current.dpvec_index = -1;
2475 it->base_face_id = base_face_id;
2476 it->string = Qnil;
2477 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2479 /* The window in which we iterate over current_buffer: */
2480 XSETWINDOW (it->window, w);
2481 it->w = w;
2482 it->f = XFRAME (w->frame);
2484 /* Extra space between lines (on window systems only). */
2485 if (base_face_id == DEFAULT_FACE_ID
2486 && FRAME_WINDOW_P (it->f))
2488 if (NATNUMP (current_buffer->extra_line_spacing))
2489 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2490 else if (FLOATP (current_buffer->extra_line_spacing))
2491 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2492 * FRAME_LINE_HEIGHT (it->f));
2493 else if (it->f->extra_line_spacing > 0)
2494 it->extra_line_spacing = it->f->extra_line_spacing;
2495 it->max_extra_line_spacing = 0;
2498 /* If realized faces have been removed, e.g. because of face
2499 attribute changes of named faces, recompute them. When running
2500 in batch mode, the face cache of Vterminal_frame is null. If
2501 we happen to get called, make a dummy face cache. */
2502 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2503 init_frame_faces (it->f);
2504 if (FRAME_FACE_CACHE (it->f)->used == 0)
2505 recompute_basic_faces (it->f);
2507 /* Current value of the `slice', `space-width', and 'height' properties. */
2508 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2509 it->space_width = Qnil;
2510 it->font_height = Qnil;
2511 it->override_ascent = -1;
2513 /* Are control characters displayed as `^C'? */
2514 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2516 /* -1 means everything between a CR and the following line end
2517 is invisible. >0 means lines indented more than this value are
2518 invisible. */
2519 it->selective = (INTEGERP (current_buffer->selective_display)
2520 ? XFASTINT (current_buffer->selective_display)
2521 : (!NILP (current_buffer->selective_display)
2522 ? -1 : 0));
2523 it->selective_display_ellipsis_p
2524 = !NILP (current_buffer->selective_display_ellipses);
2526 /* Display table to use. */
2527 it->dp = window_display_table (w);
2529 /* Are multibyte characters enabled in current_buffer? */
2530 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2532 /* Non-zero if we should highlight the region. */
2533 highlight_region_p
2534 = (!NILP (Vtransient_mark_mode)
2535 && !NILP (current_buffer->mark_active)
2536 && XMARKER (current_buffer->mark)->buffer != 0);
2538 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2539 start and end of a visible region in window IT->w. Set both to
2540 -1 to indicate no region. */
2541 if (highlight_region_p
2542 /* Maybe highlight only in selected window. */
2543 && (/* Either show region everywhere. */
2544 highlight_nonselected_windows
2545 /* Or show region in the selected window. */
2546 || w == XWINDOW (selected_window)
2547 /* Or show the region if we are in the mini-buffer and W is
2548 the window the mini-buffer refers to. */
2549 || (MINI_WINDOW_P (XWINDOW (selected_window))
2550 && WINDOWP (minibuf_selected_window)
2551 && w == XWINDOW (minibuf_selected_window))))
2553 int charpos = marker_position (current_buffer->mark);
2554 it->region_beg_charpos = min (PT, charpos);
2555 it->region_end_charpos = max (PT, charpos);
2557 else
2558 it->region_beg_charpos = it->region_end_charpos = -1;
2560 /* Get the position at which the redisplay_end_trigger hook should
2561 be run, if it is to be run at all. */
2562 if (MARKERP (w->redisplay_end_trigger)
2563 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2564 it->redisplay_end_trigger_charpos
2565 = marker_position (w->redisplay_end_trigger);
2566 else if (INTEGERP (w->redisplay_end_trigger))
2567 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2569 /* Correct bogus values of tab_width. */
2570 it->tab_width = XINT (current_buffer->tab_width);
2571 if (it->tab_width <= 0 || it->tab_width > 1000)
2572 it->tab_width = 8;
2574 /* Are lines in the display truncated? */
2575 it->truncate_lines_p
2576 = (base_face_id != DEFAULT_FACE_ID
2577 || XINT (it->w->hscroll)
2578 || (truncate_partial_width_windows
2579 && !WINDOW_FULL_WIDTH_P (it->w))
2580 || !NILP (current_buffer->truncate_lines));
2582 /* Get dimensions of truncation and continuation glyphs. These are
2583 displayed as fringe bitmaps under X, so we don't need them for such
2584 frames. */
2585 if (!FRAME_WINDOW_P (it->f))
2587 if (it->truncate_lines_p)
2589 /* We will need the truncation glyph. */
2590 xassert (it->glyph_row == NULL);
2591 produce_special_glyphs (it, IT_TRUNCATION);
2592 it->truncation_pixel_width = it->pixel_width;
2594 else
2596 /* We will need the continuation glyph. */
2597 xassert (it->glyph_row == NULL);
2598 produce_special_glyphs (it, IT_CONTINUATION);
2599 it->continuation_pixel_width = it->pixel_width;
2602 /* Reset these values to zero because the produce_special_glyphs
2603 above has changed them. */
2604 it->pixel_width = it->ascent = it->descent = 0;
2605 it->phys_ascent = it->phys_descent = 0;
2608 /* Set this after getting the dimensions of truncation and
2609 continuation glyphs, so that we don't produce glyphs when calling
2610 produce_special_glyphs, above. */
2611 it->glyph_row = row;
2612 it->area = TEXT_AREA;
2614 /* Get the dimensions of the display area. The display area
2615 consists of the visible window area plus a horizontally scrolled
2616 part to the left of the window. All x-values are relative to the
2617 start of this total display area. */
2618 if (base_face_id != DEFAULT_FACE_ID)
2620 /* Mode lines, menu bar in terminal frames. */
2621 it->first_visible_x = 0;
2622 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2624 else
2626 it->first_visible_x
2627 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2628 it->last_visible_x = (it->first_visible_x
2629 + window_box_width (w, TEXT_AREA));
2631 /* If we truncate lines, leave room for the truncator glyph(s) at
2632 the right margin. Otherwise, leave room for the continuation
2633 glyph(s). Truncation and continuation glyphs are not inserted
2634 for window-based redisplay. */
2635 if (!FRAME_WINDOW_P (it->f))
2637 if (it->truncate_lines_p)
2638 it->last_visible_x -= it->truncation_pixel_width;
2639 else
2640 it->last_visible_x -= it->continuation_pixel_width;
2643 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2644 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2647 /* Leave room for a border glyph. */
2648 if (!FRAME_WINDOW_P (it->f)
2649 && !WINDOW_RIGHTMOST_P (it->w))
2650 it->last_visible_x -= 1;
2652 it->last_visible_y = window_text_bottom_y (w);
2654 /* For mode lines and alike, arrange for the first glyph having a
2655 left box line if the face specifies a box. */
2656 if (base_face_id != DEFAULT_FACE_ID)
2658 struct face *face;
2660 it->face_id = base_face_id;
2662 /* If we have a boxed mode line, make the first character appear
2663 with a left box line. */
2664 face = FACE_FROM_ID (it->f, base_face_id);
2665 if (face->box != FACE_NO_BOX)
2666 it->start_of_box_run_p = 1;
2669 /* If a buffer position was specified, set the iterator there,
2670 getting overlays and face properties from that position. */
2671 if (charpos >= BUF_BEG (current_buffer))
2673 it->end_charpos = ZV;
2674 it->face_id = -1;
2675 IT_CHARPOS (*it) = charpos;
2677 /* Compute byte position if not specified. */
2678 if (bytepos < charpos)
2679 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2680 else
2681 IT_BYTEPOS (*it) = bytepos;
2683 it->start = it->current;
2685 /* Compute faces etc. */
2686 reseat (it, it->current.pos, 1);
2689 CHECK_IT (it);
2693 /* Initialize IT for the display of window W with window start POS. */
2695 void
2696 start_display (it, w, pos)
2697 struct it *it;
2698 struct window *w;
2699 struct text_pos pos;
2701 struct glyph_row *row;
2702 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2704 row = w->desired_matrix->rows + first_vpos;
2705 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2706 it->first_vpos = first_vpos;
2708 /* Don't reseat to previous visible line start if current start
2709 position is in a string or image. */
2710 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2712 int start_at_line_beg_p;
2713 int first_y = it->current_y;
2715 /* If window start is not at a line start, skip forward to POS to
2716 get the correct continuation lines width. */
2717 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2718 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2719 if (!start_at_line_beg_p)
2721 int new_x;
2723 reseat_at_previous_visible_line_start (it);
2724 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2726 new_x = it->current_x + it->pixel_width;
2728 /* If lines are continued, this line may end in the middle
2729 of a multi-glyph character (e.g. a control character
2730 displayed as \003, or in the middle of an overlay
2731 string). In this case move_it_to above will not have
2732 taken us to the start of the continuation line but to the
2733 end of the continued line. */
2734 if (it->current_x > 0
2735 && !it->truncate_lines_p /* Lines are continued. */
2736 && (/* And glyph doesn't fit on the line. */
2737 new_x > it->last_visible_x
2738 /* Or it fits exactly and we're on a window
2739 system frame. */
2740 || (new_x == it->last_visible_x
2741 && FRAME_WINDOW_P (it->f))))
2743 if (it->current.dpvec_index >= 0
2744 || it->current.overlay_string_index >= 0)
2746 set_iterator_to_next (it, 1);
2747 move_it_in_display_line_to (it, -1, -1, 0);
2750 it->continuation_lines_width += it->current_x;
2753 /* We're starting a new display line, not affected by the
2754 height of the continued line, so clear the appropriate
2755 fields in the iterator structure. */
2756 it->max_ascent = it->max_descent = 0;
2757 it->max_phys_ascent = it->max_phys_descent = 0;
2759 it->current_y = first_y;
2760 it->vpos = 0;
2761 it->current_x = it->hpos = 0;
2765 #if 0 /* Don't assert the following because start_display is sometimes
2766 called intentionally with a window start that is not at a
2767 line start. Please leave this code in as a comment. */
2769 /* Window start should be on a line start, now. */
2770 xassert (it->continuation_lines_width
2771 || IT_CHARPOS (it) == BEGV
2772 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2773 #endif /* 0 */
2777 /* Return 1 if POS is a position in ellipses displayed for invisible
2778 text. W is the window we display, for text property lookup. */
2780 static int
2781 in_ellipses_for_invisible_text_p (pos, w)
2782 struct display_pos *pos;
2783 struct window *w;
2785 Lisp_Object prop, window;
2786 int ellipses_p = 0;
2787 int charpos = CHARPOS (pos->pos);
2789 /* If POS specifies a position in a display vector, this might
2790 be for an ellipsis displayed for invisible text. We won't
2791 get the iterator set up for delivering that ellipsis unless
2792 we make sure that it gets aware of the invisible text. */
2793 if (pos->dpvec_index >= 0
2794 && pos->overlay_string_index < 0
2795 && CHARPOS (pos->string_pos) < 0
2796 && charpos > BEGV
2797 && (XSETWINDOW (window, w),
2798 prop = Fget_char_property (make_number (charpos),
2799 Qinvisible, window),
2800 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2802 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2803 window);
2804 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2807 return ellipses_p;
2811 /* Initialize IT for stepping through current_buffer in window W,
2812 starting at position POS that includes overlay string and display
2813 vector/ control character translation position information. Value
2814 is zero if there are overlay strings with newlines at POS. */
2816 static int
2817 init_from_display_pos (it, w, pos)
2818 struct it *it;
2819 struct window *w;
2820 struct display_pos *pos;
2822 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2823 int i, overlay_strings_with_newlines = 0;
2825 /* If POS specifies a position in a display vector, this might
2826 be for an ellipsis displayed for invisible text. We won't
2827 get the iterator set up for delivering that ellipsis unless
2828 we make sure that it gets aware of the invisible text. */
2829 if (in_ellipses_for_invisible_text_p (pos, w))
2831 --charpos;
2832 bytepos = 0;
2835 /* Keep in mind: the call to reseat in init_iterator skips invisible
2836 text, so we might end up at a position different from POS. This
2837 is only a problem when POS is a row start after a newline and an
2838 overlay starts there with an after-string, and the overlay has an
2839 invisible property. Since we don't skip invisible text in
2840 display_line and elsewhere immediately after consuming the
2841 newline before the row start, such a POS will not be in a string,
2842 but the call to init_iterator below will move us to the
2843 after-string. */
2844 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2846 /* This only scans the current chunk -- it should scan all chunks.
2847 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2848 to 16 in 22.1 to make this a lesser problem. */
2849 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2851 const char *s = SDATA (it->overlay_strings[i]);
2852 const char *e = s + SBYTES (it->overlay_strings[i]);
2854 while (s < e && *s != '\n')
2855 ++s;
2857 if (s < e)
2859 overlay_strings_with_newlines = 1;
2860 break;
2864 /* If position is within an overlay string, set up IT to the right
2865 overlay string. */
2866 if (pos->overlay_string_index >= 0)
2868 int relative_index;
2870 /* If the first overlay string happens to have a `display'
2871 property for an image, the iterator will be set up for that
2872 image, and we have to undo that setup first before we can
2873 correct the overlay string index. */
2874 if (it->method == GET_FROM_IMAGE)
2875 pop_it (it);
2877 /* We already have the first chunk of overlay strings in
2878 IT->overlay_strings. Load more until the one for
2879 pos->overlay_string_index is in IT->overlay_strings. */
2880 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2882 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2883 it->current.overlay_string_index = 0;
2884 while (n--)
2886 load_overlay_strings (it, 0);
2887 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2891 it->current.overlay_string_index = pos->overlay_string_index;
2892 relative_index = (it->current.overlay_string_index
2893 % OVERLAY_STRING_CHUNK_SIZE);
2894 it->string = it->overlay_strings[relative_index];
2895 xassert (STRINGP (it->string));
2896 it->current.string_pos = pos->string_pos;
2897 it->method = GET_FROM_STRING;
2900 #if 0 /* This is bogus because POS not having an overlay string
2901 position does not mean it's after the string. Example: A
2902 line starting with a before-string and initialization of IT
2903 to the previous row's end position. */
2904 else if (it->current.overlay_string_index >= 0)
2906 /* If POS says we're already after an overlay string ending at
2907 POS, make sure to pop the iterator because it will be in
2908 front of that overlay string. When POS is ZV, we've thereby
2909 also ``processed'' overlay strings at ZV. */
2910 while (it->sp)
2911 pop_it (it);
2912 it->current.overlay_string_index = -1;
2913 it->method = GET_FROM_BUFFER;
2914 if (CHARPOS (pos->pos) == ZV)
2915 it->overlay_strings_at_end_processed_p = 1;
2917 #endif /* 0 */
2919 if (CHARPOS (pos->string_pos) >= 0)
2921 /* Recorded position is not in an overlay string, but in another
2922 string. This can only be a string from a `display' property.
2923 IT should already be filled with that string. */
2924 it->current.string_pos = pos->string_pos;
2925 xassert (STRINGP (it->string));
2928 /* Restore position in display vector translations, control
2929 character translations or ellipses. */
2930 if (pos->dpvec_index >= 0)
2932 if (it->dpvec == NULL)
2933 get_next_display_element (it);
2934 xassert (it->dpvec && it->current.dpvec_index == 0);
2935 it->current.dpvec_index = pos->dpvec_index;
2938 CHECK_IT (it);
2939 return !overlay_strings_with_newlines;
2943 /* Initialize IT for stepping through current_buffer in window W
2944 starting at ROW->start. */
2946 static void
2947 init_to_row_start (it, w, row)
2948 struct it *it;
2949 struct window *w;
2950 struct glyph_row *row;
2952 init_from_display_pos (it, w, &row->start);
2953 it->start = row->start;
2954 it->continuation_lines_width = row->continuation_lines_width;
2955 CHECK_IT (it);
2959 /* Initialize IT for stepping through current_buffer in window W
2960 starting in the line following ROW, i.e. starting at ROW->end.
2961 Value is zero if there are overlay strings with newlines at ROW's
2962 end position. */
2964 static int
2965 init_to_row_end (it, w, row)
2966 struct it *it;
2967 struct window *w;
2968 struct glyph_row *row;
2970 int success = 0;
2972 if (init_from_display_pos (it, w, &row->end))
2974 if (row->continued_p)
2975 it->continuation_lines_width
2976 = row->continuation_lines_width + row->pixel_width;
2977 CHECK_IT (it);
2978 success = 1;
2981 return success;
2987 /***********************************************************************
2988 Text properties
2989 ***********************************************************************/
2991 /* Called when IT reaches IT->stop_charpos. Handle text property and
2992 overlay changes. Set IT->stop_charpos to the next position where
2993 to stop. */
2995 static void
2996 handle_stop (it)
2997 struct it *it;
2999 enum prop_handled handled;
3000 int handle_overlay_change_p;
3001 struct props *p;
3003 it->dpvec = NULL;
3004 it->current.dpvec_index = -1;
3005 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3006 it->ignore_overlay_strings_at_pos_p = 0;
3008 /* Use face of preceding text for ellipsis (if invisible) */
3009 if (it->selective_display_ellipsis_p)
3010 it->saved_face_id = it->face_id;
3014 handled = HANDLED_NORMALLY;
3016 /* Call text property handlers. */
3017 for (p = it_props; p->handler; ++p)
3019 handled = p->handler (it);
3021 if (handled == HANDLED_RECOMPUTE_PROPS)
3022 break;
3023 else if (handled == HANDLED_RETURN)
3024 return;
3025 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3026 handle_overlay_change_p = 0;
3029 if (handled != HANDLED_RECOMPUTE_PROPS)
3031 /* Don't check for overlay strings below when set to deliver
3032 characters from a display vector. */
3033 if (it->method == GET_FROM_DISPLAY_VECTOR)
3034 handle_overlay_change_p = 0;
3036 /* Handle overlay changes. */
3037 if (handle_overlay_change_p)
3038 handled = handle_overlay_change (it);
3040 /* Determine where to stop next. */
3041 if (handled == HANDLED_NORMALLY)
3042 compute_stop_pos (it);
3045 while (handled == HANDLED_RECOMPUTE_PROPS);
3049 /* Compute IT->stop_charpos from text property and overlay change
3050 information for IT's current position. */
3052 static void
3053 compute_stop_pos (it)
3054 struct it *it;
3056 register INTERVAL iv, next_iv;
3057 Lisp_Object object, limit, position;
3059 /* If nowhere else, stop at the end. */
3060 it->stop_charpos = it->end_charpos;
3062 if (STRINGP (it->string))
3064 /* Strings are usually short, so don't limit the search for
3065 properties. */
3066 object = it->string;
3067 limit = Qnil;
3068 position = make_number (IT_STRING_CHARPOS (*it));
3070 else
3072 int charpos;
3074 /* If next overlay change is in front of the current stop pos
3075 (which is IT->end_charpos), stop there. Note: value of
3076 next_overlay_change is point-max if no overlay change
3077 follows. */
3078 charpos = next_overlay_change (IT_CHARPOS (*it));
3079 if (charpos < it->stop_charpos)
3080 it->stop_charpos = charpos;
3082 /* If showing the region, we have to stop at the region
3083 start or end because the face might change there. */
3084 if (it->region_beg_charpos > 0)
3086 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3087 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3088 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3089 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3092 /* Set up variables for computing the stop position from text
3093 property changes. */
3094 XSETBUFFER (object, current_buffer);
3095 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3096 position = make_number (IT_CHARPOS (*it));
3100 /* Get the interval containing IT's position. Value is a null
3101 interval if there isn't such an interval. */
3102 iv = validate_interval_range (object, &position, &position, 0);
3103 if (!NULL_INTERVAL_P (iv))
3105 Lisp_Object values_here[LAST_PROP_IDX];
3106 struct props *p;
3108 /* Get properties here. */
3109 for (p = it_props; p->handler; ++p)
3110 values_here[p->idx] = textget (iv->plist, *p->name);
3112 /* Look for an interval following iv that has different
3113 properties. */
3114 for (next_iv = next_interval (iv);
3115 (!NULL_INTERVAL_P (next_iv)
3116 && (NILP (limit)
3117 || XFASTINT (limit) > next_iv->position));
3118 next_iv = next_interval (next_iv))
3120 for (p = it_props; p->handler; ++p)
3122 Lisp_Object new_value;
3124 new_value = textget (next_iv->plist, *p->name);
3125 if (!EQ (values_here[p->idx], new_value))
3126 break;
3129 if (p->handler)
3130 break;
3133 if (!NULL_INTERVAL_P (next_iv))
3135 if (INTEGERP (limit)
3136 && next_iv->position >= XFASTINT (limit))
3137 /* No text property change up to limit. */
3138 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3139 else
3140 /* Text properties change in next_iv. */
3141 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3145 xassert (STRINGP (it->string)
3146 || (it->stop_charpos >= BEGV
3147 && it->stop_charpos >= IT_CHARPOS (*it)));
3151 /* Return the position of the next overlay change after POS in
3152 current_buffer. Value is point-max if no overlay change
3153 follows. This is like `next-overlay-change' but doesn't use
3154 xmalloc. */
3156 static int
3157 next_overlay_change (pos)
3158 int pos;
3160 int noverlays;
3161 int endpos;
3162 Lisp_Object *overlays;
3163 int i;
3165 /* Get all overlays at the given position. */
3166 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3168 /* If any of these overlays ends before endpos,
3169 use its ending point instead. */
3170 for (i = 0; i < noverlays; ++i)
3172 Lisp_Object oend;
3173 int oendpos;
3175 oend = OVERLAY_END (overlays[i]);
3176 oendpos = OVERLAY_POSITION (oend);
3177 endpos = min (endpos, oendpos);
3180 return endpos;
3185 /***********************************************************************
3186 Fontification
3187 ***********************************************************************/
3189 /* Handle changes in the `fontified' property of the current buffer by
3190 calling hook functions from Qfontification_functions to fontify
3191 regions of text. */
3193 static enum prop_handled
3194 handle_fontified_prop (it)
3195 struct it *it;
3197 Lisp_Object prop, pos;
3198 enum prop_handled handled = HANDLED_NORMALLY;
3200 if (!NILP (Vmemory_full))
3201 return handled;
3203 /* Get the value of the `fontified' property at IT's current buffer
3204 position. (The `fontified' property doesn't have a special
3205 meaning in strings.) If the value is nil, call functions from
3206 Qfontification_functions. */
3207 if (!STRINGP (it->string)
3208 && it->s == NULL
3209 && !NILP (Vfontification_functions)
3210 && !NILP (Vrun_hooks)
3211 && (pos = make_number (IT_CHARPOS (*it)),
3212 prop = Fget_char_property (pos, Qfontified, Qnil),
3213 NILP (prop)))
3215 int count = SPECPDL_INDEX ();
3216 Lisp_Object val;
3218 val = Vfontification_functions;
3219 specbind (Qfontification_functions, Qnil);
3221 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3222 safe_call1 (val, pos);
3223 else
3225 Lisp_Object globals, fn;
3226 struct gcpro gcpro1, gcpro2;
3228 globals = Qnil;
3229 GCPRO2 (val, globals);
3231 for (; CONSP (val); val = XCDR (val))
3233 fn = XCAR (val);
3235 if (EQ (fn, Qt))
3237 /* A value of t indicates this hook has a local
3238 binding; it means to run the global binding too.
3239 In a global value, t should not occur. If it
3240 does, we must ignore it to avoid an endless
3241 loop. */
3242 for (globals = Fdefault_value (Qfontification_functions);
3243 CONSP (globals);
3244 globals = XCDR (globals))
3246 fn = XCAR (globals);
3247 if (!EQ (fn, Qt))
3248 safe_call1 (fn, pos);
3251 else
3252 safe_call1 (fn, pos);
3255 UNGCPRO;
3258 unbind_to (count, Qnil);
3260 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3261 something. This avoids an endless loop if they failed to
3262 fontify the text for which reason ever. */
3263 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3264 handled = HANDLED_RECOMPUTE_PROPS;
3267 return handled;
3272 /***********************************************************************
3273 Faces
3274 ***********************************************************************/
3276 /* Set up iterator IT from face properties at its current position.
3277 Called from handle_stop. */
3279 static enum prop_handled
3280 handle_face_prop (it)
3281 struct it *it;
3283 int new_face_id, next_stop;
3285 if (!STRINGP (it->string))
3287 new_face_id
3288 = face_at_buffer_position (it->w,
3289 IT_CHARPOS (*it),
3290 it->region_beg_charpos,
3291 it->region_end_charpos,
3292 &next_stop,
3293 (IT_CHARPOS (*it)
3294 + TEXT_PROP_DISTANCE_LIMIT),
3297 /* Is this a start of a run of characters with box face?
3298 Caveat: this can be called for a freshly initialized
3299 iterator; face_id is -1 in this case. We know that the new
3300 face will not change until limit, i.e. if the new face has a
3301 box, all characters up to limit will have one. But, as
3302 usual, we don't know whether limit is really the end. */
3303 if (new_face_id != it->face_id)
3305 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3307 /* If new face has a box but old face has not, this is
3308 the start of a run of characters with box, i.e. it has
3309 a shadow on the left side. The value of face_id of the
3310 iterator will be -1 if this is the initial call that gets
3311 the face. In this case, we have to look in front of IT's
3312 position and see whether there is a face != new_face_id. */
3313 it->start_of_box_run_p
3314 = (new_face->box != FACE_NO_BOX
3315 && (it->face_id >= 0
3316 || IT_CHARPOS (*it) == BEG
3317 || new_face_id != face_before_it_pos (it)));
3318 it->face_box_p = new_face->box != FACE_NO_BOX;
3321 else
3323 int base_face_id, bufpos;
3325 if (it->current.overlay_string_index >= 0)
3326 bufpos = IT_CHARPOS (*it);
3327 else
3328 bufpos = 0;
3330 /* For strings from a buffer, i.e. overlay strings or strings
3331 from a `display' property, use the face at IT's current
3332 buffer position as the base face to merge with, so that
3333 overlay strings appear in the same face as surrounding
3334 text, unless they specify their own faces. */
3335 base_face_id = underlying_face_id (it);
3337 new_face_id = face_at_string_position (it->w,
3338 it->string,
3339 IT_STRING_CHARPOS (*it),
3340 bufpos,
3341 it->region_beg_charpos,
3342 it->region_end_charpos,
3343 &next_stop,
3344 base_face_id, 0);
3346 #if 0 /* This shouldn't be neccessary. Let's check it. */
3347 /* If IT is used to display a mode line we would really like to
3348 use the mode line face instead of the frame's default face. */
3349 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3350 && new_face_id == DEFAULT_FACE_ID)
3351 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3352 #endif
3354 /* Is this a start of a run of characters with box? Caveat:
3355 this can be called for a freshly allocated iterator; face_id
3356 is -1 is this case. We know that the new face will not
3357 change until the next check pos, i.e. if the new face has a
3358 box, all characters up to that position will have a
3359 box. But, as usual, we don't know whether that position
3360 is really the end. */
3361 if (new_face_id != it->face_id)
3363 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3364 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3366 /* If new face has a box but old face hasn't, this is the
3367 start of a run of characters with box, i.e. it has a
3368 shadow on the left side. */
3369 it->start_of_box_run_p
3370 = new_face->box && (old_face == NULL || !old_face->box);
3371 it->face_box_p = new_face->box != FACE_NO_BOX;
3375 it->face_id = new_face_id;
3376 return HANDLED_NORMALLY;
3380 /* Return the ID of the face ``underlying'' IT's current position,
3381 which is in a string. If the iterator is associated with a
3382 buffer, return the face at IT's current buffer position.
3383 Otherwise, use the iterator's base_face_id. */
3385 static int
3386 underlying_face_id (it)
3387 struct it *it;
3389 int face_id = it->base_face_id, i;
3391 xassert (STRINGP (it->string));
3393 for (i = it->sp - 1; i >= 0; --i)
3394 if (NILP (it->stack[i].string))
3395 face_id = it->stack[i].face_id;
3397 return face_id;
3401 /* Compute the face one character before or after the current position
3402 of IT. BEFORE_P non-zero means get the face in front of IT's
3403 position. Value is the id of the face. */
3405 static int
3406 face_before_or_after_it_pos (it, before_p)
3407 struct it *it;
3408 int before_p;
3410 int face_id, limit;
3411 int next_check_charpos;
3412 struct text_pos pos;
3414 xassert (it->s == NULL);
3416 if (STRINGP (it->string))
3418 int bufpos, base_face_id;
3420 /* No face change past the end of the string (for the case
3421 we are padding with spaces). No face change before the
3422 string start. */
3423 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3424 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3425 return it->face_id;
3427 /* Set pos to the position before or after IT's current position. */
3428 if (before_p)
3429 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3430 else
3431 /* For composition, we must check the character after the
3432 composition. */
3433 pos = (it->what == IT_COMPOSITION
3434 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3435 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3437 if (it->current.overlay_string_index >= 0)
3438 bufpos = IT_CHARPOS (*it);
3439 else
3440 bufpos = 0;
3442 base_face_id = underlying_face_id (it);
3444 /* Get the face for ASCII, or unibyte. */
3445 face_id = face_at_string_position (it->w,
3446 it->string,
3447 CHARPOS (pos),
3448 bufpos,
3449 it->region_beg_charpos,
3450 it->region_end_charpos,
3451 &next_check_charpos,
3452 base_face_id, 0);
3454 /* Correct the face for charsets different from ASCII. Do it
3455 for the multibyte case only. The face returned above is
3456 suitable for unibyte text if IT->string is unibyte. */
3457 if (STRING_MULTIBYTE (it->string))
3459 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3460 int rest = SBYTES (it->string) - BYTEPOS (pos);
3461 int c, len;
3462 struct face *face = FACE_FROM_ID (it->f, face_id);
3464 c = string_char_and_length (p, rest, &len);
3465 face_id = FACE_FOR_CHAR (it->f, face, c);
3468 else
3470 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3471 || (IT_CHARPOS (*it) <= BEGV && before_p))
3472 return it->face_id;
3474 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3475 pos = it->current.pos;
3477 if (before_p)
3478 DEC_TEXT_POS (pos, it->multibyte_p);
3479 else
3481 if (it->what == IT_COMPOSITION)
3482 /* For composition, we must check the position after the
3483 composition. */
3484 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3485 else
3486 INC_TEXT_POS (pos, it->multibyte_p);
3489 /* Determine face for CHARSET_ASCII, or unibyte. */
3490 face_id = face_at_buffer_position (it->w,
3491 CHARPOS (pos),
3492 it->region_beg_charpos,
3493 it->region_end_charpos,
3494 &next_check_charpos,
3495 limit, 0);
3497 /* Correct the face for charsets different from ASCII. Do it
3498 for the multibyte case only. The face returned above is
3499 suitable for unibyte text if current_buffer is unibyte. */
3500 if (it->multibyte_p)
3502 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3503 struct face *face = FACE_FROM_ID (it->f, face_id);
3504 face_id = FACE_FOR_CHAR (it->f, face, c);
3508 return face_id;
3513 /***********************************************************************
3514 Invisible text
3515 ***********************************************************************/
3517 /* Set up iterator IT from invisible properties at its current
3518 position. Called from handle_stop. */
3520 static enum prop_handled
3521 handle_invisible_prop (it)
3522 struct it *it;
3524 enum prop_handled handled = HANDLED_NORMALLY;
3526 if (STRINGP (it->string))
3528 extern Lisp_Object Qinvisible;
3529 Lisp_Object prop, end_charpos, limit, charpos;
3531 /* Get the value of the invisible text property at the
3532 current position. Value will be nil if there is no such
3533 property. */
3534 charpos = make_number (IT_STRING_CHARPOS (*it));
3535 prop = Fget_text_property (charpos, Qinvisible, it->string);
3537 if (!NILP (prop)
3538 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3540 handled = HANDLED_RECOMPUTE_PROPS;
3542 /* Get the position at which the next change of the
3543 invisible text property can be found in IT->string.
3544 Value will be nil if the property value is the same for
3545 all the rest of IT->string. */
3546 XSETINT (limit, SCHARS (it->string));
3547 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3548 it->string, limit);
3550 /* Text at current position is invisible. The next
3551 change in the property is at position end_charpos.
3552 Move IT's current position to that position. */
3553 if (INTEGERP (end_charpos)
3554 && XFASTINT (end_charpos) < XFASTINT (limit))
3556 struct text_pos old;
3557 old = it->current.string_pos;
3558 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3559 compute_string_pos (&it->current.string_pos, old, it->string);
3561 else
3563 /* The rest of the string is invisible. If this is an
3564 overlay string, proceed with the next overlay string
3565 or whatever comes and return a character from there. */
3566 if (it->current.overlay_string_index >= 0)
3568 next_overlay_string (it);
3569 /* Don't check for overlay strings when we just
3570 finished processing them. */
3571 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3573 else
3575 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3576 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3581 else
3583 int invis_p, newpos, next_stop, start_charpos;
3584 Lisp_Object pos, prop, overlay;
3586 /* First of all, is there invisible text at this position? */
3587 start_charpos = IT_CHARPOS (*it);
3588 pos = make_number (IT_CHARPOS (*it));
3589 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3590 &overlay);
3591 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3593 /* If we are on invisible text, skip over it. */
3594 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3596 /* Record whether we have to display an ellipsis for the
3597 invisible text. */
3598 int display_ellipsis_p = invis_p == 2;
3600 handled = HANDLED_RECOMPUTE_PROPS;
3602 /* Loop skipping over invisible text. The loop is left at
3603 ZV or with IT on the first char being visible again. */
3606 /* Try to skip some invisible text. Return value is the
3607 position reached which can be equal to IT's position
3608 if there is nothing invisible here. This skips both
3609 over invisible text properties and overlays with
3610 invisible property. */
3611 newpos = skip_invisible (IT_CHARPOS (*it),
3612 &next_stop, ZV, it->window);
3614 /* If we skipped nothing at all we weren't at invisible
3615 text in the first place. If everything to the end of
3616 the buffer was skipped, end the loop. */
3617 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3618 invis_p = 0;
3619 else
3621 /* We skipped some characters but not necessarily
3622 all there are. Check if we ended up on visible
3623 text. Fget_char_property returns the property of
3624 the char before the given position, i.e. if we
3625 get invis_p = 0, this means that the char at
3626 newpos is visible. */
3627 pos = make_number (newpos);
3628 prop = Fget_char_property (pos, Qinvisible, it->window);
3629 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3632 /* If we ended up on invisible text, proceed to
3633 skip starting with next_stop. */
3634 if (invis_p)
3635 IT_CHARPOS (*it) = next_stop;
3637 /* If there are adjacent invisible texts, don't lose the
3638 second one's ellipsis. */
3639 if (invis_p == 2)
3640 display_ellipsis_p = 1;
3642 while (invis_p);
3644 /* The position newpos is now either ZV or on visible text. */
3645 IT_CHARPOS (*it) = newpos;
3646 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3648 /* If there are before-strings at the start of invisible
3649 text, and the text is invisible because of a text
3650 property, arrange to show before-strings because 20.x did
3651 it that way. (If the text is invisible because of an
3652 overlay property instead of a text property, this is
3653 already handled in the overlay code.) */
3654 if (NILP (overlay)
3655 && get_overlay_strings (it, start_charpos))
3657 handled = HANDLED_RECOMPUTE_PROPS;
3658 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3660 else if (display_ellipsis_p)
3662 /* Make sure that the glyphs of the ellipsis will get
3663 correct `charpos' values. If we would not update
3664 it->position here, the glyphs would belong to the
3665 last visible character _before_ the invisible
3666 text, which confuses `set_cursor_from_row'.
3668 We use the last invisible position instead of the
3669 first because this way the cursor is always drawn on
3670 the first "." of the ellipsis, whenever PT is inside
3671 the invisible text. Otherwise the cursor would be
3672 placed _after_ the ellipsis when the point is after the
3673 first invisible character. */
3674 if (!STRINGP (it->object))
3676 it->position.charpos = IT_CHARPOS (*it) - 1;
3677 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3679 setup_for_ellipsis (it, 0);
3684 return handled;
3688 /* Make iterator IT return `...' next.
3689 Replaces LEN characters from buffer. */
3691 static void
3692 setup_for_ellipsis (it, len)
3693 struct it *it;
3694 int len;
3696 /* Use the display table definition for `...'. Invalid glyphs
3697 will be handled by the method returning elements from dpvec. */
3698 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3700 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3701 it->dpvec = v->contents;
3702 it->dpend = v->contents + v->size;
3704 else
3706 /* Default `...'. */
3707 it->dpvec = default_invis_vector;
3708 it->dpend = default_invis_vector + 3;
3711 it->dpvec_char_len = len;
3712 it->current.dpvec_index = 0;
3713 it->dpvec_face_id = -1;
3715 /* Remember the current face id in case glyphs specify faces.
3716 IT's face is restored in set_iterator_to_next.
3717 saved_face_id was set to preceding char's face in handle_stop. */
3718 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3719 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3721 it->method = GET_FROM_DISPLAY_VECTOR;
3722 it->ellipsis_p = 1;
3727 /***********************************************************************
3728 'display' property
3729 ***********************************************************************/
3731 /* Set up iterator IT from `display' property at its current position.
3732 Called from handle_stop.
3733 We return HANDLED_RETURN if some part of the display property
3734 overrides the display of the buffer text itself.
3735 Otherwise we return HANDLED_NORMALLY. */
3737 static enum prop_handled
3738 handle_display_prop (it)
3739 struct it *it;
3741 Lisp_Object prop, object;
3742 struct text_pos *position;
3743 /* Nonzero if some property replaces the display of the text itself. */
3744 int display_replaced_p = 0;
3746 if (STRINGP (it->string))
3748 object = it->string;
3749 position = &it->current.string_pos;
3751 else
3753 XSETWINDOW (object, it->w);
3754 position = &it->current.pos;
3757 /* Reset those iterator values set from display property values. */
3758 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3759 it->space_width = Qnil;
3760 it->font_height = Qnil;
3761 it->voffset = 0;
3763 /* We don't support recursive `display' properties, i.e. string
3764 values that have a string `display' property, that have a string
3765 `display' property etc. */
3766 if (!it->string_from_display_prop_p)
3767 it->area = TEXT_AREA;
3769 prop = Fget_char_property (make_number (position->charpos),
3770 Qdisplay, object);
3771 if (NILP (prop))
3772 return HANDLED_NORMALLY;
3774 if (!STRINGP (it->string))
3775 object = it->w->buffer;
3777 if (CONSP (prop)
3778 /* Simple properties. */
3779 && !EQ (XCAR (prop), Qimage)
3780 && !EQ (XCAR (prop), Qspace)
3781 && !EQ (XCAR (prop), Qwhen)
3782 && !EQ (XCAR (prop), Qslice)
3783 && !EQ (XCAR (prop), Qspace_width)
3784 && !EQ (XCAR (prop), Qheight)
3785 && !EQ (XCAR (prop), Qraise)
3786 /* Marginal area specifications. */
3787 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3788 && !EQ (XCAR (prop), Qleft_fringe)
3789 && !EQ (XCAR (prop), Qright_fringe)
3790 && !NILP (XCAR (prop)))
3792 for (; CONSP (prop); prop = XCDR (prop))
3794 if (handle_single_display_spec (it, XCAR (prop), object,
3795 position, display_replaced_p))
3796 display_replaced_p = 1;
3799 else if (VECTORP (prop))
3801 int i;
3802 for (i = 0; i < ASIZE (prop); ++i)
3803 if (handle_single_display_spec (it, AREF (prop, i), object,
3804 position, display_replaced_p))
3805 display_replaced_p = 1;
3807 else
3809 int ret = handle_single_display_spec (it, prop, object, position, 0);
3810 if (ret < 0) /* Replaced by "", i.e. nothing. */
3811 return HANDLED_RECOMPUTE_PROPS;
3812 if (ret)
3813 display_replaced_p = 1;
3816 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3820 /* Value is the position of the end of the `display' property starting
3821 at START_POS in OBJECT. */
3823 static struct text_pos
3824 display_prop_end (it, object, start_pos)
3825 struct it *it;
3826 Lisp_Object object;
3827 struct text_pos start_pos;
3829 Lisp_Object end;
3830 struct text_pos end_pos;
3832 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3833 Qdisplay, object, Qnil);
3834 CHARPOS (end_pos) = XFASTINT (end);
3835 if (STRINGP (object))
3836 compute_string_pos (&end_pos, start_pos, it->string);
3837 else
3838 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3840 return end_pos;
3844 /* Set up IT from a single `display' specification PROP. OBJECT
3845 is the object in which the `display' property was found. *POSITION
3846 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3847 means that we previously saw a display specification which already
3848 replaced text display with something else, for example an image;
3849 we ignore such properties after the first one has been processed.
3851 If PROP is a `space' or `image' specification, and in some other
3852 cases too, set *POSITION to the position where the `display'
3853 property ends.
3855 Value is non-zero if something was found which replaces the display
3856 of buffer or string text. Specifically, the value is -1 if that
3857 "something" is "nothing". */
3859 static int
3860 handle_single_display_spec (it, spec, object, position,
3861 display_replaced_before_p)
3862 struct it *it;
3863 Lisp_Object spec;
3864 Lisp_Object object;
3865 struct text_pos *position;
3866 int display_replaced_before_p;
3868 Lisp_Object form;
3869 Lisp_Object location, value;
3870 struct text_pos start_pos;
3871 int valid_p;
3873 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3874 If the result is non-nil, use VALUE instead of SPEC. */
3875 form = Qt;
3876 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3878 spec = XCDR (spec);
3879 if (!CONSP (spec))
3880 return 0;
3881 form = XCAR (spec);
3882 spec = XCDR (spec);
3885 if (!NILP (form) && !EQ (form, Qt))
3887 int count = SPECPDL_INDEX ();
3888 struct gcpro gcpro1;
3890 /* Bind `object' to the object having the `display' property, a
3891 buffer or string. Bind `position' to the position in the
3892 object where the property was found, and `buffer-position'
3893 to the current position in the buffer. */
3894 specbind (Qobject, object);
3895 specbind (Qposition, make_number (CHARPOS (*position)));
3896 specbind (Qbuffer_position,
3897 make_number (STRINGP (object)
3898 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3899 GCPRO1 (form);
3900 form = safe_eval (form);
3901 UNGCPRO;
3902 unbind_to (count, Qnil);
3905 if (NILP (form))
3906 return 0;
3908 /* Handle `(height HEIGHT)' specifications. */
3909 if (CONSP (spec)
3910 && EQ (XCAR (spec), Qheight)
3911 && CONSP (XCDR (spec)))
3913 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3914 return 0;
3916 it->font_height = XCAR (XCDR (spec));
3917 if (!NILP (it->font_height))
3919 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3920 int new_height = -1;
3922 if (CONSP (it->font_height)
3923 && (EQ (XCAR (it->font_height), Qplus)
3924 || EQ (XCAR (it->font_height), Qminus))
3925 && CONSP (XCDR (it->font_height))
3926 && INTEGERP (XCAR (XCDR (it->font_height))))
3928 /* `(+ N)' or `(- N)' where N is an integer. */
3929 int steps = XINT (XCAR (XCDR (it->font_height)));
3930 if (EQ (XCAR (it->font_height), Qplus))
3931 steps = - steps;
3932 it->face_id = smaller_face (it->f, it->face_id, steps);
3934 else if (FUNCTIONP (it->font_height))
3936 /* Call function with current height as argument.
3937 Value is the new height. */
3938 Lisp_Object height;
3939 height = safe_call1 (it->font_height,
3940 face->lface[LFACE_HEIGHT_INDEX]);
3941 if (NUMBERP (height))
3942 new_height = XFLOATINT (height);
3944 else if (NUMBERP (it->font_height))
3946 /* Value is a multiple of the canonical char height. */
3947 struct face *face;
3949 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3950 new_height = (XFLOATINT (it->font_height)
3951 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3953 else
3955 /* Evaluate IT->font_height with `height' bound to the
3956 current specified height to get the new height. */
3957 int count = SPECPDL_INDEX ();
3959 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3960 value = safe_eval (it->font_height);
3961 unbind_to (count, Qnil);
3963 if (NUMBERP (value))
3964 new_height = XFLOATINT (value);
3967 if (new_height > 0)
3968 it->face_id = face_with_height (it->f, it->face_id, new_height);
3971 return 0;
3974 /* Handle `(space_width WIDTH)'. */
3975 if (CONSP (spec)
3976 && EQ (XCAR (spec), Qspace_width)
3977 && CONSP (XCDR (spec)))
3979 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3980 return 0;
3982 value = XCAR (XCDR (spec));
3983 if (NUMBERP (value) && XFLOATINT (value) > 0)
3984 it->space_width = value;
3986 return 0;
3989 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3990 if (CONSP (spec)
3991 && EQ (XCAR (spec), Qslice))
3993 Lisp_Object tem;
3995 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3996 return 0;
3998 if (tem = XCDR (spec), CONSP (tem))
4000 it->slice.x = XCAR (tem);
4001 if (tem = XCDR (tem), CONSP (tem))
4003 it->slice.y = XCAR (tem);
4004 if (tem = XCDR (tem), CONSP (tem))
4006 it->slice.width = XCAR (tem);
4007 if (tem = XCDR (tem), CONSP (tem))
4008 it->slice.height = XCAR (tem);
4013 return 0;
4016 /* Handle `(raise FACTOR)'. */
4017 if (CONSP (spec)
4018 && EQ (XCAR (spec), Qraise)
4019 && CONSP (XCDR (spec)))
4021 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4022 return 0;
4024 #ifdef HAVE_WINDOW_SYSTEM
4025 value = XCAR (XCDR (spec));
4026 if (NUMBERP (value))
4028 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4029 it->voffset = - (XFLOATINT (value)
4030 * (FONT_HEIGHT (face->font)));
4032 #endif /* HAVE_WINDOW_SYSTEM */
4034 return 0;
4037 /* Don't handle the other kinds of display specifications
4038 inside a string that we got from a `display' property. */
4039 if (it->string_from_display_prop_p)
4040 return 0;
4042 /* Characters having this form of property are not displayed, so
4043 we have to find the end of the property. */
4044 start_pos = *position;
4045 *position = display_prop_end (it, object, start_pos);
4046 value = Qnil;
4048 /* Stop the scan at that end position--we assume that all
4049 text properties change there. */
4050 it->stop_charpos = position->charpos;
4052 /* Handle `(left-fringe BITMAP [FACE])'
4053 and `(right-fringe BITMAP [FACE])'. */
4054 if (CONSP (spec)
4055 && (EQ (XCAR (spec), Qleft_fringe)
4056 || EQ (XCAR (spec), Qright_fringe))
4057 && CONSP (XCDR (spec)))
4059 int face_id = DEFAULT_FACE_ID;
4060 int fringe_bitmap;
4062 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4063 /* If we return here, POSITION has been advanced
4064 across the text with this property. */
4065 return 0;
4067 #ifdef HAVE_WINDOW_SYSTEM
4068 value = XCAR (XCDR (spec));
4069 if (!SYMBOLP (value)
4070 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4071 /* If we return here, POSITION has been advanced
4072 across the text with this property. */
4073 return 0;
4075 if (CONSP (XCDR (XCDR (spec))))
4077 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4078 int face_id2 = lookup_derived_face (it->f, face_name,
4079 'A', FRINGE_FACE_ID, 0);
4080 if (face_id2 >= 0)
4081 face_id = face_id2;
4084 /* Save current settings of IT so that we can restore them
4085 when we are finished with the glyph property value. */
4087 push_it (it);
4089 it->area = TEXT_AREA;
4090 it->what = IT_IMAGE;
4091 it->image_id = -1; /* no image */
4092 it->position = start_pos;
4093 it->object = NILP (object) ? it->w->buffer : object;
4094 it->method = GET_FROM_IMAGE;
4095 it->face_id = face_id;
4097 /* Say that we haven't consumed the characters with
4098 `display' property yet. The call to pop_it in
4099 set_iterator_to_next will clean this up. */
4100 *position = start_pos;
4102 if (EQ (XCAR (spec), Qleft_fringe))
4104 it->left_user_fringe_bitmap = fringe_bitmap;
4105 it->left_user_fringe_face_id = face_id;
4107 else
4109 it->right_user_fringe_bitmap = fringe_bitmap;
4110 it->right_user_fringe_face_id = face_id;
4112 #endif /* HAVE_WINDOW_SYSTEM */
4113 return 1;
4116 /* Prepare to handle `((margin left-margin) ...)',
4117 `((margin right-margin) ...)' and `((margin nil) ...)'
4118 prefixes for display specifications. */
4119 location = Qunbound;
4120 if (CONSP (spec) && CONSP (XCAR (spec)))
4122 Lisp_Object tem;
4124 value = XCDR (spec);
4125 if (CONSP (value))
4126 value = XCAR (value);
4128 tem = XCAR (spec);
4129 if (EQ (XCAR (tem), Qmargin)
4130 && (tem = XCDR (tem),
4131 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4132 (NILP (tem)
4133 || EQ (tem, Qleft_margin)
4134 || EQ (tem, Qright_margin))))
4135 location = tem;
4138 if (EQ (location, Qunbound))
4140 location = Qnil;
4141 value = spec;
4144 /* After this point, VALUE is the property after any
4145 margin prefix has been stripped. It must be a string,
4146 an image specification, or `(space ...)'.
4148 LOCATION specifies where to display: `left-margin',
4149 `right-margin' or nil. */
4151 valid_p = (STRINGP (value)
4152 #ifdef HAVE_WINDOW_SYSTEM
4153 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4154 #endif /* not HAVE_WINDOW_SYSTEM */
4155 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4157 if (valid_p && !display_replaced_before_p)
4159 /* Save current settings of IT so that we can restore them
4160 when we are finished with the glyph property value. */
4161 push_it (it);
4163 if (NILP (location))
4164 it->area = TEXT_AREA;
4165 else if (EQ (location, Qleft_margin))
4166 it->area = LEFT_MARGIN_AREA;
4167 else
4168 it->area = RIGHT_MARGIN_AREA;
4170 if (STRINGP (value))
4172 if (SCHARS (value) == 0)
4174 pop_it (it);
4175 return -1; /* Replaced by "", i.e. nothing. */
4177 it->string = value;
4178 it->multibyte_p = STRING_MULTIBYTE (it->string);
4179 it->current.overlay_string_index = -1;
4180 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4181 it->end_charpos = it->string_nchars = SCHARS (it->string);
4182 it->method = GET_FROM_STRING;
4183 it->stop_charpos = 0;
4184 it->string_from_display_prop_p = 1;
4185 /* Say that we haven't consumed the characters with
4186 `display' property yet. The call to pop_it in
4187 set_iterator_to_next will clean this up. */
4188 *position = start_pos;
4190 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4192 it->method = GET_FROM_STRETCH;
4193 it->object = value;
4194 *position = it->position = start_pos;
4196 #ifdef HAVE_WINDOW_SYSTEM
4197 else
4199 it->what = IT_IMAGE;
4200 it->image_id = lookup_image (it->f, value);
4201 it->position = start_pos;
4202 it->object = NILP (object) ? it->w->buffer : object;
4203 it->method = GET_FROM_IMAGE;
4205 /* Say that we haven't consumed the characters with
4206 `display' property yet. The call to pop_it in
4207 set_iterator_to_next will clean this up. */
4208 *position = start_pos;
4210 #endif /* HAVE_WINDOW_SYSTEM */
4212 return 1;
4215 /* Invalid property or property not supported. Restore
4216 POSITION to what it was before. */
4217 *position = start_pos;
4218 return 0;
4222 /* Check if SPEC is a display specification value whose text should be
4223 treated as intangible. */
4225 static int
4226 single_display_spec_intangible_p (prop)
4227 Lisp_Object prop;
4229 /* Skip over `when FORM'. */
4230 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4232 prop = XCDR (prop);
4233 if (!CONSP (prop))
4234 return 0;
4235 prop = XCDR (prop);
4238 if (STRINGP (prop))
4239 return 1;
4241 if (!CONSP (prop))
4242 return 0;
4244 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4245 we don't need to treat text as intangible. */
4246 if (EQ (XCAR (prop), Qmargin))
4248 prop = XCDR (prop);
4249 if (!CONSP (prop))
4250 return 0;
4252 prop = XCDR (prop);
4253 if (!CONSP (prop)
4254 || EQ (XCAR (prop), Qleft_margin)
4255 || EQ (XCAR (prop), Qright_margin))
4256 return 0;
4259 return (CONSP (prop)
4260 && (EQ (XCAR (prop), Qimage)
4261 || EQ (XCAR (prop), Qspace)));
4265 /* Check if PROP is a display property value whose text should be
4266 treated as intangible. */
4269 display_prop_intangible_p (prop)
4270 Lisp_Object prop;
4272 if (CONSP (prop)
4273 && CONSP (XCAR (prop))
4274 && !EQ (Qmargin, XCAR (XCAR (prop))))
4276 /* A list of sub-properties. */
4277 while (CONSP (prop))
4279 if (single_display_spec_intangible_p (XCAR (prop)))
4280 return 1;
4281 prop = XCDR (prop);
4284 else if (VECTORP (prop))
4286 /* A vector of sub-properties. */
4287 int i;
4288 for (i = 0; i < ASIZE (prop); ++i)
4289 if (single_display_spec_intangible_p (AREF (prop, i)))
4290 return 1;
4292 else
4293 return single_display_spec_intangible_p (prop);
4295 return 0;
4299 /* Return 1 if PROP is a display sub-property value containing STRING. */
4301 static int
4302 single_display_spec_string_p (prop, string)
4303 Lisp_Object prop, string;
4305 if (EQ (string, prop))
4306 return 1;
4308 /* Skip over `when FORM'. */
4309 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4311 prop = XCDR (prop);
4312 if (!CONSP (prop))
4313 return 0;
4314 prop = XCDR (prop);
4317 if (CONSP (prop))
4318 /* Skip over `margin LOCATION'. */
4319 if (EQ (XCAR (prop), Qmargin))
4321 prop = XCDR (prop);
4322 if (!CONSP (prop))
4323 return 0;
4325 prop = XCDR (prop);
4326 if (!CONSP (prop))
4327 return 0;
4330 return CONSP (prop) && EQ (XCAR (prop), string);
4334 /* Return 1 if STRING appears in the `display' property PROP. */
4336 static int
4337 display_prop_string_p (prop, string)
4338 Lisp_Object prop, string;
4340 if (CONSP (prop)
4341 && CONSP (XCAR (prop))
4342 && !EQ (Qmargin, XCAR (XCAR (prop))))
4344 /* A list of sub-properties. */
4345 while (CONSP (prop))
4347 if (single_display_spec_string_p (XCAR (prop), string))
4348 return 1;
4349 prop = XCDR (prop);
4352 else if (VECTORP (prop))
4354 /* A vector of sub-properties. */
4355 int i;
4356 for (i = 0; i < ASIZE (prop); ++i)
4357 if (single_display_spec_string_p (AREF (prop, i), string))
4358 return 1;
4360 else
4361 return single_display_spec_string_p (prop, string);
4363 return 0;
4367 /* Determine from which buffer position in W's buffer STRING comes
4368 from. AROUND_CHARPOS is an approximate position where it could
4369 be from. Value is the buffer position or 0 if it couldn't be
4370 determined.
4372 W's buffer must be current.
4374 This function is necessary because we don't record buffer positions
4375 in glyphs generated from strings (to keep struct glyph small).
4376 This function may only use code that doesn't eval because it is
4377 called asynchronously from note_mouse_highlight. */
4380 string_buffer_position (w, string, around_charpos)
4381 struct window *w;
4382 Lisp_Object string;
4383 int around_charpos;
4385 Lisp_Object limit, prop, pos;
4386 const int MAX_DISTANCE = 1000;
4387 int found = 0;
4389 pos = make_number (around_charpos);
4390 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
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 = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4400 if (!found)
4402 pos = make_number (around_charpos);
4403 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4404 while (!found && !EQ (pos, limit))
4406 prop = Fget_char_property (pos, Qdisplay, Qnil);
4407 if (!NILP (prop) && display_prop_string_p (prop, string))
4408 found = 1;
4409 else
4410 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4411 limit);
4415 return found ? XINT (pos) : 0;
4420 /***********************************************************************
4421 `composition' property
4422 ***********************************************************************/
4424 /* Set up iterator IT from `composition' property at its current
4425 position. Called from handle_stop. */
4427 static enum prop_handled
4428 handle_composition_prop (it)
4429 struct it *it;
4431 Lisp_Object prop, string;
4432 int pos, pos_byte, end;
4433 enum prop_handled handled = HANDLED_NORMALLY;
4435 if (STRINGP (it->string))
4437 pos = IT_STRING_CHARPOS (*it);
4438 pos_byte = IT_STRING_BYTEPOS (*it);
4439 string = it->string;
4441 else
4443 pos = IT_CHARPOS (*it);
4444 pos_byte = IT_BYTEPOS (*it);
4445 string = Qnil;
4448 /* If there's a valid composition and point is not inside of the
4449 composition (in the case that the composition is from the current
4450 buffer), draw a glyph composed from the composition components. */
4451 if (find_composition (pos, -1, &pos, &end, &prop, string)
4452 && COMPOSITION_VALID_P (pos, end, prop)
4453 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4455 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4457 if (id >= 0)
4459 struct composition *cmp = composition_table[id];
4461 if (cmp->glyph_len == 0)
4463 /* No glyph. */
4464 if (STRINGP (it->string))
4466 IT_STRING_CHARPOS (*it) = end;
4467 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4468 end);
4470 else
4472 IT_CHARPOS (*it) = end;
4473 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4475 return HANDLED_RECOMPUTE_PROPS;
4477 it->method = GET_FROM_COMPOSITION;
4478 it->cmp_id = id;
4479 it->cmp_len = COMPOSITION_LENGTH (prop);
4480 /* For a terminal, draw only the first character of the
4481 components. */
4482 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4483 it->len = (STRINGP (it->string)
4484 ? string_char_to_byte (it->string, end)
4485 : CHAR_TO_BYTE (end)) - pos_byte;
4486 it->stop_charpos = end;
4487 handled = HANDLED_RETURN;
4491 return handled;
4496 /***********************************************************************
4497 Overlay strings
4498 ***********************************************************************/
4500 /* The following structure is used to record overlay strings for
4501 later sorting in load_overlay_strings. */
4503 struct overlay_entry
4505 Lisp_Object overlay;
4506 Lisp_Object string;
4507 int priority;
4508 int after_string_p;
4512 /* Set up iterator IT from overlay strings at its current position.
4513 Called from handle_stop. */
4515 static enum prop_handled
4516 handle_overlay_change (it)
4517 struct it *it;
4519 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4520 return HANDLED_RECOMPUTE_PROPS;
4521 else
4522 return HANDLED_NORMALLY;
4526 /* Set up the next overlay string for delivery by IT, if there is an
4527 overlay string to deliver. Called by set_iterator_to_next when the
4528 end of the current overlay string is reached. If there are more
4529 overlay strings to display, IT->string and
4530 IT->current.overlay_string_index are set appropriately here.
4531 Otherwise IT->string is set to nil. */
4533 static void
4534 next_overlay_string (it)
4535 struct it *it;
4537 ++it->current.overlay_string_index;
4538 if (it->current.overlay_string_index == it->n_overlay_strings)
4540 /* No more overlay strings. Restore IT's settings to what
4541 they were before overlay strings were processed, and
4542 continue to deliver from current_buffer. */
4543 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4545 pop_it (it);
4546 xassert (it->stop_charpos >= BEGV
4547 && it->stop_charpos <= it->end_charpos);
4548 it->string = Qnil;
4549 it->current.overlay_string_index = -1;
4550 SET_TEXT_POS (it->current.string_pos, -1, -1);
4551 it->n_overlay_strings = 0;
4552 it->method = GET_FROM_BUFFER;
4554 /* If we're at the end of the buffer, record that we have
4555 processed the overlay strings there already, so that
4556 next_element_from_buffer doesn't try it again. */
4557 if (IT_CHARPOS (*it) >= it->end_charpos)
4558 it->overlay_strings_at_end_processed_p = 1;
4560 /* If we have to display `...' for invisible text, set
4561 the iterator up for that. */
4562 if (display_ellipsis_p)
4563 setup_for_ellipsis (it, 0);
4565 else
4567 /* There are more overlay strings to process. If
4568 IT->current.overlay_string_index has advanced to a position
4569 where we must load IT->overlay_strings with more strings, do
4570 it. */
4571 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4573 if (it->current.overlay_string_index && i == 0)
4574 load_overlay_strings (it, 0);
4576 /* Initialize IT to deliver display elements from the overlay
4577 string. */
4578 it->string = it->overlay_strings[i];
4579 it->multibyte_p = STRING_MULTIBYTE (it->string);
4580 SET_TEXT_POS (it->current.string_pos, 0, 0);
4581 it->method = GET_FROM_STRING;
4582 it->stop_charpos = 0;
4585 CHECK_IT (it);
4589 /* Compare two overlay_entry structures E1 and E2. Used as a
4590 comparison function for qsort in load_overlay_strings. Overlay
4591 strings for the same position are sorted so that
4593 1. All after-strings come in front of before-strings, except
4594 when they come from the same overlay.
4596 2. Within after-strings, strings are sorted so that overlay strings
4597 from overlays with higher priorities come first.
4599 2. Within before-strings, strings are sorted so that overlay
4600 strings from overlays with higher priorities come last.
4602 Value is analogous to strcmp. */
4605 static int
4606 compare_overlay_entries (e1, e2)
4607 void *e1, *e2;
4609 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4610 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4611 int result;
4613 if (entry1->after_string_p != entry2->after_string_p)
4615 /* Let after-strings appear in front of before-strings if
4616 they come from different overlays. */
4617 if (EQ (entry1->overlay, entry2->overlay))
4618 result = entry1->after_string_p ? 1 : -1;
4619 else
4620 result = entry1->after_string_p ? -1 : 1;
4622 else if (entry1->after_string_p)
4623 /* After-strings sorted in order of decreasing priority. */
4624 result = entry2->priority - entry1->priority;
4625 else
4626 /* Before-strings sorted in order of increasing priority. */
4627 result = entry1->priority - entry2->priority;
4629 return result;
4633 /* Load the vector IT->overlay_strings with overlay strings from IT's
4634 current buffer position, or from CHARPOS if that is > 0. Set
4635 IT->n_overlays to the total number of overlay strings found.
4637 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4638 a time. On entry into load_overlay_strings,
4639 IT->current.overlay_string_index gives the number of overlay
4640 strings that have already been loaded by previous calls to this
4641 function.
4643 IT->add_overlay_start contains an additional overlay start
4644 position to consider for taking overlay strings from, if non-zero.
4645 This position comes into play when the overlay has an `invisible'
4646 property, and both before and after-strings. When we've skipped to
4647 the end of the overlay, because of its `invisible' property, we
4648 nevertheless want its before-string to appear.
4649 IT->add_overlay_start will contain the overlay start position
4650 in this case.
4652 Overlay strings are sorted so that after-string strings come in
4653 front of before-string strings. Within before and after-strings,
4654 strings are sorted by overlay priority. See also function
4655 compare_overlay_entries. */
4657 static void
4658 load_overlay_strings (it, charpos)
4659 struct it *it;
4660 int charpos;
4662 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4663 Lisp_Object overlay, window, str, invisible;
4664 struct Lisp_Overlay *ov;
4665 int start, end;
4666 int size = 20;
4667 int n = 0, i, j, invis_p;
4668 struct overlay_entry *entries
4669 = (struct overlay_entry *) alloca (size * sizeof *entries);
4671 if (charpos <= 0)
4672 charpos = IT_CHARPOS (*it);
4674 /* Append the overlay string STRING of overlay OVERLAY to vector
4675 `entries' which has size `size' and currently contains `n'
4676 elements. AFTER_P non-zero means STRING is an after-string of
4677 OVERLAY. */
4678 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4679 do \
4681 Lisp_Object priority; \
4683 if (n == size) \
4685 int new_size = 2 * size; \
4686 struct overlay_entry *old = entries; \
4687 entries = \
4688 (struct overlay_entry *) alloca (new_size \
4689 * sizeof *entries); \
4690 bcopy (old, entries, size * sizeof *entries); \
4691 size = new_size; \
4694 entries[n].string = (STRING); \
4695 entries[n].overlay = (OVERLAY); \
4696 priority = Foverlay_get ((OVERLAY), Qpriority); \
4697 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4698 entries[n].after_string_p = (AFTER_P); \
4699 ++n; \
4701 while (0)
4703 /* Process overlay before the overlay center. */
4704 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4706 XSETMISC (overlay, ov);
4707 xassert (OVERLAYP (overlay));
4708 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4709 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4711 if (end < charpos)
4712 break;
4714 /* Skip this overlay if it doesn't start or end at IT's current
4715 position. */
4716 if (end != charpos && start != charpos)
4717 continue;
4719 /* Skip this overlay if it doesn't apply to IT->w. */
4720 window = Foverlay_get (overlay, Qwindow);
4721 if (WINDOWP (window) && XWINDOW (window) != it->w)
4722 continue;
4724 /* If the text ``under'' the overlay is invisible, both before-
4725 and after-strings from this overlay are visible; start and
4726 end position are indistinguishable. */
4727 invisible = Foverlay_get (overlay, Qinvisible);
4728 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4730 /* If overlay has a non-empty before-string, record it. */
4731 if ((start == charpos || (end == charpos && invis_p))
4732 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4733 && SCHARS (str))
4734 RECORD_OVERLAY_STRING (overlay, str, 0);
4736 /* If overlay has a non-empty after-string, record it. */
4737 if ((end == charpos || (start == charpos && invis_p))
4738 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4739 && SCHARS (str))
4740 RECORD_OVERLAY_STRING (overlay, str, 1);
4743 /* Process overlays after the overlay center. */
4744 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4746 XSETMISC (overlay, ov);
4747 xassert (OVERLAYP (overlay));
4748 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4749 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4751 if (start > charpos)
4752 break;
4754 /* Skip this overlay if it doesn't start or end at IT's current
4755 position. */
4756 if (end != charpos && start != charpos)
4757 continue;
4759 /* Skip this overlay if it doesn't apply to IT->w. */
4760 window = Foverlay_get (overlay, Qwindow);
4761 if (WINDOWP (window) && XWINDOW (window) != it->w)
4762 continue;
4764 /* If the text ``under'' the overlay is invisible, it has a zero
4765 dimension, and both before- and after-strings apply. */
4766 invisible = Foverlay_get (overlay, Qinvisible);
4767 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4769 /* If overlay has a non-empty before-string, record it. */
4770 if ((start == charpos || (end == charpos && invis_p))
4771 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4772 && SCHARS (str))
4773 RECORD_OVERLAY_STRING (overlay, str, 0);
4775 /* If overlay has a non-empty after-string, record it. */
4776 if ((end == charpos || (start == charpos && invis_p))
4777 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4778 && SCHARS (str))
4779 RECORD_OVERLAY_STRING (overlay, str, 1);
4782 #undef RECORD_OVERLAY_STRING
4784 /* Sort entries. */
4785 if (n > 1)
4786 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4788 /* Record the total number of strings to process. */
4789 it->n_overlay_strings = n;
4791 /* IT->current.overlay_string_index is the number of overlay strings
4792 that have already been consumed by IT. Copy some of the
4793 remaining overlay strings to IT->overlay_strings. */
4794 i = 0;
4795 j = it->current.overlay_string_index;
4796 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4797 it->overlay_strings[i++] = entries[j++].string;
4799 CHECK_IT (it);
4803 /* Get the first chunk of overlay strings at IT's current buffer
4804 position, or at CHARPOS if that is > 0. Value is non-zero if at
4805 least one overlay string was found. */
4807 static int
4808 get_overlay_strings (it, charpos)
4809 struct it *it;
4810 int charpos;
4812 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4813 process. This fills IT->overlay_strings with strings, and sets
4814 IT->n_overlay_strings to the total number of strings to process.
4815 IT->pos.overlay_string_index has to be set temporarily to zero
4816 because load_overlay_strings needs this; it must be set to -1
4817 when no overlay strings are found because a zero value would
4818 indicate a position in the first overlay string. */
4819 it->current.overlay_string_index = 0;
4820 load_overlay_strings (it, charpos);
4822 /* If we found overlay strings, set up IT to deliver display
4823 elements from the first one. Otherwise set up IT to deliver
4824 from current_buffer. */
4825 if (it->n_overlay_strings)
4827 /* Make sure we know settings in current_buffer, so that we can
4828 restore meaningful values when we're done with the overlay
4829 strings. */
4830 compute_stop_pos (it);
4831 xassert (it->face_id >= 0);
4833 /* Save IT's settings. They are restored after all overlay
4834 strings have been processed. */
4835 xassert (it->sp == 0);
4836 push_it (it);
4838 /* Set up IT to deliver display elements from the first overlay
4839 string. */
4840 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4841 it->string = it->overlay_strings[0];
4842 it->stop_charpos = 0;
4843 xassert (STRINGP (it->string));
4844 it->end_charpos = SCHARS (it->string);
4845 it->multibyte_p = STRING_MULTIBYTE (it->string);
4846 it->method = GET_FROM_STRING;
4848 else
4850 it->string = Qnil;
4851 it->current.overlay_string_index = -1;
4852 it->method = GET_FROM_BUFFER;
4855 CHECK_IT (it);
4857 /* Value is non-zero if we found at least one overlay string. */
4858 return STRINGP (it->string);
4863 /***********************************************************************
4864 Saving and restoring state
4865 ***********************************************************************/
4867 /* Save current settings of IT on IT->stack. Called, for example,
4868 before setting up IT for an overlay string, to be able to restore
4869 IT's settings to what they were after the overlay string has been
4870 processed. */
4872 static void
4873 push_it (it)
4874 struct it *it;
4876 struct iterator_stack_entry *p;
4878 xassert (it->sp < 2);
4879 p = it->stack + it->sp;
4881 p->stop_charpos = it->stop_charpos;
4882 xassert (it->face_id >= 0);
4883 p->face_id = it->face_id;
4884 p->string = it->string;
4885 p->pos = it->current;
4886 p->end_charpos = it->end_charpos;
4887 p->string_nchars = it->string_nchars;
4888 p->area = it->area;
4889 p->multibyte_p = it->multibyte_p;
4890 p->slice = it->slice;
4891 p->space_width = it->space_width;
4892 p->font_height = it->font_height;
4893 p->voffset = it->voffset;
4894 p->string_from_display_prop_p = it->string_from_display_prop_p;
4895 p->display_ellipsis_p = 0;
4896 ++it->sp;
4900 /* Restore IT's settings from IT->stack. Called, for example, when no
4901 more overlay strings must be processed, and we return to delivering
4902 display elements from a buffer, or when the end of a string from a
4903 `display' property is reached and we return to delivering display
4904 elements from an overlay string, or from a buffer. */
4906 static void
4907 pop_it (it)
4908 struct it *it;
4910 struct iterator_stack_entry *p;
4912 xassert (it->sp > 0);
4913 --it->sp;
4914 p = it->stack + it->sp;
4915 it->stop_charpos = p->stop_charpos;
4916 it->face_id = p->face_id;
4917 it->string = p->string;
4918 it->current = p->pos;
4919 it->end_charpos = p->end_charpos;
4920 it->string_nchars = p->string_nchars;
4921 it->area = p->area;
4922 it->multibyte_p = p->multibyte_p;
4923 it->slice = p->slice;
4924 it->space_width = p->space_width;
4925 it->font_height = p->font_height;
4926 it->voffset = p->voffset;
4927 it->string_from_display_prop_p = p->string_from_display_prop_p;
4932 /***********************************************************************
4933 Moving over lines
4934 ***********************************************************************/
4936 /* Set IT's current position to the previous line start. */
4938 static void
4939 back_to_previous_line_start (it)
4940 struct it *it;
4942 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4943 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4947 /* Move IT to the next line start.
4949 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4950 we skipped over part of the text (as opposed to moving the iterator
4951 continuously over the text). Otherwise, don't change the value
4952 of *SKIPPED_P.
4954 Newlines may come from buffer text, overlay strings, or strings
4955 displayed via the `display' property. That's the reason we can't
4956 simply use find_next_newline_no_quit.
4958 Note that this function may not skip over invisible text that is so
4959 because of text properties and immediately follows a newline. If
4960 it would, function reseat_at_next_visible_line_start, when called
4961 from set_iterator_to_next, would effectively make invisible
4962 characters following a newline part of the wrong glyph row, which
4963 leads to wrong cursor motion. */
4965 static int
4966 forward_to_next_line_start (it, skipped_p)
4967 struct it *it;
4968 int *skipped_p;
4970 int old_selective, newline_found_p, n;
4971 const int MAX_NEWLINE_DISTANCE = 500;
4973 /* If already on a newline, just consume it to avoid unintended
4974 skipping over invisible text below. */
4975 if (it->what == IT_CHARACTER
4976 && it->c == '\n'
4977 && CHARPOS (it->position) == IT_CHARPOS (*it))
4979 set_iterator_to_next (it, 0);
4980 it->c = 0;
4981 return 1;
4984 /* Don't handle selective display in the following. It's (a)
4985 unnecessary because it's done by the caller, and (b) leads to an
4986 infinite recursion because next_element_from_ellipsis indirectly
4987 calls this function. */
4988 old_selective = it->selective;
4989 it->selective = 0;
4991 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4992 from buffer text. */
4993 for (n = newline_found_p = 0;
4994 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4995 n += STRINGP (it->string) ? 0 : 1)
4997 if (!get_next_display_element (it))
4998 return 0;
4999 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5000 set_iterator_to_next (it, 0);
5003 /* If we didn't find a newline near enough, see if we can use a
5004 short-cut. */
5005 if (!newline_found_p)
5007 int start = IT_CHARPOS (*it);
5008 int limit = find_next_newline_no_quit (start, 1);
5009 Lisp_Object pos;
5011 xassert (!STRINGP (it->string));
5013 /* If there isn't any `display' property in sight, and no
5014 overlays, we can just use the position of the newline in
5015 buffer text. */
5016 if (it->stop_charpos >= limit
5017 || ((pos = Fnext_single_property_change (make_number (start),
5018 Qdisplay,
5019 Qnil, make_number (limit)),
5020 NILP (pos))
5021 && next_overlay_change (start) == ZV))
5023 IT_CHARPOS (*it) = limit;
5024 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5025 *skipped_p = newline_found_p = 1;
5027 else
5029 while (get_next_display_element (it)
5030 && !newline_found_p)
5032 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5033 set_iterator_to_next (it, 0);
5038 it->selective = old_selective;
5039 return newline_found_p;
5043 /* Set IT's current position to the previous visible line start. Skip
5044 invisible text that is so either due to text properties or due to
5045 selective display. Caution: this does not change IT->current_x and
5046 IT->hpos. */
5048 static void
5049 back_to_previous_visible_line_start (it)
5050 struct it *it;
5052 while (IT_CHARPOS (*it) > BEGV)
5054 back_to_previous_line_start (it);
5055 if (IT_CHARPOS (*it) <= BEGV)
5056 break;
5058 /* If selective > 0, then lines indented more than that values
5059 are invisible. */
5060 if (it->selective > 0
5061 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5062 (double) it->selective)) /* iftc */
5063 continue;
5065 /* Check the newline before point for invisibility. */
5067 Lisp_Object prop;
5068 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5069 Qinvisible, it->window);
5070 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5071 continue;
5074 /* If newline has a display property that replaces the newline with something
5075 else (image or text), find start of overlay or interval and continue search
5076 from that point. */
5077 if (IT_CHARPOS (*it) > BEGV)
5079 struct it it2 = *it;
5080 int pos;
5081 int beg, end;
5082 Lisp_Object val, overlay;
5084 pos = --IT_CHARPOS (it2);
5085 --IT_BYTEPOS (it2);
5086 it2.sp = 0;
5087 if (handle_display_prop (&it2) == HANDLED_RETURN
5088 && !NILP (val = get_char_property_and_overlay
5089 (make_number (pos), Qdisplay, Qnil, &overlay))
5090 && (OVERLAYP (overlay)
5091 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5092 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5094 if (beg < BEGV)
5095 beg = BEGV;
5096 IT_CHARPOS (*it) = beg;
5097 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5098 continue;
5102 break;
5105 xassert (IT_CHARPOS (*it) >= BEGV);
5106 xassert (IT_CHARPOS (*it) == BEGV
5107 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5108 CHECK_IT (it);
5112 /* Reseat iterator IT at the previous visible line start. Skip
5113 invisible text that is so either due to text properties or due to
5114 selective display. At the end, update IT's overlay information,
5115 face information etc. */
5117 void
5118 reseat_at_previous_visible_line_start (it)
5119 struct it *it;
5121 back_to_previous_visible_line_start (it);
5122 reseat (it, it->current.pos, 1);
5123 CHECK_IT (it);
5127 /* Reseat iterator IT on the next visible line start in the current
5128 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5129 preceding the line start. Skip over invisible text that is so
5130 because of selective display. Compute faces, overlays etc at the
5131 new position. Note that this function does not skip over text that
5132 is invisible because of text properties. */
5134 static void
5135 reseat_at_next_visible_line_start (it, on_newline_p)
5136 struct it *it;
5137 int on_newline_p;
5139 int newline_found_p, skipped_p = 0;
5141 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5143 /* Skip over lines that are invisible because they are indented
5144 more than the value of IT->selective. */
5145 if (it->selective > 0)
5146 while (IT_CHARPOS (*it) < ZV
5147 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5148 (double) it->selective)) /* iftc */
5150 xassert (IT_BYTEPOS (*it) == BEGV
5151 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5152 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5155 /* Position on the newline if that's what's requested. */
5156 if (on_newline_p && newline_found_p)
5158 if (STRINGP (it->string))
5160 if (IT_STRING_CHARPOS (*it) > 0)
5162 --IT_STRING_CHARPOS (*it);
5163 --IT_STRING_BYTEPOS (*it);
5166 else if (IT_CHARPOS (*it) > BEGV)
5168 --IT_CHARPOS (*it);
5169 --IT_BYTEPOS (*it);
5170 reseat (it, it->current.pos, 0);
5173 else if (skipped_p)
5174 reseat (it, it->current.pos, 0);
5176 CHECK_IT (it);
5181 /***********************************************************************
5182 Changing an iterator's position
5183 ***********************************************************************/
5185 /* Change IT's current position to POS in current_buffer. If FORCE_P
5186 is non-zero, always check for text properties at the new position.
5187 Otherwise, text properties are only looked up if POS >=
5188 IT->check_charpos of a property. */
5190 static void
5191 reseat (it, pos, force_p)
5192 struct it *it;
5193 struct text_pos pos;
5194 int force_p;
5196 int original_pos = IT_CHARPOS (*it);
5198 reseat_1 (it, pos, 0);
5200 /* Determine where to check text properties. Avoid doing it
5201 where possible because text property lookup is very expensive. */
5202 if (force_p
5203 || CHARPOS (pos) > it->stop_charpos
5204 || CHARPOS (pos) < original_pos)
5205 handle_stop (it);
5207 CHECK_IT (it);
5211 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5212 IT->stop_pos to POS, also. */
5214 static void
5215 reseat_1 (it, pos, set_stop_p)
5216 struct it *it;
5217 struct text_pos pos;
5218 int set_stop_p;
5220 /* Don't call this function when scanning a C string. */
5221 xassert (it->s == NULL);
5223 /* POS must be a reasonable value. */
5224 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5226 it->current.pos = it->position = pos;
5227 XSETBUFFER (it->object, current_buffer);
5228 it->end_charpos = ZV;
5229 it->dpvec = NULL;
5230 it->current.dpvec_index = -1;
5231 it->current.overlay_string_index = -1;
5232 IT_STRING_CHARPOS (*it) = -1;
5233 IT_STRING_BYTEPOS (*it) = -1;
5234 it->string = Qnil;
5235 it->method = GET_FROM_BUFFER;
5236 /* RMS: I added this to fix a bug in move_it_vertically_backward
5237 where it->area continued to relate to the starting point
5238 for the backward motion. Bug report from
5239 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5240 However, I am not sure whether reseat still does the right thing
5241 in general after this change. */
5242 it->area = TEXT_AREA;
5243 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5244 it->sp = 0;
5245 it->face_before_selective_p = 0;
5247 if (set_stop_p)
5248 it->stop_charpos = CHARPOS (pos);
5252 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5253 If S is non-null, it is a C string to iterate over. Otherwise,
5254 STRING gives a Lisp string to iterate over.
5256 If PRECISION > 0, don't return more then PRECISION number of
5257 characters from the string.
5259 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5260 characters have been returned. FIELD_WIDTH < 0 means an infinite
5261 field width.
5263 MULTIBYTE = 0 means disable processing of multibyte characters,
5264 MULTIBYTE > 0 means enable it,
5265 MULTIBYTE < 0 means use IT->multibyte_p.
5267 IT must be initialized via a prior call to init_iterator before
5268 calling this function. */
5270 static void
5271 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5272 struct it *it;
5273 unsigned char *s;
5274 Lisp_Object string;
5275 int charpos;
5276 int precision, field_width, multibyte;
5278 /* No region in strings. */
5279 it->region_beg_charpos = it->region_end_charpos = -1;
5281 /* No text property checks performed by default, but see below. */
5282 it->stop_charpos = -1;
5284 /* Set iterator position and end position. */
5285 bzero (&it->current, sizeof it->current);
5286 it->current.overlay_string_index = -1;
5287 it->current.dpvec_index = -1;
5288 xassert (charpos >= 0);
5290 /* If STRING is specified, use its multibyteness, otherwise use the
5291 setting of MULTIBYTE, if specified. */
5292 if (multibyte >= 0)
5293 it->multibyte_p = multibyte > 0;
5295 if (s == NULL)
5297 xassert (STRINGP (string));
5298 it->string = string;
5299 it->s = NULL;
5300 it->end_charpos = it->string_nchars = SCHARS (string);
5301 it->method = GET_FROM_STRING;
5302 it->current.string_pos = string_pos (charpos, string);
5304 else
5306 it->s = s;
5307 it->string = Qnil;
5309 /* Note that we use IT->current.pos, not it->current.string_pos,
5310 for displaying C strings. */
5311 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5312 if (it->multibyte_p)
5314 it->current.pos = c_string_pos (charpos, s, 1);
5315 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5317 else
5319 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5320 it->end_charpos = it->string_nchars = strlen (s);
5323 it->method = GET_FROM_C_STRING;
5326 /* PRECISION > 0 means don't return more than PRECISION characters
5327 from the string. */
5328 if (precision > 0 && it->end_charpos - charpos > precision)
5329 it->end_charpos = it->string_nchars = charpos + precision;
5331 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5332 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5333 FIELD_WIDTH < 0 means infinite field width. This is useful for
5334 padding with `-' at the end of a mode line. */
5335 if (field_width < 0)
5336 field_width = INFINITY;
5337 if (field_width > it->end_charpos - charpos)
5338 it->end_charpos = charpos + field_width;
5340 /* Use the standard display table for displaying strings. */
5341 if (DISP_TABLE_P (Vstandard_display_table))
5342 it->dp = XCHAR_TABLE (Vstandard_display_table);
5344 it->stop_charpos = charpos;
5345 CHECK_IT (it);
5350 /***********************************************************************
5351 Iteration
5352 ***********************************************************************/
5354 /* Map enum it_method value to corresponding next_element_from_* function. */
5356 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5358 next_element_from_buffer,
5359 next_element_from_display_vector,
5360 next_element_from_composition,
5361 next_element_from_string,
5362 next_element_from_c_string,
5363 next_element_from_image,
5364 next_element_from_stretch
5368 /* Load IT's display element fields with information about the next
5369 display element from the current position of IT. Value is zero if
5370 end of buffer (or C string) is reached. */
5372 static struct frame *last_escape_glyph_frame = NULL;
5373 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5374 static int last_escape_glyph_merged_face_id = 0;
5377 get_next_display_element (it)
5378 struct it *it;
5380 /* Non-zero means that we found a display element. Zero means that
5381 we hit the end of what we iterate over. Performance note: the
5382 function pointer `method' used here turns out to be faster than
5383 using a sequence of if-statements. */
5384 int success_p;
5386 get_next:
5387 success_p = (*get_next_element[it->method]) (it);
5389 if (it->what == IT_CHARACTER)
5391 /* Map via display table or translate control characters.
5392 IT->c, IT->len etc. have been set to the next character by
5393 the function call above. If we have a display table, and it
5394 contains an entry for IT->c, translate it. Don't do this if
5395 IT->c itself comes from a display table, otherwise we could
5396 end up in an infinite recursion. (An alternative could be to
5397 count the recursion depth of this function and signal an
5398 error when a certain maximum depth is reached.) Is it worth
5399 it? */
5400 if (success_p && it->dpvec == NULL)
5402 Lisp_Object dv;
5404 if (it->dp
5405 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5406 VECTORP (dv)))
5408 struct Lisp_Vector *v = XVECTOR (dv);
5410 /* Return the first character from the display table
5411 entry, if not empty. If empty, don't display the
5412 current character. */
5413 if (v->size)
5415 it->dpvec_char_len = it->len;
5416 it->dpvec = v->contents;
5417 it->dpend = v->contents + v->size;
5418 it->current.dpvec_index = 0;
5419 it->dpvec_face_id = -1;
5420 it->saved_face_id = it->face_id;
5421 it->method = GET_FROM_DISPLAY_VECTOR;
5422 it->ellipsis_p = 0;
5424 else
5426 set_iterator_to_next (it, 0);
5428 goto get_next;
5431 /* Translate control characters into `\003' or `^C' form.
5432 Control characters coming from a display table entry are
5433 currently not translated because we use IT->dpvec to hold
5434 the translation. This could easily be changed but I
5435 don't believe that it is worth doing.
5437 If it->multibyte_p is nonzero, eight-bit characters and
5438 non-printable multibyte characters are also translated to
5439 octal form.
5441 If it->multibyte_p is zero, eight-bit characters that
5442 don't have corresponding multibyte char code are also
5443 translated to octal form. */
5444 else if ((it->c < ' '
5445 && (it->area != TEXT_AREA
5446 /* In mode line, treat \n like other crl chars. */
5447 || (it->c != '\t'
5448 && it->glyph_row && it->glyph_row->mode_line_p)
5449 || (it->c != '\n' && it->c != '\t')))
5450 || (it->multibyte_p
5451 ? ((it->c >= 127
5452 && it->len == 1)
5453 || !CHAR_PRINTABLE_P (it->c)
5454 || (!NILP (Vnobreak_char_display)
5455 && (it->c == 0x8a0 || it->c == 0x8ad
5456 || it->c == 0x920 || it->c == 0x92d
5457 || it->c == 0xe20 || it->c == 0xe2d
5458 || it->c == 0xf20 || it->c == 0xf2d)))
5459 : (it->c >= 127
5460 && (!unibyte_display_via_language_environment
5461 || it->c == unibyte_char_to_multibyte (it->c)))))
5463 /* IT->c is a control character which must be displayed
5464 either as '\003' or as `^C' where the '\\' and '^'
5465 can be defined in the display table. Fill
5466 IT->ctl_chars with glyphs for what we have to
5467 display. Then, set IT->dpvec to these glyphs. */
5468 GLYPH g;
5469 int ctl_len;
5470 int face_id, lface_id = 0 ;
5471 GLYPH escape_glyph;
5473 /* Handle control characters with ^. */
5475 if (it->c < 128 && it->ctl_arrow_p)
5477 g = '^'; /* default glyph for Control */
5478 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5479 if (it->dp
5480 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5481 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5483 g = XINT (DISP_CTRL_GLYPH (it->dp));
5484 lface_id = FAST_GLYPH_FACE (g);
5486 if (lface_id)
5488 g = FAST_GLYPH_CHAR (g);
5489 face_id = merge_faces (it->f, Qt, lface_id,
5490 it->face_id);
5492 else if (it->f == last_escape_glyph_frame
5493 && it->face_id == last_escape_glyph_face_id)
5495 face_id = last_escape_glyph_merged_face_id;
5497 else
5499 /* Merge the escape-glyph face into the current face. */
5500 face_id = merge_faces (it->f, Qescape_glyph, 0,
5501 it->face_id);
5502 last_escape_glyph_frame = it->f;
5503 last_escape_glyph_face_id = it->face_id;
5504 last_escape_glyph_merged_face_id = face_id;
5507 XSETINT (it->ctl_chars[0], g);
5508 g = it->c ^ 0100;
5509 XSETINT (it->ctl_chars[1], g);
5510 ctl_len = 2;
5511 goto display_control;
5514 /* Handle non-break space in the mode where it only gets
5515 highlighting. */
5517 if (EQ (Vnobreak_char_display, Qt)
5518 && (it->c == 0x8a0 || it->c == 0x920
5519 || it->c == 0xe20 || it->c == 0xf20))
5521 /* Merge the no-break-space face into the current face. */
5522 face_id = merge_faces (it->f, Qnobreak_space, 0,
5523 it->face_id);
5525 g = it->c = ' ';
5526 XSETINT (it->ctl_chars[0], g);
5527 ctl_len = 1;
5528 goto display_control;
5531 /* Handle sequences that start with the "escape glyph". */
5533 /* the default escape glyph is \. */
5534 escape_glyph = '\\';
5536 if (it->dp
5537 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5538 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5540 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5541 lface_id = FAST_GLYPH_FACE (escape_glyph);
5543 if (lface_id)
5545 /* The display table specified a face.
5546 Merge it into face_id and also into escape_glyph. */
5547 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5548 face_id = merge_faces (it->f, Qt, lface_id,
5549 it->face_id);
5551 else if (it->f == last_escape_glyph_frame
5552 && it->face_id == last_escape_glyph_face_id)
5554 face_id = last_escape_glyph_merged_face_id;
5556 else
5558 /* Merge the escape-glyph face into the current face. */
5559 face_id = merge_faces (it->f, Qescape_glyph, 0,
5560 it->face_id);
5561 last_escape_glyph_frame = it->f;
5562 last_escape_glyph_face_id = it->face_id;
5563 last_escape_glyph_merged_face_id = face_id;
5566 /* Handle soft hyphens in the mode where they only get
5567 highlighting. */
5569 if (EQ (Vnobreak_char_display, Qt)
5570 && (it->c == 0x8ad || it->c == 0x92d
5571 || it->c == 0xe2d || it->c == 0xf2d))
5573 g = it->c = '-';
5574 XSETINT (it->ctl_chars[0], g);
5575 ctl_len = 1;
5576 goto display_control;
5579 /* Handle non-break space and soft hyphen
5580 with the escape glyph. */
5582 if (it->c == 0x8a0 || it->c == 0x8ad
5583 || it->c == 0x920 || it->c == 0x92d
5584 || it->c == 0xe20 || it->c == 0xe2d
5585 || it->c == 0xf20 || it->c == 0xf2d)
5587 XSETINT (it->ctl_chars[0], escape_glyph);
5588 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5589 XSETINT (it->ctl_chars[1], g);
5590 ctl_len = 2;
5591 goto display_control;
5595 unsigned char str[MAX_MULTIBYTE_LENGTH];
5596 int len;
5597 int i;
5599 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5600 if (SINGLE_BYTE_CHAR_P (it->c))
5601 str[0] = it->c, len = 1;
5602 else
5604 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5605 if (len < 0)
5607 /* It's an invalid character, which shouldn't
5608 happen actually, but due to bugs it may
5609 happen. Let's print the char as is, there's
5610 not much meaningful we can do with it. */
5611 str[0] = it->c;
5612 str[1] = it->c >> 8;
5613 str[2] = it->c >> 16;
5614 str[3] = it->c >> 24;
5615 len = 4;
5619 for (i = 0; i < len; i++)
5621 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5622 /* Insert three more glyphs into IT->ctl_chars for
5623 the octal display of the character. */
5624 g = ((str[i] >> 6) & 7) + '0';
5625 XSETINT (it->ctl_chars[i * 4 + 1], g);
5626 g = ((str[i] >> 3) & 7) + '0';
5627 XSETINT (it->ctl_chars[i * 4 + 2], g);
5628 g = (str[i] & 7) + '0';
5629 XSETINT (it->ctl_chars[i * 4 + 3], g);
5631 ctl_len = len * 4;
5634 display_control:
5635 /* Set up IT->dpvec and return first character from it. */
5636 it->dpvec_char_len = it->len;
5637 it->dpvec = it->ctl_chars;
5638 it->dpend = it->dpvec + ctl_len;
5639 it->current.dpvec_index = 0;
5640 it->dpvec_face_id = face_id;
5641 it->saved_face_id = it->face_id;
5642 it->method = GET_FROM_DISPLAY_VECTOR;
5643 it->ellipsis_p = 0;
5644 goto get_next;
5648 /* Adjust face id for a multibyte character. There are no
5649 multibyte character in unibyte text. */
5650 if (it->multibyte_p
5651 && success_p
5652 && FRAME_WINDOW_P (it->f))
5654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5655 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5659 /* Is this character the last one of a run of characters with
5660 box? If yes, set IT->end_of_box_run_p to 1. */
5661 if (it->face_box_p
5662 && it->s == NULL)
5664 int face_id;
5665 struct face *face;
5667 it->end_of_box_run_p
5668 = ((face_id = face_after_it_pos (it),
5669 face_id != it->face_id)
5670 && (face = FACE_FROM_ID (it->f, face_id),
5671 face->box == FACE_NO_BOX));
5674 /* Value is 0 if end of buffer or string reached. */
5675 return success_p;
5679 /* Move IT to the next display element.
5681 RESEAT_P non-zero means if called on a newline in buffer text,
5682 skip to the next visible line start.
5684 Functions get_next_display_element and set_iterator_to_next are
5685 separate because I find this arrangement easier to handle than a
5686 get_next_display_element function that also increments IT's
5687 position. The way it is we can first look at an iterator's current
5688 display element, decide whether it fits on a line, and if it does,
5689 increment the iterator position. The other way around we probably
5690 would either need a flag indicating whether the iterator has to be
5691 incremented the next time, or we would have to implement a
5692 decrement position function which would not be easy to write. */
5694 void
5695 set_iterator_to_next (it, reseat_p)
5696 struct it *it;
5697 int reseat_p;
5699 /* Reset flags indicating start and end of a sequence of characters
5700 with box. Reset them at the start of this function because
5701 moving the iterator to a new position might set them. */
5702 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5704 switch (it->method)
5706 case GET_FROM_BUFFER:
5707 /* The current display element of IT is a character from
5708 current_buffer. Advance in the buffer, and maybe skip over
5709 invisible lines that are so because of selective display. */
5710 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5711 reseat_at_next_visible_line_start (it, 0);
5712 else
5714 xassert (it->len != 0);
5715 IT_BYTEPOS (*it) += it->len;
5716 IT_CHARPOS (*it) += 1;
5717 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5719 break;
5721 case GET_FROM_COMPOSITION:
5722 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5723 if (STRINGP (it->string))
5725 IT_STRING_BYTEPOS (*it) += it->len;
5726 IT_STRING_CHARPOS (*it) += it->cmp_len;
5727 it->method = GET_FROM_STRING;
5728 goto consider_string_end;
5730 else
5732 IT_BYTEPOS (*it) += it->len;
5733 IT_CHARPOS (*it) += it->cmp_len;
5734 it->method = GET_FROM_BUFFER;
5736 break;
5738 case GET_FROM_C_STRING:
5739 /* Current display element of IT is from a C string. */
5740 IT_BYTEPOS (*it) += it->len;
5741 IT_CHARPOS (*it) += 1;
5742 break;
5744 case GET_FROM_DISPLAY_VECTOR:
5745 /* Current display element of IT is from a display table entry.
5746 Advance in the display table definition. Reset it to null if
5747 end reached, and continue with characters from buffers/
5748 strings. */
5749 ++it->current.dpvec_index;
5751 /* Restore face of the iterator to what they were before the
5752 display vector entry (these entries may contain faces). */
5753 it->face_id = it->saved_face_id;
5755 if (it->dpvec + it->current.dpvec_index == it->dpend)
5757 int recheck_faces = it->ellipsis_p;
5759 if (it->s)
5760 it->method = GET_FROM_C_STRING;
5761 else if (STRINGP (it->string))
5762 it->method = GET_FROM_STRING;
5763 else
5764 it->method = GET_FROM_BUFFER;
5766 it->dpvec = NULL;
5767 it->current.dpvec_index = -1;
5769 /* Skip over characters which were displayed via IT->dpvec. */
5770 if (it->dpvec_char_len < 0)
5771 reseat_at_next_visible_line_start (it, 1);
5772 else if (it->dpvec_char_len > 0)
5774 if (it->method == GET_FROM_STRING
5775 && it->n_overlay_strings > 0)
5776 it->ignore_overlay_strings_at_pos_p = 1;
5777 it->len = it->dpvec_char_len;
5778 set_iterator_to_next (it, reseat_p);
5781 /* Maybe recheck faces after display vector */
5782 if (recheck_faces)
5783 it->stop_charpos = IT_CHARPOS (*it);
5785 break;
5787 case GET_FROM_STRING:
5788 /* Current display element is a character from a Lisp string. */
5789 xassert (it->s == NULL && STRINGP (it->string));
5790 IT_STRING_BYTEPOS (*it) += it->len;
5791 IT_STRING_CHARPOS (*it) += 1;
5793 consider_string_end:
5795 if (it->current.overlay_string_index >= 0)
5797 /* IT->string is an overlay string. Advance to the
5798 next, if there is one. */
5799 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5800 next_overlay_string (it);
5802 else
5804 /* IT->string is not an overlay string. If we reached
5805 its end, and there is something on IT->stack, proceed
5806 with what is on the stack. This can be either another
5807 string, this time an overlay string, or a buffer. */
5808 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5809 && it->sp > 0)
5811 pop_it (it);
5812 if (STRINGP (it->string))
5813 goto consider_string_end;
5814 it->method = GET_FROM_BUFFER;
5817 break;
5819 case GET_FROM_IMAGE:
5820 case GET_FROM_STRETCH:
5821 /* The position etc with which we have to proceed are on
5822 the stack. The position may be at the end of a string,
5823 if the `display' property takes up the whole string. */
5824 xassert (it->sp > 0);
5825 pop_it (it);
5826 it->image_id = 0;
5827 if (STRINGP (it->string))
5829 it->method = GET_FROM_STRING;
5830 goto consider_string_end;
5832 it->method = GET_FROM_BUFFER;
5833 break;
5835 default:
5836 /* There are no other methods defined, so this should be a bug. */
5837 abort ();
5840 xassert (it->method != GET_FROM_STRING
5841 || (STRINGP (it->string)
5842 && IT_STRING_CHARPOS (*it) >= 0));
5845 /* Load IT's display element fields with information about the next
5846 display element which comes from a display table entry or from the
5847 result of translating a control character to one of the forms `^C'
5848 or `\003'.
5850 IT->dpvec holds the glyphs to return as characters.
5851 IT->saved_face_id holds the face id before the display vector--
5852 it is restored into IT->face_idin set_iterator_to_next. */
5854 static int
5855 next_element_from_display_vector (it)
5856 struct it *it;
5858 /* Precondition. */
5859 xassert (it->dpvec && it->current.dpvec_index >= 0);
5861 it->face_id = it->saved_face_id;
5863 if (INTEGERP (*it->dpvec)
5864 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5866 GLYPH g;
5868 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5869 it->c = FAST_GLYPH_CHAR (g);
5870 it->len = CHAR_BYTES (it->c);
5872 /* The entry may contain a face id to use. Such a face id is
5873 the id of a Lisp face, not a realized face. A face id of
5874 zero means no face is specified. */
5875 if (it->dpvec_face_id >= 0)
5876 it->face_id = it->dpvec_face_id;
5877 else
5879 int lface_id = FAST_GLYPH_FACE (g);
5880 if (lface_id > 0)
5881 it->face_id = merge_faces (it->f, Qt, lface_id,
5882 it->saved_face_id);
5885 else
5886 /* Display table entry is invalid. Return a space. */
5887 it->c = ' ', it->len = 1;
5889 /* Don't change position and object of the iterator here. They are
5890 still the values of the character that had this display table
5891 entry or was translated, and that's what we want. */
5892 it->what = IT_CHARACTER;
5893 return 1;
5897 /* Load IT with the next display element from Lisp string IT->string.
5898 IT->current.string_pos is the current position within the string.
5899 If IT->current.overlay_string_index >= 0, the Lisp string is an
5900 overlay string. */
5902 static int
5903 next_element_from_string (it)
5904 struct it *it;
5906 struct text_pos position;
5908 xassert (STRINGP (it->string));
5909 xassert (IT_STRING_CHARPOS (*it) >= 0);
5910 position = it->current.string_pos;
5912 /* Time to check for invisible text? */
5913 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5914 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5916 handle_stop (it);
5918 /* Since a handler may have changed IT->method, we must
5919 recurse here. */
5920 return get_next_display_element (it);
5923 if (it->current.overlay_string_index >= 0)
5925 /* Get the next character from an overlay string. In overlay
5926 strings, There is no field width or padding with spaces to
5927 do. */
5928 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5930 it->what = IT_EOB;
5931 return 0;
5933 else if (STRING_MULTIBYTE (it->string))
5935 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5936 const unsigned char *s = (SDATA (it->string)
5937 + IT_STRING_BYTEPOS (*it));
5938 it->c = string_char_and_length (s, remaining, &it->len);
5940 else
5942 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5943 it->len = 1;
5946 else
5948 /* Get the next character from a Lisp string that is not an
5949 overlay string. Such strings come from the mode line, for
5950 example. We may have to pad with spaces, or truncate the
5951 string. See also next_element_from_c_string. */
5952 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5954 it->what = IT_EOB;
5955 return 0;
5957 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5959 /* Pad with spaces. */
5960 it->c = ' ', it->len = 1;
5961 CHARPOS (position) = BYTEPOS (position) = -1;
5963 else if (STRING_MULTIBYTE (it->string))
5965 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5966 const unsigned char *s = (SDATA (it->string)
5967 + IT_STRING_BYTEPOS (*it));
5968 it->c = string_char_and_length (s, maxlen, &it->len);
5970 else
5972 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5973 it->len = 1;
5977 /* Record what we have and where it came from. Note that we store a
5978 buffer position in IT->position although it could arguably be a
5979 string position. */
5980 it->what = IT_CHARACTER;
5981 it->object = it->string;
5982 it->position = position;
5983 return 1;
5987 /* Load IT with next display element from C string IT->s.
5988 IT->string_nchars is the maximum number of characters to return
5989 from the string. IT->end_charpos may be greater than
5990 IT->string_nchars when this function is called, in which case we
5991 may have to return padding spaces. Value is zero if end of string
5992 reached, including padding spaces. */
5994 static int
5995 next_element_from_c_string (it)
5996 struct it *it;
5998 int success_p = 1;
6000 xassert (it->s);
6001 it->what = IT_CHARACTER;
6002 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6003 it->object = Qnil;
6005 /* IT's position can be greater IT->string_nchars in case a field
6006 width or precision has been specified when the iterator was
6007 initialized. */
6008 if (IT_CHARPOS (*it) >= it->end_charpos)
6010 /* End of the game. */
6011 it->what = IT_EOB;
6012 success_p = 0;
6014 else if (IT_CHARPOS (*it) >= it->string_nchars)
6016 /* Pad with spaces. */
6017 it->c = ' ', it->len = 1;
6018 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6020 else if (it->multibyte_p)
6022 /* Implementation note: The calls to strlen apparently aren't a
6023 performance problem because there is no noticeable performance
6024 difference between Emacs running in unibyte or multibyte mode. */
6025 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6026 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6027 maxlen, &it->len);
6029 else
6030 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6032 return success_p;
6036 /* Set up IT to return characters from an ellipsis, if appropriate.
6037 The definition of the ellipsis glyphs may come from a display table
6038 entry. This function Fills IT with the first glyph from the
6039 ellipsis if an ellipsis is to be displayed. */
6041 static int
6042 next_element_from_ellipsis (it)
6043 struct it *it;
6045 if (it->selective_display_ellipsis_p)
6046 setup_for_ellipsis (it, it->len);
6047 else
6049 /* The face at the current position may be different from the
6050 face we find after the invisible text. Remember what it
6051 was in IT->saved_face_id, and signal that it's there by
6052 setting face_before_selective_p. */
6053 it->saved_face_id = it->face_id;
6054 it->method = GET_FROM_BUFFER;
6055 reseat_at_next_visible_line_start (it, 1);
6056 it->face_before_selective_p = 1;
6059 return get_next_display_element (it);
6063 /* Deliver an image display element. The iterator IT is already
6064 filled with image information (done in handle_display_prop). Value
6065 is always 1. */
6068 static int
6069 next_element_from_image (it)
6070 struct it *it;
6072 it->what = IT_IMAGE;
6073 return 1;
6077 /* Fill iterator IT with next display element from a stretch glyph
6078 property. IT->object is the value of the text property. Value is
6079 always 1. */
6081 static int
6082 next_element_from_stretch (it)
6083 struct it *it;
6085 it->what = IT_STRETCH;
6086 return 1;
6090 /* Load IT with the next display element from current_buffer. Value
6091 is zero if end of buffer reached. IT->stop_charpos is the next
6092 position at which to stop and check for text properties or buffer
6093 end. */
6095 static int
6096 next_element_from_buffer (it)
6097 struct it *it;
6099 int success_p = 1;
6101 /* Check this assumption, otherwise, we would never enter the
6102 if-statement, below. */
6103 xassert (IT_CHARPOS (*it) >= BEGV
6104 && IT_CHARPOS (*it) <= it->stop_charpos);
6106 if (IT_CHARPOS (*it) >= it->stop_charpos)
6108 if (IT_CHARPOS (*it) >= it->end_charpos)
6110 int overlay_strings_follow_p;
6112 /* End of the game, except when overlay strings follow that
6113 haven't been returned yet. */
6114 if (it->overlay_strings_at_end_processed_p)
6115 overlay_strings_follow_p = 0;
6116 else
6118 it->overlay_strings_at_end_processed_p = 1;
6119 overlay_strings_follow_p = get_overlay_strings (it, 0);
6122 if (overlay_strings_follow_p)
6123 success_p = get_next_display_element (it);
6124 else
6126 it->what = IT_EOB;
6127 it->position = it->current.pos;
6128 success_p = 0;
6131 else
6133 handle_stop (it);
6134 return get_next_display_element (it);
6137 else
6139 /* No face changes, overlays etc. in sight, so just return a
6140 character from current_buffer. */
6141 unsigned char *p;
6143 /* Maybe run the redisplay end trigger hook. Performance note:
6144 This doesn't seem to cost measurable time. */
6145 if (it->redisplay_end_trigger_charpos
6146 && it->glyph_row
6147 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6148 run_redisplay_end_trigger_hook (it);
6150 /* Get the next character, maybe multibyte. */
6151 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6152 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6154 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6155 - IT_BYTEPOS (*it));
6156 it->c = string_char_and_length (p, maxlen, &it->len);
6158 else
6159 it->c = *p, it->len = 1;
6161 /* Record what we have and where it came from. */
6162 it->what = IT_CHARACTER;;
6163 it->object = it->w->buffer;
6164 it->position = it->current.pos;
6166 /* Normally we return the character found above, except when we
6167 really want to return an ellipsis for selective display. */
6168 if (it->selective)
6170 if (it->c == '\n')
6172 /* A value of selective > 0 means hide lines indented more
6173 than that number of columns. */
6174 if (it->selective > 0
6175 && IT_CHARPOS (*it) + 1 < ZV
6176 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6177 IT_BYTEPOS (*it) + 1,
6178 (double) it->selective)) /* iftc */
6180 success_p = next_element_from_ellipsis (it);
6181 it->dpvec_char_len = -1;
6184 else if (it->c == '\r' && it->selective == -1)
6186 /* A value of selective == -1 means that everything from the
6187 CR to the end of the line is invisible, with maybe an
6188 ellipsis displayed for it. */
6189 success_p = next_element_from_ellipsis (it);
6190 it->dpvec_char_len = -1;
6195 /* Value is zero if end of buffer reached. */
6196 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6197 return success_p;
6201 /* Run the redisplay end trigger hook for IT. */
6203 static void
6204 run_redisplay_end_trigger_hook (it)
6205 struct it *it;
6207 Lisp_Object args[3];
6209 /* IT->glyph_row should be non-null, i.e. we should be actually
6210 displaying something, or otherwise we should not run the hook. */
6211 xassert (it->glyph_row);
6213 /* Set up hook arguments. */
6214 args[0] = Qredisplay_end_trigger_functions;
6215 args[1] = it->window;
6216 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6217 it->redisplay_end_trigger_charpos = 0;
6219 /* Since we are *trying* to run these functions, don't try to run
6220 them again, even if they get an error. */
6221 it->w->redisplay_end_trigger = Qnil;
6222 Frun_hook_with_args (3, args);
6224 /* Notice if it changed the face of the character we are on. */
6225 handle_face_prop (it);
6229 /* Deliver a composition display element. The iterator IT is already
6230 filled with composition information (done in
6231 handle_composition_prop). Value is always 1. */
6233 static int
6234 next_element_from_composition (it)
6235 struct it *it;
6237 it->what = IT_COMPOSITION;
6238 it->position = (STRINGP (it->string)
6239 ? it->current.string_pos
6240 : it->current.pos);
6241 if (STRINGP (it->string))
6242 it->object = it->string;
6243 return 1;
6248 /***********************************************************************
6249 Moving an iterator without producing glyphs
6250 ***********************************************************************/
6252 /* Check if iterator is at a position corresponding to a valid buffer
6253 position after some move_it_ call. */
6255 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6256 ((it)->method == GET_FROM_STRING \
6257 ? IT_STRING_CHARPOS (*it) == 0 \
6258 : 1)
6261 /* Move iterator IT to a specified buffer or X position within one
6262 line on the display without producing glyphs.
6264 OP should be a bit mask including some or all of these bits:
6265 MOVE_TO_X: Stop on reaching x-position TO_X.
6266 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6267 Regardless of OP's value, stop in reaching the end of the display line.
6269 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6270 This means, in particular, that TO_X includes window's horizontal
6271 scroll amount.
6273 The return value has several possible values that
6274 say what condition caused the scan to stop:
6276 MOVE_POS_MATCH_OR_ZV
6277 - when TO_POS or ZV was reached.
6279 MOVE_X_REACHED
6280 -when TO_X was reached before TO_POS or ZV were reached.
6282 MOVE_LINE_CONTINUED
6283 - when we reached the end of the display area and the line must
6284 be continued.
6286 MOVE_LINE_TRUNCATED
6287 - when we reached the end of the display area and the line is
6288 truncated.
6290 MOVE_NEWLINE_OR_CR
6291 - when we stopped at a line end, i.e. a newline or a CR and selective
6292 display is on. */
6294 static enum move_it_result
6295 move_it_in_display_line_to (it, to_charpos, to_x, op)
6296 struct it *it;
6297 int to_charpos, to_x, op;
6299 enum move_it_result result = MOVE_UNDEFINED;
6300 struct glyph_row *saved_glyph_row;
6302 /* Don't produce glyphs in produce_glyphs. */
6303 saved_glyph_row = it->glyph_row;
6304 it->glyph_row = NULL;
6306 #define BUFFER_POS_REACHED_P() \
6307 ((op & MOVE_TO_POS) != 0 \
6308 && BUFFERP (it->object) \
6309 && IT_CHARPOS (*it) >= to_charpos \
6310 && (it->method == GET_FROM_BUFFER \
6311 || (it->method == GET_FROM_DISPLAY_VECTOR \
6312 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6315 while (1)
6317 int x, i, ascent = 0, descent = 0;
6319 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6320 if ((op & MOVE_TO_POS) != 0
6321 && BUFFERP (it->object)
6322 && it->method == GET_FROM_BUFFER
6323 && IT_CHARPOS (*it) > to_charpos)
6325 result = MOVE_POS_MATCH_OR_ZV;
6326 break;
6329 /* Stop when ZV reached.
6330 We used to stop here when TO_CHARPOS reached as well, but that is
6331 too soon if this glyph does not fit on this line. So we handle it
6332 explicitly below. */
6333 if (!get_next_display_element (it)
6334 || (it->truncate_lines_p
6335 && BUFFER_POS_REACHED_P ()))
6337 result = MOVE_POS_MATCH_OR_ZV;
6338 break;
6341 /* The call to produce_glyphs will get the metrics of the
6342 display element IT is loaded with. We record in x the
6343 x-position before this display element in case it does not
6344 fit on the line. */
6345 x = it->current_x;
6347 /* Remember the line height so far in case the next element doesn't
6348 fit on the line. */
6349 if (!it->truncate_lines_p)
6351 ascent = it->max_ascent;
6352 descent = it->max_descent;
6355 PRODUCE_GLYPHS (it);
6357 if (it->area != TEXT_AREA)
6359 set_iterator_to_next (it, 1);
6360 continue;
6363 /* The number of glyphs we get back in IT->nglyphs will normally
6364 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6365 character on a terminal frame, or (iii) a line end. For the
6366 second case, IT->nglyphs - 1 padding glyphs will be present
6367 (on X frames, there is only one glyph produced for a
6368 composite character.
6370 The behavior implemented below means, for continuation lines,
6371 that as many spaces of a TAB as fit on the current line are
6372 displayed there. For terminal frames, as many glyphs of a
6373 multi-glyph character are displayed in the current line, too.
6374 This is what the old redisplay code did, and we keep it that
6375 way. Under X, the whole shape of a complex character must
6376 fit on the line or it will be completely displayed in the
6377 next line.
6379 Note that both for tabs and padding glyphs, all glyphs have
6380 the same width. */
6381 if (it->nglyphs)
6383 /* More than one glyph or glyph doesn't fit on line. All
6384 glyphs have the same width. */
6385 int single_glyph_width = it->pixel_width / it->nglyphs;
6386 int new_x;
6387 int x_before_this_char = x;
6388 int hpos_before_this_char = it->hpos;
6390 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6392 new_x = x + single_glyph_width;
6394 /* We want to leave anything reaching TO_X to the caller. */
6395 if ((op & MOVE_TO_X) && new_x > to_x)
6397 if (BUFFER_POS_REACHED_P ())
6398 goto buffer_pos_reached;
6399 it->current_x = x;
6400 result = MOVE_X_REACHED;
6401 break;
6403 else if (/* Lines are continued. */
6404 !it->truncate_lines_p
6405 && (/* And glyph doesn't fit on the line. */
6406 new_x > it->last_visible_x
6407 /* Or it fits exactly and we're on a window
6408 system frame. */
6409 || (new_x == it->last_visible_x
6410 && FRAME_WINDOW_P (it->f))))
6412 if (/* IT->hpos == 0 means the very first glyph
6413 doesn't fit on the line, e.g. a wide image. */
6414 it->hpos == 0
6415 || (new_x == it->last_visible_x
6416 && FRAME_WINDOW_P (it->f)))
6418 ++it->hpos;
6419 it->current_x = new_x;
6421 /* The character's last glyph just barely fits
6422 in this row. */
6423 if (i == it->nglyphs - 1)
6425 /* If this is the destination position,
6426 return a position *before* it in this row,
6427 now that we know it fits in this row. */
6428 if (BUFFER_POS_REACHED_P ())
6430 it->hpos = hpos_before_this_char;
6431 it->current_x = x_before_this_char;
6432 result = MOVE_POS_MATCH_OR_ZV;
6433 break;
6436 set_iterator_to_next (it, 1);
6437 #ifdef HAVE_WINDOW_SYSTEM
6438 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6440 if (!get_next_display_element (it))
6442 result = MOVE_POS_MATCH_OR_ZV;
6443 break;
6445 if (BUFFER_POS_REACHED_P ())
6447 if (ITERATOR_AT_END_OF_LINE_P (it))
6448 result = MOVE_POS_MATCH_OR_ZV;
6449 else
6450 result = MOVE_LINE_CONTINUED;
6451 break;
6453 if (ITERATOR_AT_END_OF_LINE_P (it))
6455 result = MOVE_NEWLINE_OR_CR;
6456 break;
6459 #endif /* HAVE_WINDOW_SYSTEM */
6462 else
6464 it->current_x = x;
6465 it->max_ascent = ascent;
6466 it->max_descent = descent;
6469 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6470 IT_CHARPOS (*it)));
6471 result = MOVE_LINE_CONTINUED;
6472 break;
6474 else if (BUFFER_POS_REACHED_P ())
6475 goto buffer_pos_reached;
6476 else if (new_x > it->first_visible_x)
6478 /* Glyph is visible. Increment number of glyphs that
6479 would be displayed. */
6480 ++it->hpos;
6482 else
6484 /* Glyph is completely off the left margin of the display
6485 area. Nothing to do. */
6489 if (result != MOVE_UNDEFINED)
6490 break;
6492 else if (BUFFER_POS_REACHED_P ())
6494 buffer_pos_reached:
6495 it->current_x = x;
6496 it->max_ascent = ascent;
6497 it->max_descent = descent;
6498 result = MOVE_POS_MATCH_OR_ZV;
6499 break;
6501 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6503 /* Stop when TO_X specified and reached. This check is
6504 necessary here because of lines consisting of a line end,
6505 only. The line end will not produce any glyphs and we
6506 would never get MOVE_X_REACHED. */
6507 xassert (it->nglyphs == 0);
6508 result = MOVE_X_REACHED;
6509 break;
6512 /* Is this a line end? If yes, we're done. */
6513 if (ITERATOR_AT_END_OF_LINE_P (it))
6515 result = MOVE_NEWLINE_OR_CR;
6516 break;
6519 /* The current display element has been consumed. Advance
6520 to the next. */
6521 set_iterator_to_next (it, 1);
6523 /* Stop if lines are truncated and IT's current x-position is
6524 past the right edge of the window now. */
6525 if (it->truncate_lines_p
6526 && it->current_x >= it->last_visible_x)
6528 #ifdef HAVE_WINDOW_SYSTEM
6529 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6531 if (!get_next_display_element (it)
6532 || BUFFER_POS_REACHED_P ())
6534 result = MOVE_POS_MATCH_OR_ZV;
6535 break;
6537 if (ITERATOR_AT_END_OF_LINE_P (it))
6539 result = MOVE_NEWLINE_OR_CR;
6540 break;
6543 #endif /* HAVE_WINDOW_SYSTEM */
6544 result = MOVE_LINE_TRUNCATED;
6545 break;
6549 #undef BUFFER_POS_REACHED_P
6551 /* Restore the iterator settings altered at the beginning of this
6552 function. */
6553 it->glyph_row = saved_glyph_row;
6554 return result;
6558 /* Move IT forward until it satisfies one or more of the criteria in
6559 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6561 OP is a bit-mask that specifies where to stop, and in particular,
6562 which of those four position arguments makes a difference. See the
6563 description of enum move_operation_enum.
6565 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6566 screen line, this function will set IT to the next position >
6567 TO_CHARPOS. */
6569 void
6570 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6571 struct it *it;
6572 int to_charpos, to_x, to_y, to_vpos;
6573 int op;
6575 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6576 int line_height;
6577 int reached = 0;
6579 for (;;)
6581 if (op & MOVE_TO_VPOS)
6583 /* If no TO_CHARPOS and no TO_X specified, stop at the
6584 start of the line TO_VPOS. */
6585 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6587 if (it->vpos == to_vpos)
6589 reached = 1;
6590 break;
6592 else
6593 skip = move_it_in_display_line_to (it, -1, -1, 0);
6595 else
6597 /* TO_VPOS >= 0 means stop at TO_X in the line at
6598 TO_VPOS, or at TO_POS, whichever comes first. */
6599 if (it->vpos == to_vpos)
6601 reached = 2;
6602 break;
6605 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6607 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6609 reached = 3;
6610 break;
6612 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6614 /* We have reached TO_X but not in the line we want. */
6615 skip = move_it_in_display_line_to (it, to_charpos,
6616 -1, MOVE_TO_POS);
6617 if (skip == MOVE_POS_MATCH_OR_ZV)
6619 reached = 4;
6620 break;
6625 else if (op & MOVE_TO_Y)
6627 struct it it_backup;
6629 /* TO_Y specified means stop at TO_X in the line containing
6630 TO_Y---or at TO_CHARPOS if this is reached first. The
6631 problem is that we can't really tell whether the line
6632 contains TO_Y before we have completely scanned it, and
6633 this may skip past TO_X. What we do is to first scan to
6634 TO_X.
6636 If TO_X is not specified, use a TO_X of zero. The reason
6637 is to make the outcome of this function more predictable.
6638 If we didn't use TO_X == 0, we would stop at the end of
6639 the line which is probably not what a caller would expect
6640 to happen. */
6641 skip = move_it_in_display_line_to (it, to_charpos,
6642 ((op & MOVE_TO_X)
6643 ? to_x : 0),
6644 (MOVE_TO_X
6645 | (op & MOVE_TO_POS)));
6647 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6648 if (skip == MOVE_POS_MATCH_OR_ZV)
6650 reached = 5;
6651 break;
6654 /* If TO_X was reached, we would like to know whether TO_Y
6655 is in the line. This can only be said if we know the
6656 total line height which requires us to scan the rest of
6657 the line. */
6658 if (skip == MOVE_X_REACHED)
6660 it_backup = *it;
6661 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6662 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6663 op & MOVE_TO_POS);
6664 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6667 /* Now, decide whether TO_Y is in this line. */
6668 line_height = it->max_ascent + it->max_descent;
6669 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6671 if (to_y >= it->current_y
6672 && to_y < it->current_y + line_height)
6674 if (skip == MOVE_X_REACHED)
6675 /* If TO_Y is in this line and TO_X was reached above,
6676 we scanned too far. We have to restore IT's settings
6677 to the ones before skipping. */
6678 *it = it_backup;
6679 reached = 6;
6681 else if (skip == MOVE_X_REACHED)
6683 skip = skip2;
6684 if (skip == MOVE_POS_MATCH_OR_ZV)
6685 reached = 7;
6688 if (reached)
6689 break;
6691 else
6692 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6694 switch (skip)
6696 case MOVE_POS_MATCH_OR_ZV:
6697 reached = 8;
6698 goto out;
6700 case MOVE_NEWLINE_OR_CR:
6701 set_iterator_to_next (it, 1);
6702 it->continuation_lines_width = 0;
6703 break;
6705 case MOVE_LINE_TRUNCATED:
6706 it->continuation_lines_width = 0;
6707 reseat_at_next_visible_line_start (it, 0);
6708 if ((op & MOVE_TO_POS) != 0
6709 && IT_CHARPOS (*it) > to_charpos)
6711 reached = 9;
6712 goto out;
6714 break;
6716 case MOVE_LINE_CONTINUED:
6717 it->continuation_lines_width += it->current_x;
6718 break;
6720 default:
6721 abort ();
6724 /* Reset/increment for the next run. */
6725 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6726 it->current_x = it->hpos = 0;
6727 it->current_y += it->max_ascent + it->max_descent;
6728 ++it->vpos;
6729 last_height = it->max_ascent + it->max_descent;
6730 last_max_ascent = it->max_ascent;
6731 it->max_ascent = it->max_descent = 0;
6734 out:
6736 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6740 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6742 If DY > 0, move IT backward at least that many pixels. DY = 0
6743 means move IT backward to the preceding line start or BEGV. This
6744 function may move over more than DY pixels if IT->current_y - DY
6745 ends up in the middle of a line; in this case IT->current_y will be
6746 set to the top of the line moved to. */
6748 void
6749 move_it_vertically_backward (it, dy)
6750 struct it *it;
6751 int dy;
6753 int nlines, h;
6754 struct it it2, it3;
6755 int start_pos;
6757 move_further_back:
6758 xassert (dy >= 0);
6760 start_pos = IT_CHARPOS (*it);
6762 /* Estimate how many newlines we must move back. */
6763 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6765 /* Set the iterator's position that many lines back. */
6766 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6767 back_to_previous_visible_line_start (it);
6769 /* Reseat the iterator here. When moving backward, we don't want
6770 reseat to skip forward over invisible text, set up the iterator
6771 to deliver from overlay strings at the new position etc. So,
6772 use reseat_1 here. */
6773 reseat_1 (it, it->current.pos, 1);
6775 /* We are now surely at a line start. */
6776 it->current_x = it->hpos = 0;
6777 it->continuation_lines_width = 0;
6779 /* Move forward and see what y-distance we moved. First move to the
6780 start of the next line so that we get its height. We need this
6781 height to be able to tell whether we reached the specified
6782 y-distance. */
6783 it2 = *it;
6784 it2.max_ascent = it2.max_descent = 0;
6787 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6788 MOVE_TO_POS | MOVE_TO_VPOS);
6790 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6791 xassert (IT_CHARPOS (*it) >= BEGV);
6792 it3 = it2;
6794 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6795 xassert (IT_CHARPOS (*it) >= BEGV);
6796 /* H is the actual vertical distance from the position in *IT
6797 and the starting position. */
6798 h = it2.current_y - it->current_y;
6799 /* NLINES is the distance in number of lines. */
6800 nlines = it2.vpos - it->vpos;
6802 /* Correct IT's y and vpos position
6803 so that they are relative to the starting point. */
6804 it->vpos -= nlines;
6805 it->current_y -= h;
6807 if (dy == 0)
6809 /* DY == 0 means move to the start of the screen line. The
6810 value of nlines is > 0 if continuation lines were involved. */
6811 if (nlines > 0)
6812 move_it_by_lines (it, nlines, 1);
6813 #if 0
6814 /* I think this assert is bogus if buffer contains
6815 invisible text or images. KFS. */
6816 xassert (IT_CHARPOS (*it) <= start_pos);
6817 #endif
6819 else
6821 /* The y-position we try to reach, relative to *IT.
6822 Note that H has been subtracted in front of the if-statement. */
6823 int target_y = it->current_y + h - dy;
6824 int y0 = it3.current_y;
6825 int y1 = line_bottom_y (&it3);
6826 int line_height = y1 - y0;
6828 /* If we did not reach target_y, try to move further backward if
6829 we can. If we moved too far backward, try to move forward. */
6830 if (target_y < it->current_y
6831 /* This is heuristic. In a window that's 3 lines high, with
6832 a line height of 13 pixels each, recentering with point
6833 on the bottom line will try to move -39/2 = 19 pixels
6834 backward. Try to avoid moving into the first line. */
6835 && (it->current_y - target_y
6836 > min (window_box_height (it->w), line_height * 2 / 3))
6837 && IT_CHARPOS (*it) > BEGV)
6839 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6840 target_y - it->current_y));
6841 dy = it->current_y - target_y;
6842 goto move_further_back;
6844 else if (target_y >= it->current_y + line_height
6845 && IT_CHARPOS (*it) < ZV)
6847 /* Should move forward by at least one line, maybe more.
6849 Note: Calling move_it_by_lines can be expensive on
6850 terminal frames, where compute_motion is used (via
6851 vmotion) to do the job, when there are very long lines
6852 and truncate-lines is nil. That's the reason for
6853 treating terminal frames specially here. */
6855 if (!FRAME_WINDOW_P (it->f))
6856 move_it_vertically (it, target_y - (it->current_y + line_height));
6857 else
6861 move_it_by_lines (it, 1, 1);
6863 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6866 #if 0
6867 /* I think this assert is bogus if buffer contains
6868 invisible text or images. KFS. */
6869 xassert (IT_CHARPOS (*it) >= BEGV);
6870 #endif
6876 /* Move IT by a specified amount of pixel lines DY. DY negative means
6877 move backwards. DY = 0 means move to start of screen line. At the
6878 end, IT will be on the start of a screen line. */
6880 void
6881 move_it_vertically (it, dy)
6882 struct it *it;
6883 int dy;
6885 if (dy <= 0)
6886 move_it_vertically_backward (it, -dy);
6887 else
6889 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6890 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6891 MOVE_TO_POS | MOVE_TO_Y);
6892 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6894 /* If buffer ends in ZV without a newline, move to the start of
6895 the line to satisfy the post-condition. */
6896 if (IT_CHARPOS (*it) == ZV
6897 && ZV > BEGV
6898 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6899 move_it_by_lines (it, 0, 0);
6904 /* Move iterator IT past the end of the text line it is in. */
6906 void
6907 move_it_past_eol (it)
6908 struct it *it;
6910 enum move_it_result rc;
6912 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6913 if (rc == MOVE_NEWLINE_OR_CR)
6914 set_iterator_to_next (it, 0);
6918 #if 0 /* Currently not used. */
6920 /* Return non-zero if some text between buffer positions START_CHARPOS
6921 and END_CHARPOS is invisible. IT->window is the window for text
6922 property lookup. */
6924 static int
6925 invisible_text_between_p (it, start_charpos, end_charpos)
6926 struct it *it;
6927 int start_charpos, end_charpos;
6929 Lisp_Object prop, limit;
6930 int invisible_found_p;
6932 xassert (it != NULL && start_charpos <= end_charpos);
6934 /* Is text at START invisible? */
6935 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6936 it->window);
6937 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6938 invisible_found_p = 1;
6939 else
6941 limit = Fnext_single_char_property_change (make_number (start_charpos),
6942 Qinvisible, Qnil,
6943 make_number (end_charpos));
6944 invisible_found_p = XFASTINT (limit) < end_charpos;
6947 return invisible_found_p;
6950 #endif /* 0 */
6953 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6954 negative means move up. DVPOS == 0 means move to the start of the
6955 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6956 NEED_Y_P is zero, IT->current_y will be left unchanged.
6958 Further optimization ideas: If we would know that IT->f doesn't use
6959 a face with proportional font, we could be faster for
6960 truncate-lines nil. */
6962 void
6963 move_it_by_lines (it, dvpos, need_y_p)
6964 struct it *it;
6965 int dvpos, need_y_p;
6967 struct position pos;
6969 if (!FRAME_WINDOW_P (it->f))
6971 struct text_pos textpos;
6973 /* We can use vmotion on frames without proportional fonts. */
6974 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6975 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6976 reseat (it, textpos, 1);
6977 it->vpos += pos.vpos;
6978 it->current_y += pos.vpos;
6980 else if (dvpos == 0)
6982 /* DVPOS == 0 means move to the start of the screen line. */
6983 move_it_vertically_backward (it, 0);
6984 xassert (it->current_x == 0 && it->hpos == 0);
6985 /* Let next call to line_bottom_y calculate real line height */
6986 last_height = 0;
6988 else if (dvpos > 0)
6990 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6991 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6992 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6994 else
6996 struct it it2;
6997 int start_charpos, i;
6999 /* Start at the beginning of the screen line containing IT's
7000 position. This may actually move vertically backwards,
7001 in case of overlays, so adjust dvpos accordingly. */
7002 dvpos += it->vpos;
7003 move_it_vertically_backward (it, 0);
7004 dvpos -= it->vpos;
7006 /* Go back -DVPOS visible lines and reseat the iterator there. */
7007 start_charpos = IT_CHARPOS (*it);
7008 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7009 back_to_previous_visible_line_start (it);
7010 reseat (it, it->current.pos, 1);
7012 /* Move further back if we end up in a string or an image. */
7013 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7015 /* First try to move to start of display line. */
7016 dvpos += it->vpos;
7017 move_it_vertically_backward (it, 0);
7018 dvpos -= it->vpos;
7019 if (IT_POS_VALID_AFTER_MOVE_P (it))
7020 break;
7021 /* If start of line is still in string or image,
7022 move further back. */
7023 back_to_previous_visible_line_start (it);
7024 reseat (it, it->current.pos, 1);
7025 dvpos--;
7028 it->current_x = it->hpos = 0;
7030 /* Above call may have moved too far if continuation lines
7031 are involved. Scan forward and see if it did. */
7032 it2 = *it;
7033 it2.vpos = it2.current_y = 0;
7034 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7035 it->vpos -= it2.vpos;
7036 it->current_y -= it2.current_y;
7037 it->current_x = it->hpos = 0;
7039 /* If we moved too far back, move IT some lines forward. */
7040 if (it2.vpos > -dvpos)
7042 int delta = it2.vpos + dvpos;
7043 it2 = *it;
7044 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7045 /* Move back again if we got too far ahead. */
7046 if (IT_CHARPOS (*it) >= start_charpos)
7047 *it = it2;
7052 /* Return 1 if IT points into the middle of a display vector. */
7055 in_display_vector_p (it)
7056 struct it *it;
7058 return (it->method == GET_FROM_DISPLAY_VECTOR
7059 && it->current.dpvec_index > 0
7060 && it->dpvec + it->current.dpvec_index != it->dpend);
7064 /***********************************************************************
7065 Messages
7066 ***********************************************************************/
7069 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7070 to *Messages*. */
7072 void
7073 add_to_log (format, arg1, arg2)
7074 char *format;
7075 Lisp_Object arg1, arg2;
7077 Lisp_Object args[3];
7078 Lisp_Object msg, fmt;
7079 char *buffer;
7080 int len;
7081 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7082 USE_SAFE_ALLOCA;
7084 /* Do nothing if called asynchronously. Inserting text into
7085 a buffer may call after-change-functions and alike and
7086 that would means running Lisp asynchronously. */
7087 if (handling_signal)
7088 return;
7090 fmt = msg = Qnil;
7091 GCPRO4 (fmt, msg, arg1, arg2);
7093 args[0] = fmt = build_string (format);
7094 args[1] = arg1;
7095 args[2] = arg2;
7096 msg = Fformat (3, args);
7098 len = SBYTES (msg) + 1;
7099 SAFE_ALLOCA (buffer, char *, len);
7100 bcopy (SDATA (msg), buffer, len);
7102 message_dolog (buffer, len - 1, 1, 0);
7103 SAFE_FREE ();
7105 UNGCPRO;
7109 /* Output a newline in the *Messages* buffer if "needs" one. */
7111 void
7112 message_log_maybe_newline ()
7114 if (message_log_need_newline)
7115 message_dolog ("", 0, 1, 0);
7119 /* Add a string M of length NBYTES to the message log, optionally
7120 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7121 nonzero, means interpret the contents of M as multibyte. This
7122 function calls low-level routines in order to bypass text property
7123 hooks, etc. which might not be safe to run.
7125 This may GC (insert may run before/after change hooks),
7126 so the buffer M must NOT point to a Lisp string. */
7128 void
7129 message_dolog (m, nbytes, nlflag, multibyte)
7130 const char *m;
7131 int nbytes, nlflag, multibyte;
7133 if (!NILP (Vmemory_full))
7134 return;
7136 if (!NILP (Vmessage_log_max))
7138 struct buffer *oldbuf;
7139 Lisp_Object oldpoint, oldbegv, oldzv;
7140 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7141 int point_at_end = 0;
7142 int zv_at_end = 0;
7143 Lisp_Object old_deactivate_mark, tem;
7144 struct gcpro gcpro1;
7146 old_deactivate_mark = Vdeactivate_mark;
7147 oldbuf = current_buffer;
7148 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7149 current_buffer->undo_list = Qt;
7151 oldpoint = message_dolog_marker1;
7152 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7153 oldbegv = message_dolog_marker2;
7154 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7155 oldzv = message_dolog_marker3;
7156 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7157 GCPRO1 (old_deactivate_mark);
7159 if (PT == Z)
7160 point_at_end = 1;
7161 if (ZV == Z)
7162 zv_at_end = 1;
7164 BEGV = BEG;
7165 BEGV_BYTE = BEG_BYTE;
7166 ZV = Z;
7167 ZV_BYTE = Z_BYTE;
7168 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7170 /* Insert the string--maybe converting multibyte to single byte
7171 or vice versa, so that all the text fits the buffer. */
7172 if (multibyte
7173 && NILP (current_buffer->enable_multibyte_characters))
7175 int i, c, char_bytes;
7176 unsigned char work[1];
7178 /* Convert a multibyte string to single-byte
7179 for the *Message* buffer. */
7180 for (i = 0; i < nbytes; i += char_bytes)
7182 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7183 work[0] = (SINGLE_BYTE_CHAR_P (c)
7185 : multibyte_char_to_unibyte (c, Qnil));
7186 insert_1_both (work, 1, 1, 1, 0, 0);
7189 else if (! multibyte
7190 && ! NILP (current_buffer->enable_multibyte_characters))
7192 int i, c, char_bytes;
7193 unsigned char *msg = (unsigned char *) m;
7194 unsigned char str[MAX_MULTIBYTE_LENGTH];
7195 /* Convert a single-byte string to multibyte
7196 for the *Message* buffer. */
7197 for (i = 0; i < nbytes; i++)
7199 c = unibyte_char_to_multibyte (msg[i]);
7200 char_bytes = CHAR_STRING (c, str);
7201 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7204 else if (nbytes)
7205 insert_1 (m, nbytes, 1, 0, 0);
7207 if (nlflag)
7209 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7210 insert_1 ("\n", 1, 1, 0, 0);
7212 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7213 this_bol = PT;
7214 this_bol_byte = PT_BYTE;
7216 /* See if this line duplicates the previous one.
7217 If so, combine duplicates. */
7218 if (this_bol > BEG)
7220 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7221 prev_bol = PT;
7222 prev_bol_byte = PT_BYTE;
7224 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7225 this_bol, this_bol_byte);
7226 if (dup)
7228 del_range_both (prev_bol, prev_bol_byte,
7229 this_bol, this_bol_byte, 0);
7230 if (dup > 1)
7232 char dupstr[40];
7233 int duplen;
7235 /* If you change this format, don't forget to also
7236 change message_log_check_duplicate. */
7237 sprintf (dupstr, " [%d times]", dup);
7238 duplen = strlen (dupstr);
7239 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7240 insert_1 (dupstr, duplen, 1, 0, 1);
7245 /* If we have more than the desired maximum number of lines
7246 in the *Messages* buffer now, delete the oldest ones.
7247 This is safe because we don't have undo in this buffer. */
7249 if (NATNUMP (Vmessage_log_max))
7251 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7252 -XFASTINT (Vmessage_log_max) - 1, 0);
7253 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7256 BEGV = XMARKER (oldbegv)->charpos;
7257 BEGV_BYTE = marker_byte_position (oldbegv);
7259 if (zv_at_end)
7261 ZV = Z;
7262 ZV_BYTE = Z_BYTE;
7264 else
7266 ZV = XMARKER (oldzv)->charpos;
7267 ZV_BYTE = marker_byte_position (oldzv);
7270 if (point_at_end)
7271 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7272 else
7273 /* We can't do Fgoto_char (oldpoint) because it will run some
7274 Lisp code. */
7275 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7276 XMARKER (oldpoint)->bytepos);
7278 UNGCPRO;
7279 unchain_marker (XMARKER (oldpoint));
7280 unchain_marker (XMARKER (oldbegv));
7281 unchain_marker (XMARKER (oldzv));
7283 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7284 set_buffer_internal (oldbuf);
7285 if (NILP (tem))
7286 windows_or_buffers_changed = old_windows_or_buffers_changed;
7287 message_log_need_newline = !nlflag;
7288 Vdeactivate_mark = old_deactivate_mark;
7293 /* We are at the end of the buffer after just having inserted a newline.
7294 (Note: We depend on the fact we won't be crossing the gap.)
7295 Check to see if the most recent message looks a lot like the previous one.
7296 Return 0 if different, 1 if the new one should just replace it, or a
7297 value N > 1 if we should also append " [N times]". */
7299 static int
7300 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7301 int prev_bol, this_bol;
7302 int prev_bol_byte, this_bol_byte;
7304 int i;
7305 int len = Z_BYTE - 1 - this_bol_byte;
7306 int seen_dots = 0;
7307 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7308 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7310 for (i = 0; i < len; i++)
7312 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7313 seen_dots = 1;
7314 if (p1[i] != p2[i])
7315 return seen_dots;
7317 p1 += len;
7318 if (*p1 == '\n')
7319 return 2;
7320 if (*p1++ == ' ' && *p1++ == '[')
7322 int n = 0;
7323 while (*p1 >= '0' && *p1 <= '9')
7324 n = n * 10 + *p1++ - '0';
7325 if (strncmp (p1, " times]\n", 8) == 0)
7326 return n+1;
7328 return 0;
7332 /* Display an echo area message M with a specified length of NBYTES
7333 bytes. The string may include null characters. If M is 0, clear
7334 out any existing message, and let the mini-buffer text show
7335 through.
7337 This may GC, so the buffer M must NOT point to a Lisp string. */
7339 void
7340 message2 (m, nbytes, multibyte)
7341 const char *m;
7342 int nbytes;
7343 int multibyte;
7345 /* First flush out any partial line written with print. */
7346 message_log_maybe_newline ();
7347 if (m)
7348 message_dolog (m, nbytes, 1, multibyte);
7349 message2_nolog (m, nbytes, multibyte);
7353 /* The non-logging counterpart of message2. */
7355 void
7356 message2_nolog (m, nbytes, multibyte)
7357 const char *m;
7358 int nbytes, multibyte;
7360 struct frame *sf = SELECTED_FRAME ();
7361 message_enable_multibyte = multibyte;
7363 if (noninteractive)
7365 if (noninteractive_need_newline)
7366 putc ('\n', stderr);
7367 noninteractive_need_newline = 0;
7368 if (m)
7369 fwrite (m, nbytes, 1, stderr);
7370 if (cursor_in_echo_area == 0)
7371 fprintf (stderr, "\n");
7372 fflush (stderr);
7374 /* A null message buffer means that the frame hasn't really been
7375 initialized yet. Error messages get reported properly by
7376 cmd_error, so this must be just an informative message; toss it. */
7377 else if (INTERACTIVE
7378 && sf->glyphs_initialized_p
7379 && FRAME_MESSAGE_BUF (sf))
7381 Lisp_Object mini_window;
7382 struct frame *f;
7384 /* Get the frame containing the mini-buffer
7385 that the selected frame is using. */
7386 mini_window = FRAME_MINIBUF_WINDOW (sf);
7387 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7389 FRAME_SAMPLE_VISIBILITY (f);
7390 if (FRAME_VISIBLE_P (sf)
7391 && ! FRAME_VISIBLE_P (f))
7392 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7394 if (m)
7396 set_message (m, Qnil, nbytes, multibyte);
7397 if (minibuffer_auto_raise)
7398 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7400 else
7401 clear_message (1, 1);
7403 do_pending_window_change (0);
7404 echo_area_display (1);
7405 do_pending_window_change (0);
7406 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7407 (*frame_up_to_date_hook) (f);
7412 /* Display an echo area message M with a specified length of NBYTES
7413 bytes. The string may include null characters. If M is not a
7414 string, clear out any existing message, and let the mini-buffer
7415 text show through.
7417 This function cancels echoing. */
7419 void
7420 message3 (m, nbytes, multibyte)
7421 Lisp_Object m;
7422 int nbytes;
7423 int multibyte;
7425 struct gcpro gcpro1;
7427 GCPRO1 (m);
7428 clear_message (1,1);
7429 cancel_echoing ();
7431 /* First flush out any partial line written with print. */
7432 message_log_maybe_newline ();
7433 if (STRINGP (m))
7435 char *buffer;
7436 USE_SAFE_ALLOCA;
7438 SAFE_ALLOCA (buffer, char *, nbytes);
7439 bcopy (SDATA (m), buffer, nbytes);
7440 message_dolog (buffer, nbytes, 1, multibyte);
7441 SAFE_FREE ();
7443 message3_nolog (m, nbytes, multibyte);
7445 UNGCPRO;
7449 /* The non-logging version of message3.
7450 This does not cancel echoing, because it is used for echoing.
7451 Perhaps we need to make a separate function for echoing
7452 and make this cancel echoing. */
7454 void
7455 message3_nolog (m, nbytes, multibyte)
7456 Lisp_Object m;
7457 int nbytes, multibyte;
7459 struct frame *sf = SELECTED_FRAME ();
7460 message_enable_multibyte = multibyte;
7462 if (noninteractive)
7464 if (noninteractive_need_newline)
7465 putc ('\n', stderr);
7466 noninteractive_need_newline = 0;
7467 if (STRINGP (m))
7468 fwrite (SDATA (m), nbytes, 1, stderr);
7469 if (cursor_in_echo_area == 0)
7470 fprintf (stderr, "\n");
7471 fflush (stderr);
7473 /* A null message buffer means that the frame hasn't really been
7474 initialized yet. Error messages get reported properly by
7475 cmd_error, so this must be just an informative message; toss it. */
7476 else if (INTERACTIVE
7477 && sf->glyphs_initialized_p
7478 && FRAME_MESSAGE_BUF (sf))
7480 Lisp_Object mini_window;
7481 Lisp_Object frame;
7482 struct frame *f;
7484 /* Get the frame containing the mini-buffer
7485 that the selected frame is using. */
7486 mini_window = FRAME_MINIBUF_WINDOW (sf);
7487 frame = XWINDOW (mini_window)->frame;
7488 f = XFRAME (frame);
7490 FRAME_SAMPLE_VISIBILITY (f);
7491 if (FRAME_VISIBLE_P (sf)
7492 && !FRAME_VISIBLE_P (f))
7493 Fmake_frame_visible (frame);
7495 if (STRINGP (m) && SCHARS (m) > 0)
7497 set_message (NULL, m, nbytes, multibyte);
7498 if (minibuffer_auto_raise)
7499 Fraise_frame (frame);
7500 /* Assume we are not echoing.
7501 (If we are, echo_now will override this.) */
7502 echo_message_buffer = Qnil;
7504 else
7505 clear_message (1, 1);
7507 do_pending_window_change (0);
7508 echo_area_display (1);
7509 do_pending_window_change (0);
7510 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7511 (*frame_up_to_date_hook) (f);
7516 /* Display a null-terminated echo area message M. If M is 0, clear
7517 out any existing message, and let the mini-buffer text show through.
7519 The buffer M must continue to exist until after the echo area gets
7520 cleared or some other message gets displayed there. Do not pass
7521 text that is stored in a Lisp string. Do not pass text in a buffer
7522 that was alloca'd. */
7524 void
7525 message1 (m)
7526 char *m;
7528 message2 (m, (m ? strlen (m) : 0), 0);
7532 /* The non-logging counterpart of message1. */
7534 void
7535 message1_nolog (m)
7536 char *m;
7538 message2_nolog (m, (m ? strlen (m) : 0), 0);
7541 /* Display a message M which contains a single %s
7542 which gets replaced with STRING. */
7544 void
7545 message_with_string (m, string, log)
7546 char *m;
7547 Lisp_Object string;
7548 int log;
7550 CHECK_STRING (string);
7552 if (noninteractive)
7554 if (m)
7556 if (noninteractive_need_newline)
7557 putc ('\n', stderr);
7558 noninteractive_need_newline = 0;
7559 fprintf (stderr, m, SDATA (string));
7560 if (cursor_in_echo_area == 0)
7561 fprintf (stderr, "\n");
7562 fflush (stderr);
7565 else if (INTERACTIVE)
7567 /* The frame whose minibuffer we're going to display the message on.
7568 It may be larger than the selected frame, so we need
7569 to use its buffer, not the selected frame's buffer. */
7570 Lisp_Object mini_window;
7571 struct frame *f, *sf = SELECTED_FRAME ();
7573 /* Get the frame containing the minibuffer
7574 that the selected frame is using. */
7575 mini_window = FRAME_MINIBUF_WINDOW (sf);
7576 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7578 /* A null message buffer means that the frame hasn't really been
7579 initialized yet. Error messages get reported properly by
7580 cmd_error, so this must be just an informative message; toss it. */
7581 if (FRAME_MESSAGE_BUF (f))
7583 Lisp_Object args[2], message;
7584 struct gcpro gcpro1, gcpro2;
7586 args[0] = build_string (m);
7587 args[1] = message = string;
7588 GCPRO2 (args[0], message);
7589 gcpro1.nvars = 2;
7591 message = Fformat (2, args);
7593 if (log)
7594 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7595 else
7596 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7598 UNGCPRO;
7600 /* Print should start at the beginning of the message
7601 buffer next time. */
7602 message_buf_print = 0;
7608 /* Dump an informative message to the minibuf. If M is 0, clear out
7609 any existing message, and let the mini-buffer text show through. */
7611 /* VARARGS 1 */
7612 void
7613 message (m, a1, a2, a3)
7614 char *m;
7615 EMACS_INT a1, a2, a3;
7617 if (noninteractive)
7619 if (m)
7621 if (noninteractive_need_newline)
7622 putc ('\n', stderr);
7623 noninteractive_need_newline = 0;
7624 fprintf (stderr, m, a1, a2, a3);
7625 if (cursor_in_echo_area == 0)
7626 fprintf (stderr, "\n");
7627 fflush (stderr);
7630 else if (INTERACTIVE)
7632 /* The frame whose mini-buffer we're going to display the message
7633 on. It may be larger than the selected frame, so we need to
7634 use its buffer, not the selected frame's buffer. */
7635 Lisp_Object mini_window;
7636 struct frame *f, *sf = SELECTED_FRAME ();
7638 /* Get the frame containing the mini-buffer
7639 that the selected frame is using. */
7640 mini_window = FRAME_MINIBUF_WINDOW (sf);
7641 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7643 /* A null message buffer means that the frame hasn't really been
7644 initialized yet. Error messages get reported properly by
7645 cmd_error, so this must be just an informative message; toss
7646 it. */
7647 if (FRAME_MESSAGE_BUF (f))
7649 if (m)
7651 int len;
7652 #ifdef NO_ARG_ARRAY
7653 char *a[3];
7654 a[0] = (char *) a1;
7655 a[1] = (char *) a2;
7656 a[2] = (char *) a3;
7658 len = doprnt (FRAME_MESSAGE_BUF (f),
7659 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7660 #else
7661 len = doprnt (FRAME_MESSAGE_BUF (f),
7662 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7663 (char **) &a1);
7664 #endif /* NO_ARG_ARRAY */
7666 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7668 else
7669 message1 (0);
7671 /* Print should start at the beginning of the message
7672 buffer next time. */
7673 message_buf_print = 0;
7679 /* The non-logging version of message. */
7681 void
7682 message_nolog (m, a1, a2, a3)
7683 char *m;
7684 EMACS_INT a1, a2, a3;
7686 Lisp_Object old_log_max;
7687 old_log_max = Vmessage_log_max;
7688 Vmessage_log_max = Qnil;
7689 message (m, a1, a2, a3);
7690 Vmessage_log_max = old_log_max;
7694 /* Display the current message in the current mini-buffer. This is
7695 only called from error handlers in process.c, and is not time
7696 critical. */
7698 void
7699 update_echo_area ()
7701 if (!NILP (echo_area_buffer[0]))
7703 Lisp_Object string;
7704 string = Fcurrent_message ();
7705 message3 (string, SBYTES (string),
7706 !NILP (current_buffer->enable_multibyte_characters));
7711 /* Make sure echo area buffers in `echo_buffers' are live.
7712 If they aren't, make new ones. */
7714 static void
7715 ensure_echo_area_buffers ()
7717 int i;
7719 for (i = 0; i < 2; ++i)
7720 if (!BUFFERP (echo_buffer[i])
7721 || NILP (XBUFFER (echo_buffer[i])->name))
7723 char name[30];
7724 Lisp_Object old_buffer;
7725 int j;
7727 old_buffer = echo_buffer[i];
7728 sprintf (name, " *Echo Area %d*", i);
7729 echo_buffer[i] = Fget_buffer_create (build_string (name));
7730 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7732 for (j = 0; j < 2; ++j)
7733 if (EQ (old_buffer, echo_area_buffer[j]))
7734 echo_area_buffer[j] = echo_buffer[i];
7739 /* Call FN with args A1..A4 with either the current or last displayed
7740 echo_area_buffer as current buffer.
7742 WHICH zero means use the current message buffer
7743 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7744 from echo_buffer[] and clear it.
7746 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7747 suitable buffer from echo_buffer[] and clear it.
7749 Value is what FN returns. */
7751 static int
7752 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7753 struct window *w;
7754 int which;
7755 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7756 EMACS_INT a1;
7757 Lisp_Object a2;
7758 EMACS_INT a3, a4;
7760 Lisp_Object buffer;
7761 int this_one, the_other, clear_buffer_p, rc;
7762 int count = SPECPDL_INDEX ();
7764 /* If buffers aren't live, make new ones. */
7765 ensure_echo_area_buffers ();
7767 clear_buffer_p = 0;
7769 if (which == 0)
7770 this_one = 0, the_other = 1;
7771 else if (which > 0)
7772 this_one = 1, the_other = 0;
7774 /* Choose a suitable buffer from echo_buffer[] is we don't
7775 have one. */
7776 if (NILP (echo_area_buffer[this_one]))
7778 echo_area_buffer[this_one]
7779 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7780 ? echo_buffer[the_other]
7781 : echo_buffer[this_one]);
7782 clear_buffer_p = 1;
7785 buffer = echo_area_buffer[this_one];
7787 /* Don't get confused by reusing the buffer used for echoing
7788 for a different purpose. */
7789 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7790 cancel_echoing ();
7792 record_unwind_protect (unwind_with_echo_area_buffer,
7793 with_echo_area_buffer_unwind_data (w));
7795 /* Make the echo area buffer current. Note that for display
7796 purposes, it is not necessary that the displayed window's buffer
7797 == current_buffer, except for text property lookup. So, let's
7798 only set that buffer temporarily here without doing a full
7799 Fset_window_buffer. We must also change w->pointm, though,
7800 because otherwise an assertions in unshow_buffer fails, and Emacs
7801 aborts. */
7802 set_buffer_internal_1 (XBUFFER (buffer));
7803 if (w)
7805 w->buffer = buffer;
7806 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7809 current_buffer->undo_list = Qt;
7810 current_buffer->read_only = Qnil;
7811 specbind (Qinhibit_read_only, Qt);
7812 specbind (Qinhibit_modification_hooks, Qt);
7814 if (clear_buffer_p && Z > BEG)
7815 del_range (BEG, Z);
7817 xassert (BEGV >= BEG);
7818 xassert (ZV <= Z && ZV >= BEGV);
7820 rc = fn (a1, a2, a3, a4);
7822 xassert (BEGV >= BEG);
7823 xassert (ZV <= Z && ZV >= BEGV);
7825 unbind_to (count, Qnil);
7826 return rc;
7830 /* Save state that should be preserved around the call to the function
7831 FN called in with_echo_area_buffer. */
7833 static Lisp_Object
7834 with_echo_area_buffer_unwind_data (w)
7835 struct window *w;
7837 int i = 0;
7838 Lisp_Object vector;
7840 /* Reduce consing by keeping one vector in
7841 Vwith_echo_area_save_vector. */
7842 vector = Vwith_echo_area_save_vector;
7843 Vwith_echo_area_save_vector = Qnil;
7845 if (NILP (vector))
7846 vector = Fmake_vector (make_number (7), Qnil);
7848 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7849 AREF (vector, i) = Vdeactivate_mark, ++i;
7850 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7852 if (w)
7854 XSETWINDOW (AREF (vector, i), w); ++i;
7855 AREF (vector, i) = w->buffer; ++i;
7856 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7857 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7859 else
7861 int end = i + 4;
7862 for (; i < end; ++i)
7863 AREF (vector, i) = Qnil;
7866 xassert (i == ASIZE (vector));
7867 return vector;
7871 /* Restore global state from VECTOR which was created by
7872 with_echo_area_buffer_unwind_data. */
7874 static Lisp_Object
7875 unwind_with_echo_area_buffer (vector)
7876 Lisp_Object vector;
7878 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7879 Vdeactivate_mark = AREF (vector, 1);
7880 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7882 if (WINDOWP (AREF (vector, 3)))
7884 struct window *w;
7885 Lisp_Object buffer, charpos, bytepos;
7887 w = XWINDOW (AREF (vector, 3));
7888 buffer = AREF (vector, 4);
7889 charpos = AREF (vector, 5);
7890 bytepos = AREF (vector, 6);
7892 w->buffer = buffer;
7893 set_marker_both (w->pointm, buffer,
7894 XFASTINT (charpos), XFASTINT (bytepos));
7897 Vwith_echo_area_save_vector = vector;
7898 return Qnil;
7902 /* Set up the echo area for use by print functions. MULTIBYTE_P
7903 non-zero means we will print multibyte. */
7905 void
7906 setup_echo_area_for_printing (multibyte_p)
7907 int multibyte_p;
7909 /* If we can't find an echo area any more, exit. */
7910 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7911 Fkill_emacs (Qnil);
7913 ensure_echo_area_buffers ();
7915 if (!message_buf_print)
7917 /* A message has been output since the last time we printed.
7918 Choose a fresh echo area buffer. */
7919 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7920 echo_area_buffer[0] = echo_buffer[1];
7921 else
7922 echo_area_buffer[0] = echo_buffer[0];
7924 /* Switch to that buffer and clear it. */
7925 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7926 current_buffer->truncate_lines = Qnil;
7928 if (Z > BEG)
7930 int count = SPECPDL_INDEX ();
7931 specbind (Qinhibit_read_only, Qt);
7932 /* Note that undo recording is always disabled. */
7933 del_range (BEG, Z);
7934 unbind_to (count, Qnil);
7936 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7938 /* Set up the buffer for the multibyteness we need. */
7939 if (multibyte_p
7940 != !NILP (current_buffer->enable_multibyte_characters))
7941 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7943 /* Raise the frame containing the echo area. */
7944 if (minibuffer_auto_raise)
7946 struct frame *sf = SELECTED_FRAME ();
7947 Lisp_Object mini_window;
7948 mini_window = FRAME_MINIBUF_WINDOW (sf);
7949 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7952 message_log_maybe_newline ();
7953 message_buf_print = 1;
7955 else
7957 if (NILP (echo_area_buffer[0]))
7959 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7960 echo_area_buffer[0] = echo_buffer[1];
7961 else
7962 echo_area_buffer[0] = echo_buffer[0];
7965 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7967 /* Someone switched buffers between print requests. */
7968 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7969 current_buffer->truncate_lines = Qnil;
7975 /* Display an echo area message in window W. Value is non-zero if W's
7976 height is changed. If display_last_displayed_message_p is
7977 non-zero, display the message that was last displayed, otherwise
7978 display the current message. */
7980 static int
7981 display_echo_area (w)
7982 struct window *w;
7984 int i, no_message_p, window_height_changed_p, count;
7986 /* Temporarily disable garbage collections while displaying the echo
7987 area. This is done because a GC can print a message itself.
7988 That message would modify the echo area buffer's contents while a
7989 redisplay of the buffer is going on, and seriously confuse
7990 redisplay. */
7991 count = inhibit_garbage_collection ();
7993 /* If there is no message, we must call display_echo_area_1
7994 nevertheless because it resizes the window. But we will have to
7995 reset the echo_area_buffer in question to nil at the end because
7996 with_echo_area_buffer will sets it to an empty buffer. */
7997 i = display_last_displayed_message_p ? 1 : 0;
7998 no_message_p = NILP (echo_area_buffer[i]);
8000 window_height_changed_p
8001 = with_echo_area_buffer (w, display_last_displayed_message_p,
8002 display_echo_area_1,
8003 (EMACS_INT) w, Qnil, 0, 0);
8005 if (no_message_p)
8006 echo_area_buffer[i] = Qnil;
8008 unbind_to (count, Qnil);
8009 return window_height_changed_p;
8013 /* Helper for display_echo_area. Display the current buffer which
8014 contains the current echo area message in window W, a mini-window,
8015 a pointer to which is passed in A1. A2..A4 are currently not used.
8016 Change the height of W so that all of the message is displayed.
8017 Value is non-zero if height of W was changed. */
8019 static int
8020 display_echo_area_1 (a1, a2, a3, a4)
8021 EMACS_INT a1;
8022 Lisp_Object a2;
8023 EMACS_INT a3, a4;
8025 struct window *w = (struct window *) a1;
8026 Lisp_Object window;
8027 struct text_pos start;
8028 int window_height_changed_p = 0;
8030 /* Do this before displaying, so that we have a large enough glyph
8031 matrix for the display. If we can't get enough space for the
8032 whole text, display the last N lines. That works by setting w->start. */
8033 window_height_changed_p = resize_mini_window (w, 0);
8035 /* Use the starting position chosen by resize_mini_window. */
8036 SET_TEXT_POS_FROM_MARKER (start, w->start);
8038 /* Display. */
8039 clear_glyph_matrix (w->desired_matrix);
8040 XSETWINDOW (window, w);
8041 try_window (window, start, 0);
8043 return window_height_changed_p;
8047 /* Resize the echo area window to exactly the size needed for the
8048 currently displayed message, if there is one. If a mini-buffer
8049 is active, don't shrink it. */
8051 void
8052 resize_echo_area_exactly ()
8054 if (BUFFERP (echo_area_buffer[0])
8055 && WINDOWP (echo_area_window))
8057 struct window *w = XWINDOW (echo_area_window);
8058 int resized_p;
8059 Lisp_Object resize_exactly;
8061 if (minibuf_level == 0)
8062 resize_exactly = Qt;
8063 else
8064 resize_exactly = Qnil;
8066 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8067 (EMACS_INT) w, resize_exactly, 0, 0);
8068 if (resized_p)
8070 ++windows_or_buffers_changed;
8071 ++update_mode_lines;
8072 redisplay_internal (0);
8078 /* Callback function for with_echo_area_buffer, when used from
8079 resize_echo_area_exactly. A1 contains a pointer to the window to
8080 resize, EXACTLY non-nil means resize the mini-window exactly to the
8081 size of the text displayed. A3 and A4 are not used. Value is what
8082 resize_mini_window returns. */
8084 static int
8085 resize_mini_window_1 (a1, exactly, a3, a4)
8086 EMACS_INT a1;
8087 Lisp_Object exactly;
8088 EMACS_INT a3, a4;
8090 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8094 /* Resize mini-window W to fit the size of its contents. EXACT:P
8095 means size the window exactly to the size needed. Otherwise, it's
8096 only enlarged until W's buffer is empty.
8098 Set W->start to the right place to begin display. If the whole
8099 contents fit, start at the beginning. Otherwise, start so as
8100 to make the end of the contents appear. This is particularly
8101 important for y-or-n-p, but seems desirable generally.
8103 Value is non-zero if the window height has been changed. */
8106 resize_mini_window (w, exact_p)
8107 struct window *w;
8108 int exact_p;
8110 struct frame *f = XFRAME (w->frame);
8111 int window_height_changed_p = 0;
8113 xassert (MINI_WINDOW_P (w));
8115 /* By default, start display at the beginning. */
8116 set_marker_both (w->start, w->buffer,
8117 BUF_BEGV (XBUFFER (w->buffer)),
8118 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8120 /* Don't resize windows while redisplaying a window; it would
8121 confuse redisplay functions when the size of the window they are
8122 displaying changes from under them. Such a resizing can happen,
8123 for instance, when which-func prints a long message while
8124 we are running fontification-functions. We're running these
8125 functions with safe_call which binds inhibit-redisplay to t. */
8126 if (!NILP (Vinhibit_redisplay))
8127 return 0;
8129 /* Nil means don't try to resize. */
8130 if (NILP (Vresize_mini_windows)
8131 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8132 return 0;
8134 if (!FRAME_MINIBUF_ONLY_P (f))
8136 struct it it;
8137 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8138 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8139 int height, max_height;
8140 int unit = FRAME_LINE_HEIGHT (f);
8141 struct text_pos start;
8142 struct buffer *old_current_buffer = NULL;
8144 if (current_buffer != XBUFFER (w->buffer))
8146 old_current_buffer = current_buffer;
8147 set_buffer_internal (XBUFFER (w->buffer));
8150 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8152 /* Compute the max. number of lines specified by the user. */
8153 if (FLOATP (Vmax_mini_window_height))
8154 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8155 else if (INTEGERP (Vmax_mini_window_height))
8156 max_height = XINT (Vmax_mini_window_height);
8157 else
8158 max_height = total_height / 4;
8160 /* Correct that max. height if it's bogus. */
8161 max_height = max (1, max_height);
8162 max_height = min (total_height, max_height);
8164 /* Find out the height of the text in the window. */
8165 if (it.truncate_lines_p)
8166 height = 1;
8167 else
8169 last_height = 0;
8170 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8171 if (it.max_ascent == 0 && it.max_descent == 0)
8172 height = it.current_y + last_height;
8173 else
8174 height = it.current_y + it.max_ascent + it.max_descent;
8175 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8176 height = (height + unit - 1) / unit;
8179 /* Compute a suitable window start. */
8180 if (height > max_height)
8182 height = max_height;
8183 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8184 move_it_vertically_backward (&it, (height - 1) * unit);
8185 start = it.current.pos;
8187 else
8188 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8189 SET_MARKER_FROM_TEXT_POS (w->start, start);
8191 if (EQ (Vresize_mini_windows, Qgrow_only))
8193 /* Let it grow only, until we display an empty message, in which
8194 case the window shrinks again. */
8195 if (height > WINDOW_TOTAL_LINES (w))
8197 int old_height = WINDOW_TOTAL_LINES (w);
8198 freeze_window_starts (f, 1);
8199 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8200 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8202 else if (height < WINDOW_TOTAL_LINES (w)
8203 && (exact_p || BEGV == ZV))
8205 int old_height = WINDOW_TOTAL_LINES (w);
8206 freeze_window_starts (f, 0);
8207 shrink_mini_window (w);
8208 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8211 else
8213 /* Always resize to exact size needed. */
8214 if (height > WINDOW_TOTAL_LINES (w))
8216 int old_height = WINDOW_TOTAL_LINES (w);
8217 freeze_window_starts (f, 1);
8218 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8219 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8221 else if (height < WINDOW_TOTAL_LINES (w))
8223 int old_height = WINDOW_TOTAL_LINES (w);
8224 freeze_window_starts (f, 0);
8225 shrink_mini_window (w);
8227 if (height)
8229 freeze_window_starts (f, 1);
8230 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8233 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8237 if (old_current_buffer)
8238 set_buffer_internal (old_current_buffer);
8241 return window_height_changed_p;
8245 /* Value is the current message, a string, or nil if there is no
8246 current message. */
8248 Lisp_Object
8249 current_message ()
8251 Lisp_Object msg;
8253 if (NILP (echo_area_buffer[0]))
8254 msg = Qnil;
8255 else
8257 with_echo_area_buffer (0, 0, current_message_1,
8258 (EMACS_INT) &msg, Qnil, 0, 0);
8259 if (NILP (msg))
8260 echo_area_buffer[0] = Qnil;
8263 return msg;
8267 static int
8268 current_message_1 (a1, a2, a3, a4)
8269 EMACS_INT a1;
8270 Lisp_Object a2;
8271 EMACS_INT a3, a4;
8273 Lisp_Object *msg = (Lisp_Object *) a1;
8275 if (Z > BEG)
8276 *msg = make_buffer_string (BEG, Z, 1);
8277 else
8278 *msg = Qnil;
8279 return 0;
8283 /* Push the current message on Vmessage_stack for later restauration
8284 by restore_message. Value is non-zero if the current message isn't
8285 empty. This is a relatively infrequent operation, so it's not
8286 worth optimizing. */
8289 push_message ()
8291 Lisp_Object msg;
8292 msg = current_message ();
8293 Vmessage_stack = Fcons (msg, Vmessage_stack);
8294 return STRINGP (msg);
8298 /* Restore message display from the top of Vmessage_stack. */
8300 void
8301 restore_message ()
8303 Lisp_Object msg;
8305 xassert (CONSP (Vmessage_stack));
8306 msg = XCAR (Vmessage_stack);
8307 if (STRINGP (msg))
8308 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8309 else
8310 message3_nolog (msg, 0, 0);
8314 /* Handler for record_unwind_protect calling pop_message. */
8316 Lisp_Object
8317 pop_message_unwind (dummy)
8318 Lisp_Object dummy;
8320 pop_message ();
8321 return Qnil;
8324 /* Pop the top-most entry off Vmessage_stack. */
8326 void
8327 pop_message ()
8329 xassert (CONSP (Vmessage_stack));
8330 Vmessage_stack = XCDR (Vmessage_stack);
8334 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8335 exits. If the stack is not empty, we have a missing pop_message
8336 somewhere. */
8338 void
8339 check_message_stack ()
8341 if (!NILP (Vmessage_stack))
8342 abort ();
8346 /* Truncate to NCHARS what will be displayed in the echo area the next
8347 time we display it---but don't redisplay it now. */
8349 void
8350 truncate_echo_area (nchars)
8351 int nchars;
8353 if (nchars == 0)
8354 echo_area_buffer[0] = Qnil;
8355 /* A null message buffer means that the frame hasn't really been
8356 initialized yet. Error messages get reported properly by
8357 cmd_error, so this must be just an informative message; toss it. */
8358 else if (!noninteractive
8359 && INTERACTIVE
8360 && !NILP (echo_area_buffer[0]))
8362 struct frame *sf = SELECTED_FRAME ();
8363 if (FRAME_MESSAGE_BUF (sf))
8364 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8369 /* Helper function for truncate_echo_area. Truncate the current
8370 message to at most NCHARS characters. */
8372 static int
8373 truncate_message_1 (nchars, a2, a3, a4)
8374 EMACS_INT nchars;
8375 Lisp_Object a2;
8376 EMACS_INT a3, a4;
8378 if (BEG + nchars < Z)
8379 del_range (BEG + nchars, Z);
8380 if (Z == BEG)
8381 echo_area_buffer[0] = Qnil;
8382 return 0;
8386 /* Set the current message to a substring of S or STRING.
8388 If STRING is a Lisp string, set the message to the first NBYTES
8389 bytes from STRING. NBYTES zero means use the whole string. If
8390 STRING is multibyte, the message will be displayed multibyte.
8392 If S is not null, set the message to the first LEN bytes of S. LEN
8393 zero means use the whole string. MULTIBYTE_P non-zero means S is
8394 multibyte. Display the message multibyte in that case.
8396 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8397 to t before calling set_message_1 (which calls insert).
8400 void
8401 set_message (s, string, nbytes, multibyte_p)
8402 const char *s;
8403 Lisp_Object string;
8404 int nbytes, multibyte_p;
8406 message_enable_multibyte
8407 = ((s && multibyte_p)
8408 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8410 with_echo_area_buffer (0, 0, set_message_1,
8411 (EMACS_INT) s, string, nbytes, multibyte_p);
8412 message_buf_print = 0;
8413 help_echo_showing_p = 0;
8417 /* Helper function for set_message. Arguments have the same meaning
8418 as there, with A1 corresponding to S and A2 corresponding to STRING
8419 This function is called with the echo area buffer being
8420 current. */
8422 static int
8423 set_message_1 (a1, a2, nbytes, multibyte_p)
8424 EMACS_INT a1;
8425 Lisp_Object a2;
8426 EMACS_INT nbytes, multibyte_p;
8428 const char *s = (const char *) a1;
8429 Lisp_Object string = a2;
8431 /* Change multibyteness of the echo buffer appropriately. */
8432 if (message_enable_multibyte
8433 != !NILP (current_buffer->enable_multibyte_characters))
8434 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8436 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8438 /* Insert new message at BEG. */
8439 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8440 Ferase_buffer ();
8442 if (STRINGP (string))
8444 int nchars;
8446 if (nbytes == 0)
8447 nbytes = SBYTES (string);
8448 nchars = string_byte_to_char (string, nbytes);
8450 /* This function takes care of single/multibyte conversion. We
8451 just have to ensure that the echo area buffer has the right
8452 setting of enable_multibyte_characters. */
8453 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8455 else if (s)
8457 if (nbytes == 0)
8458 nbytes = strlen (s);
8460 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8462 /* Convert from multi-byte to single-byte. */
8463 int i, c, n;
8464 unsigned char work[1];
8466 /* Convert a multibyte string to single-byte. */
8467 for (i = 0; i < nbytes; i += n)
8469 c = string_char_and_length (s + i, nbytes - i, &n);
8470 work[0] = (SINGLE_BYTE_CHAR_P (c)
8472 : multibyte_char_to_unibyte (c, Qnil));
8473 insert_1_both (work, 1, 1, 1, 0, 0);
8476 else if (!multibyte_p
8477 && !NILP (current_buffer->enable_multibyte_characters))
8479 /* Convert from single-byte to multi-byte. */
8480 int i, c, n;
8481 const unsigned char *msg = (const unsigned char *) s;
8482 unsigned char str[MAX_MULTIBYTE_LENGTH];
8484 /* Convert a single-byte string to multibyte. */
8485 for (i = 0; i < nbytes; i++)
8487 c = unibyte_char_to_multibyte (msg[i]);
8488 n = CHAR_STRING (c, str);
8489 insert_1_both (str, 1, n, 1, 0, 0);
8492 else
8493 insert_1 (s, nbytes, 1, 0, 0);
8496 return 0;
8500 /* Clear messages. CURRENT_P non-zero means clear the current
8501 message. LAST_DISPLAYED_P non-zero means clear the message
8502 last displayed. */
8504 void
8505 clear_message (current_p, last_displayed_p)
8506 int current_p, last_displayed_p;
8508 if (current_p)
8510 echo_area_buffer[0] = Qnil;
8511 message_cleared_p = 1;
8514 if (last_displayed_p)
8515 echo_area_buffer[1] = Qnil;
8517 message_buf_print = 0;
8520 /* Clear garbaged frames.
8522 This function is used where the old redisplay called
8523 redraw_garbaged_frames which in turn called redraw_frame which in
8524 turn called clear_frame. The call to clear_frame was a source of
8525 flickering. I believe a clear_frame is not necessary. It should
8526 suffice in the new redisplay to invalidate all current matrices,
8527 and ensure a complete redisplay of all windows. */
8529 static void
8530 clear_garbaged_frames ()
8532 if (frame_garbaged)
8534 Lisp_Object tail, frame;
8535 int changed_count = 0;
8537 FOR_EACH_FRAME (tail, frame)
8539 struct frame *f = XFRAME (frame);
8541 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8543 if (f->resized_p)
8545 Fredraw_frame (frame);
8546 f->force_flush_display_p = 1;
8548 clear_current_matrices (f);
8549 changed_count++;
8550 f->garbaged = 0;
8551 f->resized_p = 0;
8555 frame_garbaged = 0;
8556 if (changed_count)
8557 ++windows_or_buffers_changed;
8562 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8563 is non-zero update selected_frame. Value is non-zero if the
8564 mini-windows height has been changed. */
8566 static int
8567 echo_area_display (update_frame_p)
8568 int update_frame_p;
8570 Lisp_Object mini_window;
8571 struct window *w;
8572 struct frame *f;
8573 int window_height_changed_p = 0;
8574 struct frame *sf = SELECTED_FRAME ();
8576 mini_window = FRAME_MINIBUF_WINDOW (sf);
8577 w = XWINDOW (mini_window);
8578 f = XFRAME (WINDOW_FRAME (w));
8580 /* Don't display if frame is invisible or not yet initialized. */
8581 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8582 return 0;
8584 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8585 #ifndef MAC_OS8
8586 #ifdef HAVE_WINDOW_SYSTEM
8587 /* When Emacs starts, selected_frame may be a visible terminal
8588 frame, even if we run under a window system. If we let this
8589 through, a message would be displayed on the terminal. */
8590 if (EQ (selected_frame, Vterminal_frame)
8591 && !NILP (Vwindow_system))
8592 return 0;
8593 #endif /* HAVE_WINDOW_SYSTEM */
8594 #endif
8596 /* Redraw garbaged frames. */
8597 if (frame_garbaged)
8598 clear_garbaged_frames ();
8600 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8602 echo_area_window = mini_window;
8603 window_height_changed_p = display_echo_area (w);
8604 w->must_be_updated_p = 1;
8606 /* Update the display, unless called from redisplay_internal.
8607 Also don't update the screen during redisplay itself. The
8608 update will happen at the end of redisplay, and an update
8609 here could cause confusion. */
8610 if (update_frame_p && !redisplaying_p)
8612 int n = 0;
8614 /* If the display update has been interrupted by pending
8615 input, update mode lines in the frame. Due to the
8616 pending input, it might have been that redisplay hasn't
8617 been called, so that mode lines above the echo area are
8618 garbaged. This looks odd, so we prevent it here. */
8619 if (!display_completed)
8620 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8622 if (window_height_changed_p
8623 /* Don't do this if Emacs is shutting down. Redisplay
8624 needs to run hooks. */
8625 && !NILP (Vrun_hooks))
8627 /* Must update other windows. Likewise as in other
8628 cases, don't let this update be interrupted by
8629 pending input. */
8630 int count = SPECPDL_INDEX ();
8631 specbind (Qredisplay_dont_pause, Qt);
8632 windows_or_buffers_changed = 1;
8633 redisplay_internal (0);
8634 unbind_to (count, Qnil);
8636 else if (FRAME_WINDOW_P (f) && n == 0)
8638 /* Window configuration is the same as before.
8639 Can do with a display update of the echo area,
8640 unless we displayed some mode lines. */
8641 update_single_window (w, 1);
8642 rif->flush_display (f);
8644 else
8645 update_frame (f, 1, 1);
8647 /* If cursor is in the echo area, make sure that the next
8648 redisplay displays the minibuffer, so that the cursor will
8649 be replaced with what the minibuffer wants. */
8650 if (cursor_in_echo_area)
8651 ++windows_or_buffers_changed;
8654 else if (!EQ (mini_window, selected_window))
8655 windows_or_buffers_changed++;
8657 /* The current message is now also the last one displayed. */
8658 echo_area_buffer[1] = echo_area_buffer[0];
8660 /* Prevent redisplay optimization in redisplay_internal by resetting
8661 this_line_start_pos. This is done because the mini-buffer now
8662 displays the message instead of its buffer text. */
8663 if (EQ (mini_window, selected_window))
8664 CHARPOS (this_line_start_pos) = 0;
8666 return window_height_changed_p;
8671 /***********************************************************************
8672 Mode Lines and Frame Titles
8673 ***********************************************************************/
8675 /* A buffer for constructing non-propertized mode-line strings and
8676 frame titles in it; allocated from the heap in init_xdisp and
8677 resized as needed in store_mode_line_noprop_char. */
8679 static char *mode_line_noprop_buf;
8681 /* The buffer's end, and a current output position in it. */
8683 static char *mode_line_noprop_buf_end;
8684 static char *mode_line_noprop_ptr;
8686 #define MODE_LINE_NOPROP_LEN(start) \
8687 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8689 static enum {
8690 MODE_LINE_DISPLAY = 0,
8691 MODE_LINE_TITLE,
8692 MODE_LINE_NOPROP,
8693 MODE_LINE_STRING
8694 } mode_line_target;
8696 /* Alist that caches the results of :propertize.
8697 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8698 static Lisp_Object mode_line_proptrans_alist;
8700 /* List of strings making up the mode-line. */
8701 static Lisp_Object mode_line_string_list;
8703 /* Base face property when building propertized mode line string. */
8704 static Lisp_Object mode_line_string_face;
8705 static Lisp_Object mode_line_string_face_prop;
8708 /* Unwind data for mode line strings */
8710 static Lisp_Object Vmode_line_unwind_vector;
8712 static Lisp_Object
8713 format_mode_line_unwind_data (obuf, save_proptrans)
8714 struct buffer *obuf;
8716 Lisp_Object vector;
8718 /* Reduce consing by keeping one vector in
8719 Vwith_echo_area_save_vector. */
8720 vector = Vmode_line_unwind_vector;
8721 Vmode_line_unwind_vector = Qnil;
8723 if (NILP (vector))
8724 vector = Fmake_vector (make_number (7), Qnil);
8726 AREF (vector, 0) = make_number (mode_line_target);
8727 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8728 AREF (vector, 2) = mode_line_string_list;
8729 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8730 AREF (vector, 4) = mode_line_string_face;
8731 AREF (vector, 5) = mode_line_string_face_prop;
8733 if (obuf)
8734 XSETBUFFER (AREF (vector, 6), obuf);
8735 else
8736 AREF (vector, 6) = Qnil;
8738 return vector;
8741 static Lisp_Object
8742 unwind_format_mode_line (vector)
8743 Lisp_Object vector;
8745 mode_line_target = XINT (AREF (vector, 0));
8746 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8747 mode_line_string_list = AREF (vector, 2);
8748 if (! EQ (AREF (vector, 3), Qt))
8749 mode_line_proptrans_alist = AREF (vector, 3);
8750 mode_line_string_face = AREF (vector, 4);
8751 mode_line_string_face_prop = AREF (vector, 5);
8753 if (!NILP (AREF (vector, 6)))
8755 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8756 AREF (vector, 6) = Qnil;
8759 Vmode_line_unwind_vector = vector;
8760 return Qnil;
8764 /* Store a single character C for the frame title in mode_line_noprop_buf.
8765 Re-allocate mode_line_noprop_buf if necessary. */
8767 static void
8768 #ifdef PROTOTYPES
8769 store_mode_line_noprop_char (char c)
8770 #else
8771 store_mode_line_noprop_char (c)
8772 char c;
8773 #endif
8775 /* If output position has reached the end of the allocated buffer,
8776 double the buffer's size. */
8777 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8779 int len = MODE_LINE_NOPROP_LEN (0);
8780 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8781 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8782 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8783 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8786 *mode_line_noprop_ptr++ = c;
8790 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8791 mode_line_noprop_ptr. STR is the string to store. Do not copy
8792 characters that yield more columns than PRECISION; PRECISION <= 0
8793 means copy the whole string. Pad with spaces until FIELD_WIDTH
8794 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8795 pad. Called from display_mode_element when it is used to build a
8796 frame title. */
8798 static int
8799 store_mode_line_noprop (str, field_width, precision)
8800 const unsigned char *str;
8801 int field_width, precision;
8803 int n = 0;
8804 int dummy, nbytes;
8806 /* Copy at most PRECISION chars from STR. */
8807 nbytes = strlen (str);
8808 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8809 while (nbytes--)
8810 store_mode_line_noprop_char (*str++);
8812 /* Fill up with spaces until FIELD_WIDTH reached. */
8813 while (field_width > 0
8814 && n < field_width)
8816 store_mode_line_noprop_char (' ');
8817 ++n;
8820 return n;
8823 /***********************************************************************
8824 Frame Titles
8825 ***********************************************************************/
8827 #ifdef HAVE_WINDOW_SYSTEM
8829 /* Set the title of FRAME, if it has changed. The title format is
8830 Vicon_title_format if FRAME is iconified, otherwise it is
8831 frame_title_format. */
8833 static void
8834 x_consider_frame_title (frame)
8835 Lisp_Object frame;
8837 struct frame *f = XFRAME (frame);
8839 if (FRAME_WINDOW_P (f)
8840 || FRAME_MINIBUF_ONLY_P (f)
8841 || f->explicit_name)
8843 /* Do we have more than one visible frame on this X display? */
8844 Lisp_Object tail;
8845 Lisp_Object fmt;
8846 int title_start;
8847 char *title;
8848 int len;
8849 struct it it;
8850 int count = SPECPDL_INDEX ();
8852 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8854 Lisp_Object other_frame = XCAR (tail);
8855 struct frame *tf = XFRAME (other_frame);
8857 if (tf != f
8858 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8859 && !FRAME_MINIBUF_ONLY_P (tf)
8860 && !EQ (other_frame, tip_frame)
8861 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8862 break;
8865 /* Set global variable indicating that multiple frames exist. */
8866 multiple_frames = CONSP (tail);
8868 /* Switch to the buffer of selected window of the frame. Set up
8869 mode_line_target so that display_mode_element will output into
8870 mode_line_noprop_buf; then display the title. */
8871 record_unwind_protect (unwind_format_mode_line,
8872 format_mode_line_unwind_data (current_buffer, 0));
8874 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8875 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8877 mode_line_target = MODE_LINE_TITLE;
8878 title_start = MODE_LINE_NOPROP_LEN (0);
8879 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8880 NULL, DEFAULT_FACE_ID);
8881 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8882 len = MODE_LINE_NOPROP_LEN (title_start);
8883 title = mode_line_noprop_buf + title_start;
8884 unbind_to (count, Qnil);
8886 /* Set the title only if it's changed. This avoids consing in
8887 the common case where it hasn't. (If it turns out that we've
8888 already wasted too much time by walking through the list with
8889 display_mode_element, then we might need to optimize at a
8890 higher level than this.) */
8891 if (! STRINGP (f->name)
8892 || SBYTES (f->name) != len
8893 || bcmp (title, SDATA (f->name), len) != 0)
8894 x_implicitly_set_name (f, make_string (title, len), Qnil);
8898 #endif /* not HAVE_WINDOW_SYSTEM */
8903 /***********************************************************************
8904 Menu Bars
8905 ***********************************************************************/
8908 /* Prepare for redisplay by updating menu-bar item lists when
8909 appropriate. This can call eval. */
8911 void
8912 prepare_menu_bars ()
8914 int all_windows;
8915 struct gcpro gcpro1, gcpro2;
8916 struct frame *f;
8917 Lisp_Object tooltip_frame;
8919 #ifdef HAVE_WINDOW_SYSTEM
8920 tooltip_frame = tip_frame;
8921 #else
8922 tooltip_frame = Qnil;
8923 #endif
8925 /* Update all frame titles based on their buffer names, etc. We do
8926 this before the menu bars so that the buffer-menu will show the
8927 up-to-date frame titles. */
8928 #ifdef HAVE_WINDOW_SYSTEM
8929 if (windows_or_buffers_changed || update_mode_lines)
8931 Lisp_Object tail, frame;
8933 FOR_EACH_FRAME (tail, frame)
8935 f = XFRAME (frame);
8936 if (!EQ (frame, tooltip_frame)
8937 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8938 x_consider_frame_title (frame);
8941 #endif /* HAVE_WINDOW_SYSTEM */
8943 /* Update the menu bar item lists, if appropriate. This has to be
8944 done before any actual redisplay or generation of display lines. */
8945 all_windows = (update_mode_lines
8946 || buffer_shared > 1
8947 || windows_or_buffers_changed);
8948 if (all_windows)
8950 Lisp_Object tail, frame;
8951 int count = SPECPDL_INDEX ();
8953 record_unwind_save_match_data ();
8955 FOR_EACH_FRAME (tail, frame)
8957 f = XFRAME (frame);
8959 /* Ignore tooltip frame. */
8960 if (EQ (frame, tooltip_frame))
8961 continue;
8963 /* If a window on this frame changed size, report that to
8964 the user and clear the size-change flag. */
8965 if (FRAME_WINDOW_SIZES_CHANGED (f))
8967 Lisp_Object functions;
8969 /* Clear flag first in case we get an error below. */
8970 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8971 functions = Vwindow_size_change_functions;
8972 GCPRO2 (tail, functions);
8974 while (CONSP (functions))
8976 call1 (XCAR (functions), frame);
8977 functions = XCDR (functions);
8979 UNGCPRO;
8982 GCPRO1 (tail);
8983 update_menu_bar (f, 0);
8984 #ifdef HAVE_WINDOW_SYSTEM
8985 update_tool_bar (f, 0);
8986 #ifdef MAC_OS
8987 mac_update_title_bar (f, 0);
8988 #endif
8989 #endif
8990 UNGCPRO;
8993 unbind_to (count, Qnil);
8995 else
8997 struct frame *sf = SELECTED_FRAME ();
8998 update_menu_bar (sf, 1);
8999 #ifdef HAVE_WINDOW_SYSTEM
9000 update_tool_bar (sf, 1);
9001 #ifdef MAC_OS
9002 mac_update_title_bar (sf, 1);
9003 #endif
9004 #endif
9007 /* Motif needs this. See comment in xmenu.c. Turn it off when
9008 pending_menu_activation is not defined. */
9009 #ifdef USE_X_TOOLKIT
9010 pending_menu_activation = 0;
9011 #endif
9015 /* Update the menu bar item list for frame F. This has to be done
9016 before we start to fill in any display lines, because it can call
9017 eval.
9019 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9021 static void
9022 update_menu_bar (f, save_match_data)
9023 struct frame *f;
9024 int save_match_data;
9026 Lisp_Object window;
9027 register struct window *w;
9029 /* If called recursively during a menu update, do nothing. This can
9030 happen when, for instance, an activate-menubar-hook causes a
9031 redisplay. */
9032 if (inhibit_menubar_update)
9033 return;
9035 window = FRAME_SELECTED_WINDOW (f);
9036 w = XWINDOW (window);
9038 #if 0 /* The if statement below this if statement used to include the
9039 condition !NILP (w->update_mode_line), rather than using
9040 update_mode_lines directly, and this if statement may have
9041 been added to make that condition work. Now the if
9042 statement below matches its comment, this isn't needed. */
9043 if (update_mode_lines)
9044 w->update_mode_line = Qt;
9045 #endif
9047 if (FRAME_WINDOW_P (f)
9049 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9050 || defined (USE_GTK)
9051 FRAME_EXTERNAL_MENU_BAR (f)
9052 #else
9053 FRAME_MENU_BAR_LINES (f) > 0
9054 #endif
9055 : FRAME_MENU_BAR_LINES (f) > 0)
9057 /* If the user has switched buffers or windows, we need to
9058 recompute to reflect the new bindings. But we'll
9059 recompute when update_mode_lines is set too; that means
9060 that people can use force-mode-line-update to request
9061 that the menu bar be recomputed. The adverse effect on
9062 the rest of the redisplay algorithm is about the same as
9063 windows_or_buffers_changed anyway. */
9064 if (windows_or_buffers_changed
9065 /* This used to test w->update_mode_line, but we believe
9066 there is no need to recompute the menu in that case. */
9067 || update_mode_lines
9068 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9069 < BUF_MODIFF (XBUFFER (w->buffer)))
9070 != !NILP (w->last_had_star))
9071 || ((!NILP (Vtransient_mark_mode)
9072 && !NILP (XBUFFER (w->buffer)->mark_active))
9073 != !NILP (w->region_showing)))
9075 struct buffer *prev = current_buffer;
9076 int count = SPECPDL_INDEX ();
9078 specbind (Qinhibit_menubar_update, Qt);
9080 set_buffer_internal_1 (XBUFFER (w->buffer));
9081 if (save_match_data)
9082 record_unwind_save_match_data ();
9083 if (NILP (Voverriding_local_map_menu_flag))
9085 specbind (Qoverriding_terminal_local_map, Qnil);
9086 specbind (Qoverriding_local_map, Qnil);
9089 /* Run the Lucid hook. */
9090 safe_run_hooks (Qactivate_menubar_hook);
9092 /* If it has changed current-menubar from previous value,
9093 really recompute the menu-bar from the value. */
9094 if (! NILP (Vlucid_menu_bar_dirty_flag))
9095 call0 (Qrecompute_lucid_menubar);
9097 safe_run_hooks (Qmenu_bar_update_hook);
9098 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9100 /* Redisplay the menu bar in case we changed it. */
9101 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9102 || defined (USE_GTK)
9103 if (FRAME_WINDOW_P (f))
9105 #ifdef MAC_OS
9106 /* All frames on Mac OS share the same menubar. So only
9107 the selected frame should be allowed to set it. */
9108 if (f == SELECTED_FRAME ())
9109 #endif
9110 set_frame_menubar (f, 0, 0);
9112 else
9113 /* On a terminal screen, the menu bar is an ordinary screen
9114 line, and this makes it get updated. */
9115 w->update_mode_line = Qt;
9116 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9117 /* In the non-toolkit version, the menu bar is an ordinary screen
9118 line, and this makes it get updated. */
9119 w->update_mode_line = Qt;
9120 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9122 unbind_to (count, Qnil);
9123 set_buffer_internal_1 (prev);
9130 /***********************************************************************
9131 Output Cursor
9132 ***********************************************************************/
9134 #ifdef HAVE_WINDOW_SYSTEM
9136 /* EXPORT:
9137 Nominal cursor position -- where to draw output.
9138 HPOS and VPOS are window relative glyph matrix coordinates.
9139 X and Y are window relative pixel coordinates. */
9141 struct cursor_pos output_cursor;
9144 /* EXPORT:
9145 Set the global variable output_cursor to CURSOR. All cursor
9146 positions are relative to updated_window. */
9148 void
9149 set_output_cursor (cursor)
9150 struct cursor_pos *cursor;
9152 output_cursor.hpos = cursor->hpos;
9153 output_cursor.vpos = cursor->vpos;
9154 output_cursor.x = cursor->x;
9155 output_cursor.y = cursor->y;
9159 /* EXPORT for RIF:
9160 Set a nominal cursor position.
9162 HPOS and VPOS are column/row positions in a window glyph matrix. X
9163 and Y are window text area relative pixel positions.
9165 If this is done during an update, updated_window will contain the
9166 window that is being updated and the position is the future output
9167 cursor position for that window. If updated_window is null, use
9168 selected_window and display the cursor at the given position. */
9170 void
9171 x_cursor_to (vpos, hpos, y, x)
9172 int vpos, hpos, y, x;
9174 struct window *w;
9176 /* If updated_window is not set, work on selected_window. */
9177 if (updated_window)
9178 w = updated_window;
9179 else
9180 w = XWINDOW (selected_window);
9182 /* Set the output cursor. */
9183 output_cursor.hpos = hpos;
9184 output_cursor.vpos = vpos;
9185 output_cursor.x = x;
9186 output_cursor.y = y;
9188 /* If not called as part of an update, really display the cursor.
9189 This will also set the cursor position of W. */
9190 if (updated_window == NULL)
9192 BLOCK_INPUT;
9193 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9194 if (rif->flush_display_optional)
9195 rif->flush_display_optional (SELECTED_FRAME ());
9196 UNBLOCK_INPUT;
9200 #endif /* HAVE_WINDOW_SYSTEM */
9203 /***********************************************************************
9204 Tool-bars
9205 ***********************************************************************/
9207 #ifdef HAVE_WINDOW_SYSTEM
9209 /* Where the mouse was last time we reported a mouse event. */
9211 FRAME_PTR last_mouse_frame;
9213 /* Tool-bar item index of the item on which a mouse button was pressed
9214 or -1. */
9216 int last_tool_bar_item;
9219 /* Update the tool-bar item list for frame F. This has to be done
9220 before we start to fill in any display lines. Called from
9221 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9222 and restore it here. */
9224 static void
9225 update_tool_bar (f, save_match_data)
9226 struct frame *f;
9227 int save_match_data;
9229 #ifdef USE_GTK
9230 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9231 #else
9232 int do_update = WINDOWP (f->tool_bar_window)
9233 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9234 #endif
9236 if (do_update)
9238 Lisp_Object window;
9239 struct window *w;
9241 window = FRAME_SELECTED_WINDOW (f);
9242 w = XWINDOW (window);
9244 /* If the user has switched buffers or windows, we need to
9245 recompute to reflect the new bindings. But we'll
9246 recompute when update_mode_lines is set too; that means
9247 that people can use force-mode-line-update to request
9248 that the menu bar be recomputed. The adverse effect on
9249 the rest of the redisplay algorithm is about the same as
9250 windows_or_buffers_changed anyway. */
9251 if (windows_or_buffers_changed
9252 || !NILP (w->update_mode_line)
9253 || update_mode_lines
9254 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9255 < BUF_MODIFF (XBUFFER (w->buffer)))
9256 != !NILP (w->last_had_star))
9257 || ((!NILP (Vtransient_mark_mode)
9258 && !NILP (XBUFFER (w->buffer)->mark_active))
9259 != !NILP (w->region_showing)))
9261 struct buffer *prev = current_buffer;
9262 int count = SPECPDL_INDEX ();
9263 Lisp_Object new_tool_bar;
9264 int new_n_tool_bar;
9265 struct gcpro gcpro1;
9267 /* Set current_buffer to the buffer of the selected
9268 window of the frame, so that we get the right local
9269 keymaps. */
9270 set_buffer_internal_1 (XBUFFER (w->buffer));
9272 /* Save match data, if we must. */
9273 if (save_match_data)
9274 record_unwind_save_match_data ();
9276 /* Make sure that we don't accidentally use bogus keymaps. */
9277 if (NILP (Voverriding_local_map_menu_flag))
9279 specbind (Qoverriding_terminal_local_map, Qnil);
9280 specbind (Qoverriding_local_map, Qnil);
9283 GCPRO1 (new_tool_bar);
9285 /* Build desired tool-bar items from keymaps. */
9286 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9287 &new_n_tool_bar);
9289 /* Redisplay the tool-bar if we changed it. */
9290 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9292 /* Redisplay that happens asynchronously due to an expose event
9293 may access f->tool_bar_items. Make sure we update both
9294 variables within BLOCK_INPUT so no such event interrupts. */
9295 BLOCK_INPUT;
9296 f->tool_bar_items = new_tool_bar;
9297 f->n_tool_bar_items = new_n_tool_bar;
9298 w->update_mode_line = Qt;
9299 UNBLOCK_INPUT;
9302 UNGCPRO;
9304 unbind_to (count, Qnil);
9305 set_buffer_internal_1 (prev);
9311 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9312 F's desired tool-bar contents. F->tool_bar_items must have
9313 been set up previously by calling prepare_menu_bars. */
9315 static void
9316 build_desired_tool_bar_string (f)
9317 struct frame *f;
9319 int i, size, size_needed;
9320 struct gcpro gcpro1, gcpro2, gcpro3;
9321 Lisp_Object image, plist, props;
9323 image = plist = props = Qnil;
9324 GCPRO3 (image, plist, props);
9326 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9327 Otherwise, make a new string. */
9329 /* The size of the string we might be able to reuse. */
9330 size = (STRINGP (f->desired_tool_bar_string)
9331 ? SCHARS (f->desired_tool_bar_string)
9332 : 0);
9334 /* We need one space in the string for each image. */
9335 size_needed = f->n_tool_bar_items;
9337 /* Reuse f->desired_tool_bar_string, if possible. */
9338 if (size < size_needed || NILP (f->desired_tool_bar_string))
9339 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9340 make_number (' '));
9341 else
9343 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9344 Fremove_text_properties (make_number (0), make_number (size),
9345 props, f->desired_tool_bar_string);
9348 /* Put a `display' property on the string for the images to display,
9349 put a `menu_item' property on tool-bar items with a value that
9350 is the index of the item in F's tool-bar item vector. */
9351 for (i = 0; i < f->n_tool_bar_items; ++i)
9353 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9355 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9356 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9357 int hmargin, vmargin, relief, idx, end;
9358 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9360 /* If image is a vector, choose the image according to the
9361 button state. */
9362 image = PROP (TOOL_BAR_ITEM_IMAGES);
9363 if (VECTORP (image))
9365 if (enabled_p)
9366 idx = (selected_p
9367 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9368 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9369 else
9370 idx = (selected_p
9371 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9372 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9374 xassert (ASIZE (image) >= idx);
9375 image = AREF (image, idx);
9377 else
9378 idx = -1;
9380 /* Ignore invalid image specifications. */
9381 if (!valid_image_p (image))
9382 continue;
9384 /* Display the tool-bar button pressed, or depressed. */
9385 plist = Fcopy_sequence (XCDR (image));
9387 /* Compute margin and relief to draw. */
9388 relief = (tool_bar_button_relief >= 0
9389 ? tool_bar_button_relief
9390 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9391 hmargin = vmargin = relief;
9393 if (INTEGERP (Vtool_bar_button_margin)
9394 && XINT (Vtool_bar_button_margin) > 0)
9396 hmargin += XFASTINT (Vtool_bar_button_margin);
9397 vmargin += XFASTINT (Vtool_bar_button_margin);
9399 else if (CONSP (Vtool_bar_button_margin))
9401 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9402 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9403 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9405 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9406 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9407 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9410 if (auto_raise_tool_bar_buttons_p)
9412 /* Add a `:relief' property to the image spec if the item is
9413 selected. */
9414 if (selected_p)
9416 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9417 hmargin -= relief;
9418 vmargin -= relief;
9421 else
9423 /* If image is selected, display it pressed, i.e. with a
9424 negative relief. If it's not selected, display it with a
9425 raised relief. */
9426 plist = Fplist_put (plist, QCrelief,
9427 (selected_p
9428 ? make_number (-relief)
9429 : make_number (relief)));
9430 hmargin -= relief;
9431 vmargin -= relief;
9434 /* Put a margin around the image. */
9435 if (hmargin || vmargin)
9437 if (hmargin == vmargin)
9438 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9439 else
9440 plist = Fplist_put (plist, QCmargin,
9441 Fcons (make_number (hmargin),
9442 make_number (vmargin)));
9445 /* If button is not enabled, and we don't have special images
9446 for the disabled state, make the image appear disabled by
9447 applying an appropriate algorithm to it. */
9448 if (!enabled_p && idx < 0)
9449 plist = Fplist_put (plist, QCconversion, Qdisabled);
9451 /* Put a `display' text property on the string for the image to
9452 display. Put a `menu-item' property on the string that gives
9453 the start of this item's properties in the tool-bar items
9454 vector. */
9455 image = Fcons (Qimage, plist);
9456 props = list4 (Qdisplay, image,
9457 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9459 /* Let the last image hide all remaining spaces in the tool bar
9460 string. The string can be longer than needed when we reuse a
9461 previous string. */
9462 if (i + 1 == f->n_tool_bar_items)
9463 end = SCHARS (f->desired_tool_bar_string);
9464 else
9465 end = i + 1;
9466 Fadd_text_properties (make_number (i), make_number (end),
9467 props, f->desired_tool_bar_string);
9468 #undef PROP
9471 UNGCPRO;
9475 /* Display one line of the tool-bar of frame IT->f.
9477 HEIGHT specifies the desired height of the tool-bar line.
9478 If the actual height of the glyph row is less than HEIGHT, the
9479 row's height is increased to HEIGHT, and the icons are centered
9480 vertically in the new height.
9482 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9483 count a final empty row in case the tool-bar width exactly matches
9484 the window width.
9487 static void
9488 display_tool_bar_line (it, height)
9489 struct it *it;
9490 int height;
9492 struct glyph_row *row = it->glyph_row;
9493 int max_x = it->last_visible_x;
9494 struct glyph *last;
9496 prepare_desired_row (row);
9497 row->y = it->current_y;
9499 /* Note that this isn't made use of if the face hasn't a box,
9500 so there's no need to check the face here. */
9501 it->start_of_box_run_p = 1;
9503 while (it->current_x < max_x)
9505 int x, n_glyphs_before, i, nglyphs;
9506 struct it it_before;
9508 /* Get the next display element. */
9509 if (!get_next_display_element (it))
9511 /* Don't count empty row if we are counting needed tool-bar lines. */
9512 if (height < 0 && !it->hpos)
9513 return;
9514 break;
9517 /* Produce glyphs. */
9518 n_glyphs_before = row->used[TEXT_AREA];
9519 it_before = *it;
9521 PRODUCE_GLYPHS (it);
9523 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9524 i = 0;
9525 x = it_before.current_x;
9526 while (i < nglyphs)
9528 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9530 if (x + glyph->pixel_width > max_x)
9532 /* Glyph doesn't fit on line. Backtrack. */
9533 row->used[TEXT_AREA] = n_glyphs_before;
9534 *it = it_before;
9535 goto out;
9538 ++it->hpos;
9539 x += glyph->pixel_width;
9540 ++i;
9543 /* Stop at line ends. */
9544 if (ITERATOR_AT_END_OF_LINE_P (it))
9545 break;
9547 set_iterator_to_next (it, 1);
9550 out:;
9552 row->displays_text_p = row->used[TEXT_AREA] != 0;
9553 /* Use default face for the border below the tool bar. */
9554 if (!row->displays_text_p)
9555 it->face_id = DEFAULT_FACE_ID;
9556 extend_face_to_end_of_line (it);
9557 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9558 last->right_box_line_p = 1;
9559 if (last == row->glyphs[TEXT_AREA])
9560 last->left_box_line_p = 1;
9562 /* Make line the desired height and center it vertically. */
9563 if ((height -= it->max_ascent + it->max_descent) > 0)
9565 /* Don't add more than one line height. */
9566 height %= FRAME_LINE_HEIGHT (it->f);
9567 it->max_ascent += height / 2;
9568 it->max_descent += (height + 1) / 2;
9571 compute_line_metrics (it);
9573 /* If line is empty, make it occupy the rest of the tool-bar. */
9574 if (!row->displays_text_p)
9576 row->height = row->phys_height = it->last_visible_y - row->y;
9577 row->ascent = row->phys_ascent = 0;
9578 row->extra_line_spacing = 0;
9581 row->full_width_p = 1;
9582 row->continued_p = 0;
9583 row->truncated_on_left_p = 0;
9584 row->truncated_on_right_p = 0;
9586 it->current_x = it->hpos = 0;
9587 it->current_y += row->height;
9588 ++it->vpos;
9589 ++it->glyph_row;
9593 /* Value is the number of screen lines needed to make all tool-bar
9594 items of frame F visible. The number of actual rows needed is
9595 returned in *N_ROWS if non-NULL. */
9597 static int
9598 tool_bar_lines_needed (f, n_rows)
9599 struct frame *f;
9600 int *n_rows;
9602 struct window *w = XWINDOW (f->tool_bar_window);
9603 struct it it;
9604 struct glyph_row *temp_row = w->desired_matrix->rows;
9606 /* Initialize an iterator for iteration over
9607 F->desired_tool_bar_string in the tool-bar window of frame F. */
9608 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9609 it.first_visible_x = 0;
9610 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9611 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9613 while (!ITERATOR_AT_END_P (&it))
9615 clear_glyph_row (temp_row);
9616 it.glyph_row = temp_row;
9617 display_tool_bar_line (&it, -1);
9619 clear_glyph_row (temp_row);
9621 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9622 if (n_rows)
9623 *n_rows = it.vpos > 0 ? it.vpos : -1;
9625 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9629 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9630 0, 1, 0,
9631 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9632 (frame)
9633 Lisp_Object frame;
9635 struct frame *f;
9636 struct window *w;
9637 int nlines = 0;
9639 if (NILP (frame))
9640 frame = selected_frame;
9641 else
9642 CHECK_FRAME (frame);
9643 f = XFRAME (frame);
9645 if (WINDOWP (f->tool_bar_window)
9646 || (w = XWINDOW (f->tool_bar_window),
9647 WINDOW_TOTAL_LINES (w) > 0))
9649 update_tool_bar (f, 1);
9650 if (f->n_tool_bar_items)
9652 build_desired_tool_bar_string (f);
9653 nlines = tool_bar_lines_needed (f, NULL);
9657 return make_number (nlines);
9661 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9662 height should be changed. */
9664 static int
9665 redisplay_tool_bar (f)
9666 struct frame *f;
9668 struct window *w;
9669 struct it it;
9670 struct glyph_row *row;
9671 int change_height_p = 0;
9673 #ifdef USE_GTK
9674 if (FRAME_EXTERNAL_TOOL_BAR (f))
9675 update_frame_tool_bar (f);
9676 return 0;
9677 #endif
9679 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9680 do anything. This means you must start with tool-bar-lines
9681 non-zero to get the auto-sizing effect. Or in other words, you
9682 can turn off tool-bars by specifying tool-bar-lines zero. */
9683 if (!WINDOWP (f->tool_bar_window)
9684 || (w = XWINDOW (f->tool_bar_window),
9685 WINDOW_TOTAL_LINES (w) == 0))
9686 return 0;
9688 /* Set up an iterator for the tool-bar window. */
9689 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9690 it.first_visible_x = 0;
9691 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9692 row = it.glyph_row;
9694 /* Build a string that represents the contents of the tool-bar. */
9695 build_desired_tool_bar_string (f);
9696 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9698 if (f->n_tool_bar_rows == 0)
9700 int nlines;
9702 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9703 nlines != WINDOW_TOTAL_LINES (w)))
9705 extern Lisp_Object Qtool_bar_lines;
9706 Lisp_Object frame;
9707 int old_height = WINDOW_TOTAL_LINES (w);
9709 XSETFRAME (frame, f);
9710 clear_glyph_matrix (w->desired_matrix);
9711 Fmodify_frame_parameters (frame,
9712 Fcons (Fcons (Qtool_bar_lines,
9713 make_number (nlines)),
9714 Qnil));
9715 if (WINDOW_TOTAL_LINES (w) != old_height)
9717 fonts_changed_p = 1;
9718 return 1;
9723 /* Display as many lines as needed to display all tool-bar items. */
9725 if (f->n_tool_bar_rows > 0)
9727 int border, rows, height, extra;
9729 if (INTEGERP (Vtool_bar_border))
9730 border = XINT (Vtool_bar_border);
9731 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9732 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9733 else if (EQ (Vtool_bar_border, Qborder_width))
9734 border = f->border_width;
9735 else
9736 border = 0;
9737 if (border < 0)
9738 border = 0;
9740 rows = f->n_tool_bar_rows;
9741 height = max (1, (it.last_visible_y - border) / rows);
9742 extra = it.last_visible_y - border - height * rows;
9744 while (it.current_y < it.last_visible_y)
9746 int h = 0;
9747 if (extra > 0 && rows-- > 0)
9749 h = (extra + rows - 1) / rows;
9750 extra -= h;
9752 display_tool_bar_line (&it, height + h);
9755 else
9757 while (it.current_y < it.last_visible_y)
9758 display_tool_bar_line (&it, 0);
9761 /* It doesn't make much sense to try scrolling in the tool-bar
9762 window, so don't do it. */
9763 w->desired_matrix->no_scrolling_p = 1;
9764 w->must_be_updated_p = 1;
9766 if (auto_resize_tool_bars_p)
9768 int nlines;
9770 /* If we couldn't display everything, change the tool-bar's
9771 height. */
9772 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9773 change_height_p = 1;
9775 /* If there are blank lines at the end, except for a partially
9776 visible blank line at the end that is smaller than
9777 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9778 row = it.glyph_row - 1;
9779 if (!row->displays_text_p
9780 && row->height >= FRAME_LINE_HEIGHT (f))
9781 change_height_p = 1;
9783 /* If row displays tool-bar items, but is partially visible,
9784 change the tool-bar's height. */
9785 if (row->displays_text_p
9786 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9787 change_height_p = 1;
9789 /* Resize windows as needed by changing the `tool-bar-lines'
9790 frame parameter. */
9791 if (change_height_p
9792 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9793 nlines != WINDOW_TOTAL_LINES (w)))
9795 extern Lisp_Object Qtool_bar_lines;
9796 Lisp_Object frame;
9797 int old_height = WINDOW_TOTAL_LINES (w);
9799 XSETFRAME (frame, f);
9800 clear_glyph_matrix (w->desired_matrix);
9801 Fmodify_frame_parameters (frame,
9802 Fcons (Fcons (Qtool_bar_lines,
9803 make_number (nlines)),
9804 Qnil));
9805 if (WINDOW_TOTAL_LINES (w) != old_height)
9806 fonts_changed_p = 1;
9810 return change_height_p;
9814 /* Get information about the tool-bar item which is displayed in GLYPH
9815 on frame F. Return in *PROP_IDX the index where tool-bar item
9816 properties start in F->tool_bar_items. Value is zero if
9817 GLYPH doesn't display a tool-bar item. */
9819 static int
9820 tool_bar_item_info (f, glyph, prop_idx)
9821 struct frame *f;
9822 struct glyph *glyph;
9823 int *prop_idx;
9825 Lisp_Object prop;
9826 int success_p;
9827 int charpos;
9829 /* This function can be called asynchronously, which means we must
9830 exclude any possibility that Fget_text_property signals an
9831 error. */
9832 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9833 charpos = max (0, charpos);
9835 /* Get the text property `menu-item' at pos. The value of that
9836 property is the start index of this item's properties in
9837 F->tool_bar_items. */
9838 prop = Fget_text_property (make_number (charpos),
9839 Qmenu_item, f->current_tool_bar_string);
9840 if (INTEGERP (prop))
9842 *prop_idx = XINT (prop);
9843 success_p = 1;
9845 else
9846 success_p = 0;
9848 return success_p;
9852 /* Get information about the tool-bar item at position X/Y on frame F.
9853 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9854 the current matrix of the tool-bar window of F, or NULL if not
9855 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9856 item in F->tool_bar_items. Value is
9858 -1 if X/Y is not on a tool-bar item
9859 0 if X/Y is on the same item that was highlighted before.
9860 1 otherwise. */
9862 static int
9863 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9864 struct frame *f;
9865 int x, y;
9866 struct glyph **glyph;
9867 int *hpos, *vpos, *prop_idx;
9869 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9870 struct window *w = XWINDOW (f->tool_bar_window);
9871 int area;
9873 /* Find the glyph under X/Y. */
9874 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9875 if (*glyph == NULL)
9876 return -1;
9878 /* Get the start of this tool-bar item's properties in
9879 f->tool_bar_items. */
9880 if (!tool_bar_item_info (f, *glyph, prop_idx))
9881 return -1;
9883 /* Is mouse on the highlighted item? */
9884 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9885 && *vpos >= dpyinfo->mouse_face_beg_row
9886 && *vpos <= dpyinfo->mouse_face_end_row
9887 && (*vpos > dpyinfo->mouse_face_beg_row
9888 || *hpos >= dpyinfo->mouse_face_beg_col)
9889 && (*vpos < dpyinfo->mouse_face_end_row
9890 || *hpos < dpyinfo->mouse_face_end_col
9891 || dpyinfo->mouse_face_past_end))
9892 return 0;
9894 return 1;
9898 /* EXPORT:
9899 Handle mouse button event on the tool-bar of frame F, at
9900 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9901 0 for button release. MODIFIERS is event modifiers for button
9902 release. */
9904 void
9905 handle_tool_bar_click (f, x, y, down_p, modifiers)
9906 struct frame *f;
9907 int x, y, down_p;
9908 unsigned int modifiers;
9910 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9911 struct window *w = XWINDOW (f->tool_bar_window);
9912 int hpos, vpos, prop_idx;
9913 struct glyph *glyph;
9914 Lisp_Object enabled_p;
9916 /* If not on the highlighted tool-bar item, return. */
9917 frame_to_window_pixel_xy (w, &x, &y);
9918 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9919 return;
9921 /* If item is disabled, do nothing. */
9922 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9923 if (NILP (enabled_p))
9924 return;
9926 if (down_p)
9928 /* Show item in pressed state. */
9929 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9930 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9931 last_tool_bar_item = prop_idx;
9933 else
9935 Lisp_Object key, frame;
9936 struct input_event event;
9937 EVENT_INIT (event);
9939 /* Show item in released state. */
9940 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9941 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9943 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9945 XSETFRAME (frame, f);
9946 event.kind = TOOL_BAR_EVENT;
9947 event.frame_or_window = frame;
9948 event.arg = frame;
9949 kbd_buffer_store_event (&event);
9951 event.kind = TOOL_BAR_EVENT;
9952 event.frame_or_window = frame;
9953 event.arg = key;
9954 event.modifiers = modifiers;
9955 kbd_buffer_store_event (&event);
9956 last_tool_bar_item = -1;
9961 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9962 tool-bar window-relative coordinates X/Y. Called from
9963 note_mouse_highlight. */
9965 static void
9966 note_tool_bar_highlight (f, x, y)
9967 struct frame *f;
9968 int x, y;
9970 Lisp_Object window = f->tool_bar_window;
9971 struct window *w = XWINDOW (window);
9972 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9973 int hpos, vpos;
9974 struct glyph *glyph;
9975 struct glyph_row *row;
9976 int i;
9977 Lisp_Object enabled_p;
9978 int prop_idx;
9979 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9980 int mouse_down_p, rc;
9982 /* Function note_mouse_highlight is called with negative x(y
9983 values when mouse moves outside of the frame. */
9984 if (x <= 0 || y <= 0)
9986 clear_mouse_face (dpyinfo);
9987 return;
9990 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9991 if (rc < 0)
9993 /* Not on tool-bar item. */
9994 clear_mouse_face (dpyinfo);
9995 return;
9997 else if (rc == 0)
9998 /* On same tool-bar item as before. */
9999 goto set_help_echo;
10001 clear_mouse_face (dpyinfo);
10003 /* Mouse is down, but on different tool-bar item? */
10004 mouse_down_p = (dpyinfo->grabbed
10005 && f == last_mouse_frame
10006 && FRAME_LIVE_P (f));
10007 if (mouse_down_p
10008 && last_tool_bar_item != prop_idx)
10009 return;
10011 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10012 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10014 /* If tool-bar item is not enabled, don't highlight it. */
10015 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10016 if (!NILP (enabled_p))
10018 /* Compute the x-position of the glyph. In front and past the
10019 image is a space. We include this in the highlighted area. */
10020 row = MATRIX_ROW (w->current_matrix, vpos);
10021 for (i = x = 0; i < hpos; ++i)
10022 x += row->glyphs[TEXT_AREA][i].pixel_width;
10024 /* Record this as the current active region. */
10025 dpyinfo->mouse_face_beg_col = hpos;
10026 dpyinfo->mouse_face_beg_row = vpos;
10027 dpyinfo->mouse_face_beg_x = x;
10028 dpyinfo->mouse_face_beg_y = row->y;
10029 dpyinfo->mouse_face_past_end = 0;
10031 dpyinfo->mouse_face_end_col = hpos + 1;
10032 dpyinfo->mouse_face_end_row = vpos;
10033 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10034 dpyinfo->mouse_face_end_y = row->y;
10035 dpyinfo->mouse_face_window = window;
10036 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10038 /* Display it as active. */
10039 show_mouse_face (dpyinfo, draw);
10040 dpyinfo->mouse_face_image_state = draw;
10043 set_help_echo:
10045 /* Set help_echo_string to a help string to display for this tool-bar item.
10046 XTread_socket does the rest. */
10047 help_echo_object = help_echo_window = Qnil;
10048 help_echo_pos = -1;
10049 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10050 if (NILP (help_echo_string))
10051 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10054 #endif /* HAVE_WINDOW_SYSTEM */
10058 /************************************************************************
10059 Horizontal scrolling
10060 ************************************************************************/
10062 static int hscroll_window_tree P_ ((Lisp_Object));
10063 static int hscroll_windows P_ ((Lisp_Object));
10065 /* For all leaf windows in the window tree rooted at WINDOW, set their
10066 hscroll value so that PT is (i) visible in the window, and (ii) so
10067 that it is not within a certain margin at the window's left and
10068 right border. Value is non-zero if any window's hscroll has been
10069 changed. */
10071 static int
10072 hscroll_window_tree (window)
10073 Lisp_Object window;
10075 int hscrolled_p = 0;
10076 int hscroll_relative_p = FLOATP (Vhscroll_step);
10077 int hscroll_step_abs = 0;
10078 double hscroll_step_rel = 0;
10080 if (hscroll_relative_p)
10082 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10083 if (hscroll_step_rel < 0)
10085 hscroll_relative_p = 0;
10086 hscroll_step_abs = 0;
10089 else if (INTEGERP (Vhscroll_step))
10091 hscroll_step_abs = XINT (Vhscroll_step);
10092 if (hscroll_step_abs < 0)
10093 hscroll_step_abs = 0;
10095 else
10096 hscroll_step_abs = 0;
10098 while (WINDOWP (window))
10100 struct window *w = XWINDOW (window);
10102 if (WINDOWP (w->hchild))
10103 hscrolled_p |= hscroll_window_tree (w->hchild);
10104 else if (WINDOWP (w->vchild))
10105 hscrolled_p |= hscroll_window_tree (w->vchild);
10106 else if (w->cursor.vpos >= 0)
10108 int h_margin;
10109 int text_area_width;
10110 struct glyph_row *current_cursor_row
10111 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10112 struct glyph_row *desired_cursor_row
10113 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10114 struct glyph_row *cursor_row
10115 = (desired_cursor_row->enabled_p
10116 ? desired_cursor_row
10117 : current_cursor_row);
10119 text_area_width = window_box_width (w, TEXT_AREA);
10121 /* Scroll when cursor is inside this scroll margin. */
10122 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10124 if ((XFASTINT (w->hscroll)
10125 && w->cursor.x <= h_margin)
10126 || (cursor_row->enabled_p
10127 && cursor_row->truncated_on_right_p
10128 && (w->cursor.x >= text_area_width - h_margin)))
10130 struct it it;
10131 int hscroll;
10132 struct buffer *saved_current_buffer;
10133 int pt;
10134 int wanted_x;
10136 /* Find point in a display of infinite width. */
10137 saved_current_buffer = current_buffer;
10138 current_buffer = XBUFFER (w->buffer);
10140 if (w == XWINDOW (selected_window))
10141 pt = BUF_PT (current_buffer);
10142 else
10144 pt = marker_position (w->pointm);
10145 pt = max (BEGV, pt);
10146 pt = min (ZV, pt);
10149 /* Move iterator to pt starting at cursor_row->start in
10150 a line with infinite width. */
10151 init_to_row_start (&it, w, cursor_row);
10152 it.last_visible_x = INFINITY;
10153 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10154 current_buffer = saved_current_buffer;
10156 /* Position cursor in window. */
10157 if (!hscroll_relative_p && hscroll_step_abs == 0)
10158 hscroll = max (0, (it.current_x
10159 - (ITERATOR_AT_END_OF_LINE_P (&it)
10160 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10161 : (text_area_width / 2))))
10162 / FRAME_COLUMN_WIDTH (it.f);
10163 else if (w->cursor.x >= text_area_width - h_margin)
10165 if (hscroll_relative_p)
10166 wanted_x = text_area_width * (1 - hscroll_step_rel)
10167 - h_margin;
10168 else
10169 wanted_x = text_area_width
10170 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10171 - h_margin;
10172 hscroll
10173 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10175 else
10177 if (hscroll_relative_p)
10178 wanted_x = text_area_width * hscroll_step_rel
10179 + h_margin;
10180 else
10181 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10182 + h_margin;
10183 hscroll
10184 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10186 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10188 /* Don't call Fset_window_hscroll if value hasn't
10189 changed because it will prevent redisplay
10190 optimizations. */
10191 if (XFASTINT (w->hscroll) != hscroll)
10193 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10194 w->hscroll = make_number (hscroll);
10195 hscrolled_p = 1;
10200 window = w->next;
10203 /* Value is non-zero if hscroll of any leaf window has been changed. */
10204 return hscrolled_p;
10208 /* Set hscroll so that cursor is visible and not inside horizontal
10209 scroll margins for all windows in the tree rooted at WINDOW. See
10210 also hscroll_window_tree above. Value is non-zero if any window's
10211 hscroll has been changed. If it has, desired matrices on the frame
10212 of WINDOW are cleared. */
10214 static int
10215 hscroll_windows (window)
10216 Lisp_Object window;
10218 int hscrolled_p;
10220 if (automatic_hscrolling_p)
10222 hscrolled_p = hscroll_window_tree (window);
10223 if (hscrolled_p)
10224 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10226 else
10227 hscrolled_p = 0;
10228 return hscrolled_p;
10233 /************************************************************************
10234 Redisplay
10235 ************************************************************************/
10237 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10238 to a non-zero value. This is sometimes handy to have in a debugger
10239 session. */
10241 #if GLYPH_DEBUG
10243 /* First and last unchanged row for try_window_id. */
10245 int debug_first_unchanged_at_end_vpos;
10246 int debug_last_unchanged_at_beg_vpos;
10248 /* Delta vpos and y. */
10250 int debug_dvpos, debug_dy;
10252 /* Delta in characters and bytes for try_window_id. */
10254 int debug_delta, debug_delta_bytes;
10256 /* Values of window_end_pos and window_end_vpos at the end of
10257 try_window_id. */
10259 EMACS_INT debug_end_pos, debug_end_vpos;
10261 /* Append a string to W->desired_matrix->method. FMT is a printf
10262 format string. A1...A9 are a supplement for a variable-length
10263 argument list. If trace_redisplay_p is non-zero also printf the
10264 resulting string to stderr. */
10266 static void
10267 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10268 struct window *w;
10269 char *fmt;
10270 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10272 char buffer[512];
10273 char *method = w->desired_matrix->method;
10274 int len = strlen (method);
10275 int size = sizeof w->desired_matrix->method;
10276 int remaining = size - len - 1;
10278 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10279 if (len && remaining)
10281 method[len] = '|';
10282 --remaining, ++len;
10285 strncpy (method + len, buffer, remaining);
10287 if (trace_redisplay_p)
10288 fprintf (stderr, "%p (%s): %s\n",
10290 ((BUFFERP (w->buffer)
10291 && STRINGP (XBUFFER (w->buffer)->name))
10292 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10293 : "no buffer"),
10294 buffer);
10297 #endif /* GLYPH_DEBUG */
10300 /* Value is non-zero if all changes in window W, which displays
10301 current_buffer, are in the text between START and END. START is a
10302 buffer position, END is given as a distance from Z. Used in
10303 redisplay_internal for display optimization. */
10305 static INLINE int
10306 text_outside_line_unchanged_p (w, start, end)
10307 struct window *w;
10308 int start, end;
10310 int unchanged_p = 1;
10312 /* If text or overlays have changed, see where. */
10313 if (XFASTINT (w->last_modified) < MODIFF
10314 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10316 /* Gap in the line? */
10317 if (GPT < start || Z - GPT < end)
10318 unchanged_p = 0;
10320 /* Changes start in front of the line, or end after it? */
10321 if (unchanged_p
10322 && (BEG_UNCHANGED < start - 1
10323 || END_UNCHANGED < end))
10324 unchanged_p = 0;
10326 /* If selective display, can't optimize if changes start at the
10327 beginning of the line. */
10328 if (unchanged_p
10329 && INTEGERP (current_buffer->selective_display)
10330 && XINT (current_buffer->selective_display) > 0
10331 && (BEG_UNCHANGED < start || GPT <= start))
10332 unchanged_p = 0;
10334 /* If there are overlays at the start or end of the line, these
10335 may have overlay strings with newlines in them. A change at
10336 START, for instance, may actually concern the display of such
10337 overlay strings as well, and they are displayed on different
10338 lines. So, quickly rule out this case. (For the future, it
10339 might be desirable to implement something more telling than
10340 just BEG/END_UNCHANGED.) */
10341 if (unchanged_p)
10343 if (BEG + BEG_UNCHANGED == start
10344 && overlay_touches_p (start))
10345 unchanged_p = 0;
10346 if (END_UNCHANGED == end
10347 && overlay_touches_p (Z - end))
10348 unchanged_p = 0;
10352 return unchanged_p;
10356 /* Do a frame update, taking possible shortcuts into account. This is
10357 the main external entry point for redisplay.
10359 If the last redisplay displayed an echo area message and that message
10360 is no longer requested, we clear the echo area or bring back the
10361 mini-buffer if that is in use. */
10363 void
10364 redisplay ()
10366 redisplay_internal (0);
10370 static Lisp_Object
10371 overlay_arrow_string_or_property (var)
10372 Lisp_Object var;
10374 Lisp_Object val;
10376 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10377 return val;
10379 return Voverlay_arrow_string;
10382 /* Return 1 if there are any overlay-arrows in current_buffer. */
10383 static int
10384 overlay_arrow_in_current_buffer_p ()
10386 Lisp_Object vlist;
10388 for (vlist = Voverlay_arrow_variable_list;
10389 CONSP (vlist);
10390 vlist = XCDR (vlist))
10392 Lisp_Object var = XCAR (vlist);
10393 Lisp_Object val;
10395 if (!SYMBOLP (var))
10396 continue;
10397 val = find_symbol_value (var);
10398 if (MARKERP (val)
10399 && current_buffer == XMARKER (val)->buffer)
10400 return 1;
10402 return 0;
10406 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10407 has changed. */
10409 static int
10410 overlay_arrows_changed_p ()
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, pstr;
10421 if (!SYMBOLP (var))
10422 continue;
10423 val = find_symbol_value (var);
10424 if (!MARKERP (val))
10425 continue;
10426 if (! EQ (COERCE_MARKER (val),
10427 Fget (var, Qlast_arrow_position))
10428 || ! (pstr = overlay_arrow_string_or_property (var),
10429 EQ (pstr, Fget (var, Qlast_arrow_string))))
10430 return 1;
10432 return 0;
10435 /* Mark overlay arrows to be updated on next redisplay. */
10437 static void
10438 update_overlay_arrows (up_to_date)
10439 int up_to_date;
10441 Lisp_Object vlist;
10443 for (vlist = Voverlay_arrow_variable_list;
10444 CONSP (vlist);
10445 vlist = XCDR (vlist))
10447 Lisp_Object var = XCAR (vlist);
10449 if (!SYMBOLP (var))
10450 continue;
10452 if (up_to_date > 0)
10454 Lisp_Object val = find_symbol_value (var);
10455 Fput (var, Qlast_arrow_position,
10456 COERCE_MARKER (val));
10457 Fput (var, Qlast_arrow_string,
10458 overlay_arrow_string_or_property (var));
10460 else if (up_to_date < 0
10461 || !NILP (Fget (var, Qlast_arrow_position)))
10463 Fput (var, Qlast_arrow_position, Qt);
10464 Fput (var, Qlast_arrow_string, Qt);
10470 /* Return overlay arrow string to display at row.
10471 Return integer (bitmap number) for arrow bitmap in left fringe.
10472 Return nil if no overlay arrow. */
10474 static Lisp_Object
10475 overlay_arrow_at_row (it, row)
10476 struct it *it;
10477 struct glyph_row *row;
10479 Lisp_Object vlist;
10481 for (vlist = Voverlay_arrow_variable_list;
10482 CONSP (vlist);
10483 vlist = XCDR (vlist))
10485 Lisp_Object var = XCAR (vlist);
10486 Lisp_Object val;
10488 if (!SYMBOLP (var))
10489 continue;
10491 val = find_symbol_value (var);
10493 if (MARKERP (val)
10494 && current_buffer == XMARKER (val)->buffer
10495 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10497 if (FRAME_WINDOW_P (it->f)
10498 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10500 #ifdef HAVE_WINDOW_SYSTEM
10501 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10503 int fringe_bitmap;
10504 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10505 return make_number (fringe_bitmap);
10507 #endif
10508 return make_number (-1); /* Use default arrow bitmap */
10510 return overlay_arrow_string_or_property (var);
10514 return Qnil;
10517 /* Return 1 if point moved out of or into a composition. Otherwise
10518 return 0. PREV_BUF and PREV_PT are the last point buffer and
10519 position. BUF and PT are the current point buffer and position. */
10522 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10523 struct buffer *prev_buf, *buf;
10524 int prev_pt, pt;
10526 int start, end;
10527 Lisp_Object prop;
10528 Lisp_Object buffer;
10530 XSETBUFFER (buffer, buf);
10531 /* Check a composition at the last point if point moved within the
10532 same buffer. */
10533 if (prev_buf == buf)
10535 if (prev_pt == pt)
10536 /* Point didn't move. */
10537 return 0;
10539 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10540 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10541 && COMPOSITION_VALID_P (start, end, prop)
10542 && start < prev_pt && end > prev_pt)
10543 /* The last point was within the composition. Return 1 iff
10544 point moved out of the composition. */
10545 return (pt <= start || pt >= end);
10548 /* Check a composition at the current point. */
10549 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10550 && find_composition (pt, -1, &start, &end, &prop, buffer)
10551 && COMPOSITION_VALID_P (start, end, prop)
10552 && start < pt && end > pt);
10556 /* Reconsider the setting of B->clip_changed which is displayed
10557 in window W. */
10559 static INLINE void
10560 reconsider_clip_changes (w, b)
10561 struct window *w;
10562 struct buffer *b;
10564 if (b->clip_changed
10565 && !NILP (w->window_end_valid)
10566 && w->current_matrix->buffer == b
10567 && w->current_matrix->zv == BUF_ZV (b)
10568 && w->current_matrix->begv == BUF_BEGV (b))
10569 b->clip_changed = 0;
10571 /* If display wasn't paused, and W is not a tool bar window, see if
10572 point has been moved into or out of a composition. In that case,
10573 we set b->clip_changed to 1 to force updating the screen. If
10574 b->clip_changed has already been set to 1, we can skip this
10575 check. */
10576 if (!b->clip_changed
10577 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10579 int pt;
10581 if (w == XWINDOW (selected_window))
10582 pt = BUF_PT (current_buffer);
10583 else
10584 pt = marker_position (w->pointm);
10586 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10587 || pt != XINT (w->last_point))
10588 && check_point_in_composition (w->current_matrix->buffer,
10589 XINT (w->last_point),
10590 XBUFFER (w->buffer), pt))
10591 b->clip_changed = 1;
10596 /* Select FRAME to forward the values of frame-local variables into C
10597 variables so that the redisplay routines can access those values
10598 directly. */
10600 static void
10601 select_frame_for_redisplay (frame)
10602 Lisp_Object frame;
10604 Lisp_Object tail, sym, val;
10605 Lisp_Object old = selected_frame;
10607 selected_frame = frame;
10609 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10610 if (CONSP (XCAR (tail))
10611 && (sym = XCAR (XCAR (tail)),
10612 SYMBOLP (sym))
10613 && (sym = indirect_variable (sym),
10614 val = SYMBOL_VALUE (sym),
10615 (BUFFER_LOCAL_VALUEP (val)
10616 || SOME_BUFFER_LOCAL_VALUEP (val)))
10617 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10618 /* Use find_symbol_value rather than Fsymbol_value
10619 to avoid an error if it is void. */
10620 find_symbol_value (sym);
10622 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10623 if (CONSP (XCAR (tail))
10624 && (sym = XCAR (XCAR (tail)),
10625 SYMBOLP (sym))
10626 && (sym = indirect_variable (sym),
10627 val = SYMBOL_VALUE (sym),
10628 (BUFFER_LOCAL_VALUEP (val)
10629 || SOME_BUFFER_LOCAL_VALUEP (val)))
10630 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10631 find_symbol_value (sym);
10635 #define STOP_POLLING \
10636 do { if (! polling_stopped_here) stop_polling (); \
10637 polling_stopped_here = 1; } while (0)
10639 #define RESUME_POLLING \
10640 do { if (polling_stopped_here) start_polling (); \
10641 polling_stopped_here = 0; } while (0)
10644 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10645 response to any user action; therefore, we should preserve the echo
10646 area. (Actually, our caller does that job.) Perhaps in the future
10647 avoid recentering windows if it is not necessary; currently that
10648 causes some problems. */
10650 static void
10651 redisplay_internal (preserve_echo_area)
10652 int preserve_echo_area;
10654 struct window *w = XWINDOW (selected_window);
10655 struct frame *f = XFRAME (w->frame);
10656 int pause;
10657 int must_finish = 0;
10658 struct text_pos tlbufpos, tlendpos;
10659 int number_of_visible_frames;
10660 int count;
10661 struct frame *sf = SELECTED_FRAME ();
10662 int polling_stopped_here = 0;
10664 /* Non-zero means redisplay has to consider all windows on all
10665 frames. Zero means, only selected_window is considered. */
10666 int consider_all_windows_p;
10668 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10670 /* No redisplay if running in batch mode or frame is not yet fully
10671 initialized, or redisplay is explicitly turned off by setting
10672 Vinhibit_redisplay. */
10673 if (noninteractive
10674 || !NILP (Vinhibit_redisplay)
10675 || !f->glyphs_initialized_p)
10676 return;
10678 /* The flag redisplay_performed_directly_p is set by
10679 direct_output_for_insert when it already did the whole screen
10680 update necessary. */
10681 if (redisplay_performed_directly_p)
10683 redisplay_performed_directly_p = 0;
10684 if (!hscroll_windows (selected_window))
10685 return;
10688 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10689 if (popup_activated ())
10690 return;
10691 #endif
10693 /* I don't think this happens but let's be paranoid. */
10694 if (redisplaying_p)
10695 return;
10697 /* Record a function that resets redisplaying_p to its old value
10698 when we leave this function. */
10699 count = SPECPDL_INDEX ();
10700 record_unwind_protect (unwind_redisplay,
10701 Fcons (make_number (redisplaying_p), selected_frame));
10702 ++redisplaying_p;
10703 specbind (Qinhibit_free_realized_faces, Qnil);
10706 Lisp_Object tail, frame;
10708 FOR_EACH_FRAME (tail, frame)
10710 struct frame *f = XFRAME (frame);
10711 f->already_hscrolled_p = 0;
10715 retry:
10716 pause = 0;
10717 reconsider_clip_changes (w, current_buffer);
10718 last_escape_glyph_frame = NULL;
10719 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10721 /* If new fonts have been loaded that make a glyph matrix adjustment
10722 necessary, do it. */
10723 if (fonts_changed_p)
10725 adjust_glyphs (NULL);
10726 ++windows_or_buffers_changed;
10727 fonts_changed_p = 0;
10730 /* If face_change_count is non-zero, init_iterator will free all
10731 realized faces, which includes the faces referenced from current
10732 matrices. So, we can't reuse current matrices in this case. */
10733 if (face_change_count)
10734 ++windows_or_buffers_changed;
10736 if (! FRAME_WINDOW_P (sf)
10737 && previous_terminal_frame != sf)
10739 /* Since frames on an ASCII terminal share the same display
10740 area, displaying a different frame means redisplay the whole
10741 thing. */
10742 windows_or_buffers_changed++;
10743 SET_FRAME_GARBAGED (sf);
10744 XSETFRAME (Vterminal_frame, sf);
10746 previous_terminal_frame = sf;
10748 /* Set the visible flags for all frames. Do this before checking
10749 for resized or garbaged frames; they want to know if their frames
10750 are visible. See the comment in frame.h for
10751 FRAME_SAMPLE_VISIBILITY. */
10753 Lisp_Object tail, frame;
10755 number_of_visible_frames = 0;
10757 FOR_EACH_FRAME (tail, frame)
10759 struct frame *f = XFRAME (frame);
10761 FRAME_SAMPLE_VISIBILITY (f);
10762 if (FRAME_VISIBLE_P (f))
10763 ++number_of_visible_frames;
10764 clear_desired_matrices (f);
10768 /* Notice any pending interrupt request to change frame size. */
10769 do_pending_window_change (1);
10771 /* Clear frames marked as garbaged. */
10772 if (frame_garbaged)
10773 clear_garbaged_frames ();
10775 /* Build menubar and tool-bar items. */
10776 if (NILP (Vmemory_full))
10777 prepare_menu_bars ();
10779 if (windows_or_buffers_changed)
10780 update_mode_lines++;
10782 /* Detect case that we need to write or remove a star in the mode line. */
10783 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10785 w->update_mode_line = Qt;
10786 if (buffer_shared > 1)
10787 update_mode_lines++;
10790 /* If %c is in the mode line, update it if needed. */
10791 if (!NILP (w->column_number_displayed)
10792 /* This alternative quickly identifies a common case
10793 where no change is needed. */
10794 && !(PT == XFASTINT (w->last_point)
10795 && XFASTINT (w->last_modified) >= MODIFF
10796 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10797 && (XFASTINT (w->column_number_displayed)
10798 != (int) current_column ())) /* iftc */
10799 w->update_mode_line = Qt;
10801 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10803 /* The variable buffer_shared is set in redisplay_window and
10804 indicates that we redisplay a buffer in different windows. See
10805 there. */
10806 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10807 || cursor_type_changed);
10809 /* If specs for an arrow have changed, do thorough redisplay
10810 to ensure we remove any arrow that should no longer exist. */
10811 if (overlay_arrows_changed_p ())
10812 consider_all_windows_p = windows_or_buffers_changed = 1;
10814 /* Normally the message* functions will have already displayed and
10815 updated the echo area, but the frame may have been trashed, or
10816 the update may have been preempted, so display the echo area
10817 again here. Checking message_cleared_p captures the case that
10818 the echo area should be cleared. */
10819 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10820 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10821 || (message_cleared_p
10822 && minibuf_level == 0
10823 /* If the mini-window is currently selected, this means the
10824 echo-area doesn't show through. */
10825 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10827 int window_height_changed_p = echo_area_display (0);
10828 must_finish = 1;
10830 /* If we don't display the current message, don't clear the
10831 message_cleared_p flag, because, if we did, we wouldn't clear
10832 the echo area in the next redisplay which doesn't preserve
10833 the echo area. */
10834 if (!display_last_displayed_message_p)
10835 message_cleared_p = 0;
10837 if (fonts_changed_p)
10838 goto retry;
10839 else if (window_height_changed_p)
10841 consider_all_windows_p = 1;
10842 ++update_mode_lines;
10843 ++windows_or_buffers_changed;
10845 /* If window configuration was changed, frames may have been
10846 marked garbaged. Clear them or we will experience
10847 surprises wrt scrolling. */
10848 if (frame_garbaged)
10849 clear_garbaged_frames ();
10852 else if (EQ (selected_window, minibuf_window)
10853 && (current_buffer->clip_changed
10854 || XFASTINT (w->last_modified) < MODIFF
10855 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10856 && resize_mini_window (w, 0))
10858 /* Resized active mini-window to fit the size of what it is
10859 showing if its contents might have changed. */
10860 must_finish = 1;
10861 consider_all_windows_p = 1;
10862 ++windows_or_buffers_changed;
10863 ++update_mode_lines;
10865 /* If window configuration was changed, frames may have been
10866 marked garbaged. Clear them or we will experience
10867 surprises wrt scrolling. */
10868 if (frame_garbaged)
10869 clear_garbaged_frames ();
10873 /* If showing the region, and mark has changed, we must redisplay
10874 the whole window. The assignment to this_line_start_pos prevents
10875 the optimization directly below this if-statement. */
10876 if (((!NILP (Vtransient_mark_mode)
10877 && !NILP (XBUFFER (w->buffer)->mark_active))
10878 != !NILP (w->region_showing))
10879 || (!NILP (w->region_showing)
10880 && !EQ (w->region_showing,
10881 Fmarker_position (XBUFFER (w->buffer)->mark))))
10882 CHARPOS (this_line_start_pos) = 0;
10884 /* Optimize the case that only the line containing the cursor in the
10885 selected window has changed. Variables starting with this_ are
10886 set in display_line and record information about the line
10887 containing the cursor. */
10888 tlbufpos = this_line_start_pos;
10889 tlendpos = this_line_end_pos;
10890 if (!consider_all_windows_p
10891 && CHARPOS (tlbufpos) > 0
10892 && NILP (w->update_mode_line)
10893 && !current_buffer->clip_changed
10894 && !current_buffer->prevent_redisplay_optimizations_p
10895 && FRAME_VISIBLE_P (XFRAME (w->frame))
10896 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10897 /* Make sure recorded data applies to current buffer, etc. */
10898 && this_line_buffer == current_buffer
10899 && current_buffer == XBUFFER (w->buffer)
10900 && NILP (w->force_start)
10901 && NILP (w->optional_new_start)
10902 /* Point must be on the line that we have info recorded about. */
10903 && PT >= CHARPOS (tlbufpos)
10904 && PT <= Z - CHARPOS (tlendpos)
10905 /* All text outside that line, including its final newline,
10906 must be unchanged */
10907 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10908 CHARPOS (tlendpos)))
10910 if (CHARPOS (tlbufpos) > BEGV
10911 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10912 && (CHARPOS (tlbufpos) == ZV
10913 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10914 /* Former continuation line has disappeared by becoming empty */
10915 goto cancel;
10916 else if (XFASTINT (w->last_modified) < MODIFF
10917 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10918 || MINI_WINDOW_P (w))
10920 /* We have to handle the case of continuation around a
10921 wide-column character (See the comment in indent.c around
10922 line 885).
10924 For instance, in the following case:
10926 -------- Insert --------
10927 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10928 J_I_ ==> J_I_ `^^' are cursors.
10929 ^^ ^^
10930 -------- --------
10932 As we have to redraw the line above, we should goto cancel. */
10934 struct it it;
10935 int line_height_before = this_line_pixel_height;
10937 /* Note that start_display will handle the case that the
10938 line starting at tlbufpos is a continuation lines. */
10939 start_display (&it, w, tlbufpos);
10941 /* Implementation note: It this still necessary? */
10942 if (it.current_x != this_line_start_x)
10943 goto cancel;
10945 TRACE ((stderr, "trying display optimization 1\n"));
10946 w->cursor.vpos = -1;
10947 overlay_arrow_seen = 0;
10948 it.vpos = this_line_vpos;
10949 it.current_y = this_line_y;
10950 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10951 display_line (&it);
10953 /* If line contains point, is not continued,
10954 and ends at same distance from eob as before, we win */
10955 if (w->cursor.vpos >= 0
10956 /* Line is not continued, otherwise this_line_start_pos
10957 would have been set to 0 in display_line. */
10958 && CHARPOS (this_line_start_pos)
10959 /* Line ends as before. */
10960 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10961 /* Line has same height as before. Otherwise other lines
10962 would have to be shifted up or down. */
10963 && this_line_pixel_height == line_height_before)
10965 /* If this is not the window's last line, we must adjust
10966 the charstarts of the lines below. */
10967 if (it.current_y < it.last_visible_y)
10969 struct glyph_row *row
10970 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10971 int delta, delta_bytes;
10973 if (Z - CHARPOS (tlendpos) == ZV)
10975 /* This line ends at end of (accessible part of)
10976 buffer. There is no newline to count. */
10977 delta = (Z
10978 - CHARPOS (tlendpos)
10979 - MATRIX_ROW_START_CHARPOS (row));
10980 delta_bytes = (Z_BYTE
10981 - BYTEPOS (tlendpos)
10982 - MATRIX_ROW_START_BYTEPOS (row));
10984 else
10986 /* This line ends in a newline. Must take
10987 account of the newline and the rest of the
10988 text that follows. */
10989 delta = (Z
10990 - CHARPOS (tlendpos)
10991 - MATRIX_ROW_START_CHARPOS (row));
10992 delta_bytes = (Z_BYTE
10993 - BYTEPOS (tlendpos)
10994 - MATRIX_ROW_START_BYTEPOS (row));
10997 increment_matrix_positions (w->current_matrix,
10998 this_line_vpos + 1,
10999 w->current_matrix->nrows,
11000 delta, delta_bytes);
11003 /* If this row displays text now but previously didn't,
11004 or vice versa, w->window_end_vpos may have to be
11005 adjusted. */
11006 if ((it.glyph_row - 1)->displays_text_p)
11008 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11009 XSETINT (w->window_end_vpos, this_line_vpos);
11011 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11012 && this_line_vpos > 0)
11013 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11014 w->window_end_valid = Qnil;
11016 /* Update hint: No need to try to scroll in update_window. */
11017 w->desired_matrix->no_scrolling_p = 1;
11019 #if GLYPH_DEBUG
11020 *w->desired_matrix->method = 0;
11021 debug_method_add (w, "optimization 1");
11022 #endif
11023 #ifdef HAVE_WINDOW_SYSTEM
11024 update_window_fringes (w, 0);
11025 #endif
11026 goto update;
11028 else
11029 goto cancel;
11031 else if (/* Cursor position hasn't changed. */
11032 PT == XFASTINT (w->last_point)
11033 /* Make sure the cursor was last displayed
11034 in this window. Otherwise we have to reposition it. */
11035 && 0 <= w->cursor.vpos
11036 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11038 if (!must_finish)
11040 do_pending_window_change (1);
11042 /* We used to always goto end_of_redisplay here, but this
11043 isn't enough if we have a blinking cursor. */
11044 if (w->cursor_off_p == w->last_cursor_off_p)
11045 goto end_of_redisplay;
11047 goto update;
11049 /* If highlighting the region, or if the cursor is in the echo area,
11050 then we can't just move the cursor. */
11051 else if (! (!NILP (Vtransient_mark_mode)
11052 && !NILP (current_buffer->mark_active))
11053 && (EQ (selected_window, current_buffer->last_selected_window)
11054 || highlight_nonselected_windows)
11055 && NILP (w->region_showing)
11056 && NILP (Vshow_trailing_whitespace)
11057 && !cursor_in_echo_area)
11059 struct it it;
11060 struct glyph_row *row;
11062 /* Skip from tlbufpos to PT and see where it is. Note that
11063 PT may be in invisible text. If so, we will end at the
11064 next visible position. */
11065 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11066 NULL, DEFAULT_FACE_ID);
11067 it.current_x = this_line_start_x;
11068 it.current_y = this_line_y;
11069 it.vpos = this_line_vpos;
11071 /* The call to move_it_to stops in front of PT, but
11072 moves over before-strings. */
11073 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11075 if (it.vpos == this_line_vpos
11076 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11077 row->enabled_p))
11079 xassert (this_line_vpos == it.vpos);
11080 xassert (this_line_y == it.current_y);
11081 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11082 #if GLYPH_DEBUG
11083 *w->desired_matrix->method = 0;
11084 debug_method_add (w, "optimization 3");
11085 #endif
11086 goto update;
11088 else
11089 goto cancel;
11092 cancel:
11093 /* Text changed drastically or point moved off of line. */
11094 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11097 CHARPOS (this_line_start_pos) = 0;
11098 consider_all_windows_p |= buffer_shared > 1;
11099 ++clear_face_cache_count;
11100 #ifdef HAVE_WINDOW_SYSTEM
11101 ++clear_image_cache_count;
11102 #endif
11104 /* Build desired matrices, and update the display. If
11105 consider_all_windows_p is non-zero, do it for all windows on all
11106 frames. Otherwise do it for selected_window, only. */
11108 if (consider_all_windows_p)
11110 Lisp_Object tail, frame;
11112 FOR_EACH_FRAME (tail, frame)
11113 XFRAME (frame)->updated_p = 0;
11115 /* Recompute # windows showing selected buffer. This will be
11116 incremented each time such a window is displayed. */
11117 buffer_shared = 0;
11119 FOR_EACH_FRAME (tail, frame)
11121 struct frame *f = XFRAME (frame);
11123 if (FRAME_WINDOW_P (f) || f == sf)
11125 if (! EQ (frame, selected_frame))
11126 /* Select the frame, for the sake of frame-local
11127 variables. */
11128 select_frame_for_redisplay (frame);
11130 /* Mark all the scroll bars to be removed; we'll redeem
11131 the ones we want when we redisplay their windows. */
11132 if (condemn_scroll_bars_hook)
11133 condemn_scroll_bars_hook (f);
11135 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11136 redisplay_windows (FRAME_ROOT_WINDOW (f));
11138 /* Any scroll bars which redisplay_windows should have
11139 nuked should now go away. */
11140 if (judge_scroll_bars_hook)
11141 judge_scroll_bars_hook (f);
11143 /* If fonts changed, display again. */
11144 /* ??? rms: I suspect it is a mistake to jump all the way
11145 back to retry here. It should just retry this frame. */
11146 if (fonts_changed_p)
11147 goto retry;
11149 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11151 /* See if we have to hscroll. */
11152 if (!f->already_hscrolled_p)
11154 f->already_hscrolled_p = 1;
11155 if (hscroll_windows (f->root_window))
11156 goto retry;
11159 /* Prevent various kinds of signals during display
11160 update. stdio is not robust about handling
11161 signals, which can cause an apparent I/O
11162 error. */
11163 if (interrupt_input)
11164 unrequest_sigio ();
11165 STOP_POLLING;
11167 /* Update the display. */
11168 set_window_update_flags (XWINDOW (f->root_window), 1);
11169 pause |= update_frame (f, 0, 0);
11170 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11171 if (pause)
11172 break;
11173 #endif
11175 f->updated_p = 1;
11180 if (!pause)
11182 /* Do the mark_window_display_accurate after all windows have
11183 been redisplayed because this call resets flags in buffers
11184 which are needed for proper redisplay. */
11185 FOR_EACH_FRAME (tail, frame)
11187 struct frame *f = XFRAME (frame);
11188 if (f->updated_p)
11190 mark_window_display_accurate (f->root_window, 1);
11191 if (frame_up_to_date_hook)
11192 frame_up_to_date_hook (f);
11197 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11199 Lisp_Object mini_window;
11200 struct frame *mini_frame;
11202 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11203 /* Use list_of_error, not Qerror, so that
11204 we catch only errors and don't run the debugger. */
11205 internal_condition_case_1 (redisplay_window_1, selected_window,
11206 list_of_error,
11207 redisplay_window_error);
11209 /* Compare desired and current matrices, perform output. */
11211 update:
11212 /* If fonts changed, display again. */
11213 if (fonts_changed_p)
11214 goto retry;
11216 /* Prevent various kinds of signals during display update.
11217 stdio is not robust about handling signals,
11218 which can cause an apparent I/O error. */
11219 if (interrupt_input)
11220 unrequest_sigio ();
11221 STOP_POLLING;
11223 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11225 if (hscroll_windows (selected_window))
11226 goto retry;
11228 XWINDOW (selected_window)->must_be_updated_p = 1;
11229 pause = update_frame (sf, 0, 0);
11232 /* We may have called echo_area_display at the top of this
11233 function. If the echo area is on another frame, that may
11234 have put text on a frame other than the selected one, so the
11235 above call to update_frame would not have caught it. Catch
11236 it here. */
11237 mini_window = FRAME_MINIBUF_WINDOW (sf);
11238 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11240 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11242 XWINDOW (mini_window)->must_be_updated_p = 1;
11243 pause |= update_frame (mini_frame, 0, 0);
11244 if (!pause && hscroll_windows (mini_window))
11245 goto retry;
11249 /* If display was paused because of pending input, make sure we do a
11250 thorough update the next time. */
11251 if (pause)
11253 /* Prevent the optimization at the beginning of
11254 redisplay_internal that tries a single-line update of the
11255 line containing the cursor in the selected window. */
11256 CHARPOS (this_line_start_pos) = 0;
11258 /* Let the overlay arrow be updated the next time. */
11259 update_overlay_arrows (0);
11261 /* If we pause after scrolling, some rows in the current
11262 matrices of some windows are not valid. */
11263 if (!WINDOW_FULL_WIDTH_P (w)
11264 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11265 update_mode_lines = 1;
11267 else
11269 if (!consider_all_windows_p)
11271 /* This has already been done above if
11272 consider_all_windows_p is set. */
11273 mark_window_display_accurate_1 (w, 1);
11275 /* Say overlay arrows are up to date. */
11276 update_overlay_arrows (1);
11278 if (frame_up_to_date_hook != 0)
11279 frame_up_to_date_hook (sf);
11282 update_mode_lines = 0;
11283 windows_or_buffers_changed = 0;
11284 cursor_type_changed = 0;
11287 /* Start SIGIO interrupts coming again. Having them off during the
11288 code above makes it less likely one will discard output, but not
11289 impossible, since there might be stuff in the system buffer here.
11290 But it is much hairier to try to do anything about that. */
11291 if (interrupt_input)
11292 request_sigio ();
11293 RESUME_POLLING;
11295 /* If a frame has become visible which was not before, redisplay
11296 again, so that we display it. Expose events for such a frame
11297 (which it gets when becoming visible) don't call the parts of
11298 redisplay constructing glyphs, so simply exposing a frame won't
11299 display anything in this case. So, we have to display these
11300 frames here explicitly. */
11301 if (!pause)
11303 Lisp_Object tail, frame;
11304 int new_count = 0;
11306 FOR_EACH_FRAME (tail, frame)
11308 int this_is_visible = 0;
11310 if (XFRAME (frame)->visible)
11311 this_is_visible = 1;
11312 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11313 if (XFRAME (frame)->visible)
11314 this_is_visible = 1;
11316 if (this_is_visible)
11317 new_count++;
11320 if (new_count != number_of_visible_frames)
11321 windows_or_buffers_changed++;
11324 /* Change frame size now if a change is pending. */
11325 do_pending_window_change (1);
11327 /* If we just did a pending size change, or have additional
11328 visible frames, redisplay again. */
11329 if (windows_or_buffers_changed && !pause)
11330 goto retry;
11332 /* Clear the face cache eventually. */
11333 if (consider_all_windows_p)
11335 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11337 clear_face_cache (0);
11338 clear_face_cache_count = 0;
11340 #ifdef HAVE_WINDOW_SYSTEM
11341 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11343 Lisp_Object tail, frame;
11344 FOR_EACH_FRAME (tail, frame)
11346 struct frame *f = XFRAME (frame);
11347 if (FRAME_WINDOW_P (f))
11348 clear_image_cache (f, 0);
11350 clear_image_cache_count = 0;
11352 #endif /* HAVE_WINDOW_SYSTEM */
11355 end_of_redisplay:
11356 unbind_to (count, Qnil);
11357 RESUME_POLLING;
11361 /* Redisplay, but leave alone any recent echo area message unless
11362 another message has been requested in its place.
11364 This is useful in situations where you need to redisplay but no
11365 user action has occurred, making it inappropriate for the message
11366 area to be cleared. See tracking_off and
11367 wait_reading_process_output for examples of these situations.
11369 FROM_WHERE is an integer saying from where this function was
11370 called. This is useful for debugging. */
11372 void
11373 redisplay_preserve_echo_area (from_where)
11374 int from_where;
11376 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11378 if (!NILP (echo_area_buffer[1]))
11380 /* We have a previously displayed message, but no current
11381 message. Redisplay the previous message. */
11382 display_last_displayed_message_p = 1;
11383 redisplay_internal (1);
11384 display_last_displayed_message_p = 0;
11386 else
11387 redisplay_internal (1);
11389 if (rif != NULL && rif->flush_display_optional)
11390 rif->flush_display_optional (NULL);
11394 /* Function registered with record_unwind_protect in
11395 redisplay_internal. Reset redisplaying_p to the value it had
11396 before redisplay_internal was called, and clear
11397 prevent_freeing_realized_faces_p. It also selects the previously
11398 selected frame. */
11400 static Lisp_Object
11401 unwind_redisplay (val)
11402 Lisp_Object val;
11404 Lisp_Object old_redisplaying_p, old_frame;
11406 old_redisplaying_p = XCAR (val);
11407 redisplaying_p = XFASTINT (old_redisplaying_p);
11408 old_frame = XCDR (val);
11409 if (! EQ (old_frame, selected_frame))
11410 select_frame_for_redisplay (old_frame);
11411 return Qnil;
11415 /* Mark the display of window W as accurate or inaccurate. If
11416 ACCURATE_P is non-zero mark display of W as accurate. If
11417 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11418 redisplay_internal is called. */
11420 static void
11421 mark_window_display_accurate_1 (w, accurate_p)
11422 struct window *w;
11423 int accurate_p;
11425 if (BUFFERP (w->buffer))
11427 struct buffer *b = XBUFFER (w->buffer);
11429 w->last_modified
11430 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11431 w->last_overlay_modified
11432 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11433 w->last_had_star
11434 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11436 if (accurate_p)
11438 b->clip_changed = 0;
11439 b->prevent_redisplay_optimizations_p = 0;
11441 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11442 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11443 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11444 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11446 w->current_matrix->buffer = b;
11447 w->current_matrix->begv = BUF_BEGV (b);
11448 w->current_matrix->zv = BUF_ZV (b);
11450 w->last_cursor = w->cursor;
11451 w->last_cursor_off_p = w->cursor_off_p;
11453 if (w == XWINDOW (selected_window))
11454 w->last_point = make_number (BUF_PT (b));
11455 else
11456 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11460 if (accurate_p)
11462 w->window_end_valid = w->buffer;
11463 #if 0 /* This is incorrect with variable-height lines. */
11464 xassert (XINT (w->window_end_vpos)
11465 < (WINDOW_TOTAL_LINES (w)
11466 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11467 #endif
11468 w->update_mode_line = Qnil;
11473 /* Mark the display of windows in the window tree rooted at WINDOW as
11474 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11475 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11476 be redisplayed the next time redisplay_internal is called. */
11478 void
11479 mark_window_display_accurate (window, accurate_p)
11480 Lisp_Object window;
11481 int accurate_p;
11483 struct window *w;
11485 for (; !NILP (window); window = w->next)
11487 w = XWINDOW (window);
11488 mark_window_display_accurate_1 (w, accurate_p);
11490 if (!NILP (w->vchild))
11491 mark_window_display_accurate (w->vchild, accurate_p);
11492 if (!NILP (w->hchild))
11493 mark_window_display_accurate (w->hchild, accurate_p);
11496 if (accurate_p)
11498 update_overlay_arrows (1);
11500 else
11502 /* Force a thorough redisplay the next time by setting
11503 last_arrow_position and last_arrow_string to t, which is
11504 unequal to any useful value of Voverlay_arrow_... */
11505 update_overlay_arrows (-1);
11510 /* Return value in display table DP (Lisp_Char_Table *) for character
11511 C. Since a display table doesn't have any parent, we don't have to
11512 follow parent. Do not call this function directly but use the
11513 macro DISP_CHAR_VECTOR. */
11515 Lisp_Object
11516 disp_char_vector (dp, c)
11517 struct Lisp_Char_Table *dp;
11518 int c;
11520 int code[4], i;
11521 Lisp_Object val;
11523 if (SINGLE_BYTE_CHAR_P (c))
11524 return (dp->contents[c]);
11526 SPLIT_CHAR (c, code[0], code[1], code[2]);
11527 if (code[1] < 32)
11528 code[1] = -1;
11529 else if (code[2] < 32)
11530 code[2] = -1;
11532 /* Here, the possible range of code[0] (== charset ID) is
11533 128..max_charset. Since the top level char table contains data
11534 for multibyte characters after 256th element, we must increment
11535 code[0] by 128 to get a correct index. */
11536 code[0] += 128;
11537 code[3] = -1; /* anchor */
11539 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11541 val = dp->contents[code[i]];
11542 if (!SUB_CHAR_TABLE_P (val))
11543 return (NILP (val) ? dp->defalt : val);
11546 /* Here, val is a sub char table. We return the default value of
11547 it. */
11548 return (dp->defalt);
11553 /***********************************************************************
11554 Window Redisplay
11555 ***********************************************************************/
11557 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11559 static void
11560 redisplay_windows (window)
11561 Lisp_Object window;
11563 while (!NILP (window))
11565 struct window *w = XWINDOW (window);
11567 if (!NILP (w->hchild))
11568 redisplay_windows (w->hchild);
11569 else if (!NILP (w->vchild))
11570 redisplay_windows (w->vchild);
11571 else
11573 displayed_buffer = XBUFFER (w->buffer);
11574 /* Use list_of_error, not Qerror, so that
11575 we catch only errors and don't run the debugger. */
11576 internal_condition_case_1 (redisplay_window_0, window,
11577 list_of_error,
11578 redisplay_window_error);
11581 window = w->next;
11585 static Lisp_Object
11586 redisplay_window_error ()
11588 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11589 return Qnil;
11592 static Lisp_Object
11593 redisplay_window_0 (window)
11594 Lisp_Object window;
11596 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11597 redisplay_window (window, 0);
11598 return Qnil;
11601 static Lisp_Object
11602 redisplay_window_1 (window)
11603 Lisp_Object window;
11605 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11606 redisplay_window (window, 1);
11607 return Qnil;
11611 /* Increment GLYPH until it reaches END or CONDITION fails while
11612 adding (GLYPH)->pixel_width to X. */
11614 #define SKIP_GLYPHS(glyph, end, x, condition) \
11615 do \
11617 (x) += (glyph)->pixel_width; \
11618 ++(glyph); \
11620 while ((glyph) < (end) && (condition))
11623 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11624 DELTA is the number of bytes by which positions recorded in ROW
11625 differ from current buffer positions.
11627 Return 0 if cursor is not on this row. 1 otherwise. */
11630 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11631 struct window *w;
11632 struct glyph_row *row;
11633 struct glyph_matrix *matrix;
11634 int delta, delta_bytes, dy, dvpos;
11636 struct glyph *glyph = row->glyphs[TEXT_AREA];
11637 struct glyph *end = glyph + row->used[TEXT_AREA];
11638 struct glyph *cursor = NULL;
11639 /* The first glyph that starts a sequence of glyphs from string. */
11640 struct glyph *string_start;
11641 /* The X coordinate of string_start. */
11642 int string_start_x;
11643 /* The last known character position. */
11644 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11645 /* The last known character position before string_start. */
11646 int string_before_pos;
11647 int x = row->x;
11648 int cursor_x = x;
11649 int cursor_from_overlay_pos = 0;
11650 int pt_old = PT - delta;
11652 /* Skip over glyphs not having an object at the start of the row.
11653 These are special glyphs like truncation marks on terminal
11654 frames. */
11655 if (row->displays_text_p)
11656 while (glyph < end
11657 && INTEGERP (glyph->object)
11658 && glyph->charpos < 0)
11660 x += glyph->pixel_width;
11661 ++glyph;
11664 string_start = NULL;
11665 while (glyph < end
11666 && !INTEGERP (glyph->object)
11667 && (!BUFFERP (glyph->object)
11668 || (last_pos = glyph->charpos) < pt_old))
11670 if (! STRINGP (glyph->object))
11672 string_start = NULL;
11673 x += glyph->pixel_width;
11674 ++glyph;
11675 if (cursor_from_overlay_pos
11676 && last_pos >= cursor_from_overlay_pos)
11678 cursor_from_overlay_pos = 0;
11679 cursor = 0;
11682 else
11684 string_before_pos = last_pos;
11685 string_start = glyph;
11686 string_start_x = x;
11687 /* Skip all glyphs from string. */
11690 Lisp_Object cprop;
11691 int pos;
11692 if ((cursor == NULL || glyph > cursor)
11693 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11694 Qcursor, (glyph)->object),
11695 !NILP (cprop))
11696 && (pos = string_buffer_position (w, glyph->object,
11697 string_before_pos),
11698 (pos == 0 /* From overlay */
11699 || pos == pt_old)))
11701 /* Estimate overlay buffer position from the buffer
11702 positions of the glyphs before and after the overlay.
11703 Add 1 to last_pos so that if point corresponds to the
11704 glyph right after the overlay, we still use a 'cursor'
11705 property found in that overlay. */
11706 cursor_from_overlay_pos = (pos ? 0 : last_pos
11707 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11708 cursor = glyph;
11709 cursor_x = x;
11711 x += glyph->pixel_width;
11712 ++glyph;
11714 while (glyph < end && EQ (glyph->object, string_start->object));
11718 if (cursor != NULL)
11720 glyph = cursor;
11721 x = cursor_x;
11723 else if (row->ends_in_ellipsis_p && glyph == end)
11725 /* Scan back over the ellipsis glyphs, decrementing positions. */
11726 while (glyph > row->glyphs[TEXT_AREA]
11727 && (glyph - 1)->charpos == last_pos)
11728 glyph--, x -= glyph->pixel_width;
11729 /* That loop always goes one position too far,
11730 including the glyph before the ellipsis.
11731 So scan forward over that one. */
11732 x += glyph->pixel_width;
11733 glyph++;
11735 else if (string_start
11736 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11738 /* We may have skipped over point because the previous glyphs
11739 are from string. As there's no easy way to know the
11740 character position of the current glyph, find the correct
11741 glyph on point by scanning from string_start again. */
11742 Lisp_Object limit;
11743 Lisp_Object string;
11744 int pos;
11746 limit = make_number (pt_old + 1);
11747 end = glyph;
11748 glyph = string_start;
11749 x = string_start_x;
11750 string = glyph->object;
11751 pos = string_buffer_position (w, string, string_before_pos);
11752 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11753 because we always put cursor after overlay strings. */
11754 while (pos == 0 && glyph < end)
11756 string = glyph->object;
11757 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11758 if (glyph < end)
11759 pos = string_buffer_position (w, glyph->object, string_before_pos);
11762 while (glyph < end)
11764 pos = XINT (Fnext_single_char_property_change
11765 (make_number (pos), Qdisplay, Qnil, limit));
11766 if (pos > pt_old)
11767 break;
11768 /* Skip glyphs from the same string. */
11769 string = glyph->object;
11770 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11771 /* Skip glyphs from an overlay. */
11772 while (glyph < end
11773 && ! string_buffer_position (w, glyph->object, pos))
11775 string = glyph->object;
11776 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11780 /* If we reached the end of the line, and end was from a string,
11781 cursor is not on this line. */
11782 if (glyph == end && row->continued_p)
11783 return 0;
11786 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11787 w->cursor.x = x;
11788 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11789 w->cursor.y = row->y + dy;
11791 if (w == XWINDOW (selected_window))
11793 if (!row->continued_p
11794 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11795 && row->x == 0)
11797 this_line_buffer = XBUFFER (w->buffer);
11799 CHARPOS (this_line_start_pos)
11800 = MATRIX_ROW_START_CHARPOS (row) + delta;
11801 BYTEPOS (this_line_start_pos)
11802 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11804 CHARPOS (this_line_end_pos)
11805 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11806 BYTEPOS (this_line_end_pos)
11807 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11809 this_line_y = w->cursor.y;
11810 this_line_pixel_height = row->height;
11811 this_line_vpos = w->cursor.vpos;
11812 this_line_start_x = row->x;
11814 else
11815 CHARPOS (this_line_start_pos) = 0;
11818 return 1;
11822 /* Run window scroll functions, if any, for WINDOW with new window
11823 start STARTP. Sets the window start of WINDOW to that position.
11825 We assume that the window's buffer is really current. */
11827 static INLINE struct text_pos
11828 run_window_scroll_functions (window, startp)
11829 Lisp_Object window;
11830 struct text_pos startp;
11832 struct window *w = XWINDOW (window);
11833 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11835 if (current_buffer != XBUFFER (w->buffer))
11836 abort ();
11838 if (!NILP (Vwindow_scroll_functions))
11840 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11841 make_number (CHARPOS (startp)));
11842 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11843 /* In case the hook functions switch buffers. */
11844 if (current_buffer != XBUFFER (w->buffer))
11845 set_buffer_internal_1 (XBUFFER (w->buffer));
11848 return startp;
11852 /* Make sure the line containing the cursor is fully visible.
11853 A value of 1 means there is nothing to be done.
11854 (Either the line is fully visible, or it cannot be made so,
11855 or we cannot tell.)
11857 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11858 is higher than window.
11860 A value of 0 means the caller should do scrolling
11861 as if point had gone off the screen. */
11863 static int
11864 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11865 struct window *w;
11866 int force_p;
11867 int current_matrix_p;
11869 struct glyph_matrix *matrix;
11870 struct glyph_row *row;
11871 int window_height;
11873 if (!make_cursor_line_fully_visible_p)
11874 return 1;
11876 /* It's not always possible to find the cursor, e.g, when a window
11877 is full of overlay strings. Don't do anything in that case. */
11878 if (w->cursor.vpos < 0)
11879 return 1;
11881 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11882 row = MATRIX_ROW (matrix, w->cursor.vpos);
11884 /* If the cursor row is not partially visible, there's nothing to do. */
11885 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11886 return 1;
11888 /* If the row the cursor is in is taller than the window's height,
11889 it's not clear what to do, so do nothing. */
11890 window_height = window_box_height (w);
11891 if (row->height >= window_height)
11893 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11894 return 1;
11896 return 0;
11898 #if 0
11899 /* This code used to try to scroll the window just enough to make
11900 the line visible. It returned 0 to say that the caller should
11901 allocate larger glyph matrices. */
11903 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11905 int dy = row->height - row->visible_height;
11906 w->vscroll = 0;
11907 w->cursor.y += dy;
11908 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11910 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11912 int dy = - (row->height - row->visible_height);
11913 w->vscroll = dy;
11914 w->cursor.y += dy;
11915 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11918 /* When we change the cursor y-position of the selected window,
11919 change this_line_y as well so that the display optimization for
11920 the cursor line of the selected window in redisplay_internal uses
11921 the correct y-position. */
11922 if (w == XWINDOW (selected_window))
11923 this_line_y = w->cursor.y;
11925 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11926 redisplay with larger matrices. */
11927 if (matrix->nrows < required_matrix_height (w))
11929 fonts_changed_p = 1;
11930 return 0;
11933 return 1;
11934 #endif /* 0 */
11938 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11939 non-zero means only WINDOW is redisplayed in redisplay_internal.
11940 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11941 in redisplay_window to bring a partially visible line into view in
11942 the case that only the cursor has moved.
11944 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11945 last screen line's vertical height extends past the end of the screen.
11947 Value is
11949 1 if scrolling succeeded
11951 0 if scrolling didn't find point.
11953 -1 if new fonts have been loaded so that we must interrupt
11954 redisplay, adjust glyph matrices, and try again. */
11956 enum
11958 SCROLLING_SUCCESS,
11959 SCROLLING_FAILED,
11960 SCROLLING_NEED_LARGER_MATRICES
11963 static int
11964 try_scrolling (window, just_this_one_p, scroll_conservatively,
11965 scroll_step, temp_scroll_step, last_line_misfit)
11966 Lisp_Object window;
11967 int just_this_one_p;
11968 EMACS_INT scroll_conservatively, scroll_step;
11969 int temp_scroll_step;
11970 int last_line_misfit;
11972 struct window *w = XWINDOW (window);
11973 struct frame *f = XFRAME (w->frame);
11974 struct text_pos scroll_margin_pos;
11975 struct text_pos pos;
11976 struct text_pos startp;
11977 struct it it;
11978 Lisp_Object window_end;
11979 int this_scroll_margin;
11980 int dy = 0;
11981 int scroll_max;
11982 int rc;
11983 int amount_to_scroll = 0;
11984 Lisp_Object aggressive;
11985 int height;
11986 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11988 #if GLYPH_DEBUG
11989 debug_method_add (w, "try_scrolling");
11990 #endif
11992 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11994 /* Compute scroll margin height in pixels. We scroll when point is
11995 within this distance from the top or bottom of the window. */
11996 if (scroll_margin > 0)
11998 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11999 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12001 else
12002 this_scroll_margin = 0;
12004 /* Force scroll_conservatively to have a reasonable value so it doesn't
12005 cause an overflow while computing how much to scroll. */
12006 if (scroll_conservatively)
12007 scroll_conservatively = min (scroll_conservatively,
12008 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12010 /* Compute how much we should try to scroll maximally to bring point
12011 into view. */
12012 if (scroll_step || scroll_conservatively || temp_scroll_step)
12013 scroll_max = max (scroll_step,
12014 max (scroll_conservatively, temp_scroll_step));
12015 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12016 || NUMBERP (current_buffer->scroll_up_aggressively))
12017 /* We're trying to scroll because of aggressive scrolling
12018 but no scroll_step is set. Choose an arbitrary one. Maybe
12019 there should be a variable for this. */
12020 scroll_max = 10;
12021 else
12022 scroll_max = 0;
12023 scroll_max *= FRAME_LINE_HEIGHT (f);
12025 /* Decide whether we have to scroll down. Start at the window end
12026 and move this_scroll_margin up to find the position of the scroll
12027 margin. */
12028 window_end = Fwindow_end (window, Qt);
12030 too_near_end:
12032 CHARPOS (scroll_margin_pos) = XINT (window_end);
12033 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12035 if (this_scroll_margin || extra_scroll_margin_lines)
12037 start_display (&it, w, scroll_margin_pos);
12038 if (this_scroll_margin)
12039 move_it_vertically_backward (&it, this_scroll_margin);
12040 if (extra_scroll_margin_lines)
12041 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12042 scroll_margin_pos = it.current.pos;
12045 if (PT >= CHARPOS (scroll_margin_pos))
12047 int y0;
12049 /* Point is in the scroll margin at the bottom of the window, or
12050 below. Compute a new window start that makes point visible. */
12052 /* Compute the distance from the scroll margin to PT.
12053 Give up if the distance is greater than scroll_max. */
12054 start_display (&it, w, scroll_margin_pos);
12055 y0 = it.current_y;
12056 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12057 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12059 /* To make point visible, we have to move the window start
12060 down so that the line the cursor is in is visible, which
12061 means we have to add in the height of the cursor line. */
12062 dy = line_bottom_y (&it) - y0;
12064 if (dy > scroll_max)
12065 return SCROLLING_FAILED;
12067 /* Move the window start down. If scrolling conservatively,
12068 move it just enough down to make point visible. If
12069 scroll_step is set, move it down by scroll_step. */
12070 start_display (&it, w, startp);
12072 if (scroll_conservatively)
12073 /* Set AMOUNT_TO_SCROLL to at least one line,
12074 and at most scroll_conservatively lines. */
12075 amount_to_scroll
12076 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12077 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12078 else if (scroll_step || temp_scroll_step)
12079 amount_to_scroll = scroll_max;
12080 else
12082 aggressive = current_buffer->scroll_up_aggressively;
12083 height = WINDOW_BOX_TEXT_HEIGHT (w);
12084 if (NUMBERP (aggressive))
12086 double float_amount = XFLOATINT (aggressive) * height;
12087 amount_to_scroll = float_amount;
12088 if (amount_to_scroll == 0 && float_amount > 0)
12089 amount_to_scroll = 1;
12093 if (amount_to_scroll <= 0)
12094 return SCROLLING_FAILED;
12096 /* If moving by amount_to_scroll leaves STARTP unchanged,
12097 move it down one screen line. */
12099 move_it_vertically (&it, amount_to_scroll);
12100 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12101 move_it_by_lines (&it, 1, 1);
12102 startp = it.current.pos;
12104 else
12106 /* See if point is inside the scroll margin at the top of the
12107 window. */
12108 scroll_margin_pos = startp;
12109 if (this_scroll_margin)
12111 start_display (&it, w, startp);
12112 move_it_vertically (&it, this_scroll_margin);
12113 scroll_margin_pos = it.current.pos;
12116 if (PT < CHARPOS (scroll_margin_pos))
12118 /* Point is in the scroll margin at the top of the window or
12119 above what is displayed in the window. */
12120 int y0;
12122 /* Compute the vertical distance from PT to the scroll
12123 margin position. Give up if distance is greater than
12124 scroll_max. */
12125 SET_TEXT_POS (pos, PT, PT_BYTE);
12126 start_display (&it, w, pos);
12127 y0 = it.current_y;
12128 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12129 it.last_visible_y, -1,
12130 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12131 dy = it.current_y - y0;
12132 if (dy > scroll_max)
12133 return SCROLLING_FAILED;
12135 /* Compute new window start. */
12136 start_display (&it, w, startp);
12138 if (scroll_conservatively)
12139 amount_to_scroll
12140 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12141 else if (scroll_step || temp_scroll_step)
12142 amount_to_scroll = scroll_max;
12143 else
12145 aggressive = current_buffer->scroll_down_aggressively;
12146 height = WINDOW_BOX_TEXT_HEIGHT (w);
12147 if (NUMBERP (aggressive))
12149 double float_amount = XFLOATINT (aggressive) * height;
12150 amount_to_scroll = float_amount;
12151 if (amount_to_scroll == 0 && float_amount > 0)
12152 amount_to_scroll = 1;
12156 if (amount_to_scroll <= 0)
12157 return SCROLLING_FAILED;
12159 move_it_vertically_backward (&it, amount_to_scroll);
12160 startp = it.current.pos;
12164 /* Run window scroll functions. */
12165 startp = run_window_scroll_functions (window, startp);
12167 /* Display the window. Give up if new fonts are loaded, or if point
12168 doesn't appear. */
12169 if (!try_window (window, startp, 0))
12170 rc = SCROLLING_NEED_LARGER_MATRICES;
12171 else if (w->cursor.vpos < 0)
12173 clear_glyph_matrix (w->desired_matrix);
12174 rc = SCROLLING_FAILED;
12176 else
12178 /* Maybe forget recorded base line for line number display. */
12179 if (!just_this_one_p
12180 || current_buffer->clip_changed
12181 || BEG_UNCHANGED < CHARPOS (startp))
12182 w->base_line_number = Qnil;
12184 /* If cursor ends up on a partially visible line,
12185 treat that as being off the bottom of the screen. */
12186 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12188 clear_glyph_matrix (w->desired_matrix);
12189 ++extra_scroll_margin_lines;
12190 goto too_near_end;
12192 rc = SCROLLING_SUCCESS;
12195 return rc;
12199 /* Compute a suitable window start for window W if display of W starts
12200 on a continuation line. Value is non-zero if a new window start
12201 was computed.
12203 The new window start will be computed, based on W's width, starting
12204 from the start of the continued line. It is the start of the
12205 screen line with the minimum distance from the old start W->start. */
12207 static int
12208 compute_window_start_on_continuation_line (w)
12209 struct window *w;
12211 struct text_pos pos, start_pos;
12212 int window_start_changed_p = 0;
12214 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12216 /* If window start is on a continuation line... Window start may be
12217 < BEGV in case there's invisible text at the start of the
12218 buffer (M-x rmail, for example). */
12219 if (CHARPOS (start_pos) > BEGV
12220 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12222 struct it it;
12223 struct glyph_row *row;
12225 /* Handle the case that the window start is out of range. */
12226 if (CHARPOS (start_pos) < BEGV)
12227 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12228 else if (CHARPOS (start_pos) > ZV)
12229 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12231 /* Find the start of the continued line. This should be fast
12232 because scan_buffer is fast (newline cache). */
12233 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12234 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12235 row, DEFAULT_FACE_ID);
12236 reseat_at_previous_visible_line_start (&it);
12238 /* If the line start is "too far" away from the window start,
12239 say it takes too much time to compute a new window start. */
12240 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12241 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12243 int min_distance, distance;
12245 /* Move forward by display lines to find the new window
12246 start. If window width was enlarged, the new start can
12247 be expected to be > the old start. If window width was
12248 decreased, the new window start will be < the old start.
12249 So, we're looking for the display line start with the
12250 minimum distance from the old window start. */
12251 pos = it.current.pos;
12252 min_distance = INFINITY;
12253 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12254 distance < min_distance)
12256 min_distance = distance;
12257 pos = it.current.pos;
12258 move_it_by_lines (&it, 1, 0);
12261 /* Set the window start there. */
12262 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12263 window_start_changed_p = 1;
12267 return window_start_changed_p;
12271 /* Try cursor movement in case text has not changed in window WINDOW,
12272 with window start STARTP. Value is
12274 CURSOR_MOVEMENT_SUCCESS if successful
12276 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12278 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12279 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12280 we want to scroll as if scroll-step were set to 1. See the code.
12282 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12283 which case we have to abort this redisplay, and adjust matrices
12284 first. */
12286 enum
12288 CURSOR_MOVEMENT_SUCCESS,
12289 CURSOR_MOVEMENT_CANNOT_BE_USED,
12290 CURSOR_MOVEMENT_MUST_SCROLL,
12291 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12294 static int
12295 try_cursor_movement (window, startp, scroll_step)
12296 Lisp_Object window;
12297 struct text_pos startp;
12298 int *scroll_step;
12300 struct window *w = XWINDOW (window);
12301 struct frame *f = XFRAME (w->frame);
12302 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12304 #if GLYPH_DEBUG
12305 if (inhibit_try_cursor_movement)
12306 return rc;
12307 #endif
12309 /* Handle case where text has not changed, only point, and it has
12310 not moved off the frame. */
12311 if (/* Point may be in this window. */
12312 PT >= CHARPOS (startp)
12313 /* Selective display hasn't changed. */
12314 && !current_buffer->clip_changed
12315 /* Function force-mode-line-update is used to force a thorough
12316 redisplay. It sets either windows_or_buffers_changed or
12317 update_mode_lines. So don't take a shortcut here for these
12318 cases. */
12319 && !update_mode_lines
12320 && !windows_or_buffers_changed
12321 && !cursor_type_changed
12322 /* Can't use this case if highlighting a region. When a
12323 region exists, cursor movement has to do more than just
12324 set the cursor. */
12325 && !(!NILP (Vtransient_mark_mode)
12326 && !NILP (current_buffer->mark_active))
12327 && NILP (w->region_showing)
12328 && NILP (Vshow_trailing_whitespace)
12329 /* Right after splitting windows, last_point may be nil. */
12330 && INTEGERP (w->last_point)
12331 /* This code is not used for mini-buffer for the sake of the case
12332 of redisplaying to replace an echo area message; since in
12333 that case the mini-buffer contents per se are usually
12334 unchanged. This code is of no real use in the mini-buffer
12335 since the handling of this_line_start_pos, etc., in redisplay
12336 handles the same cases. */
12337 && !EQ (window, minibuf_window)
12338 /* When splitting windows or for new windows, it happens that
12339 redisplay is called with a nil window_end_vpos or one being
12340 larger than the window. This should really be fixed in
12341 window.c. I don't have this on my list, now, so we do
12342 approximately the same as the old redisplay code. --gerd. */
12343 && INTEGERP (w->window_end_vpos)
12344 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12345 && (FRAME_WINDOW_P (f)
12346 || !overlay_arrow_in_current_buffer_p ()))
12348 int this_scroll_margin, top_scroll_margin;
12349 struct glyph_row *row = NULL;
12351 #if GLYPH_DEBUG
12352 debug_method_add (w, "cursor movement");
12353 #endif
12355 /* Scroll if point within this distance from the top or bottom
12356 of the window. This is a pixel value. */
12357 this_scroll_margin = max (0, scroll_margin);
12358 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12359 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12361 top_scroll_margin = this_scroll_margin;
12362 if (WINDOW_WANTS_HEADER_LINE_P (w))
12363 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12365 /* Start with the row the cursor was displayed during the last
12366 not paused redisplay. Give up if that row is not valid. */
12367 if (w->last_cursor.vpos < 0
12368 || w->last_cursor.vpos >= w->current_matrix->nrows)
12369 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12370 else
12372 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12373 if (row->mode_line_p)
12374 ++row;
12375 if (!row->enabled_p)
12376 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12379 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12381 int scroll_p = 0;
12382 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12384 if (PT > XFASTINT (w->last_point))
12386 /* Point has moved forward. */
12387 while (MATRIX_ROW_END_CHARPOS (row) < PT
12388 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12390 xassert (row->enabled_p);
12391 ++row;
12394 /* The end position of a row equals the start position
12395 of the next row. If PT is there, we would rather
12396 display it in the next line. */
12397 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12398 && MATRIX_ROW_END_CHARPOS (row) == PT
12399 && !cursor_row_p (w, row))
12400 ++row;
12402 /* If within the scroll margin, scroll. Note that
12403 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12404 the next line would be drawn, and that
12405 this_scroll_margin can be zero. */
12406 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12407 || PT > MATRIX_ROW_END_CHARPOS (row)
12408 /* Line is completely visible last line in window
12409 and PT is to be set in the next line. */
12410 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12411 && PT == MATRIX_ROW_END_CHARPOS (row)
12412 && !row->ends_at_zv_p
12413 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12414 scroll_p = 1;
12416 else if (PT < XFASTINT (w->last_point))
12418 /* Cursor has to be moved backward. Note that PT >=
12419 CHARPOS (startp) because of the outer if-statement. */
12420 while (!row->mode_line_p
12421 && (MATRIX_ROW_START_CHARPOS (row) > PT
12422 || (MATRIX_ROW_START_CHARPOS (row) == PT
12423 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12424 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12425 row > w->current_matrix->rows
12426 && (row-1)->ends_in_newline_from_string_p))))
12427 && (row->y > top_scroll_margin
12428 || CHARPOS (startp) == BEGV))
12430 xassert (row->enabled_p);
12431 --row;
12434 /* Consider the following case: Window starts at BEGV,
12435 there is invisible, intangible text at BEGV, so that
12436 display starts at some point START > BEGV. It can
12437 happen that we are called with PT somewhere between
12438 BEGV and START. Try to handle that case. */
12439 if (row < w->current_matrix->rows
12440 || row->mode_line_p)
12442 row = w->current_matrix->rows;
12443 if (row->mode_line_p)
12444 ++row;
12447 /* Due to newlines in overlay strings, we may have to
12448 skip forward over overlay strings. */
12449 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12450 && MATRIX_ROW_END_CHARPOS (row) == PT
12451 && !cursor_row_p (w, row))
12452 ++row;
12454 /* If within the scroll margin, scroll. */
12455 if (row->y < top_scroll_margin
12456 && CHARPOS (startp) != BEGV)
12457 scroll_p = 1;
12459 else
12461 /* Cursor did not move. So don't scroll even if cursor line
12462 is partially visible, as it was so before. */
12463 rc = CURSOR_MOVEMENT_SUCCESS;
12466 if (PT < MATRIX_ROW_START_CHARPOS (row)
12467 || PT > MATRIX_ROW_END_CHARPOS (row))
12469 /* if PT is not in the glyph row, give up. */
12470 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12472 else if (rc != CURSOR_MOVEMENT_SUCCESS
12473 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12474 && make_cursor_line_fully_visible_p)
12476 if (PT == MATRIX_ROW_END_CHARPOS (row)
12477 && !row->ends_at_zv_p
12478 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12479 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12480 else if (row->height > window_box_height (w))
12482 /* If we end up in a partially visible line, let's
12483 make it fully visible, except when it's taller
12484 than the window, in which case we can't do much
12485 about it. */
12486 *scroll_step = 1;
12487 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12489 else
12491 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12492 if (!cursor_row_fully_visible_p (w, 0, 1))
12493 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12494 else
12495 rc = CURSOR_MOVEMENT_SUCCESS;
12498 else if (scroll_p)
12499 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12500 else
12504 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12506 rc = CURSOR_MOVEMENT_SUCCESS;
12507 break;
12509 ++row;
12511 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12512 && MATRIX_ROW_START_CHARPOS (row) == PT
12513 && cursor_row_p (w, row));
12518 return rc;
12521 void
12522 set_vertical_scroll_bar (w)
12523 struct window *w;
12525 int start, end, whole;
12527 /* Calculate the start and end positions for the current window.
12528 At some point, it would be nice to choose between scrollbars
12529 which reflect the whole buffer size, with special markers
12530 indicating narrowing, and scrollbars which reflect only the
12531 visible region.
12533 Note that mini-buffers sometimes aren't displaying any text. */
12534 if (!MINI_WINDOW_P (w)
12535 || (w == XWINDOW (minibuf_window)
12536 && NILP (echo_area_buffer[0])))
12538 struct buffer *buf = XBUFFER (w->buffer);
12539 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12540 start = marker_position (w->start) - BUF_BEGV (buf);
12541 /* I don't think this is guaranteed to be right. For the
12542 moment, we'll pretend it is. */
12543 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12545 if (end < start)
12546 end = start;
12547 if (whole < (end - start))
12548 whole = end - start;
12550 else
12551 start = end = whole = 0;
12553 /* Indicate what this scroll bar ought to be displaying now. */
12554 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12558 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12559 selected_window is redisplayed.
12561 We can return without actually redisplaying the window if
12562 fonts_changed_p is nonzero. In that case, redisplay_internal will
12563 retry. */
12565 static void
12566 redisplay_window (window, just_this_one_p)
12567 Lisp_Object window;
12568 int just_this_one_p;
12570 struct window *w = XWINDOW (window);
12571 struct frame *f = XFRAME (w->frame);
12572 struct buffer *buffer = XBUFFER (w->buffer);
12573 struct buffer *old = current_buffer;
12574 struct text_pos lpoint, opoint, startp;
12575 int update_mode_line;
12576 int tem;
12577 struct it it;
12578 /* Record it now because it's overwritten. */
12579 int current_matrix_up_to_date_p = 0;
12580 int used_current_matrix_p = 0;
12581 /* This is less strict than current_matrix_up_to_date_p.
12582 It indictes that the buffer contents and narrowing are unchanged. */
12583 int buffer_unchanged_p = 0;
12584 int temp_scroll_step = 0;
12585 int count = SPECPDL_INDEX ();
12586 int rc;
12587 int centering_position = -1;
12588 int last_line_misfit = 0;
12590 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12591 opoint = lpoint;
12593 /* W must be a leaf window here. */
12594 xassert (!NILP (w->buffer));
12595 #if GLYPH_DEBUG
12596 *w->desired_matrix->method = 0;
12597 #endif
12599 specbind (Qinhibit_point_motion_hooks, Qt);
12601 reconsider_clip_changes (w, buffer);
12603 /* Has the mode line to be updated? */
12604 update_mode_line = (!NILP (w->update_mode_line)
12605 || update_mode_lines
12606 || buffer->clip_changed
12607 || buffer->prevent_redisplay_optimizations_p);
12609 if (MINI_WINDOW_P (w))
12611 if (w == XWINDOW (echo_area_window)
12612 && !NILP (echo_area_buffer[0]))
12614 if (update_mode_line)
12615 /* We may have to update a tty frame's menu bar or a
12616 tool-bar. Example `M-x C-h C-h C-g'. */
12617 goto finish_menu_bars;
12618 else
12619 /* We've already displayed the echo area glyphs in this window. */
12620 goto finish_scroll_bars;
12622 else if ((w != XWINDOW (minibuf_window)
12623 || minibuf_level == 0)
12624 /* When buffer is nonempty, redisplay window normally. */
12625 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12626 /* Quail displays non-mini buffers in minibuffer window.
12627 In that case, redisplay the window normally. */
12628 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12630 /* W is a mini-buffer window, but it's not active, so clear
12631 it. */
12632 int yb = window_text_bottom_y (w);
12633 struct glyph_row *row;
12634 int y;
12636 for (y = 0, row = w->desired_matrix->rows;
12637 y < yb;
12638 y += row->height, ++row)
12639 blank_row (w, row, y);
12640 goto finish_scroll_bars;
12643 clear_glyph_matrix (w->desired_matrix);
12646 /* Otherwise set up data on this window; select its buffer and point
12647 value. */
12648 /* Really select the buffer, for the sake of buffer-local
12649 variables. */
12650 set_buffer_internal_1 (XBUFFER (w->buffer));
12651 SET_TEXT_POS (opoint, PT, PT_BYTE);
12653 current_matrix_up_to_date_p
12654 = (!NILP (w->window_end_valid)
12655 && !current_buffer->clip_changed
12656 && !current_buffer->prevent_redisplay_optimizations_p
12657 && XFASTINT (w->last_modified) >= MODIFF
12658 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12660 buffer_unchanged_p
12661 = (!NILP (w->window_end_valid)
12662 && !current_buffer->clip_changed
12663 && XFASTINT (w->last_modified) >= MODIFF
12664 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12666 /* When windows_or_buffers_changed is non-zero, we can't rely on
12667 the window end being valid, so set it to nil there. */
12668 if (windows_or_buffers_changed)
12670 /* If window starts on a continuation line, maybe adjust the
12671 window start in case the window's width changed. */
12672 if (XMARKER (w->start)->buffer == current_buffer)
12673 compute_window_start_on_continuation_line (w);
12675 w->window_end_valid = Qnil;
12678 /* Some sanity checks. */
12679 CHECK_WINDOW_END (w);
12680 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12681 abort ();
12682 if (BYTEPOS (opoint) < CHARPOS (opoint))
12683 abort ();
12685 /* If %c is in mode line, update it if needed. */
12686 if (!NILP (w->column_number_displayed)
12687 /* This alternative quickly identifies a common case
12688 where no change is needed. */
12689 && !(PT == XFASTINT (w->last_point)
12690 && XFASTINT (w->last_modified) >= MODIFF
12691 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12692 && (XFASTINT (w->column_number_displayed)
12693 != (int) current_column ())) /* iftc */
12694 update_mode_line = 1;
12696 /* Count number of windows showing the selected buffer. An indirect
12697 buffer counts as its base buffer. */
12698 if (!just_this_one_p)
12700 struct buffer *current_base, *window_base;
12701 current_base = current_buffer;
12702 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12703 if (current_base->base_buffer)
12704 current_base = current_base->base_buffer;
12705 if (window_base->base_buffer)
12706 window_base = window_base->base_buffer;
12707 if (current_base == window_base)
12708 buffer_shared++;
12711 /* Point refers normally to the selected window. For any other
12712 window, set up appropriate value. */
12713 if (!EQ (window, selected_window))
12715 int new_pt = XMARKER (w->pointm)->charpos;
12716 int new_pt_byte = marker_byte_position (w->pointm);
12717 if (new_pt < BEGV)
12719 new_pt = BEGV;
12720 new_pt_byte = BEGV_BYTE;
12721 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12723 else if (new_pt > (ZV - 1))
12725 new_pt = ZV;
12726 new_pt_byte = ZV_BYTE;
12727 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12730 /* We don't use SET_PT so that the point-motion hooks don't run. */
12731 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12734 /* If any of the character widths specified in the display table
12735 have changed, invalidate the width run cache. It's true that
12736 this may be a bit late to catch such changes, but the rest of
12737 redisplay goes (non-fatally) haywire when the display table is
12738 changed, so why should we worry about doing any better? */
12739 if (current_buffer->width_run_cache)
12741 struct Lisp_Char_Table *disptab = buffer_display_table ();
12743 if (! disptab_matches_widthtab (disptab,
12744 XVECTOR (current_buffer->width_table)))
12746 invalidate_region_cache (current_buffer,
12747 current_buffer->width_run_cache,
12748 BEG, Z);
12749 recompute_width_table (current_buffer, disptab);
12753 /* If window-start is screwed up, choose a new one. */
12754 if (XMARKER (w->start)->buffer != current_buffer)
12755 goto recenter;
12757 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12759 /* If someone specified a new starting point but did not insist,
12760 check whether it can be used. */
12761 if (!NILP (w->optional_new_start)
12762 && CHARPOS (startp) >= BEGV
12763 && CHARPOS (startp) <= ZV)
12765 w->optional_new_start = Qnil;
12766 start_display (&it, w, startp);
12767 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12768 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12769 if (IT_CHARPOS (it) == PT)
12770 w->force_start = Qt;
12771 /* IT may overshoot PT if text at PT is invisible. */
12772 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12773 w->force_start = Qt;
12776 /* Handle case where place to start displaying has been specified,
12777 unless the specified location is outside the accessible range. */
12778 if (!NILP (w->force_start)
12779 || w->frozen_window_start_p)
12781 /* We set this later on if we have to adjust point. */
12782 int new_vpos = -1;
12783 int val;
12785 w->force_start = Qnil;
12786 w->vscroll = 0;
12787 w->window_end_valid = Qnil;
12789 /* Forget any recorded base line for line number display. */
12790 if (!buffer_unchanged_p)
12791 w->base_line_number = Qnil;
12793 /* Redisplay the mode line. Select the buffer properly for that.
12794 Also, run the hook window-scroll-functions
12795 because we have scrolled. */
12796 /* Note, we do this after clearing force_start because
12797 if there's an error, it is better to forget about force_start
12798 than to get into an infinite loop calling the hook functions
12799 and having them get more errors. */
12800 if (!update_mode_line
12801 || ! NILP (Vwindow_scroll_functions))
12803 update_mode_line = 1;
12804 w->update_mode_line = Qt;
12805 startp = run_window_scroll_functions (window, startp);
12808 w->last_modified = make_number (0);
12809 w->last_overlay_modified = make_number (0);
12810 if (CHARPOS (startp) < BEGV)
12811 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12812 else if (CHARPOS (startp) > ZV)
12813 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12815 /* Redisplay, then check if cursor has been set during the
12816 redisplay. Give up if new fonts were loaded. */
12817 val = try_window (window, startp, 1);
12818 if (!val)
12820 w->force_start = Qt;
12821 clear_glyph_matrix (w->desired_matrix);
12822 goto need_larger_matrices;
12824 /* Point was outside the scroll margins. */
12825 if (val < 0)
12826 new_vpos = window_box_height (w) / 2;
12828 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12830 /* If point does not appear, try to move point so it does
12831 appear. The desired matrix has been built above, so we
12832 can use it here. */
12833 new_vpos = window_box_height (w) / 2;
12836 if (!cursor_row_fully_visible_p (w, 0, 0))
12838 /* Point does appear, but on a line partly visible at end of window.
12839 Move it back to a fully-visible line. */
12840 new_vpos = window_box_height (w);
12843 /* If we need to move point for either of the above reasons,
12844 now actually do it. */
12845 if (new_vpos >= 0)
12847 struct glyph_row *row;
12849 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12850 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12851 ++row;
12853 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12854 MATRIX_ROW_START_BYTEPOS (row));
12856 if (w != XWINDOW (selected_window))
12857 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12858 else if (current_buffer == old)
12859 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12861 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12863 /* If we are highlighting the region, then we just changed
12864 the region, so redisplay to show it. */
12865 if (!NILP (Vtransient_mark_mode)
12866 && !NILP (current_buffer->mark_active))
12868 clear_glyph_matrix (w->desired_matrix);
12869 if (!try_window (window, startp, 0))
12870 goto need_larger_matrices;
12874 #if GLYPH_DEBUG
12875 debug_method_add (w, "forced window start");
12876 #endif
12877 goto done;
12880 /* Handle case where text has not changed, only point, and it has
12881 not moved off the frame, and we are not retrying after hscroll.
12882 (current_matrix_up_to_date_p is nonzero when retrying.) */
12883 if (current_matrix_up_to_date_p
12884 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12885 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12887 switch (rc)
12889 case CURSOR_MOVEMENT_SUCCESS:
12890 used_current_matrix_p = 1;
12891 goto done;
12893 #if 0 /* try_cursor_movement never returns this value. */
12894 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12895 goto need_larger_matrices;
12896 #endif
12898 case CURSOR_MOVEMENT_MUST_SCROLL:
12899 goto try_to_scroll;
12901 default:
12902 abort ();
12905 /* If current starting point was originally the beginning of a line
12906 but no longer is, find a new starting point. */
12907 else if (!NILP (w->start_at_line_beg)
12908 && !(CHARPOS (startp) <= BEGV
12909 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12911 #if GLYPH_DEBUG
12912 debug_method_add (w, "recenter 1");
12913 #endif
12914 goto recenter;
12917 /* Try scrolling with try_window_id. Value is > 0 if update has
12918 been done, it is -1 if we know that the same window start will
12919 not work. It is 0 if unsuccessful for some other reason. */
12920 else if ((tem = try_window_id (w)) != 0)
12922 #if GLYPH_DEBUG
12923 debug_method_add (w, "try_window_id %d", tem);
12924 #endif
12926 if (fonts_changed_p)
12927 goto need_larger_matrices;
12928 if (tem > 0)
12929 goto done;
12931 /* Otherwise try_window_id has returned -1 which means that we
12932 don't want the alternative below this comment to execute. */
12934 else if (CHARPOS (startp) >= BEGV
12935 && CHARPOS (startp) <= ZV
12936 && PT >= CHARPOS (startp)
12937 && (CHARPOS (startp) < ZV
12938 /* Avoid starting at end of buffer. */
12939 || CHARPOS (startp) == BEGV
12940 || (XFASTINT (w->last_modified) >= MODIFF
12941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12944 /* If first window line is a continuation line, and window start
12945 is inside the modified region, but the first change is before
12946 current window start, we must select a new window start.*/
12947 if (NILP (w->start_at_line_beg)
12948 && CHARPOS (startp) > BEGV)
12950 /* Make sure beg_unchanged and end_unchanged are up to date.
12951 Do it only if buffer has really changed. This may or may
12952 not have been done by try_window_id (see which) already. */
12953 if (MODIFF > SAVE_MODIFF
12954 /* This seems to happen sometimes after saving a buffer. */
12955 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12957 if (GPT - BEG < BEG_UNCHANGED)
12958 BEG_UNCHANGED = GPT - BEG;
12959 if (Z - GPT < END_UNCHANGED)
12960 END_UNCHANGED = Z - GPT;
12963 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
12964 && CHARPOS (startp) <= Z - END_UNCHANGED)
12966 /* There doesn't seems to be a simple way to find a new
12967 window start that is near the old window start, so
12968 we just recenter. */
12969 goto recenter;
12973 #if GLYPH_DEBUG
12974 debug_method_add (w, "same window start");
12975 #endif
12977 /* Try to redisplay starting at same place as before.
12978 If point has not moved off frame, accept the results. */
12979 if (!current_matrix_up_to_date_p
12980 /* Don't use try_window_reusing_current_matrix in this case
12981 because a window scroll function can have changed the
12982 buffer. */
12983 || !NILP (Vwindow_scroll_functions)
12984 || MINI_WINDOW_P (w)
12985 || !(used_current_matrix_p
12986 = try_window_reusing_current_matrix (w)))
12988 IF_DEBUG (debug_method_add (w, "1"));
12989 if (try_window (window, startp, 1) < 0)
12990 /* -1 means we need to scroll.
12991 0 means we need new matrices, but fonts_changed_p
12992 is set in that case, so we will detect it below. */
12993 goto try_to_scroll;
12996 if (fonts_changed_p)
12997 goto need_larger_matrices;
12999 if (w->cursor.vpos >= 0)
13001 if (!just_this_one_p
13002 || current_buffer->clip_changed
13003 || BEG_UNCHANGED < CHARPOS (startp))
13004 /* Forget any recorded base line for line number display. */
13005 w->base_line_number = Qnil;
13007 if (!cursor_row_fully_visible_p (w, 1, 0))
13009 clear_glyph_matrix (w->desired_matrix);
13010 last_line_misfit = 1;
13012 /* Drop through and scroll. */
13013 else
13014 goto done;
13016 else
13017 clear_glyph_matrix (w->desired_matrix);
13020 try_to_scroll:
13022 w->last_modified = make_number (0);
13023 w->last_overlay_modified = make_number (0);
13025 /* Redisplay the mode line. Select the buffer properly for that. */
13026 if (!update_mode_line)
13028 update_mode_line = 1;
13029 w->update_mode_line = Qt;
13032 /* Try to scroll by specified few lines. */
13033 if ((scroll_conservatively
13034 || scroll_step
13035 || temp_scroll_step
13036 || NUMBERP (current_buffer->scroll_up_aggressively)
13037 || NUMBERP (current_buffer->scroll_down_aggressively))
13038 && !current_buffer->clip_changed
13039 && CHARPOS (startp) >= BEGV
13040 && CHARPOS (startp) <= ZV)
13042 /* The function returns -1 if new fonts were loaded, 1 if
13043 successful, 0 if not successful. */
13044 int rc = try_scrolling (window, just_this_one_p,
13045 scroll_conservatively,
13046 scroll_step,
13047 temp_scroll_step, last_line_misfit);
13048 switch (rc)
13050 case SCROLLING_SUCCESS:
13051 goto done;
13053 case SCROLLING_NEED_LARGER_MATRICES:
13054 goto need_larger_matrices;
13056 case SCROLLING_FAILED:
13057 break;
13059 default:
13060 abort ();
13064 /* Finally, just choose place to start which centers point */
13066 recenter:
13067 if (centering_position < 0)
13068 centering_position = window_box_height (w) / 2;
13070 #if GLYPH_DEBUG
13071 debug_method_add (w, "recenter");
13072 #endif
13074 /* w->vscroll = 0; */
13076 /* Forget any previously recorded base line for line number display. */
13077 if (!buffer_unchanged_p)
13078 w->base_line_number = Qnil;
13080 /* Move backward half the height of the window. */
13081 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13082 it.current_y = it.last_visible_y;
13083 move_it_vertically_backward (&it, centering_position);
13084 xassert (IT_CHARPOS (it) >= BEGV);
13086 /* The function move_it_vertically_backward may move over more
13087 than the specified y-distance. If it->w is small, e.g. a
13088 mini-buffer window, we may end up in front of the window's
13089 display area. Start displaying at the start of the line
13090 containing PT in this case. */
13091 if (it.current_y <= 0)
13093 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13094 move_it_vertically_backward (&it, 0);
13095 #if 0
13096 /* I think this assert is bogus if buffer contains
13097 invisible text or images. KFS. */
13098 xassert (IT_CHARPOS (it) <= PT);
13099 #endif
13100 it.current_y = 0;
13103 it.current_x = it.hpos = 0;
13105 /* Set startp here explicitly in case that helps avoid an infinite loop
13106 in case the window-scroll-functions functions get errors. */
13107 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13109 /* Run scroll hooks. */
13110 startp = run_window_scroll_functions (window, it.current.pos);
13112 /* Redisplay the window. */
13113 if (!current_matrix_up_to_date_p
13114 || windows_or_buffers_changed
13115 || cursor_type_changed
13116 /* Don't use try_window_reusing_current_matrix in this case
13117 because it can have changed the buffer. */
13118 || !NILP (Vwindow_scroll_functions)
13119 || !just_this_one_p
13120 || MINI_WINDOW_P (w)
13121 || !(used_current_matrix_p
13122 = try_window_reusing_current_matrix (w)))
13123 try_window (window, startp, 0);
13125 /* If new fonts have been loaded (due to fontsets), give up. We
13126 have to start a new redisplay since we need to re-adjust glyph
13127 matrices. */
13128 if (fonts_changed_p)
13129 goto need_larger_matrices;
13131 /* If cursor did not appear assume that the middle of the window is
13132 in the first line of the window. Do it again with the next line.
13133 (Imagine a window of height 100, displaying two lines of height
13134 60. Moving back 50 from it->last_visible_y will end in the first
13135 line.) */
13136 if (w->cursor.vpos < 0)
13138 if (!NILP (w->window_end_valid)
13139 && PT >= Z - XFASTINT (w->window_end_pos))
13141 clear_glyph_matrix (w->desired_matrix);
13142 move_it_by_lines (&it, 1, 0);
13143 try_window (window, it.current.pos, 0);
13145 else if (PT < IT_CHARPOS (it))
13147 clear_glyph_matrix (w->desired_matrix);
13148 move_it_by_lines (&it, -1, 0);
13149 try_window (window, it.current.pos, 0);
13151 else
13153 /* Not much we can do about it. */
13157 /* Consider the following case: Window starts at BEGV, there is
13158 invisible, intangible text at BEGV, so that display starts at
13159 some point START > BEGV. It can happen that we are called with
13160 PT somewhere between BEGV and START. Try to handle that case. */
13161 if (w->cursor.vpos < 0)
13163 struct glyph_row *row = w->current_matrix->rows;
13164 if (row->mode_line_p)
13165 ++row;
13166 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13169 if (!cursor_row_fully_visible_p (w, 0, 0))
13171 /* If vscroll is enabled, disable it and try again. */
13172 if (w->vscroll)
13174 w->vscroll = 0;
13175 clear_glyph_matrix (w->desired_matrix);
13176 goto recenter;
13179 /* If centering point failed to make the whole line visible,
13180 put point at the top instead. That has to make the whole line
13181 visible, if it can be done. */
13182 if (centering_position == 0)
13183 goto done;
13185 clear_glyph_matrix (w->desired_matrix);
13186 centering_position = 0;
13187 goto recenter;
13190 done:
13192 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13193 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13194 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13195 ? Qt : Qnil);
13197 /* Display the mode line, if we must. */
13198 if ((update_mode_line
13199 /* If window not full width, must redo its mode line
13200 if (a) the window to its side is being redone and
13201 (b) we do a frame-based redisplay. This is a consequence
13202 of how inverted lines are drawn in frame-based redisplay. */
13203 || (!just_this_one_p
13204 && !FRAME_WINDOW_P (f)
13205 && !WINDOW_FULL_WIDTH_P (w))
13206 /* Line number to display. */
13207 || INTEGERP (w->base_line_pos)
13208 /* Column number is displayed and different from the one displayed. */
13209 || (!NILP (w->column_number_displayed)
13210 && (XFASTINT (w->column_number_displayed)
13211 != (int) current_column ()))) /* iftc */
13212 /* This means that the window has a mode line. */
13213 && (WINDOW_WANTS_MODELINE_P (w)
13214 || WINDOW_WANTS_HEADER_LINE_P (w)))
13216 display_mode_lines (w);
13218 /* If mode line height has changed, arrange for a thorough
13219 immediate redisplay using the correct mode line height. */
13220 if (WINDOW_WANTS_MODELINE_P (w)
13221 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13223 fonts_changed_p = 1;
13224 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13225 = DESIRED_MODE_LINE_HEIGHT (w);
13228 /* If top line height has changed, arrange for a thorough
13229 immediate redisplay using the correct mode line height. */
13230 if (WINDOW_WANTS_HEADER_LINE_P (w)
13231 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13233 fonts_changed_p = 1;
13234 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13235 = DESIRED_HEADER_LINE_HEIGHT (w);
13238 if (fonts_changed_p)
13239 goto need_larger_matrices;
13242 if (!line_number_displayed
13243 && !BUFFERP (w->base_line_pos))
13245 w->base_line_pos = Qnil;
13246 w->base_line_number = Qnil;
13249 finish_menu_bars:
13251 /* When we reach a frame's selected window, redo the frame's menu bar. */
13252 if (update_mode_line
13253 && EQ (FRAME_SELECTED_WINDOW (f), window))
13255 int redisplay_menu_p = 0;
13256 int redisplay_tool_bar_p = 0;
13258 if (FRAME_WINDOW_P (f))
13260 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13261 || defined (USE_GTK)
13262 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13263 #else
13264 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13265 #endif
13267 else
13268 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13270 if (redisplay_menu_p)
13271 display_menu_bar (w);
13273 #ifdef HAVE_WINDOW_SYSTEM
13274 #ifdef USE_GTK
13275 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13276 #else
13277 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13278 && (FRAME_TOOL_BAR_LINES (f) > 0
13279 || auto_resize_tool_bars_p);
13281 #endif
13283 if (redisplay_tool_bar_p)
13284 redisplay_tool_bar (f);
13285 #endif
13288 #ifdef HAVE_WINDOW_SYSTEM
13289 if (FRAME_WINDOW_P (f)
13290 && update_window_fringes (w, (just_this_one_p
13291 || (!used_current_matrix_p && !overlay_arrow_seen)
13292 || w->pseudo_window_p)))
13294 update_begin (f);
13295 BLOCK_INPUT;
13296 if (draw_window_fringes (w, 1))
13297 x_draw_vertical_border (w);
13298 UNBLOCK_INPUT;
13299 update_end (f);
13301 #endif /* HAVE_WINDOW_SYSTEM */
13303 /* We go to this label, with fonts_changed_p nonzero,
13304 if it is necessary to try again using larger glyph matrices.
13305 We have to redeem the scroll bar even in this case,
13306 because the loop in redisplay_internal expects that. */
13307 need_larger_matrices:
13309 finish_scroll_bars:
13311 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13313 /* Set the thumb's position and size. */
13314 set_vertical_scroll_bar (w);
13316 /* Note that we actually used the scroll bar attached to this
13317 window, so it shouldn't be deleted at the end of redisplay. */
13318 redeem_scroll_bar_hook (w);
13321 /* Restore current_buffer and value of point in it. */
13322 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13323 set_buffer_internal_1 (old);
13324 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13326 unbind_to (count, Qnil);
13330 /* Build the complete desired matrix of WINDOW with a window start
13331 buffer position POS.
13333 Value is 1 if successful. It is zero if fonts were loaded during
13334 redisplay which makes re-adjusting glyph matrices necessary, and -1
13335 if point would appear in the scroll margins.
13336 (We check that only if CHECK_MARGINS is nonzero. */
13339 try_window (window, pos, check_margins)
13340 Lisp_Object window;
13341 struct text_pos pos;
13342 int check_margins;
13344 struct window *w = XWINDOW (window);
13345 struct it it;
13346 struct glyph_row *last_text_row = NULL;
13348 /* Make POS the new window start. */
13349 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13351 /* Mark cursor position as unknown. No overlay arrow seen. */
13352 w->cursor.vpos = -1;
13353 overlay_arrow_seen = 0;
13355 /* Initialize iterator and info to start at POS. */
13356 start_display (&it, w, pos);
13358 /* Display all lines of W. */
13359 while (it.current_y < it.last_visible_y)
13361 if (display_line (&it))
13362 last_text_row = it.glyph_row - 1;
13363 if (fonts_changed_p)
13364 return 0;
13367 /* Don't let the cursor end in the scroll margins. */
13368 if (check_margins
13369 && !MINI_WINDOW_P (w))
13371 int this_scroll_margin;
13373 this_scroll_margin = max (0, scroll_margin);
13374 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13375 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13377 if ((w->cursor.y < this_scroll_margin
13378 && CHARPOS (pos) > BEGV
13379 && IT_CHARPOS (it) < ZV)
13380 /* rms: considering make_cursor_line_fully_visible_p here
13381 seems to give wrong results. We don't want to recenter
13382 when the last line is partly visible, we want to allow
13383 that case to be handled in the usual way. */
13384 || (w->cursor.y + 1) > it.last_visible_y)
13386 w->cursor.vpos = -1;
13387 clear_glyph_matrix (w->desired_matrix);
13388 return -1;
13392 /* If bottom moved off end of frame, change mode line percentage. */
13393 if (XFASTINT (w->window_end_pos) <= 0
13394 && Z != IT_CHARPOS (it))
13395 w->update_mode_line = Qt;
13397 /* Set window_end_pos to the offset of the last character displayed
13398 on the window from the end of current_buffer. Set
13399 window_end_vpos to its row number. */
13400 if (last_text_row)
13402 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13403 w->window_end_bytepos
13404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13405 w->window_end_pos
13406 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13407 w->window_end_vpos
13408 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13409 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13410 ->displays_text_p);
13412 else
13414 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13415 w->window_end_pos = make_number (Z - ZV);
13416 w->window_end_vpos = make_number (0);
13419 /* But that is not valid info until redisplay finishes. */
13420 w->window_end_valid = Qnil;
13421 return 1;
13426 /************************************************************************
13427 Window redisplay reusing current matrix when buffer has not changed
13428 ************************************************************************/
13430 /* Try redisplay of window W showing an unchanged buffer with a
13431 different window start than the last time it was displayed by
13432 reusing its current matrix. Value is non-zero if successful.
13433 W->start is the new window start. */
13435 static int
13436 try_window_reusing_current_matrix (w)
13437 struct window *w;
13439 struct frame *f = XFRAME (w->frame);
13440 struct glyph_row *row, *bottom_row;
13441 struct it it;
13442 struct run run;
13443 struct text_pos start, new_start;
13444 int nrows_scrolled, i;
13445 struct glyph_row *last_text_row;
13446 struct glyph_row *last_reused_text_row;
13447 struct glyph_row *start_row;
13448 int start_vpos, min_y, max_y;
13450 #if GLYPH_DEBUG
13451 if (inhibit_try_window_reusing)
13452 return 0;
13453 #endif
13455 if (/* This function doesn't handle terminal frames. */
13456 !FRAME_WINDOW_P (f)
13457 /* Don't try to reuse the display if windows have been split
13458 or such. */
13459 || windows_or_buffers_changed
13460 || cursor_type_changed)
13461 return 0;
13463 /* Can't do this if region may have changed. */
13464 if ((!NILP (Vtransient_mark_mode)
13465 && !NILP (current_buffer->mark_active))
13466 || !NILP (w->region_showing)
13467 || !NILP (Vshow_trailing_whitespace))
13468 return 0;
13470 /* If top-line visibility has changed, give up. */
13471 if (WINDOW_WANTS_HEADER_LINE_P (w)
13472 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13473 return 0;
13475 /* Give up if old or new display is scrolled vertically. We could
13476 make this function handle this, but right now it doesn't. */
13477 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13478 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13479 return 0;
13481 /* The variable new_start now holds the new window start. The old
13482 start `start' can be determined from the current matrix. */
13483 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13484 start = start_row->start.pos;
13485 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13487 /* Clear the desired matrix for the display below. */
13488 clear_glyph_matrix (w->desired_matrix);
13490 if (CHARPOS (new_start) <= CHARPOS (start))
13492 int first_row_y;
13494 /* Don't use this method if the display starts with an ellipsis
13495 displayed for invisible text. It's not easy to handle that case
13496 below, and it's certainly not worth the effort since this is
13497 not a frequent case. */
13498 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13499 return 0;
13501 IF_DEBUG (debug_method_add (w, "twu1"));
13503 /* Display up to a row that can be reused. The variable
13504 last_text_row is set to the last row displayed that displays
13505 text. Note that it.vpos == 0 if or if not there is a
13506 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13507 start_display (&it, w, new_start);
13508 first_row_y = it.current_y;
13509 w->cursor.vpos = -1;
13510 last_text_row = last_reused_text_row = NULL;
13512 while (it.current_y < it.last_visible_y
13513 && !fonts_changed_p)
13515 /* If we have reached into the characters in the START row,
13516 that means the line boundaries have changed. So we
13517 can't start copying with the row START. Maybe it will
13518 work to start copying with the following row. */
13519 while (IT_CHARPOS (it) > CHARPOS (start))
13521 /* Advance to the next row as the "start". */
13522 start_row++;
13523 start = start_row->start.pos;
13524 /* If there are no more rows to try, or just one, give up. */
13525 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13526 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13527 || CHARPOS (start) == ZV)
13529 clear_glyph_matrix (w->desired_matrix);
13530 return 0;
13533 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13535 /* If we have reached alignment,
13536 we can copy the rest of the rows. */
13537 if (IT_CHARPOS (it) == CHARPOS (start))
13538 break;
13540 if (display_line (&it))
13541 last_text_row = it.glyph_row - 1;
13544 /* A value of current_y < last_visible_y means that we stopped
13545 at the previous window start, which in turn means that we
13546 have at least one reusable row. */
13547 if (it.current_y < it.last_visible_y)
13549 /* IT.vpos always starts from 0; it counts text lines. */
13550 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13552 /* Find PT if not already found in the lines displayed. */
13553 if (w->cursor.vpos < 0)
13555 int dy = it.current_y - start_row->y;
13557 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13558 row = row_containing_pos (w, PT, row, NULL, dy);
13559 if (row)
13560 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13561 dy, nrows_scrolled);
13562 else
13564 clear_glyph_matrix (w->desired_matrix);
13565 return 0;
13569 /* Scroll the display. Do it before the current matrix is
13570 changed. The problem here is that update has not yet
13571 run, i.e. part of the current matrix is not up to date.
13572 scroll_run_hook will clear the cursor, and use the
13573 current matrix to get the height of the row the cursor is
13574 in. */
13575 run.current_y = start_row->y;
13576 run.desired_y = it.current_y;
13577 run.height = it.last_visible_y - it.current_y;
13579 if (run.height > 0 && run.current_y != run.desired_y)
13581 update_begin (f);
13582 rif->update_window_begin_hook (w);
13583 rif->clear_window_mouse_face (w);
13584 rif->scroll_run_hook (w, &run);
13585 rif->update_window_end_hook (w, 0, 0);
13586 update_end (f);
13589 /* Shift current matrix down by nrows_scrolled lines. */
13590 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13591 rotate_matrix (w->current_matrix,
13592 start_vpos,
13593 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13594 nrows_scrolled);
13596 /* Disable lines that must be updated. */
13597 for (i = 0; i < it.vpos; ++i)
13598 (start_row + i)->enabled_p = 0;
13600 /* Re-compute Y positions. */
13601 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13602 max_y = it.last_visible_y;
13603 for (row = start_row + nrows_scrolled;
13604 row < bottom_row;
13605 ++row)
13607 row->y = it.current_y;
13608 row->visible_height = row->height;
13610 if (row->y < min_y)
13611 row->visible_height -= min_y - row->y;
13612 if (row->y + row->height > max_y)
13613 row->visible_height -= row->y + row->height - max_y;
13614 row->redraw_fringe_bitmaps_p = 1;
13616 it.current_y += row->height;
13618 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13619 last_reused_text_row = row;
13620 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13621 break;
13624 /* Disable lines in the current matrix which are now
13625 below the window. */
13626 for (++row; row < bottom_row; ++row)
13627 row->enabled_p = row->mode_line_p = 0;
13630 /* Update window_end_pos etc.; last_reused_text_row is the last
13631 reused row from the current matrix containing text, if any.
13632 The value of last_text_row is the last displayed line
13633 containing text. */
13634 if (last_reused_text_row)
13636 w->window_end_bytepos
13637 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13638 w->window_end_pos
13639 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13640 w->window_end_vpos
13641 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13642 w->current_matrix));
13644 else if (last_text_row)
13646 w->window_end_bytepos
13647 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13648 w->window_end_pos
13649 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13650 w->window_end_vpos
13651 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13653 else
13655 /* This window must be completely empty. */
13656 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13657 w->window_end_pos = make_number (Z - ZV);
13658 w->window_end_vpos = make_number (0);
13660 w->window_end_valid = Qnil;
13662 /* Update hint: don't try scrolling again in update_window. */
13663 w->desired_matrix->no_scrolling_p = 1;
13665 #if GLYPH_DEBUG
13666 debug_method_add (w, "try_window_reusing_current_matrix 1");
13667 #endif
13668 return 1;
13670 else if (CHARPOS (new_start) > CHARPOS (start))
13672 struct glyph_row *pt_row, *row;
13673 struct glyph_row *first_reusable_row;
13674 struct glyph_row *first_row_to_display;
13675 int dy;
13676 int yb = window_text_bottom_y (w);
13678 /* Find the row starting at new_start, if there is one. Don't
13679 reuse a partially visible line at the end. */
13680 first_reusable_row = start_row;
13681 while (first_reusable_row->enabled_p
13682 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13683 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13684 < CHARPOS (new_start)))
13685 ++first_reusable_row;
13687 /* Give up if there is no row to reuse. */
13688 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13689 || !first_reusable_row->enabled_p
13690 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13691 != CHARPOS (new_start)))
13692 return 0;
13694 /* We can reuse fully visible rows beginning with
13695 first_reusable_row to the end of the window. Set
13696 first_row_to_display to the first row that cannot be reused.
13697 Set pt_row to the row containing point, if there is any. */
13698 pt_row = NULL;
13699 for (first_row_to_display = first_reusable_row;
13700 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13701 ++first_row_to_display)
13703 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13704 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13705 pt_row = first_row_to_display;
13708 /* Start displaying at the start of first_row_to_display. */
13709 xassert (first_row_to_display->y < yb);
13710 init_to_row_start (&it, w, first_row_to_display);
13712 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13713 - start_vpos);
13714 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13715 - nrows_scrolled);
13716 it.current_y = (first_row_to_display->y - first_reusable_row->y
13717 + WINDOW_HEADER_LINE_HEIGHT (w));
13719 /* Display lines beginning with first_row_to_display in the
13720 desired matrix. Set last_text_row to the last row displayed
13721 that displays text. */
13722 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13723 if (pt_row == NULL)
13724 w->cursor.vpos = -1;
13725 last_text_row = NULL;
13726 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13727 if (display_line (&it))
13728 last_text_row = it.glyph_row - 1;
13730 /* Give up If point isn't in a row displayed or reused. */
13731 if (w->cursor.vpos < 0)
13733 clear_glyph_matrix (w->desired_matrix);
13734 return 0;
13737 /* If point is in a reused row, adjust y and vpos of the cursor
13738 position. */
13739 if (pt_row)
13741 w->cursor.vpos -= nrows_scrolled;
13742 w->cursor.y -= first_reusable_row->y - start_row->y;
13745 /* Scroll the display. */
13746 run.current_y = first_reusable_row->y;
13747 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13748 run.height = it.last_visible_y - run.current_y;
13749 dy = run.current_y - run.desired_y;
13751 if (run.height)
13753 update_begin (f);
13754 rif->update_window_begin_hook (w);
13755 rif->clear_window_mouse_face (w);
13756 rif->scroll_run_hook (w, &run);
13757 rif->update_window_end_hook (w, 0, 0);
13758 update_end (f);
13761 /* Adjust Y positions of reused rows. */
13762 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13763 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13764 max_y = it.last_visible_y;
13765 for (row = first_reusable_row; row < first_row_to_display; ++row)
13767 row->y -= dy;
13768 row->visible_height = row->height;
13769 if (row->y < min_y)
13770 row->visible_height -= min_y - row->y;
13771 if (row->y + row->height > max_y)
13772 row->visible_height -= row->y + row->height - max_y;
13773 row->redraw_fringe_bitmaps_p = 1;
13776 /* Scroll the current matrix. */
13777 xassert (nrows_scrolled > 0);
13778 rotate_matrix (w->current_matrix,
13779 start_vpos,
13780 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13781 -nrows_scrolled);
13783 /* Disable rows not reused. */
13784 for (row -= nrows_scrolled; row < bottom_row; ++row)
13785 row->enabled_p = 0;
13787 /* Point may have moved to a different line, so we cannot assume that
13788 the previous cursor position is valid; locate the correct row. */
13789 if (pt_row)
13791 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13792 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13793 row++)
13795 w->cursor.vpos++;
13796 w->cursor.y = row->y;
13798 if (row < bottom_row)
13800 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13801 while (glyph->charpos < PT)
13803 w->cursor.hpos++;
13804 w->cursor.x += glyph->pixel_width;
13805 glyph++;
13810 /* Adjust window end. A null value of last_text_row means that
13811 the window end is in reused rows which in turn means that
13812 only its vpos can have changed. */
13813 if (last_text_row)
13815 w->window_end_bytepos
13816 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13817 w->window_end_pos
13818 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13819 w->window_end_vpos
13820 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13822 else
13824 w->window_end_vpos
13825 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13828 w->window_end_valid = Qnil;
13829 w->desired_matrix->no_scrolling_p = 1;
13831 #if GLYPH_DEBUG
13832 debug_method_add (w, "try_window_reusing_current_matrix 2");
13833 #endif
13834 return 1;
13837 return 0;
13842 /************************************************************************
13843 Window redisplay reusing current matrix when buffer has changed
13844 ************************************************************************/
13846 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13847 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13848 int *, int *));
13849 static struct glyph_row *
13850 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13851 struct glyph_row *));
13854 /* Return the last row in MATRIX displaying text. If row START is
13855 non-null, start searching with that row. IT gives the dimensions
13856 of the display. Value is null if matrix is empty; otherwise it is
13857 a pointer to the row found. */
13859 static struct glyph_row *
13860 find_last_row_displaying_text (matrix, it, start)
13861 struct glyph_matrix *matrix;
13862 struct it *it;
13863 struct glyph_row *start;
13865 struct glyph_row *row, *row_found;
13867 /* Set row_found to the last row in IT->w's current matrix
13868 displaying text. The loop looks funny but think of partially
13869 visible lines. */
13870 row_found = NULL;
13871 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13872 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13874 xassert (row->enabled_p);
13875 row_found = row;
13876 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13877 break;
13878 ++row;
13881 return row_found;
13885 /* Return the last row in the current matrix of W that is not affected
13886 by changes at the start of current_buffer that occurred since W's
13887 current matrix was built. Value is null if no such row exists.
13889 BEG_UNCHANGED us the number of characters unchanged at the start of
13890 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13891 first changed character in current_buffer. Characters at positions <
13892 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13893 when the current matrix was built. */
13895 static struct glyph_row *
13896 find_last_unchanged_at_beg_row (w)
13897 struct window *w;
13899 int first_changed_pos = BEG + BEG_UNCHANGED;
13900 struct glyph_row *row;
13901 struct glyph_row *row_found = NULL;
13902 int yb = window_text_bottom_y (w);
13904 /* Find the last row displaying unchanged text. */
13905 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13906 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13907 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13909 if (/* If row ends before first_changed_pos, it is unchanged,
13910 except in some case. */
13911 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13912 /* When row ends in ZV and we write at ZV it is not
13913 unchanged. */
13914 && !row->ends_at_zv_p
13915 /* When first_changed_pos is the end of a continued line,
13916 row is not unchanged because it may be no longer
13917 continued. */
13918 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13919 && (row->continued_p
13920 || row->exact_window_width_line_p)))
13921 row_found = row;
13923 /* Stop if last visible row. */
13924 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13925 break;
13927 ++row;
13930 return row_found;
13934 /* Find the first glyph row in the current matrix of W that is not
13935 affected by changes at the end of current_buffer since the
13936 time W's current matrix was built.
13938 Return in *DELTA the number of chars by which buffer positions in
13939 unchanged text at the end of current_buffer must be adjusted.
13941 Return in *DELTA_BYTES the corresponding number of bytes.
13943 Value is null if no such row exists, i.e. all rows are affected by
13944 changes. */
13946 static struct glyph_row *
13947 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13948 struct window *w;
13949 int *delta, *delta_bytes;
13951 struct glyph_row *row;
13952 struct glyph_row *row_found = NULL;
13954 *delta = *delta_bytes = 0;
13956 /* Display must not have been paused, otherwise the current matrix
13957 is not up to date. */
13958 if (NILP (w->window_end_valid))
13959 abort ();
13961 /* A value of window_end_pos >= END_UNCHANGED means that the window
13962 end is in the range of changed text. If so, there is no
13963 unchanged row at the end of W's current matrix. */
13964 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13965 return NULL;
13967 /* Set row to the last row in W's current matrix displaying text. */
13968 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13970 /* If matrix is entirely empty, no unchanged row exists. */
13971 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13973 /* The value of row is the last glyph row in the matrix having a
13974 meaningful buffer position in it. The end position of row
13975 corresponds to window_end_pos. This allows us to translate
13976 buffer positions in the current matrix to current buffer
13977 positions for characters not in changed text. */
13978 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13979 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13980 int last_unchanged_pos, last_unchanged_pos_old;
13981 struct glyph_row *first_text_row
13982 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13984 *delta = Z - Z_old;
13985 *delta_bytes = Z_BYTE - Z_BYTE_old;
13987 /* Set last_unchanged_pos to the buffer position of the last
13988 character in the buffer that has not been changed. Z is the
13989 index + 1 of the last character in current_buffer, i.e. by
13990 subtracting END_UNCHANGED we get the index of the last
13991 unchanged character, and we have to add BEG to get its buffer
13992 position. */
13993 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13994 last_unchanged_pos_old = last_unchanged_pos - *delta;
13996 /* Search backward from ROW for a row displaying a line that
13997 starts at a minimum position >= last_unchanged_pos_old. */
13998 for (; row > first_text_row; --row)
14000 /* This used to abort, but it can happen.
14001 It is ok to just stop the search instead here. KFS. */
14002 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14003 break;
14005 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14006 row_found = row;
14010 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14011 abort ();
14013 return row_found;
14017 /* Make sure that glyph rows in the current matrix of window W
14018 reference the same glyph memory as corresponding rows in the
14019 frame's frame matrix. This function is called after scrolling W's
14020 current matrix on a terminal frame in try_window_id and
14021 try_window_reusing_current_matrix. */
14023 static void
14024 sync_frame_with_window_matrix_rows (w)
14025 struct window *w;
14027 struct frame *f = XFRAME (w->frame);
14028 struct glyph_row *window_row, *window_row_end, *frame_row;
14030 /* Preconditions: W must be a leaf window and full-width. Its frame
14031 must have a frame matrix. */
14032 xassert (NILP (w->hchild) && NILP (w->vchild));
14033 xassert (WINDOW_FULL_WIDTH_P (w));
14034 xassert (!FRAME_WINDOW_P (f));
14036 /* If W is a full-width window, glyph pointers in W's current matrix
14037 have, by definition, to be the same as glyph pointers in the
14038 corresponding frame matrix. Note that frame matrices have no
14039 marginal areas (see build_frame_matrix). */
14040 window_row = w->current_matrix->rows;
14041 window_row_end = window_row + w->current_matrix->nrows;
14042 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14043 while (window_row < window_row_end)
14045 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14046 struct glyph *end = window_row->glyphs[LAST_AREA];
14048 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14049 frame_row->glyphs[TEXT_AREA] = start;
14050 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14051 frame_row->glyphs[LAST_AREA] = end;
14053 /* Disable frame rows whose corresponding window rows have
14054 been disabled in try_window_id. */
14055 if (!window_row->enabled_p)
14056 frame_row->enabled_p = 0;
14058 ++window_row, ++frame_row;
14063 /* Find the glyph row in window W containing CHARPOS. Consider all
14064 rows between START and END (not inclusive). END null means search
14065 all rows to the end of the display area of W. Value is the row
14066 containing CHARPOS or null. */
14068 struct glyph_row *
14069 row_containing_pos (w, charpos, start, end, dy)
14070 struct window *w;
14071 int charpos;
14072 struct glyph_row *start, *end;
14073 int dy;
14075 struct glyph_row *row = start;
14076 int last_y;
14078 /* If we happen to start on a header-line, skip that. */
14079 if (row->mode_line_p)
14080 ++row;
14082 if ((end && row >= end) || !row->enabled_p)
14083 return NULL;
14085 last_y = window_text_bottom_y (w) - dy;
14087 while (1)
14089 /* Give up if we have gone too far. */
14090 if (end && row >= end)
14091 return NULL;
14092 /* This formerly returned if they were equal.
14093 I think that both quantities are of a "last plus one" type;
14094 if so, when they are equal, the row is within the screen. -- rms. */
14095 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14096 return NULL;
14098 /* If it is in this row, return this row. */
14099 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14100 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14101 /* The end position of a row equals the start
14102 position of the next row. If CHARPOS is there, we
14103 would rather display it in the next line, except
14104 when this line ends in ZV. */
14105 && !row->ends_at_zv_p
14106 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14107 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14108 return row;
14109 ++row;
14114 /* Try to redisplay window W by reusing its existing display. W's
14115 current matrix must be up to date when this function is called,
14116 i.e. window_end_valid must not be nil.
14118 Value is
14120 1 if display has been updated
14121 0 if otherwise unsuccessful
14122 -1 if redisplay with same window start is known not to succeed
14124 The following steps are performed:
14126 1. Find the last row in the current matrix of W that is not
14127 affected by changes at the start of current_buffer. If no such row
14128 is found, give up.
14130 2. Find the first row in W's current matrix that is not affected by
14131 changes at the end of current_buffer. Maybe there is no such row.
14133 3. Display lines beginning with the row + 1 found in step 1 to the
14134 row found in step 2 or, if step 2 didn't find a row, to the end of
14135 the window.
14137 4. If cursor is not known to appear on the window, give up.
14139 5. If display stopped at the row found in step 2, scroll the
14140 display and current matrix as needed.
14142 6. Maybe display some lines at the end of W, if we must. This can
14143 happen under various circumstances, like a partially visible line
14144 becoming fully visible, or because newly displayed lines are displayed
14145 in smaller font sizes.
14147 7. Update W's window end information. */
14149 static int
14150 try_window_id (w)
14151 struct window *w;
14153 struct frame *f = XFRAME (w->frame);
14154 struct glyph_matrix *current_matrix = w->current_matrix;
14155 struct glyph_matrix *desired_matrix = w->desired_matrix;
14156 struct glyph_row *last_unchanged_at_beg_row;
14157 struct glyph_row *first_unchanged_at_end_row;
14158 struct glyph_row *row;
14159 struct glyph_row *bottom_row;
14160 int bottom_vpos;
14161 struct it it;
14162 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14163 struct text_pos start_pos;
14164 struct run run;
14165 int first_unchanged_at_end_vpos = 0;
14166 struct glyph_row *last_text_row, *last_text_row_at_end;
14167 struct text_pos start;
14168 int first_changed_charpos, last_changed_charpos;
14170 #if GLYPH_DEBUG
14171 if (inhibit_try_window_id)
14172 return 0;
14173 #endif
14175 /* This is handy for debugging. */
14176 #if 0
14177 #define GIVE_UP(X) \
14178 do { \
14179 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14180 return 0; \
14181 } while (0)
14182 #else
14183 #define GIVE_UP(X) return 0
14184 #endif
14186 SET_TEXT_POS_FROM_MARKER (start, w->start);
14188 /* Don't use this for mini-windows because these can show
14189 messages and mini-buffers, and we don't handle that here. */
14190 if (MINI_WINDOW_P (w))
14191 GIVE_UP (1);
14193 /* This flag is used to prevent redisplay optimizations. */
14194 if (windows_or_buffers_changed || cursor_type_changed)
14195 GIVE_UP (2);
14197 /* Verify that narrowing has not changed.
14198 Also verify that we were not told to prevent redisplay optimizations.
14199 It would be nice to further
14200 reduce the number of cases where this prevents try_window_id. */
14201 if (current_buffer->clip_changed
14202 || current_buffer->prevent_redisplay_optimizations_p)
14203 GIVE_UP (3);
14205 /* Window must either use window-based redisplay or be full width. */
14206 if (!FRAME_WINDOW_P (f)
14207 && (!line_ins_del_ok
14208 || !WINDOW_FULL_WIDTH_P (w)))
14209 GIVE_UP (4);
14211 /* Give up if point is not known NOT to appear in W. */
14212 if (PT < CHARPOS (start))
14213 GIVE_UP (5);
14215 /* Another way to prevent redisplay optimizations. */
14216 if (XFASTINT (w->last_modified) == 0)
14217 GIVE_UP (6);
14219 /* Verify that window is not hscrolled. */
14220 if (XFASTINT (w->hscroll) != 0)
14221 GIVE_UP (7);
14223 /* Verify that display wasn't paused. */
14224 if (NILP (w->window_end_valid))
14225 GIVE_UP (8);
14227 /* Can't use this if highlighting a region because a cursor movement
14228 will do more than just set the cursor. */
14229 if (!NILP (Vtransient_mark_mode)
14230 && !NILP (current_buffer->mark_active))
14231 GIVE_UP (9);
14233 /* Likewise if highlighting trailing whitespace. */
14234 if (!NILP (Vshow_trailing_whitespace))
14235 GIVE_UP (11);
14237 /* Likewise if showing a region. */
14238 if (!NILP (w->region_showing))
14239 GIVE_UP (10);
14241 /* Can use this if overlay arrow position and or string have changed. */
14242 if (overlay_arrows_changed_p ())
14243 GIVE_UP (12);
14246 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14247 only if buffer has really changed. The reason is that the gap is
14248 initially at Z for freshly visited files. The code below would
14249 set end_unchanged to 0 in that case. */
14250 if (MODIFF > SAVE_MODIFF
14251 /* This seems to happen sometimes after saving a buffer. */
14252 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14254 if (GPT - BEG < BEG_UNCHANGED)
14255 BEG_UNCHANGED = GPT - BEG;
14256 if (Z - GPT < END_UNCHANGED)
14257 END_UNCHANGED = Z - GPT;
14260 /* The position of the first and last character that has been changed. */
14261 first_changed_charpos = BEG + BEG_UNCHANGED;
14262 last_changed_charpos = Z - END_UNCHANGED;
14264 /* If window starts after a line end, and the last change is in
14265 front of that newline, then changes don't affect the display.
14266 This case happens with stealth-fontification. Note that although
14267 the display is unchanged, glyph positions in the matrix have to
14268 be adjusted, of course. */
14269 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14270 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14271 && ((last_changed_charpos < CHARPOS (start)
14272 && CHARPOS (start) == BEGV)
14273 || (last_changed_charpos < CHARPOS (start) - 1
14274 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14276 int Z_old, delta, Z_BYTE_old, delta_bytes;
14277 struct glyph_row *r0;
14279 /* Compute how many chars/bytes have been added to or removed
14280 from the buffer. */
14281 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14282 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14283 delta = Z - Z_old;
14284 delta_bytes = Z_BYTE - Z_BYTE_old;
14286 /* Give up if PT is not in the window. Note that it already has
14287 been checked at the start of try_window_id that PT is not in
14288 front of the window start. */
14289 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14290 GIVE_UP (13);
14292 /* If window start is unchanged, we can reuse the whole matrix
14293 as is, after adjusting glyph positions. No need to compute
14294 the window end again, since its offset from Z hasn't changed. */
14295 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14296 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14297 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14298 /* PT must not be in a partially visible line. */
14299 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14300 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14302 /* Adjust positions in the glyph matrix. */
14303 if (delta || delta_bytes)
14305 struct glyph_row *r1
14306 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14307 increment_matrix_positions (w->current_matrix,
14308 MATRIX_ROW_VPOS (r0, current_matrix),
14309 MATRIX_ROW_VPOS (r1, current_matrix),
14310 delta, delta_bytes);
14313 /* Set the cursor. */
14314 row = row_containing_pos (w, PT, r0, NULL, 0);
14315 if (row)
14316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14317 else
14318 abort ();
14319 return 1;
14323 /* Handle the case that changes are all below what is displayed in
14324 the window, and that PT is in the window. This shortcut cannot
14325 be taken if ZV is visible in the window, and text has been added
14326 there that is visible in the window. */
14327 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14328 /* ZV is not visible in the window, or there are no
14329 changes at ZV, actually. */
14330 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14331 || first_changed_charpos == last_changed_charpos))
14333 struct glyph_row *r0;
14335 /* Give up if PT is not in the window. Note that it already has
14336 been checked at the start of try_window_id that PT is not in
14337 front of the window start. */
14338 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14339 GIVE_UP (14);
14341 /* If window start is unchanged, we can reuse the whole matrix
14342 as is, without changing glyph positions since no text has
14343 been added/removed in front of the window end. */
14344 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14345 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14346 /* PT must not be in a partially visible line. */
14347 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14348 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14350 /* We have to compute the window end anew since text
14351 can have been added/removed after it. */
14352 w->window_end_pos
14353 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14354 w->window_end_bytepos
14355 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14357 /* Set the cursor. */
14358 row = row_containing_pos (w, PT, r0, NULL, 0);
14359 if (row)
14360 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14361 else
14362 abort ();
14363 return 2;
14367 /* Give up if window start is in the changed area.
14369 The condition used to read
14371 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14373 but why that was tested escapes me at the moment. */
14374 if (CHARPOS (start) >= first_changed_charpos
14375 && CHARPOS (start) <= last_changed_charpos)
14376 GIVE_UP (15);
14378 /* Check that window start agrees with the start of the first glyph
14379 row in its current matrix. Check this after we know the window
14380 start is not in changed text, otherwise positions would not be
14381 comparable. */
14382 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14383 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14384 GIVE_UP (16);
14386 /* Give up if the window ends in strings. Overlay strings
14387 at the end are difficult to handle, so don't try. */
14388 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14389 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14390 GIVE_UP (20);
14392 /* Compute the position at which we have to start displaying new
14393 lines. Some of the lines at the top of the window might be
14394 reusable because they are not displaying changed text. Find the
14395 last row in W's current matrix not affected by changes at the
14396 start of current_buffer. Value is null if changes start in the
14397 first line of window. */
14398 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14399 if (last_unchanged_at_beg_row)
14401 /* Avoid starting to display in the moddle of a character, a TAB
14402 for instance. This is easier than to set up the iterator
14403 exactly, and it's not a frequent case, so the additional
14404 effort wouldn't really pay off. */
14405 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14406 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14407 && last_unchanged_at_beg_row > w->current_matrix->rows)
14408 --last_unchanged_at_beg_row;
14410 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14411 GIVE_UP (17);
14413 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14414 GIVE_UP (18);
14415 start_pos = it.current.pos;
14417 /* Start displaying new lines in the desired matrix at the same
14418 vpos we would use in the current matrix, i.e. below
14419 last_unchanged_at_beg_row. */
14420 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14421 current_matrix);
14422 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14423 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14425 xassert (it.hpos == 0 && it.current_x == 0);
14427 else
14429 /* There are no reusable lines at the start of the window.
14430 Start displaying in the first text line. */
14431 start_display (&it, w, start);
14432 it.vpos = it.first_vpos;
14433 start_pos = it.current.pos;
14436 /* Find the first row that is not affected by changes at the end of
14437 the buffer. Value will be null if there is no unchanged row, in
14438 which case we must redisplay to the end of the window. delta
14439 will be set to the value by which buffer positions beginning with
14440 first_unchanged_at_end_row have to be adjusted due to text
14441 changes. */
14442 first_unchanged_at_end_row
14443 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14444 IF_DEBUG (debug_delta = delta);
14445 IF_DEBUG (debug_delta_bytes = delta_bytes);
14447 /* Set stop_pos to the buffer position up to which we will have to
14448 display new lines. If first_unchanged_at_end_row != NULL, this
14449 is the buffer position of the start of the line displayed in that
14450 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14451 that we don't stop at a buffer position. */
14452 stop_pos = 0;
14453 if (first_unchanged_at_end_row)
14455 xassert (last_unchanged_at_beg_row == NULL
14456 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14458 /* If this is a continuation line, move forward to the next one
14459 that isn't. Changes in lines above affect this line.
14460 Caution: this may move first_unchanged_at_end_row to a row
14461 not displaying text. */
14462 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14463 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14464 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14465 < it.last_visible_y))
14466 ++first_unchanged_at_end_row;
14468 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14469 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14470 >= it.last_visible_y))
14471 first_unchanged_at_end_row = NULL;
14472 else
14474 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14475 + delta);
14476 first_unchanged_at_end_vpos
14477 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14478 xassert (stop_pos >= Z - END_UNCHANGED);
14481 else if (last_unchanged_at_beg_row == NULL)
14482 GIVE_UP (19);
14485 #if GLYPH_DEBUG
14487 /* Either there is no unchanged row at the end, or the one we have
14488 now displays text. This is a necessary condition for the window
14489 end pos calculation at the end of this function. */
14490 xassert (first_unchanged_at_end_row == NULL
14491 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14493 debug_last_unchanged_at_beg_vpos
14494 = (last_unchanged_at_beg_row
14495 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14496 : -1);
14497 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14499 #endif /* GLYPH_DEBUG != 0 */
14502 /* Display new lines. Set last_text_row to the last new line
14503 displayed which has text on it, i.e. might end up as being the
14504 line where the window_end_vpos is. */
14505 w->cursor.vpos = -1;
14506 last_text_row = NULL;
14507 overlay_arrow_seen = 0;
14508 while (it.current_y < it.last_visible_y
14509 && !fonts_changed_p
14510 && (first_unchanged_at_end_row == NULL
14511 || IT_CHARPOS (it) < stop_pos))
14513 if (display_line (&it))
14514 last_text_row = it.glyph_row - 1;
14517 if (fonts_changed_p)
14518 return -1;
14521 /* Compute differences in buffer positions, y-positions etc. for
14522 lines reused at the bottom of the window. Compute what we can
14523 scroll. */
14524 if (first_unchanged_at_end_row
14525 /* No lines reused because we displayed everything up to the
14526 bottom of the window. */
14527 && it.current_y < it.last_visible_y)
14529 dvpos = (it.vpos
14530 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14531 current_matrix));
14532 dy = it.current_y - first_unchanged_at_end_row->y;
14533 run.current_y = first_unchanged_at_end_row->y;
14534 run.desired_y = run.current_y + dy;
14535 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14537 else
14539 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14540 first_unchanged_at_end_row = NULL;
14542 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14545 /* Find the cursor if not already found. We have to decide whether
14546 PT will appear on this window (it sometimes doesn't, but this is
14547 not a very frequent case.) This decision has to be made before
14548 the current matrix is altered. A value of cursor.vpos < 0 means
14549 that PT is either in one of the lines beginning at
14550 first_unchanged_at_end_row or below the window. Don't care for
14551 lines that might be displayed later at the window end; as
14552 mentioned, this is not a frequent case. */
14553 if (w->cursor.vpos < 0)
14555 /* Cursor in unchanged rows at the top? */
14556 if (PT < CHARPOS (start_pos)
14557 && last_unchanged_at_beg_row)
14559 row = row_containing_pos (w, PT,
14560 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14561 last_unchanged_at_beg_row + 1, 0);
14562 if (row)
14563 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14566 /* Start from first_unchanged_at_end_row looking for PT. */
14567 else if (first_unchanged_at_end_row)
14569 row = row_containing_pos (w, PT - delta,
14570 first_unchanged_at_end_row, NULL, 0);
14571 if (row)
14572 set_cursor_from_row (w, row, w->current_matrix, delta,
14573 delta_bytes, dy, dvpos);
14576 /* Give up if cursor was not found. */
14577 if (w->cursor.vpos < 0)
14579 clear_glyph_matrix (w->desired_matrix);
14580 return -1;
14584 /* Don't let the cursor end in the scroll margins. */
14586 int this_scroll_margin, cursor_height;
14588 this_scroll_margin = max (0, scroll_margin);
14589 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14590 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14591 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14593 if ((w->cursor.y < this_scroll_margin
14594 && CHARPOS (start) > BEGV)
14595 /* Old redisplay didn't take scroll margin into account at the bottom,
14596 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14597 || (w->cursor.y + (make_cursor_line_fully_visible_p
14598 ? cursor_height + this_scroll_margin
14599 : 1)) > it.last_visible_y)
14601 w->cursor.vpos = -1;
14602 clear_glyph_matrix (w->desired_matrix);
14603 return -1;
14607 /* Scroll the display. Do it before changing the current matrix so
14608 that xterm.c doesn't get confused about where the cursor glyph is
14609 found. */
14610 if (dy && run.height)
14612 update_begin (f);
14614 if (FRAME_WINDOW_P (f))
14616 rif->update_window_begin_hook (w);
14617 rif->clear_window_mouse_face (w);
14618 rif->scroll_run_hook (w, &run);
14619 rif->update_window_end_hook (w, 0, 0);
14621 else
14623 /* Terminal frame. In this case, dvpos gives the number of
14624 lines to scroll by; dvpos < 0 means scroll up. */
14625 int first_unchanged_at_end_vpos
14626 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14627 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14628 int end = (WINDOW_TOP_EDGE_LINE (w)
14629 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14630 + window_internal_height (w));
14632 /* Perform the operation on the screen. */
14633 if (dvpos > 0)
14635 /* Scroll last_unchanged_at_beg_row to the end of the
14636 window down dvpos lines. */
14637 set_terminal_window (end);
14639 /* On dumb terminals delete dvpos lines at the end
14640 before inserting dvpos empty lines. */
14641 if (!scroll_region_ok)
14642 ins_del_lines (end - dvpos, -dvpos);
14644 /* Insert dvpos empty lines in front of
14645 last_unchanged_at_beg_row. */
14646 ins_del_lines (from, dvpos);
14648 else if (dvpos < 0)
14650 /* Scroll up last_unchanged_at_beg_vpos to the end of
14651 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14652 set_terminal_window (end);
14654 /* Delete dvpos lines in front of
14655 last_unchanged_at_beg_vpos. ins_del_lines will set
14656 the cursor to the given vpos and emit |dvpos| delete
14657 line sequences. */
14658 ins_del_lines (from + dvpos, dvpos);
14660 /* On a dumb terminal insert dvpos empty lines at the
14661 end. */
14662 if (!scroll_region_ok)
14663 ins_del_lines (end + dvpos, -dvpos);
14666 set_terminal_window (0);
14669 update_end (f);
14672 /* Shift reused rows of the current matrix to the right position.
14673 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14674 text. */
14675 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14676 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14677 if (dvpos < 0)
14679 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14680 bottom_vpos, dvpos);
14681 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14682 bottom_vpos, 0);
14684 else if (dvpos > 0)
14686 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14687 bottom_vpos, dvpos);
14688 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14689 first_unchanged_at_end_vpos + dvpos, 0);
14692 /* For frame-based redisplay, make sure that current frame and window
14693 matrix are in sync with respect to glyph memory. */
14694 if (!FRAME_WINDOW_P (f))
14695 sync_frame_with_window_matrix_rows (w);
14697 /* Adjust buffer positions in reused rows. */
14698 if (delta)
14699 increment_matrix_positions (current_matrix,
14700 first_unchanged_at_end_vpos + dvpos,
14701 bottom_vpos, delta, delta_bytes);
14703 /* Adjust Y positions. */
14704 if (dy)
14705 shift_glyph_matrix (w, current_matrix,
14706 first_unchanged_at_end_vpos + dvpos,
14707 bottom_vpos, dy);
14709 if (first_unchanged_at_end_row)
14711 first_unchanged_at_end_row += dvpos;
14712 if (first_unchanged_at_end_row->y >= it.last_visible_y
14713 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14714 first_unchanged_at_end_row = NULL;
14717 /* If scrolling up, there may be some lines to display at the end of
14718 the window. */
14719 last_text_row_at_end = NULL;
14720 if (dy < 0)
14722 /* Scrolling up can leave for example a partially visible line
14723 at the end of the window to be redisplayed. */
14724 /* Set last_row to the glyph row in the current matrix where the
14725 window end line is found. It has been moved up or down in
14726 the matrix by dvpos. */
14727 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14728 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14730 /* If last_row is the window end line, it should display text. */
14731 xassert (last_row->displays_text_p);
14733 /* If window end line was partially visible before, begin
14734 displaying at that line. Otherwise begin displaying with the
14735 line following it. */
14736 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14738 init_to_row_start (&it, w, last_row);
14739 it.vpos = last_vpos;
14740 it.current_y = last_row->y;
14742 else
14744 init_to_row_end (&it, w, last_row);
14745 it.vpos = 1 + last_vpos;
14746 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14747 ++last_row;
14750 /* We may start in a continuation line. If so, we have to
14751 get the right continuation_lines_width and current_x. */
14752 it.continuation_lines_width = last_row->continuation_lines_width;
14753 it.hpos = it.current_x = 0;
14755 /* Display the rest of the lines at the window end. */
14756 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14757 while (it.current_y < it.last_visible_y
14758 && !fonts_changed_p)
14760 /* Is it always sure that the display agrees with lines in
14761 the current matrix? I don't think so, so we mark rows
14762 displayed invalid in the current matrix by setting their
14763 enabled_p flag to zero. */
14764 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14765 if (display_line (&it))
14766 last_text_row_at_end = it.glyph_row - 1;
14770 /* Update window_end_pos and window_end_vpos. */
14771 if (first_unchanged_at_end_row
14772 && !last_text_row_at_end)
14774 /* Window end line if one of the preserved rows from the current
14775 matrix. Set row to the last row displaying text in current
14776 matrix starting at first_unchanged_at_end_row, after
14777 scrolling. */
14778 xassert (first_unchanged_at_end_row->displays_text_p);
14779 row = find_last_row_displaying_text (w->current_matrix, &it,
14780 first_unchanged_at_end_row);
14781 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14783 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14784 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14785 w->window_end_vpos
14786 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14787 xassert (w->window_end_bytepos >= 0);
14788 IF_DEBUG (debug_method_add (w, "A"));
14790 else if (last_text_row_at_end)
14792 w->window_end_pos
14793 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14794 w->window_end_bytepos
14795 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14796 w->window_end_vpos
14797 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14798 xassert (w->window_end_bytepos >= 0);
14799 IF_DEBUG (debug_method_add (w, "B"));
14801 else if (last_text_row)
14803 /* We have displayed either to the end of the window or at the
14804 end of the window, i.e. the last row with text is to be found
14805 in the desired matrix. */
14806 w->window_end_pos
14807 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14808 w->window_end_bytepos
14809 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14810 w->window_end_vpos
14811 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14812 xassert (w->window_end_bytepos >= 0);
14814 else if (first_unchanged_at_end_row == NULL
14815 && last_text_row == NULL
14816 && last_text_row_at_end == NULL)
14818 /* Displayed to end of window, but no line containing text was
14819 displayed. Lines were deleted at the end of the window. */
14820 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14821 int vpos = XFASTINT (w->window_end_vpos);
14822 struct glyph_row *current_row = current_matrix->rows + vpos;
14823 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14825 for (row = NULL;
14826 row == NULL && vpos >= first_vpos;
14827 --vpos, --current_row, --desired_row)
14829 if (desired_row->enabled_p)
14831 if (desired_row->displays_text_p)
14832 row = desired_row;
14834 else if (current_row->displays_text_p)
14835 row = current_row;
14838 xassert (row != NULL);
14839 w->window_end_vpos = make_number (vpos + 1);
14840 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14841 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14842 xassert (w->window_end_bytepos >= 0);
14843 IF_DEBUG (debug_method_add (w, "C"));
14845 else
14846 abort ();
14848 #if 0 /* This leads to problems, for instance when the cursor is
14849 at ZV, and the cursor line displays no text. */
14850 /* Disable rows below what's displayed in the window. This makes
14851 debugging easier. */
14852 enable_glyph_matrix_rows (current_matrix,
14853 XFASTINT (w->window_end_vpos) + 1,
14854 bottom_vpos, 0);
14855 #endif
14857 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14858 debug_end_vpos = XFASTINT (w->window_end_vpos));
14860 /* Record that display has not been completed. */
14861 w->window_end_valid = Qnil;
14862 w->desired_matrix->no_scrolling_p = 1;
14863 return 3;
14865 #undef GIVE_UP
14870 /***********************************************************************
14871 More debugging support
14872 ***********************************************************************/
14874 #if GLYPH_DEBUG
14876 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14877 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14878 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14881 /* Dump the contents of glyph matrix MATRIX on stderr.
14883 GLYPHS 0 means don't show glyph contents.
14884 GLYPHS 1 means show glyphs in short form
14885 GLYPHS > 1 means show glyphs in long form. */
14887 void
14888 dump_glyph_matrix (matrix, glyphs)
14889 struct glyph_matrix *matrix;
14890 int glyphs;
14892 int i;
14893 for (i = 0; i < matrix->nrows; ++i)
14894 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14898 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14899 the glyph row and area where the glyph comes from. */
14901 void
14902 dump_glyph (row, glyph, area)
14903 struct glyph_row *row;
14904 struct glyph *glyph;
14905 int area;
14907 if (glyph->type == CHAR_GLYPH)
14909 fprintf (stderr,
14910 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14911 glyph - row->glyphs[TEXT_AREA],
14912 'C',
14913 glyph->charpos,
14914 (BUFFERP (glyph->object)
14915 ? 'B'
14916 : (STRINGP (glyph->object)
14917 ? 'S'
14918 : '-')),
14919 glyph->pixel_width,
14920 glyph->u.ch,
14921 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14922 ? glyph->u.ch
14923 : '.'),
14924 glyph->face_id,
14925 glyph->left_box_line_p,
14926 glyph->right_box_line_p);
14928 else if (glyph->type == STRETCH_GLYPH)
14930 fprintf (stderr,
14931 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14932 glyph - row->glyphs[TEXT_AREA],
14933 'S',
14934 glyph->charpos,
14935 (BUFFERP (glyph->object)
14936 ? 'B'
14937 : (STRINGP (glyph->object)
14938 ? 'S'
14939 : '-')),
14940 glyph->pixel_width,
14942 '.',
14943 glyph->face_id,
14944 glyph->left_box_line_p,
14945 glyph->right_box_line_p);
14947 else if (glyph->type == IMAGE_GLYPH)
14949 fprintf (stderr,
14950 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14951 glyph - row->glyphs[TEXT_AREA],
14952 'I',
14953 glyph->charpos,
14954 (BUFFERP (glyph->object)
14955 ? 'B'
14956 : (STRINGP (glyph->object)
14957 ? 'S'
14958 : '-')),
14959 glyph->pixel_width,
14960 glyph->u.img_id,
14961 '.',
14962 glyph->face_id,
14963 glyph->left_box_line_p,
14964 glyph->right_box_line_p);
14966 else if (glyph->type == COMPOSITE_GLYPH)
14968 fprintf (stderr,
14969 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14970 glyph - row->glyphs[TEXT_AREA],
14971 '+',
14972 glyph->charpos,
14973 (BUFFERP (glyph->object)
14974 ? 'B'
14975 : (STRINGP (glyph->object)
14976 ? 'S'
14977 : '-')),
14978 glyph->pixel_width,
14979 glyph->u.cmp_id,
14980 '.',
14981 glyph->face_id,
14982 glyph->left_box_line_p,
14983 glyph->right_box_line_p);
14988 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14989 GLYPHS 0 means don't show glyph contents.
14990 GLYPHS 1 means show glyphs in short form
14991 GLYPHS > 1 means show glyphs in long form. */
14993 void
14994 dump_glyph_row (row, vpos, glyphs)
14995 struct glyph_row *row;
14996 int vpos, glyphs;
14998 if (glyphs != 1)
15000 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15001 fprintf (stderr, "======================================================================\n");
15003 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15004 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15005 vpos,
15006 MATRIX_ROW_START_CHARPOS (row),
15007 MATRIX_ROW_END_CHARPOS (row),
15008 row->used[TEXT_AREA],
15009 row->contains_overlapping_glyphs_p,
15010 row->enabled_p,
15011 row->truncated_on_left_p,
15012 row->truncated_on_right_p,
15013 row->continued_p,
15014 MATRIX_ROW_CONTINUATION_LINE_P (row),
15015 row->displays_text_p,
15016 row->ends_at_zv_p,
15017 row->fill_line_p,
15018 row->ends_in_middle_of_char_p,
15019 row->starts_in_middle_of_char_p,
15020 row->mouse_face_p,
15021 row->x,
15022 row->y,
15023 row->pixel_width,
15024 row->height,
15025 row->visible_height,
15026 row->ascent,
15027 row->phys_ascent);
15028 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15029 row->end.overlay_string_index,
15030 row->continuation_lines_width);
15031 fprintf (stderr, "%9d %5d\n",
15032 CHARPOS (row->start.string_pos),
15033 CHARPOS (row->end.string_pos));
15034 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15035 row->end.dpvec_index);
15038 if (glyphs > 1)
15040 int area;
15042 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15044 struct glyph *glyph = row->glyphs[area];
15045 struct glyph *glyph_end = glyph + row->used[area];
15047 /* Glyph for a line end in text. */
15048 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15049 ++glyph_end;
15051 if (glyph < glyph_end)
15052 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15054 for (; glyph < glyph_end; ++glyph)
15055 dump_glyph (row, glyph, area);
15058 else if (glyphs == 1)
15060 int area;
15062 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15064 char *s = (char *) alloca (row->used[area] + 1);
15065 int i;
15067 for (i = 0; i < row->used[area]; ++i)
15069 struct glyph *glyph = row->glyphs[area] + i;
15070 if (glyph->type == CHAR_GLYPH
15071 && glyph->u.ch < 0x80
15072 && glyph->u.ch >= ' ')
15073 s[i] = glyph->u.ch;
15074 else
15075 s[i] = '.';
15078 s[i] = '\0';
15079 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15085 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15086 Sdump_glyph_matrix, 0, 1, "p",
15087 doc: /* Dump the current matrix of the selected window to stderr.
15088 Shows contents of glyph row structures. With non-nil
15089 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15090 glyphs in short form, otherwise show glyphs in long form. */)
15091 (glyphs)
15092 Lisp_Object glyphs;
15094 struct window *w = XWINDOW (selected_window);
15095 struct buffer *buffer = XBUFFER (w->buffer);
15097 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15098 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15099 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15100 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15101 fprintf (stderr, "=============================================\n");
15102 dump_glyph_matrix (w->current_matrix,
15103 NILP (glyphs) ? 0 : XINT (glyphs));
15104 return Qnil;
15108 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15109 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15112 struct frame *f = XFRAME (selected_frame);
15113 dump_glyph_matrix (f->current_matrix, 1);
15114 return Qnil;
15118 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15119 doc: /* Dump glyph row ROW to stderr.
15120 GLYPH 0 means don't dump glyphs.
15121 GLYPH 1 means dump glyphs in short form.
15122 GLYPH > 1 or omitted means dump glyphs in long form. */)
15123 (row, glyphs)
15124 Lisp_Object row, glyphs;
15126 struct glyph_matrix *matrix;
15127 int vpos;
15129 CHECK_NUMBER (row);
15130 matrix = XWINDOW (selected_window)->current_matrix;
15131 vpos = XINT (row);
15132 if (vpos >= 0 && vpos < matrix->nrows)
15133 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15134 vpos,
15135 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15136 return Qnil;
15140 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15141 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15142 GLYPH 0 means don't dump glyphs.
15143 GLYPH 1 means dump glyphs in short form.
15144 GLYPH > 1 or omitted means dump glyphs in long form. */)
15145 (row, glyphs)
15146 Lisp_Object row, glyphs;
15148 struct frame *sf = SELECTED_FRAME ();
15149 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15150 int vpos;
15152 CHECK_NUMBER (row);
15153 vpos = XINT (row);
15154 if (vpos >= 0 && vpos < m->nrows)
15155 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15156 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15157 return Qnil;
15161 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15162 doc: /* Toggle tracing of redisplay.
15163 With ARG, turn tracing on if and only if ARG is positive. */)
15164 (arg)
15165 Lisp_Object arg;
15167 if (NILP (arg))
15168 trace_redisplay_p = !trace_redisplay_p;
15169 else
15171 arg = Fprefix_numeric_value (arg);
15172 trace_redisplay_p = XINT (arg) > 0;
15175 return Qnil;
15179 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15180 doc: /* Like `format', but print result to stderr.
15181 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15182 (nargs, args)
15183 int nargs;
15184 Lisp_Object *args;
15186 Lisp_Object s = Fformat (nargs, args);
15187 fprintf (stderr, "%s", SDATA (s));
15188 return Qnil;
15191 #endif /* GLYPH_DEBUG */
15195 /***********************************************************************
15196 Building Desired Matrix Rows
15197 ***********************************************************************/
15199 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15200 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15202 static struct glyph_row *
15203 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15204 struct window *w;
15205 Lisp_Object overlay_arrow_string;
15207 struct frame *f = XFRAME (WINDOW_FRAME (w));
15208 struct buffer *buffer = XBUFFER (w->buffer);
15209 struct buffer *old = current_buffer;
15210 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15211 int arrow_len = SCHARS (overlay_arrow_string);
15212 const unsigned char *arrow_end = arrow_string + arrow_len;
15213 const unsigned char *p;
15214 struct it it;
15215 int multibyte_p;
15216 int n_glyphs_before;
15218 set_buffer_temp (buffer);
15219 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15220 it.glyph_row->used[TEXT_AREA] = 0;
15221 SET_TEXT_POS (it.position, 0, 0);
15223 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15224 p = arrow_string;
15225 while (p < arrow_end)
15227 Lisp_Object face, ilisp;
15229 /* Get the next character. */
15230 if (multibyte_p)
15231 it.c = string_char_and_length (p, arrow_len, &it.len);
15232 else
15233 it.c = *p, it.len = 1;
15234 p += it.len;
15236 /* Get its face. */
15237 ilisp = make_number (p - arrow_string);
15238 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15239 it.face_id = compute_char_face (f, it.c, face);
15241 /* Compute its width, get its glyphs. */
15242 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15243 SET_TEXT_POS (it.position, -1, -1);
15244 PRODUCE_GLYPHS (&it);
15246 /* If this character doesn't fit any more in the line, we have
15247 to remove some glyphs. */
15248 if (it.current_x > it.last_visible_x)
15250 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15251 break;
15255 set_buffer_temp (old);
15256 return it.glyph_row;
15260 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15261 glyphs are only inserted for terminal frames since we can't really
15262 win with truncation glyphs when partially visible glyphs are
15263 involved. Which glyphs to insert is determined by
15264 produce_special_glyphs. */
15266 static void
15267 insert_left_trunc_glyphs (it)
15268 struct it *it;
15270 struct it truncate_it;
15271 struct glyph *from, *end, *to, *toend;
15273 xassert (!FRAME_WINDOW_P (it->f));
15275 /* Get the truncation glyphs. */
15276 truncate_it = *it;
15277 truncate_it.current_x = 0;
15278 truncate_it.face_id = DEFAULT_FACE_ID;
15279 truncate_it.glyph_row = &scratch_glyph_row;
15280 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15281 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15282 truncate_it.object = make_number (0);
15283 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15285 /* Overwrite glyphs from IT with truncation glyphs. */
15286 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15287 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15288 to = it->glyph_row->glyphs[TEXT_AREA];
15289 toend = to + it->glyph_row->used[TEXT_AREA];
15291 while (from < end)
15292 *to++ = *from++;
15294 /* There may be padding glyphs left over. Overwrite them too. */
15295 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15297 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15298 while (from < end)
15299 *to++ = *from++;
15302 if (to > toend)
15303 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15307 /* Compute the pixel height and width of IT->glyph_row.
15309 Most of the time, ascent and height of a display line will be equal
15310 to the max_ascent and max_height values of the display iterator
15311 structure. This is not the case if
15313 1. We hit ZV without displaying anything. In this case, max_ascent
15314 and max_height will be zero.
15316 2. We have some glyphs that don't contribute to the line height.
15317 (The glyph row flag contributes_to_line_height_p is for future
15318 pixmap extensions).
15320 The first case is easily covered by using default values because in
15321 these cases, the line height does not really matter, except that it
15322 must not be zero. */
15324 static void
15325 compute_line_metrics (it)
15326 struct it *it;
15328 struct glyph_row *row = it->glyph_row;
15329 int area, i;
15331 if (FRAME_WINDOW_P (it->f))
15333 int i, min_y, max_y;
15335 /* The line may consist of one space only, that was added to
15336 place the cursor on it. If so, the row's height hasn't been
15337 computed yet. */
15338 if (row->height == 0)
15340 if (it->max_ascent + it->max_descent == 0)
15341 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15342 row->ascent = it->max_ascent;
15343 row->height = it->max_ascent + it->max_descent;
15344 row->phys_ascent = it->max_phys_ascent;
15345 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15346 row->extra_line_spacing = it->max_extra_line_spacing;
15349 /* Compute the width of this line. */
15350 row->pixel_width = row->x;
15351 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15352 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15354 xassert (row->pixel_width >= 0);
15355 xassert (row->ascent >= 0 && row->height > 0);
15357 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15358 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15360 /* If first line's physical ascent is larger than its logical
15361 ascent, use the physical ascent, and make the row taller.
15362 This makes accented characters fully visible. */
15363 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15364 && row->phys_ascent > row->ascent)
15366 row->height += row->phys_ascent - row->ascent;
15367 row->ascent = row->phys_ascent;
15370 /* Compute how much of the line is visible. */
15371 row->visible_height = row->height;
15373 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15374 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15376 if (row->y < min_y)
15377 row->visible_height -= min_y - row->y;
15378 if (row->y + row->height > max_y)
15379 row->visible_height -= row->y + row->height - max_y;
15381 else
15383 row->pixel_width = row->used[TEXT_AREA];
15384 if (row->continued_p)
15385 row->pixel_width -= it->continuation_pixel_width;
15386 else if (row->truncated_on_right_p)
15387 row->pixel_width -= it->truncation_pixel_width;
15388 row->ascent = row->phys_ascent = 0;
15389 row->height = row->phys_height = row->visible_height = 1;
15390 row->extra_line_spacing = 0;
15393 /* Compute a hash code for this row. */
15394 row->hash = 0;
15395 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15396 for (i = 0; i < row->used[area]; ++i)
15397 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15398 + row->glyphs[area][i].u.val
15399 + row->glyphs[area][i].face_id
15400 + row->glyphs[area][i].padding_p
15401 + (row->glyphs[area][i].type << 2));
15403 it->max_ascent = it->max_descent = 0;
15404 it->max_phys_ascent = it->max_phys_descent = 0;
15408 /* Append one space to the glyph row of iterator IT if doing a
15409 window-based redisplay. The space has the same face as
15410 IT->face_id. Value is non-zero if a space was added.
15412 This function is called to make sure that there is always one glyph
15413 at the end of a glyph row that the cursor can be set on under
15414 window-systems. (If there weren't such a glyph we would not know
15415 how wide and tall a box cursor should be displayed).
15417 At the same time this space let's a nicely handle clearing to the
15418 end of the line if the row ends in italic text. */
15420 static int
15421 append_space_for_newline (it, default_face_p)
15422 struct it *it;
15423 int default_face_p;
15425 if (FRAME_WINDOW_P (it->f))
15427 int n = it->glyph_row->used[TEXT_AREA];
15429 if (it->glyph_row->glyphs[TEXT_AREA] + n
15430 < it->glyph_row->glyphs[1 + TEXT_AREA])
15432 /* Save some values that must not be changed.
15433 Must save IT->c and IT->len because otherwise
15434 ITERATOR_AT_END_P wouldn't work anymore after
15435 append_space_for_newline has been called. */
15436 enum display_element_type saved_what = it->what;
15437 int saved_c = it->c, saved_len = it->len;
15438 int saved_x = it->current_x;
15439 int saved_face_id = it->face_id;
15440 struct text_pos saved_pos;
15441 Lisp_Object saved_object;
15442 struct face *face;
15444 saved_object = it->object;
15445 saved_pos = it->position;
15447 it->what = IT_CHARACTER;
15448 bzero (&it->position, sizeof it->position);
15449 it->object = make_number (0);
15450 it->c = ' ';
15451 it->len = 1;
15453 if (default_face_p)
15454 it->face_id = DEFAULT_FACE_ID;
15455 else if (it->face_before_selective_p)
15456 it->face_id = it->saved_face_id;
15457 face = FACE_FROM_ID (it->f, it->face_id);
15458 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15460 PRODUCE_GLYPHS (it);
15462 it->override_ascent = -1;
15463 it->constrain_row_ascent_descent_p = 0;
15464 it->current_x = saved_x;
15465 it->object = saved_object;
15466 it->position = saved_pos;
15467 it->what = saved_what;
15468 it->face_id = saved_face_id;
15469 it->len = saved_len;
15470 it->c = saved_c;
15471 return 1;
15475 return 0;
15479 /* Extend the face of the last glyph in the text area of IT->glyph_row
15480 to the end of the display line. Called from display_line.
15481 If the glyph row is empty, add a space glyph to it so that we
15482 know the face to draw. Set the glyph row flag fill_line_p. */
15484 static void
15485 extend_face_to_end_of_line (it)
15486 struct it *it;
15488 struct face *face;
15489 struct frame *f = it->f;
15491 /* If line is already filled, do nothing. */
15492 if (it->current_x >= it->last_visible_x)
15493 return;
15495 /* Face extension extends the background and box of IT->face_id
15496 to the end of the line. If the background equals the background
15497 of the frame, we don't have to do anything. */
15498 if (it->face_before_selective_p)
15499 face = FACE_FROM_ID (it->f, it->saved_face_id);
15500 else
15501 face = FACE_FROM_ID (f, it->face_id);
15503 if (FRAME_WINDOW_P (f)
15504 && it->glyph_row->displays_text_p
15505 && face->box == FACE_NO_BOX
15506 && face->background == FRAME_BACKGROUND_PIXEL (f)
15507 && !face->stipple)
15508 return;
15510 /* Set the glyph row flag indicating that the face of the last glyph
15511 in the text area has to be drawn to the end of the text area. */
15512 it->glyph_row->fill_line_p = 1;
15514 /* If current character of IT is not ASCII, make sure we have the
15515 ASCII face. This will be automatically undone the next time
15516 get_next_display_element returns a multibyte character. Note
15517 that the character will always be single byte in unibyte text. */
15518 if (!SINGLE_BYTE_CHAR_P (it->c))
15520 it->face_id = FACE_FOR_CHAR (f, face, 0);
15523 if (FRAME_WINDOW_P (f))
15525 /* If the row is empty, add a space with the current face of IT,
15526 so that we know which face to draw. */
15527 if (it->glyph_row->used[TEXT_AREA] == 0)
15529 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15530 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15531 it->glyph_row->used[TEXT_AREA] = 1;
15534 else
15536 /* Save some values that must not be changed. */
15537 int saved_x = it->current_x;
15538 struct text_pos saved_pos;
15539 Lisp_Object saved_object;
15540 enum display_element_type saved_what = it->what;
15541 int saved_face_id = it->face_id;
15543 saved_object = it->object;
15544 saved_pos = it->position;
15546 it->what = IT_CHARACTER;
15547 bzero (&it->position, sizeof it->position);
15548 it->object = make_number (0);
15549 it->c = ' ';
15550 it->len = 1;
15551 it->face_id = face->id;
15553 PRODUCE_GLYPHS (it);
15555 while (it->current_x <= it->last_visible_x)
15556 PRODUCE_GLYPHS (it);
15558 /* Don't count these blanks really. It would let us insert a left
15559 truncation glyph below and make us set the cursor on them, maybe. */
15560 it->current_x = saved_x;
15561 it->object = saved_object;
15562 it->position = saved_pos;
15563 it->what = saved_what;
15564 it->face_id = saved_face_id;
15569 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15570 trailing whitespace. */
15572 static int
15573 trailing_whitespace_p (charpos)
15574 int charpos;
15576 int bytepos = CHAR_TO_BYTE (charpos);
15577 int c = 0;
15579 while (bytepos < ZV_BYTE
15580 && (c = FETCH_CHAR (bytepos),
15581 c == ' ' || c == '\t'))
15582 ++bytepos;
15584 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15586 if (bytepos != PT_BYTE)
15587 return 1;
15589 return 0;
15593 /* Highlight trailing whitespace, if any, in ROW. */
15595 void
15596 highlight_trailing_whitespace (f, row)
15597 struct frame *f;
15598 struct glyph_row *row;
15600 int used = row->used[TEXT_AREA];
15602 if (used)
15604 struct glyph *start = row->glyphs[TEXT_AREA];
15605 struct glyph *glyph = start + used - 1;
15607 /* Skip over glyphs inserted to display the cursor at the
15608 end of a line, for extending the face of the last glyph
15609 to the end of the line on terminals, and for truncation
15610 and continuation glyphs. */
15611 while (glyph >= start
15612 && glyph->type == CHAR_GLYPH
15613 && INTEGERP (glyph->object))
15614 --glyph;
15616 /* If last glyph is a space or stretch, and it's trailing
15617 whitespace, set the face of all trailing whitespace glyphs in
15618 IT->glyph_row to `trailing-whitespace'. */
15619 if (glyph >= start
15620 && BUFFERP (glyph->object)
15621 && (glyph->type == STRETCH_GLYPH
15622 || (glyph->type == CHAR_GLYPH
15623 && glyph->u.ch == ' '))
15624 && trailing_whitespace_p (glyph->charpos))
15626 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15627 if (face_id < 0)
15628 return;
15630 while (glyph >= start
15631 && BUFFERP (glyph->object)
15632 && (glyph->type == STRETCH_GLYPH
15633 || (glyph->type == CHAR_GLYPH
15634 && glyph->u.ch == ' ')))
15635 (glyph--)->face_id = face_id;
15641 /* Value is non-zero if glyph row ROW in window W should be
15642 used to hold the cursor. */
15644 static int
15645 cursor_row_p (w, row)
15646 struct window *w;
15647 struct glyph_row *row;
15649 int cursor_row_p = 1;
15651 if (PT == MATRIX_ROW_END_CHARPOS (row))
15653 /* If the row ends with a newline from a string, we don't want
15654 the cursor there, but we still want it at the start of the
15655 string if the string starts in this row.
15656 If the row is continued it doesn't end in a newline. */
15657 if (CHARPOS (row->end.string_pos) >= 0)
15658 cursor_row_p = (row->continued_p
15659 || PT >= MATRIX_ROW_START_CHARPOS (row));
15660 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15662 /* If the row ends in middle of a real character,
15663 and the line is continued, we want the cursor here.
15664 That's because MATRIX_ROW_END_CHARPOS would equal
15665 PT if PT is before the character. */
15666 if (!row->ends_in_ellipsis_p)
15667 cursor_row_p = row->continued_p;
15668 else
15669 /* If the row ends in an ellipsis, then
15670 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15671 We want that position to be displayed after the ellipsis. */
15672 cursor_row_p = 0;
15674 /* If the row ends at ZV, display the cursor at the end of that
15675 row instead of at the start of the row below. */
15676 else if (row->ends_at_zv_p)
15677 cursor_row_p = 1;
15678 else
15679 cursor_row_p = 0;
15682 return cursor_row_p;
15686 /* Construct the glyph row IT->glyph_row in the desired matrix of
15687 IT->w from text at the current position of IT. See dispextern.h
15688 for an overview of struct it. Value is non-zero if
15689 IT->glyph_row displays text, as opposed to a line displaying ZV
15690 only. */
15692 static int
15693 display_line (it)
15694 struct it *it;
15696 struct glyph_row *row = it->glyph_row;
15697 Lisp_Object overlay_arrow_string;
15699 /* We always start displaying at hpos zero even if hscrolled. */
15700 xassert (it->hpos == 0 && it->current_x == 0);
15702 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15703 >= it->w->desired_matrix->nrows)
15705 it->w->nrows_scale_factor++;
15706 fonts_changed_p = 1;
15707 return 0;
15710 /* Is IT->w showing the region? */
15711 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15713 /* Clear the result glyph row and enable it. */
15714 prepare_desired_row (row);
15716 row->y = it->current_y;
15717 row->start = it->start;
15718 row->continuation_lines_width = it->continuation_lines_width;
15719 row->displays_text_p = 1;
15720 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15721 it->starts_in_middle_of_char_p = 0;
15723 /* Arrange the overlays nicely for our purposes. Usually, we call
15724 display_line on only one line at a time, in which case this
15725 can't really hurt too much, or we call it on lines which appear
15726 one after another in the buffer, in which case all calls to
15727 recenter_overlay_lists but the first will be pretty cheap. */
15728 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15730 /* Move over display elements that are not visible because we are
15731 hscrolled. This may stop at an x-position < IT->first_visible_x
15732 if the first glyph is partially visible or if we hit a line end. */
15733 if (it->current_x < it->first_visible_x)
15735 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15736 MOVE_TO_POS | MOVE_TO_X);
15739 /* Get the initial row height. This is either the height of the
15740 text hscrolled, if there is any, or zero. */
15741 row->ascent = it->max_ascent;
15742 row->height = it->max_ascent + it->max_descent;
15743 row->phys_ascent = it->max_phys_ascent;
15744 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15745 row->extra_line_spacing = it->max_extra_line_spacing;
15747 /* Loop generating characters. The loop is left with IT on the next
15748 character to display. */
15749 while (1)
15751 int n_glyphs_before, hpos_before, x_before;
15752 int x, i, nglyphs;
15753 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15755 /* Retrieve the next thing to display. Value is zero if end of
15756 buffer reached. */
15757 if (!get_next_display_element (it))
15759 /* Maybe add a space at the end of this line that is used to
15760 display the cursor there under X. Set the charpos of the
15761 first glyph of blank lines not corresponding to any text
15762 to -1. */
15763 #ifdef HAVE_WINDOW_SYSTEM
15764 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15765 row->exact_window_width_line_p = 1;
15766 else
15767 #endif /* HAVE_WINDOW_SYSTEM */
15768 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15769 || row->used[TEXT_AREA] == 0)
15771 row->glyphs[TEXT_AREA]->charpos = -1;
15772 row->displays_text_p = 0;
15774 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15775 && (!MINI_WINDOW_P (it->w)
15776 || (minibuf_level && EQ (it->window, minibuf_window))))
15777 row->indicate_empty_line_p = 1;
15780 it->continuation_lines_width = 0;
15781 row->ends_at_zv_p = 1;
15782 break;
15785 /* Now, get the metrics of what we want to display. This also
15786 generates glyphs in `row' (which is IT->glyph_row). */
15787 n_glyphs_before = row->used[TEXT_AREA];
15788 x = it->current_x;
15790 /* Remember the line height so far in case the next element doesn't
15791 fit on the line. */
15792 if (!it->truncate_lines_p)
15794 ascent = it->max_ascent;
15795 descent = it->max_descent;
15796 phys_ascent = it->max_phys_ascent;
15797 phys_descent = it->max_phys_descent;
15800 PRODUCE_GLYPHS (it);
15802 /* If this display element was in marginal areas, continue with
15803 the next one. */
15804 if (it->area != TEXT_AREA)
15806 row->ascent = max (row->ascent, it->max_ascent);
15807 row->height = max (row->height, it->max_ascent + it->max_descent);
15808 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15809 row->phys_height = max (row->phys_height,
15810 it->max_phys_ascent + it->max_phys_descent);
15811 row->extra_line_spacing = max (row->extra_line_spacing,
15812 it->max_extra_line_spacing);
15813 set_iterator_to_next (it, 1);
15814 continue;
15817 /* Does the display element fit on the line? If we truncate
15818 lines, we should draw past the right edge of the window. If
15819 we don't truncate, we want to stop so that we can display the
15820 continuation glyph before the right margin. If lines are
15821 continued, there are two possible strategies for characters
15822 resulting in more than 1 glyph (e.g. tabs): Display as many
15823 glyphs as possible in this line and leave the rest for the
15824 continuation line, or display the whole element in the next
15825 line. Original redisplay did the former, so we do it also. */
15826 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15827 hpos_before = it->hpos;
15828 x_before = x;
15830 if (/* Not a newline. */
15831 nglyphs > 0
15832 /* Glyphs produced fit entirely in the line. */
15833 && it->current_x < it->last_visible_x)
15835 it->hpos += nglyphs;
15836 row->ascent = max (row->ascent, it->max_ascent);
15837 row->height = max (row->height, it->max_ascent + it->max_descent);
15838 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15839 row->phys_height = max (row->phys_height,
15840 it->max_phys_ascent + it->max_phys_descent);
15841 row->extra_line_spacing = max (row->extra_line_spacing,
15842 it->max_extra_line_spacing);
15843 if (it->current_x - it->pixel_width < it->first_visible_x)
15844 row->x = x - it->first_visible_x;
15846 else
15848 int new_x;
15849 struct glyph *glyph;
15851 for (i = 0; i < nglyphs; ++i, x = new_x)
15853 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15854 new_x = x + glyph->pixel_width;
15856 if (/* Lines are continued. */
15857 !it->truncate_lines_p
15858 && (/* Glyph doesn't fit on the line. */
15859 new_x > it->last_visible_x
15860 /* Or it fits exactly on a window system frame. */
15861 || (new_x == it->last_visible_x
15862 && FRAME_WINDOW_P (it->f))))
15864 /* End of a continued line. */
15866 if (it->hpos == 0
15867 || (new_x == it->last_visible_x
15868 && FRAME_WINDOW_P (it->f)))
15870 /* Current glyph is the only one on the line or
15871 fits exactly on the line. We must continue
15872 the line because we can't draw the cursor
15873 after the glyph. */
15874 row->continued_p = 1;
15875 it->current_x = new_x;
15876 it->continuation_lines_width += new_x;
15877 ++it->hpos;
15878 if (i == nglyphs - 1)
15880 set_iterator_to_next (it, 1);
15881 #ifdef HAVE_WINDOW_SYSTEM
15882 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15884 if (!get_next_display_element (it))
15886 row->exact_window_width_line_p = 1;
15887 it->continuation_lines_width = 0;
15888 row->continued_p = 0;
15889 row->ends_at_zv_p = 1;
15891 else if (ITERATOR_AT_END_OF_LINE_P (it))
15893 row->continued_p = 0;
15894 row->exact_window_width_line_p = 1;
15897 #endif /* HAVE_WINDOW_SYSTEM */
15900 else if (CHAR_GLYPH_PADDING_P (*glyph)
15901 && !FRAME_WINDOW_P (it->f))
15903 /* A padding glyph that doesn't fit on this line.
15904 This means the whole character doesn't fit
15905 on the line. */
15906 row->used[TEXT_AREA] = n_glyphs_before;
15908 /* Fill the rest of the row with continuation
15909 glyphs like in 20.x. */
15910 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15911 < row->glyphs[1 + TEXT_AREA])
15912 produce_special_glyphs (it, IT_CONTINUATION);
15914 row->continued_p = 1;
15915 it->current_x = x_before;
15916 it->continuation_lines_width += x_before;
15918 /* Restore the height to what it was before the
15919 element not fitting on the line. */
15920 it->max_ascent = ascent;
15921 it->max_descent = descent;
15922 it->max_phys_ascent = phys_ascent;
15923 it->max_phys_descent = phys_descent;
15925 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15927 /* A TAB that extends past the right edge of the
15928 window. This produces a single glyph on
15929 window system frames. We leave the glyph in
15930 this row and let it fill the row, but don't
15931 consume the TAB. */
15932 it->continuation_lines_width += it->last_visible_x;
15933 row->ends_in_middle_of_char_p = 1;
15934 row->continued_p = 1;
15935 glyph->pixel_width = it->last_visible_x - x;
15936 it->starts_in_middle_of_char_p = 1;
15938 else
15940 /* Something other than a TAB that draws past
15941 the right edge of the window. Restore
15942 positions to values before the element. */
15943 row->used[TEXT_AREA] = n_glyphs_before + i;
15945 /* Display continuation glyphs. */
15946 if (!FRAME_WINDOW_P (it->f))
15947 produce_special_glyphs (it, IT_CONTINUATION);
15948 row->continued_p = 1;
15950 it->current_x = x_before;
15951 it->continuation_lines_width += x;
15952 extend_face_to_end_of_line (it);
15954 if (nglyphs > 1 && i > 0)
15956 row->ends_in_middle_of_char_p = 1;
15957 it->starts_in_middle_of_char_p = 1;
15960 /* Restore the height to what it was before the
15961 element not fitting on the line. */
15962 it->max_ascent = ascent;
15963 it->max_descent = descent;
15964 it->max_phys_ascent = phys_ascent;
15965 it->max_phys_descent = phys_descent;
15968 break;
15970 else if (new_x > it->first_visible_x)
15972 /* Increment number of glyphs actually displayed. */
15973 ++it->hpos;
15975 if (x < it->first_visible_x)
15976 /* Glyph is partially visible, i.e. row starts at
15977 negative X position. */
15978 row->x = x - it->first_visible_x;
15980 else
15982 /* Glyph is completely off the left margin of the
15983 window. This should not happen because of the
15984 move_it_in_display_line at the start of this
15985 function, unless the text display area of the
15986 window is empty. */
15987 xassert (it->first_visible_x <= it->last_visible_x);
15991 row->ascent = max (row->ascent, it->max_ascent);
15992 row->height = max (row->height, it->max_ascent + it->max_descent);
15993 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15994 row->phys_height = max (row->phys_height,
15995 it->max_phys_ascent + it->max_phys_descent);
15996 row->extra_line_spacing = max (row->extra_line_spacing,
15997 it->max_extra_line_spacing);
15999 /* End of this display line if row is continued. */
16000 if (row->continued_p || row->ends_at_zv_p)
16001 break;
16004 at_end_of_line:
16005 /* Is this a line end? If yes, we're also done, after making
16006 sure that a non-default face is extended up to the right
16007 margin of the window. */
16008 if (ITERATOR_AT_END_OF_LINE_P (it))
16010 int used_before = row->used[TEXT_AREA];
16012 row->ends_in_newline_from_string_p = STRINGP (it->object);
16014 #ifdef HAVE_WINDOW_SYSTEM
16015 /* Add a space at the end of the line that is used to
16016 display the cursor there. */
16017 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16018 append_space_for_newline (it, 0);
16019 #endif /* HAVE_WINDOW_SYSTEM */
16021 /* Extend the face to the end of the line. */
16022 extend_face_to_end_of_line (it);
16024 /* Make sure we have the position. */
16025 if (used_before == 0)
16026 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16028 /* Consume the line end. This skips over invisible lines. */
16029 set_iterator_to_next (it, 1);
16030 it->continuation_lines_width = 0;
16031 break;
16034 /* Proceed with next display element. Note that this skips
16035 over lines invisible because of selective display. */
16036 set_iterator_to_next (it, 1);
16038 /* If we truncate lines, we are done when the last displayed
16039 glyphs reach past the right margin of the window. */
16040 if (it->truncate_lines_p
16041 && (FRAME_WINDOW_P (it->f)
16042 ? (it->current_x >= it->last_visible_x)
16043 : (it->current_x > it->last_visible_x)))
16045 /* Maybe add truncation glyphs. */
16046 if (!FRAME_WINDOW_P (it->f))
16048 int i, n;
16050 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16051 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16052 break;
16054 for (n = row->used[TEXT_AREA]; i < n; ++i)
16056 row->used[TEXT_AREA] = i;
16057 produce_special_glyphs (it, IT_TRUNCATION);
16060 #ifdef HAVE_WINDOW_SYSTEM
16061 else
16063 /* Don't truncate if we can overflow newline into fringe. */
16064 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16066 if (!get_next_display_element (it))
16068 it->continuation_lines_width = 0;
16069 row->ends_at_zv_p = 1;
16070 row->exact_window_width_line_p = 1;
16071 break;
16073 if (ITERATOR_AT_END_OF_LINE_P (it))
16075 row->exact_window_width_line_p = 1;
16076 goto at_end_of_line;
16080 #endif /* HAVE_WINDOW_SYSTEM */
16082 row->truncated_on_right_p = 1;
16083 it->continuation_lines_width = 0;
16084 reseat_at_next_visible_line_start (it, 0);
16085 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16086 it->hpos = hpos_before;
16087 it->current_x = x_before;
16088 break;
16092 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16093 at the left window margin. */
16094 if (it->first_visible_x
16095 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16097 if (!FRAME_WINDOW_P (it->f))
16098 insert_left_trunc_glyphs (it);
16099 row->truncated_on_left_p = 1;
16102 /* If the start of this line is the overlay arrow-position, then
16103 mark this glyph row as the one containing the overlay arrow.
16104 This is clearly a mess with variable size fonts. It would be
16105 better to let it be displayed like cursors under X. */
16106 if ((row->displays_text_p || !overlay_arrow_seen)
16107 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16108 !NILP (overlay_arrow_string)))
16110 /* Overlay arrow in window redisplay is a fringe bitmap. */
16111 if (STRINGP (overlay_arrow_string))
16113 struct glyph_row *arrow_row
16114 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16115 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16116 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16117 struct glyph *p = row->glyphs[TEXT_AREA];
16118 struct glyph *p2, *end;
16120 /* Copy the arrow glyphs. */
16121 while (glyph < arrow_end)
16122 *p++ = *glyph++;
16124 /* Throw away padding glyphs. */
16125 p2 = p;
16126 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16127 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16128 ++p2;
16129 if (p2 > p)
16131 while (p2 < end)
16132 *p++ = *p2++;
16133 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16136 else
16138 xassert (INTEGERP (overlay_arrow_string));
16139 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16141 overlay_arrow_seen = 1;
16144 /* Compute pixel dimensions of this line. */
16145 compute_line_metrics (it);
16147 /* Remember the position at which this line ends. */
16148 row->end = it->current;
16150 /* Record whether this row ends inside an ellipsis. */
16151 row->ends_in_ellipsis_p
16152 = (it->method == GET_FROM_DISPLAY_VECTOR
16153 && it->ellipsis_p);
16155 /* Save fringe bitmaps in this row. */
16156 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16157 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16158 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16159 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16161 it->left_user_fringe_bitmap = 0;
16162 it->left_user_fringe_face_id = 0;
16163 it->right_user_fringe_bitmap = 0;
16164 it->right_user_fringe_face_id = 0;
16166 /* Maybe set the cursor. */
16167 if (it->w->cursor.vpos < 0
16168 && PT >= MATRIX_ROW_START_CHARPOS (row)
16169 && PT <= MATRIX_ROW_END_CHARPOS (row)
16170 && cursor_row_p (it->w, row))
16171 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16173 /* Highlight trailing whitespace. */
16174 if (!NILP (Vshow_trailing_whitespace))
16175 highlight_trailing_whitespace (it->f, it->glyph_row);
16177 /* Prepare for the next line. This line starts horizontally at (X
16178 HPOS) = (0 0). Vertical positions are incremented. As a
16179 convenience for the caller, IT->glyph_row is set to the next
16180 row to be used. */
16181 it->current_x = it->hpos = 0;
16182 it->current_y += row->height;
16183 ++it->vpos;
16184 ++it->glyph_row;
16185 it->start = it->current;
16186 return row->displays_text_p;
16191 /***********************************************************************
16192 Menu Bar
16193 ***********************************************************************/
16195 /* Redisplay the menu bar in the frame for window W.
16197 The menu bar of X frames that don't have X toolkit support is
16198 displayed in a special window W->frame->menu_bar_window.
16200 The menu bar of terminal frames is treated specially as far as
16201 glyph matrices are concerned. Menu bar lines are not part of
16202 windows, so the update is done directly on the frame matrix rows
16203 for the menu bar. */
16205 static void
16206 display_menu_bar (w)
16207 struct window *w;
16209 struct frame *f = XFRAME (WINDOW_FRAME (w));
16210 struct it it;
16211 Lisp_Object items;
16212 int i;
16214 /* Don't do all this for graphical frames. */
16215 #ifdef HAVE_NTGUI
16216 if (!NILP (Vwindow_system))
16217 return;
16218 #endif
16219 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16220 if (FRAME_X_P (f))
16221 return;
16222 #endif
16223 #ifdef MAC_OS
16224 if (FRAME_MAC_P (f))
16225 return;
16226 #endif
16228 #ifdef USE_X_TOOLKIT
16229 xassert (!FRAME_WINDOW_P (f));
16230 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16231 it.first_visible_x = 0;
16232 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16233 #else /* not USE_X_TOOLKIT */
16234 if (FRAME_WINDOW_P (f))
16236 /* Menu bar lines are displayed in the desired matrix of the
16237 dummy window menu_bar_window. */
16238 struct window *menu_w;
16239 xassert (WINDOWP (f->menu_bar_window));
16240 menu_w = XWINDOW (f->menu_bar_window);
16241 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16242 MENU_FACE_ID);
16243 it.first_visible_x = 0;
16244 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16246 else
16248 /* This is a TTY frame, i.e. character hpos/vpos are used as
16249 pixel x/y. */
16250 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16251 MENU_FACE_ID);
16252 it.first_visible_x = 0;
16253 it.last_visible_x = FRAME_COLS (f);
16255 #endif /* not USE_X_TOOLKIT */
16257 if (! mode_line_inverse_video)
16258 /* Force the menu-bar to be displayed in the default face. */
16259 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16261 /* Clear all rows of the menu bar. */
16262 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16264 struct glyph_row *row = it.glyph_row + i;
16265 clear_glyph_row (row);
16266 row->enabled_p = 1;
16267 row->full_width_p = 1;
16270 /* Display all items of the menu bar. */
16271 items = FRAME_MENU_BAR_ITEMS (it.f);
16272 for (i = 0; i < XVECTOR (items)->size; i += 4)
16274 Lisp_Object string;
16276 /* Stop at nil string. */
16277 string = AREF (items, i + 1);
16278 if (NILP (string))
16279 break;
16281 /* Remember where item was displayed. */
16282 AREF (items, i + 3) = make_number (it.hpos);
16284 /* Display the item, pad with one space. */
16285 if (it.current_x < it.last_visible_x)
16286 display_string (NULL, string, Qnil, 0, 0, &it,
16287 SCHARS (string) + 1, 0, 0, -1);
16290 /* Fill out the line with spaces. */
16291 if (it.current_x < it.last_visible_x)
16292 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16294 /* Compute the total height of the lines. */
16295 compute_line_metrics (&it);
16300 /***********************************************************************
16301 Mode Line
16302 ***********************************************************************/
16304 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16305 FORCE is non-zero, redisplay mode lines unconditionally.
16306 Otherwise, redisplay only mode lines that are garbaged. Value is
16307 the number of windows whose mode lines were redisplayed. */
16309 static int
16310 redisplay_mode_lines (window, force)
16311 Lisp_Object window;
16312 int force;
16314 int nwindows = 0;
16316 while (!NILP (window))
16318 struct window *w = XWINDOW (window);
16320 if (WINDOWP (w->hchild))
16321 nwindows += redisplay_mode_lines (w->hchild, force);
16322 else if (WINDOWP (w->vchild))
16323 nwindows += redisplay_mode_lines (w->vchild, force);
16324 else if (force
16325 || FRAME_GARBAGED_P (XFRAME (w->frame))
16326 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16328 struct text_pos lpoint;
16329 struct buffer *old = current_buffer;
16331 /* Set the window's buffer for the mode line display. */
16332 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16333 set_buffer_internal_1 (XBUFFER (w->buffer));
16335 /* Point refers normally to the selected window. For any
16336 other window, set up appropriate value. */
16337 if (!EQ (window, selected_window))
16339 struct text_pos pt;
16341 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16342 if (CHARPOS (pt) < BEGV)
16343 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16344 else if (CHARPOS (pt) > (ZV - 1))
16345 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16346 else
16347 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16350 /* Display mode lines. */
16351 clear_glyph_matrix (w->desired_matrix);
16352 if (display_mode_lines (w))
16354 ++nwindows;
16355 w->must_be_updated_p = 1;
16358 /* Restore old settings. */
16359 set_buffer_internal_1 (old);
16360 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16363 window = w->next;
16366 return nwindows;
16370 /* Display the mode and/or top line of window W. Value is the number
16371 of mode lines displayed. */
16373 static int
16374 display_mode_lines (w)
16375 struct window *w;
16377 Lisp_Object old_selected_window, old_selected_frame;
16378 int n = 0;
16380 old_selected_frame = selected_frame;
16381 selected_frame = w->frame;
16382 old_selected_window = selected_window;
16383 XSETWINDOW (selected_window, w);
16385 /* These will be set while the mode line specs are processed. */
16386 line_number_displayed = 0;
16387 w->column_number_displayed = Qnil;
16389 if (WINDOW_WANTS_MODELINE_P (w))
16391 struct window *sel_w = XWINDOW (old_selected_window);
16393 /* Select mode line face based on the real selected window. */
16394 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16395 current_buffer->mode_line_format);
16396 ++n;
16399 if (WINDOW_WANTS_HEADER_LINE_P (w))
16401 display_mode_line (w, HEADER_LINE_FACE_ID,
16402 current_buffer->header_line_format);
16403 ++n;
16406 selected_frame = old_selected_frame;
16407 selected_window = old_selected_window;
16408 return n;
16412 /* Display mode or top line of window W. FACE_ID specifies which line
16413 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16414 FORMAT is the mode line format to display. Value is the pixel
16415 height of the mode line displayed. */
16417 static int
16418 display_mode_line (w, face_id, format)
16419 struct window *w;
16420 enum face_id face_id;
16421 Lisp_Object format;
16423 struct it it;
16424 struct face *face;
16425 int count = SPECPDL_INDEX ();
16427 init_iterator (&it, w, -1, -1, NULL, face_id);
16428 prepare_desired_row (it.glyph_row);
16430 it.glyph_row->mode_line_p = 1;
16432 if (! mode_line_inverse_video)
16433 /* Force the mode-line to be displayed in the default face. */
16434 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16436 record_unwind_protect (unwind_format_mode_line,
16437 format_mode_line_unwind_data (NULL, 0));
16439 mode_line_target = MODE_LINE_DISPLAY;
16441 /* Temporarily make frame's keyboard the current kboard so that
16442 kboard-local variables in the mode_line_format will get the right
16443 values. */
16444 push_frame_kboard (it.f);
16445 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16446 pop_frame_kboard ();
16448 unbind_to (count, Qnil);
16450 /* Fill up with spaces. */
16451 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16453 compute_line_metrics (&it);
16454 it.glyph_row->full_width_p = 1;
16455 it.glyph_row->continued_p = 0;
16456 it.glyph_row->truncated_on_left_p = 0;
16457 it.glyph_row->truncated_on_right_p = 0;
16459 /* Make a 3D mode-line have a shadow at its right end. */
16460 face = FACE_FROM_ID (it.f, face_id);
16461 extend_face_to_end_of_line (&it);
16462 if (face->box != FACE_NO_BOX)
16464 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16465 + it.glyph_row->used[TEXT_AREA] - 1);
16466 last->right_box_line_p = 1;
16469 return it.glyph_row->height;
16472 /* Move element ELT in LIST to the front of LIST.
16473 Return the updated list. */
16475 static Lisp_Object
16476 move_elt_to_front (elt, list)
16477 Lisp_Object elt, list;
16479 register Lisp_Object tail, prev;
16480 register Lisp_Object tem;
16482 tail = list;
16483 prev = Qnil;
16484 while (CONSP (tail))
16486 tem = XCAR (tail);
16488 if (EQ (elt, tem))
16490 /* Splice out the link TAIL. */
16491 if (NILP (prev))
16492 list = XCDR (tail);
16493 else
16494 Fsetcdr (prev, XCDR (tail));
16496 /* Now make it the first. */
16497 Fsetcdr (tail, list);
16498 return tail;
16500 else
16501 prev = tail;
16502 tail = XCDR (tail);
16503 QUIT;
16506 /* Not found--return unchanged LIST. */
16507 return list;
16510 /* Contribute ELT to the mode line for window IT->w. How it
16511 translates into text depends on its data type.
16513 IT describes the display environment in which we display, as usual.
16515 DEPTH is the depth in recursion. It is used to prevent
16516 infinite recursion here.
16518 FIELD_WIDTH is the number of characters the display of ELT should
16519 occupy in the mode line, and PRECISION is the maximum number of
16520 characters to display from ELT's representation. See
16521 display_string for details.
16523 Returns the hpos of the end of the text generated by ELT.
16525 PROPS is a property list to add to any string we encounter.
16527 If RISKY is nonzero, remove (disregard) any properties in any string
16528 we encounter, and ignore :eval and :propertize.
16530 The global variable `mode_line_target' determines whether the
16531 output is passed to `store_mode_line_noprop',
16532 `store_mode_line_string', or `display_string'. */
16534 static int
16535 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16536 struct it *it;
16537 int depth;
16538 int field_width, precision;
16539 Lisp_Object elt, props;
16540 int risky;
16542 int n = 0, field, prec;
16543 int literal = 0;
16545 tail_recurse:
16546 if (depth > 100)
16547 elt = build_string ("*too-deep*");
16549 depth++;
16551 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16553 case Lisp_String:
16555 /* A string: output it and check for %-constructs within it. */
16556 unsigned char c;
16557 int offset = 0;
16559 if (SCHARS (elt) > 0
16560 && (!NILP (props) || risky))
16562 Lisp_Object oprops, aelt;
16563 oprops = Ftext_properties_at (make_number (0), elt);
16565 /* If the starting string's properties are not what
16566 we want, translate the string. Also, if the string
16567 is risky, do that anyway. */
16569 if (NILP (Fequal (props, oprops)) || risky)
16571 /* If the starting string has properties,
16572 merge the specified ones onto the existing ones. */
16573 if (! NILP (oprops) && !risky)
16575 Lisp_Object tem;
16577 oprops = Fcopy_sequence (oprops);
16578 tem = props;
16579 while (CONSP (tem))
16581 oprops = Fplist_put (oprops, XCAR (tem),
16582 XCAR (XCDR (tem)));
16583 tem = XCDR (XCDR (tem));
16585 props = oprops;
16588 aelt = Fassoc (elt, mode_line_proptrans_alist);
16589 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16591 /* AELT is what we want. Move it to the front
16592 without consing. */
16593 elt = XCAR (aelt);
16594 mode_line_proptrans_alist
16595 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16597 else
16599 Lisp_Object tem;
16601 /* If AELT has the wrong props, it is useless.
16602 so get rid of it. */
16603 if (! NILP (aelt))
16604 mode_line_proptrans_alist
16605 = Fdelq (aelt, mode_line_proptrans_alist);
16607 elt = Fcopy_sequence (elt);
16608 Fset_text_properties (make_number (0), Flength (elt),
16609 props, elt);
16610 /* Add this item to mode_line_proptrans_alist. */
16611 mode_line_proptrans_alist
16612 = Fcons (Fcons (elt, props),
16613 mode_line_proptrans_alist);
16614 /* Truncate mode_line_proptrans_alist
16615 to at most 50 elements. */
16616 tem = Fnthcdr (make_number (50),
16617 mode_line_proptrans_alist);
16618 if (! NILP (tem))
16619 XSETCDR (tem, Qnil);
16624 offset = 0;
16626 if (literal)
16628 prec = precision - n;
16629 switch (mode_line_target)
16631 case MODE_LINE_NOPROP:
16632 case MODE_LINE_TITLE:
16633 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16634 break;
16635 case MODE_LINE_STRING:
16636 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16637 break;
16638 case MODE_LINE_DISPLAY:
16639 n += display_string (NULL, elt, Qnil, 0, 0, it,
16640 0, prec, 0, STRING_MULTIBYTE (elt));
16641 break;
16644 break;
16647 /* Handle the non-literal case. */
16649 while ((precision <= 0 || n < precision)
16650 && SREF (elt, offset) != 0
16651 && (mode_line_target != MODE_LINE_DISPLAY
16652 || it->current_x < it->last_visible_x))
16654 int last_offset = offset;
16656 /* Advance to end of string or next format specifier. */
16657 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16660 if (offset - 1 != last_offset)
16662 int nchars, nbytes;
16664 /* Output to end of string or up to '%'. Field width
16665 is length of string. Don't output more than
16666 PRECISION allows us. */
16667 offset--;
16669 prec = c_string_width (SDATA (elt) + last_offset,
16670 offset - last_offset, precision - n,
16671 &nchars, &nbytes);
16673 switch (mode_line_target)
16675 case MODE_LINE_NOPROP:
16676 case MODE_LINE_TITLE:
16677 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16678 break;
16679 case MODE_LINE_STRING:
16681 int bytepos = last_offset;
16682 int charpos = string_byte_to_char (elt, bytepos);
16683 int endpos = (precision <= 0
16684 ? string_byte_to_char (elt, offset)
16685 : charpos + nchars);
16687 n += store_mode_line_string (NULL,
16688 Fsubstring (elt, make_number (charpos),
16689 make_number (endpos)),
16690 0, 0, 0, Qnil);
16692 break;
16693 case MODE_LINE_DISPLAY:
16695 int bytepos = last_offset;
16696 int charpos = string_byte_to_char (elt, bytepos);
16698 if (precision <= 0)
16699 nchars = string_byte_to_char (elt, offset) - charpos;
16700 n += display_string (NULL, elt, Qnil, 0, charpos,
16701 it, 0, nchars, 0,
16702 STRING_MULTIBYTE (elt));
16704 break;
16707 else /* c == '%' */
16709 int percent_position = offset;
16711 /* Get the specified minimum width. Zero means
16712 don't pad. */
16713 field = 0;
16714 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16715 field = field * 10 + c - '0';
16717 /* Don't pad beyond the total padding allowed. */
16718 if (field_width - n > 0 && field > field_width - n)
16719 field = field_width - n;
16721 /* Note that either PRECISION <= 0 or N < PRECISION. */
16722 prec = precision - n;
16724 if (c == 'M')
16725 n += display_mode_element (it, depth, field, prec,
16726 Vglobal_mode_string, props,
16727 risky);
16728 else if (c != 0)
16730 int multibyte;
16731 int bytepos, charpos;
16732 unsigned char *spec;
16734 bytepos = percent_position;
16735 charpos = (STRING_MULTIBYTE (elt)
16736 ? string_byte_to_char (elt, bytepos)
16737 : bytepos);
16739 spec
16740 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16742 switch (mode_line_target)
16744 case MODE_LINE_NOPROP:
16745 case MODE_LINE_TITLE:
16746 n += store_mode_line_noprop (spec, field, prec);
16747 break;
16748 case MODE_LINE_STRING:
16750 int len = strlen (spec);
16751 Lisp_Object tem = make_string (spec, len);
16752 props = Ftext_properties_at (make_number (charpos), elt);
16753 /* Should only keep face property in props */
16754 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16756 break;
16757 case MODE_LINE_DISPLAY:
16759 int nglyphs_before, nwritten;
16761 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16762 nwritten = display_string (spec, Qnil, elt,
16763 charpos, 0, it,
16764 field, prec, 0,
16765 multibyte);
16767 /* Assign to the glyphs written above the
16768 string where the `%x' came from, position
16769 of the `%'. */
16770 if (nwritten > 0)
16772 struct glyph *glyph
16773 = (it->glyph_row->glyphs[TEXT_AREA]
16774 + nglyphs_before);
16775 int i;
16777 for (i = 0; i < nwritten; ++i)
16779 glyph[i].object = elt;
16780 glyph[i].charpos = charpos;
16783 n += nwritten;
16786 break;
16789 else /* c == 0 */
16790 break;
16794 break;
16796 case Lisp_Symbol:
16797 /* A symbol: process the value of the symbol recursively
16798 as if it appeared here directly. Avoid error if symbol void.
16799 Special case: if value of symbol is a string, output the string
16800 literally. */
16802 register Lisp_Object tem;
16804 /* If the variable is not marked as risky to set
16805 then its contents are risky to use. */
16806 if (NILP (Fget (elt, Qrisky_local_variable)))
16807 risky = 1;
16809 tem = Fboundp (elt);
16810 if (!NILP (tem))
16812 tem = Fsymbol_value (elt);
16813 /* If value is a string, output that string literally:
16814 don't check for % within it. */
16815 if (STRINGP (tem))
16816 literal = 1;
16818 if (!EQ (tem, elt))
16820 /* Give up right away for nil or t. */
16821 elt = tem;
16822 goto tail_recurse;
16826 break;
16828 case Lisp_Cons:
16830 register Lisp_Object car, tem;
16832 /* A cons cell: five distinct cases.
16833 If first element is :eval or :propertize, do something special.
16834 If first element is a string or a cons, process all the elements
16835 and effectively concatenate them.
16836 If first element is a negative number, truncate displaying cdr to
16837 at most that many characters. If positive, pad (with spaces)
16838 to at least that many characters.
16839 If first element is a symbol, process the cadr or caddr recursively
16840 according to whether the symbol's value is non-nil or nil. */
16841 car = XCAR (elt);
16842 if (EQ (car, QCeval))
16844 /* An element of the form (:eval FORM) means evaluate FORM
16845 and use the result as mode line elements. */
16847 if (risky)
16848 break;
16850 if (CONSP (XCDR (elt)))
16852 Lisp_Object spec;
16853 spec = safe_eval (XCAR (XCDR (elt)));
16854 n += display_mode_element (it, depth, field_width - n,
16855 precision - n, spec, props,
16856 risky);
16859 else if (EQ (car, QCpropertize))
16861 /* An element of the form (:propertize ELT PROPS...)
16862 means display ELT but applying properties PROPS. */
16864 if (risky)
16865 break;
16867 if (CONSP (XCDR (elt)))
16868 n += display_mode_element (it, depth, field_width - n,
16869 precision - n, XCAR (XCDR (elt)),
16870 XCDR (XCDR (elt)), risky);
16872 else if (SYMBOLP (car))
16874 tem = Fboundp (car);
16875 elt = XCDR (elt);
16876 if (!CONSP (elt))
16877 goto invalid;
16878 /* elt is now the cdr, and we know it is a cons cell.
16879 Use its car if CAR has a non-nil value. */
16880 if (!NILP (tem))
16882 tem = Fsymbol_value (car);
16883 if (!NILP (tem))
16885 elt = XCAR (elt);
16886 goto tail_recurse;
16889 /* Symbol's value is nil (or symbol is unbound)
16890 Get the cddr of the original list
16891 and if possible find the caddr and use that. */
16892 elt = XCDR (elt);
16893 if (NILP (elt))
16894 break;
16895 else if (!CONSP (elt))
16896 goto invalid;
16897 elt = XCAR (elt);
16898 goto tail_recurse;
16900 else if (INTEGERP (car))
16902 register int lim = XINT (car);
16903 elt = XCDR (elt);
16904 if (lim < 0)
16906 /* Negative int means reduce maximum width. */
16907 if (precision <= 0)
16908 precision = -lim;
16909 else
16910 precision = min (precision, -lim);
16912 else if (lim > 0)
16914 /* Padding specified. Don't let it be more than
16915 current maximum. */
16916 if (precision > 0)
16917 lim = min (precision, lim);
16919 /* If that's more padding than already wanted, queue it.
16920 But don't reduce padding already specified even if
16921 that is beyond the current truncation point. */
16922 field_width = max (lim, field_width);
16924 goto tail_recurse;
16926 else if (STRINGP (car) || CONSP (car))
16928 register int limit = 50;
16929 /* Limit is to protect against circular lists. */
16930 while (CONSP (elt)
16931 && --limit > 0
16932 && (precision <= 0 || n < precision))
16934 n += display_mode_element (it, depth,
16935 /* Do padding only after the last
16936 element in the list. */
16937 (! CONSP (XCDR (elt))
16938 ? field_width - n
16939 : 0),
16940 precision - n, XCAR (elt),
16941 props, risky);
16942 elt = XCDR (elt);
16946 break;
16948 default:
16949 invalid:
16950 elt = build_string ("*invalid*");
16951 goto tail_recurse;
16954 /* Pad to FIELD_WIDTH. */
16955 if (field_width > 0 && n < field_width)
16957 switch (mode_line_target)
16959 case MODE_LINE_NOPROP:
16960 case MODE_LINE_TITLE:
16961 n += store_mode_line_noprop ("", field_width - n, 0);
16962 break;
16963 case MODE_LINE_STRING:
16964 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16965 break;
16966 case MODE_LINE_DISPLAY:
16967 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16968 0, 0, 0);
16969 break;
16973 return n;
16976 /* Store a mode-line string element in mode_line_string_list.
16978 If STRING is non-null, display that C string. Otherwise, the Lisp
16979 string LISP_STRING is displayed.
16981 FIELD_WIDTH is the minimum number of output glyphs to produce.
16982 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16983 with spaces. FIELD_WIDTH <= 0 means don't pad.
16985 PRECISION is the maximum number of characters to output from
16986 STRING. PRECISION <= 0 means don't truncate the string.
16988 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16989 properties to the string.
16991 PROPS are the properties to add to the string.
16992 The mode_line_string_face face property is always added to the string.
16995 static int
16996 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16997 char *string;
16998 Lisp_Object lisp_string;
16999 int copy_string;
17000 int field_width;
17001 int precision;
17002 Lisp_Object props;
17004 int len;
17005 int n = 0;
17007 if (string != NULL)
17009 len = strlen (string);
17010 if (precision > 0 && len > precision)
17011 len = precision;
17012 lisp_string = make_string (string, len);
17013 if (NILP (props))
17014 props = mode_line_string_face_prop;
17015 else if (!NILP (mode_line_string_face))
17017 Lisp_Object face = Fplist_get (props, Qface);
17018 props = Fcopy_sequence (props);
17019 if (NILP (face))
17020 face = mode_line_string_face;
17021 else
17022 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17023 props = Fplist_put (props, Qface, face);
17025 Fadd_text_properties (make_number (0), make_number (len),
17026 props, lisp_string);
17028 else
17030 len = XFASTINT (Flength (lisp_string));
17031 if (precision > 0 && len > precision)
17033 len = precision;
17034 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17035 precision = -1;
17037 if (!NILP (mode_line_string_face))
17039 Lisp_Object face;
17040 if (NILP (props))
17041 props = Ftext_properties_at (make_number (0), lisp_string);
17042 face = Fplist_get (props, Qface);
17043 if (NILP (face))
17044 face = mode_line_string_face;
17045 else
17046 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17047 props = Fcons (Qface, Fcons (face, Qnil));
17048 if (copy_string)
17049 lisp_string = Fcopy_sequence (lisp_string);
17051 if (!NILP (props))
17052 Fadd_text_properties (make_number (0), make_number (len),
17053 props, lisp_string);
17056 if (len > 0)
17058 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17059 n += len;
17062 if (field_width > len)
17064 field_width -= len;
17065 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17066 if (!NILP (props))
17067 Fadd_text_properties (make_number (0), make_number (field_width),
17068 props, lisp_string);
17069 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17070 n += field_width;
17073 return n;
17077 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17078 1, 4, 0,
17079 doc: /* Format a string out of a mode line format specification.
17080 First arg FORMAT specifies the mode line format (see `mode-line-format'
17081 for details) to use.
17083 Optional second arg FACE specifies the face property to put
17084 on all characters for which no face is specified.
17085 t means whatever face the window's mode line currently uses
17086 \(either `mode-line' or `mode-line-inactive', depending).
17087 nil means the default is no face property.
17088 If FACE is an integer, the value string has no text properties.
17090 Optional third and fourth args WINDOW and BUFFER specify the window
17091 and buffer to use as the context for the formatting (defaults
17092 are the selected window and the window's buffer). */)
17093 (format, face, window, buffer)
17094 Lisp_Object format, face, window, buffer;
17096 struct it it;
17097 int len;
17098 struct window *w;
17099 struct buffer *old_buffer = NULL;
17100 int face_id = -1;
17101 int no_props = INTEGERP (face);
17102 int count = SPECPDL_INDEX ();
17103 Lisp_Object str;
17104 int string_start = 0;
17106 if (NILP (window))
17107 window = selected_window;
17108 CHECK_WINDOW (window);
17109 w = XWINDOW (window);
17111 if (NILP (buffer))
17112 buffer = w->buffer;
17113 CHECK_BUFFER (buffer);
17115 if (NILP (format))
17116 return build_string ("");
17118 if (no_props)
17119 face = Qnil;
17121 if (!NILP (face))
17123 if (EQ (face, Qt))
17124 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17125 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17128 if (face_id < 0)
17129 face_id = DEFAULT_FACE_ID;
17131 if (XBUFFER (buffer) != current_buffer)
17132 old_buffer = current_buffer;
17134 /* Save things including mode_line_proptrans_alist,
17135 and set that to nil so that we don't alter the outer value. */
17136 record_unwind_protect (unwind_format_mode_line,
17137 format_mode_line_unwind_data (old_buffer, 1));
17138 mode_line_proptrans_alist = Qnil;
17140 if (old_buffer)
17141 set_buffer_internal_1 (XBUFFER (buffer));
17143 init_iterator (&it, w, -1, -1, NULL, face_id);
17145 if (no_props)
17147 mode_line_target = MODE_LINE_NOPROP;
17148 mode_line_string_face_prop = Qnil;
17149 mode_line_string_list = Qnil;
17150 string_start = MODE_LINE_NOPROP_LEN (0);
17152 else
17154 mode_line_target = MODE_LINE_STRING;
17155 mode_line_string_list = Qnil;
17156 mode_line_string_face = face;
17157 mode_line_string_face_prop
17158 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17161 push_frame_kboard (it.f);
17162 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17163 pop_frame_kboard ();
17165 if (no_props)
17167 len = MODE_LINE_NOPROP_LEN (string_start);
17168 str = make_string (mode_line_noprop_buf + string_start, len);
17170 else
17172 mode_line_string_list = Fnreverse (mode_line_string_list);
17173 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17174 make_string ("", 0));
17177 unbind_to (count, Qnil);
17178 return str;
17181 /* Write a null-terminated, right justified decimal representation of
17182 the positive integer D to BUF using a minimal field width WIDTH. */
17184 static void
17185 pint2str (buf, width, d)
17186 register char *buf;
17187 register int width;
17188 register int d;
17190 register char *p = buf;
17192 if (d <= 0)
17193 *p++ = '0';
17194 else
17196 while (d > 0)
17198 *p++ = d % 10 + '0';
17199 d /= 10;
17203 for (width -= (int) (p - buf); width > 0; --width)
17204 *p++ = ' ';
17205 *p-- = '\0';
17206 while (p > buf)
17208 d = *buf;
17209 *buf++ = *p;
17210 *p-- = d;
17214 /* Write a null-terminated, right justified decimal and "human
17215 readable" representation of the nonnegative integer D to BUF using
17216 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17218 static const char power_letter[] =
17220 0, /* not used */
17221 'k', /* kilo */
17222 'M', /* mega */
17223 'G', /* giga */
17224 'T', /* tera */
17225 'P', /* peta */
17226 'E', /* exa */
17227 'Z', /* zetta */
17228 'Y' /* yotta */
17231 static void
17232 pint2hrstr (buf, width, d)
17233 char *buf;
17234 int width;
17235 int d;
17237 /* We aim to represent the nonnegative integer D as
17238 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17239 int quotient = d;
17240 int remainder = 0;
17241 /* -1 means: do not use TENTHS. */
17242 int tenths = -1;
17243 int exponent = 0;
17245 /* Length of QUOTIENT.TENTHS as a string. */
17246 int length;
17248 char * psuffix;
17249 char * p;
17251 if (1000 <= quotient)
17253 /* Scale to the appropriate EXPONENT. */
17256 remainder = quotient % 1000;
17257 quotient /= 1000;
17258 exponent++;
17260 while (1000 <= quotient);
17262 /* Round to nearest and decide whether to use TENTHS or not. */
17263 if (quotient <= 9)
17265 tenths = remainder / 100;
17266 if (50 <= remainder % 100)
17268 if (tenths < 9)
17269 tenths++;
17270 else
17272 quotient++;
17273 if (quotient == 10)
17274 tenths = -1;
17275 else
17276 tenths = 0;
17280 else
17281 if (500 <= remainder)
17283 if (quotient < 999)
17284 quotient++;
17285 else
17287 quotient = 1;
17288 exponent++;
17289 tenths = 0;
17294 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17295 if (tenths == -1 && quotient <= 99)
17296 if (quotient <= 9)
17297 length = 1;
17298 else
17299 length = 2;
17300 else
17301 length = 3;
17302 p = psuffix = buf + max (width, length);
17304 /* Print EXPONENT. */
17305 if (exponent)
17306 *psuffix++ = power_letter[exponent];
17307 *psuffix = '\0';
17309 /* Print TENTHS. */
17310 if (tenths >= 0)
17312 *--p = '0' + tenths;
17313 *--p = '.';
17316 /* Print QUOTIENT. */
17319 int digit = quotient % 10;
17320 *--p = '0' + digit;
17322 while ((quotient /= 10) != 0);
17324 /* Print leading spaces. */
17325 while (buf < p)
17326 *--p = ' ';
17329 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17330 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17331 type of CODING_SYSTEM. Return updated pointer into BUF. */
17333 static unsigned char invalid_eol_type[] = "(*invalid*)";
17335 static char *
17336 decode_mode_spec_coding (coding_system, buf, eol_flag)
17337 Lisp_Object coding_system;
17338 register char *buf;
17339 int eol_flag;
17341 Lisp_Object val;
17342 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17343 const unsigned char *eol_str;
17344 int eol_str_len;
17345 /* The EOL conversion we are using. */
17346 Lisp_Object eoltype;
17348 val = Fget (coding_system, Qcoding_system);
17349 eoltype = Qnil;
17351 if (!VECTORP (val)) /* Not yet decided. */
17353 if (multibyte)
17354 *buf++ = '-';
17355 if (eol_flag)
17356 eoltype = eol_mnemonic_undecided;
17357 /* Don't mention EOL conversion if it isn't decided. */
17359 else
17361 Lisp_Object eolvalue;
17363 eolvalue = Fget (coding_system, Qeol_type);
17365 if (multibyte)
17366 *buf++ = XFASTINT (AREF (val, 1));
17368 if (eol_flag)
17370 /* The EOL conversion that is normal on this system. */
17372 if (NILP (eolvalue)) /* Not yet decided. */
17373 eoltype = eol_mnemonic_undecided;
17374 else if (VECTORP (eolvalue)) /* Not yet decided. */
17375 eoltype = eol_mnemonic_undecided;
17376 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17377 eoltype = (XFASTINT (eolvalue) == 0
17378 ? eol_mnemonic_unix
17379 : (XFASTINT (eolvalue) == 1
17380 ? eol_mnemonic_dos : eol_mnemonic_mac));
17384 if (eol_flag)
17386 /* Mention the EOL conversion if it is not the usual one. */
17387 if (STRINGP (eoltype))
17389 eol_str = SDATA (eoltype);
17390 eol_str_len = SBYTES (eoltype);
17392 else if (INTEGERP (eoltype)
17393 && CHAR_VALID_P (XINT (eoltype), 0))
17395 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17396 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17397 eol_str = tmp;
17399 else
17401 eol_str = invalid_eol_type;
17402 eol_str_len = sizeof (invalid_eol_type) - 1;
17404 bcopy (eol_str, buf, eol_str_len);
17405 buf += eol_str_len;
17408 return buf;
17411 /* Return a string for the output of a mode line %-spec for window W,
17412 generated by character C. PRECISION >= 0 means don't return a
17413 string longer than that value. FIELD_WIDTH > 0 means pad the
17414 string returned with spaces to that value. Return 1 in *MULTIBYTE
17415 if the result is multibyte text.
17417 Note we operate on the current buffer for most purposes,
17418 the exception being w->base_line_pos. */
17420 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17422 static char *
17423 decode_mode_spec (w, c, field_width, precision, multibyte)
17424 struct window *w;
17425 register int c;
17426 int field_width, precision;
17427 int *multibyte;
17429 Lisp_Object obj;
17430 struct frame *f = XFRAME (WINDOW_FRAME (w));
17431 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17432 struct buffer *b = current_buffer;
17434 obj = Qnil;
17435 *multibyte = 0;
17437 switch (c)
17439 case '*':
17440 if (!NILP (b->read_only))
17441 return "%";
17442 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17443 return "*";
17444 return "-";
17446 case '+':
17447 /* This differs from %* only for a modified read-only buffer. */
17448 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17449 return "*";
17450 if (!NILP (b->read_only))
17451 return "%";
17452 return "-";
17454 case '&':
17455 /* This differs from %* in ignoring read-only-ness. */
17456 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17457 return "*";
17458 return "-";
17460 case '%':
17461 return "%";
17463 case '[':
17465 int i;
17466 char *p;
17468 if (command_loop_level > 5)
17469 return "[[[... ";
17470 p = decode_mode_spec_buf;
17471 for (i = 0; i < command_loop_level; i++)
17472 *p++ = '[';
17473 *p = 0;
17474 return decode_mode_spec_buf;
17477 case ']':
17479 int i;
17480 char *p;
17482 if (command_loop_level > 5)
17483 return " ...]]]";
17484 p = decode_mode_spec_buf;
17485 for (i = 0; i < command_loop_level; i++)
17486 *p++ = ']';
17487 *p = 0;
17488 return decode_mode_spec_buf;
17491 case '-':
17493 register int i;
17495 /* Let lots_of_dashes be a string of infinite length. */
17496 if (mode_line_target == MODE_LINE_NOPROP ||
17497 mode_line_target == MODE_LINE_STRING)
17498 return "--";
17499 if (field_width <= 0
17500 || field_width > sizeof (lots_of_dashes))
17502 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17503 decode_mode_spec_buf[i] = '-';
17504 decode_mode_spec_buf[i] = '\0';
17505 return decode_mode_spec_buf;
17507 else
17508 return lots_of_dashes;
17511 case 'b':
17512 obj = b->name;
17513 break;
17515 case 'c':
17517 int col = (int) current_column (); /* iftc */
17518 w->column_number_displayed = make_number (col);
17519 pint2str (decode_mode_spec_buf, field_width, col);
17520 return decode_mode_spec_buf;
17523 case 'e':
17524 #ifndef SYSTEM_MALLOC
17526 if (NILP (Vmemory_full))
17527 return "";
17528 else
17529 return "!MEM FULL! ";
17531 #else
17532 return "";
17533 #endif
17535 case 'F':
17536 /* %F displays the frame name. */
17537 if (!NILP (f->title))
17538 return (char *) SDATA (f->title);
17539 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17540 return (char *) SDATA (f->name);
17541 return "Emacs";
17543 case 'f':
17544 obj = b->filename;
17545 break;
17547 case 'i':
17549 int size = ZV - BEGV;
17550 pint2str (decode_mode_spec_buf, field_width, size);
17551 return decode_mode_spec_buf;
17554 case 'I':
17556 int size = ZV - BEGV;
17557 pint2hrstr (decode_mode_spec_buf, field_width, size);
17558 return decode_mode_spec_buf;
17561 case 'l':
17563 int startpos = XMARKER (w->start)->charpos;
17564 int startpos_byte = marker_byte_position (w->start);
17565 int line, linepos, linepos_byte, topline;
17566 int nlines, junk;
17567 int height = WINDOW_TOTAL_LINES (w);
17569 /* If we decided that this buffer isn't suitable for line numbers,
17570 don't forget that too fast. */
17571 if (EQ (w->base_line_pos, w->buffer))
17572 goto no_value;
17573 /* But do forget it, if the window shows a different buffer now. */
17574 else if (BUFFERP (w->base_line_pos))
17575 w->base_line_pos = Qnil;
17577 /* If the buffer is very big, don't waste time. */
17578 if (INTEGERP (Vline_number_display_limit)
17579 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17581 w->base_line_pos = Qnil;
17582 w->base_line_number = Qnil;
17583 goto no_value;
17586 if (!NILP (w->base_line_number)
17587 && !NILP (w->base_line_pos)
17588 && XFASTINT (w->base_line_pos) <= startpos)
17590 line = XFASTINT (w->base_line_number);
17591 linepos = XFASTINT (w->base_line_pos);
17592 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17594 else
17596 line = 1;
17597 linepos = BUF_BEGV (b);
17598 linepos_byte = BUF_BEGV_BYTE (b);
17601 /* Count lines from base line to window start position. */
17602 nlines = display_count_lines (linepos, linepos_byte,
17603 startpos_byte,
17604 startpos, &junk);
17606 topline = nlines + line;
17608 /* Determine a new base line, if the old one is too close
17609 or too far away, or if we did not have one.
17610 "Too close" means it's plausible a scroll-down would
17611 go back past it. */
17612 if (startpos == BUF_BEGV (b))
17614 w->base_line_number = make_number (topline);
17615 w->base_line_pos = make_number (BUF_BEGV (b));
17617 else if (nlines < height + 25 || nlines > height * 3 + 50
17618 || linepos == BUF_BEGV (b))
17620 int limit = BUF_BEGV (b);
17621 int limit_byte = BUF_BEGV_BYTE (b);
17622 int position;
17623 int distance = (height * 2 + 30) * line_number_display_limit_width;
17625 if (startpos - distance > limit)
17627 limit = startpos - distance;
17628 limit_byte = CHAR_TO_BYTE (limit);
17631 nlines = display_count_lines (startpos, startpos_byte,
17632 limit_byte,
17633 - (height * 2 + 30),
17634 &position);
17635 /* If we couldn't find the lines we wanted within
17636 line_number_display_limit_width chars per line,
17637 give up on line numbers for this window. */
17638 if (position == limit_byte && limit == startpos - distance)
17640 w->base_line_pos = w->buffer;
17641 w->base_line_number = Qnil;
17642 goto no_value;
17645 w->base_line_number = make_number (topline - nlines);
17646 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17649 /* Now count lines from the start pos to point. */
17650 nlines = display_count_lines (startpos, startpos_byte,
17651 PT_BYTE, PT, &junk);
17653 /* Record that we did display the line number. */
17654 line_number_displayed = 1;
17656 /* Make the string to show. */
17657 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17658 return decode_mode_spec_buf;
17659 no_value:
17661 char* p = decode_mode_spec_buf;
17662 int pad = field_width - 2;
17663 while (pad-- > 0)
17664 *p++ = ' ';
17665 *p++ = '?';
17666 *p++ = '?';
17667 *p = '\0';
17668 return decode_mode_spec_buf;
17671 break;
17673 case 'm':
17674 obj = b->mode_name;
17675 break;
17677 case 'n':
17678 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17679 return " Narrow";
17680 break;
17682 case 'p':
17684 int pos = marker_position (w->start);
17685 int total = BUF_ZV (b) - BUF_BEGV (b);
17687 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17689 if (pos <= BUF_BEGV (b))
17690 return "All";
17691 else
17692 return "Bottom";
17694 else if (pos <= BUF_BEGV (b))
17695 return "Top";
17696 else
17698 if (total > 1000000)
17699 /* Do it differently for a large value, to avoid overflow. */
17700 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17701 else
17702 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17703 /* We can't normally display a 3-digit number,
17704 so get us a 2-digit number that is close. */
17705 if (total == 100)
17706 total = 99;
17707 sprintf (decode_mode_spec_buf, "%2d%%", total);
17708 return decode_mode_spec_buf;
17712 /* Display percentage of size above the bottom of the screen. */
17713 case 'P':
17715 int toppos = marker_position (w->start);
17716 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17717 int total = BUF_ZV (b) - BUF_BEGV (b);
17719 if (botpos >= BUF_ZV (b))
17721 if (toppos <= BUF_BEGV (b))
17722 return "All";
17723 else
17724 return "Bottom";
17726 else
17728 if (total > 1000000)
17729 /* Do it differently for a large value, to avoid overflow. */
17730 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17731 else
17732 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17733 /* We can't normally display a 3-digit number,
17734 so get us a 2-digit number that is close. */
17735 if (total == 100)
17736 total = 99;
17737 if (toppos <= BUF_BEGV (b))
17738 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17739 else
17740 sprintf (decode_mode_spec_buf, "%2d%%", total);
17741 return decode_mode_spec_buf;
17745 case 's':
17746 /* status of process */
17747 obj = Fget_buffer_process (Fcurrent_buffer ());
17748 if (NILP (obj))
17749 return "no process";
17750 #ifdef subprocesses
17751 obj = Fsymbol_name (Fprocess_status (obj));
17752 #endif
17753 break;
17755 case 't': /* indicate TEXT or BINARY */
17756 #ifdef MODE_LINE_BINARY_TEXT
17757 return MODE_LINE_BINARY_TEXT (b);
17758 #else
17759 return "T";
17760 #endif
17762 case 'z':
17763 /* coding-system (not including end-of-line format) */
17764 case 'Z':
17765 /* coding-system (including end-of-line type) */
17767 int eol_flag = (c == 'Z');
17768 char *p = decode_mode_spec_buf;
17770 if (! FRAME_WINDOW_P (f))
17772 /* No need to mention EOL here--the terminal never needs
17773 to do EOL conversion. */
17774 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17775 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17777 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17778 p, eol_flag);
17780 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17781 #ifdef subprocesses
17782 obj = Fget_buffer_process (Fcurrent_buffer ());
17783 if (PROCESSP (obj))
17785 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17786 p, eol_flag);
17787 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17788 p, eol_flag);
17790 #endif /* subprocesses */
17791 #endif /* 0 */
17792 *p = 0;
17793 return decode_mode_spec_buf;
17797 if (STRINGP (obj))
17799 *multibyte = STRING_MULTIBYTE (obj);
17800 return (char *) SDATA (obj);
17802 else
17803 return "";
17807 /* Count up to COUNT lines starting from START / START_BYTE.
17808 But don't go beyond LIMIT_BYTE.
17809 Return the number of lines thus found (always nonnegative).
17811 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17813 static int
17814 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17815 int start, start_byte, limit_byte, count;
17816 int *byte_pos_ptr;
17818 register unsigned char *cursor;
17819 unsigned char *base;
17821 register int ceiling;
17822 register unsigned char *ceiling_addr;
17823 int orig_count = count;
17825 /* If we are not in selective display mode,
17826 check only for newlines. */
17827 int selective_display = (!NILP (current_buffer->selective_display)
17828 && !INTEGERP (current_buffer->selective_display));
17830 if (count > 0)
17832 while (start_byte < limit_byte)
17834 ceiling = BUFFER_CEILING_OF (start_byte);
17835 ceiling = min (limit_byte - 1, ceiling);
17836 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17837 base = (cursor = BYTE_POS_ADDR (start_byte));
17838 while (1)
17840 if (selective_display)
17841 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17843 else
17844 while (*cursor != '\n' && ++cursor != ceiling_addr)
17847 if (cursor != ceiling_addr)
17849 if (--count == 0)
17851 start_byte += cursor - base + 1;
17852 *byte_pos_ptr = start_byte;
17853 return orig_count;
17855 else
17856 if (++cursor == ceiling_addr)
17857 break;
17859 else
17860 break;
17862 start_byte += cursor - base;
17865 else
17867 while (start_byte > limit_byte)
17869 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17870 ceiling = max (limit_byte, ceiling);
17871 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17872 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17873 while (1)
17875 if (selective_display)
17876 while (--cursor != ceiling_addr
17877 && *cursor != '\n' && *cursor != 015)
17879 else
17880 while (--cursor != ceiling_addr && *cursor != '\n')
17883 if (cursor != ceiling_addr)
17885 if (++count == 0)
17887 start_byte += cursor - base + 1;
17888 *byte_pos_ptr = start_byte;
17889 /* When scanning backwards, we should
17890 not count the newline posterior to which we stop. */
17891 return - orig_count - 1;
17894 else
17895 break;
17897 /* Here we add 1 to compensate for the last decrement
17898 of CURSOR, which took it past the valid range. */
17899 start_byte += cursor - base + 1;
17903 *byte_pos_ptr = limit_byte;
17905 if (count < 0)
17906 return - orig_count + count;
17907 return orig_count - count;
17913 /***********************************************************************
17914 Displaying strings
17915 ***********************************************************************/
17917 /* Display a NUL-terminated string, starting with index START.
17919 If STRING is non-null, display that C string. Otherwise, the Lisp
17920 string LISP_STRING is displayed.
17922 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17923 FACE_STRING. Display STRING or LISP_STRING with the face at
17924 FACE_STRING_POS in FACE_STRING:
17926 Display the string in the environment given by IT, but use the
17927 standard display table, temporarily.
17929 FIELD_WIDTH is the minimum number of output glyphs to produce.
17930 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17931 with spaces. If STRING has more characters, more than FIELD_WIDTH
17932 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17934 PRECISION is the maximum number of characters to output from
17935 STRING. PRECISION < 0 means don't truncate the string.
17937 This is roughly equivalent to printf format specifiers:
17939 FIELD_WIDTH PRECISION PRINTF
17940 ----------------------------------------
17941 -1 -1 %s
17942 -1 10 %.10s
17943 10 -1 %10s
17944 20 10 %20.10s
17946 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17947 display them, and < 0 means obey the current buffer's value of
17948 enable_multibyte_characters.
17950 Value is the number of columns displayed. */
17952 static int
17953 display_string (string, lisp_string, face_string, face_string_pos,
17954 start, it, field_width, precision, max_x, multibyte)
17955 unsigned char *string;
17956 Lisp_Object lisp_string;
17957 Lisp_Object face_string;
17958 int face_string_pos;
17959 int start;
17960 struct it *it;
17961 int field_width, precision, max_x;
17962 int multibyte;
17964 int hpos_at_start = it->hpos;
17965 int saved_face_id = it->face_id;
17966 struct glyph_row *row = it->glyph_row;
17968 /* Initialize the iterator IT for iteration over STRING beginning
17969 with index START. */
17970 reseat_to_string (it, string, lisp_string, start,
17971 precision, field_width, multibyte);
17973 /* If displaying STRING, set up the face of the iterator
17974 from LISP_STRING, if that's given. */
17975 if (STRINGP (face_string))
17977 int endptr;
17978 struct face *face;
17980 it->face_id
17981 = face_at_string_position (it->w, face_string, face_string_pos,
17982 0, it->region_beg_charpos,
17983 it->region_end_charpos,
17984 &endptr, it->base_face_id, 0);
17985 face = FACE_FROM_ID (it->f, it->face_id);
17986 it->face_box_p = face->box != FACE_NO_BOX;
17989 /* Set max_x to the maximum allowed X position. Don't let it go
17990 beyond the right edge of the window. */
17991 if (max_x <= 0)
17992 max_x = it->last_visible_x;
17993 else
17994 max_x = min (max_x, it->last_visible_x);
17996 /* Skip over display elements that are not visible. because IT->w is
17997 hscrolled. */
17998 if (it->current_x < it->first_visible_x)
17999 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18000 MOVE_TO_POS | MOVE_TO_X);
18002 row->ascent = it->max_ascent;
18003 row->height = it->max_ascent + it->max_descent;
18004 row->phys_ascent = it->max_phys_ascent;
18005 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18006 row->extra_line_spacing = it->max_extra_line_spacing;
18008 /* This condition is for the case that we are called with current_x
18009 past last_visible_x. */
18010 while (it->current_x < max_x)
18012 int x_before, x, n_glyphs_before, i, nglyphs;
18014 /* Get the next display element. */
18015 if (!get_next_display_element (it))
18016 break;
18018 /* Produce glyphs. */
18019 x_before = it->current_x;
18020 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18021 PRODUCE_GLYPHS (it);
18023 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18024 i = 0;
18025 x = x_before;
18026 while (i < nglyphs)
18028 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18030 if (!it->truncate_lines_p
18031 && x + glyph->pixel_width > max_x)
18033 /* End of continued line or max_x reached. */
18034 if (CHAR_GLYPH_PADDING_P (*glyph))
18036 /* A wide character is unbreakable. */
18037 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18038 it->current_x = x_before;
18040 else
18042 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18043 it->current_x = x;
18045 break;
18047 else if (x + glyph->pixel_width > it->first_visible_x)
18049 /* Glyph is at least partially visible. */
18050 ++it->hpos;
18051 if (x < it->first_visible_x)
18052 it->glyph_row->x = x - it->first_visible_x;
18054 else
18056 /* Glyph is off the left margin of the display area.
18057 Should not happen. */
18058 abort ();
18061 row->ascent = max (row->ascent, it->max_ascent);
18062 row->height = max (row->height, it->max_ascent + it->max_descent);
18063 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18064 row->phys_height = max (row->phys_height,
18065 it->max_phys_ascent + it->max_phys_descent);
18066 row->extra_line_spacing = max (row->extra_line_spacing,
18067 it->max_extra_line_spacing);
18068 x += glyph->pixel_width;
18069 ++i;
18072 /* Stop if max_x reached. */
18073 if (i < nglyphs)
18074 break;
18076 /* Stop at line ends. */
18077 if (ITERATOR_AT_END_OF_LINE_P (it))
18079 it->continuation_lines_width = 0;
18080 break;
18083 set_iterator_to_next (it, 1);
18085 /* Stop if truncating at the right edge. */
18086 if (it->truncate_lines_p
18087 && it->current_x >= it->last_visible_x)
18089 /* Add truncation mark, but don't do it if the line is
18090 truncated at a padding space. */
18091 if (IT_CHARPOS (*it) < it->string_nchars)
18093 if (!FRAME_WINDOW_P (it->f))
18095 int i, n;
18097 if (it->current_x > it->last_visible_x)
18099 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18100 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18101 break;
18102 for (n = row->used[TEXT_AREA]; i < n; ++i)
18104 row->used[TEXT_AREA] = i;
18105 produce_special_glyphs (it, IT_TRUNCATION);
18108 produce_special_glyphs (it, IT_TRUNCATION);
18110 it->glyph_row->truncated_on_right_p = 1;
18112 break;
18116 /* Maybe insert a truncation at the left. */
18117 if (it->first_visible_x
18118 && IT_CHARPOS (*it) > 0)
18120 if (!FRAME_WINDOW_P (it->f))
18121 insert_left_trunc_glyphs (it);
18122 it->glyph_row->truncated_on_left_p = 1;
18125 it->face_id = saved_face_id;
18127 /* Value is number of columns displayed. */
18128 return it->hpos - hpos_at_start;
18133 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18134 appears as an element of LIST or as the car of an element of LIST.
18135 If PROPVAL is a list, compare each element against LIST in that
18136 way, and return 1/2 if any element of PROPVAL is found in LIST.
18137 Otherwise return 0. This function cannot quit.
18138 The return value is 2 if the text is invisible but with an ellipsis
18139 and 1 if it's invisible and without an ellipsis. */
18142 invisible_p (propval, list)
18143 register Lisp_Object propval;
18144 Lisp_Object list;
18146 register Lisp_Object tail, proptail;
18148 for (tail = list; CONSP (tail); tail = XCDR (tail))
18150 register Lisp_Object tem;
18151 tem = XCAR (tail);
18152 if (EQ (propval, tem))
18153 return 1;
18154 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18155 return NILP (XCDR (tem)) ? 1 : 2;
18158 if (CONSP (propval))
18160 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18162 Lisp_Object propelt;
18163 propelt = XCAR (proptail);
18164 for (tail = list; CONSP (tail); tail = XCDR (tail))
18166 register Lisp_Object tem;
18167 tem = XCAR (tail);
18168 if (EQ (propelt, tem))
18169 return 1;
18170 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18171 return NILP (XCDR (tem)) ? 1 : 2;
18176 return 0;
18179 /* Calculate a width or height in pixels from a specification using
18180 the following elements:
18182 SPEC ::=
18183 NUM - a (fractional) multiple of the default font width/height
18184 (NUM) - specifies exactly NUM pixels
18185 UNIT - a fixed number of pixels, see below.
18186 ELEMENT - size of a display element in pixels, see below.
18187 (NUM . SPEC) - equals NUM * SPEC
18188 (+ SPEC SPEC ...) - add pixel values
18189 (- SPEC SPEC ...) - subtract pixel values
18190 (- SPEC) - negate pixel value
18192 NUM ::=
18193 INT or FLOAT - a number constant
18194 SYMBOL - use symbol's (buffer local) variable binding.
18196 UNIT ::=
18197 in - pixels per inch *)
18198 mm - pixels per 1/1000 meter *)
18199 cm - pixels per 1/100 meter *)
18200 width - width of current font in pixels.
18201 height - height of current font in pixels.
18203 *) using the ratio(s) defined in display-pixels-per-inch.
18205 ELEMENT ::=
18207 left-fringe - left fringe width in pixels
18208 right-fringe - right fringe width in pixels
18210 left-margin - left margin width in pixels
18211 right-margin - right margin width in pixels
18213 scroll-bar - scroll-bar area width in pixels
18215 Examples:
18217 Pixels corresponding to 5 inches:
18218 (5 . in)
18220 Total width of non-text areas on left side of window (if scroll-bar is on left):
18221 '(space :width (+ left-fringe left-margin scroll-bar))
18223 Align to first text column (in header line):
18224 '(space :align-to 0)
18226 Align to middle of text area minus half the width of variable `my-image'
18227 containing a loaded image:
18228 '(space :align-to (0.5 . (- text my-image)))
18230 Width of left margin minus width of 1 character in the default font:
18231 '(space :width (- left-margin 1))
18233 Width of left margin minus width of 2 characters in the current font:
18234 '(space :width (- left-margin (2 . width)))
18236 Center 1 character over left-margin (in header line):
18237 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18239 Different ways to express width of left fringe plus left margin minus one pixel:
18240 '(space :width (- (+ left-fringe left-margin) (1)))
18241 '(space :width (+ left-fringe left-margin (- (1))))
18242 '(space :width (+ left-fringe left-margin (-1)))
18246 #define NUMVAL(X) \
18247 ((INTEGERP (X) || FLOATP (X)) \
18248 ? XFLOATINT (X) \
18249 : - 1)
18252 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18253 double *res;
18254 struct it *it;
18255 Lisp_Object prop;
18256 void *font;
18257 int width_p, *align_to;
18259 double pixels;
18261 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18262 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18264 if (NILP (prop))
18265 return OK_PIXELS (0);
18267 if (SYMBOLP (prop))
18269 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18271 char *unit = SDATA (SYMBOL_NAME (prop));
18273 if (unit[0] == 'i' && unit[1] == 'n')
18274 pixels = 1.0;
18275 else if (unit[0] == 'm' && unit[1] == 'm')
18276 pixels = 25.4;
18277 else if (unit[0] == 'c' && unit[1] == 'm')
18278 pixels = 2.54;
18279 else
18280 pixels = 0;
18281 if (pixels > 0)
18283 double ppi;
18284 #ifdef HAVE_WINDOW_SYSTEM
18285 if (FRAME_WINDOW_P (it->f)
18286 && (ppi = (width_p
18287 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18288 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18289 ppi > 0))
18290 return OK_PIXELS (ppi / pixels);
18291 #endif
18293 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18294 || (CONSP (Vdisplay_pixels_per_inch)
18295 && (ppi = (width_p
18296 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18297 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18298 ppi > 0)))
18299 return OK_PIXELS (ppi / pixels);
18301 return 0;
18305 #ifdef HAVE_WINDOW_SYSTEM
18306 if (EQ (prop, Qheight))
18307 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18308 if (EQ (prop, Qwidth))
18309 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18310 #else
18311 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18312 return OK_PIXELS (1);
18313 #endif
18315 if (EQ (prop, Qtext))
18316 return OK_PIXELS (width_p
18317 ? window_box_width (it->w, TEXT_AREA)
18318 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18320 if (align_to && *align_to < 0)
18322 *res = 0;
18323 if (EQ (prop, Qleft))
18324 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18325 if (EQ (prop, Qright))
18326 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18327 if (EQ (prop, Qcenter))
18328 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18329 + window_box_width (it->w, TEXT_AREA) / 2);
18330 if (EQ (prop, Qleft_fringe))
18331 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18332 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18333 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18334 if (EQ (prop, Qright_fringe))
18335 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18336 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18337 : window_box_right_offset (it->w, TEXT_AREA));
18338 if (EQ (prop, Qleft_margin))
18339 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18340 if (EQ (prop, Qright_margin))
18341 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18342 if (EQ (prop, Qscroll_bar))
18343 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18345 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18346 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18347 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18348 : 0)));
18350 else
18352 if (EQ (prop, Qleft_fringe))
18353 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18354 if (EQ (prop, Qright_fringe))
18355 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18356 if (EQ (prop, Qleft_margin))
18357 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18358 if (EQ (prop, Qright_margin))
18359 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18360 if (EQ (prop, Qscroll_bar))
18361 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18364 prop = Fbuffer_local_value (prop, it->w->buffer);
18367 if (INTEGERP (prop) || FLOATP (prop))
18369 int base_unit = (width_p
18370 ? FRAME_COLUMN_WIDTH (it->f)
18371 : FRAME_LINE_HEIGHT (it->f));
18372 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18375 if (CONSP (prop))
18377 Lisp_Object car = XCAR (prop);
18378 Lisp_Object cdr = XCDR (prop);
18380 if (SYMBOLP (car))
18382 #ifdef HAVE_WINDOW_SYSTEM
18383 if (valid_image_p (prop))
18385 int id = lookup_image (it->f, prop);
18386 struct image *img = IMAGE_FROM_ID (it->f, id);
18388 return OK_PIXELS (width_p ? img->width : img->height);
18390 #endif
18391 if (EQ (car, Qplus) || EQ (car, Qminus))
18393 int first = 1;
18394 double px;
18396 pixels = 0;
18397 while (CONSP (cdr))
18399 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18400 font, width_p, align_to))
18401 return 0;
18402 if (first)
18403 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18404 else
18405 pixels += px;
18406 cdr = XCDR (cdr);
18408 if (EQ (car, Qminus))
18409 pixels = -pixels;
18410 return OK_PIXELS (pixels);
18413 car = Fbuffer_local_value (car, it->w->buffer);
18416 if (INTEGERP (car) || FLOATP (car))
18418 double fact;
18419 pixels = XFLOATINT (car);
18420 if (NILP (cdr))
18421 return OK_PIXELS (pixels);
18422 if (calc_pixel_width_or_height (&fact, it, cdr,
18423 font, width_p, align_to))
18424 return OK_PIXELS (pixels * fact);
18425 return 0;
18428 return 0;
18431 return 0;
18435 /***********************************************************************
18436 Glyph Display
18437 ***********************************************************************/
18439 #ifdef HAVE_WINDOW_SYSTEM
18441 #if GLYPH_DEBUG
18443 void
18444 dump_glyph_string (s)
18445 struct glyph_string *s;
18447 fprintf (stderr, "glyph string\n");
18448 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18449 s->x, s->y, s->width, s->height);
18450 fprintf (stderr, " ybase = %d\n", s->ybase);
18451 fprintf (stderr, " hl = %d\n", s->hl);
18452 fprintf (stderr, " left overhang = %d, right = %d\n",
18453 s->left_overhang, s->right_overhang);
18454 fprintf (stderr, " nchars = %d\n", s->nchars);
18455 fprintf (stderr, " extends to end of line = %d\n",
18456 s->extends_to_end_of_line_p);
18457 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18458 fprintf (stderr, " bg width = %d\n", s->background_width);
18461 #endif /* GLYPH_DEBUG */
18463 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18464 of XChar2b structures for S; it can't be allocated in
18465 init_glyph_string because it must be allocated via `alloca'. W
18466 is the window on which S is drawn. ROW and AREA are the glyph row
18467 and area within the row from which S is constructed. START is the
18468 index of the first glyph structure covered by S. HL is a
18469 face-override for drawing S. */
18471 #ifdef HAVE_NTGUI
18472 #define OPTIONAL_HDC(hdc) hdc,
18473 #define DECLARE_HDC(hdc) HDC hdc;
18474 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18475 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18476 #endif
18478 #ifndef OPTIONAL_HDC
18479 #define OPTIONAL_HDC(hdc)
18480 #define DECLARE_HDC(hdc)
18481 #define ALLOCATE_HDC(hdc, f)
18482 #define RELEASE_HDC(hdc, f)
18483 #endif
18485 static void
18486 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18487 struct glyph_string *s;
18488 DECLARE_HDC (hdc)
18489 XChar2b *char2b;
18490 struct window *w;
18491 struct glyph_row *row;
18492 enum glyph_row_area area;
18493 int start;
18494 enum draw_glyphs_face hl;
18496 bzero (s, sizeof *s);
18497 s->w = w;
18498 s->f = XFRAME (w->frame);
18499 #ifdef HAVE_NTGUI
18500 s->hdc = hdc;
18501 #endif
18502 s->display = FRAME_X_DISPLAY (s->f);
18503 s->window = FRAME_X_WINDOW (s->f);
18504 s->char2b = char2b;
18505 s->hl = hl;
18506 s->row = row;
18507 s->area = area;
18508 s->first_glyph = row->glyphs[area] + start;
18509 s->height = row->height;
18510 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18512 /* Display the internal border below the tool-bar window. */
18513 if (WINDOWP (s->f->tool_bar_window)
18514 && s->w == XWINDOW (s->f->tool_bar_window))
18515 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18517 s->ybase = s->y + row->ascent;
18521 /* Append the list of glyph strings with head H and tail T to the list
18522 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18524 static INLINE void
18525 append_glyph_string_lists (head, tail, h, t)
18526 struct glyph_string **head, **tail;
18527 struct glyph_string *h, *t;
18529 if (h)
18531 if (*head)
18532 (*tail)->next = h;
18533 else
18534 *head = h;
18535 h->prev = *tail;
18536 *tail = t;
18541 /* Prepend the list of glyph strings with head H and tail T to the
18542 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18543 result. */
18545 static INLINE void
18546 prepend_glyph_string_lists (head, tail, h, t)
18547 struct glyph_string **head, **tail;
18548 struct glyph_string *h, *t;
18550 if (h)
18552 if (*head)
18553 (*head)->prev = t;
18554 else
18555 *tail = t;
18556 t->next = *head;
18557 *head = h;
18562 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18563 Set *HEAD and *TAIL to the resulting list. */
18565 static INLINE void
18566 append_glyph_string (head, tail, s)
18567 struct glyph_string **head, **tail;
18568 struct glyph_string *s;
18570 s->next = s->prev = NULL;
18571 append_glyph_string_lists (head, tail, s, s);
18575 /* Get face and two-byte form of character glyph GLYPH on frame F.
18576 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18577 a pointer to a realized face that is ready for display. */
18579 static INLINE struct face *
18580 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18581 struct frame *f;
18582 struct glyph *glyph;
18583 XChar2b *char2b;
18584 int *two_byte_p;
18586 struct face *face;
18588 xassert (glyph->type == CHAR_GLYPH);
18589 face = FACE_FROM_ID (f, glyph->face_id);
18591 if (two_byte_p)
18592 *two_byte_p = 0;
18594 if (!glyph->multibyte_p)
18596 /* Unibyte case. We don't have to encode, but we have to make
18597 sure to use a face suitable for unibyte. */
18598 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18600 else if (glyph->u.ch < 128)
18602 /* Case of ASCII in a face known to fit ASCII. */
18603 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18605 else
18607 int c1, c2, charset;
18609 /* Split characters into bytes. If c2 is -1 afterwards, C is
18610 really a one-byte character so that byte1 is zero. */
18611 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18612 if (c2 > 0)
18613 STORE_XCHAR2B (char2b, c1, c2);
18614 else
18615 STORE_XCHAR2B (char2b, 0, c1);
18617 /* Maybe encode the character in *CHAR2B. */
18618 if (charset != CHARSET_ASCII)
18620 struct font_info *font_info
18621 = FONT_INFO_FROM_ID (f, face->font_info_id);
18622 if (font_info)
18623 glyph->font_type
18624 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18628 /* Make sure X resources of the face are allocated. */
18629 xassert (face != NULL);
18630 PREPARE_FACE_FOR_DISPLAY (f, face);
18631 return face;
18635 /* Fill glyph string S with composition components specified by S->cmp.
18637 FACES is an array of faces for all components of this composition.
18638 S->gidx is the index of the first component for S.
18640 OVERLAPS non-zero means S should draw the foreground only, and use
18641 its physical height for clipping. See also draw_glyphs.
18643 Value is the index of a component not in S. */
18645 static int
18646 fill_composite_glyph_string (s, faces, overlaps)
18647 struct glyph_string *s;
18648 struct face **faces;
18649 int overlaps;
18651 int i;
18653 xassert (s);
18655 s->for_overlaps = overlaps;
18657 s->face = faces[s->gidx];
18658 s->font = s->face->font;
18659 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18661 /* For all glyphs of this composition, starting at the offset
18662 S->gidx, until we reach the end of the definition or encounter a
18663 glyph that requires the different face, add it to S. */
18664 ++s->nchars;
18665 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18666 ++s->nchars;
18668 /* All glyph strings for the same composition has the same width,
18669 i.e. the width set for the first component of the composition. */
18671 s->width = s->first_glyph->pixel_width;
18673 /* If the specified font could not be loaded, use the frame's
18674 default font, but record the fact that we couldn't load it in
18675 the glyph string so that we can draw rectangles for the
18676 characters of the glyph string. */
18677 if (s->font == NULL)
18679 s->font_not_found_p = 1;
18680 s->font = FRAME_FONT (s->f);
18683 /* Adjust base line for subscript/superscript text. */
18684 s->ybase += s->first_glyph->voffset;
18686 xassert (s->face && s->face->gc);
18688 /* This glyph string must always be drawn with 16-bit functions. */
18689 s->two_byte_p = 1;
18691 return s->gidx + s->nchars;
18695 /* Fill glyph string S from a sequence of character glyphs.
18697 FACE_ID is the face id of the string. START is the index of the
18698 first glyph to consider, END is the index of the last + 1.
18699 OVERLAPS non-zero means S should draw the foreground only, and use
18700 its physical height for clipping. See also draw_glyphs.
18702 Value is the index of the first glyph not in S. */
18704 static int
18705 fill_glyph_string (s, face_id, start, end, overlaps)
18706 struct glyph_string *s;
18707 int face_id;
18708 int start, end, overlaps;
18710 struct glyph *glyph, *last;
18711 int voffset;
18712 int glyph_not_available_p;
18714 xassert (s->f == XFRAME (s->w->frame));
18715 xassert (s->nchars == 0);
18716 xassert (start >= 0 && end > start);
18718 s->for_overlaps = overlaps,
18719 glyph = s->row->glyphs[s->area] + start;
18720 last = s->row->glyphs[s->area] + end;
18721 voffset = glyph->voffset;
18723 glyph_not_available_p = glyph->glyph_not_available_p;
18725 while (glyph < last
18726 && glyph->type == CHAR_GLYPH
18727 && glyph->voffset == voffset
18728 /* Same face id implies same font, nowadays. */
18729 && glyph->face_id == face_id
18730 && glyph->glyph_not_available_p == glyph_not_available_p)
18732 int two_byte_p;
18734 s->face = get_glyph_face_and_encoding (s->f, glyph,
18735 s->char2b + s->nchars,
18736 &two_byte_p);
18737 s->two_byte_p = two_byte_p;
18738 ++s->nchars;
18739 xassert (s->nchars <= end - start);
18740 s->width += glyph->pixel_width;
18741 ++glyph;
18744 s->font = s->face->font;
18745 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18747 /* If the specified font could not be loaded, use the frame's font,
18748 but record the fact that we couldn't load it in
18749 S->font_not_found_p so that we can draw rectangles for the
18750 characters of the glyph string. */
18751 if (s->font == NULL || glyph_not_available_p)
18753 s->font_not_found_p = 1;
18754 s->font = FRAME_FONT (s->f);
18757 /* Adjust base line for subscript/superscript text. */
18758 s->ybase += voffset;
18760 xassert (s->face && s->face->gc);
18761 return glyph - s->row->glyphs[s->area];
18765 /* Fill glyph string S from image glyph S->first_glyph. */
18767 static void
18768 fill_image_glyph_string (s)
18769 struct glyph_string *s;
18771 xassert (s->first_glyph->type == IMAGE_GLYPH);
18772 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18773 xassert (s->img);
18774 s->slice = s->first_glyph->slice;
18775 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18776 s->font = s->face->font;
18777 s->width = s->first_glyph->pixel_width;
18779 /* Adjust base line for subscript/superscript text. */
18780 s->ybase += s->first_glyph->voffset;
18784 /* Fill glyph string S from a sequence of stretch glyphs.
18786 ROW is the glyph row in which the glyphs are found, AREA is the
18787 area within the row. START is the index of the first glyph to
18788 consider, END is the index of the last + 1.
18790 Value is the index of the first glyph not in S. */
18792 static int
18793 fill_stretch_glyph_string (s, row, area, start, end)
18794 struct glyph_string *s;
18795 struct glyph_row *row;
18796 enum glyph_row_area area;
18797 int start, end;
18799 struct glyph *glyph, *last;
18800 int voffset, face_id;
18802 xassert (s->first_glyph->type == STRETCH_GLYPH);
18804 glyph = s->row->glyphs[s->area] + start;
18805 last = s->row->glyphs[s->area] + end;
18806 face_id = glyph->face_id;
18807 s->face = FACE_FROM_ID (s->f, face_id);
18808 s->font = s->face->font;
18809 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18810 s->width = glyph->pixel_width;
18811 s->nchars = 1;
18812 voffset = glyph->voffset;
18814 for (++glyph;
18815 (glyph < last
18816 && glyph->type == STRETCH_GLYPH
18817 && glyph->voffset == voffset
18818 && glyph->face_id == face_id);
18819 ++glyph)
18820 s->width += glyph->pixel_width;
18822 /* Adjust base line for subscript/superscript text. */
18823 s->ybase += voffset;
18825 /* The case that face->gc == 0 is handled when drawing the glyph
18826 string by calling PREPARE_FACE_FOR_DISPLAY. */
18827 xassert (s->face);
18828 return glyph - s->row->glyphs[s->area];
18832 /* EXPORT for RIF:
18833 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18834 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18835 assumed to be zero. */
18837 void
18838 x_get_glyph_overhangs (glyph, f, left, right)
18839 struct glyph *glyph;
18840 struct frame *f;
18841 int *left, *right;
18843 *left = *right = 0;
18845 if (glyph->type == CHAR_GLYPH)
18847 XFontStruct *font;
18848 struct face *face;
18849 struct font_info *font_info;
18850 XChar2b char2b;
18851 XCharStruct *pcm;
18853 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18854 font = face->font;
18855 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18856 if (font /* ++KFS: Should this be font_info ? */
18857 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18859 if (pcm->rbearing > pcm->width)
18860 *right = pcm->rbearing - pcm->width;
18861 if (pcm->lbearing < 0)
18862 *left = -pcm->lbearing;
18868 /* Return the index of the first glyph preceding glyph string S that
18869 is overwritten by S because of S's left overhang. Value is -1
18870 if no glyphs are overwritten. */
18872 static int
18873 left_overwritten (s)
18874 struct glyph_string *s;
18876 int k;
18878 if (s->left_overhang)
18880 int x = 0, i;
18881 struct glyph *glyphs = s->row->glyphs[s->area];
18882 int first = s->first_glyph - glyphs;
18884 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18885 x -= glyphs[i].pixel_width;
18887 k = i + 1;
18889 else
18890 k = -1;
18892 return k;
18896 /* Return the index of the first glyph preceding glyph string S that
18897 is overwriting S because of its right overhang. Value is -1 if no
18898 glyph in front of S overwrites S. */
18900 static int
18901 left_overwriting (s)
18902 struct glyph_string *s;
18904 int i, k, x;
18905 struct glyph *glyphs = s->row->glyphs[s->area];
18906 int first = s->first_glyph - glyphs;
18908 k = -1;
18909 x = 0;
18910 for (i = first - 1; i >= 0; --i)
18912 int left, right;
18913 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18914 if (x + right > 0)
18915 k = i;
18916 x -= glyphs[i].pixel_width;
18919 return k;
18923 /* Return the index of the last glyph following glyph string S that is
18924 not overwritten by S because of S's right overhang. Value is -1 if
18925 no such glyph is found. */
18927 static int
18928 right_overwritten (s)
18929 struct glyph_string *s;
18931 int k = -1;
18933 if (s->right_overhang)
18935 int x = 0, i;
18936 struct glyph *glyphs = s->row->glyphs[s->area];
18937 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18938 int end = s->row->used[s->area];
18940 for (i = first; i < end && s->right_overhang > x; ++i)
18941 x += glyphs[i].pixel_width;
18943 k = i;
18946 return k;
18950 /* Return the index of the last glyph following glyph string S that
18951 overwrites S because of its left overhang. Value is negative
18952 if no such glyph is found. */
18954 static int
18955 right_overwriting (s)
18956 struct glyph_string *s;
18958 int i, k, x;
18959 int end = s->row->used[s->area];
18960 struct glyph *glyphs = s->row->glyphs[s->area];
18961 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18963 k = -1;
18964 x = 0;
18965 for (i = first; i < end; ++i)
18967 int left, right;
18968 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18969 if (x - left < 0)
18970 k = i;
18971 x += glyphs[i].pixel_width;
18974 return k;
18978 /* Get face and two-byte form of character C in face FACE_ID on frame
18979 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18980 means we want to display multibyte text. DISPLAY_P non-zero means
18981 make sure that X resources for the face returned are allocated.
18982 Value is a pointer to a realized face that is ready for display if
18983 DISPLAY_P is non-zero. */
18985 static INLINE struct face *
18986 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18987 struct frame *f;
18988 int c, face_id;
18989 XChar2b *char2b;
18990 int multibyte_p, display_p;
18992 struct face *face = FACE_FROM_ID (f, face_id);
18994 if (!multibyte_p)
18996 /* Unibyte case. We don't have to encode, but we have to make
18997 sure to use a face suitable for unibyte. */
18998 STORE_XCHAR2B (char2b, 0, c);
18999 face_id = FACE_FOR_CHAR (f, face, c);
19000 face = FACE_FROM_ID (f, face_id);
19002 else if (c < 128)
19004 /* Case of ASCII in a face known to fit ASCII. */
19005 STORE_XCHAR2B (char2b, 0, c);
19007 else
19009 int c1, c2, charset;
19011 /* Split characters into bytes. If c2 is -1 afterwards, C is
19012 really a one-byte character so that byte1 is zero. */
19013 SPLIT_CHAR (c, charset, c1, c2);
19014 if (c2 > 0)
19015 STORE_XCHAR2B (char2b, c1, c2);
19016 else
19017 STORE_XCHAR2B (char2b, 0, c1);
19019 /* Maybe encode the character in *CHAR2B. */
19020 if (face->font != NULL)
19022 struct font_info *font_info
19023 = FONT_INFO_FROM_ID (f, face->font_info_id);
19024 if (font_info)
19025 rif->encode_char (c, char2b, font_info, 0);
19029 /* Make sure X resources of the face are allocated. */
19030 #ifdef HAVE_X_WINDOWS
19031 if (display_p)
19032 #endif
19034 xassert (face != NULL);
19035 PREPARE_FACE_FOR_DISPLAY (f, face);
19038 return face;
19042 /* Set background width of glyph string S. START is the index of the
19043 first glyph following S. LAST_X is the right-most x-position + 1
19044 in the drawing area. */
19046 static INLINE void
19047 set_glyph_string_background_width (s, start, last_x)
19048 struct glyph_string *s;
19049 int start;
19050 int last_x;
19052 /* If the face of this glyph string has to be drawn to the end of
19053 the drawing area, set S->extends_to_end_of_line_p. */
19055 if (start == s->row->used[s->area]
19056 && s->area == TEXT_AREA
19057 && ((s->row->fill_line_p
19058 && (s->hl == DRAW_NORMAL_TEXT
19059 || s->hl == DRAW_IMAGE_RAISED
19060 || s->hl == DRAW_IMAGE_SUNKEN))
19061 || s->hl == DRAW_MOUSE_FACE))
19062 s->extends_to_end_of_line_p = 1;
19064 /* If S extends its face to the end of the line, set its
19065 background_width to the distance to the right edge of the drawing
19066 area. */
19067 if (s->extends_to_end_of_line_p)
19068 s->background_width = last_x - s->x + 1;
19069 else
19070 s->background_width = s->width;
19074 /* Compute overhangs and x-positions for glyph string S and its
19075 predecessors, or successors. X is the starting x-position for S.
19076 BACKWARD_P non-zero means process predecessors. */
19078 static void
19079 compute_overhangs_and_x (s, x, backward_p)
19080 struct glyph_string *s;
19081 int x;
19082 int backward_p;
19084 if (backward_p)
19086 while (s)
19088 if (rif->compute_glyph_string_overhangs)
19089 rif->compute_glyph_string_overhangs (s);
19090 x -= s->width;
19091 s->x = x;
19092 s = s->prev;
19095 else
19097 while (s)
19099 if (rif->compute_glyph_string_overhangs)
19100 rif->compute_glyph_string_overhangs (s);
19101 s->x = x;
19102 x += s->width;
19103 s = s->next;
19110 /* The following macros are only called from draw_glyphs below.
19111 They reference the following parameters of that function directly:
19112 `w', `row', `area', and `overlap_p'
19113 as well as the following local variables:
19114 `s', `f', and `hdc' (in W32) */
19116 #ifdef HAVE_NTGUI
19117 /* On W32, silently add local `hdc' variable to argument list of
19118 init_glyph_string. */
19119 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19120 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19121 #else
19122 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19123 init_glyph_string (s, char2b, w, row, area, start, hl)
19124 #endif
19126 /* Add a glyph string for a stretch glyph to the list of strings
19127 between HEAD and TAIL. START is the index of the stretch glyph in
19128 row area AREA of glyph row ROW. END is the index of the last glyph
19129 in that glyph row area. X is the current output position assigned
19130 to the new glyph string constructed. HL overrides that face of the
19131 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19132 is the right-most x-position of the drawing area. */
19134 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19135 and below -- keep them on one line. */
19136 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19137 do \
19139 s = (struct glyph_string *) alloca (sizeof *s); \
19140 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19141 START = fill_stretch_glyph_string (s, row, area, START, END); \
19142 append_glyph_string (&HEAD, &TAIL, s); \
19143 s->x = (X); \
19145 while (0)
19148 /* Add a glyph string for an image glyph to the list of strings
19149 between HEAD and TAIL. START is the index of the image glyph in
19150 row area AREA of glyph row ROW. END is the index of the last glyph
19151 in that glyph row area. X is the current output position assigned
19152 to the new glyph string constructed. HL overrides that face of the
19153 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19154 is the right-most x-position of the drawing area. */
19156 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19157 do \
19159 s = (struct glyph_string *) alloca (sizeof *s); \
19160 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19161 fill_image_glyph_string (s); \
19162 append_glyph_string (&HEAD, &TAIL, s); \
19163 ++START; \
19164 s->x = (X); \
19166 while (0)
19169 /* Add a glyph string for a sequence of character glyphs to the list
19170 of strings between HEAD and TAIL. START is the index of the first
19171 glyph in row area AREA of glyph row ROW that is part of the new
19172 glyph string. END is the index of the last glyph in that glyph row
19173 area. X is the current output position assigned to the new glyph
19174 string constructed. HL overrides that face of the glyph; e.g. it
19175 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19176 right-most x-position of the drawing area. */
19178 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19179 do \
19181 int c, face_id; \
19182 XChar2b *char2b; \
19184 c = (row)->glyphs[area][START].u.ch; \
19185 face_id = (row)->glyphs[area][START].face_id; \
19187 s = (struct glyph_string *) alloca (sizeof *s); \
19188 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19189 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19190 append_glyph_string (&HEAD, &TAIL, s); \
19191 s->x = (X); \
19192 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19194 while (0)
19197 /* Add a glyph string for a composite sequence to the list of strings
19198 between HEAD and TAIL. START is the index of the first glyph in
19199 row area AREA of glyph row ROW that is part of the new glyph
19200 string. END is the index of the last glyph in that glyph row area.
19201 X is the current output position assigned to the new glyph string
19202 constructed. HL overrides that face of the glyph; e.g. it is
19203 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19204 x-position of the drawing area. */
19206 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19207 do { \
19208 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19209 int face_id = (row)->glyphs[area][START].face_id; \
19210 struct face *base_face = FACE_FROM_ID (f, face_id); \
19211 struct composition *cmp = composition_table[cmp_id]; \
19212 int glyph_len = cmp->glyph_len; \
19213 XChar2b *char2b; \
19214 struct face **faces; \
19215 struct glyph_string *first_s = NULL; \
19216 int n; \
19218 base_face = base_face->ascii_face; \
19219 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19220 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19221 /* At first, fill in `char2b' and `faces'. */ \
19222 for (n = 0; n < glyph_len; n++) \
19224 int c = COMPOSITION_GLYPH (cmp, n); \
19225 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19226 faces[n] = FACE_FROM_ID (f, this_face_id); \
19227 get_char_face_and_encoding (f, c, this_face_id, \
19228 char2b + n, 1, 1); \
19231 /* Make glyph_strings for each glyph sequence that is drawable by \
19232 the same face, and append them to HEAD/TAIL. */ \
19233 for (n = 0; n < cmp->glyph_len;) \
19235 s = (struct glyph_string *) alloca (sizeof *s); \
19236 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19237 append_glyph_string (&(HEAD), &(TAIL), s); \
19238 s->cmp = cmp; \
19239 s->gidx = n; \
19240 s->x = (X); \
19242 if (n == 0) \
19243 first_s = s; \
19245 n = fill_composite_glyph_string (s, faces, overlaps); \
19248 ++START; \
19249 s = first_s; \
19250 } while (0)
19253 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19254 of AREA of glyph row ROW on window W between indices START and END.
19255 HL overrides the face for drawing glyph strings, e.g. it is
19256 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19257 x-positions of the drawing area.
19259 This is an ugly monster macro construct because we must use alloca
19260 to allocate glyph strings (because draw_glyphs can be called
19261 asynchronously). */
19263 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19264 do \
19266 HEAD = TAIL = NULL; \
19267 while (START < END) \
19269 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19270 switch (first_glyph->type) \
19272 case CHAR_GLYPH: \
19273 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19274 HL, X, LAST_X); \
19275 break; \
19277 case COMPOSITE_GLYPH: \
19278 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19279 HL, X, LAST_X); \
19280 break; \
19282 case STRETCH_GLYPH: \
19283 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19284 HL, X, LAST_X); \
19285 break; \
19287 case IMAGE_GLYPH: \
19288 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19289 HL, X, LAST_X); \
19290 break; \
19292 default: \
19293 abort (); \
19296 set_glyph_string_background_width (s, START, LAST_X); \
19297 (X) += s->width; \
19300 while (0)
19303 /* Draw glyphs between START and END in AREA of ROW on window W,
19304 starting at x-position X. X is relative to AREA in W. HL is a
19305 face-override with the following meaning:
19307 DRAW_NORMAL_TEXT draw normally
19308 DRAW_CURSOR draw in cursor face
19309 DRAW_MOUSE_FACE draw in mouse face.
19310 DRAW_INVERSE_VIDEO draw in mode line face
19311 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19312 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19314 If OVERLAPS is non-zero, draw only the foreground of characters and
19315 clip to the physical height of ROW. Non-zero value also defines
19316 the overlapping part to be drawn:
19318 OVERLAPS_PRED overlap with preceding rows
19319 OVERLAPS_SUCC overlap with succeeding rows
19320 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19321 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19323 Value is the x-position reached, relative to AREA of W. */
19325 static int
19326 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19327 struct window *w;
19328 int x;
19329 struct glyph_row *row;
19330 enum glyph_row_area area;
19331 int start, end;
19332 enum draw_glyphs_face hl;
19333 int overlaps;
19335 struct glyph_string *head, *tail;
19336 struct glyph_string *s;
19337 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19338 int last_x, area_width;
19339 int x_reached;
19340 int i, j;
19341 struct frame *f = XFRAME (WINDOW_FRAME (w));
19342 DECLARE_HDC (hdc);
19344 ALLOCATE_HDC (hdc, f);
19346 /* Let's rather be paranoid than getting a SEGV. */
19347 end = min (end, row->used[area]);
19348 start = max (0, start);
19349 start = min (end, start);
19351 /* Translate X to frame coordinates. Set last_x to the right
19352 end of the drawing area. */
19353 if (row->full_width_p)
19355 /* X is relative to the left edge of W, without scroll bars
19356 or fringes. */
19357 x += WINDOW_LEFT_EDGE_X (w);
19358 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19360 else
19362 int area_left = window_box_left (w, area);
19363 x += area_left;
19364 area_width = window_box_width (w, area);
19365 last_x = area_left + area_width;
19368 /* Build a doubly-linked list of glyph_string structures between
19369 head and tail from what we have to draw. Note that the macro
19370 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19371 the reason we use a separate variable `i'. */
19372 i = start;
19373 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19374 if (tail)
19375 x_reached = tail->x + tail->background_width;
19376 else
19377 x_reached = x;
19379 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19380 the row, redraw some glyphs in front or following the glyph
19381 strings built above. */
19382 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19384 int dummy_x = 0;
19385 struct glyph_string *h, *t;
19387 /* Compute overhangs for all glyph strings. */
19388 if (rif->compute_glyph_string_overhangs)
19389 for (s = head; s; s = s->next)
19390 rif->compute_glyph_string_overhangs (s);
19392 /* Prepend glyph strings for glyphs in front of the first glyph
19393 string that are overwritten because of the first glyph
19394 string's left overhang. The background of all strings
19395 prepended must be drawn because the first glyph string
19396 draws over it. */
19397 i = left_overwritten (head);
19398 if (i >= 0)
19400 j = i;
19401 BUILD_GLYPH_STRINGS (j, start, h, t,
19402 DRAW_NORMAL_TEXT, dummy_x, last_x);
19403 start = i;
19404 compute_overhangs_and_x (t, head->x, 1);
19405 prepend_glyph_string_lists (&head, &tail, h, t);
19406 clip_head = head;
19409 /* Prepend glyph strings for glyphs in front of the first glyph
19410 string that overwrite that glyph string because of their
19411 right overhang. For these strings, only the foreground must
19412 be drawn, because it draws over the glyph string at `head'.
19413 The background must not be drawn because this would overwrite
19414 right overhangs of preceding glyphs for which no glyph
19415 strings exist. */
19416 i = left_overwriting (head);
19417 if (i >= 0)
19419 clip_head = head;
19420 BUILD_GLYPH_STRINGS (i, start, h, t,
19421 DRAW_NORMAL_TEXT, dummy_x, last_x);
19422 for (s = h; s; s = s->next)
19423 s->background_filled_p = 1;
19424 compute_overhangs_and_x (t, head->x, 1);
19425 prepend_glyph_string_lists (&head, &tail, h, t);
19428 /* Append glyphs strings for glyphs following the last glyph
19429 string tail that are overwritten by tail. The background of
19430 these strings has to be drawn because tail's foreground draws
19431 over it. */
19432 i = right_overwritten (tail);
19433 if (i >= 0)
19435 BUILD_GLYPH_STRINGS (end, i, h, t,
19436 DRAW_NORMAL_TEXT, x, last_x);
19437 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19438 append_glyph_string_lists (&head, &tail, h, t);
19439 clip_tail = tail;
19442 /* Append glyph strings for glyphs following the last glyph
19443 string tail that overwrite tail. The foreground of such
19444 glyphs has to be drawn because it writes into the background
19445 of tail. The background must not be drawn because it could
19446 paint over the foreground of following glyphs. */
19447 i = right_overwriting (tail);
19448 if (i >= 0)
19450 clip_tail = tail;
19451 BUILD_GLYPH_STRINGS (end, i, h, t,
19452 DRAW_NORMAL_TEXT, x, last_x);
19453 for (s = h; s; s = s->next)
19454 s->background_filled_p = 1;
19455 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19456 append_glyph_string_lists (&head, &tail, h, t);
19458 if (clip_head || clip_tail)
19459 for (s = head; s; s = s->next)
19461 s->clip_head = clip_head;
19462 s->clip_tail = clip_tail;
19466 /* Draw all strings. */
19467 for (s = head; s; s = s->next)
19468 rif->draw_glyph_string (s);
19470 if (area == TEXT_AREA
19471 && !row->full_width_p
19472 /* When drawing overlapping rows, only the glyph strings'
19473 foreground is drawn, which doesn't erase a cursor
19474 completely. */
19475 && !overlaps)
19477 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19478 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19479 : (tail ? tail->x + tail->background_width : x));
19481 int text_left = window_box_left (w, TEXT_AREA);
19482 x0 -= text_left;
19483 x1 -= text_left;
19485 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19486 row->y, MATRIX_ROW_BOTTOM_Y (row));
19489 /* Value is the x-position up to which drawn, relative to AREA of W.
19490 This doesn't include parts drawn because of overhangs. */
19491 if (row->full_width_p)
19492 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19493 else
19494 x_reached -= window_box_left (w, area);
19496 RELEASE_HDC (hdc, f);
19498 return x_reached;
19501 /* Expand row matrix if too narrow. Don't expand if area
19502 is not present. */
19504 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19506 if (!fonts_changed_p \
19507 && (it->glyph_row->glyphs[area] \
19508 < it->glyph_row->glyphs[area + 1])) \
19510 it->w->ncols_scale_factor++; \
19511 fonts_changed_p = 1; \
19515 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19516 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19518 static INLINE void
19519 append_glyph (it)
19520 struct it *it;
19522 struct glyph *glyph;
19523 enum glyph_row_area area = it->area;
19525 xassert (it->glyph_row);
19526 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19528 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19529 if (glyph < it->glyph_row->glyphs[area + 1])
19531 glyph->charpos = CHARPOS (it->position);
19532 glyph->object = it->object;
19533 glyph->pixel_width = it->pixel_width;
19534 glyph->ascent = it->ascent;
19535 glyph->descent = it->descent;
19536 glyph->voffset = it->voffset;
19537 glyph->type = CHAR_GLYPH;
19538 glyph->multibyte_p = it->multibyte_p;
19539 glyph->left_box_line_p = it->start_of_box_run_p;
19540 glyph->right_box_line_p = it->end_of_box_run_p;
19541 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19542 || it->phys_descent > it->descent);
19543 glyph->padding_p = 0;
19544 glyph->glyph_not_available_p = it->glyph_not_available_p;
19545 glyph->face_id = it->face_id;
19546 glyph->u.ch = it->char_to_display;
19547 glyph->slice = null_glyph_slice;
19548 glyph->font_type = FONT_TYPE_UNKNOWN;
19549 ++it->glyph_row->used[area];
19551 else
19552 IT_EXPAND_MATRIX_WIDTH (it, area);
19555 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19556 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19558 static INLINE void
19559 append_composite_glyph (it)
19560 struct it *it;
19562 struct glyph *glyph;
19563 enum glyph_row_area area = it->area;
19565 xassert (it->glyph_row);
19567 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19568 if (glyph < it->glyph_row->glyphs[area + 1])
19570 glyph->charpos = CHARPOS (it->position);
19571 glyph->object = it->object;
19572 glyph->pixel_width = it->pixel_width;
19573 glyph->ascent = it->ascent;
19574 glyph->descent = it->descent;
19575 glyph->voffset = it->voffset;
19576 glyph->type = COMPOSITE_GLYPH;
19577 glyph->multibyte_p = it->multibyte_p;
19578 glyph->left_box_line_p = it->start_of_box_run_p;
19579 glyph->right_box_line_p = it->end_of_box_run_p;
19580 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19581 || it->phys_descent > it->descent);
19582 glyph->padding_p = 0;
19583 glyph->glyph_not_available_p = 0;
19584 glyph->face_id = it->face_id;
19585 glyph->u.cmp_id = it->cmp_id;
19586 glyph->slice = null_glyph_slice;
19587 glyph->font_type = FONT_TYPE_UNKNOWN;
19588 ++it->glyph_row->used[area];
19590 else
19591 IT_EXPAND_MATRIX_WIDTH (it, area);
19595 /* Change IT->ascent and IT->height according to the setting of
19596 IT->voffset. */
19598 static INLINE void
19599 take_vertical_position_into_account (it)
19600 struct it *it;
19602 if (it->voffset)
19604 if (it->voffset < 0)
19605 /* Increase the ascent so that we can display the text higher
19606 in the line. */
19607 it->ascent -= it->voffset;
19608 else
19609 /* Increase the descent so that we can display the text lower
19610 in the line. */
19611 it->descent += it->voffset;
19616 /* Produce glyphs/get display metrics for the image IT is loaded with.
19617 See the description of struct display_iterator in dispextern.h for
19618 an overview of struct display_iterator. */
19620 static void
19621 produce_image_glyph (it)
19622 struct it *it;
19624 struct image *img;
19625 struct face *face;
19626 int glyph_ascent;
19627 struct glyph_slice slice;
19629 xassert (it->what == IT_IMAGE);
19631 face = FACE_FROM_ID (it->f, it->face_id);
19632 xassert (face);
19633 /* Make sure X resources of the face is loaded. */
19634 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19636 if (it->image_id < 0)
19638 /* Fringe bitmap. */
19639 it->ascent = it->phys_ascent = 0;
19640 it->descent = it->phys_descent = 0;
19641 it->pixel_width = 0;
19642 it->nglyphs = 0;
19643 return;
19646 img = IMAGE_FROM_ID (it->f, it->image_id);
19647 xassert (img);
19648 /* Make sure X resources of the image is loaded. */
19649 prepare_image_for_display (it->f, img);
19651 slice.x = slice.y = 0;
19652 slice.width = img->width;
19653 slice.height = img->height;
19655 if (INTEGERP (it->slice.x))
19656 slice.x = XINT (it->slice.x);
19657 else if (FLOATP (it->slice.x))
19658 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19660 if (INTEGERP (it->slice.y))
19661 slice.y = XINT (it->slice.y);
19662 else if (FLOATP (it->slice.y))
19663 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19665 if (INTEGERP (it->slice.width))
19666 slice.width = XINT (it->slice.width);
19667 else if (FLOATP (it->slice.width))
19668 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19670 if (INTEGERP (it->slice.height))
19671 slice.height = XINT (it->slice.height);
19672 else if (FLOATP (it->slice.height))
19673 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19675 if (slice.x >= img->width)
19676 slice.x = img->width;
19677 if (slice.y >= img->height)
19678 slice.y = img->height;
19679 if (slice.x + slice.width >= img->width)
19680 slice.width = img->width - slice.x;
19681 if (slice.y + slice.height > img->height)
19682 slice.height = img->height - slice.y;
19684 if (slice.width == 0 || slice.height == 0)
19685 return;
19687 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19689 it->descent = slice.height - glyph_ascent;
19690 if (slice.y == 0)
19691 it->descent += img->vmargin;
19692 if (slice.y + slice.height == img->height)
19693 it->descent += img->vmargin;
19694 it->phys_descent = it->descent;
19696 it->pixel_width = slice.width;
19697 if (slice.x == 0)
19698 it->pixel_width += img->hmargin;
19699 if (slice.x + slice.width == img->width)
19700 it->pixel_width += img->hmargin;
19702 /* It's quite possible for images to have an ascent greater than
19703 their height, so don't get confused in that case. */
19704 if (it->descent < 0)
19705 it->descent = 0;
19707 #if 0 /* this breaks image tiling */
19708 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19709 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19710 if (face_ascent > it->ascent)
19711 it->ascent = it->phys_ascent = face_ascent;
19712 #endif
19714 it->nglyphs = 1;
19716 if (face->box != FACE_NO_BOX)
19718 if (face->box_line_width > 0)
19720 if (slice.y == 0)
19721 it->ascent += face->box_line_width;
19722 if (slice.y + slice.height == img->height)
19723 it->descent += face->box_line_width;
19726 if (it->start_of_box_run_p && slice.x == 0)
19727 it->pixel_width += abs (face->box_line_width);
19728 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19729 it->pixel_width += abs (face->box_line_width);
19732 take_vertical_position_into_account (it);
19734 if (it->glyph_row)
19736 struct glyph *glyph;
19737 enum glyph_row_area area = it->area;
19739 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19740 if (glyph < it->glyph_row->glyphs[area + 1])
19742 glyph->charpos = CHARPOS (it->position);
19743 glyph->object = it->object;
19744 glyph->pixel_width = it->pixel_width;
19745 glyph->ascent = glyph_ascent;
19746 glyph->descent = it->descent;
19747 glyph->voffset = it->voffset;
19748 glyph->type = IMAGE_GLYPH;
19749 glyph->multibyte_p = it->multibyte_p;
19750 glyph->left_box_line_p = it->start_of_box_run_p;
19751 glyph->right_box_line_p = it->end_of_box_run_p;
19752 glyph->overlaps_vertically_p = 0;
19753 glyph->padding_p = 0;
19754 glyph->glyph_not_available_p = 0;
19755 glyph->face_id = it->face_id;
19756 glyph->u.img_id = img->id;
19757 glyph->slice = slice;
19758 glyph->font_type = FONT_TYPE_UNKNOWN;
19759 ++it->glyph_row->used[area];
19761 else
19762 IT_EXPAND_MATRIX_WIDTH (it, area);
19767 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19768 of the glyph, WIDTH and HEIGHT are the width and height of the
19769 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19771 static void
19772 append_stretch_glyph (it, object, width, height, ascent)
19773 struct it *it;
19774 Lisp_Object object;
19775 int width, height;
19776 int ascent;
19778 struct glyph *glyph;
19779 enum glyph_row_area area = it->area;
19781 xassert (ascent >= 0 && ascent <= height);
19783 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19784 if (glyph < it->glyph_row->glyphs[area + 1])
19786 glyph->charpos = CHARPOS (it->position);
19787 glyph->object = object;
19788 glyph->pixel_width = width;
19789 glyph->ascent = ascent;
19790 glyph->descent = height - ascent;
19791 glyph->voffset = it->voffset;
19792 glyph->type = STRETCH_GLYPH;
19793 glyph->multibyte_p = it->multibyte_p;
19794 glyph->left_box_line_p = it->start_of_box_run_p;
19795 glyph->right_box_line_p = it->end_of_box_run_p;
19796 glyph->overlaps_vertically_p = 0;
19797 glyph->padding_p = 0;
19798 glyph->glyph_not_available_p = 0;
19799 glyph->face_id = it->face_id;
19800 glyph->u.stretch.ascent = ascent;
19801 glyph->u.stretch.height = height;
19802 glyph->slice = null_glyph_slice;
19803 glyph->font_type = FONT_TYPE_UNKNOWN;
19804 ++it->glyph_row->used[area];
19806 else
19807 IT_EXPAND_MATRIX_WIDTH (it, area);
19811 /* Produce a stretch glyph for iterator IT. IT->object is the value
19812 of the glyph property displayed. The value must be a list
19813 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19814 being recognized:
19816 1. `:width WIDTH' specifies that the space should be WIDTH *
19817 canonical char width wide. WIDTH may be an integer or floating
19818 point number.
19820 2. `:relative-width FACTOR' specifies that the width of the stretch
19821 should be computed from the width of the first character having the
19822 `glyph' property, and should be FACTOR times that width.
19824 3. `:align-to HPOS' specifies that the space should be wide enough
19825 to reach HPOS, a value in canonical character units.
19827 Exactly one of the above pairs must be present.
19829 4. `:height HEIGHT' specifies that the height of the stretch produced
19830 should be HEIGHT, measured in canonical character units.
19832 5. `:relative-height FACTOR' specifies that the height of the
19833 stretch should be FACTOR times the height of the characters having
19834 the glyph property.
19836 Either none or exactly one of 4 or 5 must be present.
19838 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19839 of the stretch should be used for the ascent of the stretch.
19840 ASCENT must be in the range 0 <= ASCENT <= 100. */
19842 static void
19843 produce_stretch_glyph (it)
19844 struct it *it;
19846 /* (space :width WIDTH :height HEIGHT ...) */
19847 Lisp_Object prop, plist;
19848 int width = 0, height = 0, align_to = -1;
19849 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19850 int ascent = 0;
19851 double tem;
19852 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19853 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19855 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19857 /* List should start with `space'. */
19858 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19859 plist = XCDR (it->object);
19861 /* Compute the width of the stretch. */
19862 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19863 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19865 /* Absolute width `:width WIDTH' specified and valid. */
19866 zero_width_ok_p = 1;
19867 width = (int)tem;
19869 else if (prop = Fplist_get (plist, QCrelative_width),
19870 NUMVAL (prop) > 0)
19872 /* Relative width `:relative-width FACTOR' specified and valid.
19873 Compute the width of the characters having the `glyph'
19874 property. */
19875 struct it it2;
19876 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19878 it2 = *it;
19879 if (it->multibyte_p)
19881 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19882 - IT_BYTEPOS (*it));
19883 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19885 else
19886 it2.c = *p, it2.len = 1;
19888 it2.glyph_row = NULL;
19889 it2.what = IT_CHARACTER;
19890 x_produce_glyphs (&it2);
19891 width = NUMVAL (prop) * it2.pixel_width;
19893 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19894 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19896 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19897 align_to = (align_to < 0
19899 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19900 else if (align_to < 0)
19901 align_to = window_box_left_offset (it->w, TEXT_AREA);
19902 width = max (0, (int)tem + align_to - it->current_x);
19903 zero_width_ok_p = 1;
19905 else
19906 /* Nothing specified -> width defaults to canonical char width. */
19907 width = FRAME_COLUMN_WIDTH (it->f);
19909 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19910 width = 1;
19912 /* Compute height. */
19913 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19914 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19916 height = (int)tem;
19917 zero_height_ok_p = 1;
19919 else if (prop = Fplist_get (plist, QCrelative_height),
19920 NUMVAL (prop) > 0)
19921 height = FONT_HEIGHT (font) * NUMVAL (prop);
19922 else
19923 height = FONT_HEIGHT (font);
19925 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19926 height = 1;
19928 /* Compute percentage of height used for ascent. If
19929 `:ascent ASCENT' is present and valid, use that. Otherwise,
19930 derive the ascent from the font in use. */
19931 if (prop = Fplist_get (plist, QCascent),
19932 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19933 ascent = height * NUMVAL (prop) / 100.0;
19934 else if (!NILP (prop)
19935 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19936 ascent = min (max (0, (int)tem), height);
19937 else
19938 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19940 if (width > 0 && !it->truncate_lines_p
19941 && it->current_x + width > it->last_visible_x)
19942 width = it->last_visible_x - it->current_x - 1;
19944 if (width > 0 && height > 0 && it->glyph_row)
19946 Lisp_Object object = it->stack[it->sp - 1].string;
19947 if (!STRINGP (object))
19948 object = it->w->buffer;
19949 append_stretch_glyph (it, object, width, height, ascent);
19952 it->pixel_width = width;
19953 it->ascent = it->phys_ascent = ascent;
19954 it->descent = it->phys_descent = height - it->ascent;
19955 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19957 take_vertical_position_into_account (it);
19960 /* Get line-height and line-spacing property at point.
19961 If line-height has format (HEIGHT TOTAL), return TOTAL
19962 in TOTAL_HEIGHT. */
19964 static Lisp_Object
19965 get_line_height_property (it, prop)
19966 struct it *it;
19967 Lisp_Object prop;
19969 Lisp_Object position;
19971 if (STRINGP (it->object))
19972 position = make_number (IT_STRING_CHARPOS (*it));
19973 else if (BUFFERP (it->object))
19974 position = make_number (IT_CHARPOS (*it));
19975 else
19976 return Qnil;
19978 return Fget_char_property (position, prop, it->object);
19981 /* Calculate line-height and line-spacing properties.
19982 An integer value specifies explicit pixel value.
19983 A float value specifies relative value to current face height.
19984 A cons (float . face-name) specifies relative value to
19985 height of specified face font.
19987 Returns height in pixels, or nil. */
19990 static Lisp_Object
19991 calc_line_height_property (it, val, font, boff, override)
19992 struct it *it;
19993 Lisp_Object val;
19994 XFontStruct *font;
19995 int boff, override;
19997 Lisp_Object face_name = Qnil;
19998 int ascent, descent, height;
20000 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20001 return val;
20003 if (CONSP (val))
20005 face_name = XCAR (val);
20006 val = XCDR (val);
20007 if (!NUMBERP (val))
20008 val = make_number (1);
20009 if (NILP (face_name))
20011 height = it->ascent + it->descent;
20012 goto scale;
20016 if (NILP (face_name))
20018 font = FRAME_FONT (it->f);
20019 boff = FRAME_BASELINE_OFFSET (it->f);
20021 else if (EQ (face_name, Qt))
20023 override = 0;
20025 else
20027 int face_id;
20028 struct face *face;
20029 struct font_info *font_info;
20031 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20032 if (face_id < 0)
20033 return make_number (-1);
20035 face = FACE_FROM_ID (it->f, face_id);
20036 font = face->font;
20037 if (font == NULL)
20038 return make_number (-1);
20040 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20041 boff = font_info->baseline_offset;
20042 if (font_info->vertical_centering)
20043 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20046 ascent = FONT_BASE (font) + boff;
20047 descent = FONT_DESCENT (font) - boff;
20049 if (override)
20051 it->override_ascent = ascent;
20052 it->override_descent = descent;
20053 it->override_boff = boff;
20056 height = ascent + descent;
20058 scale:
20059 if (FLOATP (val))
20060 height = (int)(XFLOAT_DATA (val) * height);
20061 else if (INTEGERP (val))
20062 height *= XINT (val);
20064 return make_number (height);
20068 /* RIF:
20069 Produce glyphs/get display metrics for the display element IT is
20070 loaded with. See the description of struct it in dispextern.h
20071 for an overview of struct it. */
20073 void
20074 x_produce_glyphs (it)
20075 struct it *it;
20077 int extra_line_spacing = it->extra_line_spacing;
20079 it->glyph_not_available_p = 0;
20081 if (it->what == IT_CHARACTER)
20083 XChar2b char2b;
20084 XFontStruct *font;
20085 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20086 XCharStruct *pcm;
20087 int font_not_found_p;
20088 struct font_info *font_info;
20089 int boff; /* baseline offset */
20090 /* We may change it->multibyte_p upon unibyte<->multibyte
20091 conversion. So, save the current value now and restore it
20092 later.
20094 Note: It seems that we don't have to record multibyte_p in
20095 struct glyph because the character code itself tells if or
20096 not the character is multibyte. Thus, in the future, we must
20097 consider eliminating the field `multibyte_p' in the struct
20098 glyph. */
20099 int saved_multibyte_p = it->multibyte_p;
20101 /* Maybe translate single-byte characters to multibyte, or the
20102 other way. */
20103 it->char_to_display = it->c;
20104 if (!ASCII_BYTE_P (it->c))
20106 if (unibyte_display_via_language_environment
20107 && SINGLE_BYTE_CHAR_P (it->c)
20108 && (it->c >= 0240
20109 || !NILP (Vnonascii_translation_table)))
20111 it->char_to_display = unibyte_char_to_multibyte (it->c);
20112 it->multibyte_p = 1;
20113 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20114 face = FACE_FROM_ID (it->f, it->face_id);
20116 else if (!SINGLE_BYTE_CHAR_P (it->c)
20117 && !it->multibyte_p)
20119 it->multibyte_p = 1;
20120 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20121 face = FACE_FROM_ID (it->f, it->face_id);
20125 /* Get font to use. Encode IT->char_to_display. */
20126 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20127 &char2b, it->multibyte_p, 0);
20128 font = face->font;
20130 /* When no suitable font found, use the default font. */
20131 font_not_found_p = font == NULL;
20132 if (font_not_found_p)
20134 font = FRAME_FONT (it->f);
20135 boff = FRAME_BASELINE_OFFSET (it->f);
20136 font_info = NULL;
20138 else
20140 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20141 boff = font_info->baseline_offset;
20142 if (font_info->vertical_centering)
20143 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20146 if (it->char_to_display >= ' '
20147 && (!it->multibyte_p || it->char_to_display < 128))
20149 /* Either unibyte or ASCII. */
20150 int stretched_p;
20152 it->nglyphs = 1;
20154 pcm = rif->per_char_metric (font, &char2b,
20155 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20157 if (it->override_ascent >= 0)
20159 it->ascent = it->override_ascent;
20160 it->descent = it->override_descent;
20161 boff = it->override_boff;
20163 else
20165 it->ascent = FONT_BASE (font) + boff;
20166 it->descent = FONT_DESCENT (font) - boff;
20169 if (pcm)
20171 it->phys_ascent = pcm->ascent + boff;
20172 it->phys_descent = pcm->descent - boff;
20173 it->pixel_width = pcm->width;
20175 else
20177 it->glyph_not_available_p = 1;
20178 it->phys_ascent = it->ascent;
20179 it->phys_descent = it->descent;
20180 it->pixel_width = FONT_WIDTH (font);
20183 if (it->constrain_row_ascent_descent_p)
20185 if (it->descent > it->max_descent)
20187 it->ascent += it->descent - it->max_descent;
20188 it->descent = it->max_descent;
20190 if (it->ascent > it->max_ascent)
20192 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20193 it->ascent = it->max_ascent;
20195 it->phys_ascent = min (it->phys_ascent, it->ascent);
20196 it->phys_descent = min (it->phys_descent, it->descent);
20197 extra_line_spacing = 0;
20200 /* If this is a space inside a region of text with
20201 `space-width' property, change its width. */
20202 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20203 if (stretched_p)
20204 it->pixel_width *= XFLOATINT (it->space_width);
20206 /* If face has a box, add the box thickness to the character
20207 height. If character has a box line to the left and/or
20208 right, add the box line width to the character's width. */
20209 if (face->box != FACE_NO_BOX)
20211 int thick = face->box_line_width;
20213 if (thick > 0)
20215 it->ascent += thick;
20216 it->descent += thick;
20218 else
20219 thick = -thick;
20221 if (it->start_of_box_run_p)
20222 it->pixel_width += thick;
20223 if (it->end_of_box_run_p)
20224 it->pixel_width += thick;
20227 /* If face has an overline, add the height of the overline
20228 (1 pixel) and a 1 pixel margin to the character height. */
20229 if (face->overline_p)
20230 it->ascent += 2;
20232 if (it->constrain_row_ascent_descent_p)
20234 if (it->ascent > it->max_ascent)
20235 it->ascent = it->max_ascent;
20236 if (it->descent > it->max_descent)
20237 it->descent = it->max_descent;
20240 take_vertical_position_into_account (it);
20242 /* If we have to actually produce glyphs, do it. */
20243 if (it->glyph_row)
20245 if (stretched_p)
20247 /* Translate a space with a `space-width' property
20248 into a stretch glyph. */
20249 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20250 / FONT_HEIGHT (font));
20251 append_stretch_glyph (it, it->object, it->pixel_width,
20252 it->ascent + it->descent, ascent);
20254 else
20255 append_glyph (it);
20257 /* If characters with lbearing or rbearing are displayed
20258 in this line, record that fact in a flag of the
20259 glyph row. This is used to optimize X output code. */
20260 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20261 it->glyph_row->contains_overlapping_glyphs_p = 1;
20264 else if (it->char_to_display == '\n')
20266 /* A newline has no width but we need the height of the line.
20267 But if previous part of the line set a height, don't
20268 increase that height */
20270 Lisp_Object height;
20271 Lisp_Object total_height = Qnil;
20273 it->override_ascent = -1;
20274 it->pixel_width = 0;
20275 it->nglyphs = 0;
20277 height = get_line_height_property(it, Qline_height);
20278 /* Split (line-height total-height) list */
20279 if (CONSP (height)
20280 && CONSP (XCDR (height))
20281 && NILP (XCDR (XCDR (height))))
20283 total_height = XCAR (XCDR (height));
20284 height = XCAR (height);
20286 height = calc_line_height_property(it, height, font, boff, 1);
20288 if (it->override_ascent >= 0)
20290 it->ascent = it->override_ascent;
20291 it->descent = it->override_descent;
20292 boff = it->override_boff;
20294 else
20296 it->ascent = FONT_BASE (font) + boff;
20297 it->descent = FONT_DESCENT (font) - boff;
20300 if (EQ (height, Qt))
20302 if (it->descent > it->max_descent)
20304 it->ascent += it->descent - it->max_descent;
20305 it->descent = it->max_descent;
20307 if (it->ascent > it->max_ascent)
20309 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20310 it->ascent = it->max_ascent;
20312 it->phys_ascent = min (it->phys_ascent, it->ascent);
20313 it->phys_descent = min (it->phys_descent, it->descent);
20314 it->constrain_row_ascent_descent_p = 1;
20315 extra_line_spacing = 0;
20317 else
20319 Lisp_Object spacing;
20321 it->phys_ascent = it->ascent;
20322 it->phys_descent = it->descent;
20324 if ((it->max_ascent > 0 || it->max_descent > 0)
20325 && face->box != FACE_NO_BOX
20326 && face->box_line_width > 0)
20328 it->ascent += face->box_line_width;
20329 it->descent += face->box_line_width;
20331 if (!NILP (height)
20332 && XINT (height) > it->ascent + it->descent)
20333 it->ascent = XINT (height) - it->descent;
20335 if (!NILP (total_height))
20336 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20337 else
20339 spacing = get_line_height_property(it, Qline_spacing);
20340 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20342 if (INTEGERP (spacing))
20344 extra_line_spacing = XINT (spacing);
20345 if (!NILP (total_height))
20346 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20350 else if (it->char_to_display == '\t')
20352 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20353 int x = it->current_x + it->continuation_lines_width;
20354 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20356 /* If the distance from the current position to the next tab
20357 stop is less than a space character width, use the
20358 tab stop after that. */
20359 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20360 next_tab_x += tab_width;
20362 it->pixel_width = next_tab_x - x;
20363 it->nglyphs = 1;
20364 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20365 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20367 if (it->glyph_row)
20369 append_stretch_glyph (it, it->object, it->pixel_width,
20370 it->ascent + it->descent, it->ascent);
20373 else
20375 /* A multi-byte character. Assume that the display width of the
20376 character is the width of the character multiplied by the
20377 width of the font. */
20379 /* If we found a font, this font should give us the right
20380 metrics. If we didn't find a font, use the frame's
20381 default font and calculate the width of the character
20382 from the charset width; this is what old redisplay code
20383 did. */
20385 pcm = rif->per_char_metric (font, &char2b,
20386 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20388 if (font_not_found_p || !pcm)
20390 int charset = CHAR_CHARSET (it->char_to_display);
20392 it->glyph_not_available_p = 1;
20393 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20394 * CHARSET_WIDTH (charset));
20395 it->phys_ascent = FONT_BASE (font) + boff;
20396 it->phys_descent = FONT_DESCENT (font) - boff;
20398 else
20400 it->pixel_width = pcm->width;
20401 it->phys_ascent = pcm->ascent + boff;
20402 it->phys_descent = pcm->descent - boff;
20403 if (it->glyph_row
20404 && (pcm->lbearing < 0
20405 || pcm->rbearing > pcm->width))
20406 it->glyph_row->contains_overlapping_glyphs_p = 1;
20408 it->nglyphs = 1;
20409 it->ascent = FONT_BASE (font) + boff;
20410 it->descent = FONT_DESCENT (font) - boff;
20411 if (face->box != FACE_NO_BOX)
20413 int thick = face->box_line_width;
20415 if (thick > 0)
20417 it->ascent += thick;
20418 it->descent += thick;
20420 else
20421 thick = - thick;
20423 if (it->start_of_box_run_p)
20424 it->pixel_width += thick;
20425 if (it->end_of_box_run_p)
20426 it->pixel_width += thick;
20429 /* If face has an overline, add the height of the overline
20430 (1 pixel) and a 1 pixel margin to the character height. */
20431 if (face->overline_p)
20432 it->ascent += 2;
20434 take_vertical_position_into_account (it);
20436 if (it->glyph_row)
20437 append_glyph (it);
20439 it->multibyte_p = saved_multibyte_p;
20441 else if (it->what == IT_COMPOSITION)
20443 /* Note: A composition is represented as one glyph in the
20444 glyph matrix. There are no padding glyphs. */
20445 XChar2b char2b;
20446 XFontStruct *font;
20447 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20448 XCharStruct *pcm;
20449 int font_not_found_p;
20450 struct font_info *font_info;
20451 int boff; /* baseline offset */
20452 struct composition *cmp = composition_table[it->cmp_id];
20454 /* Maybe translate single-byte characters to multibyte. */
20455 it->char_to_display = it->c;
20456 if (unibyte_display_via_language_environment
20457 && SINGLE_BYTE_CHAR_P (it->c)
20458 && (it->c >= 0240
20459 || (it->c >= 0200
20460 && !NILP (Vnonascii_translation_table))))
20462 it->char_to_display = unibyte_char_to_multibyte (it->c);
20465 /* Get face and font to use. Encode IT->char_to_display. */
20466 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20467 face = FACE_FROM_ID (it->f, it->face_id);
20468 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20469 &char2b, it->multibyte_p, 0);
20470 font = face->font;
20472 /* When no suitable font found, use the default font. */
20473 font_not_found_p = font == NULL;
20474 if (font_not_found_p)
20476 font = FRAME_FONT (it->f);
20477 boff = FRAME_BASELINE_OFFSET (it->f);
20478 font_info = NULL;
20480 else
20482 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20483 boff = font_info->baseline_offset;
20484 if (font_info->vertical_centering)
20485 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20488 /* There are no padding glyphs, so there is only one glyph to
20489 produce for the composition. Important is that pixel_width,
20490 ascent and descent are the values of what is drawn by
20491 draw_glyphs (i.e. the values of the overall glyphs composed). */
20492 it->nglyphs = 1;
20494 /* If we have not yet calculated pixel size data of glyphs of
20495 the composition for the current face font, calculate them
20496 now. Theoretically, we have to check all fonts for the
20497 glyphs, but that requires much time and memory space. So,
20498 here we check only the font of the first glyph. This leads
20499 to incorrect display very rarely, and C-l (recenter) can
20500 correct the display anyway. */
20501 if (cmp->font != (void *) font)
20503 /* Ascent and descent of the font of the first character of
20504 this composition (adjusted by baseline offset). Ascent
20505 and descent of overall glyphs should not be less than
20506 them respectively. */
20507 int font_ascent = FONT_BASE (font) + boff;
20508 int font_descent = FONT_DESCENT (font) - boff;
20509 /* Bounding box of the overall glyphs. */
20510 int leftmost, rightmost, lowest, highest;
20511 int i, width, ascent, descent;
20513 cmp->font = (void *) font;
20515 /* Initialize the bounding box. */
20516 if (font_info
20517 && (pcm = rif->per_char_metric (font, &char2b,
20518 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20520 width = pcm->width;
20521 ascent = pcm->ascent;
20522 descent = pcm->descent;
20524 else
20526 width = FONT_WIDTH (font);
20527 ascent = FONT_BASE (font);
20528 descent = FONT_DESCENT (font);
20531 rightmost = width;
20532 lowest = - descent + boff;
20533 highest = ascent + boff;
20534 leftmost = 0;
20536 if (font_info
20537 && font_info->default_ascent
20538 && CHAR_TABLE_P (Vuse_default_ascent)
20539 && !NILP (Faref (Vuse_default_ascent,
20540 make_number (it->char_to_display))))
20541 highest = font_info->default_ascent + boff;
20543 /* Draw the first glyph at the normal position. It may be
20544 shifted to right later if some other glyphs are drawn at
20545 the left. */
20546 cmp->offsets[0] = 0;
20547 cmp->offsets[1] = boff;
20549 /* Set cmp->offsets for the remaining glyphs. */
20550 for (i = 1; i < cmp->glyph_len; i++)
20552 int left, right, btm, top;
20553 int ch = COMPOSITION_GLYPH (cmp, i);
20554 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20556 face = FACE_FROM_ID (it->f, face_id);
20557 get_char_face_and_encoding (it->f, ch, face->id,
20558 &char2b, it->multibyte_p, 0);
20559 font = face->font;
20560 if (font == NULL)
20562 font = FRAME_FONT (it->f);
20563 boff = FRAME_BASELINE_OFFSET (it->f);
20564 font_info = NULL;
20566 else
20568 font_info
20569 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20570 boff = font_info->baseline_offset;
20571 if (font_info->vertical_centering)
20572 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20575 if (font_info
20576 && (pcm = rif->per_char_metric (font, &char2b,
20577 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20579 width = pcm->width;
20580 ascent = pcm->ascent;
20581 descent = pcm->descent;
20583 else
20585 width = FONT_WIDTH (font);
20586 ascent = 1;
20587 descent = 0;
20590 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20592 /* Relative composition with or without
20593 alternate chars. */
20594 left = (leftmost + rightmost - width) / 2;
20595 btm = - descent + boff;
20596 if (font_info && font_info->relative_compose
20597 && (! CHAR_TABLE_P (Vignore_relative_composition)
20598 || NILP (Faref (Vignore_relative_composition,
20599 make_number (ch)))))
20602 if (- descent >= font_info->relative_compose)
20603 /* One extra pixel between two glyphs. */
20604 btm = highest + 1;
20605 else if (ascent <= 0)
20606 /* One extra pixel between two glyphs. */
20607 btm = lowest - 1 - ascent - descent;
20610 else
20612 /* A composition rule is specified by an integer
20613 value that encodes global and new reference
20614 points (GREF and NREF). GREF and NREF are
20615 specified by numbers as below:
20617 0---1---2 -- ascent
20621 9--10--11 -- center
20623 ---3---4---5--- baseline
20625 6---7---8 -- descent
20627 int rule = COMPOSITION_RULE (cmp, i);
20628 int gref, nref, grefx, grefy, nrefx, nrefy;
20630 COMPOSITION_DECODE_RULE (rule, gref, nref);
20631 grefx = gref % 3, nrefx = nref % 3;
20632 grefy = gref / 3, nrefy = nref / 3;
20634 left = (leftmost
20635 + grefx * (rightmost - leftmost) / 2
20636 - nrefx * width / 2);
20637 btm = ((grefy == 0 ? highest
20638 : grefy == 1 ? 0
20639 : grefy == 2 ? lowest
20640 : (highest + lowest) / 2)
20641 - (nrefy == 0 ? ascent + descent
20642 : nrefy == 1 ? descent - boff
20643 : nrefy == 2 ? 0
20644 : (ascent + descent) / 2));
20647 cmp->offsets[i * 2] = left;
20648 cmp->offsets[i * 2 + 1] = btm + descent;
20650 /* Update the bounding box of the overall glyphs. */
20651 right = left + width;
20652 top = btm + descent + ascent;
20653 if (left < leftmost)
20654 leftmost = left;
20655 if (right > rightmost)
20656 rightmost = right;
20657 if (top > highest)
20658 highest = top;
20659 if (btm < lowest)
20660 lowest = btm;
20663 /* If there are glyphs whose x-offsets are negative,
20664 shift all glyphs to the right and make all x-offsets
20665 non-negative. */
20666 if (leftmost < 0)
20668 for (i = 0; i < cmp->glyph_len; i++)
20669 cmp->offsets[i * 2] -= leftmost;
20670 rightmost -= leftmost;
20673 cmp->pixel_width = rightmost;
20674 cmp->ascent = highest;
20675 cmp->descent = - lowest;
20676 if (cmp->ascent < font_ascent)
20677 cmp->ascent = font_ascent;
20678 if (cmp->descent < font_descent)
20679 cmp->descent = font_descent;
20682 it->pixel_width = cmp->pixel_width;
20683 it->ascent = it->phys_ascent = cmp->ascent;
20684 it->descent = it->phys_descent = cmp->descent;
20686 if (face->box != FACE_NO_BOX)
20688 int thick = face->box_line_width;
20690 if (thick > 0)
20692 it->ascent += thick;
20693 it->descent += thick;
20695 else
20696 thick = - thick;
20698 if (it->start_of_box_run_p)
20699 it->pixel_width += thick;
20700 if (it->end_of_box_run_p)
20701 it->pixel_width += thick;
20704 /* If face has an overline, add the height of the overline
20705 (1 pixel) and a 1 pixel margin to the character height. */
20706 if (face->overline_p)
20707 it->ascent += 2;
20709 take_vertical_position_into_account (it);
20711 if (it->glyph_row)
20712 append_composite_glyph (it);
20714 else if (it->what == IT_IMAGE)
20715 produce_image_glyph (it);
20716 else if (it->what == IT_STRETCH)
20717 produce_stretch_glyph (it);
20719 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20720 because this isn't true for images with `:ascent 100'. */
20721 xassert (it->ascent >= 0 && it->descent >= 0);
20722 if (it->area == TEXT_AREA)
20723 it->current_x += it->pixel_width;
20725 if (extra_line_spacing > 0)
20727 it->descent += extra_line_spacing;
20728 if (extra_line_spacing > it->max_extra_line_spacing)
20729 it->max_extra_line_spacing = extra_line_spacing;
20732 it->max_ascent = max (it->max_ascent, it->ascent);
20733 it->max_descent = max (it->max_descent, it->descent);
20734 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20735 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20738 /* EXPORT for RIF:
20739 Output LEN glyphs starting at START at the nominal cursor position.
20740 Advance the nominal cursor over the text. The global variable
20741 updated_window contains the window being updated, updated_row is
20742 the glyph row being updated, and updated_area is the area of that
20743 row being updated. */
20745 void
20746 x_write_glyphs (start, len)
20747 struct glyph *start;
20748 int len;
20750 int x, hpos;
20752 xassert (updated_window && updated_row);
20753 BLOCK_INPUT;
20755 /* Write glyphs. */
20757 hpos = start - updated_row->glyphs[updated_area];
20758 x = draw_glyphs (updated_window, output_cursor.x,
20759 updated_row, updated_area,
20760 hpos, hpos + len,
20761 DRAW_NORMAL_TEXT, 0);
20763 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20764 if (updated_area == TEXT_AREA
20765 && updated_window->phys_cursor_on_p
20766 && updated_window->phys_cursor.vpos == output_cursor.vpos
20767 && updated_window->phys_cursor.hpos >= hpos
20768 && updated_window->phys_cursor.hpos < hpos + len)
20769 updated_window->phys_cursor_on_p = 0;
20771 UNBLOCK_INPUT;
20773 /* Advance the output cursor. */
20774 output_cursor.hpos += len;
20775 output_cursor.x = x;
20779 /* EXPORT for RIF:
20780 Insert LEN glyphs from START at the nominal cursor position. */
20782 void
20783 x_insert_glyphs (start, len)
20784 struct glyph *start;
20785 int len;
20787 struct frame *f;
20788 struct window *w;
20789 int line_height, shift_by_width, shifted_region_width;
20790 struct glyph_row *row;
20791 struct glyph *glyph;
20792 int frame_x, frame_y, hpos;
20794 xassert (updated_window && updated_row);
20795 BLOCK_INPUT;
20796 w = updated_window;
20797 f = XFRAME (WINDOW_FRAME (w));
20799 /* Get the height of the line we are in. */
20800 row = updated_row;
20801 line_height = row->height;
20803 /* Get the width of the glyphs to insert. */
20804 shift_by_width = 0;
20805 for (glyph = start; glyph < start + len; ++glyph)
20806 shift_by_width += glyph->pixel_width;
20808 /* Get the width of the region to shift right. */
20809 shifted_region_width = (window_box_width (w, updated_area)
20810 - output_cursor.x
20811 - shift_by_width);
20813 /* Shift right. */
20814 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20815 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20817 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20818 line_height, shift_by_width);
20820 /* Write the glyphs. */
20821 hpos = start - row->glyphs[updated_area];
20822 draw_glyphs (w, output_cursor.x, row, updated_area,
20823 hpos, hpos + len,
20824 DRAW_NORMAL_TEXT, 0);
20826 /* Advance the output cursor. */
20827 output_cursor.hpos += len;
20828 output_cursor.x += shift_by_width;
20829 UNBLOCK_INPUT;
20833 /* EXPORT for RIF:
20834 Erase the current text line from the nominal cursor position
20835 (inclusive) to pixel column TO_X (exclusive). The idea is that
20836 everything from TO_X onward is already erased.
20838 TO_X is a pixel position relative to updated_area of
20839 updated_window. TO_X == -1 means clear to the end of this area. */
20841 void
20842 x_clear_end_of_line (to_x)
20843 int to_x;
20845 struct frame *f;
20846 struct window *w = updated_window;
20847 int max_x, min_y, max_y;
20848 int from_x, from_y, to_y;
20850 xassert (updated_window && updated_row);
20851 f = XFRAME (w->frame);
20853 if (updated_row->full_width_p)
20854 max_x = WINDOW_TOTAL_WIDTH (w);
20855 else
20856 max_x = window_box_width (w, updated_area);
20857 max_y = window_text_bottom_y (w);
20859 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20860 of window. For TO_X > 0, truncate to end of drawing area. */
20861 if (to_x == 0)
20862 return;
20863 else if (to_x < 0)
20864 to_x = max_x;
20865 else
20866 to_x = min (to_x, max_x);
20868 to_y = min (max_y, output_cursor.y + updated_row->height);
20870 /* Notice if the cursor will be cleared by this operation. */
20871 if (!updated_row->full_width_p)
20872 notice_overwritten_cursor (w, updated_area,
20873 output_cursor.x, -1,
20874 updated_row->y,
20875 MATRIX_ROW_BOTTOM_Y (updated_row));
20877 from_x = output_cursor.x;
20879 /* Translate to frame coordinates. */
20880 if (updated_row->full_width_p)
20882 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20883 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20885 else
20887 int area_left = window_box_left (w, updated_area);
20888 from_x += area_left;
20889 to_x += area_left;
20892 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20893 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20894 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20896 /* Prevent inadvertently clearing to end of the X window. */
20897 if (to_x > from_x && to_y > from_y)
20899 BLOCK_INPUT;
20900 rif->clear_frame_area (f, from_x, from_y,
20901 to_x - from_x, to_y - from_y);
20902 UNBLOCK_INPUT;
20906 #endif /* HAVE_WINDOW_SYSTEM */
20910 /***********************************************************************
20911 Cursor types
20912 ***********************************************************************/
20914 /* Value is the internal representation of the specified cursor type
20915 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20916 of the bar cursor. */
20918 static enum text_cursor_kinds
20919 get_specified_cursor_type (arg, width)
20920 Lisp_Object arg;
20921 int *width;
20923 enum text_cursor_kinds type;
20925 if (NILP (arg))
20926 return NO_CURSOR;
20928 if (EQ (arg, Qbox))
20929 return FILLED_BOX_CURSOR;
20931 if (EQ (arg, Qhollow))
20932 return HOLLOW_BOX_CURSOR;
20934 if (EQ (arg, Qbar))
20936 *width = 2;
20937 return BAR_CURSOR;
20940 if (CONSP (arg)
20941 && EQ (XCAR (arg), Qbar)
20942 && INTEGERP (XCDR (arg))
20943 && XINT (XCDR (arg)) >= 0)
20945 *width = XINT (XCDR (arg));
20946 return BAR_CURSOR;
20949 if (EQ (arg, Qhbar))
20951 *width = 2;
20952 return HBAR_CURSOR;
20955 if (CONSP (arg)
20956 && EQ (XCAR (arg), Qhbar)
20957 && INTEGERP (XCDR (arg))
20958 && XINT (XCDR (arg)) >= 0)
20960 *width = XINT (XCDR (arg));
20961 return HBAR_CURSOR;
20964 /* Treat anything unknown as "hollow box cursor".
20965 It was bad to signal an error; people have trouble fixing
20966 .Xdefaults with Emacs, when it has something bad in it. */
20967 type = HOLLOW_BOX_CURSOR;
20969 return type;
20972 /* Set the default cursor types for specified frame. */
20973 void
20974 set_frame_cursor_types (f, arg)
20975 struct frame *f;
20976 Lisp_Object arg;
20978 int width;
20979 Lisp_Object tem;
20981 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20982 FRAME_CURSOR_WIDTH (f) = width;
20984 /* By default, set up the blink-off state depending on the on-state. */
20986 tem = Fassoc (arg, Vblink_cursor_alist);
20987 if (!NILP (tem))
20989 FRAME_BLINK_OFF_CURSOR (f)
20990 = get_specified_cursor_type (XCDR (tem), &width);
20991 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20993 else
20994 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20998 /* Return the cursor we want to be displayed in window W. Return
20999 width of bar/hbar cursor through WIDTH arg. Return with
21000 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21001 (i.e. if the `system caret' should track this cursor).
21003 In a mini-buffer window, we want the cursor only to appear if we
21004 are reading input from this window. For the selected window, we
21005 want the cursor type given by the frame parameter or buffer local
21006 setting of cursor-type. If explicitly marked off, draw no cursor.
21007 In all other cases, we want a hollow box cursor. */
21009 static enum text_cursor_kinds
21010 get_window_cursor_type (w, glyph, width, active_cursor)
21011 struct window *w;
21012 struct glyph *glyph;
21013 int *width;
21014 int *active_cursor;
21016 struct frame *f = XFRAME (w->frame);
21017 struct buffer *b = XBUFFER (w->buffer);
21018 int cursor_type = DEFAULT_CURSOR;
21019 Lisp_Object alt_cursor;
21020 int non_selected = 0;
21022 *active_cursor = 1;
21024 /* Echo area */
21025 if (cursor_in_echo_area
21026 && FRAME_HAS_MINIBUF_P (f)
21027 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21029 if (w == XWINDOW (echo_area_window))
21031 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21033 *width = FRAME_CURSOR_WIDTH (f);
21034 return FRAME_DESIRED_CURSOR (f);
21036 else
21037 return get_specified_cursor_type (b->cursor_type, width);
21040 *active_cursor = 0;
21041 non_selected = 1;
21044 /* Nonselected window or nonselected frame. */
21045 else if (w != XWINDOW (f->selected_window)
21046 #ifdef HAVE_WINDOW_SYSTEM
21047 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21048 #endif
21051 *active_cursor = 0;
21053 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21054 return NO_CURSOR;
21056 non_selected = 1;
21059 /* Never display a cursor in a window in which cursor-type is nil. */
21060 if (NILP (b->cursor_type))
21061 return NO_CURSOR;
21063 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21064 if (non_selected)
21066 alt_cursor = b->cursor_in_non_selected_windows;
21067 return get_specified_cursor_type (alt_cursor, width);
21070 /* Get the normal cursor type for this window. */
21071 if (EQ (b->cursor_type, Qt))
21073 cursor_type = FRAME_DESIRED_CURSOR (f);
21074 *width = FRAME_CURSOR_WIDTH (f);
21076 else
21077 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21079 /* Use normal cursor if not blinked off. */
21080 if (!w->cursor_off_p)
21082 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21083 if (cursor_type == FILLED_BOX_CURSOR)
21084 cursor_type = HOLLOW_BOX_CURSOR;
21086 return cursor_type;
21089 /* Cursor is blinked off, so determine how to "toggle" it. */
21091 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21092 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21093 return get_specified_cursor_type (XCDR (alt_cursor), width);
21095 /* Then see if frame has specified a specific blink off cursor type. */
21096 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21098 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21099 return FRAME_BLINK_OFF_CURSOR (f);
21102 #if 0
21103 /* Some people liked having a permanently visible blinking cursor,
21104 while others had very strong opinions against it. So it was
21105 decided to remove it. KFS 2003-09-03 */
21107 /* Finally perform built-in cursor blinking:
21108 filled box <-> hollow box
21109 wide [h]bar <-> narrow [h]bar
21110 narrow [h]bar <-> no cursor
21111 other type <-> no cursor */
21113 if (cursor_type == FILLED_BOX_CURSOR)
21114 return HOLLOW_BOX_CURSOR;
21116 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21118 *width = 1;
21119 return cursor_type;
21121 #endif
21123 return NO_CURSOR;
21127 #ifdef HAVE_WINDOW_SYSTEM
21129 /* Notice when the text cursor of window W has been completely
21130 overwritten by a drawing operation that outputs glyphs in AREA
21131 starting at X0 and ending at X1 in the line starting at Y0 and
21132 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21133 the rest of the line after X0 has been written. Y coordinates
21134 are window-relative. */
21136 static void
21137 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21138 struct window *w;
21139 enum glyph_row_area area;
21140 int x0, y0, x1, y1;
21142 int cx0, cx1, cy0, cy1;
21143 struct glyph_row *row;
21145 if (!w->phys_cursor_on_p)
21146 return;
21147 if (area != TEXT_AREA)
21148 return;
21150 if (w->phys_cursor.vpos < 0
21151 || w->phys_cursor.vpos >= w->current_matrix->nrows
21152 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21153 !(row->enabled_p && row->displays_text_p)))
21154 return;
21156 if (row->cursor_in_fringe_p)
21158 row->cursor_in_fringe_p = 0;
21159 draw_fringe_bitmap (w, row, 0);
21160 w->phys_cursor_on_p = 0;
21161 return;
21164 cx0 = w->phys_cursor.x;
21165 cx1 = cx0 + w->phys_cursor_width;
21166 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21167 return;
21169 /* The cursor image will be completely removed from the
21170 screen if the output area intersects the cursor area in
21171 y-direction. When we draw in [y0 y1[, and some part of
21172 the cursor is at y < y0, that part must have been drawn
21173 before. When scrolling, the cursor is erased before
21174 actually scrolling, so we don't come here. When not
21175 scrolling, the rows above the old cursor row must have
21176 changed, and in this case these rows must have written
21177 over the cursor image.
21179 Likewise if part of the cursor is below y1, with the
21180 exception of the cursor being in the first blank row at
21181 the buffer and window end because update_text_area
21182 doesn't draw that row. (Except when it does, but
21183 that's handled in update_text_area.) */
21185 cy0 = w->phys_cursor.y;
21186 cy1 = cy0 + w->phys_cursor_height;
21187 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21188 return;
21190 w->phys_cursor_on_p = 0;
21193 #endif /* HAVE_WINDOW_SYSTEM */
21196 /************************************************************************
21197 Mouse Face
21198 ************************************************************************/
21200 #ifdef HAVE_WINDOW_SYSTEM
21202 /* EXPORT for RIF:
21203 Fix the display of area AREA of overlapping row ROW in window W
21204 with respect to the overlapping part OVERLAPS. */
21206 void
21207 x_fix_overlapping_area (w, row, area, overlaps)
21208 struct window *w;
21209 struct glyph_row *row;
21210 enum glyph_row_area area;
21211 int overlaps;
21213 int i, x;
21215 BLOCK_INPUT;
21217 x = 0;
21218 for (i = 0; i < row->used[area];)
21220 if (row->glyphs[area][i].overlaps_vertically_p)
21222 int start = i, start_x = x;
21226 x += row->glyphs[area][i].pixel_width;
21227 ++i;
21229 while (i < row->used[area]
21230 && row->glyphs[area][i].overlaps_vertically_p);
21232 draw_glyphs (w, start_x, row, area,
21233 start, i,
21234 DRAW_NORMAL_TEXT, overlaps);
21236 else
21238 x += row->glyphs[area][i].pixel_width;
21239 ++i;
21243 UNBLOCK_INPUT;
21247 /* EXPORT:
21248 Draw the cursor glyph of window W in glyph row ROW. See the
21249 comment of draw_glyphs for the meaning of HL. */
21251 void
21252 draw_phys_cursor_glyph (w, row, hl)
21253 struct window *w;
21254 struct glyph_row *row;
21255 enum draw_glyphs_face hl;
21257 /* If cursor hpos is out of bounds, don't draw garbage. This can
21258 happen in mini-buffer windows when switching between echo area
21259 glyphs and mini-buffer. */
21260 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21262 int on_p = w->phys_cursor_on_p;
21263 int x1;
21264 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21265 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21266 hl, 0);
21267 w->phys_cursor_on_p = on_p;
21269 if (hl == DRAW_CURSOR)
21270 w->phys_cursor_width = x1 - w->phys_cursor.x;
21271 /* When we erase the cursor, and ROW is overlapped by other
21272 rows, make sure that these overlapping parts of other rows
21273 are redrawn. */
21274 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21276 w->phys_cursor_width = x1 - w->phys_cursor.x;
21278 if (row > w->current_matrix->rows
21279 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21280 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21281 OVERLAPS_ERASED_CURSOR);
21283 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21284 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21285 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21286 OVERLAPS_ERASED_CURSOR);
21292 /* EXPORT:
21293 Erase the image of a cursor of window W from the screen. */
21295 void
21296 erase_phys_cursor (w)
21297 struct window *w;
21299 struct frame *f = XFRAME (w->frame);
21300 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21301 int hpos = w->phys_cursor.hpos;
21302 int vpos = w->phys_cursor.vpos;
21303 int mouse_face_here_p = 0;
21304 struct glyph_matrix *active_glyphs = w->current_matrix;
21305 struct glyph_row *cursor_row;
21306 struct glyph *cursor_glyph;
21307 enum draw_glyphs_face hl;
21309 /* No cursor displayed or row invalidated => nothing to do on the
21310 screen. */
21311 if (w->phys_cursor_type == NO_CURSOR)
21312 goto mark_cursor_off;
21314 /* VPOS >= active_glyphs->nrows means that window has been resized.
21315 Don't bother to erase the cursor. */
21316 if (vpos >= active_glyphs->nrows)
21317 goto mark_cursor_off;
21319 /* If row containing cursor is marked invalid, there is nothing we
21320 can do. */
21321 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21322 if (!cursor_row->enabled_p)
21323 goto mark_cursor_off;
21325 /* If line spacing is > 0, old cursor may only be partially visible in
21326 window after split-window. So adjust visible height. */
21327 cursor_row->visible_height = min (cursor_row->visible_height,
21328 window_text_bottom_y (w) - cursor_row->y);
21330 /* If row is completely invisible, don't attempt to delete a cursor which
21331 isn't there. This can happen if cursor is at top of a window, and
21332 we switch to a buffer with a header line in that window. */
21333 if (cursor_row->visible_height <= 0)
21334 goto mark_cursor_off;
21336 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21337 if (cursor_row->cursor_in_fringe_p)
21339 cursor_row->cursor_in_fringe_p = 0;
21340 draw_fringe_bitmap (w, cursor_row, 0);
21341 goto mark_cursor_off;
21344 /* This can happen when the new row is shorter than the old one.
21345 In this case, either draw_glyphs or clear_end_of_line
21346 should have cleared the cursor. Note that we wouldn't be
21347 able to erase the cursor in this case because we don't have a
21348 cursor glyph at hand. */
21349 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21350 goto mark_cursor_off;
21352 /* If the cursor is in the mouse face area, redisplay that when
21353 we clear the cursor. */
21354 if (! NILP (dpyinfo->mouse_face_window)
21355 && w == XWINDOW (dpyinfo->mouse_face_window)
21356 && (vpos > dpyinfo->mouse_face_beg_row
21357 || (vpos == dpyinfo->mouse_face_beg_row
21358 && hpos >= dpyinfo->mouse_face_beg_col))
21359 && (vpos < dpyinfo->mouse_face_end_row
21360 || (vpos == dpyinfo->mouse_face_end_row
21361 && hpos < dpyinfo->mouse_face_end_col))
21362 /* Don't redraw the cursor's spot in mouse face if it is at the
21363 end of a line (on a newline). The cursor appears there, but
21364 mouse highlighting does not. */
21365 && cursor_row->used[TEXT_AREA] > hpos)
21366 mouse_face_here_p = 1;
21368 /* Maybe clear the display under the cursor. */
21369 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21371 int x, y, left_x;
21372 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21373 int width;
21375 cursor_glyph = get_phys_cursor_glyph (w);
21376 if (cursor_glyph == NULL)
21377 goto mark_cursor_off;
21379 width = cursor_glyph->pixel_width;
21380 left_x = window_box_left_offset (w, TEXT_AREA);
21381 x = w->phys_cursor.x;
21382 if (x < left_x)
21383 width -= left_x - x;
21384 width = min (width, window_box_width (w, TEXT_AREA) - x);
21385 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21386 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21388 if (width > 0)
21389 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21392 /* Erase the cursor by redrawing the character underneath it. */
21393 if (mouse_face_here_p)
21394 hl = DRAW_MOUSE_FACE;
21395 else
21396 hl = DRAW_NORMAL_TEXT;
21397 draw_phys_cursor_glyph (w, cursor_row, hl);
21399 mark_cursor_off:
21400 w->phys_cursor_on_p = 0;
21401 w->phys_cursor_type = NO_CURSOR;
21405 /* EXPORT:
21406 Display or clear cursor of window W. If ON is zero, clear the
21407 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21408 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21410 void
21411 display_and_set_cursor (w, on, hpos, vpos, x, y)
21412 struct window *w;
21413 int on, hpos, vpos, x, y;
21415 struct frame *f = XFRAME (w->frame);
21416 int new_cursor_type;
21417 int new_cursor_width;
21418 int active_cursor;
21419 struct glyph_row *glyph_row;
21420 struct glyph *glyph;
21422 /* This is pointless on invisible frames, and dangerous on garbaged
21423 windows and frames; in the latter case, the frame or window may
21424 be in the midst of changing its size, and x and y may be off the
21425 window. */
21426 if (! FRAME_VISIBLE_P (f)
21427 || FRAME_GARBAGED_P (f)
21428 || vpos >= w->current_matrix->nrows
21429 || hpos >= w->current_matrix->matrix_w)
21430 return;
21432 /* If cursor is off and we want it off, return quickly. */
21433 if (!on && !w->phys_cursor_on_p)
21434 return;
21436 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21437 /* If cursor row is not enabled, we don't really know where to
21438 display the cursor. */
21439 if (!glyph_row->enabled_p)
21441 w->phys_cursor_on_p = 0;
21442 return;
21445 glyph = NULL;
21446 if (!glyph_row->exact_window_width_line_p
21447 || hpos < glyph_row->used[TEXT_AREA])
21448 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21450 xassert (interrupt_input_blocked);
21452 /* Set new_cursor_type to the cursor we want to be displayed. */
21453 new_cursor_type = get_window_cursor_type (w, glyph,
21454 &new_cursor_width, &active_cursor);
21456 /* If cursor is currently being shown and we don't want it to be or
21457 it is in the wrong place, or the cursor type is not what we want,
21458 erase it. */
21459 if (w->phys_cursor_on_p
21460 && (!on
21461 || w->phys_cursor.x != x
21462 || w->phys_cursor.y != y
21463 || new_cursor_type != w->phys_cursor_type
21464 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21465 && new_cursor_width != w->phys_cursor_width)))
21466 erase_phys_cursor (w);
21468 /* Don't check phys_cursor_on_p here because that flag is only set
21469 to zero in some cases where we know that the cursor has been
21470 completely erased, to avoid the extra work of erasing the cursor
21471 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21472 still not be visible, or it has only been partly erased. */
21473 if (on)
21475 w->phys_cursor_ascent = glyph_row->ascent;
21476 w->phys_cursor_height = glyph_row->height;
21478 /* Set phys_cursor_.* before x_draw_.* is called because some
21479 of them may need the information. */
21480 w->phys_cursor.x = x;
21481 w->phys_cursor.y = glyph_row->y;
21482 w->phys_cursor.hpos = hpos;
21483 w->phys_cursor.vpos = vpos;
21486 rif->draw_window_cursor (w, glyph_row, x, y,
21487 new_cursor_type, new_cursor_width,
21488 on, active_cursor);
21492 /* Switch the display of W's cursor on or off, according to the value
21493 of ON. */
21495 static void
21496 update_window_cursor (w, on)
21497 struct window *w;
21498 int on;
21500 /* Don't update cursor in windows whose frame is in the process
21501 of being deleted. */
21502 if (w->current_matrix)
21504 BLOCK_INPUT;
21505 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21506 w->phys_cursor.x, w->phys_cursor.y);
21507 UNBLOCK_INPUT;
21512 /* Call update_window_cursor with parameter ON_P on all leaf windows
21513 in the window tree rooted at W. */
21515 static void
21516 update_cursor_in_window_tree (w, on_p)
21517 struct window *w;
21518 int on_p;
21520 while (w)
21522 if (!NILP (w->hchild))
21523 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21524 else if (!NILP (w->vchild))
21525 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21526 else
21527 update_window_cursor (w, on_p);
21529 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21534 /* EXPORT:
21535 Display the cursor on window W, or clear it, according to ON_P.
21536 Don't change the cursor's position. */
21538 void
21539 x_update_cursor (f, on_p)
21540 struct frame *f;
21541 int on_p;
21543 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21547 /* EXPORT:
21548 Clear the cursor of window W to background color, and mark the
21549 cursor as not shown. This is used when the text where the cursor
21550 is is about to be rewritten. */
21552 void
21553 x_clear_cursor (w)
21554 struct window *w;
21556 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21557 update_window_cursor (w, 0);
21561 /* EXPORT:
21562 Display the active region described by mouse_face_* according to DRAW. */
21564 void
21565 show_mouse_face (dpyinfo, draw)
21566 Display_Info *dpyinfo;
21567 enum draw_glyphs_face draw;
21569 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21570 struct frame *f = XFRAME (WINDOW_FRAME (w));
21572 if (/* If window is in the process of being destroyed, don't bother
21573 to do anything. */
21574 w->current_matrix != NULL
21575 /* Don't update mouse highlight if hidden */
21576 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21577 /* Recognize when we are called to operate on rows that don't exist
21578 anymore. This can happen when a window is split. */
21579 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21581 int phys_cursor_on_p = w->phys_cursor_on_p;
21582 struct glyph_row *row, *first, *last;
21584 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21585 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21587 for (row = first; row <= last && row->enabled_p; ++row)
21589 int start_hpos, end_hpos, start_x;
21591 /* For all but the first row, the highlight starts at column 0. */
21592 if (row == first)
21594 start_hpos = dpyinfo->mouse_face_beg_col;
21595 start_x = dpyinfo->mouse_face_beg_x;
21597 else
21599 start_hpos = 0;
21600 start_x = 0;
21603 if (row == last)
21604 end_hpos = dpyinfo->mouse_face_end_col;
21605 else
21607 end_hpos = row->used[TEXT_AREA];
21608 if (draw == DRAW_NORMAL_TEXT)
21609 row->fill_line_p = 1; /* Clear to end of line */
21612 if (end_hpos > start_hpos)
21614 draw_glyphs (w, start_x, row, TEXT_AREA,
21615 start_hpos, end_hpos,
21616 draw, 0);
21618 row->mouse_face_p
21619 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21623 /* When we've written over the cursor, arrange for it to
21624 be displayed again. */
21625 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21627 BLOCK_INPUT;
21628 display_and_set_cursor (w, 1,
21629 w->phys_cursor.hpos, w->phys_cursor.vpos,
21630 w->phys_cursor.x, w->phys_cursor.y);
21631 UNBLOCK_INPUT;
21635 /* Change the mouse cursor. */
21636 if (draw == DRAW_NORMAL_TEXT)
21637 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21638 else if (draw == DRAW_MOUSE_FACE)
21639 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21640 else
21641 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21644 /* EXPORT:
21645 Clear out the mouse-highlighted active region.
21646 Redraw it un-highlighted first. Value is non-zero if mouse
21647 face was actually drawn unhighlighted. */
21650 clear_mouse_face (dpyinfo)
21651 Display_Info *dpyinfo;
21653 int cleared = 0;
21655 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21657 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21658 cleared = 1;
21661 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21662 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21663 dpyinfo->mouse_face_window = Qnil;
21664 dpyinfo->mouse_face_overlay = Qnil;
21665 return cleared;
21669 /* EXPORT:
21670 Non-zero if physical cursor of window W is within mouse face. */
21673 cursor_in_mouse_face_p (w)
21674 struct window *w;
21676 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21677 int in_mouse_face = 0;
21679 if (WINDOWP (dpyinfo->mouse_face_window)
21680 && XWINDOW (dpyinfo->mouse_face_window) == w)
21682 int hpos = w->phys_cursor.hpos;
21683 int vpos = w->phys_cursor.vpos;
21685 if (vpos >= dpyinfo->mouse_face_beg_row
21686 && vpos <= dpyinfo->mouse_face_end_row
21687 && (vpos > dpyinfo->mouse_face_beg_row
21688 || hpos >= dpyinfo->mouse_face_beg_col)
21689 && (vpos < dpyinfo->mouse_face_end_row
21690 || hpos < dpyinfo->mouse_face_end_col
21691 || dpyinfo->mouse_face_past_end))
21692 in_mouse_face = 1;
21695 return in_mouse_face;
21701 /* Find the glyph matrix position of buffer position CHARPOS in window
21702 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21703 current glyphs must be up to date. If CHARPOS is above window
21704 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21705 of last line in W. In the row containing CHARPOS, stop before glyphs
21706 having STOP as object. */
21708 #if 1 /* This is a version of fast_find_position that's more correct
21709 in the presence of hscrolling, for example. I didn't install
21710 it right away because the problem fixed is minor, it failed
21711 in 20.x as well, and I think it's too risky to install
21712 so near the release of 21.1. 2001-09-25 gerd. */
21714 static int
21715 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21716 struct window *w;
21717 int charpos;
21718 int *hpos, *vpos, *x, *y;
21719 Lisp_Object stop;
21721 struct glyph_row *row, *first;
21722 struct glyph *glyph, *end;
21723 int past_end = 0;
21725 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21726 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21728 *x = first->x;
21729 *y = first->y;
21730 *hpos = 0;
21731 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21732 return 1;
21735 row = row_containing_pos (w, charpos, first, NULL, 0);
21736 if (row == NULL)
21738 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21739 past_end = 1;
21742 /* If whole rows or last part of a row came from a display overlay,
21743 row_containing_pos will skip over such rows because their end pos
21744 equals the start pos of the overlay or interval.
21746 Move back if we have a STOP object and previous row's
21747 end glyph came from STOP. */
21748 if (!NILP (stop))
21750 struct glyph_row *prev;
21751 while ((prev = row - 1, prev >= first)
21752 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21753 && prev->used[TEXT_AREA] > 0)
21755 struct glyph *beg = prev->glyphs[TEXT_AREA];
21756 glyph = beg + prev->used[TEXT_AREA];
21757 while (--glyph >= beg
21758 && INTEGERP (glyph->object));
21759 if (glyph < beg
21760 || !EQ (stop, glyph->object))
21761 break;
21762 row = prev;
21766 *x = row->x;
21767 *y = row->y;
21768 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21770 glyph = row->glyphs[TEXT_AREA];
21771 end = glyph + row->used[TEXT_AREA];
21773 /* Skip over glyphs not having an object at the start of the row.
21774 These are special glyphs like truncation marks on terminal
21775 frames. */
21776 if (row->displays_text_p)
21777 while (glyph < end
21778 && INTEGERP (glyph->object)
21779 && !EQ (stop, glyph->object)
21780 && glyph->charpos < 0)
21782 *x += glyph->pixel_width;
21783 ++glyph;
21786 while (glyph < end
21787 && !INTEGERP (glyph->object)
21788 && !EQ (stop, glyph->object)
21789 && (!BUFFERP (glyph->object)
21790 || glyph->charpos < charpos))
21792 *x += glyph->pixel_width;
21793 ++glyph;
21796 *hpos = glyph - row->glyphs[TEXT_AREA];
21797 return !past_end;
21800 #else /* not 1 */
21802 static int
21803 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21804 struct window *w;
21805 int pos;
21806 int *hpos, *vpos, *x, *y;
21807 Lisp_Object stop;
21809 int i;
21810 int lastcol;
21811 int maybe_next_line_p = 0;
21812 int line_start_position;
21813 int yb = window_text_bottom_y (w);
21814 struct glyph_row *row, *best_row;
21815 int row_vpos, best_row_vpos;
21816 int current_x;
21818 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21819 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21821 while (row->y < yb)
21823 if (row->used[TEXT_AREA])
21824 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21825 else
21826 line_start_position = 0;
21828 if (line_start_position > pos)
21829 break;
21830 /* If the position sought is the end of the buffer,
21831 don't include the blank lines at the bottom of the window. */
21832 else if (line_start_position == pos
21833 && pos == BUF_ZV (XBUFFER (w->buffer)))
21835 maybe_next_line_p = 1;
21836 break;
21838 else if (line_start_position > 0)
21840 best_row = row;
21841 best_row_vpos = row_vpos;
21844 if (row->y + row->height >= yb)
21845 break;
21847 ++row;
21848 ++row_vpos;
21851 /* Find the right column within BEST_ROW. */
21852 lastcol = 0;
21853 current_x = best_row->x;
21854 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21856 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21857 int charpos = glyph->charpos;
21859 if (BUFFERP (glyph->object))
21861 if (charpos == pos)
21863 *hpos = i;
21864 *vpos = best_row_vpos;
21865 *x = current_x;
21866 *y = best_row->y;
21867 return 1;
21869 else if (charpos > pos)
21870 break;
21872 else if (EQ (glyph->object, stop))
21873 break;
21875 if (charpos > 0)
21876 lastcol = i;
21877 current_x += glyph->pixel_width;
21880 /* If we're looking for the end of the buffer,
21881 and we didn't find it in the line we scanned,
21882 use the start of the following line. */
21883 if (maybe_next_line_p)
21885 ++best_row;
21886 ++best_row_vpos;
21887 lastcol = 0;
21888 current_x = best_row->x;
21891 *vpos = best_row_vpos;
21892 *hpos = lastcol + 1;
21893 *x = current_x;
21894 *y = best_row->y;
21895 return 0;
21898 #endif /* not 1 */
21901 /* Find the position of the glyph for position POS in OBJECT in
21902 window W's current matrix, and return in *X, *Y the pixel
21903 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21905 RIGHT_P non-zero means return the position of the right edge of the
21906 glyph, RIGHT_P zero means return the left edge position.
21908 If no glyph for POS exists in the matrix, return the position of
21909 the glyph with the next smaller position that is in the matrix, if
21910 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21911 exists in the matrix, return the position of the glyph with the
21912 next larger position in OBJECT.
21914 Value is non-zero if a glyph was found. */
21916 static int
21917 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21918 struct window *w;
21919 int pos;
21920 Lisp_Object object;
21921 int *hpos, *vpos, *x, *y;
21922 int right_p;
21924 int yb = window_text_bottom_y (w);
21925 struct glyph_row *r;
21926 struct glyph *best_glyph = NULL;
21927 struct glyph_row *best_row = NULL;
21928 int best_x = 0;
21930 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21931 r->enabled_p && r->y < yb;
21932 ++r)
21934 struct glyph *g = r->glyphs[TEXT_AREA];
21935 struct glyph *e = g + r->used[TEXT_AREA];
21936 int gx;
21938 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21939 if (EQ (g->object, object))
21941 if (g->charpos == pos)
21943 best_glyph = g;
21944 best_x = gx;
21945 best_row = r;
21946 goto found;
21948 else if (best_glyph == NULL
21949 || ((abs (g->charpos - pos)
21950 < abs (best_glyph->charpos - pos))
21951 && (right_p
21952 ? g->charpos < pos
21953 : g->charpos > pos)))
21955 best_glyph = g;
21956 best_x = gx;
21957 best_row = r;
21962 found:
21964 if (best_glyph)
21966 *x = best_x;
21967 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21969 if (right_p)
21971 *x += best_glyph->pixel_width;
21972 ++*hpos;
21975 *y = best_row->y;
21976 *vpos = best_row - w->current_matrix->rows;
21979 return best_glyph != NULL;
21983 /* See if position X, Y is within a hot-spot of an image. */
21985 static int
21986 on_hot_spot_p (hot_spot, x, y)
21987 Lisp_Object hot_spot;
21988 int x, y;
21990 if (!CONSP (hot_spot))
21991 return 0;
21993 if (EQ (XCAR (hot_spot), Qrect))
21995 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21996 Lisp_Object rect = XCDR (hot_spot);
21997 Lisp_Object tem;
21998 if (!CONSP (rect))
21999 return 0;
22000 if (!CONSP (XCAR (rect)))
22001 return 0;
22002 if (!CONSP (XCDR (rect)))
22003 return 0;
22004 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22005 return 0;
22006 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22007 return 0;
22008 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22009 return 0;
22010 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22011 return 0;
22012 return 1;
22014 else if (EQ (XCAR (hot_spot), Qcircle))
22016 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22017 Lisp_Object circ = XCDR (hot_spot);
22018 Lisp_Object lr, lx0, ly0;
22019 if (CONSP (circ)
22020 && CONSP (XCAR (circ))
22021 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22022 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22023 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22025 double r = XFLOATINT (lr);
22026 double dx = XINT (lx0) - x;
22027 double dy = XINT (ly0) - y;
22028 return (dx * dx + dy * dy <= r * r);
22031 else if (EQ (XCAR (hot_spot), Qpoly))
22033 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22034 if (VECTORP (XCDR (hot_spot)))
22036 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22037 Lisp_Object *poly = v->contents;
22038 int n = v->size;
22039 int i;
22040 int inside = 0;
22041 Lisp_Object lx, ly;
22042 int x0, y0;
22044 /* Need an even number of coordinates, and at least 3 edges. */
22045 if (n < 6 || n & 1)
22046 return 0;
22048 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22049 If count is odd, we are inside polygon. Pixels on edges
22050 may or may not be included depending on actual geometry of the
22051 polygon. */
22052 if ((lx = poly[n-2], !INTEGERP (lx))
22053 || (ly = poly[n-1], !INTEGERP (lx)))
22054 return 0;
22055 x0 = XINT (lx), y0 = XINT (ly);
22056 for (i = 0; i < n; i += 2)
22058 int x1 = x0, y1 = y0;
22059 if ((lx = poly[i], !INTEGERP (lx))
22060 || (ly = poly[i+1], !INTEGERP (ly)))
22061 return 0;
22062 x0 = XINT (lx), y0 = XINT (ly);
22064 /* Does this segment cross the X line? */
22065 if (x0 >= x)
22067 if (x1 >= x)
22068 continue;
22070 else if (x1 < x)
22071 continue;
22072 if (y > y0 && y > y1)
22073 continue;
22074 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22075 inside = !inside;
22077 return inside;
22080 /* If we don't understand the format, pretend we're not in the hot-spot. */
22081 return 0;
22084 Lisp_Object
22085 find_hot_spot (map, x, y)
22086 Lisp_Object map;
22087 int x, y;
22089 while (CONSP (map))
22091 if (CONSP (XCAR (map))
22092 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22093 return XCAR (map);
22094 map = XCDR (map);
22097 return Qnil;
22100 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22101 3, 3, 0,
22102 doc: /* Lookup in image map MAP coordinates X and Y.
22103 An image map is an alist where each element has the format (AREA ID PLIST).
22104 An AREA is specified as either a rectangle, a circle, or a polygon:
22105 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22106 pixel coordinates of the upper left and bottom right corners.
22107 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22108 and the radius of the circle; r may be a float or integer.
22109 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22110 vector describes one corner in the polygon.
22111 Returns the alist element for the first matching AREA in MAP. */)
22112 (map, x, y)
22113 Lisp_Object map;
22114 Lisp_Object x, y;
22116 if (NILP (map))
22117 return Qnil;
22119 CHECK_NUMBER (x);
22120 CHECK_NUMBER (y);
22122 return find_hot_spot (map, XINT (x), XINT (y));
22126 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22127 static void
22128 define_frame_cursor1 (f, cursor, pointer)
22129 struct frame *f;
22130 Cursor cursor;
22131 Lisp_Object pointer;
22133 /* Do not change cursor shape while dragging mouse. */
22134 if (!NILP (do_mouse_tracking))
22135 return;
22137 if (!NILP (pointer))
22139 if (EQ (pointer, Qarrow))
22140 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22141 else if (EQ (pointer, Qhand))
22142 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22143 else if (EQ (pointer, Qtext))
22144 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22145 else if (EQ (pointer, intern ("hdrag")))
22146 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22147 #ifdef HAVE_X_WINDOWS
22148 else if (EQ (pointer, intern ("vdrag")))
22149 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22150 #endif
22151 else if (EQ (pointer, intern ("hourglass")))
22152 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22153 else if (EQ (pointer, Qmodeline))
22154 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22155 else
22156 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22159 if (cursor != No_Cursor)
22160 rif->define_frame_cursor (f, cursor);
22163 /* Take proper action when mouse has moved to the mode or header line
22164 or marginal area AREA of window W, x-position X and y-position Y.
22165 X is relative to the start of the text display area of W, so the
22166 width of bitmap areas and scroll bars must be subtracted to get a
22167 position relative to the start of the mode line. */
22169 static void
22170 note_mode_line_or_margin_highlight (window, x, y, area)
22171 Lisp_Object window;
22172 int x, y;
22173 enum window_part area;
22175 struct window *w = XWINDOW (window);
22176 struct frame *f = XFRAME (w->frame);
22177 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22178 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22179 Lisp_Object pointer = Qnil;
22180 int charpos, dx, dy, width, height;
22181 Lisp_Object string, object = Qnil;
22182 Lisp_Object pos, help;
22184 Lisp_Object mouse_face;
22185 int original_x_pixel = x;
22186 struct glyph * glyph = NULL;
22187 struct glyph_row *row;
22189 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22191 int x0;
22192 struct glyph *end;
22194 string = mode_line_string (w, area, &x, &y, &charpos,
22195 &object, &dx, &dy, &width, &height);
22197 row = (area == ON_MODE_LINE
22198 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22199 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22201 /* Find glyph */
22202 if (row->mode_line_p && row->enabled_p)
22204 glyph = row->glyphs[TEXT_AREA];
22205 end = glyph + row->used[TEXT_AREA];
22207 for (x0 = original_x_pixel;
22208 glyph < end && x0 >= glyph->pixel_width;
22209 ++glyph)
22210 x0 -= glyph->pixel_width;
22212 if (glyph >= end)
22213 glyph = NULL;
22216 else
22218 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22219 string = marginal_area_string (w, area, &x, &y, &charpos,
22220 &object, &dx, &dy, &width, &height);
22223 help = Qnil;
22225 if (IMAGEP (object))
22227 Lisp_Object image_map, hotspot;
22228 if ((image_map = Fplist_get (XCDR (object), QCmap),
22229 !NILP (image_map))
22230 && (hotspot = find_hot_spot (image_map, dx, dy),
22231 CONSP (hotspot))
22232 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22234 Lisp_Object area_id, plist;
22236 area_id = XCAR (hotspot);
22237 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22238 If so, we could look for mouse-enter, mouse-leave
22239 properties in PLIST (and do something...). */
22240 hotspot = XCDR (hotspot);
22241 if (CONSP (hotspot)
22242 && (plist = XCAR (hotspot), CONSP (plist)))
22244 pointer = Fplist_get (plist, Qpointer);
22245 if (NILP (pointer))
22246 pointer = Qhand;
22247 help = Fplist_get (plist, Qhelp_echo);
22248 if (!NILP (help))
22250 help_echo_string = help;
22251 /* Is this correct? ++kfs */
22252 XSETWINDOW (help_echo_window, w);
22253 help_echo_object = w->buffer;
22254 help_echo_pos = charpos;
22258 if (NILP (pointer))
22259 pointer = Fplist_get (XCDR (object), QCpointer);
22262 if (STRINGP (string))
22264 pos = make_number (charpos);
22265 /* If we're on a string with `help-echo' text property, arrange
22266 for the help to be displayed. This is done by setting the
22267 global variable help_echo_string to the help string. */
22268 if (NILP (help))
22270 help = Fget_text_property (pos, Qhelp_echo, string);
22271 if (!NILP (help))
22273 help_echo_string = help;
22274 XSETWINDOW (help_echo_window, w);
22275 help_echo_object = string;
22276 help_echo_pos = charpos;
22280 if (NILP (pointer))
22281 pointer = Fget_text_property (pos, Qpointer, string);
22283 /* Change the mouse pointer according to what is under X/Y. */
22284 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22286 Lisp_Object map;
22287 map = Fget_text_property (pos, Qlocal_map, string);
22288 if (!KEYMAPP (map))
22289 map = Fget_text_property (pos, Qkeymap, string);
22290 if (!KEYMAPP (map))
22291 cursor = dpyinfo->vertical_scroll_bar_cursor;
22294 /* Change the mouse face according to what is under X/Y. */
22295 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22296 if (!NILP (mouse_face)
22297 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22298 && glyph)
22300 Lisp_Object b, e;
22302 struct glyph * tmp_glyph;
22304 int gpos;
22305 int gseq_length;
22306 int total_pixel_width;
22307 int ignore;
22309 int vpos, hpos;
22311 b = Fprevious_single_property_change (make_number (charpos + 1),
22312 Qmouse_face, string, Qnil);
22313 if (NILP (b))
22314 b = make_number (0);
22316 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22317 if (NILP (e))
22318 e = make_number (SCHARS (string));
22320 /* Calculate the position(glyph position: GPOS) of GLYPH in
22321 displayed string. GPOS is different from CHARPOS.
22323 CHARPOS is the position of glyph in internal string
22324 object. A mode line string format has structures which
22325 is converted to a flatten by emacs lisp interpreter.
22326 The internal string is an element of the structures.
22327 The displayed string is the flatten string. */
22328 for (tmp_glyph = glyph - 1, gpos = 0;
22329 tmp_glyph->charpos >= XINT (b);
22330 tmp_glyph--, gpos++)
22332 if (!EQ (tmp_glyph->object, glyph->object))
22333 break;
22336 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22337 displayed string holding GLYPH.
22339 GSEQ_LENGTH is different from SCHARS (STRING).
22340 SCHARS (STRING) returns the length of the internal string. */
22341 for (tmp_glyph = glyph, gseq_length = gpos;
22342 tmp_glyph->charpos < XINT (e);
22343 tmp_glyph++, gseq_length++)
22345 if (!EQ (tmp_glyph->object, glyph->object))
22346 break;
22349 total_pixel_width = 0;
22350 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22351 total_pixel_width += tmp_glyph->pixel_width;
22353 /* Pre calculation of re-rendering position */
22354 vpos = (x - gpos);
22355 hpos = (area == ON_MODE_LINE
22356 ? (w->current_matrix)->nrows - 1
22357 : 0);
22359 /* If the re-rendering position is included in the last
22360 re-rendering area, we should do nothing. */
22361 if ( EQ (window, dpyinfo->mouse_face_window)
22362 && dpyinfo->mouse_face_beg_col <= vpos
22363 && vpos < dpyinfo->mouse_face_end_col
22364 && dpyinfo->mouse_face_beg_row == hpos )
22365 return;
22367 if (clear_mouse_face (dpyinfo))
22368 cursor = No_Cursor;
22370 dpyinfo->mouse_face_beg_col = vpos;
22371 dpyinfo->mouse_face_beg_row = hpos;
22373 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22374 dpyinfo->mouse_face_beg_y = 0;
22376 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22377 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22379 dpyinfo->mouse_face_end_x = 0;
22380 dpyinfo->mouse_face_end_y = 0;
22382 dpyinfo->mouse_face_past_end = 0;
22383 dpyinfo->mouse_face_window = window;
22385 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22386 charpos,
22387 0, 0, 0, &ignore,
22388 glyph->face_id, 1);
22389 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22391 if (NILP (pointer))
22392 pointer = Qhand;
22394 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22395 clear_mouse_face (dpyinfo);
22397 define_frame_cursor1 (f, cursor, pointer);
22401 /* EXPORT:
22402 Take proper action when the mouse has moved to position X, Y on
22403 frame F as regards highlighting characters that have mouse-face
22404 properties. Also de-highlighting chars where the mouse was before.
22405 X and Y can be negative or out of range. */
22407 void
22408 note_mouse_highlight (f, x, y)
22409 struct frame *f;
22410 int x, y;
22412 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22413 enum window_part part;
22414 Lisp_Object window;
22415 struct window *w;
22416 Cursor cursor = No_Cursor;
22417 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22418 struct buffer *b;
22420 /* When a menu is active, don't highlight because this looks odd. */
22421 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22422 if (popup_activated ())
22423 return;
22424 #endif
22426 if (NILP (Vmouse_highlight)
22427 || !f->glyphs_initialized_p)
22428 return;
22430 dpyinfo->mouse_face_mouse_x = x;
22431 dpyinfo->mouse_face_mouse_y = y;
22432 dpyinfo->mouse_face_mouse_frame = f;
22434 if (dpyinfo->mouse_face_defer)
22435 return;
22437 if (gc_in_progress)
22439 dpyinfo->mouse_face_deferred_gc = 1;
22440 return;
22443 /* Which window is that in? */
22444 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22446 /* If we were displaying active text in another window, clear that.
22447 Also clear if we move out of text area in same window. */
22448 if (! EQ (window, dpyinfo->mouse_face_window)
22449 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22450 && !NILP (dpyinfo->mouse_face_window)))
22451 clear_mouse_face (dpyinfo);
22453 /* Not on a window -> return. */
22454 if (!WINDOWP (window))
22455 return;
22457 /* Reset help_echo_string. It will get recomputed below. */
22458 help_echo_string = Qnil;
22460 /* Convert to window-relative pixel coordinates. */
22461 w = XWINDOW (window);
22462 frame_to_window_pixel_xy (w, &x, &y);
22464 /* Handle tool-bar window differently since it doesn't display a
22465 buffer. */
22466 if (EQ (window, f->tool_bar_window))
22468 note_tool_bar_highlight (f, x, y);
22469 return;
22472 /* Mouse is on the mode, header line or margin? */
22473 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22474 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22476 note_mode_line_or_margin_highlight (window, x, y, part);
22477 return;
22480 if (part == ON_VERTICAL_BORDER)
22482 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22483 help_echo_string = build_string ("drag-mouse-1: resize");
22485 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22486 || part == ON_SCROLL_BAR)
22487 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22488 else
22489 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22491 /* Are we in a window whose display is up to date?
22492 And verify the buffer's text has not changed. */
22493 b = XBUFFER (w->buffer);
22494 if (part == ON_TEXT
22495 && EQ (w->window_end_valid, w->buffer)
22496 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22497 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22499 int hpos, vpos, pos, i, dx, dy, area;
22500 struct glyph *glyph;
22501 Lisp_Object object;
22502 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22503 Lisp_Object *overlay_vec = NULL;
22504 int noverlays;
22505 struct buffer *obuf;
22506 int obegv, ozv, same_region;
22508 /* Find the glyph under X/Y. */
22509 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22511 /* Look for :pointer property on image. */
22512 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22514 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22515 if (img != NULL && IMAGEP (img->spec))
22517 Lisp_Object image_map, hotspot;
22518 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22519 !NILP (image_map))
22520 && (hotspot = find_hot_spot (image_map,
22521 glyph->slice.x + dx,
22522 glyph->slice.y + dy),
22523 CONSP (hotspot))
22524 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22526 Lisp_Object area_id, plist;
22528 area_id = XCAR (hotspot);
22529 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22530 If so, we could look for mouse-enter, mouse-leave
22531 properties in PLIST (and do something...). */
22532 hotspot = XCDR (hotspot);
22533 if (CONSP (hotspot)
22534 && (plist = XCAR (hotspot), CONSP (plist)))
22536 pointer = Fplist_get (plist, Qpointer);
22537 if (NILP (pointer))
22538 pointer = Qhand;
22539 help_echo_string = Fplist_get (plist, Qhelp_echo);
22540 if (!NILP (help_echo_string))
22542 help_echo_window = window;
22543 help_echo_object = glyph->object;
22544 help_echo_pos = glyph->charpos;
22548 if (NILP (pointer))
22549 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22553 /* Clear mouse face if X/Y not over text. */
22554 if (glyph == NULL
22555 || area != TEXT_AREA
22556 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22558 if (clear_mouse_face (dpyinfo))
22559 cursor = No_Cursor;
22560 if (NILP (pointer))
22562 if (area != TEXT_AREA)
22563 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22564 else
22565 pointer = Vvoid_text_area_pointer;
22567 goto set_cursor;
22570 pos = glyph->charpos;
22571 object = glyph->object;
22572 if (!STRINGP (object) && !BUFFERP (object))
22573 goto set_cursor;
22575 /* If we get an out-of-range value, return now; avoid an error. */
22576 if (BUFFERP (object) && pos > BUF_Z (b))
22577 goto set_cursor;
22579 /* Make the window's buffer temporarily current for
22580 overlays_at and compute_char_face. */
22581 obuf = current_buffer;
22582 current_buffer = b;
22583 obegv = BEGV;
22584 ozv = ZV;
22585 BEGV = BEG;
22586 ZV = Z;
22588 /* Is this char mouse-active or does it have help-echo? */
22589 position = make_number (pos);
22591 if (BUFFERP (object))
22593 /* Put all the overlays we want in a vector in overlay_vec. */
22594 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22595 /* Sort overlays into increasing priority order. */
22596 noverlays = sort_overlays (overlay_vec, noverlays, w);
22598 else
22599 noverlays = 0;
22601 same_region = (EQ (window, dpyinfo->mouse_face_window)
22602 && vpos >= dpyinfo->mouse_face_beg_row
22603 && vpos <= dpyinfo->mouse_face_end_row
22604 && (vpos > dpyinfo->mouse_face_beg_row
22605 || hpos >= dpyinfo->mouse_face_beg_col)
22606 && (vpos < dpyinfo->mouse_face_end_row
22607 || hpos < dpyinfo->mouse_face_end_col
22608 || dpyinfo->mouse_face_past_end));
22610 if (same_region)
22611 cursor = No_Cursor;
22613 /* Check mouse-face highlighting. */
22614 if (! same_region
22615 /* If there exists an overlay with mouse-face overlapping
22616 the one we are currently highlighting, we have to
22617 check if we enter the overlapping overlay, and then
22618 highlight only that. */
22619 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22620 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22622 /* Find the highest priority overlay that has a mouse-face
22623 property. */
22624 overlay = Qnil;
22625 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22627 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22628 if (!NILP (mouse_face))
22629 overlay = overlay_vec[i];
22632 /* If we're actually highlighting the same overlay as
22633 before, there's no need to do that again. */
22634 if (!NILP (overlay)
22635 && EQ (overlay, dpyinfo->mouse_face_overlay))
22636 goto check_help_echo;
22638 dpyinfo->mouse_face_overlay = overlay;
22640 /* Clear the display of the old active region, if any. */
22641 if (clear_mouse_face (dpyinfo))
22642 cursor = No_Cursor;
22644 /* If no overlay applies, get a text property. */
22645 if (NILP (overlay))
22646 mouse_face = Fget_text_property (position, Qmouse_face, object);
22648 /* Handle the overlay case. */
22649 if (!NILP (overlay))
22651 /* Find the range of text around this char that
22652 should be active. */
22653 Lisp_Object before, after;
22654 int ignore;
22656 before = Foverlay_start (overlay);
22657 after = Foverlay_end (overlay);
22658 /* Record this as the current active region. */
22659 fast_find_position (w, XFASTINT (before),
22660 &dpyinfo->mouse_face_beg_col,
22661 &dpyinfo->mouse_face_beg_row,
22662 &dpyinfo->mouse_face_beg_x,
22663 &dpyinfo->mouse_face_beg_y, Qnil);
22665 dpyinfo->mouse_face_past_end
22666 = !fast_find_position (w, XFASTINT (after),
22667 &dpyinfo->mouse_face_end_col,
22668 &dpyinfo->mouse_face_end_row,
22669 &dpyinfo->mouse_face_end_x,
22670 &dpyinfo->mouse_face_end_y, Qnil);
22671 dpyinfo->mouse_face_window = window;
22673 dpyinfo->mouse_face_face_id
22674 = face_at_buffer_position (w, pos, 0, 0,
22675 &ignore, pos + 1,
22676 !dpyinfo->mouse_face_hidden);
22678 /* Display it as active. */
22679 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22680 cursor = No_Cursor;
22682 /* Handle the text property case. */
22683 else if (!NILP (mouse_face) && BUFFERP (object))
22685 /* Find the range of text around this char that
22686 should be active. */
22687 Lisp_Object before, after, beginning, end;
22688 int ignore;
22690 beginning = Fmarker_position (w->start);
22691 end = make_number (BUF_Z (XBUFFER (object))
22692 - XFASTINT (w->window_end_pos));
22693 before
22694 = Fprevious_single_property_change (make_number (pos + 1),
22695 Qmouse_face,
22696 object, beginning);
22697 after
22698 = Fnext_single_property_change (position, Qmouse_face,
22699 object, end);
22701 /* Record this as the current active region. */
22702 fast_find_position (w, XFASTINT (before),
22703 &dpyinfo->mouse_face_beg_col,
22704 &dpyinfo->mouse_face_beg_row,
22705 &dpyinfo->mouse_face_beg_x,
22706 &dpyinfo->mouse_face_beg_y, Qnil);
22707 dpyinfo->mouse_face_past_end
22708 = !fast_find_position (w, XFASTINT (after),
22709 &dpyinfo->mouse_face_end_col,
22710 &dpyinfo->mouse_face_end_row,
22711 &dpyinfo->mouse_face_end_x,
22712 &dpyinfo->mouse_face_end_y, Qnil);
22713 dpyinfo->mouse_face_window = window;
22715 if (BUFFERP (object))
22716 dpyinfo->mouse_face_face_id
22717 = face_at_buffer_position (w, pos, 0, 0,
22718 &ignore, pos + 1,
22719 !dpyinfo->mouse_face_hidden);
22721 /* Display it as active. */
22722 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22723 cursor = No_Cursor;
22725 else if (!NILP (mouse_face) && STRINGP (object))
22727 Lisp_Object b, e;
22728 int ignore;
22730 b = Fprevious_single_property_change (make_number (pos + 1),
22731 Qmouse_face,
22732 object, Qnil);
22733 e = Fnext_single_property_change (position, Qmouse_face,
22734 object, Qnil);
22735 if (NILP (b))
22736 b = make_number (0);
22737 if (NILP (e))
22738 e = make_number (SCHARS (object) - 1);
22740 fast_find_string_pos (w, XINT (b), object,
22741 &dpyinfo->mouse_face_beg_col,
22742 &dpyinfo->mouse_face_beg_row,
22743 &dpyinfo->mouse_face_beg_x,
22744 &dpyinfo->mouse_face_beg_y, 0);
22745 fast_find_string_pos (w, XINT (e), object,
22746 &dpyinfo->mouse_face_end_col,
22747 &dpyinfo->mouse_face_end_row,
22748 &dpyinfo->mouse_face_end_x,
22749 &dpyinfo->mouse_face_end_y, 1);
22750 dpyinfo->mouse_face_past_end = 0;
22751 dpyinfo->mouse_face_window = window;
22752 dpyinfo->mouse_face_face_id
22753 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22754 glyph->face_id, 1);
22755 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22756 cursor = No_Cursor;
22758 else if (STRINGP (object) && NILP (mouse_face))
22760 /* A string which doesn't have mouse-face, but
22761 the text ``under'' it might have. */
22762 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22763 int start = MATRIX_ROW_START_CHARPOS (r);
22765 pos = string_buffer_position (w, object, start);
22766 if (pos > 0)
22767 mouse_face = get_char_property_and_overlay (make_number (pos),
22768 Qmouse_face,
22769 w->buffer,
22770 &overlay);
22771 if (!NILP (mouse_face) && !NILP (overlay))
22773 Lisp_Object before = Foverlay_start (overlay);
22774 Lisp_Object after = Foverlay_end (overlay);
22775 int ignore;
22777 /* Note that we might not be able to find position
22778 BEFORE in the glyph matrix if the overlay is
22779 entirely covered by a `display' property. In
22780 this case, we overshoot. So let's stop in
22781 the glyph matrix before glyphs for OBJECT. */
22782 fast_find_position (w, XFASTINT (before),
22783 &dpyinfo->mouse_face_beg_col,
22784 &dpyinfo->mouse_face_beg_row,
22785 &dpyinfo->mouse_face_beg_x,
22786 &dpyinfo->mouse_face_beg_y,
22787 object);
22789 dpyinfo->mouse_face_past_end
22790 = !fast_find_position (w, XFASTINT (after),
22791 &dpyinfo->mouse_face_end_col,
22792 &dpyinfo->mouse_face_end_row,
22793 &dpyinfo->mouse_face_end_x,
22794 &dpyinfo->mouse_face_end_y,
22795 Qnil);
22796 dpyinfo->mouse_face_window = window;
22797 dpyinfo->mouse_face_face_id
22798 = face_at_buffer_position (w, pos, 0, 0,
22799 &ignore, pos + 1,
22800 !dpyinfo->mouse_face_hidden);
22802 /* Display it as active. */
22803 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22804 cursor = No_Cursor;
22809 check_help_echo:
22811 /* Look for a `help-echo' property. */
22812 if (NILP (help_echo_string)) {
22813 Lisp_Object help, overlay;
22815 /* Check overlays first. */
22816 help = overlay = Qnil;
22817 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22819 overlay = overlay_vec[i];
22820 help = Foverlay_get (overlay, Qhelp_echo);
22823 if (!NILP (help))
22825 help_echo_string = help;
22826 help_echo_window = window;
22827 help_echo_object = overlay;
22828 help_echo_pos = pos;
22830 else
22832 Lisp_Object object = glyph->object;
22833 int charpos = glyph->charpos;
22835 /* Try text properties. */
22836 if (STRINGP (object)
22837 && charpos >= 0
22838 && charpos < SCHARS (object))
22840 help = Fget_text_property (make_number (charpos),
22841 Qhelp_echo, object);
22842 if (NILP (help))
22844 /* If the string itself doesn't specify a help-echo,
22845 see if the buffer text ``under'' it does. */
22846 struct glyph_row *r
22847 = MATRIX_ROW (w->current_matrix, vpos);
22848 int start = MATRIX_ROW_START_CHARPOS (r);
22849 int pos = string_buffer_position (w, object, start);
22850 if (pos > 0)
22852 help = Fget_char_property (make_number (pos),
22853 Qhelp_echo, w->buffer);
22854 if (!NILP (help))
22856 charpos = pos;
22857 object = w->buffer;
22862 else if (BUFFERP (object)
22863 && charpos >= BEGV
22864 && charpos < ZV)
22865 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22866 object);
22868 if (!NILP (help))
22870 help_echo_string = help;
22871 help_echo_window = window;
22872 help_echo_object = object;
22873 help_echo_pos = charpos;
22878 /* Look for a `pointer' property. */
22879 if (NILP (pointer))
22881 /* Check overlays first. */
22882 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22883 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22885 if (NILP (pointer))
22887 Lisp_Object object = glyph->object;
22888 int charpos = glyph->charpos;
22890 /* Try text properties. */
22891 if (STRINGP (object)
22892 && charpos >= 0
22893 && charpos < SCHARS (object))
22895 pointer = Fget_text_property (make_number (charpos),
22896 Qpointer, object);
22897 if (NILP (pointer))
22899 /* If the string itself doesn't specify a pointer,
22900 see if the buffer text ``under'' it does. */
22901 struct glyph_row *r
22902 = MATRIX_ROW (w->current_matrix, vpos);
22903 int start = MATRIX_ROW_START_CHARPOS (r);
22904 int pos = string_buffer_position (w, object, start);
22905 if (pos > 0)
22906 pointer = Fget_char_property (make_number (pos),
22907 Qpointer, w->buffer);
22910 else if (BUFFERP (object)
22911 && charpos >= BEGV
22912 && charpos < ZV)
22913 pointer = Fget_text_property (make_number (charpos),
22914 Qpointer, object);
22918 BEGV = obegv;
22919 ZV = ozv;
22920 current_buffer = obuf;
22923 set_cursor:
22925 define_frame_cursor1 (f, cursor, pointer);
22929 /* EXPORT for RIF:
22930 Clear any mouse-face on window W. This function is part of the
22931 redisplay interface, and is called from try_window_id and similar
22932 functions to ensure the mouse-highlight is off. */
22934 void
22935 x_clear_window_mouse_face (w)
22936 struct window *w;
22938 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22939 Lisp_Object window;
22941 BLOCK_INPUT;
22942 XSETWINDOW (window, w);
22943 if (EQ (window, dpyinfo->mouse_face_window))
22944 clear_mouse_face (dpyinfo);
22945 UNBLOCK_INPUT;
22949 /* EXPORT:
22950 Just discard the mouse face information for frame F, if any.
22951 This is used when the size of F is changed. */
22953 void
22954 cancel_mouse_face (f)
22955 struct frame *f;
22957 Lisp_Object window;
22958 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22960 window = dpyinfo->mouse_face_window;
22961 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22963 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22964 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22965 dpyinfo->mouse_face_window = Qnil;
22970 #endif /* HAVE_WINDOW_SYSTEM */
22973 /***********************************************************************
22974 Exposure Events
22975 ***********************************************************************/
22977 #ifdef HAVE_WINDOW_SYSTEM
22979 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22980 which intersects rectangle R. R is in window-relative coordinates. */
22982 static void
22983 expose_area (w, row, r, area)
22984 struct window *w;
22985 struct glyph_row *row;
22986 XRectangle *r;
22987 enum glyph_row_area area;
22989 struct glyph *first = row->glyphs[area];
22990 struct glyph *end = row->glyphs[area] + row->used[area];
22991 struct glyph *last;
22992 int first_x, start_x, x;
22994 if (area == TEXT_AREA && row->fill_line_p)
22995 /* If row extends face to end of line write the whole line. */
22996 draw_glyphs (w, 0, row, area,
22997 0, row->used[area],
22998 DRAW_NORMAL_TEXT, 0);
22999 else
23001 /* Set START_X to the window-relative start position for drawing glyphs of
23002 AREA. The first glyph of the text area can be partially visible.
23003 The first glyphs of other areas cannot. */
23004 start_x = window_box_left_offset (w, area);
23005 x = start_x;
23006 if (area == TEXT_AREA)
23007 x += row->x;
23009 /* Find the first glyph that must be redrawn. */
23010 while (first < end
23011 && x + first->pixel_width < r->x)
23013 x += first->pixel_width;
23014 ++first;
23017 /* Find the last one. */
23018 last = first;
23019 first_x = x;
23020 while (last < end
23021 && x < r->x + r->width)
23023 x += last->pixel_width;
23024 ++last;
23027 /* Repaint. */
23028 if (last > first)
23029 draw_glyphs (w, first_x - start_x, row, area,
23030 first - row->glyphs[area], last - row->glyphs[area],
23031 DRAW_NORMAL_TEXT, 0);
23036 /* Redraw the parts of the glyph row ROW on window W intersecting
23037 rectangle R. R is in window-relative coordinates. Value is
23038 non-zero if mouse-face was overwritten. */
23040 static int
23041 expose_line (w, row, r)
23042 struct window *w;
23043 struct glyph_row *row;
23044 XRectangle *r;
23046 xassert (row->enabled_p);
23048 if (row->mode_line_p || w->pseudo_window_p)
23049 draw_glyphs (w, 0, row, TEXT_AREA,
23050 0, row->used[TEXT_AREA],
23051 DRAW_NORMAL_TEXT, 0);
23052 else
23054 if (row->used[LEFT_MARGIN_AREA])
23055 expose_area (w, row, r, LEFT_MARGIN_AREA);
23056 if (row->used[TEXT_AREA])
23057 expose_area (w, row, r, TEXT_AREA);
23058 if (row->used[RIGHT_MARGIN_AREA])
23059 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23060 draw_row_fringe_bitmaps (w, row);
23063 return row->mouse_face_p;
23067 /* Redraw those parts of glyphs rows during expose event handling that
23068 overlap other rows. Redrawing of an exposed line writes over parts
23069 of lines overlapping that exposed line; this function fixes that.
23071 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23072 row in W's current matrix that is exposed and overlaps other rows.
23073 LAST_OVERLAPPING_ROW is the last such row. */
23075 static void
23076 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23077 struct window *w;
23078 struct glyph_row *first_overlapping_row;
23079 struct glyph_row *last_overlapping_row;
23081 struct glyph_row *row;
23083 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23084 if (row->overlapping_p)
23086 xassert (row->enabled_p && !row->mode_line_p);
23088 if (row->used[LEFT_MARGIN_AREA])
23089 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23091 if (row->used[TEXT_AREA])
23092 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23094 if (row->used[RIGHT_MARGIN_AREA])
23095 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23100 /* Return non-zero if W's cursor intersects rectangle R. */
23102 static int
23103 phys_cursor_in_rect_p (w, r)
23104 struct window *w;
23105 XRectangle *r;
23107 XRectangle cr, result;
23108 struct glyph *cursor_glyph;
23110 cursor_glyph = get_phys_cursor_glyph (w);
23111 if (cursor_glyph)
23113 /* r is relative to W's box, but w->phys_cursor.x is relative
23114 to left edge of W's TEXT area. Adjust it. */
23115 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23116 cr.y = w->phys_cursor.y;
23117 cr.width = cursor_glyph->pixel_width;
23118 cr.height = w->phys_cursor_height;
23119 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23120 I assume the effect is the same -- and this is portable. */
23121 return x_intersect_rectangles (&cr, r, &result);
23123 else
23124 return 0;
23128 /* EXPORT:
23129 Draw a vertical window border to the right of window W if W doesn't
23130 have vertical scroll bars. */
23132 void
23133 x_draw_vertical_border (w)
23134 struct window *w;
23136 /* We could do better, if we knew what type of scroll-bar the adjacent
23137 windows (on either side) have... But we don't :-(
23138 However, I think this works ok. ++KFS 2003-04-25 */
23140 /* Redraw borders between horizontally adjacent windows. Don't
23141 do it for frames with vertical scroll bars because either the
23142 right scroll bar of a window, or the left scroll bar of its
23143 neighbor will suffice as a border. */
23144 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23145 return;
23147 if (!WINDOW_RIGHTMOST_P (w)
23148 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23150 int x0, x1, y0, y1;
23152 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23153 y1 -= 1;
23155 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23156 x1 -= 1;
23158 rif->draw_vertical_window_border (w, x1, y0, y1);
23160 else if (!WINDOW_LEFTMOST_P (w)
23161 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23163 int x0, x1, y0, y1;
23165 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23166 y1 -= 1;
23168 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23169 x0 -= 1;
23171 rif->draw_vertical_window_border (w, x0, y0, y1);
23176 /* Redraw the part of window W intersection rectangle FR. Pixel
23177 coordinates in FR are frame-relative. Call this function with
23178 input blocked. Value is non-zero if the exposure overwrites
23179 mouse-face. */
23181 static int
23182 expose_window (w, fr)
23183 struct window *w;
23184 XRectangle *fr;
23186 struct frame *f = XFRAME (w->frame);
23187 XRectangle wr, r;
23188 int mouse_face_overwritten_p = 0;
23190 /* If window is not yet fully initialized, do nothing. This can
23191 happen when toolkit scroll bars are used and a window is split.
23192 Reconfiguring the scroll bar will generate an expose for a newly
23193 created window. */
23194 if (w->current_matrix == NULL)
23195 return 0;
23197 /* When we're currently updating the window, display and current
23198 matrix usually don't agree. Arrange for a thorough display
23199 later. */
23200 if (w == updated_window)
23202 SET_FRAME_GARBAGED (f);
23203 return 0;
23206 /* Frame-relative pixel rectangle of W. */
23207 wr.x = WINDOW_LEFT_EDGE_X (w);
23208 wr.y = WINDOW_TOP_EDGE_Y (w);
23209 wr.width = WINDOW_TOTAL_WIDTH (w);
23210 wr.height = WINDOW_TOTAL_HEIGHT (w);
23212 if (x_intersect_rectangles (fr, &wr, &r))
23214 int yb = window_text_bottom_y (w);
23215 struct glyph_row *row;
23216 int cursor_cleared_p;
23217 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23219 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23220 r.x, r.y, r.width, r.height));
23222 /* Convert to window coordinates. */
23223 r.x -= WINDOW_LEFT_EDGE_X (w);
23224 r.y -= WINDOW_TOP_EDGE_Y (w);
23226 /* Turn off the cursor. */
23227 if (!w->pseudo_window_p
23228 && phys_cursor_in_rect_p (w, &r))
23230 x_clear_cursor (w);
23231 cursor_cleared_p = 1;
23233 else
23234 cursor_cleared_p = 0;
23236 /* Update lines intersecting rectangle R. */
23237 first_overlapping_row = last_overlapping_row = NULL;
23238 for (row = w->current_matrix->rows;
23239 row->enabled_p;
23240 ++row)
23242 int y0 = row->y;
23243 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23245 if ((y0 >= r.y && y0 < r.y + r.height)
23246 || (y1 > r.y && y1 < r.y + r.height)
23247 || (r.y >= y0 && r.y < y1)
23248 || (r.y + r.height > y0 && r.y + r.height < y1))
23250 /* A header line may be overlapping, but there is no need
23251 to fix overlapping areas for them. KFS 2005-02-12 */
23252 if (row->overlapping_p && !row->mode_line_p)
23254 if (first_overlapping_row == NULL)
23255 first_overlapping_row = row;
23256 last_overlapping_row = row;
23259 if (expose_line (w, row, &r))
23260 mouse_face_overwritten_p = 1;
23263 if (y1 >= yb)
23264 break;
23267 /* Display the mode line if there is one. */
23268 if (WINDOW_WANTS_MODELINE_P (w)
23269 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23270 row->enabled_p)
23271 && row->y < r.y + r.height)
23273 if (expose_line (w, row, &r))
23274 mouse_face_overwritten_p = 1;
23277 if (!w->pseudo_window_p)
23279 /* Fix the display of overlapping rows. */
23280 if (first_overlapping_row)
23281 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23283 /* Draw border between windows. */
23284 x_draw_vertical_border (w);
23286 /* Turn the cursor on again. */
23287 if (cursor_cleared_p)
23288 update_window_cursor (w, 1);
23292 return mouse_face_overwritten_p;
23297 /* Redraw (parts) of all windows in the window tree rooted at W that
23298 intersect R. R contains frame pixel coordinates. Value is
23299 non-zero if the exposure overwrites mouse-face. */
23301 static int
23302 expose_window_tree (w, r)
23303 struct window *w;
23304 XRectangle *r;
23306 struct frame *f = XFRAME (w->frame);
23307 int mouse_face_overwritten_p = 0;
23309 while (w && !FRAME_GARBAGED_P (f))
23311 if (!NILP (w->hchild))
23312 mouse_face_overwritten_p
23313 |= expose_window_tree (XWINDOW (w->hchild), r);
23314 else if (!NILP (w->vchild))
23315 mouse_face_overwritten_p
23316 |= expose_window_tree (XWINDOW (w->vchild), r);
23317 else
23318 mouse_face_overwritten_p |= expose_window (w, r);
23320 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23323 return mouse_face_overwritten_p;
23327 /* EXPORT:
23328 Redisplay an exposed area of frame F. X and Y are the upper-left
23329 corner of the exposed rectangle. W and H are width and height of
23330 the exposed area. All are pixel values. W or H zero means redraw
23331 the entire frame. */
23333 void
23334 expose_frame (f, x, y, w, h)
23335 struct frame *f;
23336 int x, y, w, h;
23338 XRectangle r;
23339 int mouse_face_overwritten_p = 0;
23341 TRACE ((stderr, "expose_frame "));
23343 /* No need to redraw if frame will be redrawn soon. */
23344 if (FRAME_GARBAGED_P (f))
23346 TRACE ((stderr, " garbaged\n"));
23347 return;
23350 /* If basic faces haven't been realized yet, there is no point in
23351 trying to redraw anything. This can happen when we get an expose
23352 event while Emacs is starting, e.g. by moving another window. */
23353 if (FRAME_FACE_CACHE (f) == NULL
23354 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23356 TRACE ((stderr, " no faces\n"));
23357 return;
23360 if (w == 0 || h == 0)
23362 r.x = r.y = 0;
23363 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23364 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23366 else
23368 r.x = x;
23369 r.y = y;
23370 r.width = w;
23371 r.height = h;
23374 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23375 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23377 if (WINDOWP (f->tool_bar_window))
23378 mouse_face_overwritten_p
23379 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23381 #ifdef HAVE_X_WINDOWS
23382 #ifndef MSDOS
23383 #ifndef USE_X_TOOLKIT
23384 if (WINDOWP (f->menu_bar_window))
23385 mouse_face_overwritten_p
23386 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23387 #endif /* not USE_X_TOOLKIT */
23388 #endif
23389 #endif
23391 /* Some window managers support a focus-follows-mouse style with
23392 delayed raising of frames. Imagine a partially obscured frame,
23393 and moving the mouse into partially obscured mouse-face on that
23394 frame. The visible part of the mouse-face will be highlighted,
23395 then the WM raises the obscured frame. With at least one WM, KDE
23396 2.1, Emacs is not getting any event for the raising of the frame
23397 (even tried with SubstructureRedirectMask), only Expose events.
23398 These expose events will draw text normally, i.e. not
23399 highlighted. Which means we must redo the highlight here.
23400 Subsume it under ``we love X''. --gerd 2001-08-15 */
23401 /* Included in Windows version because Windows most likely does not
23402 do the right thing if any third party tool offers
23403 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23404 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23406 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23407 if (f == dpyinfo->mouse_face_mouse_frame)
23409 int x = dpyinfo->mouse_face_mouse_x;
23410 int y = dpyinfo->mouse_face_mouse_y;
23411 clear_mouse_face (dpyinfo);
23412 note_mouse_highlight (f, x, y);
23418 /* EXPORT:
23419 Determine the intersection of two rectangles R1 and R2. Return
23420 the intersection in *RESULT. Value is non-zero if RESULT is not
23421 empty. */
23424 x_intersect_rectangles (r1, r2, result)
23425 XRectangle *r1, *r2, *result;
23427 XRectangle *left, *right;
23428 XRectangle *upper, *lower;
23429 int intersection_p = 0;
23431 /* Rearrange so that R1 is the left-most rectangle. */
23432 if (r1->x < r2->x)
23433 left = r1, right = r2;
23434 else
23435 left = r2, right = r1;
23437 /* X0 of the intersection is right.x0, if this is inside R1,
23438 otherwise there is no intersection. */
23439 if (right->x <= left->x + left->width)
23441 result->x = right->x;
23443 /* The right end of the intersection is the minimum of the
23444 the right ends of left and right. */
23445 result->width = (min (left->x + left->width, right->x + right->width)
23446 - result->x);
23448 /* Same game for Y. */
23449 if (r1->y < r2->y)
23450 upper = r1, lower = r2;
23451 else
23452 upper = r2, lower = r1;
23454 /* The upper end of the intersection is lower.y0, if this is inside
23455 of upper. Otherwise, there is no intersection. */
23456 if (lower->y <= upper->y + upper->height)
23458 result->y = lower->y;
23460 /* The lower end of the intersection is the minimum of the lower
23461 ends of upper and lower. */
23462 result->height = (min (lower->y + lower->height,
23463 upper->y + upper->height)
23464 - result->y);
23465 intersection_p = 1;
23469 return intersection_p;
23472 #endif /* HAVE_WINDOW_SYSTEM */
23475 /***********************************************************************
23476 Initialization
23477 ***********************************************************************/
23479 void
23480 syms_of_xdisp ()
23482 Vwith_echo_area_save_vector = Qnil;
23483 staticpro (&Vwith_echo_area_save_vector);
23485 Vmessage_stack = Qnil;
23486 staticpro (&Vmessage_stack);
23488 Qinhibit_redisplay = intern ("inhibit-redisplay");
23489 staticpro (&Qinhibit_redisplay);
23491 message_dolog_marker1 = Fmake_marker ();
23492 staticpro (&message_dolog_marker1);
23493 message_dolog_marker2 = Fmake_marker ();
23494 staticpro (&message_dolog_marker2);
23495 message_dolog_marker3 = Fmake_marker ();
23496 staticpro (&message_dolog_marker3);
23498 #if GLYPH_DEBUG
23499 defsubr (&Sdump_frame_glyph_matrix);
23500 defsubr (&Sdump_glyph_matrix);
23501 defsubr (&Sdump_glyph_row);
23502 defsubr (&Sdump_tool_bar_row);
23503 defsubr (&Strace_redisplay);
23504 defsubr (&Strace_to_stderr);
23505 #endif
23506 #ifdef HAVE_WINDOW_SYSTEM
23507 defsubr (&Stool_bar_lines_needed);
23508 defsubr (&Slookup_image_map);
23509 #endif
23510 defsubr (&Sformat_mode_line);
23512 staticpro (&Qmenu_bar_update_hook);
23513 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23515 staticpro (&Qoverriding_terminal_local_map);
23516 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23518 staticpro (&Qoverriding_local_map);
23519 Qoverriding_local_map = intern ("overriding-local-map");
23521 staticpro (&Qwindow_scroll_functions);
23522 Qwindow_scroll_functions = intern ("window-scroll-functions");
23524 staticpro (&Qredisplay_end_trigger_functions);
23525 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23527 staticpro (&Qinhibit_point_motion_hooks);
23528 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23530 QCdata = intern (":data");
23531 staticpro (&QCdata);
23532 Qdisplay = intern ("display");
23533 staticpro (&Qdisplay);
23534 Qspace_width = intern ("space-width");
23535 staticpro (&Qspace_width);
23536 Qraise = intern ("raise");
23537 staticpro (&Qraise);
23538 Qslice = intern ("slice");
23539 staticpro (&Qslice);
23540 Qspace = intern ("space");
23541 staticpro (&Qspace);
23542 Qmargin = intern ("margin");
23543 staticpro (&Qmargin);
23544 Qpointer = intern ("pointer");
23545 staticpro (&Qpointer);
23546 Qleft_margin = intern ("left-margin");
23547 staticpro (&Qleft_margin);
23548 Qright_margin = intern ("right-margin");
23549 staticpro (&Qright_margin);
23550 Qcenter = intern ("center");
23551 staticpro (&Qcenter);
23552 Qline_height = intern ("line-height");
23553 staticpro (&Qline_height);
23554 QCalign_to = intern (":align-to");
23555 staticpro (&QCalign_to);
23556 QCrelative_width = intern (":relative-width");
23557 staticpro (&QCrelative_width);
23558 QCrelative_height = intern (":relative-height");
23559 staticpro (&QCrelative_height);
23560 QCeval = intern (":eval");
23561 staticpro (&QCeval);
23562 QCpropertize = intern (":propertize");
23563 staticpro (&QCpropertize);
23564 QCfile = intern (":file");
23565 staticpro (&QCfile);
23566 Qfontified = intern ("fontified");
23567 staticpro (&Qfontified);
23568 Qfontification_functions = intern ("fontification-functions");
23569 staticpro (&Qfontification_functions);
23570 Qtrailing_whitespace = intern ("trailing-whitespace");
23571 staticpro (&Qtrailing_whitespace);
23572 Qescape_glyph = intern ("escape-glyph");
23573 staticpro (&Qescape_glyph);
23574 Qnobreak_space = intern ("nobreak-space");
23575 staticpro (&Qnobreak_space);
23576 Qimage = intern ("image");
23577 staticpro (&Qimage);
23578 QCmap = intern (":map");
23579 staticpro (&QCmap);
23580 QCpointer = intern (":pointer");
23581 staticpro (&QCpointer);
23582 Qrect = intern ("rect");
23583 staticpro (&Qrect);
23584 Qcircle = intern ("circle");
23585 staticpro (&Qcircle);
23586 Qpoly = intern ("poly");
23587 staticpro (&Qpoly);
23588 Qmessage_truncate_lines = intern ("message-truncate-lines");
23589 staticpro (&Qmessage_truncate_lines);
23590 Qgrow_only = intern ("grow-only");
23591 staticpro (&Qgrow_only);
23592 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23593 staticpro (&Qinhibit_menubar_update);
23594 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23595 staticpro (&Qinhibit_eval_during_redisplay);
23596 Qposition = intern ("position");
23597 staticpro (&Qposition);
23598 Qbuffer_position = intern ("buffer-position");
23599 staticpro (&Qbuffer_position);
23600 Qobject = intern ("object");
23601 staticpro (&Qobject);
23602 Qbar = intern ("bar");
23603 staticpro (&Qbar);
23604 Qhbar = intern ("hbar");
23605 staticpro (&Qhbar);
23606 Qbox = intern ("box");
23607 staticpro (&Qbox);
23608 Qhollow = intern ("hollow");
23609 staticpro (&Qhollow);
23610 Qhand = intern ("hand");
23611 staticpro (&Qhand);
23612 Qarrow = intern ("arrow");
23613 staticpro (&Qarrow);
23614 Qtext = intern ("text");
23615 staticpro (&Qtext);
23616 Qrisky_local_variable = intern ("risky-local-variable");
23617 staticpro (&Qrisky_local_variable);
23618 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23619 staticpro (&Qinhibit_free_realized_faces);
23621 list_of_error = Fcons (Fcons (intern ("error"),
23622 Fcons (intern ("void-variable"), Qnil)),
23623 Qnil);
23624 staticpro (&list_of_error);
23626 Qlast_arrow_position = intern ("last-arrow-position");
23627 staticpro (&Qlast_arrow_position);
23628 Qlast_arrow_string = intern ("last-arrow-string");
23629 staticpro (&Qlast_arrow_string);
23631 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23632 staticpro (&Qoverlay_arrow_string);
23633 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23634 staticpro (&Qoverlay_arrow_bitmap);
23636 echo_buffer[0] = echo_buffer[1] = Qnil;
23637 staticpro (&echo_buffer[0]);
23638 staticpro (&echo_buffer[1]);
23640 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23641 staticpro (&echo_area_buffer[0]);
23642 staticpro (&echo_area_buffer[1]);
23644 Vmessages_buffer_name = build_string ("*Messages*");
23645 staticpro (&Vmessages_buffer_name);
23647 mode_line_proptrans_alist = Qnil;
23648 staticpro (&mode_line_proptrans_alist);
23649 mode_line_string_list = Qnil;
23650 staticpro (&mode_line_string_list);
23651 mode_line_string_face = Qnil;
23652 staticpro (&mode_line_string_face);
23653 mode_line_string_face_prop = Qnil;
23654 staticpro (&mode_line_string_face_prop);
23655 Vmode_line_unwind_vector = Qnil;
23656 staticpro (&Vmode_line_unwind_vector);
23658 help_echo_string = Qnil;
23659 staticpro (&help_echo_string);
23660 help_echo_object = Qnil;
23661 staticpro (&help_echo_object);
23662 help_echo_window = Qnil;
23663 staticpro (&help_echo_window);
23664 previous_help_echo_string = Qnil;
23665 staticpro (&previous_help_echo_string);
23666 help_echo_pos = -1;
23668 #ifdef HAVE_WINDOW_SYSTEM
23669 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23670 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23671 For example, if a block cursor is over a tab, it will be drawn as
23672 wide as that tab on the display. */);
23673 x_stretch_cursor_p = 0;
23674 #endif
23676 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23677 doc: /* *Non-nil means highlight trailing whitespace.
23678 The face used for trailing whitespace is `trailing-whitespace'. */);
23679 Vshow_trailing_whitespace = Qnil;
23681 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23682 doc: /* *Control highlighting of nobreak space and soft hyphen.
23683 A value of t means highlight the character itself (for nobreak space,
23684 use face `nobreak-space').
23685 A value of nil means no highlighting.
23686 Other values mean display the escape glyph followed by an ordinary
23687 space or ordinary hyphen. */);
23688 Vnobreak_char_display = Qt;
23690 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23691 doc: /* *The pointer shape to show in void text areas.
23692 A value of nil means to show the text pointer. Other options are `arrow',
23693 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23694 Vvoid_text_area_pointer = Qarrow;
23696 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23697 doc: /* Non-nil means don't actually do any redisplay.
23698 This is used for internal purposes. */);
23699 Vinhibit_redisplay = Qnil;
23701 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23702 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23703 Vglobal_mode_string = Qnil;
23705 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23706 doc: /* Marker for where to display an arrow on top of the buffer text.
23707 This must be the beginning of a line in order to work.
23708 See also `overlay-arrow-string'. */);
23709 Voverlay_arrow_position = Qnil;
23711 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23712 doc: /* String to display as an arrow in non-window frames.
23713 See also `overlay-arrow-position'. */);
23714 Voverlay_arrow_string = build_string ("=>");
23716 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23717 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23718 The symbols on this list are examined during redisplay to determine
23719 where to display overlay arrows. */);
23720 Voverlay_arrow_variable_list
23721 = Fcons (intern ("overlay-arrow-position"), Qnil);
23723 DEFVAR_INT ("scroll-step", &scroll_step,
23724 doc: /* *The number of lines to try scrolling a window by when point moves out.
23725 If that fails to bring point back on frame, point is centered instead.
23726 If this is zero, point is always centered after it moves off frame.
23727 If you want scrolling to always be a line at a time, you should set
23728 `scroll-conservatively' to a large value rather than set this to 1. */);
23730 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23731 doc: /* *Scroll up to this many lines, to bring point back on screen.
23732 A value of zero means to scroll the text to center point vertically
23733 in the window. */);
23734 scroll_conservatively = 0;
23736 DEFVAR_INT ("scroll-margin", &scroll_margin,
23737 doc: /* *Number of lines of margin at the top and bottom of a window.
23738 Recenter the window whenever point gets within this many lines
23739 of the top or bottom of the window. */);
23740 scroll_margin = 0;
23742 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23743 doc: /* Pixels per inch value for non-window system displays.
23744 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23745 Vdisplay_pixels_per_inch = make_float (72.0);
23747 #if GLYPH_DEBUG
23748 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23749 #endif
23751 DEFVAR_BOOL ("truncate-partial-width-windows",
23752 &truncate_partial_width_windows,
23753 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23754 truncate_partial_width_windows = 1;
23756 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23757 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23758 Any other value means to use the appropriate face, `mode-line',
23759 `header-line', or `menu' respectively. */);
23760 mode_line_inverse_video = 1;
23762 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23763 doc: /* *Maximum buffer size for which line number should be displayed.
23764 If the buffer is bigger than this, the line number does not appear
23765 in the mode line. A value of nil means no limit. */);
23766 Vline_number_display_limit = Qnil;
23768 DEFVAR_INT ("line-number-display-limit-width",
23769 &line_number_display_limit_width,
23770 doc: /* *Maximum line width (in characters) for line number display.
23771 If the average length of the lines near point is bigger than this, then the
23772 line number may be omitted from the mode line. */);
23773 line_number_display_limit_width = 200;
23775 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23776 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23777 highlight_nonselected_windows = 0;
23779 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23780 doc: /* Non-nil if more than one frame is visible on this display.
23781 Minibuffer-only frames don't count, but iconified frames do.
23782 This variable is not guaranteed to be accurate except while processing
23783 `frame-title-format' and `icon-title-format'. */);
23785 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23786 doc: /* Template for displaying the title bar of visible frames.
23787 \(Assuming the window manager supports this feature.)
23788 This variable has the same structure as `mode-line-format' (which see),
23789 and is used only on frames for which no explicit name has been set
23790 \(see `modify-frame-parameters'). */);
23792 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23793 doc: /* Template for displaying the title bar of an iconified frame.
23794 \(Assuming the window manager supports this feature.)
23795 This variable has the same structure as `mode-line-format' (which see),
23796 and is used only on frames for which no explicit name has been set
23797 \(see `modify-frame-parameters'). */);
23798 Vicon_title_format
23799 = Vframe_title_format
23800 = Fcons (intern ("multiple-frames"),
23801 Fcons (build_string ("%b"),
23802 Fcons (Fcons (empty_string,
23803 Fcons (intern ("invocation-name"),
23804 Fcons (build_string ("@"),
23805 Fcons (intern ("system-name"),
23806 Qnil)))),
23807 Qnil)));
23809 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23810 doc: /* Maximum number of lines to keep in the message log buffer.
23811 If nil, disable message logging. If t, log messages but don't truncate
23812 the buffer when it becomes large. */);
23813 Vmessage_log_max = make_number (50);
23815 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23816 doc: /* Functions called before redisplay, if window sizes have changed.
23817 The value should be a list of functions that take one argument.
23818 Just before redisplay, for each frame, if any of its windows have changed
23819 size since the last redisplay, or have been split or deleted,
23820 all the functions in the list are called, with the frame as argument. */);
23821 Vwindow_size_change_functions = Qnil;
23823 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23824 doc: /* List of functions to call before redisplaying a window with scrolling.
23825 Each function is called with two arguments, the window
23826 and its new display-start position. Note that the value of `window-end'
23827 is not valid when these functions are called. */);
23828 Vwindow_scroll_functions = Qnil;
23830 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23831 doc: /* Functions called when redisplay of a window reaches the end trigger.
23832 Each function is called with two arguments, the window and the end trigger value.
23833 See `set-window-redisplay-end-trigger'. */);
23834 Vredisplay_end_trigger_functions = Qnil;
23836 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23837 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23838 mouse_autoselect_window = 0;
23840 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23841 doc: /* *Non-nil means automatically resize tool-bars.
23842 This increases a tool-bar's height if not all tool-bar items are visible.
23843 It decreases a tool-bar's height when it would display blank lines
23844 otherwise. */);
23845 auto_resize_tool_bars_p = 1;
23847 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23848 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23849 auto_raise_tool_bar_buttons_p = 1;
23851 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23852 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23853 make_cursor_line_fully_visible_p = 1;
23855 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23856 doc: /* *Border below tool-bar in pixels.
23857 If an integer, use it as the height of the border.
23858 If it is one of `internal-border-width' or `border-width', use the
23859 value of the corresponding frame parameter.
23860 Otherwise, no border is added below the tool-bar. */);
23861 Vtool_bar_border = Qinternal_border_width;
23863 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23864 doc: /* *Margin around tool-bar buttons in pixels.
23865 If an integer, use that for both horizontal and vertical margins.
23866 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23867 HORZ specifying the horizontal margin, and VERT specifying the
23868 vertical margin. */);
23869 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23871 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23872 doc: /* *Relief thickness of tool-bar buttons. */);
23873 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23875 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23876 doc: /* List of functions to call to fontify regions of text.
23877 Each function is called with one argument POS. Functions must
23878 fontify a region starting at POS in the current buffer, and give
23879 fontified regions the property `fontified'. */);
23880 Vfontification_functions = Qnil;
23881 Fmake_variable_buffer_local (Qfontification_functions);
23883 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23884 &unibyte_display_via_language_environment,
23885 doc: /* *Non-nil means display unibyte text according to language environment.
23886 Specifically this means that unibyte non-ASCII characters
23887 are displayed by converting them to the equivalent multibyte characters
23888 according to the current language environment. As a result, they are
23889 displayed according to the current fontset. */);
23890 unibyte_display_via_language_environment = 0;
23892 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23893 doc: /* *Maximum height for resizing mini-windows.
23894 If a float, it specifies a fraction of the mini-window frame's height.
23895 If an integer, it specifies a number of lines. */);
23896 Vmax_mini_window_height = make_float (0.25);
23898 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23899 doc: /* *How to resize mini-windows.
23900 A value of nil means don't automatically resize mini-windows.
23901 A value of t means resize them to fit the text displayed in them.
23902 A value of `grow-only', the default, means let mini-windows grow
23903 only, until their display becomes empty, at which point the windows
23904 go back to their normal size. */);
23905 Vresize_mini_windows = Qgrow_only;
23907 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23908 doc: /* Alist specifying how to blink the cursor off.
23909 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23910 `cursor-type' frame-parameter or variable equals ON-STATE,
23911 comparing using `equal', Emacs uses OFF-STATE to specify
23912 how to blink it off. ON-STATE and OFF-STATE are values for
23913 the `cursor-type' frame parameter.
23915 If a frame's ON-STATE has no entry in this list,
23916 the frame's other specifications determine how to blink the cursor off. */);
23917 Vblink_cursor_alist = Qnil;
23919 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23920 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23921 automatic_hscrolling_p = 1;
23923 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23924 doc: /* *How many columns away from the window edge point is allowed to get
23925 before automatic hscrolling will horizontally scroll the window. */);
23926 hscroll_margin = 5;
23928 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23929 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23930 When point is less than `hscroll-margin' columns from the window
23931 edge, automatic hscrolling will scroll the window by the amount of columns
23932 determined by this variable. If its value is a positive integer, scroll that
23933 many columns. If it's a positive floating-point number, it specifies the
23934 fraction of the window's width to scroll. If it's nil or zero, point will be
23935 centered horizontally after the scroll. Any other value, including negative
23936 numbers, are treated as if the value were zero.
23938 Automatic hscrolling always moves point outside the scroll margin, so if
23939 point was more than scroll step columns inside the margin, the window will
23940 scroll more than the value given by the scroll step.
23942 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23943 and `scroll-right' overrides this variable's effect. */);
23944 Vhscroll_step = make_number (0);
23946 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23947 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23948 Bind this around calls to `message' to let it take effect. */);
23949 message_truncate_lines = 0;
23951 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23952 doc: /* Normal hook run to update the menu bar definitions.
23953 Redisplay runs this hook before it redisplays the menu bar.
23954 This is used to update submenus such as Buffers,
23955 whose contents depend on various data. */);
23956 Vmenu_bar_update_hook = Qnil;
23958 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23959 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23960 inhibit_menubar_update = 0;
23962 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23963 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23964 inhibit_eval_during_redisplay = 0;
23966 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23967 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23968 inhibit_free_realized_faces = 0;
23970 #if GLYPH_DEBUG
23971 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23972 doc: /* Inhibit try_window_id display optimization. */);
23973 inhibit_try_window_id = 0;
23975 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23976 doc: /* Inhibit try_window_reusing display optimization. */);
23977 inhibit_try_window_reusing = 0;
23979 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23980 doc: /* Inhibit try_cursor_movement display optimization. */);
23981 inhibit_try_cursor_movement = 0;
23982 #endif /* GLYPH_DEBUG */
23986 /* Initialize this module when Emacs starts. */
23988 void
23989 init_xdisp ()
23991 Lisp_Object root_window;
23992 struct window *mini_w;
23994 current_header_line_height = current_mode_line_height = -1;
23996 CHARPOS (this_line_start_pos) = 0;
23998 mini_w = XWINDOW (minibuf_window);
23999 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24001 if (!noninteractive)
24003 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24004 int i;
24006 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24007 set_window_height (root_window,
24008 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24010 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24011 set_window_height (minibuf_window, 1, 0);
24013 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24014 mini_w->total_cols = make_number (FRAME_COLS (f));
24016 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24017 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24018 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24020 /* The default ellipsis glyphs `...'. */
24021 for (i = 0; i < 3; ++i)
24022 default_invis_vector[i] = make_number ('.');
24026 /* Allocate the buffer for frame titles.
24027 Also used for `format-mode-line'. */
24028 int size = 100;
24029 mode_line_noprop_buf = (char *) xmalloc (size);
24030 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24031 mode_line_noprop_ptr = mode_line_noprop_buf;
24032 mode_line_target = MODE_LINE_DISPLAY;
24035 help_echo_showing_p = 0;
24039 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24040 (do not change this comment) */