(bbdb/mail_auto_create_p, rmail-summary-mode-map): Add defvars.
[emacs.git] / src / xdisp.c
blobf81dfb6755a2a8189042c3fad714054b15c51b37
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 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 around tool bar buttons in pixels. */
274 Lisp_Object Vtool_bar_button_margin;
276 /* Thickness of shadow to draw around tool bar buttons. */
278 EMACS_INT tool_bar_button_relief;
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
283 int auto_resize_tool_bars_p;
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
289 int x_stretch_cursor_p;
291 /* Non-nil means don't actually do any redisplay. */
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
297 int inhibit_eval_during_redisplay;
299 /* Names of text properties relevant for redisplay. */
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
304 /* Symbols used in text property values. */
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
318 /* Non-nil means highlight trailing whitespace. */
320 Lisp_Object Vshow_trailing_whitespace;
322 /* Non-nil means escape non-break space and hyphens. */
324 Lisp_Object Vnobreak_char_display;
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
338 #endif /* HAVE_WINDOW_SYSTEM */
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
344 Lisp_Object Vvoid_text_area_pointer;
346 /* Name of the face used to highlight trailing whitespace. */
348 Lisp_Object Qtrailing_whitespace;
350 /* Name and number of the face used to highlight escape glyphs. */
352 Lisp_Object Qescape_glyph;
354 /* Name and number of the face used to highlight non-breaking spaces. */
356 Lisp_Object Qnobreak_space;
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
361 Lisp_Object Qimage;
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
370 int noninteractive_need_newline;
372 /* Non-zero means print newline to message log before next message. */
374 static int message_log_need_newline;
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
388 static struct text_pos this_line_start_pos;
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
393 static struct text_pos this_line_end_pos;
395 /* The vertical positions and the height of this line. */
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
404 static int this_line_start_x;
406 /* Buffer that this_line_.* variables are referring to. */
408 static struct buffer *this_line_buffer;
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
413 int truncate_partial_width_windows;
415 /* A flag to control how to display unibyte 8-bit character. */
417 int unibyte_display_via_language_environment;
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
423 int multiple_frames;
425 Lisp_Object Vglobal_mode_string;
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
432 Lisp_Object Voverlay_arrow_variable_list;
434 /* Marker for where to display an arrow on top of the buffer text. */
436 Lisp_Object Voverlay_arrow_position;
438 /* String to display for the arrow. Only used on terminal frames. */
440 Lisp_Object Voverlay_arrow_string;
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
454 /* Like mode-line-format, but for the title bar on a visible frame. */
456 Lisp_Object Vframe_title_format;
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
460 Lisp_Object Vicon_title_format;
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
466 static Lisp_Object Vwindow_size_change_functions;
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
470 /* Nonzero if an overlay arrow has been displayed in this window. */
472 static int overlay_arrow_seen;
474 /* Nonzero means highlight the region even in nonselected windows. */
476 int highlight_nonselected_windows;
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
481 static EMACS_INT scroll_step;
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
486 static EMACS_INT scroll_conservatively;
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
493 EMACS_INT scroll_margin;
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
499 int buffer_shared;
501 /* Vector containing glyphs for an ellipsis `...'. */
503 static Lisp_Object default_invis_vector[3];
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
509 This variable is deprecated. */
511 int mode_line_inverse_video;
513 /* Prompt to display in front of the mini-buffer contents. */
515 Lisp_Object minibuf_prompt;
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
520 int minibuf_prompt_width;
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
526 Lisp_Object echo_area_window;
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
533 Lisp_Object Vmessage_stack;
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
538 int message_enable_multibyte;
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
542 int update_mode_lines;
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
547 int windows_or_buffers_changed;
549 /* Nonzero means a frame's cursor type has been changed. */
551 int cursor_type_changed;
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
556 int line_number_displayed;
558 /* Maximum buffer size for which to display line numbers. */
560 Lisp_Object Vline_number_display_limit;
562 /* Line width to consider when repositioning for line number display. */
564 static EMACS_INT line_number_display_limit_width;
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
569 Lisp_Object Vmessage_log_max;
571 /* The name of the *Messages* buffer, a string. */
573 static Lisp_Object Vmessages_buffer_name;
575 /* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
584 Lisp_Object echo_area_buffer[2];
586 /* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
591 static Lisp_Object echo_buffer[2];
593 /* A vector saved used in with_area_buffer to reduce consing. */
595 static Lisp_Object Vwith_echo_area_save_vector;
597 /* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
600 static int display_last_displayed_message_p;
602 /* Nonzero if echo area is being used by print; zero if being used by
603 message. */
605 int message_buf_print;
607 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
609 Lisp_Object Qinhibit_menubar_update;
610 int inhibit_menubar_update;
612 /* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
616 Lisp_Object Vmax_mini_window_height;
618 /* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
621 int message_truncate_lines;
622 Lisp_Object Qmessage_truncate_lines;
624 /* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
627 static int message_cleared_p;
629 /* How to blink the default frame cursor off. */
630 Lisp_Object Vblink_cursor_alist;
632 /* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
635 #define MAX_SCRATCH_GLYPHS 100
636 struct glyph_row scratch_glyph_row;
637 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
639 /* Ascent and height of the last line processed by move_it_to. */
641 static int last_max_ascent, last_height;
643 /* Non-zero if there's a help-echo in the echo area. */
645 int help_echo_showing_p;
647 /* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
651 int current_mode_line_height, current_header_line_height;
653 /* The maximum distance to look ahead for text properties. Values
654 that are too small let us call compute_char_face and similar
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
659 #define TEXT_PROP_DISTANCE_LIMIT 100
661 #if GLYPH_DEBUG
663 /* Variables to turn off display optimizations from Lisp. */
665 int inhibit_try_window_id, inhibit_try_window_reusing;
666 int inhibit_try_cursor_movement;
668 /* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
671 int trace_redisplay_p;
673 #endif /* GLYPH_DEBUG */
675 #ifdef DEBUG_TRACE_MOVE
676 /* Non-zero means trace with TRACE_MOVE to stderr. */
677 int trace_move;
679 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680 #else
681 #define TRACE_MOVE(x) (void) 0
682 #endif
684 /* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
687 int automatic_hscrolling_p;
689 /* How close to the margin can point get before the window is scrolled
690 horizontally. */
691 EMACS_INT hscroll_margin;
693 /* How much to scroll horizontally when point is inside the above margin. */
694 Lisp_Object Vhscroll_step;
696 /* The variable `resize-mini-windows'. If nil, don't resize
697 mini-windows. If t, always resize them to fit the text they
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
701 Lisp_Object Vresize_mini_windows;
703 /* Buffer being redisplayed -- for redisplay_window_error. */
705 struct buffer *displayed_buffer;
707 /* Value returned from text property handlers (see below). */
709 enum prop_handled
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
717 /* A description of text properties that redisplay is interested
718 in. */
720 struct props
722 /* The name of the property. */
723 Lisp_Object *name;
725 /* A unique index for the property. */
726 enum prop_idx idx;
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
733 static enum prop_handled handle_face_prop P_ ((struct it *));
734 static enum prop_handled handle_invisible_prop P_ ((struct it *));
735 static enum prop_handled handle_display_prop P_ ((struct it *));
736 static enum prop_handled handle_composition_prop P_ ((struct it *));
737 static enum prop_handled handle_overlay_change P_ ((struct it *));
738 static enum prop_handled handle_fontified_prop P_ ((struct it *));
740 /* Properties handled by iterators. */
742 static struct props it_props[] =
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
751 {NULL, 0, NULL}
754 /* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
757 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
759 /* Enumeration returned by some move_it_.* functions internally. */
761 enum move_it_result
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
784 /* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
789 #define CLEAR_FACE_CACHE_COUNT 500
790 static int clear_face_cache_count;
792 /* Similarly for the image cache. */
794 #ifdef HAVE_WINDOW_SYSTEM
795 #define CLEAR_IMAGE_CACHE_COUNT 101
796 static int clear_image_cache_count;
797 #endif
799 /* Record the previous terminal frame we displayed. */
801 static struct frame *previous_terminal_frame;
803 /* Non-zero while redisplay_internal is in progress. */
805 int redisplaying_p;
807 /* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
811 int inhibit_free_realized_faces;
812 Lisp_Object Qinhibit_free_realized_faces;
814 /* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
817 Lisp_Object help_echo_string;
818 Lisp_Object help_echo_window;
819 Lisp_Object help_echo_object;
820 int help_echo_pos;
822 /* Temporary variable for XTread_socket. */
824 Lisp_Object previous_help_echo_string;
826 /* Null glyph slice */
828 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
831 /* Function prototypes. */
833 static void setup_for_ellipsis P_ ((struct it *, int));
834 static void mark_window_display_accurate_1 P_ ((struct window *, int));
835 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
838 static int redisplay_mode_lines P_ ((Lisp_Object, int));
839 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
841 #if 0
842 static int invisible_text_between_p P_ ((struct it *, int, int));
843 #endif
845 static void pint2str P_ ((char *, int, int));
846 static void pint2hrstr P_ ((char *, int, int));
847 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
851 static void store_mode_line_noprop_char P_ ((char));
852 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
853 static void x_consider_frame_title P_ ((Lisp_Object));
854 static void handle_stop P_ ((struct it *));
855 static int tool_bar_lines_needed P_ ((struct frame *));
856 static int single_display_spec_intangible_p P_ ((Lisp_Object));
857 static void ensure_echo_area_buffers P_ ((void));
858 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
860 static int with_echo_area_buffer P_ ((struct window *, int,
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static void clear_garbaged_frames P_ ((void));
864 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int display_echo_area P_ ((struct window *));
868 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
871 static int string_char_and_length P_ ((const unsigned char *, int, int *));
872 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874 static int compute_window_start_on_continuation_line P_ ((struct window *));
875 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
876 static void insert_left_trunc_glyphs P_ ((struct it *));
877 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
879 static void extend_face_to_end_of_line P_ ((struct it *));
880 static int append_space_for_newline P_ ((struct it *, int));
881 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
882 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
883 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
884 static int trailing_whitespace_p P_ ((int));
885 static int message_log_check_duplicate P_ ((int, int, int, int));
886 static void push_it P_ ((struct it *));
887 static void pop_it P_ ((struct it *));
888 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
889 static void select_frame_for_redisplay P_ ((Lisp_Object));
890 static void redisplay_internal P_ ((int));
891 static int echo_area_display P_ ((int));
892 static void redisplay_windows P_ ((Lisp_Object));
893 static void redisplay_window P_ ((Lisp_Object, int));
894 static Lisp_Object redisplay_window_error ();
895 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
897 static void update_menu_bar P_ ((struct frame *, int));
898 static int try_window_reusing_current_matrix P_ ((struct window *));
899 static int try_window_id P_ ((struct window *));
900 static int display_line P_ ((struct it *));
901 static int display_mode_lines P_ ((struct window *));
902 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
903 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
904 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
905 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
906 static void display_menu_bar P_ ((struct window *));
907 static int display_count_lines P_ ((int, int, int, int, int *));
908 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910 static void compute_line_metrics P_ ((struct it *));
911 static void run_redisplay_end_trigger_hook P_ ((struct it *));
912 static int get_overlay_strings P_ ((struct it *, int));
913 static void next_overlay_string P_ ((struct it *));
914 static void reseat P_ ((struct it *, struct text_pos, int));
915 static void reseat_1 P_ ((struct it *, struct text_pos, int));
916 static void back_to_previous_visible_line_start P_ ((struct it *));
917 void reseat_at_previous_visible_line_start P_ ((struct it *));
918 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
919 static int next_element_from_ellipsis P_ ((struct it *));
920 static int next_element_from_display_vector P_ ((struct it *));
921 static int next_element_from_string P_ ((struct it *));
922 static int next_element_from_c_string P_ ((struct it *));
923 static int next_element_from_buffer P_ ((struct it *));
924 static int next_element_from_composition P_ ((struct it *));
925 static int next_element_from_image P_ ((struct it *));
926 static int next_element_from_stretch P_ ((struct it *));
927 static void load_overlay_strings P_ ((struct it *, int));
928 static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
930 static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
932 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934 void move_it_vertically_backward P_ ((struct it *, int));
935 static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
937 static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static void back_to_previous_line_start P_ ((struct it *));
940 static int forward_to_next_line_start P_ ((struct it *, int *));
941 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943 static struct text_pos string_pos P_ ((int, Lisp_Object));
944 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945 static int number_of_chars P_ ((unsigned char *, int));
946 static void compute_stop_pos P_ ((struct it *));
947 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949 static int face_before_or_after_it_pos P_ ((struct it *, int));
950 static int next_overlay_change P_ ((int));
951 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
952 Lisp_Object, struct text_pos *,
953 int));
954 static int underlying_face_id P_ ((struct it *));
955 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
961 #ifdef HAVE_WINDOW_SYSTEM
963 static void update_tool_bar P_ ((struct frame *, int));
964 static void build_desired_tool_bar_string P_ ((struct frame *f));
965 static int redisplay_tool_bar P_ ((struct frame *));
966 static void display_tool_bar_line P_ ((struct it *));
967 static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
973 #endif /* HAVE_WINDOW_SYSTEM */
976 /***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
980 /* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
984 This is the height of W minus the height of a mode line, if any. */
986 INLINE int
987 window_text_bottom_y (w)
988 struct window *w;
990 int height = WINDOW_TOTAL_HEIGHT (w);
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1001 INLINE int
1002 window_box_width (w, area)
1003 struct window *w;
1004 int area;
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
1009 if (!w->pseudo_window_p)
1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
1013 if (area == TEXT_AREA)
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1021 else if (area == LEFT_MARGIN_AREA)
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1027 else if (area == RIGHT_MARGIN_AREA)
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1042 INLINE int
1043 window_box_height (w)
1044 struct window *w;
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1049 xassert (height >= 0);
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1057 if (WINDOW_WANTS_MODELINE_P (w))
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1090 INLINE int
1091 window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1095 int x;
1097 if (w->pseudo_window_p)
1098 return 0;
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1116 return x;
1120 /* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1124 INLINE int
1125 window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1136 INLINE int
1137 window_box_left (w, area)
1138 struct window *w;
1139 int area;
1141 struct frame *f = XFRAME (w->frame);
1142 int x;
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
1150 return x;
1154 /* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
1156 whole window, to the left of the right fringe of W. */
1158 INLINE int
1159 window_box_right (w, area)
1160 struct window *w;
1161 int area;
1163 return window_box_left (w, area) + window_box_width (w, area);
1166 /* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
1168 whole window, not including the left and right fringes of
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1173 INLINE void
1174 window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
1202 INLINE void
1203 window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
1217 /***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1221 /* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1225 line_bottom_y (it)
1226 struct it *it;
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
1231 if (line_height == 0)
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1242 else
1244 struct glyph_row *row = it->glyph_row;
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1257 return line_top_y + line_height;
1261 /* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
1268 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1269 struct window *w;
1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1272 struct it it;
1273 struct text_pos top;
1274 int visible_p = 0;
1275 struct buffer *old_buffer = NULL;
1277 if (noninteractive)
1278 return visible_p;
1280 if (XBUFFER (w->buffer) != current_buffer)
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1294 current_buffer->mode_line_format);
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1302 start_display (&it, w, top);
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
1309 int top_x = it.current_x;
1310 int top_y = it.current_y;
1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
1317 visible_p = 1;
1318 if (visible_p)
1320 *x = top_x;
1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
1326 else
1328 struct it it2;
1330 it2 = it;
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
1333 if (charpos < IT_CHARPOS (it))
1335 visible_p = 1;
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
1348 current_header_line_height = current_mode_line_height = -1;
1350 if (visible_p && XFASTINT (w->hscroll) > 0)
1351 *x -= XFASTINT (w->hscroll);
1353 return visible_p;
1357 /* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1360 we find one, we return a `?', but with the length of the invalid
1361 character. */
1363 static INLINE int
1364 string_char_and_length (str, maxlen, len)
1365 const unsigned char *str;
1366 int maxlen, *len;
1368 int c;
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
1373 don't use this function, i.e. they silently accept invalid
1374 characters. */
1375 c = '?';
1377 return c;
1382 /* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1385 static struct text_pos
1386 string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
1391 xassert (STRINGP (string) && nchars >= 0);
1393 if (STRING_MULTIBYTE (string))
1395 int rest = SBYTES (string) - BYTEPOS (pos);
1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1397 int len;
1399 while (nchars--)
1401 string_char_and_length (p, rest, &len);
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1411 return pos;
1415 /* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1418 static INLINE struct text_pos
1419 string_pos (charpos, string)
1420 int charpos;
1421 Lisp_Object string;
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1431 /* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1435 static struct text_pos
1436 c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1441 struct text_pos pos;
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1446 if (multibyte_p)
1448 int rest = strlen (s), len;
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
1453 string_char_and_length (s, rest, &len);
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
1463 return pos;
1467 /* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
1470 static int
1471 number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1475 int nchars;
1477 if (multibyte_p)
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
1482 for (nchars = 0; rest > 0; ++nchars)
1484 string_char_and_length (p, rest, &len);
1485 rest -= len, p += len;
1488 else
1489 nchars = strlen (s);
1491 return nchars;
1495 /* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
1499 static void
1500 compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1519 estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1542 return height;
1544 #endif
1546 return 1;
1549 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1554 void
1555 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1563 #ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1567 even for negative values. */
1568 if (pix_x < 0)
1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1570 if (pix_y < 0)
1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
1583 if (!noclip)
1585 if (pix_x < 0)
1586 pix_x = 0;
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
1590 if (pix_y < 0)
1591 pix_y = 0;
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
1596 #endif
1598 *x = pix_x;
1599 *y = pix_y;
1603 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1609 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1617 int success_p;
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1622 if (display_completed)
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1640 success_p = 1;
1642 else
1644 hpos = vpos = 0;
1645 success_p = 0;
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1652 #endif
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1660 #ifdef HAVE_WINDOW_SYSTEM
1662 /* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1670 static struct glyph *
1671 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1672 struct window *w;
1673 int x, y;
1674 int *hpos, *vpos, *dx, *dy, *area;
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
1678 int x0, i;
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1690 *vpos = i;
1691 *hpos = 0;
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1700 *area = TEXT_AREA;
1701 x0 = 0;
1703 else
1705 if (x < window_box_left_offset (w, TEXT_AREA))
1707 *area = LEFT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1710 else if (x < window_box_right_offset (w, TEXT_AREA))
1712 *area = TEXT_AREA;
1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1715 else
1717 *area = RIGHT_MARGIN_AREA;
1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
1728 x -= glyph->pixel_width;
1729 ++glyph;
1732 if (glyph == end)
1733 return NULL;
1735 if (dx)
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1746 /* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1750 void
1751 frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1755 if (w->pseudo_window_p)
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1763 else
1765 *x -= WINDOW_LEFT_EDGE_X (w);
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1770 /* EXPORT:
1771 Return in *R the clipping rectangle for glyph string S. */
1773 void
1774 get_glyph_string_clip_rect (s, nr)
1775 struct glyph_string *s;
1776 NativeRectangle *nr;
1778 XRectangle r;
1780 if (s->row->full_width_p)
1782 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1783 r.x = WINDOW_LEFT_EDGE_X (s->w);
1784 r.width = WINDOW_TOTAL_WIDTH (s->w);
1786 /* Unless displaying a mode or menu bar line, which are always
1787 fully visible, clip to the visible part of the row. */
1788 if (s->w->pseudo_window_p)
1789 r.height = s->row->visible_height;
1790 else
1791 r.height = s->height;
1793 else
1795 /* This is a text line that may be partially visible. */
1796 r.x = window_box_left (s->w, s->area);
1797 r.width = window_box_width (s->w, s->area);
1798 r.height = s->row->visible_height;
1801 if (s->clip_head)
1802 if (r.x < s->clip_head->x)
1804 if (r.width >= s->clip_head->x - r.x)
1805 r.width -= s->clip_head->x - r.x;
1806 else
1807 r.width = 0;
1808 r.x = s->clip_head->x;
1810 if (s->clip_tail)
1811 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1813 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1814 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1815 else
1816 r.width = 0;
1819 /* If S draws overlapping rows, it's sufficient to use the top and
1820 bottom of the window for clipping because this glyph string
1821 intentionally draws over other lines. */
1822 if (s->for_overlaps_p)
1824 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1825 r.height = window_text_bottom_y (s->w) - r.y;
1827 else
1829 /* Don't use S->y for clipping because it doesn't take partially
1830 visible lines into account. For example, it can be negative for
1831 partially visible lines at the top of a window. */
1832 if (!s->row->full_width_p
1833 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1834 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1835 else
1836 r.y = max (0, s->row->y);
1838 /* If drawing a tool-bar window, draw it over the internal border
1839 at the top of the window. */
1840 if (WINDOWP (s->f->tool_bar_window)
1841 && s->w == XWINDOW (s->f->tool_bar_window))
1842 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1845 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1847 /* If drawing the cursor, don't let glyph draw outside its
1848 advertised boundaries. Cleartype does this under some circumstances. */
1849 if (s->hl == DRAW_CURSOR)
1851 struct glyph *glyph = s->first_glyph;
1852 int height, max_y;
1854 if (s->x > r.x)
1856 r.width -= s->x - r.x;
1857 r.x = s->x;
1859 r.width = min (r.width, glyph->pixel_width);
1861 /* If r.y is below window bottom, ensure that we still see a cursor. */
1862 height = min (glyph->ascent + glyph->descent,
1863 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1864 max_y = window_text_bottom_y (s->w) - height;
1865 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1866 if (s->ybase - glyph->ascent > max_y)
1868 r.y = max_y;
1869 r.height = height;
1871 else
1873 /* Don't draw cursor glyph taller than our actual glyph. */
1874 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1875 if (height < r.height)
1877 max_y = r.y + r.height;
1878 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1879 r.height = min (max_y - r.y, height);
1884 #ifdef CONVERT_FROM_XRECT
1885 CONVERT_FROM_XRECT (r, *nr);
1886 #else
1887 *nr = r;
1888 #endif
1892 /* EXPORT:
1893 Return the position and height of the phys cursor in window W.
1894 Set w->phys_cursor_width to width of phys cursor.
1898 get_phys_cursor_geometry (w, row, glyph, heightp)
1899 struct window *w;
1900 struct glyph_row *row;
1901 struct glyph *glyph;
1902 int *heightp;
1904 struct frame *f = XFRAME (WINDOW_FRAME (w));
1905 int y, wd, h, h0, y0;
1907 /* Compute the width of the rectangle to draw. If on a stretch
1908 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1909 rectangle as wide as the glyph, but use a canonical character
1910 width instead. */
1911 wd = glyph->pixel_width - 1;
1912 #ifdef HAVE_NTGUI
1913 wd++; /* Why? */
1914 #endif
1915 if (glyph->type == STRETCH_GLYPH
1916 && !x_stretch_cursor_p)
1917 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1918 w->phys_cursor_width = wd;
1920 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1922 /* If y is below window bottom, ensure that we still see a cursor. */
1923 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1925 h = max (h0, glyph->ascent + glyph->descent);
1926 h0 = min (h0, glyph->ascent + glyph->descent);
1928 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1929 if (y < y0)
1931 h = max (h - (y0 - y) + 1, h0);
1932 y = y0 - 1;
1934 else
1936 y0 = window_text_bottom_y (w) - h0;
1937 if (y > y0)
1939 h += y - y0;
1940 y = y0;
1944 *heightp = h - 1;
1945 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1949 #endif /* HAVE_WINDOW_SYSTEM */
1952 /***********************************************************************
1953 Lisp form evaluation
1954 ***********************************************************************/
1956 /* Error handler for safe_eval and safe_call. */
1958 static Lisp_Object
1959 safe_eval_handler (arg)
1960 Lisp_Object arg;
1962 add_to_log ("Error during redisplay: %s", arg, Qnil);
1963 return Qnil;
1967 /* Evaluate SEXPR and return the result, or nil if something went
1968 wrong. Prevent redisplay during the evaluation. */
1970 Lisp_Object
1971 safe_eval (sexpr)
1972 Lisp_Object sexpr;
1974 Lisp_Object val;
1976 if (inhibit_eval_during_redisplay)
1977 val = Qnil;
1978 else
1980 int count = SPECPDL_INDEX ();
1981 struct gcpro gcpro1;
1983 GCPRO1 (sexpr);
1984 specbind (Qinhibit_redisplay, Qt);
1985 /* Use Qt to ensure debugger does not run,
1986 so there is no possibility of wanting to redisplay. */
1987 val = internal_condition_case_1 (Feval, sexpr, Qt,
1988 safe_eval_handler);
1989 UNGCPRO;
1990 val = unbind_to (count, val);
1993 return val;
1997 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1998 Return the result, or nil if something went wrong. Prevent
1999 redisplay during the evaluation. */
2001 Lisp_Object
2002 safe_call (nargs, args)
2003 int nargs;
2004 Lisp_Object *args;
2006 Lisp_Object val;
2008 if (inhibit_eval_during_redisplay)
2009 val = Qnil;
2010 else
2012 int count = SPECPDL_INDEX ();
2013 struct gcpro gcpro1;
2015 GCPRO1 (args[0]);
2016 gcpro1.nvars = nargs;
2017 specbind (Qinhibit_redisplay, Qt);
2018 /* Use Qt to ensure debugger does not run,
2019 so there is no possibility of wanting to redisplay. */
2020 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2021 safe_eval_handler);
2022 UNGCPRO;
2023 val = unbind_to (count, val);
2026 return val;
2030 /* Call function FN with one argument ARG.
2031 Return the result, or nil if something went wrong. */
2033 Lisp_Object
2034 safe_call1 (fn, arg)
2035 Lisp_Object fn, arg;
2037 Lisp_Object args[2];
2038 args[0] = fn;
2039 args[1] = arg;
2040 return safe_call (2, args);
2045 /***********************************************************************
2046 Debugging
2047 ***********************************************************************/
2049 #if 0
2051 /* Define CHECK_IT to perform sanity checks on iterators.
2052 This is for debugging. It is too slow to do unconditionally. */
2054 static void
2055 check_it (it)
2056 struct it *it;
2058 if (it->method == GET_FROM_STRING)
2060 xassert (STRINGP (it->string));
2061 xassert (IT_STRING_CHARPOS (*it) >= 0);
2063 else
2065 xassert (IT_STRING_CHARPOS (*it) < 0);
2066 if (it->method == GET_FROM_BUFFER)
2068 /* Check that character and byte positions agree. */
2069 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2073 if (it->dpvec)
2074 xassert (it->current.dpvec_index >= 0);
2075 else
2076 xassert (it->current.dpvec_index < 0);
2079 #define CHECK_IT(IT) check_it ((IT))
2081 #else /* not 0 */
2083 #define CHECK_IT(IT) (void) 0
2085 #endif /* not 0 */
2088 #if GLYPH_DEBUG
2090 /* Check that the window end of window W is what we expect it
2091 to be---the last row in the current matrix displaying text. */
2093 static void
2094 check_window_end (w)
2095 struct window *w;
2097 if (!MINI_WINDOW_P (w)
2098 && !NILP (w->window_end_valid))
2100 struct glyph_row *row;
2101 xassert ((row = MATRIX_ROW (w->current_matrix,
2102 XFASTINT (w->window_end_vpos)),
2103 !row->enabled_p
2104 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2105 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2109 #define CHECK_WINDOW_END(W) check_window_end ((W))
2111 #else /* not GLYPH_DEBUG */
2113 #define CHECK_WINDOW_END(W) (void) 0
2115 #endif /* not GLYPH_DEBUG */
2119 /***********************************************************************
2120 Iterator initialization
2121 ***********************************************************************/
2123 /* Initialize IT for displaying current_buffer in window W, starting
2124 at character position CHARPOS. CHARPOS < 0 means that no buffer
2125 position is specified which is useful when the iterator is assigned
2126 a position later. BYTEPOS is the byte position corresponding to
2127 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2129 If ROW is not null, calls to produce_glyphs with IT as parameter
2130 will produce glyphs in that row.
2132 BASE_FACE_ID is the id of a base face to use. It must be one of
2133 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2135 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2137 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2138 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2139 will be initialized to use the corresponding mode line glyph row of
2140 the desired matrix of W. */
2142 void
2143 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2144 struct it *it;
2145 struct window *w;
2146 int charpos, bytepos;
2147 struct glyph_row *row;
2148 enum face_id base_face_id;
2150 int highlight_region_p;
2152 /* Some precondition checks. */
2153 xassert (w != NULL && it != NULL);
2154 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2155 && charpos <= ZV));
2157 /* If face attributes have been changed since the last redisplay,
2158 free realized faces now because they depend on face definitions
2159 that might have changed. Don't free faces while there might be
2160 desired matrices pending which reference these faces. */
2161 if (face_change_count && !inhibit_free_realized_faces)
2163 face_change_count = 0;
2164 free_all_realized_faces (Qnil);
2167 /* Use one of the mode line rows of W's desired matrix if
2168 appropriate. */
2169 if (row == NULL)
2171 if (base_face_id == MODE_LINE_FACE_ID
2172 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2173 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2174 else if (base_face_id == HEADER_LINE_FACE_ID)
2175 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2178 /* Clear IT. */
2179 bzero (it, sizeof *it);
2180 it->current.overlay_string_index = -1;
2181 it->current.dpvec_index = -1;
2182 it->base_face_id = base_face_id;
2183 it->string = Qnil;
2184 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2186 /* The window in which we iterate over current_buffer: */
2187 XSETWINDOW (it->window, w);
2188 it->w = w;
2189 it->f = XFRAME (w->frame);
2191 /* Extra space between lines (on window systems only). */
2192 if (base_face_id == DEFAULT_FACE_ID
2193 && FRAME_WINDOW_P (it->f))
2195 if (NATNUMP (current_buffer->extra_line_spacing))
2196 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2197 else if (FLOATP (current_buffer->extra_line_spacing))
2198 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2199 * FRAME_LINE_HEIGHT (it->f));
2200 else if (it->f->extra_line_spacing > 0)
2201 it->extra_line_spacing = it->f->extra_line_spacing;
2202 it->max_extra_line_spacing = 0;
2205 /* If realized faces have been removed, e.g. because of face
2206 attribute changes of named faces, recompute them. When running
2207 in batch mode, the face cache of Vterminal_frame is null. If
2208 we happen to get called, make a dummy face cache. */
2209 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2210 init_frame_faces (it->f);
2211 if (FRAME_FACE_CACHE (it->f)->used == 0)
2212 recompute_basic_faces (it->f);
2214 /* Current value of the `slice', `space-width', and 'height' properties. */
2215 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2216 it->space_width = Qnil;
2217 it->font_height = Qnil;
2218 it->override_ascent = -1;
2220 /* Are control characters displayed as `^C'? */
2221 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2223 /* -1 means everything between a CR and the following line end
2224 is invisible. >0 means lines indented more than this value are
2225 invisible. */
2226 it->selective = (INTEGERP (current_buffer->selective_display)
2227 ? XFASTINT (current_buffer->selective_display)
2228 : (!NILP (current_buffer->selective_display)
2229 ? -1 : 0));
2230 it->selective_display_ellipsis_p
2231 = !NILP (current_buffer->selective_display_ellipses);
2233 /* Display table to use. */
2234 it->dp = window_display_table (w);
2236 /* Are multibyte characters enabled in current_buffer? */
2237 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2239 /* Non-zero if we should highlight the region. */
2240 highlight_region_p
2241 = (!NILP (Vtransient_mark_mode)
2242 && !NILP (current_buffer->mark_active)
2243 && XMARKER (current_buffer->mark)->buffer != 0);
2245 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2246 start and end of a visible region in window IT->w. Set both to
2247 -1 to indicate no region. */
2248 if (highlight_region_p
2249 /* Maybe highlight only in selected window. */
2250 && (/* Either show region everywhere. */
2251 highlight_nonselected_windows
2252 /* Or show region in the selected window. */
2253 || w == XWINDOW (selected_window)
2254 /* Or show the region if we are in the mini-buffer and W is
2255 the window the mini-buffer refers to. */
2256 || (MINI_WINDOW_P (XWINDOW (selected_window))
2257 && WINDOWP (minibuf_selected_window)
2258 && w == XWINDOW (minibuf_selected_window))))
2260 int charpos = marker_position (current_buffer->mark);
2261 it->region_beg_charpos = min (PT, charpos);
2262 it->region_end_charpos = max (PT, charpos);
2264 else
2265 it->region_beg_charpos = it->region_end_charpos = -1;
2267 /* Get the position at which the redisplay_end_trigger hook should
2268 be run, if it is to be run at all. */
2269 if (MARKERP (w->redisplay_end_trigger)
2270 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2271 it->redisplay_end_trigger_charpos
2272 = marker_position (w->redisplay_end_trigger);
2273 else if (INTEGERP (w->redisplay_end_trigger))
2274 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2276 /* Correct bogus values of tab_width. */
2277 it->tab_width = XINT (current_buffer->tab_width);
2278 if (it->tab_width <= 0 || it->tab_width > 1000)
2279 it->tab_width = 8;
2281 /* Are lines in the display truncated? */
2282 it->truncate_lines_p
2283 = (base_face_id != DEFAULT_FACE_ID
2284 || XINT (it->w->hscroll)
2285 || (truncate_partial_width_windows
2286 && !WINDOW_FULL_WIDTH_P (it->w))
2287 || !NILP (current_buffer->truncate_lines));
2289 /* Get dimensions of truncation and continuation glyphs. These are
2290 displayed as fringe bitmaps under X, so we don't need them for such
2291 frames. */
2292 if (!FRAME_WINDOW_P (it->f))
2294 if (it->truncate_lines_p)
2296 /* We will need the truncation glyph. */
2297 xassert (it->glyph_row == NULL);
2298 produce_special_glyphs (it, IT_TRUNCATION);
2299 it->truncation_pixel_width = it->pixel_width;
2301 else
2303 /* We will need the continuation glyph. */
2304 xassert (it->glyph_row == NULL);
2305 produce_special_glyphs (it, IT_CONTINUATION);
2306 it->continuation_pixel_width = it->pixel_width;
2309 /* Reset these values to zero because the produce_special_glyphs
2310 above has changed them. */
2311 it->pixel_width = it->ascent = it->descent = 0;
2312 it->phys_ascent = it->phys_descent = 0;
2315 /* Set this after getting the dimensions of truncation and
2316 continuation glyphs, so that we don't produce glyphs when calling
2317 produce_special_glyphs, above. */
2318 it->glyph_row = row;
2319 it->area = TEXT_AREA;
2321 /* Get the dimensions of the display area. The display area
2322 consists of the visible window area plus a horizontally scrolled
2323 part to the left of the window. All x-values are relative to the
2324 start of this total display area. */
2325 if (base_face_id != DEFAULT_FACE_ID)
2327 /* Mode lines, menu bar in terminal frames. */
2328 it->first_visible_x = 0;
2329 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2331 else
2333 it->first_visible_x
2334 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2335 it->last_visible_x = (it->first_visible_x
2336 + window_box_width (w, TEXT_AREA));
2338 /* If we truncate lines, leave room for the truncator glyph(s) at
2339 the right margin. Otherwise, leave room for the continuation
2340 glyph(s). Truncation and continuation glyphs are not inserted
2341 for window-based redisplay. */
2342 if (!FRAME_WINDOW_P (it->f))
2344 if (it->truncate_lines_p)
2345 it->last_visible_x -= it->truncation_pixel_width;
2346 else
2347 it->last_visible_x -= it->continuation_pixel_width;
2350 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2351 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2354 /* Leave room for a border glyph. */
2355 if (!FRAME_WINDOW_P (it->f)
2356 && !WINDOW_RIGHTMOST_P (it->w))
2357 it->last_visible_x -= 1;
2359 it->last_visible_y = window_text_bottom_y (w);
2361 /* For mode lines and alike, arrange for the first glyph having a
2362 left box line if the face specifies a box. */
2363 if (base_face_id != DEFAULT_FACE_ID)
2365 struct face *face;
2367 it->face_id = base_face_id;
2369 /* If we have a boxed mode line, make the first character appear
2370 with a left box line. */
2371 face = FACE_FROM_ID (it->f, base_face_id);
2372 if (face->box != FACE_NO_BOX)
2373 it->start_of_box_run_p = 1;
2376 /* If a buffer position was specified, set the iterator there,
2377 getting overlays and face properties from that position. */
2378 if (charpos >= BUF_BEG (current_buffer))
2380 it->end_charpos = ZV;
2381 it->face_id = -1;
2382 IT_CHARPOS (*it) = charpos;
2384 /* Compute byte position if not specified. */
2385 if (bytepos < charpos)
2386 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2387 else
2388 IT_BYTEPOS (*it) = bytepos;
2390 it->start = it->current;
2392 /* Compute faces etc. */
2393 reseat (it, it->current.pos, 1);
2396 CHECK_IT (it);
2400 /* Initialize IT for the display of window W with window start POS. */
2402 void
2403 start_display (it, w, pos)
2404 struct it *it;
2405 struct window *w;
2406 struct text_pos pos;
2408 struct glyph_row *row;
2409 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2411 row = w->desired_matrix->rows + first_vpos;
2412 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2413 it->first_vpos = first_vpos;
2415 /* Don't reseat to previous visible line start if current start
2416 position is in a string or image. */
2417 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2419 int start_at_line_beg_p;
2420 int first_y = it->current_y;
2422 /* If window start is not at a line start, skip forward to POS to
2423 get the correct continuation lines width. */
2424 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2425 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2426 if (!start_at_line_beg_p)
2428 int new_x;
2430 reseat_at_previous_visible_line_start (it);
2431 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2433 new_x = it->current_x + it->pixel_width;
2435 /* If lines are continued, this line may end in the middle
2436 of a multi-glyph character (e.g. a control character
2437 displayed as \003, or in the middle of an overlay
2438 string). In this case move_it_to above will not have
2439 taken us to the start of the continuation line but to the
2440 end of the continued line. */
2441 if (it->current_x > 0
2442 && !it->truncate_lines_p /* Lines are continued. */
2443 && (/* And glyph doesn't fit on the line. */
2444 new_x > it->last_visible_x
2445 /* Or it fits exactly and we're on a window
2446 system frame. */
2447 || (new_x == it->last_visible_x
2448 && FRAME_WINDOW_P (it->f))))
2450 if (it->current.dpvec_index >= 0
2451 || it->current.overlay_string_index >= 0)
2453 set_iterator_to_next (it, 1);
2454 move_it_in_display_line_to (it, -1, -1, 0);
2457 it->continuation_lines_width += it->current_x;
2460 /* We're starting a new display line, not affected by the
2461 height of the continued line, so clear the appropriate
2462 fields in the iterator structure. */
2463 it->max_ascent = it->max_descent = 0;
2464 it->max_phys_ascent = it->max_phys_descent = 0;
2466 it->current_y = first_y;
2467 it->vpos = 0;
2468 it->current_x = it->hpos = 0;
2472 #if 0 /* Don't assert the following because start_display is sometimes
2473 called intentionally with a window start that is not at a
2474 line start. Please leave this code in as a comment. */
2476 /* Window start should be on a line start, now. */
2477 xassert (it->continuation_lines_width
2478 || IT_CHARPOS (it) == BEGV
2479 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2480 #endif /* 0 */
2484 /* Return 1 if POS is a position in ellipses displayed for invisible
2485 text. W is the window we display, for text property lookup. */
2487 static int
2488 in_ellipses_for_invisible_text_p (pos, w)
2489 struct display_pos *pos;
2490 struct window *w;
2492 Lisp_Object prop, window;
2493 int ellipses_p = 0;
2494 int charpos = CHARPOS (pos->pos);
2496 /* If POS specifies a position in a display vector, this might
2497 be for an ellipsis displayed for invisible text. We won't
2498 get the iterator set up for delivering that ellipsis unless
2499 we make sure that it gets aware of the invisible text. */
2500 if (pos->dpvec_index >= 0
2501 && pos->overlay_string_index < 0
2502 && CHARPOS (pos->string_pos) < 0
2503 && charpos > BEGV
2504 && (XSETWINDOW (window, w),
2505 prop = Fget_char_property (make_number (charpos),
2506 Qinvisible, window),
2507 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2509 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2510 window);
2511 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2514 return ellipses_p;
2518 /* Initialize IT for stepping through current_buffer in window W,
2519 starting at position POS that includes overlay string and display
2520 vector/ control character translation position information. Value
2521 is zero if there are overlay strings with newlines at POS. */
2523 static int
2524 init_from_display_pos (it, w, pos)
2525 struct it *it;
2526 struct window *w;
2527 struct display_pos *pos;
2529 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2530 int i, overlay_strings_with_newlines = 0;
2532 /* If POS specifies a position in a display vector, this might
2533 be for an ellipsis displayed for invisible text. We won't
2534 get the iterator set up for delivering that ellipsis unless
2535 we make sure that it gets aware of the invisible text. */
2536 if (in_ellipses_for_invisible_text_p (pos, w))
2538 --charpos;
2539 bytepos = 0;
2542 /* Keep in mind: the call to reseat in init_iterator skips invisible
2543 text, so we might end up at a position different from POS. This
2544 is only a problem when POS is a row start after a newline and an
2545 overlay starts there with an after-string, and the overlay has an
2546 invisible property. Since we don't skip invisible text in
2547 display_line and elsewhere immediately after consuming the
2548 newline before the row start, such a POS will not be in a string,
2549 but the call to init_iterator below will move us to the
2550 after-string. */
2551 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2553 /* This only scans the current chunk -- it should scan all chunks.
2554 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2555 to 16 in 22.1 to make this a lesser problem. */
2556 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2558 const char *s = SDATA (it->overlay_strings[i]);
2559 const char *e = s + SBYTES (it->overlay_strings[i]);
2561 while (s < e && *s != '\n')
2562 ++s;
2564 if (s < e)
2566 overlay_strings_with_newlines = 1;
2567 break;
2571 /* If position is within an overlay string, set up IT to the right
2572 overlay string. */
2573 if (pos->overlay_string_index >= 0)
2575 int relative_index;
2577 /* If the first overlay string happens to have a `display'
2578 property for an image, the iterator will be set up for that
2579 image, and we have to undo that setup first before we can
2580 correct the overlay string index. */
2581 if (it->method == GET_FROM_IMAGE)
2582 pop_it (it);
2584 /* We already have the first chunk of overlay strings in
2585 IT->overlay_strings. Load more until the one for
2586 pos->overlay_string_index is in IT->overlay_strings. */
2587 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2589 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2590 it->current.overlay_string_index = 0;
2591 while (n--)
2593 load_overlay_strings (it, 0);
2594 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2598 it->current.overlay_string_index = pos->overlay_string_index;
2599 relative_index = (it->current.overlay_string_index
2600 % OVERLAY_STRING_CHUNK_SIZE);
2601 it->string = it->overlay_strings[relative_index];
2602 xassert (STRINGP (it->string));
2603 it->current.string_pos = pos->string_pos;
2604 it->method = GET_FROM_STRING;
2607 #if 0 /* This is bogus because POS not having an overlay string
2608 position does not mean it's after the string. Example: A
2609 line starting with a before-string and initialization of IT
2610 to the previous row's end position. */
2611 else if (it->current.overlay_string_index >= 0)
2613 /* If POS says we're already after an overlay string ending at
2614 POS, make sure to pop the iterator because it will be in
2615 front of that overlay string. When POS is ZV, we've thereby
2616 also ``processed'' overlay strings at ZV. */
2617 while (it->sp)
2618 pop_it (it);
2619 it->current.overlay_string_index = -1;
2620 it->method = GET_FROM_BUFFER;
2621 if (CHARPOS (pos->pos) == ZV)
2622 it->overlay_strings_at_end_processed_p = 1;
2624 #endif /* 0 */
2626 if (CHARPOS (pos->string_pos) >= 0)
2628 /* Recorded position is not in an overlay string, but in another
2629 string. This can only be a string from a `display' property.
2630 IT should already be filled with that string. */
2631 it->current.string_pos = pos->string_pos;
2632 xassert (STRINGP (it->string));
2635 /* Restore position in display vector translations, control
2636 character translations or ellipses. */
2637 if (pos->dpvec_index >= 0)
2639 if (it->dpvec == NULL)
2640 get_next_display_element (it);
2641 xassert (it->dpvec && it->current.dpvec_index == 0);
2642 it->current.dpvec_index = pos->dpvec_index;
2645 CHECK_IT (it);
2646 return !overlay_strings_with_newlines;
2650 /* Initialize IT for stepping through current_buffer in window W
2651 starting at ROW->start. */
2653 static void
2654 init_to_row_start (it, w, row)
2655 struct it *it;
2656 struct window *w;
2657 struct glyph_row *row;
2659 init_from_display_pos (it, w, &row->start);
2660 it->start = row->start;
2661 it->continuation_lines_width = row->continuation_lines_width;
2662 CHECK_IT (it);
2666 /* Initialize IT for stepping through current_buffer in window W
2667 starting in the line following ROW, i.e. starting at ROW->end.
2668 Value is zero if there are overlay strings with newlines at ROW's
2669 end position. */
2671 static int
2672 init_to_row_end (it, w, row)
2673 struct it *it;
2674 struct window *w;
2675 struct glyph_row *row;
2677 int success = 0;
2679 if (init_from_display_pos (it, w, &row->end))
2681 if (row->continued_p)
2682 it->continuation_lines_width
2683 = row->continuation_lines_width + row->pixel_width;
2684 CHECK_IT (it);
2685 success = 1;
2688 return success;
2694 /***********************************************************************
2695 Text properties
2696 ***********************************************************************/
2698 /* Called when IT reaches IT->stop_charpos. Handle text property and
2699 overlay changes. Set IT->stop_charpos to the next position where
2700 to stop. */
2702 static void
2703 handle_stop (it)
2704 struct it *it;
2706 enum prop_handled handled;
2707 int handle_overlay_change_p = 1;
2708 struct props *p;
2710 it->dpvec = NULL;
2711 it->current.dpvec_index = -1;
2713 /* Use face of preceding text for ellipsis (if invisible) */
2714 if (it->selective_display_ellipsis_p)
2715 it->saved_face_id = it->face_id;
2719 handled = HANDLED_NORMALLY;
2721 /* Call text property handlers. */
2722 for (p = it_props; p->handler; ++p)
2724 handled = p->handler (it);
2726 if (handled == HANDLED_RECOMPUTE_PROPS)
2727 break;
2728 else if (handled == HANDLED_RETURN)
2729 return;
2730 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2731 handle_overlay_change_p = 0;
2734 if (handled != HANDLED_RECOMPUTE_PROPS)
2736 /* Don't check for overlay strings below when set to deliver
2737 characters from a display vector. */
2738 if (it->method == GET_FROM_DISPLAY_VECTOR)
2739 handle_overlay_change_p = 0;
2741 /* Handle overlay changes. */
2742 if (handle_overlay_change_p)
2743 handled = handle_overlay_change (it);
2745 /* Determine where to stop next. */
2746 if (handled == HANDLED_NORMALLY)
2747 compute_stop_pos (it);
2750 while (handled == HANDLED_RECOMPUTE_PROPS);
2754 /* Compute IT->stop_charpos from text property and overlay change
2755 information for IT's current position. */
2757 static void
2758 compute_stop_pos (it)
2759 struct it *it;
2761 register INTERVAL iv, next_iv;
2762 Lisp_Object object, limit, position;
2764 /* If nowhere else, stop at the end. */
2765 it->stop_charpos = it->end_charpos;
2767 if (STRINGP (it->string))
2769 /* Strings are usually short, so don't limit the search for
2770 properties. */
2771 object = it->string;
2772 limit = Qnil;
2773 position = make_number (IT_STRING_CHARPOS (*it));
2775 else
2777 int charpos;
2779 /* If next overlay change is in front of the current stop pos
2780 (which is IT->end_charpos), stop there. Note: value of
2781 next_overlay_change is point-max if no overlay change
2782 follows. */
2783 charpos = next_overlay_change (IT_CHARPOS (*it));
2784 if (charpos < it->stop_charpos)
2785 it->stop_charpos = charpos;
2787 /* If showing the region, we have to stop at the region
2788 start or end because the face might change there. */
2789 if (it->region_beg_charpos > 0)
2791 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2792 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2793 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2794 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2797 /* Set up variables for computing the stop position from text
2798 property changes. */
2799 XSETBUFFER (object, current_buffer);
2800 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2801 position = make_number (IT_CHARPOS (*it));
2805 /* Get the interval containing IT's position. Value is a null
2806 interval if there isn't such an interval. */
2807 iv = validate_interval_range (object, &position, &position, 0);
2808 if (!NULL_INTERVAL_P (iv))
2810 Lisp_Object values_here[LAST_PROP_IDX];
2811 struct props *p;
2813 /* Get properties here. */
2814 for (p = it_props; p->handler; ++p)
2815 values_here[p->idx] = textget (iv->plist, *p->name);
2817 /* Look for an interval following iv that has different
2818 properties. */
2819 for (next_iv = next_interval (iv);
2820 (!NULL_INTERVAL_P (next_iv)
2821 && (NILP (limit)
2822 || XFASTINT (limit) > next_iv->position));
2823 next_iv = next_interval (next_iv))
2825 for (p = it_props; p->handler; ++p)
2827 Lisp_Object new_value;
2829 new_value = textget (next_iv->plist, *p->name);
2830 if (!EQ (values_here[p->idx], new_value))
2831 break;
2834 if (p->handler)
2835 break;
2838 if (!NULL_INTERVAL_P (next_iv))
2840 if (INTEGERP (limit)
2841 && next_iv->position >= XFASTINT (limit))
2842 /* No text property change up to limit. */
2843 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2844 else
2845 /* Text properties change in next_iv. */
2846 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2850 xassert (STRINGP (it->string)
2851 || (it->stop_charpos >= BEGV
2852 && it->stop_charpos >= IT_CHARPOS (*it)));
2856 /* Return the position of the next overlay change after POS in
2857 current_buffer. Value is point-max if no overlay change
2858 follows. This is like `next-overlay-change' but doesn't use
2859 xmalloc. */
2861 static int
2862 next_overlay_change (pos)
2863 int pos;
2865 int noverlays;
2866 int endpos;
2867 Lisp_Object *overlays;
2868 int i;
2870 /* Get all overlays at the given position. */
2871 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2873 /* If any of these overlays ends before endpos,
2874 use its ending point instead. */
2875 for (i = 0; i < noverlays; ++i)
2877 Lisp_Object oend;
2878 int oendpos;
2880 oend = OVERLAY_END (overlays[i]);
2881 oendpos = OVERLAY_POSITION (oend);
2882 endpos = min (endpos, oendpos);
2885 return endpos;
2890 /***********************************************************************
2891 Fontification
2892 ***********************************************************************/
2894 /* Handle changes in the `fontified' property of the current buffer by
2895 calling hook functions from Qfontification_functions to fontify
2896 regions of text. */
2898 static enum prop_handled
2899 handle_fontified_prop (it)
2900 struct it *it;
2902 Lisp_Object prop, pos;
2903 enum prop_handled handled = HANDLED_NORMALLY;
2905 /* Get the value of the `fontified' property at IT's current buffer
2906 position. (The `fontified' property doesn't have a special
2907 meaning in strings.) If the value is nil, call functions from
2908 Qfontification_functions. */
2909 if (!STRINGP (it->string)
2910 && it->s == NULL
2911 && !NILP (Vfontification_functions)
2912 && !NILP (Vrun_hooks)
2913 && (pos = make_number (IT_CHARPOS (*it)),
2914 prop = Fget_char_property (pos, Qfontified, Qnil),
2915 NILP (prop)))
2917 int count = SPECPDL_INDEX ();
2918 Lisp_Object val;
2920 val = Vfontification_functions;
2921 specbind (Qfontification_functions, Qnil);
2923 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2924 safe_call1 (val, pos);
2925 else
2927 Lisp_Object globals, fn;
2928 struct gcpro gcpro1, gcpro2;
2930 globals = Qnil;
2931 GCPRO2 (val, globals);
2933 for (; CONSP (val); val = XCDR (val))
2935 fn = XCAR (val);
2937 if (EQ (fn, Qt))
2939 /* A value of t indicates this hook has a local
2940 binding; it means to run the global binding too.
2941 In a global value, t should not occur. If it
2942 does, we must ignore it to avoid an endless
2943 loop. */
2944 for (globals = Fdefault_value (Qfontification_functions);
2945 CONSP (globals);
2946 globals = XCDR (globals))
2948 fn = XCAR (globals);
2949 if (!EQ (fn, Qt))
2950 safe_call1 (fn, pos);
2953 else
2954 safe_call1 (fn, pos);
2957 UNGCPRO;
2960 unbind_to (count, Qnil);
2962 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2963 something. This avoids an endless loop if they failed to
2964 fontify the text for which reason ever. */
2965 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2966 handled = HANDLED_RECOMPUTE_PROPS;
2969 return handled;
2974 /***********************************************************************
2975 Faces
2976 ***********************************************************************/
2978 /* Set up iterator IT from face properties at its current position.
2979 Called from handle_stop. */
2981 static enum prop_handled
2982 handle_face_prop (it)
2983 struct it *it;
2985 int new_face_id, next_stop;
2987 if (!STRINGP (it->string))
2989 new_face_id
2990 = face_at_buffer_position (it->w,
2991 IT_CHARPOS (*it),
2992 it->region_beg_charpos,
2993 it->region_end_charpos,
2994 &next_stop,
2995 (IT_CHARPOS (*it)
2996 + TEXT_PROP_DISTANCE_LIMIT),
2999 /* Is this a start of a run of characters with box face?
3000 Caveat: this can be called for a freshly initialized
3001 iterator; face_id is -1 in this case. We know that the new
3002 face will not change until limit, i.e. if the new face has a
3003 box, all characters up to limit will have one. But, as
3004 usual, we don't know whether limit is really the end. */
3005 if (new_face_id != it->face_id)
3007 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3009 /* If new face has a box but old face has not, this is
3010 the start of a run of characters with box, i.e. it has
3011 a shadow on the left side. The value of face_id of the
3012 iterator will be -1 if this is the initial call that gets
3013 the face. In this case, we have to look in front of IT's
3014 position and see whether there is a face != new_face_id. */
3015 it->start_of_box_run_p
3016 = (new_face->box != FACE_NO_BOX
3017 && (it->face_id >= 0
3018 || IT_CHARPOS (*it) == BEG
3019 || new_face_id != face_before_it_pos (it)));
3020 it->face_box_p = new_face->box != FACE_NO_BOX;
3023 else
3025 int base_face_id, bufpos;
3027 if (it->current.overlay_string_index >= 0)
3028 bufpos = IT_CHARPOS (*it);
3029 else
3030 bufpos = 0;
3032 /* For strings from a buffer, i.e. overlay strings or strings
3033 from a `display' property, use the face at IT's current
3034 buffer position as the base face to merge with, so that
3035 overlay strings appear in the same face as surrounding
3036 text, unless they specify their own faces. */
3037 base_face_id = underlying_face_id (it);
3039 new_face_id = face_at_string_position (it->w,
3040 it->string,
3041 IT_STRING_CHARPOS (*it),
3042 bufpos,
3043 it->region_beg_charpos,
3044 it->region_end_charpos,
3045 &next_stop,
3046 base_face_id, 0);
3048 #if 0 /* This shouldn't be neccessary. Let's check it. */
3049 /* If IT is used to display a mode line we would really like to
3050 use the mode line face instead of the frame's default face. */
3051 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3052 && new_face_id == DEFAULT_FACE_ID)
3053 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3054 #endif
3056 /* Is this a start of a run of characters with box? Caveat:
3057 this can be called for a freshly allocated iterator; face_id
3058 is -1 is this case. We know that the new face will not
3059 change until the next check pos, i.e. if the new face has a
3060 box, all characters up to that position will have a
3061 box. But, as usual, we don't know whether that position
3062 is really the end. */
3063 if (new_face_id != it->face_id)
3065 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3066 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3068 /* If new face has a box but old face hasn't, this is the
3069 start of a run of characters with box, i.e. it has a
3070 shadow on the left side. */
3071 it->start_of_box_run_p
3072 = new_face->box && (old_face == NULL || !old_face->box);
3073 it->face_box_p = new_face->box != FACE_NO_BOX;
3077 it->face_id = new_face_id;
3078 return HANDLED_NORMALLY;
3082 /* Return the ID of the face ``underlying'' IT's current position,
3083 which is in a string. If the iterator is associated with a
3084 buffer, return the face at IT's current buffer position.
3085 Otherwise, use the iterator's base_face_id. */
3087 static int
3088 underlying_face_id (it)
3089 struct it *it;
3091 int face_id = it->base_face_id, i;
3093 xassert (STRINGP (it->string));
3095 for (i = it->sp - 1; i >= 0; --i)
3096 if (NILP (it->stack[i].string))
3097 face_id = it->stack[i].face_id;
3099 return face_id;
3103 /* Compute the face one character before or after the current position
3104 of IT. BEFORE_P non-zero means get the face in front of IT's
3105 position. Value is the id of the face. */
3107 static int
3108 face_before_or_after_it_pos (it, before_p)
3109 struct it *it;
3110 int before_p;
3112 int face_id, limit;
3113 int next_check_charpos;
3114 struct text_pos pos;
3116 xassert (it->s == NULL);
3118 if (STRINGP (it->string))
3120 int bufpos, base_face_id;
3122 /* No face change past the end of the string (for the case
3123 we are padding with spaces). No face change before the
3124 string start. */
3125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3126 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3127 return it->face_id;
3129 /* Set pos to the position before or after IT's current position. */
3130 if (before_p)
3131 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3132 else
3133 /* For composition, we must check the character after the
3134 composition. */
3135 pos = (it->what == IT_COMPOSITION
3136 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3137 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3139 if (it->current.overlay_string_index >= 0)
3140 bufpos = IT_CHARPOS (*it);
3141 else
3142 bufpos = 0;
3144 base_face_id = underlying_face_id (it);
3146 /* Get the face for ASCII, or unibyte. */
3147 face_id = face_at_string_position (it->w,
3148 it->string,
3149 CHARPOS (pos),
3150 bufpos,
3151 it->region_beg_charpos,
3152 it->region_end_charpos,
3153 &next_check_charpos,
3154 base_face_id, 0);
3156 /* Correct the face for charsets different from ASCII. Do it
3157 for the multibyte case only. The face returned above is
3158 suitable for unibyte text if IT->string is unibyte. */
3159 if (STRING_MULTIBYTE (it->string))
3161 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3162 int rest = SBYTES (it->string) - BYTEPOS (pos);
3163 int c, len;
3164 struct face *face = FACE_FROM_ID (it->f, face_id);
3166 c = string_char_and_length (p, rest, &len);
3167 face_id = FACE_FOR_CHAR (it->f, face, c);
3170 else
3172 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3173 || (IT_CHARPOS (*it) <= BEGV && before_p))
3174 return it->face_id;
3176 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3177 pos = it->current.pos;
3179 if (before_p)
3180 DEC_TEXT_POS (pos, it->multibyte_p);
3181 else
3183 if (it->what == IT_COMPOSITION)
3184 /* For composition, we must check the position after the
3185 composition. */
3186 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3187 else
3188 INC_TEXT_POS (pos, it->multibyte_p);
3191 /* Determine face for CHARSET_ASCII, or unibyte. */
3192 face_id = face_at_buffer_position (it->w,
3193 CHARPOS (pos),
3194 it->region_beg_charpos,
3195 it->region_end_charpos,
3196 &next_check_charpos,
3197 limit, 0);
3199 /* Correct the face for charsets different from ASCII. Do it
3200 for the multibyte case only. The face returned above is
3201 suitable for unibyte text if current_buffer is unibyte. */
3202 if (it->multibyte_p)
3204 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3205 struct face *face = FACE_FROM_ID (it->f, face_id);
3206 face_id = FACE_FOR_CHAR (it->f, face, c);
3210 return face_id;
3215 /***********************************************************************
3216 Invisible text
3217 ***********************************************************************/
3219 /* Set up iterator IT from invisible properties at its current
3220 position. Called from handle_stop. */
3222 static enum prop_handled
3223 handle_invisible_prop (it)
3224 struct it *it;
3226 enum prop_handled handled = HANDLED_NORMALLY;
3228 if (STRINGP (it->string))
3230 extern Lisp_Object Qinvisible;
3231 Lisp_Object prop, end_charpos, limit, charpos;
3233 /* Get the value of the invisible text property at the
3234 current position. Value will be nil if there is no such
3235 property. */
3236 charpos = make_number (IT_STRING_CHARPOS (*it));
3237 prop = Fget_text_property (charpos, Qinvisible, it->string);
3239 if (!NILP (prop)
3240 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3242 handled = HANDLED_RECOMPUTE_PROPS;
3244 /* Get the position at which the next change of the
3245 invisible text property can be found in IT->string.
3246 Value will be nil if the property value is the same for
3247 all the rest of IT->string. */
3248 XSETINT (limit, SCHARS (it->string));
3249 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3250 it->string, limit);
3252 /* Text at current position is invisible. The next
3253 change in the property is at position end_charpos.
3254 Move IT's current position to that position. */
3255 if (INTEGERP (end_charpos)
3256 && XFASTINT (end_charpos) < XFASTINT (limit))
3258 struct text_pos old;
3259 old = it->current.string_pos;
3260 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3261 compute_string_pos (&it->current.string_pos, old, it->string);
3263 else
3265 /* The rest of the string is invisible. If this is an
3266 overlay string, proceed with the next overlay string
3267 or whatever comes and return a character from there. */
3268 if (it->current.overlay_string_index >= 0)
3270 next_overlay_string (it);
3271 /* Don't check for overlay strings when we just
3272 finished processing them. */
3273 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3275 else
3277 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3278 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3283 else
3285 int invis_p, newpos, next_stop, start_charpos;
3286 Lisp_Object pos, prop, overlay;
3288 /* First of all, is there invisible text at this position? */
3289 start_charpos = IT_CHARPOS (*it);
3290 pos = make_number (IT_CHARPOS (*it));
3291 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3292 &overlay);
3293 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3295 /* If we are on invisible text, skip over it. */
3296 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3298 /* Record whether we have to display an ellipsis for the
3299 invisible text. */
3300 int display_ellipsis_p = invis_p == 2;
3302 handled = HANDLED_RECOMPUTE_PROPS;
3304 /* Loop skipping over invisible text. The loop is left at
3305 ZV or with IT on the first char being visible again. */
3308 /* Try to skip some invisible text. Return value is the
3309 position reached which can be equal to IT's position
3310 if there is nothing invisible here. This skips both
3311 over invisible text properties and overlays with
3312 invisible property. */
3313 newpos = skip_invisible (IT_CHARPOS (*it),
3314 &next_stop, ZV, it->window);
3316 /* If we skipped nothing at all we weren't at invisible
3317 text in the first place. If everything to the end of
3318 the buffer was skipped, end the loop. */
3319 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3320 invis_p = 0;
3321 else
3323 /* We skipped some characters but not necessarily
3324 all there are. Check if we ended up on visible
3325 text. Fget_char_property returns the property of
3326 the char before the given position, i.e. if we
3327 get invis_p = 0, this means that the char at
3328 newpos is visible. */
3329 pos = make_number (newpos);
3330 prop = Fget_char_property (pos, Qinvisible, it->window);
3331 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3334 /* If we ended up on invisible text, proceed to
3335 skip starting with next_stop. */
3336 if (invis_p)
3337 IT_CHARPOS (*it) = next_stop;
3339 while (invis_p);
3341 /* The position newpos is now either ZV or on visible text. */
3342 IT_CHARPOS (*it) = newpos;
3343 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3345 /* If there are before-strings at the start of invisible
3346 text, and the text is invisible because of a text
3347 property, arrange to show before-strings because 20.x did
3348 it that way. (If the text is invisible because of an
3349 overlay property instead of a text property, this is
3350 already handled in the overlay code.) */
3351 if (NILP (overlay)
3352 && get_overlay_strings (it, start_charpos))
3354 handled = HANDLED_RECOMPUTE_PROPS;
3355 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3357 else if (display_ellipsis_p)
3358 setup_for_ellipsis (it, 0);
3362 return handled;
3366 /* Make iterator IT return `...' next.
3367 Replaces LEN characters from buffer. */
3369 static void
3370 setup_for_ellipsis (it, len)
3371 struct it *it;
3372 int len;
3374 /* Use the display table definition for `...'. Invalid glyphs
3375 will be handled by the method returning elements from dpvec. */
3376 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3378 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3379 it->dpvec = v->contents;
3380 it->dpend = v->contents + v->size;
3382 else
3384 /* Default `...'. */
3385 it->dpvec = default_invis_vector;
3386 it->dpend = default_invis_vector + 3;
3389 it->dpvec_char_len = len;
3390 it->current.dpvec_index = 0;
3391 it->dpvec_face_id = -1;
3393 /* Remember the current face id in case glyphs specify faces.
3394 IT's face is restored in set_iterator_to_next.
3395 saved_face_id was set to preceding char's face in handle_stop. */
3396 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3397 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3399 it->method = GET_FROM_DISPLAY_VECTOR;
3400 it->ellipsis_p = 1;
3405 /***********************************************************************
3406 'display' property
3407 ***********************************************************************/
3409 /* Set up iterator IT from `display' property at its current position.
3410 Called from handle_stop.
3411 We return HANDLED_RETURN if some part of the display property
3412 overrides the display of the buffer text itself.
3413 Otherwise we return HANDLED_NORMALLY. */
3415 static enum prop_handled
3416 handle_display_prop (it)
3417 struct it *it;
3419 Lisp_Object prop, object;
3420 struct text_pos *position;
3421 /* Nonzero if some property replaces the display of the text itself. */
3422 int display_replaced_p = 0;
3424 if (STRINGP (it->string))
3426 object = it->string;
3427 position = &it->current.string_pos;
3429 else
3431 object = it->w->buffer;
3432 position = &it->current.pos;
3435 /* Reset those iterator values set from display property values. */
3436 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3437 it->space_width = Qnil;
3438 it->font_height = Qnil;
3439 it->voffset = 0;
3441 /* We don't support recursive `display' properties, i.e. string
3442 values that have a string `display' property, that have a string
3443 `display' property etc. */
3444 if (!it->string_from_display_prop_p)
3445 it->area = TEXT_AREA;
3447 prop = Fget_char_property (make_number (position->charpos),
3448 Qdisplay, object);
3449 if (NILP (prop))
3450 return HANDLED_NORMALLY;
3452 if (CONSP (prop)
3453 /* Simple properties. */
3454 && !EQ (XCAR (prop), Qimage)
3455 && !EQ (XCAR (prop), Qspace)
3456 && !EQ (XCAR (prop), Qwhen)
3457 && !EQ (XCAR (prop), Qslice)
3458 && !EQ (XCAR (prop), Qspace_width)
3459 && !EQ (XCAR (prop), Qheight)
3460 && !EQ (XCAR (prop), Qraise)
3461 /* Marginal area specifications. */
3462 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3463 && !EQ (XCAR (prop), Qleft_fringe)
3464 && !EQ (XCAR (prop), Qright_fringe)
3465 && !NILP (XCAR (prop)))
3467 for (; CONSP (prop); prop = XCDR (prop))
3469 if (handle_single_display_spec (it, XCAR (prop), object,
3470 position, display_replaced_p))
3471 display_replaced_p = 1;
3474 else if (VECTORP (prop))
3476 int i;
3477 for (i = 0; i < ASIZE (prop); ++i)
3478 if (handle_single_display_spec (it, AREF (prop, i), object,
3479 position, display_replaced_p))
3480 display_replaced_p = 1;
3482 else
3484 int ret = handle_single_display_spec (it, prop, object, position, 0);
3485 if (ret < 0) /* Replaced by "", i.e. nothing. */
3486 return HANDLED_RECOMPUTE_PROPS;
3487 if (ret)
3488 display_replaced_p = 1;
3491 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3495 /* Value is the position of the end of the `display' property starting
3496 at START_POS in OBJECT. */
3498 static struct text_pos
3499 display_prop_end (it, object, start_pos)
3500 struct it *it;
3501 Lisp_Object object;
3502 struct text_pos start_pos;
3504 Lisp_Object end;
3505 struct text_pos end_pos;
3507 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3508 Qdisplay, object, Qnil);
3509 CHARPOS (end_pos) = XFASTINT (end);
3510 if (STRINGP (object))
3511 compute_string_pos (&end_pos, start_pos, it->string);
3512 else
3513 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3515 return end_pos;
3519 /* Set up IT from a single `display' specification PROP. OBJECT
3520 is the object in which the `display' property was found. *POSITION
3521 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3522 means that we previously saw a display specification which already
3523 replaced text display with something else, for example an image;
3524 we ignore such properties after the first one has been processed.
3526 If PROP is a `space' or `image' specification, and in some other
3527 cases too, set *POSITION to the position where the `display'
3528 property ends.
3530 Value is non-zero if something was found which replaces the display
3531 of buffer or string text. Specifically, the value is -1 if that
3532 "something" is "nothing". */
3534 static int
3535 handle_single_display_spec (it, spec, object, position,
3536 display_replaced_before_p)
3537 struct it *it;
3538 Lisp_Object spec;
3539 Lisp_Object object;
3540 struct text_pos *position;
3541 int display_replaced_before_p;
3543 Lisp_Object form;
3544 Lisp_Object location, value;
3545 struct text_pos start_pos;
3546 int valid_p;
3548 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3549 If the result is non-nil, use VALUE instead of SPEC. */
3550 form = Qt;
3551 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3553 spec = XCDR (spec);
3554 if (!CONSP (spec))
3555 return 0;
3556 form = XCAR (spec);
3557 spec = XCDR (spec);
3560 if (!NILP (form) && !EQ (form, Qt))
3562 int count = SPECPDL_INDEX ();
3563 struct gcpro gcpro1;
3565 /* Bind `object' to the object having the `display' property, a
3566 buffer or string. Bind `position' to the position in the
3567 object where the property was found, and `buffer-position'
3568 to the current position in the buffer. */
3569 specbind (Qobject, object);
3570 specbind (Qposition, make_number (CHARPOS (*position)));
3571 specbind (Qbuffer_position,
3572 make_number (STRINGP (object)
3573 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3574 GCPRO1 (form);
3575 form = safe_eval (form);
3576 UNGCPRO;
3577 unbind_to (count, Qnil);
3580 if (NILP (form))
3581 return 0;
3583 /* Handle `(height HEIGHT)' specifications. */
3584 if (CONSP (spec)
3585 && EQ (XCAR (spec), Qheight)
3586 && CONSP (XCDR (spec)))
3588 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3589 return 0;
3591 it->font_height = XCAR (XCDR (spec));
3592 if (!NILP (it->font_height))
3594 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3595 int new_height = -1;
3597 if (CONSP (it->font_height)
3598 && (EQ (XCAR (it->font_height), Qplus)
3599 || EQ (XCAR (it->font_height), Qminus))
3600 && CONSP (XCDR (it->font_height))
3601 && INTEGERP (XCAR (XCDR (it->font_height))))
3603 /* `(+ N)' or `(- N)' where N is an integer. */
3604 int steps = XINT (XCAR (XCDR (it->font_height)));
3605 if (EQ (XCAR (it->font_height), Qplus))
3606 steps = - steps;
3607 it->face_id = smaller_face (it->f, it->face_id, steps);
3609 else if (FUNCTIONP (it->font_height))
3611 /* Call function with current height as argument.
3612 Value is the new height. */
3613 Lisp_Object height;
3614 height = safe_call1 (it->font_height,
3615 face->lface[LFACE_HEIGHT_INDEX]);
3616 if (NUMBERP (height))
3617 new_height = XFLOATINT (height);
3619 else if (NUMBERP (it->font_height))
3621 /* Value is a multiple of the canonical char height. */
3622 struct face *face;
3624 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3625 new_height = (XFLOATINT (it->font_height)
3626 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3628 else
3630 /* Evaluate IT->font_height with `height' bound to the
3631 current specified height to get the new height. */
3632 int count = SPECPDL_INDEX ();
3634 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3635 value = safe_eval (it->font_height);
3636 unbind_to (count, Qnil);
3638 if (NUMBERP (value))
3639 new_height = XFLOATINT (value);
3642 if (new_height > 0)
3643 it->face_id = face_with_height (it->f, it->face_id, new_height);
3646 return 0;
3649 /* Handle `(space_width WIDTH)'. */
3650 if (CONSP (spec)
3651 && EQ (XCAR (spec), Qspace_width)
3652 && CONSP (XCDR (spec)))
3654 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3655 return 0;
3657 value = XCAR (XCDR (spec));
3658 if (NUMBERP (value) && XFLOATINT (value) > 0)
3659 it->space_width = value;
3661 return 0;
3664 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3665 if (CONSP (spec)
3666 && EQ (XCAR (spec), Qslice))
3668 Lisp_Object tem;
3670 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3671 return 0;
3673 if (tem = XCDR (spec), CONSP (tem))
3675 it->slice.x = XCAR (tem);
3676 if (tem = XCDR (tem), CONSP (tem))
3678 it->slice.y = XCAR (tem);
3679 if (tem = XCDR (tem), CONSP (tem))
3681 it->slice.width = XCAR (tem);
3682 if (tem = XCDR (tem), CONSP (tem))
3683 it->slice.height = XCAR (tem);
3688 return 0;
3691 /* Handle `(raise FACTOR)'. */
3692 if (CONSP (spec)
3693 && EQ (XCAR (spec), Qraise)
3694 && CONSP (XCDR (spec)))
3696 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3697 return 0;
3699 #ifdef HAVE_WINDOW_SYSTEM
3700 value = XCAR (XCDR (spec));
3701 if (NUMBERP (value))
3703 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3704 it->voffset = - (XFLOATINT (value)
3705 * (FONT_HEIGHT (face->font)));
3707 #endif /* HAVE_WINDOW_SYSTEM */
3709 return 0;
3712 /* Don't handle the other kinds of display specifications
3713 inside a string that we got from a `display' property. */
3714 if (it->string_from_display_prop_p)
3715 return 0;
3717 /* Characters having this form of property are not displayed, so
3718 we have to find the end of the property. */
3719 start_pos = *position;
3720 *position = display_prop_end (it, object, start_pos);
3721 value = Qnil;
3723 /* Stop the scan at that end position--we assume that all
3724 text properties change there. */
3725 it->stop_charpos = position->charpos;
3727 /* Handle `(left-fringe BITMAP [FACE])'
3728 and `(right-fringe BITMAP [FACE])'. */
3729 if (CONSP (spec)
3730 && (EQ (XCAR (spec), Qleft_fringe)
3731 || EQ (XCAR (spec), Qright_fringe))
3732 && CONSP (XCDR (spec)))
3734 int face_id = DEFAULT_FACE_ID;
3735 int fringe_bitmap;
3737 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3738 /* If we return here, POSITION has been advanced
3739 across the text with this property. */
3740 return 0;
3742 #ifdef HAVE_WINDOW_SYSTEM
3743 value = XCAR (XCDR (spec));
3744 if (!SYMBOLP (value)
3745 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3746 /* If we return here, POSITION has been advanced
3747 across the text with this property. */
3748 return 0;
3750 if (CONSP (XCDR (XCDR (spec))))
3752 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3753 int face_id2 = lookup_derived_face (it->f, face_name,
3754 'A', FRINGE_FACE_ID, 0);
3755 if (face_id2 >= 0)
3756 face_id = face_id2;
3759 /* Save current settings of IT so that we can restore them
3760 when we are finished with the glyph property value. */
3762 push_it (it);
3764 it->area = TEXT_AREA;
3765 it->what = IT_IMAGE;
3766 it->image_id = -1; /* no image */
3767 it->position = start_pos;
3768 it->object = NILP (object) ? it->w->buffer : object;
3769 it->method = GET_FROM_IMAGE;
3770 it->face_id = face_id;
3772 /* Say that we haven't consumed the characters with
3773 `display' property yet. The call to pop_it in
3774 set_iterator_to_next will clean this up. */
3775 *position = start_pos;
3777 if (EQ (XCAR (spec), Qleft_fringe))
3779 it->left_user_fringe_bitmap = fringe_bitmap;
3780 it->left_user_fringe_face_id = face_id;
3782 else
3784 it->right_user_fringe_bitmap = fringe_bitmap;
3785 it->right_user_fringe_face_id = face_id;
3787 #endif /* HAVE_WINDOW_SYSTEM */
3788 return 1;
3791 /* Prepare to handle `((margin left-margin) ...)',
3792 `((margin right-margin) ...)' and `((margin nil) ...)'
3793 prefixes for display specifications. */
3794 location = Qunbound;
3795 if (CONSP (spec) && CONSP (XCAR (spec)))
3797 Lisp_Object tem;
3799 value = XCDR (spec);
3800 if (CONSP (value))
3801 value = XCAR (value);
3803 tem = XCAR (spec);
3804 if (EQ (XCAR (tem), Qmargin)
3805 && (tem = XCDR (tem),
3806 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3807 (NILP (tem)
3808 || EQ (tem, Qleft_margin)
3809 || EQ (tem, Qright_margin))))
3810 location = tem;
3813 if (EQ (location, Qunbound))
3815 location = Qnil;
3816 value = spec;
3819 /* After this point, VALUE is the property after any
3820 margin prefix has been stripped. It must be a string,
3821 an image specification, or `(space ...)'.
3823 LOCATION specifies where to display: `left-margin',
3824 `right-margin' or nil. */
3826 valid_p = (STRINGP (value)
3827 #ifdef HAVE_WINDOW_SYSTEM
3828 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3829 #endif /* not HAVE_WINDOW_SYSTEM */
3830 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3832 if (valid_p && !display_replaced_before_p)
3834 /* Save current settings of IT so that we can restore them
3835 when we are finished with the glyph property value. */
3836 push_it (it);
3838 if (NILP (location))
3839 it->area = TEXT_AREA;
3840 else if (EQ (location, Qleft_margin))
3841 it->area = LEFT_MARGIN_AREA;
3842 else
3843 it->area = RIGHT_MARGIN_AREA;
3845 if (STRINGP (value))
3847 if (SCHARS (value) == 0)
3849 pop_it (it);
3850 return -1; /* Replaced by "", i.e. nothing. */
3852 it->string = value;
3853 it->multibyte_p = STRING_MULTIBYTE (it->string);
3854 it->current.overlay_string_index = -1;
3855 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3856 it->end_charpos = it->string_nchars = SCHARS (it->string);
3857 it->method = GET_FROM_STRING;
3858 it->stop_charpos = 0;
3859 it->string_from_display_prop_p = 1;
3860 /* Say that we haven't consumed the characters with
3861 `display' property yet. The call to pop_it in
3862 set_iterator_to_next will clean this up. */
3863 *position = start_pos;
3865 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3867 it->method = GET_FROM_STRETCH;
3868 it->object = value;
3869 it->current.pos = it->position = start_pos;
3871 #ifdef HAVE_WINDOW_SYSTEM
3872 else
3874 it->what = IT_IMAGE;
3875 it->image_id = lookup_image (it->f, value);
3876 it->position = start_pos;
3877 it->object = NILP (object) ? it->w->buffer : object;
3878 it->method = GET_FROM_IMAGE;
3880 /* Say that we haven't consumed the characters with
3881 `display' property yet. The call to pop_it in
3882 set_iterator_to_next will clean this up. */
3883 *position = start_pos;
3885 #endif /* HAVE_WINDOW_SYSTEM */
3887 return 1;
3890 /* Invalid property or property not supported. Restore
3891 POSITION to what it was before. */
3892 *position = start_pos;
3893 return 0;
3897 /* Check if SPEC is a display specification value whose text should be
3898 treated as intangible. */
3900 static int
3901 single_display_spec_intangible_p (prop)
3902 Lisp_Object prop;
3904 /* Skip over `when FORM'. */
3905 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3907 prop = XCDR (prop);
3908 if (!CONSP (prop))
3909 return 0;
3910 prop = XCDR (prop);
3913 if (STRINGP (prop))
3914 return 1;
3916 if (!CONSP (prop))
3917 return 0;
3919 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3920 we don't need to treat text as intangible. */
3921 if (EQ (XCAR (prop), Qmargin))
3923 prop = XCDR (prop);
3924 if (!CONSP (prop))
3925 return 0;
3927 prop = XCDR (prop);
3928 if (!CONSP (prop)
3929 || EQ (XCAR (prop), Qleft_margin)
3930 || EQ (XCAR (prop), Qright_margin))
3931 return 0;
3934 return (CONSP (prop)
3935 && (EQ (XCAR (prop), Qimage)
3936 || EQ (XCAR (prop), Qspace)));
3940 /* Check if PROP is a display property value whose text should be
3941 treated as intangible. */
3944 display_prop_intangible_p (prop)
3945 Lisp_Object prop;
3947 if (CONSP (prop)
3948 && CONSP (XCAR (prop))
3949 && !EQ (Qmargin, XCAR (XCAR (prop))))
3951 /* A list of sub-properties. */
3952 while (CONSP (prop))
3954 if (single_display_spec_intangible_p (XCAR (prop)))
3955 return 1;
3956 prop = XCDR (prop);
3959 else if (VECTORP (prop))
3961 /* A vector of sub-properties. */
3962 int i;
3963 for (i = 0; i < ASIZE (prop); ++i)
3964 if (single_display_spec_intangible_p (AREF (prop, i)))
3965 return 1;
3967 else
3968 return single_display_spec_intangible_p (prop);
3970 return 0;
3974 /* Return 1 if PROP is a display sub-property value containing STRING. */
3976 static int
3977 single_display_spec_string_p (prop, string)
3978 Lisp_Object prop, string;
3980 if (EQ (string, prop))
3981 return 1;
3983 /* Skip over `when FORM'. */
3984 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3986 prop = XCDR (prop);
3987 if (!CONSP (prop))
3988 return 0;
3989 prop = XCDR (prop);
3992 if (CONSP (prop))
3993 /* Skip over `margin LOCATION'. */
3994 if (EQ (XCAR (prop), Qmargin))
3996 prop = XCDR (prop);
3997 if (!CONSP (prop))
3998 return 0;
4000 prop = XCDR (prop);
4001 if (!CONSP (prop))
4002 return 0;
4005 return CONSP (prop) && EQ (XCAR (prop), string);
4009 /* Return 1 if STRING appears in the `display' property PROP. */
4011 static int
4012 display_prop_string_p (prop, string)
4013 Lisp_Object prop, string;
4015 if (CONSP (prop)
4016 && CONSP (XCAR (prop))
4017 && !EQ (Qmargin, XCAR (XCAR (prop))))
4019 /* A list of sub-properties. */
4020 while (CONSP (prop))
4022 if (single_display_spec_string_p (XCAR (prop), string))
4023 return 1;
4024 prop = XCDR (prop);
4027 else if (VECTORP (prop))
4029 /* A vector of sub-properties. */
4030 int i;
4031 for (i = 0; i < ASIZE (prop); ++i)
4032 if (single_display_spec_string_p (AREF (prop, i), string))
4033 return 1;
4035 else
4036 return single_display_spec_string_p (prop, string);
4038 return 0;
4042 /* Determine from which buffer position in W's buffer STRING comes
4043 from. AROUND_CHARPOS is an approximate position where it could
4044 be from. Value is the buffer position or 0 if it couldn't be
4045 determined.
4047 W's buffer must be current.
4049 This function is necessary because we don't record buffer positions
4050 in glyphs generated from strings (to keep struct glyph small).
4051 This function may only use code that doesn't eval because it is
4052 called asynchronously from note_mouse_highlight. */
4055 string_buffer_position (w, string, around_charpos)
4056 struct window *w;
4057 Lisp_Object string;
4058 int around_charpos;
4060 Lisp_Object limit, prop, pos;
4061 const int MAX_DISTANCE = 1000;
4062 int found = 0;
4064 pos = make_number (around_charpos);
4065 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4066 while (!found && !EQ (pos, limit))
4068 prop = Fget_char_property (pos, Qdisplay, Qnil);
4069 if (!NILP (prop) && display_prop_string_p (prop, string))
4070 found = 1;
4071 else
4072 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4075 if (!found)
4077 pos = make_number (around_charpos);
4078 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4079 while (!found && !EQ (pos, limit))
4081 prop = Fget_char_property (pos, Qdisplay, Qnil);
4082 if (!NILP (prop) && display_prop_string_p (prop, string))
4083 found = 1;
4084 else
4085 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4086 limit);
4090 return found ? XINT (pos) : 0;
4095 /***********************************************************************
4096 `composition' property
4097 ***********************************************************************/
4099 /* Set up iterator IT from `composition' property at its current
4100 position. Called from handle_stop. */
4102 static enum prop_handled
4103 handle_composition_prop (it)
4104 struct it *it;
4106 Lisp_Object prop, string;
4107 int pos, pos_byte, end;
4108 enum prop_handled handled = HANDLED_NORMALLY;
4110 if (STRINGP (it->string))
4112 pos = IT_STRING_CHARPOS (*it);
4113 pos_byte = IT_STRING_BYTEPOS (*it);
4114 string = it->string;
4116 else
4118 pos = IT_CHARPOS (*it);
4119 pos_byte = IT_BYTEPOS (*it);
4120 string = Qnil;
4123 /* If there's a valid composition and point is not inside of the
4124 composition (in the case that the composition is from the current
4125 buffer), draw a glyph composed from the composition components. */
4126 if (find_composition (pos, -1, &pos, &end, &prop, string)
4127 && COMPOSITION_VALID_P (pos, end, prop)
4128 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4130 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4132 if (id >= 0)
4134 it->method = GET_FROM_COMPOSITION;
4135 it->cmp_id = id;
4136 it->cmp_len = COMPOSITION_LENGTH (prop);
4137 /* For a terminal, draw only the first character of the
4138 components. */
4139 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4140 it->len = (STRINGP (it->string)
4141 ? string_char_to_byte (it->string, end)
4142 : CHAR_TO_BYTE (end)) - pos_byte;
4143 it->stop_charpos = end;
4144 handled = HANDLED_RETURN;
4148 return handled;
4153 /***********************************************************************
4154 Overlay strings
4155 ***********************************************************************/
4157 /* The following structure is used to record overlay strings for
4158 later sorting in load_overlay_strings. */
4160 struct overlay_entry
4162 Lisp_Object overlay;
4163 Lisp_Object string;
4164 int priority;
4165 int after_string_p;
4169 /* Set up iterator IT from overlay strings at its current position.
4170 Called from handle_stop. */
4172 static enum prop_handled
4173 handle_overlay_change (it)
4174 struct it *it;
4176 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4177 return HANDLED_RECOMPUTE_PROPS;
4178 else
4179 return HANDLED_NORMALLY;
4183 /* Set up the next overlay string for delivery by IT, if there is an
4184 overlay string to deliver. Called by set_iterator_to_next when the
4185 end of the current overlay string is reached. If there are more
4186 overlay strings to display, IT->string and
4187 IT->current.overlay_string_index are set appropriately here.
4188 Otherwise IT->string is set to nil. */
4190 static void
4191 next_overlay_string (it)
4192 struct it *it;
4194 ++it->current.overlay_string_index;
4195 if (it->current.overlay_string_index == it->n_overlay_strings)
4197 /* No more overlay strings. Restore IT's settings to what
4198 they were before overlay strings were processed, and
4199 continue to deliver from current_buffer. */
4200 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4202 pop_it (it);
4203 xassert (it->stop_charpos >= BEGV
4204 && it->stop_charpos <= it->end_charpos);
4205 it->string = Qnil;
4206 it->current.overlay_string_index = -1;
4207 SET_TEXT_POS (it->current.string_pos, -1, -1);
4208 it->n_overlay_strings = 0;
4209 it->method = GET_FROM_BUFFER;
4211 /* If we're at the end of the buffer, record that we have
4212 processed the overlay strings there already, so that
4213 next_element_from_buffer doesn't try it again. */
4214 if (IT_CHARPOS (*it) >= it->end_charpos)
4215 it->overlay_strings_at_end_processed_p = 1;
4217 /* If we have to display `...' for invisible text, set
4218 the iterator up for that. */
4219 if (display_ellipsis_p)
4220 setup_for_ellipsis (it, 0);
4222 else
4224 /* There are more overlay strings to process. If
4225 IT->current.overlay_string_index has advanced to a position
4226 where we must load IT->overlay_strings with more strings, do
4227 it. */
4228 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4230 if (it->current.overlay_string_index && i == 0)
4231 load_overlay_strings (it, 0);
4233 /* Initialize IT to deliver display elements from the overlay
4234 string. */
4235 it->string = it->overlay_strings[i];
4236 it->multibyte_p = STRING_MULTIBYTE (it->string);
4237 SET_TEXT_POS (it->current.string_pos, 0, 0);
4238 it->method = GET_FROM_STRING;
4239 it->stop_charpos = 0;
4242 CHECK_IT (it);
4246 /* Compare two overlay_entry structures E1 and E2. Used as a
4247 comparison function for qsort in load_overlay_strings. Overlay
4248 strings for the same position are sorted so that
4250 1. All after-strings come in front of before-strings, except
4251 when they come from the same overlay.
4253 2. Within after-strings, strings are sorted so that overlay strings
4254 from overlays with higher priorities come first.
4256 2. Within before-strings, strings are sorted so that overlay
4257 strings from overlays with higher priorities come last.
4259 Value is analogous to strcmp. */
4262 static int
4263 compare_overlay_entries (e1, e2)
4264 void *e1, *e2;
4266 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4267 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4268 int result;
4270 if (entry1->after_string_p != entry2->after_string_p)
4272 /* Let after-strings appear in front of before-strings if
4273 they come from different overlays. */
4274 if (EQ (entry1->overlay, entry2->overlay))
4275 result = entry1->after_string_p ? 1 : -1;
4276 else
4277 result = entry1->after_string_p ? -1 : 1;
4279 else if (entry1->after_string_p)
4280 /* After-strings sorted in order of decreasing priority. */
4281 result = entry2->priority - entry1->priority;
4282 else
4283 /* Before-strings sorted in order of increasing priority. */
4284 result = entry1->priority - entry2->priority;
4286 return result;
4290 /* Load the vector IT->overlay_strings with overlay strings from IT's
4291 current buffer position, or from CHARPOS if that is > 0. Set
4292 IT->n_overlays to the total number of overlay strings found.
4294 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4295 a time. On entry into load_overlay_strings,
4296 IT->current.overlay_string_index gives the number of overlay
4297 strings that have already been loaded by previous calls to this
4298 function.
4300 IT->add_overlay_start contains an additional overlay start
4301 position to consider for taking overlay strings from, if non-zero.
4302 This position comes into play when the overlay has an `invisible'
4303 property, and both before and after-strings. When we've skipped to
4304 the end of the overlay, because of its `invisible' property, we
4305 nevertheless want its before-string to appear.
4306 IT->add_overlay_start will contain the overlay start position
4307 in this case.
4309 Overlay strings are sorted so that after-string strings come in
4310 front of before-string strings. Within before and after-strings,
4311 strings are sorted by overlay priority. See also function
4312 compare_overlay_entries. */
4314 static void
4315 load_overlay_strings (it, charpos)
4316 struct it *it;
4317 int charpos;
4319 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4320 Lisp_Object overlay, window, str, invisible;
4321 struct Lisp_Overlay *ov;
4322 int start, end;
4323 int size = 20;
4324 int n = 0, i, j, invis_p;
4325 struct overlay_entry *entries
4326 = (struct overlay_entry *) alloca (size * sizeof *entries);
4328 if (charpos <= 0)
4329 charpos = IT_CHARPOS (*it);
4331 /* Append the overlay string STRING of overlay OVERLAY to vector
4332 `entries' which has size `size' and currently contains `n'
4333 elements. AFTER_P non-zero means STRING is an after-string of
4334 OVERLAY. */
4335 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4336 do \
4338 Lisp_Object priority; \
4340 if (n == size) \
4342 int new_size = 2 * size; \
4343 struct overlay_entry *old = entries; \
4344 entries = \
4345 (struct overlay_entry *) alloca (new_size \
4346 * sizeof *entries); \
4347 bcopy (old, entries, size * sizeof *entries); \
4348 size = new_size; \
4351 entries[n].string = (STRING); \
4352 entries[n].overlay = (OVERLAY); \
4353 priority = Foverlay_get ((OVERLAY), Qpriority); \
4354 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4355 entries[n].after_string_p = (AFTER_P); \
4356 ++n; \
4358 while (0)
4360 /* Process overlay before the overlay center. */
4361 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4363 XSETMISC (overlay, ov);
4364 xassert (OVERLAYP (overlay));
4365 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4366 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4368 if (end < charpos)
4369 break;
4371 /* Skip this overlay if it doesn't start or end at IT's current
4372 position. */
4373 if (end != charpos && start != charpos)
4374 continue;
4376 /* Skip this overlay if it doesn't apply to IT->w. */
4377 window = Foverlay_get (overlay, Qwindow);
4378 if (WINDOWP (window) && XWINDOW (window) != it->w)
4379 continue;
4381 /* If the text ``under'' the overlay is invisible, both before-
4382 and after-strings from this overlay are visible; start and
4383 end position are indistinguishable. */
4384 invisible = Foverlay_get (overlay, Qinvisible);
4385 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4387 /* If overlay has a non-empty before-string, record it. */
4388 if ((start == charpos || (end == charpos && invis_p))
4389 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4390 && SCHARS (str))
4391 RECORD_OVERLAY_STRING (overlay, str, 0);
4393 /* If overlay has a non-empty after-string, record it. */
4394 if ((end == charpos || (start == charpos && invis_p))
4395 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4396 && SCHARS (str))
4397 RECORD_OVERLAY_STRING (overlay, str, 1);
4400 /* Process overlays after the overlay center. */
4401 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4403 XSETMISC (overlay, ov);
4404 xassert (OVERLAYP (overlay));
4405 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4406 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4408 if (start > charpos)
4409 break;
4411 /* Skip this overlay if it doesn't start or end at IT's current
4412 position. */
4413 if (end != charpos && start != charpos)
4414 continue;
4416 /* Skip this overlay if it doesn't apply to IT->w. */
4417 window = Foverlay_get (overlay, Qwindow);
4418 if (WINDOWP (window) && XWINDOW (window) != it->w)
4419 continue;
4421 /* If the text ``under'' the overlay is invisible, it has a zero
4422 dimension, and both before- and after-strings apply. */
4423 invisible = Foverlay_get (overlay, Qinvisible);
4424 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4426 /* If overlay has a non-empty before-string, record it. */
4427 if ((start == charpos || (end == charpos && invis_p))
4428 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4429 && SCHARS (str))
4430 RECORD_OVERLAY_STRING (overlay, str, 0);
4432 /* If overlay has a non-empty after-string, record it. */
4433 if ((end == charpos || (start == charpos && invis_p))
4434 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4435 && SCHARS (str))
4436 RECORD_OVERLAY_STRING (overlay, str, 1);
4439 #undef RECORD_OVERLAY_STRING
4441 /* Sort entries. */
4442 if (n > 1)
4443 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4445 /* Record the total number of strings to process. */
4446 it->n_overlay_strings = n;
4448 /* IT->current.overlay_string_index is the number of overlay strings
4449 that have already been consumed by IT. Copy some of the
4450 remaining overlay strings to IT->overlay_strings. */
4451 i = 0;
4452 j = it->current.overlay_string_index;
4453 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4454 it->overlay_strings[i++] = entries[j++].string;
4456 CHECK_IT (it);
4460 /* Get the first chunk of overlay strings at IT's current buffer
4461 position, or at CHARPOS if that is > 0. Value is non-zero if at
4462 least one overlay string was found. */
4464 static int
4465 get_overlay_strings (it, charpos)
4466 struct it *it;
4467 int charpos;
4469 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4470 process. This fills IT->overlay_strings with strings, and sets
4471 IT->n_overlay_strings to the total number of strings to process.
4472 IT->pos.overlay_string_index has to be set temporarily to zero
4473 because load_overlay_strings needs this; it must be set to -1
4474 when no overlay strings are found because a zero value would
4475 indicate a position in the first overlay string. */
4476 it->current.overlay_string_index = 0;
4477 load_overlay_strings (it, charpos);
4479 /* If we found overlay strings, set up IT to deliver display
4480 elements from the first one. Otherwise set up IT to deliver
4481 from current_buffer. */
4482 if (it->n_overlay_strings)
4484 /* Make sure we know settings in current_buffer, so that we can
4485 restore meaningful values when we're done with the overlay
4486 strings. */
4487 compute_stop_pos (it);
4488 xassert (it->face_id >= 0);
4490 /* Save IT's settings. They are restored after all overlay
4491 strings have been processed. */
4492 xassert (it->sp == 0);
4493 push_it (it);
4495 /* Set up IT to deliver display elements from the first overlay
4496 string. */
4497 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4498 it->string = it->overlay_strings[0];
4499 it->stop_charpos = 0;
4500 xassert (STRINGP (it->string));
4501 it->end_charpos = SCHARS (it->string);
4502 it->multibyte_p = STRING_MULTIBYTE (it->string);
4503 it->method = GET_FROM_STRING;
4505 else
4507 it->string = Qnil;
4508 it->current.overlay_string_index = -1;
4509 it->method = GET_FROM_BUFFER;
4512 CHECK_IT (it);
4514 /* Value is non-zero if we found at least one overlay string. */
4515 return STRINGP (it->string);
4520 /***********************************************************************
4521 Saving and restoring state
4522 ***********************************************************************/
4524 /* Save current settings of IT on IT->stack. Called, for example,
4525 before setting up IT for an overlay string, to be able to restore
4526 IT's settings to what they were after the overlay string has been
4527 processed. */
4529 static void
4530 push_it (it)
4531 struct it *it;
4533 struct iterator_stack_entry *p;
4535 xassert (it->sp < 2);
4536 p = it->stack + it->sp;
4538 p->stop_charpos = it->stop_charpos;
4539 xassert (it->face_id >= 0);
4540 p->face_id = it->face_id;
4541 p->string = it->string;
4542 p->pos = it->current;
4543 p->end_charpos = it->end_charpos;
4544 p->string_nchars = it->string_nchars;
4545 p->area = it->area;
4546 p->multibyte_p = it->multibyte_p;
4547 p->slice = it->slice;
4548 p->space_width = it->space_width;
4549 p->font_height = it->font_height;
4550 p->voffset = it->voffset;
4551 p->string_from_display_prop_p = it->string_from_display_prop_p;
4552 p->display_ellipsis_p = 0;
4553 ++it->sp;
4557 /* Restore IT's settings from IT->stack. Called, for example, when no
4558 more overlay strings must be processed, and we return to delivering
4559 display elements from a buffer, or when the end of a string from a
4560 `display' property is reached and we return to delivering display
4561 elements from an overlay string, or from a buffer. */
4563 static void
4564 pop_it (it)
4565 struct it *it;
4567 struct iterator_stack_entry *p;
4569 xassert (it->sp > 0);
4570 --it->sp;
4571 p = it->stack + it->sp;
4572 it->stop_charpos = p->stop_charpos;
4573 it->face_id = p->face_id;
4574 it->string = p->string;
4575 it->current = p->pos;
4576 it->end_charpos = p->end_charpos;
4577 it->string_nchars = p->string_nchars;
4578 it->area = p->area;
4579 it->multibyte_p = p->multibyte_p;
4580 it->slice = p->slice;
4581 it->space_width = p->space_width;
4582 it->font_height = p->font_height;
4583 it->voffset = p->voffset;
4584 it->string_from_display_prop_p = p->string_from_display_prop_p;
4589 /***********************************************************************
4590 Moving over lines
4591 ***********************************************************************/
4593 /* Set IT's current position to the previous line start. */
4595 static void
4596 back_to_previous_line_start (it)
4597 struct it *it;
4599 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4600 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4604 /* Move IT to the next line start.
4606 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4607 we skipped over part of the text (as opposed to moving the iterator
4608 continuously over the text). Otherwise, don't change the value
4609 of *SKIPPED_P.
4611 Newlines may come from buffer text, overlay strings, or strings
4612 displayed via the `display' property. That's the reason we can't
4613 simply use find_next_newline_no_quit.
4615 Note that this function may not skip over invisible text that is so
4616 because of text properties and immediately follows a newline. If
4617 it would, function reseat_at_next_visible_line_start, when called
4618 from set_iterator_to_next, would effectively make invisible
4619 characters following a newline part of the wrong glyph row, which
4620 leads to wrong cursor motion. */
4622 static int
4623 forward_to_next_line_start (it, skipped_p)
4624 struct it *it;
4625 int *skipped_p;
4627 int old_selective, newline_found_p, n;
4628 const int MAX_NEWLINE_DISTANCE = 500;
4630 /* If already on a newline, just consume it to avoid unintended
4631 skipping over invisible text below. */
4632 if (it->what == IT_CHARACTER
4633 && it->c == '\n'
4634 && CHARPOS (it->position) == IT_CHARPOS (*it))
4636 set_iterator_to_next (it, 0);
4637 it->c = 0;
4638 return 1;
4641 /* Don't handle selective display in the following. It's (a)
4642 unnecessary because it's done by the caller, and (b) leads to an
4643 infinite recursion because next_element_from_ellipsis indirectly
4644 calls this function. */
4645 old_selective = it->selective;
4646 it->selective = 0;
4648 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4649 from buffer text. */
4650 for (n = newline_found_p = 0;
4651 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4652 n += STRINGP (it->string) ? 0 : 1)
4654 if (!get_next_display_element (it))
4655 return 0;
4656 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4657 set_iterator_to_next (it, 0);
4660 /* If we didn't find a newline near enough, see if we can use a
4661 short-cut. */
4662 if (!newline_found_p)
4664 int start = IT_CHARPOS (*it);
4665 int limit = find_next_newline_no_quit (start, 1);
4666 Lisp_Object pos;
4668 xassert (!STRINGP (it->string));
4670 /* If there isn't any `display' property in sight, and no
4671 overlays, we can just use the position of the newline in
4672 buffer text. */
4673 if (it->stop_charpos >= limit
4674 || ((pos = Fnext_single_property_change (make_number (start),
4675 Qdisplay,
4676 Qnil, make_number (limit)),
4677 NILP (pos))
4678 && next_overlay_change (start) == ZV))
4680 IT_CHARPOS (*it) = limit;
4681 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4682 *skipped_p = newline_found_p = 1;
4684 else
4686 while (get_next_display_element (it)
4687 && !newline_found_p)
4689 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4690 set_iterator_to_next (it, 0);
4695 it->selective = old_selective;
4696 return newline_found_p;
4700 /* Set IT's current position to the previous visible line start. Skip
4701 invisible text that is so either due to text properties or due to
4702 selective display. Caution: this does not change IT->current_x and
4703 IT->hpos. */
4705 static void
4706 back_to_previous_visible_line_start (it)
4707 struct it *it;
4709 while (IT_CHARPOS (*it) > BEGV)
4711 back_to_previous_line_start (it);
4712 if (IT_CHARPOS (*it) <= BEGV)
4713 break;
4715 /* If selective > 0, then lines indented more than that values
4716 are invisible. */
4717 if (it->selective > 0
4718 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4719 (double) it->selective)) /* iftc */
4720 continue;
4722 /* Check the newline before point for invisibility. */
4724 Lisp_Object prop;
4725 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4726 Qinvisible, it->window);
4727 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4728 continue;
4731 /* If newline has a display property that replaces the newline with something
4732 else (image or text), find start of overlay or interval and continue search
4733 from that point. */
4734 if (IT_CHARPOS (*it) > BEGV)
4736 struct it it2 = *it;
4737 int pos;
4738 int beg, end;
4739 Lisp_Object val, overlay;
4741 pos = --IT_CHARPOS (it2);
4742 --IT_BYTEPOS (it2);
4743 it2.sp = 0;
4744 if (handle_display_prop (&it2) == HANDLED_RETURN
4745 && !NILP (val = get_char_property_and_overlay
4746 (make_number (pos), Qdisplay, Qnil, &overlay))
4747 && (OVERLAYP (overlay)
4748 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4749 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4751 if (beg < BEGV)
4752 beg = BEGV;
4753 IT_CHARPOS (*it) = beg;
4754 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4755 continue;
4759 break;
4762 xassert (IT_CHARPOS (*it) >= BEGV);
4763 xassert (IT_CHARPOS (*it) == BEGV
4764 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4765 CHECK_IT (it);
4769 /* Reseat iterator IT at the previous visible line start. Skip
4770 invisible text that is so either due to text properties or due to
4771 selective display. At the end, update IT's overlay information,
4772 face information etc. */
4774 void
4775 reseat_at_previous_visible_line_start (it)
4776 struct it *it;
4778 back_to_previous_visible_line_start (it);
4779 reseat (it, it->current.pos, 1);
4780 CHECK_IT (it);
4784 /* Reseat iterator IT on the next visible line start in the current
4785 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4786 preceding the line start. Skip over invisible text that is so
4787 because of selective display. Compute faces, overlays etc at the
4788 new position. Note that this function does not skip over text that
4789 is invisible because of text properties. */
4791 static void
4792 reseat_at_next_visible_line_start (it, on_newline_p)
4793 struct it *it;
4794 int on_newline_p;
4796 int newline_found_p, skipped_p = 0;
4798 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4800 /* Skip over lines that are invisible because they are indented
4801 more than the value of IT->selective. */
4802 if (it->selective > 0)
4803 while (IT_CHARPOS (*it) < ZV
4804 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4805 (double) it->selective)) /* iftc */
4807 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4808 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4811 /* Position on the newline if that's what's requested. */
4812 if (on_newline_p && newline_found_p)
4814 if (STRINGP (it->string))
4816 if (IT_STRING_CHARPOS (*it) > 0)
4818 --IT_STRING_CHARPOS (*it);
4819 --IT_STRING_BYTEPOS (*it);
4822 else if (IT_CHARPOS (*it) > BEGV)
4824 --IT_CHARPOS (*it);
4825 --IT_BYTEPOS (*it);
4826 reseat (it, it->current.pos, 0);
4829 else if (skipped_p)
4830 reseat (it, it->current.pos, 0);
4832 CHECK_IT (it);
4837 /***********************************************************************
4838 Changing an iterator's position
4839 ***********************************************************************/
4841 /* Change IT's current position to POS in current_buffer. If FORCE_P
4842 is non-zero, always check for text properties at the new position.
4843 Otherwise, text properties are only looked up if POS >=
4844 IT->check_charpos of a property. */
4846 static void
4847 reseat (it, pos, force_p)
4848 struct it *it;
4849 struct text_pos pos;
4850 int force_p;
4852 int original_pos = IT_CHARPOS (*it);
4854 reseat_1 (it, pos, 0);
4856 /* Determine where to check text properties. Avoid doing it
4857 where possible because text property lookup is very expensive. */
4858 if (force_p
4859 || CHARPOS (pos) > it->stop_charpos
4860 || CHARPOS (pos) < original_pos)
4861 handle_stop (it);
4863 CHECK_IT (it);
4867 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4868 IT->stop_pos to POS, also. */
4870 static void
4871 reseat_1 (it, pos, set_stop_p)
4872 struct it *it;
4873 struct text_pos pos;
4874 int set_stop_p;
4876 /* Don't call this function when scanning a C string. */
4877 xassert (it->s == NULL);
4879 /* POS must be a reasonable value. */
4880 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4882 it->current.pos = it->position = pos;
4883 XSETBUFFER (it->object, current_buffer);
4884 it->end_charpos = ZV;
4885 it->dpvec = NULL;
4886 it->current.dpvec_index = -1;
4887 it->current.overlay_string_index = -1;
4888 IT_STRING_CHARPOS (*it) = -1;
4889 IT_STRING_BYTEPOS (*it) = -1;
4890 it->string = Qnil;
4891 it->method = GET_FROM_BUFFER;
4892 /* RMS: I added this to fix a bug in move_it_vertically_backward
4893 where it->area continued to relate to the starting point
4894 for the backward motion. Bug report from
4895 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4896 However, I am not sure whether reseat still does the right thing
4897 in general after this change. */
4898 it->area = TEXT_AREA;
4899 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4900 it->sp = 0;
4901 it->face_before_selective_p = 0;
4903 if (set_stop_p)
4904 it->stop_charpos = CHARPOS (pos);
4908 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4909 If S is non-null, it is a C string to iterate over. Otherwise,
4910 STRING gives a Lisp string to iterate over.
4912 If PRECISION > 0, don't return more then PRECISION number of
4913 characters from the string.
4915 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4916 characters have been returned. FIELD_WIDTH < 0 means an infinite
4917 field width.
4919 MULTIBYTE = 0 means disable processing of multibyte characters,
4920 MULTIBYTE > 0 means enable it,
4921 MULTIBYTE < 0 means use IT->multibyte_p.
4923 IT must be initialized via a prior call to init_iterator before
4924 calling this function. */
4926 static void
4927 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4928 struct it *it;
4929 unsigned char *s;
4930 Lisp_Object string;
4931 int charpos;
4932 int precision, field_width, multibyte;
4934 /* No region in strings. */
4935 it->region_beg_charpos = it->region_end_charpos = -1;
4937 /* No text property checks performed by default, but see below. */
4938 it->stop_charpos = -1;
4940 /* Set iterator position and end position. */
4941 bzero (&it->current, sizeof it->current);
4942 it->current.overlay_string_index = -1;
4943 it->current.dpvec_index = -1;
4944 xassert (charpos >= 0);
4946 /* If STRING is specified, use its multibyteness, otherwise use the
4947 setting of MULTIBYTE, if specified. */
4948 if (multibyte >= 0)
4949 it->multibyte_p = multibyte > 0;
4951 if (s == NULL)
4953 xassert (STRINGP (string));
4954 it->string = string;
4955 it->s = NULL;
4956 it->end_charpos = it->string_nchars = SCHARS (string);
4957 it->method = GET_FROM_STRING;
4958 it->current.string_pos = string_pos (charpos, string);
4960 else
4962 it->s = s;
4963 it->string = Qnil;
4965 /* Note that we use IT->current.pos, not it->current.string_pos,
4966 for displaying C strings. */
4967 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4968 if (it->multibyte_p)
4970 it->current.pos = c_string_pos (charpos, s, 1);
4971 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4973 else
4975 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4976 it->end_charpos = it->string_nchars = strlen (s);
4979 it->method = GET_FROM_C_STRING;
4982 /* PRECISION > 0 means don't return more than PRECISION characters
4983 from the string. */
4984 if (precision > 0 && it->end_charpos - charpos > precision)
4985 it->end_charpos = it->string_nchars = charpos + precision;
4987 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4988 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4989 FIELD_WIDTH < 0 means infinite field width. This is useful for
4990 padding with `-' at the end of a mode line. */
4991 if (field_width < 0)
4992 field_width = INFINITY;
4993 if (field_width > it->end_charpos - charpos)
4994 it->end_charpos = charpos + field_width;
4996 /* Use the standard display table for displaying strings. */
4997 if (DISP_TABLE_P (Vstandard_display_table))
4998 it->dp = XCHAR_TABLE (Vstandard_display_table);
5000 it->stop_charpos = charpos;
5001 CHECK_IT (it);
5006 /***********************************************************************
5007 Iteration
5008 ***********************************************************************/
5010 /* Map enum it_method value to corresponding next_element_from_* function. */
5012 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5014 next_element_from_buffer,
5015 next_element_from_display_vector,
5016 next_element_from_composition,
5017 next_element_from_string,
5018 next_element_from_c_string,
5019 next_element_from_image,
5020 next_element_from_stretch
5024 /* Load IT's display element fields with information about the next
5025 display element from the current position of IT. Value is zero if
5026 end of buffer (or C string) is reached. */
5029 get_next_display_element (it)
5030 struct it *it;
5032 /* Non-zero means that we found a display element. Zero means that
5033 we hit the end of what we iterate over. Performance note: the
5034 function pointer `method' used here turns out to be faster than
5035 using a sequence of if-statements. */
5036 int success_p;
5038 get_next:
5039 success_p = (*get_next_element[it->method]) (it);
5041 if (it->what == IT_CHARACTER)
5043 /* Map via display table or translate control characters.
5044 IT->c, IT->len etc. have been set to the next character by
5045 the function call above. If we have a display table, and it
5046 contains an entry for IT->c, translate it. Don't do this if
5047 IT->c itself comes from a display table, otherwise we could
5048 end up in an infinite recursion. (An alternative could be to
5049 count the recursion depth of this function and signal an
5050 error when a certain maximum depth is reached.) Is it worth
5051 it? */
5052 if (success_p && it->dpvec == NULL)
5054 Lisp_Object dv;
5056 if (it->dp
5057 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5058 VECTORP (dv)))
5060 struct Lisp_Vector *v = XVECTOR (dv);
5062 /* Return the first character from the display table
5063 entry, if not empty. If empty, don't display the
5064 current character. */
5065 if (v->size)
5067 it->dpvec_char_len = it->len;
5068 it->dpvec = v->contents;
5069 it->dpend = v->contents + v->size;
5070 it->current.dpvec_index = 0;
5071 it->dpvec_face_id = -1;
5072 it->saved_face_id = it->face_id;
5073 it->method = GET_FROM_DISPLAY_VECTOR;
5074 it->ellipsis_p = 0;
5076 else
5078 set_iterator_to_next (it, 0);
5080 goto get_next;
5083 /* Translate control characters into `\003' or `^C' form.
5084 Control characters coming from a display table entry are
5085 currently not translated because we use IT->dpvec to hold
5086 the translation. This could easily be changed but I
5087 don't believe that it is worth doing.
5089 If it->multibyte_p is nonzero, eight-bit characters and
5090 non-printable multibyte characters are also translated to
5091 octal form.
5093 If it->multibyte_p is zero, eight-bit characters that
5094 don't have corresponding multibyte char code are also
5095 translated to octal form. */
5096 else if ((it->c < ' '
5097 && (it->area != TEXT_AREA
5098 /* In mode line, treat \n like other crl chars. */
5099 || (it->c != '\t'
5100 && it->glyph_row && it->glyph_row->mode_line_p)
5101 || (it->c != '\n' && it->c != '\t')))
5102 || (it->multibyte_p
5103 ? ((it->c >= 127
5104 && it->len == 1)
5105 || !CHAR_PRINTABLE_P (it->c)
5106 || (!NILP (Vnobreak_char_display)
5107 && (it->c == 0x8a0 || it->c == 0x8ad
5108 || it->c == 0x920 || it->c == 0x92d
5109 || it->c == 0xe20 || it->c == 0xe2d
5110 || it->c == 0xf20 || it->c == 0xf2d)))
5111 : (it->c >= 127
5112 && (!unibyte_display_via_language_environment
5113 || it->c == unibyte_char_to_multibyte (it->c)))))
5115 /* IT->c is a control character which must be displayed
5116 either as '\003' or as `^C' where the '\\' and '^'
5117 can be defined in the display table. Fill
5118 IT->ctl_chars with glyphs for what we have to
5119 display. Then, set IT->dpvec to these glyphs. */
5120 GLYPH g;
5121 int ctl_len;
5122 int face_id, lface_id = 0 ;
5123 GLYPH escape_glyph;
5125 /* Handle control characters with ^. */
5127 if (it->c < 128 && it->ctl_arrow_p)
5129 g = '^'; /* default glyph for Control */
5130 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5131 if (it->dp
5132 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5133 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5135 g = XINT (DISP_CTRL_GLYPH (it->dp));
5136 lface_id = FAST_GLYPH_FACE (g);
5138 if (lface_id)
5140 g = FAST_GLYPH_CHAR (g);
5141 face_id = merge_faces (it->f, Qt, lface_id,
5142 it->face_id);
5144 else
5146 /* Merge the escape-glyph face into the current face. */
5147 face_id = merge_faces (it->f, Qescape_glyph, 0,
5148 it->face_id);
5151 XSETINT (it->ctl_chars[0], g);
5152 g = it->c ^ 0100;
5153 XSETINT (it->ctl_chars[1], g);
5154 ctl_len = 2;
5155 goto display_control;
5158 /* Handle non-break space in the mode where it only gets
5159 highlighting. */
5161 if (EQ (Vnobreak_char_display, Qt)
5162 && (it->c == 0x8a0 || it->c == 0x920
5163 || it->c == 0xe20 || it->c == 0xf20))
5165 /* Merge the no-break-space face into the current face. */
5166 face_id = merge_faces (it->f, Qnobreak_space, 0,
5167 it->face_id);
5169 g = it->c = ' ';
5170 XSETINT (it->ctl_chars[0], g);
5171 ctl_len = 1;
5172 goto display_control;
5175 /* Handle sequences that start with the "escape glyph". */
5177 /* the default escape glyph is \. */
5178 escape_glyph = '\\';
5180 if (it->dp
5181 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5182 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5184 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5185 lface_id = FAST_GLYPH_FACE (escape_glyph);
5187 if (lface_id)
5189 /* The display table specified a face.
5190 Merge it into face_id and also into escape_glyph. */
5191 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5192 face_id = merge_faces (it->f, Qt, lface_id,
5193 it->face_id);
5195 else
5197 /* Merge the escape-glyph face into the current face. */
5198 face_id = merge_faces (it->f, Qescape_glyph, 0,
5199 it->face_id);
5202 /* Handle soft hyphens in the mode where they only get
5203 highlighting. */
5205 if (EQ (Vnobreak_char_display, Qt)
5206 && (it->c == 0x8ad || it->c == 0x92d
5207 || it->c == 0xe2d || it->c == 0xf2d))
5209 g = it->c = '-';
5210 XSETINT (it->ctl_chars[0], g);
5211 ctl_len = 1;
5212 goto display_control;
5215 /* Handle non-break space and soft hyphen
5216 with the escape glyph. */
5218 if (it->c == 0x8a0 || it->c == 0x8ad
5219 || it->c == 0x920 || it->c == 0x92d
5220 || it->c == 0xe20 || it->c == 0xe2d
5221 || it->c == 0xf20 || it->c == 0xf2d)
5223 XSETINT (it->ctl_chars[0], escape_glyph);
5224 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5225 XSETINT (it->ctl_chars[1], g);
5226 ctl_len = 2;
5227 goto display_control;
5231 unsigned char str[MAX_MULTIBYTE_LENGTH];
5232 int len;
5233 int i;
5235 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5236 if (SINGLE_BYTE_CHAR_P (it->c))
5237 str[0] = it->c, len = 1;
5238 else
5240 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5241 if (len < 0)
5243 /* It's an invalid character, which shouldn't
5244 happen actually, but due to bugs it may
5245 happen. Let's print the char as is, there's
5246 not much meaningful we can do with it. */
5247 str[0] = it->c;
5248 str[1] = it->c >> 8;
5249 str[2] = it->c >> 16;
5250 str[3] = it->c >> 24;
5251 len = 4;
5255 for (i = 0; i < len; i++)
5257 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5258 /* Insert three more glyphs into IT->ctl_chars for
5259 the octal display of the character. */
5260 g = ((str[i] >> 6) & 7) + '0';
5261 XSETINT (it->ctl_chars[i * 4 + 1], g);
5262 g = ((str[i] >> 3) & 7) + '0';
5263 XSETINT (it->ctl_chars[i * 4 + 2], g);
5264 g = (str[i] & 7) + '0';
5265 XSETINT (it->ctl_chars[i * 4 + 3], g);
5267 ctl_len = len * 4;
5270 display_control:
5271 /* Set up IT->dpvec and return first character from it. */
5272 it->dpvec_char_len = it->len;
5273 it->dpvec = it->ctl_chars;
5274 it->dpend = it->dpvec + ctl_len;
5275 it->current.dpvec_index = 0;
5276 it->dpvec_face_id = face_id;
5277 it->saved_face_id = it->face_id;
5278 it->method = GET_FROM_DISPLAY_VECTOR;
5279 it->ellipsis_p = 0;
5280 goto get_next;
5284 /* Adjust face id for a multibyte character. There are no
5285 multibyte character in unibyte text. */
5286 if (it->multibyte_p
5287 && success_p
5288 && FRAME_WINDOW_P (it->f))
5290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5291 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5295 /* Is this character the last one of a run of characters with
5296 box? If yes, set IT->end_of_box_run_p to 1. */
5297 if (it->face_box_p
5298 && it->s == NULL)
5300 int face_id;
5301 struct face *face;
5303 it->end_of_box_run_p
5304 = ((face_id = face_after_it_pos (it),
5305 face_id != it->face_id)
5306 && (face = FACE_FROM_ID (it->f, face_id),
5307 face->box == FACE_NO_BOX));
5310 /* Value is 0 if end of buffer or string reached. */
5311 return success_p;
5315 /* Move IT to the next display element.
5317 RESEAT_P non-zero means if called on a newline in buffer text,
5318 skip to the next visible line start.
5320 Functions get_next_display_element and set_iterator_to_next are
5321 separate because I find this arrangement easier to handle than a
5322 get_next_display_element function that also increments IT's
5323 position. The way it is we can first look at an iterator's current
5324 display element, decide whether it fits on a line, and if it does,
5325 increment the iterator position. The other way around we probably
5326 would either need a flag indicating whether the iterator has to be
5327 incremented the next time, or we would have to implement a
5328 decrement position function which would not be easy to write. */
5330 void
5331 set_iterator_to_next (it, reseat_p)
5332 struct it *it;
5333 int reseat_p;
5335 /* Reset flags indicating start and end of a sequence of characters
5336 with box. Reset them at the start of this function because
5337 moving the iterator to a new position might set them. */
5338 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5340 switch (it->method)
5342 case GET_FROM_BUFFER:
5343 /* The current display element of IT is a character from
5344 current_buffer. Advance in the buffer, and maybe skip over
5345 invisible lines that are so because of selective display. */
5346 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5347 reseat_at_next_visible_line_start (it, 0);
5348 else
5350 xassert (it->len != 0);
5351 IT_BYTEPOS (*it) += it->len;
5352 IT_CHARPOS (*it) += 1;
5353 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5355 break;
5357 case GET_FROM_COMPOSITION:
5358 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5359 if (STRINGP (it->string))
5361 IT_STRING_BYTEPOS (*it) += it->len;
5362 IT_STRING_CHARPOS (*it) += it->cmp_len;
5363 it->method = GET_FROM_STRING;
5364 goto consider_string_end;
5366 else
5368 IT_BYTEPOS (*it) += it->len;
5369 IT_CHARPOS (*it) += it->cmp_len;
5370 it->method = GET_FROM_BUFFER;
5372 break;
5374 case GET_FROM_C_STRING:
5375 /* Current display element of IT is from a C string. */
5376 IT_BYTEPOS (*it) += it->len;
5377 IT_CHARPOS (*it) += 1;
5378 break;
5380 case GET_FROM_DISPLAY_VECTOR:
5381 /* Current display element of IT is from a display table entry.
5382 Advance in the display table definition. Reset it to null if
5383 end reached, and continue with characters from buffers/
5384 strings. */
5385 ++it->current.dpvec_index;
5387 /* Restore face of the iterator to what they were before the
5388 display vector entry (these entries may contain faces). */
5389 it->face_id = it->saved_face_id;
5391 if (it->dpvec + it->current.dpvec_index == it->dpend)
5393 if (it->s)
5394 it->method = GET_FROM_C_STRING;
5395 else if (STRINGP (it->string))
5396 it->method = GET_FROM_STRING;
5397 else
5398 it->method = GET_FROM_BUFFER;
5400 it->dpvec = NULL;
5401 it->current.dpvec_index = -1;
5403 /* Skip over characters which were displayed via IT->dpvec. */
5404 if (it->dpvec_char_len < 0)
5405 reseat_at_next_visible_line_start (it, 1);
5406 else if (it->dpvec_char_len > 0)
5408 it->len = it->dpvec_char_len;
5409 set_iterator_to_next (it, reseat_p);
5412 /* Recheck faces after display vector */
5413 it->stop_charpos = IT_CHARPOS (*it);
5415 break;
5417 case GET_FROM_STRING:
5418 /* Current display element is a character from a Lisp string. */
5419 xassert (it->s == NULL && STRINGP (it->string));
5420 IT_STRING_BYTEPOS (*it) += it->len;
5421 IT_STRING_CHARPOS (*it) += 1;
5423 consider_string_end:
5425 if (it->current.overlay_string_index >= 0)
5427 /* IT->string is an overlay string. Advance to the
5428 next, if there is one. */
5429 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5430 next_overlay_string (it);
5432 else
5434 /* IT->string is not an overlay string. If we reached
5435 its end, and there is something on IT->stack, proceed
5436 with what is on the stack. This can be either another
5437 string, this time an overlay string, or a buffer. */
5438 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5439 && it->sp > 0)
5441 pop_it (it);
5442 if (STRINGP (it->string))
5443 goto consider_string_end;
5444 it->method = GET_FROM_BUFFER;
5447 break;
5449 case GET_FROM_IMAGE:
5450 case GET_FROM_STRETCH:
5451 /* The position etc with which we have to proceed are on
5452 the stack. The position may be at the end of a string,
5453 if the `display' property takes up the whole string. */
5454 xassert (it->sp > 0);
5455 pop_it (it);
5456 it->image_id = 0;
5457 if (STRINGP (it->string))
5459 it->method = GET_FROM_STRING;
5460 goto consider_string_end;
5462 it->method = GET_FROM_BUFFER;
5463 break;
5465 default:
5466 /* There are no other methods defined, so this should be a bug. */
5467 abort ();
5470 xassert (it->method != GET_FROM_STRING
5471 || (STRINGP (it->string)
5472 && IT_STRING_CHARPOS (*it) >= 0));
5475 /* Load IT's display element fields with information about the next
5476 display element which comes from a display table entry or from the
5477 result of translating a control character to one of the forms `^C'
5478 or `\003'.
5480 IT->dpvec holds the glyphs to return as characters.
5481 IT->saved_face_id holds the face id before the display vector--
5482 it is restored into IT->face_idin set_iterator_to_next. */
5484 static int
5485 next_element_from_display_vector (it)
5486 struct it *it;
5488 /* Precondition. */
5489 xassert (it->dpvec && it->current.dpvec_index >= 0);
5491 it->face_id = it->saved_face_id;
5493 if (INTEGERP (*it->dpvec)
5494 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5496 GLYPH g;
5498 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5499 it->c = FAST_GLYPH_CHAR (g);
5500 it->len = CHAR_BYTES (it->c);
5502 /* The entry may contain a face id to use. Such a face id is
5503 the id of a Lisp face, not a realized face. A face id of
5504 zero means no face is specified. */
5505 if (it->dpvec_face_id >= 0)
5506 it->face_id = it->dpvec_face_id;
5507 else
5509 int lface_id = FAST_GLYPH_FACE (g);
5510 if (lface_id > 0)
5511 it->face_id = merge_faces (it->f, Qt, lface_id,
5512 it->saved_face_id);
5515 else
5516 /* Display table entry is invalid. Return a space. */
5517 it->c = ' ', it->len = 1;
5519 /* Don't change position and object of the iterator here. They are
5520 still the values of the character that had this display table
5521 entry or was translated, and that's what we want. */
5522 it->what = IT_CHARACTER;
5523 return 1;
5527 /* Load IT with the next display element from Lisp string IT->string.
5528 IT->current.string_pos is the current position within the string.
5529 If IT->current.overlay_string_index >= 0, the Lisp string is an
5530 overlay string. */
5532 static int
5533 next_element_from_string (it)
5534 struct it *it;
5536 struct text_pos position;
5538 xassert (STRINGP (it->string));
5539 xassert (IT_STRING_CHARPOS (*it) >= 0);
5540 position = it->current.string_pos;
5542 /* Time to check for invisible text? */
5543 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5544 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5546 handle_stop (it);
5548 /* Since a handler may have changed IT->method, we must
5549 recurse here. */
5550 return get_next_display_element (it);
5553 if (it->current.overlay_string_index >= 0)
5555 /* Get the next character from an overlay string. In overlay
5556 strings, There is no field width or padding with spaces to
5557 do. */
5558 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5560 it->what = IT_EOB;
5561 return 0;
5563 else if (STRING_MULTIBYTE (it->string))
5565 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5566 const unsigned char *s = (SDATA (it->string)
5567 + IT_STRING_BYTEPOS (*it));
5568 it->c = string_char_and_length (s, remaining, &it->len);
5570 else
5572 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5573 it->len = 1;
5576 else
5578 /* Get the next character from a Lisp string that is not an
5579 overlay string. Such strings come from the mode line, for
5580 example. We may have to pad with spaces, or truncate the
5581 string. See also next_element_from_c_string. */
5582 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5584 it->what = IT_EOB;
5585 return 0;
5587 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5589 /* Pad with spaces. */
5590 it->c = ' ', it->len = 1;
5591 CHARPOS (position) = BYTEPOS (position) = -1;
5593 else if (STRING_MULTIBYTE (it->string))
5595 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5596 const unsigned char *s = (SDATA (it->string)
5597 + IT_STRING_BYTEPOS (*it));
5598 it->c = string_char_and_length (s, maxlen, &it->len);
5600 else
5602 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5603 it->len = 1;
5607 /* Record what we have and where it came from. Note that we store a
5608 buffer position in IT->position although it could arguably be a
5609 string position. */
5610 it->what = IT_CHARACTER;
5611 it->object = it->string;
5612 it->position = position;
5613 return 1;
5617 /* Load IT with next display element from C string IT->s.
5618 IT->string_nchars is the maximum number of characters to return
5619 from the string. IT->end_charpos may be greater than
5620 IT->string_nchars when this function is called, in which case we
5621 may have to return padding spaces. Value is zero if end of string
5622 reached, including padding spaces. */
5624 static int
5625 next_element_from_c_string (it)
5626 struct it *it;
5628 int success_p = 1;
5630 xassert (it->s);
5631 it->what = IT_CHARACTER;
5632 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5633 it->object = Qnil;
5635 /* IT's position can be greater IT->string_nchars in case a field
5636 width or precision has been specified when the iterator was
5637 initialized. */
5638 if (IT_CHARPOS (*it) >= it->end_charpos)
5640 /* End of the game. */
5641 it->what = IT_EOB;
5642 success_p = 0;
5644 else if (IT_CHARPOS (*it) >= it->string_nchars)
5646 /* Pad with spaces. */
5647 it->c = ' ', it->len = 1;
5648 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5650 else if (it->multibyte_p)
5652 /* Implementation note: The calls to strlen apparently aren't a
5653 performance problem because there is no noticeable performance
5654 difference between Emacs running in unibyte or multibyte mode. */
5655 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5656 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5657 maxlen, &it->len);
5659 else
5660 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5662 return success_p;
5666 /* Set up IT to return characters from an ellipsis, if appropriate.
5667 The definition of the ellipsis glyphs may come from a display table
5668 entry. This function Fills IT with the first glyph from the
5669 ellipsis if an ellipsis is to be displayed. */
5671 static int
5672 next_element_from_ellipsis (it)
5673 struct it *it;
5675 if (it->selective_display_ellipsis_p)
5676 setup_for_ellipsis (it, it->len);
5677 else
5679 /* The face at the current position may be different from the
5680 face we find after the invisible text. Remember what it
5681 was in IT->saved_face_id, and signal that it's there by
5682 setting face_before_selective_p. */
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_BUFFER;
5685 reseat_at_next_visible_line_start (it, 1);
5686 it->face_before_selective_p = 1;
5689 return get_next_display_element (it);
5693 /* Deliver an image display element. The iterator IT is already
5694 filled with image information (done in handle_display_prop). Value
5695 is always 1. */
5698 static int
5699 next_element_from_image (it)
5700 struct it *it;
5702 it->what = IT_IMAGE;
5703 return 1;
5707 /* Fill iterator IT with next display element from a stretch glyph
5708 property. IT->object is the value of the text property. Value is
5709 always 1. */
5711 static int
5712 next_element_from_stretch (it)
5713 struct it *it;
5715 it->what = IT_STRETCH;
5716 return 1;
5720 /* Load IT with the next display element from current_buffer. Value
5721 is zero if end of buffer reached. IT->stop_charpos is the next
5722 position at which to stop and check for text properties or buffer
5723 end. */
5725 static int
5726 next_element_from_buffer (it)
5727 struct it *it;
5729 int success_p = 1;
5731 /* Check this assumption, otherwise, we would never enter the
5732 if-statement, below. */
5733 xassert (IT_CHARPOS (*it) >= BEGV
5734 && IT_CHARPOS (*it) <= it->stop_charpos);
5736 if (IT_CHARPOS (*it) >= it->stop_charpos)
5738 if (IT_CHARPOS (*it) >= it->end_charpos)
5740 int overlay_strings_follow_p;
5742 /* End of the game, except when overlay strings follow that
5743 haven't been returned yet. */
5744 if (it->overlay_strings_at_end_processed_p)
5745 overlay_strings_follow_p = 0;
5746 else
5748 it->overlay_strings_at_end_processed_p = 1;
5749 overlay_strings_follow_p = get_overlay_strings (it, 0);
5752 if (overlay_strings_follow_p)
5753 success_p = get_next_display_element (it);
5754 else
5756 it->what = IT_EOB;
5757 it->position = it->current.pos;
5758 success_p = 0;
5761 else
5763 handle_stop (it);
5764 return get_next_display_element (it);
5767 else
5769 /* No face changes, overlays etc. in sight, so just return a
5770 character from current_buffer. */
5771 unsigned char *p;
5773 /* Maybe run the redisplay end trigger hook. Performance note:
5774 This doesn't seem to cost measurable time. */
5775 if (it->redisplay_end_trigger_charpos
5776 && it->glyph_row
5777 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5778 run_redisplay_end_trigger_hook (it);
5780 /* Get the next character, maybe multibyte. */
5781 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5782 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5784 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5785 - IT_BYTEPOS (*it));
5786 it->c = string_char_and_length (p, maxlen, &it->len);
5788 else
5789 it->c = *p, it->len = 1;
5791 /* Record what we have and where it came from. */
5792 it->what = IT_CHARACTER;;
5793 it->object = it->w->buffer;
5794 it->position = it->current.pos;
5796 /* Normally we return the character found above, except when we
5797 really want to return an ellipsis for selective display. */
5798 if (it->selective)
5800 if (it->c == '\n')
5802 /* A value of selective > 0 means hide lines indented more
5803 than that number of columns. */
5804 if (it->selective > 0
5805 && IT_CHARPOS (*it) + 1 < ZV
5806 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5807 IT_BYTEPOS (*it) + 1,
5808 (double) it->selective)) /* iftc */
5810 success_p = next_element_from_ellipsis (it);
5811 it->dpvec_char_len = -1;
5814 else if (it->c == '\r' && it->selective == -1)
5816 /* A value of selective == -1 means that everything from the
5817 CR to the end of the line is invisible, with maybe an
5818 ellipsis displayed for it. */
5819 success_p = next_element_from_ellipsis (it);
5820 it->dpvec_char_len = -1;
5825 /* Value is zero if end of buffer reached. */
5826 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5827 return success_p;
5831 /* Run the redisplay end trigger hook for IT. */
5833 static void
5834 run_redisplay_end_trigger_hook (it)
5835 struct it *it;
5837 Lisp_Object args[3];
5839 /* IT->glyph_row should be non-null, i.e. we should be actually
5840 displaying something, or otherwise we should not run the hook. */
5841 xassert (it->glyph_row);
5843 /* Set up hook arguments. */
5844 args[0] = Qredisplay_end_trigger_functions;
5845 args[1] = it->window;
5846 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5847 it->redisplay_end_trigger_charpos = 0;
5849 /* Since we are *trying* to run these functions, don't try to run
5850 them again, even if they get an error. */
5851 it->w->redisplay_end_trigger = Qnil;
5852 Frun_hook_with_args (3, args);
5854 /* Notice if it changed the face of the character we are on. */
5855 handle_face_prop (it);
5859 /* Deliver a composition display element. The iterator IT is already
5860 filled with composition information (done in
5861 handle_composition_prop). Value is always 1. */
5863 static int
5864 next_element_from_composition (it)
5865 struct it *it;
5867 it->what = IT_COMPOSITION;
5868 it->position = (STRINGP (it->string)
5869 ? it->current.string_pos
5870 : it->current.pos);
5871 return 1;
5876 /***********************************************************************
5877 Moving an iterator without producing glyphs
5878 ***********************************************************************/
5880 /* Check if iterator is at a position corresponding to a valid buffer
5881 position after some move_it_ call. */
5883 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5884 ((it)->method == GET_FROM_STRING \
5885 ? IT_STRING_CHARPOS (*it) == 0 \
5886 : 1)
5889 /* Move iterator IT to a specified buffer or X position within one
5890 line on the display without producing glyphs.
5892 OP should be a bit mask including some or all of these bits:
5893 MOVE_TO_X: Stop on reaching x-position TO_X.
5894 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5895 Regardless of OP's value, stop in reaching the end of the display line.
5897 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5898 This means, in particular, that TO_X includes window's horizontal
5899 scroll amount.
5901 The return value has several possible values that
5902 say what condition caused the scan to stop:
5904 MOVE_POS_MATCH_OR_ZV
5905 - when TO_POS or ZV was reached.
5907 MOVE_X_REACHED
5908 -when TO_X was reached before TO_POS or ZV were reached.
5910 MOVE_LINE_CONTINUED
5911 - when we reached the end of the display area and the line must
5912 be continued.
5914 MOVE_LINE_TRUNCATED
5915 - when we reached the end of the display area and the line is
5916 truncated.
5918 MOVE_NEWLINE_OR_CR
5919 - when we stopped at a line end, i.e. a newline or a CR and selective
5920 display is on. */
5922 static enum move_it_result
5923 move_it_in_display_line_to (it, to_charpos, to_x, op)
5924 struct it *it;
5925 int to_charpos, to_x, op;
5927 enum move_it_result result = MOVE_UNDEFINED;
5928 struct glyph_row *saved_glyph_row;
5930 /* Don't produce glyphs in produce_glyphs. */
5931 saved_glyph_row = it->glyph_row;
5932 it->glyph_row = NULL;
5934 #define BUFFER_POS_REACHED_P() \
5935 ((op & MOVE_TO_POS) != 0 \
5936 && BUFFERP (it->object) \
5937 && IT_CHARPOS (*it) >= to_charpos \
5938 && (it->method == GET_FROM_BUFFER \
5939 || (it->method == GET_FROM_DISPLAY_VECTOR \
5940 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5943 while (1)
5945 int x, i, ascent = 0, descent = 0;
5947 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5948 if ((op & MOVE_TO_POS) != 0
5949 && BUFFERP (it->object)
5950 && it->method == GET_FROM_BUFFER
5951 && IT_CHARPOS (*it) > to_charpos)
5953 result = MOVE_POS_MATCH_OR_ZV;
5954 break;
5957 /* Stop when ZV reached.
5958 We used to stop here when TO_CHARPOS reached as well, but that is
5959 too soon if this glyph does not fit on this line. So we handle it
5960 explicitly below. */
5961 if (!get_next_display_element (it)
5962 || (it->truncate_lines_p
5963 && BUFFER_POS_REACHED_P ()))
5965 result = MOVE_POS_MATCH_OR_ZV;
5966 break;
5969 /* The call to produce_glyphs will get the metrics of the
5970 display element IT is loaded with. We record in x the
5971 x-position before this display element in case it does not
5972 fit on the line. */
5973 x = it->current_x;
5975 /* Remember the line height so far in case the next element doesn't
5976 fit on the line. */
5977 if (!it->truncate_lines_p)
5979 ascent = it->max_ascent;
5980 descent = it->max_descent;
5983 PRODUCE_GLYPHS (it);
5985 if (it->area != TEXT_AREA)
5987 set_iterator_to_next (it, 1);
5988 continue;
5991 /* The number of glyphs we get back in IT->nglyphs will normally
5992 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5993 character on a terminal frame, or (iii) a line end. For the
5994 second case, IT->nglyphs - 1 padding glyphs will be present
5995 (on X frames, there is only one glyph produced for a
5996 composite character.
5998 The behavior implemented below means, for continuation lines,
5999 that as many spaces of a TAB as fit on the current line are
6000 displayed there. For terminal frames, as many glyphs of a
6001 multi-glyph character are displayed in the current line, too.
6002 This is what the old redisplay code did, and we keep it that
6003 way. Under X, the whole shape of a complex character must
6004 fit on the line or it will be completely displayed in the
6005 next line.
6007 Note that both for tabs and padding glyphs, all glyphs have
6008 the same width. */
6009 if (it->nglyphs)
6011 /* More than one glyph or glyph doesn't fit on line. All
6012 glyphs have the same width. */
6013 int single_glyph_width = it->pixel_width / it->nglyphs;
6014 int new_x;
6016 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6018 new_x = x + single_glyph_width;
6020 /* We want to leave anything reaching TO_X to the caller. */
6021 if ((op & MOVE_TO_X) && new_x > to_x)
6023 if (BUFFER_POS_REACHED_P ())
6024 goto buffer_pos_reached;
6025 it->current_x = x;
6026 result = MOVE_X_REACHED;
6027 break;
6029 else if (/* Lines are continued. */
6030 !it->truncate_lines_p
6031 && (/* And glyph doesn't fit on the line. */
6032 new_x > it->last_visible_x
6033 /* Or it fits exactly and we're on a window
6034 system frame. */
6035 || (new_x == it->last_visible_x
6036 && FRAME_WINDOW_P (it->f))))
6038 if (/* IT->hpos == 0 means the very first glyph
6039 doesn't fit on the line, e.g. a wide image. */
6040 it->hpos == 0
6041 || (new_x == it->last_visible_x
6042 && FRAME_WINDOW_P (it->f)))
6044 ++it->hpos;
6045 it->current_x = new_x;
6046 if (i == it->nglyphs - 1)
6048 set_iterator_to_next (it, 1);
6049 #ifdef HAVE_WINDOW_SYSTEM
6050 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6052 if (!get_next_display_element (it))
6054 result = MOVE_POS_MATCH_OR_ZV;
6055 break;
6057 if (BUFFER_POS_REACHED_P ())
6059 if (ITERATOR_AT_END_OF_LINE_P (it))
6060 result = MOVE_POS_MATCH_OR_ZV;
6061 else
6062 result = MOVE_LINE_CONTINUED;
6063 break;
6065 if (ITERATOR_AT_END_OF_LINE_P (it))
6067 result = MOVE_NEWLINE_OR_CR;
6068 break;
6071 #endif /* HAVE_WINDOW_SYSTEM */
6074 else
6076 it->current_x = x;
6077 it->max_ascent = ascent;
6078 it->max_descent = descent;
6081 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6082 IT_CHARPOS (*it)));
6083 result = MOVE_LINE_CONTINUED;
6084 break;
6086 else if (BUFFER_POS_REACHED_P ())
6087 goto buffer_pos_reached;
6088 else if (new_x > it->first_visible_x)
6090 /* Glyph is visible. Increment number of glyphs that
6091 would be displayed. */
6092 ++it->hpos;
6094 else
6096 /* Glyph is completely off the left margin of the display
6097 area. Nothing to do. */
6101 if (result != MOVE_UNDEFINED)
6102 break;
6104 else if (BUFFER_POS_REACHED_P ())
6106 buffer_pos_reached:
6107 it->current_x = x;
6108 it->max_ascent = ascent;
6109 it->max_descent = descent;
6110 result = MOVE_POS_MATCH_OR_ZV;
6111 break;
6113 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6115 /* Stop when TO_X specified and reached. This check is
6116 necessary here because of lines consisting of a line end,
6117 only. The line end will not produce any glyphs and we
6118 would never get MOVE_X_REACHED. */
6119 xassert (it->nglyphs == 0);
6120 result = MOVE_X_REACHED;
6121 break;
6124 /* Is this a line end? If yes, we're done. */
6125 if (ITERATOR_AT_END_OF_LINE_P (it))
6127 result = MOVE_NEWLINE_OR_CR;
6128 break;
6131 /* The current display element has been consumed. Advance
6132 to the next. */
6133 set_iterator_to_next (it, 1);
6135 /* Stop if lines are truncated and IT's current x-position is
6136 past the right edge of the window now. */
6137 if (it->truncate_lines_p
6138 && it->current_x >= it->last_visible_x)
6140 #ifdef HAVE_WINDOW_SYSTEM
6141 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6143 if (!get_next_display_element (it)
6144 || BUFFER_POS_REACHED_P ())
6146 result = MOVE_POS_MATCH_OR_ZV;
6147 break;
6149 if (ITERATOR_AT_END_OF_LINE_P (it))
6151 result = MOVE_NEWLINE_OR_CR;
6152 break;
6155 #endif /* HAVE_WINDOW_SYSTEM */
6156 result = MOVE_LINE_TRUNCATED;
6157 break;
6161 #undef BUFFER_POS_REACHED_P
6163 /* Restore the iterator settings altered at the beginning of this
6164 function. */
6165 it->glyph_row = saved_glyph_row;
6166 return result;
6170 /* Move IT forward until it satisfies one or more of the criteria in
6171 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6173 OP is a bit-mask that specifies where to stop, and in particular,
6174 which of those four position arguments makes a difference. See the
6175 description of enum move_operation_enum.
6177 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6178 screen line, this function will set IT to the next position >
6179 TO_CHARPOS. */
6181 void
6182 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6183 struct it *it;
6184 int to_charpos, to_x, to_y, to_vpos;
6185 int op;
6187 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6188 int line_height;
6189 int reached = 0;
6191 for (;;)
6193 if (op & MOVE_TO_VPOS)
6195 /* If no TO_CHARPOS and no TO_X specified, stop at the
6196 start of the line TO_VPOS. */
6197 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6199 if (it->vpos == to_vpos)
6201 reached = 1;
6202 break;
6204 else
6205 skip = move_it_in_display_line_to (it, -1, -1, 0);
6207 else
6209 /* TO_VPOS >= 0 means stop at TO_X in the line at
6210 TO_VPOS, or at TO_POS, whichever comes first. */
6211 if (it->vpos == to_vpos)
6213 reached = 2;
6214 break;
6217 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6219 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6221 reached = 3;
6222 break;
6224 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6226 /* We have reached TO_X but not in the line we want. */
6227 skip = move_it_in_display_line_to (it, to_charpos,
6228 -1, MOVE_TO_POS);
6229 if (skip == MOVE_POS_MATCH_OR_ZV)
6231 reached = 4;
6232 break;
6237 else if (op & MOVE_TO_Y)
6239 struct it it_backup;
6241 /* TO_Y specified means stop at TO_X in the line containing
6242 TO_Y---or at TO_CHARPOS if this is reached first. The
6243 problem is that we can't really tell whether the line
6244 contains TO_Y before we have completely scanned it, and
6245 this may skip past TO_X. What we do is to first scan to
6246 TO_X.
6248 If TO_X is not specified, use a TO_X of zero. The reason
6249 is to make the outcome of this function more predictable.
6250 If we didn't use TO_X == 0, we would stop at the end of
6251 the line which is probably not what a caller would expect
6252 to happen. */
6253 skip = move_it_in_display_line_to (it, to_charpos,
6254 ((op & MOVE_TO_X)
6255 ? to_x : 0),
6256 (MOVE_TO_X
6257 | (op & MOVE_TO_POS)));
6259 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6260 if (skip == MOVE_POS_MATCH_OR_ZV)
6262 reached = 5;
6263 break;
6266 /* If TO_X was reached, we would like to know whether TO_Y
6267 is in the line. This can only be said if we know the
6268 total line height which requires us to scan the rest of
6269 the line. */
6270 if (skip == MOVE_X_REACHED)
6272 it_backup = *it;
6273 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6274 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6275 op & MOVE_TO_POS);
6276 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6279 /* Now, decide whether TO_Y is in this line. */
6280 line_height = it->max_ascent + it->max_descent;
6281 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6283 if (to_y >= it->current_y
6284 && to_y < it->current_y + line_height)
6286 if (skip == MOVE_X_REACHED)
6287 /* If TO_Y is in this line and TO_X was reached above,
6288 we scanned too far. We have to restore IT's settings
6289 to the ones before skipping. */
6290 *it = it_backup;
6291 reached = 6;
6293 else if (skip == MOVE_X_REACHED)
6295 skip = skip2;
6296 if (skip == MOVE_POS_MATCH_OR_ZV)
6297 reached = 7;
6300 if (reached)
6301 break;
6303 else
6304 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6306 switch (skip)
6308 case MOVE_POS_MATCH_OR_ZV:
6309 reached = 8;
6310 goto out;
6312 case MOVE_NEWLINE_OR_CR:
6313 set_iterator_to_next (it, 1);
6314 it->continuation_lines_width = 0;
6315 break;
6317 case MOVE_LINE_TRUNCATED:
6318 it->continuation_lines_width = 0;
6319 reseat_at_next_visible_line_start (it, 0);
6320 if ((op & MOVE_TO_POS) != 0
6321 && IT_CHARPOS (*it) > to_charpos)
6323 reached = 9;
6324 goto out;
6326 break;
6328 case MOVE_LINE_CONTINUED:
6329 it->continuation_lines_width += it->current_x;
6330 break;
6332 default:
6333 abort ();
6336 /* Reset/increment for the next run. */
6337 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6338 it->current_x = it->hpos = 0;
6339 it->current_y += it->max_ascent + it->max_descent;
6340 ++it->vpos;
6341 last_height = it->max_ascent + it->max_descent;
6342 last_max_ascent = it->max_ascent;
6343 it->max_ascent = it->max_descent = 0;
6346 out:
6348 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6352 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6354 If DY > 0, move IT backward at least that many pixels. DY = 0
6355 means move IT backward to the preceding line start or BEGV. This
6356 function may move over more than DY pixels if IT->current_y - DY
6357 ends up in the middle of a line; in this case IT->current_y will be
6358 set to the top of the line moved to. */
6360 void
6361 move_it_vertically_backward (it, dy)
6362 struct it *it;
6363 int dy;
6365 int nlines, h;
6366 struct it it2, it3;
6367 int start_pos;
6369 move_further_back:
6370 xassert (dy >= 0);
6372 start_pos = IT_CHARPOS (*it);
6374 /* Estimate how many newlines we must move back. */
6375 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6377 /* Set the iterator's position that many lines back. */
6378 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6379 back_to_previous_visible_line_start (it);
6381 /* Reseat the iterator here. When moving backward, we don't want
6382 reseat to skip forward over invisible text, set up the iterator
6383 to deliver from overlay strings at the new position etc. So,
6384 use reseat_1 here. */
6385 reseat_1 (it, it->current.pos, 1);
6387 /* We are now surely at a line start. */
6388 it->current_x = it->hpos = 0;
6389 it->continuation_lines_width = 0;
6391 /* Move forward and see what y-distance we moved. First move to the
6392 start of the next line so that we get its height. We need this
6393 height to be able to tell whether we reached the specified
6394 y-distance. */
6395 it2 = *it;
6396 it2.max_ascent = it2.max_descent = 0;
6399 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6400 MOVE_TO_POS | MOVE_TO_VPOS);
6402 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6403 xassert (IT_CHARPOS (*it) >= BEGV);
6404 it3 = it2;
6406 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6407 xassert (IT_CHARPOS (*it) >= BEGV);
6408 /* H is the actual vertical distance from the position in *IT
6409 and the starting position. */
6410 h = it2.current_y - it->current_y;
6411 /* NLINES is the distance in number of lines. */
6412 nlines = it2.vpos - it->vpos;
6414 /* Correct IT's y and vpos position
6415 so that they are relative to the starting point. */
6416 it->vpos -= nlines;
6417 it->current_y -= h;
6419 if (dy == 0)
6421 /* DY == 0 means move to the start of the screen line. The
6422 value of nlines is > 0 if continuation lines were involved. */
6423 if (nlines > 0)
6424 move_it_by_lines (it, nlines, 1);
6425 #if 0
6426 /* I think this assert is bogus if buffer contains
6427 invisible text or images. KFS. */
6428 xassert (IT_CHARPOS (*it) <= start_pos);
6429 #endif
6431 else
6433 /* The y-position we try to reach, relative to *IT.
6434 Note that H has been subtracted in front of the if-statement. */
6435 int target_y = it->current_y + h - dy;
6436 int y0 = it3.current_y;
6437 int y1 = line_bottom_y (&it3);
6438 int line_height = y1 - y0;
6440 /* If we did not reach target_y, try to move further backward if
6441 we can. If we moved too far backward, try to move forward. */
6442 if (target_y < it->current_y
6443 /* This is heuristic. In a window that's 3 lines high, with
6444 a line height of 13 pixels each, recentering with point
6445 on the bottom line will try to move -39/2 = 19 pixels
6446 backward. Try to avoid moving into the first line. */
6447 && (it->current_y - target_y
6448 > min (window_box_height (it->w), line_height * 2 / 3))
6449 && IT_CHARPOS (*it) > BEGV)
6451 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6452 target_y - it->current_y));
6453 dy = it->current_y - target_y;
6454 goto move_further_back;
6456 else if (target_y >= it->current_y + line_height
6457 && IT_CHARPOS (*it) < ZV)
6459 /* Should move forward by at least one line, maybe more.
6461 Note: Calling move_it_by_lines can be expensive on
6462 terminal frames, where compute_motion is used (via
6463 vmotion) to do the job, when there are very long lines
6464 and truncate-lines is nil. That's the reason for
6465 treating terminal frames specially here. */
6467 if (!FRAME_WINDOW_P (it->f))
6468 move_it_vertically (it, target_y - (it->current_y + line_height));
6469 else
6473 move_it_by_lines (it, 1, 1);
6475 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6478 #if 0
6479 /* I think this assert is bogus if buffer contains
6480 invisible text or images. KFS. */
6481 xassert (IT_CHARPOS (*it) >= BEGV);
6482 #endif
6488 /* Move IT by a specified amount of pixel lines DY. DY negative means
6489 move backwards. DY = 0 means move to start of screen line. At the
6490 end, IT will be on the start of a screen line. */
6492 void
6493 move_it_vertically (it, dy)
6494 struct it *it;
6495 int dy;
6497 if (dy <= 0)
6498 move_it_vertically_backward (it, -dy);
6499 else
6501 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6502 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6503 MOVE_TO_POS | MOVE_TO_Y);
6504 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6506 /* If buffer ends in ZV without a newline, move to the start of
6507 the line to satisfy the post-condition. */
6508 if (IT_CHARPOS (*it) == ZV
6509 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6510 move_it_by_lines (it, 0, 0);
6515 /* Move iterator IT past the end of the text line it is in. */
6517 void
6518 move_it_past_eol (it)
6519 struct it *it;
6521 enum move_it_result rc;
6523 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6524 if (rc == MOVE_NEWLINE_OR_CR)
6525 set_iterator_to_next (it, 0);
6529 #if 0 /* Currently not used. */
6531 /* Return non-zero if some text between buffer positions START_CHARPOS
6532 and END_CHARPOS is invisible. IT->window is the window for text
6533 property lookup. */
6535 static int
6536 invisible_text_between_p (it, start_charpos, end_charpos)
6537 struct it *it;
6538 int start_charpos, end_charpos;
6540 Lisp_Object prop, limit;
6541 int invisible_found_p;
6543 xassert (it != NULL && start_charpos <= end_charpos);
6545 /* Is text at START invisible? */
6546 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6547 it->window);
6548 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6549 invisible_found_p = 1;
6550 else
6552 limit = Fnext_single_char_property_change (make_number (start_charpos),
6553 Qinvisible, Qnil,
6554 make_number (end_charpos));
6555 invisible_found_p = XFASTINT (limit) < end_charpos;
6558 return invisible_found_p;
6561 #endif /* 0 */
6564 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6565 negative means move up. DVPOS == 0 means move to the start of the
6566 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6567 NEED_Y_P is zero, IT->current_y will be left unchanged.
6569 Further optimization ideas: If we would know that IT->f doesn't use
6570 a face with proportional font, we could be faster for
6571 truncate-lines nil. */
6573 void
6574 move_it_by_lines (it, dvpos, need_y_p)
6575 struct it *it;
6576 int dvpos, need_y_p;
6578 struct position pos;
6580 if (!FRAME_WINDOW_P (it->f))
6582 struct text_pos textpos;
6584 /* We can use vmotion on frames without proportional fonts. */
6585 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6586 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6587 reseat (it, textpos, 1);
6588 it->vpos += pos.vpos;
6589 it->current_y += pos.vpos;
6591 else if (dvpos == 0)
6593 /* DVPOS == 0 means move to the start of the screen line. */
6594 move_it_vertically_backward (it, 0);
6595 xassert (it->current_x == 0 && it->hpos == 0);
6596 /* Let next call to line_bottom_y calculate real line height */
6597 last_height = 0;
6599 else if (dvpos > 0)
6601 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6602 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6603 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6605 else
6607 struct it it2;
6608 int start_charpos, i;
6610 /* Start at the beginning of the screen line containing IT's
6611 position. This may actually move vertically backwards,
6612 in case of overlays, so adjust dvpos accordingly. */
6613 dvpos += it->vpos;
6614 move_it_vertically_backward (it, 0);
6615 dvpos -= it->vpos;
6617 /* Go back -DVPOS visible lines and reseat the iterator there. */
6618 start_charpos = IT_CHARPOS (*it);
6619 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6620 back_to_previous_visible_line_start (it);
6621 reseat (it, it->current.pos, 1);
6623 /* Move further back if we end up in a string or an image. */
6624 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6626 /* First try to move to start of display line. */
6627 dvpos += it->vpos;
6628 move_it_vertically_backward (it, 0);
6629 dvpos -= it->vpos;
6630 if (IT_POS_VALID_AFTER_MOVE_P (it))
6631 break;
6632 /* If start of line is still in string or image,
6633 move further back. */
6634 back_to_previous_visible_line_start (it);
6635 reseat (it, it->current.pos, 1);
6636 dvpos--;
6639 it->current_x = it->hpos = 0;
6641 /* Above call may have moved too far if continuation lines
6642 are involved. Scan forward and see if it did. */
6643 it2 = *it;
6644 it2.vpos = it2.current_y = 0;
6645 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6646 it->vpos -= it2.vpos;
6647 it->current_y -= it2.current_y;
6648 it->current_x = it->hpos = 0;
6650 /* If we moved too far back, move IT some lines forward. */
6651 if (it2.vpos > -dvpos)
6653 int delta = it2.vpos + dvpos;
6654 it2 = *it;
6655 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6656 /* Move back again if we got too far ahead. */
6657 if (IT_CHARPOS (*it) >= start_charpos)
6658 *it = it2;
6663 /* Return 1 if IT points into the middle of a display vector. */
6666 in_display_vector_p (it)
6667 struct it *it;
6669 return (it->method == GET_FROM_DISPLAY_VECTOR
6670 && it->current.dpvec_index > 0
6671 && it->dpvec + it->current.dpvec_index != it->dpend);
6675 /***********************************************************************
6676 Messages
6677 ***********************************************************************/
6680 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6681 to *Messages*. */
6683 void
6684 add_to_log (format, arg1, arg2)
6685 char *format;
6686 Lisp_Object arg1, arg2;
6688 Lisp_Object args[3];
6689 Lisp_Object msg, fmt;
6690 char *buffer;
6691 int len;
6692 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6693 USE_SAFE_ALLOCA;
6695 /* Do nothing if called asynchronously. Inserting text into
6696 a buffer may call after-change-functions and alike and
6697 that would means running Lisp asynchronously. */
6698 if (handling_signal)
6699 return;
6701 fmt = msg = Qnil;
6702 GCPRO4 (fmt, msg, arg1, arg2);
6704 args[0] = fmt = build_string (format);
6705 args[1] = arg1;
6706 args[2] = arg2;
6707 msg = Fformat (3, args);
6709 len = SBYTES (msg) + 1;
6710 SAFE_ALLOCA (buffer, char *, len);
6711 bcopy (SDATA (msg), buffer, len);
6713 message_dolog (buffer, len - 1, 1, 0);
6714 SAFE_FREE ();
6716 UNGCPRO;
6720 /* Output a newline in the *Messages* buffer if "needs" one. */
6722 void
6723 message_log_maybe_newline ()
6725 if (message_log_need_newline)
6726 message_dolog ("", 0, 1, 0);
6730 /* Add a string M of length NBYTES to the message log, optionally
6731 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6732 nonzero, means interpret the contents of M as multibyte. This
6733 function calls low-level routines in order to bypass text property
6734 hooks, etc. which might not be safe to run. */
6736 void
6737 message_dolog (m, nbytes, nlflag, multibyte)
6738 const char *m;
6739 int nbytes, nlflag, multibyte;
6741 if (!NILP (Vmemory_full))
6742 return;
6744 if (!NILP (Vmessage_log_max))
6746 struct buffer *oldbuf;
6747 Lisp_Object oldpoint, oldbegv, oldzv;
6748 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6749 int point_at_end = 0;
6750 int zv_at_end = 0;
6751 Lisp_Object old_deactivate_mark, tem;
6752 struct gcpro gcpro1;
6754 old_deactivate_mark = Vdeactivate_mark;
6755 oldbuf = current_buffer;
6756 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6757 current_buffer->undo_list = Qt;
6759 oldpoint = message_dolog_marker1;
6760 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6761 oldbegv = message_dolog_marker2;
6762 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6763 oldzv = message_dolog_marker3;
6764 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6765 GCPRO1 (old_deactivate_mark);
6767 if (PT == Z)
6768 point_at_end = 1;
6769 if (ZV == Z)
6770 zv_at_end = 1;
6772 BEGV = BEG;
6773 BEGV_BYTE = BEG_BYTE;
6774 ZV = Z;
6775 ZV_BYTE = Z_BYTE;
6776 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6778 /* Insert the string--maybe converting multibyte to single byte
6779 or vice versa, so that all the text fits the buffer. */
6780 if (multibyte
6781 && NILP (current_buffer->enable_multibyte_characters))
6783 int i, c, char_bytes;
6784 unsigned char work[1];
6786 /* Convert a multibyte string to single-byte
6787 for the *Message* buffer. */
6788 for (i = 0; i < nbytes; i += char_bytes)
6790 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6791 work[0] = (SINGLE_BYTE_CHAR_P (c)
6793 : multibyte_char_to_unibyte (c, Qnil));
6794 insert_1_both (work, 1, 1, 1, 0, 0);
6797 else if (! multibyte
6798 && ! NILP (current_buffer->enable_multibyte_characters))
6800 int i, c, char_bytes;
6801 unsigned char *msg = (unsigned char *) m;
6802 unsigned char str[MAX_MULTIBYTE_LENGTH];
6803 /* Convert a single-byte string to multibyte
6804 for the *Message* buffer. */
6805 for (i = 0; i < nbytes; i++)
6807 c = unibyte_char_to_multibyte (msg[i]);
6808 char_bytes = CHAR_STRING (c, str);
6809 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6812 else if (nbytes)
6813 insert_1 (m, nbytes, 1, 0, 0);
6815 if (nlflag)
6817 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6818 insert_1 ("\n", 1, 1, 0, 0);
6820 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6821 this_bol = PT;
6822 this_bol_byte = PT_BYTE;
6824 /* See if this line duplicates the previous one.
6825 If so, combine duplicates. */
6826 if (this_bol > BEG)
6828 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6829 prev_bol = PT;
6830 prev_bol_byte = PT_BYTE;
6832 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6833 this_bol, this_bol_byte);
6834 if (dup)
6836 del_range_both (prev_bol, prev_bol_byte,
6837 this_bol, this_bol_byte, 0);
6838 if (dup > 1)
6840 char dupstr[40];
6841 int duplen;
6843 /* If you change this format, don't forget to also
6844 change message_log_check_duplicate. */
6845 sprintf (dupstr, " [%d times]", dup);
6846 duplen = strlen (dupstr);
6847 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6848 insert_1 (dupstr, duplen, 1, 0, 1);
6853 /* If we have more than the desired maximum number of lines
6854 in the *Messages* buffer now, delete the oldest ones.
6855 This is safe because we don't have undo in this buffer. */
6857 if (NATNUMP (Vmessage_log_max))
6859 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6860 -XFASTINT (Vmessage_log_max) - 1, 0);
6861 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6864 BEGV = XMARKER (oldbegv)->charpos;
6865 BEGV_BYTE = marker_byte_position (oldbegv);
6867 if (zv_at_end)
6869 ZV = Z;
6870 ZV_BYTE = Z_BYTE;
6872 else
6874 ZV = XMARKER (oldzv)->charpos;
6875 ZV_BYTE = marker_byte_position (oldzv);
6878 if (point_at_end)
6879 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6880 else
6881 /* We can't do Fgoto_char (oldpoint) because it will run some
6882 Lisp code. */
6883 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6884 XMARKER (oldpoint)->bytepos);
6886 UNGCPRO;
6887 unchain_marker (XMARKER (oldpoint));
6888 unchain_marker (XMARKER (oldbegv));
6889 unchain_marker (XMARKER (oldzv));
6891 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6892 set_buffer_internal (oldbuf);
6893 if (NILP (tem))
6894 windows_or_buffers_changed = old_windows_or_buffers_changed;
6895 message_log_need_newline = !nlflag;
6896 Vdeactivate_mark = old_deactivate_mark;
6901 /* We are at the end of the buffer after just having inserted a newline.
6902 (Note: We depend on the fact we won't be crossing the gap.)
6903 Check to see if the most recent message looks a lot like the previous one.
6904 Return 0 if different, 1 if the new one should just replace it, or a
6905 value N > 1 if we should also append " [N times]". */
6907 static int
6908 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6909 int prev_bol, this_bol;
6910 int prev_bol_byte, this_bol_byte;
6912 int i;
6913 int len = Z_BYTE - 1 - this_bol_byte;
6914 int seen_dots = 0;
6915 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6916 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6918 for (i = 0; i < len; i++)
6920 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6921 seen_dots = 1;
6922 if (p1[i] != p2[i])
6923 return seen_dots;
6925 p1 += len;
6926 if (*p1 == '\n')
6927 return 2;
6928 if (*p1++ == ' ' && *p1++ == '[')
6930 int n = 0;
6931 while (*p1 >= '0' && *p1 <= '9')
6932 n = n * 10 + *p1++ - '0';
6933 if (strncmp (p1, " times]\n", 8) == 0)
6934 return n+1;
6936 return 0;
6940 /* Display an echo area message M with a specified length of NBYTES
6941 bytes. The string may include null characters. If M is 0, clear
6942 out any existing message, and let the mini-buffer text show
6943 through.
6945 The buffer M must continue to exist until after the echo area gets
6946 cleared or some other message gets displayed there. This means do
6947 not pass text that is stored in a Lisp string; do not pass text in
6948 a buffer that was alloca'd. */
6950 void
6951 message2 (m, nbytes, multibyte)
6952 const char *m;
6953 int nbytes;
6954 int multibyte;
6956 /* First flush out any partial line written with print. */
6957 message_log_maybe_newline ();
6958 if (m)
6959 message_dolog (m, nbytes, 1, multibyte);
6960 message2_nolog (m, nbytes, multibyte);
6964 /* The non-logging counterpart of message2. */
6966 void
6967 message2_nolog (m, nbytes, multibyte)
6968 const char *m;
6969 int nbytes, multibyte;
6971 struct frame *sf = SELECTED_FRAME ();
6972 message_enable_multibyte = multibyte;
6974 if (noninteractive)
6976 if (noninteractive_need_newline)
6977 putc ('\n', stderr);
6978 noninteractive_need_newline = 0;
6979 if (m)
6980 fwrite (m, nbytes, 1, stderr);
6981 if (cursor_in_echo_area == 0)
6982 fprintf (stderr, "\n");
6983 fflush (stderr);
6985 /* A null message buffer means that the frame hasn't really been
6986 initialized yet. Error messages get reported properly by
6987 cmd_error, so this must be just an informative message; toss it. */
6988 else if (INTERACTIVE
6989 && sf->glyphs_initialized_p
6990 && FRAME_MESSAGE_BUF (sf))
6992 Lisp_Object mini_window;
6993 struct frame *f;
6995 /* Get the frame containing the mini-buffer
6996 that the selected frame is using. */
6997 mini_window = FRAME_MINIBUF_WINDOW (sf);
6998 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7000 FRAME_SAMPLE_VISIBILITY (f);
7001 if (FRAME_VISIBLE_P (sf)
7002 && ! FRAME_VISIBLE_P (f))
7003 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7005 if (m)
7007 set_message (m, Qnil, nbytes, multibyte);
7008 if (minibuffer_auto_raise)
7009 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7011 else
7012 clear_message (1, 1);
7014 do_pending_window_change (0);
7015 echo_area_display (1);
7016 do_pending_window_change (0);
7017 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7018 (*frame_up_to_date_hook) (f);
7023 /* Display an echo area message M with a specified length of NBYTES
7024 bytes. The string may include null characters. If M is not a
7025 string, clear out any existing message, and let the mini-buffer
7026 text show through.
7028 This function cancels echoing. */
7030 void
7031 message3 (m, nbytes, multibyte)
7032 Lisp_Object m;
7033 int nbytes;
7034 int multibyte;
7036 struct gcpro gcpro1;
7038 GCPRO1 (m);
7039 clear_message (1,1);
7040 cancel_echoing ();
7042 /* First flush out any partial line written with print. */
7043 message_log_maybe_newline ();
7044 if (STRINGP (m))
7045 message_dolog (SDATA (m), nbytes, 1, multibyte);
7046 message3_nolog (m, nbytes, multibyte);
7048 UNGCPRO;
7052 /* The non-logging version of message3.
7053 This does not cancel echoing, because it is used for echoing.
7054 Perhaps we need to make a separate function for echoing
7055 and make this cancel echoing. */
7057 void
7058 message3_nolog (m, nbytes, multibyte)
7059 Lisp_Object m;
7060 int nbytes, multibyte;
7062 struct frame *sf = SELECTED_FRAME ();
7063 message_enable_multibyte = multibyte;
7065 if (noninteractive)
7067 if (noninteractive_need_newline)
7068 putc ('\n', stderr);
7069 noninteractive_need_newline = 0;
7070 if (STRINGP (m))
7071 fwrite (SDATA (m), nbytes, 1, stderr);
7072 if (cursor_in_echo_area == 0)
7073 fprintf (stderr, "\n");
7074 fflush (stderr);
7076 /* A null message buffer means that the frame hasn't really been
7077 initialized yet. Error messages get reported properly by
7078 cmd_error, so this must be just an informative message; toss it. */
7079 else if (INTERACTIVE
7080 && sf->glyphs_initialized_p
7081 && FRAME_MESSAGE_BUF (sf))
7083 Lisp_Object mini_window;
7084 Lisp_Object frame;
7085 struct frame *f;
7087 /* Get the frame containing the mini-buffer
7088 that the selected frame is using. */
7089 mini_window = FRAME_MINIBUF_WINDOW (sf);
7090 frame = XWINDOW (mini_window)->frame;
7091 f = XFRAME (frame);
7093 FRAME_SAMPLE_VISIBILITY (f);
7094 if (FRAME_VISIBLE_P (sf)
7095 && !FRAME_VISIBLE_P (f))
7096 Fmake_frame_visible (frame);
7098 if (STRINGP (m) && SCHARS (m) > 0)
7100 set_message (NULL, m, nbytes, multibyte);
7101 if (minibuffer_auto_raise)
7102 Fraise_frame (frame);
7103 /* Assume we are not echoing.
7104 (If we are, echo_now will override this.) */
7105 echo_message_buffer = Qnil;
7107 else
7108 clear_message (1, 1);
7110 do_pending_window_change (0);
7111 echo_area_display (1);
7112 do_pending_window_change (0);
7113 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7114 (*frame_up_to_date_hook) (f);
7119 /* Display a null-terminated echo area message M. If M is 0, clear
7120 out any existing message, and let the mini-buffer text show through.
7122 The buffer M must continue to exist until after the echo area gets
7123 cleared or some other message gets displayed there. Do not pass
7124 text that is stored in a Lisp string. Do not pass text in a buffer
7125 that was alloca'd. */
7127 void
7128 message1 (m)
7129 char *m;
7131 message2 (m, (m ? strlen (m) : 0), 0);
7135 /* The non-logging counterpart of message1. */
7137 void
7138 message1_nolog (m)
7139 char *m;
7141 message2_nolog (m, (m ? strlen (m) : 0), 0);
7144 /* Display a message M which contains a single %s
7145 which gets replaced with STRING. */
7147 void
7148 message_with_string (m, string, log)
7149 char *m;
7150 Lisp_Object string;
7151 int log;
7153 CHECK_STRING (string);
7155 if (noninteractive)
7157 if (m)
7159 if (noninteractive_need_newline)
7160 putc ('\n', stderr);
7161 noninteractive_need_newline = 0;
7162 fprintf (stderr, m, SDATA (string));
7163 if (cursor_in_echo_area == 0)
7164 fprintf (stderr, "\n");
7165 fflush (stderr);
7168 else if (INTERACTIVE)
7170 /* The frame whose minibuffer we're going to display the message on.
7171 It may be larger than the selected frame, so we need
7172 to use its buffer, not the selected frame's buffer. */
7173 Lisp_Object mini_window;
7174 struct frame *f, *sf = SELECTED_FRAME ();
7176 /* Get the frame containing the minibuffer
7177 that the selected frame is using. */
7178 mini_window = FRAME_MINIBUF_WINDOW (sf);
7179 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7181 /* A null message buffer means that the frame hasn't really been
7182 initialized yet. Error messages get reported properly by
7183 cmd_error, so this must be just an informative message; toss it. */
7184 if (FRAME_MESSAGE_BUF (f))
7186 Lisp_Object args[2], message;
7187 struct gcpro gcpro1, gcpro2;
7189 args[0] = build_string (m);
7190 args[1] = message = string;
7191 GCPRO2 (args[0], message);
7192 gcpro1.nvars = 2;
7194 message = Fformat (2, args);
7196 if (log)
7197 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7198 else
7199 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7201 UNGCPRO;
7203 /* Print should start at the beginning of the message
7204 buffer next time. */
7205 message_buf_print = 0;
7211 /* Dump an informative message to the minibuf. If M is 0, clear out
7212 any existing message, and let the mini-buffer text show through. */
7214 /* VARARGS 1 */
7215 void
7216 message (m, a1, a2, a3)
7217 char *m;
7218 EMACS_INT a1, a2, a3;
7220 if (noninteractive)
7222 if (m)
7224 if (noninteractive_need_newline)
7225 putc ('\n', stderr);
7226 noninteractive_need_newline = 0;
7227 fprintf (stderr, m, a1, a2, a3);
7228 if (cursor_in_echo_area == 0)
7229 fprintf (stderr, "\n");
7230 fflush (stderr);
7233 else if (INTERACTIVE)
7235 /* The frame whose mini-buffer we're going to display the message
7236 on. It may be larger than the selected frame, so we need to
7237 use its buffer, not the selected frame's buffer. */
7238 Lisp_Object mini_window;
7239 struct frame *f, *sf = SELECTED_FRAME ();
7241 /* Get the frame containing the mini-buffer
7242 that the selected frame is using. */
7243 mini_window = FRAME_MINIBUF_WINDOW (sf);
7244 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7246 /* A null message buffer means that the frame hasn't really been
7247 initialized yet. Error messages get reported properly by
7248 cmd_error, so this must be just an informative message; toss
7249 it. */
7250 if (FRAME_MESSAGE_BUF (f))
7252 if (m)
7254 int len;
7255 #ifdef NO_ARG_ARRAY
7256 char *a[3];
7257 a[0] = (char *) a1;
7258 a[1] = (char *) a2;
7259 a[2] = (char *) a3;
7261 len = doprnt (FRAME_MESSAGE_BUF (f),
7262 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7263 #else
7264 len = doprnt (FRAME_MESSAGE_BUF (f),
7265 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7266 (char **) &a1);
7267 #endif /* NO_ARG_ARRAY */
7269 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7271 else
7272 message1 (0);
7274 /* Print should start at the beginning of the message
7275 buffer next time. */
7276 message_buf_print = 0;
7282 /* The non-logging version of message. */
7284 void
7285 message_nolog (m, a1, a2, a3)
7286 char *m;
7287 EMACS_INT a1, a2, a3;
7289 Lisp_Object old_log_max;
7290 old_log_max = Vmessage_log_max;
7291 Vmessage_log_max = Qnil;
7292 message (m, a1, a2, a3);
7293 Vmessage_log_max = old_log_max;
7297 /* Display the current message in the current mini-buffer. This is
7298 only called from error handlers in process.c, and is not time
7299 critical. */
7301 void
7302 update_echo_area ()
7304 if (!NILP (echo_area_buffer[0]))
7306 Lisp_Object string;
7307 string = Fcurrent_message ();
7308 message3 (string, SBYTES (string),
7309 !NILP (current_buffer->enable_multibyte_characters));
7314 /* Make sure echo area buffers in `echo_buffers' are live.
7315 If they aren't, make new ones. */
7317 static void
7318 ensure_echo_area_buffers ()
7320 int i;
7322 for (i = 0; i < 2; ++i)
7323 if (!BUFFERP (echo_buffer[i])
7324 || NILP (XBUFFER (echo_buffer[i])->name))
7326 char name[30];
7327 Lisp_Object old_buffer;
7328 int j;
7330 old_buffer = echo_buffer[i];
7331 sprintf (name, " *Echo Area %d*", i);
7332 echo_buffer[i] = Fget_buffer_create (build_string (name));
7333 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7335 for (j = 0; j < 2; ++j)
7336 if (EQ (old_buffer, echo_area_buffer[j]))
7337 echo_area_buffer[j] = echo_buffer[i];
7342 /* Call FN with args A1..A4 with either the current or last displayed
7343 echo_area_buffer as current buffer.
7345 WHICH zero means use the current message buffer
7346 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7347 from echo_buffer[] and clear it.
7349 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7350 suitable buffer from echo_buffer[] and clear it.
7352 Value is what FN returns. */
7354 static int
7355 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7356 struct window *w;
7357 int which;
7358 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7359 EMACS_INT a1;
7360 Lisp_Object a2;
7361 EMACS_INT a3, a4;
7363 Lisp_Object buffer;
7364 int this_one, the_other, clear_buffer_p, rc;
7365 int count = SPECPDL_INDEX ();
7367 /* If buffers aren't live, make new ones. */
7368 ensure_echo_area_buffers ();
7370 clear_buffer_p = 0;
7372 if (which == 0)
7373 this_one = 0, the_other = 1;
7374 else if (which > 0)
7375 this_one = 1, the_other = 0;
7377 /* Choose a suitable buffer from echo_buffer[] is we don't
7378 have one. */
7379 if (NILP (echo_area_buffer[this_one]))
7381 echo_area_buffer[this_one]
7382 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7383 ? echo_buffer[the_other]
7384 : echo_buffer[this_one]);
7385 clear_buffer_p = 1;
7388 buffer = echo_area_buffer[this_one];
7390 /* Don't get confused by reusing the buffer used for echoing
7391 for a different purpose. */
7392 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7393 cancel_echoing ();
7395 record_unwind_protect (unwind_with_echo_area_buffer,
7396 with_echo_area_buffer_unwind_data (w));
7398 /* Make the echo area buffer current. Note that for display
7399 purposes, it is not necessary that the displayed window's buffer
7400 == current_buffer, except for text property lookup. So, let's
7401 only set that buffer temporarily here without doing a full
7402 Fset_window_buffer. We must also change w->pointm, though,
7403 because otherwise an assertions in unshow_buffer fails, and Emacs
7404 aborts. */
7405 set_buffer_internal_1 (XBUFFER (buffer));
7406 if (w)
7408 w->buffer = buffer;
7409 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7412 current_buffer->undo_list = Qt;
7413 current_buffer->read_only = Qnil;
7414 specbind (Qinhibit_read_only, Qt);
7415 specbind (Qinhibit_modification_hooks, Qt);
7417 if (clear_buffer_p && Z > BEG)
7418 del_range (BEG, Z);
7420 xassert (BEGV >= BEG);
7421 xassert (ZV <= Z && ZV >= BEGV);
7423 rc = fn (a1, a2, a3, a4);
7425 xassert (BEGV >= BEG);
7426 xassert (ZV <= Z && ZV >= BEGV);
7428 unbind_to (count, Qnil);
7429 return rc;
7433 /* Save state that should be preserved around the call to the function
7434 FN called in with_echo_area_buffer. */
7436 static Lisp_Object
7437 with_echo_area_buffer_unwind_data (w)
7438 struct window *w;
7440 int i = 0;
7441 Lisp_Object vector;
7443 /* Reduce consing by keeping one vector in
7444 Vwith_echo_area_save_vector. */
7445 vector = Vwith_echo_area_save_vector;
7446 Vwith_echo_area_save_vector = Qnil;
7448 if (NILP (vector))
7449 vector = Fmake_vector (make_number (7), Qnil);
7451 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7452 AREF (vector, i) = Vdeactivate_mark, ++i;
7453 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7455 if (w)
7457 XSETWINDOW (AREF (vector, i), w); ++i;
7458 AREF (vector, i) = w->buffer; ++i;
7459 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7460 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7462 else
7464 int end = i + 4;
7465 for (; i < end; ++i)
7466 AREF (vector, i) = Qnil;
7469 xassert (i == ASIZE (vector));
7470 return vector;
7474 /* Restore global state from VECTOR which was created by
7475 with_echo_area_buffer_unwind_data. */
7477 static Lisp_Object
7478 unwind_with_echo_area_buffer (vector)
7479 Lisp_Object vector;
7481 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7482 Vdeactivate_mark = AREF (vector, 1);
7483 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7485 if (WINDOWP (AREF (vector, 3)))
7487 struct window *w;
7488 Lisp_Object buffer, charpos, bytepos;
7490 w = XWINDOW (AREF (vector, 3));
7491 buffer = AREF (vector, 4);
7492 charpos = AREF (vector, 5);
7493 bytepos = AREF (vector, 6);
7495 w->buffer = buffer;
7496 set_marker_both (w->pointm, buffer,
7497 XFASTINT (charpos), XFASTINT (bytepos));
7500 Vwith_echo_area_save_vector = vector;
7501 return Qnil;
7505 /* Set up the echo area for use by print functions. MULTIBYTE_P
7506 non-zero means we will print multibyte. */
7508 void
7509 setup_echo_area_for_printing (multibyte_p)
7510 int multibyte_p;
7512 /* If we can't find an echo area any more, exit. */
7513 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7514 Fkill_emacs (Qnil);
7516 ensure_echo_area_buffers ();
7518 if (!message_buf_print)
7520 /* A message has been output since the last time we printed.
7521 Choose a fresh echo area buffer. */
7522 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7523 echo_area_buffer[0] = echo_buffer[1];
7524 else
7525 echo_area_buffer[0] = echo_buffer[0];
7527 /* Switch to that buffer and clear it. */
7528 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7529 current_buffer->truncate_lines = Qnil;
7531 if (Z > BEG)
7533 int count = SPECPDL_INDEX ();
7534 specbind (Qinhibit_read_only, Qt);
7535 /* Note that undo recording is always disabled. */
7536 del_range (BEG, Z);
7537 unbind_to (count, Qnil);
7539 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7541 /* Set up the buffer for the multibyteness we need. */
7542 if (multibyte_p
7543 != !NILP (current_buffer->enable_multibyte_characters))
7544 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7546 /* Raise the frame containing the echo area. */
7547 if (minibuffer_auto_raise)
7549 struct frame *sf = SELECTED_FRAME ();
7550 Lisp_Object mini_window;
7551 mini_window = FRAME_MINIBUF_WINDOW (sf);
7552 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7555 message_log_maybe_newline ();
7556 message_buf_print = 1;
7558 else
7560 if (NILP (echo_area_buffer[0]))
7562 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7563 echo_area_buffer[0] = echo_buffer[1];
7564 else
7565 echo_area_buffer[0] = echo_buffer[0];
7568 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7570 /* Someone switched buffers between print requests. */
7571 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7572 current_buffer->truncate_lines = Qnil;
7578 /* Display an echo area message in window W. Value is non-zero if W's
7579 height is changed. If display_last_displayed_message_p is
7580 non-zero, display the message that was last displayed, otherwise
7581 display the current message. */
7583 static int
7584 display_echo_area (w)
7585 struct window *w;
7587 int i, no_message_p, window_height_changed_p, count;
7589 /* Temporarily disable garbage collections while displaying the echo
7590 area. This is done because a GC can print a message itself.
7591 That message would modify the echo area buffer's contents while a
7592 redisplay of the buffer is going on, and seriously confuse
7593 redisplay. */
7594 count = inhibit_garbage_collection ();
7596 /* If there is no message, we must call display_echo_area_1
7597 nevertheless because it resizes the window. But we will have to
7598 reset the echo_area_buffer in question to nil at the end because
7599 with_echo_area_buffer will sets it to an empty buffer. */
7600 i = display_last_displayed_message_p ? 1 : 0;
7601 no_message_p = NILP (echo_area_buffer[i]);
7603 window_height_changed_p
7604 = with_echo_area_buffer (w, display_last_displayed_message_p,
7605 display_echo_area_1,
7606 (EMACS_INT) w, Qnil, 0, 0);
7608 if (no_message_p)
7609 echo_area_buffer[i] = Qnil;
7611 unbind_to (count, Qnil);
7612 return window_height_changed_p;
7616 /* Helper for display_echo_area. Display the current buffer which
7617 contains the current echo area message in window W, a mini-window,
7618 a pointer to which is passed in A1. A2..A4 are currently not used.
7619 Change the height of W so that all of the message is displayed.
7620 Value is non-zero if height of W was changed. */
7622 static int
7623 display_echo_area_1 (a1, a2, a3, a4)
7624 EMACS_INT a1;
7625 Lisp_Object a2;
7626 EMACS_INT a3, a4;
7628 struct window *w = (struct window *) a1;
7629 Lisp_Object window;
7630 struct text_pos start;
7631 int window_height_changed_p = 0;
7633 /* Do this before displaying, so that we have a large enough glyph
7634 matrix for the display. If we can't get enough space for the
7635 whole text, display the last N lines. That works by setting w->start. */
7636 window_height_changed_p = resize_mini_window (w, 0);
7638 /* Use the starting position chosen by resize_mini_window. */
7639 SET_TEXT_POS_FROM_MARKER (start, w->start);
7641 /* Display. */
7642 clear_glyph_matrix (w->desired_matrix);
7643 XSETWINDOW (window, w);
7644 try_window (window, start, 0);
7646 return window_height_changed_p;
7650 /* Resize the echo area window to exactly the size needed for the
7651 currently displayed message, if there is one. If a mini-buffer
7652 is active, don't shrink it. */
7654 void
7655 resize_echo_area_exactly ()
7657 if (BUFFERP (echo_area_buffer[0])
7658 && WINDOWP (echo_area_window))
7660 struct window *w = XWINDOW (echo_area_window);
7661 int resized_p;
7662 Lisp_Object resize_exactly;
7664 if (minibuf_level == 0)
7665 resize_exactly = Qt;
7666 else
7667 resize_exactly = Qnil;
7669 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7670 (EMACS_INT) w, resize_exactly, 0, 0);
7671 if (resized_p)
7673 ++windows_or_buffers_changed;
7674 ++update_mode_lines;
7675 redisplay_internal (0);
7681 /* Callback function for with_echo_area_buffer, when used from
7682 resize_echo_area_exactly. A1 contains a pointer to the window to
7683 resize, EXACTLY non-nil means resize the mini-window exactly to the
7684 size of the text displayed. A3 and A4 are not used. Value is what
7685 resize_mini_window returns. */
7687 static int
7688 resize_mini_window_1 (a1, exactly, a3, a4)
7689 EMACS_INT a1;
7690 Lisp_Object exactly;
7691 EMACS_INT a3, a4;
7693 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7697 /* Resize mini-window W to fit the size of its contents. EXACT:P
7698 means size the window exactly to the size needed. Otherwise, it's
7699 only enlarged until W's buffer is empty.
7701 Set W->start to the right place to begin display. If the whole
7702 contents fit, start at the beginning. Otherwise, start so as
7703 to make the end of the contents appear. This is particularly
7704 important for y-or-n-p, but seems desirable generally.
7706 Value is non-zero if the window height has been changed. */
7709 resize_mini_window (w, exact_p)
7710 struct window *w;
7711 int exact_p;
7713 struct frame *f = XFRAME (w->frame);
7714 int window_height_changed_p = 0;
7716 xassert (MINI_WINDOW_P (w));
7718 /* By default, start display at the beginning. */
7719 set_marker_both (w->start, w->buffer,
7720 BUF_BEGV (XBUFFER (w->buffer)),
7721 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
7723 /* Don't resize windows while redisplaying a window; it would
7724 confuse redisplay functions when the size of the window they are
7725 displaying changes from under them. Such a resizing can happen,
7726 for instance, when which-func prints a long message while
7727 we are running fontification-functions. We're running these
7728 functions with safe_call which binds inhibit-redisplay to t. */
7729 if (!NILP (Vinhibit_redisplay))
7730 return 0;
7732 /* Nil means don't try to resize. */
7733 if (NILP (Vresize_mini_windows)
7734 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7735 return 0;
7737 if (!FRAME_MINIBUF_ONLY_P (f))
7739 struct it it;
7740 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7741 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7742 int height, max_height;
7743 int unit = FRAME_LINE_HEIGHT (f);
7744 struct text_pos start;
7745 struct buffer *old_current_buffer = NULL;
7747 if (current_buffer != XBUFFER (w->buffer))
7749 old_current_buffer = current_buffer;
7750 set_buffer_internal (XBUFFER (w->buffer));
7753 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7755 /* Compute the max. number of lines specified by the user. */
7756 if (FLOATP (Vmax_mini_window_height))
7757 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7758 else if (INTEGERP (Vmax_mini_window_height))
7759 max_height = XINT (Vmax_mini_window_height);
7760 else
7761 max_height = total_height / 4;
7763 /* Correct that max. height if it's bogus. */
7764 max_height = max (1, max_height);
7765 max_height = min (total_height, max_height);
7767 /* Find out the height of the text in the window. */
7768 if (it.truncate_lines_p)
7769 height = 1;
7770 else
7772 last_height = 0;
7773 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7774 if (it.max_ascent == 0 && it.max_descent == 0)
7775 height = it.current_y + last_height;
7776 else
7777 height = it.current_y + it.max_ascent + it.max_descent;
7778 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7779 height = (height + unit - 1) / unit;
7782 /* Compute a suitable window start. */
7783 if (height > max_height)
7785 height = max_height;
7786 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
7787 move_it_vertically_backward (&it, (height - 1) * unit);
7788 start = it.current.pos;
7790 else
7791 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7792 SET_MARKER_FROM_TEXT_POS (w->start, start);
7794 if (EQ (Vresize_mini_windows, Qgrow_only))
7796 /* Let it grow only, until we display an empty message, in which
7797 case the window shrinks again. */
7798 if (height > WINDOW_TOTAL_LINES (w))
7800 int old_height = WINDOW_TOTAL_LINES (w);
7801 freeze_window_starts (f, 1);
7802 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7803 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7805 else if (height < WINDOW_TOTAL_LINES (w)
7806 && (exact_p || BEGV == ZV))
7808 int old_height = WINDOW_TOTAL_LINES (w);
7809 freeze_window_starts (f, 0);
7810 shrink_mini_window (w);
7811 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7814 else
7816 /* Always resize to exact size needed. */
7817 if (height > WINDOW_TOTAL_LINES (w))
7819 int old_height = WINDOW_TOTAL_LINES (w);
7820 freeze_window_starts (f, 1);
7821 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7822 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7824 else if (height < WINDOW_TOTAL_LINES (w))
7826 int old_height = WINDOW_TOTAL_LINES (w);
7827 freeze_window_starts (f, 0);
7828 shrink_mini_window (w);
7830 if (height)
7832 freeze_window_starts (f, 1);
7833 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7836 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7840 if (old_current_buffer)
7841 set_buffer_internal (old_current_buffer);
7844 return window_height_changed_p;
7848 /* Value is the current message, a string, or nil if there is no
7849 current message. */
7851 Lisp_Object
7852 current_message ()
7854 Lisp_Object msg;
7856 if (NILP (echo_area_buffer[0]))
7857 msg = Qnil;
7858 else
7860 with_echo_area_buffer (0, 0, current_message_1,
7861 (EMACS_INT) &msg, Qnil, 0, 0);
7862 if (NILP (msg))
7863 echo_area_buffer[0] = Qnil;
7866 return msg;
7870 static int
7871 current_message_1 (a1, a2, a3, a4)
7872 EMACS_INT a1;
7873 Lisp_Object a2;
7874 EMACS_INT a3, a4;
7876 Lisp_Object *msg = (Lisp_Object *) a1;
7878 if (Z > BEG)
7879 *msg = make_buffer_string (BEG, Z, 1);
7880 else
7881 *msg = Qnil;
7882 return 0;
7886 /* Push the current message on Vmessage_stack for later restauration
7887 by restore_message. Value is non-zero if the current message isn't
7888 empty. This is a relatively infrequent operation, so it's not
7889 worth optimizing. */
7892 push_message ()
7894 Lisp_Object msg;
7895 msg = current_message ();
7896 Vmessage_stack = Fcons (msg, Vmessage_stack);
7897 return STRINGP (msg);
7901 /* Restore message display from the top of Vmessage_stack. */
7903 void
7904 restore_message ()
7906 Lisp_Object msg;
7908 xassert (CONSP (Vmessage_stack));
7909 msg = XCAR (Vmessage_stack);
7910 if (STRINGP (msg))
7911 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7912 else
7913 message3_nolog (msg, 0, 0);
7917 /* Handler for record_unwind_protect calling pop_message. */
7919 Lisp_Object
7920 pop_message_unwind (dummy)
7921 Lisp_Object dummy;
7923 pop_message ();
7924 return Qnil;
7927 /* Pop the top-most entry off Vmessage_stack. */
7929 void
7930 pop_message ()
7932 xassert (CONSP (Vmessage_stack));
7933 Vmessage_stack = XCDR (Vmessage_stack);
7937 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7938 exits. If the stack is not empty, we have a missing pop_message
7939 somewhere. */
7941 void
7942 check_message_stack ()
7944 if (!NILP (Vmessage_stack))
7945 abort ();
7949 /* Truncate to NCHARS what will be displayed in the echo area the next
7950 time we display it---but don't redisplay it now. */
7952 void
7953 truncate_echo_area (nchars)
7954 int nchars;
7956 if (nchars == 0)
7957 echo_area_buffer[0] = Qnil;
7958 /* A null message buffer means that the frame hasn't really been
7959 initialized yet. Error messages get reported properly by
7960 cmd_error, so this must be just an informative message; toss it. */
7961 else if (!noninteractive
7962 && INTERACTIVE
7963 && !NILP (echo_area_buffer[0]))
7965 struct frame *sf = SELECTED_FRAME ();
7966 if (FRAME_MESSAGE_BUF (sf))
7967 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7972 /* Helper function for truncate_echo_area. Truncate the current
7973 message to at most NCHARS characters. */
7975 static int
7976 truncate_message_1 (nchars, a2, a3, a4)
7977 EMACS_INT nchars;
7978 Lisp_Object a2;
7979 EMACS_INT a3, a4;
7981 if (BEG + nchars < Z)
7982 del_range (BEG + nchars, Z);
7983 if (Z == BEG)
7984 echo_area_buffer[0] = Qnil;
7985 return 0;
7989 /* Set the current message to a substring of S or STRING.
7991 If STRING is a Lisp string, set the message to the first NBYTES
7992 bytes from STRING. NBYTES zero means use the whole string. If
7993 STRING is multibyte, the message will be displayed multibyte.
7995 If S is not null, set the message to the first LEN bytes of S. LEN
7996 zero means use the whole string. MULTIBYTE_P non-zero means S is
7997 multibyte. Display the message multibyte in that case. */
7999 void
8000 set_message (s, string, nbytes, multibyte_p)
8001 const char *s;
8002 Lisp_Object string;
8003 int nbytes, multibyte_p;
8005 message_enable_multibyte
8006 = ((s && multibyte_p)
8007 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8009 with_echo_area_buffer (0, 0, set_message_1,
8010 (EMACS_INT) s, string, nbytes, multibyte_p);
8011 message_buf_print = 0;
8012 help_echo_showing_p = 0;
8016 /* Helper function for set_message. Arguments have the same meaning
8017 as there, with A1 corresponding to S and A2 corresponding to STRING
8018 This function is called with the echo area buffer being
8019 current. */
8021 static int
8022 set_message_1 (a1, a2, nbytes, multibyte_p)
8023 EMACS_INT a1;
8024 Lisp_Object a2;
8025 EMACS_INT nbytes, multibyte_p;
8027 const char *s = (const char *) a1;
8028 Lisp_Object string = a2;
8030 /* Change multibyteness of the echo buffer appropriately. */
8031 if (message_enable_multibyte
8032 != !NILP (current_buffer->enable_multibyte_characters))
8033 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8035 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8037 /* Insert new message at BEG. */
8038 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8039 Ferase_buffer ();
8041 if (STRINGP (string))
8043 int nchars;
8045 if (nbytes == 0)
8046 nbytes = SBYTES (string);
8047 nchars = string_byte_to_char (string, nbytes);
8049 /* This function takes care of single/multibyte conversion. We
8050 just have to ensure that the echo area buffer has the right
8051 setting of enable_multibyte_characters. */
8052 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8054 else if (s)
8056 if (nbytes == 0)
8057 nbytes = strlen (s);
8059 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8061 /* Convert from multi-byte to single-byte. */
8062 int i, c, n;
8063 unsigned char work[1];
8065 /* Convert a multibyte string to single-byte. */
8066 for (i = 0; i < nbytes; i += n)
8068 c = string_char_and_length (s + i, nbytes - i, &n);
8069 work[0] = (SINGLE_BYTE_CHAR_P (c)
8071 : multibyte_char_to_unibyte (c, Qnil));
8072 insert_1_both (work, 1, 1, 1, 0, 0);
8075 else if (!multibyte_p
8076 && !NILP (current_buffer->enable_multibyte_characters))
8078 /* Convert from single-byte to multi-byte. */
8079 int i, c, n;
8080 const unsigned char *msg = (const unsigned char *) s;
8081 unsigned char str[MAX_MULTIBYTE_LENGTH];
8083 /* Convert a single-byte string to multibyte. */
8084 for (i = 0; i < nbytes; i++)
8086 c = unibyte_char_to_multibyte (msg[i]);
8087 n = CHAR_STRING (c, str);
8088 insert_1_both (str, 1, n, 1, 0, 0);
8091 else
8092 insert_1 (s, nbytes, 1, 0, 0);
8095 return 0;
8099 /* Clear messages. CURRENT_P non-zero means clear the current
8100 message. LAST_DISPLAYED_P non-zero means clear the message
8101 last displayed. */
8103 void
8104 clear_message (current_p, last_displayed_p)
8105 int current_p, last_displayed_p;
8107 if (current_p)
8109 echo_area_buffer[0] = Qnil;
8110 message_cleared_p = 1;
8113 if (last_displayed_p)
8114 echo_area_buffer[1] = Qnil;
8116 message_buf_print = 0;
8119 /* Clear garbaged frames.
8121 This function is used where the old redisplay called
8122 redraw_garbaged_frames which in turn called redraw_frame which in
8123 turn called clear_frame. The call to clear_frame was a source of
8124 flickering. I believe a clear_frame is not necessary. It should
8125 suffice in the new redisplay to invalidate all current matrices,
8126 and ensure a complete redisplay of all windows. */
8128 static void
8129 clear_garbaged_frames ()
8131 if (frame_garbaged)
8133 Lisp_Object tail, frame;
8134 int changed_count = 0;
8136 FOR_EACH_FRAME (tail, frame)
8138 struct frame *f = XFRAME (frame);
8140 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8142 if (f->resized_p)
8144 Fredraw_frame (frame);
8145 f->force_flush_display_p = 1;
8147 clear_current_matrices (f);
8148 changed_count++;
8149 f->garbaged = 0;
8150 f->resized_p = 0;
8154 frame_garbaged = 0;
8155 if (changed_count)
8156 ++windows_or_buffers_changed;
8161 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8162 is non-zero update selected_frame. Value is non-zero if the
8163 mini-windows height has been changed. */
8165 static int
8166 echo_area_display (update_frame_p)
8167 int update_frame_p;
8169 Lisp_Object mini_window;
8170 struct window *w;
8171 struct frame *f;
8172 int window_height_changed_p = 0;
8173 struct frame *sf = SELECTED_FRAME ();
8175 mini_window = FRAME_MINIBUF_WINDOW (sf);
8176 w = XWINDOW (mini_window);
8177 f = XFRAME (WINDOW_FRAME (w));
8179 /* Don't display if frame is invisible or not yet initialized. */
8180 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8181 return 0;
8183 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8184 #ifndef MAC_OS8
8185 #ifdef HAVE_WINDOW_SYSTEM
8186 /* When Emacs starts, selected_frame may be a visible terminal
8187 frame, even if we run under a window system. If we let this
8188 through, a message would be displayed on the terminal. */
8189 if (EQ (selected_frame, Vterminal_frame)
8190 && !NILP (Vwindow_system))
8191 return 0;
8192 #endif /* HAVE_WINDOW_SYSTEM */
8193 #endif
8195 /* Redraw garbaged frames. */
8196 if (frame_garbaged)
8197 clear_garbaged_frames ();
8199 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8201 echo_area_window = mini_window;
8202 window_height_changed_p = display_echo_area (w);
8203 w->must_be_updated_p = 1;
8205 /* Update the display, unless called from redisplay_internal.
8206 Also don't update the screen during redisplay itself. The
8207 update will happen at the end of redisplay, and an update
8208 here could cause confusion. */
8209 if (update_frame_p && !redisplaying_p)
8211 int n = 0;
8213 /* If the display update has been interrupted by pending
8214 input, update mode lines in the frame. Due to the
8215 pending input, it might have been that redisplay hasn't
8216 been called, so that mode lines above the echo area are
8217 garbaged. This looks odd, so we prevent it here. */
8218 if (!display_completed)
8219 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8221 if (window_height_changed_p
8222 /* Don't do this if Emacs is shutting down. Redisplay
8223 needs to run hooks. */
8224 && !NILP (Vrun_hooks))
8226 /* Must update other windows. Likewise as in other
8227 cases, don't let this update be interrupted by
8228 pending input. */
8229 int count = SPECPDL_INDEX ();
8230 specbind (Qredisplay_dont_pause, Qt);
8231 windows_or_buffers_changed = 1;
8232 redisplay_internal (0);
8233 unbind_to (count, Qnil);
8235 else if (FRAME_WINDOW_P (f) && n == 0)
8237 /* Window configuration is the same as before.
8238 Can do with a display update of the echo area,
8239 unless we displayed some mode lines. */
8240 update_single_window (w, 1);
8241 rif->flush_display (f);
8243 else
8244 update_frame (f, 1, 1);
8246 /* If cursor is in the echo area, make sure that the next
8247 redisplay displays the minibuffer, so that the cursor will
8248 be replaced with what the minibuffer wants. */
8249 if (cursor_in_echo_area)
8250 ++windows_or_buffers_changed;
8253 else if (!EQ (mini_window, selected_window))
8254 windows_or_buffers_changed++;
8256 /* The current message is now also the last one displayed. */
8257 echo_area_buffer[1] = echo_area_buffer[0];
8259 /* Prevent redisplay optimization in redisplay_internal by resetting
8260 this_line_start_pos. This is done because the mini-buffer now
8261 displays the message instead of its buffer text. */
8262 if (EQ (mini_window, selected_window))
8263 CHARPOS (this_line_start_pos) = 0;
8265 return window_height_changed_p;
8270 /***********************************************************************
8271 Mode Lines and Frame Titles
8272 ***********************************************************************/
8274 /* A buffer for constructing non-propertized mode-line strings and
8275 frame titles in it; allocated from the heap in init_xdisp and
8276 resized as needed in store_mode_line_noprop_char. */
8278 static char *mode_line_noprop_buf;
8280 /* The buffer's end, and a current output position in it. */
8282 static char *mode_line_noprop_buf_end;
8283 static char *mode_line_noprop_ptr;
8285 #define MODE_LINE_NOPROP_LEN(start) \
8286 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8288 static enum {
8289 MODE_LINE_DISPLAY = 0,
8290 MODE_LINE_TITLE,
8291 MODE_LINE_NOPROP,
8292 MODE_LINE_STRING
8293 } mode_line_target;
8295 /* Alist that caches the results of :propertize.
8296 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8297 static Lisp_Object mode_line_proptrans_alist;
8299 /* List of strings making up the mode-line. */
8300 static Lisp_Object mode_line_string_list;
8302 /* Base face property when building propertized mode line string. */
8303 static Lisp_Object mode_line_string_face;
8304 static Lisp_Object mode_line_string_face_prop;
8307 /* Unwind data for mode line strings */
8309 static Lisp_Object Vmode_line_unwind_vector;
8311 static Lisp_Object
8312 format_mode_line_unwind_data (obuf)
8313 struct buffer *obuf;
8315 Lisp_Object vector;
8317 /* Reduce consing by keeping one vector in
8318 Vwith_echo_area_save_vector. */
8319 vector = Vmode_line_unwind_vector;
8320 Vmode_line_unwind_vector = Qnil;
8322 if (NILP (vector))
8323 vector = Fmake_vector (make_number (7), Qnil);
8325 AREF (vector, 0) = make_number (mode_line_target);
8326 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8327 AREF (vector, 2) = mode_line_string_list;
8328 AREF (vector, 3) = mode_line_proptrans_alist;
8329 AREF (vector, 4) = mode_line_string_face;
8330 AREF (vector, 5) = mode_line_string_face_prop;
8332 if (obuf)
8333 XSETBUFFER (AREF (vector, 6), obuf);
8334 else
8335 AREF (vector, 6) = Qnil;
8337 return vector;
8340 static Lisp_Object
8341 unwind_format_mode_line (vector)
8342 Lisp_Object vector;
8344 mode_line_target = XINT (AREF (vector, 0));
8345 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8346 mode_line_string_list = AREF (vector, 2);
8347 mode_line_proptrans_alist = AREF (vector, 3);
8348 mode_line_string_face = AREF (vector, 4);
8349 mode_line_string_face_prop = AREF (vector, 5);
8351 if (!NILP (AREF (vector, 6)))
8353 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8354 AREF (vector, 6) = Qnil;
8357 Vmode_line_unwind_vector = vector;
8358 return Qnil;
8362 /* Store a single character C for the frame title in mode_line_noprop_buf.
8363 Re-allocate mode_line_noprop_buf if necessary. */
8365 static void
8366 #ifdef PROTOTYPES
8367 store_mode_line_noprop_char (char c)
8368 #else
8369 store_mode_line_noprop_char (c)
8370 char c;
8371 #endif
8373 /* If output position has reached the end of the allocated buffer,
8374 double the buffer's size. */
8375 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8377 int len = MODE_LINE_NOPROP_LEN (0);
8378 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8379 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8380 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8381 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8384 *mode_line_noprop_ptr++ = c;
8388 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8389 mode_line_noprop_ptr. STR is the string to store. Do not copy
8390 characters that yield more columns than PRECISION; PRECISION <= 0
8391 means copy the whole string. Pad with spaces until FIELD_WIDTH
8392 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8393 pad. Called from display_mode_element when it is used to build a
8394 frame title. */
8396 static int
8397 store_mode_line_noprop (str, field_width, precision)
8398 const unsigned char *str;
8399 int field_width, precision;
8401 int n = 0;
8402 int dummy, nbytes;
8404 /* Copy at most PRECISION chars from STR. */
8405 nbytes = strlen (str);
8406 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8407 while (nbytes--)
8408 store_mode_line_noprop_char (*str++);
8410 /* Fill up with spaces until FIELD_WIDTH reached. */
8411 while (field_width > 0
8412 && n < field_width)
8414 store_mode_line_noprop_char (' ');
8415 ++n;
8418 return n;
8421 /***********************************************************************
8422 Frame Titles
8423 ***********************************************************************/
8425 #ifdef HAVE_WINDOW_SYSTEM
8427 /* Set the title of FRAME, if it has changed. The title format is
8428 Vicon_title_format if FRAME is iconified, otherwise it is
8429 frame_title_format. */
8431 static void
8432 x_consider_frame_title (frame)
8433 Lisp_Object frame;
8435 struct frame *f = XFRAME (frame);
8437 if (FRAME_WINDOW_P (f)
8438 || FRAME_MINIBUF_ONLY_P (f)
8439 || f->explicit_name)
8441 /* Do we have more than one visible frame on this X display? */
8442 Lisp_Object tail;
8443 Lisp_Object fmt;
8444 int title_start;
8445 char *title;
8446 int len;
8447 struct it it;
8448 int count = SPECPDL_INDEX ();
8450 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8452 Lisp_Object other_frame = XCAR (tail);
8453 struct frame *tf = XFRAME (other_frame);
8455 if (tf != f
8456 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8457 && !FRAME_MINIBUF_ONLY_P (tf)
8458 && !EQ (other_frame, tip_frame)
8459 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8460 break;
8463 /* Set global variable indicating that multiple frames exist. */
8464 multiple_frames = CONSP (tail);
8466 /* Switch to the buffer of selected window of the frame. Set up
8467 mode_line_target so that display_mode_element will output into
8468 mode_line_noprop_buf; then display the title. */
8469 record_unwind_protect (unwind_format_mode_line,
8470 format_mode_line_unwind_data (current_buffer));
8472 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8473 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8475 mode_line_target = MODE_LINE_TITLE;
8476 title_start = MODE_LINE_NOPROP_LEN (0);
8477 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8478 NULL, DEFAULT_FACE_ID);
8479 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8480 len = MODE_LINE_NOPROP_LEN (title_start);
8481 title = mode_line_noprop_buf + title_start;
8482 unbind_to (count, Qnil);
8484 /* Set the title only if it's changed. This avoids consing in
8485 the common case where it hasn't. (If it turns out that we've
8486 already wasted too much time by walking through the list with
8487 display_mode_element, then we might need to optimize at a
8488 higher level than this.) */
8489 if (! STRINGP (f->name)
8490 || SBYTES (f->name) != len
8491 || bcmp (title, SDATA (f->name), len) != 0)
8492 x_implicitly_set_name (f, make_string (title, len), Qnil);
8496 #endif /* not HAVE_WINDOW_SYSTEM */
8501 /***********************************************************************
8502 Menu Bars
8503 ***********************************************************************/
8506 /* Prepare for redisplay by updating menu-bar item lists when
8507 appropriate. This can call eval. */
8509 void
8510 prepare_menu_bars ()
8512 int all_windows;
8513 struct gcpro gcpro1, gcpro2;
8514 struct frame *f;
8515 Lisp_Object tooltip_frame;
8517 #ifdef HAVE_WINDOW_SYSTEM
8518 tooltip_frame = tip_frame;
8519 #else
8520 tooltip_frame = Qnil;
8521 #endif
8523 /* Update all frame titles based on their buffer names, etc. We do
8524 this before the menu bars so that the buffer-menu will show the
8525 up-to-date frame titles. */
8526 #ifdef HAVE_WINDOW_SYSTEM
8527 if (windows_or_buffers_changed || update_mode_lines)
8529 Lisp_Object tail, frame;
8531 FOR_EACH_FRAME (tail, frame)
8533 f = XFRAME (frame);
8534 if (!EQ (frame, tooltip_frame)
8535 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8536 x_consider_frame_title (frame);
8539 #endif /* HAVE_WINDOW_SYSTEM */
8541 /* Update the menu bar item lists, if appropriate. This has to be
8542 done before any actual redisplay or generation of display lines. */
8543 all_windows = (update_mode_lines
8544 || buffer_shared > 1
8545 || windows_or_buffers_changed);
8546 if (all_windows)
8548 Lisp_Object tail, frame;
8549 int count = SPECPDL_INDEX ();
8551 record_unwind_save_match_data ();
8553 FOR_EACH_FRAME (tail, frame)
8555 f = XFRAME (frame);
8557 /* Ignore tooltip frame. */
8558 if (EQ (frame, tooltip_frame))
8559 continue;
8561 /* If a window on this frame changed size, report that to
8562 the user and clear the size-change flag. */
8563 if (FRAME_WINDOW_SIZES_CHANGED (f))
8565 Lisp_Object functions;
8567 /* Clear flag first in case we get an error below. */
8568 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8569 functions = Vwindow_size_change_functions;
8570 GCPRO2 (tail, functions);
8572 while (CONSP (functions))
8574 call1 (XCAR (functions), frame);
8575 functions = XCDR (functions);
8577 UNGCPRO;
8580 GCPRO1 (tail);
8581 update_menu_bar (f, 0);
8582 #ifdef HAVE_WINDOW_SYSTEM
8583 update_tool_bar (f, 0);
8584 #endif
8585 UNGCPRO;
8588 unbind_to (count, Qnil);
8590 else
8592 struct frame *sf = SELECTED_FRAME ();
8593 update_menu_bar (sf, 1);
8594 #ifdef HAVE_WINDOW_SYSTEM
8595 update_tool_bar (sf, 1);
8596 #endif
8599 /* Motif needs this. See comment in xmenu.c. Turn it off when
8600 pending_menu_activation is not defined. */
8601 #ifdef USE_X_TOOLKIT
8602 pending_menu_activation = 0;
8603 #endif
8607 /* Update the menu bar item list for frame F. This has to be done
8608 before we start to fill in any display lines, because it can call
8609 eval.
8611 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8613 static void
8614 update_menu_bar (f, save_match_data)
8615 struct frame *f;
8616 int save_match_data;
8618 Lisp_Object window;
8619 register struct window *w;
8621 /* If called recursively during a menu update, do nothing. This can
8622 happen when, for instance, an activate-menubar-hook causes a
8623 redisplay. */
8624 if (inhibit_menubar_update)
8625 return;
8627 window = FRAME_SELECTED_WINDOW (f);
8628 w = XWINDOW (window);
8630 #if 0 /* The if statement below this if statement used to include the
8631 condition !NILP (w->update_mode_line), rather than using
8632 update_mode_lines directly, and this if statement may have
8633 been added to make that condition work. Now the if
8634 statement below matches its comment, this isn't needed. */
8635 if (update_mode_lines)
8636 w->update_mode_line = Qt;
8637 #endif
8639 if (FRAME_WINDOW_P (f)
8641 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8642 || defined (USE_GTK)
8643 FRAME_EXTERNAL_MENU_BAR (f)
8644 #else
8645 FRAME_MENU_BAR_LINES (f) > 0
8646 #endif
8647 : FRAME_MENU_BAR_LINES (f) > 0)
8649 /* If the user has switched buffers or windows, we need to
8650 recompute to reflect the new bindings. But we'll
8651 recompute when update_mode_lines is set too; that means
8652 that people can use force-mode-line-update to request
8653 that the menu bar be recomputed. The adverse effect on
8654 the rest of the redisplay algorithm is about the same as
8655 windows_or_buffers_changed anyway. */
8656 if (windows_or_buffers_changed
8657 /* This used to test w->update_mode_line, but we believe
8658 there is no need to recompute the menu in that case. */
8659 || update_mode_lines
8660 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8661 < BUF_MODIFF (XBUFFER (w->buffer)))
8662 != !NILP (w->last_had_star))
8663 || ((!NILP (Vtransient_mark_mode)
8664 && !NILP (XBUFFER (w->buffer)->mark_active))
8665 != !NILP (w->region_showing)))
8667 struct buffer *prev = current_buffer;
8668 int count = SPECPDL_INDEX ();
8670 specbind (Qinhibit_menubar_update, Qt);
8672 set_buffer_internal_1 (XBUFFER (w->buffer));
8673 if (save_match_data)
8674 record_unwind_save_match_data ();
8675 if (NILP (Voverriding_local_map_menu_flag))
8677 specbind (Qoverriding_terminal_local_map, Qnil);
8678 specbind (Qoverriding_local_map, Qnil);
8681 /* Run the Lucid hook. */
8682 safe_run_hooks (Qactivate_menubar_hook);
8684 /* If it has changed current-menubar from previous value,
8685 really recompute the menu-bar from the value. */
8686 if (! NILP (Vlucid_menu_bar_dirty_flag))
8687 call0 (Qrecompute_lucid_menubar);
8689 safe_run_hooks (Qmenu_bar_update_hook);
8690 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8692 /* Redisplay the menu bar in case we changed it. */
8693 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8694 || defined (USE_GTK)
8695 if (FRAME_WINDOW_P (f)
8696 #if defined (MAC_OS)
8697 /* All frames on Mac OS share the same menubar. So only the
8698 selected frame should be allowed to set it. */
8699 && f == SELECTED_FRAME ()
8700 #endif
8702 set_frame_menubar (f, 0, 0);
8703 else
8704 /* On a terminal screen, the menu bar is an ordinary screen
8705 line, and this makes it get updated. */
8706 w->update_mode_line = Qt;
8707 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8708 /* In the non-toolkit version, the menu bar is an ordinary screen
8709 line, and this makes it get updated. */
8710 w->update_mode_line = Qt;
8711 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8713 unbind_to (count, Qnil);
8714 set_buffer_internal_1 (prev);
8721 /***********************************************************************
8722 Output Cursor
8723 ***********************************************************************/
8725 #ifdef HAVE_WINDOW_SYSTEM
8727 /* EXPORT:
8728 Nominal cursor position -- where to draw output.
8729 HPOS and VPOS are window relative glyph matrix coordinates.
8730 X and Y are window relative pixel coordinates. */
8732 struct cursor_pos output_cursor;
8735 /* EXPORT:
8736 Set the global variable output_cursor to CURSOR. All cursor
8737 positions are relative to updated_window. */
8739 void
8740 set_output_cursor (cursor)
8741 struct cursor_pos *cursor;
8743 output_cursor.hpos = cursor->hpos;
8744 output_cursor.vpos = cursor->vpos;
8745 output_cursor.x = cursor->x;
8746 output_cursor.y = cursor->y;
8750 /* EXPORT for RIF:
8751 Set a nominal cursor position.
8753 HPOS and VPOS are column/row positions in a window glyph matrix. X
8754 and Y are window text area relative pixel positions.
8756 If this is done during an update, updated_window will contain the
8757 window that is being updated and the position is the future output
8758 cursor position for that window. If updated_window is null, use
8759 selected_window and display the cursor at the given position. */
8761 void
8762 x_cursor_to (vpos, hpos, y, x)
8763 int vpos, hpos, y, x;
8765 struct window *w;
8767 /* If updated_window is not set, work on selected_window. */
8768 if (updated_window)
8769 w = updated_window;
8770 else
8771 w = XWINDOW (selected_window);
8773 /* Set the output cursor. */
8774 output_cursor.hpos = hpos;
8775 output_cursor.vpos = vpos;
8776 output_cursor.x = x;
8777 output_cursor.y = y;
8779 /* If not called as part of an update, really display the cursor.
8780 This will also set the cursor position of W. */
8781 if (updated_window == NULL)
8783 BLOCK_INPUT;
8784 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8785 if (rif->flush_display_optional)
8786 rif->flush_display_optional (SELECTED_FRAME ());
8787 UNBLOCK_INPUT;
8791 #endif /* HAVE_WINDOW_SYSTEM */
8794 /***********************************************************************
8795 Tool-bars
8796 ***********************************************************************/
8798 #ifdef HAVE_WINDOW_SYSTEM
8800 /* Where the mouse was last time we reported a mouse event. */
8802 FRAME_PTR last_mouse_frame;
8804 /* Tool-bar item index of the item on which a mouse button was pressed
8805 or -1. */
8807 int last_tool_bar_item;
8810 /* Update the tool-bar item list for frame F. This has to be done
8811 before we start to fill in any display lines. Called from
8812 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8813 and restore it here. */
8815 static void
8816 update_tool_bar (f, save_match_data)
8817 struct frame *f;
8818 int save_match_data;
8820 #ifdef USE_GTK
8821 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8822 #else
8823 int do_update = WINDOWP (f->tool_bar_window)
8824 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8825 #endif
8827 if (do_update)
8829 Lisp_Object window;
8830 struct window *w;
8832 window = FRAME_SELECTED_WINDOW (f);
8833 w = XWINDOW (window);
8835 /* If the user has switched buffers or windows, we need to
8836 recompute to reflect the new bindings. But we'll
8837 recompute when update_mode_lines is set too; that means
8838 that people can use force-mode-line-update to request
8839 that the menu bar be recomputed. The adverse effect on
8840 the rest of the redisplay algorithm is about the same as
8841 windows_or_buffers_changed anyway. */
8842 if (windows_or_buffers_changed
8843 || !NILP (w->update_mode_line)
8844 || update_mode_lines
8845 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8846 < BUF_MODIFF (XBUFFER (w->buffer)))
8847 != !NILP (w->last_had_star))
8848 || ((!NILP (Vtransient_mark_mode)
8849 && !NILP (XBUFFER (w->buffer)->mark_active))
8850 != !NILP (w->region_showing)))
8852 struct buffer *prev = current_buffer;
8853 int count = SPECPDL_INDEX ();
8854 Lisp_Object new_tool_bar;
8855 int new_n_tool_bar;
8856 struct gcpro gcpro1;
8858 /* Set current_buffer to the buffer of the selected
8859 window of the frame, so that we get the right local
8860 keymaps. */
8861 set_buffer_internal_1 (XBUFFER (w->buffer));
8863 /* Save match data, if we must. */
8864 if (save_match_data)
8865 record_unwind_save_match_data ();
8867 /* Make sure that we don't accidentally use bogus keymaps. */
8868 if (NILP (Voverriding_local_map_menu_flag))
8870 specbind (Qoverriding_terminal_local_map, Qnil);
8871 specbind (Qoverriding_local_map, Qnil);
8874 GCPRO1 (new_tool_bar);
8876 /* Build desired tool-bar items from keymaps. */
8877 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8878 &new_n_tool_bar);
8880 /* Redisplay the tool-bar if we changed it. */
8881 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8883 /* Redisplay that happens asynchronously due to an expose event
8884 may access f->tool_bar_items. Make sure we update both
8885 variables within BLOCK_INPUT so no such event interrupts. */
8886 BLOCK_INPUT;
8887 f->tool_bar_items = new_tool_bar;
8888 f->n_tool_bar_items = new_n_tool_bar;
8889 w->update_mode_line = Qt;
8890 UNBLOCK_INPUT;
8893 UNGCPRO;
8895 unbind_to (count, Qnil);
8896 set_buffer_internal_1 (prev);
8902 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8903 F's desired tool-bar contents. F->tool_bar_items must have
8904 been set up previously by calling prepare_menu_bars. */
8906 static void
8907 build_desired_tool_bar_string (f)
8908 struct frame *f;
8910 int i, size, size_needed;
8911 struct gcpro gcpro1, gcpro2, gcpro3;
8912 Lisp_Object image, plist, props;
8914 image = plist = props = Qnil;
8915 GCPRO3 (image, plist, props);
8917 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8918 Otherwise, make a new string. */
8920 /* The size of the string we might be able to reuse. */
8921 size = (STRINGP (f->desired_tool_bar_string)
8922 ? SCHARS (f->desired_tool_bar_string)
8923 : 0);
8925 /* We need one space in the string for each image. */
8926 size_needed = f->n_tool_bar_items;
8928 /* Reuse f->desired_tool_bar_string, if possible. */
8929 if (size < size_needed || NILP (f->desired_tool_bar_string))
8930 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8931 make_number (' '));
8932 else
8934 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8935 Fremove_text_properties (make_number (0), make_number (size),
8936 props, f->desired_tool_bar_string);
8939 /* Put a `display' property on the string for the images to display,
8940 put a `menu_item' property on tool-bar items with a value that
8941 is the index of the item in F's tool-bar item vector. */
8942 for (i = 0; i < f->n_tool_bar_items; ++i)
8944 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8946 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8947 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8948 int hmargin, vmargin, relief, idx, end;
8949 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8951 /* If image is a vector, choose the image according to the
8952 button state. */
8953 image = PROP (TOOL_BAR_ITEM_IMAGES);
8954 if (VECTORP (image))
8956 if (enabled_p)
8957 idx = (selected_p
8958 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8959 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8960 else
8961 idx = (selected_p
8962 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8963 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8965 xassert (ASIZE (image) >= idx);
8966 image = AREF (image, idx);
8968 else
8969 idx = -1;
8971 /* Ignore invalid image specifications. */
8972 if (!valid_image_p (image))
8973 continue;
8975 /* Display the tool-bar button pressed, or depressed. */
8976 plist = Fcopy_sequence (XCDR (image));
8978 /* Compute margin and relief to draw. */
8979 relief = (tool_bar_button_relief >= 0
8980 ? tool_bar_button_relief
8981 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8982 hmargin = vmargin = relief;
8984 if (INTEGERP (Vtool_bar_button_margin)
8985 && XINT (Vtool_bar_button_margin) > 0)
8987 hmargin += XFASTINT (Vtool_bar_button_margin);
8988 vmargin += XFASTINT (Vtool_bar_button_margin);
8990 else if (CONSP (Vtool_bar_button_margin))
8992 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8993 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8994 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8996 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8997 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8998 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9001 if (auto_raise_tool_bar_buttons_p)
9003 /* Add a `:relief' property to the image spec if the item is
9004 selected. */
9005 if (selected_p)
9007 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9008 hmargin -= relief;
9009 vmargin -= relief;
9012 else
9014 /* If image is selected, display it pressed, i.e. with a
9015 negative relief. If it's not selected, display it with a
9016 raised relief. */
9017 plist = Fplist_put (plist, QCrelief,
9018 (selected_p
9019 ? make_number (-relief)
9020 : make_number (relief)));
9021 hmargin -= relief;
9022 vmargin -= relief;
9025 /* Put a margin around the image. */
9026 if (hmargin || vmargin)
9028 if (hmargin == vmargin)
9029 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9030 else
9031 plist = Fplist_put (plist, QCmargin,
9032 Fcons (make_number (hmargin),
9033 make_number (vmargin)));
9036 /* If button is not enabled, and we don't have special images
9037 for the disabled state, make the image appear disabled by
9038 applying an appropriate algorithm to it. */
9039 if (!enabled_p && idx < 0)
9040 plist = Fplist_put (plist, QCconversion, Qdisabled);
9042 /* Put a `display' text property on the string for the image to
9043 display. Put a `menu-item' property on the string that gives
9044 the start of this item's properties in the tool-bar items
9045 vector. */
9046 image = Fcons (Qimage, plist);
9047 props = list4 (Qdisplay, image,
9048 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9050 /* Let the last image hide all remaining spaces in the tool bar
9051 string. The string can be longer than needed when we reuse a
9052 previous string. */
9053 if (i + 1 == f->n_tool_bar_items)
9054 end = SCHARS (f->desired_tool_bar_string);
9055 else
9056 end = i + 1;
9057 Fadd_text_properties (make_number (i), make_number (end),
9058 props, f->desired_tool_bar_string);
9059 #undef PROP
9062 UNGCPRO;
9066 /* Display one line of the tool-bar of frame IT->f. */
9068 static void
9069 display_tool_bar_line (it)
9070 struct it *it;
9072 struct glyph_row *row = it->glyph_row;
9073 int max_x = it->last_visible_x;
9074 struct glyph *last;
9076 prepare_desired_row (row);
9077 row->y = it->current_y;
9079 /* Note that this isn't made use of if the face hasn't a box,
9080 so there's no need to check the face here. */
9081 it->start_of_box_run_p = 1;
9083 while (it->current_x < max_x)
9085 int x_before, x, n_glyphs_before, i, nglyphs;
9087 /* Get the next display element. */
9088 if (!get_next_display_element (it))
9089 break;
9091 /* Produce glyphs. */
9092 x_before = it->current_x;
9093 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9094 PRODUCE_GLYPHS (it);
9096 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9097 i = 0;
9098 x = x_before;
9099 while (i < nglyphs)
9101 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9103 if (x + glyph->pixel_width > max_x)
9105 /* Glyph doesn't fit on line. */
9106 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9107 it->current_x = x;
9108 goto out;
9111 ++it->hpos;
9112 x += glyph->pixel_width;
9113 ++i;
9116 /* Stop at line ends. */
9117 if (ITERATOR_AT_END_OF_LINE_P (it))
9118 break;
9120 set_iterator_to_next (it, 1);
9123 out:;
9125 row->displays_text_p = row->used[TEXT_AREA] != 0;
9126 extend_face_to_end_of_line (it);
9127 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9128 last->right_box_line_p = 1;
9129 if (last == row->glyphs[TEXT_AREA])
9130 last->left_box_line_p = 1;
9131 compute_line_metrics (it);
9133 /* If line is empty, make it occupy the rest of the tool-bar. */
9134 if (!row->displays_text_p)
9136 row->height = row->phys_height = it->last_visible_y - row->y;
9137 row->ascent = row->phys_ascent = 0;
9138 row->extra_line_spacing = 0;
9141 row->full_width_p = 1;
9142 row->continued_p = 0;
9143 row->truncated_on_left_p = 0;
9144 row->truncated_on_right_p = 0;
9146 it->current_x = it->hpos = 0;
9147 it->current_y += row->height;
9148 ++it->vpos;
9149 ++it->glyph_row;
9153 /* Value is the number of screen lines needed to make all tool-bar
9154 items of frame F visible. */
9156 static int
9157 tool_bar_lines_needed (f)
9158 struct frame *f;
9160 struct window *w = XWINDOW (f->tool_bar_window);
9161 struct it it;
9163 /* Initialize an iterator for iteration over
9164 F->desired_tool_bar_string in the tool-bar window of frame F. */
9165 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9166 it.first_visible_x = 0;
9167 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9168 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9170 while (!ITERATOR_AT_END_P (&it))
9172 it.glyph_row = w->desired_matrix->rows;
9173 clear_glyph_row (it.glyph_row);
9174 display_tool_bar_line (&it);
9177 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9181 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9182 0, 1, 0,
9183 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9184 (frame)
9185 Lisp_Object frame;
9187 struct frame *f;
9188 struct window *w;
9189 int nlines = 0;
9191 if (NILP (frame))
9192 frame = selected_frame;
9193 else
9194 CHECK_FRAME (frame);
9195 f = XFRAME (frame);
9197 if (WINDOWP (f->tool_bar_window)
9198 || (w = XWINDOW (f->tool_bar_window),
9199 WINDOW_TOTAL_LINES (w) > 0))
9201 update_tool_bar (f, 1);
9202 if (f->n_tool_bar_items)
9204 build_desired_tool_bar_string (f);
9205 nlines = tool_bar_lines_needed (f);
9209 return make_number (nlines);
9213 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9214 height should be changed. */
9216 static int
9217 redisplay_tool_bar (f)
9218 struct frame *f;
9220 struct window *w;
9221 struct it it;
9222 struct glyph_row *row;
9223 int change_height_p = 0;
9225 #ifdef USE_GTK
9226 if (FRAME_EXTERNAL_TOOL_BAR (f))
9227 update_frame_tool_bar (f);
9228 return 0;
9229 #endif
9231 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9232 do anything. This means you must start with tool-bar-lines
9233 non-zero to get the auto-sizing effect. Or in other words, you
9234 can turn off tool-bars by specifying tool-bar-lines zero. */
9235 if (!WINDOWP (f->tool_bar_window)
9236 || (w = XWINDOW (f->tool_bar_window),
9237 WINDOW_TOTAL_LINES (w) == 0))
9238 return 0;
9240 /* Set up an iterator for the tool-bar window. */
9241 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9242 it.first_visible_x = 0;
9243 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9244 row = it.glyph_row;
9246 /* Build a string that represents the contents of the tool-bar. */
9247 build_desired_tool_bar_string (f);
9248 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9250 /* Display as many lines as needed to display all tool-bar items. */
9251 while (it.current_y < it.last_visible_y)
9252 display_tool_bar_line (&it);
9254 /* It doesn't make much sense to try scrolling in the tool-bar
9255 window, so don't do it. */
9256 w->desired_matrix->no_scrolling_p = 1;
9257 w->must_be_updated_p = 1;
9259 if (auto_resize_tool_bars_p)
9261 int nlines;
9263 /* If we couldn't display everything, change the tool-bar's
9264 height. */
9265 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9266 change_height_p = 1;
9268 /* If there are blank lines at the end, except for a partially
9269 visible blank line at the end that is smaller than
9270 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9271 row = it.glyph_row - 1;
9272 if (!row->displays_text_p
9273 && row->height >= FRAME_LINE_HEIGHT (f))
9274 change_height_p = 1;
9276 /* If row displays tool-bar items, but is partially visible,
9277 change the tool-bar's height. */
9278 if (row->displays_text_p
9279 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9280 change_height_p = 1;
9282 /* Resize windows as needed by changing the `tool-bar-lines'
9283 frame parameter. */
9284 if (change_height_p
9285 && (nlines = tool_bar_lines_needed (f),
9286 nlines != WINDOW_TOTAL_LINES (w)))
9288 extern Lisp_Object Qtool_bar_lines;
9289 Lisp_Object frame;
9290 int old_height = WINDOW_TOTAL_LINES (w);
9292 XSETFRAME (frame, f);
9293 clear_glyph_matrix (w->desired_matrix);
9294 Fmodify_frame_parameters (frame,
9295 Fcons (Fcons (Qtool_bar_lines,
9296 make_number (nlines)),
9297 Qnil));
9298 if (WINDOW_TOTAL_LINES (w) != old_height)
9299 fonts_changed_p = 1;
9303 return change_height_p;
9307 /* Get information about the tool-bar item which is displayed in GLYPH
9308 on frame F. Return in *PROP_IDX the index where tool-bar item
9309 properties start in F->tool_bar_items. Value is zero if
9310 GLYPH doesn't display a tool-bar item. */
9312 static int
9313 tool_bar_item_info (f, glyph, prop_idx)
9314 struct frame *f;
9315 struct glyph *glyph;
9316 int *prop_idx;
9318 Lisp_Object prop;
9319 int success_p;
9320 int charpos;
9322 /* This function can be called asynchronously, which means we must
9323 exclude any possibility that Fget_text_property signals an
9324 error. */
9325 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9326 charpos = max (0, charpos);
9328 /* Get the text property `menu-item' at pos. The value of that
9329 property is the start index of this item's properties in
9330 F->tool_bar_items. */
9331 prop = Fget_text_property (make_number (charpos),
9332 Qmenu_item, f->current_tool_bar_string);
9333 if (INTEGERP (prop))
9335 *prop_idx = XINT (prop);
9336 success_p = 1;
9338 else
9339 success_p = 0;
9341 return success_p;
9345 /* Get information about the tool-bar item at position X/Y on frame F.
9346 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9347 the current matrix of the tool-bar window of F, or NULL if not
9348 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9349 item in F->tool_bar_items. Value is
9351 -1 if X/Y is not on a tool-bar item
9352 0 if X/Y is on the same item that was highlighted before.
9353 1 otherwise. */
9355 static int
9356 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9357 struct frame *f;
9358 int x, y;
9359 struct glyph **glyph;
9360 int *hpos, *vpos, *prop_idx;
9362 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9363 struct window *w = XWINDOW (f->tool_bar_window);
9364 int area;
9366 /* Find the glyph under X/Y. */
9367 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9368 if (*glyph == NULL)
9369 return -1;
9371 /* Get the start of this tool-bar item's properties in
9372 f->tool_bar_items. */
9373 if (!tool_bar_item_info (f, *glyph, prop_idx))
9374 return -1;
9376 /* Is mouse on the highlighted item? */
9377 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9378 && *vpos >= dpyinfo->mouse_face_beg_row
9379 && *vpos <= dpyinfo->mouse_face_end_row
9380 && (*vpos > dpyinfo->mouse_face_beg_row
9381 || *hpos >= dpyinfo->mouse_face_beg_col)
9382 && (*vpos < dpyinfo->mouse_face_end_row
9383 || *hpos < dpyinfo->mouse_face_end_col
9384 || dpyinfo->mouse_face_past_end))
9385 return 0;
9387 return 1;
9391 /* EXPORT:
9392 Handle mouse button event on the tool-bar of frame F, at
9393 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9394 0 for button release. MODIFIERS is event modifiers for button
9395 release. */
9397 void
9398 handle_tool_bar_click (f, x, y, down_p, modifiers)
9399 struct frame *f;
9400 int x, y, down_p;
9401 unsigned int modifiers;
9403 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9404 struct window *w = XWINDOW (f->tool_bar_window);
9405 int hpos, vpos, prop_idx;
9406 struct glyph *glyph;
9407 Lisp_Object enabled_p;
9409 /* If not on the highlighted tool-bar item, return. */
9410 frame_to_window_pixel_xy (w, &x, &y);
9411 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9412 return;
9414 /* If item is disabled, do nothing. */
9415 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9416 if (NILP (enabled_p))
9417 return;
9419 if (down_p)
9421 /* Show item in pressed state. */
9422 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9423 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9424 last_tool_bar_item = prop_idx;
9426 else
9428 Lisp_Object key, frame;
9429 struct input_event event;
9430 EVENT_INIT (event);
9432 /* Show item in released state. */
9433 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9434 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9436 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9438 XSETFRAME (frame, f);
9439 event.kind = TOOL_BAR_EVENT;
9440 event.frame_or_window = frame;
9441 event.arg = frame;
9442 kbd_buffer_store_event (&event);
9444 event.kind = TOOL_BAR_EVENT;
9445 event.frame_or_window = frame;
9446 event.arg = key;
9447 event.modifiers = modifiers;
9448 kbd_buffer_store_event (&event);
9449 last_tool_bar_item = -1;
9454 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9455 tool-bar window-relative coordinates X/Y. Called from
9456 note_mouse_highlight. */
9458 static void
9459 note_tool_bar_highlight (f, x, y)
9460 struct frame *f;
9461 int x, y;
9463 Lisp_Object window = f->tool_bar_window;
9464 struct window *w = XWINDOW (window);
9465 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9466 int hpos, vpos;
9467 struct glyph *glyph;
9468 struct glyph_row *row;
9469 int i;
9470 Lisp_Object enabled_p;
9471 int prop_idx;
9472 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9473 int mouse_down_p, rc;
9475 /* Function note_mouse_highlight is called with negative x(y
9476 values when mouse moves outside of the frame. */
9477 if (x <= 0 || y <= 0)
9479 clear_mouse_face (dpyinfo);
9480 return;
9483 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9484 if (rc < 0)
9486 /* Not on tool-bar item. */
9487 clear_mouse_face (dpyinfo);
9488 return;
9490 else if (rc == 0)
9491 /* On same tool-bar item as before. */
9492 goto set_help_echo;
9494 clear_mouse_face (dpyinfo);
9496 /* Mouse is down, but on different tool-bar item? */
9497 mouse_down_p = (dpyinfo->grabbed
9498 && f == last_mouse_frame
9499 && FRAME_LIVE_P (f));
9500 if (mouse_down_p
9501 && last_tool_bar_item != prop_idx)
9502 return;
9504 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9505 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9507 /* If tool-bar item is not enabled, don't highlight it. */
9508 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9509 if (!NILP (enabled_p))
9511 /* Compute the x-position of the glyph. In front and past the
9512 image is a space. We include this in the highlighted area. */
9513 row = MATRIX_ROW (w->current_matrix, vpos);
9514 for (i = x = 0; i < hpos; ++i)
9515 x += row->glyphs[TEXT_AREA][i].pixel_width;
9517 /* Record this as the current active region. */
9518 dpyinfo->mouse_face_beg_col = hpos;
9519 dpyinfo->mouse_face_beg_row = vpos;
9520 dpyinfo->mouse_face_beg_x = x;
9521 dpyinfo->mouse_face_beg_y = row->y;
9522 dpyinfo->mouse_face_past_end = 0;
9524 dpyinfo->mouse_face_end_col = hpos + 1;
9525 dpyinfo->mouse_face_end_row = vpos;
9526 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9527 dpyinfo->mouse_face_end_y = row->y;
9528 dpyinfo->mouse_face_window = window;
9529 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9531 /* Display it as active. */
9532 show_mouse_face (dpyinfo, draw);
9533 dpyinfo->mouse_face_image_state = draw;
9536 set_help_echo:
9538 /* Set help_echo_string to a help string to display for this tool-bar item.
9539 XTread_socket does the rest. */
9540 help_echo_object = help_echo_window = Qnil;
9541 help_echo_pos = -1;
9542 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9543 if (NILP (help_echo_string))
9544 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9547 #endif /* HAVE_WINDOW_SYSTEM */
9551 /************************************************************************
9552 Horizontal scrolling
9553 ************************************************************************/
9555 static int hscroll_window_tree P_ ((Lisp_Object));
9556 static int hscroll_windows P_ ((Lisp_Object));
9558 /* For all leaf windows in the window tree rooted at WINDOW, set their
9559 hscroll value so that PT is (i) visible in the window, and (ii) so
9560 that it is not within a certain margin at the window's left and
9561 right border. Value is non-zero if any window's hscroll has been
9562 changed. */
9564 static int
9565 hscroll_window_tree (window)
9566 Lisp_Object window;
9568 int hscrolled_p = 0;
9569 int hscroll_relative_p = FLOATP (Vhscroll_step);
9570 int hscroll_step_abs = 0;
9571 double hscroll_step_rel = 0;
9573 if (hscroll_relative_p)
9575 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9576 if (hscroll_step_rel < 0)
9578 hscroll_relative_p = 0;
9579 hscroll_step_abs = 0;
9582 else if (INTEGERP (Vhscroll_step))
9584 hscroll_step_abs = XINT (Vhscroll_step);
9585 if (hscroll_step_abs < 0)
9586 hscroll_step_abs = 0;
9588 else
9589 hscroll_step_abs = 0;
9591 while (WINDOWP (window))
9593 struct window *w = XWINDOW (window);
9595 if (WINDOWP (w->hchild))
9596 hscrolled_p |= hscroll_window_tree (w->hchild);
9597 else if (WINDOWP (w->vchild))
9598 hscrolled_p |= hscroll_window_tree (w->vchild);
9599 else if (w->cursor.vpos >= 0)
9601 int h_margin;
9602 int text_area_width;
9603 struct glyph_row *current_cursor_row
9604 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9605 struct glyph_row *desired_cursor_row
9606 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9607 struct glyph_row *cursor_row
9608 = (desired_cursor_row->enabled_p
9609 ? desired_cursor_row
9610 : current_cursor_row);
9612 text_area_width = window_box_width (w, TEXT_AREA);
9614 /* Scroll when cursor is inside this scroll margin. */
9615 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9617 if ((XFASTINT (w->hscroll)
9618 && w->cursor.x <= h_margin)
9619 || (cursor_row->enabled_p
9620 && cursor_row->truncated_on_right_p
9621 && (w->cursor.x >= text_area_width - h_margin)))
9623 struct it it;
9624 int hscroll;
9625 struct buffer *saved_current_buffer;
9626 int pt;
9627 int wanted_x;
9629 /* Find point in a display of infinite width. */
9630 saved_current_buffer = current_buffer;
9631 current_buffer = XBUFFER (w->buffer);
9633 if (w == XWINDOW (selected_window))
9634 pt = BUF_PT (current_buffer);
9635 else
9637 pt = marker_position (w->pointm);
9638 pt = max (BEGV, pt);
9639 pt = min (ZV, pt);
9642 /* Move iterator to pt starting at cursor_row->start in
9643 a line with infinite width. */
9644 init_to_row_start (&it, w, cursor_row);
9645 it.last_visible_x = INFINITY;
9646 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9647 current_buffer = saved_current_buffer;
9649 /* Position cursor in window. */
9650 if (!hscroll_relative_p && hscroll_step_abs == 0)
9651 hscroll = max (0, (it.current_x
9652 - (ITERATOR_AT_END_OF_LINE_P (&it)
9653 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9654 : (text_area_width / 2))))
9655 / FRAME_COLUMN_WIDTH (it.f);
9656 else if (w->cursor.x >= text_area_width - h_margin)
9658 if (hscroll_relative_p)
9659 wanted_x = text_area_width * (1 - hscroll_step_rel)
9660 - h_margin;
9661 else
9662 wanted_x = text_area_width
9663 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9664 - h_margin;
9665 hscroll
9666 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9668 else
9670 if (hscroll_relative_p)
9671 wanted_x = text_area_width * hscroll_step_rel
9672 + h_margin;
9673 else
9674 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9675 + h_margin;
9676 hscroll
9677 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9679 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9681 /* Don't call Fset_window_hscroll if value hasn't
9682 changed because it will prevent redisplay
9683 optimizations. */
9684 if (XFASTINT (w->hscroll) != hscroll)
9686 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9687 w->hscroll = make_number (hscroll);
9688 hscrolled_p = 1;
9693 window = w->next;
9696 /* Value is non-zero if hscroll of any leaf window has been changed. */
9697 return hscrolled_p;
9701 /* Set hscroll so that cursor is visible and not inside horizontal
9702 scroll margins for all windows in the tree rooted at WINDOW. See
9703 also hscroll_window_tree above. Value is non-zero if any window's
9704 hscroll has been changed. If it has, desired matrices on the frame
9705 of WINDOW are cleared. */
9707 static int
9708 hscroll_windows (window)
9709 Lisp_Object window;
9711 int hscrolled_p;
9713 if (automatic_hscrolling_p)
9715 hscrolled_p = hscroll_window_tree (window);
9716 if (hscrolled_p)
9717 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9719 else
9720 hscrolled_p = 0;
9721 return hscrolled_p;
9726 /************************************************************************
9727 Redisplay
9728 ************************************************************************/
9730 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9731 to a non-zero value. This is sometimes handy to have in a debugger
9732 session. */
9734 #if GLYPH_DEBUG
9736 /* First and last unchanged row for try_window_id. */
9738 int debug_first_unchanged_at_end_vpos;
9739 int debug_last_unchanged_at_beg_vpos;
9741 /* Delta vpos and y. */
9743 int debug_dvpos, debug_dy;
9745 /* Delta in characters and bytes for try_window_id. */
9747 int debug_delta, debug_delta_bytes;
9749 /* Values of window_end_pos and window_end_vpos at the end of
9750 try_window_id. */
9752 EMACS_INT debug_end_pos, debug_end_vpos;
9754 /* Append a string to W->desired_matrix->method. FMT is a printf
9755 format string. A1...A9 are a supplement for a variable-length
9756 argument list. If trace_redisplay_p is non-zero also printf the
9757 resulting string to stderr. */
9759 static void
9760 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9761 struct window *w;
9762 char *fmt;
9763 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9765 char buffer[512];
9766 char *method = w->desired_matrix->method;
9767 int len = strlen (method);
9768 int size = sizeof w->desired_matrix->method;
9769 int remaining = size - len - 1;
9771 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9772 if (len && remaining)
9774 method[len] = '|';
9775 --remaining, ++len;
9778 strncpy (method + len, buffer, remaining);
9780 if (trace_redisplay_p)
9781 fprintf (stderr, "%p (%s): %s\n",
9783 ((BUFFERP (w->buffer)
9784 && STRINGP (XBUFFER (w->buffer)->name))
9785 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9786 : "no buffer"),
9787 buffer);
9790 #endif /* GLYPH_DEBUG */
9793 /* Value is non-zero if all changes in window W, which displays
9794 current_buffer, are in the text between START and END. START is a
9795 buffer position, END is given as a distance from Z. Used in
9796 redisplay_internal for display optimization. */
9798 static INLINE int
9799 text_outside_line_unchanged_p (w, start, end)
9800 struct window *w;
9801 int start, end;
9803 int unchanged_p = 1;
9805 /* If text or overlays have changed, see where. */
9806 if (XFASTINT (w->last_modified) < MODIFF
9807 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9809 /* Gap in the line? */
9810 if (GPT < start || Z - GPT < end)
9811 unchanged_p = 0;
9813 /* Changes start in front of the line, or end after it? */
9814 if (unchanged_p
9815 && (BEG_UNCHANGED < start - 1
9816 || END_UNCHANGED < end))
9817 unchanged_p = 0;
9819 /* If selective display, can't optimize if changes start at the
9820 beginning of the line. */
9821 if (unchanged_p
9822 && INTEGERP (current_buffer->selective_display)
9823 && XINT (current_buffer->selective_display) > 0
9824 && (BEG_UNCHANGED < start || GPT <= start))
9825 unchanged_p = 0;
9827 /* If there are overlays at the start or end of the line, these
9828 may have overlay strings with newlines in them. A change at
9829 START, for instance, may actually concern the display of such
9830 overlay strings as well, and they are displayed on different
9831 lines. So, quickly rule out this case. (For the future, it
9832 might be desirable to implement something more telling than
9833 just BEG/END_UNCHANGED.) */
9834 if (unchanged_p)
9836 if (BEG + BEG_UNCHANGED == start
9837 && overlay_touches_p (start))
9838 unchanged_p = 0;
9839 if (END_UNCHANGED == end
9840 && overlay_touches_p (Z - end))
9841 unchanged_p = 0;
9845 return unchanged_p;
9849 /* Do a frame update, taking possible shortcuts into account. This is
9850 the main external entry point for redisplay.
9852 If the last redisplay displayed an echo area message and that message
9853 is no longer requested, we clear the echo area or bring back the
9854 mini-buffer if that is in use. */
9856 void
9857 redisplay ()
9859 redisplay_internal (0);
9863 static Lisp_Object
9864 overlay_arrow_string_or_property (var)
9865 Lisp_Object var;
9867 Lisp_Object val;
9869 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9870 return val;
9872 return Voverlay_arrow_string;
9875 /* Return 1 if there are any overlay-arrows in current_buffer. */
9876 static int
9877 overlay_arrow_in_current_buffer_p ()
9879 Lisp_Object vlist;
9881 for (vlist = Voverlay_arrow_variable_list;
9882 CONSP (vlist);
9883 vlist = XCDR (vlist))
9885 Lisp_Object var = XCAR (vlist);
9886 Lisp_Object val;
9888 if (!SYMBOLP (var))
9889 continue;
9890 val = find_symbol_value (var);
9891 if (MARKERP (val)
9892 && current_buffer == XMARKER (val)->buffer)
9893 return 1;
9895 return 0;
9899 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9900 has changed. */
9902 static int
9903 overlay_arrows_changed_p ()
9905 Lisp_Object vlist;
9907 for (vlist = Voverlay_arrow_variable_list;
9908 CONSP (vlist);
9909 vlist = XCDR (vlist))
9911 Lisp_Object var = XCAR (vlist);
9912 Lisp_Object val, pstr;
9914 if (!SYMBOLP (var))
9915 continue;
9916 val = find_symbol_value (var);
9917 if (!MARKERP (val))
9918 continue;
9919 if (! EQ (COERCE_MARKER (val),
9920 Fget (var, Qlast_arrow_position))
9921 || ! (pstr = overlay_arrow_string_or_property (var),
9922 EQ (pstr, Fget (var, Qlast_arrow_string))))
9923 return 1;
9925 return 0;
9928 /* Mark overlay arrows to be updated on next redisplay. */
9930 static void
9931 update_overlay_arrows (up_to_date)
9932 int up_to_date;
9934 Lisp_Object vlist;
9936 for (vlist = Voverlay_arrow_variable_list;
9937 CONSP (vlist);
9938 vlist = XCDR (vlist))
9940 Lisp_Object var = XCAR (vlist);
9942 if (!SYMBOLP (var))
9943 continue;
9945 if (up_to_date > 0)
9947 Lisp_Object val = find_symbol_value (var);
9948 Fput (var, Qlast_arrow_position,
9949 COERCE_MARKER (val));
9950 Fput (var, Qlast_arrow_string,
9951 overlay_arrow_string_or_property (var));
9953 else if (up_to_date < 0
9954 || !NILP (Fget (var, Qlast_arrow_position)))
9956 Fput (var, Qlast_arrow_position, Qt);
9957 Fput (var, Qlast_arrow_string, Qt);
9963 /* Return overlay arrow string to display at row.
9964 Return integer (bitmap number) for arrow bitmap in left fringe.
9965 Return nil if no overlay arrow. */
9967 static Lisp_Object
9968 overlay_arrow_at_row (it, row)
9969 struct it *it;
9970 struct glyph_row *row;
9972 Lisp_Object vlist;
9974 for (vlist = Voverlay_arrow_variable_list;
9975 CONSP (vlist);
9976 vlist = XCDR (vlist))
9978 Lisp_Object var = XCAR (vlist);
9979 Lisp_Object val;
9981 if (!SYMBOLP (var))
9982 continue;
9984 val = find_symbol_value (var);
9986 if (MARKERP (val)
9987 && current_buffer == XMARKER (val)->buffer
9988 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9990 if (FRAME_WINDOW_P (it->f)
9991 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9993 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9995 int fringe_bitmap;
9996 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9997 return make_number (fringe_bitmap);
9999 return make_number (-1); /* Use default arrow bitmap */
10001 return overlay_arrow_string_or_property (var);
10005 return Qnil;
10008 /* Return 1 if point moved out of or into a composition. Otherwise
10009 return 0. PREV_BUF and PREV_PT are the last point buffer and
10010 position. BUF and PT are the current point buffer and position. */
10013 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10014 struct buffer *prev_buf, *buf;
10015 int prev_pt, pt;
10017 int start, end;
10018 Lisp_Object prop;
10019 Lisp_Object buffer;
10021 XSETBUFFER (buffer, buf);
10022 /* Check a composition at the last point if point moved within the
10023 same buffer. */
10024 if (prev_buf == buf)
10026 if (prev_pt == pt)
10027 /* Point didn't move. */
10028 return 0;
10030 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10031 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10032 && COMPOSITION_VALID_P (start, end, prop)
10033 && start < prev_pt && end > prev_pt)
10034 /* The last point was within the composition. Return 1 iff
10035 point moved out of the composition. */
10036 return (pt <= start || pt >= end);
10039 /* Check a composition at the current point. */
10040 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10041 && find_composition (pt, -1, &start, &end, &prop, buffer)
10042 && COMPOSITION_VALID_P (start, end, prop)
10043 && start < pt && end > pt);
10047 /* Reconsider the setting of B->clip_changed which is displayed
10048 in window W. */
10050 static INLINE void
10051 reconsider_clip_changes (w, b)
10052 struct window *w;
10053 struct buffer *b;
10055 if (b->clip_changed
10056 && !NILP (w->window_end_valid)
10057 && w->current_matrix->buffer == b
10058 && w->current_matrix->zv == BUF_ZV (b)
10059 && w->current_matrix->begv == BUF_BEGV (b))
10060 b->clip_changed = 0;
10062 /* If display wasn't paused, and W is not a tool bar window, see if
10063 point has been moved into or out of a composition. In that case,
10064 we set b->clip_changed to 1 to force updating the screen. If
10065 b->clip_changed has already been set to 1, we can skip this
10066 check. */
10067 if (!b->clip_changed
10068 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10070 int pt;
10072 if (w == XWINDOW (selected_window))
10073 pt = BUF_PT (current_buffer);
10074 else
10075 pt = marker_position (w->pointm);
10077 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10078 || pt != XINT (w->last_point))
10079 && check_point_in_composition (w->current_matrix->buffer,
10080 XINT (w->last_point),
10081 XBUFFER (w->buffer), pt))
10082 b->clip_changed = 1;
10087 /* Select FRAME to forward the values of frame-local variables into C
10088 variables so that the redisplay routines can access those values
10089 directly. */
10091 static void
10092 select_frame_for_redisplay (frame)
10093 Lisp_Object frame;
10095 Lisp_Object tail, sym, val;
10096 Lisp_Object old = selected_frame;
10098 selected_frame = frame;
10100 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10101 if (CONSP (XCAR (tail))
10102 && (sym = XCAR (XCAR (tail)),
10103 SYMBOLP (sym))
10104 && (sym = indirect_variable (sym),
10105 val = SYMBOL_VALUE (sym),
10106 (BUFFER_LOCAL_VALUEP (val)
10107 || SOME_BUFFER_LOCAL_VALUEP (val)))
10108 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10109 /* Use find_symbol_value rather than Fsymbol_value
10110 to avoid an error if it is void. */
10111 find_symbol_value (sym);
10113 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10114 if (CONSP (XCAR (tail))
10115 && (sym = XCAR (XCAR (tail)),
10116 SYMBOLP (sym))
10117 && (sym = indirect_variable (sym),
10118 val = SYMBOL_VALUE (sym),
10119 (BUFFER_LOCAL_VALUEP (val)
10120 || SOME_BUFFER_LOCAL_VALUEP (val)))
10121 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10122 find_symbol_value (sym);
10126 #define STOP_POLLING \
10127 do { if (! polling_stopped_here) stop_polling (); \
10128 polling_stopped_here = 1; } while (0)
10130 #define RESUME_POLLING \
10131 do { if (polling_stopped_here) start_polling (); \
10132 polling_stopped_here = 0; } while (0)
10135 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10136 response to any user action; therefore, we should preserve the echo
10137 area. (Actually, our caller does that job.) Perhaps in the future
10138 avoid recentering windows if it is not necessary; currently that
10139 causes some problems. */
10141 static void
10142 redisplay_internal (preserve_echo_area)
10143 int preserve_echo_area;
10145 struct window *w = XWINDOW (selected_window);
10146 struct frame *f = XFRAME (w->frame);
10147 int pause;
10148 int must_finish = 0;
10149 struct text_pos tlbufpos, tlendpos;
10150 int number_of_visible_frames;
10151 int count;
10152 struct frame *sf = SELECTED_FRAME ();
10153 int polling_stopped_here = 0;
10155 /* Non-zero means redisplay has to consider all windows on all
10156 frames. Zero means, only selected_window is considered. */
10157 int consider_all_windows_p;
10159 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10161 /* No redisplay if running in batch mode or frame is not yet fully
10162 initialized, or redisplay is explicitly turned off by setting
10163 Vinhibit_redisplay. */
10164 if (noninteractive
10165 || !NILP (Vinhibit_redisplay)
10166 || !f->glyphs_initialized_p)
10167 return;
10169 /* The flag redisplay_performed_directly_p is set by
10170 direct_output_for_insert when it already did the whole screen
10171 update necessary. */
10172 if (redisplay_performed_directly_p)
10174 redisplay_performed_directly_p = 0;
10175 if (!hscroll_windows (selected_window))
10176 return;
10179 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10180 if (popup_activated ())
10181 return;
10182 #endif
10184 /* I don't think this happens but let's be paranoid. */
10185 if (redisplaying_p)
10186 return;
10188 /* Record a function that resets redisplaying_p to its old value
10189 when we leave this function. */
10190 count = SPECPDL_INDEX ();
10191 record_unwind_protect (unwind_redisplay,
10192 Fcons (make_number (redisplaying_p), selected_frame));
10193 ++redisplaying_p;
10194 specbind (Qinhibit_free_realized_faces, Qnil);
10197 Lisp_Object tail, frame;
10199 FOR_EACH_FRAME (tail, frame)
10201 struct frame *f = XFRAME (frame);
10202 f->already_hscrolled_p = 0;
10206 retry:
10207 pause = 0;
10208 reconsider_clip_changes (w, current_buffer);
10210 /* If new fonts have been loaded that make a glyph matrix adjustment
10211 necessary, do it. */
10212 if (fonts_changed_p)
10214 adjust_glyphs (NULL);
10215 ++windows_or_buffers_changed;
10216 fonts_changed_p = 0;
10219 /* If face_change_count is non-zero, init_iterator will free all
10220 realized faces, which includes the faces referenced from current
10221 matrices. So, we can't reuse current matrices in this case. */
10222 if (face_change_count)
10223 ++windows_or_buffers_changed;
10225 if (! FRAME_WINDOW_P (sf)
10226 && previous_terminal_frame != sf)
10228 /* Since frames on an ASCII terminal share the same display
10229 area, displaying a different frame means redisplay the whole
10230 thing. */
10231 windows_or_buffers_changed++;
10232 SET_FRAME_GARBAGED (sf);
10233 XSETFRAME (Vterminal_frame, sf);
10235 previous_terminal_frame = sf;
10237 /* Set the visible flags for all frames. Do this before checking
10238 for resized or garbaged frames; they want to know if their frames
10239 are visible. See the comment in frame.h for
10240 FRAME_SAMPLE_VISIBILITY. */
10242 Lisp_Object tail, frame;
10244 number_of_visible_frames = 0;
10246 FOR_EACH_FRAME (tail, frame)
10248 struct frame *f = XFRAME (frame);
10250 FRAME_SAMPLE_VISIBILITY (f);
10251 if (FRAME_VISIBLE_P (f))
10252 ++number_of_visible_frames;
10253 clear_desired_matrices (f);
10257 /* Notice any pending interrupt request to change frame size. */
10258 do_pending_window_change (1);
10260 /* Clear frames marked as garbaged. */
10261 if (frame_garbaged)
10262 clear_garbaged_frames ();
10264 /* Build menubar and tool-bar items. */
10265 prepare_menu_bars ();
10267 if (windows_or_buffers_changed)
10268 update_mode_lines++;
10270 /* Detect case that we need to write or remove a star in the mode line. */
10271 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10273 w->update_mode_line = Qt;
10274 if (buffer_shared > 1)
10275 update_mode_lines++;
10278 /* If %c is in the mode line, update it if needed. */
10279 if (!NILP (w->column_number_displayed)
10280 /* This alternative quickly identifies a common case
10281 where no change is needed. */
10282 && !(PT == XFASTINT (w->last_point)
10283 && XFASTINT (w->last_modified) >= MODIFF
10284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10285 && (XFASTINT (w->column_number_displayed)
10286 != (int) current_column ())) /* iftc */
10287 w->update_mode_line = Qt;
10289 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10291 /* The variable buffer_shared is set in redisplay_window and
10292 indicates that we redisplay a buffer in different windows. See
10293 there. */
10294 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10295 || cursor_type_changed);
10297 /* If specs for an arrow have changed, do thorough redisplay
10298 to ensure we remove any arrow that should no longer exist. */
10299 if (overlay_arrows_changed_p ())
10300 consider_all_windows_p = windows_or_buffers_changed = 1;
10302 /* Normally the message* functions will have already displayed and
10303 updated the echo area, but the frame may have been trashed, or
10304 the update may have been preempted, so display the echo area
10305 again here. Checking message_cleared_p captures the case that
10306 the echo area should be cleared. */
10307 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10308 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10309 || (message_cleared_p
10310 && minibuf_level == 0
10311 /* If the mini-window is currently selected, this means the
10312 echo-area doesn't show through. */
10313 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10315 int window_height_changed_p = echo_area_display (0);
10316 must_finish = 1;
10318 /* If we don't display the current message, don't clear the
10319 message_cleared_p flag, because, if we did, we wouldn't clear
10320 the echo area in the next redisplay which doesn't preserve
10321 the echo area. */
10322 if (!display_last_displayed_message_p)
10323 message_cleared_p = 0;
10325 if (fonts_changed_p)
10326 goto retry;
10327 else if (window_height_changed_p)
10329 consider_all_windows_p = 1;
10330 ++update_mode_lines;
10331 ++windows_or_buffers_changed;
10333 /* If window configuration was changed, frames may have been
10334 marked garbaged. Clear them or we will experience
10335 surprises wrt scrolling. */
10336 if (frame_garbaged)
10337 clear_garbaged_frames ();
10340 else if (EQ (selected_window, minibuf_window)
10341 && (current_buffer->clip_changed
10342 || XFASTINT (w->last_modified) < MODIFF
10343 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10344 && resize_mini_window (w, 0))
10346 /* Resized active mini-window to fit the size of what it is
10347 showing if its contents might have changed. */
10348 must_finish = 1;
10349 consider_all_windows_p = 1;
10350 ++windows_or_buffers_changed;
10351 ++update_mode_lines;
10353 /* If window configuration was changed, frames may have been
10354 marked garbaged. Clear them or we will experience
10355 surprises wrt scrolling. */
10356 if (frame_garbaged)
10357 clear_garbaged_frames ();
10361 /* If showing the region, and mark has changed, we must redisplay
10362 the whole window. The assignment to this_line_start_pos prevents
10363 the optimization directly below this if-statement. */
10364 if (((!NILP (Vtransient_mark_mode)
10365 && !NILP (XBUFFER (w->buffer)->mark_active))
10366 != !NILP (w->region_showing))
10367 || (!NILP (w->region_showing)
10368 && !EQ (w->region_showing,
10369 Fmarker_position (XBUFFER (w->buffer)->mark))))
10370 CHARPOS (this_line_start_pos) = 0;
10372 /* Optimize the case that only the line containing the cursor in the
10373 selected window has changed. Variables starting with this_ are
10374 set in display_line and record information about the line
10375 containing the cursor. */
10376 tlbufpos = this_line_start_pos;
10377 tlendpos = this_line_end_pos;
10378 if (!consider_all_windows_p
10379 && CHARPOS (tlbufpos) > 0
10380 && NILP (w->update_mode_line)
10381 && !current_buffer->clip_changed
10382 && !current_buffer->prevent_redisplay_optimizations_p
10383 && FRAME_VISIBLE_P (XFRAME (w->frame))
10384 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10385 /* Make sure recorded data applies to current buffer, etc. */
10386 && this_line_buffer == current_buffer
10387 && current_buffer == XBUFFER (w->buffer)
10388 && NILP (w->force_start)
10389 && NILP (w->optional_new_start)
10390 /* Point must be on the line that we have info recorded about. */
10391 && PT >= CHARPOS (tlbufpos)
10392 && PT <= Z - CHARPOS (tlendpos)
10393 /* All text outside that line, including its final newline,
10394 must be unchanged */
10395 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10396 CHARPOS (tlendpos)))
10398 if (CHARPOS (tlbufpos) > BEGV
10399 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10400 && (CHARPOS (tlbufpos) == ZV
10401 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10402 /* Former continuation line has disappeared by becoming empty */
10403 goto cancel;
10404 else if (XFASTINT (w->last_modified) < MODIFF
10405 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10406 || MINI_WINDOW_P (w))
10408 /* We have to handle the case of continuation around a
10409 wide-column character (See the comment in indent.c around
10410 line 885).
10412 For instance, in the following case:
10414 -------- Insert --------
10415 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10416 J_I_ ==> J_I_ `^^' are cursors.
10417 ^^ ^^
10418 -------- --------
10420 As we have to redraw the line above, we should goto cancel. */
10422 struct it it;
10423 int line_height_before = this_line_pixel_height;
10425 /* Note that start_display will handle the case that the
10426 line starting at tlbufpos is a continuation lines. */
10427 start_display (&it, w, tlbufpos);
10429 /* Implementation note: It this still necessary? */
10430 if (it.current_x != this_line_start_x)
10431 goto cancel;
10433 TRACE ((stderr, "trying display optimization 1\n"));
10434 w->cursor.vpos = -1;
10435 overlay_arrow_seen = 0;
10436 it.vpos = this_line_vpos;
10437 it.current_y = this_line_y;
10438 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10439 display_line (&it);
10441 /* If line contains point, is not continued,
10442 and ends at same distance from eob as before, we win */
10443 if (w->cursor.vpos >= 0
10444 /* Line is not continued, otherwise this_line_start_pos
10445 would have been set to 0 in display_line. */
10446 && CHARPOS (this_line_start_pos)
10447 /* Line ends as before. */
10448 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10449 /* Line has same height as before. Otherwise other lines
10450 would have to be shifted up or down. */
10451 && this_line_pixel_height == line_height_before)
10453 /* If this is not the window's last line, we must adjust
10454 the charstarts of the lines below. */
10455 if (it.current_y < it.last_visible_y)
10457 struct glyph_row *row
10458 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10459 int delta, delta_bytes;
10461 if (Z - CHARPOS (tlendpos) == ZV)
10463 /* This line ends at end of (accessible part of)
10464 buffer. There is no newline to count. */
10465 delta = (Z
10466 - CHARPOS (tlendpos)
10467 - MATRIX_ROW_START_CHARPOS (row));
10468 delta_bytes = (Z_BYTE
10469 - BYTEPOS (tlendpos)
10470 - MATRIX_ROW_START_BYTEPOS (row));
10472 else
10474 /* This line ends in a newline. Must take
10475 account of the newline and the rest of the
10476 text that follows. */
10477 delta = (Z
10478 - CHARPOS (tlendpos)
10479 - MATRIX_ROW_START_CHARPOS (row));
10480 delta_bytes = (Z_BYTE
10481 - BYTEPOS (tlendpos)
10482 - MATRIX_ROW_START_BYTEPOS (row));
10485 increment_matrix_positions (w->current_matrix,
10486 this_line_vpos + 1,
10487 w->current_matrix->nrows,
10488 delta, delta_bytes);
10491 /* If this row displays text now but previously didn't,
10492 or vice versa, w->window_end_vpos may have to be
10493 adjusted. */
10494 if ((it.glyph_row - 1)->displays_text_p)
10496 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10497 XSETINT (w->window_end_vpos, this_line_vpos);
10499 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10500 && this_line_vpos > 0)
10501 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10502 w->window_end_valid = Qnil;
10504 /* Update hint: No need to try to scroll in update_window. */
10505 w->desired_matrix->no_scrolling_p = 1;
10507 #if GLYPH_DEBUG
10508 *w->desired_matrix->method = 0;
10509 debug_method_add (w, "optimization 1");
10510 #endif
10511 #ifdef HAVE_WINDOW_SYSTEM
10512 update_window_fringes (w, 0);
10513 #endif
10514 goto update;
10516 else
10517 goto cancel;
10519 else if (/* Cursor position hasn't changed. */
10520 PT == XFASTINT (w->last_point)
10521 /* Make sure the cursor was last displayed
10522 in this window. Otherwise we have to reposition it. */
10523 && 0 <= w->cursor.vpos
10524 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10526 if (!must_finish)
10528 do_pending_window_change (1);
10530 /* We used to always goto end_of_redisplay here, but this
10531 isn't enough if we have a blinking cursor. */
10532 if (w->cursor_off_p == w->last_cursor_off_p)
10533 goto end_of_redisplay;
10535 goto update;
10537 /* If highlighting the region, or if the cursor is in the echo area,
10538 then we can't just move the cursor. */
10539 else if (! (!NILP (Vtransient_mark_mode)
10540 && !NILP (current_buffer->mark_active))
10541 && (EQ (selected_window, current_buffer->last_selected_window)
10542 || highlight_nonselected_windows)
10543 && NILP (w->region_showing)
10544 && NILP (Vshow_trailing_whitespace)
10545 && !cursor_in_echo_area)
10547 struct it it;
10548 struct glyph_row *row;
10550 /* Skip from tlbufpos to PT and see where it is. Note that
10551 PT may be in invisible text. If so, we will end at the
10552 next visible position. */
10553 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10554 NULL, DEFAULT_FACE_ID);
10555 it.current_x = this_line_start_x;
10556 it.current_y = this_line_y;
10557 it.vpos = this_line_vpos;
10559 /* The call to move_it_to stops in front of PT, but
10560 moves over before-strings. */
10561 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10563 if (it.vpos == this_line_vpos
10564 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10565 row->enabled_p))
10567 xassert (this_line_vpos == it.vpos);
10568 xassert (this_line_y == it.current_y);
10569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10570 #if GLYPH_DEBUG
10571 *w->desired_matrix->method = 0;
10572 debug_method_add (w, "optimization 3");
10573 #endif
10574 goto update;
10576 else
10577 goto cancel;
10580 cancel:
10581 /* Text changed drastically or point moved off of line. */
10582 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10585 CHARPOS (this_line_start_pos) = 0;
10586 consider_all_windows_p |= buffer_shared > 1;
10587 ++clear_face_cache_count;
10588 #ifdef HAVE_WINDOW_SYSTEM
10589 ++clear_image_cache_count;
10590 #endif
10592 /* Build desired matrices, and update the display. If
10593 consider_all_windows_p is non-zero, do it for all windows on all
10594 frames. Otherwise do it for selected_window, only. */
10596 if (consider_all_windows_p)
10598 Lisp_Object tail, frame;
10599 int i, n = 0, size = 50;
10600 struct frame **updated
10601 = (struct frame **) alloca (size * sizeof *updated);
10603 /* Recompute # windows showing selected buffer. This will be
10604 incremented each time such a window is displayed. */
10605 buffer_shared = 0;
10607 FOR_EACH_FRAME (tail, frame)
10609 struct frame *f = XFRAME (frame);
10611 if (FRAME_WINDOW_P (f) || f == sf)
10613 if (! EQ (frame, selected_frame))
10614 /* Select the frame, for the sake of frame-local
10615 variables. */
10616 select_frame_for_redisplay (frame);
10618 /* Mark all the scroll bars to be removed; we'll redeem
10619 the ones we want when we redisplay their windows. */
10620 if (condemn_scroll_bars_hook)
10621 condemn_scroll_bars_hook (f);
10623 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10624 redisplay_windows (FRAME_ROOT_WINDOW (f));
10626 /* Any scroll bars which redisplay_windows should have
10627 nuked should now go away. */
10628 if (judge_scroll_bars_hook)
10629 judge_scroll_bars_hook (f);
10631 /* If fonts changed, display again. */
10632 /* ??? rms: I suspect it is a mistake to jump all the way
10633 back to retry here. It should just retry this frame. */
10634 if (fonts_changed_p)
10635 goto retry;
10637 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10639 /* See if we have to hscroll. */
10640 if (!f->already_hscrolled_p)
10642 f->already_hscrolled_p = 1;
10643 if (hscroll_windows (f->root_window))
10644 goto retry;
10647 /* Prevent various kinds of signals during display
10648 update. stdio is not robust about handling
10649 signals, which can cause an apparent I/O
10650 error. */
10651 if (interrupt_input)
10652 unrequest_sigio ();
10653 STOP_POLLING;
10655 /* Update the display. */
10656 set_window_update_flags (XWINDOW (f->root_window), 1);
10657 pause |= update_frame (f, 0, 0);
10658 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10659 if (pause)
10660 break;
10661 #endif
10663 if (n == size)
10665 int nbytes = size * sizeof *updated;
10666 struct frame **p = (struct frame **) alloca (2 * nbytes);
10667 bcopy (updated, p, nbytes);
10668 size *= 2;
10671 updated[n++] = f;
10676 if (!pause)
10678 /* Do the mark_window_display_accurate after all windows have
10679 been redisplayed because this call resets flags in buffers
10680 which are needed for proper redisplay. */
10681 for (i = 0; i < n; ++i)
10683 struct frame *f = updated[i];
10684 mark_window_display_accurate (f->root_window, 1);
10685 if (frame_up_to_date_hook)
10686 frame_up_to_date_hook (f);
10690 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10692 Lisp_Object mini_window;
10693 struct frame *mini_frame;
10695 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10696 /* Use list_of_error, not Qerror, so that
10697 we catch only errors and don't run the debugger. */
10698 internal_condition_case_1 (redisplay_window_1, selected_window,
10699 list_of_error,
10700 redisplay_window_error);
10702 /* Compare desired and current matrices, perform output. */
10704 update:
10705 /* If fonts changed, display again. */
10706 if (fonts_changed_p)
10707 goto retry;
10709 /* Prevent various kinds of signals during display update.
10710 stdio is not robust about handling signals,
10711 which can cause an apparent I/O error. */
10712 if (interrupt_input)
10713 unrequest_sigio ();
10714 STOP_POLLING;
10716 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10718 if (hscroll_windows (selected_window))
10719 goto retry;
10721 XWINDOW (selected_window)->must_be_updated_p = 1;
10722 pause = update_frame (sf, 0, 0);
10725 /* We may have called echo_area_display at the top of this
10726 function. If the echo area is on another frame, that may
10727 have put text on a frame other than the selected one, so the
10728 above call to update_frame would not have caught it. Catch
10729 it here. */
10730 mini_window = FRAME_MINIBUF_WINDOW (sf);
10731 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10733 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10735 XWINDOW (mini_window)->must_be_updated_p = 1;
10736 pause |= update_frame (mini_frame, 0, 0);
10737 if (!pause && hscroll_windows (mini_window))
10738 goto retry;
10742 /* If display was paused because of pending input, make sure we do a
10743 thorough update the next time. */
10744 if (pause)
10746 /* Prevent the optimization at the beginning of
10747 redisplay_internal that tries a single-line update of the
10748 line containing the cursor in the selected window. */
10749 CHARPOS (this_line_start_pos) = 0;
10751 /* Let the overlay arrow be updated the next time. */
10752 update_overlay_arrows (0);
10754 /* If we pause after scrolling, some rows in the current
10755 matrices of some windows are not valid. */
10756 if (!WINDOW_FULL_WIDTH_P (w)
10757 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10758 update_mode_lines = 1;
10760 else
10762 if (!consider_all_windows_p)
10764 /* This has already been done above if
10765 consider_all_windows_p is set. */
10766 mark_window_display_accurate_1 (w, 1);
10768 /* Say overlay arrows are up to date. */
10769 update_overlay_arrows (1);
10771 if (frame_up_to_date_hook != 0)
10772 frame_up_to_date_hook (sf);
10775 update_mode_lines = 0;
10776 windows_or_buffers_changed = 0;
10777 cursor_type_changed = 0;
10780 /* Start SIGIO interrupts coming again. Having them off during the
10781 code above makes it less likely one will discard output, but not
10782 impossible, since there might be stuff in the system buffer here.
10783 But it is much hairier to try to do anything about that. */
10784 if (interrupt_input)
10785 request_sigio ();
10786 RESUME_POLLING;
10788 /* If a frame has become visible which was not before, redisplay
10789 again, so that we display it. Expose events for such a frame
10790 (which it gets when becoming visible) don't call the parts of
10791 redisplay constructing glyphs, so simply exposing a frame won't
10792 display anything in this case. So, we have to display these
10793 frames here explicitly. */
10794 if (!pause)
10796 Lisp_Object tail, frame;
10797 int new_count = 0;
10799 FOR_EACH_FRAME (tail, frame)
10801 int this_is_visible = 0;
10803 if (XFRAME (frame)->visible)
10804 this_is_visible = 1;
10805 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10806 if (XFRAME (frame)->visible)
10807 this_is_visible = 1;
10809 if (this_is_visible)
10810 new_count++;
10813 if (new_count != number_of_visible_frames)
10814 windows_or_buffers_changed++;
10817 /* Change frame size now if a change is pending. */
10818 do_pending_window_change (1);
10820 /* If we just did a pending size change, or have additional
10821 visible frames, redisplay again. */
10822 if (windows_or_buffers_changed && !pause)
10823 goto retry;
10825 /* Clear the face cache eventually. */
10826 if (consider_all_windows_p)
10828 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10830 clear_face_cache (0);
10831 clear_face_cache_count = 0;
10833 #ifdef HAVE_WINDOW_SYSTEM
10834 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10836 Lisp_Object tail, frame;
10837 FOR_EACH_FRAME (tail, frame)
10839 struct frame *f = XFRAME (frame);
10840 if (FRAME_WINDOW_P (f))
10841 clear_image_cache (f, 0);
10843 clear_image_cache_count = 0;
10845 #endif /* HAVE_WINDOW_SYSTEM */
10848 end_of_redisplay:
10849 unbind_to (count, Qnil);
10850 RESUME_POLLING;
10854 /* Redisplay, but leave alone any recent echo area message unless
10855 another message has been requested in its place.
10857 This is useful in situations where you need to redisplay but no
10858 user action has occurred, making it inappropriate for the message
10859 area to be cleared. See tracking_off and
10860 wait_reading_process_output for examples of these situations.
10862 FROM_WHERE is an integer saying from where this function was
10863 called. This is useful for debugging. */
10865 void
10866 redisplay_preserve_echo_area (from_where)
10867 int from_where;
10869 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10871 if (!NILP (echo_area_buffer[1]))
10873 /* We have a previously displayed message, but no current
10874 message. Redisplay the previous message. */
10875 display_last_displayed_message_p = 1;
10876 redisplay_internal (1);
10877 display_last_displayed_message_p = 0;
10879 else
10880 redisplay_internal (1);
10882 if (rif != NULL && rif->flush_display_optional)
10883 rif->flush_display_optional (NULL);
10887 /* Function registered with record_unwind_protect in
10888 redisplay_internal. Reset redisplaying_p to the value it had
10889 before redisplay_internal was called, and clear
10890 prevent_freeing_realized_faces_p. It also selects the previously
10891 selected frame. */
10893 static Lisp_Object
10894 unwind_redisplay (val)
10895 Lisp_Object val;
10897 Lisp_Object old_redisplaying_p, old_frame;
10899 old_redisplaying_p = XCAR (val);
10900 redisplaying_p = XFASTINT (old_redisplaying_p);
10901 old_frame = XCDR (val);
10902 if (! EQ (old_frame, selected_frame))
10903 select_frame_for_redisplay (old_frame);
10904 return Qnil;
10908 /* Mark the display of window W as accurate or inaccurate. If
10909 ACCURATE_P is non-zero mark display of W as accurate. If
10910 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10911 redisplay_internal is called. */
10913 static void
10914 mark_window_display_accurate_1 (w, accurate_p)
10915 struct window *w;
10916 int accurate_p;
10918 if (BUFFERP (w->buffer))
10920 struct buffer *b = XBUFFER (w->buffer);
10922 w->last_modified
10923 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10924 w->last_overlay_modified
10925 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10926 w->last_had_star
10927 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10929 if (accurate_p)
10931 b->clip_changed = 0;
10932 b->prevent_redisplay_optimizations_p = 0;
10934 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10935 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10936 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10937 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10939 w->current_matrix->buffer = b;
10940 w->current_matrix->begv = BUF_BEGV (b);
10941 w->current_matrix->zv = BUF_ZV (b);
10943 w->last_cursor = w->cursor;
10944 w->last_cursor_off_p = w->cursor_off_p;
10946 if (w == XWINDOW (selected_window))
10947 w->last_point = make_number (BUF_PT (b));
10948 else
10949 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10953 if (accurate_p)
10955 w->window_end_valid = w->buffer;
10956 #if 0 /* This is incorrect with variable-height lines. */
10957 xassert (XINT (w->window_end_vpos)
10958 < (WINDOW_TOTAL_LINES (w)
10959 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10960 #endif
10961 w->update_mode_line = Qnil;
10966 /* Mark the display of windows in the window tree rooted at WINDOW as
10967 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10968 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10969 be redisplayed the next time redisplay_internal is called. */
10971 void
10972 mark_window_display_accurate (window, accurate_p)
10973 Lisp_Object window;
10974 int accurate_p;
10976 struct window *w;
10978 for (; !NILP (window); window = w->next)
10980 w = XWINDOW (window);
10981 mark_window_display_accurate_1 (w, accurate_p);
10983 if (!NILP (w->vchild))
10984 mark_window_display_accurate (w->vchild, accurate_p);
10985 if (!NILP (w->hchild))
10986 mark_window_display_accurate (w->hchild, accurate_p);
10989 if (accurate_p)
10991 update_overlay_arrows (1);
10993 else
10995 /* Force a thorough redisplay the next time by setting
10996 last_arrow_position and last_arrow_string to t, which is
10997 unequal to any useful value of Voverlay_arrow_... */
10998 update_overlay_arrows (-1);
11003 /* Return value in display table DP (Lisp_Char_Table *) for character
11004 C. Since a display table doesn't have any parent, we don't have to
11005 follow parent. Do not call this function directly but use the
11006 macro DISP_CHAR_VECTOR. */
11008 Lisp_Object
11009 disp_char_vector (dp, c)
11010 struct Lisp_Char_Table *dp;
11011 int c;
11013 int code[4], i;
11014 Lisp_Object val;
11016 if (SINGLE_BYTE_CHAR_P (c))
11017 return (dp->contents[c]);
11019 SPLIT_CHAR (c, code[0], code[1], code[2]);
11020 if (code[1] < 32)
11021 code[1] = -1;
11022 else if (code[2] < 32)
11023 code[2] = -1;
11025 /* Here, the possible range of code[0] (== charset ID) is
11026 128..max_charset. Since the top level char table contains data
11027 for multibyte characters after 256th element, we must increment
11028 code[0] by 128 to get a correct index. */
11029 code[0] += 128;
11030 code[3] = -1; /* anchor */
11032 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11034 val = dp->contents[code[i]];
11035 if (!SUB_CHAR_TABLE_P (val))
11036 return (NILP (val) ? dp->defalt : val);
11039 /* Here, val is a sub char table. We return the default value of
11040 it. */
11041 return (dp->defalt);
11046 /***********************************************************************
11047 Window Redisplay
11048 ***********************************************************************/
11050 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11052 static void
11053 redisplay_windows (window)
11054 Lisp_Object window;
11056 while (!NILP (window))
11058 struct window *w = XWINDOW (window);
11060 if (!NILP (w->hchild))
11061 redisplay_windows (w->hchild);
11062 else if (!NILP (w->vchild))
11063 redisplay_windows (w->vchild);
11064 else
11066 displayed_buffer = XBUFFER (w->buffer);
11067 /* Use list_of_error, not Qerror, so that
11068 we catch only errors and don't run the debugger. */
11069 internal_condition_case_1 (redisplay_window_0, window,
11070 list_of_error,
11071 redisplay_window_error);
11074 window = w->next;
11078 static Lisp_Object
11079 redisplay_window_error ()
11081 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11082 return Qnil;
11085 static Lisp_Object
11086 redisplay_window_0 (window)
11087 Lisp_Object window;
11089 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11090 redisplay_window (window, 0);
11091 return Qnil;
11094 static Lisp_Object
11095 redisplay_window_1 (window)
11096 Lisp_Object window;
11098 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11099 redisplay_window (window, 1);
11100 return Qnil;
11104 /* Increment GLYPH until it reaches END or CONDITION fails while
11105 adding (GLYPH)->pixel_width to X. */
11107 #define SKIP_GLYPHS(glyph, end, x, condition) \
11108 do \
11110 (x) += (glyph)->pixel_width; \
11111 ++(glyph); \
11113 while ((glyph) < (end) && (condition))
11116 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11117 DELTA is the number of bytes by which positions recorded in ROW
11118 differ from current buffer positions. */
11120 void
11121 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11122 struct window *w;
11123 struct glyph_row *row;
11124 struct glyph_matrix *matrix;
11125 int delta, delta_bytes, dy, dvpos;
11127 struct glyph *glyph = row->glyphs[TEXT_AREA];
11128 struct glyph *end = glyph + row->used[TEXT_AREA];
11129 struct glyph *cursor = NULL;
11130 /* The first glyph that starts a sequence of glyphs from string. */
11131 struct glyph *string_start;
11132 /* The X coordinate of string_start. */
11133 int string_start_x;
11134 /* The last known character position. */
11135 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11136 /* The last known character position before string_start. */
11137 int string_before_pos;
11138 int x = row->x;
11139 int cursor_x = x;
11140 int cursor_from_overlay_pos = 0;
11141 int pt_old = PT - delta;
11143 /* Skip over glyphs not having an object at the start of the row.
11144 These are special glyphs like truncation marks on terminal
11145 frames. */
11146 if (row->displays_text_p)
11147 while (glyph < end
11148 && INTEGERP (glyph->object)
11149 && glyph->charpos < 0)
11151 x += glyph->pixel_width;
11152 ++glyph;
11155 string_start = NULL;
11156 while (glyph < end
11157 && !INTEGERP (glyph->object)
11158 && (!BUFFERP (glyph->object)
11159 || (last_pos = glyph->charpos) < pt_old))
11161 if (! STRINGP (glyph->object))
11163 string_start = NULL;
11164 x += glyph->pixel_width;
11165 ++glyph;
11166 if (cursor_from_overlay_pos
11167 && last_pos > cursor_from_overlay_pos)
11169 cursor_from_overlay_pos = 0;
11170 cursor = 0;
11173 else
11175 string_before_pos = last_pos;
11176 string_start = glyph;
11177 string_start_x = x;
11178 /* Skip all glyphs from string. */
11181 int pos;
11182 if ((cursor == NULL || glyph > cursor)
11183 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11184 Qcursor, (glyph)->object))
11185 && (pos = string_buffer_position (w, glyph->object,
11186 string_before_pos),
11187 (pos == 0 /* From overlay */
11188 || pos == pt_old)))
11190 /* Estimate overlay buffer position from the buffer
11191 positions of the glyphs before and after the overlay.
11192 Add 1 to last_pos so that if point corresponds to the
11193 glyph right after the overlay, we still use a 'cursor'
11194 property found in that overlay. */
11195 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11196 cursor = glyph;
11197 cursor_x = x;
11199 x += glyph->pixel_width;
11200 ++glyph;
11202 while (glyph < end && STRINGP (glyph->object));
11206 if (cursor != NULL)
11208 glyph = cursor;
11209 x = cursor_x;
11211 else if (row->ends_in_ellipsis_p && glyph == end)
11213 /* Scan back over the ellipsis glyphs, decrementing positions. */
11214 while (glyph > row->glyphs[TEXT_AREA]
11215 && (glyph - 1)->charpos == last_pos)
11216 glyph--, x -= glyph->pixel_width;
11217 /* That loop always goes one position too far,
11218 including the glyph before the ellipsis.
11219 So scan forward over that one. */
11220 x += glyph->pixel_width;
11221 glyph++;
11223 else if (string_start
11224 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11226 /* We may have skipped over point because the previous glyphs
11227 are from string. As there's no easy way to know the
11228 character position of the current glyph, find the correct
11229 glyph on point by scanning from string_start again. */
11230 Lisp_Object limit;
11231 Lisp_Object string;
11232 int pos;
11234 limit = make_number (pt_old + 1);
11235 end = glyph;
11236 glyph = string_start;
11237 x = string_start_x;
11238 string = glyph->object;
11239 pos = string_buffer_position (w, string, string_before_pos);
11240 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11241 because we always put cursor after overlay strings. */
11242 while (pos == 0 && glyph < end)
11244 string = glyph->object;
11245 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11246 if (glyph < end)
11247 pos = string_buffer_position (w, glyph->object, string_before_pos);
11250 while (glyph < end)
11252 pos = XINT (Fnext_single_char_property_change
11253 (make_number (pos), Qdisplay, Qnil, limit));
11254 if (pos > pt_old)
11255 break;
11256 /* Skip glyphs from the same string. */
11257 string = glyph->object;
11258 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11259 /* Skip glyphs from an overlay. */
11260 while (glyph < end
11261 && ! string_buffer_position (w, glyph->object, pos))
11263 string = glyph->object;
11264 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11269 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11270 w->cursor.x = x;
11271 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11272 w->cursor.y = row->y + dy;
11274 if (w == XWINDOW (selected_window))
11276 if (!row->continued_p
11277 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11278 && row->x == 0)
11280 this_line_buffer = XBUFFER (w->buffer);
11282 CHARPOS (this_line_start_pos)
11283 = MATRIX_ROW_START_CHARPOS (row) + delta;
11284 BYTEPOS (this_line_start_pos)
11285 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11287 CHARPOS (this_line_end_pos)
11288 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11289 BYTEPOS (this_line_end_pos)
11290 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11292 this_line_y = w->cursor.y;
11293 this_line_pixel_height = row->height;
11294 this_line_vpos = w->cursor.vpos;
11295 this_line_start_x = row->x;
11297 else
11298 CHARPOS (this_line_start_pos) = 0;
11303 /* Run window scroll functions, if any, for WINDOW with new window
11304 start STARTP. Sets the window start of WINDOW to that position.
11306 We assume that the window's buffer is really current. */
11308 static INLINE struct text_pos
11309 run_window_scroll_functions (window, startp)
11310 Lisp_Object window;
11311 struct text_pos startp;
11313 struct window *w = XWINDOW (window);
11314 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11316 if (current_buffer != XBUFFER (w->buffer))
11317 abort ();
11319 if (!NILP (Vwindow_scroll_functions))
11321 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11322 make_number (CHARPOS (startp)));
11323 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11324 /* In case the hook functions switch buffers. */
11325 if (current_buffer != XBUFFER (w->buffer))
11326 set_buffer_internal_1 (XBUFFER (w->buffer));
11329 return startp;
11333 /* Make sure the line containing the cursor is fully visible.
11334 A value of 1 means there is nothing to be done.
11335 (Either the line is fully visible, or it cannot be made so,
11336 or we cannot tell.)
11338 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11339 is higher than window.
11341 A value of 0 means the caller should do scrolling
11342 as if point had gone off the screen. */
11344 static int
11345 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11346 struct window *w;
11347 int force_p;
11349 struct glyph_matrix *matrix;
11350 struct glyph_row *row;
11351 int window_height;
11353 if (!make_cursor_line_fully_visible_p)
11354 return 1;
11356 /* It's not always possible to find the cursor, e.g, when a window
11357 is full of overlay strings. Don't do anything in that case. */
11358 if (w->cursor.vpos < 0)
11359 return 1;
11361 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11362 row = MATRIX_ROW (matrix, w->cursor.vpos);
11364 /* If the cursor row is not partially visible, there's nothing to do. */
11365 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11366 return 1;
11368 /* If the row the cursor is in is taller than the window's height,
11369 it's not clear what to do, so do nothing. */
11370 window_height = window_box_height (w);
11371 if (row->height >= window_height)
11373 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11374 return 1;
11376 return 0;
11378 #if 0
11379 /* This code used to try to scroll the window just enough to make
11380 the line visible. It returned 0 to say that the caller should
11381 allocate larger glyph matrices. */
11383 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11385 int dy = row->height - row->visible_height;
11386 w->vscroll = 0;
11387 w->cursor.y += dy;
11388 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11390 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11392 int dy = - (row->height - row->visible_height);
11393 w->vscroll = dy;
11394 w->cursor.y += dy;
11395 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11398 /* When we change the cursor y-position of the selected window,
11399 change this_line_y as well so that the display optimization for
11400 the cursor line of the selected window in redisplay_internal uses
11401 the correct y-position. */
11402 if (w == XWINDOW (selected_window))
11403 this_line_y = w->cursor.y;
11405 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11406 redisplay with larger matrices. */
11407 if (matrix->nrows < required_matrix_height (w))
11409 fonts_changed_p = 1;
11410 return 0;
11413 return 1;
11414 #endif /* 0 */
11418 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11419 non-zero means only WINDOW is redisplayed in redisplay_internal.
11420 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11421 in redisplay_window to bring a partially visible line into view in
11422 the case that only the cursor has moved.
11424 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11425 last screen line's vertical height extends past the end of the screen.
11427 Value is
11429 1 if scrolling succeeded
11431 0 if scrolling didn't find point.
11433 -1 if new fonts have been loaded so that we must interrupt
11434 redisplay, adjust glyph matrices, and try again. */
11436 enum
11438 SCROLLING_SUCCESS,
11439 SCROLLING_FAILED,
11440 SCROLLING_NEED_LARGER_MATRICES
11443 static int
11444 try_scrolling (window, just_this_one_p, scroll_conservatively,
11445 scroll_step, temp_scroll_step, last_line_misfit)
11446 Lisp_Object window;
11447 int just_this_one_p;
11448 EMACS_INT scroll_conservatively, scroll_step;
11449 int temp_scroll_step;
11450 int last_line_misfit;
11452 struct window *w = XWINDOW (window);
11453 struct frame *f = XFRAME (w->frame);
11454 struct text_pos scroll_margin_pos;
11455 struct text_pos pos;
11456 struct text_pos startp;
11457 struct it it;
11458 Lisp_Object window_end;
11459 int this_scroll_margin;
11460 int dy = 0;
11461 int scroll_max;
11462 int rc;
11463 int amount_to_scroll = 0;
11464 Lisp_Object aggressive;
11465 int height;
11466 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11468 #if GLYPH_DEBUG
11469 debug_method_add (w, "try_scrolling");
11470 #endif
11472 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11474 /* Compute scroll margin height in pixels. We scroll when point is
11475 within this distance from the top or bottom of the window. */
11476 if (scroll_margin > 0)
11478 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11479 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11481 else
11482 this_scroll_margin = 0;
11484 /* Force scroll_conservatively to have a reasonable value so it doesn't
11485 cause an overflow while computing how much to scroll. */
11486 if (scroll_conservatively)
11487 scroll_conservatively = min (scroll_conservatively,
11488 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11490 /* Compute how much we should try to scroll maximally to bring point
11491 into view. */
11492 if (scroll_step || scroll_conservatively || temp_scroll_step)
11493 scroll_max = max (scroll_step,
11494 max (scroll_conservatively, temp_scroll_step));
11495 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11496 || NUMBERP (current_buffer->scroll_up_aggressively))
11497 /* We're trying to scroll because of aggressive scrolling
11498 but no scroll_step is set. Choose an arbitrary one. Maybe
11499 there should be a variable for this. */
11500 scroll_max = 10;
11501 else
11502 scroll_max = 0;
11503 scroll_max *= FRAME_LINE_HEIGHT (f);
11505 /* Decide whether we have to scroll down. Start at the window end
11506 and move this_scroll_margin up to find the position of the scroll
11507 margin. */
11508 window_end = Fwindow_end (window, Qt);
11510 too_near_end:
11512 CHARPOS (scroll_margin_pos) = XINT (window_end);
11513 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11515 if (this_scroll_margin || extra_scroll_margin_lines)
11517 start_display (&it, w, scroll_margin_pos);
11518 if (this_scroll_margin)
11519 move_it_vertically_backward (&it, this_scroll_margin);
11520 if (extra_scroll_margin_lines)
11521 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11522 scroll_margin_pos = it.current.pos;
11525 if (PT >= CHARPOS (scroll_margin_pos))
11527 int y0;
11529 /* Point is in the scroll margin at the bottom of the window, or
11530 below. Compute a new window start that makes point visible. */
11532 /* Compute the distance from the scroll margin to PT.
11533 Give up if the distance is greater than scroll_max. */
11534 start_display (&it, w, scroll_margin_pos);
11535 y0 = it.current_y;
11536 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11537 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11539 /* To make point visible, we have to move the window start
11540 down so that the line the cursor is in is visible, which
11541 means we have to add in the height of the cursor line. */
11542 dy = line_bottom_y (&it) - y0;
11544 if (dy > scroll_max)
11545 return SCROLLING_FAILED;
11547 /* Move the window start down. If scrolling conservatively,
11548 move it just enough down to make point visible. If
11549 scroll_step is set, move it down by scroll_step. */
11550 start_display (&it, w, startp);
11552 if (scroll_conservatively)
11553 /* Set AMOUNT_TO_SCROLL to at least one line,
11554 and at most scroll_conservatively lines. */
11555 amount_to_scroll
11556 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11557 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11558 else if (scroll_step || temp_scroll_step)
11559 amount_to_scroll = scroll_max;
11560 else
11562 aggressive = current_buffer->scroll_up_aggressively;
11563 height = WINDOW_BOX_TEXT_HEIGHT (w);
11564 if (NUMBERP (aggressive))
11566 double float_amount = XFLOATINT (aggressive) * height;
11567 amount_to_scroll = float_amount;
11568 if (amount_to_scroll == 0 && float_amount > 0)
11569 amount_to_scroll = 1;
11573 if (amount_to_scroll <= 0)
11574 return SCROLLING_FAILED;
11576 /* If moving by amount_to_scroll leaves STARTP unchanged,
11577 move it down one screen line. */
11579 move_it_vertically (&it, amount_to_scroll);
11580 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11581 move_it_by_lines (&it, 1, 1);
11582 startp = it.current.pos;
11584 else
11586 /* See if point is inside the scroll margin at the top of the
11587 window. */
11588 scroll_margin_pos = startp;
11589 if (this_scroll_margin)
11591 start_display (&it, w, startp);
11592 move_it_vertically (&it, this_scroll_margin);
11593 scroll_margin_pos = it.current.pos;
11596 if (PT < CHARPOS (scroll_margin_pos))
11598 /* Point is in the scroll margin at the top of the window or
11599 above what is displayed in the window. */
11600 int y0;
11602 /* Compute the vertical distance from PT to the scroll
11603 margin position. Give up if distance is greater than
11604 scroll_max. */
11605 SET_TEXT_POS (pos, PT, PT_BYTE);
11606 start_display (&it, w, pos);
11607 y0 = it.current_y;
11608 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11609 it.last_visible_y, -1,
11610 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11611 dy = it.current_y - y0;
11612 if (dy > scroll_max)
11613 return SCROLLING_FAILED;
11615 /* Compute new window start. */
11616 start_display (&it, w, startp);
11618 if (scroll_conservatively)
11619 amount_to_scroll
11620 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11621 else if (scroll_step || temp_scroll_step)
11622 amount_to_scroll = scroll_max;
11623 else
11625 aggressive = current_buffer->scroll_down_aggressively;
11626 height = WINDOW_BOX_TEXT_HEIGHT (w);
11627 if (NUMBERP (aggressive))
11629 double float_amount = XFLOATINT (aggressive) * height;
11630 amount_to_scroll = float_amount;
11631 if (amount_to_scroll == 0 && float_amount > 0)
11632 amount_to_scroll = 1;
11636 if (amount_to_scroll <= 0)
11637 return SCROLLING_FAILED;
11639 move_it_vertically_backward (&it, amount_to_scroll);
11640 startp = it.current.pos;
11644 /* Run window scroll functions. */
11645 startp = run_window_scroll_functions (window, startp);
11647 /* Display the window. Give up if new fonts are loaded, or if point
11648 doesn't appear. */
11649 if (!try_window (window, startp, 0))
11650 rc = SCROLLING_NEED_LARGER_MATRICES;
11651 else if (w->cursor.vpos < 0)
11653 clear_glyph_matrix (w->desired_matrix);
11654 rc = SCROLLING_FAILED;
11656 else
11658 /* Maybe forget recorded base line for line number display. */
11659 if (!just_this_one_p
11660 || current_buffer->clip_changed
11661 || BEG_UNCHANGED < CHARPOS (startp))
11662 w->base_line_number = Qnil;
11664 /* If cursor ends up on a partially visible line,
11665 treat that as being off the bottom of the screen. */
11666 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11668 clear_glyph_matrix (w->desired_matrix);
11669 ++extra_scroll_margin_lines;
11670 goto too_near_end;
11672 rc = SCROLLING_SUCCESS;
11675 return rc;
11679 /* Compute a suitable window start for window W if display of W starts
11680 on a continuation line. Value is non-zero if a new window start
11681 was computed.
11683 The new window start will be computed, based on W's width, starting
11684 from the start of the continued line. It is the start of the
11685 screen line with the minimum distance from the old start W->start. */
11687 static int
11688 compute_window_start_on_continuation_line (w)
11689 struct window *w;
11691 struct text_pos pos, start_pos;
11692 int window_start_changed_p = 0;
11694 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11696 /* If window start is on a continuation line... Window start may be
11697 < BEGV in case there's invisible text at the start of the
11698 buffer (M-x rmail, for example). */
11699 if (CHARPOS (start_pos) > BEGV
11700 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11702 struct it it;
11703 struct glyph_row *row;
11705 /* Handle the case that the window start is out of range. */
11706 if (CHARPOS (start_pos) < BEGV)
11707 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11708 else if (CHARPOS (start_pos) > ZV)
11709 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11711 /* Find the start of the continued line. This should be fast
11712 because scan_buffer is fast (newline cache). */
11713 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11714 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11715 row, DEFAULT_FACE_ID);
11716 reseat_at_previous_visible_line_start (&it);
11718 /* If the line start is "too far" away from the window start,
11719 say it takes too much time to compute a new window start. */
11720 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11721 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11723 int min_distance, distance;
11725 /* Move forward by display lines to find the new window
11726 start. If window width was enlarged, the new start can
11727 be expected to be > the old start. If window width was
11728 decreased, the new window start will be < the old start.
11729 So, we're looking for the display line start with the
11730 minimum distance from the old window start. */
11731 pos = it.current.pos;
11732 min_distance = INFINITY;
11733 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11734 distance < min_distance)
11736 min_distance = distance;
11737 pos = it.current.pos;
11738 move_it_by_lines (&it, 1, 0);
11741 /* Set the window start there. */
11742 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11743 window_start_changed_p = 1;
11747 return window_start_changed_p;
11751 /* Try cursor movement in case text has not changed in window WINDOW,
11752 with window start STARTP. Value is
11754 CURSOR_MOVEMENT_SUCCESS if successful
11756 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11758 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11759 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11760 we want to scroll as if scroll-step were set to 1. See the code.
11762 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11763 which case we have to abort this redisplay, and adjust matrices
11764 first. */
11766 enum
11768 CURSOR_MOVEMENT_SUCCESS,
11769 CURSOR_MOVEMENT_CANNOT_BE_USED,
11770 CURSOR_MOVEMENT_MUST_SCROLL,
11771 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11774 static int
11775 try_cursor_movement (window, startp, scroll_step)
11776 Lisp_Object window;
11777 struct text_pos startp;
11778 int *scroll_step;
11780 struct window *w = XWINDOW (window);
11781 struct frame *f = XFRAME (w->frame);
11782 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11784 #if GLYPH_DEBUG
11785 if (inhibit_try_cursor_movement)
11786 return rc;
11787 #endif
11789 /* Handle case where text has not changed, only point, and it has
11790 not moved off the frame. */
11791 if (/* Point may be in this window. */
11792 PT >= CHARPOS (startp)
11793 /* Selective display hasn't changed. */
11794 && !current_buffer->clip_changed
11795 /* Function force-mode-line-update is used to force a thorough
11796 redisplay. It sets either windows_or_buffers_changed or
11797 update_mode_lines. So don't take a shortcut here for these
11798 cases. */
11799 && !update_mode_lines
11800 && !windows_or_buffers_changed
11801 && !cursor_type_changed
11802 /* Can't use this case if highlighting a region. When a
11803 region exists, cursor movement has to do more than just
11804 set the cursor. */
11805 && !(!NILP (Vtransient_mark_mode)
11806 && !NILP (current_buffer->mark_active))
11807 && NILP (w->region_showing)
11808 && NILP (Vshow_trailing_whitespace)
11809 /* Right after splitting windows, last_point may be nil. */
11810 && INTEGERP (w->last_point)
11811 /* This code is not used for mini-buffer for the sake of the case
11812 of redisplaying to replace an echo area message; since in
11813 that case the mini-buffer contents per se are usually
11814 unchanged. This code is of no real use in the mini-buffer
11815 since the handling of this_line_start_pos, etc., in redisplay
11816 handles the same cases. */
11817 && !EQ (window, minibuf_window)
11818 /* When splitting windows or for new windows, it happens that
11819 redisplay is called with a nil window_end_vpos or one being
11820 larger than the window. This should really be fixed in
11821 window.c. I don't have this on my list, now, so we do
11822 approximately the same as the old redisplay code. --gerd. */
11823 && INTEGERP (w->window_end_vpos)
11824 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11825 && (FRAME_WINDOW_P (f)
11826 || !overlay_arrow_in_current_buffer_p ()))
11828 int this_scroll_margin, top_scroll_margin;
11829 struct glyph_row *row = NULL;
11831 #if GLYPH_DEBUG
11832 debug_method_add (w, "cursor movement");
11833 #endif
11835 /* Scroll if point within this distance from the top or bottom
11836 of the window. This is a pixel value. */
11837 this_scroll_margin = max (0, scroll_margin);
11838 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11839 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11841 top_scroll_margin = this_scroll_margin;
11842 if (WINDOW_WANTS_HEADER_LINE_P (w))
11843 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11845 /* Start with the row the cursor was displayed during the last
11846 not paused redisplay. Give up if that row is not valid. */
11847 if (w->last_cursor.vpos < 0
11848 || w->last_cursor.vpos >= w->current_matrix->nrows)
11849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11850 else
11852 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11853 if (row->mode_line_p)
11854 ++row;
11855 if (!row->enabled_p)
11856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11859 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11861 int scroll_p = 0;
11862 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11864 if (PT > XFASTINT (w->last_point))
11866 /* Point has moved forward. */
11867 while (MATRIX_ROW_END_CHARPOS (row) < PT
11868 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11870 xassert (row->enabled_p);
11871 ++row;
11874 /* The end position of a row equals the start position
11875 of the next row. If PT is there, we would rather
11876 display it in the next line. */
11877 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11878 && MATRIX_ROW_END_CHARPOS (row) == PT
11879 && !cursor_row_p (w, row))
11880 ++row;
11882 /* If within the scroll margin, scroll. Note that
11883 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11884 the next line would be drawn, and that
11885 this_scroll_margin can be zero. */
11886 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11887 || PT > MATRIX_ROW_END_CHARPOS (row)
11888 /* Line is completely visible last line in window
11889 and PT is to be set in the next line. */
11890 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11891 && PT == MATRIX_ROW_END_CHARPOS (row)
11892 && !row->ends_at_zv_p
11893 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11894 scroll_p = 1;
11896 else if (PT < XFASTINT (w->last_point))
11898 /* Cursor has to be moved backward. Note that PT >=
11899 CHARPOS (startp) because of the outer if-statement. */
11900 while (!row->mode_line_p
11901 && (MATRIX_ROW_START_CHARPOS (row) > PT
11902 || (MATRIX_ROW_START_CHARPOS (row) == PT
11903 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11904 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11905 row > w->current_matrix->rows
11906 && (row-1)->ends_in_newline_from_string_p))))
11907 && (row->y > top_scroll_margin
11908 || CHARPOS (startp) == BEGV))
11910 xassert (row->enabled_p);
11911 --row;
11914 /* Consider the following case: Window starts at BEGV,
11915 there is invisible, intangible text at BEGV, so that
11916 display starts at some point START > BEGV. It can
11917 happen that we are called with PT somewhere between
11918 BEGV and START. Try to handle that case. */
11919 if (row < w->current_matrix->rows
11920 || row->mode_line_p)
11922 row = w->current_matrix->rows;
11923 if (row->mode_line_p)
11924 ++row;
11927 /* Due to newlines in overlay strings, we may have to
11928 skip forward over overlay strings. */
11929 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11930 && MATRIX_ROW_END_CHARPOS (row) == PT
11931 && !cursor_row_p (w, row))
11932 ++row;
11934 /* If within the scroll margin, scroll. */
11935 if (row->y < top_scroll_margin
11936 && CHARPOS (startp) != BEGV)
11937 scroll_p = 1;
11939 else
11941 /* Cursor did not move. So don't scroll even if cursor line
11942 is partially visible, as it was so before. */
11943 rc = CURSOR_MOVEMENT_SUCCESS;
11946 if (PT < MATRIX_ROW_START_CHARPOS (row)
11947 || PT > MATRIX_ROW_END_CHARPOS (row))
11949 /* if PT is not in the glyph row, give up. */
11950 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11952 else if (rc != CURSOR_MOVEMENT_SUCCESS
11953 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11954 && make_cursor_line_fully_visible_p)
11956 if (PT == MATRIX_ROW_END_CHARPOS (row)
11957 && !row->ends_at_zv_p
11958 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11959 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11960 else if (row->height > window_box_height (w))
11962 /* If we end up in a partially visible line, let's
11963 make it fully visible, except when it's taller
11964 than the window, in which case we can't do much
11965 about it. */
11966 *scroll_step = 1;
11967 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11969 else
11971 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11972 if (!cursor_row_fully_visible_p (w, 0, 1))
11973 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11974 else
11975 rc = CURSOR_MOVEMENT_SUCCESS;
11978 else if (scroll_p)
11979 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11980 else
11982 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11983 rc = CURSOR_MOVEMENT_SUCCESS;
11988 return rc;
11991 void
11992 set_vertical_scroll_bar (w)
11993 struct window *w;
11995 int start, end, whole;
11997 /* Calculate the start and end positions for the current window.
11998 At some point, it would be nice to choose between scrollbars
11999 which reflect the whole buffer size, with special markers
12000 indicating narrowing, and scrollbars which reflect only the
12001 visible region.
12003 Note that mini-buffers sometimes aren't displaying any text. */
12004 if (!MINI_WINDOW_P (w)
12005 || (w == XWINDOW (minibuf_window)
12006 && NILP (echo_area_buffer[0])))
12008 struct buffer *buf = XBUFFER (w->buffer);
12009 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12010 start = marker_position (w->start) - BUF_BEGV (buf);
12011 /* I don't think this is guaranteed to be right. For the
12012 moment, we'll pretend it is. */
12013 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12015 if (end < start)
12016 end = start;
12017 if (whole < (end - start))
12018 whole = end - start;
12020 else
12021 start = end = whole = 0;
12023 /* Indicate what this scroll bar ought to be displaying now. */
12024 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12028 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12029 selected_window is redisplayed.
12031 We can return without actually redisplaying the window if
12032 fonts_changed_p is nonzero. In that case, redisplay_internal will
12033 retry. */
12035 static void
12036 redisplay_window (window, just_this_one_p)
12037 Lisp_Object window;
12038 int just_this_one_p;
12040 struct window *w = XWINDOW (window);
12041 struct frame *f = XFRAME (w->frame);
12042 struct buffer *buffer = XBUFFER (w->buffer);
12043 struct buffer *old = current_buffer;
12044 struct text_pos lpoint, opoint, startp;
12045 int update_mode_line;
12046 int tem;
12047 struct it it;
12048 /* Record it now because it's overwritten. */
12049 int current_matrix_up_to_date_p = 0;
12050 int used_current_matrix_p = 0;
12051 /* This is less strict than current_matrix_up_to_date_p.
12052 It indictes that the buffer contents and narrowing are unchanged. */
12053 int buffer_unchanged_p = 0;
12054 int temp_scroll_step = 0;
12055 int count = SPECPDL_INDEX ();
12056 int rc;
12057 int centering_position = -1;
12058 int last_line_misfit = 0;
12060 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12061 opoint = lpoint;
12063 /* W must be a leaf window here. */
12064 xassert (!NILP (w->buffer));
12065 #if GLYPH_DEBUG
12066 *w->desired_matrix->method = 0;
12067 #endif
12069 specbind (Qinhibit_point_motion_hooks, Qt);
12071 reconsider_clip_changes (w, buffer);
12073 /* Has the mode line to be updated? */
12074 update_mode_line = (!NILP (w->update_mode_line)
12075 || update_mode_lines
12076 || buffer->clip_changed
12077 || buffer->prevent_redisplay_optimizations_p);
12079 if (MINI_WINDOW_P (w))
12081 if (w == XWINDOW (echo_area_window)
12082 && !NILP (echo_area_buffer[0]))
12084 if (update_mode_line)
12085 /* We may have to update a tty frame's menu bar or a
12086 tool-bar. Example `M-x C-h C-h C-g'. */
12087 goto finish_menu_bars;
12088 else
12089 /* We've already displayed the echo area glyphs in this window. */
12090 goto finish_scroll_bars;
12092 else if ((w != XWINDOW (minibuf_window)
12093 || minibuf_level == 0)
12094 /* When buffer is nonempty, redisplay window normally. */
12095 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12096 /* Quail displays non-mini buffers in minibuffer window.
12097 In that case, redisplay the window normally. */
12098 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12100 /* W is a mini-buffer window, but it's not active, so clear
12101 it. */
12102 int yb = window_text_bottom_y (w);
12103 struct glyph_row *row;
12104 int y;
12106 for (y = 0, row = w->desired_matrix->rows;
12107 y < yb;
12108 y += row->height, ++row)
12109 blank_row (w, row, y);
12110 goto finish_scroll_bars;
12113 clear_glyph_matrix (w->desired_matrix);
12116 /* Otherwise set up data on this window; select its buffer and point
12117 value. */
12118 /* Really select the buffer, for the sake of buffer-local
12119 variables. */
12120 set_buffer_internal_1 (XBUFFER (w->buffer));
12121 SET_TEXT_POS (opoint, PT, PT_BYTE);
12123 current_matrix_up_to_date_p
12124 = (!NILP (w->window_end_valid)
12125 && !current_buffer->clip_changed
12126 && !current_buffer->prevent_redisplay_optimizations_p
12127 && XFASTINT (w->last_modified) >= MODIFF
12128 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12130 buffer_unchanged_p
12131 = (!NILP (w->window_end_valid)
12132 && !current_buffer->clip_changed
12133 && XFASTINT (w->last_modified) >= MODIFF
12134 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12136 /* When windows_or_buffers_changed is non-zero, we can't rely on
12137 the window end being valid, so set it to nil there. */
12138 if (windows_or_buffers_changed)
12140 /* If window starts on a continuation line, maybe adjust the
12141 window start in case the window's width changed. */
12142 if (XMARKER (w->start)->buffer == current_buffer)
12143 compute_window_start_on_continuation_line (w);
12145 w->window_end_valid = Qnil;
12148 /* Some sanity checks. */
12149 CHECK_WINDOW_END (w);
12150 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12151 abort ();
12152 if (BYTEPOS (opoint) < CHARPOS (opoint))
12153 abort ();
12155 /* If %c is in mode line, update it if needed. */
12156 if (!NILP (w->column_number_displayed)
12157 /* This alternative quickly identifies a common case
12158 where no change is needed. */
12159 && !(PT == XFASTINT (w->last_point)
12160 && XFASTINT (w->last_modified) >= MODIFF
12161 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12162 && (XFASTINT (w->column_number_displayed)
12163 != (int) current_column ())) /* iftc */
12164 update_mode_line = 1;
12166 /* Count number of windows showing the selected buffer. An indirect
12167 buffer counts as its base buffer. */
12168 if (!just_this_one_p)
12170 struct buffer *current_base, *window_base;
12171 current_base = current_buffer;
12172 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12173 if (current_base->base_buffer)
12174 current_base = current_base->base_buffer;
12175 if (window_base->base_buffer)
12176 window_base = window_base->base_buffer;
12177 if (current_base == window_base)
12178 buffer_shared++;
12181 /* Point refers normally to the selected window. For any other
12182 window, set up appropriate value. */
12183 if (!EQ (window, selected_window))
12185 int new_pt = XMARKER (w->pointm)->charpos;
12186 int new_pt_byte = marker_byte_position (w->pointm);
12187 if (new_pt < BEGV)
12189 new_pt = BEGV;
12190 new_pt_byte = BEGV_BYTE;
12191 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12193 else if (new_pt > (ZV - 1))
12195 new_pt = ZV;
12196 new_pt_byte = ZV_BYTE;
12197 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12200 /* We don't use SET_PT so that the point-motion hooks don't run. */
12201 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12204 /* If any of the character widths specified in the display table
12205 have changed, invalidate the width run cache. It's true that
12206 this may be a bit late to catch such changes, but the rest of
12207 redisplay goes (non-fatally) haywire when the display table is
12208 changed, so why should we worry about doing any better? */
12209 if (current_buffer->width_run_cache)
12211 struct Lisp_Char_Table *disptab = buffer_display_table ();
12213 if (! disptab_matches_widthtab (disptab,
12214 XVECTOR (current_buffer->width_table)))
12216 invalidate_region_cache (current_buffer,
12217 current_buffer->width_run_cache,
12218 BEG, Z);
12219 recompute_width_table (current_buffer, disptab);
12223 /* If window-start is screwed up, choose a new one. */
12224 if (XMARKER (w->start)->buffer != current_buffer)
12225 goto recenter;
12227 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12229 /* If someone specified a new starting point but did not insist,
12230 check whether it can be used. */
12231 if (!NILP (w->optional_new_start)
12232 && CHARPOS (startp) >= BEGV
12233 && CHARPOS (startp) <= ZV)
12235 w->optional_new_start = Qnil;
12236 start_display (&it, w, startp);
12237 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12238 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12239 if (IT_CHARPOS (it) == PT)
12240 w->force_start = Qt;
12241 /* IT may overshoot PT if text at PT is invisible. */
12242 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12243 w->force_start = Qt;
12248 /* Handle case where place to start displaying has been specified,
12249 unless the specified location is outside the accessible range. */
12250 if (!NILP (w->force_start)
12251 || w->frozen_window_start_p)
12253 /* We set this later on if we have to adjust point. */
12254 int new_vpos = -1;
12255 int val;
12257 w->force_start = Qnil;
12258 w->vscroll = 0;
12259 w->window_end_valid = Qnil;
12261 /* Forget any recorded base line for line number display. */
12262 if (!buffer_unchanged_p)
12263 w->base_line_number = Qnil;
12265 /* Redisplay the mode line. Select the buffer properly for that.
12266 Also, run the hook window-scroll-functions
12267 because we have scrolled. */
12268 /* Note, we do this after clearing force_start because
12269 if there's an error, it is better to forget about force_start
12270 than to get into an infinite loop calling the hook functions
12271 and having them get more errors. */
12272 if (!update_mode_line
12273 || ! NILP (Vwindow_scroll_functions))
12275 update_mode_line = 1;
12276 w->update_mode_line = Qt;
12277 startp = run_window_scroll_functions (window, startp);
12280 w->last_modified = make_number (0);
12281 w->last_overlay_modified = make_number (0);
12282 if (CHARPOS (startp) < BEGV)
12283 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12284 else if (CHARPOS (startp) > ZV)
12285 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12287 /* Redisplay, then check if cursor has been set during the
12288 redisplay. Give up if new fonts were loaded. */
12289 val = try_window (window, startp, 1);
12290 if (!val)
12292 w->force_start = Qt;
12293 clear_glyph_matrix (w->desired_matrix);
12294 goto need_larger_matrices;
12296 /* Point was outside the scroll margins. */
12297 if (val < 0)
12298 new_vpos = window_box_height (w) / 2;
12300 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12302 /* If point does not appear, try to move point so it does
12303 appear. The desired matrix has been built above, so we
12304 can use it here. */
12305 new_vpos = window_box_height (w) / 2;
12308 if (!cursor_row_fully_visible_p (w, 0, 0))
12310 /* Point does appear, but on a line partly visible at end of window.
12311 Move it back to a fully-visible line. */
12312 new_vpos = window_box_height (w);
12315 /* If we need to move point for either of the above reasons,
12316 now actually do it. */
12317 if (new_vpos >= 0)
12319 struct glyph_row *row;
12321 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12322 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12323 ++row;
12325 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12326 MATRIX_ROW_START_BYTEPOS (row));
12328 if (w != XWINDOW (selected_window))
12329 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12330 else if (current_buffer == old)
12331 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12333 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12335 /* If we are highlighting the region, then we just changed
12336 the region, so redisplay to show it. */
12337 if (!NILP (Vtransient_mark_mode)
12338 && !NILP (current_buffer->mark_active))
12340 clear_glyph_matrix (w->desired_matrix);
12341 if (!try_window (window, startp, 0))
12342 goto need_larger_matrices;
12346 #if GLYPH_DEBUG
12347 debug_method_add (w, "forced window start");
12348 #endif
12349 goto done;
12352 /* Handle case where text has not changed, only point, and it has
12353 not moved off the frame, and we are not retrying after hscroll.
12354 (current_matrix_up_to_date_p is nonzero when retrying.) */
12355 if (current_matrix_up_to_date_p
12356 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12357 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12359 switch (rc)
12361 case CURSOR_MOVEMENT_SUCCESS:
12362 used_current_matrix_p = 1;
12363 goto done;
12365 #if 0 /* try_cursor_movement never returns this value. */
12366 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12367 goto need_larger_matrices;
12368 #endif
12370 case CURSOR_MOVEMENT_MUST_SCROLL:
12371 goto try_to_scroll;
12373 default:
12374 abort ();
12377 /* If current starting point was originally the beginning of a line
12378 but no longer is, find a new starting point. */
12379 else if (!NILP (w->start_at_line_beg)
12380 && !(CHARPOS (startp) <= BEGV
12381 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12383 #if GLYPH_DEBUG
12384 debug_method_add (w, "recenter 1");
12385 #endif
12386 goto recenter;
12389 /* Try scrolling with try_window_id. Value is > 0 if update has
12390 been done, it is -1 if we know that the same window start will
12391 not work. It is 0 if unsuccessful for some other reason. */
12392 else if ((tem = try_window_id (w)) != 0)
12394 #if GLYPH_DEBUG
12395 debug_method_add (w, "try_window_id %d", tem);
12396 #endif
12398 if (fonts_changed_p)
12399 goto need_larger_matrices;
12400 if (tem > 0)
12401 goto done;
12403 /* Otherwise try_window_id has returned -1 which means that we
12404 don't want the alternative below this comment to execute. */
12406 else if (CHARPOS (startp) >= BEGV
12407 && CHARPOS (startp) <= ZV
12408 && PT >= CHARPOS (startp)
12409 && (CHARPOS (startp) < ZV
12410 /* Avoid starting at end of buffer. */
12411 || CHARPOS (startp) == BEGV
12412 || (XFASTINT (w->last_modified) >= MODIFF
12413 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12415 #if GLYPH_DEBUG
12416 debug_method_add (w, "same window start");
12417 #endif
12419 /* Try to redisplay starting at same place as before.
12420 If point has not moved off frame, accept the results. */
12421 if (!current_matrix_up_to_date_p
12422 /* Don't use try_window_reusing_current_matrix in this case
12423 because a window scroll function can have changed the
12424 buffer. */
12425 || !NILP (Vwindow_scroll_functions)
12426 || MINI_WINDOW_P (w)
12427 || !(used_current_matrix_p
12428 = try_window_reusing_current_matrix (w)))
12430 IF_DEBUG (debug_method_add (w, "1"));
12431 if (try_window (window, startp, 1) < 0)
12432 /* -1 means we need to scroll.
12433 0 means we need new matrices, but fonts_changed_p
12434 is set in that case, so we will detect it below. */
12435 goto try_to_scroll;
12438 if (fonts_changed_p)
12439 goto need_larger_matrices;
12441 if (w->cursor.vpos >= 0)
12443 if (!just_this_one_p
12444 || current_buffer->clip_changed
12445 || BEG_UNCHANGED < CHARPOS (startp))
12446 /* Forget any recorded base line for line number display. */
12447 w->base_line_number = Qnil;
12449 if (!cursor_row_fully_visible_p (w, 1, 0))
12451 clear_glyph_matrix (w->desired_matrix);
12452 last_line_misfit = 1;
12454 /* Drop through and scroll. */
12455 else
12456 goto done;
12458 else
12459 clear_glyph_matrix (w->desired_matrix);
12462 try_to_scroll:
12464 w->last_modified = make_number (0);
12465 w->last_overlay_modified = make_number (0);
12467 /* Redisplay the mode line. Select the buffer properly for that. */
12468 if (!update_mode_line)
12470 update_mode_line = 1;
12471 w->update_mode_line = Qt;
12474 /* Try to scroll by specified few lines. */
12475 if ((scroll_conservatively
12476 || scroll_step
12477 || temp_scroll_step
12478 || NUMBERP (current_buffer->scroll_up_aggressively)
12479 || NUMBERP (current_buffer->scroll_down_aggressively))
12480 && !current_buffer->clip_changed
12481 && CHARPOS (startp) >= BEGV
12482 && CHARPOS (startp) <= ZV)
12484 /* The function returns -1 if new fonts were loaded, 1 if
12485 successful, 0 if not successful. */
12486 int rc = try_scrolling (window, just_this_one_p,
12487 scroll_conservatively,
12488 scroll_step,
12489 temp_scroll_step, last_line_misfit);
12490 switch (rc)
12492 case SCROLLING_SUCCESS:
12493 goto done;
12495 case SCROLLING_NEED_LARGER_MATRICES:
12496 goto need_larger_matrices;
12498 case SCROLLING_FAILED:
12499 break;
12501 default:
12502 abort ();
12506 /* Finally, just choose place to start which centers point */
12508 recenter:
12509 if (centering_position < 0)
12510 centering_position = window_box_height (w) / 2;
12512 #if GLYPH_DEBUG
12513 debug_method_add (w, "recenter");
12514 #endif
12516 /* w->vscroll = 0; */
12518 /* Forget any previously recorded base line for line number display. */
12519 if (!buffer_unchanged_p)
12520 w->base_line_number = Qnil;
12522 /* Move backward half the height of the window. */
12523 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12524 it.current_y = it.last_visible_y;
12525 move_it_vertically_backward (&it, centering_position);
12526 xassert (IT_CHARPOS (it) >= BEGV);
12528 /* The function move_it_vertically_backward may move over more
12529 than the specified y-distance. If it->w is small, e.g. a
12530 mini-buffer window, we may end up in front of the window's
12531 display area. Start displaying at the start of the line
12532 containing PT in this case. */
12533 if (it.current_y <= 0)
12535 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12536 move_it_vertically_backward (&it, 0);
12537 #if 0
12538 /* I think this assert is bogus if buffer contains
12539 invisible text or images. KFS. */
12540 xassert (IT_CHARPOS (it) <= PT);
12541 #endif
12542 it.current_y = 0;
12545 it.current_x = it.hpos = 0;
12547 /* Set startp here explicitly in case that helps avoid an infinite loop
12548 in case the window-scroll-functions functions get errors. */
12549 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12551 /* Run scroll hooks. */
12552 startp = run_window_scroll_functions (window, it.current.pos);
12554 /* Redisplay the window. */
12555 if (!current_matrix_up_to_date_p
12556 || windows_or_buffers_changed
12557 || cursor_type_changed
12558 /* Don't use try_window_reusing_current_matrix in this case
12559 because it can have changed the buffer. */
12560 || !NILP (Vwindow_scroll_functions)
12561 || !just_this_one_p
12562 || MINI_WINDOW_P (w)
12563 || !(used_current_matrix_p
12564 = try_window_reusing_current_matrix (w)))
12565 try_window (window, startp, 0);
12567 /* If new fonts have been loaded (due to fontsets), give up. We
12568 have to start a new redisplay since we need to re-adjust glyph
12569 matrices. */
12570 if (fonts_changed_p)
12571 goto need_larger_matrices;
12573 /* If cursor did not appear assume that the middle of the window is
12574 in the first line of the window. Do it again with the next line.
12575 (Imagine a window of height 100, displaying two lines of height
12576 60. Moving back 50 from it->last_visible_y will end in the first
12577 line.) */
12578 if (w->cursor.vpos < 0)
12580 if (!NILP (w->window_end_valid)
12581 && PT >= Z - XFASTINT (w->window_end_pos))
12583 clear_glyph_matrix (w->desired_matrix);
12584 move_it_by_lines (&it, 1, 0);
12585 try_window (window, it.current.pos, 0);
12587 else if (PT < IT_CHARPOS (it))
12589 clear_glyph_matrix (w->desired_matrix);
12590 move_it_by_lines (&it, -1, 0);
12591 try_window (window, it.current.pos, 0);
12593 else
12595 /* Not much we can do about it. */
12599 /* Consider the following case: Window starts at BEGV, there is
12600 invisible, intangible text at BEGV, so that display starts at
12601 some point START > BEGV. It can happen that we are called with
12602 PT somewhere between BEGV and START. Try to handle that case. */
12603 if (w->cursor.vpos < 0)
12605 struct glyph_row *row = w->current_matrix->rows;
12606 if (row->mode_line_p)
12607 ++row;
12608 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12611 if (!cursor_row_fully_visible_p (w, 0, 0))
12613 /* If vscroll is enabled, disable it and try again. */
12614 if (w->vscroll)
12616 w->vscroll = 0;
12617 clear_glyph_matrix (w->desired_matrix);
12618 goto recenter;
12621 /* If centering point failed to make the whole line visible,
12622 put point at the top instead. That has to make the whole line
12623 visible, if it can be done. */
12624 if (centering_position == 0)
12625 goto done;
12627 clear_glyph_matrix (w->desired_matrix);
12628 centering_position = 0;
12629 goto recenter;
12632 done:
12634 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12635 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12636 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12637 ? Qt : Qnil);
12639 /* Display the mode line, if we must. */
12640 if ((update_mode_line
12641 /* If window not full width, must redo its mode line
12642 if (a) the window to its side is being redone and
12643 (b) we do a frame-based redisplay. This is a consequence
12644 of how inverted lines are drawn in frame-based redisplay. */
12645 || (!just_this_one_p
12646 && !FRAME_WINDOW_P (f)
12647 && !WINDOW_FULL_WIDTH_P (w))
12648 /* Line number to display. */
12649 || INTEGERP (w->base_line_pos)
12650 /* Column number is displayed and different from the one displayed. */
12651 || (!NILP (w->column_number_displayed)
12652 && (XFASTINT (w->column_number_displayed)
12653 != (int) current_column ()))) /* iftc */
12654 /* This means that the window has a mode line. */
12655 && (WINDOW_WANTS_MODELINE_P (w)
12656 || WINDOW_WANTS_HEADER_LINE_P (w)))
12658 display_mode_lines (w);
12660 /* If mode line height has changed, arrange for a thorough
12661 immediate redisplay using the correct mode line height. */
12662 if (WINDOW_WANTS_MODELINE_P (w)
12663 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12665 fonts_changed_p = 1;
12666 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12667 = DESIRED_MODE_LINE_HEIGHT (w);
12670 /* If top line height has changed, arrange for a thorough
12671 immediate redisplay using the correct mode line height. */
12672 if (WINDOW_WANTS_HEADER_LINE_P (w)
12673 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12675 fonts_changed_p = 1;
12676 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12677 = DESIRED_HEADER_LINE_HEIGHT (w);
12680 if (fonts_changed_p)
12681 goto need_larger_matrices;
12684 if (!line_number_displayed
12685 && !BUFFERP (w->base_line_pos))
12687 w->base_line_pos = Qnil;
12688 w->base_line_number = Qnil;
12691 finish_menu_bars:
12693 /* When we reach a frame's selected window, redo the frame's menu bar. */
12694 if (update_mode_line
12695 && EQ (FRAME_SELECTED_WINDOW (f), window))
12697 int redisplay_menu_p = 0;
12698 int redisplay_tool_bar_p = 0;
12700 if (FRAME_WINDOW_P (f))
12702 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12703 || defined (USE_GTK)
12704 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12705 #else
12706 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12707 #endif
12709 else
12710 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12712 if (redisplay_menu_p)
12713 display_menu_bar (w);
12715 #ifdef HAVE_WINDOW_SYSTEM
12716 #ifdef USE_GTK
12717 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12718 #else
12719 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12720 && (FRAME_TOOL_BAR_LINES (f) > 0
12721 || auto_resize_tool_bars_p);
12723 #endif
12725 if (redisplay_tool_bar_p)
12726 redisplay_tool_bar (f);
12727 #endif
12730 #ifdef HAVE_WINDOW_SYSTEM
12731 if (FRAME_WINDOW_P (f)
12732 && update_window_fringes (w, (just_this_one_p
12733 || (!used_current_matrix_p && !overlay_arrow_seen)
12734 || w->pseudo_window_p)))
12736 update_begin (f);
12737 BLOCK_INPUT;
12738 if (draw_window_fringes (w, 1))
12739 x_draw_vertical_border (w);
12740 UNBLOCK_INPUT;
12741 update_end (f);
12743 #endif /* HAVE_WINDOW_SYSTEM */
12745 /* We go to this label, with fonts_changed_p nonzero,
12746 if it is necessary to try again using larger glyph matrices.
12747 We have to redeem the scroll bar even in this case,
12748 because the loop in redisplay_internal expects that. */
12749 need_larger_matrices:
12751 finish_scroll_bars:
12753 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12755 /* Set the thumb's position and size. */
12756 set_vertical_scroll_bar (w);
12758 /* Note that we actually used the scroll bar attached to this
12759 window, so it shouldn't be deleted at the end of redisplay. */
12760 redeem_scroll_bar_hook (w);
12763 /* Restore current_buffer and value of point in it. */
12764 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12765 set_buffer_internal_1 (old);
12766 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12768 unbind_to (count, Qnil);
12772 /* Build the complete desired matrix of WINDOW with a window start
12773 buffer position POS.
12775 Value is 1 if successful. It is zero if fonts were loaded during
12776 redisplay which makes re-adjusting glyph matrices necessary, and -1
12777 if point would appear in the scroll margins.
12778 (We check that only if CHECK_MARGINS is nonzero. */
12781 try_window (window, pos, check_margins)
12782 Lisp_Object window;
12783 struct text_pos pos;
12784 int check_margins;
12786 struct window *w = XWINDOW (window);
12787 struct it it;
12788 struct glyph_row *last_text_row = NULL;
12790 /* Make POS the new window start. */
12791 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12793 /* Mark cursor position as unknown. No overlay arrow seen. */
12794 w->cursor.vpos = -1;
12795 overlay_arrow_seen = 0;
12797 /* Initialize iterator and info to start at POS. */
12798 start_display (&it, w, pos);
12800 /* Display all lines of W. */
12801 while (it.current_y < it.last_visible_y)
12803 if (display_line (&it))
12804 last_text_row = it.glyph_row - 1;
12805 if (fonts_changed_p)
12806 return 0;
12809 /* Don't let the cursor end in the scroll margins. */
12810 if (check_margins
12811 && !MINI_WINDOW_P (w))
12813 int this_scroll_margin, cursor_height;
12815 this_scroll_margin = max (0, scroll_margin);
12816 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12817 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12818 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12820 if ((w->cursor.y < this_scroll_margin
12821 && CHARPOS (pos) > BEGV)
12822 /* rms: considering make_cursor_line_fully_visible_p here
12823 seems to give wrong results. We don't want to recenter
12824 when the last line is partly visible, we want to allow
12825 that case to be handled in the usual way. */
12826 || (w->cursor.y + 1) > it.last_visible_y)
12828 w->cursor.vpos = -1;
12829 clear_glyph_matrix (w->desired_matrix);
12830 return -1;
12834 /* If bottom moved off end of frame, change mode line percentage. */
12835 if (XFASTINT (w->window_end_pos) <= 0
12836 && Z != IT_CHARPOS (it))
12837 w->update_mode_line = Qt;
12839 /* Set window_end_pos to the offset of the last character displayed
12840 on the window from the end of current_buffer. Set
12841 window_end_vpos to its row number. */
12842 if (last_text_row)
12844 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12845 w->window_end_bytepos
12846 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12847 w->window_end_pos
12848 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12849 w->window_end_vpos
12850 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12851 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12852 ->displays_text_p);
12854 else
12856 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12857 w->window_end_pos = make_number (Z - ZV);
12858 w->window_end_vpos = make_number (0);
12861 /* But that is not valid info until redisplay finishes. */
12862 w->window_end_valid = Qnil;
12863 return 1;
12868 /************************************************************************
12869 Window redisplay reusing current matrix when buffer has not changed
12870 ************************************************************************/
12872 /* Try redisplay of window W showing an unchanged buffer with a
12873 different window start than the last time it was displayed by
12874 reusing its current matrix. Value is non-zero if successful.
12875 W->start is the new window start. */
12877 static int
12878 try_window_reusing_current_matrix (w)
12879 struct window *w;
12881 struct frame *f = XFRAME (w->frame);
12882 struct glyph_row *row, *bottom_row;
12883 struct it it;
12884 struct run run;
12885 struct text_pos start, new_start;
12886 int nrows_scrolled, i;
12887 struct glyph_row *last_text_row;
12888 struct glyph_row *last_reused_text_row;
12889 struct glyph_row *start_row;
12890 int start_vpos, min_y, max_y;
12892 #if GLYPH_DEBUG
12893 if (inhibit_try_window_reusing)
12894 return 0;
12895 #endif
12897 if (/* This function doesn't handle terminal frames. */
12898 !FRAME_WINDOW_P (f)
12899 /* Don't try to reuse the display if windows have been split
12900 or such. */
12901 || windows_or_buffers_changed
12902 || cursor_type_changed)
12903 return 0;
12905 /* Can't do this if region may have changed. */
12906 if ((!NILP (Vtransient_mark_mode)
12907 && !NILP (current_buffer->mark_active))
12908 || !NILP (w->region_showing)
12909 || !NILP (Vshow_trailing_whitespace))
12910 return 0;
12912 /* If top-line visibility has changed, give up. */
12913 if (WINDOW_WANTS_HEADER_LINE_P (w)
12914 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12915 return 0;
12917 /* Give up if old or new display is scrolled vertically. We could
12918 make this function handle this, but right now it doesn't. */
12919 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12920 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12921 return 0;
12923 /* The variable new_start now holds the new window start. The old
12924 start `start' can be determined from the current matrix. */
12925 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12926 start = start_row->start.pos;
12927 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12929 /* Clear the desired matrix for the display below. */
12930 clear_glyph_matrix (w->desired_matrix);
12932 if (CHARPOS (new_start) <= CHARPOS (start))
12934 int first_row_y;
12936 /* Don't use this method if the display starts with an ellipsis
12937 displayed for invisible text. It's not easy to handle that case
12938 below, and it's certainly not worth the effort since this is
12939 not a frequent case. */
12940 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12941 return 0;
12943 IF_DEBUG (debug_method_add (w, "twu1"));
12945 /* Display up to a row that can be reused. The variable
12946 last_text_row is set to the last row displayed that displays
12947 text. Note that it.vpos == 0 if or if not there is a
12948 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12949 start_display (&it, w, new_start);
12950 first_row_y = it.current_y;
12951 w->cursor.vpos = -1;
12952 last_text_row = last_reused_text_row = NULL;
12954 while (it.current_y < it.last_visible_y
12955 && !fonts_changed_p)
12957 /* If we have reached into the characters in the START row,
12958 that means the line boundaries have changed. So we
12959 can't start copying with the row START. Maybe it will
12960 work to start copying with the following row. */
12961 while (IT_CHARPOS (it) > CHARPOS (start))
12963 /* Advance to the next row as the "start". */
12964 start_row++;
12965 start = start_row->start.pos;
12966 /* If there are no more rows to try, or just one, give up. */
12967 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12968 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12969 || CHARPOS (start) == ZV)
12971 clear_glyph_matrix (w->desired_matrix);
12972 return 0;
12975 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12977 /* If we have reached alignment,
12978 we can copy the rest of the rows. */
12979 if (IT_CHARPOS (it) == CHARPOS (start))
12980 break;
12982 if (display_line (&it))
12983 last_text_row = it.glyph_row - 1;
12986 /* A value of current_y < last_visible_y means that we stopped
12987 at the previous window start, which in turn means that we
12988 have at least one reusable row. */
12989 if (it.current_y < it.last_visible_y)
12991 /* IT.vpos always starts from 0; it counts text lines. */
12992 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12994 /* Find PT if not already found in the lines displayed. */
12995 if (w->cursor.vpos < 0)
12997 int dy = it.current_y - start_row->y;
12999 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13000 row = row_containing_pos (w, PT, row, NULL, dy);
13001 if (row)
13002 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13003 dy, nrows_scrolled);
13004 else
13006 clear_glyph_matrix (w->desired_matrix);
13007 return 0;
13011 /* Scroll the display. Do it before the current matrix is
13012 changed. The problem here is that update has not yet
13013 run, i.e. part of the current matrix is not up to date.
13014 scroll_run_hook will clear the cursor, and use the
13015 current matrix to get the height of the row the cursor is
13016 in. */
13017 run.current_y = start_row->y;
13018 run.desired_y = it.current_y;
13019 run.height = it.last_visible_y - it.current_y;
13021 if (run.height > 0 && run.current_y != run.desired_y)
13023 update_begin (f);
13024 rif->update_window_begin_hook (w);
13025 rif->clear_window_mouse_face (w);
13026 rif->scroll_run_hook (w, &run);
13027 rif->update_window_end_hook (w, 0, 0);
13028 update_end (f);
13031 /* Shift current matrix down by nrows_scrolled lines. */
13032 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13033 rotate_matrix (w->current_matrix,
13034 start_vpos,
13035 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13036 nrows_scrolled);
13038 /* Disable lines that must be updated. */
13039 for (i = 0; i < it.vpos; ++i)
13040 (start_row + i)->enabled_p = 0;
13042 /* Re-compute Y positions. */
13043 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13044 max_y = it.last_visible_y;
13045 for (row = start_row + nrows_scrolled;
13046 row < bottom_row;
13047 ++row)
13049 row->y = it.current_y;
13050 row->visible_height = row->height;
13052 if (row->y < min_y)
13053 row->visible_height -= min_y - row->y;
13054 if (row->y + row->height > max_y)
13055 row->visible_height -= row->y + row->height - max_y;
13056 row->redraw_fringe_bitmaps_p = 1;
13058 it.current_y += row->height;
13060 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13061 last_reused_text_row = row;
13062 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13063 break;
13066 /* Disable lines in the current matrix which are now
13067 below the window. */
13068 for (++row; row < bottom_row; ++row)
13069 row->enabled_p = 0;
13072 /* Update window_end_pos etc.; last_reused_text_row is the last
13073 reused row from the current matrix containing text, if any.
13074 The value of last_text_row is the last displayed line
13075 containing text. */
13076 if (last_reused_text_row)
13078 w->window_end_bytepos
13079 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13080 w->window_end_pos
13081 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13082 w->window_end_vpos
13083 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13084 w->current_matrix));
13086 else if (last_text_row)
13088 w->window_end_bytepos
13089 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13090 w->window_end_pos
13091 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13092 w->window_end_vpos
13093 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13095 else
13097 /* This window must be completely empty. */
13098 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13099 w->window_end_pos = make_number (Z - ZV);
13100 w->window_end_vpos = make_number (0);
13102 w->window_end_valid = Qnil;
13104 /* Update hint: don't try scrolling again in update_window. */
13105 w->desired_matrix->no_scrolling_p = 1;
13107 #if GLYPH_DEBUG
13108 debug_method_add (w, "try_window_reusing_current_matrix 1");
13109 #endif
13110 return 1;
13112 else if (CHARPOS (new_start) > CHARPOS (start))
13114 struct glyph_row *pt_row, *row;
13115 struct glyph_row *first_reusable_row;
13116 struct glyph_row *first_row_to_display;
13117 int dy;
13118 int yb = window_text_bottom_y (w);
13120 /* Find the row starting at new_start, if there is one. Don't
13121 reuse a partially visible line at the end. */
13122 first_reusable_row = start_row;
13123 while (first_reusable_row->enabled_p
13124 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13125 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13126 < CHARPOS (new_start)))
13127 ++first_reusable_row;
13129 /* Give up if there is no row to reuse. */
13130 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13131 || !first_reusable_row->enabled_p
13132 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13133 != CHARPOS (new_start)))
13134 return 0;
13136 /* We can reuse fully visible rows beginning with
13137 first_reusable_row to the end of the window. Set
13138 first_row_to_display to the first row that cannot be reused.
13139 Set pt_row to the row containing point, if there is any. */
13140 pt_row = NULL;
13141 for (first_row_to_display = first_reusable_row;
13142 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13143 ++first_row_to_display)
13145 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13146 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13147 pt_row = first_row_to_display;
13150 /* Start displaying at the start of first_row_to_display. */
13151 xassert (first_row_to_display->y < yb);
13152 init_to_row_start (&it, w, first_row_to_display);
13154 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13155 - start_vpos);
13156 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13157 - nrows_scrolled);
13158 it.current_y = (first_row_to_display->y - first_reusable_row->y
13159 + WINDOW_HEADER_LINE_HEIGHT (w));
13161 /* Display lines beginning with first_row_to_display in the
13162 desired matrix. Set last_text_row to the last row displayed
13163 that displays text. */
13164 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13165 if (pt_row == NULL)
13166 w->cursor.vpos = -1;
13167 last_text_row = NULL;
13168 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13169 if (display_line (&it))
13170 last_text_row = it.glyph_row - 1;
13172 /* Give up If point isn't in a row displayed or reused. */
13173 if (w->cursor.vpos < 0)
13175 clear_glyph_matrix (w->desired_matrix);
13176 return 0;
13179 /* If point is in a reused row, adjust y and vpos of the cursor
13180 position. */
13181 if (pt_row)
13183 w->cursor.vpos -= nrows_scrolled;
13184 w->cursor.y -= first_reusable_row->y - start_row->y;
13187 /* Scroll the display. */
13188 run.current_y = first_reusable_row->y;
13189 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13190 run.height = it.last_visible_y - run.current_y;
13191 dy = run.current_y - run.desired_y;
13193 if (run.height)
13195 update_begin (f);
13196 rif->update_window_begin_hook (w);
13197 rif->clear_window_mouse_face (w);
13198 rif->scroll_run_hook (w, &run);
13199 rif->update_window_end_hook (w, 0, 0);
13200 update_end (f);
13203 /* Adjust Y positions of reused rows. */
13204 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13205 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13206 max_y = it.last_visible_y;
13207 for (row = first_reusable_row; row < first_row_to_display; ++row)
13209 row->y -= dy;
13210 row->visible_height = row->height;
13211 if (row->y < min_y)
13212 row->visible_height -= min_y - row->y;
13213 if (row->y + row->height > max_y)
13214 row->visible_height -= row->y + row->height - max_y;
13215 row->redraw_fringe_bitmaps_p = 1;
13218 /* Scroll the current matrix. */
13219 xassert (nrows_scrolled > 0);
13220 rotate_matrix (w->current_matrix,
13221 start_vpos,
13222 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13223 -nrows_scrolled);
13225 /* Disable rows not reused. */
13226 for (row -= nrows_scrolled; row < bottom_row; ++row)
13227 row->enabled_p = 0;
13229 /* Point may have moved to a different line, so we cannot assume that
13230 the previous cursor position is valid; locate the correct row. */
13231 if (pt_row)
13233 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13234 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13235 row++)
13237 w->cursor.vpos++;
13238 w->cursor.y = row->y;
13240 if (row < bottom_row)
13242 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13243 while (glyph->charpos < PT)
13245 w->cursor.hpos++;
13246 w->cursor.x += glyph->pixel_width;
13247 glyph++;
13252 /* Adjust window end. A null value of last_text_row means that
13253 the window end is in reused rows which in turn means that
13254 only its vpos can have changed. */
13255 if (last_text_row)
13257 w->window_end_bytepos
13258 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13259 w->window_end_pos
13260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13261 w->window_end_vpos
13262 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13264 else
13266 w->window_end_vpos
13267 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13270 w->window_end_valid = Qnil;
13271 w->desired_matrix->no_scrolling_p = 1;
13273 #if GLYPH_DEBUG
13274 debug_method_add (w, "try_window_reusing_current_matrix 2");
13275 #endif
13276 return 1;
13279 return 0;
13284 /************************************************************************
13285 Window redisplay reusing current matrix when buffer has changed
13286 ************************************************************************/
13288 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13289 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13290 int *, int *));
13291 static struct glyph_row *
13292 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13293 struct glyph_row *));
13296 /* Return the last row in MATRIX displaying text. If row START is
13297 non-null, start searching with that row. IT gives the dimensions
13298 of the display. Value is null if matrix is empty; otherwise it is
13299 a pointer to the row found. */
13301 static struct glyph_row *
13302 find_last_row_displaying_text (matrix, it, start)
13303 struct glyph_matrix *matrix;
13304 struct it *it;
13305 struct glyph_row *start;
13307 struct glyph_row *row, *row_found;
13309 /* Set row_found to the last row in IT->w's current matrix
13310 displaying text. The loop looks funny but think of partially
13311 visible lines. */
13312 row_found = NULL;
13313 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13314 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13316 xassert (row->enabled_p);
13317 row_found = row;
13318 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13319 break;
13320 ++row;
13323 return row_found;
13327 /* Return the last row in the current matrix of W that is not affected
13328 by changes at the start of current_buffer that occurred since W's
13329 current matrix was built. Value is null if no such row exists.
13331 BEG_UNCHANGED us the number of characters unchanged at the start of
13332 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13333 first changed character in current_buffer. Characters at positions <
13334 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13335 when the current matrix was built. */
13337 static struct glyph_row *
13338 find_last_unchanged_at_beg_row (w)
13339 struct window *w;
13341 int first_changed_pos = BEG + BEG_UNCHANGED;
13342 struct glyph_row *row;
13343 struct glyph_row *row_found = NULL;
13344 int yb = window_text_bottom_y (w);
13346 /* Find the last row displaying unchanged text. */
13347 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13348 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13349 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13351 if (/* If row ends before first_changed_pos, it is unchanged,
13352 except in some case. */
13353 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13354 /* When row ends in ZV and we write at ZV it is not
13355 unchanged. */
13356 && !row->ends_at_zv_p
13357 /* When first_changed_pos is the end of a continued line,
13358 row is not unchanged because it may be no longer
13359 continued. */
13360 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13361 && (row->continued_p
13362 || row->exact_window_width_line_p)))
13363 row_found = row;
13365 /* Stop if last visible row. */
13366 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13367 break;
13369 ++row;
13372 return row_found;
13376 /* Find the first glyph row in the current matrix of W that is not
13377 affected by changes at the end of current_buffer since the
13378 time W's current matrix was built.
13380 Return in *DELTA the number of chars by which buffer positions in
13381 unchanged text at the end of current_buffer must be adjusted.
13383 Return in *DELTA_BYTES the corresponding number of bytes.
13385 Value is null if no such row exists, i.e. all rows are affected by
13386 changes. */
13388 static struct glyph_row *
13389 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13390 struct window *w;
13391 int *delta, *delta_bytes;
13393 struct glyph_row *row;
13394 struct glyph_row *row_found = NULL;
13396 *delta = *delta_bytes = 0;
13398 /* Display must not have been paused, otherwise the current matrix
13399 is not up to date. */
13400 if (NILP (w->window_end_valid))
13401 abort ();
13403 /* A value of window_end_pos >= END_UNCHANGED means that the window
13404 end is in the range of changed text. If so, there is no
13405 unchanged row at the end of W's current matrix. */
13406 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13407 return NULL;
13409 /* Set row to the last row in W's current matrix displaying text. */
13410 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13412 /* If matrix is entirely empty, no unchanged row exists. */
13413 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13415 /* The value of row is the last glyph row in the matrix having a
13416 meaningful buffer position in it. The end position of row
13417 corresponds to window_end_pos. This allows us to translate
13418 buffer positions in the current matrix to current buffer
13419 positions for characters not in changed text. */
13420 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13421 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13422 int last_unchanged_pos, last_unchanged_pos_old;
13423 struct glyph_row *first_text_row
13424 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13426 *delta = Z - Z_old;
13427 *delta_bytes = Z_BYTE - Z_BYTE_old;
13429 /* Set last_unchanged_pos to the buffer position of the last
13430 character in the buffer that has not been changed. Z is the
13431 index + 1 of the last character in current_buffer, i.e. by
13432 subtracting END_UNCHANGED we get the index of the last
13433 unchanged character, and we have to add BEG to get its buffer
13434 position. */
13435 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13436 last_unchanged_pos_old = last_unchanged_pos - *delta;
13438 /* Search backward from ROW for a row displaying a line that
13439 starts at a minimum position >= last_unchanged_pos_old. */
13440 for (; row > first_text_row; --row)
13442 /* This used to abort, but it can happen.
13443 It is ok to just stop the search instead here. KFS. */
13444 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13445 break;
13447 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13448 row_found = row;
13452 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13453 abort ();
13455 return row_found;
13459 /* Make sure that glyph rows in the current matrix of window W
13460 reference the same glyph memory as corresponding rows in the
13461 frame's frame matrix. This function is called after scrolling W's
13462 current matrix on a terminal frame in try_window_id and
13463 try_window_reusing_current_matrix. */
13465 static void
13466 sync_frame_with_window_matrix_rows (w)
13467 struct window *w;
13469 struct frame *f = XFRAME (w->frame);
13470 struct glyph_row *window_row, *window_row_end, *frame_row;
13472 /* Preconditions: W must be a leaf window and full-width. Its frame
13473 must have a frame matrix. */
13474 xassert (NILP (w->hchild) && NILP (w->vchild));
13475 xassert (WINDOW_FULL_WIDTH_P (w));
13476 xassert (!FRAME_WINDOW_P (f));
13478 /* If W is a full-width window, glyph pointers in W's current matrix
13479 have, by definition, to be the same as glyph pointers in the
13480 corresponding frame matrix. Note that frame matrices have no
13481 marginal areas (see build_frame_matrix). */
13482 window_row = w->current_matrix->rows;
13483 window_row_end = window_row + w->current_matrix->nrows;
13484 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13485 while (window_row < window_row_end)
13487 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13488 struct glyph *end = window_row->glyphs[LAST_AREA];
13490 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13491 frame_row->glyphs[TEXT_AREA] = start;
13492 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13493 frame_row->glyphs[LAST_AREA] = end;
13495 /* Disable frame rows whose corresponding window rows have
13496 been disabled in try_window_id. */
13497 if (!window_row->enabled_p)
13498 frame_row->enabled_p = 0;
13500 ++window_row, ++frame_row;
13505 /* Find the glyph row in window W containing CHARPOS. Consider all
13506 rows between START and END (not inclusive). END null means search
13507 all rows to the end of the display area of W. Value is the row
13508 containing CHARPOS or null. */
13510 struct glyph_row *
13511 row_containing_pos (w, charpos, start, end, dy)
13512 struct window *w;
13513 int charpos;
13514 struct glyph_row *start, *end;
13515 int dy;
13517 struct glyph_row *row = start;
13518 int last_y;
13520 /* If we happen to start on a header-line, skip that. */
13521 if (row->mode_line_p)
13522 ++row;
13524 if ((end && row >= end) || !row->enabled_p)
13525 return NULL;
13527 last_y = window_text_bottom_y (w) - dy;
13529 while (1)
13531 /* Give up if we have gone too far. */
13532 if (end && row >= end)
13533 return NULL;
13534 /* This formerly returned if they were equal.
13535 I think that both quantities are of a "last plus one" type;
13536 if so, when they are equal, the row is within the screen. -- rms. */
13537 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13538 return NULL;
13540 /* If it is in this row, return this row. */
13541 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13542 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13543 /* The end position of a row equals the start
13544 position of the next row. If CHARPOS is there, we
13545 would rather display it in the next line, except
13546 when this line ends in ZV. */
13547 && !row->ends_at_zv_p
13548 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13549 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13550 return row;
13551 ++row;
13556 /* Try to redisplay window W by reusing its existing display. W's
13557 current matrix must be up to date when this function is called,
13558 i.e. window_end_valid must not be nil.
13560 Value is
13562 1 if display has been updated
13563 0 if otherwise unsuccessful
13564 -1 if redisplay with same window start is known not to succeed
13566 The following steps are performed:
13568 1. Find the last row in the current matrix of W that is not
13569 affected by changes at the start of current_buffer. If no such row
13570 is found, give up.
13572 2. Find the first row in W's current matrix that is not affected by
13573 changes at the end of current_buffer. Maybe there is no such row.
13575 3. Display lines beginning with the row + 1 found in step 1 to the
13576 row found in step 2 or, if step 2 didn't find a row, to the end of
13577 the window.
13579 4. If cursor is not known to appear on the window, give up.
13581 5. If display stopped at the row found in step 2, scroll the
13582 display and current matrix as needed.
13584 6. Maybe display some lines at the end of W, if we must. This can
13585 happen under various circumstances, like a partially visible line
13586 becoming fully visible, or because newly displayed lines are displayed
13587 in smaller font sizes.
13589 7. Update W's window end information. */
13591 static int
13592 try_window_id (w)
13593 struct window *w;
13595 struct frame *f = XFRAME (w->frame);
13596 struct glyph_matrix *current_matrix = w->current_matrix;
13597 struct glyph_matrix *desired_matrix = w->desired_matrix;
13598 struct glyph_row *last_unchanged_at_beg_row;
13599 struct glyph_row *first_unchanged_at_end_row;
13600 struct glyph_row *row;
13601 struct glyph_row *bottom_row;
13602 int bottom_vpos;
13603 struct it it;
13604 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13605 struct text_pos start_pos;
13606 struct run run;
13607 int first_unchanged_at_end_vpos = 0;
13608 struct glyph_row *last_text_row, *last_text_row_at_end;
13609 struct text_pos start;
13610 int first_changed_charpos, last_changed_charpos;
13612 #if GLYPH_DEBUG
13613 if (inhibit_try_window_id)
13614 return 0;
13615 #endif
13617 /* This is handy for debugging. */
13618 #if 0
13619 #define GIVE_UP(X) \
13620 do { \
13621 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13622 return 0; \
13623 } while (0)
13624 #else
13625 #define GIVE_UP(X) return 0
13626 #endif
13628 SET_TEXT_POS_FROM_MARKER (start, w->start);
13630 /* Don't use this for mini-windows because these can show
13631 messages and mini-buffers, and we don't handle that here. */
13632 if (MINI_WINDOW_P (w))
13633 GIVE_UP (1);
13635 /* This flag is used to prevent redisplay optimizations. */
13636 if (windows_or_buffers_changed || cursor_type_changed)
13637 GIVE_UP (2);
13639 /* Verify that narrowing has not changed.
13640 Also verify that we were not told to prevent redisplay optimizations.
13641 It would be nice to further
13642 reduce the number of cases where this prevents try_window_id. */
13643 if (current_buffer->clip_changed
13644 || current_buffer->prevent_redisplay_optimizations_p)
13645 GIVE_UP (3);
13647 /* Window must either use window-based redisplay or be full width. */
13648 if (!FRAME_WINDOW_P (f)
13649 && (!line_ins_del_ok
13650 || !WINDOW_FULL_WIDTH_P (w)))
13651 GIVE_UP (4);
13653 /* Give up if point is not known NOT to appear in W. */
13654 if (PT < CHARPOS (start))
13655 GIVE_UP (5);
13657 /* Another way to prevent redisplay optimizations. */
13658 if (XFASTINT (w->last_modified) == 0)
13659 GIVE_UP (6);
13661 /* Verify that window is not hscrolled. */
13662 if (XFASTINT (w->hscroll) != 0)
13663 GIVE_UP (7);
13665 /* Verify that display wasn't paused. */
13666 if (NILP (w->window_end_valid))
13667 GIVE_UP (8);
13669 /* Can't use this if highlighting a region because a cursor movement
13670 will do more than just set the cursor. */
13671 if (!NILP (Vtransient_mark_mode)
13672 && !NILP (current_buffer->mark_active))
13673 GIVE_UP (9);
13675 /* Likewise if highlighting trailing whitespace. */
13676 if (!NILP (Vshow_trailing_whitespace))
13677 GIVE_UP (11);
13679 /* Likewise if showing a region. */
13680 if (!NILP (w->region_showing))
13681 GIVE_UP (10);
13683 /* Can use this if overlay arrow position and or string have changed. */
13684 if (overlay_arrows_changed_p ())
13685 GIVE_UP (12);
13688 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13689 only if buffer has really changed. The reason is that the gap is
13690 initially at Z for freshly visited files. The code below would
13691 set end_unchanged to 0 in that case. */
13692 if (MODIFF > SAVE_MODIFF
13693 /* This seems to happen sometimes after saving a buffer. */
13694 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13696 if (GPT - BEG < BEG_UNCHANGED)
13697 BEG_UNCHANGED = GPT - BEG;
13698 if (Z - GPT < END_UNCHANGED)
13699 END_UNCHANGED = Z - GPT;
13702 /* The position of the first and last character that has been changed. */
13703 first_changed_charpos = BEG + BEG_UNCHANGED;
13704 last_changed_charpos = Z - END_UNCHANGED;
13706 /* If window starts after a line end, and the last change is in
13707 front of that newline, then changes don't affect the display.
13708 This case happens with stealth-fontification. Note that although
13709 the display is unchanged, glyph positions in the matrix have to
13710 be adjusted, of course. */
13711 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13712 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13713 && ((last_changed_charpos < CHARPOS (start)
13714 && CHARPOS (start) == BEGV)
13715 || (last_changed_charpos < CHARPOS (start) - 1
13716 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13718 int Z_old, delta, Z_BYTE_old, delta_bytes;
13719 struct glyph_row *r0;
13721 /* Compute how many chars/bytes have been added to or removed
13722 from the buffer. */
13723 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13724 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13725 delta = Z - Z_old;
13726 delta_bytes = Z_BYTE - Z_BYTE_old;
13728 /* Give up if PT is not in the window. Note that it already has
13729 been checked at the start of try_window_id that PT is not in
13730 front of the window start. */
13731 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13732 GIVE_UP (13);
13734 /* If window start is unchanged, we can reuse the whole matrix
13735 as is, after adjusting glyph positions. No need to compute
13736 the window end again, since its offset from Z hasn't changed. */
13737 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13738 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13739 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13740 /* PT must not be in a partially visible line. */
13741 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13742 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13744 /* Adjust positions in the glyph matrix. */
13745 if (delta || delta_bytes)
13747 struct glyph_row *r1
13748 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13749 increment_matrix_positions (w->current_matrix,
13750 MATRIX_ROW_VPOS (r0, current_matrix),
13751 MATRIX_ROW_VPOS (r1, current_matrix),
13752 delta, delta_bytes);
13755 /* Set the cursor. */
13756 row = row_containing_pos (w, PT, r0, NULL, 0);
13757 if (row)
13758 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13759 else
13760 abort ();
13761 return 1;
13765 /* Handle the case that changes are all below what is displayed in
13766 the window, and that PT is in the window. This shortcut cannot
13767 be taken if ZV is visible in the window, and text has been added
13768 there that is visible in the window. */
13769 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13770 /* ZV is not visible in the window, or there are no
13771 changes at ZV, actually. */
13772 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13773 || first_changed_charpos == last_changed_charpos))
13775 struct glyph_row *r0;
13777 /* Give up if PT is not in the window. Note that it already has
13778 been checked at the start of try_window_id that PT is not in
13779 front of the window start. */
13780 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13781 GIVE_UP (14);
13783 /* If window start is unchanged, we can reuse the whole matrix
13784 as is, without changing glyph positions since no text has
13785 been added/removed in front of the window end. */
13786 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13787 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13788 /* PT must not be in a partially visible line. */
13789 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13790 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13792 /* We have to compute the window end anew since text
13793 can have been added/removed after it. */
13794 w->window_end_pos
13795 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13796 w->window_end_bytepos
13797 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13799 /* Set the cursor. */
13800 row = row_containing_pos (w, PT, r0, NULL, 0);
13801 if (row)
13802 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13803 else
13804 abort ();
13805 return 2;
13809 /* Give up if window start is in the changed area.
13811 The condition used to read
13813 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13815 but why that was tested escapes me at the moment. */
13816 if (CHARPOS (start) >= first_changed_charpos
13817 && CHARPOS (start) <= last_changed_charpos)
13818 GIVE_UP (15);
13820 /* Check that window start agrees with the start of the first glyph
13821 row in its current matrix. Check this after we know the window
13822 start is not in changed text, otherwise positions would not be
13823 comparable. */
13824 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13825 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13826 GIVE_UP (16);
13828 /* Give up if the window ends in strings. Overlay strings
13829 at the end are difficult to handle, so don't try. */
13830 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13831 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13832 GIVE_UP (20);
13834 /* Compute the position at which we have to start displaying new
13835 lines. Some of the lines at the top of the window might be
13836 reusable because they are not displaying changed text. Find the
13837 last row in W's current matrix not affected by changes at the
13838 start of current_buffer. Value is null if changes start in the
13839 first line of window. */
13840 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13841 if (last_unchanged_at_beg_row)
13843 /* Avoid starting to display in the moddle of a character, a TAB
13844 for instance. This is easier than to set up the iterator
13845 exactly, and it's not a frequent case, so the additional
13846 effort wouldn't really pay off. */
13847 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13848 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13849 && last_unchanged_at_beg_row > w->current_matrix->rows)
13850 --last_unchanged_at_beg_row;
13852 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13853 GIVE_UP (17);
13855 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13856 GIVE_UP (18);
13857 start_pos = it.current.pos;
13859 /* Start displaying new lines in the desired matrix at the same
13860 vpos we would use in the current matrix, i.e. below
13861 last_unchanged_at_beg_row. */
13862 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13863 current_matrix);
13864 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13865 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13867 xassert (it.hpos == 0 && it.current_x == 0);
13869 else
13871 /* There are no reusable lines at the start of the window.
13872 Start displaying in the first text line. */
13873 start_display (&it, w, start);
13874 it.vpos = it.first_vpos;
13875 start_pos = it.current.pos;
13878 /* Find the first row that is not affected by changes at the end of
13879 the buffer. Value will be null if there is no unchanged row, in
13880 which case we must redisplay to the end of the window. delta
13881 will be set to the value by which buffer positions beginning with
13882 first_unchanged_at_end_row have to be adjusted due to text
13883 changes. */
13884 first_unchanged_at_end_row
13885 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13886 IF_DEBUG (debug_delta = delta);
13887 IF_DEBUG (debug_delta_bytes = delta_bytes);
13889 /* Set stop_pos to the buffer position up to which we will have to
13890 display new lines. If first_unchanged_at_end_row != NULL, this
13891 is the buffer position of the start of the line displayed in that
13892 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13893 that we don't stop at a buffer position. */
13894 stop_pos = 0;
13895 if (first_unchanged_at_end_row)
13897 xassert (last_unchanged_at_beg_row == NULL
13898 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13900 /* If this is a continuation line, move forward to the next one
13901 that isn't. Changes in lines above affect this line.
13902 Caution: this may move first_unchanged_at_end_row to a row
13903 not displaying text. */
13904 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13905 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13906 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13907 < it.last_visible_y))
13908 ++first_unchanged_at_end_row;
13910 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13911 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13912 >= it.last_visible_y))
13913 first_unchanged_at_end_row = NULL;
13914 else
13916 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13917 + delta);
13918 first_unchanged_at_end_vpos
13919 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13920 xassert (stop_pos >= Z - END_UNCHANGED);
13923 else if (last_unchanged_at_beg_row == NULL)
13924 GIVE_UP (19);
13927 #if GLYPH_DEBUG
13929 /* Either there is no unchanged row at the end, or the one we have
13930 now displays text. This is a necessary condition for the window
13931 end pos calculation at the end of this function. */
13932 xassert (first_unchanged_at_end_row == NULL
13933 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13935 debug_last_unchanged_at_beg_vpos
13936 = (last_unchanged_at_beg_row
13937 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13938 : -1);
13939 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13941 #endif /* GLYPH_DEBUG != 0 */
13944 /* Display new lines. Set last_text_row to the last new line
13945 displayed which has text on it, i.e. might end up as being the
13946 line where the window_end_vpos is. */
13947 w->cursor.vpos = -1;
13948 last_text_row = NULL;
13949 overlay_arrow_seen = 0;
13950 while (it.current_y < it.last_visible_y
13951 && !fonts_changed_p
13952 && (first_unchanged_at_end_row == NULL
13953 || IT_CHARPOS (it) < stop_pos))
13955 if (display_line (&it))
13956 last_text_row = it.glyph_row - 1;
13959 if (fonts_changed_p)
13960 return -1;
13963 /* Compute differences in buffer positions, y-positions etc. for
13964 lines reused at the bottom of the window. Compute what we can
13965 scroll. */
13966 if (first_unchanged_at_end_row
13967 /* No lines reused because we displayed everything up to the
13968 bottom of the window. */
13969 && it.current_y < it.last_visible_y)
13971 dvpos = (it.vpos
13972 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13973 current_matrix));
13974 dy = it.current_y - first_unchanged_at_end_row->y;
13975 run.current_y = first_unchanged_at_end_row->y;
13976 run.desired_y = run.current_y + dy;
13977 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13979 else
13981 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13982 first_unchanged_at_end_row = NULL;
13984 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13987 /* Find the cursor if not already found. We have to decide whether
13988 PT will appear on this window (it sometimes doesn't, but this is
13989 not a very frequent case.) This decision has to be made before
13990 the current matrix is altered. A value of cursor.vpos < 0 means
13991 that PT is either in one of the lines beginning at
13992 first_unchanged_at_end_row or below the window. Don't care for
13993 lines that might be displayed later at the window end; as
13994 mentioned, this is not a frequent case. */
13995 if (w->cursor.vpos < 0)
13997 /* Cursor in unchanged rows at the top? */
13998 if (PT < CHARPOS (start_pos)
13999 && last_unchanged_at_beg_row)
14001 row = row_containing_pos (w, PT,
14002 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14003 last_unchanged_at_beg_row + 1, 0);
14004 if (row)
14005 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14008 /* Start from first_unchanged_at_end_row looking for PT. */
14009 else if (first_unchanged_at_end_row)
14011 row = row_containing_pos (w, PT - delta,
14012 first_unchanged_at_end_row, NULL, 0);
14013 if (row)
14014 set_cursor_from_row (w, row, w->current_matrix, delta,
14015 delta_bytes, dy, dvpos);
14018 /* Give up if cursor was not found. */
14019 if (w->cursor.vpos < 0)
14021 clear_glyph_matrix (w->desired_matrix);
14022 return -1;
14026 /* Don't let the cursor end in the scroll margins. */
14028 int this_scroll_margin, cursor_height;
14030 this_scroll_margin = max (0, scroll_margin);
14031 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14032 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14033 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14035 if ((w->cursor.y < this_scroll_margin
14036 && CHARPOS (start) > BEGV)
14037 /* Old redisplay didn't take scroll margin into account at the bottom,
14038 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14039 || (w->cursor.y + (make_cursor_line_fully_visible_p
14040 ? cursor_height + this_scroll_margin
14041 : 1)) > it.last_visible_y)
14043 w->cursor.vpos = -1;
14044 clear_glyph_matrix (w->desired_matrix);
14045 return -1;
14049 /* Scroll the display. Do it before changing the current matrix so
14050 that xterm.c doesn't get confused about where the cursor glyph is
14051 found. */
14052 if (dy && run.height)
14054 update_begin (f);
14056 if (FRAME_WINDOW_P (f))
14058 rif->update_window_begin_hook (w);
14059 rif->clear_window_mouse_face (w);
14060 rif->scroll_run_hook (w, &run);
14061 rif->update_window_end_hook (w, 0, 0);
14063 else
14065 /* Terminal frame. In this case, dvpos gives the number of
14066 lines to scroll by; dvpos < 0 means scroll up. */
14067 int first_unchanged_at_end_vpos
14068 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14069 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14070 int end = (WINDOW_TOP_EDGE_LINE (w)
14071 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14072 + window_internal_height (w));
14074 /* Perform the operation on the screen. */
14075 if (dvpos > 0)
14077 /* Scroll last_unchanged_at_beg_row to the end of the
14078 window down dvpos lines. */
14079 set_terminal_window (end);
14081 /* On dumb terminals delete dvpos lines at the end
14082 before inserting dvpos empty lines. */
14083 if (!scroll_region_ok)
14084 ins_del_lines (end - dvpos, -dvpos);
14086 /* Insert dvpos empty lines in front of
14087 last_unchanged_at_beg_row. */
14088 ins_del_lines (from, dvpos);
14090 else if (dvpos < 0)
14092 /* Scroll up last_unchanged_at_beg_vpos to the end of
14093 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14094 set_terminal_window (end);
14096 /* Delete dvpos lines in front of
14097 last_unchanged_at_beg_vpos. ins_del_lines will set
14098 the cursor to the given vpos and emit |dvpos| delete
14099 line sequences. */
14100 ins_del_lines (from + dvpos, dvpos);
14102 /* On a dumb terminal insert dvpos empty lines at the
14103 end. */
14104 if (!scroll_region_ok)
14105 ins_del_lines (end + dvpos, -dvpos);
14108 set_terminal_window (0);
14111 update_end (f);
14114 /* Shift reused rows of the current matrix to the right position.
14115 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14116 text. */
14117 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14118 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14119 if (dvpos < 0)
14121 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14122 bottom_vpos, dvpos);
14123 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14124 bottom_vpos, 0);
14126 else if (dvpos > 0)
14128 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14129 bottom_vpos, dvpos);
14130 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14131 first_unchanged_at_end_vpos + dvpos, 0);
14134 /* For frame-based redisplay, make sure that current frame and window
14135 matrix are in sync with respect to glyph memory. */
14136 if (!FRAME_WINDOW_P (f))
14137 sync_frame_with_window_matrix_rows (w);
14139 /* Adjust buffer positions in reused rows. */
14140 if (delta)
14141 increment_matrix_positions (current_matrix,
14142 first_unchanged_at_end_vpos + dvpos,
14143 bottom_vpos, delta, delta_bytes);
14145 /* Adjust Y positions. */
14146 if (dy)
14147 shift_glyph_matrix (w, current_matrix,
14148 first_unchanged_at_end_vpos + dvpos,
14149 bottom_vpos, dy);
14151 if (first_unchanged_at_end_row)
14153 first_unchanged_at_end_row += dvpos;
14154 if (first_unchanged_at_end_row->y >= it.last_visible_y
14155 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14156 first_unchanged_at_end_row = NULL;
14159 /* If scrolling up, there may be some lines to display at the end of
14160 the window. */
14161 last_text_row_at_end = NULL;
14162 if (dy < 0)
14164 /* Scrolling up can leave for example a partially visible line
14165 at the end of the window to be redisplayed. */
14166 /* Set last_row to the glyph row in the current matrix where the
14167 window end line is found. It has been moved up or down in
14168 the matrix by dvpos. */
14169 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14170 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14172 /* If last_row is the window end line, it should display text. */
14173 xassert (last_row->displays_text_p);
14175 /* If window end line was partially visible before, begin
14176 displaying at that line. Otherwise begin displaying with the
14177 line following it. */
14178 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14180 init_to_row_start (&it, w, last_row);
14181 it.vpos = last_vpos;
14182 it.current_y = last_row->y;
14184 else
14186 init_to_row_end (&it, w, last_row);
14187 it.vpos = 1 + last_vpos;
14188 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14189 ++last_row;
14192 /* We may start in a continuation line. If so, we have to
14193 get the right continuation_lines_width and current_x. */
14194 it.continuation_lines_width = last_row->continuation_lines_width;
14195 it.hpos = it.current_x = 0;
14197 /* Display the rest of the lines at the window end. */
14198 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14199 while (it.current_y < it.last_visible_y
14200 && !fonts_changed_p)
14202 /* Is it always sure that the display agrees with lines in
14203 the current matrix? I don't think so, so we mark rows
14204 displayed invalid in the current matrix by setting their
14205 enabled_p flag to zero. */
14206 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14207 if (display_line (&it))
14208 last_text_row_at_end = it.glyph_row - 1;
14212 /* Update window_end_pos and window_end_vpos. */
14213 if (first_unchanged_at_end_row
14214 && !last_text_row_at_end)
14216 /* Window end line if one of the preserved rows from the current
14217 matrix. Set row to the last row displaying text in current
14218 matrix starting at first_unchanged_at_end_row, after
14219 scrolling. */
14220 xassert (first_unchanged_at_end_row->displays_text_p);
14221 row = find_last_row_displaying_text (w->current_matrix, &it,
14222 first_unchanged_at_end_row);
14223 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14225 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14226 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14227 w->window_end_vpos
14228 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14229 xassert (w->window_end_bytepos >= 0);
14230 IF_DEBUG (debug_method_add (w, "A"));
14232 else if (last_text_row_at_end)
14234 w->window_end_pos
14235 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14236 w->window_end_bytepos
14237 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14238 w->window_end_vpos
14239 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14240 xassert (w->window_end_bytepos >= 0);
14241 IF_DEBUG (debug_method_add (w, "B"));
14243 else if (last_text_row)
14245 /* We have displayed either to the end of the window or at the
14246 end of the window, i.e. the last row with text is to be found
14247 in the desired matrix. */
14248 w->window_end_pos
14249 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14250 w->window_end_bytepos
14251 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14252 w->window_end_vpos
14253 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14254 xassert (w->window_end_bytepos >= 0);
14256 else if (first_unchanged_at_end_row == NULL
14257 && last_text_row == NULL
14258 && last_text_row_at_end == NULL)
14260 /* Displayed to end of window, but no line containing text was
14261 displayed. Lines were deleted at the end of the window. */
14262 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14263 int vpos = XFASTINT (w->window_end_vpos);
14264 struct glyph_row *current_row = current_matrix->rows + vpos;
14265 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14267 for (row = NULL;
14268 row == NULL && vpos >= first_vpos;
14269 --vpos, --current_row, --desired_row)
14271 if (desired_row->enabled_p)
14273 if (desired_row->displays_text_p)
14274 row = desired_row;
14276 else if (current_row->displays_text_p)
14277 row = current_row;
14280 xassert (row != NULL);
14281 w->window_end_vpos = make_number (vpos + 1);
14282 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14283 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14284 xassert (w->window_end_bytepos >= 0);
14285 IF_DEBUG (debug_method_add (w, "C"));
14287 else
14288 abort ();
14290 #if 0 /* This leads to problems, for instance when the cursor is
14291 at ZV, and the cursor line displays no text. */
14292 /* Disable rows below what's displayed in the window. This makes
14293 debugging easier. */
14294 enable_glyph_matrix_rows (current_matrix,
14295 XFASTINT (w->window_end_vpos) + 1,
14296 bottom_vpos, 0);
14297 #endif
14299 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14300 debug_end_vpos = XFASTINT (w->window_end_vpos));
14302 /* Record that display has not been completed. */
14303 w->window_end_valid = Qnil;
14304 w->desired_matrix->no_scrolling_p = 1;
14305 return 3;
14307 #undef GIVE_UP
14312 /***********************************************************************
14313 More debugging support
14314 ***********************************************************************/
14316 #if GLYPH_DEBUG
14318 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14319 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14320 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14323 /* Dump the contents of glyph matrix MATRIX on stderr.
14325 GLYPHS 0 means don't show glyph contents.
14326 GLYPHS 1 means show glyphs in short form
14327 GLYPHS > 1 means show glyphs in long form. */
14329 void
14330 dump_glyph_matrix (matrix, glyphs)
14331 struct glyph_matrix *matrix;
14332 int glyphs;
14334 int i;
14335 for (i = 0; i < matrix->nrows; ++i)
14336 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14340 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14341 the glyph row and area where the glyph comes from. */
14343 void
14344 dump_glyph (row, glyph, area)
14345 struct glyph_row *row;
14346 struct glyph *glyph;
14347 int area;
14349 if (glyph->type == CHAR_GLYPH)
14351 fprintf (stderr,
14352 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14353 glyph - row->glyphs[TEXT_AREA],
14354 'C',
14355 glyph->charpos,
14356 (BUFFERP (glyph->object)
14357 ? 'B'
14358 : (STRINGP (glyph->object)
14359 ? 'S'
14360 : '-')),
14361 glyph->pixel_width,
14362 glyph->u.ch,
14363 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14364 ? glyph->u.ch
14365 : '.'),
14366 glyph->face_id,
14367 glyph->left_box_line_p,
14368 glyph->right_box_line_p);
14370 else if (glyph->type == STRETCH_GLYPH)
14372 fprintf (stderr,
14373 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14374 glyph - row->glyphs[TEXT_AREA],
14375 'S',
14376 glyph->charpos,
14377 (BUFFERP (glyph->object)
14378 ? 'B'
14379 : (STRINGP (glyph->object)
14380 ? 'S'
14381 : '-')),
14382 glyph->pixel_width,
14384 '.',
14385 glyph->face_id,
14386 glyph->left_box_line_p,
14387 glyph->right_box_line_p);
14389 else if (glyph->type == IMAGE_GLYPH)
14391 fprintf (stderr,
14392 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14393 glyph - row->glyphs[TEXT_AREA],
14394 'I',
14395 glyph->charpos,
14396 (BUFFERP (glyph->object)
14397 ? 'B'
14398 : (STRINGP (glyph->object)
14399 ? 'S'
14400 : '-')),
14401 glyph->pixel_width,
14402 glyph->u.img_id,
14403 '.',
14404 glyph->face_id,
14405 glyph->left_box_line_p,
14406 glyph->right_box_line_p);
14411 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14412 GLYPHS 0 means don't show glyph contents.
14413 GLYPHS 1 means show glyphs in short form
14414 GLYPHS > 1 means show glyphs in long form. */
14416 void
14417 dump_glyph_row (row, vpos, glyphs)
14418 struct glyph_row *row;
14419 int vpos, glyphs;
14421 if (glyphs != 1)
14423 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14424 fprintf (stderr, "======================================================================\n");
14426 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14427 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14428 vpos,
14429 MATRIX_ROW_START_CHARPOS (row),
14430 MATRIX_ROW_END_CHARPOS (row),
14431 row->used[TEXT_AREA],
14432 row->contains_overlapping_glyphs_p,
14433 row->enabled_p,
14434 row->truncated_on_left_p,
14435 row->truncated_on_right_p,
14436 row->continued_p,
14437 MATRIX_ROW_CONTINUATION_LINE_P (row),
14438 row->displays_text_p,
14439 row->ends_at_zv_p,
14440 row->fill_line_p,
14441 row->ends_in_middle_of_char_p,
14442 row->starts_in_middle_of_char_p,
14443 row->mouse_face_p,
14444 row->x,
14445 row->y,
14446 row->pixel_width,
14447 row->height,
14448 row->visible_height,
14449 row->ascent,
14450 row->phys_ascent);
14451 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14452 row->end.overlay_string_index,
14453 row->continuation_lines_width);
14454 fprintf (stderr, "%9d %5d\n",
14455 CHARPOS (row->start.string_pos),
14456 CHARPOS (row->end.string_pos));
14457 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14458 row->end.dpvec_index);
14461 if (glyphs > 1)
14463 int area;
14465 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14467 struct glyph *glyph = row->glyphs[area];
14468 struct glyph *glyph_end = glyph + row->used[area];
14470 /* Glyph for a line end in text. */
14471 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14472 ++glyph_end;
14474 if (glyph < glyph_end)
14475 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14477 for (; glyph < glyph_end; ++glyph)
14478 dump_glyph (row, glyph, area);
14481 else if (glyphs == 1)
14483 int area;
14485 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14487 char *s = (char *) alloca (row->used[area] + 1);
14488 int i;
14490 for (i = 0; i < row->used[area]; ++i)
14492 struct glyph *glyph = row->glyphs[area] + i;
14493 if (glyph->type == CHAR_GLYPH
14494 && glyph->u.ch < 0x80
14495 && glyph->u.ch >= ' ')
14496 s[i] = glyph->u.ch;
14497 else
14498 s[i] = '.';
14501 s[i] = '\0';
14502 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14508 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14509 Sdump_glyph_matrix, 0, 1, "p",
14510 doc: /* Dump the current matrix of the selected window to stderr.
14511 Shows contents of glyph row structures. With non-nil
14512 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14513 glyphs in short form, otherwise show glyphs in long form. */)
14514 (glyphs)
14515 Lisp_Object glyphs;
14517 struct window *w = XWINDOW (selected_window);
14518 struct buffer *buffer = XBUFFER (w->buffer);
14520 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14521 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14522 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14523 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14524 fprintf (stderr, "=============================================\n");
14525 dump_glyph_matrix (w->current_matrix,
14526 NILP (glyphs) ? 0 : XINT (glyphs));
14527 return Qnil;
14531 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14532 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14535 struct frame *f = XFRAME (selected_frame);
14536 dump_glyph_matrix (f->current_matrix, 1);
14537 return Qnil;
14541 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14542 doc: /* Dump glyph row ROW to stderr.
14543 GLYPH 0 means don't dump glyphs.
14544 GLYPH 1 means dump glyphs in short form.
14545 GLYPH > 1 or omitted means dump glyphs in long form. */)
14546 (row, glyphs)
14547 Lisp_Object row, glyphs;
14549 struct glyph_matrix *matrix;
14550 int vpos;
14552 CHECK_NUMBER (row);
14553 matrix = XWINDOW (selected_window)->current_matrix;
14554 vpos = XINT (row);
14555 if (vpos >= 0 && vpos < matrix->nrows)
14556 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14557 vpos,
14558 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14559 return Qnil;
14563 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14564 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14565 GLYPH 0 means don't dump glyphs.
14566 GLYPH 1 means dump glyphs in short form.
14567 GLYPH > 1 or omitted means dump glyphs in long form. */)
14568 (row, glyphs)
14569 Lisp_Object row, glyphs;
14571 struct frame *sf = SELECTED_FRAME ();
14572 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14573 int vpos;
14575 CHECK_NUMBER (row);
14576 vpos = XINT (row);
14577 if (vpos >= 0 && vpos < m->nrows)
14578 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14579 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14580 return Qnil;
14584 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14585 doc: /* Toggle tracing of redisplay.
14586 With ARG, turn tracing on if and only if ARG is positive. */)
14587 (arg)
14588 Lisp_Object arg;
14590 if (NILP (arg))
14591 trace_redisplay_p = !trace_redisplay_p;
14592 else
14594 arg = Fprefix_numeric_value (arg);
14595 trace_redisplay_p = XINT (arg) > 0;
14598 return Qnil;
14602 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14603 doc: /* Like `format', but print result to stderr.
14604 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14605 (nargs, args)
14606 int nargs;
14607 Lisp_Object *args;
14609 Lisp_Object s = Fformat (nargs, args);
14610 fprintf (stderr, "%s", SDATA (s));
14611 return Qnil;
14614 #endif /* GLYPH_DEBUG */
14618 /***********************************************************************
14619 Building Desired Matrix Rows
14620 ***********************************************************************/
14622 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14623 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14625 static struct glyph_row *
14626 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14627 struct window *w;
14628 Lisp_Object overlay_arrow_string;
14630 struct frame *f = XFRAME (WINDOW_FRAME (w));
14631 struct buffer *buffer = XBUFFER (w->buffer);
14632 struct buffer *old = current_buffer;
14633 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14634 int arrow_len = SCHARS (overlay_arrow_string);
14635 const unsigned char *arrow_end = arrow_string + arrow_len;
14636 const unsigned char *p;
14637 struct it it;
14638 int multibyte_p;
14639 int n_glyphs_before;
14641 set_buffer_temp (buffer);
14642 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14643 it.glyph_row->used[TEXT_AREA] = 0;
14644 SET_TEXT_POS (it.position, 0, 0);
14646 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14647 p = arrow_string;
14648 while (p < arrow_end)
14650 Lisp_Object face, ilisp;
14652 /* Get the next character. */
14653 if (multibyte_p)
14654 it.c = string_char_and_length (p, arrow_len, &it.len);
14655 else
14656 it.c = *p, it.len = 1;
14657 p += it.len;
14659 /* Get its face. */
14660 ilisp = make_number (p - arrow_string);
14661 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14662 it.face_id = compute_char_face (f, it.c, face);
14664 /* Compute its width, get its glyphs. */
14665 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14666 SET_TEXT_POS (it.position, -1, -1);
14667 PRODUCE_GLYPHS (&it);
14669 /* If this character doesn't fit any more in the line, we have
14670 to remove some glyphs. */
14671 if (it.current_x > it.last_visible_x)
14673 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14674 break;
14678 set_buffer_temp (old);
14679 return it.glyph_row;
14683 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14684 glyphs are only inserted for terminal frames since we can't really
14685 win with truncation glyphs when partially visible glyphs are
14686 involved. Which glyphs to insert is determined by
14687 produce_special_glyphs. */
14689 static void
14690 insert_left_trunc_glyphs (it)
14691 struct it *it;
14693 struct it truncate_it;
14694 struct glyph *from, *end, *to, *toend;
14696 xassert (!FRAME_WINDOW_P (it->f));
14698 /* Get the truncation glyphs. */
14699 truncate_it = *it;
14700 truncate_it.current_x = 0;
14701 truncate_it.face_id = DEFAULT_FACE_ID;
14702 truncate_it.glyph_row = &scratch_glyph_row;
14703 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14704 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14705 truncate_it.object = make_number (0);
14706 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14708 /* Overwrite glyphs from IT with truncation glyphs. */
14709 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14710 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14711 to = it->glyph_row->glyphs[TEXT_AREA];
14712 toend = to + it->glyph_row->used[TEXT_AREA];
14714 while (from < end)
14715 *to++ = *from++;
14717 /* There may be padding glyphs left over. Overwrite them too. */
14718 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14720 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14721 while (from < end)
14722 *to++ = *from++;
14725 if (to > toend)
14726 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14730 /* Compute the pixel height and width of IT->glyph_row.
14732 Most of the time, ascent and height of a display line will be equal
14733 to the max_ascent and max_height values of the display iterator
14734 structure. This is not the case if
14736 1. We hit ZV without displaying anything. In this case, max_ascent
14737 and max_height will be zero.
14739 2. We have some glyphs that don't contribute to the line height.
14740 (The glyph row flag contributes_to_line_height_p is for future
14741 pixmap extensions).
14743 The first case is easily covered by using default values because in
14744 these cases, the line height does not really matter, except that it
14745 must not be zero. */
14747 static void
14748 compute_line_metrics (it)
14749 struct it *it;
14751 struct glyph_row *row = it->glyph_row;
14752 int area, i;
14754 if (FRAME_WINDOW_P (it->f))
14756 int i, min_y, max_y;
14758 /* The line may consist of one space only, that was added to
14759 place the cursor on it. If so, the row's height hasn't been
14760 computed yet. */
14761 if (row->height == 0)
14763 if (it->max_ascent + it->max_descent == 0)
14764 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14765 row->ascent = it->max_ascent;
14766 row->height = it->max_ascent + it->max_descent;
14767 row->phys_ascent = it->max_phys_ascent;
14768 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14769 row->extra_line_spacing = it->max_extra_line_spacing;
14772 /* Compute the width of this line. */
14773 row->pixel_width = row->x;
14774 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14775 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14777 xassert (row->pixel_width >= 0);
14778 xassert (row->ascent >= 0 && row->height > 0);
14780 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14781 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14783 /* If first line's physical ascent is larger than its logical
14784 ascent, use the physical ascent, and make the row taller.
14785 This makes accented characters fully visible. */
14786 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14787 && row->phys_ascent > row->ascent)
14789 row->height += row->phys_ascent - row->ascent;
14790 row->ascent = row->phys_ascent;
14793 /* Compute how much of the line is visible. */
14794 row->visible_height = row->height;
14796 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14797 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14799 if (row->y < min_y)
14800 row->visible_height -= min_y - row->y;
14801 if (row->y + row->height > max_y)
14802 row->visible_height -= row->y + row->height - max_y;
14804 else
14806 row->pixel_width = row->used[TEXT_AREA];
14807 if (row->continued_p)
14808 row->pixel_width -= it->continuation_pixel_width;
14809 else if (row->truncated_on_right_p)
14810 row->pixel_width -= it->truncation_pixel_width;
14811 row->ascent = row->phys_ascent = 0;
14812 row->height = row->phys_height = row->visible_height = 1;
14813 row->extra_line_spacing = 0;
14816 /* Compute a hash code for this row. */
14817 row->hash = 0;
14818 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14819 for (i = 0; i < row->used[area]; ++i)
14820 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14821 + row->glyphs[area][i].u.val
14822 + row->glyphs[area][i].face_id
14823 + row->glyphs[area][i].padding_p
14824 + (row->glyphs[area][i].type << 2));
14826 it->max_ascent = it->max_descent = 0;
14827 it->max_phys_ascent = it->max_phys_descent = 0;
14831 /* Append one space to the glyph row of iterator IT if doing a
14832 window-based redisplay. The space has the same face as
14833 IT->face_id. Value is non-zero if a space was added.
14835 This function is called to make sure that there is always one glyph
14836 at the end of a glyph row that the cursor can be set on under
14837 window-systems. (If there weren't such a glyph we would not know
14838 how wide and tall a box cursor should be displayed).
14840 At the same time this space let's a nicely handle clearing to the
14841 end of the line if the row ends in italic text. */
14843 static int
14844 append_space_for_newline (it, default_face_p)
14845 struct it *it;
14846 int default_face_p;
14848 if (FRAME_WINDOW_P (it->f))
14850 int n = it->glyph_row->used[TEXT_AREA];
14852 if (it->glyph_row->glyphs[TEXT_AREA] + n
14853 < it->glyph_row->glyphs[1 + TEXT_AREA])
14855 /* Save some values that must not be changed.
14856 Must save IT->c and IT->len because otherwise
14857 ITERATOR_AT_END_P wouldn't work anymore after
14858 append_space_for_newline has been called. */
14859 enum display_element_type saved_what = it->what;
14860 int saved_c = it->c, saved_len = it->len;
14861 int saved_x = it->current_x;
14862 int saved_face_id = it->face_id;
14863 struct text_pos saved_pos;
14864 Lisp_Object saved_object;
14865 struct face *face;
14867 saved_object = it->object;
14868 saved_pos = it->position;
14870 it->what = IT_CHARACTER;
14871 bzero (&it->position, sizeof it->position);
14872 it->object = make_number (0);
14873 it->c = ' ';
14874 it->len = 1;
14876 if (default_face_p)
14877 it->face_id = DEFAULT_FACE_ID;
14878 else if (it->face_before_selective_p)
14879 it->face_id = it->saved_face_id;
14880 face = FACE_FROM_ID (it->f, it->face_id);
14881 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14883 PRODUCE_GLYPHS (it);
14885 it->override_ascent = -1;
14886 it->constrain_row_ascent_descent_p = 0;
14887 it->current_x = saved_x;
14888 it->object = saved_object;
14889 it->position = saved_pos;
14890 it->what = saved_what;
14891 it->face_id = saved_face_id;
14892 it->len = saved_len;
14893 it->c = saved_c;
14894 return 1;
14898 return 0;
14902 /* Extend the face of the last glyph in the text area of IT->glyph_row
14903 to the end of the display line. Called from display_line.
14904 If the glyph row is empty, add a space glyph to it so that we
14905 know the face to draw. Set the glyph row flag fill_line_p. */
14907 static void
14908 extend_face_to_end_of_line (it)
14909 struct it *it;
14911 struct face *face;
14912 struct frame *f = it->f;
14914 /* If line is already filled, do nothing. */
14915 if (it->current_x >= it->last_visible_x)
14916 return;
14918 /* Face extension extends the background and box of IT->face_id
14919 to the end of the line. If the background equals the background
14920 of the frame, we don't have to do anything. */
14921 if (it->face_before_selective_p)
14922 face = FACE_FROM_ID (it->f, it->saved_face_id);
14923 else
14924 face = FACE_FROM_ID (f, it->face_id);
14926 if (FRAME_WINDOW_P (f)
14927 && face->box == FACE_NO_BOX
14928 && face->background == FRAME_BACKGROUND_PIXEL (f)
14929 && !face->stipple)
14930 return;
14932 /* Set the glyph row flag indicating that the face of the last glyph
14933 in the text area has to be drawn to the end of the text area. */
14934 it->glyph_row->fill_line_p = 1;
14936 /* If current character of IT is not ASCII, make sure we have the
14937 ASCII face. This will be automatically undone the next time
14938 get_next_display_element returns a multibyte character. Note
14939 that the character will always be single byte in unibyte text. */
14940 if (!SINGLE_BYTE_CHAR_P (it->c))
14942 it->face_id = FACE_FOR_CHAR (f, face, 0);
14945 if (FRAME_WINDOW_P (f))
14947 /* If the row is empty, add a space with the current face of IT,
14948 so that we know which face to draw. */
14949 if (it->glyph_row->used[TEXT_AREA] == 0)
14951 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14952 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14953 it->glyph_row->used[TEXT_AREA] = 1;
14956 else
14958 /* Save some values that must not be changed. */
14959 int saved_x = it->current_x;
14960 struct text_pos saved_pos;
14961 Lisp_Object saved_object;
14962 enum display_element_type saved_what = it->what;
14963 int saved_face_id = it->face_id;
14965 saved_object = it->object;
14966 saved_pos = it->position;
14968 it->what = IT_CHARACTER;
14969 bzero (&it->position, sizeof it->position);
14970 it->object = make_number (0);
14971 it->c = ' ';
14972 it->len = 1;
14973 it->face_id = face->id;
14975 PRODUCE_GLYPHS (it);
14977 while (it->current_x <= it->last_visible_x)
14978 PRODUCE_GLYPHS (it);
14980 /* Don't count these blanks really. It would let us insert a left
14981 truncation glyph below and make us set the cursor on them, maybe. */
14982 it->current_x = saved_x;
14983 it->object = saved_object;
14984 it->position = saved_pos;
14985 it->what = saved_what;
14986 it->face_id = saved_face_id;
14991 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14992 trailing whitespace. */
14994 static int
14995 trailing_whitespace_p (charpos)
14996 int charpos;
14998 int bytepos = CHAR_TO_BYTE (charpos);
14999 int c = 0;
15001 while (bytepos < ZV_BYTE
15002 && (c = FETCH_CHAR (bytepos),
15003 c == ' ' || c == '\t'))
15004 ++bytepos;
15006 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15008 if (bytepos != PT_BYTE)
15009 return 1;
15011 return 0;
15015 /* Highlight trailing whitespace, if any, in ROW. */
15017 void
15018 highlight_trailing_whitespace (f, row)
15019 struct frame *f;
15020 struct glyph_row *row;
15022 int used = row->used[TEXT_AREA];
15024 if (used)
15026 struct glyph *start = row->glyphs[TEXT_AREA];
15027 struct glyph *glyph = start + used - 1;
15029 /* Skip over glyphs inserted to display the cursor at the
15030 end of a line, for extending the face of the last glyph
15031 to the end of the line on terminals, and for truncation
15032 and continuation glyphs. */
15033 while (glyph >= start
15034 && glyph->type == CHAR_GLYPH
15035 && INTEGERP (glyph->object))
15036 --glyph;
15038 /* If last glyph is a space or stretch, and it's trailing
15039 whitespace, set the face of all trailing whitespace glyphs in
15040 IT->glyph_row to `trailing-whitespace'. */
15041 if (glyph >= start
15042 && BUFFERP (glyph->object)
15043 && (glyph->type == STRETCH_GLYPH
15044 || (glyph->type == CHAR_GLYPH
15045 && glyph->u.ch == ' '))
15046 && trailing_whitespace_p (glyph->charpos))
15048 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15049 if (face_id < 0)
15050 return;
15052 while (glyph >= start
15053 && BUFFERP (glyph->object)
15054 && (glyph->type == STRETCH_GLYPH
15055 || (glyph->type == CHAR_GLYPH
15056 && glyph->u.ch == ' ')))
15057 (glyph--)->face_id = face_id;
15063 /* Value is non-zero if glyph row ROW in window W should be
15064 used to hold the cursor. */
15066 static int
15067 cursor_row_p (w, row)
15068 struct window *w;
15069 struct glyph_row *row;
15071 int cursor_row_p = 1;
15073 if (PT == MATRIX_ROW_END_CHARPOS (row))
15075 /* If the row ends with a newline from a string, we don't want
15076 the cursor there, but we still want it at the start of the
15077 string if the string starts in this row.
15078 If the row is continued it doesn't end in a newline. */
15079 if (CHARPOS (row->end.string_pos) >= 0)
15080 cursor_row_p = (row->continued_p
15081 || PT >= MATRIX_ROW_START_CHARPOS (row));
15082 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15084 /* If the row ends in middle of a real character,
15085 and the line is continued, we want the cursor here.
15086 That's because MATRIX_ROW_END_CHARPOS would equal
15087 PT if PT is before the character. */
15088 if (!row->ends_in_ellipsis_p)
15089 cursor_row_p = row->continued_p;
15090 else
15091 /* If the row ends in an ellipsis, then
15092 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15093 We want that position to be displayed after the ellipsis. */
15094 cursor_row_p = 0;
15096 /* If the row ends at ZV, display the cursor at the end of that
15097 row instead of at the start of the row below. */
15098 else if (row->ends_at_zv_p)
15099 cursor_row_p = 1;
15100 else
15101 cursor_row_p = 0;
15104 return cursor_row_p;
15108 /* Construct the glyph row IT->glyph_row in the desired matrix of
15109 IT->w from text at the current position of IT. See dispextern.h
15110 for an overview of struct it. Value is non-zero if
15111 IT->glyph_row displays text, as opposed to a line displaying ZV
15112 only. */
15114 static int
15115 display_line (it)
15116 struct it *it;
15118 struct glyph_row *row = it->glyph_row;
15119 Lisp_Object overlay_arrow_string;
15121 /* We always start displaying at hpos zero even if hscrolled. */
15122 xassert (it->hpos == 0 && it->current_x == 0);
15124 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15125 >= it->w->desired_matrix->nrows)
15127 it->w->nrows_scale_factor++;
15128 fonts_changed_p = 1;
15129 return 0;
15132 /* Is IT->w showing the region? */
15133 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15135 /* Clear the result glyph row and enable it. */
15136 prepare_desired_row (row);
15138 row->y = it->current_y;
15139 row->start = it->start;
15140 row->continuation_lines_width = it->continuation_lines_width;
15141 row->displays_text_p = 1;
15142 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15143 it->starts_in_middle_of_char_p = 0;
15145 /* Arrange the overlays nicely for our purposes. Usually, we call
15146 display_line on only one line at a time, in which case this
15147 can't really hurt too much, or we call it on lines which appear
15148 one after another in the buffer, in which case all calls to
15149 recenter_overlay_lists but the first will be pretty cheap. */
15150 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15152 /* Move over display elements that are not visible because we are
15153 hscrolled. This may stop at an x-position < IT->first_visible_x
15154 if the first glyph is partially visible or if we hit a line end. */
15155 if (it->current_x < it->first_visible_x)
15157 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15158 MOVE_TO_POS | MOVE_TO_X);
15161 /* Get the initial row height. This is either the height of the
15162 text hscrolled, if there is any, or zero. */
15163 row->ascent = it->max_ascent;
15164 row->height = it->max_ascent + it->max_descent;
15165 row->phys_ascent = it->max_phys_ascent;
15166 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15167 row->extra_line_spacing = it->max_extra_line_spacing;
15169 /* Loop generating characters. The loop is left with IT on the next
15170 character to display. */
15171 while (1)
15173 int n_glyphs_before, hpos_before, x_before;
15174 int x, i, nglyphs;
15175 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15177 /* Retrieve the next thing to display. Value is zero if end of
15178 buffer reached. */
15179 if (!get_next_display_element (it))
15181 /* Maybe add a space at the end of this line that is used to
15182 display the cursor there under X. Set the charpos of the
15183 first glyph of blank lines not corresponding to any text
15184 to -1. */
15185 #ifdef HAVE_WINDOW_SYSTEM
15186 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15187 row->exact_window_width_line_p = 1;
15188 else
15189 #endif /* HAVE_WINDOW_SYSTEM */
15190 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15191 || row->used[TEXT_AREA] == 0)
15193 row->glyphs[TEXT_AREA]->charpos = -1;
15194 row->displays_text_p = 0;
15196 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15197 && (!MINI_WINDOW_P (it->w)
15198 || (minibuf_level && EQ (it->window, minibuf_window))))
15199 row->indicate_empty_line_p = 1;
15202 it->continuation_lines_width = 0;
15203 row->ends_at_zv_p = 1;
15204 break;
15207 /* Now, get the metrics of what we want to display. This also
15208 generates glyphs in `row' (which is IT->glyph_row). */
15209 n_glyphs_before = row->used[TEXT_AREA];
15210 x = it->current_x;
15212 /* Remember the line height so far in case the next element doesn't
15213 fit on the line. */
15214 if (!it->truncate_lines_p)
15216 ascent = it->max_ascent;
15217 descent = it->max_descent;
15218 phys_ascent = it->max_phys_ascent;
15219 phys_descent = it->max_phys_descent;
15222 PRODUCE_GLYPHS (it);
15224 /* If this display element was in marginal areas, continue with
15225 the next one. */
15226 if (it->area != TEXT_AREA)
15228 row->ascent = max (row->ascent, it->max_ascent);
15229 row->height = max (row->height, it->max_ascent + it->max_descent);
15230 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15231 row->phys_height = max (row->phys_height,
15232 it->max_phys_ascent + it->max_phys_descent);
15233 row->extra_line_spacing = max (row->extra_line_spacing,
15234 it->max_extra_line_spacing);
15235 set_iterator_to_next (it, 1);
15236 continue;
15239 /* Does the display element fit on the line? If we truncate
15240 lines, we should draw past the right edge of the window. If
15241 we don't truncate, we want to stop so that we can display the
15242 continuation glyph before the right margin. If lines are
15243 continued, there are two possible strategies for characters
15244 resulting in more than 1 glyph (e.g. tabs): Display as many
15245 glyphs as possible in this line and leave the rest for the
15246 continuation line, or display the whole element in the next
15247 line. Original redisplay did the former, so we do it also. */
15248 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15249 hpos_before = it->hpos;
15250 x_before = x;
15252 if (/* Not a newline. */
15253 nglyphs > 0
15254 /* Glyphs produced fit entirely in the line. */
15255 && it->current_x < it->last_visible_x)
15257 it->hpos += nglyphs;
15258 row->ascent = max (row->ascent, it->max_ascent);
15259 row->height = max (row->height, it->max_ascent + it->max_descent);
15260 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15261 row->phys_height = max (row->phys_height,
15262 it->max_phys_ascent + it->max_phys_descent);
15263 row->extra_line_spacing = max (row->extra_line_spacing,
15264 it->max_extra_line_spacing);
15265 if (it->current_x - it->pixel_width < it->first_visible_x)
15266 row->x = x - it->first_visible_x;
15268 else
15270 int new_x;
15271 struct glyph *glyph;
15273 for (i = 0; i < nglyphs; ++i, x = new_x)
15275 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15276 new_x = x + glyph->pixel_width;
15278 if (/* Lines are continued. */
15279 !it->truncate_lines_p
15280 && (/* Glyph doesn't fit on the line. */
15281 new_x > it->last_visible_x
15282 /* Or it fits exactly on a window system frame. */
15283 || (new_x == it->last_visible_x
15284 && FRAME_WINDOW_P (it->f))))
15286 /* End of a continued line. */
15288 if (it->hpos == 0
15289 || (new_x == it->last_visible_x
15290 && FRAME_WINDOW_P (it->f)))
15292 /* Current glyph is the only one on the line or
15293 fits exactly on the line. We must continue
15294 the line because we can't draw the cursor
15295 after the glyph. */
15296 row->continued_p = 1;
15297 it->current_x = new_x;
15298 it->continuation_lines_width += new_x;
15299 ++it->hpos;
15300 if (i == nglyphs - 1)
15302 set_iterator_to_next (it, 1);
15303 #ifdef HAVE_WINDOW_SYSTEM
15304 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15306 if (!get_next_display_element (it))
15308 row->exact_window_width_line_p = 1;
15309 it->continuation_lines_width = 0;
15310 row->continued_p = 0;
15311 row->ends_at_zv_p = 1;
15313 else if (ITERATOR_AT_END_OF_LINE_P (it))
15315 row->continued_p = 0;
15316 row->exact_window_width_line_p = 1;
15319 #endif /* HAVE_WINDOW_SYSTEM */
15322 else if (CHAR_GLYPH_PADDING_P (*glyph)
15323 && !FRAME_WINDOW_P (it->f))
15325 /* A padding glyph that doesn't fit on this line.
15326 This means the whole character doesn't fit
15327 on the line. */
15328 row->used[TEXT_AREA] = n_glyphs_before;
15330 /* Fill the rest of the row with continuation
15331 glyphs like in 20.x. */
15332 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15333 < row->glyphs[1 + TEXT_AREA])
15334 produce_special_glyphs (it, IT_CONTINUATION);
15336 row->continued_p = 1;
15337 it->current_x = x_before;
15338 it->continuation_lines_width += x_before;
15340 /* Restore the height to what it was before the
15341 element not fitting on the line. */
15342 it->max_ascent = ascent;
15343 it->max_descent = descent;
15344 it->max_phys_ascent = phys_ascent;
15345 it->max_phys_descent = phys_descent;
15347 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15349 /* A TAB that extends past the right edge of the
15350 window. This produces a single glyph on
15351 window system frames. We leave the glyph in
15352 this row and let it fill the row, but don't
15353 consume the TAB. */
15354 it->continuation_lines_width += it->last_visible_x;
15355 row->ends_in_middle_of_char_p = 1;
15356 row->continued_p = 1;
15357 glyph->pixel_width = it->last_visible_x - x;
15358 it->starts_in_middle_of_char_p = 1;
15360 else
15362 /* Something other than a TAB that draws past
15363 the right edge of the window. Restore
15364 positions to values before the element. */
15365 row->used[TEXT_AREA] = n_glyphs_before + i;
15367 /* Display continuation glyphs. */
15368 if (!FRAME_WINDOW_P (it->f))
15369 produce_special_glyphs (it, IT_CONTINUATION);
15370 row->continued_p = 1;
15372 it->continuation_lines_width += x;
15374 if (nglyphs > 1 && i > 0)
15376 row->ends_in_middle_of_char_p = 1;
15377 it->starts_in_middle_of_char_p = 1;
15380 /* Restore the height to what it was before the
15381 element not fitting on the line. */
15382 it->max_ascent = ascent;
15383 it->max_descent = descent;
15384 it->max_phys_ascent = phys_ascent;
15385 it->max_phys_descent = phys_descent;
15388 break;
15390 else if (new_x > it->first_visible_x)
15392 /* Increment number of glyphs actually displayed. */
15393 ++it->hpos;
15395 if (x < it->first_visible_x)
15396 /* Glyph is partially visible, i.e. row starts at
15397 negative X position. */
15398 row->x = x - it->first_visible_x;
15400 else
15402 /* Glyph is completely off the left margin of the
15403 window. This should not happen because of the
15404 move_it_in_display_line at the start of this
15405 function, unless the text display area of the
15406 window is empty. */
15407 xassert (it->first_visible_x <= it->last_visible_x);
15411 row->ascent = max (row->ascent, it->max_ascent);
15412 row->height = max (row->height, it->max_ascent + it->max_descent);
15413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15414 row->phys_height = max (row->phys_height,
15415 it->max_phys_ascent + it->max_phys_descent);
15416 row->extra_line_spacing = max (row->extra_line_spacing,
15417 it->max_extra_line_spacing);
15419 /* End of this display line if row is continued. */
15420 if (row->continued_p || row->ends_at_zv_p)
15421 break;
15424 at_end_of_line:
15425 /* Is this a line end? If yes, we're also done, after making
15426 sure that a non-default face is extended up to the right
15427 margin of the window. */
15428 if (ITERATOR_AT_END_OF_LINE_P (it))
15430 int used_before = row->used[TEXT_AREA];
15432 row->ends_in_newline_from_string_p = STRINGP (it->object);
15434 #ifdef HAVE_WINDOW_SYSTEM
15435 /* Add a space at the end of the line that is used to
15436 display the cursor there. */
15437 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15438 append_space_for_newline (it, 0);
15439 #endif /* HAVE_WINDOW_SYSTEM */
15441 /* Extend the face to the end of the line. */
15442 extend_face_to_end_of_line (it);
15444 /* Make sure we have the position. */
15445 if (used_before == 0)
15446 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15448 /* Consume the line end. This skips over invisible lines. */
15449 set_iterator_to_next (it, 1);
15450 it->continuation_lines_width = 0;
15451 break;
15454 /* Proceed with next display element. Note that this skips
15455 over lines invisible because of selective display. */
15456 set_iterator_to_next (it, 1);
15458 /* If we truncate lines, we are done when the last displayed
15459 glyphs reach past the right margin of the window. */
15460 if (it->truncate_lines_p
15461 && (FRAME_WINDOW_P (it->f)
15462 ? (it->current_x >= it->last_visible_x)
15463 : (it->current_x > it->last_visible_x)))
15465 /* Maybe add truncation glyphs. */
15466 if (!FRAME_WINDOW_P (it->f))
15468 int i, n;
15470 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15471 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15472 break;
15474 for (n = row->used[TEXT_AREA]; i < n; ++i)
15476 row->used[TEXT_AREA] = i;
15477 produce_special_glyphs (it, IT_TRUNCATION);
15480 #ifdef HAVE_WINDOW_SYSTEM
15481 else
15483 /* Don't truncate if we can overflow newline into fringe. */
15484 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15486 if (!get_next_display_element (it))
15488 it->continuation_lines_width = 0;
15489 row->ends_at_zv_p = 1;
15490 row->exact_window_width_line_p = 1;
15491 break;
15493 if (ITERATOR_AT_END_OF_LINE_P (it))
15495 row->exact_window_width_line_p = 1;
15496 goto at_end_of_line;
15500 #endif /* HAVE_WINDOW_SYSTEM */
15502 row->truncated_on_right_p = 1;
15503 it->continuation_lines_width = 0;
15504 reseat_at_next_visible_line_start (it, 0);
15505 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15506 it->hpos = hpos_before;
15507 it->current_x = x_before;
15508 break;
15512 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15513 at the left window margin. */
15514 if (it->first_visible_x
15515 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15517 if (!FRAME_WINDOW_P (it->f))
15518 insert_left_trunc_glyphs (it);
15519 row->truncated_on_left_p = 1;
15522 /* If the start of this line is the overlay arrow-position, then
15523 mark this glyph row as the one containing the overlay arrow.
15524 This is clearly a mess with variable size fonts. It would be
15525 better to let it be displayed like cursors under X. */
15526 if ((row->displays_text_p || !overlay_arrow_seen)
15527 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15528 !NILP (overlay_arrow_string)))
15530 /* Overlay arrow in window redisplay is a fringe bitmap. */
15531 if (STRINGP (overlay_arrow_string))
15533 struct glyph_row *arrow_row
15534 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15535 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15536 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15537 struct glyph *p = row->glyphs[TEXT_AREA];
15538 struct glyph *p2, *end;
15540 /* Copy the arrow glyphs. */
15541 while (glyph < arrow_end)
15542 *p++ = *glyph++;
15544 /* Throw away padding glyphs. */
15545 p2 = p;
15546 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15547 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15548 ++p2;
15549 if (p2 > p)
15551 while (p2 < end)
15552 *p++ = *p2++;
15553 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15556 else
15558 xassert (INTEGERP (overlay_arrow_string));
15559 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15561 overlay_arrow_seen = 1;
15564 /* Compute pixel dimensions of this line. */
15565 compute_line_metrics (it);
15567 /* Remember the position at which this line ends. */
15568 row->end = it->current;
15570 /* Record whether this row ends inside an ellipsis. */
15571 row->ends_in_ellipsis_p
15572 = (it->method == GET_FROM_DISPLAY_VECTOR
15573 && it->ellipsis_p);
15575 /* Save fringe bitmaps in this row. */
15576 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15577 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15578 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15579 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15581 it->left_user_fringe_bitmap = 0;
15582 it->left_user_fringe_face_id = 0;
15583 it->right_user_fringe_bitmap = 0;
15584 it->right_user_fringe_face_id = 0;
15586 /* Maybe set the cursor. */
15587 if (it->w->cursor.vpos < 0
15588 && PT >= MATRIX_ROW_START_CHARPOS (row)
15589 && PT <= MATRIX_ROW_END_CHARPOS (row)
15590 && cursor_row_p (it->w, row))
15591 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15593 /* Highlight trailing whitespace. */
15594 if (!NILP (Vshow_trailing_whitespace))
15595 highlight_trailing_whitespace (it->f, it->glyph_row);
15597 /* Prepare for the next line. This line starts horizontally at (X
15598 HPOS) = (0 0). Vertical positions are incremented. As a
15599 convenience for the caller, IT->glyph_row is set to the next
15600 row to be used. */
15601 it->current_x = it->hpos = 0;
15602 it->current_y += row->height;
15603 ++it->vpos;
15604 ++it->glyph_row;
15605 it->start = it->current;
15606 return row->displays_text_p;
15611 /***********************************************************************
15612 Menu Bar
15613 ***********************************************************************/
15615 /* Redisplay the menu bar in the frame for window W.
15617 The menu bar of X frames that don't have X toolkit support is
15618 displayed in a special window W->frame->menu_bar_window.
15620 The menu bar of terminal frames is treated specially as far as
15621 glyph matrices are concerned. Menu bar lines are not part of
15622 windows, so the update is done directly on the frame matrix rows
15623 for the menu bar. */
15625 static void
15626 display_menu_bar (w)
15627 struct window *w;
15629 struct frame *f = XFRAME (WINDOW_FRAME (w));
15630 struct it it;
15631 Lisp_Object items;
15632 int i;
15634 /* Don't do all this for graphical frames. */
15635 #ifdef HAVE_NTGUI
15636 if (!NILP (Vwindow_system))
15637 return;
15638 #endif
15639 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15640 if (FRAME_X_P (f))
15641 return;
15642 #endif
15643 #ifdef MAC_OS
15644 if (FRAME_MAC_P (f))
15645 return;
15646 #endif
15648 #ifdef USE_X_TOOLKIT
15649 xassert (!FRAME_WINDOW_P (f));
15650 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15651 it.first_visible_x = 0;
15652 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15653 #else /* not USE_X_TOOLKIT */
15654 if (FRAME_WINDOW_P (f))
15656 /* Menu bar lines are displayed in the desired matrix of the
15657 dummy window menu_bar_window. */
15658 struct window *menu_w;
15659 xassert (WINDOWP (f->menu_bar_window));
15660 menu_w = XWINDOW (f->menu_bar_window);
15661 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15662 MENU_FACE_ID);
15663 it.first_visible_x = 0;
15664 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15666 else
15668 /* This is a TTY frame, i.e. character hpos/vpos are used as
15669 pixel x/y. */
15670 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15671 MENU_FACE_ID);
15672 it.first_visible_x = 0;
15673 it.last_visible_x = FRAME_COLS (f);
15675 #endif /* not USE_X_TOOLKIT */
15677 if (! mode_line_inverse_video)
15678 /* Force the menu-bar to be displayed in the default face. */
15679 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15681 /* Clear all rows of the menu bar. */
15682 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15684 struct glyph_row *row = it.glyph_row + i;
15685 clear_glyph_row (row);
15686 row->enabled_p = 1;
15687 row->full_width_p = 1;
15690 /* Display all items of the menu bar. */
15691 items = FRAME_MENU_BAR_ITEMS (it.f);
15692 for (i = 0; i < XVECTOR (items)->size; i += 4)
15694 Lisp_Object string;
15696 /* Stop at nil string. */
15697 string = AREF (items, i + 1);
15698 if (NILP (string))
15699 break;
15701 /* Remember where item was displayed. */
15702 AREF (items, i + 3) = make_number (it.hpos);
15704 /* Display the item, pad with one space. */
15705 if (it.current_x < it.last_visible_x)
15706 display_string (NULL, string, Qnil, 0, 0, &it,
15707 SCHARS (string) + 1, 0, 0, -1);
15710 /* Fill out the line with spaces. */
15711 if (it.current_x < it.last_visible_x)
15712 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15714 /* Compute the total height of the lines. */
15715 compute_line_metrics (&it);
15720 /***********************************************************************
15721 Mode Line
15722 ***********************************************************************/
15724 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15725 FORCE is non-zero, redisplay mode lines unconditionally.
15726 Otherwise, redisplay only mode lines that are garbaged. Value is
15727 the number of windows whose mode lines were redisplayed. */
15729 static int
15730 redisplay_mode_lines (window, force)
15731 Lisp_Object window;
15732 int force;
15734 int nwindows = 0;
15736 while (!NILP (window))
15738 struct window *w = XWINDOW (window);
15740 if (WINDOWP (w->hchild))
15741 nwindows += redisplay_mode_lines (w->hchild, force);
15742 else if (WINDOWP (w->vchild))
15743 nwindows += redisplay_mode_lines (w->vchild, force);
15744 else if (force
15745 || FRAME_GARBAGED_P (XFRAME (w->frame))
15746 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15748 struct text_pos lpoint;
15749 struct buffer *old = current_buffer;
15751 /* Set the window's buffer for the mode line display. */
15752 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15753 set_buffer_internal_1 (XBUFFER (w->buffer));
15755 /* Point refers normally to the selected window. For any
15756 other window, set up appropriate value. */
15757 if (!EQ (window, selected_window))
15759 struct text_pos pt;
15761 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15762 if (CHARPOS (pt) < BEGV)
15763 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15764 else if (CHARPOS (pt) > (ZV - 1))
15765 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15766 else
15767 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15770 /* Display mode lines. */
15771 clear_glyph_matrix (w->desired_matrix);
15772 if (display_mode_lines (w))
15774 ++nwindows;
15775 w->must_be_updated_p = 1;
15778 /* Restore old settings. */
15779 set_buffer_internal_1 (old);
15780 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15783 window = w->next;
15786 return nwindows;
15790 /* Display the mode and/or top line of window W. Value is the number
15791 of mode lines displayed. */
15793 static int
15794 display_mode_lines (w)
15795 struct window *w;
15797 Lisp_Object old_selected_window, old_selected_frame;
15798 int n = 0;
15800 old_selected_frame = selected_frame;
15801 selected_frame = w->frame;
15802 old_selected_window = selected_window;
15803 XSETWINDOW (selected_window, w);
15805 /* These will be set while the mode line specs are processed. */
15806 line_number_displayed = 0;
15807 w->column_number_displayed = Qnil;
15809 if (WINDOW_WANTS_MODELINE_P (w))
15811 struct window *sel_w = XWINDOW (old_selected_window);
15813 /* Select mode line face based on the real selected window. */
15814 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15815 current_buffer->mode_line_format);
15816 ++n;
15819 if (WINDOW_WANTS_HEADER_LINE_P (w))
15821 display_mode_line (w, HEADER_LINE_FACE_ID,
15822 current_buffer->header_line_format);
15823 ++n;
15826 selected_frame = old_selected_frame;
15827 selected_window = old_selected_window;
15828 return n;
15832 /* Display mode or top line of window W. FACE_ID specifies which line
15833 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15834 FORMAT is the mode line format to display. Value is the pixel
15835 height of the mode line displayed. */
15837 static int
15838 display_mode_line (w, face_id, format)
15839 struct window *w;
15840 enum face_id face_id;
15841 Lisp_Object format;
15843 struct it it;
15844 struct face *face;
15845 int count = SPECPDL_INDEX ();
15847 init_iterator (&it, w, -1, -1, NULL, face_id);
15848 prepare_desired_row (it.glyph_row);
15850 it.glyph_row->mode_line_p = 1;
15852 if (! mode_line_inverse_video)
15853 /* Force the mode-line to be displayed in the default face. */
15854 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15856 record_unwind_protect (unwind_format_mode_line,
15857 format_mode_line_unwind_data (NULL));
15859 mode_line_target = MODE_LINE_DISPLAY;
15861 /* Temporarily make frame's keyboard the current kboard so that
15862 kboard-local variables in the mode_line_format will get the right
15863 values. */
15864 push_frame_kboard (it.f);
15865 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15866 pop_frame_kboard ();
15868 unbind_to (count, Qnil);
15870 /* Fill up with spaces. */
15871 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15873 compute_line_metrics (&it);
15874 it.glyph_row->full_width_p = 1;
15875 it.glyph_row->continued_p = 0;
15876 it.glyph_row->truncated_on_left_p = 0;
15877 it.glyph_row->truncated_on_right_p = 0;
15879 /* Make a 3D mode-line have a shadow at its right end. */
15880 face = FACE_FROM_ID (it.f, face_id);
15881 extend_face_to_end_of_line (&it);
15882 if (face->box != FACE_NO_BOX)
15884 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15885 + it.glyph_row->used[TEXT_AREA] - 1);
15886 last->right_box_line_p = 1;
15889 return it.glyph_row->height;
15892 /* Contribute ELT to the mode line for window IT->w. How it
15893 translates into text depends on its data type.
15895 IT describes the display environment in which we display, as usual.
15897 DEPTH is the depth in recursion. It is used to prevent
15898 infinite recursion here.
15900 FIELD_WIDTH is the number of characters the display of ELT should
15901 occupy in the mode line, and PRECISION is the maximum number of
15902 characters to display from ELT's representation. See
15903 display_string for details.
15905 Returns the hpos of the end of the text generated by ELT.
15907 PROPS is a property list to add to any string we encounter.
15909 If RISKY is nonzero, remove (disregard) any properties in any string
15910 we encounter, and ignore :eval and :propertize.
15912 The global variable `mode_line_target' determines whether the
15913 output is passed to `store_mode_line_noprop',
15914 `store_mode_line_string', or `display_string'. */
15916 static int
15917 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15918 struct it *it;
15919 int depth;
15920 int field_width, precision;
15921 Lisp_Object elt, props;
15922 int risky;
15924 int n = 0, field, prec;
15925 int literal = 0;
15927 tail_recurse:
15928 if (depth > 100)
15929 elt = build_string ("*too-deep*");
15931 depth++;
15933 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15935 case Lisp_String:
15937 /* A string: output it and check for %-constructs within it. */
15938 unsigned char c;
15939 const unsigned char *this, *lisp_string;
15941 if (!NILP (props) || risky)
15943 Lisp_Object oprops, aelt;
15944 oprops = Ftext_properties_at (make_number (0), elt);
15946 /* If the starting string's properties are not what
15947 we want, translate the string. Also, if the string
15948 is risky, do that anyway. */
15950 if (NILP (Fequal (props, oprops)) || risky)
15952 /* If the starting string has properties,
15953 merge the specified ones onto the existing ones. */
15954 if (! NILP (oprops) && !risky)
15956 Lisp_Object tem;
15958 oprops = Fcopy_sequence (oprops);
15959 tem = props;
15960 while (CONSP (tem))
15962 oprops = Fplist_put (oprops, XCAR (tem),
15963 XCAR (XCDR (tem)));
15964 tem = XCDR (XCDR (tem));
15966 props = oprops;
15969 aelt = Fassoc (elt, mode_line_proptrans_alist);
15970 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15972 mode_line_proptrans_alist
15973 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15974 elt = XCAR (aelt);
15976 else
15978 Lisp_Object tem;
15980 elt = Fcopy_sequence (elt);
15981 Fset_text_properties (make_number (0), Flength (elt),
15982 props, elt);
15983 /* Add this item to mode_line_proptrans_alist. */
15984 mode_line_proptrans_alist
15985 = Fcons (Fcons (elt, props),
15986 mode_line_proptrans_alist);
15987 /* Truncate mode_line_proptrans_alist
15988 to at most 50 elements. */
15989 tem = Fnthcdr (make_number (50),
15990 mode_line_proptrans_alist);
15991 if (! NILP (tem))
15992 XSETCDR (tem, Qnil);
15997 this = SDATA (elt);
15998 lisp_string = this;
16000 if (literal)
16002 prec = precision - n;
16003 switch (mode_line_target)
16005 case MODE_LINE_NOPROP:
16006 case MODE_LINE_TITLE:
16007 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16008 break;
16009 case MODE_LINE_STRING:
16010 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16011 break;
16012 case MODE_LINE_DISPLAY:
16013 n += display_string (NULL, elt, Qnil, 0, 0, it,
16014 0, prec, 0, STRING_MULTIBYTE (elt));
16015 break;
16018 break;
16021 while ((precision <= 0 || n < precision)
16022 && *this
16023 && (mode_line_target != MODE_LINE_DISPLAY
16024 || it->current_x < it->last_visible_x))
16026 const unsigned char *last = this;
16028 /* Advance to end of string or next format specifier. */
16029 while ((c = *this++) != '\0' && c != '%')
16032 if (this - 1 != last)
16034 int nchars, nbytes;
16036 /* Output to end of string or up to '%'. Field width
16037 is length of string. Don't output more than
16038 PRECISION allows us. */
16039 --this;
16041 prec = c_string_width (last, this - last, precision - n,
16042 &nchars, &nbytes);
16044 switch (mode_line_target)
16046 case MODE_LINE_NOPROP:
16047 case MODE_LINE_TITLE:
16048 n += store_mode_line_noprop (last, 0, prec);
16049 break;
16050 case MODE_LINE_STRING:
16052 int bytepos = last - lisp_string;
16053 int charpos = string_byte_to_char (elt, bytepos);
16054 int endpos = (precision <= 0
16055 ? string_byte_to_char (elt,
16056 this - lisp_string)
16057 : charpos + nchars);
16059 n += store_mode_line_string (NULL,
16060 Fsubstring (elt, make_number (charpos),
16061 make_number (endpos)),
16062 0, 0, 0, Qnil);
16064 break;
16065 case MODE_LINE_DISPLAY:
16067 int bytepos = last - lisp_string;
16068 int charpos = string_byte_to_char (elt, bytepos);
16069 n += display_string (NULL, elt, Qnil, 0, charpos,
16070 it, 0, prec, 0,
16071 STRING_MULTIBYTE (elt));
16073 break;
16076 else /* c == '%' */
16078 const unsigned char *percent_position = this;
16080 /* Get the specified minimum width. Zero means
16081 don't pad. */
16082 field = 0;
16083 while ((c = *this++) >= '0' && c <= '9')
16084 field = field * 10 + c - '0';
16086 /* Don't pad beyond the total padding allowed. */
16087 if (field_width - n > 0 && field > field_width - n)
16088 field = field_width - n;
16090 /* Note that either PRECISION <= 0 or N < PRECISION. */
16091 prec = precision - n;
16093 if (c == 'M')
16094 n += display_mode_element (it, depth, field, prec,
16095 Vglobal_mode_string, props,
16096 risky);
16097 else if (c != 0)
16099 int multibyte;
16100 int bytepos, charpos;
16101 unsigned char *spec;
16103 bytepos = percent_position - lisp_string;
16104 charpos = (STRING_MULTIBYTE (elt)
16105 ? string_byte_to_char (elt, bytepos)
16106 : bytepos);
16108 spec
16109 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16111 switch (mode_line_target)
16113 case MODE_LINE_NOPROP:
16114 case MODE_LINE_TITLE:
16115 n += store_mode_line_noprop (spec, field, prec);
16116 break;
16117 case MODE_LINE_STRING:
16119 int len = strlen (spec);
16120 Lisp_Object tem = make_string (spec, len);
16121 props = Ftext_properties_at (make_number (charpos), elt);
16122 /* Should only keep face property in props */
16123 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16125 break;
16126 case MODE_LINE_DISPLAY:
16128 int nglyphs_before, nwritten;
16130 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16131 nwritten = display_string (spec, Qnil, elt,
16132 charpos, 0, it,
16133 field, prec, 0,
16134 multibyte);
16136 /* Assign to the glyphs written above the
16137 string where the `%x' came from, position
16138 of the `%'. */
16139 if (nwritten > 0)
16141 struct glyph *glyph
16142 = (it->glyph_row->glyphs[TEXT_AREA]
16143 + nglyphs_before);
16144 int i;
16146 for (i = 0; i < nwritten; ++i)
16148 glyph[i].object = elt;
16149 glyph[i].charpos = charpos;
16152 n += nwritten;
16155 break;
16158 else /* c == 0 */
16159 break;
16163 break;
16165 case Lisp_Symbol:
16166 /* A symbol: process the value of the symbol recursively
16167 as if it appeared here directly. Avoid error if symbol void.
16168 Special case: if value of symbol is a string, output the string
16169 literally. */
16171 register Lisp_Object tem;
16173 /* If the variable is not marked as risky to set
16174 then its contents are risky to use. */
16175 if (NILP (Fget (elt, Qrisky_local_variable)))
16176 risky = 1;
16178 tem = Fboundp (elt);
16179 if (!NILP (tem))
16181 tem = Fsymbol_value (elt);
16182 /* If value is a string, output that string literally:
16183 don't check for % within it. */
16184 if (STRINGP (tem))
16185 literal = 1;
16187 if (!EQ (tem, elt))
16189 /* Give up right away for nil or t. */
16190 elt = tem;
16191 goto tail_recurse;
16195 break;
16197 case Lisp_Cons:
16199 register Lisp_Object car, tem;
16201 /* A cons cell: five distinct cases.
16202 If first element is :eval or :propertize, do something special.
16203 If first element is a string or a cons, process all the elements
16204 and effectively concatenate them.
16205 If first element is a negative number, truncate displaying cdr to
16206 at most that many characters. If positive, pad (with spaces)
16207 to at least that many characters.
16208 If first element is a symbol, process the cadr or caddr recursively
16209 according to whether the symbol's value is non-nil or nil. */
16210 car = XCAR (elt);
16211 if (EQ (car, QCeval))
16213 /* An element of the form (:eval FORM) means evaluate FORM
16214 and use the result as mode line elements. */
16216 if (risky)
16217 break;
16219 if (CONSP (XCDR (elt)))
16221 Lisp_Object spec;
16222 spec = safe_eval (XCAR (XCDR (elt)));
16223 n += display_mode_element (it, depth, field_width - n,
16224 precision - n, spec, props,
16225 risky);
16228 else if (EQ (car, QCpropertize))
16230 /* An element of the form (:propertize ELT PROPS...)
16231 means display ELT but applying properties PROPS. */
16233 if (risky)
16234 break;
16236 if (CONSP (XCDR (elt)))
16237 n += display_mode_element (it, depth, field_width - n,
16238 precision - n, XCAR (XCDR (elt)),
16239 XCDR (XCDR (elt)), risky);
16241 else if (SYMBOLP (car))
16243 tem = Fboundp (car);
16244 elt = XCDR (elt);
16245 if (!CONSP (elt))
16246 goto invalid;
16247 /* elt is now the cdr, and we know it is a cons cell.
16248 Use its car if CAR has a non-nil value. */
16249 if (!NILP (tem))
16251 tem = Fsymbol_value (car);
16252 if (!NILP (tem))
16254 elt = XCAR (elt);
16255 goto tail_recurse;
16258 /* Symbol's value is nil (or symbol is unbound)
16259 Get the cddr of the original list
16260 and if possible find the caddr and use that. */
16261 elt = XCDR (elt);
16262 if (NILP (elt))
16263 break;
16264 else if (!CONSP (elt))
16265 goto invalid;
16266 elt = XCAR (elt);
16267 goto tail_recurse;
16269 else if (INTEGERP (car))
16271 register int lim = XINT (car);
16272 elt = XCDR (elt);
16273 if (lim < 0)
16275 /* Negative int means reduce maximum width. */
16276 if (precision <= 0)
16277 precision = -lim;
16278 else
16279 precision = min (precision, -lim);
16281 else if (lim > 0)
16283 /* Padding specified. Don't let it be more than
16284 current maximum. */
16285 if (precision > 0)
16286 lim = min (precision, lim);
16288 /* If that's more padding than already wanted, queue it.
16289 But don't reduce padding already specified even if
16290 that is beyond the current truncation point. */
16291 field_width = max (lim, field_width);
16293 goto tail_recurse;
16295 else if (STRINGP (car) || CONSP (car))
16297 register int limit = 50;
16298 /* Limit is to protect against circular lists. */
16299 while (CONSP (elt)
16300 && --limit > 0
16301 && (precision <= 0 || n < precision))
16303 n += display_mode_element (it, depth,
16304 /* Do padding only after the last
16305 element in the list. */
16306 (! CONSP (XCDR (elt))
16307 ? field_width - n
16308 : 0),
16309 precision - n, XCAR (elt),
16310 props, risky);
16311 elt = XCDR (elt);
16315 break;
16317 default:
16318 invalid:
16319 elt = build_string ("*invalid*");
16320 goto tail_recurse;
16323 /* Pad to FIELD_WIDTH. */
16324 if (field_width > 0 && n < field_width)
16326 switch (mode_line_target)
16328 case MODE_LINE_NOPROP:
16329 case MODE_LINE_TITLE:
16330 n += store_mode_line_noprop ("", field_width - n, 0);
16331 break;
16332 case MODE_LINE_STRING:
16333 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16334 break;
16335 case MODE_LINE_DISPLAY:
16336 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16337 0, 0, 0);
16338 break;
16342 return n;
16345 /* Store a mode-line string element in mode_line_string_list.
16347 If STRING is non-null, display that C string. Otherwise, the Lisp
16348 string LISP_STRING is displayed.
16350 FIELD_WIDTH is the minimum number of output glyphs to produce.
16351 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16352 with spaces. FIELD_WIDTH <= 0 means don't pad.
16354 PRECISION is the maximum number of characters to output from
16355 STRING. PRECISION <= 0 means don't truncate the string.
16357 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16358 properties to the string.
16360 PROPS are the properties to add to the string.
16361 The mode_line_string_face face property is always added to the string.
16364 static int
16365 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16366 char *string;
16367 Lisp_Object lisp_string;
16368 int copy_string;
16369 int field_width;
16370 int precision;
16371 Lisp_Object props;
16373 int len;
16374 int n = 0;
16376 if (string != NULL)
16378 len = strlen (string);
16379 if (precision > 0 && len > precision)
16380 len = precision;
16381 lisp_string = make_string (string, len);
16382 if (NILP (props))
16383 props = mode_line_string_face_prop;
16384 else if (!NILP (mode_line_string_face))
16386 Lisp_Object face = Fplist_get (props, Qface);
16387 props = Fcopy_sequence (props);
16388 if (NILP (face))
16389 face = mode_line_string_face;
16390 else
16391 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16392 props = Fplist_put (props, Qface, face);
16394 Fadd_text_properties (make_number (0), make_number (len),
16395 props, lisp_string);
16397 else
16399 len = XFASTINT (Flength (lisp_string));
16400 if (precision > 0 && len > precision)
16402 len = precision;
16403 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16404 precision = -1;
16406 if (!NILP (mode_line_string_face))
16408 Lisp_Object face;
16409 if (NILP (props))
16410 props = Ftext_properties_at (make_number (0), lisp_string);
16411 face = Fplist_get (props, Qface);
16412 if (NILP (face))
16413 face = mode_line_string_face;
16414 else
16415 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16416 props = Fcons (Qface, Fcons (face, Qnil));
16417 if (copy_string)
16418 lisp_string = Fcopy_sequence (lisp_string);
16420 if (!NILP (props))
16421 Fadd_text_properties (make_number (0), make_number (len),
16422 props, lisp_string);
16425 if (len > 0)
16427 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16428 n += len;
16431 if (field_width > len)
16433 field_width -= len;
16434 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16435 if (!NILP (props))
16436 Fadd_text_properties (make_number (0), make_number (field_width),
16437 props, lisp_string);
16438 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16439 n += field_width;
16442 return n;
16446 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16447 1, 4, 0,
16448 doc: /* Format a string out of a mode line format specification.
16449 First arg FORMAT specifies the mode line format (see `mode-line-format'
16450 for details) to use.
16452 Optional second arg FACE specifies the face property to put
16453 on all characters for which no face is specified.
16454 t means whatever face the window's mode line currently uses
16455 \(either `mode-line' or `mode-line-inactive', depending).
16456 nil means the default is no face property.
16457 If FACE is an integer, the value string has no text properties.
16459 Optional third and fourth args WINDOW and BUFFER specify the window
16460 and buffer to use as the context for the formatting (defaults
16461 are the selected window and the window's buffer). */)
16462 (format, face, window, buffer)
16463 Lisp_Object format, face, window, buffer;
16465 struct it it;
16466 int len;
16467 struct window *w;
16468 struct buffer *old_buffer = NULL;
16469 int face_id = -1;
16470 int no_props = INTEGERP (face);
16471 int count = SPECPDL_INDEX ();
16472 Lisp_Object str;
16473 int string_start = 0;
16475 if (NILP (window))
16476 window = selected_window;
16477 CHECK_WINDOW (window);
16478 w = XWINDOW (window);
16480 if (NILP (buffer))
16481 buffer = w->buffer;
16482 CHECK_BUFFER (buffer);
16484 if (NILP (format))
16485 return build_string ("");
16487 if (no_props)
16488 face = Qnil;
16490 if (!NILP (face))
16492 if (EQ (face, Qt))
16493 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16494 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16497 if (face_id < 0)
16498 face_id = DEFAULT_FACE_ID;
16500 if (XBUFFER (buffer) != current_buffer)
16501 old_buffer = current_buffer;
16503 record_unwind_protect (unwind_format_mode_line,
16504 format_mode_line_unwind_data (old_buffer));
16506 if (old_buffer)
16507 set_buffer_internal_1 (XBUFFER (buffer));
16509 init_iterator (&it, w, -1, -1, NULL, face_id);
16511 if (no_props)
16513 mode_line_target = MODE_LINE_NOPROP;
16514 mode_line_string_face_prop = Qnil;
16515 mode_line_string_list = Qnil;
16516 string_start = MODE_LINE_NOPROP_LEN (0);
16518 else
16520 mode_line_target = MODE_LINE_STRING;
16521 mode_line_string_list = Qnil;
16522 mode_line_string_face = face;
16523 mode_line_string_face_prop
16524 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16527 push_frame_kboard (it.f);
16528 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16529 pop_frame_kboard ();
16531 if (no_props)
16533 len = MODE_LINE_NOPROP_LEN (string_start);
16534 str = make_string (mode_line_noprop_buf + string_start, len);
16536 else
16538 mode_line_string_list = Fnreverse (mode_line_string_list);
16539 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16540 make_string ("", 0));
16543 unbind_to (count, Qnil);
16544 return str;
16547 /* Write a null-terminated, right justified decimal representation of
16548 the positive integer D to BUF using a minimal field width WIDTH. */
16550 static void
16551 pint2str (buf, width, d)
16552 register char *buf;
16553 register int width;
16554 register int d;
16556 register char *p = buf;
16558 if (d <= 0)
16559 *p++ = '0';
16560 else
16562 while (d > 0)
16564 *p++ = d % 10 + '0';
16565 d /= 10;
16569 for (width -= (int) (p - buf); width > 0; --width)
16570 *p++ = ' ';
16571 *p-- = '\0';
16572 while (p > buf)
16574 d = *buf;
16575 *buf++ = *p;
16576 *p-- = d;
16580 /* Write a null-terminated, right justified decimal and "human
16581 readable" representation of the nonnegative integer D to BUF using
16582 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16584 static const char power_letter[] =
16586 0, /* not used */
16587 'k', /* kilo */
16588 'M', /* mega */
16589 'G', /* giga */
16590 'T', /* tera */
16591 'P', /* peta */
16592 'E', /* exa */
16593 'Z', /* zetta */
16594 'Y' /* yotta */
16597 static void
16598 pint2hrstr (buf, width, d)
16599 char *buf;
16600 int width;
16601 int d;
16603 /* We aim to represent the nonnegative integer D as
16604 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16605 int quotient = d;
16606 int remainder = 0;
16607 /* -1 means: do not use TENTHS. */
16608 int tenths = -1;
16609 int exponent = 0;
16611 /* Length of QUOTIENT.TENTHS as a string. */
16612 int length;
16614 char * psuffix;
16615 char * p;
16617 if (1000 <= quotient)
16619 /* Scale to the appropriate EXPONENT. */
16622 remainder = quotient % 1000;
16623 quotient /= 1000;
16624 exponent++;
16626 while (1000 <= quotient);
16628 /* Round to nearest and decide whether to use TENTHS or not. */
16629 if (quotient <= 9)
16631 tenths = remainder / 100;
16632 if (50 <= remainder % 100)
16634 if (tenths < 9)
16635 tenths++;
16636 else
16638 quotient++;
16639 if (quotient == 10)
16640 tenths = -1;
16641 else
16642 tenths = 0;
16646 else
16647 if (500 <= remainder)
16649 if (quotient < 999)
16650 quotient++;
16651 else
16653 quotient = 1;
16654 exponent++;
16655 tenths = 0;
16660 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16661 if (tenths == -1 && quotient <= 99)
16662 if (quotient <= 9)
16663 length = 1;
16664 else
16665 length = 2;
16666 else
16667 length = 3;
16668 p = psuffix = buf + max (width, length);
16670 /* Print EXPONENT. */
16671 if (exponent)
16672 *psuffix++ = power_letter[exponent];
16673 *psuffix = '\0';
16675 /* Print TENTHS. */
16676 if (tenths >= 0)
16678 *--p = '0' + tenths;
16679 *--p = '.';
16682 /* Print QUOTIENT. */
16685 int digit = quotient % 10;
16686 *--p = '0' + digit;
16688 while ((quotient /= 10) != 0);
16690 /* Print leading spaces. */
16691 while (buf < p)
16692 *--p = ' ';
16695 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16696 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16697 type of CODING_SYSTEM. Return updated pointer into BUF. */
16699 static unsigned char invalid_eol_type[] = "(*invalid*)";
16701 static char *
16702 decode_mode_spec_coding (coding_system, buf, eol_flag)
16703 Lisp_Object coding_system;
16704 register char *buf;
16705 int eol_flag;
16707 Lisp_Object val;
16708 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16709 const unsigned char *eol_str;
16710 int eol_str_len;
16711 /* The EOL conversion we are using. */
16712 Lisp_Object eoltype;
16714 val = Fget (coding_system, Qcoding_system);
16715 eoltype = Qnil;
16717 if (!VECTORP (val)) /* Not yet decided. */
16719 if (multibyte)
16720 *buf++ = '-';
16721 if (eol_flag)
16722 eoltype = eol_mnemonic_undecided;
16723 /* Don't mention EOL conversion if it isn't decided. */
16725 else
16727 Lisp_Object eolvalue;
16729 eolvalue = Fget (coding_system, Qeol_type);
16731 if (multibyte)
16732 *buf++ = XFASTINT (AREF (val, 1));
16734 if (eol_flag)
16736 /* The EOL conversion that is normal on this system. */
16738 if (NILP (eolvalue)) /* Not yet decided. */
16739 eoltype = eol_mnemonic_undecided;
16740 else if (VECTORP (eolvalue)) /* Not yet decided. */
16741 eoltype = eol_mnemonic_undecided;
16742 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16743 eoltype = (XFASTINT (eolvalue) == 0
16744 ? eol_mnemonic_unix
16745 : (XFASTINT (eolvalue) == 1
16746 ? eol_mnemonic_dos : eol_mnemonic_mac));
16750 if (eol_flag)
16752 /* Mention the EOL conversion if it is not the usual one. */
16753 if (STRINGP (eoltype))
16755 eol_str = SDATA (eoltype);
16756 eol_str_len = SBYTES (eoltype);
16758 else if (INTEGERP (eoltype)
16759 && CHAR_VALID_P (XINT (eoltype), 0))
16761 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16762 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16763 eol_str = tmp;
16765 else
16767 eol_str = invalid_eol_type;
16768 eol_str_len = sizeof (invalid_eol_type) - 1;
16770 bcopy (eol_str, buf, eol_str_len);
16771 buf += eol_str_len;
16774 return buf;
16777 /* Return a string for the output of a mode line %-spec for window W,
16778 generated by character C. PRECISION >= 0 means don't return a
16779 string longer than that value. FIELD_WIDTH > 0 means pad the
16780 string returned with spaces to that value. Return 1 in *MULTIBYTE
16781 if the result is multibyte text.
16783 Note we operate on the current buffer for most purposes,
16784 the exception being w->base_line_pos. */
16786 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16788 static char *
16789 decode_mode_spec (w, c, field_width, precision, multibyte)
16790 struct window *w;
16791 register int c;
16792 int field_width, precision;
16793 int *multibyte;
16795 Lisp_Object obj;
16796 struct frame *f = XFRAME (WINDOW_FRAME (w));
16797 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16798 struct buffer *b = current_buffer;
16800 obj = Qnil;
16801 *multibyte = 0;
16803 switch (c)
16805 case '*':
16806 if (!NILP (b->read_only))
16807 return "%";
16808 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16809 return "*";
16810 return "-";
16812 case '+':
16813 /* This differs from %* only for a modified read-only buffer. */
16814 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16815 return "*";
16816 if (!NILP (b->read_only))
16817 return "%";
16818 return "-";
16820 case '&':
16821 /* This differs from %* in ignoring read-only-ness. */
16822 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16823 return "*";
16824 return "-";
16826 case '%':
16827 return "%";
16829 case '[':
16831 int i;
16832 char *p;
16834 if (command_loop_level > 5)
16835 return "[[[... ";
16836 p = decode_mode_spec_buf;
16837 for (i = 0; i < command_loop_level; i++)
16838 *p++ = '[';
16839 *p = 0;
16840 return decode_mode_spec_buf;
16843 case ']':
16845 int i;
16846 char *p;
16848 if (command_loop_level > 5)
16849 return " ...]]]";
16850 p = decode_mode_spec_buf;
16851 for (i = 0; i < command_loop_level; i++)
16852 *p++ = ']';
16853 *p = 0;
16854 return decode_mode_spec_buf;
16857 case '-':
16859 register int i;
16861 /* Let lots_of_dashes be a string of infinite length. */
16862 if (mode_line_target == MODE_LINE_NOPROP ||
16863 mode_line_target == MODE_LINE_STRING)
16864 return "--";
16865 if (field_width <= 0
16866 || field_width > sizeof (lots_of_dashes))
16868 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16869 decode_mode_spec_buf[i] = '-';
16870 decode_mode_spec_buf[i] = '\0';
16871 return decode_mode_spec_buf;
16873 else
16874 return lots_of_dashes;
16877 case 'b':
16878 obj = b->name;
16879 break;
16881 case 'c':
16883 int col = (int) current_column (); /* iftc */
16884 w->column_number_displayed = make_number (col);
16885 pint2str (decode_mode_spec_buf, field_width, col);
16886 return decode_mode_spec_buf;
16889 case 'F':
16890 /* %F displays the frame name. */
16891 if (!NILP (f->title))
16892 return (char *) SDATA (f->title);
16893 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16894 return (char *) SDATA (f->name);
16895 return "Emacs";
16897 case 'f':
16898 obj = b->filename;
16899 break;
16901 case 'i':
16903 int size = ZV - BEGV;
16904 pint2str (decode_mode_spec_buf, field_width, size);
16905 return decode_mode_spec_buf;
16908 case 'I':
16910 int size = ZV - BEGV;
16911 pint2hrstr (decode_mode_spec_buf, field_width, size);
16912 return decode_mode_spec_buf;
16915 case 'l':
16917 int startpos = XMARKER (w->start)->charpos;
16918 int startpos_byte = marker_byte_position (w->start);
16919 int line, linepos, linepos_byte, topline;
16920 int nlines, junk;
16921 int height = WINDOW_TOTAL_LINES (w);
16923 /* If we decided that this buffer isn't suitable for line numbers,
16924 don't forget that too fast. */
16925 if (EQ (w->base_line_pos, w->buffer))
16926 goto no_value;
16927 /* But do forget it, if the window shows a different buffer now. */
16928 else if (BUFFERP (w->base_line_pos))
16929 w->base_line_pos = Qnil;
16931 /* If the buffer is very big, don't waste time. */
16932 if (INTEGERP (Vline_number_display_limit)
16933 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16935 w->base_line_pos = Qnil;
16936 w->base_line_number = Qnil;
16937 goto no_value;
16940 if (!NILP (w->base_line_number)
16941 && !NILP (w->base_line_pos)
16942 && XFASTINT (w->base_line_pos) <= startpos)
16944 line = XFASTINT (w->base_line_number);
16945 linepos = XFASTINT (w->base_line_pos);
16946 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16948 else
16950 line = 1;
16951 linepos = BUF_BEGV (b);
16952 linepos_byte = BUF_BEGV_BYTE (b);
16955 /* Count lines from base line to window start position. */
16956 nlines = display_count_lines (linepos, linepos_byte,
16957 startpos_byte,
16958 startpos, &junk);
16960 topline = nlines + line;
16962 /* Determine a new base line, if the old one is too close
16963 or too far away, or if we did not have one.
16964 "Too close" means it's plausible a scroll-down would
16965 go back past it. */
16966 if (startpos == BUF_BEGV (b))
16968 w->base_line_number = make_number (topline);
16969 w->base_line_pos = make_number (BUF_BEGV (b));
16971 else if (nlines < height + 25 || nlines > height * 3 + 50
16972 || linepos == BUF_BEGV (b))
16974 int limit = BUF_BEGV (b);
16975 int limit_byte = BUF_BEGV_BYTE (b);
16976 int position;
16977 int distance = (height * 2 + 30) * line_number_display_limit_width;
16979 if (startpos - distance > limit)
16981 limit = startpos - distance;
16982 limit_byte = CHAR_TO_BYTE (limit);
16985 nlines = display_count_lines (startpos, startpos_byte,
16986 limit_byte,
16987 - (height * 2 + 30),
16988 &position);
16989 /* If we couldn't find the lines we wanted within
16990 line_number_display_limit_width chars per line,
16991 give up on line numbers for this window. */
16992 if (position == limit_byte && limit == startpos - distance)
16994 w->base_line_pos = w->buffer;
16995 w->base_line_number = Qnil;
16996 goto no_value;
16999 w->base_line_number = make_number (topline - nlines);
17000 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17003 /* Now count lines from the start pos to point. */
17004 nlines = display_count_lines (startpos, startpos_byte,
17005 PT_BYTE, PT, &junk);
17007 /* Record that we did display the line number. */
17008 line_number_displayed = 1;
17010 /* Make the string to show. */
17011 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17012 return decode_mode_spec_buf;
17013 no_value:
17015 char* p = decode_mode_spec_buf;
17016 int pad = field_width - 2;
17017 while (pad-- > 0)
17018 *p++ = ' ';
17019 *p++ = '?';
17020 *p++ = '?';
17021 *p = '\0';
17022 return decode_mode_spec_buf;
17025 break;
17027 case 'm':
17028 obj = b->mode_name;
17029 break;
17031 case 'n':
17032 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17033 return " Narrow";
17034 break;
17036 case 'p':
17038 int pos = marker_position (w->start);
17039 int total = BUF_ZV (b) - BUF_BEGV (b);
17041 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17043 if (pos <= BUF_BEGV (b))
17044 return "All";
17045 else
17046 return "Bottom";
17048 else if (pos <= BUF_BEGV (b))
17049 return "Top";
17050 else
17052 if (total > 1000000)
17053 /* Do it differently for a large value, to avoid overflow. */
17054 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17055 else
17056 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17057 /* We can't normally display a 3-digit number,
17058 so get us a 2-digit number that is close. */
17059 if (total == 100)
17060 total = 99;
17061 sprintf (decode_mode_spec_buf, "%2d%%", total);
17062 return decode_mode_spec_buf;
17066 /* Display percentage of size above the bottom of the screen. */
17067 case 'P':
17069 int toppos = marker_position (w->start);
17070 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17071 int total = BUF_ZV (b) - BUF_BEGV (b);
17073 if (botpos >= BUF_ZV (b))
17075 if (toppos <= BUF_BEGV (b))
17076 return "All";
17077 else
17078 return "Bottom";
17080 else
17082 if (total > 1000000)
17083 /* Do it differently for a large value, to avoid overflow. */
17084 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17085 else
17086 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17087 /* We can't normally display a 3-digit number,
17088 so get us a 2-digit number that is close. */
17089 if (total == 100)
17090 total = 99;
17091 if (toppos <= BUF_BEGV (b))
17092 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17093 else
17094 sprintf (decode_mode_spec_buf, "%2d%%", total);
17095 return decode_mode_spec_buf;
17099 case 's':
17100 /* status of process */
17101 obj = Fget_buffer_process (Fcurrent_buffer ());
17102 if (NILP (obj))
17103 return "no process";
17104 #ifdef subprocesses
17105 obj = Fsymbol_name (Fprocess_status (obj));
17106 #endif
17107 break;
17109 case 't': /* indicate TEXT or BINARY */
17110 #ifdef MODE_LINE_BINARY_TEXT
17111 return MODE_LINE_BINARY_TEXT (b);
17112 #else
17113 return "T";
17114 #endif
17116 case 'z':
17117 /* coding-system (not including end-of-line format) */
17118 case 'Z':
17119 /* coding-system (including end-of-line type) */
17121 int eol_flag = (c == 'Z');
17122 char *p = decode_mode_spec_buf;
17124 if (! FRAME_WINDOW_P (f))
17126 /* No need to mention EOL here--the terminal never needs
17127 to do EOL conversion. */
17128 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17129 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17131 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17132 p, eol_flag);
17134 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17135 #ifdef subprocesses
17136 obj = Fget_buffer_process (Fcurrent_buffer ());
17137 if (PROCESSP (obj))
17139 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17140 p, eol_flag);
17141 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17142 p, eol_flag);
17144 #endif /* subprocesses */
17145 #endif /* 0 */
17146 *p = 0;
17147 return decode_mode_spec_buf;
17151 if (STRINGP (obj))
17153 *multibyte = STRING_MULTIBYTE (obj);
17154 return (char *) SDATA (obj);
17156 else
17157 return "";
17161 /* Count up to COUNT lines starting from START / START_BYTE.
17162 But don't go beyond LIMIT_BYTE.
17163 Return the number of lines thus found (always nonnegative).
17165 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17167 static int
17168 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17169 int start, start_byte, limit_byte, count;
17170 int *byte_pos_ptr;
17172 register unsigned char *cursor;
17173 unsigned char *base;
17175 register int ceiling;
17176 register unsigned char *ceiling_addr;
17177 int orig_count = count;
17179 /* If we are not in selective display mode,
17180 check only for newlines. */
17181 int selective_display = (!NILP (current_buffer->selective_display)
17182 && !INTEGERP (current_buffer->selective_display));
17184 if (count > 0)
17186 while (start_byte < limit_byte)
17188 ceiling = BUFFER_CEILING_OF (start_byte);
17189 ceiling = min (limit_byte - 1, ceiling);
17190 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17191 base = (cursor = BYTE_POS_ADDR (start_byte));
17192 while (1)
17194 if (selective_display)
17195 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17197 else
17198 while (*cursor != '\n' && ++cursor != ceiling_addr)
17201 if (cursor != ceiling_addr)
17203 if (--count == 0)
17205 start_byte += cursor - base + 1;
17206 *byte_pos_ptr = start_byte;
17207 return orig_count;
17209 else
17210 if (++cursor == ceiling_addr)
17211 break;
17213 else
17214 break;
17216 start_byte += cursor - base;
17219 else
17221 while (start_byte > limit_byte)
17223 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17224 ceiling = max (limit_byte, ceiling);
17225 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17226 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17227 while (1)
17229 if (selective_display)
17230 while (--cursor != ceiling_addr
17231 && *cursor != '\n' && *cursor != 015)
17233 else
17234 while (--cursor != ceiling_addr && *cursor != '\n')
17237 if (cursor != ceiling_addr)
17239 if (++count == 0)
17241 start_byte += cursor - base + 1;
17242 *byte_pos_ptr = start_byte;
17243 /* When scanning backwards, we should
17244 not count the newline posterior to which we stop. */
17245 return - orig_count - 1;
17248 else
17249 break;
17251 /* Here we add 1 to compensate for the last decrement
17252 of CURSOR, which took it past the valid range. */
17253 start_byte += cursor - base + 1;
17257 *byte_pos_ptr = limit_byte;
17259 if (count < 0)
17260 return - orig_count + count;
17261 return orig_count - count;
17267 /***********************************************************************
17268 Displaying strings
17269 ***********************************************************************/
17271 /* Display a NUL-terminated string, starting with index START.
17273 If STRING is non-null, display that C string. Otherwise, the Lisp
17274 string LISP_STRING is displayed.
17276 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17277 FACE_STRING. Display STRING or LISP_STRING with the face at
17278 FACE_STRING_POS in FACE_STRING:
17280 Display the string in the environment given by IT, but use the
17281 standard display table, temporarily.
17283 FIELD_WIDTH is the minimum number of output glyphs to produce.
17284 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17285 with spaces. If STRING has more characters, more than FIELD_WIDTH
17286 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17288 PRECISION is the maximum number of characters to output from
17289 STRING. PRECISION < 0 means don't truncate the string.
17291 This is roughly equivalent to printf format specifiers:
17293 FIELD_WIDTH PRECISION PRINTF
17294 ----------------------------------------
17295 -1 -1 %s
17296 -1 10 %.10s
17297 10 -1 %10s
17298 20 10 %20.10s
17300 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17301 display them, and < 0 means obey the current buffer's value of
17302 enable_multibyte_characters.
17304 Value is the number of glyphs produced. */
17306 static int
17307 display_string (string, lisp_string, face_string, face_string_pos,
17308 start, it, field_width, precision, max_x, multibyte)
17309 unsigned char *string;
17310 Lisp_Object lisp_string;
17311 Lisp_Object face_string;
17312 int face_string_pos;
17313 int start;
17314 struct it *it;
17315 int field_width, precision, max_x;
17316 int multibyte;
17318 int hpos_at_start = it->hpos;
17319 int saved_face_id = it->face_id;
17320 struct glyph_row *row = it->glyph_row;
17322 /* Initialize the iterator IT for iteration over STRING beginning
17323 with index START. */
17324 reseat_to_string (it, string, lisp_string, start,
17325 precision, field_width, multibyte);
17327 /* If displaying STRING, set up the face of the iterator
17328 from LISP_STRING, if that's given. */
17329 if (STRINGP (face_string))
17331 int endptr;
17332 struct face *face;
17334 it->face_id
17335 = face_at_string_position (it->w, face_string, face_string_pos,
17336 0, it->region_beg_charpos,
17337 it->region_end_charpos,
17338 &endptr, it->base_face_id, 0);
17339 face = FACE_FROM_ID (it->f, it->face_id);
17340 it->face_box_p = face->box != FACE_NO_BOX;
17343 /* Set max_x to the maximum allowed X position. Don't let it go
17344 beyond the right edge of the window. */
17345 if (max_x <= 0)
17346 max_x = it->last_visible_x;
17347 else
17348 max_x = min (max_x, it->last_visible_x);
17350 /* Skip over display elements that are not visible. because IT->w is
17351 hscrolled. */
17352 if (it->current_x < it->first_visible_x)
17353 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17354 MOVE_TO_POS | MOVE_TO_X);
17356 row->ascent = it->max_ascent;
17357 row->height = it->max_ascent + it->max_descent;
17358 row->phys_ascent = it->max_phys_ascent;
17359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17360 row->extra_line_spacing = it->max_extra_line_spacing;
17362 /* This condition is for the case that we are called with current_x
17363 past last_visible_x. */
17364 while (it->current_x < max_x)
17366 int x_before, x, n_glyphs_before, i, nglyphs;
17368 /* Get the next display element. */
17369 if (!get_next_display_element (it))
17370 break;
17372 /* Produce glyphs. */
17373 x_before = it->current_x;
17374 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17375 PRODUCE_GLYPHS (it);
17377 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17378 i = 0;
17379 x = x_before;
17380 while (i < nglyphs)
17382 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17384 if (!it->truncate_lines_p
17385 && x + glyph->pixel_width > max_x)
17387 /* End of continued line or max_x reached. */
17388 if (CHAR_GLYPH_PADDING_P (*glyph))
17390 /* A wide character is unbreakable. */
17391 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17392 it->current_x = x_before;
17394 else
17396 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17397 it->current_x = x;
17399 break;
17401 else if (x + glyph->pixel_width > it->first_visible_x)
17403 /* Glyph is at least partially visible. */
17404 ++it->hpos;
17405 if (x < it->first_visible_x)
17406 it->glyph_row->x = x - it->first_visible_x;
17408 else
17410 /* Glyph is off the left margin of the display area.
17411 Should not happen. */
17412 abort ();
17415 row->ascent = max (row->ascent, it->max_ascent);
17416 row->height = max (row->height, it->max_ascent + it->max_descent);
17417 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17418 row->phys_height = max (row->phys_height,
17419 it->max_phys_ascent + it->max_phys_descent);
17420 row->extra_line_spacing = max (row->extra_line_spacing,
17421 it->max_extra_line_spacing);
17422 x += glyph->pixel_width;
17423 ++i;
17426 /* Stop if max_x reached. */
17427 if (i < nglyphs)
17428 break;
17430 /* Stop at line ends. */
17431 if (ITERATOR_AT_END_OF_LINE_P (it))
17433 it->continuation_lines_width = 0;
17434 break;
17437 set_iterator_to_next (it, 1);
17439 /* Stop if truncating at the right edge. */
17440 if (it->truncate_lines_p
17441 && it->current_x >= it->last_visible_x)
17443 /* Add truncation mark, but don't do it if the line is
17444 truncated at a padding space. */
17445 if (IT_CHARPOS (*it) < it->string_nchars)
17447 if (!FRAME_WINDOW_P (it->f))
17449 int i, n;
17451 if (it->current_x > it->last_visible_x)
17453 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17454 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17455 break;
17456 for (n = row->used[TEXT_AREA]; i < n; ++i)
17458 row->used[TEXT_AREA] = i;
17459 produce_special_glyphs (it, IT_TRUNCATION);
17462 produce_special_glyphs (it, IT_TRUNCATION);
17464 it->glyph_row->truncated_on_right_p = 1;
17466 break;
17470 /* Maybe insert a truncation at the left. */
17471 if (it->first_visible_x
17472 && IT_CHARPOS (*it) > 0)
17474 if (!FRAME_WINDOW_P (it->f))
17475 insert_left_trunc_glyphs (it);
17476 it->glyph_row->truncated_on_left_p = 1;
17479 it->face_id = saved_face_id;
17481 /* Value is number of columns displayed. */
17482 return it->hpos - hpos_at_start;
17487 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17488 appears as an element of LIST or as the car of an element of LIST.
17489 If PROPVAL is a list, compare each element against LIST in that
17490 way, and return 1/2 if any element of PROPVAL is found in LIST.
17491 Otherwise return 0. This function cannot quit.
17492 The return value is 2 if the text is invisible but with an ellipsis
17493 and 1 if it's invisible and without an ellipsis. */
17496 invisible_p (propval, list)
17497 register Lisp_Object propval;
17498 Lisp_Object list;
17500 register Lisp_Object tail, proptail;
17502 for (tail = list; CONSP (tail); tail = XCDR (tail))
17504 register Lisp_Object tem;
17505 tem = XCAR (tail);
17506 if (EQ (propval, tem))
17507 return 1;
17508 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17509 return NILP (XCDR (tem)) ? 1 : 2;
17512 if (CONSP (propval))
17514 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17516 Lisp_Object propelt;
17517 propelt = XCAR (proptail);
17518 for (tail = list; CONSP (tail); tail = XCDR (tail))
17520 register Lisp_Object tem;
17521 tem = XCAR (tail);
17522 if (EQ (propelt, tem))
17523 return 1;
17524 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17525 return NILP (XCDR (tem)) ? 1 : 2;
17530 return 0;
17533 /* Calculate a width or height in pixels from a specification using
17534 the following elements:
17536 SPEC ::=
17537 NUM - a (fractional) multiple of the default font width/height
17538 (NUM) - specifies exactly NUM pixels
17539 UNIT - a fixed number of pixels, see below.
17540 ELEMENT - size of a display element in pixels, see below.
17541 (NUM . SPEC) - equals NUM * SPEC
17542 (+ SPEC SPEC ...) - add pixel values
17543 (- SPEC SPEC ...) - subtract pixel values
17544 (- SPEC) - negate pixel value
17546 NUM ::=
17547 INT or FLOAT - a number constant
17548 SYMBOL - use symbol's (buffer local) variable binding.
17550 UNIT ::=
17551 in - pixels per inch *)
17552 mm - pixels per 1/1000 meter *)
17553 cm - pixels per 1/100 meter *)
17554 width - width of current font in pixels.
17555 height - height of current font in pixels.
17557 *) using the ratio(s) defined in display-pixels-per-inch.
17559 ELEMENT ::=
17561 left-fringe - left fringe width in pixels
17562 right-fringe - right fringe width in pixels
17564 left-margin - left margin width in pixels
17565 right-margin - right margin width in pixels
17567 scroll-bar - scroll-bar area width in pixels
17569 Examples:
17571 Pixels corresponding to 5 inches:
17572 (5 . in)
17574 Total width of non-text areas on left side of window (if scroll-bar is on left):
17575 '(space :width (+ left-fringe left-margin scroll-bar))
17577 Align to first text column (in header line):
17578 '(space :align-to 0)
17580 Align to middle of text area minus half the width of variable `my-image'
17581 containing a loaded image:
17582 '(space :align-to (0.5 . (- text my-image)))
17584 Width of left margin minus width of 1 character in the default font:
17585 '(space :width (- left-margin 1))
17587 Width of left margin minus width of 2 characters in the current font:
17588 '(space :width (- left-margin (2 . width)))
17590 Center 1 character over left-margin (in header line):
17591 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17593 Different ways to express width of left fringe plus left margin minus one pixel:
17594 '(space :width (- (+ left-fringe left-margin) (1)))
17595 '(space :width (+ left-fringe left-margin (- (1))))
17596 '(space :width (+ left-fringe left-margin (-1)))
17600 #define NUMVAL(X) \
17601 ((INTEGERP (X) || FLOATP (X)) \
17602 ? XFLOATINT (X) \
17603 : - 1)
17606 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17607 double *res;
17608 struct it *it;
17609 Lisp_Object prop;
17610 void *font;
17611 int width_p, *align_to;
17613 double pixels;
17615 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17616 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17618 if (NILP (prop))
17619 return OK_PIXELS (0);
17621 if (SYMBOLP (prop))
17623 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17625 char *unit = SDATA (SYMBOL_NAME (prop));
17627 if (unit[0] == 'i' && unit[1] == 'n')
17628 pixels = 1.0;
17629 else if (unit[0] == 'm' && unit[1] == 'm')
17630 pixels = 25.4;
17631 else if (unit[0] == 'c' && unit[1] == 'm')
17632 pixels = 2.54;
17633 else
17634 pixels = 0;
17635 if (pixels > 0)
17637 double ppi;
17638 #ifdef HAVE_WINDOW_SYSTEM
17639 if (FRAME_WINDOW_P (it->f)
17640 && (ppi = (width_p
17641 ? FRAME_X_DISPLAY_INFO (it->f)->resx
17642 : FRAME_X_DISPLAY_INFO (it->f)->resy),
17643 ppi > 0))
17644 return OK_PIXELS (ppi / pixels);
17645 #endif
17647 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17648 || (CONSP (Vdisplay_pixels_per_inch)
17649 && (ppi = (width_p
17650 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17651 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17652 ppi > 0)))
17653 return OK_PIXELS (ppi / pixels);
17655 return 0;
17659 #ifdef HAVE_WINDOW_SYSTEM
17660 if (EQ (prop, Qheight))
17661 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17662 if (EQ (prop, Qwidth))
17663 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17664 #else
17665 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17666 return OK_PIXELS (1);
17667 #endif
17669 if (EQ (prop, Qtext))
17670 return OK_PIXELS (width_p
17671 ? window_box_width (it->w, TEXT_AREA)
17672 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17674 if (align_to && *align_to < 0)
17676 *res = 0;
17677 if (EQ (prop, Qleft))
17678 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17679 if (EQ (prop, Qright))
17680 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17681 if (EQ (prop, Qcenter))
17682 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17683 + window_box_width (it->w, TEXT_AREA) / 2);
17684 if (EQ (prop, Qleft_fringe))
17685 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17686 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17687 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17688 if (EQ (prop, Qright_fringe))
17689 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17690 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17691 : window_box_right_offset (it->w, TEXT_AREA));
17692 if (EQ (prop, Qleft_margin))
17693 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17694 if (EQ (prop, Qright_margin))
17695 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17696 if (EQ (prop, Qscroll_bar))
17697 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17699 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17700 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17701 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17702 : 0)));
17704 else
17706 if (EQ (prop, Qleft_fringe))
17707 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17708 if (EQ (prop, Qright_fringe))
17709 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17710 if (EQ (prop, Qleft_margin))
17711 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17712 if (EQ (prop, Qright_margin))
17713 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17714 if (EQ (prop, Qscroll_bar))
17715 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17718 prop = Fbuffer_local_value (prop, it->w->buffer);
17721 if (INTEGERP (prop) || FLOATP (prop))
17723 int base_unit = (width_p
17724 ? FRAME_COLUMN_WIDTH (it->f)
17725 : FRAME_LINE_HEIGHT (it->f));
17726 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17729 if (CONSP (prop))
17731 Lisp_Object car = XCAR (prop);
17732 Lisp_Object cdr = XCDR (prop);
17734 if (SYMBOLP (car))
17736 #ifdef HAVE_WINDOW_SYSTEM
17737 if (valid_image_p (prop))
17739 int id = lookup_image (it->f, prop);
17740 struct image *img = IMAGE_FROM_ID (it->f, id);
17742 return OK_PIXELS (width_p ? img->width : img->height);
17744 #endif
17745 if (EQ (car, Qplus) || EQ (car, Qminus))
17747 int first = 1;
17748 double px;
17750 pixels = 0;
17751 while (CONSP (cdr))
17753 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17754 font, width_p, align_to))
17755 return 0;
17756 if (first)
17757 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17758 else
17759 pixels += px;
17760 cdr = XCDR (cdr);
17762 if (EQ (car, Qminus))
17763 pixels = -pixels;
17764 return OK_PIXELS (pixels);
17767 car = Fbuffer_local_value (car, it->w->buffer);
17770 if (INTEGERP (car) || FLOATP (car))
17772 double fact;
17773 pixels = XFLOATINT (car);
17774 if (NILP (cdr))
17775 return OK_PIXELS (pixels);
17776 if (calc_pixel_width_or_height (&fact, it, cdr,
17777 font, width_p, align_to))
17778 return OK_PIXELS (pixels * fact);
17779 return 0;
17782 return 0;
17785 return 0;
17789 /***********************************************************************
17790 Glyph Display
17791 ***********************************************************************/
17793 #ifdef HAVE_WINDOW_SYSTEM
17795 #if GLYPH_DEBUG
17797 void
17798 dump_glyph_string (s)
17799 struct glyph_string *s;
17801 fprintf (stderr, "glyph string\n");
17802 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17803 s->x, s->y, s->width, s->height);
17804 fprintf (stderr, " ybase = %d\n", s->ybase);
17805 fprintf (stderr, " hl = %d\n", s->hl);
17806 fprintf (stderr, " left overhang = %d, right = %d\n",
17807 s->left_overhang, s->right_overhang);
17808 fprintf (stderr, " nchars = %d\n", s->nchars);
17809 fprintf (stderr, " extends to end of line = %d\n",
17810 s->extends_to_end_of_line_p);
17811 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17812 fprintf (stderr, " bg width = %d\n", s->background_width);
17815 #endif /* GLYPH_DEBUG */
17817 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17818 of XChar2b structures for S; it can't be allocated in
17819 init_glyph_string because it must be allocated via `alloca'. W
17820 is the window on which S is drawn. ROW and AREA are the glyph row
17821 and area within the row from which S is constructed. START is the
17822 index of the first glyph structure covered by S. HL is a
17823 face-override for drawing S. */
17825 #ifdef HAVE_NTGUI
17826 #define OPTIONAL_HDC(hdc) hdc,
17827 #define DECLARE_HDC(hdc) HDC hdc;
17828 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17829 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17830 #endif
17832 #ifndef OPTIONAL_HDC
17833 #define OPTIONAL_HDC(hdc)
17834 #define DECLARE_HDC(hdc)
17835 #define ALLOCATE_HDC(hdc, f)
17836 #define RELEASE_HDC(hdc, f)
17837 #endif
17839 static void
17840 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17841 struct glyph_string *s;
17842 DECLARE_HDC (hdc)
17843 XChar2b *char2b;
17844 struct window *w;
17845 struct glyph_row *row;
17846 enum glyph_row_area area;
17847 int start;
17848 enum draw_glyphs_face hl;
17850 bzero (s, sizeof *s);
17851 s->w = w;
17852 s->f = XFRAME (w->frame);
17853 #ifdef HAVE_NTGUI
17854 s->hdc = hdc;
17855 #endif
17856 s->display = FRAME_X_DISPLAY (s->f);
17857 s->window = FRAME_X_WINDOW (s->f);
17858 s->char2b = char2b;
17859 s->hl = hl;
17860 s->row = row;
17861 s->area = area;
17862 s->first_glyph = row->glyphs[area] + start;
17863 s->height = row->height;
17864 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17866 /* Display the internal border below the tool-bar window. */
17867 if (WINDOWP (s->f->tool_bar_window)
17868 && s->w == XWINDOW (s->f->tool_bar_window))
17869 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17871 s->ybase = s->y + row->ascent;
17875 /* Append the list of glyph strings with head H and tail T to the list
17876 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17878 static INLINE void
17879 append_glyph_string_lists (head, tail, h, t)
17880 struct glyph_string **head, **tail;
17881 struct glyph_string *h, *t;
17883 if (h)
17885 if (*head)
17886 (*tail)->next = h;
17887 else
17888 *head = h;
17889 h->prev = *tail;
17890 *tail = t;
17895 /* Prepend the list of glyph strings with head H and tail T to the
17896 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17897 result. */
17899 static INLINE void
17900 prepend_glyph_string_lists (head, tail, h, t)
17901 struct glyph_string **head, **tail;
17902 struct glyph_string *h, *t;
17904 if (h)
17906 if (*head)
17907 (*head)->prev = t;
17908 else
17909 *tail = t;
17910 t->next = *head;
17911 *head = h;
17916 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17917 Set *HEAD and *TAIL to the resulting list. */
17919 static INLINE void
17920 append_glyph_string (head, tail, s)
17921 struct glyph_string **head, **tail;
17922 struct glyph_string *s;
17924 s->next = s->prev = NULL;
17925 append_glyph_string_lists (head, tail, s, s);
17929 /* Get face and two-byte form of character glyph GLYPH on frame F.
17930 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17931 a pointer to a realized face that is ready for display. */
17933 static INLINE struct face *
17934 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17935 struct frame *f;
17936 struct glyph *glyph;
17937 XChar2b *char2b;
17938 int *two_byte_p;
17940 struct face *face;
17942 xassert (glyph->type == CHAR_GLYPH);
17943 face = FACE_FROM_ID (f, glyph->face_id);
17945 if (two_byte_p)
17946 *two_byte_p = 0;
17948 if (!glyph->multibyte_p)
17950 /* Unibyte case. We don't have to encode, but we have to make
17951 sure to use a face suitable for unibyte. */
17952 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17954 else if (glyph->u.ch < 128
17955 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17957 /* Case of ASCII in a face known to fit ASCII. */
17958 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17960 else
17962 int c1, c2, charset;
17964 /* Split characters into bytes. If c2 is -1 afterwards, C is
17965 really a one-byte character so that byte1 is zero. */
17966 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17967 if (c2 > 0)
17968 STORE_XCHAR2B (char2b, c1, c2);
17969 else
17970 STORE_XCHAR2B (char2b, 0, c1);
17972 /* Maybe encode the character in *CHAR2B. */
17973 if (charset != CHARSET_ASCII)
17975 struct font_info *font_info
17976 = FONT_INFO_FROM_ID (f, face->font_info_id);
17977 if (font_info)
17978 glyph->font_type
17979 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17983 /* Make sure X resources of the face are allocated. */
17984 xassert (face != NULL);
17985 PREPARE_FACE_FOR_DISPLAY (f, face);
17986 return face;
17990 /* Fill glyph string S with composition components specified by S->cmp.
17992 FACES is an array of faces for all components of this composition.
17993 S->gidx is the index of the first component for S.
17994 OVERLAPS_P non-zero means S should draw the foreground only, and
17995 use its physical height for clipping.
17997 Value is the index of a component not in S. */
17999 static int
18000 fill_composite_glyph_string (s, faces, overlaps_p)
18001 struct glyph_string *s;
18002 struct face **faces;
18003 int overlaps_p;
18005 int i;
18007 xassert (s);
18009 s->for_overlaps_p = overlaps_p;
18011 s->face = faces[s->gidx];
18012 s->font = s->face->font;
18013 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18015 /* For all glyphs of this composition, starting at the offset
18016 S->gidx, until we reach the end of the definition or encounter a
18017 glyph that requires the different face, add it to S. */
18018 ++s->nchars;
18019 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18020 ++s->nchars;
18022 /* All glyph strings for the same composition has the same width,
18023 i.e. the width set for the first component of the composition. */
18025 s->width = s->first_glyph->pixel_width;
18027 /* If the specified font could not be loaded, use the frame's
18028 default font, but record the fact that we couldn't load it in
18029 the glyph string so that we can draw rectangles for the
18030 characters of the glyph string. */
18031 if (s->font == NULL)
18033 s->font_not_found_p = 1;
18034 s->font = FRAME_FONT (s->f);
18037 /* Adjust base line for subscript/superscript text. */
18038 s->ybase += s->first_glyph->voffset;
18040 xassert (s->face && s->face->gc);
18042 /* This glyph string must always be drawn with 16-bit functions. */
18043 s->two_byte_p = 1;
18045 return s->gidx + s->nchars;
18049 /* Fill glyph string S from a sequence of character glyphs.
18051 FACE_ID is the face id of the string. START is the index of the
18052 first glyph to consider, END is the index of the last + 1.
18053 OVERLAPS_P non-zero means S should draw the foreground only, and
18054 use its physical height for clipping.
18056 Value is the index of the first glyph not in S. */
18058 static int
18059 fill_glyph_string (s, face_id, start, end, overlaps_p)
18060 struct glyph_string *s;
18061 int face_id;
18062 int start, end, overlaps_p;
18064 struct glyph *glyph, *last;
18065 int voffset;
18066 int glyph_not_available_p;
18068 xassert (s->f == XFRAME (s->w->frame));
18069 xassert (s->nchars == 0);
18070 xassert (start >= 0 && end > start);
18072 s->for_overlaps_p = overlaps_p,
18073 glyph = s->row->glyphs[s->area] + start;
18074 last = s->row->glyphs[s->area] + end;
18075 voffset = glyph->voffset;
18077 glyph_not_available_p = glyph->glyph_not_available_p;
18079 while (glyph < last
18080 && glyph->type == CHAR_GLYPH
18081 && glyph->voffset == voffset
18082 /* Same face id implies same font, nowadays. */
18083 && glyph->face_id == face_id
18084 && glyph->glyph_not_available_p == glyph_not_available_p)
18086 int two_byte_p;
18088 s->face = get_glyph_face_and_encoding (s->f, glyph,
18089 s->char2b + s->nchars,
18090 &two_byte_p);
18091 s->two_byte_p = two_byte_p;
18092 ++s->nchars;
18093 xassert (s->nchars <= end - start);
18094 s->width += glyph->pixel_width;
18095 ++glyph;
18098 s->font = s->face->font;
18099 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18101 /* If the specified font could not be loaded, use the frame's font,
18102 but record the fact that we couldn't load it in
18103 S->font_not_found_p so that we can draw rectangles for the
18104 characters of the glyph string. */
18105 if (s->font == NULL || glyph_not_available_p)
18107 s->font_not_found_p = 1;
18108 s->font = FRAME_FONT (s->f);
18111 /* Adjust base line for subscript/superscript text. */
18112 s->ybase += voffset;
18114 xassert (s->face && s->face->gc);
18115 return glyph - s->row->glyphs[s->area];
18119 /* Fill glyph string S from image glyph S->first_glyph. */
18121 static void
18122 fill_image_glyph_string (s)
18123 struct glyph_string *s;
18125 xassert (s->first_glyph->type == IMAGE_GLYPH);
18126 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18127 xassert (s->img);
18128 s->slice = s->first_glyph->slice;
18129 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18130 s->font = s->face->font;
18131 s->width = s->first_glyph->pixel_width;
18133 /* Adjust base line for subscript/superscript text. */
18134 s->ybase += s->first_glyph->voffset;
18138 /* Fill glyph string S from a sequence of stretch glyphs.
18140 ROW is the glyph row in which the glyphs are found, AREA is the
18141 area within the row. START is the index of the first glyph to
18142 consider, END is the index of the last + 1.
18144 Value is the index of the first glyph not in S. */
18146 static int
18147 fill_stretch_glyph_string (s, row, area, start, end)
18148 struct glyph_string *s;
18149 struct glyph_row *row;
18150 enum glyph_row_area area;
18151 int start, end;
18153 struct glyph *glyph, *last;
18154 int voffset, face_id;
18156 xassert (s->first_glyph->type == STRETCH_GLYPH);
18158 glyph = s->row->glyphs[s->area] + start;
18159 last = s->row->glyphs[s->area] + end;
18160 face_id = glyph->face_id;
18161 s->face = FACE_FROM_ID (s->f, face_id);
18162 s->font = s->face->font;
18163 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18164 s->width = glyph->pixel_width;
18165 voffset = glyph->voffset;
18167 for (++glyph;
18168 (glyph < last
18169 && glyph->type == STRETCH_GLYPH
18170 && glyph->voffset == voffset
18171 && glyph->face_id == face_id);
18172 ++glyph)
18173 s->width += glyph->pixel_width;
18175 /* Adjust base line for subscript/superscript text. */
18176 s->ybase += voffset;
18178 /* The case that face->gc == 0 is handled when drawing the glyph
18179 string by calling PREPARE_FACE_FOR_DISPLAY. */
18180 xassert (s->face);
18181 return glyph - s->row->glyphs[s->area];
18185 /* EXPORT for RIF:
18186 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18187 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18188 assumed to be zero. */
18190 void
18191 x_get_glyph_overhangs (glyph, f, left, right)
18192 struct glyph *glyph;
18193 struct frame *f;
18194 int *left, *right;
18196 *left = *right = 0;
18198 if (glyph->type == CHAR_GLYPH)
18200 XFontStruct *font;
18201 struct face *face;
18202 struct font_info *font_info;
18203 XChar2b char2b;
18204 XCharStruct *pcm;
18206 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18207 font = face->font;
18208 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18209 if (font /* ++KFS: Should this be font_info ? */
18210 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18212 if (pcm->rbearing > pcm->width)
18213 *right = pcm->rbearing - pcm->width;
18214 if (pcm->lbearing < 0)
18215 *left = -pcm->lbearing;
18221 /* Return the index of the first glyph preceding glyph string S that
18222 is overwritten by S because of S's left overhang. Value is -1
18223 if no glyphs are overwritten. */
18225 static int
18226 left_overwritten (s)
18227 struct glyph_string *s;
18229 int k;
18231 if (s->left_overhang)
18233 int x = 0, i;
18234 struct glyph *glyphs = s->row->glyphs[s->area];
18235 int first = s->first_glyph - glyphs;
18237 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18238 x -= glyphs[i].pixel_width;
18240 k = i + 1;
18242 else
18243 k = -1;
18245 return k;
18249 /* Return the index of the first glyph preceding glyph string S that
18250 is overwriting S because of its right overhang. Value is -1 if no
18251 glyph in front of S overwrites S. */
18253 static int
18254 left_overwriting (s)
18255 struct glyph_string *s;
18257 int i, k, x;
18258 struct glyph *glyphs = s->row->glyphs[s->area];
18259 int first = s->first_glyph - glyphs;
18261 k = -1;
18262 x = 0;
18263 for (i = first - 1; i >= 0; --i)
18265 int left, right;
18266 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18267 if (x + right > 0)
18268 k = i;
18269 x -= glyphs[i].pixel_width;
18272 return k;
18276 /* Return the index of the last glyph following glyph string S that is
18277 not overwritten by S because of S's right overhang. Value is -1 if
18278 no such glyph is found. */
18280 static int
18281 right_overwritten (s)
18282 struct glyph_string *s;
18284 int k = -1;
18286 if (s->right_overhang)
18288 int x = 0, i;
18289 struct glyph *glyphs = s->row->glyphs[s->area];
18290 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18291 int end = s->row->used[s->area];
18293 for (i = first; i < end && s->right_overhang > x; ++i)
18294 x += glyphs[i].pixel_width;
18296 k = i;
18299 return k;
18303 /* Return the index of the last glyph following glyph string S that
18304 overwrites S because of its left overhang. Value is negative
18305 if no such glyph is found. */
18307 static int
18308 right_overwriting (s)
18309 struct glyph_string *s;
18311 int i, k, x;
18312 int end = s->row->used[s->area];
18313 struct glyph *glyphs = s->row->glyphs[s->area];
18314 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18316 k = -1;
18317 x = 0;
18318 for (i = first; i < end; ++i)
18320 int left, right;
18321 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18322 if (x - left < 0)
18323 k = i;
18324 x += glyphs[i].pixel_width;
18327 return k;
18331 /* Get face and two-byte form of character C in face FACE_ID on frame
18332 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18333 means we want to display multibyte text. DISPLAY_P non-zero means
18334 make sure that X resources for the face returned are allocated.
18335 Value is a pointer to a realized face that is ready for display if
18336 DISPLAY_P is non-zero. */
18338 static INLINE struct face *
18339 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18340 struct frame *f;
18341 int c, face_id;
18342 XChar2b *char2b;
18343 int multibyte_p, display_p;
18345 struct face *face = FACE_FROM_ID (f, face_id);
18347 if (!multibyte_p)
18349 /* Unibyte case. We don't have to encode, but we have to make
18350 sure to use a face suitable for unibyte. */
18351 STORE_XCHAR2B (char2b, 0, c);
18352 face_id = FACE_FOR_CHAR (f, face, c);
18353 face = FACE_FROM_ID (f, face_id);
18355 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18357 /* Case of ASCII in a face known to fit ASCII. */
18358 STORE_XCHAR2B (char2b, 0, c);
18360 else
18362 int c1, c2, charset;
18364 /* Split characters into bytes. If c2 is -1 afterwards, C is
18365 really a one-byte character so that byte1 is zero. */
18366 SPLIT_CHAR (c, charset, c1, c2);
18367 if (c2 > 0)
18368 STORE_XCHAR2B (char2b, c1, c2);
18369 else
18370 STORE_XCHAR2B (char2b, 0, c1);
18372 /* Maybe encode the character in *CHAR2B. */
18373 if (face->font != NULL)
18375 struct font_info *font_info
18376 = FONT_INFO_FROM_ID (f, face->font_info_id);
18377 if (font_info)
18378 rif->encode_char (c, char2b, font_info, 0);
18382 /* Make sure X resources of the face are allocated. */
18383 #ifdef HAVE_X_WINDOWS
18384 if (display_p)
18385 #endif
18387 xassert (face != NULL);
18388 PREPARE_FACE_FOR_DISPLAY (f, face);
18391 return face;
18395 /* Set background width of glyph string S. START is the index of the
18396 first glyph following S. LAST_X is the right-most x-position + 1
18397 in the drawing area. */
18399 static INLINE void
18400 set_glyph_string_background_width (s, start, last_x)
18401 struct glyph_string *s;
18402 int start;
18403 int last_x;
18405 /* If the face of this glyph string has to be drawn to the end of
18406 the drawing area, set S->extends_to_end_of_line_p. */
18407 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18409 if (start == s->row->used[s->area]
18410 && s->area == TEXT_AREA
18411 && ((s->hl == DRAW_NORMAL_TEXT
18412 && (s->row->fill_line_p
18413 || s->face->background != default_face->background
18414 || s->face->stipple != default_face->stipple
18415 || s->row->mouse_face_p))
18416 || s->hl == DRAW_MOUSE_FACE
18417 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18418 && s->row->fill_line_p)))
18419 s->extends_to_end_of_line_p = 1;
18421 /* If S extends its face to the end of the line, set its
18422 background_width to the distance to the right edge of the drawing
18423 area. */
18424 if (s->extends_to_end_of_line_p)
18425 s->background_width = last_x - s->x + 1;
18426 else
18427 s->background_width = s->width;
18431 /* Compute overhangs and x-positions for glyph string S and its
18432 predecessors, or successors. X is the starting x-position for S.
18433 BACKWARD_P non-zero means process predecessors. */
18435 static void
18436 compute_overhangs_and_x (s, x, backward_p)
18437 struct glyph_string *s;
18438 int x;
18439 int backward_p;
18441 if (backward_p)
18443 while (s)
18445 if (rif->compute_glyph_string_overhangs)
18446 rif->compute_glyph_string_overhangs (s);
18447 x -= s->width;
18448 s->x = x;
18449 s = s->prev;
18452 else
18454 while (s)
18456 if (rif->compute_glyph_string_overhangs)
18457 rif->compute_glyph_string_overhangs (s);
18458 s->x = x;
18459 x += s->width;
18460 s = s->next;
18467 /* The following macros are only called from draw_glyphs below.
18468 They reference the following parameters of that function directly:
18469 `w', `row', `area', and `overlap_p'
18470 as well as the following local variables:
18471 `s', `f', and `hdc' (in W32) */
18473 #ifdef HAVE_NTGUI
18474 /* On W32, silently add local `hdc' variable to argument list of
18475 init_glyph_string. */
18476 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18477 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18478 #else
18479 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18480 init_glyph_string (s, char2b, w, row, area, start, hl)
18481 #endif
18483 /* Add a glyph string for a stretch glyph to the list of strings
18484 between HEAD and TAIL. START is the index of the stretch glyph in
18485 row area AREA of glyph row ROW. END is the index of the last glyph
18486 in that glyph row area. X is the current output position assigned
18487 to the new glyph string constructed. HL overrides that face of the
18488 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18489 is the right-most x-position of the drawing area. */
18491 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18492 and below -- keep them on one line. */
18493 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18494 do \
18496 s = (struct glyph_string *) alloca (sizeof *s); \
18497 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18498 START = fill_stretch_glyph_string (s, row, area, START, END); \
18499 append_glyph_string (&HEAD, &TAIL, s); \
18500 s->x = (X); \
18502 while (0)
18505 /* Add a glyph string for an image glyph to the list of strings
18506 between HEAD and TAIL. START is the index of the image glyph in
18507 row area AREA of glyph row ROW. END is the index of the last glyph
18508 in that glyph row area. X is the current output position assigned
18509 to the new glyph string constructed. HL overrides that face of the
18510 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18511 is the right-most x-position of the drawing area. */
18513 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18514 do \
18516 s = (struct glyph_string *) alloca (sizeof *s); \
18517 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18518 fill_image_glyph_string (s); \
18519 append_glyph_string (&HEAD, &TAIL, s); \
18520 ++START; \
18521 s->x = (X); \
18523 while (0)
18526 /* Add a glyph string for a sequence of character glyphs to the list
18527 of strings between HEAD and TAIL. START is the index of the first
18528 glyph in row area AREA of glyph row ROW that is part of the new
18529 glyph string. END is the index of the last glyph in that glyph row
18530 area. X is the current output position assigned to the new glyph
18531 string constructed. HL overrides that face of the glyph; e.g. it
18532 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18533 right-most x-position of the drawing area. */
18535 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18536 do \
18538 int c, face_id; \
18539 XChar2b *char2b; \
18541 c = (row)->glyphs[area][START].u.ch; \
18542 face_id = (row)->glyphs[area][START].face_id; \
18544 s = (struct glyph_string *) alloca (sizeof *s); \
18545 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18546 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18547 append_glyph_string (&HEAD, &TAIL, s); \
18548 s->x = (X); \
18549 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18551 while (0)
18554 /* Add a glyph string for a composite sequence to the list of strings
18555 between HEAD and TAIL. START is the index of the first glyph in
18556 row area AREA of glyph row ROW that is part of the new glyph
18557 string. END is the index of the last glyph in that glyph row area.
18558 X is the current output position assigned to the new glyph string
18559 constructed. HL overrides that face of the glyph; e.g. it is
18560 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18561 x-position of the drawing area. */
18563 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18564 do { \
18565 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18566 int face_id = (row)->glyphs[area][START].face_id; \
18567 struct face *base_face = FACE_FROM_ID (f, face_id); \
18568 struct composition *cmp = composition_table[cmp_id]; \
18569 int glyph_len = cmp->glyph_len; \
18570 XChar2b *char2b; \
18571 struct face **faces; \
18572 struct glyph_string *first_s = NULL; \
18573 int n; \
18575 base_face = base_face->ascii_face; \
18576 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18577 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18578 /* At first, fill in `char2b' and `faces'. */ \
18579 for (n = 0; n < glyph_len; n++) \
18581 int c = COMPOSITION_GLYPH (cmp, n); \
18582 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18583 faces[n] = FACE_FROM_ID (f, this_face_id); \
18584 get_char_face_and_encoding (f, c, this_face_id, \
18585 char2b + n, 1, 1); \
18588 /* Make glyph_strings for each glyph sequence that is drawable by \
18589 the same face, and append them to HEAD/TAIL. */ \
18590 for (n = 0; n < cmp->glyph_len;) \
18592 s = (struct glyph_string *) alloca (sizeof *s); \
18593 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18594 append_glyph_string (&(HEAD), &(TAIL), s); \
18595 s->cmp = cmp; \
18596 s->gidx = n; \
18597 s->x = (X); \
18599 if (n == 0) \
18600 first_s = s; \
18602 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18605 ++START; \
18606 s = first_s; \
18607 } while (0)
18610 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18611 of AREA of glyph row ROW on window W between indices START and END.
18612 HL overrides the face for drawing glyph strings, e.g. it is
18613 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18614 x-positions of the drawing area.
18616 This is an ugly monster macro construct because we must use alloca
18617 to allocate glyph strings (because draw_glyphs can be called
18618 asynchronously). */
18620 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18621 do \
18623 HEAD = TAIL = NULL; \
18624 while (START < END) \
18626 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18627 switch (first_glyph->type) \
18629 case CHAR_GLYPH: \
18630 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18631 HL, X, LAST_X); \
18632 break; \
18634 case COMPOSITE_GLYPH: \
18635 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18636 HL, X, LAST_X); \
18637 break; \
18639 case STRETCH_GLYPH: \
18640 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18641 HL, X, LAST_X); \
18642 break; \
18644 case IMAGE_GLYPH: \
18645 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18646 HL, X, LAST_X); \
18647 break; \
18649 default: \
18650 abort (); \
18653 set_glyph_string_background_width (s, START, LAST_X); \
18654 (X) += s->width; \
18657 while (0)
18660 /* Draw glyphs between START and END in AREA of ROW on window W,
18661 starting at x-position X. X is relative to AREA in W. HL is a
18662 face-override with the following meaning:
18664 DRAW_NORMAL_TEXT draw normally
18665 DRAW_CURSOR draw in cursor face
18666 DRAW_MOUSE_FACE draw in mouse face.
18667 DRAW_INVERSE_VIDEO draw in mode line face
18668 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18669 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18671 If OVERLAPS_P is non-zero, draw only the foreground of characters
18672 and clip to the physical height of ROW.
18674 Value is the x-position reached, relative to AREA of W. */
18676 static int
18677 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18678 struct window *w;
18679 int x;
18680 struct glyph_row *row;
18681 enum glyph_row_area area;
18682 int start, end;
18683 enum draw_glyphs_face hl;
18684 int overlaps_p;
18686 struct glyph_string *head, *tail;
18687 struct glyph_string *s;
18688 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18689 int last_x, area_width;
18690 int x_reached;
18691 int i, j;
18692 struct frame *f = XFRAME (WINDOW_FRAME (w));
18693 DECLARE_HDC (hdc);
18695 ALLOCATE_HDC (hdc, f);
18697 /* Let's rather be paranoid than getting a SEGV. */
18698 end = min (end, row->used[area]);
18699 start = max (0, start);
18700 start = min (end, start);
18702 /* Translate X to frame coordinates. Set last_x to the right
18703 end of the drawing area. */
18704 if (row->full_width_p)
18706 /* X is relative to the left edge of W, without scroll bars
18707 or fringes. */
18708 x += WINDOW_LEFT_EDGE_X (w);
18709 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18711 else
18713 int area_left = window_box_left (w, area);
18714 x += area_left;
18715 area_width = window_box_width (w, area);
18716 last_x = area_left + area_width;
18719 /* Build a doubly-linked list of glyph_string structures between
18720 head and tail from what we have to draw. Note that the macro
18721 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18722 the reason we use a separate variable `i'. */
18723 i = start;
18724 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18725 if (tail)
18726 x_reached = tail->x + tail->background_width;
18727 else
18728 x_reached = x;
18730 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18731 the row, redraw some glyphs in front or following the glyph
18732 strings built above. */
18733 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18735 int dummy_x = 0;
18736 struct glyph_string *h, *t;
18738 /* Compute overhangs for all glyph strings. */
18739 if (rif->compute_glyph_string_overhangs)
18740 for (s = head; s; s = s->next)
18741 rif->compute_glyph_string_overhangs (s);
18743 /* Prepend glyph strings for glyphs in front of the first glyph
18744 string that are overwritten because of the first glyph
18745 string's left overhang. The background of all strings
18746 prepended must be drawn because the first glyph string
18747 draws over it. */
18748 i = left_overwritten (head);
18749 if (i >= 0)
18751 j = i;
18752 BUILD_GLYPH_STRINGS (j, start, h, t,
18753 DRAW_NORMAL_TEXT, dummy_x, last_x);
18754 start = i;
18755 compute_overhangs_and_x (t, head->x, 1);
18756 prepend_glyph_string_lists (&head, &tail, h, t);
18757 clip_head = head;
18760 /* Prepend glyph strings for glyphs in front of the first glyph
18761 string that overwrite that glyph string because of their
18762 right overhang. For these strings, only the foreground must
18763 be drawn, because it draws over the glyph string at `head'.
18764 The background must not be drawn because this would overwrite
18765 right overhangs of preceding glyphs for which no glyph
18766 strings exist. */
18767 i = left_overwriting (head);
18768 if (i >= 0)
18770 clip_head = head;
18771 BUILD_GLYPH_STRINGS (i, start, h, t,
18772 DRAW_NORMAL_TEXT, dummy_x, last_x);
18773 for (s = h; s; s = s->next)
18774 s->background_filled_p = 1;
18775 compute_overhangs_and_x (t, head->x, 1);
18776 prepend_glyph_string_lists (&head, &tail, h, t);
18779 /* Append glyphs strings for glyphs following the last glyph
18780 string tail that are overwritten by tail. The background of
18781 these strings has to be drawn because tail's foreground draws
18782 over it. */
18783 i = right_overwritten (tail);
18784 if (i >= 0)
18786 BUILD_GLYPH_STRINGS (end, i, h, t,
18787 DRAW_NORMAL_TEXT, x, last_x);
18788 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18789 append_glyph_string_lists (&head, &tail, h, t);
18790 clip_tail = tail;
18793 /* Append glyph strings for glyphs following the last glyph
18794 string tail that overwrite tail. The foreground of such
18795 glyphs has to be drawn because it writes into the background
18796 of tail. The background must not be drawn because it could
18797 paint over the foreground of following glyphs. */
18798 i = right_overwriting (tail);
18799 if (i >= 0)
18801 clip_tail = tail;
18802 BUILD_GLYPH_STRINGS (end, i, h, t,
18803 DRAW_NORMAL_TEXT, x, last_x);
18804 for (s = h; s; s = s->next)
18805 s->background_filled_p = 1;
18806 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18807 append_glyph_string_lists (&head, &tail, h, t);
18809 if (clip_head || clip_tail)
18810 for (s = head; s; s = s->next)
18812 s->clip_head = clip_head;
18813 s->clip_tail = clip_tail;
18817 /* Draw all strings. */
18818 for (s = head; s; s = s->next)
18819 rif->draw_glyph_string (s);
18821 if (area == TEXT_AREA
18822 && !row->full_width_p
18823 /* When drawing overlapping rows, only the glyph strings'
18824 foreground is drawn, which doesn't erase a cursor
18825 completely. */
18826 && !overlaps_p)
18828 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18829 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18830 : (tail ? tail->x + tail->background_width : x));
18832 int text_left = window_box_left (w, TEXT_AREA);
18833 x0 -= text_left;
18834 x1 -= text_left;
18836 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18837 row->y, MATRIX_ROW_BOTTOM_Y (row));
18840 /* Value is the x-position up to which drawn, relative to AREA of W.
18841 This doesn't include parts drawn because of overhangs. */
18842 if (row->full_width_p)
18843 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18844 else
18845 x_reached -= window_box_left (w, area);
18847 RELEASE_HDC (hdc, f);
18849 return x_reached;
18852 /* Expand row matrix if too narrow. Don't expand if area
18853 is not present. */
18855 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18857 if (!fonts_changed_p \
18858 && (it->glyph_row->glyphs[area] \
18859 < it->glyph_row->glyphs[area + 1])) \
18861 it->w->ncols_scale_factor++; \
18862 fonts_changed_p = 1; \
18866 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18867 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18869 static INLINE void
18870 append_glyph (it)
18871 struct it *it;
18873 struct glyph *glyph;
18874 enum glyph_row_area area = it->area;
18876 xassert (it->glyph_row);
18877 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18879 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18880 if (glyph < it->glyph_row->glyphs[area + 1])
18882 glyph->charpos = CHARPOS (it->position);
18883 glyph->object = it->object;
18884 glyph->pixel_width = it->pixel_width;
18885 glyph->ascent = it->ascent;
18886 glyph->descent = it->descent;
18887 glyph->voffset = it->voffset;
18888 glyph->type = CHAR_GLYPH;
18889 glyph->multibyte_p = it->multibyte_p;
18890 glyph->left_box_line_p = it->start_of_box_run_p;
18891 glyph->right_box_line_p = it->end_of_box_run_p;
18892 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18893 || it->phys_descent > it->descent);
18894 glyph->padding_p = 0;
18895 glyph->glyph_not_available_p = it->glyph_not_available_p;
18896 glyph->face_id = it->face_id;
18897 glyph->u.ch = it->char_to_display;
18898 glyph->slice = null_glyph_slice;
18899 glyph->font_type = FONT_TYPE_UNKNOWN;
18900 ++it->glyph_row->used[area];
18902 else
18903 IT_EXPAND_MATRIX_WIDTH (it, area);
18906 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18907 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18909 static INLINE void
18910 append_composite_glyph (it)
18911 struct it *it;
18913 struct glyph *glyph;
18914 enum glyph_row_area area = it->area;
18916 xassert (it->glyph_row);
18918 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18919 if (glyph < it->glyph_row->glyphs[area + 1])
18921 glyph->charpos = CHARPOS (it->position);
18922 glyph->object = it->object;
18923 glyph->pixel_width = it->pixel_width;
18924 glyph->ascent = it->ascent;
18925 glyph->descent = it->descent;
18926 glyph->voffset = it->voffset;
18927 glyph->type = COMPOSITE_GLYPH;
18928 glyph->multibyte_p = it->multibyte_p;
18929 glyph->left_box_line_p = it->start_of_box_run_p;
18930 glyph->right_box_line_p = it->end_of_box_run_p;
18931 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18932 || it->phys_descent > it->descent);
18933 glyph->padding_p = 0;
18934 glyph->glyph_not_available_p = 0;
18935 glyph->face_id = it->face_id;
18936 glyph->u.cmp_id = it->cmp_id;
18937 glyph->slice = null_glyph_slice;
18938 glyph->font_type = FONT_TYPE_UNKNOWN;
18939 ++it->glyph_row->used[area];
18941 else
18942 IT_EXPAND_MATRIX_WIDTH (it, area);
18946 /* Change IT->ascent and IT->height according to the setting of
18947 IT->voffset. */
18949 static INLINE void
18950 take_vertical_position_into_account (it)
18951 struct it *it;
18953 if (it->voffset)
18955 if (it->voffset < 0)
18956 /* Increase the ascent so that we can display the text higher
18957 in the line. */
18958 it->ascent -= it->voffset;
18959 else
18960 /* Increase the descent so that we can display the text lower
18961 in the line. */
18962 it->descent += it->voffset;
18967 /* Produce glyphs/get display metrics for the image IT is loaded with.
18968 See the description of struct display_iterator in dispextern.h for
18969 an overview of struct display_iterator. */
18971 static void
18972 produce_image_glyph (it)
18973 struct it *it;
18975 struct image *img;
18976 struct face *face;
18977 int glyph_ascent;
18978 struct glyph_slice slice;
18980 xassert (it->what == IT_IMAGE);
18982 face = FACE_FROM_ID (it->f, it->face_id);
18983 xassert (face);
18984 /* Make sure X resources of the face is loaded. */
18985 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18987 if (it->image_id < 0)
18989 /* Fringe bitmap. */
18990 it->ascent = it->phys_ascent = 0;
18991 it->descent = it->phys_descent = 0;
18992 it->pixel_width = 0;
18993 it->nglyphs = 0;
18994 return;
18997 img = IMAGE_FROM_ID (it->f, it->image_id);
18998 xassert (img);
18999 /* Make sure X resources of the image is loaded. */
19000 prepare_image_for_display (it->f, img);
19002 slice.x = slice.y = 0;
19003 slice.width = img->width;
19004 slice.height = img->height;
19006 if (INTEGERP (it->slice.x))
19007 slice.x = XINT (it->slice.x);
19008 else if (FLOATP (it->slice.x))
19009 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19011 if (INTEGERP (it->slice.y))
19012 slice.y = XINT (it->slice.y);
19013 else if (FLOATP (it->slice.y))
19014 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19016 if (INTEGERP (it->slice.width))
19017 slice.width = XINT (it->slice.width);
19018 else if (FLOATP (it->slice.width))
19019 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19021 if (INTEGERP (it->slice.height))
19022 slice.height = XINT (it->slice.height);
19023 else if (FLOATP (it->slice.height))
19024 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19026 if (slice.x >= img->width)
19027 slice.x = img->width;
19028 if (slice.y >= img->height)
19029 slice.y = img->height;
19030 if (slice.x + slice.width >= img->width)
19031 slice.width = img->width - slice.x;
19032 if (slice.y + slice.height > img->height)
19033 slice.height = img->height - slice.y;
19035 if (slice.width == 0 || slice.height == 0)
19036 return;
19038 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19040 it->descent = slice.height - glyph_ascent;
19041 if (slice.y == 0)
19042 it->descent += img->vmargin;
19043 if (slice.y + slice.height == img->height)
19044 it->descent += img->vmargin;
19045 it->phys_descent = it->descent;
19047 it->pixel_width = slice.width;
19048 if (slice.x == 0)
19049 it->pixel_width += img->hmargin;
19050 if (slice.x + slice.width == img->width)
19051 it->pixel_width += img->hmargin;
19053 /* It's quite possible for images to have an ascent greater than
19054 their height, so don't get confused in that case. */
19055 if (it->descent < 0)
19056 it->descent = 0;
19058 #if 0 /* this breaks image tiling */
19059 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19060 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19061 if (face_ascent > it->ascent)
19062 it->ascent = it->phys_ascent = face_ascent;
19063 #endif
19065 it->nglyphs = 1;
19067 if (face->box != FACE_NO_BOX)
19069 if (face->box_line_width > 0)
19071 if (slice.y == 0)
19072 it->ascent += face->box_line_width;
19073 if (slice.y + slice.height == img->height)
19074 it->descent += face->box_line_width;
19077 if (it->start_of_box_run_p && slice.x == 0)
19078 it->pixel_width += abs (face->box_line_width);
19079 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19080 it->pixel_width += abs (face->box_line_width);
19083 take_vertical_position_into_account (it);
19085 if (it->glyph_row)
19087 struct glyph *glyph;
19088 enum glyph_row_area area = it->area;
19090 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19091 if (glyph < it->glyph_row->glyphs[area + 1])
19093 glyph->charpos = CHARPOS (it->position);
19094 glyph->object = it->object;
19095 glyph->pixel_width = it->pixel_width;
19096 glyph->ascent = glyph_ascent;
19097 glyph->descent = it->descent;
19098 glyph->voffset = it->voffset;
19099 glyph->type = IMAGE_GLYPH;
19100 glyph->multibyte_p = it->multibyte_p;
19101 glyph->left_box_line_p = it->start_of_box_run_p;
19102 glyph->right_box_line_p = it->end_of_box_run_p;
19103 glyph->overlaps_vertically_p = 0;
19104 glyph->padding_p = 0;
19105 glyph->glyph_not_available_p = 0;
19106 glyph->face_id = it->face_id;
19107 glyph->u.img_id = img->id;
19108 glyph->slice = slice;
19109 glyph->font_type = FONT_TYPE_UNKNOWN;
19110 ++it->glyph_row->used[area];
19112 else
19113 IT_EXPAND_MATRIX_WIDTH (it, area);
19118 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19119 of the glyph, WIDTH and HEIGHT are the width and height of the
19120 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19122 static void
19123 append_stretch_glyph (it, object, width, height, ascent)
19124 struct it *it;
19125 Lisp_Object object;
19126 int width, height;
19127 int ascent;
19129 struct glyph *glyph;
19130 enum glyph_row_area area = it->area;
19132 xassert (ascent >= 0 && ascent <= height);
19134 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19135 if (glyph < it->glyph_row->glyphs[area + 1])
19137 glyph->charpos = CHARPOS (it->position);
19138 glyph->object = object;
19139 glyph->pixel_width = width;
19140 glyph->ascent = ascent;
19141 glyph->descent = height - ascent;
19142 glyph->voffset = it->voffset;
19143 glyph->type = STRETCH_GLYPH;
19144 glyph->multibyte_p = it->multibyte_p;
19145 glyph->left_box_line_p = it->start_of_box_run_p;
19146 glyph->right_box_line_p = it->end_of_box_run_p;
19147 glyph->overlaps_vertically_p = 0;
19148 glyph->padding_p = 0;
19149 glyph->glyph_not_available_p = 0;
19150 glyph->face_id = it->face_id;
19151 glyph->u.stretch.ascent = ascent;
19152 glyph->u.stretch.height = height;
19153 glyph->slice = null_glyph_slice;
19154 glyph->font_type = FONT_TYPE_UNKNOWN;
19155 ++it->glyph_row->used[area];
19157 else
19158 IT_EXPAND_MATRIX_WIDTH (it, area);
19162 /* Produce a stretch glyph for iterator IT. IT->object is the value
19163 of the glyph property displayed. The value must be a list
19164 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19165 being recognized:
19167 1. `:width WIDTH' specifies that the space should be WIDTH *
19168 canonical char width wide. WIDTH may be an integer or floating
19169 point number.
19171 2. `:relative-width FACTOR' specifies that the width of the stretch
19172 should be computed from the width of the first character having the
19173 `glyph' property, and should be FACTOR times that width.
19175 3. `:align-to HPOS' specifies that the space should be wide enough
19176 to reach HPOS, a value in canonical character units.
19178 Exactly one of the above pairs must be present.
19180 4. `:height HEIGHT' specifies that the height of the stretch produced
19181 should be HEIGHT, measured in canonical character units.
19183 5. `:relative-height FACTOR' specifies that the height of the
19184 stretch should be FACTOR times the height of the characters having
19185 the glyph property.
19187 Either none or exactly one of 4 or 5 must be present.
19189 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19190 of the stretch should be used for the ascent of the stretch.
19191 ASCENT must be in the range 0 <= ASCENT <= 100. */
19193 static void
19194 produce_stretch_glyph (it)
19195 struct it *it;
19197 /* (space :width WIDTH :height HEIGHT ...) */
19198 Lisp_Object prop, plist;
19199 int width = 0, height = 0, align_to = -1;
19200 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19201 int ascent = 0;
19202 double tem;
19203 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19204 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19206 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19208 /* List should start with `space'. */
19209 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19210 plist = XCDR (it->object);
19212 /* Compute the width of the stretch. */
19213 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19214 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19216 /* Absolute width `:width WIDTH' specified and valid. */
19217 zero_width_ok_p = 1;
19218 width = (int)tem;
19220 else if (prop = Fplist_get (plist, QCrelative_width),
19221 NUMVAL (prop) > 0)
19223 /* Relative width `:relative-width FACTOR' specified and valid.
19224 Compute the width of the characters having the `glyph'
19225 property. */
19226 struct it it2;
19227 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19229 it2 = *it;
19230 if (it->multibyte_p)
19232 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19233 - IT_BYTEPOS (*it));
19234 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19236 else
19237 it2.c = *p, it2.len = 1;
19239 it2.glyph_row = NULL;
19240 it2.what = IT_CHARACTER;
19241 x_produce_glyphs (&it2);
19242 width = NUMVAL (prop) * it2.pixel_width;
19244 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19245 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19247 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19248 align_to = (align_to < 0
19250 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19251 else if (align_to < 0)
19252 align_to = window_box_left_offset (it->w, TEXT_AREA);
19253 width = max (0, (int)tem + align_to - it->current_x);
19254 zero_width_ok_p = 1;
19256 else
19257 /* Nothing specified -> width defaults to canonical char width. */
19258 width = FRAME_COLUMN_WIDTH (it->f);
19260 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19261 width = 1;
19263 /* Compute height. */
19264 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19265 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19267 height = (int)tem;
19268 zero_height_ok_p = 1;
19270 else if (prop = Fplist_get (plist, QCrelative_height),
19271 NUMVAL (prop) > 0)
19272 height = FONT_HEIGHT (font) * NUMVAL (prop);
19273 else
19274 height = FONT_HEIGHT (font);
19276 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19277 height = 1;
19279 /* Compute percentage of height used for ascent. If
19280 `:ascent ASCENT' is present and valid, use that. Otherwise,
19281 derive the ascent from the font in use. */
19282 if (prop = Fplist_get (plist, QCascent),
19283 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19284 ascent = height * NUMVAL (prop) / 100.0;
19285 else if (!NILP (prop)
19286 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19287 ascent = min (max (0, (int)tem), height);
19288 else
19289 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19291 if (width > 0 && height > 0 && it->glyph_row)
19293 Lisp_Object object = it->stack[it->sp - 1].string;
19294 if (!STRINGP (object))
19295 object = it->w->buffer;
19296 append_stretch_glyph (it, object, width, height, ascent);
19299 it->pixel_width = width;
19300 it->ascent = it->phys_ascent = ascent;
19301 it->descent = it->phys_descent = height - it->ascent;
19302 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19304 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19306 if (face->box_line_width > 0)
19308 it->ascent += face->box_line_width;
19309 it->descent += face->box_line_width;
19312 if (it->start_of_box_run_p)
19313 it->pixel_width += abs (face->box_line_width);
19314 if (it->end_of_box_run_p)
19315 it->pixel_width += abs (face->box_line_width);
19318 take_vertical_position_into_account (it);
19321 /* Get line-height and line-spacing property at point.
19322 If line-height has format (HEIGHT TOTAL), return TOTAL
19323 in TOTAL_HEIGHT. */
19325 static Lisp_Object
19326 get_line_height_property (it, prop)
19327 struct it *it;
19328 Lisp_Object prop;
19330 Lisp_Object position;
19332 if (STRINGP (it->object))
19333 position = make_number (IT_STRING_CHARPOS (*it));
19334 else if (BUFFERP (it->object))
19335 position = make_number (IT_CHARPOS (*it));
19336 else
19337 return Qnil;
19339 return Fget_char_property (position, prop, it->object);
19342 /* Calculate line-height and line-spacing properties.
19343 An integer value specifies explicit pixel value.
19344 A float value specifies relative value to current face height.
19345 A cons (float . face-name) specifies relative value to
19346 height of specified face font.
19348 Returns height in pixels, or nil. */
19351 static Lisp_Object
19352 calc_line_height_property (it, val, font, boff, override)
19353 struct it *it;
19354 Lisp_Object val;
19355 XFontStruct *font;
19356 int boff, override;
19358 Lisp_Object face_name = Qnil;
19359 int ascent, descent, height;
19361 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19362 return val;
19364 if (CONSP (val))
19366 face_name = XCAR (val);
19367 val = XCDR (val);
19368 if (!NUMBERP (val))
19369 val = make_number (1);
19370 if (NILP (face_name))
19372 height = it->ascent + it->descent;
19373 goto scale;
19377 if (NILP (face_name))
19379 font = FRAME_FONT (it->f);
19380 boff = FRAME_BASELINE_OFFSET (it->f);
19382 else if (EQ (face_name, Qt))
19384 override = 0;
19386 else
19388 int face_id;
19389 struct face *face;
19390 struct font_info *font_info;
19392 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19393 if (face_id < 0)
19394 return make_number (-1);
19396 face = FACE_FROM_ID (it->f, face_id);
19397 font = face->font;
19398 if (font == NULL)
19399 return make_number (-1);
19401 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19402 boff = font_info->baseline_offset;
19403 if (font_info->vertical_centering)
19404 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19407 ascent = FONT_BASE (font) + boff;
19408 descent = FONT_DESCENT (font) - boff;
19410 if (override)
19412 it->override_ascent = ascent;
19413 it->override_descent = descent;
19414 it->override_boff = boff;
19417 height = ascent + descent;
19419 scale:
19420 if (FLOATP (val))
19421 height = (int)(XFLOAT_DATA (val) * height);
19422 else if (INTEGERP (val))
19423 height *= XINT (val);
19425 return make_number (height);
19429 /* RIF:
19430 Produce glyphs/get display metrics for the display element IT is
19431 loaded with. See the description of struct display_iterator in
19432 dispextern.h for an overview of struct display_iterator. */
19434 void
19435 x_produce_glyphs (it)
19436 struct it *it;
19438 int extra_line_spacing = it->extra_line_spacing;
19440 it->glyph_not_available_p = 0;
19442 if (it->what == IT_CHARACTER)
19444 XChar2b char2b;
19445 XFontStruct *font;
19446 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19447 XCharStruct *pcm;
19448 int font_not_found_p;
19449 struct font_info *font_info;
19450 int boff; /* baseline offset */
19451 /* We may change it->multibyte_p upon unibyte<->multibyte
19452 conversion. So, save the current value now and restore it
19453 later.
19455 Note: It seems that we don't have to record multibyte_p in
19456 struct glyph because the character code itself tells if or
19457 not the character is multibyte. Thus, in the future, we must
19458 consider eliminating the field `multibyte_p' in the struct
19459 glyph. */
19460 int saved_multibyte_p = it->multibyte_p;
19462 /* Maybe translate single-byte characters to multibyte, or the
19463 other way. */
19464 it->char_to_display = it->c;
19465 if (!ASCII_BYTE_P (it->c))
19467 if (unibyte_display_via_language_environment
19468 && SINGLE_BYTE_CHAR_P (it->c)
19469 && (it->c >= 0240
19470 || !NILP (Vnonascii_translation_table)))
19472 it->char_to_display = unibyte_char_to_multibyte (it->c);
19473 it->multibyte_p = 1;
19474 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19475 face = FACE_FROM_ID (it->f, it->face_id);
19477 else if (!SINGLE_BYTE_CHAR_P (it->c)
19478 && !it->multibyte_p)
19480 it->multibyte_p = 1;
19481 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19482 face = FACE_FROM_ID (it->f, it->face_id);
19486 /* Get font to use. Encode IT->char_to_display. */
19487 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19488 &char2b, it->multibyte_p, 0);
19489 font = face->font;
19491 /* When no suitable font found, use the default font. */
19492 font_not_found_p = font == NULL;
19493 if (font_not_found_p)
19495 font = FRAME_FONT (it->f);
19496 boff = FRAME_BASELINE_OFFSET (it->f);
19497 font_info = NULL;
19499 else
19501 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19502 boff = font_info->baseline_offset;
19503 if (font_info->vertical_centering)
19504 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19507 if (it->char_to_display >= ' '
19508 && (!it->multibyte_p || it->char_to_display < 128))
19510 /* Either unibyte or ASCII. */
19511 int stretched_p;
19513 it->nglyphs = 1;
19515 pcm = rif->per_char_metric (font, &char2b,
19516 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19518 if (it->override_ascent >= 0)
19520 it->ascent = it->override_ascent;
19521 it->descent = it->override_descent;
19522 boff = it->override_boff;
19524 else
19526 it->ascent = FONT_BASE (font) + boff;
19527 it->descent = FONT_DESCENT (font) - boff;
19530 if (pcm)
19532 it->phys_ascent = pcm->ascent + boff;
19533 it->phys_descent = pcm->descent - boff;
19534 it->pixel_width = pcm->width;
19536 else
19538 it->glyph_not_available_p = 1;
19539 it->phys_ascent = it->ascent;
19540 it->phys_descent = it->descent;
19541 it->pixel_width = FONT_WIDTH (font);
19544 if (it->constrain_row_ascent_descent_p)
19546 if (it->descent > it->max_descent)
19548 it->ascent += it->descent - it->max_descent;
19549 it->descent = it->max_descent;
19551 if (it->ascent > it->max_ascent)
19553 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19554 it->ascent = it->max_ascent;
19556 it->phys_ascent = min (it->phys_ascent, it->ascent);
19557 it->phys_descent = min (it->phys_descent, it->descent);
19558 extra_line_spacing = 0;
19561 /* If this is a space inside a region of text with
19562 `space-width' property, change its width. */
19563 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19564 if (stretched_p)
19565 it->pixel_width *= XFLOATINT (it->space_width);
19567 /* If face has a box, add the box thickness to the character
19568 height. If character has a box line to the left and/or
19569 right, add the box line width to the character's width. */
19570 if (face->box != FACE_NO_BOX)
19572 int thick = face->box_line_width;
19574 if (thick > 0)
19576 it->ascent += thick;
19577 it->descent += thick;
19579 else
19580 thick = -thick;
19582 if (it->start_of_box_run_p)
19583 it->pixel_width += thick;
19584 if (it->end_of_box_run_p)
19585 it->pixel_width += thick;
19588 /* If face has an overline, add the height of the overline
19589 (1 pixel) and a 1 pixel margin to the character height. */
19590 if (face->overline_p)
19591 it->ascent += 2;
19593 if (it->constrain_row_ascent_descent_p)
19595 if (it->ascent > it->max_ascent)
19596 it->ascent = it->max_ascent;
19597 if (it->descent > it->max_descent)
19598 it->descent = it->max_descent;
19601 take_vertical_position_into_account (it);
19603 /* If we have to actually produce glyphs, do it. */
19604 if (it->glyph_row)
19606 if (stretched_p)
19608 /* Translate a space with a `space-width' property
19609 into a stretch glyph. */
19610 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19611 / FONT_HEIGHT (font));
19612 append_stretch_glyph (it, it->object, it->pixel_width,
19613 it->ascent + it->descent, ascent);
19615 else
19616 append_glyph (it);
19618 /* If characters with lbearing or rbearing are displayed
19619 in this line, record that fact in a flag of the
19620 glyph row. This is used to optimize X output code. */
19621 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19622 it->glyph_row->contains_overlapping_glyphs_p = 1;
19625 else if (it->char_to_display == '\n')
19627 /* A newline has no width but we need the height of the line.
19628 But if previous part of the line set a height, don't
19629 increase that height */
19631 Lisp_Object height;
19632 Lisp_Object total_height = Qnil;
19634 it->override_ascent = -1;
19635 it->pixel_width = 0;
19636 it->nglyphs = 0;
19638 height = get_line_height_property(it, Qline_height);
19639 /* Split (line-height total-height) list */
19640 if (CONSP (height)
19641 && CONSP (XCDR (height))
19642 && NILP (XCDR (XCDR (height))))
19644 total_height = XCAR (XCDR (height));
19645 height = XCAR (height);
19647 height = calc_line_height_property(it, height, font, boff, 1);
19649 if (it->override_ascent >= 0)
19651 it->ascent = it->override_ascent;
19652 it->descent = it->override_descent;
19653 boff = it->override_boff;
19655 else
19657 it->ascent = FONT_BASE (font) + boff;
19658 it->descent = FONT_DESCENT (font) - boff;
19661 if (EQ (height, Qt))
19663 if (it->descent > it->max_descent)
19665 it->ascent += it->descent - it->max_descent;
19666 it->descent = it->max_descent;
19668 if (it->ascent > it->max_ascent)
19670 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19671 it->ascent = it->max_ascent;
19673 it->phys_ascent = min (it->phys_ascent, it->ascent);
19674 it->phys_descent = min (it->phys_descent, it->descent);
19675 it->constrain_row_ascent_descent_p = 1;
19676 extra_line_spacing = 0;
19678 else
19680 Lisp_Object spacing;
19682 it->phys_ascent = it->ascent;
19683 it->phys_descent = it->descent;
19685 if ((it->max_ascent > 0 || it->max_descent > 0)
19686 && face->box != FACE_NO_BOX
19687 && face->box_line_width > 0)
19689 it->ascent += face->box_line_width;
19690 it->descent += face->box_line_width;
19692 if (!NILP (height)
19693 && XINT (height) > it->ascent + it->descent)
19694 it->ascent = XINT (height) - it->descent;
19696 if (!NILP (total_height))
19697 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19698 else
19700 spacing = get_line_height_property(it, Qline_spacing);
19701 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19703 if (INTEGERP (spacing))
19705 extra_line_spacing = XINT (spacing);
19706 if (!NILP (total_height))
19707 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19711 else if (it->char_to_display == '\t')
19713 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19714 int x = it->current_x + it->continuation_lines_width;
19715 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19717 /* If the distance from the current position to the next tab
19718 stop is less than a space character width, use the
19719 tab stop after that. */
19720 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19721 next_tab_x += tab_width;
19723 it->pixel_width = next_tab_x - x;
19724 it->nglyphs = 1;
19725 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19726 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19728 if (it->glyph_row)
19730 append_stretch_glyph (it, it->object, it->pixel_width,
19731 it->ascent + it->descent, it->ascent);
19734 else
19736 /* A multi-byte character. Assume that the display width of the
19737 character is the width of the character multiplied by the
19738 width of the font. */
19740 /* If we found a font, this font should give us the right
19741 metrics. If we didn't find a font, use the frame's
19742 default font and calculate the width of the character
19743 from the charset width; this is what old redisplay code
19744 did. */
19746 pcm = rif->per_char_metric (font, &char2b,
19747 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19749 if (font_not_found_p || !pcm)
19751 int charset = CHAR_CHARSET (it->char_to_display);
19753 it->glyph_not_available_p = 1;
19754 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19755 * CHARSET_WIDTH (charset));
19756 it->phys_ascent = FONT_BASE (font) + boff;
19757 it->phys_descent = FONT_DESCENT (font) - boff;
19759 else
19761 it->pixel_width = pcm->width;
19762 it->phys_ascent = pcm->ascent + boff;
19763 it->phys_descent = pcm->descent - boff;
19764 if (it->glyph_row
19765 && (pcm->lbearing < 0
19766 || pcm->rbearing > pcm->width))
19767 it->glyph_row->contains_overlapping_glyphs_p = 1;
19769 it->nglyphs = 1;
19770 it->ascent = FONT_BASE (font) + boff;
19771 it->descent = FONT_DESCENT (font) - boff;
19772 if (face->box != FACE_NO_BOX)
19774 int thick = face->box_line_width;
19776 if (thick > 0)
19778 it->ascent += thick;
19779 it->descent += thick;
19781 else
19782 thick = - thick;
19784 if (it->start_of_box_run_p)
19785 it->pixel_width += thick;
19786 if (it->end_of_box_run_p)
19787 it->pixel_width += thick;
19790 /* If face has an overline, add the height of the overline
19791 (1 pixel) and a 1 pixel margin to the character height. */
19792 if (face->overline_p)
19793 it->ascent += 2;
19795 take_vertical_position_into_account (it);
19797 if (it->glyph_row)
19798 append_glyph (it);
19800 it->multibyte_p = saved_multibyte_p;
19802 else if (it->what == IT_COMPOSITION)
19804 /* Note: A composition is represented as one glyph in the
19805 glyph matrix. There are no padding glyphs. */
19806 XChar2b char2b;
19807 XFontStruct *font;
19808 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19809 XCharStruct *pcm;
19810 int font_not_found_p;
19811 struct font_info *font_info;
19812 int boff; /* baseline offset */
19813 struct composition *cmp = composition_table[it->cmp_id];
19815 /* Maybe translate single-byte characters to multibyte. */
19816 it->char_to_display = it->c;
19817 if (unibyte_display_via_language_environment
19818 && SINGLE_BYTE_CHAR_P (it->c)
19819 && (it->c >= 0240
19820 || (it->c >= 0200
19821 && !NILP (Vnonascii_translation_table))))
19823 it->char_to_display = unibyte_char_to_multibyte (it->c);
19826 /* Get face and font to use. Encode IT->char_to_display. */
19827 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19828 face = FACE_FROM_ID (it->f, it->face_id);
19829 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19830 &char2b, it->multibyte_p, 0);
19831 font = face->font;
19833 /* When no suitable font found, use the default font. */
19834 font_not_found_p = font == NULL;
19835 if (font_not_found_p)
19837 font = FRAME_FONT (it->f);
19838 boff = FRAME_BASELINE_OFFSET (it->f);
19839 font_info = NULL;
19841 else
19843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19844 boff = font_info->baseline_offset;
19845 if (font_info->vertical_centering)
19846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19849 /* There are no padding glyphs, so there is only one glyph to
19850 produce for the composition. Important is that pixel_width,
19851 ascent and descent are the values of what is drawn by
19852 draw_glyphs (i.e. the values of the overall glyphs composed). */
19853 it->nglyphs = 1;
19855 /* If we have not yet calculated pixel size data of glyphs of
19856 the composition for the current face font, calculate them
19857 now. Theoretically, we have to check all fonts for the
19858 glyphs, but that requires much time and memory space. So,
19859 here we check only the font of the first glyph. This leads
19860 to incorrect display very rarely, and C-l (recenter) can
19861 correct the display anyway. */
19862 if (cmp->font != (void *) font)
19864 /* Ascent and descent of the font of the first character of
19865 this composition (adjusted by baseline offset). Ascent
19866 and descent of overall glyphs should not be less than
19867 them respectively. */
19868 int font_ascent = FONT_BASE (font) + boff;
19869 int font_descent = FONT_DESCENT (font) - boff;
19870 /* Bounding box of the overall glyphs. */
19871 int leftmost, rightmost, lowest, highest;
19872 int i, width, ascent, descent;
19874 cmp->font = (void *) font;
19876 /* Initialize the bounding box. */
19877 if (font_info
19878 && (pcm = rif->per_char_metric (font, &char2b,
19879 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19881 width = pcm->width;
19882 ascent = pcm->ascent;
19883 descent = pcm->descent;
19885 else
19887 width = FONT_WIDTH (font);
19888 ascent = FONT_BASE (font);
19889 descent = FONT_DESCENT (font);
19892 rightmost = width;
19893 lowest = - descent + boff;
19894 highest = ascent + boff;
19895 leftmost = 0;
19897 if (font_info
19898 && font_info->default_ascent
19899 && CHAR_TABLE_P (Vuse_default_ascent)
19900 && !NILP (Faref (Vuse_default_ascent,
19901 make_number (it->char_to_display))))
19902 highest = font_info->default_ascent + boff;
19904 /* Draw the first glyph at the normal position. It may be
19905 shifted to right later if some other glyphs are drawn at
19906 the left. */
19907 cmp->offsets[0] = 0;
19908 cmp->offsets[1] = boff;
19910 /* Set cmp->offsets for the remaining glyphs. */
19911 for (i = 1; i < cmp->glyph_len; i++)
19913 int left, right, btm, top;
19914 int ch = COMPOSITION_GLYPH (cmp, i);
19915 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19917 face = FACE_FROM_ID (it->f, face_id);
19918 get_char_face_and_encoding (it->f, ch, face->id,
19919 &char2b, it->multibyte_p, 0);
19920 font = face->font;
19921 if (font == NULL)
19923 font = FRAME_FONT (it->f);
19924 boff = FRAME_BASELINE_OFFSET (it->f);
19925 font_info = NULL;
19927 else
19929 font_info
19930 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19931 boff = font_info->baseline_offset;
19932 if (font_info->vertical_centering)
19933 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19936 if (font_info
19937 && (pcm = rif->per_char_metric (font, &char2b,
19938 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19940 width = pcm->width;
19941 ascent = pcm->ascent;
19942 descent = pcm->descent;
19944 else
19946 width = FONT_WIDTH (font);
19947 ascent = 1;
19948 descent = 0;
19951 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19953 /* Relative composition with or without
19954 alternate chars. */
19955 left = (leftmost + rightmost - width) / 2;
19956 btm = - descent + boff;
19957 if (font_info && font_info->relative_compose
19958 && (! CHAR_TABLE_P (Vignore_relative_composition)
19959 || NILP (Faref (Vignore_relative_composition,
19960 make_number (ch)))))
19963 if (- descent >= font_info->relative_compose)
19964 /* One extra pixel between two glyphs. */
19965 btm = highest + 1;
19966 else if (ascent <= 0)
19967 /* One extra pixel between two glyphs. */
19968 btm = lowest - 1 - ascent - descent;
19971 else
19973 /* A composition rule is specified by an integer
19974 value that encodes global and new reference
19975 points (GREF and NREF). GREF and NREF are
19976 specified by numbers as below:
19978 0---1---2 -- ascent
19982 9--10--11 -- center
19984 ---3---4---5--- baseline
19986 6---7---8 -- descent
19988 int rule = COMPOSITION_RULE (cmp, i);
19989 int gref, nref, grefx, grefy, nrefx, nrefy;
19991 COMPOSITION_DECODE_RULE (rule, gref, nref);
19992 grefx = gref % 3, nrefx = nref % 3;
19993 grefy = gref / 3, nrefy = nref / 3;
19995 left = (leftmost
19996 + grefx * (rightmost - leftmost) / 2
19997 - nrefx * width / 2);
19998 btm = ((grefy == 0 ? highest
19999 : grefy == 1 ? 0
20000 : grefy == 2 ? lowest
20001 : (highest + lowest) / 2)
20002 - (nrefy == 0 ? ascent + descent
20003 : nrefy == 1 ? descent - boff
20004 : nrefy == 2 ? 0
20005 : (ascent + descent) / 2));
20008 cmp->offsets[i * 2] = left;
20009 cmp->offsets[i * 2 + 1] = btm + descent;
20011 /* Update the bounding box of the overall glyphs. */
20012 right = left + width;
20013 top = btm + descent + ascent;
20014 if (left < leftmost)
20015 leftmost = left;
20016 if (right > rightmost)
20017 rightmost = right;
20018 if (top > highest)
20019 highest = top;
20020 if (btm < lowest)
20021 lowest = btm;
20024 /* If there are glyphs whose x-offsets are negative,
20025 shift all glyphs to the right and make all x-offsets
20026 non-negative. */
20027 if (leftmost < 0)
20029 for (i = 0; i < cmp->glyph_len; i++)
20030 cmp->offsets[i * 2] -= leftmost;
20031 rightmost -= leftmost;
20034 cmp->pixel_width = rightmost;
20035 cmp->ascent = highest;
20036 cmp->descent = - lowest;
20037 if (cmp->ascent < font_ascent)
20038 cmp->ascent = font_ascent;
20039 if (cmp->descent < font_descent)
20040 cmp->descent = font_descent;
20043 it->pixel_width = cmp->pixel_width;
20044 it->ascent = it->phys_ascent = cmp->ascent;
20045 it->descent = it->phys_descent = cmp->descent;
20047 if (face->box != FACE_NO_BOX)
20049 int thick = face->box_line_width;
20051 if (thick > 0)
20053 it->ascent += thick;
20054 it->descent += thick;
20056 else
20057 thick = - thick;
20059 if (it->start_of_box_run_p)
20060 it->pixel_width += thick;
20061 if (it->end_of_box_run_p)
20062 it->pixel_width += thick;
20065 /* If face has an overline, add the height of the overline
20066 (1 pixel) and a 1 pixel margin to the character height. */
20067 if (face->overline_p)
20068 it->ascent += 2;
20070 take_vertical_position_into_account (it);
20072 if (it->glyph_row)
20073 append_composite_glyph (it);
20075 else if (it->what == IT_IMAGE)
20076 produce_image_glyph (it);
20077 else if (it->what == IT_STRETCH)
20078 produce_stretch_glyph (it);
20080 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20081 because this isn't true for images with `:ascent 100'. */
20082 xassert (it->ascent >= 0 && it->descent >= 0);
20083 if (it->area == TEXT_AREA)
20084 it->current_x += it->pixel_width;
20086 if (extra_line_spacing > 0)
20088 it->descent += extra_line_spacing;
20089 if (extra_line_spacing > it->max_extra_line_spacing)
20090 it->max_extra_line_spacing = extra_line_spacing;
20093 it->max_ascent = max (it->max_ascent, it->ascent);
20094 it->max_descent = max (it->max_descent, it->descent);
20095 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20096 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20099 /* EXPORT for RIF:
20100 Output LEN glyphs starting at START at the nominal cursor position.
20101 Advance the nominal cursor over the text. The global variable
20102 updated_window contains the window being updated, updated_row is
20103 the glyph row being updated, and updated_area is the area of that
20104 row being updated. */
20106 void
20107 x_write_glyphs (start, len)
20108 struct glyph *start;
20109 int len;
20111 int x, hpos;
20113 xassert (updated_window && updated_row);
20114 BLOCK_INPUT;
20116 /* Write glyphs. */
20118 hpos = start - updated_row->glyphs[updated_area];
20119 x = draw_glyphs (updated_window, output_cursor.x,
20120 updated_row, updated_area,
20121 hpos, hpos + len,
20122 DRAW_NORMAL_TEXT, 0);
20124 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20125 if (updated_area == TEXT_AREA
20126 && updated_window->phys_cursor_on_p
20127 && updated_window->phys_cursor.vpos == output_cursor.vpos
20128 && updated_window->phys_cursor.hpos >= hpos
20129 && updated_window->phys_cursor.hpos < hpos + len)
20130 updated_window->phys_cursor_on_p = 0;
20132 UNBLOCK_INPUT;
20134 /* Advance the output cursor. */
20135 output_cursor.hpos += len;
20136 output_cursor.x = x;
20140 /* EXPORT for RIF:
20141 Insert LEN glyphs from START at the nominal cursor position. */
20143 void
20144 x_insert_glyphs (start, len)
20145 struct glyph *start;
20146 int len;
20148 struct frame *f;
20149 struct window *w;
20150 int line_height, shift_by_width, shifted_region_width;
20151 struct glyph_row *row;
20152 struct glyph *glyph;
20153 int frame_x, frame_y, hpos;
20155 xassert (updated_window && updated_row);
20156 BLOCK_INPUT;
20157 w = updated_window;
20158 f = XFRAME (WINDOW_FRAME (w));
20160 /* Get the height of the line we are in. */
20161 row = updated_row;
20162 line_height = row->height;
20164 /* Get the width of the glyphs to insert. */
20165 shift_by_width = 0;
20166 for (glyph = start; glyph < start + len; ++glyph)
20167 shift_by_width += glyph->pixel_width;
20169 /* Get the width of the region to shift right. */
20170 shifted_region_width = (window_box_width (w, updated_area)
20171 - output_cursor.x
20172 - shift_by_width);
20174 /* Shift right. */
20175 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20176 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20178 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20179 line_height, shift_by_width);
20181 /* Write the glyphs. */
20182 hpos = start - row->glyphs[updated_area];
20183 draw_glyphs (w, output_cursor.x, row, updated_area,
20184 hpos, hpos + len,
20185 DRAW_NORMAL_TEXT, 0);
20187 /* Advance the output cursor. */
20188 output_cursor.hpos += len;
20189 output_cursor.x += shift_by_width;
20190 UNBLOCK_INPUT;
20194 /* EXPORT for RIF:
20195 Erase the current text line from the nominal cursor position
20196 (inclusive) to pixel column TO_X (exclusive). The idea is that
20197 everything from TO_X onward is already erased.
20199 TO_X is a pixel position relative to updated_area of
20200 updated_window. TO_X == -1 means clear to the end of this area. */
20202 void
20203 x_clear_end_of_line (to_x)
20204 int to_x;
20206 struct frame *f;
20207 struct window *w = updated_window;
20208 int max_x, min_y, max_y;
20209 int from_x, from_y, to_y;
20211 xassert (updated_window && updated_row);
20212 f = XFRAME (w->frame);
20214 if (updated_row->full_width_p)
20215 max_x = WINDOW_TOTAL_WIDTH (w);
20216 else
20217 max_x = window_box_width (w, updated_area);
20218 max_y = window_text_bottom_y (w);
20220 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20221 of window. For TO_X > 0, truncate to end of drawing area. */
20222 if (to_x == 0)
20223 return;
20224 else if (to_x < 0)
20225 to_x = max_x;
20226 else
20227 to_x = min (to_x, max_x);
20229 to_y = min (max_y, output_cursor.y + updated_row->height);
20231 /* Notice if the cursor will be cleared by this operation. */
20232 if (!updated_row->full_width_p)
20233 notice_overwritten_cursor (w, updated_area,
20234 output_cursor.x, -1,
20235 updated_row->y,
20236 MATRIX_ROW_BOTTOM_Y (updated_row));
20238 from_x = output_cursor.x;
20240 /* Translate to frame coordinates. */
20241 if (updated_row->full_width_p)
20243 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20244 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20246 else
20248 int area_left = window_box_left (w, updated_area);
20249 from_x += area_left;
20250 to_x += area_left;
20253 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20254 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20255 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20257 /* Prevent inadvertently clearing to end of the X window. */
20258 if (to_x > from_x && to_y > from_y)
20260 BLOCK_INPUT;
20261 rif->clear_frame_area (f, from_x, from_y,
20262 to_x - from_x, to_y - from_y);
20263 UNBLOCK_INPUT;
20267 #endif /* HAVE_WINDOW_SYSTEM */
20271 /***********************************************************************
20272 Cursor types
20273 ***********************************************************************/
20275 /* Value is the internal representation of the specified cursor type
20276 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20277 of the bar cursor. */
20279 static enum text_cursor_kinds
20280 get_specified_cursor_type (arg, width)
20281 Lisp_Object arg;
20282 int *width;
20284 enum text_cursor_kinds type;
20286 if (NILP (arg))
20287 return NO_CURSOR;
20289 if (EQ (arg, Qbox))
20290 return FILLED_BOX_CURSOR;
20292 if (EQ (arg, Qhollow))
20293 return HOLLOW_BOX_CURSOR;
20295 if (EQ (arg, Qbar))
20297 *width = 2;
20298 return BAR_CURSOR;
20301 if (CONSP (arg)
20302 && EQ (XCAR (arg), Qbar)
20303 && INTEGERP (XCDR (arg))
20304 && XINT (XCDR (arg)) >= 0)
20306 *width = XINT (XCDR (arg));
20307 return BAR_CURSOR;
20310 if (EQ (arg, Qhbar))
20312 *width = 2;
20313 return HBAR_CURSOR;
20316 if (CONSP (arg)
20317 && EQ (XCAR (arg), Qhbar)
20318 && INTEGERP (XCDR (arg))
20319 && XINT (XCDR (arg)) >= 0)
20321 *width = XINT (XCDR (arg));
20322 return HBAR_CURSOR;
20325 /* Treat anything unknown as "hollow box cursor".
20326 It was bad to signal an error; people have trouble fixing
20327 .Xdefaults with Emacs, when it has something bad in it. */
20328 type = HOLLOW_BOX_CURSOR;
20330 return type;
20333 /* Set the default cursor types for specified frame. */
20334 void
20335 set_frame_cursor_types (f, arg)
20336 struct frame *f;
20337 Lisp_Object arg;
20339 int width;
20340 Lisp_Object tem;
20342 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20343 FRAME_CURSOR_WIDTH (f) = width;
20345 /* By default, set up the blink-off state depending on the on-state. */
20347 tem = Fassoc (arg, Vblink_cursor_alist);
20348 if (!NILP (tem))
20350 FRAME_BLINK_OFF_CURSOR (f)
20351 = get_specified_cursor_type (XCDR (tem), &width);
20352 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20354 else
20355 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20359 /* Return the cursor we want to be displayed in window W. Return
20360 width of bar/hbar cursor through WIDTH arg. Return with
20361 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20362 (i.e. if the `system caret' should track this cursor).
20364 In a mini-buffer window, we want the cursor only to appear if we
20365 are reading input from this window. For the selected window, we
20366 want the cursor type given by the frame parameter or buffer local
20367 setting of cursor-type. If explicitly marked off, draw no cursor.
20368 In all other cases, we want a hollow box cursor. */
20370 static enum text_cursor_kinds
20371 get_window_cursor_type (w, glyph, width, active_cursor)
20372 struct window *w;
20373 struct glyph *glyph;
20374 int *width;
20375 int *active_cursor;
20377 struct frame *f = XFRAME (w->frame);
20378 struct buffer *b = XBUFFER (w->buffer);
20379 int cursor_type = DEFAULT_CURSOR;
20380 Lisp_Object alt_cursor;
20381 int non_selected = 0;
20383 *active_cursor = 1;
20385 /* Echo area */
20386 if (cursor_in_echo_area
20387 && FRAME_HAS_MINIBUF_P (f)
20388 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20390 if (w == XWINDOW (echo_area_window))
20392 *width = FRAME_CURSOR_WIDTH (f);
20393 return FRAME_DESIRED_CURSOR (f);
20396 *active_cursor = 0;
20397 non_selected = 1;
20400 /* Nonselected window or nonselected frame. */
20401 else if (w != XWINDOW (f->selected_window)
20402 #ifdef HAVE_WINDOW_SYSTEM
20403 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20404 #endif
20407 *active_cursor = 0;
20409 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20410 return NO_CURSOR;
20412 non_selected = 1;
20415 /* Never display a cursor in a window in which cursor-type is nil. */
20416 if (NILP (b->cursor_type))
20417 return NO_CURSOR;
20419 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20420 if (non_selected)
20422 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20423 return get_specified_cursor_type (alt_cursor, width);
20426 /* Get the normal cursor type for this window. */
20427 if (EQ (b->cursor_type, Qt))
20429 cursor_type = FRAME_DESIRED_CURSOR (f);
20430 *width = FRAME_CURSOR_WIDTH (f);
20432 else
20433 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20435 /* Use normal cursor if not blinked off. */
20436 if (!w->cursor_off_p)
20438 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20439 if (cursor_type == FILLED_BOX_CURSOR)
20440 cursor_type = HOLLOW_BOX_CURSOR;
20442 return cursor_type;
20445 /* Cursor is blinked off, so determine how to "toggle" it. */
20447 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20448 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20449 return get_specified_cursor_type (XCDR (alt_cursor), width);
20451 /* Then see if frame has specified a specific blink off cursor type. */
20452 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20454 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20455 return FRAME_BLINK_OFF_CURSOR (f);
20458 #if 0
20459 /* Some people liked having a permanently visible blinking cursor,
20460 while others had very strong opinions against it. So it was
20461 decided to remove it. KFS 2003-09-03 */
20463 /* Finally perform built-in cursor blinking:
20464 filled box <-> hollow box
20465 wide [h]bar <-> narrow [h]bar
20466 narrow [h]bar <-> no cursor
20467 other type <-> no cursor */
20469 if (cursor_type == FILLED_BOX_CURSOR)
20470 return HOLLOW_BOX_CURSOR;
20472 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20474 *width = 1;
20475 return cursor_type;
20477 #endif
20479 return NO_CURSOR;
20483 #ifdef HAVE_WINDOW_SYSTEM
20485 /* Notice when the text cursor of window W has been completely
20486 overwritten by a drawing operation that outputs glyphs in AREA
20487 starting at X0 and ending at X1 in the line starting at Y0 and
20488 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20489 the rest of the line after X0 has been written. Y coordinates
20490 are window-relative. */
20492 static void
20493 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20494 struct window *w;
20495 enum glyph_row_area area;
20496 int x0, y0, x1, y1;
20498 int cx0, cx1, cy0, cy1;
20499 struct glyph_row *row;
20501 if (!w->phys_cursor_on_p)
20502 return;
20503 if (area != TEXT_AREA)
20504 return;
20506 if (w->phys_cursor.vpos < 0
20507 || w->phys_cursor.vpos >= w->current_matrix->nrows
20508 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20509 !(row->enabled_p && row->displays_text_p)))
20510 return;
20512 if (row->cursor_in_fringe_p)
20514 row->cursor_in_fringe_p = 0;
20515 draw_fringe_bitmap (w, row, 0);
20516 w->phys_cursor_on_p = 0;
20517 return;
20520 cx0 = w->phys_cursor.x;
20521 cx1 = cx0 + w->phys_cursor_width;
20522 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20523 return;
20525 /* The cursor image will be completely removed from the
20526 screen if the output area intersects the cursor area in
20527 y-direction. When we draw in [y0 y1[, and some part of
20528 the cursor is at y < y0, that part must have been drawn
20529 before. When scrolling, the cursor is erased before
20530 actually scrolling, so we don't come here. When not
20531 scrolling, the rows above the old cursor row must have
20532 changed, and in this case these rows must have written
20533 over the cursor image.
20535 Likewise if part of the cursor is below y1, with the
20536 exception of the cursor being in the first blank row at
20537 the buffer and window end because update_text_area
20538 doesn't draw that row. (Except when it does, but
20539 that's handled in update_text_area.) */
20541 cy0 = w->phys_cursor.y;
20542 cy1 = cy0 + w->phys_cursor_height;
20543 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20544 return;
20546 w->phys_cursor_on_p = 0;
20549 #endif /* HAVE_WINDOW_SYSTEM */
20552 /************************************************************************
20553 Mouse Face
20554 ************************************************************************/
20556 #ifdef HAVE_WINDOW_SYSTEM
20558 /* EXPORT for RIF:
20559 Fix the display of area AREA of overlapping row ROW in window W. */
20561 void
20562 x_fix_overlapping_area (w, row, area)
20563 struct window *w;
20564 struct glyph_row *row;
20565 enum glyph_row_area area;
20567 int i, x;
20569 BLOCK_INPUT;
20571 x = 0;
20572 for (i = 0; i < row->used[area];)
20574 if (row->glyphs[area][i].overlaps_vertically_p)
20576 int start = i, start_x = x;
20580 x += row->glyphs[area][i].pixel_width;
20581 ++i;
20583 while (i < row->used[area]
20584 && row->glyphs[area][i].overlaps_vertically_p);
20586 draw_glyphs (w, start_x, row, area,
20587 start, i,
20588 DRAW_NORMAL_TEXT, 1);
20590 else
20592 x += row->glyphs[area][i].pixel_width;
20593 ++i;
20597 UNBLOCK_INPUT;
20601 /* EXPORT:
20602 Draw the cursor glyph of window W in glyph row ROW. See the
20603 comment of draw_glyphs for the meaning of HL. */
20605 void
20606 draw_phys_cursor_glyph (w, row, hl)
20607 struct window *w;
20608 struct glyph_row *row;
20609 enum draw_glyphs_face hl;
20611 /* If cursor hpos is out of bounds, don't draw garbage. This can
20612 happen in mini-buffer windows when switching between echo area
20613 glyphs and mini-buffer. */
20614 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20616 int on_p = w->phys_cursor_on_p;
20617 int x1;
20618 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20619 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20620 hl, 0);
20621 w->phys_cursor_on_p = on_p;
20623 if (hl == DRAW_CURSOR)
20624 w->phys_cursor_width = x1 - w->phys_cursor.x;
20625 /* When we erase the cursor, and ROW is overlapped by other
20626 rows, make sure that these overlapping parts of other rows
20627 are redrawn. */
20628 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20630 if (row > w->current_matrix->rows
20631 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20632 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20634 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20635 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20636 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20642 /* EXPORT:
20643 Erase the image of a cursor of window W from the screen. */
20645 void
20646 erase_phys_cursor (w)
20647 struct window *w;
20649 struct frame *f = XFRAME (w->frame);
20650 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20651 int hpos = w->phys_cursor.hpos;
20652 int vpos = w->phys_cursor.vpos;
20653 int mouse_face_here_p = 0;
20654 struct glyph_matrix *active_glyphs = w->current_matrix;
20655 struct glyph_row *cursor_row;
20656 struct glyph *cursor_glyph;
20657 enum draw_glyphs_face hl;
20659 /* No cursor displayed or row invalidated => nothing to do on the
20660 screen. */
20661 if (w->phys_cursor_type == NO_CURSOR)
20662 goto mark_cursor_off;
20664 /* VPOS >= active_glyphs->nrows means that window has been resized.
20665 Don't bother to erase the cursor. */
20666 if (vpos >= active_glyphs->nrows)
20667 goto mark_cursor_off;
20669 /* If row containing cursor is marked invalid, there is nothing we
20670 can do. */
20671 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20672 if (!cursor_row->enabled_p)
20673 goto mark_cursor_off;
20675 /* If line spacing is > 0, old cursor may only be partially visible in
20676 window after split-window. So adjust visible height. */
20677 cursor_row->visible_height = min (cursor_row->visible_height,
20678 window_text_bottom_y (w) - cursor_row->y);
20680 /* If row is completely invisible, don't attempt to delete a cursor which
20681 isn't there. This can happen if cursor is at top of a window, and
20682 we switch to a buffer with a header line in that window. */
20683 if (cursor_row->visible_height <= 0)
20684 goto mark_cursor_off;
20686 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20687 if (cursor_row->cursor_in_fringe_p)
20689 cursor_row->cursor_in_fringe_p = 0;
20690 draw_fringe_bitmap (w, cursor_row, 0);
20691 goto mark_cursor_off;
20694 /* This can happen when the new row is shorter than the old one.
20695 In this case, either draw_glyphs or clear_end_of_line
20696 should have cleared the cursor. Note that we wouldn't be
20697 able to erase the cursor in this case because we don't have a
20698 cursor glyph at hand. */
20699 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20700 goto mark_cursor_off;
20702 /* If the cursor is in the mouse face area, redisplay that when
20703 we clear the cursor. */
20704 if (! NILP (dpyinfo->mouse_face_window)
20705 && w == XWINDOW (dpyinfo->mouse_face_window)
20706 && (vpos > dpyinfo->mouse_face_beg_row
20707 || (vpos == dpyinfo->mouse_face_beg_row
20708 && hpos >= dpyinfo->mouse_face_beg_col))
20709 && (vpos < dpyinfo->mouse_face_end_row
20710 || (vpos == dpyinfo->mouse_face_end_row
20711 && hpos < dpyinfo->mouse_face_end_col))
20712 /* Don't redraw the cursor's spot in mouse face if it is at the
20713 end of a line (on a newline). The cursor appears there, but
20714 mouse highlighting does not. */
20715 && cursor_row->used[TEXT_AREA] > hpos)
20716 mouse_face_here_p = 1;
20718 /* Maybe clear the display under the cursor. */
20719 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20721 int x, y;
20722 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20723 int width;
20725 cursor_glyph = get_phys_cursor_glyph (w);
20726 if (cursor_glyph == NULL)
20727 goto mark_cursor_off;
20729 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20730 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20731 width = min (cursor_glyph->pixel_width,
20732 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20734 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20737 /* Erase the cursor by redrawing the character underneath it. */
20738 if (mouse_face_here_p)
20739 hl = DRAW_MOUSE_FACE;
20740 else
20741 hl = DRAW_NORMAL_TEXT;
20742 draw_phys_cursor_glyph (w, cursor_row, hl);
20744 mark_cursor_off:
20745 w->phys_cursor_on_p = 0;
20746 w->phys_cursor_type = NO_CURSOR;
20750 /* EXPORT:
20751 Display or clear cursor of window W. If ON is zero, clear the
20752 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20753 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20755 void
20756 display_and_set_cursor (w, on, hpos, vpos, x, y)
20757 struct window *w;
20758 int on, hpos, vpos, x, y;
20760 struct frame *f = XFRAME (w->frame);
20761 int new_cursor_type;
20762 int new_cursor_width;
20763 int active_cursor;
20764 struct glyph_row *glyph_row;
20765 struct glyph *glyph;
20767 /* This is pointless on invisible frames, and dangerous on garbaged
20768 windows and frames; in the latter case, the frame or window may
20769 be in the midst of changing its size, and x and y may be off the
20770 window. */
20771 if (! FRAME_VISIBLE_P (f)
20772 || FRAME_GARBAGED_P (f)
20773 || vpos >= w->current_matrix->nrows
20774 || hpos >= w->current_matrix->matrix_w)
20775 return;
20777 /* If cursor is off and we want it off, return quickly. */
20778 if (!on && !w->phys_cursor_on_p)
20779 return;
20781 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20782 /* If cursor row is not enabled, we don't really know where to
20783 display the cursor. */
20784 if (!glyph_row->enabled_p)
20786 w->phys_cursor_on_p = 0;
20787 return;
20790 glyph = NULL;
20791 if (!glyph_row->exact_window_width_line_p
20792 || hpos < glyph_row->used[TEXT_AREA])
20793 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20795 xassert (interrupt_input_blocked);
20797 /* Set new_cursor_type to the cursor we want to be displayed. */
20798 new_cursor_type = get_window_cursor_type (w, glyph,
20799 &new_cursor_width, &active_cursor);
20801 /* If cursor is currently being shown and we don't want it to be or
20802 it is in the wrong place, or the cursor type is not what we want,
20803 erase it. */
20804 if (w->phys_cursor_on_p
20805 && (!on
20806 || w->phys_cursor.x != x
20807 || w->phys_cursor.y != y
20808 || new_cursor_type != w->phys_cursor_type
20809 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20810 && new_cursor_width != w->phys_cursor_width)))
20811 erase_phys_cursor (w);
20813 /* Don't check phys_cursor_on_p here because that flag is only set
20814 to zero in some cases where we know that the cursor has been
20815 completely erased, to avoid the extra work of erasing the cursor
20816 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20817 still not be visible, or it has only been partly erased. */
20818 if (on)
20820 w->phys_cursor_ascent = glyph_row->ascent;
20821 w->phys_cursor_height = glyph_row->height;
20823 /* Set phys_cursor_.* before x_draw_.* is called because some
20824 of them may need the information. */
20825 w->phys_cursor.x = x;
20826 w->phys_cursor.y = glyph_row->y;
20827 w->phys_cursor.hpos = hpos;
20828 w->phys_cursor.vpos = vpos;
20831 rif->draw_window_cursor (w, glyph_row, x, y,
20832 new_cursor_type, new_cursor_width,
20833 on, active_cursor);
20837 /* Switch the display of W's cursor on or off, according to the value
20838 of ON. */
20840 static void
20841 update_window_cursor (w, on)
20842 struct window *w;
20843 int on;
20845 /* Don't update cursor in windows whose frame is in the process
20846 of being deleted. */
20847 if (w->current_matrix)
20849 BLOCK_INPUT;
20850 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20851 w->phys_cursor.x, w->phys_cursor.y);
20852 UNBLOCK_INPUT;
20857 /* Call update_window_cursor with parameter ON_P on all leaf windows
20858 in the window tree rooted at W. */
20860 static void
20861 update_cursor_in_window_tree (w, on_p)
20862 struct window *w;
20863 int on_p;
20865 while (w)
20867 if (!NILP (w->hchild))
20868 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20869 else if (!NILP (w->vchild))
20870 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20871 else
20872 update_window_cursor (w, on_p);
20874 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20879 /* EXPORT:
20880 Display the cursor on window W, or clear it, according to ON_P.
20881 Don't change the cursor's position. */
20883 void
20884 x_update_cursor (f, on_p)
20885 struct frame *f;
20886 int on_p;
20888 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20892 /* EXPORT:
20893 Clear the cursor of window W to background color, and mark the
20894 cursor as not shown. This is used when the text where the cursor
20895 is is about to be rewritten. */
20897 void
20898 x_clear_cursor (w)
20899 struct window *w;
20901 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20902 update_window_cursor (w, 0);
20906 /* EXPORT:
20907 Display the active region described by mouse_face_* according to DRAW. */
20909 void
20910 show_mouse_face (dpyinfo, draw)
20911 Display_Info *dpyinfo;
20912 enum draw_glyphs_face draw;
20914 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20915 struct frame *f = XFRAME (WINDOW_FRAME (w));
20917 if (/* If window is in the process of being destroyed, don't bother
20918 to do anything. */
20919 w->current_matrix != NULL
20920 /* Don't update mouse highlight if hidden */
20921 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20922 /* Recognize when we are called to operate on rows that don't exist
20923 anymore. This can happen when a window is split. */
20924 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20926 int phys_cursor_on_p = w->phys_cursor_on_p;
20927 struct glyph_row *row, *first, *last;
20929 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20930 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20932 for (row = first; row <= last && row->enabled_p; ++row)
20934 int start_hpos, end_hpos, start_x;
20936 /* For all but the first row, the highlight starts at column 0. */
20937 if (row == first)
20939 start_hpos = dpyinfo->mouse_face_beg_col;
20940 start_x = dpyinfo->mouse_face_beg_x;
20942 else
20944 start_hpos = 0;
20945 start_x = 0;
20948 if (row == last)
20949 end_hpos = dpyinfo->mouse_face_end_col;
20950 else
20951 end_hpos = row->used[TEXT_AREA];
20953 if (end_hpos > start_hpos)
20955 draw_glyphs (w, start_x, row, TEXT_AREA,
20956 start_hpos, end_hpos,
20957 draw, 0);
20959 row->mouse_face_p
20960 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20964 /* When we've written over the cursor, arrange for it to
20965 be displayed again. */
20966 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20968 BLOCK_INPUT;
20969 display_and_set_cursor (w, 1,
20970 w->phys_cursor.hpos, w->phys_cursor.vpos,
20971 w->phys_cursor.x, w->phys_cursor.y);
20972 UNBLOCK_INPUT;
20976 /* Change the mouse cursor. */
20977 if (draw == DRAW_NORMAL_TEXT)
20978 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20979 else if (draw == DRAW_MOUSE_FACE)
20980 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20981 else
20982 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20985 /* EXPORT:
20986 Clear out the mouse-highlighted active region.
20987 Redraw it un-highlighted first. Value is non-zero if mouse
20988 face was actually drawn unhighlighted. */
20991 clear_mouse_face (dpyinfo)
20992 Display_Info *dpyinfo;
20994 int cleared = 0;
20996 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20998 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20999 cleared = 1;
21002 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21003 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21004 dpyinfo->mouse_face_window = Qnil;
21005 dpyinfo->mouse_face_overlay = Qnil;
21006 return cleared;
21010 /* EXPORT:
21011 Non-zero if physical cursor of window W is within mouse face. */
21014 cursor_in_mouse_face_p (w)
21015 struct window *w;
21017 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21018 int in_mouse_face = 0;
21020 if (WINDOWP (dpyinfo->mouse_face_window)
21021 && XWINDOW (dpyinfo->mouse_face_window) == w)
21023 int hpos = w->phys_cursor.hpos;
21024 int vpos = w->phys_cursor.vpos;
21026 if (vpos >= dpyinfo->mouse_face_beg_row
21027 && vpos <= dpyinfo->mouse_face_end_row
21028 && (vpos > dpyinfo->mouse_face_beg_row
21029 || hpos >= dpyinfo->mouse_face_beg_col)
21030 && (vpos < dpyinfo->mouse_face_end_row
21031 || hpos < dpyinfo->mouse_face_end_col
21032 || dpyinfo->mouse_face_past_end))
21033 in_mouse_face = 1;
21036 return in_mouse_face;
21042 /* Find the glyph matrix position of buffer position CHARPOS in window
21043 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21044 current glyphs must be up to date. If CHARPOS is above window
21045 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21046 of last line in W. In the row containing CHARPOS, stop before glyphs
21047 having STOP as object. */
21049 #if 1 /* This is a version of fast_find_position that's more correct
21050 in the presence of hscrolling, for example. I didn't install
21051 it right away because the problem fixed is minor, it failed
21052 in 20.x as well, and I think it's too risky to install
21053 so near the release of 21.1. 2001-09-25 gerd. */
21055 static int
21056 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21057 struct window *w;
21058 int charpos;
21059 int *hpos, *vpos, *x, *y;
21060 Lisp_Object stop;
21062 struct glyph_row *row, *first;
21063 struct glyph *glyph, *end;
21064 int past_end = 0;
21066 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21067 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21069 *x = first->x;
21070 *y = first->y;
21071 *hpos = 0;
21072 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21073 return 1;
21076 row = row_containing_pos (w, charpos, first, NULL, 0);
21077 if (row == NULL)
21079 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21080 past_end = 1;
21083 /* If whole rows or last part of a row came from a display overlay,
21084 row_containing_pos will skip over such rows because their end pos
21085 equals the start pos of the overlay or interval.
21087 Move back if we have a STOP object and previous row's
21088 end glyph came from STOP. */
21089 if (!NILP (stop))
21091 struct glyph_row *prev;
21092 while ((prev = row - 1, prev >= first)
21093 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21094 && prev->used[TEXT_AREA] > 0)
21096 struct glyph *beg = prev->glyphs[TEXT_AREA];
21097 glyph = beg + prev->used[TEXT_AREA];
21098 while (--glyph >= beg
21099 && INTEGERP (glyph->object));
21100 if (glyph < beg
21101 || !EQ (stop, glyph->object))
21102 break;
21103 row = prev;
21107 *x = row->x;
21108 *y = row->y;
21109 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21111 glyph = row->glyphs[TEXT_AREA];
21112 end = glyph + row->used[TEXT_AREA];
21114 /* Skip over glyphs not having an object at the start of the row.
21115 These are special glyphs like truncation marks on terminal
21116 frames. */
21117 if (row->displays_text_p)
21118 while (glyph < end
21119 && INTEGERP (glyph->object)
21120 && !EQ (stop, glyph->object)
21121 && glyph->charpos < 0)
21123 *x += glyph->pixel_width;
21124 ++glyph;
21127 while (glyph < end
21128 && !INTEGERP (glyph->object)
21129 && !EQ (stop, glyph->object)
21130 && (!BUFFERP (glyph->object)
21131 || glyph->charpos < charpos))
21133 *x += glyph->pixel_width;
21134 ++glyph;
21137 *hpos = glyph - row->glyphs[TEXT_AREA];
21138 return !past_end;
21141 #else /* not 1 */
21143 static int
21144 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21145 struct window *w;
21146 int pos;
21147 int *hpos, *vpos, *x, *y;
21148 Lisp_Object stop;
21150 int i;
21151 int lastcol;
21152 int maybe_next_line_p = 0;
21153 int line_start_position;
21154 int yb = window_text_bottom_y (w);
21155 struct glyph_row *row, *best_row;
21156 int row_vpos, best_row_vpos;
21157 int current_x;
21159 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21160 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21162 while (row->y < yb)
21164 if (row->used[TEXT_AREA])
21165 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21166 else
21167 line_start_position = 0;
21169 if (line_start_position > pos)
21170 break;
21171 /* If the position sought is the end of the buffer,
21172 don't include the blank lines at the bottom of the window. */
21173 else if (line_start_position == pos
21174 && pos == BUF_ZV (XBUFFER (w->buffer)))
21176 maybe_next_line_p = 1;
21177 break;
21179 else if (line_start_position > 0)
21181 best_row = row;
21182 best_row_vpos = row_vpos;
21185 if (row->y + row->height >= yb)
21186 break;
21188 ++row;
21189 ++row_vpos;
21192 /* Find the right column within BEST_ROW. */
21193 lastcol = 0;
21194 current_x = best_row->x;
21195 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21197 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21198 int charpos = glyph->charpos;
21200 if (BUFFERP (glyph->object))
21202 if (charpos == pos)
21204 *hpos = i;
21205 *vpos = best_row_vpos;
21206 *x = current_x;
21207 *y = best_row->y;
21208 return 1;
21210 else if (charpos > pos)
21211 break;
21213 else if (EQ (glyph->object, stop))
21214 break;
21216 if (charpos > 0)
21217 lastcol = i;
21218 current_x += glyph->pixel_width;
21221 /* If we're looking for the end of the buffer,
21222 and we didn't find it in the line we scanned,
21223 use the start of the following line. */
21224 if (maybe_next_line_p)
21226 ++best_row;
21227 ++best_row_vpos;
21228 lastcol = 0;
21229 current_x = best_row->x;
21232 *vpos = best_row_vpos;
21233 *hpos = lastcol + 1;
21234 *x = current_x;
21235 *y = best_row->y;
21236 return 0;
21239 #endif /* not 1 */
21242 /* Find the position of the glyph for position POS in OBJECT in
21243 window W's current matrix, and return in *X, *Y the pixel
21244 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21246 RIGHT_P non-zero means return the position of the right edge of the
21247 glyph, RIGHT_P zero means return the left edge position.
21249 If no glyph for POS exists in the matrix, return the position of
21250 the glyph with the next smaller position that is in the matrix, if
21251 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21252 exists in the matrix, return the position of the glyph with the
21253 next larger position in OBJECT.
21255 Value is non-zero if a glyph was found. */
21257 static int
21258 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21259 struct window *w;
21260 int pos;
21261 Lisp_Object object;
21262 int *hpos, *vpos, *x, *y;
21263 int right_p;
21265 int yb = window_text_bottom_y (w);
21266 struct glyph_row *r;
21267 struct glyph *best_glyph = NULL;
21268 struct glyph_row *best_row = NULL;
21269 int best_x = 0;
21271 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21272 r->enabled_p && r->y < yb;
21273 ++r)
21275 struct glyph *g = r->glyphs[TEXT_AREA];
21276 struct glyph *e = g + r->used[TEXT_AREA];
21277 int gx;
21279 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21280 if (EQ (g->object, object))
21282 if (g->charpos == pos)
21284 best_glyph = g;
21285 best_x = gx;
21286 best_row = r;
21287 goto found;
21289 else if (best_glyph == NULL
21290 || ((abs (g->charpos - pos)
21291 < abs (best_glyph->charpos - pos))
21292 && (right_p
21293 ? g->charpos < pos
21294 : g->charpos > pos)))
21296 best_glyph = g;
21297 best_x = gx;
21298 best_row = r;
21303 found:
21305 if (best_glyph)
21307 *x = best_x;
21308 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21310 if (right_p)
21312 *x += best_glyph->pixel_width;
21313 ++*hpos;
21316 *y = best_row->y;
21317 *vpos = best_row - w->current_matrix->rows;
21320 return best_glyph != NULL;
21324 /* See if position X, Y is within a hot-spot of an image. */
21326 static int
21327 on_hot_spot_p (hot_spot, x, y)
21328 Lisp_Object hot_spot;
21329 int x, y;
21331 if (!CONSP (hot_spot))
21332 return 0;
21334 if (EQ (XCAR (hot_spot), Qrect))
21336 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21337 Lisp_Object rect = XCDR (hot_spot);
21338 Lisp_Object tem;
21339 if (!CONSP (rect))
21340 return 0;
21341 if (!CONSP (XCAR (rect)))
21342 return 0;
21343 if (!CONSP (XCDR (rect)))
21344 return 0;
21345 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21346 return 0;
21347 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21348 return 0;
21349 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21350 return 0;
21351 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21352 return 0;
21353 return 1;
21355 else if (EQ (XCAR (hot_spot), Qcircle))
21357 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21358 Lisp_Object circ = XCDR (hot_spot);
21359 Lisp_Object lr, lx0, ly0;
21360 if (CONSP (circ)
21361 && CONSP (XCAR (circ))
21362 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21363 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21364 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21366 double r = XFLOATINT (lr);
21367 double dx = XINT (lx0) - x;
21368 double dy = XINT (ly0) - y;
21369 return (dx * dx + dy * dy <= r * r);
21372 else if (EQ (XCAR (hot_spot), Qpoly))
21374 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21375 if (VECTORP (XCDR (hot_spot)))
21377 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21378 Lisp_Object *poly = v->contents;
21379 int n = v->size;
21380 int i;
21381 int inside = 0;
21382 Lisp_Object lx, ly;
21383 int x0, y0;
21385 /* Need an even number of coordinates, and at least 3 edges. */
21386 if (n < 6 || n & 1)
21387 return 0;
21389 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21390 If count is odd, we are inside polygon. Pixels on edges
21391 may or may not be included depending on actual geometry of the
21392 polygon. */
21393 if ((lx = poly[n-2], !INTEGERP (lx))
21394 || (ly = poly[n-1], !INTEGERP (lx)))
21395 return 0;
21396 x0 = XINT (lx), y0 = XINT (ly);
21397 for (i = 0; i < n; i += 2)
21399 int x1 = x0, y1 = y0;
21400 if ((lx = poly[i], !INTEGERP (lx))
21401 || (ly = poly[i+1], !INTEGERP (ly)))
21402 return 0;
21403 x0 = XINT (lx), y0 = XINT (ly);
21405 /* Does this segment cross the X line? */
21406 if (x0 >= x)
21408 if (x1 >= x)
21409 continue;
21411 else if (x1 < x)
21412 continue;
21413 if (y > y0 && y > y1)
21414 continue;
21415 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21416 inside = !inside;
21418 return inside;
21421 /* If we don't understand the format, pretend we're not in the hot-spot. */
21422 return 0;
21425 Lisp_Object
21426 find_hot_spot (map, x, y)
21427 Lisp_Object map;
21428 int x, y;
21430 while (CONSP (map))
21432 if (CONSP (XCAR (map))
21433 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21434 return XCAR (map);
21435 map = XCDR (map);
21438 return Qnil;
21441 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21442 3, 3, 0,
21443 doc: /* Lookup in image map MAP coordinates X and Y.
21444 An image map is an alist where each element has the format (AREA ID PLIST).
21445 An AREA is specified as either a rectangle, a circle, or a polygon:
21446 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21447 pixel coordinates of the upper left and bottom right corners.
21448 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21449 and the radius of the circle; r may be a float or integer.
21450 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21451 vector describes one corner in the polygon.
21452 Returns the alist element for the first matching AREA in MAP. */)
21453 (map, x, y)
21454 Lisp_Object map;
21455 Lisp_Object x, y;
21457 if (NILP (map))
21458 return Qnil;
21460 CHECK_NUMBER (x);
21461 CHECK_NUMBER (y);
21463 return find_hot_spot (map, XINT (x), XINT (y));
21467 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21468 static void
21469 define_frame_cursor1 (f, cursor, pointer)
21470 struct frame *f;
21471 Cursor cursor;
21472 Lisp_Object pointer;
21474 /* Do not change cursor shape while dragging mouse. */
21475 if (!NILP (do_mouse_tracking))
21476 return;
21478 if (!NILP (pointer))
21480 if (EQ (pointer, Qarrow))
21481 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21482 else if (EQ (pointer, Qhand))
21483 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21484 else if (EQ (pointer, Qtext))
21485 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21486 else if (EQ (pointer, intern ("hdrag")))
21487 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21488 #ifdef HAVE_X_WINDOWS
21489 else if (EQ (pointer, intern ("vdrag")))
21490 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21491 #endif
21492 else if (EQ (pointer, intern ("hourglass")))
21493 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21494 else if (EQ (pointer, Qmodeline))
21495 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21496 else
21497 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21500 if (cursor != No_Cursor)
21501 rif->define_frame_cursor (f, cursor);
21504 /* Take proper action when mouse has moved to the mode or header line
21505 or marginal area AREA of window W, x-position X and y-position Y.
21506 X is relative to the start of the text display area of W, so the
21507 width of bitmap areas and scroll bars must be subtracted to get a
21508 position relative to the start of the mode line. */
21510 static void
21511 note_mode_line_or_margin_highlight (window, x, y, area)
21512 Lisp_Object window;
21513 int x, y;
21514 enum window_part area;
21516 struct window *w = XWINDOW (window);
21517 struct frame *f = XFRAME (w->frame);
21518 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21519 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21520 Lisp_Object pointer = Qnil;
21521 int charpos, dx, dy, width, height;
21522 Lisp_Object string, object = Qnil;
21523 Lisp_Object pos, help;
21525 Lisp_Object mouse_face;
21526 int original_x_pixel = x;
21527 struct glyph * glyph = NULL;
21528 struct glyph_row *row;
21530 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21532 int x0;
21533 struct glyph *end;
21535 string = mode_line_string (w, area, &x, &y, &charpos,
21536 &object, &dx, &dy, &width, &height);
21538 row = (area == ON_MODE_LINE
21539 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21540 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21542 /* Find glyph */
21543 if (row->mode_line_p && row->enabled_p)
21545 glyph = row->glyphs[TEXT_AREA];
21546 end = glyph + row->used[TEXT_AREA];
21548 for (x0 = original_x_pixel;
21549 glyph < end && x0 >= glyph->pixel_width;
21550 ++glyph)
21551 x0 -= glyph->pixel_width;
21553 if (glyph >= end)
21554 glyph = NULL;
21557 else
21559 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21560 string = marginal_area_string (w, area, &x, &y, &charpos,
21561 &object, &dx, &dy, &width, &height);
21564 help = Qnil;
21566 if (IMAGEP (object))
21568 Lisp_Object image_map, hotspot;
21569 if ((image_map = Fplist_get (XCDR (object), QCmap),
21570 !NILP (image_map))
21571 && (hotspot = find_hot_spot (image_map, dx, dy),
21572 CONSP (hotspot))
21573 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21575 Lisp_Object area_id, plist;
21577 area_id = XCAR (hotspot);
21578 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21579 If so, we could look for mouse-enter, mouse-leave
21580 properties in PLIST (and do something...). */
21581 hotspot = XCDR (hotspot);
21582 if (CONSP (hotspot)
21583 && (plist = XCAR (hotspot), CONSP (plist)))
21585 pointer = Fplist_get (plist, Qpointer);
21586 if (NILP (pointer))
21587 pointer = Qhand;
21588 help = Fplist_get (plist, Qhelp_echo);
21589 if (!NILP (help))
21591 help_echo_string = help;
21592 /* Is this correct? ++kfs */
21593 XSETWINDOW (help_echo_window, w);
21594 help_echo_object = w->buffer;
21595 help_echo_pos = charpos;
21599 if (NILP (pointer))
21600 pointer = Fplist_get (XCDR (object), QCpointer);
21603 if (STRINGP (string))
21605 pos = make_number (charpos);
21606 /* If we're on a string with `help-echo' text property, arrange
21607 for the help to be displayed. This is done by setting the
21608 global variable help_echo_string to the help string. */
21609 if (NILP (help))
21611 help = Fget_text_property (pos, Qhelp_echo, string);
21612 if (!NILP (help))
21614 help_echo_string = help;
21615 XSETWINDOW (help_echo_window, w);
21616 help_echo_object = string;
21617 help_echo_pos = charpos;
21621 if (NILP (pointer))
21622 pointer = Fget_text_property (pos, Qpointer, string);
21624 /* Change the mouse pointer according to what is under X/Y. */
21625 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21627 Lisp_Object map;
21628 map = Fget_text_property (pos, Qlocal_map, string);
21629 if (!KEYMAPP (map))
21630 map = Fget_text_property (pos, Qkeymap, string);
21631 if (!KEYMAPP (map))
21632 cursor = dpyinfo->vertical_scroll_bar_cursor;
21635 /* Change the mouse face according to what is under X/Y. */
21636 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21637 if (!NILP (mouse_face)
21638 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21639 && glyph)
21641 Lisp_Object b, e;
21643 struct glyph * tmp_glyph;
21645 int gpos;
21646 int gseq_length;
21647 int total_pixel_width;
21648 int ignore;
21650 int vpos, hpos;
21652 b = Fprevious_single_property_change (make_number (charpos + 1),
21653 Qmouse_face, string, Qnil);
21654 if (NILP (b))
21655 b = make_number (0);
21657 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21658 if (NILP (e))
21659 e = make_number (SCHARS (string));
21661 /* Calculate the position(glyph position: GPOS) of GLYPH in
21662 displayed string. GPOS is different from CHARPOS.
21664 CHARPOS is the position of glyph in internal string
21665 object. A mode line string format has structures which
21666 is converted to a flatten by emacs lisp interpreter.
21667 The internal string is an element of the structures.
21668 The displayed string is the flatten string. */
21669 for (tmp_glyph = glyph - 1, gpos = 0;
21670 tmp_glyph->charpos >= XINT (b);
21671 tmp_glyph--, gpos++)
21673 if (!EQ (tmp_glyph->object, glyph->object))
21674 break;
21677 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21678 displayed string holding GLYPH.
21680 GSEQ_LENGTH is different from SCHARS (STRING).
21681 SCHARS (STRING) returns the length of the internal string. */
21682 for (tmp_glyph = glyph, gseq_length = gpos;
21683 tmp_glyph->charpos < XINT (e);
21684 tmp_glyph++, gseq_length++)
21686 if (!EQ (tmp_glyph->object, glyph->object))
21687 break;
21690 total_pixel_width = 0;
21691 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21692 total_pixel_width += tmp_glyph->pixel_width;
21694 /* Pre calculation of re-rendering position */
21695 vpos = (x - gpos);
21696 hpos = (area == ON_MODE_LINE
21697 ? (w->current_matrix)->nrows - 1
21698 : 0);
21700 /* If the re-rendering position is included in the last
21701 re-rendering area, we should do nothing. */
21702 if ( EQ (window, dpyinfo->mouse_face_window)
21703 && dpyinfo->mouse_face_beg_col <= vpos
21704 && vpos < dpyinfo->mouse_face_end_col
21705 && dpyinfo->mouse_face_beg_row == hpos )
21706 return;
21708 if (clear_mouse_face (dpyinfo))
21709 cursor = No_Cursor;
21711 dpyinfo->mouse_face_beg_col = vpos;
21712 dpyinfo->mouse_face_beg_row = hpos;
21714 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21715 dpyinfo->mouse_face_beg_y = 0;
21717 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21718 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21720 dpyinfo->mouse_face_end_x = 0;
21721 dpyinfo->mouse_face_end_y = 0;
21723 dpyinfo->mouse_face_past_end = 0;
21724 dpyinfo->mouse_face_window = window;
21726 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21727 charpos,
21728 0, 0, 0, &ignore,
21729 glyph->face_id, 1);
21730 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21732 if (NILP (pointer))
21733 pointer = Qhand;
21735 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21736 clear_mouse_face (dpyinfo);
21738 define_frame_cursor1 (f, cursor, pointer);
21742 /* EXPORT:
21743 Take proper action when the mouse has moved to position X, Y on
21744 frame F as regards highlighting characters that have mouse-face
21745 properties. Also de-highlighting chars where the mouse was before.
21746 X and Y can be negative or out of range. */
21748 void
21749 note_mouse_highlight (f, x, y)
21750 struct frame *f;
21751 int x, y;
21753 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21754 enum window_part part;
21755 Lisp_Object window;
21756 struct window *w;
21757 Cursor cursor = No_Cursor;
21758 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21759 struct buffer *b;
21761 /* When a menu is active, don't highlight because this looks odd. */
21762 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21763 if (popup_activated ())
21764 return;
21765 #endif
21767 if (NILP (Vmouse_highlight)
21768 || !f->glyphs_initialized_p)
21769 return;
21771 dpyinfo->mouse_face_mouse_x = x;
21772 dpyinfo->mouse_face_mouse_y = y;
21773 dpyinfo->mouse_face_mouse_frame = f;
21775 if (dpyinfo->mouse_face_defer)
21776 return;
21778 if (gc_in_progress)
21780 dpyinfo->mouse_face_deferred_gc = 1;
21781 return;
21784 /* Which window is that in? */
21785 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21787 /* If we were displaying active text in another window, clear that.
21788 Also clear if we move out of text area in same window. */
21789 if (! EQ (window, dpyinfo->mouse_face_window)
21790 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21791 && !NILP (dpyinfo->mouse_face_window)))
21792 clear_mouse_face (dpyinfo);
21794 /* Not on a window -> return. */
21795 if (!WINDOWP (window))
21796 return;
21798 /* Reset help_echo_string. It will get recomputed below. */
21799 help_echo_string = Qnil;
21801 /* Convert to window-relative pixel coordinates. */
21802 w = XWINDOW (window);
21803 frame_to_window_pixel_xy (w, &x, &y);
21805 /* Handle tool-bar window differently since it doesn't display a
21806 buffer. */
21807 if (EQ (window, f->tool_bar_window))
21809 note_tool_bar_highlight (f, x, y);
21810 return;
21813 /* Mouse is on the mode, header line or margin? */
21814 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21815 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21817 note_mode_line_or_margin_highlight (window, x, y, part);
21818 return;
21821 if (part == ON_VERTICAL_BORDER)
21822 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21823 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21824 || part == ON_SCROLL_BAR)
21825 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21826 else
21827 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21829 /* Are we in a window whose display is up to date?
21830 And verify the buffer's text has not changed. */
21831 b = XBUFFER (w->buffer);
21832 if (part == ON_TEXT
21833 && EQ (w->window_end_valid, w->buffer)
21834 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21835 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21837 int hpos, vpos, pos, i, dx, dy, area;
21838 struct glyph *glyph;
21839 Lisp_Object object;
21840 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21841 Lisp_Object *overlay_vec = NULL;
21842 int noverlays;
21843 struct buffer *obuf;
21844 int obegv, ozv, same_region;
21846 /* Find the glyph under X/Y. */
21847 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21849 /* Look for :pointer property on image. */
21850 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21852 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21853 if (img != NULL && IMAGEP (img->spec))
21855 Lisp_Object image_map, hotspot;
21856 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21857 !NILP (image_map))
21858 && (hotspot = find_hot_spot (image_map,
21859 glyph->slice.x + dx,
21860 glyph->slice.y + dy),
21861 CONSP (hotspot))
21862 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21864 Lisp_Object area_id, plist;
21866 area_id = XCAR (hotspot);
21867 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21868 If so, we could look for mouse-enter, mouse-leave
21869 properties in PLIST (and do something...). */
21870 hotspot = XCDR (hotspot);
21871 if (CONSP (hotspot)
21872 && (plist = XCAR (hotspot), CONSP (plist)))
21874 pointer = Fplist_get (plist, Qpointer);
21875 if (NILP (pointer))
21876 pointer = Qhand;
21877 help_echo_string = Fplist_get (plist, Qhelp_echo);
21878 if (!NILP (help_echo_string))
21880 help_echo_window = window;
21881 help_echo_object = glyph->object;
21882 help_echo_pos = glyph->charpos;
21886 if (NILP (pointer))
21887 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21891 /* Clear mouse face if X/Y not over text. */
21892 if (glyph == NULL
21893 || area != TEXT_AREA
21894 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21896 if (clear_mouse_face (dpyinfo))
21897 cursor = No_Cursor;
21898 if (NILP (pointer))
21900 if (area != TEXT_AREA)
21901 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21902 else
21903 pointer = Vvoid_text_area_pointer;
21905 goto set_cursor;
21908 pos = glyph->charpos;
21909 object = glyph->object;
21910 if (!STRINGP (object) && !BUFFERP (object))
21911 goto set_cursor;
21913 /* If we get an out-of-range value, return now; avoid an error. */
21914 if (BUFFERP (object) && pos > BUF_Z (b))
21915 goto set_cursor;
21917 /* Make the window's buffer temporarily current for
21918 overlays_at and compute_char_face. */
21919 obuf = current_buffer;
21920 current_buffer = b;
21921 obegv = BEGV;
21922 ozv = ZV;
21923 BEGV = BEG;
21924 ZV = Z;
21926 /* Is this char mouse-active or does it have help-echo? */
21927 position = make_number (pos);
21929 if (BUFFERP (object))
21931 /* Put all the overlays we want in a vector in overlay_vec. */
21932 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21933 /* Sort overlays into increasing priority order. */
21934 noverlays = sort_overlays (overlay_vec, noverlays, w);
21936 else
21937 noverlays = 0;
21939 same_region = (EQ (window, dpyinfo->mouse_face_window)
21940 && vpos >= dpyinfo->mouse_face_beg_row
21941 && vpos <= dpyinfo->mouse_face_end_row
21942 && (vpos > dpyinfo->mouse_face_beg_row
21943 || hpos >= dpyinfo->mouse_face_beg_col)
21944 && (vpos < dpyinfo->mouse_face_end_row
21945 || hpos < dpyinfo->mouse_face_end_col
21946 || dpyinfo->mouse_face_past_end));
21948 if (same_region)
21949 cursor = No_Cursor;
21951 /* Check mouse-face highlighting. */
21952 if (! same_region
21953 /* If there exists an overlay with mouse-face overlapping
21954 the one we are currently highlighting, we have to
21955 check if we enter the overlapping overlay, and then
21956 highlight only that. */
21957 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21958 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21960 /* Find the highest priority overlay that has a mouse-face
21961 property. */
21962 overlay = Qnil;
21963 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21965 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21966 if (!NILP (mouse_face))
21967 overlay = overlay_vec[i];
21970 /* If we're actually highlighting the same overlay as
21971 before, there's no need to do that again. */
21972 if (!NILP (overlay)
21973 && EQ (overlay, dpyinfo->mouse_face_overlay))
21974 goto check_help_echo;
21976 dpyinfo->mouse_face_overlay = overlay;
21978 /* Clear the display of the old active region, if any. */
21979 if (clear_mouse_face (dpyinfo))
21980 cursor = No_Cursor;
21982 /* If no overlay applies, get a text property. */
21983 if (NILP (overlay))
21984 mouse_face = Fget_text_property (position, Qmouse_face, object);
21986 /* Handle the overlay case. */
21987 if (!NILP (overlay))
21989 /* Find the range of text around this char that
21990 should be active. */
21991 Lisp_Object before, after;
21992 int ignore;
21994 before = Foverlay_start (overlay);
21995 after = Foverlay_end (overlay);
21996 /* Record this as the current active region. */
21997 fast_find_position (w, XFASTINT (before),
21998 &dpyinfo->mouse_face_beg_col,
21999 &dpyinfo->mouse_face_beg_row,
22000 &dpyinfo->mouse_face_beg_x,
22001 &dpyinfo->mouse_face_beg_y, Qnil);
22003 dpyinfo->mouse_face_past_end
22004 = !fast_find_position (w, XFASTINT (after),
22005 &dpyinfo->mouse_face_end_col,
22006 &dpyinfo->mouse_face_end_row,
22007 &dpyinfo->mouse_face_end_x,
22008 &dpyinfo->mouse_face_end_y, Qnil);
22009 dpyinfo->mouse_face_window = window;
22011 dpyinfo->mouse_face_face_id
22012 = face_at_buffer_position (w, pos, 0, 0,
22013 &ignore, pos + 1,
22014 !dpyinfo->mouse_face_hidden);
22016 /* Display it as active. */
22017 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22018 cursor = No_Cursor;
22020 /* Handle the text property case. */
22021 else if (!NILP (mouse_face) && BUFFERP (object))
22023 /* Find the range of text around this char that
22024 should be active. */
22025 Lisp_Object before, after, beginning, end;
22026 int ignore;
22028 beginning = Fmarker_position (w->start);
22029 end = make_number (BUF_Z (XBUFFER (object))
22030 - XFASTINT (w->window_end_pos));
22031 before
22032 = Fprevious_single_property_change (make_number (pos + 1),
22033 Qmouse_face,
22034 object, beginning);
22035 after
22036 = Fnext_single_property_change (position, Qmouse_face,
22037 object, end);
22039 /* Record this as the current active region. */
22040 fast_find_position (w, XFASTINT (before),
22041 &dpyinfo->mouse_face_beg_col,
22042 &dpyinfo->mouse_face_beg_row,
22043 &dpyinfo->mouse_face_beg_x,
22044 &dpyinfo->mouse_face_beg_y, Qnil);
22045 dpyinfo->mouse_face_past_end
22046 = !fast_find_position (w, XFASTINT (after),
22047 &dpyinfo->mouse_face_end_col,
22048 &dpyinfo->mouse_face_end_row,
22049 &dpyinfo->mouse_face_end_x,
22050 &dpyinfo->mouse_face_end_y, Qnil);
22051 dpyinfo->mouse_face_window = window;
22053 if (BUFFERP (object))
22054 dpyinfo->mouse_face_face_id
22055 = face_at_buffer_position (w, pos, 0, 0,
22056 &ignore, pos + 1,
22057 !dpyinfo->mouse_face_hidden);
22059 /* Display it as active. */
22060 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22061 cursor = No_Cursor;
22063 else if (!NILP (mouse_face) && STRINGP (object))
22065 Lisp_Object b, e;
22066 int ignore;
22068 b = Fprevious_single_property_change (make_number (pos + 1),
22069 Qmouse_face,
22070 object, Qnil);
22071 e = Fnext_single_property_change (position, Qmouse_face,
22072 object, Qnil);
22073 if (NILP (b))
22074 b = make_number (0);
22075 if (NILP (e))
22076 e = make_number (SCHARS (object) - 1);
22078 fast_find_string_pos (w, XINT (b), object,
22079 &dpyinfo->mouse_face_beg_col,
22080 &dpyinfo->mouse_face_beg_row,
22081 &dpyinfo->mouse_face_beg_x,
22082 &dpyinfo->mouse_face_beg_y, 0);
22083 fast_find_string_pos (w, XINT (e), object,
22084 &dpyinfo->mouse_face_end_col,
22085 &dpyinfo->mouse_face_end_row,
22086 &dpyinfo->mouse_face_end_x,
22087 &dpyinfo->mouse_face_end_y, 1);
22088 dpyinfo->mouse_face_past_end = 0;
22089 dpyinfo->mouse_face_window = window;
22090 dpyinfo->mouse_face_face_id
22091 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22092 glyph->face_id, 1);
22093 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22094 cursor = No_Cursor;
22096 else if (STRINGP (object) && NILP (mouse_face))
22098 /* A string which doesn't have mouse-face, but
22099 the text ``under'' it might have. */
22100 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22101 int start = MATRIX_ROW_START_CHARPOS (r);
22103 pos = string_buffer_position (w, object, start);
22104 if (pos > 0)
22105 mouse_face = get_char_property_and_overlay (make_number (pos),
22106 Qmouse_face,
22107 w->buffer,
22108 &overlay);
22109 if (!NILP (mouse_face) && !NILP (overlay))
22111 Lisp_Object before = Foverlay_start (overlay);
22112 Lisp_Object after = Foverlay_end (overlay);
22113 int ignore;
22115 /* Note that we might not be able to find position
22116 BEFORE in the glyph matrix if the overlay is
22117 entirely covered by a `display' property. In
22118 this case, we overshoot. So let's stop in
22119 the glyph matrix before glyphs for OBJECT. */
22120 fast_find_position (w, XFASTINT (before),
22121 &dpyinfo->mouse_face_beg_col,
22122 &dpyinfo->mouse_face_beg_row,
22123 &dpyinfo->mouse_face_beg_x,
22124 &dpyinfo->mouse_face_beg_y,
22125 object);
22127 dpyinfo->mouse_face_past_end
22128 = !fast_find_position (w, XFASTINT (after),
22129 &dpyinfo->mouse_face_end_col,
22130 &dpyinfo->mouse_face_end_row,
22131 &dpyinfo->mouse_face_end_x,
22132 &dpyinfo->mouse_face_end_y,
22133 Qnil);
22134 dpyinfo->mouse_face_window = window;
22135 dpyinfo->mouse_face_face_id
22136 = face_at_buffer_position (w, pos, 0, 0,
22137 &ignore, pos + 1,
22138 !dpyinfo->mouse_face_hidden);
22140 /* Display it as active. */
22141 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22142 cursor = No_Cursor;
22147 check_help_echo:
22149 /* Look for a `help-echo' property. */
22150 if (NILP (help_echo_string)) {
22151 Lisp_Object help, overlay;
22153 /* Check overlays first. */
22154 help = overlay = Qnil;
22155 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22157 overlay = overlay_vec[i];
22158 help = Foverlay_get (overlay, Qhelp_echo);
22161 if (!NILP (help))
22163 help_echo_string = help;
22164 help_echo_window = window;
22165 help_echo_object = overlay;
22166 help_echo_pos = pos;
22168 else
22170 Lisp_Object object = glyph->object;
22171 int charpos = glyph->charpos;
22173 /* Try text properties. */
22174 if (STRINGP (object)
22175 && charpos >= 0
22176 && charpos < SCHARS (object))
22178 help = Fget_text_property (make_number (charpos),
22179 Qhelp_echo, object);
22180 if (NILP (help))
22182 /* If the string itself doesn't specify a help-echo,
22183 see if the buffer text ``under'' it does. */
22184 struct glyph_row *r
22185 = MATRIX_ROW (w->current_matrix, vpos);
22186 int start = MATRIX_ROW_START_CHARPOS (r);
22187 int pos = string_buffer_position (w, object, start);
22188 if (pos > 0)
22190 help = Fget_char_property (make_number (pos),
22191 Qhelp_echo, w->buffer);
22192 if (!NILP (help))
22194 charpos = pos;
22195 object = w->buffer;
22200 else if (BUFFERP (object)
22201 && charpos >= BEGV
22202 && charpos < ZV)
22203 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22204 object);
22206 if (!NILP (help))
22208 help_echo_string = help;
22209 help_echo_window = window;
22210 help_echo_object = object;
22211 help_echo_pos = charpos;
22216 /* Look for a `pointer' property. */
22217 if (NILP (pointer))
22219 /* Check overlays first. */
22220 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22221 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22223 if (NILP (pointer))
22225 Lisp_Object object = glyph->object;
22226 int charpos = glyph->charpos;
22228 /* Try text properties. */
22229 if (STRINGP (object)
22230 && charpos >= 0
22231 && charpos < SCHARS (object))
22233 pointer = Fget_text_property (make_number (charpos),
22234 Qpointer, object);
22235 if (NILP (pointer))
22237 /* If the string itself doesn't specify a pointer,
22238 see if the buffer text ``under'' it does. */
22239 struct glyph_row *r
22240 = MATRIX_ROW (w->current_matrix, vpos);
22241 int start = MATRIX_ROW_START_CHARPOS (r);
22242 int pos = string_buffer_position (w, object, start);
22243 if (pos > 0)
22244 pointer = Fget_char_property (make_number (pos),
22245 Qpointer, w->buffer);
22248 else if (BUFFERP (object)
22249 && charpos >= BEGV
22250 && charpos < ZV)
22251 pointer = Fget_text_property (make_number (charpos),
22252 Qpointer, object);
22256 BEGV = obegv;
22257 ZV = ozv;
22258 current_buffer = obuf;
22261 set_cursor:
22263 define_frame_cursor1 (f, cursor, pointer);
22267 /* EXPORT for RIF:
22268 Clear any mouse-face on window W. This function is part of the
22269 redisplay interface, and is called from try_window_id and similar
22270 functions to ensure the mouse-highlight is off. */
22272 void
22273 x_clear_window_mouse_face (w)
22274 struct window *w;
22276 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22277 Lisp_Object window;
22279 BLOCK_INPUT;
22280 XSETWINDOW (window, w);
22281 if (EQ (window, dpyinfo->mouse_face_window))
22282 clear_mouse_face (dpyinfo);
22283 UNBLOCK_INPUT;
22287 /* EXPORT:
22288 Just discard the mouse face information for frame F, if any.
22289 This is used when the size of F is changed. */
22291 void
22292 cancel_mouse_face (f)
22293 struct frame *f;
22295 Lisp_Object window;
22296 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22298 window = dpyinfo->mouse_face_window;
22299 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22301 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22302 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22303 dpyinfo->mouse_face_window = Qnil;
22308 #endif /* HAVE_WINDOW_SYSTEM */
22311 /***********************************************************************
22312 Exposure Events
22313 ***********************************************************************/
22315 #ifdef HAVE_WINDOW_SYSTEM
22317 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22318 which intersects rectangle R. R is in window-relative coordinates. */
22320 static void
22321 expose_area (w, row, r, area)
22322 struct window *w;
22323 struct glyph_row *row;
22324 XRectangle *r;
22325 enum glyph_row_area area;
22327 struct glyph *first = row->glyphs[area];
22328 struct glyph *end = row->glyphs[area] + row->used[area];
22329 struct glyph *last;
22330 int first_x, start_x, x;
22332 if (area == TEXT_AREA && row->fill_line_p)
22333 /* If row extends face to end of line write the whole line. */
22334 draw_glyphs (w, 0, row, area,
22335 0, row->used[area],
22336 DRAW_NORMAL_TEXT, 0);
22337 else
22339 /* Set START_X to the window-relative start position for drawing glyphs of
22340 AREA. The first glyph of the text area can be partially visible.
22341 The first glyphs of other areas cannot. */
22342 start_x = window_box_left_offset (w, area);
22343 x = start_x;
22344 if (area == TEXT_AREA)
22345 x += row->x;
22347 /* Find the first glyph that must be redrawn. */
22348 while (first < end
22349 && x + first->pixel_width < r->x)
22351 x += first->pixel_width;
22352 ++first;
22355 /* Find the last one. */
22356 last = first;
22357 first_x = x;
22358 while (last < end
22359 && x < r->x + r->width)
22361 x += last->pixel_width;
22362 ++last;
22365 /* Repaint. */
22366 if (last > first)
22367 draw_glyphs (w, first_x - start_x, row, area,
22368 first - row->glyphs[area], last - row->glyphs[area],
22369 DRAW_NORMAL_TEXT, 0);
22374 /* Redraw the parts of the glyph row ROW on window W intersecting
22375 rectangle R. R is in window-relative coordinates. Value is
22376 non-zero if mouse-face was overwritten. */
22378 static int
22379 expose_line (w, row, r)
22380 struct window *w;
22381 struct glyph_row *row;
22382 XRectangle *r;
22384 xassert (row->enabled_p);
22386 if (row->mode_line_p || w->pseudo_window_p)
22387 draw_glyphs (w, 0, row, TEXT_AREA,
22388 0, row->used[TEXT_AREA],
22389 DRAW_NORMAL_TEXT, 0);
22390 else
22392 if (row->used[LEFT_MARGIN_AREA])
22393 expose_area (w, row, r, LEFT_MARGIN_AREA);
22394 if (row->used[TEXT_AREA])
22395 expose_area (w, row, r, TEXT_AREA);
22396 if (row->used[RIGHT_MARGIN_AREA])
22397 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22398 draw_row_fringe_bitmaps (w, row);
22401 return row->mouse_face_p;
22405 /* Redraw those parts of glyphs rows during expose event handling that
22406 overlap other rows. Redrawing of an exposed line writes over parts
22407 of lines overlapping that exposed line; this function fixes that.
22409 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22410 row in W's current matrix that is exposed and overlaps other rows.
22411 LAST_OVERLAPPING_ROW is the last such row. */
22413 static void
22414 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22415 struct window *w;
22416 struct glyph_row *first_overlapping_row;
22417 struct glyph_row *last_overlapping_row;
22419 struct glyph_row *row;
22421 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22422 if (row->overlapping_p)
22424 xassert (row->enabled_p && !row->mode_line_p);
22426 if (row->used[LEFT_MARGIN_AREA])
22427 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22429 if (row->used[TEXT_AREA])
22430 x_fix_overlapping_area (w, row, TEXT_AREA);
22432 if (row->used[RIGHT_MARGIN_AREA])
22433 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22438 /* Return non-zero if W's cursor intersects rectangle R. */
22440 static int
22441 phys_cursor_in_rect_p (w, r)
22442 struct window *w;
22443 XRectangle *r;
22445 XRectangle cr, result;
22446 struct glyph *cursor_glyph;
22448 cursor_glyph = get_phys_cursor_glyph (w);
22449 if (cursor_glyph)
22451 /* r is relative to W's box, but w->phys_cursor.x is relative
22452 to left edge of W's TEXT area. Adjust it. */
22453 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22454 cr.y = w->phys_cursor.y;
22455 cr.width = cursor_glyph->pixel_width;
22456 cr.height = w->phys_cursor_height;
22457 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22458 I assume the effect is the same -- and this is portable. */
22459 return x_intersect_rectangles (&cr, r, &result);
22461 else
22462 return 0;
22466 /* EXPORT:
22467 Draw a vertical window border to the right of window W if W doesn't
22468 have vertical scroll bars. */
22470 void
22471 x_draw_vertical_border (w)
22472 struct window *w;
22474 /* We could do better, if we knew what type of scroll-bar the adjacent
22475 windows (on either side) have... But we don't :-(
22476 However, I think this works ok. ++KFS 2003-04-25 */
22478 /* Redraw borders between horizontally adjacent windows. Don't
22479 do it for frames with vertical scroll bars because either the
22480 right scroll bar of a window, or the left scroll bar of its
22481 neighbor will suffice as a border. */
22482 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22483 return;
22485 if (!WINDOW_RIGHTMOST_P (w)
22486 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22488 int x0, x1, y0, y1;
22490 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22491 y1 -= 1;
22493 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22494 x1 -= 1;
22496 rif->draw_vertical_window_border (w, x1, y0, y1);
22498 else if (!WINDOW_LEFTMOST_P (w)
22499 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22501 int x0, x1, y0, y1;
22503 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22504 y1 -= 1;
22506 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22507 x0 -= 1;
22509 rif->draw_vertical_window_border (w, x0, y0, y1);
22514 /* Redraw the part of window W intersection rectangle FR. Pixel
22515 coordinates in FR are frame-relative. Call this function with
22516 input blocked. Value is non-zero if the exposure overwrites
22517 mouse-face. */
22519 static int
22520 expose_window (w, fr)
22521 struct window *w;
22522 XRectangle *fr;
22524 struct frame *f = XFRAME (w->frame);
22525 XRectangle wr, r;
22526 int mouse_face_overwritten_p = 0;
22528 /* If window is not yet fully initialized, do nothing. This can
22529 happen when toolkit scroll bars are used and a window is split.
22530 Reconfiguring the scroll bar will generate an expose for a newly
22531 created window. */
22532 if (w->current_matrix == NULL)
22533 return 0;
22535 /* When we're currently updating the window, display and current
22536 matrix usually don't agree. Arrange for a thorough display
22537 later. */
22538 if (w == updated_window)
22540 SET_FRAME_GARBAGED (f);
22541 return 0;
22544 /* Frame-relative pixel rectangle of W. */
22545 wr.x = WINDOW_LEFT_EDGE_X (w);
22546 wr.y = WINDOW_TOP_EDGE_Y (w);
22547 wr.width = WINDOW_TOTAL_WIDTH (w);
22548 wr.height = WINDOW_TOTAL_HEIGHT (w);
22550 if (x_intersect_rectangles (fr, &wr, &r))
22552 int yb = window_text_bottom_y (w);
22553 struct glyph_row *row;
22554 int cursor_cleared_p;
22555 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22557 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22558 r.x, r.y, r.width, r.height));
22560 /* Convert to window coordinates. */
22561 r.x -= WINDOW_LEFT_EDGE_X (w);
22562 r.y -= WINDOW_TOP_EDGE_Y (w);
22564 /* Turn off the cursor. */
22565 if (!w->pseudo_window_p
22566 && phys_cursor_in_rect_p (w, &r))
22568 x_clear_cursor (w);
22569 cursor_cleared_p = 1;
22571 else
22572 cursor_cleared_p = 0;
22574 /* Update lines intersecting rectangle R. */
22575 first_overlapping_row = last_overlapping_row = NULL;
22576 for (row = w->current_matrix->rows;
22577 row->enabled_p;
22578 ++row)
22580 int y0 = row->y;
22581 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22583 if ((y0 >= r.y && y0 < r.y + r.height)
22584 || (y1 > r.y && y1 < r.y + r.height)
22585 || (r.y >= y0 && r.y < y1)
22586 || (r.y + r.height > y0 && r.y + r.height < y1))
22588 /* A header line may be overlapping, but there is no need
22589 to fix overlapping areas for them. KFS 2005-02-12 */
22590 if (row->overlapping_p && !row->mode_line_p)
22592 if (first_overlapping_row == NULL)
22593 first_overlapping_row = row;
22594 last_overlapping_row = row;
22597 if (expose_line (w, row, &r))
22598 mouse_face_overwritten_p = 1;
22601 if (y1 >= yb)
22602 break;
22605 /* Display the mode line if there is one. */
22606 if (WINDOW_WANTS_MODELINE_P (w)
22607 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22608 row->enabled_p)
22609 && row->y < r.y + r.height)
22611 if (expose_line (w, row, &r))
22612 mouse_face_overwritten_p = 1;
22615 if (!w->pseudo_window_p)
22617 /* Fix the display of overlapping rows. */
22618 if (first_overlapping_row)
22619 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22621 /* Draw border between windows. */
22622 x_draw_vertical_border (w);
22624 /* Turn the cursor on again. */
22625 if (cursor_cleared_p)
22626 update_window_cursor (w, 1);
22630 return mouse_face_overwritten_p;
22635 /* Redraw (parts) of all windows in the window tree rooted at W that
22636 intersect R. R contains frame pixel coordinates. Value is
22637 non-zero if the exposure overwrites mouse-face. */
22639 static int
22640 expose_window_tree (w, r)
22641 struct window *w;
22642 XRectangle *r;
22644 struct frame *f = XFRAME (w->frame);
22645 int mouse_face_overwritten_p = 0;
22647 while (w && !FRAME_GARBAGED_P (f))
22649 if (!NILP (w->hchild))
22650 mouse_face_overwritten_p
22651 |= expose_window_tree (XWINDOW (w->hchild), r);
22652 else if (!NILP (w->vchild))
22653 mouse_face_overwritten_p
22654 |= expose_window_tree (XWINDOW (w->vchild), r);
22655 else
22656 mouse_face_overwritten_p |= expose_window (w, r);
22658 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22661 return mouse_face_overwritten_p;
22665 /* EXPORT:
22666 Redisplay an exposed area of frame F. X and Y are the upper-left
22667 corner of the exposed rectangle. W and H are width and height of
22668 the exposed area. All are pixel values. W or H zero means redraw
22669 the entire frame. */
22671 void
22672 expose_frame (f, x, y, w, h)
22673 struct frame *f;
22674 int x, y, w, h;
22676 XRectangle r;
22677 int mouse_face_overwritten_p = 0;
22679 TRACE ((stderr, "expose_frame "));
22681 /* No need to redraw if frame will be redrawn soon. */
22682 if (FRAME_GARBAGED_P (f))
22684 TRACE ((stderr, " garbaged\n"));
22685 return;
22688 /* If basic faces haven't been realized yet, there is no point in
22689 trying to redraw anything. This can happen when we get an expose
22690 event while Emacs is starting, e.g. by moving another window. */
22691 if (FRAME_FACE_CACHE (f) == NULL
22692 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22694 TRACE ((stderr, " no faces\n"));
22695 return;
22698 if (w == 0 || h == 0)
22700 r.x = r.y = 0;
22701 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22702 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22704 else
22706 r.x = x;
22707 r.y = y;
22708 r.width = w;
22709 r.height = h;
22712 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22713 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22715 if (WINDOWP (f->tool_bar_window))
22716 mouse_face_overwritten_p
22717 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22719 #ifdef HAVE_X_WINDOWS
22720 #ifndef MSDOS
22721 #ifndef USE_X_TOOLKIT
22722 if (WINDOWP (f->menu_bar_window))
22723 mouse_face_overwritten_p
22724 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22725 #endif /* not USE_X_TOOLKIT */
22726 #endif
22727 #endif
22729 /* Some window managers support a focus-follows-mouse style with
22730 delayed raising of frames. Imagine a partially obscured frame,
22731 and moving the mouse into partially obscured mouse-face on that
22732 frame. The visible part of the mouse-face will be highlighted,
22733 then the WM raises the obscured frame. With at least one WM, KDE
22734 2.1, Emacs is not getting any event for the raising of the frame
22735 (even tried with SubstructureRedirectMask), only Expose events.
22736 These expose events will draw text normally, i.e. not
22737 highlighted. Which means we must redo the highlight here.
22738 Subsume it under ``we love X''. --gerd 2001-08-15 */
22739 /* Included in Windows version because Windows most likely does not
22740 do the right thing if any third party tool offers
22741 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22742 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22744 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22745 if (f == dpyinfo->mouse_face_mouse_frame)
22747 int x = dpyinfo->mouse_face_mouse_x;
22748 int y = dpyinfo->mouse_face_mouse_y;
22749 clear_mouse_face (dpyinfo);
22750 note_mouse_highlight (f, x, y);
22756 /* EXPORT:
22757 Determine the intersection of two rectangles R1 and R2. Return
22758 the intersection in *RESULT. Value is non-zero if RESULT is not
22759 empty. */
22762 x_intersect_rectangles (r1, r2, result)
22763 XRectangle *r1, *r2, *result;
22765 XRectangle *left, *right;
22766 XRectangle *upper, *lower;
22767 int intersection_p = 0;
22769 /* Rearrange so that R1 is the left-most rectangle. */
22770 if (r1->x < r2->x)
22771 left = r1, right = r2;
22772 else
22773 left = r2, right = r1;
22775 /* X0 of the intersection is right.x0, if this is inside R1,
22776 otherwise there is no intersection. */
22777 if (right->x <= left->x + left->width)
22779 result->x = right->x;
22781 /* The right end of the intersection is the minimum of the
22782 the right ends of left and right. */
22783 result->width = (min (left->x + left->width, right->x + right->width)
22784 - result->x);
22786 /* Same game for Y. */
22787 if (r1->y < r2->y)
22788 upper = r1, lower = r2;
22789 else
22790 upper = r2, lower = r1;
22792 /* The upper end of the intersection is lower.y0, if this is inside
22793 of upper. Otherwise, there is no intersection. */
22794 if (lower->y <= upper->y + upper->height)
22796 result->y = lower->y;
22798 /* The lower end of the intersection is the minimum of the lower
22799 ends of upper and lower. */
22800 result->height = (min (lower->y + lower->height,
22801 upper->y + upper->height)
22802 - result->y);
22803 intersection_p = 1;
22807 return intersection_p;
22810 #endif /* HAVE_WINDOW_SYSTEM */
22813 /***********************************************************************
22814 Initialization
22815 ***********************************************************************/
22817 void
22818 syms_of_xdisp ()
22820 Vwith_echo_area_save_vector = Qnil;
22821 staticpro (&Vwith_echo_area_save_vector);
22823 Vmessage_stack = Qnil;
22824 staticpro (&Vmessage_stack);
22826 Qinhibit_redisplay = intern ("inhibit-redisplay");
22827 staticpro (&Qinhibit_redisplay);
22829 message_dolog_marker1 = Fmake_marker ();
22830 staticpro (&message_dolog_marker1);
22831 message_dolog_marker2 = Fmake_marker ();
22832 staticpro (&message_dolog_marker2);
22833 message_dolog_marker3 = Fmake_marker ();
22834 staticpro (&message_dolog_marker3);
22836 #if GLYPH_DEBUG
22837 defsubr (&Sdump_frame_glyph_matrix);
22838 defsubr (&Sdump_glyph_matrix);
22839 defsubr (&Sdump_glyph_row);
22840 defsubr (&Sdump_tool_bar_row);
22841 defsubr (&Strace_redisplay);
22842 defsubr (&Strace_to_stderr);
22843 #endif
22844 #ifdef HAVE_WINDOW_SYSTEM
22845 defsubr (&Stool_bar_lines_needed);
22846 defsubr (&Slookup_image_map);
22847 #endif
22848 defsubr (&Sformat_mode_line);
22850 staticpro (&Qmenu_bar_update_hook);
22851 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22853 staticpro (&Qoverriding_terminal_local_map);
22854 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22856 staticpro (&Qoverriding_local_map);
22857 Qoverriding_local_map = intern ("overriding-local-map");
22859 staticpro (&Qwindow_scroll_functions);
22860 Qwindow_scroll_functions = intern ("window-scroll-functions");
22862 staticpro (&Qredisplay_end_trigger_functions);
22863 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22865 staticpro (&Qinhibit_point_motion_hooks);
22866 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22868 QCdata = intern (":data");
22869 staticpro (&QCdata);
22870 Qdisplay = intern ("display");
22871 staticpro (&Qdisplay);
22872 Qspace_width = intern ("space-width");
22873 staticpro (&Qspace_width);
22874 Qraise = intern ("raise");
22875 staticpro (&Qraise);
22876 Qslice = intern ("slice");
22877 staticpro (&Qslice);
22878 Qspace = intern ("space");
22879 staticpro (&Qspace);
22880 Qmargin = intern ("margin");
22881 staticpro (&Qmargin);
22882 Qpointer = intern ("pointer");
22883 staticpro (&Qpointer);
22884 Qleft_margin = intern ("left-margin");
22885 staticpro (&Qleft_margin);
22886 Qright_margin = intern ("right-margin");
22887 staticpro (&Qright_margin);
22888 Qcenter = intern ("center");
22889 staticpro (&Qcenter);
22890 Qline_height = intern ("line-height");
22891 staticpro (&Qline_height);
22892 QCalign_to = intern (":align-to");
22893 staticpro (&QCalign_to);
22894 QCrelative_width = intern (":relative-width");
22895 staticpro (&QCrelative_width);
22896 QCrelative_height = intern (":relative-height");
22897 staticpro (&QCrelative_height);
22898 QCeval = intern (":eval");
22899 staticpro (&QCeval);
22900 QCpropertize = intern (":propertize");
22901 staticpro (&QCpropertize);
22902 QCfile = intern (":file");
22903 staticpro (&QCfile);
22904 Qfontified = intern ("fontified");
22905 staticpro (&Qfontified);
22906 Qfontification_functions = intern ("fontification-functions");
22907 staticpro (&Qfontification_functions);
22908 Qtrailing_whitespace = intern ("trailing-whitespace");
22909 staticpro (&Qtrailing_whitespace);
22910 Qescape_glyph = intern ("escape-glyph");
22911 staticpro (&Qescape_glyph);
22912 Qnobreak_space = intern ("nobreak-space");
22913 staticpro (&Qnobreak_space);
22914 Qimage = intern ("image");
22915 staticpro (&Qimage);
22916 QCmap = intern (":map");
22917 staticpro (&QCmap);
22918 QCpointer = intern (":pointer");
22919 staticpro (&QCpointer);
22920 Qrect = intern ("rect");
22921 staticpro (&Qrect);
22922 Qcircle = intern ("circle");
22923 staticpro (&Qcircle);
22924 Qpoly = intern ("poly");
22925 staticpro (&Qpoly);
22926 Qmessage_truncate_lines = intern ("message-truncate-lines");
22927 staticpro (&Qmessage_truncate_lines);
22928 Qgrow_only = intern ("grow-only");
22929 staticpro (&Qgrow_only);
22930 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22931 staticpro (&Qinhibit_menubar_update);
22932 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22933 staticpro (&Qinhibit_eval_during_redisplay);
22934 Qposition = intern ("position");
22935 staticpro (&Qposition);
22936 Qbuffer_position = intern ("buffer-position");
22937 staticpro (&Qbuffer_position);
22938 Qobject = intern ("object");
22939 staticpro (&Qobject);
22940 Qbar = intern ("bar");
22941 staticpro (&Qbar);
22942 Qhbar = intern ("hbar");
22943 staticpro (&Qhbar);
22944 Qbox = intern ("box");
22945 staticpro (&Qbox);
22946 Qhollow = intern ("hollow");
22947 staticpro (&Qhollow);
22948 Qhand = intern ("hand");
22949 staticpro (&Qhand);
22950 Qarrow = intern ("arrow");
22951 staticpro (&Qarrow);
22952 Qtext = intern ("text");
22953 staticpro (&Qtext);
22954 Qrisky_local_variable = intern ("risky-local-variable");
22955 staticpro (&Qrisky_local_variable);
22956 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22957 staticpro (&Qinhibit_free_realized_faces);
22959 list_of_error = Fcons (Fcons (intern ("error"),
22960 Fcons (intern ("void-variable"), Qnil)),
22961 Qnil);
22962 staticpro (&list_of_error);
22964 Qlast_arrow_position = intern ("last-arrow-position");
22965 staticpro (&Qlast_arrow_position);
22966 Qlast_arrow_string = intern ("last-arrow-string");
22967 staticpro (&Qlast_arrow_string);
22969 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22970 staticpro (&Qoverlay_arrow_string);
22971 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22972 staticpro (&Qoverlay_arrow_bitmap);
22974 echo_buffer[0] = echo_buffer[1] = Qnil;
22975 staticpro (&echo_buffer[0]);
22976 staticpro (&echo_buffer[1]);
22978 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22979 staticpro (&echo_area_buffer[0]);
22980 staticpro (&echo_area_buffer[1]);
22982 Vmessages_buffer_name = build_string ("*Messages*");
22983 staticpro (&Vmessages_buffer_name);
22985 mode_line_proptrans_alist = Qnil;
22986 staticpro (&mode_line_proptrans_alist);
22987 mode_line_string_list = Qnil;
22988 staticpro (&mode_line_string_list);
22989 mode_line_string_face = Qnil;
22990 staticpro (&mode_line_string_face);
22991 mode_line_string_face_prop = Qnil;
22992 staticpro (&mode_line_string_face_prop);
22993 Vmode_line_unwind_vector = Qnil;
22994 staticpro (&Vmode_line_unwind_vector);
22996 help_echo_string = Qnil;
22997 staticpro (&help_echo_string);
22998 help_echo_object = Qnil;
22999 staticpro (&help_echo_object);
23000 help_echo_window = Qnil;
23001 staticpro (&help_echo_window);
23002 previous_help_echo_string = Qnil;
23003 staticpro (&previous_help_echo_string);
23004 help_echo_pos = -1;
23006 #ifdef HAVE_WINDOW_SYSTEM
23007 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23008 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23009 For example, if a block cursor is over a tab, it will be drawn as
23010 wide as that tab on the display. */);
23011 x_stretch_cursor_p = 0;
23012 #endif
23014 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23015 doc: /* *Non-nil means highlight trailing whitespace.
23016 The face used for trailing whitespace is `trailing-whitespace'. */);
23017 Vshow_trailing_whitespace = Qnil;
23019 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23020 doc: /* *Control highlighting of nobreak space and soft hyphen.
23021 A value of t means highlight the character itself (for nobreak space,
23022 use face `nobreak-space').
23023 A value of nil means no highlighting.
23024 Other values mean display the escape glyph followed by an ordinary
23025 space or ordinary hyphen. */);
23026 Vnobreak_char_display = Qt;
23028 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23029 doc: /* *The pointer shape to show in void text areas.
23030 A value of nil means to show the text pointer. Other options are `arrow',
23031 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23032 Vvoid_text_area_pointer = Qarrow;
23034 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23035 doc: /* Non-nil means don't actually do any redisplay.
23036 This is used for internal purposes. */);
23037 Vinhibit_redisplay = Qnil;
23039 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23040 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23041 Vglobal_mode_string = Qnil;
23043 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23044 doc: /* Marker for where to display an arrow on top of the buffer text.
23045 This must be the beginning of a line in order to work.
23046 See also `overlay-arrow-string'. */);
23047 Voverlay_arrow_position = Qnil;
23049 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23050 doc: /* String to display as an arrow in non-window frames.
23051 See also `overlay-arrow-position'. */);
23052 Voverlay_arrow_string = build_string ("=>");
23054 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23055 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23056 The symbols on this list are examined during redisplay to determine
23057 where to display overlay arrows. */);
23058 Voverlay_arrow_variable_list
23059 = Fcons (intern ("overlay-arrow-position"), Qnil);
23061 DEFVAR_INT ("scroll-step", &scroll_step,
23062 doc: /* *The number of lines to try scrolling a window by when point moves out.
23063 If that fails to bring point back on frame, point is centered instead.
23064 If this is zero, point is always centered after it moves off frame.
23065 If you want scrolling to always be a line at a time, you should set
23066 `scroll-conservatively' to a large value rather than set this to 1. */);
23068 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23069 doc: /* *Scroll up to this many lines, to bring point back on screen.
23070 A value of zero means to scroll the text to center point vertically
23071 in the window. */);
23072 scroll_conservatively = 0;
23074 DEFVAR_INT ("scroll-margin", &scroll_margin,
23075 doc: /* *Number of lines of margin at the top and bottom of a window.
23076 Recenter the window whenever point gets within this many lines
23077 of the top or bottom of the window. */);
23078 scroll_margin = 0;
23080 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23081 doc: /* Pixels per inch value for non-window system displays.
23082 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23083 Vdisplay_pixels_per_inch = make_float (72.0);
23085 #if GLYPH_DEBUG
23086 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23087 #endif
23089 DEFVAR_BOOL ("truncate-partial-width-windows",
23090 &truncate_partial_width_windows,
23091 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23092 truncate_partial_width_windows = 1;
23094 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23095 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23096 Any other value means to use the appropriate face, `mode-line',
23097 `header-line', or `menu' respectively. */);
23098 mode_line_inverse_video = 1;
23100 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23101 doc: /* *Maximum buffer size for which line number should be displayed.
23102 If the buffer is bigger than this, the line number does not appear
23103 in the mode line. A value of nil means no limit. */);
23104 Vline_number_display_limit = Qnil;
23106 DEFVAR_INT ("line-number-display-limit-width",
23107 &line_number_display_limit_width,
23108 doc: /* *Maximum line width (in characters) for line number display.
23109 If the average length of the lines near point is bigger than this, then the
23110 line number may be omitted from the mode line. */);
23111 line_number_display_limit_width = 200;
23113 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23114 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23115 highlight_nonselected_windows = 0;
23117 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23118 doc: /* Non-nil if more than one frame is visible on this display.
23119 Minibuffer-only frames don't count, but iconified frames do.
23120 This variable is not guaranteed to be accurate except while processing
23121 `frame-title-format' and `icon-title-format'. */);
23123 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23124 doc: /* Template for displaying the title bar of visible frames.
23125 \(Assuming the window manager supports this feature.)
23126 This variable has the same structure as `mode-line-format' (which see),
23127 and is used only on frames for which no explicit name has been set
23128 \(see `modify-frame-parameters'). */);
23130 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23131 doc: /* Template for displaying the title bar of an iconified frame.
23132 \(Assuming the window manager supports this feature.)
23133 This variable has the same structure as `mode-line-format' (which see),
23134 and is used only on frames for which no explicit name has been set
23135 \(see `modify-frame-parameters'). */);
23136 Vicon_title_format
23137 = Vframe_title_format
23138 = Fcons (intern ("multiple-frames"),
23139 Fcons (build_string ("%b"),
23140 Fcons (Fcons (empty_string,
23141 Fcons (intern ("invocation-name"),
23142 Fcons (build_string ("@"),
23143 Fcons (intern ("system-name"),
23144 Qnil)))),
23145 Qnil)));
23147 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23148 doc: /* Maximum number of lines to keep in the message log buffer.
23149 If nil, disable message logging. If t, log messages but don't truncate
23150 the buffer when it becomes large. */);
23151 Vmessage_log_max = make_number (50);
23153 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23154 doc: /* Functions called before redisplay, if window sizes have changed.
23155 The value should be a list of functions that take one argument.
23156 Just before redisplay, for each frame, if any of its windows have changed
23157 size since the last redisplay, or have been split or deleted,
23158 all the functions in the list are called, with the frame as argument. */);
23159 Vwindow_size_change_functions = Qnil;
23161 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23162 doc: /* List of functions to call before redisplaying a window with scrolling.
23163 Each function is called with two arguments, the window
23164 and its new display-start position. Note that the value of `window-end'
23165 is not valid when these functions are called. */);
23166 Vwindow_scroll_functions = Qnil;
23168 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23169 doc: /* Functions called when redisplay of a window reaches the end trigger.
23170 Each function is called with two arguments, the window and the end trigger value.
23171 See `set-window-redisplay-end-trigger'. */);
23172 Vredisplay_end_trigger_functions = Qnil;
23174 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23175 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23176 mouse_autoselect_window = 0;
23178 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23179 doc: /* *Non-nil means automatically resize tool-bars.
23180 This increases a tool-bar's height if not all tool-bar items are visible.
23181 It decreases a tool-bar's height when it would display blank lines
23182 otherwise. */);
23183 auto_resize_tool_bars_p = 1;
23185 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23186 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23187 auto_raise_tool_bar_buttons_p = 1;
23189 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23190 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23191 make_cursor_line_fully_visible_p = 1;
23193 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23194 doc: /* *Margin around tool-bar buttons in pixels.
23195 If an integer, use that for both horizontal and vertical margins.
23196 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23197 HORZ specifying the horizontal margin, and VERT specifying the
23198 vertical margin. */);
23199 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23201 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23202 doc: /* *Relief thickness of tool-bar buttons. */);
23203 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23205 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23206 doc: /* List of functions to call to fontify regions of text.
23207 Each function is called with one argument POS. Functions must
23208 fontify a region starting at POS in the current buffer, and give
23209 fontified regions the property `fontified'. */);
23210 Vfontification_functions = Qnil;
23211 Fmake_variable_buffer_local (Qfontification_functions);
23213 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23214 &unibyte_display_via_language_environment,
23215 doc: /* *Non-nil means display unibyte text according to language environment.
23216 Specifically this means that unibyte non-ASCII characters
23217 are displayed by converting them to the equivalent multibyte characters
23218 according to the current language environment. As a result, they are
23219 displayed according to the current fontset. */);
23220 unibyte_display_via_language_environment = 0;
23222 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23223 doc: /* *Maximum height for resizing mini-windows.
23224 If a float, it specifies a fraction of the mini-window frame's height.
23225 If an integer, it specifies a number of lines. */);
23226 Vmax_mini_window_height = make_float (0.25);
23228 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23229 doc: /* *How to resize mini-windows.
23230 A value of nil means don't automatically resize mini-windows.
23231 A value of t means resize them to fit the text displayed in them.
23232 A value of `grow-only', the default, means let mini-windows grow
23233 only, until their display becomes empty, at which point the windows
23234 go back to their normal size. */);
23235 Vresize_mini_windows = Qgrow_only;
23237 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23238 doc: /* Alist specifying how to blink the cursor off.
23239 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23240 `cursor-type' frame-parameter or variable equals ON-STATE,
23241 comparing using `equal', Emacs uses OFF-STATE to specify
23242 how to blink it off. */);
23243 Vblink_cursor_alist = Qnil;
23245 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23246 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23247 automatic_hscrolling_p = 1;
23249 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23250 doc: /* *How many columns away from the window edge point is allowed to get
23251 before automatic hscrolling will horizontally scroll the window. */);
23252 hscroll_margin = 5;
23254 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23255 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23256 When point is less than `automatic-hscroll-margin' columns from the window
23257 edge, automatic hscrolling will scroll the window by the amount of columns
23258 determined by this variable. If its value is a positive integer, scroll that
23259 many columns. If it's a positive floating-point number, it specifies the
23260 fraction of the window's width to scroll. If it's nil or zero, point will be
23261 centered horizontally after the scroll. Any other value, including negative
23262 numbers, are treated as if the value were zero.
23264 Automatic hscrolling always moves point outside the scroll margin, so if
23265 point was more than scroll step columns inside the margin, the window will
23266 scroll more than the value given by the scroll step.
23268 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23269 and `scroll-right' overrides this variable's effect. */);
23270 Vhscroll_step = make_number (0);
23272 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23273 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23274 Bind this around calls to `message' to let it take effect. */);
23275 message_truncate_lines = 0;
23277 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23278 doc: /* Normal hook run to update the menu bar definitions.
23279 Redisplay runs this hook before it redisplays the menu bar.
23280 This is used to update submenus such as Buffers,
23281 whose contents depend on various data. */);
23282 Vmenu_bar_update_hook = Qnil;
23284 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23285 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23286 inhibit_menubar_update = 0;
23288 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23289 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23290 inhibit_eval_during_redisplay = 0;
23292 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23293 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23294 inhibit_free_realized_faces = 0;
23296 #if GLYPH_DEBUG
23297 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23298 doc: /* Inhibit try_window_id display optimization. */);
23299 inhibit_try_window_id = 0;
23301 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23302 doc: /* Inhibit try_window_reusing display optimization. */);
23303 inhibit_try_window_reusing = 0;
23305 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23306 doc: /* Inhibit try_cursor_movement display optimization. */);
23307 inhibit_try_cursor_movement = 0;
23308 #endif /* GLYPH_DEBUG */
23312 /* Initialize this module when Emacs starts. */
23314 void
23315 init_xdisp ()
23317 Lisp_Object root_window;
23318 struct window *mini_w;
23320 current_header_line_height = current_mode_line_height = -1;
23322 CHARPOS (this_line_start_pos) = 0;
23324 mini_w = XWINDOW (minibuf_window);
23325 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23327 if (!noninteractive)
23329 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23330 int i;
23332 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23333 set_window_height (root_window,
23334 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23336 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23337 set_window_height (minibuf_window, 1, 0);
23339 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23340 mini_w->total_cols = make_number (FRAME_COLS (f));
23342 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23343 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23344 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23346 /* The default ellipsis glyphs `...'. */
23347 for (i = 0; i < 3; ++i)
23348 default_invis_vector[i] = make_number ('.');
23352 /* Allocate the buffer for frame titles.
23353 Also used for `format-mode-line'. */
23354 int size = 100;
23355 mode_line_noprop_buf = (char *) xmalloc (size);
23356 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23357 mode_line_noprop_ptr = mode_line_noprop_buf;
23358 mode_line_target = MODE_LINE_DISPLAY;
23361 help_echo_showing_p = 0;
23365 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23366 (do not change this comment) */